aboutsummaryrefslogtreecommitdiff
path: root/private_key.go
diff options
context:
space:
mode:
Diffstat (limited to 'private_key.go')
-rw-r--r--private_key.go24
1 files changed, 17 insertions, 7 deletions
diff --git a/private_key.go b/private_key.go
index 3e6aee3..0591e18 100644
--- a/private_key.go
+++ b/private_key.go
@@ -5,25 +5,22 @@ import (
"github.com/gibheer/pki"
)
-const (
- TypeLabelRSA = "RSA PRIVATE KEY"
- TypeLabelECDSA = "EC PRIVATE KEY"
-)
-
var (
ErrNoPKFound = errors.New("no private key found")
+ ErrNoPUFound = errors.New("no public key found")
+ ErrUnknownFormat = errors.New("key is an unknown format")
)
// Read the private key from the path and try to figure out which type of key it
// might be.
func ReadPrivateKeyFile(path string) (pki.PrivateKey, error) {
- raw_pk, err := readSectionFromFile(path, TypeLabelECDSA)
+ raw_pk, err := readSectionFromFile(path, pki.PemLabelEcdsa)
if err == nil {
pk, err := pki.LoadPrivateKeyEcdsa(raw_pk)
if err != nil { return nil, err }
return pk, nil
}
- raw_pk, err = readSectionFromFile(path, TypeLabelRSA)
+ raw_pk, err = readSectionFromFile(path, pki.PemLabelRsa)
if err == nil {
pk, err := pki.LoadPrivateKeyRsa(raw_pk)
if err != nil { return nil, err }
@@ -31,3 +28,16 @@ func ReadPrivateKeyFile(path string) (pki.PrivateKey, error) {
}
return nil, ErrNoPKFound
}
+
+// read the public key and try to figure out what kind of key it might be
+func ReadPublicKeyFile(path string) (pki.PublicKey, error) {
+ raw_pu, err := readSectionFromFile(path, pki.PemLabelPublic)
+ if err != nil { return nil, ErrNoPUFound }
+
+ var public pki.PublicKey
+ public, err = pki.LoadPublicKeyEcdsa(raw_pu)
+ if err == nil { return public, nil }
+ public, err = pki.LoadPublicKeyRsa(raw_pu)
+ if err == nil { return public, nil }
+ return nil, ErrUnknownFormat
+}