Skip to content

Commit 87a2fe7

Browse files
darakiancbroglie
authored andcommitted
Ignore EC parameters when parsing private keys
1 parent 6449172 commit 87a2fe7

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

helpers/helpers.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"io/ioutil"
1919
"os"
2020

21-
"github.com/google/certificate-transparency-go"
21+
ct "github.com/google/certificate-transparency-go"
2222
cttls "github.com/google/certificate-transparency-go/tls"
2323
ctx509 "github.com/google/certificate-transparency-go/x509"
2424
"golang.org/x/crypto/ocsp"
@@ -378,7 +378,15 @@ func ParsePrivateKeyPEMWithPassword(keyPEM []byte, password []byte) (key crypto.
378378

379379
// GetKeyDERFromPEM parses a PEM-encoded private key and returns DER-format key bytes.
380380
func GetKeyDERFromPEM(in []byte, password []byte) ([]byte, error) {
381-
keyDER, _ := pem.Decode(in)
381+
// Ignore any EC PARAMETERS blocks when looking for a key (openssl includes
382+
// them by default).
383+
var keyDER *pem.Block
384+
for {
385+
keyDER, in = pem.Decode(in)
386+
if keyDER == nil || keyDER.Type != "EC PARAMETERS" {
387+
break
388+
}
389+
}
382390
if keyDER != nil {
383391
if procType, ok := keyDER.Headers["Proc-Type"]; ok {
384392
if strings.Contains(procType, "ENCRYPTED") {

helpers/helpers_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
testPrivateRSAKey = "testdata/priv_rsa_key.pem"
3333
testPrivateECDSAKey = "testdata/private_ecdsa_key.pem"
3434
testPrivateEd25519Key = "testdata/private_ed25519_key.pem"
35+
testPrivateOpenSSLECKey = "testdata/openssl_secp384.pem"
3536
testUnsupportedECDSAKey = "testdata/secp256k1-key.pem"
3637
testMessedUpPrivateKey = "testdata/messed_up_priv_key.pem"
3738
testEncryptedPrivateKey = "testdata/enc_priv_key.pem"
@@ -374,11 +375,22 @@ func TestParsePrivateKeyPEM(t *testing.T) {
374375
if err != nil {
375376
t.Fatal(err)
376377
}
378+
377379
_, err = ParsePrivateKeyPEM(testEd25519PEM)
378380
if err != nil {
379381
t.Fatal(err)
380382
}
381383

384+
testOpenSSLECKey, err := ioutil.ReadFile(testPrivateOpenSSLECKey)
385+
if err != nil {
386+
t.Fatal(err)
387+
}
388+
389+
_, err = ParsePrivateKeyPEM(testOpenSSLECKey)
390+
if err != nil {
391+
t.Fatal(err)
392+
}
393+
382394
// error cases
383395
errCases := []string{
384396
testMessedUpPrivateKey, // a few lines deleted
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN EC PARAMETERS-----
2+
BgUrgQQAIg==
3+
-----END EC PARAMETERS-----
4+
-----BEGIN EC PRIVATE KEY-----
5+
MIGkAgEBBDCn5safCQ6/JAUEbf1/BvOBvP9XHfcsEvQooEd0g0v4akMNmH53nXKQ
6+
qvsZBUP14X6gBwYFK4EEACKhZANiAAR1q1+sGy8Pmgdco9LEB10gJkIO0lBid8aK
7+
0xmtEL7U1RTQnNyraswwI0hxHwzwSHHKojD8Msdy5uOngxKnGrUBTuMubezfGbWz
8+
ULOFvrTemUIlNmSsWMcrzEBEnZxvOqY=
9+
-----END EC PRIVATE KEY-----

0 commit comments

Comments
 (0)