X509V2CRLGenerator.cs 8.0 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. [Obsolete("Not needed if Generate used with an ISignatureFactory")]
  126. public void SetSignatureAlgorithm(
  127. string signatureAlgorithm)
  128. {
  129. this.signatureAlgorithm = signatureAlgorithm;
  130. try
  131. {
  132. sigOID = X509Utilities.GetAlgorithmOid(signatureAlgorithm);
  133. }
  134. catch (Exception e)
  135. {
  136. throw new ArgumentException("Unknown signature type requested", e);
  137. }
  138. sigAlgId = X509Utilities.GetSigAlgID(sigOID, signatureAlgorithm);
  139. tbsGen.SetSignature(sigAlgId);
  140. }
  141. /**
  142. * add a given extension field for the standard extensions tag (tag 0)
  143. */
  144. public void AddExtension(
  145. string oid,
  146. bool critical,
  147. Asn1Encodable extensionValue)
  148. {
  149. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, extensionValue);
  150. }
  151. /**
  152. * add a given extension field for the standard extensions tag (tag 0)
  153. */
  154. public void AddExtension(
  155. DerObjectIdentifier oid,
  156. bool critical,
  157. Asn1Encodable extensionValue)
  158. {
  159. extGenerator.AddExtension(oid, critical, extensionValue);
  160. }
  161. /**
  162. * add a given extension field for the standard extensions tag (tag 0)
  163. */
  164. public void AddExtension(
  165. string oid,
  166. bool critical,
  167. byte[] extensionValue)
  168. {
  169. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, new DerOctetString(extensionValue));
  170. }
  171. /**
  172. * add a given extension field for the standard extensions tag (tag 0)
  173. */
  174. public void AddExtension(
  175. DerObjectIdentifier oid,
  176. bool critical,
  177. byte[] extensionValue)
  178. {
  179. extGenerator.AddExtension(oid, critical, new DerOctetString(extensionValue));
  180. }
  181. /// <summary>
  182. /// Generate an X.509 CRL, based on the current issuer and subject.
  183. /// </summary>
  184. /// <param name="privateKey">The private key of the issuer that is signing this certificate.</param>
  185. /// <returns>An X509Crl.</returns>
  186. [Obsolete("Use Generate with an ISignatureFactory")]
  187. public X509Crl Generate(
  188. AsymmetricKeyParameter privateKey)
  189. {
  190. return Generate(privateKey, null);
  191. }
  192. /// <summary>
  193. /// Generate an X.509 CRL, based on the current issuer and subject using the specified secure random.
  194. /// </summary>
  195. /// <param name="privateKey">The private key of the issuer that is signing this certificate.</param>
  196. /// <param name="random">Your Secure Random instance.</param>
  197. /// <returns>An X509Crl.</returns>
  198. [Obsolete("Use Generate with an ISignatureFactory")]
  199. public X509Crl Generate(
  200. AsymmetricKeyParameter privateKey,
  201. SecureRandom random)
  202. {
  203. return Generate(new Asn1SignatureFactory(signatureAlgorithm, privateKey, random));
  204. }
  205. /// <summary>
  206. /// Generate a new X509Crl using the passed in SignatureCalculator.
  207. /// </summary>
  208. /// <param name="signatureCalculatorFactory">A signature calculator factory with the necessary algorithm details.</param>
  209. /// <returns>An X509Crl.</returns>
  210. public X509Crl Generate(ISignatureFactory signatureCalculatorFactory)
  211. {
  212. tbsGen.SetSignature((AlgorithmIdentifier)signatureCalculatorFactory.AlgorithmDetails);
  213. TbsCertificateList tbsCertList = GenerateCertList();
  214. IStreamCalculator streamCalculator = signatureCalculatorFactory.CreateCalculator();
  215. byte[] encoded = tbsCertList.GetDerEncoded();
  216. streamCalculator.Stream.Write(encoded, 0, encoded.Length);
  217. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  218. return GenerateJcaObject(tbsCertList, (AlgorithmIdentifier)signatureCalculatorFactory.AlgorithmDetails, ((IBlockResult)streamCalculator.GetResult()).Collect());
  219. }
  220. private TbsCertificateList GenerateCertList()
  221. {
  222. if (!extGenerator.IsEmpty)
  223. {
  224. tbsGen.SetExtensions(extGenerator.Generate());
  225. }
  226. return tbsGen.GenerateTbsCertList();
  227. }
  228. private X509Crl GenerateJcaObject(
  229. TbsCertificateList tbsCrl,
  230. AlgorithmIdentifier algId,
  231. byte[] signature)
  232. {
  233. return new X509Crl(
  234. CertificateList.GetInstance(
  235. new DerSequence(tbsCrl, algId, new DerBitString(signature))));
  236. }
  237. /// <summary>
  238. /// Allows enumeration of the signature names supported by the generator.
  239. /// </summary>
  240. public IEnumerable SignatureAlgNames
  241. {
  242. get { return X509Utilities.GetAlgNames(); }
  243. }
  244. }
  245. }
  246. #pragma warning restore
  247. #endif