X509V2CRLGenerator.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  12. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.X509
  13. {
  14. /**
  15. * class to produce an X.509 Version 2 CRL.
  16. */
  17. public class X509V2CrlGenerator
  18. {
  19. private readonly X509ExtensionsGenerator extGenerator = new X509ExtensionsGenerator();
  20. private V2TbsCertListGenerator tbsGen;
  21. public X509V2CrlGenerator()
  22. {
  23. tbsGen = new V2TbsCertListGenerator();
  24. }
  25. /**
  26. * reset the generator
  27. */
  28. public void Reset()
  29. {
  30. tbsGen = new V2TbsCertListGenerator();
  31. extGenerator.Reset();
  32. }
  33. /**
  34. * Set the issuer distinguished name - the issuer is the entity whose private key is used to sign the
  35. * certificate.
  36. */
  37. public void SetIssuerDN(
  38. X509Name issuer)
  39. {
  40. tbsGen.SetIssuer(issuer);
  41. }
  42. public void SetThisUpdate(
  43. DateTime date)
  44. {
  45. tbsGen.SetThisUpdate(new Time(date));
  46. }
  47. public void SetNextUpdate(
  48. DateTime date)
  49. {
  50. tbsGen.SetNextUpdate(new Time(date));
  51. }
  52. /**
  53. * Reason being as indicated by CrlReason, i.e. CrlReason.KeyCompromise
  54. * or 0 if CrlReason is not to be used
  55. **/
  56. public void AddCrlEntry(
  57. BigInteger userCertificate,
  58. DateTime revocationDate,
  59. int reason)
  60. {
  61. tbsGen.AddCrlEntry(new DerInteger(userCertificate), new Time(revocationDate), reason);
  62. }
  63. /**
  64. * Add a CRL entry with an Invalidity Date extension as well as a CrlReason extension.
  65. * Reason being as indicated by CrlReason, i.e. CrlReason.KeyCompromise
  66. * or 0 if CrlReason is not to be used
  67. **/
  68. public void AddCrlEntry(
  69. BigInteger userCertificate,
  70. DateTime revocationDate,
  71. int reason,
  72. DateTime invalidityDate)
  73. {
  74. tbsGen.AddCrlEntry(new DerInteger(userCertificate), new Time(revocationDate), reason,
  75. new Asn1GeneralizedTime(invalidityDate));
  76. }
  77. /**
  78. * Add a CRL entry with extensions.
  79. **/
  80. public void AddCrlEntry(
  81. BigInteger userCertificate,
  82. DateTime revocationDate,
  83. X509Extensions extensions)
  84. {
  85. tbsGen.AddCrlEntry(new DerInteger(userCertificate), new Time(revocationDate), extensions);
  86. }
  87. /**
  88. * Add the CRLEntry objects contained in a previous CRL.
  89. *
  90. * @param other the X509Crl to source the other entries from.
  91. */
  92. public void AddCrl(X509Crl other)
  93. {
  94. if (other == null)
  95. throw new ArgumentNullException("other");
  96. var revocations = other.GetRevokedCertificates();
  97. if (revocations != null)
  98. {
  99. foreach (X509CrlEntry entry in revocations)
  100. {
  101. try
  102. {
  103. tbsGen.AddCrlEntry(
  104. Asn1Sequence.GetInstance(
  105. Asn1Object.FromByteArray(entry.GetEncoded())));
  106. }
  107. catch (IOException e)
  108. {
  109. throw new CrlException("exception processing encoding of CRL", e);
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * add a given extension field for the standard extensions tag (tag 0)
  116. */
  117. public void AddExtension(
  118. string oid,
  119. bool critical,
  120. Asn1Encodable extensionValue)
  121. {
  122. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, extensionValue);
  123. }
  124. /**
  125. * add a given extension field for the standard extensions tag (tag 0)
  126. */
  127. public void AddExtension(
  128. DerObjectIdentifier oid,
  129. bool critical,
  130. Asn1Encodable extensionValue)
  131. {
  132. extGenerator.AddExtension(oid, critical, extensionValue);
  133. }
  134. /**
  135. * add a given extension field for the standard extensions tag (tag 0)
  136. */
  137. public void AddExtension(
  138. string oid,
  139. bool critical,
  140. byte[] extensionValue)
  141. {
  142. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, new DerOctetString(extensionValue));
  143. }
  144. /**
  145. * add a given extension field for the standard extensions tag (tag 0)
  146. */
  147. public void AddExtension(
  148. DerObjectIdentifier oid,
  149. bool critical,
  150. byte[] extensionValue)
  151. {
  152. extGenerator.AddExtension(oid, critical, new DerOctetString(extensionValue));
  153. }
  154. /// <summary>
  155. /// Generate a new <see cref="X509Crl"/> using the provided <see cref="ISignatureFactory"/>.
  156. /// </summary>
  157. /// <param name="signatureFactory">A <see cref="ISignatureFactory">signature factory</see> with the necessary
  158. /// algorithm details.</param>
  159. /// <returns>An <see cref="X509Crl"/>.</returns>
  160. public X509Crl Generate(ISignatureFactory signatureFactory)
  161. {
  162. var sigAlgID = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails;
  163. tbsGen.SetSignature(sigAlgID);
  164. if (!extGenerator.IsEmpty)
  165. {
  166. tbsGen.SetExtensions(extGenerator.Generate());
  167. }
  168. TbsCertificateList tbsCertList = tbsGen.GenerateTbsCertList();
  169. IStreamCalculator<IBlockResult> streamCalculator = signatureFactory.CreateCalculator();
  170. using (Stream sigStream = streamCalculator.Stream)
  171. {
  172. tbsCertList.EncodeTo(sigStream, Asn1Encodable.Der);
  173. }
  174. var signature = streamCalculator.GetResult().Collect();
  175. return new X509Crl(
  176. CertificateList.GetInstance(new DerSequence(tbsCertList, sigAlgID, new DerBitString(signature))));
  177. }
  178. /// <summary>
  179. /// Allows enumeration of the signature names supported by the generator.
  180. /// </summary>
  181. public IEnumerable<string> SignatureAlgNames
  182. {
  183. get { return X509Utilities.GetAlgNames(); }
  184. }
  185. }
  186. }
  187. #pragma warning restore
  188. #endif