TimeStampRequest.cs 4.5 KB

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