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