Add support for parsing public keys from certificates
This commit is contained in:
parent
5df108772b
commit
02de513a2b
1 changed files with 22 additions and 1 deletions
|
|
@ -147,10 +147,31 @@ func parsePrivateKey(privateKeyStr string) (*rsa.PrivateKey, error) {
|
|||
func parsePublicKey(publicKeyStr string) (*rsa.PublicKey, error) {
|
||||
|
||||
publicKeyStr = strings.TrimSpace(publicKeyStr)
|
||||
if !strings.HasPrefix(publicKeyStr, "-----BEGIN RSA PUBLIC KEY-----") {
|
||||
if !strings.HasPrefix(publicKeyStr, "-----BEGIN RSA PUBLIC KEY-----") && !strings.HasPrefix(publicKeyStr, "-----BEGIN CERTIFICATE-----") {
|
||||
publicKeyStr = fmt.Sprintf("-----BEGIN RSA PUBLIC KEY-----\n%s\n-----END RSA PUBLIC KEY-----\n", publicKeyStr)
|
||||
}
|
||||
|
||||
// if it is a certificate, get the public key from the certificate
|
||||
if strings.HasPrefix(publicKeyStr, "-----BEGIN CERTIFICATE-----") {
|
||||
|
||||
block, _ := pem.Decode([]byte(publicKeyStr))
|
||||
if block == nil {
|
||||
return nil, fmt.Errorf("cannot decode PEM block")
|
||||
}
|
||||
|
||||
cert, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pub, ok := cert.PublicKey.(*rsa.PublicKey)
|
||||
if !ok {
|
||||
return nil, errors.New("public key error")
|
||||
}
|
||||
|
||||
return pub, nil
|
||||
}
|
||||
|
||||
block, _ := pem.Decode([]byte(publicKeyStr))
|
||||
if block == nil {
|
||||
return nil, fmt.Errorf("cannot decode PEM block")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue