SrpTlsServer.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 SrpTlsServer
  10. : AbstractTlsServer
  11. {
  12. private static readonly int[] DefaultCipherSuites = new int[]
  13. {
  14. CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA,
  15. CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA,
  16. CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA,
  17. CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA,
  18. CipherSuite.TLS_SRP_SHA_WITH_AES_256_CBC_SHA,
  19. CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA
  20. };
  21. protected readonly TlsSrpIdentityManager m_srpIdentityManager;
  22. protected byte[] m_srpIdentity = null;
  23. protected TlsSrpLoginParameters m_srpLoginParameters = null;
  24. public SrpTlsServer(TlsCrypto crypto, TlsSrpIdentityManager srpIdentityManager)
  25. : base(crypto)
  26. {
  27. this.m_srpIdentityManager = srpIdentityManager;
  28. }
  29. /// <exception cref="IOException"/>
  30. protected virtual TlsCredentialedSigner GetDsaSignerCredentials()
  31. {
  32. throw new TlsFatalAlert(AlertDescription.internal_error);
  33. }
  34. /// <exception cref="IOException"/>
  35. protected virtual TlsCredentialedSigner GetRsaSignerCredentials()
  36. {
  37. throw new TlsFatalAlert(AlertDescription.internal_error);
  38. }
  39. protected override ProtocolVersion[] GetSupportedVersions()
  40. {
  41. return ProtocolVersion.TLSv12.DownTo(ProtocolVersion.TLSv10);
  42. }
  43. protected override int[] GetSupportedCipherSuites()
  44. {
  45. return TlsUtilities.GetSupportedCipherSuites(Crypto, DefaultCipherSuites);
  46. }
  47. public override void ProcessClientExtensions(IDictionary clientExtensions)
  48. {
  49. base.ProcessClientExtensions(clientExtensions);
  50. this.m_srpIdentity = TlsSrpUtilities.GetSrpExtension(clientExtensions);
  51. }
  52. public override int GetSelectedCipherSuite()
  53. {
  54. int cipherSuite = base.GetSelectedCipherSuite();
  55. if (TlsSrpUtilities.IsSrpCipherSuite(cipherSuite))
  56. {
  57. if (m_srpIdentity != null)
  58. {
  59. this.m_srpLoginParameters = m_srpIdentityManager.GetLoginParameters(m_srpIdentity);
  60. }
  61. if (m_srpLoginParameters == null)
  62. throw new TlsFatalAlert(AlertDescription.unknown_psk_identity);
  63. }
  64. return cipherSuite;
  65. }
  66. public override TlsCredentials GetCredentials()
  67. {
  68. int keyExchangeAlgorithm = m_context.SecurityParameters.KeyExchangeAlgorithm;
  69. switch (keyExchangeAlgorithm)
  70. {
  71. case KeyExchangeAlgorithm.SRP:
  72. return null;
  73. case KeyExchangeAlgorithm.SRP_DSS:
  74. return GetDsaSignerCredentials();
  75. case KeyExchangeAlgorithm.SRP_RSA:
  76. return GetRsaSignerCredentials();
  77. default:
  78. // Note: internal error here; selected a key exchange we don't implement!
  79. throw new TlsFatalAlert(AlertDescription.internal_error);
  80. }
  81. }
  82. public override TlsSrpLoginParameters GetSrpLoginParameters()
  83. {
  84. return m_srpLoginParameters;
  85. }
  86. }
  87. }
  88. #pragma warning restore
  89. #endif