RSAPrivateKeyStructure.cs 4.8 KB

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