BasicOCSPResp.cs 5.2 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.Value.IntValue + 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. [Obsolete("RespData class is no longer required as all functionality is available on this class")]
  94. public RespData GetResponseData()
  95. {
  96. return new RespData(data);
  97. }
  98. public byte[] GetSignature()
  99. {
  100. return resp.GetSignatureOctets();
  101. }
  102. private IList GetCertList()
  103. {
  104. // load the certificates and revocation lists if we have any
  105. IList certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  106. Asn1Sequence s = resp.Certs;
  107. if (s != null)
  108. {
  109. foreach (Asn1Encodable ae in s)
  110. {
  111. try
  112. {
  113. certs.Add(new X509CertificateParser().ReadCertificate(ae.GetEncoded()));
  114. }
  115. catch (IOException ex)
  116. {
  117. throw new OcspException("can't re-encode certificate!", ex);
  118. }
  119. catch (CertificateException ex)
  120. {
  121. throw new OcspException("can't re-encode certificate!", ex);
  122. }
  123. }
  124. }
  125. return certs;
  126. }
  127. public X509Certificate[] GetCerts()
  128. {
  129. IList certs = GetCertList();
  130. X509Certificate[] result = new X509Certificate[certs.Count];
  131. for (int i = 0; i < certs.Count; ++i)
  132. {
  133. result[i] = (X509Certificate)certs[i];
  134. }
  135. return result;
  136. }
  137. /// <returns>The certificates, if any, associated with the response.</returns>
  138. /// <exception cref="OcspException">In the event of an encoding error.</exception>
  139. public IX509Store GetCertificates(
  140. string type)
  141. {
  142. try
  143. {
  144. return X509StoreFactory.Create(
  145. "Certificate/" + type,
  146. new X509CollectionStoreParameters(this.GetCertList()));
  147. }
  148. catch (Exception e)
  149. {
  150. throw new OcspException("can't setup the CertStore", e);
  151. }
  152. }
  153. /// <summary>
  154. /// Verify the signature against the tbsResponseData object we contain.
  155. /// </summary>
  156. public bool Verify(
  157. AsymmetricKeyParameter publicKey)
  158. {
  159. try
  160. {
  161. ISigner signature = SignerUtilities.GetSigner(this.SignatureAlgName);
  162. signature.Init(false, publicKey);
  163. byte[] bs = data.GetDerEncoded();
  164. signature.BlockUpdate(bs, 0, bs.Length);
  165. return signature.VerifySignature(this.GetSignature());
  166. }
  167. catch (Exception e)
  168. {
  169. throw new OcspException("exception processing sig: " + e, e);
  170. }
  171. }
  172. /// <returns>The ASN.1 encoded representation of this object.</returns>
  173. public byte[] GetEncoded()
  174. {
  175. return resp.GetEncoded();
  176. }
  177. public override bool Equals(
  178. object obj)
  179. {
  180. if (obj == this)
  181. return true;
  182. BasicOcspResp other = obj as BasicOcspResp;
  183. if (other == null)
  184. return false;
  185. return resp.Equals(other.resp);
  186. }
  187. public override int GetHashCode()
  188. {
  189. return resp.GetHashCode();
  190. }
  191. }
  192. }
  193. #pragma warning restore
  194. #endif