TimeStampResponseGenerator.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.Cms;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Tsp;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  12. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tsp
  13. {
  14. /**
  15. * Generator for RFC 3161 Time Stamp Responses.
  16. */
  17. public class TimeStampResponseGenerator
  18. {
  19. private PkiStatus status;
  20. private Asn1EncodableVector statusStrings;
  21. private int failInfo;
  22. private TimeStampTokenGenerator tokenGenerator;
  23. private IList<string> acceptedAlgorithms;
  24. private IList<string> acceptedPolicies;
  25. private IList<string> acceptedExtensions;
  26. public TimeStampResponseGenerator(
  27. TimeStampTokenGenerator tokenGenerator,
  28. IList<string> acceptedAlgorithms)
  29. : this(tokenGenerator, acceptedAlgorithms, null, null)
  30. {
  31. }
  32. public TimeStampResponseGenerator(
  33. TimeStampTokenGenerator tokenGenerator,
  34. IList<string> acceptedAlgorithms,
  35. IList<string> acceptedPolicy)
  36. : this(tokenGenerator, acceptedAlgorithms, acceptedPolicy, null)
  37. {
  38. }
  39. public TimeStampResponseGenerator(
  40. TimeStampTokenGenerator tokenGenerator,
  41. IList<string> acceptedAlgorithms,
  42. IList<string> acceptedPolicies,
  43. IList<string> acceptedExtensions)
  44. {
  45. this.tokenGenerator = tokenGenerator;
  46. this.acceptedAlgorithms = acceptedAlgorithms;
  47. this.acceptedPolicies = acceptedPolicies;
  48. this.acceptedExtensions = acceptedExtensions;
  49. statusStrings = new Asn1EncodableVector();
  50. }
  51. private void AddStatusString(string statusString)
  52. {
  53. statusStrings.Add(new DerUtf8String(statusString));
  54. }
  55. private void SetFailInfoField(int field)
  56. {
  57. failInfo |= field;
  58. }
  59. private PkiStatusInfo GetPkiStatusInfo()
  60. {
  61. Asn1EncodableVector v = new Asn1EncodableVector(
  62. new DerInteger((int)status));
  63. if (statusStrings.Count > 0)
  64. {
  65. v.Add(new PkiFreeText(new DerSequence(statusStrings)));
  66. }
  67. if (failInfo != 0)
  68. {
  69. v.Add(new FailInfo(failInfo));
  70. }
  71. return new PkiStatusInfo(new DerSequence(v));
  72. }
  73. /**
  74. * Return an appropriate TimeStampResponse.
  75. * <p>
  76. * If genTime is null a timeNotAvailable error response will be returned.
  77. *
  78. * @param request the request this response is for.
  79. * @param serialNumber serial number for the response token.
  80. * @param genTime generation time for the response token.
  81. * @param provider provider to use for signature calculation.
  82. * @return
  83. * @throws NoSuchAlgorithmException
  84. * @throws NoSuchProviderException
  85. * @throws TSPException
  86. * </p>
  87. */
  88. public TimeStampResponse Generate(TimeStampRequest request, BigInteger serialNumber, DateTime? genTime)
  89. {
  90. TimeStampResp resp;
  91. try
  92. {
  93. if (genTime == null)
  94. throw new TspValidationException("The time source is not available.",
  95. PkiFailureInfo.TimeNotAvailable);
  96. request.Validate(acceptedAlgorithms, acceptedPolicies, acceptedExtensions);
  97. this.status = PkiStatus.Granted;
  98. this.AddStatusString("Operation Okay");
  99. PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
  100. ContentInfo tstTokenContentInfo;
  101. try
  102. {
  103. TimeStampToken token = tokenGenerator.Generate(request, serialNumber, genTime.Value);
  104. byte[] encoded = token.ToCmsSignedData().GetEncoded();
  105. tstTokenContentInfo = ContentInfo.GetInstance(Asn1Object.FromByteArray(encoded));
  106. }
  107. catch (IOException e)
  108. {
  109. throw new TspException("Timestamp token received cannot be converted to ContentInfo", e);
  110. }
  111. resp = new TimeStampResp(pkiStatusInfo, tstTokenContentInfo);
  112. }
  113. catch (TspValidationException e)
  114. {
  115. status = PkiStatus.Rejection;
  116. this.SetFailInfoField(e.FailureCode);
  117. this.AddStatusString(e.Message);
  118. PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
  119. resp = new TimeStampResp(pkiStatusInfo, null);
  120. }
  121. try
  122. {
  123. return new TimeStampResponse(resp);
  124. }
  125. catch (IOException e)
  126. {
  127. throw new TspException("created badly formatted response!", e);
  128. }
  129. }
  130. public TimeStampResponse GenerateGrantedResponse(TimeStampRequest request, BigInteger serialNumber,
  131. DateTime? genTime, string statusString, X509Extensions additionalExtensions)
  132. {
  133. TimeStampResp resp;
  134. try
  135. {
  136. if (genTime == null)
  137. throw new TspValidationException("The time source is not available.",
  138. PkiFailureInfo.TimeNotAvailable);
  139. request.Validate(acceptedAlgorithms, acceptedPolicies, acceptedExtensions);
  140. this.status = PkiStatus.Granted;
  141. this.AddStatusString(statusString);
  142. PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
  143. ContentInfo tstTokenContentInfo;
  144. try
  145. {
  146. TimeStampToken token = tokenGenerator.Generate(request, serialNumber, genTime.Value,additionalExtensions);
  147. byte[] encoded = token.ToCmsSignedData().GetEncoded();
  148. tstTokenContentInfo = ContentInfo.GetInstance(Asn1Object.FromByteArray(encoded));
  149. }
  150. catch (IOException e)
  151. {
  152. throw new TspException("Timestamp token received cannot be converted to ContentInfo", e);
  153. }
  154. resp = new TimeStampResp(pkiStatusInfo, tstTokenContentInfo);
  155. }
  156. catch (TspValidationException e)
  157. {
  158. status = PkiStatus.Rejection;
  159. this.SetFailInfoField(e.FailureCode);
  160. this.AddStatusString(e.Message);
  161. PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
  162. resp = new TimeStampResp(pkiStatusInfo, null);
  163. }
  164. try
  165. {
  166. return new TimeStampResponse(resp);
  167. }
  168. catch (IOException e)
  169. {
  170. throw new TspException("created badly formatted response!", e);
  171. }
  172. }
  173. class FailInfo
  174. : DerBitString
  175. {
  176. internal FailInfo(int failInfoValue)
  177. : base(failInfoValue)
  178. {
  179. }
  180. }
  181. /**
  182. * Generate a TimeStampResponse with chosen status and FailInfoField.
  183. *
  184. * @param status the PKIStatus to set.
  185. * @param failInfoField the FailInfoField to set.
  186. * @param statusString an optional string describing the failure.
  187. * @return a TimeStampResponse with a failInfoField and optional statusString
  188. * @throws TSPException in case the response could not be created
  189. */
  190. public TimeStampResponse GenerateFailResponse(PkiStatus status, int failInfoField, string statusString)
  191. {
  192. this.status = status;
  193. this.SetFailInfoField(failInfoField);
  194. if (statusString != null)
  195. {
  196. this.AddStatusString(statusString);
  197. }
  198. PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
  199. TimeStampResp resp = new TimeStampResp(pkiStatusInfo, null);
  200. try
  201. {
  202. return new TimeStampResponse(resp);
  203. }
  204. catch (IOException e)
  205. {
  206. throw new TspException("created badly formatted response!", e);
  207. }
  208. }
  209. }
  210. }
  211. #pragma warning restore
  212. #endif