PskTlsClient.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  6. {
  7. public class PskTlsClient
  8. : AbstractTlsClient
  9. {
  10. protected TlsDHVerifier mDHVerifier;
  11. protected TlsPskIdentity mPskIdentity;
  12. public PskTlsClient(TlsPskIdentity pskIdentity)
  13. : this(new DefaultTlsCipherFactory(), pskIdentity)
  14. {
  15. }
  16. public PskTlsClient(TlsCipherFactory cipherFactory, TlsPskIdentity pskIdentity)
  17. : this(cipherFactory, new DefaultTlsDHVerifier(), pskIdentity)
  18. {
  19. }
  20. public PskTlsClient(TlsCipherFactory cipherFactory, TlsDHVerifier dhVerifier, TlsPskIdentity pskIdentity)
  21. : base(cipherFactory)
  22. {
  23. this.mDHVerifier = dhVerifier;
  24. this.mPskIdentity = pskIdentity;
  25. }
  26. public override int[] GetCipherSuites()
  27. {
  28. return new int[]
  29. {
  30. CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
  31. CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  32. };
  33. }
  34. public override TlsKeyExchange GetKeyExchange()
  35. {
  36. int keyExchangeAlgorithm = TlsUtilities.GetKeyExchangeAlgorithm(mSelectedCipherSuite);
  37. switch (keyExchangeAlgorithm)
  38. {
  39. case KeyExchangeAlgorithm.DHE_PSK:
  40. case KeyExchangeAlgorithm.ECDHE_PSK:
  41. case KeyExchangeAlgorithm.PSK:
  42. case KeyExchangeAlgorithm.RSA_PSK:
  43. return CreatePskKeyExchange(keyExchangeAlgorithm);
  44. default:
  45. /*
  46. * Note: internal error here; the TlsProtocol implementation verifies that the
  47. * server-selected cipher suite was in the list of client-offered cipher suites, so if
  48. * we now can't produce an implementation, we shouldn't have offered it!
  49. */
  50. throw new TlsFatalAlert(AlertDescription.internal_error);
  51. }
  52. }
  53. public override TlsAuthentication GetAuthentication()
  54. {
  55. /*
  56. * Note: This method is not called unless a server certificate is sent, which may be the
  57. * case e.g. for RSA_PSK key exchange.
  58. */
  59. throw new TlsFatalAlert(AlertDescription.internal_error);
  60. }
  61. protected virtual TlsKeyExchange CreatePskKeyExchange(int keyExchange)
  62. {
  63. return new TlsPskKeyExchange(keyExchange, mSupportedSignatureAlgorithms, mPskIdentity, null, mDHVerifier, null,
  64. mNamedCurves, mClientECPointFormats, mServerECPointFormats);
  65. }
  66. }
  67. }
  68. #pragma warning restore
  69. #endif