aboutsummaryrefslogtreecommitdiff
path: root/crypto.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-01-19 12:33:09 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-01-19 12:33:09 +0100
commit77debcd18fde7e8c74537bd61e7b5d165fe44c47 (patch)
treeb6264acb46f98f4d43a2eaa5e9db0f43b44c5a4c /crypto.go
parent347aee40f3dc38122f3cf1144673db089e71512c (diff)
crypto.PrivateKey
Diffstat (limited to 'crypto.go')
-rw-r--r--crypto.go16
1 files changed, 13 insertions, 3 deletions
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 {