Skip to content

Commit

Permalink
[feature] Support PKCS1 "RSA PUBLIC KEY" pem block type (#1179)
Browse files Browse the repository at this point in the history
* ap: add support for PKCS1 "RSA PUBLIC KEY" pem block type

Signed-off-by: Sigrid Solveig Haflínudóttir <[email protected]>

* ap: report no PEM data or unknown pem block type

Signed-off-by: Sigrid Solveig Haflínudóttir <[email protected]>

Signed-off-by: Sigrid Solveig Haflínudóttir <[email protected]>
  • Loading branch information
ftrvxmtrx authored Nov 30, 2022
1 parent 1652633 commit 5a0e418
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/ap/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package ap

import (
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
Expand Down Expand Up @@ -318,18 +319,24 @@ func ExtractPublicKeyForOwner(i WithPublicKey, forOwner *url.URL) (*rsa.PublicKe
}

block, _ := pem.Decode([]byte(pkeyPem))
if block == nil || block.Type != "PUBLIC KEY" {
return nil, nil, errors.New("could not decode publicKeyPem to PUBLIC KEY pem block type")
if block == nil {
return nil, nil, errors.New("could not decode publicKeyPem: no PEM data")
}
var p crypto.PublicKey
switch block.Type {
case "PUBLIC KEY":
p, err = x509.ParsePKIXPublicKey(block.Bytes)
case "RSA PUBLIC KEY":
p, err = x509.ParsePKCS1PublicKey(block.Bytes)
default:
return nil, nil, fmt.Errorf("could not parse public key: unknown block type: %q", block.Type)
}

p, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, nil, fmt.Errorf("could not parse public key from block bytes: %s", err)
}
if p == nil {
return nil, nil, errors.New("returned public key was empty")
}

if publicKey, ok := p.(*rsa.PublicKey); ok {
return publicKey, pkeyID, nil
}
Expand Down

0 comments on commit 5a0e418

Please sign in to comment.