local-server/vendor/github.com/cloudflare/cfssl/helpers/derhelpers/derhelpers.go

49 lines
1.3 KiB
Go
Raw Normal View History

2018-06-17 07:32:44 +00:00
// Package derhelpers implements common functionality
// on DER encoded data
package derhelpers
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
cferr "github.com/cloudflare/cfssl/errors"
2018-12-10 13:44:05 +00:00
"golang.org/x/crypto/ed25519"
2018-06-17 07:32:44 +00:00
)
2018-12-10 13:44:05 +00:00
// ParsePrivateKeyDER parses a PKCS #1, PKCS #8, ECDSA, or Ed25519 DER-encoded
// private key. The key must not be in PEM format.
2018-06-17 07:32:44 +00:00
func ParsePrivateKeyDER(keyDER []byte) (key crypto.Signer, err error) {
generalKey, err := x509.ParsePKCS8PrivateKey(keyDER)
if err != nil {
generalKey, err = x509.ParsePKCS1PrivateKey(keyDER)
if err != nil {
generalKey, err = x509.ParseECPrivateKey(keyDER)
if err != nil {
2018-12-10 13:44:05 +00:00
generalKey, err = ParseEd25519PrivateKey(keyDER)
if err != nil {
// We don't include the actual error into
// the final error. The reason might be
// we don't want to leak any info about
// the private key.
return nil, cferr.New(cferr.PrivateKeyError,
cferr.ParseFailed)
}
2018-06-17 07:32:44 +00:00
}
}
}
switch generalKey.(type) {
case *rsa.PrivateKey:
return generalKey.(*rsa.PrivateKey), nil
case *ecdsa.PrivateKey:
return generalKey.(*ecdsa.PrivateKey), nil
2018-12-10 13:44:05 +00:00
case ed25519.PrivateKey:
return generalKey.(ed25519.PrivateKey), nil
2018-06-17 07:32:44 +00:00
}
// should never reach here
return nil, cferr.New(cferr.PrivateKeyError, cferr.ParseFailed)
}