TimeStampResponseGenerator.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.Math;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Date;
  12. namespace BestHTTP.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 acceptedAlgorithms;
  24. private IList acceptedPolicies;
  25. private IList acceptedExtensions;
  26. public TimeStampResponseGenerator(
  27. TimeStampTokenGenerator tokenGenerator,
  28. IList acceptedAlgorithms)
  29. : this(tokenGenerator, acceptedAlgorithms, null, null)
  30. {
  31. }
  32. public TimeStampResponseGenerator(
  33. TimeStampTokenGenerator tokenGenerator,
  34. IList acceptedAlgorithms,
  35. IList acceptedPolicy)
  36. : this(tokenGenerator, acceptedAlgorithms, acceptedPolicy, null)
  37. {
  38. }
  39. public TimeStampResponseGenerator(
  40. TimeStampTokenGenerator tokenGenerator,
  41. IList acceptedAlgorithms,
  42. IList acceptedPolicies,
  43. IList 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. public TimeStampResponse Generate(
  74. TimeStampRequest request,
  75. BigInteger serialNumber,
  76. DateTime genTime)
  77. {
  78. return Generate(request, serialNumber, new DateTimeObject(genTime));
  79. }
  80. /**
  81. * Return an appropriate TimeStampResponse.
  82. * <p>
  83. * If genTime is null a timeNotAvailable error response will be returned.
  84. *
  85. * @param request the request this response is for.
  86. * @param serialNumber serial number for the response token.
  87. * @param genTime generation time for the response token.
  88. * @param provider provider to use for signature calculation.
  89. * @return
  90. * @throws NoSuchAlgorithmException
  91. * @throws NoSuchProviderException
  92. * @throws TSPException
  93. * </p>
  94. */
  95. public TimeStampResponse Generate(
  96. TimeStampRequest request,
  97. BigInteger serialNumber,
  98. DateTimeObject genTime)
  99. {
  100. TimeStampResp resp;
  101. try
  102. {
  103. if (genTime == null)
  104. throw new TspValidationException("The time source is not available.",
  105. PkiFailureInfo.TimeNotAvailable);
  106. request.Validate(acceptedAlgorithms, acceptedPolicies, acceptedExtensions);
  107. this.status = PkiStatus.Granted;
  108. this.AddStatusString("Operation Okay");
  109. PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
  110. ContentInfo tstTokenContentInfo;
  111. try
  112. {
  113. TimeStampToken token = tokenGenerator.Generate(request, serialNumber, genTime.Value);
  114. byte[] encoded = token.ToCmsSignedData().GetEncoded();
  115. tstTokenContentInfo = ContentInfo.GetInstance(Asn1Object.FromByteArray(encoded));
  116. }
  117. catch (IOException e)
  118. {
  119. throw new TspException("Timestamp token received cannot be converted to ContentInfo", e);
  120. }
  121. resp = new TimeStampResp(pkiStatusInfo, tstTokenContentInfo);
  122. }
  123. catch (TspValidationException e)
  124. {
  125. status = PkiStatus.Rejection;
  126. this.SetFailInfoField(e.FailureCode);
  127. this.AddStatusString(e.Message);
  128. PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
  129. resp = new TimeStampResp(pkiStatusInfo, null);
  130. }
  131. try
  132. {
  133. return new TimeStampResponse(resp);
  134. }
  135. catch (IOException e)
  136. {
  137. throw new TspException("created badly formatted response!", e);
  138. }
  139. }
  140. class FailInfo
  141. : DerBitString
  142. {
  143. internal FailInfo(int failInfoValue)
  144. : base(failInfoValue)
  145. {
  146. }
  147. }
  148. /**
  149. * Generate a TimeStampResponse with chosen status and FailInfoField.
  150. *
  151. * @param status the PKIStatus to set.
  152. * @param failInfoField the FailInfoField to set.
  153. * @param statusString an optional string describing the failure.
  154. * @return a TimeStampResponse with a failInfoField and optional statusString
  155. * @throws TSPException in case the response could not be created
  156. */
  157. public TimeStampResponse GenerateFailResponse(PkiStatus status, int failInfoField, string statusString)
  158. {
  159. this.status = status;
  160. this.SetFailInfoField(failInfoField);
  161. if (statusString != null)
  162. {
  163. this.AddStatusString(statusString);
  164. }
  165. PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
  166. TimeStampResp resp = new TimeStampResp(pkiStatusInfo, null);
  167. try
  168. {
  169. return new TimeStampResponse(resp);
  170. }
  171. catch (IOException e)
  172. {
  173. throw new TspException("created badly formatted response!", e);
  174. }
  175. }
  176. }
  177. }
  178. #pragma warning restore
  179. #endif