MySQL 8.4.2
Source Code Documentation
tls_keylog_dumper.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_OPENSSL_INCLUDE_TLS_TLS_KEYLOG_DUMPER_H_
27#define ROUTER_SRC_OPENSSL_INCLUDE_TLS_TLS_KEYLOG_DUMPER_H_
28
29#include <openssl/ssl.h>
30#include <fstream>
31
32namespace tls {
33
35 public:
36 explicit TlsKeylogDumper(SSL_CTX *ctx) {
37 auto env_logfile = getenv("SSLKEYLOGFILE");
38 auto &stream = get_stream();
39
40 if (!stream.is_open() && env_logfile) {
41 stream.open(env_logfile, std::ios::app);
42
43 if (stream.is_open()) {
44 release_ = true;
45 SSL_CTX_set_keylog_callback(ctx, TlsKeylogDumper::keylog_callback);
46 }
47 }
48 }
49
51 if (release_) {
52 get_stream().close();
53 }
54 }
55
56 private:
57 static void keylog_callback(const SSL *, const char *line) {
58 auto &s = get_stream();
59
60 if (!s.is_open()) return;
61
62 s.seekp(0, std::ios_base::end);
63 s << line << std::endl;
64 s.flush();
65 }
66
67 static std::ofstream &get_stream() {
68 static std::ofstream file;
69
70 return file;
71 }
72
73 bool release_{false};
74};
75
76} // namespace tls
77
78#endif // ROUTER_SRC_OPENSSL_INCLUDE_TLS_TLS_KEYLOG_DUMPER_H_
Definition: tls_keylog_dumper.h:34
~TlsKeylogDumper()
Definition: tls_keylog_dumper.h:50
static void keylog_callback(const SSL *, const char *line)
Definition: tls_keylog_dumper.h:57
static std::ofstream & get_stream()
Definition: tls_keylog_dumper.h:67
TlsKeylogDumper(SSL_CTX *ctx)
Definition: tls_keylog_dumper.h:36
bool release_
Definition: tls_keylog_dumper.h:73
Definition: os0file.h:89
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
Definition: tls_keylog_dumper.h:32