BasicOCSPResp.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /// <remarks>
  18. /// <code>
  19. /// BasicOcspResponse ::= SEQUENCE {
  20. /// tbsResponseData ResponseData,
  21. /// signatureAlgorithm AlgorithmIdentifier,
  22. /// signature BIT STRING,
  23. /// certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL
  24. /// }
  25. /// </code>
  26. /// </remarks>
  27. public class BasicOcspResp
  28. : X509ExtensionBase
  29. {
  30. private readonly BasicOcspResponse resp;
  31. private readonly ResponseData data;
  32. // private readonly X509Certificate[] chain;
  33. public BasicOcspResp(
  34. BasicOcspResponse resp)
  35. {
  36. this.resp = resp;
  37. this.data = resp.TbsResponseData;
  38. }
  39. /// <returns>The DER encoding of the tbsResponseData field.</returns>
  40. /// <exception cref="OcspException">In the event of an encoding error.</exception>
  41. public byte[] GetTbsResponseData()
  42. {
  43. try
  44. {
  45. return data.GetDerEncoded();
  46. }
  47. catch (IOException e)
  48. {
  49. throw new OcspException("problem encoding tbsResponseData", e);
  50. }
  51. }
  52. public int Version
  53. {
  54. get { return data.Version.IntValueExact + 1; }
  55. }
  56. public RespID ResponderId
  57. {
  58. get { return new RespID(data.ResponderID); }
  59. }
  60. public DateTime ProducedAt
  61. {
  62. get { return data.ProducedAt.ToDateTime(); }
  63. }
  64. public SingleResp[] Responses
  65. {
  66. get
  67. {
  68. Asn1Sequence s = data.Responses;
  69. SingleResp[] rs = new SingleResp[s.Count];
  70. for (int i = 0; i != rs.Length; i++)
  71. {
  72. rs[i] = new SingleResp(SingleResponse.GetInstance(s[i]));
  73. }
  74. return rs;
  75. }
  76. }
  77. public X509Extensions ResponseExtensions
  78. {
  79. get { return data.ResponseExtensions; }
  80. }
  81. protected override X509Extensions GetX509Extensions()
  82. {
  83. return ResponseExtensions;
  84. }
  85. public string SignatureAlgName
  86. {
  87. get { return OcspUtilities.GetAlgorithmName(resp.SignatureAlgorithm.Algorithm); }
  88. }
  89. public string SignatureAlgOid
  90. {
  91. get { return resp.SignatureAlgorithm.Algorithm.Id; }
  92. }
  93. public RespData GetResponseData()
  94. {
  95. return new RespData(data);
  96. }
  97. public byte[] GetSignature()
  98. {
  99. return resp.GetSignatureOctets();
  100. }
  101. private IList GetCertList()
  102. {
  103. // load the certificates and revocation lists if we have any
  104. IList certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  105. Asn1Sequence s = resp.Certs;
  106. if (s != null)
  107. {
  108. foreach (Asn1Encodable ae in s)
  109. {
  110. try
  111. {
  112. certs.Add(new X509CertificateParser().ReadCertificate(ae.GetEncoded()));
  113. }
  114. catch (IOException ex)
  115. {
  116. throw new OcspException("can't re-encode certificate!", ex);
  117. }
  118. catch (CertificateException ex)
  119. {
  120. throw new OcspException("can't re-encode certificate!", ex);
  121. }
  122. }
  123. }
  124. return certs;
  125. }
  126. public X509Certificate[] GetCerts()
  127. {
  128. IList certs = GetCertList();
  129. X509Certificate[] result = new X509Certificate[certs.Count];
  130. for (int i = 0; i < certs.Count; ++i)
  131. {
  132. result[i] = (X509Certificate)certs[i];
  133. }
  134. return result;
  135. }
  136. /// <returns>The certificates, if any, associated with the response.</returns>
  137. /// <exception cref="OcspException">In the event of an encoding error.</exception>
  138. public IX509Store GetCertificates(
  139. string type)
  140. {
  141. try
  142. {
  143. return X509StoreFactory.Create(
  144. "Certificate/" + type,
  145. new X509CollectionStoreParameters(this.GetCertList()));
  146. }
  147. catch (Exception e)
  148. {
  149. throw new OcspException("can't setup the CertStore", e);
  150. }
  151. }
  152. /// <summary>
  153. /// Verify the signature against the tbsResponseData object we contain.
  154. /// </summary>
  155. public bool Verify(
  156. AsymmetricKeyParameter publicKey)
  157. {
  158. try
  159. {
  160. ISigner signature = SignerUtilities.GetSigner(this.SignatureAlgName);
  161. signature.Init(false, publicKey);
  162. byte[] bs = data.GetDerEncoded();
  163. signature.BlockUpdate(bs, 0, bs.Length);
  164. return signature.VerifySignature(this.GetSignature());
  165. }
  166. catch (Exception e)
  167. {
  168. throw new OcspException("exception processing sig: " + e, e);
  169. }
  170. }
  171. /// <returns>The ASN.1 encoded representation of this object.</returns>
  172. public byte[] GetEncoded()
  173. {
  174. return resp.GetEncoded();
  175. }
  176. public override bool Equals(
  177. object obj)
  178. {
  179. if (obj == this)
  180. return true;
  181. BasicOcspResp other = obj as BasicOcspResp;
  182. if (other == null)
  183. return false;
  184. return resp.Equals(other.resp);
  185. }
  186. public override int GetHashCode()
  187. {
  188. return resp.GetHashCode();
  189. }
  190. }
  191. }
  192. #pragma warning restore
  193. #endif