DefaultTlsServer.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. public abstract class DefaultTlsServer
  9. : AbstractTlsServer
  10. {
  11. private static readonly int[] DefaultCipherSuites = new int[]
  12. {
  13. /*
  14. * TODO[tls13] 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_RSA_WITH_CHACHA20_POLY1305_SHA256,
  23. CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  24. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  25. CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
  26. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  27. CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  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_256_GCM_SHA384,
  31. CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
  32. CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
  33. CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
  34. CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
  35. CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
  36. CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384,
  37. CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256,
  38. CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256,
  39. CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256,
  40. CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA,
  41. CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
  42. };
  43. public DefaultTlsServer(TlsCrypto crypto)
  44. : base(crypto)
  45. {
  46. }
  47. /// <exception cref="IOException"/>
  48. protected virtual TlsCredentialedSigner GetDsaSignerCredentials()
  49. {
  50. throw new TlsFatalAlert(AlertDescription.internal_error);
  51. }
  52. /// <exception cref="IOException"/>
  53. protected virtual TlsCredentialedSigner GetECDsaSignerCredentials()
  54. {
  55. throw new TlsFatalAlert(AlertDescription.internal_error);
  56. }
  57. /// <exception cref="IOException"/>
  58. protected virtual TlsCredentialedDecryptor GetRsaEncryptionCredentials()
  59. {
  60. throw new TlsFatalAlert(AlertDescription.internal_error);
  61. }
  62. /// <exception cref="IOException"/>
  63. protected virtual TlsCredentialedSigner GetRsaSignerCredentials()
  64. {
  65. throw new TlsFatalAlert(AlertDescription.internal_error);
  66. }
  67. protected override int[] GetSupportedCipherSuites()
  68. {
  69. return TlsUtilities.GetSupportedCipherSuites(Crypto, DefaultCipherSuites);
  70. }
  71. public override TlsCredentials GetCredentials()
  72. {
  73. int keyExchangeAlgorithm = m_context.SecurityParameters.KeyExchangeAlgorithm;
  74. switch (keyExchangeAlgorithm)
  75. {
  76. case KeyExchangeAlgorithm.DHE_DSS:
  77. return GetDsaSignerCredentials();
  78. case KeyExchangeAlgorithm.DH_anon:
  79. case KeyExchangeAlgorithm.ECDH_anon:
  80. return null;
  81. case KeyExchangeAlgorithm.ECDHE_ECDSA:
  82. return GetECDsaSignerCredentials();
  83. case KeyExchangeAlgorithm.DHE_RSA:
  84. case KeyExchangeAlgorithm.ECDHE_RSA:
  85. return GetRsaSignerCredentials();
  86. case KeyExchangeAlgorithm.RSA:
  87. return GetRsaEncryptionCredentials();
  88. default:
  89. // Note: internal error here; selected a key exchange we don't implement!
  90. throw new TlsFatalAlert(AlertDescription.internal_error);
  91. }
  92. }
  93. }
  94. }
  95. #pragma warning restore
  96. #endif