DefaultTlsClient.cs 4.2 KB

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