X509V1CertificateGenerator.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.Utilities;
  11. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.X509
  12. {
  13. /// <summary>
  14. /// Class to Generate X509V1 Certificates.
  15. /// </summary>
  16. public class X509V1CertificateGenerator
  17. {
  18. private V1TbsCertificateGenerator tbsGen;
  19. /// <summary>
  20. /// Default Constructor.
  21. /// </summary>
  22. public X509V1CertificateGenerator()
  23. {
  24. tbsGen = new V1TbsCertificateGenerator();
  25. }
  26. /// <summary>
  27. /// Reset the generator.
  28. /// </summary>
  29. public void Reset()
  30. {
  31. tbsGen = new V1TbsCertificateGenerator();
  32. }
  33. /// <summary>
  34. /// Set the certificate's serial number.
  35. /// </summary>
  36. /// <remarks>Make serial numbers long, if you have no serial number policy make sure the number is at least 16 bytes of secure random data.
  37. /// You will be surprised how ugly a serial number collision can get.</remarks>
  38. /// <param name="serialNumber">The serial number.</param>
  39. public void SetSerialNumber(
  40. BigInteger serialNumber)
  41. {
  42. if (serialNumber.SignValue <= 0)
  43. {
  44. throw new ArgumentException("serial number must be a positive integer", "serialNumber");
  45. }
  46. tbsGen.SetSerialNumber(new DerInteger(serialNumber));
  47. }
  48. /// <summary>
  49. /// Set the issuer distinguished name.
  50. /// The issuer is the entity whose private key is used to sign the certificate.
  51. /// </summary>
  52. /// <param name="issuer">The issuers DN.</param>
  53. public void SetIssuerDN(
  54. X509Name issuer)
  55. {
  56. tbsGen.SetIssuer(issuer);
  57. }
  58. /// <summary>
  59. /// Set the date that this certificate is to be valid from.
  60. /// </summary>
  61. /// <param name="date"/>
  62. public void SetNotBefore(
  63. DateTime date)
  64. {
  65. tbsGen.SetStartDate(new Time(date));
  66. }
  67. /// <summary>
  68. /// Set the date after which this certificate will no longer be valid.
  69. /// </summary>
  70. /// <param name="date"/>
  71. public void SetNotAfter(
  72. DateTime date)
  73. {
  74. tbsGen.SetEndDate(new Time(date));
  75. }
  76. /// <summary>
  77. /// Set the subject distinguished name.
  78. /// The subject describes the entity associated with the public key.
  79. /// </summary>
  80. /// <param name="subject"/>
  81. public void SetSubjectDN(
  82. X509Name subject)
  83. {
  84. tbsGen.SetSubject(subject);
  85. }
  86. /// <summary>
  87. /// Set the public key that this certificate identifies.
  88. /// </summary>
  89. /// <param name="publicKey"/>
  90. public void SetPublicKey(
  91. AsymmetricKeyParameter publicKey)
  92. {
  93. try
  94. {
  95. tbsGen.SetSubjectPublicKeyInfo(
  96. SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey));
  97. }
  98. catch (Exception e)
  99. {
  100. throw new ArgumentException("unable to process key - " + e.ToString());
  101. }
  102. }
  103. /// <summary>
  104. /// Generate a new <see cref="X509Certificate"/> using the provided <see cref="ISignatureFactory"/>.
  105. /// </summary>
  106. /// <param name="signatureFactory">A <see cref="ISignatureFactory">signature factory</see> with the necessary
  107. /// algorithm details.</param>
  108. /// <returns>An <see cref="X509Certificate"/>.</returns>
  109. public X509Certificate Generate(ISignatureFactory signatureFactory)
  110. {
  111. var sigAlgID = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails;
  112. tbsGen.SetSignature(sigAlgID);
  113. TbsCertificateStructure tbsCert = tbsGen.GenerateTbsCertificate();
  114. IStreamCalculator<IBlockResult> streamCalculator = signatureFactory.CreateCalculator();
  115. using (Stream sigStream = streamCalculator.Stream)
  116. {
  117. tbsCert.EncodeTo(sigStream, Asn1Encodable.Der);
  118. }
  119. var signature = streamCalculator.GetResult().Collect();
  120. return new X509Certificate(
  121. new X509CertificateStructure(tbsCert, sigAlgID, new DerBitString(signature)));
  122. }
  123. /// <summary>
  124. /// Allows enumeration of the signature names supported by the generator.
  125. /// </summary>
  126. public IEnumerable<string> SignatureAlgNames
  127. {
  128. get { return X509Utilities.GetAlgNames(); }
  129. }
  130. }
  131. }
  132. #pragma warning restore
  133. #endif