mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-12-01 16:12:45 +00:00
[feature] Support PKCS1 "RSA PUBLIC KEY" pem block type (#1179)
* ap: add support for PKCS1 "RSA PUBLIC KEY" pem block type Signed-off-by: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se> * ap: report no PEM data or unknown pem block type Signed-off-by: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se> Signed-off-by: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
This commit is contained in:
parent
1652633d93
commit
5a0e418281
|
@ -22,6 +22,7 @@
|
||||||
package ap
|
package ap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
|
@ -318,18 +319,24 @@ func ExtractPublicKeyForOwner(i WithPublicKey, forOwner *url.URL) (*rsa.PublicKe
|
||||||
}
|
}
|
||||||
|
|
||||||
block, _ := pem.Decode([]byte(pkeyPem))
|
block, _ := pem.Decode([]byte(pkeyPem))
|
||||||
if block == nil || block.Type != "PUBLIC KEY" {
|
if block == nil {
|
||||||
return nil, nil, errors.New("could not decode publicKeyPem to PUBLIC KEY pem block type")
|
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 {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("could not parse public key from block bytes: %s", err)
|
return nil, nil, fmt.Errorf("could not parse public key from block bytes: %s", err)
|
||||||
}
|
}
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return nil, nil, errors.New("returned public key was empty")
|
return nil, nil, errors.New("returned public key was empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if publicKey, ok := p.(*rsa.PublicKey); ok {
|
if publicKey, ok := p.(*rsa.PublicKey); ok {
|
||||||
return publicKey, pkeyID, nil
|
return publicKey, pkeyID, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue