PskTlsServer.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 class PskTlsServer
  9. : AbstractTlsServer
  10. {
  11. private static readonly int[] DefaultCipherSuites = new int[]
  12. {
  13. CipherSuite.TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  14. CipherSuite.TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384,
  15. CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
  16. CipherSuite.TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  17. CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  18. CipherSuite.TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  19. CipherSuite.TLS_DHE_PSK_WITH_AES_256_GCM_SHA384,
  20. CipherSuite.TLS_DHE_PSK_WITH_AES_128_GCM_SHA256,
  21. CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA384,
  22. CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA256,
  23. CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA,
  24. CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA
  25. };
  26. protected readonly TlsPskIdentityManager m_pskIdentityManager;
  27. public PskTlsServer(TlsCrypto crypto, TlsPskIdentityManager pskIdentityManager)
  28. : base(crypto)
  29. {
  30. this.m_pskIdentityManager = pskIdentityManager;
  31. }
  32. /// <exception cref="IOException"/>
  33. protected virtual TlsCredentialedDecryptor GetRsaEncryptionCredentials()
  34. {
  35. throw new TlsFatalAlert(AlertDescription.internal_error);
  36. }
  37. protected override ProtocolVersion[] GetSupportedVersions()
  38. {
  39. return ProtocolVersion.TLSv12.DownTo(ProtocolVersion.TLSv10);
  40. }
  41. protected override int[] GetSupportedCipherSuites()
  42. {
  43. return TlsUtilities.GetSupportedCipherSuites(Crypto, DefaultCipherSuites);
  44. }
  45. public override TlsCredentials GetCredentials()
  46. {
  47. int keyExchangeAlgorithm = m_context.SecurityParameters.KeyExchangeAlgorithm;
  48. switch (keyExchangeAlgorithm)
  49. {
  50. case KeyExchangeAlgorithm.DHE_PSK:
  51. case KeyExchangeAlgorithm.ECDHE_PSK:
  52. case KeyExchangeAlgorithm.PSK:
  53. return null;
  54. case KeyExchangeAlgorithm.RSA_PSK:
  55. return GetRsaEncryptionCredentials();
  56. default:
  57. // Note: internal error here; selected a key exchange we don't implement!
  58. throw new TlsFatalAlert(AlertDescription.internal_error);
  59. }
  60. }
  61. public override TlsPskIdentityManager GetPskIdentityManager()
  62. {
  63. return m_pskIdentityManager;
  64. }
  65. }
  66. }
  67. #pragma warning restore
  68. #endif