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