RSAPrivateKeyStructure.cs 4.7 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. public RsaPrivateKeyStructure(
  52. Asn1Sequence seq)
  53. {
  54. BigInteger version = ((DerInteger)seq[0]).Value;
  55. if (version.IntValue != 0)
  56. throw new ArgumentException("wrong version for RSA private key");
  57. modulus = ((DerInteger)seq[1]).Value;
  58. publicExponent = ((DerInteger)seq[2]).Value;
  59. privateExponent = ((DerInteger)seq[3]).Value;
  60. prime1 = ((DerInteger)seq[4]).Value;
  61. prime2 = ((DerInteger)seq[5]).Value;
  62. exponent1 = ((DerInteger)seq[6]).Value;
  63. exponent2 = ((DerInteger)seq[7]).Value;
  64. coefficient = ((DerInteger)seq[8]).Value;
  65. }
  66. public BigInteger Modulus
  67. {
  68. get { return modulus; }
  69. }
  70. public BigInteger PublicExponent
  71. {
  72. get { return publicExponent; }
  73. }
  74. public BigInteger PrivateExponent
  75. {
  76. get { return privateExponent; }
  77. }
  78. public BigInteger Prime1
  79. {
  80. get { return prime1; }
  81. }
  82. public BigInteger Prime2
  83. {
  84. get { return prime2; }
  85. }
  86. public BigInteger Exponent1
  87. {
  88. get { return exponent1; }
  89. }
  90. public BigInteger Exponent2
  91. {
  92. get { return exponent2; }
  93. }
  94. public BigInteger Coefficient
  95. {
  96. get { return coefficient; }
  97. }
  98. /**
  99. * This outputs the key in Pkcs1v2 format.
  100. * <pre>
  101. * RsaPrivateKey ::= Sequence {
  102. * version Version,
  103. * modulus Integer, -- n
  104. * publicExponent Integer, -- e
  105. * privateExponent Integer, -- d
  106. * prime1 Integer, -- p
  107. * prime2 Integer, -- q
  108. * exponent1 Integer, -- d mod (p-1)
  109. * exponent2 Integer, -- d mod (q-1)
  110. * coefficient Integer -- (inverse of q) mod p
  111. * }
  112. *
  113. * Version ::= Integer
  114. * </pre>
  115. * <p>This routine is written to output Pkcs1 version 0, private keys.</p>
  116. */
  117. public override Asn1Object ToAsn1Object()
  118. {
  119. return new DerSequence(
  120. new DerInteger(0), // version
  121. new DerInteger(Modulus),
  122. new DerInteger(PublicExponent),
  123. new DerInteger(PrivateExponent),
  124. new DerInteger(Prime1),
  125. new DerInteger(Prime2),
  126. new DerInteger(Exponent1),
  127. new DerInteger(Exponent2),
  128. new DerInteger(Coefficient));
  129. }
  130. }
  131. }
  132. #pragma warning restore
  133. #endif