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