OCSPReq.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.X509.Store;
  15. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Ocsp
  16. {
  17. /**
  18. * <pre>
  19. * OcspRequest ::= SEQUENCE {
  20. * tbsRequest TBSRequest,
  21. * optionalSignature [0] EXPLICIT Signature OPTIONAL }
  22. *
  23. * TBSRequest ::= SEQUENCE {
  24. * version [0] EXPLICIT Version DEFAULT v1,
  25. * requestorName [1] EXPLICIT GeneralName OPTIONAL,
  26. * requestList SEQUENCE OF Request,
  27. * requestExtensions [2] EXPLICIT Extensions OPTIONAL }
  28. *
  29. * Signature ::= SEQUENCE {
  30. * signatureAlgorithm AlgorithmIdentifier,
  31. * signature BIT STRING,
  32. * certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL}
  33. *
  34. * Version ::= INTEGER { v1(0) }
  35. *
  36. * Request ::= SEQUENCE {
  37. * reqCert CertID,
  38. * singleRequestExtensions [0] EXPLICIT Extensions OPTIONAL }
  39. *
  40. * CertID ::= SEQUENCE {
  41. * hashAlgorithm AlgorithmIdentifier,
  42. * issuerNameHash OCTET STRING, -- Hash of Issuer's DN
  43. * issuerKeyHash OCTET STRING, -- Hash of Issuers public key
  44. * serialNumber CertificateSerialNumber }
  45. * </pre>
  46. */
  47. public class OcspReq
  48. : X509ExtensionBase
  49. {
  50. private OcspRequest req;
  51. public OcspReq(
  52. OcspRequest req)
  53. {
  54. this.req = req;
  55. }
  56. public OcspReq(
  57. byte[] req)
  58. : this(new Asn1InputStream(req))
  59. {
  60. }
  61. public OcspReq(
  62. Stream inStr)
  63. : this(new Asn1InputStream(inStr))
  64. {
  65. }
  66. private OcspReq(
  67. Asn1InputStream aIn)
  68. {
  69. try
  70. {
  71. this.req = OcspRequest.GetInstance(aIn.ReadObject());
  72. }
  73. catch (ArgumentException e)
  74. {
  75. throw new IOException("malformed request: " + e.Message);
  76. }
  77. catch (InvalidCastException e)
  78. {
  79. throw new IOException("malformed request: " + e.Message);
  80. }
  81. }
  82. /**
  83. * Return the DER encoding of the tbsRequest field.
  84. * @return DER encoding of tbsRequest
  85. * @throws OcspException in the event of an encoding error.
  86. */
  87. public byte[] GetTbsRequest()
  88. {
  89. try
  90. {
  91. return req.TbsRequest.GetEncoded();
  92. }
  93. catch (IOException e)
  94. {
  95. throw new OcspException("problem encoding tbsRequest", e);
  96. }
  97. }
  98. public int Version
  99. {
  100. get { return req.TbsRequest.Version.IntValueExact + 1; }
  101. }
  102. public GeneralName RequestorName
  103. {
  104. get { return GeneralName.GetInstance(req.TbsRequest.RequestorName); }
  105. }
  106. public Req[] GetRequestList()
  107. {
  108. Asn1Sequence seq = req.TbsRequest.RequestList;
  109. Req[] requests = new Req[seq.Count];
  110. for (int i = 0; i != requests.Length; i++)
  111. {
  112. requests[i] = new Req(Request.GetInstance(seq[i]));
  113. }
  114. return requests;
  115. }
  116. public X509Extensions RequestExtensions
  117. {
  118. get { return X509Extensions.GetInstance(req.TbsRequest.RequestExtensions); }
  119. }
  120. protected override X509Extensions GetX509Extensions()
  121. {
  122. return RequestExtensions;
  123. }
  124. /**
  125. * return the object identifier representing the signature algorithm
  126. */
  127. public string SignatureAlgOid
  128. {
  129. get
  130. {
  131. if (!this.IsSigned)
  132. return null;
  133. return req.OptionalSignature.SignatureAlgorithm.Algorithm.Id;
  134. }
  135. }
  136. public byte[] GetSignature()
  137. {
  138. if (!this.IsSigned)
  139. return null;
  140. return req.OptionalSignature.GetSignatureOctets();
  141. }
  142. private IList GetCertList()
  143. {
  144. // load the certificates if we have any
  145. IList certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  146. Asn1Sequence s = req.OptionalSignature.Certs;
  147. if (s != null)
  148. {
  149. foreach (Asn1Encodable ae in s)
  150. {
  151. try
  152. {
  153. certs.Add(new X509CertificateParser().ReadCertificate(ae.GetEncoded()));
  154. }
  155. catch (Exception e)
  156. {
  157. throw new OcspException("can't re-encode certificate!", e);
  158. }
  159. }
  160. }
  161. return certs;
  162. }
  163. public X509Certificate[] GetCerts()
  164. {
  165. if (!this.IsSigned)
  166. return null;
  167. IList certs = this.GetCertList();
  168. X509Certificate[] result = new X509Certificate[certs.Count];
  169. for (int i = 0; i < certs.Count; ++i)
  170. {
  171. result[i] = (X509Certificate)certs[i];
  172. }
  173. return result;
  174. }
  175. /**
  176. * If the request is signed return a possibly empty CertStore containing the certificates in the
  177. * request. If the request is not signed the method returns null.
  178. *
  179. * @return null if not signed, a CertStore otherwise
  180. * @throws OcspException
  181. */
  182. public IX509Store GetCertificates(
  183. string type)
  184. {
  185. if (!this.IsSigned)
  186. return null;
  187. try
  188. {
  189. return X509StoreFactory.Create(
  190. "Certificate/" + type,
  191. new X509CollectionStoreParameters(this.GetCertList()));
  192. }
  193. catch (Exception e)
  194. {
  195. throw new OcspException("can't setup the CertStore", e);
  196. }
  197. }
  198. /**
  199. * Return whether or not this request is signed.
  200. *
  201. * @return true if signed false otherwise.
  202. */
  203. public bool IsSigned
  204. {
  205. get { return req.OptionalSignature != null; }
  206. }
  207. /**
  208. * Verify the signature against the TBSRequest object we contain.
  209. */
  210. public bool Verify(
  211. AsymmetricKeyParameter publicKey)
  212. {
  213. if (!this.IsSigned)
  214. throw new OcspException("attempt to Verify signature on unsigned object");
  215. try
  216. {
  217. ISigner signature = SignerUtilities.GetSigner(this.SignatureAlgOid);
  218. signature.Init(false, publicKey);
  219. byte[] encoded = req.TbsRequest.GetEncoded();
  220. signature.BlockUpdate(encoded, 0, encoded.Length);
  221. return signature.VerifySignature(this.GetSignature());
  222. }
  223. catch (Exception e)
  224. {
  225. throw new OcspException("exception processing sig: " + e, e);
  226. }
  227. }
  228. /**
  229. * return the ASN.1 encoded representation of this object.
  230. */
  231. public byte[] GetEncoded()
  232. {
  233. return req.GetEncoded();
  234. }
  235. }
  236. }
  237. #pragma warning restore
  238. #endif