| 1 |
package pemutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/rand" |
| 8 |
"crypto/rsa" |
| 9 |
"crypto/sha256" |
| 10 |
"crypto/x509" |
| 11 |
"crypto/x509/pkix" |
| 12 |
"encoding/asn1" |
| 13 |
"encoding/pem" |
| 14 |
"io/ioutil" |
| 15 |
"math/big" |
| 16 |
"os" |
| 17 |
"reflect" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/pkg/errors" |
| 22 |
"github.com/smallstep/assert" |
| 23 |
"github.com/smallstep/cli/crypto/keys" |
| 24 |
) |
| 25 |
|
| 26 |
type keyType int |
| 27 |
|
| 28 |
const ( |
| 29 |
ecdsaPublicKey keyType = iota |
| 30 |
ecdsaPrivateKey |
| 31 |
ed25519PublicKey |
| 32 |
ed25519PrivateKey |
| 33 |
rsaPublicKey |
| 34 |
rsaPrivateKey |
| 35 |
) |
| 36 |
|
| 37 |
const ( |
| 38 |
testCRT = `-----BEGIN CERTIFICATE----- |
| 39 |
MIICLjCCAdSgAwIBAgIQBvswFbAODY9xtJ/myiuEHzAKBggqhkjOPQQDAjAkMSIw |
| 40 |
IAYDVQQDExlTbWFsbHN0ZXAgSW50ZXJtZWRpYXRlIENBMB4XDTE4MTEzMDE5NTkw |
| 41 |
OVoXDTE4MTIwMTE5NTkwOVowHjEcMBoGA1UEAxMTaGVsbG8uc21hbGxzdGVwLmNv |
| 42 |
bTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABIqPQy8roJTMWpEt8NNA1CnRm3l1 |
| 43 |
wdjH4OrVaH3l2Gp/UW737Wbn4sqSAFahmajuwkfRG5KMh2/+xnCkGuR2fayjge0w |
| 44 |
geowDgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcD |
| 45 |
AjAdBgNVHQ4EFgQU5bqyXvZaEmtZ3OpZapq7pBIkVvgwHwYDVR0jBBgwFoAUu97P |
| 46 |
aFQPfuyKOeew7Hg45WFIAVMwHgYDVR0RBBcwFYITaGVsbG8uc21hbGxzdGVwLmNv |
| 47 |
bTBZBgwrBgEEAYKkZMYoQAEESTBHAgEBBBVtYXJpYW5vQHNtYWxsc3RlcC5jb20E |
| 48 |
K2pPMzdkdERia3UtUW5hYnM1VlIwWXc2WUZGdjl3ZUExOGRwM2h0dmRFanMwCgYI |
| 49 |
KoZIzj0EAwIDSAAwRQIhALKeC2q0HWyHoZobZFK9HQynLbPOOtAK437RaetlX5ty |
| 50 |
AiBXQzvaLlDprQu+THj18aDYLnHA//5mdD3HPJV6KmgdDg== |
| 51 |
-----END CERTIFICATE-----` |
| 52 |
testCSR = `-----BEGIN CERTIFICATE REQUEST----- |
| 53 |
MIHYMIGAAgEAMB4xHDAaBgNVBAMTE2hlbGxvLnNtYWxsc3RlcC5jb20wWTATBgcq |
| 54 |
hkjOPQIBBggqhkjOPQMBBwNCAASKj0MvK6CUzFqRLfDTQNQp0Zt5dcHYx+Dq1Wh9 |
| 55 |
5dhqf1Fu9+1m5+LKkgBWoZmo7sJH0RuSjIdv/sZwpBrkdn2soAAwCgYIKoZIzj0E |
| 56 |
AwIDRwAwRAIgZgz9gdx9inOp6bSX4EkYiUCyLV9xGvabovu5C9UkRr8CIBGBbkp0 |
| 57 |
l4tesAKoXelsLygJjPuUGRLK+OtdjPBIN1Zo |
| 58 |
-----END CERTIFICATE REQUEST-----` |
| 59 |
testCSRKeytool = `-----BEGIN NEW CERTIFICATE REQUEST----- |
| 60 |
MIHYMIGAAgEAMB4xHDAaBgNVBAMTE2hlbGxvLnNtYWxsc3RlcC5jb20wWTATBgcq |
| 61 |
hkjOPQIBBggqhkjOPQMBBwNCAASKj0MvK6CUzFqRLfDTQNQp0Zt5dcHYx+Dq1Wh9 |
| 62 |
5dhqf1Fu9+1m5+LKkgBWoZmo7sJH0RuSjIdv/sZwpBrkdn2soAAwCgYIKoZIzj0E |
| 63 |
AwIDRwAwRAIgZgz9gdx9inOp6bSX4EkYiUCyLV9xGvabovu5C9UkRr8CIBGBbkp0 |
| 64 |
l4tesAKoXelsLygJjPuUGRLK+OtdjPBIN1Zo |
| 65 |
-----END NEW CERTIFICATE REQUEST-----` |
| 66 |
) |
| 67 |
|
| 68 |
type testdata struct { |
| 69 |
typ keyType |
| 70 |
encrypted bool |
| 71 |
} |
| 72 |
|
| 73 |
var files = map[string]testdata{ |
| 74 |
"testdata/openssl.p256.pem": {ecdsaPrivateKey, false}, |
| 75 |
"testdata/openssl.p256.pub.pem": {ecdsaPublicKey, false}, |
| 76 |
"testdata/openssl.p256.enc.pem": {ecdsaPrivateKey, true}, |
| 77 |
"testdata/openssl.p384.pem": {ecdsaPrivateKey, false}, |
| 78 |
"testdata/openssl.p384.pub.pem": {ecdsaPublicKey, false}, |
| 79 |
"testdata/openssl.p384.enc.pem": {ecdsaPrivateKey, true}, |
| 80 |
"testdata/openssl.p521.pem": {ecdsaPrivateKey, false}, |
| 81 |
"testdata/openssl.p521.pub.pem": {ecdsaPublicKey, false}, |
| 82 |
"testdata/openssl.p521.enc.pem": {ecdsaPrivateKey, true}, |
| 83 |
"testdata/openssl.rsa1024.pem": {rsaPrivateKey, false}, |
| 84 |
"testdata/openssl.rsa1024.pub.pem": {rsaPublicKey, false}, |
| 85 |
"testdata/openssl.rsa1024.enc.pem": {rsaPrivateKey, true}, |
| 86 |
"testdata/openssl.rsa2048.pem": {rsaPrivateKey, false}, |
| 87 |
"testdata/openssl.rsa2048.pub.pem": {rsaPublicKey, false}, |
| 88 |
"testdata/openssl.rsa2048.enc.pem": {rsaPrivateKey, true}, |
| 89 |
"testdata/openssh.ed25519.enc.pem": {ed25519PrivateKey, true}, |
| 90 |
"testdata/openssh.ed25519.pem": {ed25519PrivateKey, false}, |
| 91 |
"testdata/openssh.ed25519.pub.pem": {ed25519PublicKey, false}, |
| 92 |
"testdata/openssh.p256.enc.pem": {ecdsaPrivateKey, true}, |
| 93 |
"testdata/openssh.p256.pem": {ecdsaPrivateKey, false}, |
| 94 |
"testdata/openssh.p256.pub.pem": {ecdsaPublicKey, false}, |
| 95 |
"testdata/openssh.p384.enc.pem": {ecdsaPrivateKey, true}, |
| 96 |
"testdata/openssh.p384.pem": {ecdsaPrivateKey, false}, |
| 97 |
"testdata/openssh.p384.pub.pem": {ecdsaPublicKey, false}, |
| 98 |
"testdata/openssh.p521.enc.pem": {ecdsaPrivateKey, true}, |
| 99 |
"testdata/openssh.p521.pem": {ecdsaPrivateKey, false}, |
| 100 |
"testdata/openssh.p521.pub.pem": {ecdsaPublicKey, false}, |
| 101 |
"testdata/openssh.rsa1024.enc.pem": {rsaPrivateKey, true}, |
| 102 |
"testdata/openssh.rsa1024.pem": {rsaPrivateKey, false}, |
| 103 |
"testdata/openssh.rsa1024.pub.pem": {rsaPublicKey, false}, |
| 104 |
"testdata/openssh.rsa2048.enc.pem": {rsaPrivateKey, true}, |
| 105 |
"testdata/openssh.rsa2048.pem": {rsaPrivateKey, false}, |
| 106 |
"testdata/openssh.rsa2048.pub.pem": {rsaPublicKey, false}, |
| 107 |
"testdata/pkcs8/openssl.ed25519.pem": {ed25519PrivateKey, false}, |
| 108 |
"testdata/pkcs8/openssl.ed25519.pub.pem": {ed25519PublicKey, false}, |
| 109 |
"testdata/pkcs8/openssl.ed25519.enc.pem": {ed25519PrivateKey, true}, |
| 110 |
"testdata/pkcs8/openssl.p256.pem": {ecdsaPrivateKey, false}, |
| 111 |
"testdata/pkcs8/openssl.p256.pub.pem": {ecdsaPublicKey, false}, |
| 112 |
"testdata/pkcs8/openssl.p256.enc.pem": {ecdsaPrivateKey, true}, |
| 113 |
"testdata/pkcs8/openssl.p384.pem": {ecdsaPrivateKey, false}, |
| 114 |
"testdata/pkcs8/openssl.p384.pub.pem": {ecdsaPublicKey, false}, |
| 115 |
"testdata/pkcs8/openssl.p384.enc.pem": {ecdsaPrivateKey, true}, |
| 116 |
"testdata/pkcs8/openssl.p521.pem": {ecdsaPrivateKey, false}, |
| 117 |
"testdata/pkcs8/openssl.p521.pub.pem": {ecdsaPublicKey, false}, |
| 118 |
"testdata/pkcs8/openssl.p521.enc.pem": {ecdsaPrivateKey, true}, |
| 119 |
"testdata/pkcs8/openssl.rsa2048.pem": {rsaPrivateKey, false}, |
| 120 |
"testdata/pkcs8/openssl.rsa2048.pub.pem": {rsaPublicKey, false}, |
| 121 |
"testdata/pkcs8/openssl.rsa2048.enc.pem": {rsaPrivateKey, true}, |
| 122 |
"testdata/pkcs8/openssl.rsa4096.pem": {rsaPrivateKey, false}, |
| 123 |
"testdata/pkcs8/openssl.rsa4096.pub.pem": {rsaPublicKey, false}, |
| 124 |
} |
| 125 |
|
| 126 |
func readOrParseSSH(fn string) (interface{}, error) { |
| 127 |
if strings.HasPrefix(fn, "testdata/openssh") && strings.HasSuffix(fn, ".pub.pem") { |
| 128 |
b, err := ioutil.ReadFile(fn) |
| 129 |
if err != nil { |
| 130 |
return nil, err |
| 131 |
} |
| 132 |
return ParseSSH(b) |
| 133 |
} |
| 134 |
return Read(fn) |
| 135 |
} |
| 136 |
|
| 137 |
func TestRead(t *testing.T) { |
| 138 |
var err error |
| 139 |
var key interface{} |
| 140 |
|
| 141 |
for fn, td := range files { |
| 142 |
t.Run(fn, func(t *testing.T) { |
| 143 |
if td.encrypted { |
| 144 |
key, err = Read(fn, WithPassword([]byte("mypassword"))) |
| 145 |
} else { |
| 146 |
key, err = readOrParseSSH(fn) |
| 147 |
} |
| 148 |
|
| 149 |
assert.NotNil(t, key) |
| 150 |
assert.NoError(t, err) |
| 151 |
|
| 152 |
switch td.typ { |
| 153 |
case ecdsaPublicKey: |
| 154 |
assert.Type(t, &ecdsa.PublicKey{}, key) |
| 155 |
case ecdsaPrivateKey: |
| 156 |
assert.Type(t, &ecdsa.PrivateKey{}, key) |
| 157 |
case ed25519PublicKey: |
| 158 |
assert.Type(t, ed25519.PublicKey{}, key) |
| 159 |
case ed25519PrivateKey: |
| 160 |
assert.Type(t, ed25519.PrivateKey{}, key) |
| 161 |
case rsaPublicKey: |
| 162 |
assert.Type(t, &rsa.PublicKey{}, key) |
| 163 |
case rsaPrivateKey: |
| 164 |
assert.Type(t, &rsa.PrivateKey{}, key) |
| 165 |
default: |
| 166 |
t.Errorf("type %T not supported", key) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
if td.encrypted { |
| 171 |
k, err := Read(strings.Replace(fn, ".enc", "", 1)) |
| 172 |
assert.NoError(t, err) |
| 173 |
assert.Equals(t, k, key) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
switch td.typ { |
| 178 |
case ecdsaPrivateKey, ed25519PrivateKey, rsaPrivateKey: |
| 179 |
pub := strings.Replace(fn, ".enc", "", 1) |
| 180 |
pub = strings.Replace(pub, "pem", "pub.pem", 1) |
| 181 |
|
| 182 |
k, err := readOrParseSSH(pub) |
| 183 |
assert.NoError(t, err) |
| 184 |
|
| 185 |
if pk, ok := key.(crypto.Signer); ok { |
| 186 |
assert.Equals(t, k, pk.Public()) |
| 187 |
|
| 188 |
var signature, digest []byte |
| 189 |
message := []byte("message") |
| 190 |
if _, ok := pk.(ed25519.PrivateKey); ok { |
| 191 |
signature, err = pk.Sign(rand.Reader, message, crypto.Hash(0)) |
| 192 |
} else { |
| 193 |
sum := sha256.Sum256(message) |
| 194 |
digest = sum[:] |
| 195 |
signature, err = pk.Sign(rand.Reader, digest, crypto.SHA256) |
| 196 |
} |
| 197 |
assert.NoError(t, err) |
| 198 |
switch k := k.(type) { |
| 199 |
case *ecdsa.PublicKey: |
| 200 |
|
| 201 |
ecdsaSignature := struct { |
| 202 |
R, S *big.Int |
| 203 |
}{} |
| 204 |
asn1.Unmarshal(signature, &ecdsaSignature) |
| 205 |
verified := ecdsa.Verify(k, digest, ecdsaSignature.R, ecdsaSignature.S) |
| 206 |
assert.True(t, verified) |
| 207 |
case ed25519.PublicKey: |
| 208 |
verified := ed25519.Verify(k, []byte("message"), signature) |
| 209 |
assert.True(t, verified) |
| 210 |
case *rsa.PublicKey: |
| 211 |
err := rsa.VerifyPKCS1v15(k, crypto.SHA256, digest, signature) |
| 212 |
assert.NoError(t, err) |
| 213 |
} |
| 214 |
|
| 215 |
} else { |
| 216 |
t.Errorf("key for %s does not satisfies the crypto.Signer interface", fn) |
| 217 |
} |
| 218 |
} |
| 219 |
}) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func TestReadCertificate(t *testing.T) { |
| 224 |
tests := []struct { |
| 225 |
fn string |
| 226 |
err error |
| 227 |
}{ |
| 228 |
{"testdata/ca.crt", nil}, |
| 229 |
{"testdata/ca.der", nil}, |
| 230 |
{"testdata/notexists.crt", errors.New("open testdata/notexists.crt failed: no such file or directory")}, |
| 231 |
{"testdata/badca.crt", errors.New("error parsing testdata/badca.crt")}, |
| 232 |
{"testdata/badpem.crt", errors.New("error decoding testdata/badpem.crt: not a valid PEM encoded block")}, |
| •233 |
{"testdata/badder.crt", errors.New("error parsing testdata/badder.crt:")}, |
| 234 |
{"testdata/openssl.p256.pem", errors.New("error decoding PEM: file 'testdata/openssl.p256.pem' does not contain a certificate")}, |
| 235 |
} |
| 236 |
|
| 237 |
for _, tc := range tests { |
| 238 |
crt, err := ReadCertificate(tc.fn) |
| 239 |
if tc.err != nil { |
| 240 |
if assert.Error(t, err) { |
| 241 |
assert.HasPrefix(t, err.Error(), tc.err.Error()) |
| 242 |
} |
| 243 |
} else { |
| 244 |
assert.NoError(t, err) |
| 245 |
assert.Type(t, &x509.Certificate{}, crt) |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
func TestReadCertificateBundle(t *testing.T) { |
| 251 |
tests := []struct { |
| 252 |
fn string |
| 253 |
len int |
| 254 |
err error |
| 255 |
}{ |
| 256 |
{"testdata/ca.crt", 1, nil}, |
| 257 |
{"testdata/ca.der", 1, nil}, |
| 258 |
{"testdata/bundle.crt", 2, nil}, |
| 259 |
{"testdata/notexists.crt", 0, errors.New("open testdata/notexists.crt failed: no such file or directory")}, |
| 260 |
{"testdata/badca.crt", 0, errors.New("error parsing testdata/badca.crt")}, |
| 261 |
{"testdata/badpem.crt", 0, errors.New("error decoding PEM: file 'testdata/badpem.crt' contains unexpected data")}, |
| •262 |
{"testdata/badder.crt", 0, errors.New("error parsing testdata/badder.crt:")}, |
| 263 |
{"testdata/openssl.p256.pem", 0, errors.New("error decoding PEM: file 'testdata/openssl.p256.pem' is not a certificate bundle")}, |
| 264 |
} |
| 265 |
|
| 266 |
for _, tc := range tests { |
| 267 |
certs, err := ReadCertificateBundle(tc.fn) |
| 268 |
if tc.err != nil { |
| 269 |
if assert.Error(t, err, tc.fn) { |
| 270 |
assert.HasPrefix(t, err.Error(), tc.err.Error()) |
| 271 |
} |
| 272 |
} else { |
| 273 |
assert.NoError(t, err) |
| 274 |
assert.Len(t, tc.len, certs, tc.fn) |
| 275 |
for i := range certs { |
| 276 |
assert.Type(t, &x509.Certificate{}, certs[i]) |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func TestParsePEM(t *testing.T) { |
| 283 |
type ParseTest struct { |
| 284 |
in []byte |
| 285 |
opts []Options |
| 286 |
cmpType interface{} |
| 287 |
err error |
| 288 |
} |
| 289 |
tests := map[string]func(t *testing.T) *ParseTest{ |
| 290 |
"success-ecdsa-public-key": func(t *testing.T) *ParseTest { |
| 291 |
b, err := ioutil.ReadFile("testdata/openssl.p256.pub.pem") |
| 292 |
assert.FatalError(t, err) |
| 293 |
return &ParseTest{ |
| 294 |
in: b, |
| 295 |
opts: nil, |
| 296 |
cmpType: &ecdsa.PublicKey{}, |
| 297 |
} |
| 298 |
}, |
| 299 |
"success-rsa-public-key": func(t *testing.T) *ParseTest { |
| 300 |
b, err := ioutil.ReadFile("testdata/openssl.rsa1024.pub.pem") |
| 301 |
assert.FatalError(t, err) |
| 302 |
return &ParseTest{ |
| 303 |
in: b, |
| 304 |
opts: nil, |
| 305 |
cmpType: &rsa.PublicKey{}, |
| 306 |
} |
| 307 |
}, |
| 308 |
"success-rsa-private-key": func(t *testing.T) *ParseTest { |
| 309 |
b, err := ioutil.ReadFile("testdata/openssl.rsa1024.pem") |
| 310 |
assert.FatalError(t, err) |
| 311 |
return &ParseTest{ |
| 312 |
in: b, |
| 313 |
opts: nil, |
| 314 |
cmpType: &rsa.PrivateKey{}, |
| 315 |
} |
| 316 |
}, |
| 317 |
"success-ecdsa-private-key": func(t *testing.T) *ParseTest { |
| 318 |
b, err := ioutil.ReadFile("testdata/openssl.p256.pem") |
| 319 |
assert.FatalError(t, err) |
| 320 |
return &ParseTest{ |
| 321 |
in: b, |
| 322 |
opts: nil, |
| 323 |
cmpType: &ecdsa.PrivateKey{}, |
| 324 |
} |
| 325 |
}, |
| 326 |
"success-ed25519-private-key": func(t *testing.T) *ParseTest { |
| 327 |
b, err := ioutil.ReadFile("testdata/pkcs8/openssl.ed25519.pem") |
| 328 |
assert.FatalError(t, err) |
| 329 |
return &ParseTest{ |
| 330 |
in: b, |
| 331 |
opts: nil, |
| 332 |
cmpType: ed25519.PrivateKey{}, |
| 333 |
} |
| 334 |
}, |
| 335 |
"success-ed25519-enc-private-key": func(t *testing.T) *ParseTest { |
| 336 |
b, err := ioutil.ReadFile("testdata/pkcs8/openssl.ed25519.enc.pem") |
| 337 |
assert.FatalError(t, err) |
| 338 |
return &ParseTest{ |
| 339 |
in: b, |
| 340 |
opts: []Options{WithPassword([]byte("mypassword"))}, |
| 341 |
cmpType: ed25519.PrivateKey{}, |
| 342 |
} |
| 343 |
}, |
| 344 |
"success-x509-crt": func(t *testing.T) *ParseTest { |
| 345 |
b, err := ioutil.ReadFile("testdata/ca.crt") |
| 346 |
assert.FatalError(t, err) |
| 347 |
return &ParseTest{ |
| 348 |
in: b, |
| 349 |
opts: nil, |
| 350 |
cmpType: &x509.Certificate{}, |
| 351 |
} |
| 352 |
}, |
| 353 |
} |
| 354 |
for name, genTestCase := range tests { |
| 355 |
t.Run(name, func(t *testing.T) { |
| 356 |
tc := genTestCase(t) |
| 357 |
|
| 358 |
i, err := Parse(tc.in, tc.opts...) |
| 359 |
if err != nil { |
| 360 |
if assert.NotNil(t, tc.err) { |
| 361 |
assert.HasPrefix(t, err.Error(), tc.err.Error()) |
| 362 |
} |
| 363 |
} else { |
| 364 |
if assert.Nil(t, tc.err) { |
| 365 |
assert.Type(t, i, tc.cmpType) |
| 366 |
} |
| 367 |
} |
| 368 |
}) |
| 369 |
} |
| 370 |
|
| 371 |
} |
| 372 |
|
| 373 |
func TestSerialize(t *testing.T) { |
| 374 |
tests := map[string]struct { |
| 375 |
in func() (interface{}, error) |
| 376 |
pass string |
| 377 |
file string |
| 378 |
err error |
| 379 |
}{ |
| 380 |
"unrecognized key type": { |
| 381 |
in: func() (interface{}, error) { |
| 382 |
return "shake and bake", nil |
| 383 |
}, |
| 384 |
err: errors.New("cannot serialize type 'string', value 'shake and bake'"), |
| 385 |
}, |
| 386 |
"RSA Private Key success": { |
| 387 |
in: func() (interface{}, error) { |
| 388 |
return keys.GenerateKey("RSA", "", 1024) |
| 389 |
}, |
| 390 |
}, |
| 391 |
"RSA Public Key success": { |
| 392 |
in: func() (interface{}, error) { |
| 393 |
pub, _, err := keys.GenerateKeyPair("RSA", "", 1024) |
| 394 |
return pub, err |
| 395 |
}, |
| 396 |
}, |
| 397 |
"EC Private Key success": { |
| 398 |
in: func() (interface{}, error) { |
| 399 |
return keys.GenerateKey("EC", "P-256", 0) |
| 400 |
}, |
| 401 |
}, |
| 402 |
"EC Private Key success - encrypt input data": { |
| 403 |
in: func() (interface{}, error) { |
| 404 |
return keys.GenerateKey("EC", "P-256", 0) |
| 405 |
}, |
| 406 |
pass: "pass", |
| 407 |
}, |
| 408 |
"EC Public Key success": { |
| 409 |
in: func() (interface{}, error) { |
| 410 |
pub, _, err := keys.GenerateKeyPair("EC", "P-256", 0) |
| 411 |
return pub, err |
| 412 |
}, |
| 413 |
}, |
| 414 |
"OKP Private Key success": { |
| 415 |
in: func() (interface{}, error) { |
| 416 |
return keys.GenerateKey("OKP", "Ed25519", 0) |
| 417 |
}, |
| 418 |
}, |
| 419 |
"OKP Public Key success": { |
| 420 |
in: func() (interface{}, error) { |
| 421 |
pub, _, err := keys.GenerateKeyPair("OKP", "Ed25519", 0) |
| 422 |
return pub, err |
| 423 |
}, |
| 424 |
}, |
| 425 |
"propagate open key out file error": { |
| 426 |
in: func() (interface{}, error) { |
| 427 |
return keys.GenerateKey("EC", "P-256", 0) |
| 428 |
}, |
| 429 |
file: "./fakeDir/test.key", |
| 430 |
err: errors.New("open ./fakeDir/test.key failed: no such file or directory"), |
| 431 |
}, |
| 432 |
"ToFile Success (EC Private Key unencrypted)": { |
| 433 |
in: func() (interface{}, error) { |
| 434 |
return keys.GenerateKey("EC", "P-256", 0) |
| 435 |
}, |
| 436 |
file: "./test.key", |
| 437 |
}, |
| 438 |
"ToFile Success (EC Private Key encrypted)": { |
| 439 |
in: func() (interface{}, error) { |
| 440 |
return keys.GenerateKey("EC", "P-256", 0) |
| 441 |
}, |
| 442 |
pass: "pass", |
| 443 |
file: "./test.key", |
| 444 |
}, |
| 445 |
} |
| 446 |
|
| 447 |
for name, test := range tests { |
| 448 |
if _, err := os.Stat("./test.key"); err == nil { |
| 449 |
assert.FatalError(t, os.Remove("./test.key")) |
| 450 |
} |
| 451 |
t.Logf("Running test case: %s", name) |
| 452 |
|
| 453 |
in, err := test.in() |
| 454 |
assert.FatalError(t, err) |
| 455 |
|
| 456 |
var p *pem.Block |
| 457 |
if test.pass == "" && test.file == "" { |
| 458 |
p, err = Serialize(in) |
| 459 |
} else if test.pass != "" && test.file != "" { |
| 460 |
p, err = Serialize(in, WithPassword([]byte(test.pass)), ToFile(test.file, 0600)) |
| 461 |
} else if test.pass != "" { |
| 462 |
p, err = Serialize(in, WithPassword([]byte(test.pass))) |
| 463 |
} else { |
| 464 |
p, err = Serialize(in, ToFile(test.file, 0600)) |
| 465 |
} |
| 466 |
|
| 467 |
if err != nil { |
| 468 |
if assert.NotNil(t, test.err) { |
| 469 |
assert.HasPrefix(t, err.Error(), test.err.Error()) |
| 470 |
} |
| 471 |
} else { |
| 472 |
if assert.Nil(t, test.err) { |
| 473 |
switch k := in.(type) { |
| 474 |
case *rsa.PrivateKey: |
| 475 |
if test.pass == "" { |
| 476 |
|
| 477 |
assert.False(t, x509.IsEncryptedPEMBlock(p)) |
| 478 |
assert.Equals(t, p.Type, "RSA PRIVATE KEY") |
| 479 |
assert.Equals(t, p.Bytes, x509.MarshalPKCS1PrivateKey(k)) |
| 480 |
} else { |
| 481 |
|
| 482 |
assert.True(t, x509.IsEncryptedPEMBlock(p)) |
| 483 |
assert.Equals(t, p.Type, "RSA PRIVATE KEY") |
| 484 |
assert.Equals(t, p.Headers["Proc-Type"], "4,ENCRYPTED") |
| 485 |
|
| 486 |
var der []byte |
| 487 |
|
| 488 |
der, err = x509.DecryptPEMBlock(p, []byte(test.pass)) |
| 489 |
assert.FatalError(t, err) |
| 490 |
assert.Equals(t, der, x509.MarshalPKCS1PrivateKey(k)) |
| 491 |
} |
| 492 |
case *rsa.PublicKey, *ecdsa.PublicKey: |
| 493 |
|
| 494 |
assert.False(t, x509.IsEncryptedPEMBlock(p)) |
| 495 |
assert.Equals(t, p.Type, "PUBLIC KEY") |
| 496 |
|
| 497 |
var b []byte |
| 498 |
b, err = x509.MarshalPKIXPublicKey(k) |
| 499 |
assert.FatalError(t, err) |
| 500 |
assert.Equals(t, p.Bytes, b) |
| 501 |
case *ecdsa.PrivateKey: |
| 502 |
assert.Equals(t, p.Type, "EC PRIVATE KEY") |
| 503 |
var actualBytes []byte |
| 504 |
if test.pass == "" { |
| 505 |
|
| 506 |
assert.False(t, x509.IsEncryptedPEMBlock(p)) |
| 507 |
actualBytes = p.Bytes |
| 508 |
} else { |
| 509 |
|
| 510 |
assert.True(t, x509.IsEncryptedPEMBlock(p)) |
| 511 |
assert.Equals(t, p.Headers["Proc-Type"], "4,ENCRYPTED") |
| 512 |
|
| 513 |
|
| 514 |
actualBytes, err = x509.DecryptPEMBlock(p, []byte(test.pass)) |
| 515 |
assert.FatalError(t, err) |
| 516 |
} |
| 517 |
var expectedBytes []byte |
| 518 |
expectedBytes, err = x509.MarshalECPrivateKey(k) |
| 519 |
assert.FatalError(t, err) |
| 520 |
assert.Equals(t, actualBytes, expectedBytes) |
| 521 |
|
| 522 |
if test.file != "" { |
| 523 |
|
| 524 |
var fileInfo os.FileInfo |
| 525 |
fileInfo, err = os.Stat(test.file) |
| 526 |
assert.FatalError(t, err) |
| 527 |
assert.Equals(t, fileInfo.Mode(), os.FileMode(0600)) |
| 528 |
|
| 529 |
var keyFileBytes []byte |
| 530 |
keyFileBytes, err = ioutil.ReadFile(test.file) |
| 531 |
assert.FatalError(t, err) |
| 532 |
pemKey, _ := pem.Decode(keyFileBytes) |
| 533 |
assert.Equals(t, pemKey.Type, "EC PRIVATE KEY") |
| 534 |
|
| 535 |
if x509.IsEncryptedPEMBlock(pemKey) { |
| 536 |
assert.Equals(t, pemKey.Headers["Proc-Type"], "4,ENCRYPTED") |
| 537 |
|
| 538 |
actualBytes, err = x509.DecryptPEMBlock(pemKey, []byte(test.pass)) |
| 539 |
assert.FatalError(t, err) |
| 540 |
} else { |
| 541 |
actualBytes = pemKey.Bytes |
| 542 |
} |
| 543 |
assert.Equals(t, actualBytes, expectedBytes) |
| 544 |
} |
| 545 |
case ed25519.PrivateKey: |
| 546 |
assert.Equals(t, p.Type, "PRIVATE KEY") |
| 547 |
var actualBytes []byte |
| 548 |
if test.pass == "" { |
| 549 |
|
| 550 |
assert.False(t, x509.IsEncryptedPEMBlock(p)) |
| 551 |
actualBytes = p.Bytes |
| 552 |
} else { |
| 553 |
|
| 554 |
assert.True(t, x509.IsEncryptedPEMBlock(p)) |
| 555 |
assert.Equals(t, p.Headers["Proc-Type"], "4,ENCRYPTED") |
| 556 |
|
| 557 |
|
| 558 |
actualBytes, err = x509.DecryptPEMBlock(p, []byte(test.pass)) |
| 559 |
assert.FatalError(t, err) |
| 560 |
} |
| 561 |
|
| 562 |
var priv pkcs8 |
| 563 |
_, err = asn1.Unmarshal(actualBytes, &priv) |
| 564 |
assert.FatalError(t, err) |
| 565 |
assert.Equals(t, priv.Version, 0) |
| 566 |
assert.Equals(t, priv.Algo, pkix.AlgorithmIdentifier{ |
| 567 |
Algorithm: asn1.ObjectIdentifier{1, 3, 101, 112}, |
| 568 |
Parameters: asn1.RawValue{}, |
| 569 |
}) |
| 570 |
assert.Equals(t, priv.PrivateKey[:2], []byte{4, 32}) |
| 571 |
assert.Equals(t, priv.PrivateKey[2:ed25519.SeedSize+2], k.Seed()) |
| 572 |
case ed25519.PublicKey: |
| 573 |
assert.Equals(t, p.Type, "PUBLIC KEY") |
| 574 |
|
| 575 |
assert.False(t, x509.IsEncryptedPEMBlock(p)) |
| 576 |
|
| 577 |
var pub publicKeyInfo |
| 578 |
_, err = asn1.Unmarshal(p.Bytes, &pub) |
| 579 |
assert.FatalError(t, err) |
| 580 |
assert.Equals(t, pub.Algo, pkix.AlgorithmIdentifier{ |
| 581 |
Algorithm: asn1.ObjectIdentifier{1, 3, 101, 112}, |
| 582 |
Parameters: asn1.RawValue{}, |
| 583 |
}) |
| 584 |
assert.Equals(t, pub.PublicKey, asn1.BitString{ |
| 585 |
Bytes: k, |
| 586 |
BitLength: ed25519.PublicKeySize * 8, |
| 587 |
}) |
| 588 |
default: |
| 589 |
t.Errorf("Unrecognized key - type: %T, value: %v", k, k) |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
if _, err := os.Stat("./test.key"); err == nil { |
| 594 |
assert.FatalError(t, os.Remove("./test.key")) |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
func TestParseDER(t *testing.T) { |
| 600 |
k1, err := Read("testdata/openssl.rsa2048.pem") |
| 601 |
assert.FatalError(t, err) |
| 602 |
k2, err := Read("testdata/openssl.p256.pem") |
| 603 |
assert.FatalError(t, err) |
| 604 |
k3, err := Read("testdata/pkcs8/openssl.ed25519.pem") |
| 605 |
assert.FatalError(t, err) |
| 606 |
rsaKey := k1.(*rsa.PrivateKey) |
| 607 |
ecdsaKey := k2.(*ecdsa.PrivateKey) |
| 608 |
edKey := k3.(ed25519.PrivateKey) |
| 609 |
|
| 610 |
edPubDer, err := ioutil.ReadFile("testdata/pkcs8/openssl.ed25519.pub.der") |
| 611 |
assert.FatalError(t, err) |
| 612 |
edPrivDer, err := ioutil.ReadFile("testdata/pkcs8/openssl.ed25519.der") |
| 613 |
assert.FatalError(t, err) |
| 614 |
|
| 615 |
toDER := func(k interface{}) []byte { |
| 616 |
switch k := k.(type) { |
| 617 |
case *rsa.PublicKey, *ecdsa.PublicKey: |
| 618 |
b, err := x509.MarshalPKIXPublicKey(k) |
| 619 |
assert.FatalError(t, err) |
| 620 |
return b |
| 621 |
case *rsa.PrivateKey: |
| 622 |
return x509.MarshalPKCS1PrivateKey(k) |
| 623 |
case *ecdsa.PrivateKey: |
| 624 |
b, err := x509.MarshalECPrivateKey(k) |
| 625 |
assert.FatalError(t, err) |
| 626 |
return b |
| 627 |
default: |
| 628 |
t.Fatalf("unsupported key type %T", k) |
| 629 |
return nil |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
type args struct { |
| 634 |
b []byte |
| 635 |
} |
| 636 |
tests := []struct { |
| 637 |
name string |
| 638 |
args args |
| 639 |
want interface{} |
| 640 |
wantErr bool |
| 641 |
}{ |
| 642 |
{"rsa public key", args{toDER(rsaKey.Public())}, rsaKey.Public(), false}, |
| 643 |
{"rsa private key", args{toDER(rsaKey)}, rsaKey, false}, |
| 644 |
{"rsa pkcs#1 public key", args{x509.MarshalPKCS1PublicKey(&rsaKey.PublicKey)}, rsaKey.Public(), false}, |
| 645 |
{"ecdsa public key", args{toDER(ecdsaKey.Public())}, ecdsaKey.Public(), false}, |
| 646 |
{"ecdsa private key", args{toDER(ecdsaKey)}, ecdsaKey, false}, |
| 647 |
{"ed25519 public key", args{edPubDer}, edKey.Public(), false}, |
| 648 |
{"ed25519 private key", args{edPrivDer}, edKey, false}, |
| 649 |
{"fail", args{[]byte("fooo")}, nil, true}, |
| 650 |
} |
| 651 |
for _, tt := range tests { |
| 652 |
t.Run(tt.name, func(t *testing.T) { |
| 653 |
got, err := ParseDER(tt.args.b) |
| 654 |
if (err != nil) != tt.wantErr { |
| 655 |
t.Errorf("ParseDER() error = %v, wantErr %v", err, tt.wantErr) |
| 656 |
return |
| 657 |
} |
| 658 |
if !reflect.DeepEqual(got, tt.want) { |
| 659 |
t.Errorf("ParseDER() = %v, want %v", got, tt.want) |
| 660 |
} |
| 661 |
}) |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
func TestParseKey(t *testing.T) { |
| 666 |
var key interface{} |
| 667 |
for fn, td := range files { |
| 668 |
|
| 669 |
if strings.HasPrefix(fn, "testdata/openssh") && strings.HasSuffix(fn, ".pub.pem") { |
| 670 |
continue |
| 671 |
} |
| 672 |
t.Run(fn, func(t *testing.T) { |
| 673 |
data, err := ioutil.ReadFile(fn) |
| 674 |
assert.FatalError(t, err) |
| 675 |
if td.encrypted { |
| 676 |
key, err = ParseKey(data, WithPassword([]byte("mypassword"))) |
| 677 |
} else { |
| 678 |
key, err = ParseKey(data) |
| 679 |
} |
| 680 |
assert.NotNil(t, key) |
| 681 |
assert.NoError(t, err) |
| 682 |
|
| 683 |
switch td.typ { |
| 684 |
case ecdsaPublicKey: |
| 685 |
assert.Type(t, &ecdsa.PublicKey{}, key) |
| 686 |
case ecdsaPrivateKey: |
| 687 |
assert.Type(t, &ecdsa.PrivateKey{}, key) |
| 688 |
case ed25519PublicKey: |
| 689 |
assert.Type(t, ed25519.PublicKey{}, key) |
| 690 |
case ed25519PrivateKey: |
| 691 |
assert.Type(t, ed25519.PrivateKey{}, key) |
| 692 |
case rsaPublicKey: |
| 693 |
assert.Type(t, &rsa.PublicKey{}, key) |
| 694 |
case rsaPrivateKey: |
| 695 |
assert.Type(t, &rsa.PrivateKey{}, key) |
| 696 |
default: |
| 697 |
t.Errorf("type %T not supported", key) |
| 698 |
} |
| 699 |
}) |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
func TestParseKey_x509(t *testing.T) { |
| 704 |
b, _ := pem.Decode([]byte(testCRT)) |
| 705 |
cert, err := x509.ParseCertificate(b.Bytes) |
| 706 |
assert.FatalError(t, err) |
| 707 |
key, err := ParseKey([]byte(testCRT)) |
| 708 |
assert.FatalError(t, err) |
| 709 |
assert.Equals(t, cert.PublicKey, key) |
| 710 |
|
| 711 |
b, _ = pem.Decode([]byte(testCSR)) |
| 712 |
csr, err := x509.ParseCertificateRequest(b.Bytes) |
| 713 |
assert.FatalError(t, err) |
| 714 |
key, err = ParseKey([]byte(testCSR)) |
| 715 |
assert.FatalError(t, err) |
| 716 |
assert.Equals(t, csr.PublicKey, key) |
| 717 |
|
| 718 |
b, _ = pem.Decode([]byte(testCSRKeytool)) |
| 719 |
csr, err = x509.ParseCertificateRequest(b.Bytes) |
| 720 |
assert.FatalError(t, err) |
| 721 |
key, err = ParseKey([]byte(testCSRKeytool)) |
| 722 |
assert.FatalError(t, err) |
| 723 |
assert.Equals(t, csr.PublicKey, key) |
| 724 |
} |
| 725 |
|
| 726 |
func TestParseSSH(t *testing.T) { |
| 727 |
var key interface{} |
| 728 |
for fn, td := range files { |
| 729 |
if !strings.HasPrefix(fn, "testdata/openssh") || !strings.HasSuffix(fn, ".pub.pem") { |
| 730 |
continue |
| 731 |
} |
| 732 |
t.Run(fn, func(t *testing.T) { |
| 733 |
data, err := ioutil.ReadFile(fn) |
| 734 |
assert.FatalError(t, err) |
| 735 |
key, err = ParseSSH(data) |
| 736 |
assert.FatalError(t, err) |
| 737 |
assert.NotNil(t, key) |
| 738 |
|
| 739 |
switch td.typ { |
| 740 |
case ecdsaPublicKey: |
| 741 |
assert.Type(t, &ecdsa.PublicKey{}, key) |
| 742 |
case ed25519PublicKey: |
| 743 |
assert.Type(t, ed25519.PublicKey{}, key) |
| 744 |
case rsaPublicKey: |
| 745 |
assert.Type(t, &rsa.PublicKey{}, key) |
| 746 |
default: |
| 747 |
t.Errorf("type %T not supported", key) |
| 748 |
} |
| 749 |
}) |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
func TestOpenSSH(t *testing.T) { |
| 754 |
for fn, td := range files { |
| 755 |
if strings.HasSuffix(fn, ".pub.pem") { |
| 756 |
continue |
| 757 |
} |
| 758 |
t.Run(fn, func(t *testing.T) { |
| 759 |
opts := []Options{ |
| 760 |
WithOpenSSH(true), |
| 761 |
WithComment("[email protected]"), |
| 762 |
} |
| 763 |
if td.encrypted { |
| 764 |
opts = append(opts, WithPassword([]byte("mypassword"))) |
| 765 |
} |
| 766 |
|
| 767 |
key, err := Read(fn, opts...) |
| 768 |
assert.FatalError(t, err) |
| 769 |
|
| 770 |
|
| 771 |
block, err := SerializeOpenSSHPrivateKey(key, opts...) |
| 772 |
assert.FatalError(t, err) |
| 773 |
|
| 774 |
key2, err := ParseOpenSSHPrivateKey(block.Bytes, opts...) |
| 775 |
assert.FatalError(t, err) |
| 776 |
|
| 777 |
assert.Equals(t, key, key2) |
| 778 |
|
| 779 |
|
| 780 |
block2, err := Serialize(key2, opts...) |
| 781 |
assert.FatalError(t, err) |
| 782 |
|
| 783 |
assert.NotEquals(t, block, block2) |
| 784 |
|
| 785 |
key3, err := Parse(pem.EncodeToMemory(block2), opts...) |
| 786 |
assert.FatalError(t, err) |
| 787 |
assert.Equals(t, key2, key3) |
| 788 |
}) |
| 789 |
} |
| 790 |
} |