X509V1CertificateGenerator.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using System.Collections;
  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.Parameters;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators;
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  16. {
  17. /// <summary>
  18. /// Class to Generate X509V1 Certificates.
  19. /// </summary>
  20. public class X509V1CertificateGenerator
  21. {
  22. private V1TbsCertificateGenerator tbsGen;
  23. private DerObjectIdentifier sigOID;
  24. private AlgorithmIdentifier sigAlgId;
  25. private string signatureAlgorithm;
  26. /// <summary>
  27. /// Default Constructor.
  28. /// </summary>
  29. public X509V1CertificateGenerator()
  30. {
  31. tbsGen = new V1TbsCertificateGenerator();
  32. }
  33. /// <summary>
  34. /// Reset the generator.
  35. /// </summary>
  36. public void Reset()
  37. {
  38. tbsGen = new V1TbsCertificateGenerator();
  39. }
  40. /// <summary>
  41. /// Set the certificate's serial number.
  42. /// </summary>
  43. /// <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.
  44. /// You will be surprised how ugly a serial number collision can get.</remarks>
  45. /// <param name="serialNumber">The serial number.</param>
  46. public void SetSerialNumber(
  47. BigInteger serialNumber)
  48. {
  49. if (serialNumber.SignValue <= 0)
  50. {
  51. throw new ArgumentException("serial number must be a positive integer", "serialNumber");
  52. }
  53. tbsGen.SetSerialNumber(new DerInteger(serialNumber));
  54. }
  55. /// <summary>
  56. /// Set the issuer distinguished name.
  57. /// The issuer is the entity whose private key is used to sign the certificate.
  58. /// </summary>
  59. /// <param name="issuer">The issuers DN.</param>
  60. public void SetIssuerDN(
  61. X509Name issuer)
  62. {
  63. tbsGen.SetIssuer(issuer);
  64. }
  65. /// <summary>
  66. /// Set the date that this certificate is to be valid from.
  67. /// </summary>
  68. /// <param name="date"/>
  69. public void SetNotBefore(
  70. DateTime date)
  71. {
  72. tbsGen.SetStartDate(new Time(date));
  73. }
  74. /// <summary>
  75. /// Set the date after which this certificate will no longer be valid.
  76. /// </summary>
  77. /// <param name="date"/>
  78. public void SetNotAfter(
  79. DateTime date)
  80. {
  81. tbsGen.SetEndDate(new Time(date));
  82. }
  83. /// <summary>
  84. /// Set the subject distinguished name.
  85. /// The subject describes the entity associated with the public key.
  86. /// </summary>
  87. /// <param name="subject"/>
  88. public void SetSubjectDN(
  89. X509Name subject)
  90. {
  91. tbsGen.SetSubject(subject);
  92. }
  93. /// <summary>
  94. /// Set the public key that this certificate identifies.
  95. /// </summary>
  96. /// <param name="publicKey"/>
  97. public void SetPublicKey(
  98. AsymmetricKeyParameter publicKey)
  99. {
  100. try
  101. {
  102. tbsGen.SetSubjectPublicKeyInfo(
  103. SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey));
  104. }
  105. catch (Exception e)
  106. {
  107. throw new ArgumentException("unable to process key - " + e.ToString());
  108. }
  109. }
  110. /// <summary>
  111. /// Set the signature algorithm that will be used to sign this certificate.
  112. /// This can be either a name or an OID, names are treated as case insensitive.
  113. /// </summary>
  114. /// <param name="signatureAlgorithm">string representation of the algorithm name</param>
  115. public void SetSignatureAlgorithm(
  116. string signatureAlgorithm)
  117. {
  118. this.signatureAlgorithm = signatureAlgorithm;
  119. try
  120. {
  121. sigOID = X509Utilities.GetAlgorithmOid(signatureAlgorithm);
  122. }
  123. catch (Exception)
  124. {
  125. throw new ArgumentException("Unknown signature type requested", "signatureAlgorithm");
  126. }
  127. sigAlgId = X509Utilities.GetSigAlgID(sigOID, signatureAlgorithm);
  128. tbsGen.SetSignature(sigAlgId);
  129. }
  130. /// <summary>
  131. /// Generate a new X509Certificate.
  132. /// </summary>
  133. /// <param name="privateKey">The private key of the issuer used to sign this certificate.</param>
  134. /// <returns>An X509Certificate.</returns>
  135. public X509Certificate Generate(
  136. AsymmetricKeyParameter privateKey)
  137. {
  138. return Generate(privateKey, null);
  139. }
  140. /// <summary>
  141. /// Generate a new X509Certificate specifying a SecureRandom instance that you would like to use.
  142. /// </summary>
  143. /// <param name="privateKey">The private key of the issuer used to sign this certificate.</param>
  144. /// <param name="random">The Secure Random you want to use.</param>
  145. /// <returns>An X509Certificate.</returns>
  146. public X509Certificate Generate(
  147. AsymmetricKeyParameter privateKey,
  148. SecureRandom random)
  149. {
  150. return Generate(new Asn1SignatureFactory(signatureAlgorithm, privateKey, random));
  151. }
  152. /// <summary>
  153. /// Generate a new X509Certificate using the passed in SignatureCalculator.
  154. /// </summary>
  155. /// <param name="signatureCalculatorFactory">A signature calculator factory with the necessary algorithm details.</param>
  156. /// <returns>An X509Certificate.</returns>
  157. public X509Certificate Generate(ISignatureFactory signatureCalculatorFactory)
  158. {
  159. tbsGen.SetSignature ((AlgorithmIdentifier)signatureCalculatorFactory.AlgorithmDetails);
  160. TbsCertificateStructure tbsCert = tbsGen.GenerateTbsCertificate();
  161. IStreamCalculator streamCalculator = signatureCalculatorFactory.CreateCalculator();
  162. byte[] encoded = tbsCert.GetDerEncoded();
  163. streamCalculator.Stream.Write(encoded, 0, encoded.Length);
  164. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  165. return GenerateJcaObject(tbsCert, (AlgorithmIdentifier)signatureCalculatorFactory.AlgorithmDetails, ((IBlockResult)streamCalculator.GetResult()).Collect());
  166. }
  167. private X509Certificate GenerateJcaObject(
  168. TbsCertificateStructure tbsCert,
  169. AlgorithmIdentifier sigAlg,
  170. byte[] signature)
  171. {
  172. return new X509Certificate(
  173. new X509CertificateStructure(tbsCert, sigAlg, new DerBitString(signature)));
  174. }
  175. /// <summary>
  176. /// Allows enumeration of the signature names supported by the generator.
  177. /// </summary>
  178. public IEnumerable SignatureAlgNames
  179. {
  180. get { return X509Utilities.GetAlgNames(); }
  181. }
  182. }
  183. }
  184. #pragma warning restore
  185. #endif