pkcs1.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/google/certificate-transparency-go/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 parses an RSA private key in PKCS#1, ASN.1 DER form.
  37. //
  38. // This kind of key is commonly encoded in PEM blocks of type "RSA PRIVATE KEY".
  39. func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error) {
  40. var priv pkcs1PrivateKey
  41. rest, err := asn1.Unmarshal(der, &priv)
  42. if len(rest) > 0 {
  43. return nil, asn1.SyntaxError{Msg: "trailing data"}
  44. }
  45. if err != nil {
  46. if _, err := asn1.Unmarshal(der, &ecPrivateKey{}); err == nil {
  47. return nil, errors.New("x509: failed to parse private key (use ParseECPrivateKey instead for this key format)")
  48. }
  49. if _, err := asn1.Unmarshal(der, &pkcs8{}); err == nil {
  50. return nil, errors.New("x509: failed to parse private key (use ParsePKCS8PrivateKey instead for this key format)")
  51. }
  52. return nil, err
  53. }
  54. if priv.Version > 1 {
  55. return nil, errors.New("x509: unsupported private key version")
  56. }
  57. if priv.N.Sign() <= 0 || priv.D.Sign() <= 0 || priv.P.Sign() <= 0 || priv.Q.Sign() <= 0 {
  58. return nil, errors.New("x509: private key contains zero or negative value")
  59. }
  60. key := new(rsa.PrivateKey)
  61. key.PublicKey = rsa.PublicKey{
  62. E: priv.E,
  63. N: priv.N,
  64. }
  65. key.D = priv.D
  66. key.Primes = make([]*big.Int, 2+len(priv.AdditionalPrimes))
  67. key.Primes[0] = priv.P
  68. key.Primes[1] = priv.Q
  69. for i, a := range priv.AdditionalPrimes {
  70. if a.Prime.Sign() <= 0 {
  71. return nil, errors.New("x509: private key contains zero or negative prime")
  72. }
  73. key.Primes[i+2] = a.Prime
  74. // We ignore the other two values because rsa will calculate
  75. // them as needed.
  76. }
  77. err = key.Validate()
  78. if err != nil {
  79. return nil, err
  80. }
  81. key.Precompute()
  82. return key, nil
  83. }
  84. // MarshalPKCS1PrivateKey converts an RSA private key to PKCS#1, ASN.1 DER form.
  85. //
  86. // This kind of key is commonly encoded in PEM blocks of type "RSA PRIVATE KEY".
  87. // For a more flexible key format which is not RSA specific, use
  88. // MarshalPKCS8PrivateKey.
  89. func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte {
  90. key.Precompute()
  91. version := 0
  92. if len(key.Primes) > 2 {
  93. version = 1
  94. }
  95. priv := pkcs1PrivateKey{
  96. Version: version,
  97. N: key.N,
  98. E: key.PublicKey.E,
  99. D: key.D,
  100. P: key.Primes[0],
  101. Q: key.Primes[1],
  102. Dp: key.Precomputed.Dp,
  103. Dq: key.Precomputed.Dq,
  104. Qinv: key.Precomputed.Qinv,
  105. }
  106. priv.AdditionalPrimes = make([]pkcs1AdditionalRSAPrime, len(key.Precomputed.CRTValues))
  107. for i, values := range key.Precomputed.CRTValues {
  108. priv.AdditionalPrimes[i].Prime = key.Primes[2+i]
  109. priv.AdditionalPrimes[i].Exp = values.Exp
  110. priv.AdditionalPrimes[i].Coeff = values.Coeff
  111. }
  112. b, _ := asn1.Marshal(priv)
  113. return b
  114. }
  115. // ParsePKCS1PublicKey parses an RSA public key in PKCS#1, ASN.1 DER form.
  116. //
  117. // This kind of key is commonly encoded in PEM blocks of type "RSA PUBLIC KEY".
  118. func ParsePKCS1PublicKey(der []byte) (*rsa.PublicKey, error) {
  119. var pub pkcs1PublicKey
  120. rest, err := asn1.Unmarshal(der, &pub)
  121. if err != nil {
  122. if _, err := asn1.Unmarshal(der, &publicKeyInfo{}); err == nil {
  123. return nil, errors.New("x509: failed to parse public key (use ParsePKIXPublicKey instead for this key format)")
  124. }
  125. return nil, err
  126. }
  127. if len(rest) > 0 {
  128. return nil, asn1.SyntaxError{Msg: "trailing data"}
  129. }
  130. if pub.N.Sign() <= 0 || pub.E <= 0 {
  131. return nil, errors.New("x509: public key contains zero or negative value")
  132. }
  133. if pub.E > 1<<31-1 {
  134. return nil, errors.New("x509: public key contains large public exponent")
  135. }
  136. return &rsa.PublicKey{
  137. E: pub.E,
  138. N: pub.N,
  139. }, nil
  140. }
  141. // MarshalPKCS1PublicKey converts an RSA public key to PKCS#1, ASN.1 DER form.
  142. //
  143. // This kind of key is commonly encoded in PEM blocks of type "RSA PUBLIC KEY".
  144. func MarshalPKCS1PublicKey(key *rsa.PublicKey) []byte {
  145. derBytes, _ := asn1.Marshal(pkcs1PublicKey{
  146. N: key.N,
  147. E: key.E,
  148. })
  149. return derBytes
  150. }