OcspStatusRequest.cs 4.8 KB

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