AuthorityKeyIdentifier.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  10. {
  11. /**
  12. * The AuthorityKeyIdentifier object.
  13. * <pre>
  14. * id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 }
  15. *
  16. * AuthorityKeyIdentifier ::= Sequence {
  17. * keyIdentifier [0] IMPLICIT KeyIdentifier OPTIONAL,
  18. * authorityCertIssuer [1] IMPLICIT GeneralNames OPTIONAL,
  19. * authorityCertSerialNumber [2] IMPLICIT CertificateSerialNumber OPTIONAL }
  20. *
  21. * KeyIdentifier ::= OCTET STRING
  22. * </pre>
  23. *
  24. */
  25. public class AuthorityKeyIdentifier
  26. : Asn1Encodable
  27. {
  28. internal readonly Asn1OctetString keyidentifier;
  29. internal readonly GeneralNames certissuer;
  30. internal readonly DerInteger certserno;
  31. public static AuthorityKeyIdentifier GetInstance(
  32. Asn1TaggedObject obj,
  33. bool explicitly)
  34. {
  35. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  36. }
  37. public static AuthorityKeyIdentifier GetInstance(
  38. object obj)
  39. {
  40. if (obj is AuthorityKeyIdentifier)
  41. {
  42. return (AuthorityKeyIdentifier) obj;
  43. }
  44. if (obj is Asn1Sequence)
  45. {
  46. return new AuthorityKeyIdentifier((Asn1Sequence) obj);
  47. }
  48. if (obj is X509Extension)
  49. {
  50. return GetInstance(X509Extension.ConvertValueToObject((X509Extension) obj));
  51. }
  52. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  53. }
  54. protected internal AuthorityKeyIdentifier(
  55. Asn1Sequence seq)
  56. {
  57. foreach (Asn1TaggedObject o in seq)
  58. {
  59. switch (o.TagNo)
  60. {
  61. case 0:
  62. this.keyidentifier = Asn1OctetString.GetInstance(o, false);
  63. break;
  64. case 1:
  65. this.certissuer = GeneralNames.GetInstance(o, false);
  66. break;
  67. case 2:
  68. this.certserno = DerInteger.GetInstance(o, false);
  69. break;
  70. default:
  71. throw new ArgumentException("illegal tag");
  72. }
  73. }
  74. }
  75. /**
  76. *
  77. * Calulates the keyidentifier using a SHA1 hash over the BIT STRING
  78. * from SubjectPublicKeyInfo as defined in RFC2459.
  79. *
  80. * Example of making a AuthorityKeyIdentifier:
  81. * <pre>
  82. * SubjectPublicKeyInfo apki = new SubjectPublicKeyInfo((ASN1Sequence)new ASN1InputStream(
  83. * publicKey.getEncoded()).readObject());
  84. * AuthorityKeyIdentifier aki = new AuthorityKeyIdentifier(apki);
  85. * </pre>
  86. *
  87. **/
  88. public AuthorityKeyIdentifier(
  89. SubjectPublicKeyInfo spki)
  90. {
  91. IDigest digest = new Sha1Digest();
  92. byte[] resBuf = new byte[digest.GetDigestSize()];
  93. byte[] bytes = spki.PublicKeyData.GetBytes();
  94. digest.BlockUpdate(bytes, 0, bytes.Length);
  95. digest.DoFinal(resBuf, 0);
  96. this.keyidentifier = new DerOctetString(resBuf);
  97. }
  98. /**
  99. * create an AuthorityKeyIdentifier with the GeneralNames tag and
  100. * the serial number provided as well.
  101. */
  102. public AuthorityKeyIdentifier(
  103. SubjectPublicKeyInfo spki,
  104. GeneralNames name,
  105. BigInteger serialNumber)
  106. {
  107. IDigest digest = new Sha1Digest();
  108. byte[] resBuf = new byte[digest.GetDigestSize()];
  109. byte[] bytes = spki.PublicKeyData.GetBytes();
  110. digest.BlockUpdate(bytes, 0, bytes.Length);
  111. digest.DoFinal(resBuf, 0);
  112. this.keyidentifier = new DerOctetString(resBuf);
  113. this.certissuer = name;
  114. this.certserno = new DerInteger(serialNumber);
  115. }
  116. /**
  117. * create an AuthorityKeyIdentifier with the GeneralNames tag and
  118. * the serial number provided.
  119. */
  120. public AuthorityKeyIdentifier(
  121. GeneralNames name,
  122. BigInteger serialNumber)
  123. {
  124. this.keyidentifier = null;
  125. this.certissuer = GeneralNames.GetInstance(name.ToAsn1Object());
  126. this.certserno = new DerInteger(serialNumber);
  127. }
  128. /**
  129. * create an AuthorityKeyIdentifier with a precomputed key identifier
  130. */
  131. public AuthorityKeyIdentifier(
  132. byte[] keyIdentifier)
  133. {
  134. this.keyidentifier = new DerOctetString(keyIdentifier);
  135. this.certissuer = null;
  136. this.certserno = null;
  137. }
  138. /**
  139. * create an AuthorityKeyIdentifier with a precomupted key identifier
  140. * and the GeneralNames tag and the serial number provided as well.
  141. */
  142. public AuthorityKeyIdentifier(
  143. byte[] keyIdentifier,
  144. GeneralNames name,
  145. BigInteger serialNumber)
  146. {
  147. this.keyidentifier = new DerOctetString(keyIdentifier);
  148. this.certissuer = GeneralNames.GetInstance(name.ToAsn1Object());
  149. this.certserno = new DerInteger(serialNumber);
  150. }
  151. public byte[] GetKeyIdentifier()
  152. {
  153. return keyidentifier == null ? null : keyidentifier.GetOctets();
  154. }
  155. public GeneralNames AuthorityCertIssuer
  156. {
  157. get { return certissuer; }
  158. }
  159. public BigInteger AuthorityCertSerialNumber
  160. {
  161. get { return certserno == null ? null : certserno.Value; }
  162. }
  163. /**
  164. * Produce an object suitable for an Asn1OutputStream.
  165. */
  166. public override Asn1Object ToAsn1Object()
  167. {
  168. Asn1EncodableVector v = new Asn1EncodableVector();
  169. if (keyidentifier != null)
  170. {
  171. v.Add(new DerTaggedObject(false, 0, keyidentifier));
  172. }
  173. if (certissuer != null)
  174. {
  175. v.Add(new DerTaggedObject(false, 1, certissuer));
  176. }
  177. if (certserno != null)
  178. {
  179. v.Add(new DerTaggedObject(false, 2, certserno));
  180. }
  181. return new DerSequence(v);
  182. }
  183. public override string ToString()
  184. {
  185. return ("AuthorityKeyIdentifier: KeyID(" + this.keyidentifier.GetOctets() + ")");
  186. }
  187. }
  188. }
  189. #pragma warning restore
  190. #endif