OCSPReq.cs 6.0 KB

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