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 BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Srp;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  10. {
  11. /**
  12. * An implementation of {@link TlsSRPIdentityManager} that simulates the existence of "unknown" identities
  13. * to obscure the fact that there is no verifier for them.
  14. */
  15. public class SimulatedTlsSrpIdentityManager
  16. : TlsSrpIdentityManager
  17. {
  18. private static readonly byte[] PREFIX_PASSWORD = Strings.ToByteArray("password");
  19. private static readonly byte[] PREFIX_SALT = Strings.ToByteArray("salt");
  20. /**
  21. * Create a {@link SimulatedTlsSRPIdentityManager} that implements the algorithm from RFC 5054 2.5.1.3
  22. *
  23. * @param group the {@link SRP6GroupParameters} defining the group that SRP is operating in
  24. * @param seedKey the secret "seed key" referred to in RFC 5054 2.5.1.3
  25. * @return an instance of {@link SimulatedTlsSRPIdentityManager}
  26. */
  27. public static SimulatedTlsSrpIdentityManager GetRfc5054Default(Srp6GroupParameters group, byte[] seedKey)
  28. {
  29. Srp6VerifierGenerator verifierGenerator = new Srp6VerifierGenerator();
  30. verifierGenerator.Init(group, TlsUtilities.CreateHash(HashAlgorithm.sha1));
  31. HMac mac = new HMac(TlsUtilities.CreateHash(HashAlgorithm.sha1));
  32. mac.Init(new KeyParameter(seedKey));
  33. return new SimulatedTlsSrpIdentityManager(group, verifierGenerator, mac);
  34. }
  35. protected readonly Srp6GroupParameters mGroup;
  36. protected readonly Srp6VerifierGenerator mVerifierGenerator;
  37. protected readonly IMac mMac;
  38. public SimulatedTlsSrpIdentityManager(Srp6GroupParameters group, Srp6VerifierGenerator verifierGenerator, IMac mac)
  39. {
  40. this.mGroup = group;
  41. this.mVerifierGenerator = verifierGenerator;
  42. this.mMac = mac;
  43. }
  44. public virtual TlsSrpLoginParameters GetLoginParameters(byte[] identity)
  45. {
  46. mMac.BlockUpdate(PREFIX_SALT, 0, PREFIX_SALT.Length);
  47. mMac.BlockUpdate(identity, 0, identity.Length);
  48. byte[] salt = new byte[mMac.GetMacSize()];
  49. mMac.DoFinal(salt, 0);
  50. mMac.BlockUpdate(PREFIX_PASSWORD, 0, PREFIX_PASSWORD.Length);
  51. mMac.BlockUpdate(identity, 0, identity.Length);
  52. byte[] password = new byte[mMac.GetMacSize()];
  53. mMac.DoFinal(password, 0);
  54. BigInteger verifier = mVerifierGenerator.GenerateVerifier(salt, identity, password);
  55. return new TlsSrpLoginParameters(mGroup, verifier, salt);
  56. }
  57. }
  58. }
  59. #pragma warning restore
  60. #endif