SrpTlsClient.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.Tls.Crypto;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  8. {
  9. public class SrpTlsClient
  10. : AbstractTlsClient
  11. {
  12. private static readonly int[] DefaultCipherSuites = new int[]
  13. {
  14. CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA
  15. };
  16. protected readonly TlsSrpIdentity m_srpIdentity;
  17. public SrpTlsClient(TlsCrypto crypto, byte[] identity, byte[] password)
  18. : this(crypto, new BasicTlsSrpIdentity(identity, password))
  19. {
  20. }
  21. public SrpTlsClient(TlsCrypto crypto, TlsSrpIdentity srpIdentity)
  22. : base(crypto)
  23. {
  24. this.m_srpIdentity = srpIdentity;
  25. }
  26. protected override int[] GetSupportedCipherSuites()
  27. {
  28. return TlsUtilities.GetSupportedCipherSuites(Crypto, DefaultCipherSuites);
  29. }
  30. protected override ProtocolVersion[] GetSupportedVersions()
  31. {
  32. return ProtocolVersion.TLSv12.DownTo(ProtocolVersion.TLSv10);
  33. }
  34. protected virtual bool RequireSrpServerExtension
  35. {
  36. // No explicit guidance in RFC 5054; by default an (empty) extension from server is optional
  37. get { return false; }
  38. }
  39. /// <exception cref="IOException"/>
  40. public override IDictionary GetClientExtensions()
  41. {
  42. IDictionary clientExtensions = TlsExtensionsUtilities.EnsureExtensionsInitialised(
  43. base.GetClientExtensions());
  44. TlsSrpUtilities.AddSrpExtension(clientExtensions, m_srpIdentity.GetSrpIdentity());
  45. return clientExtensions;
  46. }
  47. /// <exception cref="IOException"/>
  48. public override void ProcessServerExtensions(IDictionary serverExtensions)
  49. {
  50. if (!TlsUtilities.HasExpectedEmptyExtensionData(serverExtensions, ExtensionType.srp,
  51. AlertDescription.illegal_parameter))
  52. {
  53. if (RequireSrpServerExtension)
  54. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  55. }
  56. base.ProcessServerExtensions(serverExtensions);
  57. }
  58. public override TlsSrpIdentity GetSrpIdentity()
  59. {
  60. return m_srpIdentity;
  61. }
  62. /// <exception cref="IOException"/>
  63. public override TlsAuthentication GetAuthentication()
  64. {
  65. /*
  66. * Note: This method is not called unless a server certificate is sent, which may be the
  67. * case e.g. for SRP_DSS or SRP_RSA key exchange.
  68. */
  69. throw new TlsFatalAlert(AlertDescription.internal_error);
  70. }
  71. }
  72. }
  73. #pragma warning restore
  74. #endif