| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package x509andr |
| 6 |
|
| 7 |
import ( |
| 8 |
"crypto/ecdsa" |
| 9 |
"crypto/elliptic" |
| 10 |
"encoding/asn1" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"math/big" |
| 14 |
) |
| 15 |
|
| 16 |
const ecPrivKeyVersion = 1 |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
type ecPrivateKey struct { |
| 25 |
Version int |
| 26 |
PrivateKey []byte |
| 27 |
NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"` |
| 28 |
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"` |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func ParseECPrivateKey(der []byte) (*ecdsa.PrivateKey, error) { |
| 33 |
return parseECPrivateKey(nil, der) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error) { |
| 38 |
oid, ok := oidFromNamedCurve(key.Curve) |
| 39 |
if !ok { |
| 40 |
return nil, errors.New("x509: unknown elliptic curve") |
| 41 |
} |
| 42 |
|
| 43 |
privateKeyBytes := key.D.Bytes() |
| 44 |
paddedPrivateKey := make([]byte, (key.Curve.Params().N.BitLen()+7)/8) |
| 45 |
copy(paddedPrivateKey[len(paddedPrivateKey)-len(privateKeyBytes):], privateKeyBytes) |
| 46 |
|
| 47 |
return asn1.Marshal(ecPrivateKey{ |
| 48 |
Version: 1, |
| 49 |
PrivateKey: paddedPrivateKey, |
| 50 |
NamedCurveOID: oid, |
| 51 |
PublicKey: asn1.BitString{Bytes: elliptic.Marshal(key.Curve, key.X, key.Y)}, |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *ecdsa.PrivateKey, err error) { |
| 60 |
var privKey ecPrivateKey |
| 61 |
if _, err := asn1.Unmarshal(der, &privKey); err != nil { |
| 62 |
return nil, errors.New("x509: failed to parse EC private key: " + err.Error()) |
| 63 |
} |
| 64 |
if privKey.Version != ecPrivKeyVersion { |
| 65 |
return nil, fmt.Errorf("x509: unknown EC private key version %d", privKey.Version) |
| 66 |
} |
| 67 |
|
| 68 |
var curve elliptic.Curve |
| 69 |
if namedCurveOID != nil { |
| 70 |
curve = namedCurveFromOID(*namedCurveOID) |
| 71 |
} else { |
| 72 |
curve = namedCurveFromOID(privKey.NamedCurveOID) |
| 73 |
} |
| 74 |
if curve == nil { |
| 75 |
return nil, errors.New("x509: unknown elliptic curve") |
| 76 |
} |
| 77 |
|
| 78 |
k := new(big.Int).SetBytes(privKey.PrivateKey) |
| 79 |
curveOrder := curve.Params().N |
| 80 |
if k.Cmp(curveOrder) >= 0 { |
| 81 |
return nil, errors.New("x509: invalid elliptic curve private key value") |
| 82 |
} |
| 83 |
priv := new(ecdsa.PrivateKey) |
| 84 |
priv.Curve = curve |
| 85 |
priv.D = k |
| 86 |
|
| 87 |
privateKey := make([]byte, (curveOrder.BitLen()+7)/8) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
for len(privKey.PrivateKey) > len(privateKey) { |
| 92 |
if privKey.PrivateKey[0] != 0 { |
| 93 |
return nil, errors.New("x509: invalid private key length") |
| 94 |
} |
| 95 |
privKey.PrivateKey = privKey.PrivateKey[1:] |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey) |
| 102 |
priv.X, priv.Y = curve.ScalarBaseMult(privateKey) |
| 103 |
|
| 104 |
return priv, nil |
| 105 |
} |