SimulatedTlsSrpIdentityManager.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  9. {
  10. /// <summary>An implementation of <see cref="TlsSrpIdentityManager"/> that simulates the existence of "unknown"
  11. /// identities to obscure the fact that there is no verifier for them.</summary>
  12. public class SimulatedTlsSrpIdentityManager
  13. : TlsSrpIdentityManager
  14. {
  15. private static readonly byte[] PrefixPassword = Strings.ToByteArray("password");
  16. private static readonly byte[] PrefixSalt = Strings.ToByteArray("salt");
  17. /// <summary>Create a <see cref="SimulatedTlsSrpIdentityManager"/> that implements the algorithm from RFC 5054
  18. /// 2.5.1.3.</summary>
  19. /// <param name="crypto"><see cref="TlsCrypto"/></param>
  20. /// <param name="group">the <see cref="Srp6Group"/> defining the group that SRP is operating in.</param>
  21. /// <param name="seedKey">the secret "seed key" referred to in RFC 5054 2.5.1.3.</param>
  22. /// <returns>an instance of <see cref="SimulatedTlsSrpIdentityManager"/>.</returns>
  23. /// <exception cref="IOException"/>
  24. public static SimulatedTlsSrpIdentityManager GetRfc5054Default(TlsCrypto crypto, Srp6Group group, byte[] seedKey)
  25. {
  26. TlsMac mac = crypto.CreateHmac(MacAlgorithm.hmac_sha1);
  27. mac.SetKey(seedKey, 0, seedKey.Length);
  28. TlsSrpConfig srpConfig = new TlsSrpConfig();
  29. srpConfig.SetExplicitNG(new BigInteger[]{ group.N, group.G });
  30. return new SimulatedTlsSrpIdentityManager(group, crypto.CreateSrp6VerifierGenerator(srpConfig), mac);
  31. }
  32. protected readonly Srp6Group m_group;
  33. protected readonly TlsSrp6VerifierGenerator m_verifierGenerator;
  34. protected readonly TlsMac m_mac;
  35. public SimulatedTlsSrpIdentityManager(Srp6Group group, TlsSrp6VerifierGenerator verifierGenerator, TlsMac mac)
  36. {
  37. this.m_group = group;
  38. this.m_verifierGenerator = verifierGenerator;
  39. this.m_mac = mac;
  40. }
  41. public virtual TlsSrpLoginParameters GetLoginParameters(byte[] identity)
  42. {
  43. m_mac.Update(PrefixSalt, 0, PrefixSalt.Length);
  44. m_mac.Update(identity, 0, identity.Length);
  45. byte[] salt = m_mac.CalculateMac();
  46. m_mac.Update(PrefixPassword, 0, PrefixPassword.Length);
  47. m_mac.Update(identity, 0, identity.Length);
  48. byte[] password = m_mac.CalculateMac();
  49. BigInteger verifier = m_verifierGenerator.GenerateVerifier(salt, identity, password);
  50. TlsSrpConfig srpConfig = new TlsSrpConfig();
  51. srpConfig.SetExplicitNG(new BigInteger[]{ m_group.N, m_group.G });
  52. return new TlsSrpLoginParameters(identity, srpConfig, verifier, salt);
  53. }
  54. }
  55. }
  56. #pragma warning restore
  57. #endif