From 77debcd18fde7e8c74537bd61e7b5d165fe44c47 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 19 Jan 2016 12:33:09 +0100 Subject: crypto.PrivateKey --- account.go | 3 +-- crypto.go | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/account.go b/account.go index eebe2de..48849c4 100644 --- a/account.go +++ b/account.go @@ -7,7 +7,6 @@ import ( "crypto/rsa" "encoding/base64" "encoding/json" - "errors" "io" "strings" @@ -48,7 +47,7 @@ func newAccount(key crypto.PrivateKey) (*Account, error) { signer, err := jose.NewSigner(jose.ES384, k) return &Account{key: k, signer: signer}, err default: - return nil, errors.New("unknown key type") + return nil, errKeyType } } diff --git a/crypto.go b/crypto.go index cbe47fd..7b3fc6b 100644 --- a/crypto.go +++ b/crypto.go @@ -6,13 +6,16 @@ import ( "crypto/rsa" "crypto/x509" "encoding/pem" + "errors" "io" "io/ioutil" "os" "path" ) -func LoadKeyFile(fname string) (*rsa.PrivateKey, error) { +var errKeyType = errors.New("unknown key type") + +func LoadKeyFile(fname string) (crypto.PrivateKey, error) { fd, err := os.Open(fname) if err != nil { return nil, err @@ -21,13 +24,20 @@ func LoadKeyFile(fname string) (*rsa.PrivateKey, error) { return LoadKey(fd) } -func LoadKey(r io.Reader) (*rsa.PrivateKey, error) { +func LoadKey(r io.Reader) (crypto.PrivateKey, error) { der, err := ioutil.ReadAll(r) if err != nil { return nil, err } block, _ := pem.Decode(der) - return x509.ParsePKCS1PrivateKey(block.Bytes) + switch block.Type { + case "RSA PRIVATE KEY": + return x509.ParsePKCS1PrivateKey(block.Bytes) + case "EC PRIVATE KEY": + return x509.ParseECPrivateKey(block.Bytes) + default: + return nil, errKeyType + } } func SaveKey(w io.Writer, key crypto.PrivateKey) error { -- cgit v1.2.3