| |
| |
| |
|
|
| package x509 |
|
|
| import ( |
| "crypto/ecdh" |
| "crypto/ecdsa" |
| "crypto/elliptic" |
| "encoding/asn1" |
| "errors" |
| "fmt" |
| ) |
|
|
| const ecPrivKeyVersion = 1 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| type ecPrivateKey struct { |
| Version int |
| PrivateKey []byte |
| NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"` |
| PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"` |
| } |
|
|
| |
| |
| |
| func ParseECPrivateKey(der []byte) (*ecdsa.PrivateKey, error) { |
| return parseECPrivateKey(nil, der) |
| } |
|
|
| |
| |
| |
| |
| |
| func MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error) { |
| oid, ok := oidFromNamedCurve(key.Curve) |
| if !ok { |
| return nil, errors.New("x509: unknown elliptic curve") |
| } |
|
|
| return marshalECPrivateKeyWithOID(key, oid) |
| } |
|
|
| |
| |
| func marshalECPrivateKeyWithOID(key *ecdsa.PrivateKey, oid asn1.ObjectIdentifier) ([]byte, error) { |
| privateKey, err := key.Bytes() |
| if err != nil { |
| return nil, err |
| } |
| publicKey, err := key.PublicKey.Bytes() |
| if err != nil { |
| return nil, err |
| } |
| return asn1.Marshal(ecPrivateKey{ |
| Version: 1, |
| PrivateKey: privateKey, |
| NamedCurveOID: oid, |
| PublicKey: asn1.BitString{Bytes: publicKey}, |
| }) |
| } |
|
|
| |
| |
| func marshalECDHPrivateKey(key *ecdh.PrivateKey) ([]byte, error) { |
| return asn1.Marshal(ecPrivateKey{ |
| Version: 1, |
| PrivateKey: key.Bytes(), |
| PublicKey: asn1.BitString{Bytes: key.PublicKey().Bytes()}, |
| }) |
| } |
|
|
| |
| |
| |
| |
| func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *ecdsa.PrivateKey, err error) { |
| var privKey ecPrivateKey |
| if _, err := asn1.Unmarshal(der, &privKey); err != nil { |
| if _, err := asn1.Unmarshal(der, &pkcs8{}); err == nil { |
| return nil, errors.New("x509: failed to parse private key (use ParsePKCS8PrivateKey instead for this key format)") |
| } |
| if _, err := asn1.Unmarshal(der, &pkcs1PrivateKey{}); err == nil { |
| return nil, errors.New("x509: failed to parse private key (use ParsePKCS1PrivateKey instead for this key format)") |
| } |
| return nil, errors.New("x509: failed to parse EC private key: " + err.Error()) |
| } |
| if privKey.Version != ecPrivKeyVersion { |
| return nil, fmt.Errorf("x509: unknown EC private key version %d", privKey.Version) |
| } |
|
|
| var curve elliptic.Curve |
| if namedCurveOID != nil { |
| curve = namedCurveFromOID(*namedCurveOID) |
| } else { |
| curve = namedCurveFromOID(privKey.NamedCurveOID) |
| } |
| if curve == nil { |
| return nil, errors.New("x509: unknown elliptic curve") |
| } |
|
|
| size := (curve.Params().N.BitLen() + 7) / 8 |
| privateKey := make([]byte, size) |
|
|
| |
| |
| for len(privKey.PrivateKey) > len(privateKey) { |
| if privKey.PrivateKey[0] != 0 { |
| return nil, errors.New("x509: invalid private key length") |
| } |
| privKey.PrivateKey = privKey.PrivateKey[1:] |
| } |
|
|
| |
| |
| |
| copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey) |
|
|
| return ecdsa.ParseRawPrivateKey(curve, privateKey) |
| } |
|
|