X509V2AttributeCertificateGenerator.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.Math;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  14. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  15. {
  16. /// <remarks>Class to produce an X.509 Version 2 AttributeCertificate.</remarks>
  17. public class X509V2AttributeCertificateGenerator
  18. {
  19. private readonly X509ExtensionsGenerator extGenerator = new X509ExtensionsGenerator();
  20. private V2AttributeCertificateInfoGenerator acInfoGen;
  21. private DerObjectIdentifier sigOID;
  22. private AlgorithmIdentifier sigAlgId;
  23. private string signatureAlgorithm;
  24. public X509V2AttributeCertificateGenerator()
  25. {
  26. acInfoGen = new V2AttributeCertificateInfoGenerator();
  27. }
  28. /// <summary>Reset the generator</summary>
  29. public void Reset()
  30. {
  31. acInfoGen = new V2AttributeCertificateInfoGenerator();
  32. extGenerator.Reset();
  33. }
  34. /// <summary>Set the Holder of this Attribute Certificate.</summary>
  35. public void SetHolder(
  36. AttributeCertificateHolder holder)
  37. {
  38. acInfoGen.SetHolder(holder.holder);
  39. }
  40. /// <summary>Set the issuer.</summary>
  41. public void SetIssuer(
  42. AttributeCertificateIssuer issuer)
  43. {
  44. acInfoGen.SetIssuer(AttCertIssuer.GetInstance(issuer.form));
  45. }
  46. /// <summary>Set the serial number for the certificate.</summary>
  47. public void SetSerialNumber(
  48. BigInteger serialNumber)
  49. {
  50. acInfoGen.SetSerialNumber(new DerInteger(serialNumber));
  51. }
  52. public void SetNotBefore(
  53. DateTime date)
  54. {
  55. acInfoGen.SetStartDate(new DerGeneralizedTime(date));
  56. }
  57. public void SetNotAfter(
  58. DateTime date)
  59. {
  60. acInfoGen.SetEndDate(new DerGeneralizedTime(date));
  61. }
  62. /// <summary>
  63. /// Set the signature algorithm. This can be either a name or an OID, names
  64. /// are treated as case insensitive.
  65. /// </summary>
  66. /// <param name="signatureAlgorithm">The algorithm name.</param>
  67. [Obsolete("Not needed if Generate used with an ISignatureFactory")]
  68. public void SetSignatureAlgorithm(
  69. string signatureAlgorithm)
  70. {
  71. this.signatureAlgorithm = signatureAlgorithm;
  72. try
  73. {
  74. sigOID = X509Utilities.GetAlgorithmOid(signatureAlgorithm);
  75. }
  76. catch (Exception)
  77. {
  78. throw new ArgumentException("Unknown signature type requested");
  79. }
  80. sigAlgId = X509Utilities.GetSigAlgID(sigOID, signatureAlgorithm);
  81. acInfoGen.SetSignature(sigAlgId);
  82. }
  83. /// <summary>Add an attribute.</summary>
  84. public void AddAttribute(
  85. X509Attribute attribute)
  86. {
  87. acInfoGen.AddAttribute(AttributeX509.GetInstance(attribute.ToAsn1Object()));
  88. }
  89. public void SetIssuerUniqueId(
  90. bool[] iui)
  91. {
  92. // TODO convert bool array to bit string
  93. //acInfoGen.SetIssuerUniqueID(iui);
  94. throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("SetIssuerUniqueId()");
  95. }
  96. /// <summary>Add a given extension field for the standard extensions tag.</summary>
  97. public void AddExtension(
  98. string oid,
  99. bool critical,
  100. Asn1Encodable extensionValue)
  101. {
  102. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, extensionValue);
  103. }
  104. /// <summary>
  105. /// Add a given extension field for the standard extensions tag.
  106. /// The value parameter becomes the contents of the octet string associated
  107. /// with the extension.
  108. /// </summary>
  109. public void AddExtension(
  110. string oid,
  111. bool critical,
  112. byte[] extensionValue)
  113. {
  114. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, extensionValue);
  115. }
  116. /// <summary>
  117. /// Generate an X509 certificate, based on the current issuer and subject.
  118. /// </summary>
  119. [Obsolete("Use Generate with an ISignatureFactory")]
  120. public IX509AttributeCertificate Generate(
  121. AsymmetricKeyParameter privateKey)
  122. {
  123. return Generate(privateKey, null);
  124. }
  125. /// <summary>
  126. /// Generate an X509 certificate, based on the current issuer and subject,
  127. /// using the supplied source of randomness, if required.
  128. /// </summary>
  129. [Obsolete("Use Generate with an ISignatureFactory")]
  130. public IX509AttributeCertificate Generate(
  131. AsymmetricKeyParameter privateKey,
  132. SecureRandom random)
  133. {
  134. return Generate(new Asn1SignatureFactory(signatureAlgorithm, privateKey, random));
  135. }
  136. /// <summary>
  137. /// Generate a new X.509 Attribute Certificate using the passed in SignatureCalculator.
  138. /// </summary>
  139. /// <param name="signatureCalculatorFactory">A signature calculator factory with the necessary algorithm details.</param>
  140. /// <returns>An IX509AttributeCertificate.</returns>
  141. public IX509AttributeCertificate Generate(ISignatureFactory signatureCalculatorFactory)
  142. {
  143. if (!extGenerator.IsEmpty)
  144. {
  145. acInfoGen.SetExtensions(extGenerator.Generate());
  146. }
  147. AlgorithmIdentifier sigAlgID = (AlgorithmIdentifier)signatureCalculatorFactory.AlgorithmDetails;
  148. acInfoGen.SetSignature(sigAlgID);
  149. AttributeCertificateInfo acInfo = acInfoGen.GenerateAttributeCertificateInfo();
  150. byte[] encoded = acInfo.GetDerEncoded();
  151. IStreamCalculator streamCalculator = signatureCalculatorFactory.CreateCalculator();
  152. streamCalculator.Stream.Write(encoded, 0, encoded.Length);
  153. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  154. try
  155. {
  156. DerBitString signatureValue = new DerBitString(((IBlockResult)streamCalculator.GetResult()).Collect());
  157. return new X509V2AttributeCertificate(new AttributeCertificate(acInfo, sigAlgID, signatureValue));
  158. }
  159. catch (Exception e)
  160. {
  161. // TODO
  162. // throw new ExtCertificateEncodingException("constructed invalid certificate", e);
  163. throw new CertificateEncodingException("constructed invalid certificate", e);
  164. }
  165. }
  166. /// <summary>
  167. /// Allows enumeration of the signature names supported by the generator.
  168. /// </summary>
  169. public IEnumerable SignatureAlgNames
  170. {
  171. get { return X509Utilities.GetAlgNames(); }
  172. }
  173. }
  174. }
  175. #pragma warning restore
  176. #endif