RSAPrivateKeyStructure.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs
  6. {
  7. public class RsaPrivateKeyStructure
  8. : Asn1Encodable
  9. {
  10. private readonly BigInteger modulus;
  11. private readonly BigInteger publicExponent;
  12. private readonly BigInteger privateExponent;
  13. private readonly BigInteger prime1;
  14. private readonly BigInteger prime2;
  15. private readonly BigInteger exponent1;
  16. private readonly BigInteger exponent2;
  17. private readonly BigInteger coefficient;
  18. public static RsaPrivateKeyStructure GetInstance(Asn1TaggedObject obj, bool isExplicit)
  19. {
  20. return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
  21. }
  22. public static RsaPrivateKeyStructure GetInstance(object obj)
  23. {
  24. if (obj == null)
  25. return null;
  26. if (obj is RsaPrivateKeyStructure)
  27. return (RsaPrivateKeyStructure)obj;
  28. return new RsaPrivateKeyStructure(Asn1Sequence.GetInstance(obj));
  29. }
  30. public RsaPrivateKeyStructure(
  31. BigInteger modulus,
  32. BigInteger publicExponent,
  33. BigInteger privateExponent,
  34. BigInteger prime1,
  35. BigInteger prime2,
  36. BigInteger exponent1,
  37. BigInteger exponent2,
  38. BigInteger coefficient)
  39. {
  40. this.modulus = modulus;
  41. this.publicExponent = publicExponent;
  42. this.privateExponent = privateExponent;
  43. this.prime1 = prime1;
  44. this.prime2 = prime2;
  45. this.exponent1 = exponent1;
  46. this.exponent2 = exponent2;
  47. this.coefficient = coefficient;
  48. }
  49. private RsaPrivateKeyStructure(Asn1Sequence seq)
  50. {
  51. BigInteger version = ((DerInteger)seq[0]).Value;
  52. if (version.IntValue != 0)
  53. throw new ArgumentException("wrong version for RSA private key");
  54. modulus = ((DerInteger)seq[1]).Value;
  55. publicExponent = ((DerInteger)seq[2]).Value;
  56. privateExponent = ((DerInteger)seq[3]).Value;
  57. prime1 = ((DerInteger)seq[4]).Value;
  58. prime2 = ((DerInteger)seq[5]).Value;
  59. exponent1 = ((DerInteger)seq[6]).Value;
  60. exponent2 = ((DerInteger)seq[7]).Value;
  61. coefficient = ((DerInteger)seq[8]).Value;
  62. }
  63. public BigInteger Modulus
  64. {
  65. get { return modulus; }
  66. }
  67. public BigInteger PublicExponent
  68. {
  69. get { return publicExponent; }
  70. }
  71. public BigInteger PrivateExponent
  72. {
  73. get { return privateExponent; }
  74. }
  75. public BigInteger Prime1
  76. {
  77. get { return prime1; }
  78. }
  79. public BigInteger Prime2
  80. {
  81. get { return prime2; }
  82. }
  83. public BigInteger Exponent1
  84. {
  85. get { return exponent1; }
  86. }
  87. public BigInteger Exponent2
  88. {
  89. get { return exponent2; }
  90. }
  91. public BigInteger Coefficient
  92. {
  93. get { return coefficient; }
  94. }
  95. /**
  96. * This outputs the key in Pkcs1v2 format.
  97. * <pre>
  98. * RsaPrivateKey ::= Sequence {
  99. * version Version,
  100. * modulus Integer, -- n
  101. * publicExponent Integer, -- e
  102. * privateExponent Integer, -- d
  103. * prime1 Integer, -- p
  104. * prime2 Integer, -- q
  105. * exponent1 Integer, -- d mod (p-1)
  106. * exponent2 Integer, -- d mod (q-1)
  107. * coefficient Integer -- (inverse of q) mod p
  108. * }
  109. *
  110. * Version ::= Integer
  111. * </pre>
  112. * <p>This routine is written to output Pkcs1 version 0, private keys.</p>
  113. */
  114. public override Asn1Object ToAsn1Object()
  115. {
  116. return new DerSequence(
  117. new DerInteger(0), // version
  118. new DerInteger(Modulus),
  119. new DerInteger(PublicExponent),
  120. new DerInteger(PrivateExponent),
  121. new DerInteger(Prime1),
  122. new DerInteger(Prime2),
  123. new DerInteger(Exponent1),
  124. new DerInteger(Exponent2),
  125. new DerInteger(Coefficient));
  126. }
  127. }
  128. }
  129. #pragma warning restore
  130. #endif