Skip to content

Commit

Permalink
Parsing PKCS1 Public Key
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Anderson committed Nov 6, 2021
1 parent 38ebe4b commit 6296976
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions rsa_utils.go
Expand Up @@ -75,7 +75,7 @@ func ParseRSAPrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.Pr
return pkey, nil
}

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

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

// Parse the key
var parsedKey interface{}
if parsedKey, err = x509.ParsePKCS1PublicKey(block.Bytes); err != nil {
if parsedKey, err = x509.ParsePKCS8PublicKey(block.Bytes); err != nil {
return nil, err
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
parsedKey = cert.PublicKey
} else {
if parsedKey, err = x509.ParsePKCS1PublicKey(block.Bytes); err == nil {
return nil, err
}
}
}

Expand Down

0 comments on commit 6296976

Please sign in to comment.