ECPrivateKeyStructure.cs 4.8 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. public ECPrivateKeyStructure(
  25. Asn1Sequence seq)
  26. {
  27. if (seq == null)
  28. throw new ArgumentNullException("seq");
  29. this.seq = seq;
  30. }
  31. public ECPrivateKeyStructure(
  32. BigInteger key)
  33. {
  34. if (key == null)
  35. throw new ArgumentNullException("key");
  36. this.seq = new DerSequence(
  37. new DerInteger(1),
  38. new DerOctetString(key.ToByteArrayUnsigned()));
  39. }
  40. public ECPrivateKeyStructure(
  41. int orderBitLength,
  42. BigInteger key)
  43. : this(orderBitLength, key, null)
  44. {
  45. }
  46. public ECPrivateKeyStructure(
  47. BigInteger key,
  48. Asn1Encodable parameters)
  49. : this(key, null, parameters)
  50. {
  51. }
  52. public ECPrivateKeyStructure(
  53. BigInteger key,
  54. DerBitString publicKey,
  55. Asn1Encodable parameters)
  56. {
  57. if (key == null)
  58. throw new ArgumentNullException("key");
  59. Asn1EncodableVector v = new Asn1EncodableVector(
  60. new DerInteger(1),
  61. new DerOctetString(key.ToByteArrayUnsigned()));
  62. if (parameters != null)
  63. {
  64. v.Add(new DerTaggedObject(true, 0, parameters));
  65. }
  66. if (publicKey != null)
  67. {
  68. v.Add(new DerTaggedObject(true, 1, publicKey));
  69. }
  70. this.seq = new DerSequence(v);
  71. }
  72. public ECPrivateKeyStructure(
  73. int orderBitLength,
  74. BigInteger key,
  75. Asn1Encodable parameters)
  76. : this(orderBitLength, key, null, parameters)
  77. {
  78. }
  79. public ECPrivateKeyStructure(
  80. int orderBitLength,
  81. BigInteger key,
  82. DerBitString publicKey,
  83. Asn1Encodable parameters)
  84. {
  85. if (key == null)
  86. throw new ArgumentNullException("key");
  87. if (orderBitLength < key.BitLength)
  88. throw new ArgumentException("must be >= key bitlength", "orderBitLength");
  89. byte[] bytes = BigIntegers.AsUnsignedByteArray((orderBitLength + 7) / 8, key);
  90. Asn1EncodableVector v = new Asn1EncodableVector(
  91. new DerInteger(1),
  92. new DerOctetString(bytes));
  93. if (parameters != null)
  94. {
  95. v.Add(new DerTaggedObject(true, 0, parameters));
  96. }
  97. if (publicKey != null)
  98. {
  99. v.Add(new DerTaggedObject(true, 1, publicKey));
  100. }
  101. this.seq = new DerSequence(v);
  102. }
  103. public virtual BigInteger GetKey()
  104. {
  105. Asn1OctetString octs = (Asn1OctetString) seq[1];
  106. return new BigInteger(1, octs.GetOctets());
  107. }
  108. public virtual DerBitString GetPublicKey()
  109. {
  110. return (DerBitString) GetObjectInTag(1);
  111. }
  112. public virtual Asn1Object GetParameters()
  113. {
  114. return GetObjectInTag(0);
  115. }
  116. private Asn1Object GetObjectInTag(int tagNo)
  117. {
  118. foreach (Asn1Encodable ae in seq)
  119. {
  120. Asn1Object obj = ae.ToAsn1Object();
  121. if (obj is Asn1TaggedObject)
  122. {
  123. Asn1TaggedObject tag = (Asn1TaggedObject) obj;
  124. if (tag.TagNo == tagNo)
  125. {
  126. return tag.GetObject();
  127. }
  128. }
  129. }
  130. return null;
  131. }
  132. /**
  133. * ECPrivateKey ::= SEQUENCE {
  134. * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  135. * privateKey OCTET STRING,
  136. * parameters [0] Parameters OPTIONAL,
  137. * publicKey [1] BIT STRING OPTIONAL }
  138. */
  139. public override Asn1Object ToAsn1Object()
  140. {
  141. return seq;
  142. }
  143. }
  144. }
  145. #pragma warning restore
  146. #endif