X509V2AttributeCertificate.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  14. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  15. {
  16. /// <summary>An implementation of a version 2 X.509 Attribute Certificate.</summary>
  17. public class X509V2AttributeCertificate
  18. : X509ExtensionBase, IX509AttributeCertificate
  19. {
  20. private readonly AttributeCertificate cert;
  21. private readonly DateTime notBefore;
  22. private readonly DateTime notAfter;
  23. private static AttributeCertificate GetObject(Stream input)
  24. {
  25. try
  26. {
  27. return AttributeCertificate.GetInstance(Asn1Object.FromStream(input));
  28. }
  29. catch (IOException e)
  30. {
  31. throw e;
  32. }
  33. catch (Exception e)
  34. {
  35. throw new IOException("exception decoding certificate structure", e);
  36. }
  37. }
  38. public X509V2AttributeCertificate(
  39. Stream encIn)
  40. : this(GetObject(encIn))
  41. {
  42. }
  43. public X509V2AttributeCertificate(
  44. byte[] encoded)
  45. : this(new MemoryStream(encoded, false))
  46. {
  47. }
  48. internal X509V2AttributeCertificate(
  49. AttributeCertificate cert)
  50. {
  51. this.cert = cert;
  52. try
  53. {
  54. this.notAfter = cert.ACInfo.AttrCertValidityPeriod.NotAfterTime.ToDateTime();
  55. this.notBefore = cert.ACInfo.AttrCertValidityPeriod.NotBeforeTime.ToDateTime();
  56. }
  57. catch (Exception e)
  58. {
  59. throw new IOException("invalid data structure in certificate!", e);
  60. }
  61. }
  62. public virtual int Version
  63. {
  64. get { return cert.ACInfo.Version.IntValueExact + 1; }
  65. }
  66. public virtual BigInteger SerialNumber
  67. {
  68. get { return cert.ACInfo.SerialNumber.Value; }
  69. }
  70. public virtual AttributeCertificateHolder Holder
  71. {
  72. get
  73. {
  74. return new AttributeCertificateHolder((Asn1Sequence)cert.ACInfo.Holder.ToAsn1Object());
  75. }
  76. }
  77. public virtual AttributeCertificateIssuer Issuer
  78. {
  79. get
  80. {
  81. return new AttributeCertificateIssuer(cert.ACInfo.Issuer);
  82. }
  83. }
  84. public virtual DateTime NotBefore
  85. {
  86. get { return notBefore; }
  87. }
  88. public virtual DateTime NotAfter
  89. {
  90. get { return notAfter; }
  91. }
  92. public virtual bool[] GetIssuerUniqueID()
  93. {
  94. DerBitString id = cert.ACInfo.IssuerUniqueID;
  95. if (id != null)
  96. {
  97. byte[] bytes = id.GetBytes();
  98. bool[] boolId = new bool[bytes.Length * 8 - id.PadBits];
  99. for (int i = 0; i != boolId.Length; i++)
  100. {
  101. //boolId[i] = (bytes[i / 8] & (0x80 >>> (i % 8))) != 0;
  102. boolId[i] = (bytes[i / 8] & (0x80 >> (i % 8))) != 0;
  103. }
  104. return boolId;
  105. }
  106. return null;
  107. }
  108. public virtual bool IsValidNow
  109. {
  110. get { return IsValid(DateTime.UtcNow); }
  111. }
  112. public virtual bool IsValid(
  113. DateTime date)
  114. {
  115. return date.CompareTo(NotBefore) >= 0 && date.CompareTo(NotAfter) <= 0;
  116. }
  117. public virtual void CheckValidity()
  118. {
  119. this.CheckValidity(DateTime.UtcNow);
  120. }
  121. public virtual void CheckValidity(
  122. DateTime date)
  123. {
  124. if (date.CompareTo(NotAfter) > 0)
  125. throw new CertificateExpiredException("certificate expired on " + NotAfter);
  126. if (date.CompareTo(NotBefore) < 0)
  127. throw new CertificateNotYetValidException("certificate not valid until " + NotBefore);
  128. }
  129. public virtual AlgorithmIdentifier SignatureAlgorithm
  130. {
  131. get { return cert.SignatureAlgorithm; }
  132. }
  133. public virtual byte[] GetSignature()
  134. {
  135. return cert.GetSignatureOctets();
  136. }
  137. public virtual void Verify(
  138. AsymmetricKeyParameter key)
  139. {
  140. CheckSignature(new Asn1VerifierFactory(cert.SignatureAlgorithm, key));
  141. }
  142. /// <summary>
  143. /// Verify the certificate's signature using a verifier created using the passed in verifier provider.
  144. /// </summary>
  145. /// <param name="verifierProvider">An appropriate provider for verifying the certificate's signature.</param>
  146. /// <returns>True if the signature is valid.</returns>
  147. /// <exception cref="Exception">If verifier provider is not appropriate or the certificate algorithm is invalid.</exception>
  148. public virtual void Verify(
  149. IVerifierFactoryProvider verifierProvider)
  150. {
  151. CheckSignature(verifierProvider.CreateVerifierFactory(cert.SignatureAlgorithm));
  152. }
  153. protected virtual void CheckSignature(
  154. IVerifierFactory verifier)
  155. {
  156. if (!cert.SignatureAlgorithm.Equals(cert.ACInfo.Signature))
  157. {
  158. throw new CertificateException("Signature algorithm in certificate info not same as outer certificate");
  159. }
  160. IStreamCalculator streamCalculator = verifier.CreateCalculator();
  161. try
  162. {
  163. byte[] b = this.cert.ACInfo.GetEncoded();
  164. streamCalculator.Stream.Write(b, 0, b.Length);
  165. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  166. }
  167. catch (IOException e)
  168. {
  169. throw new SignatureException("Exception encoding certificate info object", e);
  170. }
  171. if (!((IVerifier)streamCalculator.GetResult()).IsVerified(this.GetSignature()))
  172. {
  173. throw new InvalidKeyException("Public key presented not for certificate signature");
  174. }
  175. }
  176. public virtual byte[] GetEncoded()
  177. {
  178. return cert.GetEncoded();
  179. }
  180. protected override X509Extensions GetX509Extensions()
  181. {
  182. return cert.ACInfo.Extensions;
  183. }
  184. public virtual X509Attribute[] GetAttributes()
  185. {
  186. Asn1Sequence seq = cert.ACInfo.Attributes;
  187. X509Attribute[] attrs = new X509Attribute[seq.Count];
  188. for (int i = 0; i != seq.Count; i++)
  189. {
  190. attrs[i] = new X509Attribute((Asn1Encodable)seq[i]);
  191. }
  192. return attrs;
  193. }
  194. public virtual X509Attribute[] GetAttributes(
  195. string oid)
  196. {
  197. Asn1Sequence seq = cert.ACInfo.Attributes;
  198. IList list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  199. for (int i = 0; i != seq.Count; i++)
  200. {
  201. X509Attribute attr = new X509Attribute((Asn1Encodable)seq[i]);
  202. if (attr.Oid.Equals(oid))
  203. {
  204. list.Add(attr);
  205. }
  206. }
  207. if (list.Count < 1)
  208. {
  209. return null;
  210. }
  211. X509Attribute[] result = new X509Attribute[list.Count];
  212. for (int i = 0; i < list.Count; ++i)
  213. {
  214. result[i] = (X509Attribute)list[i];
  215. }
  216. return result;
  217. }
  218. public override bool Equals(
  219. object obj)
  220. {
  221. if (obj == this)
  222. return true;
  223. X509V2AttributeCertificate other = obj as X509V2AttributeCertificate;
  224. if (other == null)
  225. return false;
  226. return cert.Equals(other.cert);
  227. // NB: May prefer this implementation of Equals if more than one certificate implementation in play
  228. //return Arrays.AreEqual(this.GetEncoded(), other.GetEncoded());
  229. }
  230. public override int GetHashCode()
  231. {
  232. return cert.GetHashCode();
  233. }
  234. }
  235. }
  236. #pragma warning restore
  237. #endif