TimeStampResponseGenerator.cs 8.8 KB

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