X509V2AttributeCertificateGenerator.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /// <remarks>Class to produce an X.509 Version 2 AttributeCertificate.</remarks>
  15. public class X509V2AttributeCertificateGenerator
  16. {
  17. private readonly X509ExtensionsGenerator extGenerator = new X509ExtensionsGenerator();
  18. private V2AttributeCertificateInfoGenerator acInfoGen;
  19. public X509V2AttributeCertificateGenerator()
  20. {
  21. acInfoGen = new V2AttributeCertificateInfoGenerator();
  22. }
  23. /// <summary>Reset the generator</summary>
  24. public void Reset()
  25. {
  26. acInfoGen = new V2AttributeCertificateInfoGenerator();
  27. extGenerator.Reset();
  28. }
  29. /// <summary>Set the Holder of this Attribute Certificate.</summary>
  30. public void SetHolder(
  31. AttributeCertificateHolder holder)
  32. {
  33. acInfoGen.SetHolder(holder.holder);
  34. }
  35. /// <summary>Set the issuer.</summary>
  36. public void SetIssuer(
  37. AttributeCertificateIssuer issuer)
  38. {
  39. acInfoGen.SetIssuer(AttCertIssuer.GetInstance(issuer.form));
  40. }
  41. /// <summary>Set the serial number for the certificate.</summary>
  42. public void SetSerialNumber(
  43. BigInteger serialNumber)
  44. {
  45. acInfoGen.SetSerialNumber(new DerInteger(serialNumber));
  46. }
  47. public void SetNotBefore(
  48. DateTime date)
  49. {
  50. acInfoGen.SetStartDate(new Asn1GeneralizedTime(date));
  51. }
  52. public void SetNotAfter(
  53. DateTime date)
  54. {
  55. acInfoGen.SetEndDate(new Asn1GeneralizedTime(date));
  56. }
  57. /// <summary>Add an attribute.</summary>
  58. public void AddAttribute(
  59. X509Attribute attribute)
  60. {
  61. acInfoGen.AddAttribute(AttributeX509.GetInstance(attribute.ToAsn1Object()));
  62. }
  63. public void SetIssuerUniqueId(
  64. bool[] iui)
  65. {
  66. // TODO convert bool array to bit string
  67. //acInfoGen.SetIssuerUniqueID(iui);
  68. throw new NotImplementedException("SetIssuerUniqueId()");
  69. }
  70. /// <summary>Add a given extension field for the standard extensions tag.</summary>
  71. public void AddExtension(
  72. string oid,
  73. bool critical,
  74. Asn1Encodable extensionValue)
  75. {
  76. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, extensionValue);
  77. }
  78. /// <summary>
  79. /// Add a given extension field for the standard extensions tag.
  80. /// The value parameter becomes the contents of the octet string associated
  81. /// with the extension.
  82. /// </summary>
  83. public void AddExtension(
  84. string oid,
  85. bool critical,
  86. byte[] extensionValue)
  87. {
  88. extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, extensionValue);
  89. }
  90. /// <summary>
  91. /// Generate a new <see cref="X509V2AttributeCertificate"/> using the provided <see cref="ISignatureFactory"/>.
  92. /// </summary>
  93. /// <param name="signatureFactory">A <see cref="ISignatureFactory">signature factory</see> with the necessary
  94. /// algorithm details.</param>
  95. /// <returns>An <see cref="X509V2AttributeCertificate"/>.</returns>
  96. public X509V2AttributeCertificate Generate(ISignatureFactory signatureFactory)
  97. {
  98. var sigAlgID = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails;
  99. acInfoGen.SetSignature(sigAlgID);
  100. if (!extGenerator.IsEmpty)
  101. {
  102. acInfoGen.SetExtensions(extGenerator.Generate());
  103. }
  104. AttributeCertificateInfo acInfo = acInfoGen.GenerateAttributeCertificateInfo();
  105. IStreamCalculator<IBlockResult> streamCalculator = signatureFactory.CreateCalculator();
  106. using (Stream sigStream = streamCalculator.Stream)
  107. {
  108. acInfo.EncodeTo(sigStream, Asn1Encodable.Der);
  109. }
  110. var signature = streamCalculator.GetResult().Collect();
  111. return new X509V2AttributeCertificate(
  112. new AttributeCertificate(acInfo, sigAlgID, new DerBitString(signature)));
  113. }
  114. /// <summary>
  115. /// Allows enumeration of the signature names supported by the generator.
  116. /// </summary>
  117. public IEnumerable<string> SignatureAlgNames
  118. {
  119. get { return X509Utilities.GetAlgNames(); }
  120. }
  121. }
  122. }
  123. #pragma warning restore
  124. #endif