BasicOCSPResp.cs 4.3 KB

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