AuthorityKeyIdentifier.cs 6.0 KB

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