SrpTlsClient.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  8. {
  9. public class SrpTlsClient
  10. : AbstractTlsClient
  11. {
  12. protected TlsSrpGroupVerifier mGroupVerifier;
  13. protected byte[] mIdentity;
  14. protected byte[] mPassword;
  15. public SrpTlsClient(byte[] identity, byte[] password)
  16. : this(new DefaultTlsCipherFactory(), new DefaultTlsSrpGroupVerifier(), identity, password)
  17. {
  18. }
  19. public SrpTlsClient(TlsCipherFactory cipherFactory, byte[] identity, byte[] password)
  20. : this(cipherFactory, new DefaultTlsSrpGroupVerifier(), identity, password)
  21. {
  22. }
  23. public SrpTlsClient(TlsCipherFactory cipherFactory, TlsSrpGroupVerifier groupVerifier,
  24. byte[] identity, byte[] password)
  25. : base(cipherFactory)
  26. {
  27. this.mGroupVerifier = groupVerifier;
  28. this.mIdentity = Arrays.Clone(identity);
  29. this.mPassword = Arrays.Clone(password);
  30. }
  31. protected virtual bool RequireSrpServerExtension
  32. {
  33. // No explicit guidance in RFC 5054; by default an (empty) extension from server is optional
  34. get { return false; }
  35. }
  36. public override int[] GetCipherSuites()
  37. {
  38. return new int[]
  39. {
  40. CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA
  41. };
  42. }
  43. public override IDictionary GetClientExtensions()
  44. {
  45. IDictionary clientExtensions = TlsExtensionsUtilities.EnsureExtensionsInitialised(base.GetClientExtensions());
  46. TlsSrpUtilities.AddSrpExtension(clientExtensions, this.mIdentity);
  47. return clientExtensions;
  48. }
  49. public override void ProcessServerExtensions(IDictionary serverExtensions)
  50. {
  51. if (!TlsUtilities.HasExpectedEmptyExtensionData(serverExtensions, ExtensionType.srp,
  52. AlertDescription.illegal_parameter))
  53. {
  54. if (RequireSrpServerExtension)
  55. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  56. }
  57. base.ProcessServerExtensions(serverExtensions);
  58. }
  59. public override TlsKeyExchange GetKeyExchange()
  60. {
  61. int keyExchangeAlgorithm = TlsUtilities.GetKeyExchangeAlgorithm(mSelectedCipherSuite);
  62. switch (keyExchangeAlgorithm)
  63. {
  64. case KeyExchangeAlgorithm.SRP:
  65. case KeyExchangeAlgorithm.SRP_DSS:
  66. case KeyExchangeAlgorithm.SRP_RSA:
  67. return CreateSrpKeyExchange(keyExchangeAlgorithm);
  68. default:
  69. /*
  70. * Note: internal error here; the TlsProtocol implementation verifies that the
  71. * server-selected cipher suite was in the list of client-offered cipher suites, so if
  72. * we now can't produce an implementation, we shouldn't have offered it!
  73. */
  74. throw new TlsFatalAlert(AlertDescription.internal_error);
  75. }
  76. }
  77. public override TlsAuthentication GetAuthentication()
  78. {
  79. /*
  80. * Note: This method is not called unless a server certificate is sent, which may be the
  81. * case e.g. for SRP_DSS or SRP_RSA key exchange.
  82. */
  83. throw new TlsFatalAlert(AlertDescription.internal_error);
  84. }
  85. protected virtual TlsKeyExchange CreateSrpKeyExchange(int keyExchange)
  86. {
  87. return new TlsSrpKeyExchange(keyExchange, mSupportedSignatureAlgorithms, mGroupVerifier, mIdentity, mPassword);
  88. }
  89. }
  90. }
  91. #pragma warning restore
  92. #endif