CertificateStatus.cs 3.2 KB

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