AbstractTlsPeer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls
  7. {
  8. /// <summary>Base class for a TLS client or server.</summary>
  9. public abstract class AbstractTlsPeer
  10. : TlsPeer
  11. {
  12. private readonly TlsCrypto m_crypto;
  13. private volatile TlsCloseable m_closeHandle;
  14. protected AbstractTlsPeer(TlsCrypto crypto)
  15. {
  16. this.m_crypto = crypto;
  17. }
  18. /// <summary>Get the <see cref="ProtocolVersion"/> values that are supported by this peer.</summary>
  19. /// <remarks>
  20. /// WARNING: Mixing DTLS and TLS versions in the returned array is currently NOT supported. Use a separate
  21. /// (sub-)class for each case.
  22. /// </remarks>
  23. /// <returns>an array of supported <see cref="ProtocolVersion"/> values.</returns>
  24. protected virtual ProtocolVersion[] GetSupportedVersions()
  25. {
  26. return ProtocolVersion.TLSv13.DownTo(ProtocolVersion.TLSv12);
  27. }
  28. protected abstract int[] GetSupportedCipherSuites();
  29. /// <exception cref="IOException"/>
  30. public virtual void Cancel()
  31. {
  32. TlsCloseable closeHandle = this.m_closeHandle;
  33. if (null != closeHandle)
  34. {
  35. closeHandle.Close();
  36. }
  37. }
  38. public virtual TlsCrypto Crypto
  39. {
  40. get { return m_crypto; }
  41. }
  42. public virtual void NotifyCloseHandle(TlsCloseable closeHandle)
  43. {
  44. this.m_closeHandle = closeHandle;
  45. }
  46. public abstract ProtocolVersion[] GetProtocolVersions();
  47. public abstract int[] GetCipherSuites();
  48. /// <exception cref="IOException"/>
  49. public virtual void NotifyHandshakeBeginning()
  50. {
  51. }
  52. public virtual int GetHandshakeTimeoutMillis()
  53. {
  54. return 0;
  55. }
  56. public virtual bool AllowLegacyResumption()
  57. {
  58. return false;
  59. }
  60. public virtual int GetMaxCertificateChainLength()
  61. {
  62. return 10;
  63. }
  64. public virtual int GetMaxHandshakeMessageSize()
  65. {
  66. return 32768;
  67. }
  68. public virtual short[] GetPskKeyExchangeModes()
  69. {
  70. return new short[]{ PskKeyExchangeMode.psk_dhe_ke };
  71. }
  72. public virtual bool RequiresCloseNotify()
  73. {
  74. return true;
  75. }
  76. public virtual bool RequiresExtendedMasterSecret()
  77. {
  78. return false;
  79. }
  80. public virtual bool ShouldCheckSigAlgOfPeerCerts()
  81. {
  82. return true;
  83. }
  84. public virtual bool ShouldUseExtendedMasterSecret()
  85. {
  86. return true;
  87. }
  88. public virtual bool ShouldUseExtendedPadding()
  89. {
  90. return false;
  91. }
  92. public virtual bool ShouldUseGmtUnixTime()
  93. {
  94. /*
  95. * draft-mathewson-no-gmtunixtime-00 2. For the reasons we discuss above, we recommend that
  96. * TLS implementors MUST by default set the entire value the ClientHello.Random and
  97. * ServerHello.Random fields, including gmt_unix_time, to a cryptographically random
  98. * sequence.
  99. */
  100. return false;
  101. }
  102. /// <exception cref="IOException"/>
  103. public virtual void NotifySecureRenegotiation(bool secureRenegotiation)
  104. {
  105. if (!secureRenegotiation)
  106. throw new TlsFatalAlert(AlertDescription.handshake_failure);
  107. }
  108. /// <exception cref="IOException"/>
  109. public virtual TlsKeyExchangeFactory GetKeyExchangeFactory()
  110. {
  111. return new DefaultTlsKeyExchangeFactory();
  112. }
  113. public virtual void NotifyAlertRaised(short alertLevel, short alertDescription, string message,
  114. Exception cause)
  115. {
  116. }
  117. public virtual void NotifyAlertReceived(short alertLevel, short alertDescription)
  118. {
  119. }
  120. /// <exception cref="IOException"/>
  121. public virtual void NotifyHandshakeComplete()
  122. {
  123. }
  124. public virtual TlsHeartbeat GetHeartbeat()
  125. {
  126. return null;
  127. }
  128. public virtual short GetHeartbeatPolicy()
  129. {
  130. return HeartbeatMode.peer_not_allowed_to_send;
  131. }
  132. public virtual bool IgnoreCorruptDtlsRecords => false;
  133. }
  134. }
  135. #pragma warning restore
  136. #endif