X509V2CRLGenerator.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.Crypto.Parameters;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  16. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  17. {
  18. /**
  19. * class to produce an X.509 Version 2 CRL.
  20. */
  21. public class X509V2CrlGenerator
  22. {
  23. private readonly X509ExtensionsGenerator extGenerator = new X509ExtensionsGenerator();
  24. private V2TbsCertListGenerator tbsGen;
  25. private DerObjectIdentifier sigOID;
  26. private AlgorithmIdentifier sigAlgId;
  27. private string signatureAlgorithm;
  28. public X509V2CrlGenerator()
  29. {
  30. tbsGen = new V2TbsCertListGenerator();
  31. }
  32. /**
  33. * reset the generator
  34. */
  35. public void Reset()
  36. {
  37. tbsGen = new V2TbsCertListGenerator();
  38. extGenerator.Reset();
  39. }
  40. /**
  41. * Set the issuer distinguished name - the issuer is the entity whose private key is used to sign the
  42. * certificate.
  43. */
  44. public void SetIssuerDN(
  45. X509Name issuer)
  46. {
  47. tbsGen.SetIssuer(issuer);
  48. }
  49. public void SetThisUpdate(
  50. DateTime date)
  51. {
  52. tbsGen.SetThisUpdate(new Time(date));
  53. }
  54. public void SetNextUpdate(
  55. DateTime date)
  56. {
  57. tbsGen.SetNextUpdate(new Time(date));
  58. }
  59. /**
  60. * Reason being as indicated by CrlReason, i.e. CrlReason.KeyCompromise
  61. * or 0 if CrlReason is not to be used
  62. **/
  63. public void AddCrlEntry(
  64. BigInteger userCertificate,
  65. DateTime revocationDate,
  66. int reason)
  67. {
  68. tbsGen.AddCrlEntry(new DerInteger(userCertificate), new Time(revocationDate), reason);
  69. }
  70. /**
  71. * Add a CRL entry with an Invalidity Date extension as well as a CrlReason extension.
  72. * Reason being as indicated by CrlReason, i.e. CrlReason.KeyCompromise
  73. * or 0 if CrlReason is not to be used
  74. **/
  75. public void AddCrlEntry(
  76. BigInteger userCertificate,
  77. DateTime revocationDate,
  78. int reason,
  79. DateTime invalidityDate)
  80. {
  81. tbsGen.AddCrlEntry(new DerInteger(userCertificate), new Time(revocationDate), reason, new DerGeneralizedTime(invalidityDate));
  82. }
  83. /**
  84. * Add a CRL entry with extensions.
  85. **/
  86. public void AddCrlEntry(
  87. BigInteger userCertificate,
  88. DateTime revocationDate,
  89. X509Extensions extensions)
  90. {
  91. tbsGen.AddCrlEntry(new DerInteger(userCertificate), new Time(revocationDate), extensions);
  92. }
  93. /**
  94. * Add the CRLEntry objects contained in a previous CRL.
  95. *
  96. * @param other the X509Crl to source the other entries from.
  97. */
  98. public void AddCrl(
  99. X509Crl other)
  100. {
  101. if (other == null)
  102. throw new ArgumentNullException("other");
  103. ISet revocations = other.GetRevokedCertificates();
  104. if (revocations != null)
  105. {
  106. foreach (X509CrlEntry entry in revocations)
  107. {
  108. try
  109. {
  110. tbsGen.AddCrlEntry(
  111. Asn1Sequence.GetInstance(
  112. Asn1Object.FromByteArray(entry.GetEncoded())));
  113. }
  114. catch (IOException e)
  115. {
  116. throw new CrlException("exception processing encoding of CRL", e);
  117. }
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// Set the signature algorithm that will be used to sign this CRL.
  123. /// </summary>
  124. /// <param name="signatureAlgorithm"/>
  125. public void SetSignatureAlgorithm(
  126. string signatureAlgorithm)
  127. {
  128. this.signatureAlgorithm = signatureAlgorithm;
  129. try
  130. {
  131. sigOID = X509Utilities.GetAlgorithmOid(signatureAlgorithm);
  132. }
  133. catch (Exception e)
  134. {
  135. throw new ArgumentException("Unknown signature type requested", e);
  136. }
  137. sigAlgId = X509Utilities.GetSigAlgID(sigOID, signatureAlgorithm);
  138. tbsGen.SetSignature(sigAlgId);
  139. }
  140. /**
  141. * add a given extension field for the standard extensions tag (tag 0)
  142. */
  143. public void AddExtension(
  144. string oid,
  145. bool critical,
  146. Asn1Encodable extensionValue)
  147. {
  148. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, extensionValue);
  149. }
  150. /**
  151. * add a given extension field for the standard extensions tag (tag 0)
  152. */
  153. public void AddExtension(
  154. DerObjectIdentifier oid,
  155. bool critical,
  156. Asn1Encodable extensionValue)
  157. {
  158. extGenerator.AddExtension(oid, critical, extensionValue);
  159. }
  160. /**
  161. * add a given extension field for the standard extensions tag (tag 0)
  162. */
  163. public void AddExtension(
  164. string oid,
  165. bool critical,
  166. byte[] extensionValue)
  167. {
  168. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, new DerOctetString(extensionValue));
  169. }
  170. /**
  171. * add a given extension field for the standard extensions tag (tag 0)
  172. */
  173. public void AddExtension(
  174. DerObjectIdentifier oid,
  175. bool critical,
  176. byte[] extensionValue)
  177. {
  178. extGenerator.AddExtension(oid, critical, new DerOctetString(extensionValue));
  179. }
  180. /// <summary>
  181. /// Generate an X.509 CRL, based on the current issuer and subject.
  182. /// </summary>
  183. /// <param name="privateKey">The private key of the issuer that is signing this certificate.</param>
  184. /// <returns>An X509Crl.</returns>
  185. public X509Crl Generate(
  186. AsymmetricKeyParameter privateKey)
  187. {
  188. return Generate(privateKey, null);
  189. }
  190. /// <summary>
  191. /// Generate an X.509 CRL, based on the current issuer and subject using the specified secure random.
  192. /// </summary>
  193. /// <param name="privateKey">The private key of the issuer that is signing this certificate.</param>
  194. /// <param name="random">Your Secure Random instance.</param>
  195. /// <returns>An X509Crl.</returns>
  196. public X509Crl Generate(
  197. AsymmetricKeyParameter privateKey,
  198. SecureRandom random)
  199. {
  200. return Generate(new Asn1SignatureFactory(signatureAlgorithm, privateKey, random));
  201. }
  202. /// <summary>
  203. /// Generate a new X509Crl using the passed in SignatureCalculator.
  204. /// </summary>
  205. /// <param name="signatureCalculatorFactory">A signature calculator factory with the necessary algorithm details.</param>
  206. /// <returns>An X509Crl.</returns>
  207. public X509Crl Generate(ISignatureFactory signatureCalculatorFactory)
  208. {
  209. tbsGen.SetSignature((AlgorithmIdentifier)signatureCalculatorFactory.AlgorithmDetails);
  210. TbsCertificateList tbsCertList = GenerateCertList();
  211. IStreamCalculator streamCalculator = signatureCalculatorFactory.CreateCalculator();
  212. byte[] encoded = tbsCertList.GetDerEncoded();
  213. streamCalculator.Stream.Write(encoded, 0, encoded.Length);
  214. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  215. return GenerateJcaObject(tbsCertList, (AlgorithmIdentifier)signatureCalculatorFactory.AlgorithmDetails, ((IBlockResult)streamCalculator.GetResult()).Collect());
  216. }
  217. private TbsCertificateList GenerateCertList()
  218. {
  219. if (!extGenerator.IsEmpty)
  220. {
  221. tbsGen.SetExtensions(extGenerator.Generate());
  222. }
  223. return tbsGen.GenerateTbsCertList();
  224. }
  225. private X509Crl GenerateJcaObject(
  226. TbsCertificateList tbsCrl,
  227. AlgorithmIdentifier algId,
  228. byte[] signature)
  229. {
  230. return new X509Crl(
  231. CertificateList.GetInstance(
  232. new DerSequence(tbsCrl, algId, new DerBitString(signature))));
  233. }
  234. /// <summary>
  235. /// Allows enumeration of the signature names supported by the generator.
  236. /// </summary>
  237. public IEnumerable SignatureAlgNames
  238. {
  239. get { return X509Utilities.GetAlgNames(); }
  240. }
  241. }
  242. }
  243. #pragma warning restore
  244. #endif