sec1.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "encoding/asn1"
  9. "errors"
  10. "fmt"
  11. "math/big"
  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. privateKeyBytes := key.D.Bytes()
  37. paddedPrivateKey := make([]byte, (key.Curve.Params().N.BitLen()+7)/8)
  38. copy(paddedPrivateKey[len(paddedPrivateKey)-len(privateKeyBytes):], privateKeyBytes)
  39. return asn1.Marshal(ecPrivateKey{
  40. Version: 1,
  41. PrivateKey: paddedPrivateKey,
  42. NamedCurveOID: oid,
  43. PublicKey: asn1.BitString{Bytes: elliptic.Marshal(key.Curve, key.X, key.Y)},
  44. })
  45. }
  46. // parseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
  47. // The OID for the named curve may be provided from another source (such as
  48. // the PKCS8 container) - if it is provided then use this instead of the OID
  49. // that may exist in the EC private key structure.
  50. func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *ecdsa.PrivateKey, err error) {
  51. var privKey ecPrivateKey
  52. if _, err := asn1.Unmarshal(der, &privKey); err != nil {
  53. return nil, errors.New("x509: failed to parse EC private key: " + err.Error())
  54. }
  55. if privKey.Version != ecPrivKeyVersion {
  56. return nil, fmt.Errorf("x509: unknown EC private key version %d", privKey.Version)
  57. }
  58. var curve elliptic.Curve
  59. if namedCurveOID != nil {
  60. curve = namedCurveFromOID(*namedCurveOID)
  61. } else {
  62. curve = namedCurveFromOID(privKey.NamedCurveOID)
  63. }
  64. if curve == nil {
  65. return nil, errors.New("x509: unknown elliptic curve")
  66. }
  67. k := new(big.Int).SetBytes(privKey.PrivateKey)
  68. curveOrder := curve.Params().N
  69. if k.Cmp(curveOrder) >= 0 {
  70. return nil, errors.New("x509: invalid elliptic curve private key value")
  71. }
  72. priv := new(ecdsa.PrivateKey)
  73. priv.Curve = curve
  74. priv.D = k
  75. privateKey := make([]byte, (curveOrder.BitLen()+7)/8)
  76. // Some private keys have leading zero padding. This is invalid
  77. // according to [SEC1], but this code will ignore it.
  78. for len(privKey.PrivateKey) > len(privateKey) {
  79. if privKey.PrivateKey[0] != 0 {
  80. return nil, errors.New("x509: invalid private key length")
  81. }
  82. privKey.PrivateKey = privKey.PrivateKey[1:]
  83. }
  84. // Some private keys remove all leading zeros, this is also invalid
  85. // according to [SEC1] but since OpenSSL used to do this, we ignore
  86. // this too.
  87. copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey)
  88. priv.X, priv.Y = curve.ScalarBaseMult(privateKey)
  89. return priv, nil
  90. }