ECPrivateKeyStructure.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Sec
  7. {
  8. /**
  9. * the elliptic curve private key object from SEC 1
  10. */
  11. public class ECPrivateKeyStructure
  12. : Asn1Encodable
  13. {
  14. private readonly Asn1Sequence m_seq;
  15. public static ECPrivateKeyStructure GetInstance(object obj)
  16. {
  17. if (obj == null)
  18. return null;
  19. if (obj is ECPrivateKeyStructure ecPrivateKeyStructure)
  20. return ecPrivateKeyStructure;
  21. return new ECPrivateKeyStructure(Asn1Sequence.GetInstance(obj));
  22. }
  23. private ECPrivateKeyStructure(Asn1Sequence seq)
  24. {
  25. m_seq = seq ?? throw new ArgumentNullException(nameof(seq));
  26. }
  27. public ECPrivateKeyStructure(
  28. int orderBitLength,
  29. BigInteger key)
  30. : this(orderBitLength, key, null)
  31. {
  32. }
  33. public ECPrivateKeyStructure(
  34. int orderBitLength,
  35. BigInteger key,
  36. Asn1Encodable parameters)
  37. : this(orderBitLength, key, null, parameters)
  38. {
  39. }
  40. public ECPrivateKeyStructure(
  41. int orderBitLength,
  42. BigInteger key,
  43. DerBitString publicKey,
  44. Asn1Encodable parameters)
  45. {
  46. if (key == null)
  47. throw new ArgumentNullException(nameof(key));
  48. if (orderBitLength < key.BitLength)
  49. throw new ArgumentException("must be >= key bitlength", nameof(orderBitLength));
  50. byte[] bytes = BigIntegers.AsUnsignedByteArray((orderBitLength + 7) / 8, key);
  51. Asn1EncodableVector v = new Asn1EncodableVector(
  52. new DerInteger(1),
  53. new DerOctetString(bytes));
  54. v.AddOptionalTagged(true, 0, parameters);
  55. v.AddOptionalTagged(true, 1, publicKey);
  56. m_seq = new DerSequence(v);
  57. }
  58. public virtual BigInteger GetKey()
  59. {
  60. Asn1OctetString octs = (Asn1OctetString)m_seq[1];
  61. return new BigInteger(1, octs.GetOctets());
  62. }
  63. public virtual DerBitString GetPublicKey()
  64. {
  65. return (DerBitString)GetObjectInTag(1, Asn1Tags.BitString);
  66. }
  67. public virtual Asn1Object GetParameters()
  68. {
  69. return GetObjectInTag(0, -1);
  70. }
  71. private Asn1Object GetObjectInTag(int tagNo, int baseTagNo)
  72. {
  73. foreach (Asn1Encodable ae in m_seq)
  74. {
  75. Asn1Object obj = ae.ToAsn1Object();
  76. if (obj is Asn1TaggedObject tag)
  77. {
  78. if (tag.HasContextTag(tagNo))
  79. {
  80. return baseTagNo < 0
  81. ? tag.GetExplicitBaseObject().ToAsn1Object()
  82. : tag.GetBaseUniversal(true, baseTagNo);
  83. }
  84. }
  85. }
  86. return null;
  87. }
  88. /**
  89. * ECPrivateKey ::= SEQUENCE {
  90. * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  91. * privateKey OCTET STRING,
  92. * parameters [0] Parameters OPTIONAL,
  93. * publicKey [1] BIT STRING OPTIONAL }
  94. */
  95. public override Asn1Object ToAsn1Object()
  96. {
  97. return m_seq;
  98. }
  99. }
  100. }
  101. #pragma warning restore
  102. #endif