CertificateStatusRequest.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  6. {
  7. public class CertificateStatusRequest
  8. {
  9. protected readonly byte mStatusType;
  10. protected readonly object mRequest;
  11. public CertificateStatusRequest(byte statusType, Object request)
  12. {
  13. if (!IsCorrectType(statusType, request))
  14. throw new ArgumentException("not an instance of the correct type", "request");
  15. this.mStatusType = statusType;
  16. this.mRequest = request;
  17. }
  18. public virtual byte StatusType
  19. {
  20. get { return mStatusType; }
  21. }
  22. public virtual object Request
  23. {
  24. get { return mRequest; }
  25. }
  26. public virtual OcspStatusRequest GetOcspStatusRequest()
  27. {
  28. if (!IsCorrectType(CertificateStatusType.ocsp, mRequest))
  29. throw new InvalidOperationException("'request' is not an OCSPStatusRequest");
  30. return (OcspStatusRequest)mRequest;
  31. }
  32. /**
  33. * Encode this {@link CertificateStatusRequest} to a {@link Stream}.
  34. *
  35. * @param output
  36. * the {@link Stream} to encode to.
  37. * @throws IOException
  38. */
  39. public virtual void Encode(Stream output)
  40. {
  41. TlsUtilities.WriteUint8(mStatusType, output);
  42. switch (mStatusType)
  43. {
  44. case CertificateStatusType.ocsp:
  45. ((OcspStatusRequest)mRequest).Encode(output);
  46. break;
  47. default:
  48. throw new TlsFatalAlert(AlertDescription.internal_error);
  49. }
  50. }
  51. /**
  52. * Parse a {@link CertificateStatusRequest} from a {@link Stream}.
  53. *
  54. * @param input
  55. * the {@link Stream} to parse from.
  56. * @return a {@link CertificateStatusRequest} object.
  57. * @throws IOException
  58. */
  59. public static CertificateStatusRequest Parse(Stream input)
  60. {
  61. byte status_type = TlsUtilities.ReadUint8(input);
  62. object result;
  63. switch (status_type)
  64. {
  65. case CertificateStatusType.ocsp:
  66. result = OcspStatusRequest.Parse(input);
  67. break;
  68. default:
  69. throw new TlsFatalAlert(AlertDescription.decode_error);
  70. }
  71. return new CertificateStatusRequest(status_type, result);
  72. }
  73. protected static bool IsCorrectType(byte statusType, object request)
  74. {
  75. switch (statusType)
  76. {
  77. case CertificateStatusType.ocsp:
  78. return request is OcspStatusRequest;
  79. default:
  80. throw new ArgumentException("unsupported CertificateStatusType", "statusType");
  81. }
  82. }
  83. }
  84. }
  85. #pragma warning restore
  86. #endif