CertificateStatus.cs 3.1 KB

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