TimeStampRequestGenerator.cs 3.7 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. public void AddExtension(
  36. string oid,
  37. bool critical,
  38. Asn1Encodable value)
  39. {
  40. this.AddExtension(oid, critical, value.GetEncoded());
  41. }
  42. /**
  43. * add a given extension field for the standard extensions tag
  44. * The value parameter becomes the contents of the octet string associated
  45. * with the extension.
  46. */
  47. public void AddExtension(
  48. string oid,
  49. bool critical,
  50. byte[] value)
  51. {
  52. DerObjectIdentifier derOid = new DerObjectIdentifier(oid);
  53. extensions[derOid] = new X509Extension(critical, new DerOctetString(value));
  54. extOrdering.Add(derOid);
  55. }
  56. /**
  57. * add a given extension field for the standard extensions tag (tag 3)
  58. * @throws IOException
  59. */
  60. public virtual void AddExtension(
  61. DerObjectIdentifier oid,
  62. bool critical,
  63. Asn1Encodable extValue)
  64. {
  65. this.AddExtension(oid, critical, extValue.GetEncoded());
  66. }
  67. /**
  68. * add a given extension field for the standard extensions tag
  69. * The value parameter becomes the contents of the octet string associated
  70. * with the extension.
  71. */
  72. public virtual void AddExtension(
  73. DerObjectIdentifier oid,
  74. bool critical,
  75. byte[] extValue)
  76. {
  77. extensions.Add(oid, new X509Extension(critical, new DerOctetString(extValue)));
  78. extOrdering.Add(oid);
  79. }
  80. public TimeStampRequest Generate(
  81. string digestAlgorithm,
  82. byte[] digest)
  83. {
  84. return this.Generate(digestAlgorithm, digest, null);
  85. }
  86. public TimeStampRequest Generate(
  87. string digestAlgorithmOid,
  88. byte[] digest,
  89. BigInteger nonce)
  90. {
  91. if (digestAlgorithmOid == null)
  92. {
  93. throw new ArgumentException("No digest algorithm specified");
  94. }
  95. DerObjectIdentifier digestAlgOid = new DerObjectIdentifier(digestAlgorithmOid);
  96. AlgorithmIdentifier algID = new AlgorithmIdentifier(digestAlgOid, DerNull.Instance);
  97. MessageImprint messageImprint = new MessageImprint(algID, digest);
  98. X509Extensions ext = null;
  99. if (extOrdering.Count != 0)
  100. {
  101. ext = new X509Extensions(extOrdering, extensions);
  102. }
  103. DerInteger derNonce = nonce == null
  104. ? null
  105. : new DerInteger(nonce);
  106. return new TimeStampRequest(
  107. new TimeStampReq(messageImprint, reqPolicy, derNonce, certReq, ext));
  108. }
  109. public virtual TimeStampRequest Generate(DerObjectIdentifier digestAlgorithm, byte[] digest)
  110. {
  111. return Generate(digestAlgorithm.Id, digest);
  112. }
  113. public virtual TimeStampRequest Generate(DerObjectIdentifier digestAlgorithm, byte[] digest, BigInteger nonce)
  114. {
  115. return Generate(digestAlgorithm.Id, digest, nonce);
  116. }
  117. }
  118. }
  119. #pragma warning restore
  120. #endif