DefaultTlsClient.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  13. {
  14. public abstract class DefaultTlsClient
  15. : AbstractTlsClient
  16. {
  17. protected TlsDHVerifier mDHVerifier;
  18. public DefaultTlsClient()
  19. : this(new DefaultTlsCipherFactory())
  20. {
  21. }
  22. public DefaultTlsClient(TlsCipherFactory cipherFactory)
  23. : this(cipherFactory, new DefaultTlsDHVerifier())
  24. {
  25. }
  26. public DefaultTlsClient(TlsCipherFactory cipherFactory, TlsDHVerifier dhVerifier)
  27. : base(cipherFactory)
  28. {
  29. this.mDHVerifier = dhVerifier;
  30. }
  31. public override int[] GetCipherSuites()
  32. {
  33. return new int[]
  34. {
  35. CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  36. CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
  37. CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  38. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  39. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  40. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  41. CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256,
  42. CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256,
  43. CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
  44. };
  45. }
  46. public override TlsKeyExchange GetKeyExchange()
  47. {
  48. int keyExchangeAlgorithm = TlsUtilities.GetKeyExchangeAlgorithm(mSelectedCipherSuite);
  49. switch (keyExchangeAlgorithm)
  50. {
  51. case KeyExchangeAlgorithm.DH_anon:
  52. case KeyExchangeAlgorithm.DH_DSS:
  53. case KeyExchangeAlgorithm.DH_RSA:
  54. return CreateDHKeyExchange(keyExchangeAlgorithm);
  55. case KeyExchangeAlgorithm.DHE_DSS:
  56. case KeyExchangeAlgorithm.DHE_RSA:
  57. return CreateDheKeyExchange(keyExchangeAlgorithm);
  58. case KeyExchangeAlgorithm.ECDH_anon:
  59. case KeyExchangeAlgorithm.ECDH_ECDSA:
  60. case KeyExchangeAlgorithm.ECDH_RSA:
  61. return CreateECDHKeyExchange(keyExchangeAlgorithm);
  62. case KeyExchangeAlgorithm.ECDHE_ECDSA:
  63. case KeyExchangeAlgorithm.ECDHE_RSA:
  64. return CreateECDheKeyExchange(keyExchangeAlgorithm);
  65. case KeyExchangeAlgorithm.RSA:
  66. return CreateRsaKeyExchange();
  67. default:
  68. /*
  69. * Note: internal error here; the TlsProtocol implementation verifies that the
  70. * server-selected cipher suite was in the list of client-offered cipher suites, so if
  71. * we now can't produce an implementation, we shouldn't have offered it!
  72. */
  73. throw new TlsFatalAlert(AlertDescription.internal_error);
  74. }
  75. }
  76. protected virtual TlsKeyExchange CreateDHKeyExchange(int keyExchange)
  77. {
  78. return new TlsDHKeyExchange(keyExchange, mSupportedSignatureAlgorithms, mDHVerifier, null);
  79. }
  80. protected virtual TlsKeyExchange CreateDheKeyExchange(int keyExchange)
  81. {
  82. return new TlsDheKeyExchange(keyExchange, mSupportedSignatureAlgorithms, mDHVerifier, null);
  83. }
  84. protected virtual TlsKeyExchange CreateECDHKeyExchange(int keyExchange)
  85. {
  86. return new TlsECDHKeyExchange(keyExchange, mSupportedSignatureAlgorithms, mNamedCurves, mClientECPointFormats,
  87. mServerECPointFormats);
  88. }
  89. protected virtual TlsKeyExchange CreateECDheKeyExchange(int keyExchange)
  90. {
  91. return new TlsECDheKeyExchange(keyExchange, mSupportedSignatureAlgorithms, mNamedCurves, mClientECPointFormats,
  92. mServerECPointFormats);
  93. }
  94. protected virtual TlsKeyExchange CreateRsaKeyExchange()
  95. {
  96. return new TlsRsaKeyExchange(mSupportedSignatureAlgorithms);
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif