c76ad94492
This commit adds a build step to travis to auto-delete unstable archives older than 14 days (our regular release schedule) from Azure via ci.go purge. The commit also pulls in the latest Azure storage code, also switching over from the old import path (github.com/Azure/azure-sdk-for-go) to the new split one (github.com/Azure/azure-storage-go).
70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package jwt
|
|
|
|
import (
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"errors"
|
|
)
|
|
|
|
var (
|
|
ErrKeyMustBePEMEncoded = errors.New("Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key")
|
|
ErrNotRSAPrivateKey = errors.New("Key is not a valid RSA private key")
|
|
ErrNotRSAPublicKey = errors.New("Key is not a valid RSA public key")
|
|
)
|
|
|
|
// Parse PEM encoded PKCS1 or PKCS8 private key
|
|
func ParseRSAPrivateKeyFromPEM(key []byte) (*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{}
|
|
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 {
|
|
return nil, ErrNotRSAPrivateKey
|
|
}
|
|
|
|
return pkey, nil
|
|
}
|
|
|
|
// Parse PEM encoded PKCS1 or PKCS8 public key
|
|
func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
|
|
var err error
|
|
|
|
// Parse PEM block
|
|
var block *pem.Block
|
|
if block, _ = pem.Decode(key); block == nil {
|
|
return nil, ErrKeyMustBePEMEncoded
|
|
}
|
|
|
|
// 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 {
|
|
return nil, ErrNotRSAPublicKey
|
|
}
|
|
|
|
return pkey, nil
|
|
}
|