sec1.go 3.4 KB

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