BasicOCSPRespGenerator.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.Ocsp;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators;
  15. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Ocsp
  16. {
  17. /**
  18. * Generator for basic OCSP response objects.
  19. */
  20. public class BasicOcspRespGenerator
  21. {
  22. private readonly IList list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  23. private X509Extensions responseExtensions;
  24. private RespID responderID;
  25. private class ResponseObject
  26. {
  27. internal CertificateID certId;
  28. internal CertStatus certStatus;
  29. internal DerGeneralizedTime thisUpdate;
  30. internal DerGeneralizedTime nextUpdate;
  31. internal X509Extensions extensions;
  32. public ResponseObject(
  33. CertificateID certId,
  34. CertificateStatus certStatus,
  35. DateTime thisUpdate,
  36. X509Extensions extensions)
  37. : this(certId, certStatus, new DerGeneralizedTime(thisUpdate), null, extensions)
  38. {
  39. }
  40. public ResponseObject(
  41. CertificateID certId,
  42. CertificateStatus certStatus,
  43. DateTime thisUpdate,
  44. DateTime nextUpdate,
  45. X509Extensions extensions)
  46. : this(certId, certStatus, new DerGeneralizedTime(thisUpdate), new DerGeneralizedTime(nextUpdate), extensions)
  47. {
  48. }
  49. private ResponseObject(
  50. CertificateID certId,
  51. CertificateStatus certStatus,
  52. DerGeneralizedTime thisUpdate,
  53. DerGeneralizedTime nextUpdate,
  54. X509Extensions extensions)
  55. {
  56. this.certId = certId;
  57. if (certStatus == null)
  58. {
  59. this.certStatus = new CertStatus();
  60. }
  61. else if (certStatus is UnknownStatus)
  62. {
  63. this.certStatus = new CertStatus(2, DerNull.Instance);
  64. }
  65. else
  66. {
  67. RevokedStatus rs = (RevokedStatus) certStatus;
  68. CrlReason revocationReason = rs.HasRevocationReason
  69. ? new CrlReason(rs.RevocationReason)
  70. : null;
  71. this.certStatus = new CertStatus(
  72. new RevokedInfo(new DerGeneralizedTime(rs.RevocationTime), revocationReason));
  73. }
  74. this.thisUpdate = thisUpdate;
  75. this.nextUpdate = nextUpdate;
  76. this.extensions = extensions;
  77. }
  78. public SingleResponse ToResponse()
  79. {
  80. return new SingleResponse(certId.ToAsn1Object(), certStatus, thisUpdate, nextUpdate, extensions);
  81. }
  82. }
  83. /**
  84. * basic constructor
  85. */
  86. public BasicOcspRespGenerator(
  87. RespID responderID)
  88. {
  89. this.responderID = responderID;
  90. }
  91. /**
  92. * construct with the responderID to be the SHA-1 keyHash of the passed in public key.
  93. */
  94. public BasicOcspRespGenerator(
  95. AsymmetricKeyParameter publicKey)
  96. {
  97. this.responderID = new RespID(publicKey);
  98. }
  99. /**
  100. * Add a response for a particular Certificate ID.
  101. *
  102. * @param certID certificate ID details
  103. * @param certStatus status of the certificate - null if okay
  104. */
  105. public void AddResponse(
  106. CertificateID certID,
  107. CertificateStatus certStatus)
  108. {
  109. list.Add(new ResponseObject(certID, certStatus, DateTime.UtcNow, null));
  110. }
  111. /**
  112. * Add a response for a particular Certificate ID.
  113. *
  114. * @param certID certificate ID details
  115. * @param certStatus status of the certificate - null if okay
  116. * @param singleExtensions optional extensions
  117. */
  118. public void AddResponse(
  119. CertificateID certID,
  120. CertificateStatus certStatus,
  121. X509Extensions singleExtensions)
  122. {
  123. list.Add(new ResponseObject(certID, certStatus, DateTime.UtcNow, singleExtensions));
  124. }
  125. /**
  126. * Add a response for a particular Certificate ID.
  127. *
  128. * @param certID certificate ID details
  129. * @param nextUpdate date when next update should be requested
  130. * @param certStatus status of the certificate - null if okay
  131. * @param singleExtensions optional extensions
  132. */
  133. public void AddResponse(
  134. CertificateID certID,
  135. CertificateStatus certStatus,
  136. DateTime nextUpdate,
  137. X509Extensions singleExtensions)
  138. {
  139. list.Add(new ResponseObject(certID, certStatus, DateTime.UtcNow, nextUpdate, singleExtensions));
  140. }
  141. /**
  142. * Add a response for a particular Certificate ID.
  143. *
  144. * @param certID certificate ID details
  145. * @param thisUpdate date this response was valid on
  146. * @param nextUpdate date when next update should be requested
  147. * @param certStatus status of the certificate - null if okay
  148. * @param singleExtensions optional extensions
  149. */
  150. public void AddResponse(
  151. CertificateID certID,
  152. CertificateStatus certStatus,
  153. DateTime thisUpdate,
  154. DateTime nextUpdate,
  155. X509Extensions singleExtensions)
  156. {
  157. list.Add(new ResponseObject(certID, certStatus, thisUpdate, nextUpdate, singleExtensions));
  158. }
  159. /**
  160. * Set the extensions for the response.
  161. *
  162. * @param responseExtensions the extension object to carry.
  163. */
  164. public void SetResponseExtensions(
  165. X509Extensions responseExtensions)
  166. {
  167. this.responseExtensions = responseExtensions;
  168. }
  169. private BasicOcspResp GenerateResponse(
  170. ISignatureFactory signatureCalculator,
  171. X509Certificate[] chain,
  172. DateTime producedAt)
  173. {
  174. AlgorithmIdentifier signingAlgID = (AlgorithmIdentifier)signatureCalculator.AlgorithmDetails;
  175. DerObjectIdentifier signingAlgorithm = signingAlgID.Algorithm;
  176. Asn1EncodableVector responses = new Asn1EncodableVector();
  177. foreach (ResponseObject respObj in list)
  178. {
  179. try
  180. {
  181. responses.Add(respObj.ToResponse());
  182. }
  183. catch (Exception e)
  184. {
  185. throw new OcspException("exception creating Request", e);
  186. }
  187. }
  188. ResponseData tbsResp = new ResponseData(responderID.ToAsn1Object(), new DerGeneralizedTime(producedAt), new DerSequence(responses), responseExtensions);
  189. DerBitString bitSig = null;
  190. try
  191. {
  192. IStreamCalculator streamCalculator = signatureCalculator.CreateCalculator();
  193. byte[] encoded = tbsResp.GetDerEncoded();
  194. streamCalculator.Stream.Write(encoded, 0, encoded.Length);
  195. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  196. bitSig = new DerBitString(((IBlockResult)streamCalculator.GetResult()).Collect());
  197. }
  198. catch (Exception e)
  199. {
  200. throw new OcspException("exception processing TBSRequest: " + e, e);
  201. }
  202. AlgorithmIdentifier sigAlgId = OcspUtilities.GetSigAlgID(signingAlgorithm);
  203. DerSequence chainSeq = null;
  204. if (chain != null && chain.Length > 0)
  205. {
  206. Asn1EncodableVector v = new Asn1EncodableVector();
  207. try
  208. {
  209. for (int i = 0; i != chain.Length; i++)
  210. {
  211. v.Add(
  212. X509CertificateStructure.GetInstance(
  213. Asn1Object.FromByteArray(chain[i].GetEncoded())));
  214. }
  215. }
  216. catch (IOException e)
  217. {
  218. throw new OcspException("error processing certs", e);
  219. }
  220. catch (CertificateEncodingException e)
  221. {
  222. throw new OcspException("error encoding certs", e);
  223. }
  224. chainSeq = new DerSequence(v);
  225. }
  226. return new BasicOcspResp(new BasicOcspResponse(tbsResp, sigAlgId, bitSig, chainSeq));
  227. }
  228. public BasicOcspResp Generate(
  229. string signingAlgorithm,
  230. AsymmetricKeyParameter privateKey,
  231. X509Certificate[] chain,
  232. DateTime thisUpdate)
  233. {
  234. return Generate(signingAlgorithm, privateKey, chain, thisUpdate, null);
  235. }
  236. public BasicOcspResp Generate(
  237. string signingAlgorithm,
  238. AsymmetricKeyParameter privateKey,
  239. X509Certificate[] chain,
  240. DateTime producedAt,
  241. SecureRandom random)
  242. {
  243. if (signingAlgorithm == null)
  244. {
  245. throw new ArgumentException("no signing algorithm specified");
  246. }
  247. return GenerateResponse(new Asn1SignatureFactory(signingAlgorithm, privateKey, random), chain, producedAt);
  248. }
  249. /// <summary>
  250. /// Generate the signed response using the passed in signature calculator.
  251. /// </summary>
  252. /// <param name="signatureCalculatorFactory">Implementation of signing calculator factory.</param>
  253. /// <param name="chain">The certificate chain associated with the response signer.</param>
  254. /// <param name="producedAt">"produced at" date.</param>
  255. /// <returns></returns>
  256. public BasicOcspResp Generate(
  257. ISignatureFactory signatureCalculatorFactory,
  258. X509Certificate[] chain,
  259. DateTime producedAt)
  260. {
  261. if (signatureCalculatorFactory == null)
  262. {
  263. throw new ArgumentException("no signature calculator specified");
  264. }
  265. return GenerateResponse(signatureCalculatorFactory, chain, producedAt);
  266. }
  267. /**
  268. * Return an IEnumerable of the signature names supported by the generator.
  269. *
  270. * @return an IEnumerable containing recognised names.
  271. */
  272. public IEnumerable SignatureAlgNames
  273. {
  274. get { return OcspUtilities.AlgNames; }
  275. }
  276. }
  277. }
  278. #pragma warning restore
  279. #endif