pkcs1.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2011 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/rsa"
  7. "errors"
  8. "math/big"
  9. "github.com/zmap/zcrypto/encoding/asn1"
  10. )
  11. // pkcs1PrivateKey is a structure which mirrors the PKCS#1 ASN.1 for an RSA private key.
  12. type pkcs1PrivateKey struct {
  13. Version int
  14. N *big.Int
  15. E int
  16. D *big.Int
  17. P *big.Int
  18. Q *big.Int
  19. // We ignore these values, if present, because rsa will calculate them.
  20. Dp *big.Int `asn1:"optional"`
  21. Dq *big.Int `asn1:"optional"`
  22. Qinv *big.Int `asn1:"optional"`
  23. AdditionalPrimes []pkcs1AdditionalRSAPrime `asn1:"optional,omitempty"`
  24. }
  25. type pkcs1AdditionalRSAPrime struct {
  26. Prime *big.Int
  27. // We ignore these values because rsa will calculate them.
  28. Exp *big.Int
  29. Coeff *big.Int
  30. }
  31. // pkcs1PublicKey reflects the ASN.1 structure of a PKCS#1 public key.
  32. type pkcs1PublicKey struct {
  33. N *big.Int
  34. E int
  35. }
  36. // ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form.
  37. func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error) {
  38. var priv pkcs1PrivateKey
  39. rest, err := asn1.Unmarshal(der, &priv)
  40. if len(rest) > 0 {
  41. return nil, asn1.SyntaxError{Msg: "trailing data"}
  42. }
  43. if err != nil {
  44. return nil, err
  45. }
  46. if priv.Version > 1 {
  47. return nil, errors.New("x509: unsupported private key version")
  48. }
  49. if priv.N.Sign() <= 0 || priv.D.Sign() <= 0 || priv.P.Sign() <= 0 || priv.Q.Sign() <= 0 {
  50. return nil, errors.New("x509: private key contains zero or negative value")
  51. }
  52. key := new(rsa.PrivateKey)
  53. key.PublicKey = rsa.PublicKey{
  54. E: priv.E,
  55. N: priv.N,
  56. }
  57. key.D = priv.D
  58. key.Primes = make([]*big.Int, 2+len(priv.AdditionalPrimes))
  59. key.Primes[0] = priv.P
  60. key.Primes[1] = priv.Q
  61. for i, a := range priv.AdditionalPrimes {
  62. if a.Prime.Sign() <= 0 {
  63. return nil, errors.New("x509: private key contains zero or negative prime")
  64. }
  65. key.Primes[i+2] = a.Prime
  66. // We ignore the other two values because rsa will calculate
  67. // them as needed.
  68. }
  69. err = key.Validate()
  70. if err != nil {
  71. return nil, err
  72. }
  73. key.Precompute()
  74. return key, nil
  75. }
  76. // MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form.
  77. func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte {
  78. key.Precompute()
  79. version := 0
  80. if len(key.Primes) > 2 {
  81. version = 1
  82. }
  83. priv := pkcs1PrivateKey{
  84. Version: version,
  85. N: key.N,
  86. E: key.PublicKey.E,
  87. D: key.D,
  88. P: key.Primes[0],
  89. Q: key.Primes[1],
  90. Dp: key.Precomputed.Dp,
  91. Dq: key.Precomputed.Dq,
  92. Qinv: key.Precomputed.Qinv,
  93. }
  94. priv.AdditionalPrimes = make([]pkcs1AdditionalRSAPrime, len(key.Precomputed.CRTValues))
  95. for i, values := range key.Precomputed.CRTValues {
  96. priv.AdditionalPrimes[i].Prime = key.Primes[2+i]
  97. priv.AdditionalPrimes[i].Exp = values.Exp
  98. priv.AdditionalPrimes[i].Coeff = values.Coeff
  99. }
  100. b, _ := asn1.Marshal(priv)
  101. return b
  102. }