KeyLogFileWriter.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  9. // https://www.m00nie.com/2015/05/decrypt-https-ssltls-with-wireshark/
  10. // https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format
  11. // https://github.com/bcgit/bc-csharp/issues/343
  12. namespace BestHTTP.Connections.TLS
  13. {
  14. /// <summary>
  15. /// https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format
  16. /// </summary>
  17. internal enum Labels
  18. {
  19. CLIENT_RANDOM,
  20. CLIENT_EARLY_TRAFFIC_SECRET,
  21. CLIENT_HANDSHAKE_TRAFFIC_SECRET,
  22. SERVER_HANDSHAKE_TRAFFIC_SECRET,
  23. CLIENT_TRAFFIC_SECRET_0,
  24. SERVER_TRAFFIC_SECRET_0,
  25. EARLY_EXPORTER_SECRET,
  26. EXPORTER_SECRET
  27. }
  28. internal static class KeyLogFileWriter
  29. {
  30. private static string GetKeylogFileName() => Environment.GetEnvironmentVariable("SSLKEYLOGFILE", EnvironmentVariableTarget.User);
  31. [Conditional("UNITY_EDITOR")]
  32. public static void WriteLabel(Labels label, byte[] clientRandom, TlsSecret secret)
  33. {
  34. if (clientRandom != null && secret != null)
  35. {
  36. string SSLKEYLOGFILE = GetKeylogFileName();
  37. if (!string.IsNullOrEmpty(SSLKEYLOGFILE))
  38. using (var writer = new StreamWriter(System.IO.File.Open(SSLKEYLOGFILE, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)))
  39. writer.WriteLine($"{label} {Hex.ToHexString(clientRandom)} {Hex.ToHexString((secret as AbstractTlsSecret).CopyData())}");
  40. }
  41. }
  42. [Conditional("UNITY_EDITOR")]
  43. public static void WriteLabel(Labels label, SecurityParameters securityParameters)
  44. {
  45. try
  46. {
  47. TlsSecret secret = null;
  48. switch (label)
  49. {
  50. case Labels.CLIENT_RANDOM: secret = securityParameters.MasterSecret; break;
  51. case Labels.CLIENT_HANDSHAKE_TRAFFIC_SECRET: secret = securityParameters.TrafficSecretClient; break;
  52. case Labels.SERVER_HANDSHAKE_TRAFFIC_SECRET: secret = securityParameters.TrafficSecretServer; break;
  53. case Labels.CLIENT_TRAFFIC_SECRET_0: secret = securityParameters.TrafficSecretClient; break;
  54. case Labels.SERVER_TRAFFIC_SECRET_0: secret = securityParameters.TrafficSecretServer; break;
  55. case Labels.EXPORTER_SECRET: secret = securityParameters.ExporterMasterSecret; break;
  56. case Labels.CLIENT_EARLY_TRAFFIC_SECRET: break;
  57. case Labels.EARLY_EXPORTER_SECRET: break;
  58. }
  59. if (secret != null)
  60. WriteLabel(label, securityParameters.ClientRandom, secret);
  61. }
  62. catch
  63. { }
  64. }
  65. }
  66. }
  67. #endif