DefaultTlsServer.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  9. {
  10. public abstract class DefaultTlsServer
  11. : AbstractTlsServer
  12. {
  13. public DefaultTlsServer()
  14. : base()
  15. {
  16. }
  17. public DefaultTlsServer(TlsCipherFactory cipherFactory)
  18. : base(cipherFactory)
  19. {
  20. }
  21. protected virtual TlsSignerCredentials GetDsaSignerCredentials()
  22. {
  23. throw new TlsFatalAlert(AlertDescription.internal_error);
  24. }
  25. protected virtual TlsSignerCredentials GetECDsaSignerCredentials()
  26. {
  27. throw new TlsFatalAlert(AlertDescription.internal_error);
  28. }
  29. protected virtual TlsEncryptionCredentials GetRsaEncryptionCredentials()
  30. {
  31. throw new TlsFatalAlert(AlertDescription.internal_error);
  32. }
  33. protected virtual TlsSignerCredentials GetRsaSignerCredentials()
  34. {
  35. throw new TlsFatalAlert(AlertDescription.internal_error);
  36. }
  37. protected virtual DHParameters GetDHParameters()
  38. {
  39. return DHStandardGroups.rfc7919_ffdhe2048;
  40. }
  41. protected override int[] GetCipherSuites()
  42. {
  43. return new int[]
  44. {
  45. CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  46. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  47. CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
  48. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  49. CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  50. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  51. CipherSuite.TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
  52. CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
  53. CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
  54. CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
  55. CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
  56. CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
  57. CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384,
  58. CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256,
  59. CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256,
  60. CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256,
  61. CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA,
  62. CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
  63. };
  64. }
  65. public override TlsCredentials GetCredentials()
  66. {
  67. int keyExchangeAlgorithm = TlsUtilities.GetKeyExchangeAlgorithm(mSelectedCipherSuite);
  68. switch (keyExchangeAlgorithm)
  69. {
  70. case KeyExchangeAlgorithm.DHE_DSS:
  71. return GetDsaSignerCredentials();
  72. case KeyExchangeAlgorithm.DH_anon:
  73. case KeyExchangeAlgorithm.ECDH_anon:
  74. return null;
  75. case KeyExchangeAlgorithm.ECDHE_ECDSA:
  76. return GetECDsaSignerCredentials();
  77. case KeyExchangeAlgorithm.DHE_RSA:
  78. case KeyExchangeAlgorithm.ECDHE_RSA:
  79. return GetRsaSignerCredentials();
  80. case KeyExchangeAlgorithm.RSA:
  81. return GetRsaEncryptionCredentials();
  82. default:
  83. /* Note: internal error here; selected a key exchange we don't implement! */
  84. throw new TlsFatalAlert(AlertDescription.internal_error);
  85. }
  86. }
  87. public override TlsKeyExchange GetKeyExchange()
  88. {
  89. int keyExchangeAlgorithm = TlsUtilities.GetKeyExchangeAlgorithm(mSelectedCipherSuite);
  90. switch (keyExchangeAlgorithm)
  91. {
  92. case KeyExchangeAlgorithm.DH_anon:
  93. case KeyExchangeAlgorithm.DH_DSS:
  94. case KeyExchangeAlgorithm.DH_RSA:
  95. return CreateDHKeyExchange(keyExchangeAlgorithm);
  96. case KeyExchangeAlgorithm.DHE_DSS:
  97. case KeyExchangeAlgorithm.DHE_RSA:
  98. return CreateDheKeyExchange(keyExchangeAlgorithm);
  99. case KeyExchangeAlgorithm.ECDH_anon:
  100. case KeyExchangeAlgorithm.ECDH_ECDSA:
  101. case KeyExchangeAlgorithm.ECDH_RSA:
  102. return CreateECDHKeyExchange(keyExchangeAlgorithm);
  103. case KeyExchangeAlgorithm.ECDHE_ECDSA:
  104. case KeyExchangeAlgorithm.ECDHE_RSA:
  105. return CreateECDheKeyExchange(keyExchangeAlgorithm);
  106. case KeyExchangeAlgorithm.RSA:
  107. return CreateRsaKeyExchange();
  108. default:
  109. /*
  110. * Note: internal error here; the TlsProtocol implementation verifies that the
  111. * server-selected cipher suite was in the list of client-offered cipher suites, so if
  112. * we now can't produce an implementation, we shouldn't have offered it!
  113. */
  114. throw new TlsFatalAlert(AlertDescription.internal_error);
  115. }
  116. }
  117. protected virtual TlsKeyExchange CreateDHKeyExchange(int keyExchange)
  118. {
  119. return new TlsDHKeyExchange(keyExchange, mSupportedSignatureAlgorithms, null, GetDHParameters());
  120. }
  121. protected virtual TlsKeyExchange CreateDheKeyExchange(int keyExchange)
  122. {
  123. return new TlsDheKeyExchange(keyExchange, mSupportedSignatureAlgorithms, null, GetDHParameters());
  124. }
  125. protected virtual TlsKeyExchange CreateECDHKeyExchange(int keyExchange)
  126. {
  127. return new TlsECDHKeyExchange(keyExchange, mSupportedSignatureAlgorithms, mNamedCurves, mClientECPointFormats,
  128. mServerECPointFormats);
  129. }
  130. protected virtual TlsKeyExchange CreateECDheKeyExchange(int keyExchange)
  131. {
  132. return new TlsECDheKeyExchange(keyExchange, mSupportedSignatureAlgorithms, mNamedCurves, mClientECPointFormats,
  133. mServerECPointFormats);
  134. }
  135. protected virtual TlsKeyExchange CreateRsaKeyExchange()
  136. {
  137. return new TlsRsaKeyExchange(mSupportedSignatureAlgorithms);
  138. }
  139. }
  140. }
  141. #pragma warning restore
  142. #endif