TimeStampRequestGenerator.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Tsp;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  10. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tsp
  11. {
  12. /**
  13. * Generator for RFC 3161 Time Stamp Request objects.
  14. */
  15. public class TimeStampRequestGenerator
  16. {
  17. private DerObjectIdentifier reqPolicy;
  18. private DerBoolean certReq;
  19. private IDictionary extensions = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  20. private IList extOrdering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  21. public void SetReqPolicy(
  22. string reqPolicy)
  23. {
  24. this.reqPolicy = new DerObjectIdentifier(reqPolicy);
  25. }
  26. public void SetCertReq(
  27. bool certReq)
  28. {
  29. this.certReq = DerBoolean.GetInstance(certReq);
  30. }
  31. /**
  32. * add a given extension field for the standard extensions tag (tag 3)
  33. * @throws IOException
  34. */
  35. [Obsolete("Use method taking DerObjectIdentifier")]
  36. public void AddExtension(
  37. string oid,
  38. bool critical,
  39. Asn1Encodable value)
  40. {
  41. this.AddExtension(oid, critical, value.GetEncoded());
  42. }
  43. /**
  44. * add a given extension field for the standard extensions tag
  45. * The value parameter becomes the contents of the octet string associated
  46. * with the extension.
  47. */
  48. [Obsolete("Use method taking DerObjectIdentifier")]
  49. public void AddExtension(
  50. string oid,
  51. bool critical,
  52. byte[] value)
  53. {
  54. DerObjectIdentifier derOid = new DerObjectIdentifier(oid);
  55. extensions[derOid] = new X509Extension(critical, new DerOctetString(value));
  56. extOrdering.Add(derOid);
  57. }
  58. /**
  59. * add a given extension field for the standard extensions tag (tag 3)
  60. * @throws IOException
  61. */
  62. public virtual void AddExtension(
  63. DerObjectIdentifier oid,
  64. bool critical,
  65. Asn1Encodable extValue)
  66. {
  67. this.AddExtension(oid, critical, extValue.GetEncoded());
  68. }
  69. /**
  70. * add a given extension field for the standard extensions tag
  71. * The value parameter becomes the contents of the octet string associated
  72. * with the extension.
  73. */
  74. public virtual void AddExtension(
  75. DerObjectIdentifier oid,
  76. bool critical,
  77. byte[] extValue)
  78. {
  79. extensions.Add(oid, new X509Extension(critical, new DerOctetString(extValue)));
  80. extOrdering.Add(oid);
  81. }
  82. public TimeStampRequest Generate(
  83. string digestAlgorithm,
  84. byte[] digest)
  85. {
  86. return this.Generate(digestAlgorithm, digest, null);
  87. }
  88. public TimeStampRequest Generate(
  89. string digestAlgorithmOid,
  90. byte[] digest,
  91. BigInteger nonce)
  92. {
  93. if (digestAlgorithmOid == null)
  94. {
  95. throw new ArgumentException("No digest algorithm specified");
  96. }
  97. DerObjectIdentifier digestAlgOid = new DerObjectIdentifier(digestAlgorithmOid);
  98. AlgorithmIdentifier algID = new AlgorithmIdentifier(digestAlgOid, DerNull.Instance);
  99. MessageImprint messageImprint = new MessageImprint(algID, digest);
  100. X509Extensions ext = null;
  101. if (extOrdering.Count != 0)
  102. {
  103. ext = new X509Extensions(extOrdering, extensions);
  104. }
  105. DerInteger derNonce = nonce == null
  106. ? null
  107. : new DerInteger(nonce);
  108. return new TimeStampRequest(
  109. new TimeStampReq(messageImprint, reqPolicy, derNonce, certReq, ext));
  110. }
  111. public virtual TimeStampRequest Generate(DerObjectIdentifier digestAlgorithm, byte[] digest)
  112. {
  113. return Generate(digestAlgorithm.Id, digest);
  114. }
  115. public virtual TimeStampRequest Generate(DerObjectIdentifier digestAlgorithm, byte[] digest, BigInteger nonce)
  116. {
  117. return Generate(digestAlgorithm.Id, digest, nonce);
  118. }
  119. }
  120. }
  121. #pragma warning restore
  122. #endif