AbstractTls13Client.cs 7.1 KB

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