435c0d9fc8
This PR switches the Nomad repository from using govendor to Go modules for managing dependencies. Aspects of the Nomad workflow remain pretty much the same. The usual Makefile targets should continue to work as they always did. The API submodule simply defers to the parent Nomad version on the repository, keeping the semantics of API versioning that currently exists.
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
|
|
}
|