1
2
3
4
5 package x509
6
7 import (
8 "crypto/ecdh"
9 "crypto/ecdsa"
10 "crypto/elliptic"
11 "encoding/asn1"
12 "errors"
13 "fmt"
14 "math/big"
15 )
16
17 const ecPrivKeyVersion = 1
18
19
20
21
22
23
24
25
26
27 type ecPrivateKey struct {
28 Version int
29 PrivateKey []byte
30 NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
31 PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
32 }
33
34
35
36
37 func ParseECPrivateKey(der []byte) (*ecdsa.PrivateKey, error) {
38 return parseECPrivateKey(nil, der)
39 }
40
41
42
43
44
45
46 func MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error) {
47 oid, ok := oidFromNamedCurve(key.Curve)
48 if !ok {
49 return nil, errors.New("x509: unknown elliptic curve")
50 }
51
52 return marshalECPrivateKeyWithOID(key, oid)
53 }
54
55
56
57 func marshalECPrivateKeyWithOID(key *ecdsa.PrivateKey, oid asn1.ObjectIdentifier) ([]byte, error) {
58 if !key.Curve.IsOnCurve(key.X, key.Y) {
59 return nil, errors.New("invalid elliptic key public key")
60 }
61 privateKey := make([]byte, (key.Curve.Params().N.BitLen()+7)/8)
62 return asn1.Marshal(ecPrivateKey{
63 Version: 1,
64 PrivateKey: key.D.FillBytes(privateKey),
65 NamedCurveOID: oid,
66 PublicKey: asn1.BitString{Bytes: elliptic.Marshal(key.Curve, key.X, key.Y)},
67 })
68 }
69
70
71
72 func marshalECDHPrivateKey(key *ecdh.PrivateKey) ([]byte, error) {
73 return asn1.Marshal(ecPrivateKey{
74 Version: 1,
75 PrivateKey: key.Bytes(),
76 PublicKey: asn1.BitString{Bytes: key.PublicKey().Bytes()},
77 })
78 }
79
80
81
82
83
84 func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *ecdsa.PrivateKey, err error) {
85 var privKey ecPrivateKey
86 if _, err := asn1.Unmarshal(der, &privKey); err != nil {
87 if _, err := asn1.Unmarshal(der, &pkcs8{}); err == nil {
88 return nil, errors.New("x509: failed to parse private key (use ParsePKCS8PrivateKey instead for this key format)")
89 }
90 if _, err := asn1.Unmarshal(der, &pkcs1PrivateKey{}); err == nil {
91 return nil, errors.New("x509: failed to parse private key (use ParsePKCS1PrivateKey instead for this key format)")
92 }
93 return nil, errors.New("x509: failed to parse EC private key: " + err.Error())
94 }
95 if privKey.Version != ecPrivKeyVersion {
96 return nil, fmt.Errorf("x509: unknown EC private key version %d", privKey.Version)
97 }
98
99 var curve elliptic.Curve
100 if namedCurveOID != nil {
101 curve = namedCurveFromOID(*namedCurveOID)
102 } else {
103 curve = namedCurveFromOID(privKey.NamedCurveOID)
104 }
105 if curve == nil {
106 return nil, errors.New("x509: unknown elliptic curve")
107 }
108
109 k := new(big.Int).SetBytes(privKey.PrivateKey)
110 curveOrder := curve.Params().N
111 if k.Cmp(curveOrder) >= 0 {
112 return nil, errors.New("x509: invalid elliptic curve private key value")
113 }
114 priv := new(ecdsa.PrivateKey)
115 priv.Curve = curve
116 priv.D = k
117
118 privateKey := make([]byte, (curveOrder.BitLen()+7)/8)
119
120
121
122 for len(privKey.PrivateKey) > len(privateKey) {
123 if privKey.PrivateKey[0] != 0 {
124 return nil, errors.New("x509: invalid private key length")
125 }
126 privKey.PrivateKey = privKey.PrivateKey[1:]
127 }
128
129
130
131
132 copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey)
133 priv.X, priv.Y = curve.ScalarBaseMult(privateKey)
134
135 return priv, nil
136 }
137
View as plain text