mirror of
https://gitlab.com/pulsechaincom/go-pulse.git
synced 2024-12-25 04:47:17 +00:00
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).
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package jwt
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"errors"
|
|
)
|
|
|
|
var (
|
|
ErrNotECPublicKey = errors.New("Key is not a valid ECDSA public key")
|
|
ErrNotECPrivateKey = errors.New("Key is not a valid ECDSA private key")
|
|
)
|
|
|
|
// Parse PEM encoded Elliptic Curve Private Key Structure
|
|
func ParseECPrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, 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.ParseECPrivateKey(block.Bytes); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var pkey *ecdsa.PrivateKey
|
|
var ok bool
|
|
if pkey, ok = parsedKey.(*ecdsa.PrivateKey); !ok {
|
|
return nil, ErrNotECPrivateKey
|
|
}
|
|
|
|
return pkey, nil
|
|
}
|
|
|
|
// Parse PEM encoded PKCS1 or PKCS8 public key
|
|
func ParseECPublicKeyFromPEM(key []byte) (*ecdsa.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 *ecdsa.PublicKey
|
|
var ok bool
|
|
if pkey, ok = parsedKey.(*ecdsa.PublicKey); !ok {
|
|
return nil, ErrNotECPublicKey
|
|
}
|
|
|
|
return pkey, nil
|
|
}
|