CertificateStatusRequest.cs 2.9 KB

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