ECPrivateKeyStructure.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Sec
  8. {
  9. /**
  10. * the elliptic curve private key object from SEC 1
  11. */
  12. public class ECPrivateKeyStructure
  13. : Asn1Encodable
  14. {
  15. private readonly Asn1Sequence seq;
  16. public static ECPrivateKeyStructure GetInstance(object obj)
  17. {
  18. if (obj == null)
  19. return null;
  20. if (obj is ECPrivateKeyStructure)
  21. return (ECPrivateKeyStructure)obj;
  22. return new ECPrivateKeyStructure(Asn1Sequence.GetInstance(obj));
  23. }
  24. [Obsolete("Use 'GetInstance' instead")]
  25. public ECPrivateKeyStructure(
  26. Asn1Sequence seq)
  27. {
  28. if (seq == null)
  29. throw new ArgumentNullException("seq");
  30. this.seq = seq;
  31. }
  32. [Obsolete("Use constructor which takes 'orderBitLength' instead, to guarantee correct encoding")]
  33. public ECPrivateKeyStructure(
  34. BigInteger key)
  35. {
  36. if (key == null)
  37. throw new ArgumentNullException("key");
  38. this.seq = new DerSequence(
  39. new DerInteger(1),
  40. new DerOctetString(key.ToByteArrayUnsigned()));
  41. }
  42. public ECPrivateKeyStructure(
  43. int orderBitLength,
  44. BigInteger key)
  45. : this(orderBitLength, key, null)
  46. {
  47. }
  48. [Obsolete("Use constructor which takes 'orderBitLength' instead, to guarantee correct encoding")]
  49. public ECPrivateKeyStructure(
  50. BigInteger key,
  51. Asn1Encodable parameters)
  52. : this(key, null, parameters)
  53. {
  54. }
  55. [Obsolete("Use constructor which takes 'orderBitLength' instead, to guarantee correct encoding")]
  56. public ECPrivateKeyStructure(
  57. BigInteger key,
  58. DerBitString publicKey,
  59. Asn1Encodable parameters)
  60. {
  61. if (key == null)
  62. throw new ArgumentNullException("key");
  63. Asn1EncodableVector v = new Asn1EncodableVector(
  64. new DerInteger(1),
  65. new DerOctetString(key.ToByteArrayUnsigned()));
  66. if (parameters != null)
  67. {
  68. v.Add(new DerTaggedObject(true, 0, parameters));
  69. }
  70. if (publicKey != null)
  71. {
  72. v.Add(new DerTaggedObject(true, 1, publicKey));
  73. }
  74. this.seq = new DerSequence(v);
  75. }
  76. public ECPrivateKeyStructure(
  77. int orderBitLength,
  78. BigInteger key,
  79. Asn1Encodable parameters)
  80. : this(orderBitLength, key, null, parameters)
  81. {
  82. }
  83. public ECPrivateKeyStructure(
  84. int orderBitLength,
  85. BigInteger key,
  86. DerBitString publicKey,
  87. Asn1Encodable parameters)
  88. {
  89. if (key == null)
  90. throw new ArgumentNullException("key");
  91. if (orderBitLength < key.BitLength)
  92. throw new ArgumentException("must be >= key bitlength", "orderBitLength");
  93. byte[] bytes = BigIntegers.AsUnsignedByteArray((orderBitLength + 7) / 8, key);
  94. Asn1EncodableVector v = new Asn1EncodableVector(
  95. new DerInteger(1),
  96. new DerOctetString(bytes));
  97. if (parameters != null)
  98. {
  99. v.Add(new DerTaggedObject(true, 0, parameters));
  100. }
  101. if (publicKey != null)
  102. {
  103. v.Add(new DerTaggedObject(true, 1, publicKey));
  104. }
  105. this.seq = new DerSequence(v);
  106. }
  107. public virtual BigInteger GetKey()
  108. {
  109. Asn1OctetString octs = (Asn1OctetString) seq[1];
  110. return new BigInteger(1, octs.GetOctets());
  111. }
  112. public virtual DerBitString GetPublicKey()
  113. {
  114. return (DerBitString) GetObjectInTag(1);
  115. }
  116. public virtual Asn1Object GetParameters()
  117. {
  118. return GetObjectInTag(0);
  119. }
  120. private Asn1Object GetObjectInTag(int tagNo)
  121. {
  122. foreach (Asn1Encodable ae in seq)
  123. {
  124. Asn1Object obj = ae.ToAsn1Object();
  125. if (obj is Asn1TaggedObject)
  126. {
  127. Asn1TaggedObject tag = (Asn1TaggedObject) obj;
  128. if (tag.TagNo == tagNo)
  129. {
  130. return tag.GetObject();
  131. }
  132. }
  133. }
  134. return null;
  135. }
  136. /**
  137. * ECPrivateKey ::= SEQUENCE {
  138. * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  139. * privateKey OCTET STRING,
  140. * parameters [0] Parameters OPTIONAL,
  141. * publicKey [1] BIT STRING OPTIONAL }
  142. */
  143. public override Asn1Object ToAsn1Object()
  144. {
  145. return seq;
  146. }
  147. }
  148. }
  149. #pragma warning restore
  150. #endif