TimeStampRequest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.Cmp;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Tsp;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  14. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tsp
  15. {
  16. /**
  17. * Base class for an RFC 3161 Time Stamp Request.
  18. */
  19. public class TimeStampRequest
  20. : X509ExtensionBase
  21. {
  22. private TimeStampReq req;
  23. private X509Extensions extensions;
  24. public TimeStampRequest(
  25. TimeStampReq req)
  26. {
  27. this.req = req;
  28. this.extensions = req.Extensions;
  29. }
  30. /**
  31. * Create a TimeStampRequest from the past in byte array.
  32. *
  33. * @param req byte array containing the request.
  34. * @throws IOException if the request is malformed.
  35. */
  36. public TimeStampRequest(
  37. byte[] req)
  38. : this(new Asn1InputStream(req))
  39. {
  40. }
  41. /**
  42. * Create a TimeStampRequest from the past in input stream.
  43. *
  44. * @param in input stream containing the request.
  45. * @throws IOException if the request is malformed.
  46. */
  47. public TimeStampRequest(
  48. Stream input)
  49. : this(new Asn1InputStream(input))
  50. {
  51. }
  52. private TimeStampRequest(
  53. Asn1InputStream str)
  54. {
  55. try
  56. {
  57. this.req = TimeStampReq.GetInstance(str.ReadObject());
  58. }
  59. catch (InvalidCastException e)
  60. {
  61. throw new IOException("malformed request: " + e);
  62. }
  63. catch (ArgumentException e)
  64. {
  65. throw new IOException("malformed request: " + e);
  66. }
  67. }
  68. public int Version
  69. {
  70. get { return req.Version.IntValueExact; }
  71. }
  72. public string MessageImprintAlgOid
  73. {
  74. get { return req.MessageImprint.HashAlgorithm.Algorithm.Id; }
  75. }
  76. public byte[] GetMessageImprintDigest()
  77. {
  78. return req.MessageImprint.GetHashedMessage();
  79. }
  80. public string ReqPolicy
  81. {
  82. get
  83. {
  84. return req.ReqPolicy == null
  85. ? null
  86. : req.ReqPolicy.Id;
  87. }
  88. }
  89. public BigInteger Nonce
  90. {
  91. get
  92. {
  93. return req.Nonce == null
  94. ? null
  95. : req.Nonce.Value;
  96. }
  97. }
  98. public bool CertReq
  99. {
  100. get
  101. {
  102. return req.CertReq == null
  103. ? false
  104. : req.CertReq.IsTrue;
  105. }
  106. }
  107. /**
  108. * Validate the timestamp request, checking the digest to see if it is of an
  109. * accepted type and whether it is of the correct length for the algorithm specified.
  110. *
  111. * @param algorithms a set of string OIDS giving accepted algorithms.
  112. * @param policies if non-null a set of policies we are willing to sign under.
  113. * @param extensions if non-null a set of extensions we are willing to accept.
  114. * @throws TspException if the request is invalid, or processing fails.
  115. */
  116. public void Validate(
  117. IList algorithms,
  118. IList policies,
  119. IList extensions)
  120. {
  121. if (!algorithms.Contains(this.MessageImprintAlgOid))
  122. throw new TspValidationException("request contains unknown algorithm", PkiFailureInfo.BadAlg);
  123. if (policies != null && this.ReqPolicy != null && !policies.Contains(this.ReqPolicy))
  124. throw new TspValidationException("request contains unknown policy", PkiFailureInfo.UnacceptedPolicy);
  125. if (this.Extensions != null && extensions != null)
  126. {
  127. foreach (DerObjectIdentifier oid in this.Extensions.ExtensionOids)
  128. {
  129. if (!extensions.Contains(oid.Id))
  130. throw new TspValidationException("request contains unknown extension", PkiFailureInfo.UnacceptedExtension);
  131. }
  132. }
  133. int digestLength = TspUtil.GetDigestLength(this.MessageImprintAlgOid);
  134. if (digestLength != this.GetMessageImprintDigest().Length)
  135. throw new TspValidationException("imprint digest the wrong length", PkiFailureInfo.BadDataFormat);
  136. }
  137. /**
  138. * return the ASN.1 encoded representation of this object.
  139. */
  140. public byte[] GetEncoded()
  141. {
  142. return req.GetEncoded();
  143. }
  144. internal X509Extensions Extensions
  145. {
  146. get { return req.Extensions; }
  147. }
  148. public virtual bool HasExtensions
  149. {
  150. get { return extensions != null; }
  151. }
  152. public virtual X509Extension GetExtension(DerObjectIdentifier oid)
  153. {
  154. return extensions == null ? null : extensions.GetExtension(oid);
  155. }
  156. public virtual IList GetExtensionOids()
  157. {
  158. return TspUtil.GetExtensionOids(extensions);
  159. }
  160. protected override X509Extensions GetX509Extensions()
  161. {
  162. return Extensions;
  163. }
  164. }
  165. }
  166. #pragma warning restore
  167. #endif