forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/golang-jwt/jwt/rsa_utils.go

157 lines
2.4 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
)
var (
ErrKeyMustBePEMEncoded = errors.New("Invalid Key: Key must be a PEM encoded PKCS1 or PKCS8 key")
ErrNotRSAPrivateKey = errors.New("Key is not a valid RSA private key")
ErrNotRSAPublicKey = errors.New("Key is not a valid RSA public key")
2024-02-18 10:42:21 +00:00
)
// Parse PEM encoded PKCS1 or PKCS8 private key
2024-02-18 10:42:21 +00:00
func ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
2024-02-18 10:42:21 +00:00
var err error
// Parse PEM block
2024-02-18 10:42:21 +00:00
var block *pem.Block
2024-02-18 10:42:21 +00:00
if block, _ = pem.Decode(key); block == nil {
2024-02-18 10:42:21 +00:00
return nil, ErrKeyMustBePEMEncoded
2024-02-18 10:42:21 +00:00
}
var parsedKey interface{}
2024-02-18 10:42:21 +00:00
if parsedKey, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
2024-02-18 10:42:21 +00:00
if parsedKey, err = x509.ParsePKCS8PrivateKey(block.Bytes); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
var pkey *rsa.PrivateKey
2024-02-18 10:42:21 +00:00
var ok bool
2024-02-18 10:42:21 +00:00
if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
2024-02-18 10:42:21 +00:00
return nil, ErrNotRSAPrivateKey
2024-02-18 10:42:21 +00:00
}
return pkey, nil
2024-02-18 10:42:21 +00:00
}
// Parse PEM encoded PKCS1 or PKCS8 private key protected with password
2024-02-18 10:42:21 +00:00
func ParseRSAPrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.PrivateKey, error) {
2024-02-18 10:42:21 +00:00
var err error
// Parse PEM block
2024-02-18 10:42:21 +00:00
var block *pem.Block
2024-02-18 10:42:21 +00:00
if block, _ = pem.Decode(key); block == nil {
2024-02-18 10:42:21 +00:00
return nil, ErrKeyMustBePEMEncoded
2024-02-18 10:42:21 +00:00
}
var parsedKey interface{}
var blockDecrypted []byte
2024-02-18 10:42:21 +00:00
if blockDecrypted, err = x509.DecryptPEMBlock(block, []byte(password)); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
if parsedKey, err = x509.ParsePKCS1PrivateKey(blockDecrypted); err != nil {
2024-02-18 10:42:21 +00:00
if parsedKey, err = x509.ParsePKCS8PrivateKey(blockDecrypted); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
var pkey *rsa.PrivateKey
2024-02-18 10:42:21 +00:00
var ok bool
2024-02-18 10:42:21 +00:00
if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
2024-02-18 10:42:21 +00:00
return nil, ErrNotRSAPrivateKey
2024-02-18 10:42:21 +00:00
}
return pkey, nil
2024-02-18 10:42:21 +00:00
}
// Parse PEM encoded PKCS1 or PKCS8 public key
2024-02-18 10:42:21 +00:00
func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
2024-02-18 10:42:21 +00:00
var err error
// Parse PEM block
2024-02-18 10:42:21 +00:00
var block *pem.Block
2024-02-18 10:42:21 +00:00
if block, _ = pem.Decode(key); block == nil {
2024-02-18 10:42:21 +00:00
return nil, ErrKeyMustBePEMEncoded
2024-02-18 10:42:21 +00:00
}
// Parse the key
2024-02-18 10:42:21 +00:00
var parsedKey interface{}
2024-02-18 10:42:21 +00:00
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
2024-02-18 10:42:21 +00:00
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
2024-02-18 10:42:21 +00:00
parsedKey = cert.PublicKey
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
var pkey *rsa.PublicKey
2024-02-18 10:42:21 +00:00
var ok bool
2024-02-18 10:42:21 +00:00
if pkey, ok = parsedKey.(*rsa.PublicKey); !ok {
2024-02-18 10:42:21 +00:00
return nil, ErrNotRSAPublicKey
2024-02-18 10:42:21 +00:00
}
return pkey, nil
2024-02-18 10:42:21 +00:00
}