ssl_key_logger.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_SSL_SSL_KEY_LOGGER_H_
  5. #define NET_SSL_SSL_KEY_LOGGER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/no_destructor.h"
  9. #include "net/base/net_export.h"
  10. #include "third_party/boringssl/src/include/openssl/ssl.h"
  11. namespace net {
  12. // SSLKeyLogger logs SSL key material for debugging purposes. This should only
  13. // be used when requested by the user, typically via the SSLKEYLOGFILE
  14. // environment variable. See also
  15. // https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format.
  16. class NET_EXPORT SSLKeyLogger {
  17. public:
  18. virtual ~SSLKeyLogger() = default;
  19. // Writes |line| followed by a newline. This may be called by multiple threads
  20. // simultaneously. If two calls race, the order of the lines is undefined, but
  21. // each line will be written atomically.
  22. virtual void WriteLine(const std::string& line) = 0;
  23. };
  24. // SSLKeyLoggerManager owns a single global instance of SSLKeyLogger, allowing
  25. // it to safely be registered on multiple SSL_CTX instances.
  26. class NET_EXPORT SSLKeyLoggerManager {
  27. public:
  28. ~SSLKeyLoggerManager() = delete;
  29. SSLKeyLoggerManager(const SSLKeyLoggerManager&) = delete;
  30. SSLKeyLoggerManager& operator=(const SSLKeyLoggerManager&) = delete;
  31. // Returns true if an SSLKeyLogger has been set.
  32. static bool IsActive();
  33. // Set the SSLKeyLogger to use.
  34. static void SetSSLKeyLogger(std::unique_ptr<SSLKeyLogger> logger);
  35. // Logs |line| to the |logger| that was registered with SetSSLKeyLogger.
  36. // This function will crash if a logger has not been registered.
  37. // The function signature allows it to be registered with
  38. // SSL_CTX_set_keylog_callback, the |ssl| parameter is unused.
  39. static void KeyLogCallback(const SSL* /*ssl*/, const char* line);
  40. private:
  41. friend base::NoDestructor<SSLKeyLoggerManager>;
  42. SSLKeyLoggerManager();
  43. // Get the global SSLKeyLoggerManager instance.
  44. static SSLKeyLoggerManager* Get();
  45. std::unique_ptr<SSLKeyLogger> ssl_key_logger_;
  46. };
  47. } // namespace net
  48. #endif // NET_SSL_SSL_KEY_LOGGER_H_