X509Crl.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.Text;
  5. using Org.BouncyCastle.Asn1;
  6. using Org.BouncyCastle.Asn1.Utilities;
  7. using Org.BouncyCastle.Asn1.X509;
  8. using Org.BouncyCastle.Crypto;
  9. using Org.BouncyCastle.Math;
  10. using Org.BouncyCastle.Security;
  11. using Org.BouncyCastle.Security.Certificates;
  12. using Org.BouncyCastle.Utilities;
  13. using Org.BouncyCastle.Utilities.Collections;
  14. using Org.BouncyCastle.Utilities.Date;
  15. using Org.BouncyCastle.Utilities.Encoders;
  16. using Org.BouncyCastle.X509.Extension;
  17. using Org.BouncyCastle.Crypto.Operators;
  18. namespace Org.BouncyCastle.X509
  19. {
  20. /**
  21. * The following extensions are listed in RFC 2459 as relevant to CRLs
  22. *
  23. * Authority Key Identifier
  24. * Issuer Alternative Name
  25. * CRL Number
  26. * Delta CRL Indicator (critical)
  27. * Issuing Distribution Point (critical)
  28. */
  29. public class X509Crl
  30. : X509ExtensionBase
  31. // TODO Add interface Crl?
  32. {
  33. private readonly CertificateList c;
  34. private readonly string sigAlgName;
  35. private readonly byte[] sigAlgParams;
  36. private readonly bool isIndirect;
  37. public X509Crl(
  38. CertificateList c)
  39. {
  40. this.c = c;
  41. try
  42. {
  43. this.sigAlgName = X509SignatureUtilities.GetSignatureName(c.SignatureAlgorithm);
  44. if (c.SignatureAlgorithm.Parameters != null)
  45. {
  46. this.sigAlgParams = ((Asn1Encodable)c.SignatureAlgorithm.Parameters).GetDerEncoded();
  47. }
  48. else
  49. {
  50. this.sigAlgParams = null;
  51. }
  52. this.isIndirect = IsIndirectCrl;
  53. }
  54. catch (Exception e)
  55. {
  56. throw new CrlException("CRL contents invalid: " + e);
  57. }
  58. }
  59. protected override X509Extensions GetX509Extensions()
  60. {
  61. return c.Version >= 2
  62. ? c.TbsCertList.Extensions
  63. : null;
  64. }
  65. public virtual byte[] GetEncoded()
  66. {
  67. try
  68. {
  69. return c.GetDerEncoded();
  70. }
  71. catch (Exception e)
  72. {
  73. throw new CrlException(e.ToString());
  74. }
  75. }
  76. public virtual void Verify(
  77. AsymmetricKeyParameter publicKey)
  78. {
  79. Verify(new Asn1VerifierFactoryProvider(publicKey));
  80. }
  81. /// <summary>
  82. /// Verify the CRL's signature using a verifier created using the passed in verifier provider.
  83. /// </summary>
  84. /// <param name="verifierProvider">An appropriate provider for verifying the CRL's signature.</param>
  85. /// <returns>True if the signature is valid.</returns>
  86. /// <exception cref="Exception">If verifier provider is not appropriate or the CRL algorithm is invalid.</exception>
  87. public virtual void Verify(
  88. IVerifierFactoryProvider verifierProvider)
  89. {
  90. CheckSignature(verifierProvider.CreateVerifierFactory(c.SignatureAlgorithm));
  91. }
  92. protected virtual void CheckSignature(
  93. IVerifierFactory verifier)
  94. {
  95. if (!c.SignatureAlgorithm.Equals(c.TbsCertList.Signature))
  96. {
  97. throw new CrlException("Signature algorithm on CertificateList does not match TbsCertList.");
  98. }
  99. //Asn1Encodable parameters = c.SignatureAlgorithm.Parameters;
  100. IStreamCalculator streamCalculator = verifier.CreateCalculator();
  101. byte[] b = this.GetTbsCertList();
  102. streamCalculator.Stream.Write(b, 0, b.Length);
  103. Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  104. if (!((IVerifier)streamCalculator.GetResult()).IsVerified(this.GetSignature()))
  105. {
  106. throw new InvalidKeyException("CRL does not verify with supplied public key.");
  107. }
  108. }
  109. public virtual int Version
  110. {
  111. get { return c.Version; }
  112. }
  113. public virtual X509Name IssuerDN
  114. {
  115. get { return c.Issuer; }
  116. }
  117. public virtual DateTime ThisUpdate
  118. {
  119. get { return c.ThisUpdate.ToDateTime(); }
  120. }
  121. public virtual DateTimeObject NextUpdate
  122. {
  123. get
  124. {
  125. return c.NextUpdate == null
  126. ? null
  127. : new DateTimeObject(c.NextUpdate.ToDateTime());
  128. }
  129. }
  130. private ISet LoadCrlEntries()
  131. {
  132. ISet entrySet = new HashSet();
  133. IEnumerable certs = c.GetRevokedCertificateEnumeration();
  134. X509Name previousCertificateIssuer = IssuerDN;
  135. foreach (CrlEntry entry in certs)
  136. {
  137. X509CrlEntry crlEntry = new X509CrlEntry(entry, isIndirect, previousCertificateIssuer);
  138. entrySet.Add(crlEntry);
  139. previousCertificateIssuer = crlEntry.GetCertificateIssuer();
  140. }
  141. return entrySet;
  142. }
  143. public virtual X509CrlEntry GetRevokedCertificate(
  144. BigInteger serialNumber)
  145. {
  146. IEnumerable certs = c.GetRevokedCertificateEnumeration();
  147. X509Name previousCertificateIssuer = IssuerDN;
  148. foreach (CrlEntry entry in certs)
  149. {
  150. X509CrlEntry crlEntry = new X509CrlEntry(entry, isIndirect, previousCertificateIssuer);
  151. if (serialNumber.Equals(entry.UserCertificate.Value))
  152. {
  153. return crlEntry;
  154. }
  155. previousCertificateIssuer = crlEntry.GetCertificateIssuer();
  156. }
  157. return null;
  158. }
  159. public virtual ISet GetRevokedCertificates()
  160. {
  161. ISet entrySet = LoadCrlEntries();
  162. if (entrySet.Count > 0)
  163. {
  164. return entrySet; // TODO? Collections.unmodifiableSet(entrySet);
  165. }
  166. return null;
  167. }
  168. public virtual byte[] GetTbsCertList()
  169. {
  170. try
  171. {
  172. return c.TbsCertList.GetDerEncoded();
  173. }
  174. catch (Exception e)
  175. {
  176. throw new CrlException(e.ToString());
  177. }
  178. }
  179. public virtual byte[] GetSignature()
  180. {
  181. return c.GetSignatureOctets();
  182. }
  183. public virtual string SigAlgName
  184. {
  185. get { return sigAlgName; }
  186. }
  187. public virtual string SigAlgOid
  188. {
  189. get { return c.SignatureAlgorithm.Algorithm.Id; }
  190. }
  191. public virtual byte[] GetSigAlgParams()
  192. {
  193. return Arrays.Clone(sigAlgParams);
  194. }
  195. public override bool Equals(
  196. object obj)
  197. {
  198. if (obj == this)
  199. return true;
  200. X509Crl other = obj as X509Crl;
  201. if (other == null)
  202. return false;
  203. return c.Equals(other.c);
  204. // NB: May prefer this implementation of Equals if more than one certificate implementation in play
  205. //return Arrays.AreEqual(this.GetEncoded(), other.GetEncoded());
  206. }
  207. public override int GetHashCode()
  208. {
  209. return c.GetHashCode();
  210. }
  211. /**
  212. * Returns a string representation of this CRL.
  213. *
  214. * @return a string representation of this CRL.
  215. */
  216. public override string ToString()
  217. {
  218. StringBuilder buf = new StringBuilder();
  219. string nl = Org.BouncyCastle.Utilities.Platform.NewLine;
  220. buf.Append(" Version: ").Append(this.Version).Append(nl);
  221. buf.Append(" IssuerDN: ").Append(this.IssuerDN).Append(nl);
  222. buf.Append(" This update: ").Append(this.ThisUpdate).Append(nl);
  223. buf.Append(" Next update: ").Append(this.NextUpdate).Append(nl);
  224. buf.Append(" Signature Algorithm: ").Append(this.SigAlgName).Append(nl);
  225. byte[] sig = this.GetSignature();
  226. buf.Append(" Signature: ");
  227. buf.Append(Hex.ToHexString(sig, 0, 20)).Append(nl);
  228. for (int i = 20; i < sig.Length; i += 20)
  229. {
  230. int count = System.Math.Min(20, sig.Length - i);
  231. buf.Append(" ");
  232. buf.Append(Hex.ToHexString(sig, i, count)).Append(nl);
  233. }
  234. X509Extensions extensions = c.TbsCertList.Extensions;
  235. if (extensions != null)
  236. {
  237. IEnumerator e = extensions.ExtensionOids.GetEnumerator();
  238. if (e.MoveNext())
  239. {
  240. buf.Append(" Extensions: ").Append(nl);
  241. }
  242. do
  243. {
  244. DerObjectIdentifier oid = (DerObjectIdentifier) e.Current;
  245. X509Extension ext = extensions.GetExtension(oid);
  246. if (ext.Value != null)
  247. {
  248. Asn1Object asn1Value = X509ExtensionUtilities.FromExtensionValue(ext.Value);
  249. buf.Append(" critical(").Append(ext.IsCritical).Append(") ");
  250. try
  251. {
  252. if (oid.Equals(X509Extensions.CrlNumber))
  253. {
  254. buf.Append(new CrlNumber(DerInteger.GetInstance(asn1Value).PositiveValue)).Append(nl);
  255. }
  256. else if (oid.Equals(X509Extensions.DeltaCrlIndicator))
  257. {
  258. buf.Append(
  259. "Base CRL: "
  260. + new CrlNumber(DerInteger.GetInstance(
  261. asn1Value).PositiveValue))
  262. .Append(nl);
  263. }
  264. else if (oid.Equals(X509Extensions.IssuingDistributionPoint))
  265. {
  266. buf.Append(IssuingDistributionPoint.GetInstance((Asn1Sequence) asn1Value)).Append(nl);
  267. }
  268. else if (oid.Equals(X509Extensions.CrlDistributionPoints))
  269. {
  270. buf.Append(CrlDistPoint.GetInstance((Asn1Sequence) asn1Value)).Append(nl);
  271. }
  272. else if (oid.Equals(X509Extensions.FreshestCrl))
  273. {
  274. buf.Append(CrlDistPoint.GetInstance((Asn1Sequence) asn1Value)).Append(nl);
  275. }
  276. else
  277. {
  278. buf.Append(oid.Id);
  279. buf.Append(" value = ").Append(
  280. Asn1Dump.DumpAsString(asn1Value))
  281. .Append(nl);
  282. }
  283. }
  284. catch (Exception)
  285. {
  286. buf.Append(oid.Id);
  287. buf.Append(" value = ").Append("*****").Append(nl);
  288. }
  289. }
  290. else
  291. {
  292. buf.Append(nl);
  293. }
  294. }
  295. while (e.MoveNext());
  296. }
  297. ISet certSet = GetRevokedCertificates();
  298. if (certSet != null)
  299. {
  300. foreach (X509CrlEntry entry in certSet)
  301. {
  302. buf.Append(entry);
  303. buf.Append(nl);
  304. }
  305. }
  306. return buf.ToString();
  307. }
  308. /**
  309. * Checks whether the given certificate is on this CRL.
  310. *
  311. * @param cert the certificate to check for.
  312. * @return true if the given certificate is on this CRL,
  313. * false otherwise.
  314. */
  315. // public bool IsRevoked(
  316. // Certificate cert)
  317. // {
  318. // if (!cert.getType().Equals("X.509"))
  319. // {
  320. // throw new RuntimeException("X.509 CRL used with non X.509 Cert");
  321. // }
  322. public virtual bool IsRevoked(
  323. X509Certificate cert)
  324. {
  325. CrlEntry[] certs = c.GetRevokedCertificates();
  326. if (certs != null)
  327. {
  328. // BigInteger serial = ((X509Certificate)cert).SerialNumber;
  329. BigInteger serial = cert.SerialNumber;
  330. for (int i = 0; i < certs.Length; i++)
  331. {
  332. if (certs[i].UserCertificate.Value.Equals(serial))
  333. {
  334. return true;
  335. }
  336. }
  337. }
  338. return false;
  339. }
  340. protected virtual bool IsIndirectCrl
  341. {
  342. get
  343. {
  344. Asn1OctetString idp = GetExtensionValue(X509Extensions.IssuingDistributionPoint);
  345. bool isIndirect = false;
  346. try
  347. {
  348. if (idp != null)
  349. {
  350. isIndirect = IssuingDistributionPoint.GetInstance(
  351. X509ExtensionUtilities.FromExtensionValue(idp)).IsIndirectCrl;
  352. }
  353. }
  354. catch (Exception e)
  355. {
  356. // TODO
  357. // throw new ExtCrlException("Exception reading IssuingDistributionPoint", e);
  358. throw new CrlException("Exception reading IssuingDistributionPoint" + e);
  359. }
  360. return isIndirect;
  361. }
  362. }
  363. }
  364. }
  365. #endif