2014-08-07 12:00:06 +04:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
2014-10-20 09:09:02 +04:00
|
|
|
var (
|
2019-06-25 02:25:47 +03:00
|
|
|
ErrKeyMustBePEMEncoded = errors.New("Invalid Key: Key must be a PEM encoded PKCS1 or PKCS8 key")
|
2014-10-20 09:09:02 +04:00
|
|
|
ErrNotRSAPrivateKey = errors.New("Key is not a valid RSA private key")
|
2016-05-04 20:25:48 +03:00
|
|
|
ErrNotRSAPublicKey = errors.New("Key is not a valid RSA public key")
|
2014-10-20 09:09:02 +04:00
|
|
|
)
|
|
|
|
|
2014-08-07 12:00:06 +04:00
|
|
|
// Parse PEM encoded PKCS1 or PKCS8 private key
|
2014-08-27 01:24:20 +04:00
|
|
|
func ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
|
2014-08-07 12:00:06 +04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// Parse PEM block
|
|
|
|
var block *pem.Block
|
|
|
|
if block, _ = pem.Decode(key); block == nil {
|
2014-10-20 09:09:02 +04:00
|
|
|
return nil, ErrKeyMustBePEMEncoded
|
2014-08-07 12:00:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var parsedKey interface{}
|
|
|
|
if parsedKey, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
|
|
|
|
if parsedKey, err = x509.ParsePKCS8PrivateKey(block.Bytes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var pkey *rsa.PrivateKey
|
|
|
|
var ok bool
|
|
|
|
if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
|
2014-10-20 09:09:02 +04:00
|
|
|
return nil, ErrNotRSAPrivateKey
|
2014-08-07 12:00:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pkey, nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 20:49:46 +03:00
|
|
|
// Parse PEM encoded PKCS1 or PKCS8 private key protected with password
|
2021-07-31 15:36:53 +03:00
|
|
|
|
|
|
|
// Deprecated: This function is deprecated and should not be used anymore. It uses the deprecated x509.DecryptPEMBlock
|
|
|
|
// function, which was deprecated since RFC 1423 is regarded insecure by design. Unfortunately, there is no alternative
|
|
|
|
// in the Go standard library for now. See https://github.com/golang/go/issues/8860.
|
2016-11-03 20:49:46 +03:00
|
|
|
func ParseRSAPrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.PrivateKey, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Parse PEM block
|
|
|
|
var block *pem.Block
|
|
|
|
if block, _ = pem.Decode(key); block == nil {
|
|
|
|
return nil, ErrKeyMustBePEMEncoded
|
|
|
|
}
|
|
|
|
|
|
|
|
var parsedKey interface{}
|
|
|
|
|
|
|
|
var blockDecrypted []byte
|
|
|
|
if blockDecrypted, err = x509.DecryptPEMBlock(block, []byte(password)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedKey, err = x509.ParsePKCS1PrivateKey(blockDecrypted); err != nil {
|
|
|
|
if parsedKey, err = x509.ParsePKCS8PrivateKey(blockDecrypted); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var pkey *rsa.PrivateKey
|
|
|
|
var ok bool
|
|
|
|
if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
|
|
|
|
return nil, ErrNotRSAPrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkey, nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 12:00:06 +04:00
|
|
|
// Parse PEM encoded PKCS1 or PKCS8 public key
|
2014-08-27 01:24:20 +04:00
|
|
|
func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
|
2014-08-07 12:00:06 +04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// Parse PEM block
|
|
|
|
var block *pem.Block
|
|
|
|
if block, _ = pem.Decode(key); block == nil {
|
2014-10-20 09:09:02 +04:00
|
|
|
return nil, ErrKeyMustBePEMEncoded
|
2014-08-07 12:00:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the key
|
|
|
|
var parsedKey interface{}
|
|
|
|
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
|
|
|
|
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
|
|
|
|
parsedKey = cert.PublicKey
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var pkey *rsa.PublicKey
|
|
|
|
var ok bool
|
|
|
|
if pkey, ok = parsedKey.(*rsa.PublicKey); !ok {
|
2016-05-04 20:25:48 +03:00
|
|
|
return nil, ErrNotRSAPublicKey
|
2014-08-07 12:00:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pkey, nil
|
|
|
|
}
|