forked from corestoreio/pkg
/
ecdsa_utils.go
58 lines (50 loc) · 1.48 KB
/
ecdsa_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package csjwt
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"github.com/corestoreio/errors"
)
// Parse PEM encoded Elliptic Curve Private Key Structure
func parseECPrivateKeyFromPEM(key []byte, password ...[]byte) (*ecdsa.PrivateKey, error) {
// Parse PEM block
pemBlock, _ := pem.Decode(key)
if pemBlock == nil {
return nil, errors.NotSupported.Newf(errKeyMustBePEMEncoded)
}
var blockBytes []byte
if x509.IsEncryptedPEMBlock(pemBlock) {
if len(password) != 1 || len(password[0]) == 0 {
return nil, errors.Empty.Newf(errKeyMissingPassword)
}
var errPEM error
if blockBytes, errPEM = x509.DecryptPEMBlock(pemBlock, password[0]); errPEM != nil {
return nil, errors.NotValid.Newf(errKeyDecryptPEMBlockFailed, errPEM)
}
} else {
blockBytes = pemBlock.Bytes
}
// Parse the key
return x509.ParseECPrivateKey(blockBytes)
}
// Parse PEM encoded PKCS1 or PKCS8 public key
func parseECPublicKeyFromPEM(key []byte) (*ecdsa.PublicKey, error) {
// Parse PEM block
block, _ := pem.Decode(key)
if block == nil {
return nil, errors.NotSupported.Newf(errKeyMustBePEMEncoded)
}
// Parse the key
parsedKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, errors.NotValid.Newf(errKeyParseCertificateFailed, err)
}
parsedKey = cert.PublicKey
}
if pkey, ok := parsedKey.(*ecdsa.PublicKey); ok {
return pkey, nil
}
return nil, errors.NotValid.Newf(errKeyNonECDSAPublicKey)
}