pkcs1.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright Suzhou Tongji Fintech Research Institute 2017 All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sm2
  14. import (
  15. "crypto/rsa"
  16. "encoding/asn1"
  17. "errors"
  18. "math/big"
  19. )
  20. // pkcs1PrivateKey is a structure which mirrors the PKCS#1 ASN.1 for an RSA private key.
  21. type pkcs1PrivateKey struct {
  22. Version int
  23. N *big.Int
  24. E int
  25. D *big.Int
  26. P *big.Int
  27. Q *big.Int
  28. // We ignore these values, if present, because rsa will calculate them.
  29. Dp *big.Int `asn1:"optional"`
  30. Dq *big.Int `asn1:"optional"`
  31. Qinv *big.Int `asn1:"optional"`
  32. AdditionalPrimes []pkcs1AdditionalRSAPrime `asn1:"optional,omitempty"`
  33. }
  34. type pkcs1AdditionalRSAPrime struct {
  35. Prime *big.Int
  36. // We ignore these values because rsa will calculate them.
  37. Exp *big.Int
  38. Coeff *big.Int
  39. }
  40. // ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form.
  41. func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error) {
  42. var priv pkcs1PrivateKey
  43. rest, err := asn1.Unmarshal(der, &priv)
  44. if len(rest) > 0 {
  45. return nil, asn1.SyntaxError{Msg: "trailing data"}
  46. }
  47. if err != nil {
  48. return nil, err
  49. }
  50. if priv.Version > 1 {
  51. return nil, errors.New("x509: unsupported private key version")
  52. }
  53. if priv.N.Sign() <= 0 || priv.D.Sign() <= 0 || priv.P.Sign() <= 0 || priv.Q.Sign() <= 0 {
  54. return nil, errors.New("x509: private key contains zero or negative value")
  55. }
  56. key := new(rsa.PrivateKey)
  57. key.PublicKey = rsa.PublicKey{
  58. E: priv.E,
  59. N: priv.N,
  60. }
  61. key.D = priv.D
  62. key.Primes = make([]*big.Int, 2+len(priv.AdditionalPrimes))
  63. key.Primes[0] = priv.P
  64. key.Primes[1] = priv.Q
  65. for i, a := range priv.AdditionalPrimes {
  66. if a.Prime.Sign() <= 0 {
  67. return nil, errors.New("x509: private key contains zero or negative prime")
  68. }
  69. key.Primes[i+2] = a.Prime
  70. // We ignore the other two values because rsa will calculate
  71. // them as needed.
  72. }
  73. err = key.Validate()
  74. if err != nil {
  75. return nil, err
  76. }
  77. key.Precompute()
  78. return key, nil
  79. }
  80. // MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form.
  81. func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte {
  82. key.Precompute()
  83. version := 0
  84. if len(key.Primes) > 2 {
  85. version = 1
  86. }
  87. priv := pkcs1PrivateKey{
  88. Version: version,
  89. N: key.N,
  90. E: key.PublicKey.E,
  91. D: key.D,
  92. P: key.Primes[0],
  93. Q: key.Primes[1],
  94. Dp: key.Precomputed.Dp,
  95. Dq: key.Precomputed.Dq,
  96. Qinv: key.Precomputed.Qinv,
  97. }
  98. priv.AdditionalPrimes = make([]pkcs1AdditionalRSAPrime, len(key.Precomputed.CRTValues))
  99. for i, values := range key.Precomputed.CRTValues {
  100. priv.AdditionalPrimes[i].Prime = key.Primes[2+i]
  101. priv.AdditionalPrimes[i].Exp = values.Exp
  102. priv.AdditionalPrimes[i].Coeff = values.Coeff
  103. }
  104. b, _ := asn1.Marshal(priv)
  105. return b
  106. }
  107. // rsaPublicKey reflects the ASN.1 structure of a PKCS#1 public key.
  108. type rsaPublicKey struct {
  109. N *big.Int
  110. E int
  111. }