PskTlsClient.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 PskTlsClient
  9. : AbstractTlsClient
  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_128_CBC_SHA256,
  15. CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  16. CipherSuite.TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  17. CipherSuite.TLS_DHE_PSK_WITH_AES_128_GCM_SHA256,
  18. CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA256,
  19. CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA
  20. };
  21. protected readonly TlsPskIdentity m_pskIdentity;
  22. public PskTlsClient(TlsCrypto crypto, byte[] identity, byte[] psk)
  23. : this(crypto, new BasicTlsPskIdentity(identity, psk))
  24. {
  25. }
  26. public PskTlsClient(TlsCrypto crypto, TlsPskIdentity pskIdentity)
  27. : base(crypto)
  28. {
  29. this.m_pskIdentity = pskIdentity;
  30. }
  31. protected override ProtocolVersion[] GetSupportedVersions()
  32. {
  33. return ProtocolVersion.TLSv12.DownTo(ProtocolVersion.TLSv10);
  34. }
  35. protected override int[] GetSupportedCipherSuites()
  36. {
  37. return TlsUtilities.GetSupportedCipherSuites(Crypto, DefaultCipherSuites);
  38. }
  39. public override TlsPskIdentity GetPskIdentity()
  40. {
  41. return m_pskIdentity;
  42. }
  43. /// <exception cref="IOException"/>
  44. public override TlsAuthentication GetAuthentication()
  45. {
  46. /*
  47. * Note: This method is not called unless a server certificate is sent, which may be the
  48. * case e.g. for RSA_PSK key exchange.
  49. */
  50. throw new TlsFatalAlert(AlertDescription.internal_error);
  51. }
  52. }
  53. }
  54. #pragma warning restore
  55. #endif