Skip to content

Commit 6296976

Browse files
author
Tom Anderson
committed
Parsing PKCS1 Public Key
1 parent 38ebe4b commit 6296976

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

rsa_utils.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func ParseRSAPrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.Pr
7575
return pkey, nil
7676
}
7777

78-
// ParseRSAPublicKeyFromPEM parses a PEM encoded PKCS1 or PKCS8 public key
78+
// ParseRSAPublicKeyFromPEM parses a certificate or a PEM encoded PKCS1 or PKIX public key
7979
func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
8080
var err error
8181

@@ -87,9 +87,13 @@ func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
8787

8888
// Parse the key
8989
var parsedKey interface{}
90-
if parsedKey, err = x509.ParsePKCS1PublicKey(block.Bytes); err != nil {
91-
if parsedKey, err = x509.ParsePKCS8PublicKey(block.Bytes); err != nil {
92-
return nil, err
90+
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
91+
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
92+
parsedKey = cert.PublicKey
93+
} else {
94+
if parsedKey, err = x509.ParsePKCS1PublicKey(block.Bytes); err == nil {
95+
return nil, err
96+
}
9397
}
9498
}
9599

0 commit comments

Comments
 (0)