PrivateKeyInfo.cs 6.8 KB

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