github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/crypto/x509/sec1.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package x509
     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  // ecPrivateKey reflects an ASN.1 Elliptic Curve Private Key Structure.
    19  // References:
    20  //   RFC5915
    21  //   SEC1 - http://www.secg.org/sec1-v2.pdf
    22  // Per RFC5915 the NamedCurveOID is marked as ASN.1 OPTIONAL, however in
    23  // most cases it is not.
    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  // ParseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
    32  func ParseECPrivateKey(der []byte) (key *ecdsa.PrivateKey, err error) {
    33  	return parseECPrivateKey(nil, der)
    34  }
    35  
    36  // MarshalECPrivateKey marshals an EC private key into ASN.1, DER format.
    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  	return asn1.Marshal(ecPrivateKey{
    43  		Version:       1,
    44  		PrivateKey:    key.D.Bytes(),
    45  		NamedCurveOID: oid,
    46  		PublicKey:     asn1.BitString{Bytes: elliptic.Marshal(key.Curve, key.X, key.Y)},
    47  	})
    48  }
    49  
    50  // parseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
    51  // The OID for the named curve may be provided from another source (such as
    52  // the PKCS8 container) - if it is provided then use this instead of the OID
    53  // that may exist in the EC private key structure.
    54  func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *ecdsa.PrivateKey, err error) {
    55  	var privKey ecPrivateKey
    56  	if _, err := asn1.Unmarshal(der, &privKey); err != nil {
    57  		return nil, errors.New("x509: failed to parse EC private key: " + err.Error())
    58  	}
    59  	if privKey.Version != ecPrivKeyVersion {
    60  		return nil, fmt.Errorf("x509: unknown EC private key version %d", privKey.Version)
    61  	}
    62  
    63  	var curve elliptic.Curve
    64  	if namedCurveOID != nil {
    65  		curve = namedCurveFromOID(*namedCurveOID)
    66  	} else {
    67  		curve = namedCurveFromOID(privKey.NamedCurveOID)
    68  	}
    69  	if curve == nil {
    70  		return nil, errors.New("x509: unknown elliptic curve")
    71  	}
    72  
    73  	k := new(big.Int).SetBytes(privKey.PrivateKey)
    74  	if k.Cmp(curve.Params().N) >= 0 {
    75  		return nil, errors.New("x509: invalid elliptic curve private key value")
    76  	}
    77  	priv := new(ecdsa.PrivateKey)
    78  	priv.Curve = curve
    79  	priv.D = k
    80  	priv.X, priv.Y = curve.ScalarBaseMult(privKey.PrivateKey)
    81  
    82  	return priv, nil
    83  }