AuthorityKeyIdentifier.cs 5.9 KB

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