OCSPReqGenerator.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.Crypto.Parameters;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  15. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Ocsp
  16. {
  17. public class OcspReqGenerator
  18. {
  19. private IList list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  20. private GeneralName requestorName = null;
  21. private X509Extensions requestExtensions = null;
  22. private class RequestObject
  23. {
  24. internal CertificateID certId;
  25. internal X509Extensions extensions;
  26. public RequestObject(
  27. CertificateID certId,
  28. X509Extensions extensions)
  29. {
  30. this.certId = certId;
  31. this.extensions = extensions;
  32. }
  33. public Request ToRequest()
  34. {
  35. return new Request(certId.ToAsn1Object(), extensions);
  36. }
  37. }
  38. /**
  39. * Add a request for the given CertificateID.
  40. *
  41. * @param certId certificate ID of interest
  42. */
  43. public void AddRequest(
  44. CertificateID certId)
  45. {
  46. list.Add(new RequestObject(certId, null));
  47. }
  48. /**
  49. * Add a request with extensions
  50. *
  51. * @param certId certificate ID of interest
  52. * @param singleRequestExtensions the extensions to attach to the request
  53. */
  54. public void AddRequest(
  55. CertificateID certId,
  56. X509Extensions singleRequestExtensions)
  57. {
  58. list.Add(new RequestObject(certId, singleRequestExtensions));
  59. }
  60. /**
  61. * Set the requestor name to the passed in X509Principal
  62. *
  63. * @param requestorName a X509Principal representing the requestor name.
  64. */
  65. public void SetRequestorName(
  66. X509Name requestorName)
  67. {
  68. try
  69. {
  70. this.requestorName = new GeneralName(GeneralName.DirectoryName, requestorName);
  71. }
  72. catch (Exception e)
  73. {
  74. throw new ArgumentException("cannot encode principal", e);
  75. }
  76. }
  77. public void SetRequestorName(
  78. GeneralName requestorName)
  79. {
  80. this.requestorName = requestorName;
  81. }
  82. public void SetRequestExtensions(
  83. X509Extensions requestExtensions)
  84. {
  85. this.requestExtensions = requestExtensions;
  86. }
  87. private OcspReq GenerateRequest(
  88. DerObjectIdentifier signingAlgorithm,
  89. AsymmetricKeyParameter privateKey,
  90. X509Certificate[] chain,
  91. SecureRandom random)
  92. {
  93. Asn1EncodableVector requests = new Asn1EncodableVector();
  94. foreach (RequestObject reqObj in list)
  95. {
  96. try
  97. {
  98. requests.Add(reqObj.ToRequest());
  99. }
  100. catch (Exception e)
  101. {
  102. throw new OcspException("exception creating Request", e);
  103. }
  104. }
  105. TbsRequest tbsReq = new TbsRequest(requestorName, new DerSequence(requests), requestExtensions);
  106. ISigner sig = null;
  107. Signature signature = null;
  108. if (signingAlgorithm != null)
  109. {
  110. if (requestorName == null)
  111. {
  112. throw new OcspException("requestorName must be specified if request is signed.");
  113. }
  114. try
  115. {
  116. sig = SignerUtilities.GetSigner(signingAlgorithm.Id);
  117. if (random != null)
  118. {
  119. sig.Init(true, new ParametersWithRandom(privateKey, random));
  120. }
  121. else
  122. {
  123. sig.Init(true, privateKey);
  124. }
  125. }
  126. catch (Exception e)
  127. {
  128. throw new OcspException("exception creating signature: " + e, e);
  129. }
  130. DerBitString bitSig = null;
  131. try
  132. {
  133. byte[] encoded = tbsReq.GetEncoded();
  134. sig.BlockUpdate(encoded, 0, encoded.Length);
  135. bitSig = new DerBitString(sig.GenerateSignature());
  136. }
  137. catch (Exception e)
  138. {
  139. throw new OcspException("exception processing TBSRequest: " + e, e);
  140. }
  141. AlgorithmIdentifier sigAlgId = new AlgorithmIdentifier(signingAlgorithm, DerNull.Instance);
  142. if (chain != null && chain.Length > 0)
  143. {
  144. Asn1EncodableVector v = new Asn1EncodableVector();
  145. try
  146. {
  147. for (int i = 0; i != chain.Length; i++)
  148. {
  149. v.Add(
  150. X509CertificateStructure.GetInstance(
  151. Asn1Object.FromByteArray(chain[i].GetEncoded())));
  152. }
  153. }
  154. catch (IOException e)
  155. {
  156. throw new OcspException("error processing certs", e);
  157. }
  158. catch (CertificateEncodingException e)
  159. {
  160. throw new OcspException("error encoding certs", e);
  161. }
  162. signature = new Signature(sigAlgId, bitSig, new DerSequence(v));
  163. }
  164. else
  165. {
  166. signature = new Signature(sigAlgId, bitSig);
  167. }
  168. }
  169. return new OcspReq(new OcspRequest(tbsReq, signature));
  170. }
  171. /**
  172. * Generate an unsigned request
  173. *
  174. * @return the OcspReq
  175. * @throws OcspException
  176. */
  177. public OcspReq Generate()
  178. {
  179. return GenerateRequest(null, null, null, null);
  180. }
  181. public OcspReq Generate(
  182. string signingAlgorithm,
  183. AsymmetricKeyParameter privateKey,
  184. X509Certificate[] chain)
  185. {
  186. return Generate(signingAlgorithm, privateKey, chain, null);
  187. }
  188. public OcspReq Generate(
  189. string signingAlgorithm,
  190. AsymmetricKeyParameter privateKey,
  191. X509Certificate[] chain,
  192. SecureRandom random)
  193. {
  194. if (signingAlgorithm == null)
  195. throw new ArgumentException("no signing algorithm specified");
  196. try
  197. {
  198. DerObjectIdentifier oid = OcspUtilities.GetAlgorithmOid(signingAlgorithm);
  199. return GenerateRequest(oid, privateKey, chain, random);
  200. }
  201. catch (ArgumentException)
  202. {
  203. throw new ArgumentException("unknown signing algorithm specified: " + signingAlgorithm);
  204. }
  205. }
  206. /**
  207. * Return an IEnumerable of the signature names supported by the generator.
  208. *
  209. * @return an IEnumerable containing recognised names.
  210. */
  211. public IEnumerable SignatureAlgNames
  212. {
  213. get { return OcspUtilities.AlgNames; }
  214. }
  215. }
  216. }
  217. #pragma warning restore
  218. #endif