AbstractTlsPeer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Tls.Crypto;
  6. namespace BestHTTP.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. // TODO[tls13] Enable TLSv13 by default in due course
  27. return ProtocolVersion.TLSv12.DownTo(ProtocolVersion.TLSv10);
  28. }
  29. protected abstract int[] GetSupportedCipherSuites();
  30. /// <exception cref="IOException"/>
  31. public virtual void Cancel()
  32. {
  33. TlsCloseable closeHandle = this.m_closeHandle;
  34. if (null != closeHandle)
  35. {
  36. closeHandle.Close();
  37. }
  38. }
  39. public virtual TlsCrypto Crypto
  40. {
  41. get { return m_crypto; }
  42. }
  43. public virtual void NotifyCloseHandle(TlsCloseable closeHandle)
  44. {
  45. this.m_closeHandle = closeHandle;
  46. }
  47. public abstract ProtocolVersion[] GetProtocolVersions();
  48. public abstract int[] GetCipherSuites();
  49. /// <exception cref="IOException"/>
  50. public virtual void NotifyHandshakeBeginning()
  51. {
  52. }
  53. public virtual int GetHandshakeTimeoutMillis()
  54. {
  55. return 0;
  56. }
  57. public virtual bool AllowLegacyResumption()
  58. {
  59. return false;
  60. }
  61. public virtual int GetMaxCertificateChainLength()
  62. {
  63. return 10;
  64. }
  65. public virtual int GetMaxHandshakeMessageSize()
  66. {
  67. return 32768;
  68. }
  69. public virtual short[] GetPskKeyExchangeModes()
  70. {
  71. return new short[]{ PskKeyExchangeMode.psk_dhe_ke };
  72. }
  73. public virtual bool RequiresCloseNotify()
  74. {
  75. return true;
  76. }
  77. public virtual bool RequiresExtendedMasterSecret()
  78. {
  79. return false;
  80. }
  81. public virtual bool ShouldCheckSigAlgOfPeerCerts()
  82. {
  83. return true;
  84. }
  85. public virtual bool ShouldUseExtendedMasterSecret()
  86. {
  87. return true;
  88. }
  89. public virtual bool ShouldUseExtendedPadding()
  90. {
  91. return false;
  92. }
  93. public virtual bool ShouldUseGmtUnixTime()
  94. {
  95. /*
  96. * draft-mathewson-no-gmtunixtime-00 2. For the reasons we discuss above, we recommend that
  97. * TLS implementors MUST by default set the entire value the ClientHello.Random and
  98. * ServerHello.Random fields, including gmt_unix_time, to a cryptographically random
  99. * sequence.
  100. */
  101. return false;
  102. }
  103. /// <exception cref="IOException"/>
  104. public virtual void NotifySecureRenegotiation(bool secureRenegotiation)
  105. {
  106. if (!secureRenegotiation)
  107. throw new TlsFatalAlert(AlertDescription.handshake_failure);
  108. }
  109. /// <exception cref="IOException"/>
  110. public virtual TlsKeyExchangeFactory GetKeyExchangeFactory()
  111. {
  112. return new DefaultTlsKeyExchangeFactory();
  113. }
  114. public virtual void NotifyAlertRaised(short alertLevel, short alertDescription, string message,
  115. Exception cause)
  116. {
  117. }
  118. public virtual void NotifyAlertReceived(short alertLevel, short alertDescription)
  119. {
  120. }
  121. /// <exception cref="IOException"/>
  122. public virtual void NotifyHandshakeComplete()
  123. {
  124. }
  125. public virtual TlsHeartbeat GetHeartbeat()
  126. {
  127. return null;
  128. }
  129. public virtual short GetHeartbeatPolicy()
  130. {
  131. return HeartbeatMode.peer_not_allowed_to_send;
  132. }
  133. }
  134. }
  135. #pragma warning restore
  136. #endif