CertificateStatus.cs 8.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.Utilities;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  10. {
  11. public sealed class CertificateStatus
  12. {
  13. private readonly short m_statusType;
  14. private readonly object m_response;
  15. public CertificateStatus(short statusType, object response)
  16. {
  17. if (!IsCorrectType(statusType, response))
  18. throw new ArgumentException("not an instance of the correct type", "response");
  19. this.m_statusType = statusType;
  20. this.m_response = response;
  21. }
  22. public short StatusType
  23. {
  24. get { return m_statusType; }
  25. }
  26. public object Response
  27. {
  28. get { return m_response; }
  29. }
  30. public OcspResponse OcspResponse
  31. {
  32. get
  33. {
  34. if (!IsCorrectType(CertificateStatusType.ocsp, m_response))
  35. throw new InvalidOperationException("'response' is not an OCSPResponse");
  36. return (OcspResponse)m_response;
  37. }
  38. }
  39. /// <summary>an <see cref="IList"/> of (possibly null) <see cref="Asn1.Ocsp.OcspResponse"/>.</summary>
  40. public IList OcspResponseList
  41. {
  42. get
  43. {
  44. if (!IsCorrectType(CertificateStatusType.ocsp_multi, m_response))
  45. throw new InvalidOperationException("'response' is not an OCSPResponseList");
  46. return (IList)m_response;
  47. }
  48. }
  49. /// <summary>Encode this <see cref="CertificateStatus"/> to a <see cref="Stream"/>.</summary>
  50. /// <param name="output">the <see cref="Stream"/> to encode to.</param>
  51. /// <exception cref="IOException"/>
  52. public void Encode(Stream output)
  53. {
  54. TlsUtilities.WriteUint8(m_statusType, output);
  55. switch (m_statusType)
  56. {
  57. case CertificateStatusType.ocsp:
  58. {
  59. OcspResponse ocspResponse = (OcspResponse)m_response;
  60. byte[] derEncoding = ocspResponse.GetEncoded(Asn1Encodable.Der);
  61. TlsUtilities.WriteOpaque24(derEncoding, output);
  62. break;
  63. }
  64. case CertificateStatusType.ocsp_multi:
  65. {
  66. IList ocspResponseList = (IList)m_response;
  67. int count = ocspResponseList.Count;
  68. IList derEncodings = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(count);
  69. long totalLength = 0;
  70. foreach (OcspResponse ocspResponse in ocspResponseList)
  71. {
  72. if (ocspResponse == null)
  73. {
  74. derEncodings.Add(TlsUtilities.EmptyBytes);
  75. }
  76. else
  77. {
  78. byte[] derEncoding = ocspResponse.GetEncoded(Asn1Encodable.Der);
  79. derEncodings.Add(derEncoding);
  80. totalLength += derEncoding.Length;
  81. }
  82. totalLength += 3;
  83. }
  84. TlsUtilities.CheckUint24(totalLength);
  85. TlsUtilities.WriteUint24((int)totalLength, output);
  86. foreach (byte[] derEncoding in derEncodings)
  87. {
  88. TlsUtilities.WriteOpaque24(derEncoding, output);
  89. }
  90. break;
  91. }
  92. default:
  93. throw new TlsFatalAlert(AlertDescription.internal_error);
  94. }
  95. }
  96. /// <summary>Parse a <see cref="CertificateStatus"/> from a <see cref="Stream"/>.</summary>
  97. /// <param name="context">the <see cref="TlsContext"/> of the current connection.</param>
  98. /// <param name="input">the <see cref="Stream"/> to parse from.</param>
  99. /// <returns>a <see cref="CertificateStatus"/> object.</returns>
  100. /// <exception cref="IOException"/>
  101. public static CertificateStatus Parse(TlsContext context, Stream input)
  102. {
  103. SecurityParameters securityParameters = context.SecurityParameters;
  104. Certificate peerCertificate = securityParameters.PeerCertificate;
  105. if (null == peerCertificate || peerCertificate.IsEmpty
  106. || CertificateType.X509 != peerCertificate.CertificateType)
  107. {
  108. throw new TlsFatalAlert(AlertDescription.internal_error);
  109. }
  110. int certificateCount = peerCertificate.Length;
  111. int statusRequestVersion = securityParameters.StatusRequestVersion;
  112. short status_type = TlsUtilities.ReadUint8(input);
  113. object response;
  114. switch (status_type)
  115. {
  116. case CertificateStatusType.ocsp:
  117. {
  118. RequireStatusRequestVersion(1, statusRequestVersion);
  119. byte[] derEncoding = TlsUtilities.ReadOpaque24(input, 1);
  120. Asn1Object derObject = TlsUtilities.ReadDerObject(derEncoding);
  121. response = OcspResponse.GetInstance(derObject);
  122. break;
  123. }
  124. case CertificateStatusType.ocsp_multi:
  125. {
  126. RequireStatusRequestVersion(2, statusRequestVersion);
  127. byte[] ocsp_response_list = TlsUtilities.ReadOpaque24(input, 1);
  128. MemoryStream buf = new MemoryStream(ocsp_response_list, false);
  129. IList ocspResponseList = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  130. while (buf.Position < buf.Length)
  131. {
  132. if (ocspResponseList.Count >= certificateCount)
  133. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  134. int length = TlsUtilities.ReadUint24(buf);
  135. if (length < 1)
  136. {
  137. ocspResponseList.Add(null);
  138. }
  139. else
  140. {
  141. byte[] derEncoding = TlsUtilities.ReadFully(length, buf);
  142. Asn1Object derObject = TlsUtilities.ReadDerObject(derEncoding);
  143. OcspResponse ocspResponse = OcspResponse.GetInstance(derObject);
  144. ocspResponseList.Add(ocspResponse);
  145. }
  146. }
  147. // Match IList capacity to actual size
  148. response = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(ocspResponseList);
  149. break;
  150. }
  151. default:
  152. throw new TlsFatalAlert(AlertDescription.decode_error);
  153. }
  154. return new CertificateStatus(status_type, response);
  155. }
  156. private static bool IsCorrectType(short statusType, Object response)
  157. {
  158. switch (statusType)
  159. {
  160. case CertificateStatusType.ocsp:
  161. return response is OcspResponse;
  162. case CertificateStatusType.ocsp_multi:
  163. return IsOcspResponseList(response);
  164. default:
  165. throw new ArgumentException("unsupported CertificateStatusType", "statusType");
  166. }
  167. }
  168. private static bool IsOcspResponseList(object response)
  169. {
  170. if (!(response is IList))
  171. return false;
  172. IList v = (IList)response;
  173. int count = v.Count;
  174. if (count < 1)
  175. return false;
  176. foreach (object e in v)
  177. {
  178. if (null != e && !(e is OcspResponse))
  179. return false;
  180. }
  181. return true;
  182. }
  183. /// <exception cref="IOException"/>
  184. private static void RequireStatusRequestVersion(int minVersion, int statusRequestVersion)
  185. {
  186. if (statusRequestVersion < minVersion)
  187. throw new TlsFatalAlert(AlertDescription.decode_error);
  188. }
  189. }
  190. }
  191. #pragma warning restore
  192. #endif