TimeStampTokenGenerator.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.Ess;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Tsp;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Cms;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509.Store;
  19. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tsp
  20. {
  21. public class TimeStampTokenGenerator
  22. {
  23. private int accuracySeconds = -1;
  24. private int accuracyMillis = -1;
  25. private int accuracyMicros = -1;
  26. private bool ordering = false;
  27. private GeneralName tsa = null;
  28. private string tsaPolicyOID;
  29. private AsymmetricKeyParameter key;
  30. private X509Certificate cert;
  31. private string digestOID;
  32. private Asn1.Cms.AttributeTable signedAttr;
  33. private Asn1.Cms.AttributeTable unsignedAttr;
  34. private IX509Store x509Certs;
  35. private IX509Store x509Crls;
  36. /**
  37. * basic creation - only the default attributes will be included here.
  38. */
  39. public TimeStampTokenGenerator(
  40. AsymmetricKeyParameter key,
  41. X509Certificate cert,
  42. string digestOID,
  43. string tsaPolicyOID)
  44. : this(key, cert, digestOID, tsaPolicyOID, null, null)
  45. {
  46. }
  47. /**
  48. * create with a signer with extra signed/unsigned attributes.
  49. */
  50. public TimeStampTokenGenerator(
  51. AsymmetricKeyParameter key,
  52. X509Certificate cert,
  53. string digestOID,
  54. string tsaPolicyOID,
  55. Asn1.Cms.AttributeTable signedAttr,
  56. Asn1.Cms.AttributeTable unsignedAttr)
  57. {
  58. this.key = key;
  59. this.cert = cert;
  60. this.digestOID = digestOID;
  61. this.tsaPolicyOID = tsaPolicyOID;
  62. this.unsignedAttr = unsignedAttr;
  63. TspUtil.ValidateCertificate(cert);
  64. //
  65. // Add the ESSCertID attribute
  66. //
  67. IDictionary signedAttrs;
  68. if (signedAttr != null)
  69. {
  70. signedAttrs = signedAttr.ToDictionary();
  71. }
  72. else
  73. {
  74. signedAttrs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  75. }
  76. try
  77. {
  78. byte[] hash = DigestUtilities.CalculateDigest("SHA-1", cert.GetEncoded());
  79. EssCertID essCertid = new EssCertID(hash);
  80. Asn1.Cms.Attribute attr = new Asn1.Cms.Attribute(
  81. PkcsObjectIdentifiers.IdAASigningCertificate,
  82. new DerSet(new SigningCertificate(essCertid)));
  83. signedAttrs[attr.AttrType] = attr;
  84. }
  85. catch (CertificateEncodingException e)
  86. {
  87. throw new TspException("Exception processing certificate.", e);
  88. }
  89. catch (SecurityUtilityException e)
  90. {
  91. throw new TspException("Can't find a SHA-1 implementation.", e);
  92. }
  93. this.signedAttr = new Asn1.Cms.AttributeTable(signedAttrs);
  94. }
  95. public void SetCertificates(
  96. IX509Store certificates)
  97. {
  98. this.x509Certs = certificates;
  99. }
  100. public void SetCrls(
  101. IX509Store crls)
  102. {
  103. this.x509Crls = crls;
  104. }
  105. public void SetAccuracySeconds(
  106. int accuracySeconds)
  107. {
  108. this.accuracySeconds = accuracySeconds;
  109. }
  110. public void SetAccuracyMillis(
  111. int accuracyMillis)
  112. {
  113. this.accuracyMillis = accuracyMillis;
  114. }
  115. public void SetAccuracyMicros(
  116. int accuracyMicros)
  117. {
  118. this.accuracyMicros = accuracyMicros;
  119. }
  120. public void SetOrdering(
  121. bool ordering)
  122. {
  123. this.ordering = ordering;
  124. }
  125. public void SetTsa(
  126. GeneralName tsa)
  127. {
  128. this.tsa = tsa;
  129. }
  130. //------------------------------------------------------------------------------
  131. public TimeStampToken Generate(
  132. TimeStampRequest request,
  133. BigInteger serialNumber,
  134. DateTime genTime)
  135. {
  136. DerObjectIdentifier digestAlgOID = new DerObjectIdentifier(request.MessageImprintAlgOid);
  137. AlgorithmIdentifier algID = new AlgorithmIdentifier(digestAlgOID, DerNull.Instance);
  138. MessageImprint messageImprint = new MessageImprint(algID, request.GetMessageImprintDigest());
  139. Accuracy accuracy = null;
  140. if (accuracySeconds > 0 || accuracyMillis > 0 || accuracyMicros > 0)
  141. {
  142. DerInteger seconds = null;
  143. if (accuracySeconds > 0)
  144. {
  145. seconds = new DerInteger(accuracySeconds);
  146. }
  147. DerInteger millis = null;
  148. if (accuracyMillis > 0)
  149. {
  150. millis = new DerInteger(accuracyMillis);
  151. }
  152. DerInteger micros = null;
  153. if (accuracyMicros > 0)
  154. {
  155. micros = new DerInteger(accuracyMicros);
  156. }
  157. accuracy = new Accuracy(seconds, millis, micros);
  158. }
  159. DerBoolean derOrdering = null;
  160. if (ordering)
  161. {
  162. derOrdering = DerBoolean.GetInstance(ordering);
  163. }
  164. DerInteger nonce = null;
  165. if (request.Nonce != null)
  166. {
  167. nonce = new DerInteger(request.Nonce);
  168. }
  169. DerObjectIdentifier tsaPolicy = new DerObjectIdentifier(tsaPolicyOID);
  170. if (request.ReqPolicy != null)
  171. {
  172. tsaPolicy = new DerObjectIdentifier(request.ReqPolicy);
  173. }
  174. TstInfo tstInfo = new TstInfo(tsaPolicy, messageImprint,
  175. new DerInteger(serialNumber), new DerGeneralizedTime(genTime), accuracy,
  176. derOrdering, nonce, tsa, request.Extensions);
  177. try
  178. {
  179. CmsSignedDataGenerator signedDataGenerator = new CmsSignedDataGenerator();
  180. byte[] derEncodedTstInfo = tstInfo.GetDerEncoded();
  181. if (request.CertReq)
  182. {
  183. signedDataGenerator.AddCertificates(x509Certs);
  184. }
  185. signedDataGenerator.AddCrls(x509Crls);
  186. signedDataGenerator.AddSigner(key, cert, digestOID, signedAttr, unsignedAttr);
  187. CmsSignedData signedData = signedDataGenerator.Generate(
  188. PkcsObjectIdentifiers.IdCTTstInfo.Id,
  189. new CmsProcessableByteArray(derEncodedTstInfo),
  190. true);
  191. return new TimeStampToken(signedData);
  192. }
  193. catch (CmsException cmsEx)
  194. {
  195. throw new TspException("Error generating time-stamp token", cmsEx);
  196. }
  197. catch (IOException e)
  198. {
  199. throw new TspException("Exception encoding info", e);
  200. }
  201. catch (X509StoreException e)
  202. {
  203. throw new TspException("Exception handling CertStore", e);
  204. }
  205. // catch (InvalidAlgorithmParameterException e)
  206. // {
  207. // throw new TspException("Exception handling CertStore CRLs", e);
  208. // }
  209. }
  210. }
  211. }
  212. #pragma warning restore
  213. #endif