sec1.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. package x509
  5. import (
  6. "crypto/ecdsa"
  7. "crypto/elliptic"
  8. "errors"
  9. "fmt"
  10. "math/big"
  11. "github.com/google/certificate-transparency-go/asn1"
  12. )
  13. const ecPrivKeyVersion = 1
  14. // ecPrivateKey reflects an ASN.1 Elliptic Curve Private Key Structure.
  15. // References:
  16. // RFC 5915
  17. // SEC1 - http://www.secg.org/sec1-v2.pdf
  18. // Per RFC 5915 the NamedCurveOID is marked as ASN.1 OPTIONAL, however in
  19. // most cases it is not.
  20. type ecPrivateKey struct {
  21. Version int
  22. PrivateKey []byte
  23. NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
  24. PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
  25. }
  26. // ParseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
  27. func ParseECPrivateKey(der []byte) (*ecdsa.PrivateKey, error) {
  28. return parseECPrivateKey(nil, der)
  29. }
  30. // MarshalECPrivateKey marshals an EC private key into ASN.1, DER format.
  31. func MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error) {
  32. oid, ok := OIDFromNamedCurve(key.Curve)
  33. if !ok {
  34. return nil, errors.New("x509: unknown elliptic curve")
  35. }
  36. return marshalECPrivateKeyWithOID(key, oid)
  37. }
  38. // marshalECPrivateKey marshals an EC private key into ASN.1, DER format and
  39. // sets the curve ID to the given OID, or omits it if OID is nil.
  40. func marshalECPrivateKeyWithOID(key *ecdsa.PrivateKey, oid asn1.ObjectIdentifier) ([]byte, error) {
  41. privateKeyBytes := key.D.Bytes()
  42. paddedPrivateKey := make([]byte, (key.Curve.Params().N.BitLen()+7)/8)
  43. copy(paddedPrivateKey[len(paddedPrivateKey)-len(privateKeyBytes):], privateKeyBytes)
  44. return asn1.Marshal(ecPrivateKey{
  45. Version: 1,
  46. PrivateKey: paddedPrivateKey,
  47. NamedCurveOID: oid,
  48. PublicKey: asn1.BitString{Bytes: elliptic.Marshal(key.Curve, key.X, key.Y)},
  49. })
  50. }
  51. // parseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
  52. // The OID for the named curve may be provided from another source (such as
  53. // the PKCS8 container) - if it is provided then use this instead of the OID
  54. // that may exist in the EC private key structure.
  55. func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *ecdsa.PrivateKey, err error) {
  56. var privKey ecPrivateKey
  57. if _, err := asn1.Unmarshal(der, &privKey); err != nil {
  58. return nil, errors.New("x509: failed to parse EC private key: " + err.Error())
  59. }
  60. if privKey.Version != ecPrivKeyVersion {
  61. return nil, fmt.Errorf("x509: unknown EC private key version %d", privKey.Version)
  62. }
  63. var nfe NonFatalErrors
  64. var curve elliptic.Curve
  65. if namedCurveOID != nil {
  66. curve = namedCurveFromOID(*namedCurveOID, &nfe)
  67. } else {
  68. curve = namedCurveFromOID(privKey.NamedCurveOID, &nfe)
  69. }
  70. if curve == nil {
  71. return nil, errors.New("x509: unknown elliptic curve")
  72. }
  73. k := new(big.Int).SetBytes(privKey.PrivateKey)
  74. curveOrder := curve.Params().N
  75. if k.Cmp(curveOrder) >= 0 {
  76. return nil, errors.New("x509: invalid elliptic curve private key value")
  77. }
  78. priv := new(ecdsa.PrivateKey)
  79. priv.Curve = curve
  80. priv.D = k
  81. privateKey := make([]byte, (curveOrder.BitLen()+7)/8)
  82. // Some private keys have leading zero padding. This is invalid
  83. // according to [SEC1], but this code will ignore it.
  84. for len(privKey.PrivateKey) > len(privateKey) {
  85. if privKey.PrivateKey[0] != 0 {
  86. return nil, errors.New("x509: invalid private key length")
  87. }
  88. privKey.PrivateKey = privKey.PrivateKey[1:]
  89. }
  90. // Some private keys remove all leading zeros, this is also invalid
  91. // according to [SEC1] but since OpenSSL used to do this, we ignore
  92. // this too.
  93. copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey)
  94. priv.X, priv.Y = curve.ScalarBaseMult(privateKey)
  95. return priv, nil
  96. }