SrpTlsServer.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  7. {
  8. public class SrpTlsServer
  9. : AbstractTlsServer
  10. {
  11. protected TlsSrpIdentityManager mSrpIdentityManager;
  12. protected byte[] mSrpIdentity = null;
  13. protected TlsSrpLoginParameters mLoginParameters = null;
  14. public SrpTlsServer(TlsSrpIdentityManager srpIdentityManager)
  15. : this(new DefaultTlsCipherFactory(), srpIdentityManager)
  16. {
  17. }
  18. public SrpTlsServer(TlsCipherFactory cipherFactory, TlsSrpIdentityManager srpIdentityManager)
  19. : base(cipherFactory)
  20. {
  21. this.mSrpIdentityManager = srpIdentityManager;
  22. }
  23. protected virtual TlsSignerCredentials GetDsaSignerCredentials()
  24. {
  25. throw new TlsFatalAlert(AlertDescription.internal_error);
  26. }
  27. protected virtual TlsSignerCredentials GetRsaSignerCredentials()
  28. {
  29. throw new TlsFatalAlert(AlertDescription.internal_error);
  30. }
  31. protected override int[] GetCipherSuites()
  32. {
  33. return new int[]
  34. {
  35. CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA,
  36. CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA,
  37. CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA,
  38. CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA,
  39. CipherSuite.TLS_SRP_SHA_WITH_AES_256_CBC_SHA,
  40. CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA
  41. };
  42. }
  43. public override void ProcessClientExtensions(IDictionary clientExtensions)
  44. {
  45. base.ProcessClientExtensions(clientExtensions);
  46. this.mSrpIdentity = TlsSrpUtilities.GetSrpExtension(clientExtensions);
  47. }
  48. public override int GetSelectedCipherSuite()
  49. {
  50. int cipherSuite = base.GetSelectedCipherSuite();
  51. if (TlsSrpUtilities.IsSrpCipherSuite(cipherSuite))
  52. {
  53. if (mSrpIdentity != null)
  54. {
  55. this.mLoginParameters = mSrpIdentityManager.GetLoginParameters(mSrpIdentity);
  56. }
  57. if (mLoginParameters == null)
  58. throw new TlsFatalAlert(AlertDescription.unknown_psk_identity);
  59. }
  60. return cipherSuite;
  61. }
  62. public override TlsCredentials GetCredentials()
  63. {
  64. int keyExchangeAlgorithm = TlsUtilities.GetKeyExchangeAlgorithm(mSelectedCipherSuite);
  65. switch (keyExchangeAlgorithm)
  66. {
  67. case KeyExchangeAlgorithm.SRP:
  68. return null;
  69. case KeyExchangeAlgorithm.SRP_DSS:
  70. return GetDsaSignerCredentials();
  71. case KeyExchangeAlgorithm.SRP_RSA:
  72. return GetRsaSignerCredentials();
  73. default:
  74. /* Note: internal error here; selected a key exchange we don't implement! */
  75. throw new TlsFatalAlert(AlertDescription.internal_error);
  76. }
  77. }
  78. public override TlsKeyExchange GetKeyExchange()
  79. {
  80. int keyExchangeAlgorithm = TlsUtilities.GetKeyExchangeAlgorithm(mSelectedCipherSuite);
  81. switch (keyExchangeAlgorithm)
  82. {
  83. case KeyExchangeAlgorithm.SRP:
  84. case KeyExchangeAlgorithm.SRP_DSS:
  85. case KeyExchangeAlgorithm.SRP_RSA:
  86. return CreateSrpKeyExchange(keyExchangeAlgorithm);
  87. default:
  88. /*
  89. * Note: internal error here; the TlsProtocol implementation verifies that the
  90. * server-selected cipher suite was in the list of client-offered cipher suites, so if
  91. * we now can't produce an implementation, we shouldn't have offered it!
  92. */
  93. throw new TlsFatalAlert(AlertDescription.internal_error);
  94. }
  95. }
  96. protected virtual TlsKeyExchange CreateSrpKeyExchange(int keyExchange)
  97. {
  98. return new TlsSrpKeyExchange(keyExchange, mSupportedSignatureAlgorithms, mSrpIdentity, mLoginParameters);
  99. }
  100. }
  101. }
  102. #pragma warning restore
  103. #endif