AbstractTls13Client.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls;
  7. using BestHTTP.Logger;
  8. namespace BestHTTP.Connections.TLS
  9. {
  10. public abstract class AbstractTls13Client : AbstractTlsClient, TlsAuthentication
  11. {
  12. protected static readonly int[] DefaultCipherSuites = new int[] {
  13. /*
  14. * TLS 1.3
  15. */
  16. CipherSuite.TLS_CHACHA20_POLY1305_SHA256,
  17. CipherSuite.TLS_AES_256_GCM_SHA384,
  18. CipherSuite.TLS_AES_128_GCM_SHA256,
  19. /*
  20. * pre-TLS 1.3
  21. */
  22. CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
  23. CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  24. CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
  25. CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  26. CipherSuite.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  27. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  28. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  29. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  30. CipherSuite.TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  31. CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
  32. CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
  33. CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
  34. CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256,
  35. CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256,
  36. CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
  37. };
  38. protected HTTPRequest _request;
  39. protected List<ServerName> _sniServerNames;
  40. protected List<ProtocolName> _protocols;
  41. protected LoggingContext Context { get; private set; }
  42. protected AbstractTls13Client(HTTPRequest request, List<ServerName> sniServerNames, List<ProtocolName> protocols, TlsCrypto crypto)
  43. : base(crypto)
  44. {
  45. this._request = request;
  46. // get the request's logging context. The context has no reference to the request, so it won't keep it in memory.
  47. this.Context = this._request.Context;
  48. this._sniServerNames = sniServerNames;
  49. this._protocols = protocols;
  50. }
  51. /// <summary>
  52. /// TCPConnector has to know what protocol got negotiated
  53. /// </summary>
  54. public string GetNegotiatedApplicationProtocol() => base.m_context.SecurityParameters.ApplicationProtocol?.GetUtf8Decoding();
  55. // (Abstract)TLSClient facing functions
  56. protected override ProtocolVersion[] GetSupportedVersions() => ProtocolVersion.TLSv13.DownTo(ProtocolVersion.TLSv12);
  57. protected override IList GetProtocolNames() => this._protocols;
  58. protected override IList GetSniServerNames() => this._sniServerNames;
  59. protected override int[] GetSupportedCipherSuites()
  60. {
  61. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(GetSupportedCipherSuites)}", this.Context);
  62. return TlsUtilities.GetSupportedCipherSuites(Crypto, DefaultCipherSuites);
  63. }
  64. // TlsAuthentication implementation
  65. public override TlsAuthentication GetAuthentication()
  66. {
  67. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(GetAuthentication)}", this.Context);
  68. return this;
  69. }
  70. public virtual TlsCredentials GetClientCredentials(CertificateRequest certificateRequest)
  71. {
  72. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(GetClientCredentials)}", this.Context);
  73. return null;
  74. }
  75. public virtual void NotifyServerCertificate(TlsServerCertificate serverCertificate)
  76. {
  77. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifyServerCertificate)}", this.Context);
  78. }
  79. public override void NotifyAlertReceived(short alertLevel, short alertDescription)
  80. {
  81. base.NotifyAlertReceived(alertLevel, alertDescription);
  82. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifyAlertReceived)}({alertLevel}, {alertDescription})", this.Context);
  83. }
  84. public override void NotifyAlertRaised(short alertLevel, short alertDescription, string message, Exception cause)
  85. {
  86. base.NotifyAlertRaised(alertLevel, alertDescription, message, cause);
  87. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifyAlertRaised)}({alertLevel}, {alertDescription}, {message}, {cause?.StackTrace})", this.Context);
  88. }
  89. public override void NotifyHandshakeBeginning()
  90. {
  91. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifyHandshakeBeginning)}", this.Context);
  92. }
  93. public override void NotifyHandshakeComplete()
  94. {
  95. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifyHandshakeComplete)}", this.Context);
  96. this._request = null;
  97. }
  98. public override void NotifyNewSessionTicket(NewSessionTicket newSessionTicket)
  99. {
  100. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifyNewSessionTicket)}", this.Context);
  101. base.NotifyNewSessionTicket(newSessionTicket);
  102. }
  103. public override void NotifySecureRenegotiation(bool secureRenegotiation)
  104. {
  105. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifySecureRenegotiation)}", this.Context);
  106. base.NotifySecureRenegotiation(secureRenegotiation);
  107. }
  108. public override void NotifySelectedCipherSuite(int selectedCipherSuite)
  109. {
  110. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifySelectedCipherSuite)}({selectedCipherSuite})", this.Context);
  111. base.NotifySelectedCipherSuite(selectedCipherSuite);
  112. }
  113. public override void NotifySelectedPsk(TlsPsk selectedPsk)
  114. {
  115. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifySelectedPsk)}({selectedPsk?.PrfAlgorithm})", this.Context);
  116. base.NotifySelectedPsk(selectedPsk);
  117. }
  118. public override void NotifyServerVersion(ProtocolVersion serverVersion)
  119. {
  120. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifyServerVersion)}({serverVersion})", this.Context);
  121. base.NotifyServerVersion(serverVersion);
  122. }
  123. public override void NotifySessionID(byte[] sessionID)
  124. {
  125. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifySessionID)}", this.Context);
  126. base.NotifySessionID(sessionID);
  127. }
  128. public override void NotifySessionToResume(TlsSession session)
  129. {
  130. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(NotifySessionToResume)}", this.Context);
  131. base.NotifySessionToResume(session);
  132. }
  133. public override void ProcessServerExtensions(IDictionary serverExtensions)
  134. {
  135. HTTPManager.Logger.Information(nameof(AbstractTls13Client), $"{nameof(ProcessServerExtensions)}", this.Context);
  136. base.ProcessServerExtensions(serverExtensions);
  137. }
  138. }
  139. }
  140. #endif