OcspStatusRequest.cs 4.6 KB

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