PrivateKeyInfo.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs
  10. {
  11. /**
  12. * RFC 5958
  13. *
  14. * <pre>
  15. * [IMPLICIT TAGS]
  16. *
  17. * OneAsymmetricKey ::= SEQUENCE {
  18. * version Version,
  19. * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
  20. * privateKey PrivateKey,
  21. * attributes [0] Attributes OPTIONAL,
  22. * ...,
  23. * [[2: publicKey [1] PublicKey OPTIONAL ]],
  24. * ...
  25. * }
  26. *
  27. * PrivateKeyInfo ::= OneAsymmetricKey
  28. *
  29. * Version ::= INTEGER { v1(0), v2(1) } (v1, ..., v2)
  30. *
  31. * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
  32. * { PUBLIC-KEY,
  33. * { PrivateKeyAlgorithms } }
  34. *
  35. * PrivateKey ::= OCTET STRING
  36. * -- Content varies based on type of key. The
  37. * -- algorithm identifier dictates the format of
  38. * -- the key.
  39. *
  40. * PublicKey ::= BIT STRING
  41. * -- Content varies based on type of key. The
  42. * -- algorithm identifier dictates the format of
  43. * -- the key.
  44. *
  45. * Attributes ::= SET OF Attribute { { OneAsymmetricKeyAttributes } }
  46. * </pre>
  47. */
  48. public class PrivateKeyInfo
  49. : Asn1Encodable
  50. {
  51. private readonly DerInteger version;
  52. private readonly AlgorithmIdentifier privateKeyAlgorithm;
  53. private readonly Asn1OctetString privateKey;
  54. private readonly Asn1Set attributes;
  55. private readonly DerBitString publicKey;
  56. public static PrivateKeyInfo GetInstance(Asn1TaggedObject obj, bool explicitly)
  57. {
  58. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  59. }
  60. public static PrivateKeyInfo GetInstance(
  61. object obj)
  62. {
  63. if (obj == null)
  64. return null;
  65. if (obj is PrivateKeyInfo)
  66. return (PrivateKeyInfo)obj;
  67. return new PrivateKeyInfo(Asn1Sequence.GetInstance(obj));
  68. }
  69. private static int GetVersionValue(DerInteger version)
  70. {
  71. BigInteger bigValue = version.Value;
  72. if (bigValue.CompareTo(BigInteger.Zero) < 0 || bigValue.CompareTo(BigInteger.One) > 0)
  73. throw new ArgumentException("invalid version for private key info", "version");
  74. return bigValue.IntValue;
  75. }
  76. public PrivateKeyInfo(
  77. AlgorithmIdentifier privateKeyAlgorithm,
  78. Asn1Encodable privateKey)
  79. : this(privateKeyAlgorithm, privateKey, null, null)
  80. {
  81. }
  82. public PrivateKeyInfo(
  83. AlgorithmIdentifier privateKeyAlgorithm,
  84. Asn1Encodable privateKey,
  85. Asn1Set attributes)
  86. : this(privateKeyAlgorithm, privateKey, attributes, null)
  87. {
  88. }
  89. public PrivateKeyInfo(
  90. AlgorithmIdentifier privateKeyAlgorithm,
  91. Asn1Encodable privateKey,
  92. Asn1Set attributes,
  93. byte[] publicKey)
  94. {
  95. this.version = new DerInteger(publicKey != null ? BigInteger.One : BigInteger.Zero);
  96. this.privateKeyAlgorithm = privateKeyAlgorithm;
  97. this.privateKey = new DerOctetString(privateKey);
  98. this.attributes = attributes;
  99. this.publicKey = publicKey == null ? null : new DerBitString(publicKey);
  100. }
  101. private PrivateKeyInfo(Asn1Sequence seq)
  102. {
  103. IEnumerator e = seq.GetEnumerator();
  104. this.version = DerInteger.GetInstance(CollectionUtilities.RequireNext(e));
  105. int versionValue = GetVersionValue(version);
  106. this.privateKeyAlgorithm = AlgorithmIdentifier.GetInstance(CollectionUtilities.RequireNext(e));
  107. this.privateKey = Asn1OctetString.GetInstance(CollectionUtilities.RequireNext(e));
  108. int lastTag = -1;
  109. while (e.MoveNext())
  110. {
  111. Asn1TaggedObject tagged = (Asn1TaggedObject)e.Current;
  112. int tag = tagged.TagNo;
  113. if (tag <= lastTag)
  114. throw new ArgumentException("invalid optional field in private key info", "seq");
  115. lastTag = tag;
  116. switch (tag)
  117. {
  118. case 0:
  119. {
  120. this.attributes = Asn1Set.GetInstance(tagged, false);
  121. break;
  122. }
  123. case 1:
  124. {
  125. if (versionValue < 1)
  126. throw new ArgumentException("'publicKey' requires version v2(1) or later", "seq");
  127. this.publicKey = DerBitString.GetInstance(tagged, false);
  128. break;
  129. }
  130. default:
  131. {
  132. throw new ArgumentException("unknown optional field in private key info", "seq");
  133. }
  134. }
  135. }
  136. }
  137. public virtual DerInteger Version
  138. {
  139. get { return version; }
  140. }
  141. public virtual Asn1Set Attributes
  142. {
  143. get { return attributes; }
  144. }
  145. /// <summary>Return true if a public key is present, false otherwise.</summary>
  146. public virtual bool HasPublicKey
  147. {
  148. get { return publicKey != null; }
  149. }
  150. public virtual AlgorithmIdentifier PrivateKeyAlgorithm
  151. {
  152. get { return privateKeyAlgorithm; }
  153. }
  154. public virtual Asn1OctetString PrivateKeyData
  155. {
  156. get { return privateKey; }
  157. }
  158. public virtual Asn1Object ParsePrivateKey()
  159. {
  160. return Asn1Object.FromByteArray(privateKey.GetOctets());
  161. }
  162. /// <summary>For when the public key is an ASN.1 encoding.</summary>
  163. public virtual Asn1Object ParsePublicKey()
  164. {
  165. return publicKey == null ? null : Asn1Object.FromByteArray(publicKey.GetOctets());
  166. }
  167. /// <summary>Return the public key as a raw bit string.</summary>
  168. public virtual DerBitString PublicKeyData
  169. {
  170. get { return publicKey; }
  171. }
  172. public override Asn1Object ToAsn1Object()
  173. {
  174. Asn1EncodableVector v = new Asn1EncodableVector(version, privateKeyAlgorithm, privateKey);
  175. v.AddOptionalTagged(false, 0, attributes);
  176. v.AddOptionalTagged(false, 1, publicKey);
  177. return new DerSequence(v);
  178. }
  179. }
  180. }
  181. #pragma warning restore
  182. #endif