OcspStatusRequest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.Utilities;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  12. {
  13. /// <summary>RFC 3546 3.6</summary>
  14. public sealed class OcspStatusRequest
  15. {
  16. private readonly IList m_responderIDList;
  17. private readonly X509Extensions m_requestExtensions;
  18. /// <param name="responderIDList">an <see cref="IList"/> of <see cref="ResponderID"/>, specifying the list of
  19. /// trusted OCSP responders. An empty list has the special meaning that the responders are implicitly known to
  20. /// the server - e.g., by prior arrangement.</param>
  21. /// <param name="requestExtensions">OCSP request extensions. A null value means that there are no extensions.
  22. /// </param>
  23. public OcspStatusRequest(IList responderIDList, X509Extensions requestExtensions)
  24. {
  25. this.m_responderIDList = responderIDList;
  26. this.m_requestExtensions = requestExtensions;
  27. }
  28. /// <returns>an <see cref="IList"/> of <see cref="ResponderID"/>.</returns>
  29. public IList ResponderIDList
  30. {
  31. get { return m_responderIDList; }
  32. }
  33. /// <returns>OCSP request extensions.</returns>
  34. public X509Extensions RequestExtensions
  35. {
  36. get { return m_requestExtensions; }
  37. }
  38. /// <summary>Encode this <see cref="OcspStatusRequest"/> to a <see cref="Stream"/>.</summary>
  39. /// <param name="output">the <see cref="Stream"/> to encode to.</param>
  40. /// <exception cref="IOException"/>
  41. public void Encode(Stream output)
  42. {
  43. if (m_responderIDList == null || m_responderIDList.Count < 1)
  44. {
  45. TlsUtilities.WriteUint16(0, output);
  46. }
  47. else
  48. {
  49. MemoryStream buf = new MemoryStream();
  50. foreach (ResponderID responderID in m_responderIDList)
  51. {
  52. byte[] derEncoding = responderID.GetEncoded(Asn1Encodable.Der);
  53. TlsUtilities.WriteOpaque16(derEncoding, buf);
  54. }
  55. TlsUtilities.CheckUint16(buf.Length);
  56. TlsUtilities.WriteUint16((int)buf.Length, output);
  57. Streams.WriteBufTo(buf, output);
  58. }
  59. if (m_requestExtensions == null)
  60. {
  61. TlsUtilities.WriteUint16(0, output);
  62. }
  63. else
  64. {
  65. byte[] derEncoding = m_requestExtensions.GetEncoded(Asn1Encodable.Der);
  66. TlsUtilities.CheckUint16(derEncoding.Length);
  67. TlsUtilities.WriteUint16(derEncoding.Length, output);
  68. output.Write(derEncoding, 0, derEncoding.Length);
  69. }
  70. }
  71. /// <summary>Parse an <see cref="OcspStatusRequest"/> from a <see cref="Stream"/>.</summary>
  72. /// <param name="input">the <see cref="Stream"/> to parse from.</param>
  73. /// <returns>an <see cref="OcspStatusRequest"/> object.</returns>
  74. /// <exception cref="IOException"/>
  75. public static OcspStatusRequest Parse(Stream input)
  76. {
  77. IList responderIDList = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  78. {
  79. byte[] data = TlsUtilities.ReadOpaque16(input);
  80. if (data.Length > 0)
  81. {
  82. MemoryStream buf = new MemoryStream(data, false);
  83. do
  84. {
  85. byte[] derEncoding = TlsUtilities.ReadOpaque16(buf, 1);
  86. ResponderID responderID = ResponderID.GetInstance(TlsUtilities.ReadDerObject(derEncoding));
  87. responderIDList.Add(responderID);
  88. }
  89. while (buf.Position < buf.Length);
  90. }
  91. }
  92. X509Extensions requestExtensions = null;
  93. {
  94. byte[] derEncoding = TlsUtilities.ReadOpaque16(input);
  95. if (derEncoding.Length > 0)
  96. {
  97. requestExtensions = X509Extensions.GetInstance(TlsUtilities.ReadDerObject(derEncoding));
  98. }
  99. }
  100. return new OcspStatusRequest(responderIDList, requestExtensions);
  101. }
  102. }
  103. }
  104. #pragma warning restore
  105. #endif