@@ -17,6 +17,7 @@ package pki
17
17
18
18
import (
19
19
"bytes"
20
+ "errors"
20
21
"os"
21
22
"os/exec"
22
23
"path/filepath"
@@ -117,8 +118,8 @@ func TestPrivateKeyTextMarshaling(t *testing.T) {
117
118
key := root .PrivateKey
118
119
txt , err := key .MarshalText ()
119
120
assert .NilError (t , err )
120
- assert .Assert (t , bytes .HasPrefix (txt , []byte ("-----BEGIN EC PRIVATE KEY-----\n " )), "got %q" , txt )
121
- assert .Assert (t , bytes .HasSuffix (txt , []byte ("\n -----END EC PRIVATE KEY-----\n " )), "got %q" , txt )
121
+ assert .Assert (t , bytes .HasPrefix (txt , []byte ("-----BEGIN PRIVATE KEY-----\n " )), "got %q" , txt )
122
+ assert .Assert (t , bytes .HasSuffix (txt , []byte ("\n -----END PRIVATE KEY-----\n " )), "got %q" , txt )
122
123
123
124
t .Run ("RoundTrip" , func (t * testing.T ) {
124
125
var sink PrivateKey
@@ -139,18 +140,67 @@ func TestPrivateKeyTextMarshaling(t *testing.T) {
139
140
assert .DeepEqual (t , key , sink )
140
141
})
141
142
142
- t .Run ("EncodedEmpty" , func (t * testing.T ) {
143
- txt := []byte ("-----BEGIN EC PRIVATE KEY-----\n \n -----END EC PRIVATE KEY-----\n " )
143
+ t .Run ("UnmarshalEllipticCurveSEC1" , func (t * testing.T ) {
144
+ t .Run ("EncodedEmpty" , func (t * testing.T ) {
145
+ txt := []byte ("-----BEGIN EC PRIVATE KEY-----\n \n -----END EC PRIVATE KEY-----\n " )
144
146
145
- var sink PrivateKey
146
- assert .ErrorContains (t , sink .UnmarshalText (txt ), "asn1" )
147
- })
147
+ var sink PrivateKey
148
+ assert .ErrorContains (t , sink .UnmarshalText (txt ), "asn1" )
149
+ })
148
150
149
- t .Run ("EncodedGarbage" , func (t * testing.T ) {
150
- txt := []byte ("-----BEGIN EC PRIVATE KEY-----\n asdfasdf\n -----END EC PRIVATE KEY-----\n " )
151
+ t .Run ("EncodedGarbage" , func (t * testing.T ) {
152
+ txt := []byte ("-----BEGIN EC PRIVATE KEY-----\n asdfasdf\n -----END EC PRIVATE KEY-----\n " )
151
153
152
- var sink PrivateKey
153
- assert .ErrorContains (t , sink .UnmarshalText (txt ), "asn1" )
154
+ var sink PrivateKey
155
+ assert .ErrorContains (t , sink .UnmarshalText (txt ), "asn1" )
156
+ })
157
+
158
+ t .Run ("GeneratedByOpenSSL" , func (t * testing.T ) {
159
+ openssl := require .OpenSSL (t )
160
+
161
+ // The "openssl ecparam" command generates elliptic curve keys.
162
+ cmd := exec .Command (openssl , "ecparam" ,
163
+ "-genkey" , "-name" , "prime256v1" , "-outform" , "PEM" , "-noout" , "-text" )
164
+
165
+ output , err := cmd .CombinedOutput ()
166
+ assert .NilError (t , err , "%q\n %s" , cmd .Args , output )
167
+
168
+ var sink PrivateKey
169
+ assert .NilError (t , sink .UnmarshalText (output ))
170
+ })
171
+ })
172
+
173
+ t .Run ("UnmarshalPKCS8" , func (t * testing.T ) {
174
+ t .Run ("EncodedEmpty" , func (t * testing.T ) {
175
+ txt := []byte ("-----BEGIN PRIVATE KEY-----\n \n -----END PRIVATE KEY-----\n " )
176
+
177
+ var sink PrivateKey
178
+ assert .ErrorContains (t , sink .UnmarshalText (txt ), "asn1" )
179
+ })
180
+
181
+ t .Run ("EncodedGarbage" , func (t * testing.T ) {
182
+ txt := []byte ("-----BEGIN PRIVATE KEY-----\n asdfasdf\n -----END PRIVATE KEY-----\n " )
183
+
184
+ var sink PrivateKey
185
+ assert .ErrorContains (t , sink .UnmarshalText (txt ), "asn1" )
186
+ })
187
+
188
+ t .Run ("WrongAlgorithm" , func (t * testing.T ) {
189
+ openssl := require .OpenSSL (t )
190
+ rsa , err := exec .Command ("sh" , "-ceu" ,
191
+ `"$1" genrsa | "$1" pkcs8 -topk8 -nocrypt` ,
192
+ "--" , openssl ,
193
+ ).Output ()
194
+
195
+ if exit := (* exec .ExitError )(nil ); errors .As (err , & exit ) {
196
+ assert .NilError (t , err , "\n %s" , exit .Stderr )
197
+ } else {
198
+ assert .NilError (t , err )
199
+ }
200
+
201
+ var sink PrivateKey
202
+ assert .ErrorContains (t , sink .UnmarshalText (rsa ), "algorithm: *rsa" )
203
+ })
154
204
})
155
205
156
206
t .Run ("ReadByOpenSSL" , func (t * testing.T ) {
0 commit comments