MySQL 9.0.0
Source Code Documentation
tls_message_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_MESSAGE_DUMPER_H_
27#define ROUTER_SRC_OPENSSL_INCLUDE_TLS_TLS_MESSAGE_DUMPER_H_
28
29#include <openssl/ssl.h>
30
31#include <string>
32#include <utility>
33
34namespace tls {
35
37 public:
38 TlsMessageDumper(SSL_CTX *ctx, std::string &&name)
39 : ctx_{ctx}, name_{std::move(name)} {
40 SSL_CTX_set_msg_callback(ctx_, TlsMessageDumper::message_callback);
41
42 if (!name_.empty()) SSL_CTX_set_msg_callback_arg(ctx_, &name_[0]);
43 }
44
45 TlsMessageDumper(SSL *ssl, std::string &&name)
46 : ssl_{ssl}, name_{std::move(name)} {
47 SSL_set_msg_callback(ssl_, TlsMessageDumper::message_callback);
48
49 if (!name_.empty()) SSL_set_msg_callback_arg(ssl_, &name_[0]);
50 }
51
53 if (ctx_) {
54 SSL_CTX_set_msg_callback(ctx_, nullptr);
55
56 if (!name_.empty()) SSL_CTX_set_msg_callback_arg(ctx_, nullptr);
57 }
58
59 if (ssl_) {
60 SSL_set_msg_callback(ssl_, nullptr);
61
62 if (!name_.empty()) SSL_set_msg_callback_arg(ssl_, nullptr);
63 }
64 }
65
66 private:
67 static std::string to_string_write_p(int write_p) {
68 if (0 == write_p) return "RECV";
69 return "SEND";
70 }
71
72 static std::string to_string_version(int version) {
73 switch (version) {
74 case SSL2_VERSION:
75 return "SSL2";
76 case SSL3_VERSION:
77 return "SSL3";
78
79 case TLS1_VERSION:
80 return "TLS1";
81 case TLS1_1_VERSION:
82 return "TLS1.1";
83 case TLS1_2_VERSION:
84 return "TLS1.2";
85 case TLS1_3_VERSION:
86 return "TLS1.3";
87
88 default:
89 return std::string("unknown-") + std::to_string(version);
90 }
91 }
92
93 static std::string to_string_content(int content) {
94 switch (content) {
95 case 0:
96 return "UNDEFINED";
97 case SSL3_RT_HANDSHAKE:
98 return "SSL3_RT_HANDSHAKE";
99 case SSL3_RT_CHANGE_CIPHER_SPEC:
100 return "SSL3_RT_CHANGE_CIPHER_SPEC";
101 case SSL3_RT_HEADER:
102 return "SSL3_RT_HEADER";
103 case SSL3_RT_INNER_CONTENT_TYPE:
104 return "SSL3_RT_INNER_CONTENT_TYPE";
105 case SSL3_RT_ALERT:
106 return "SSL3_RT_ALERT";
107
108 default:
109 return "UNKNOWN";
110 }
111 }
112
113 static std::string to_string_name(void *arg) {
114 if (nullptr == arg) return "";
115
116 std::string result{reinterpret_cast<char *>(arg)};
117 result += "/";
118 return result;
119 }
120
121 static void message_callback(int write_p, int version, int content_type,
122 const void *buf, size_t len, SSL *, void *arg) {
123 std::cout << to_string_name(arg) << "OpenSSL-" << to_string_write_p(write_p)
124 << ", VERSION:" << to_string_version(version)
125 << ", content:" << to_string_content(content_type)
126 << ", buffer: " << buf << ", len:" << len << std::endl;
127 }
128
129 SSL *ssl_{nullptr};
130 SSL_CTX *ctx_{nullptr};
131 std::string name_;
132};
133
134} // namespace tls
135
136#endif // ROUTER_SRC_OPENSSL_INCLUDE_TLS_TLS_MESSAGE_DUMPER_H_
Definition: tls_message_dumper.h:36
static std::string to_string_name(void *arg)
Definition: tls_message_dumper.h:113
TlsMessageDumper(SSL *ssl, std::string &&name)
Definition: tls_message_dumper.h:45
static std::string to_string_content(int content)
Definition: tls_message_dumper.h:93
~TlsMessageDumper()
Definition: tls_message_dumper.h:52
static std::string to_string_version(int version)
Definition: tls_message_dumper.h:72
static std::string to_string_write_p(int write_p)
Definition: tls_message_dumper.h:67
SSL_CTX * ctx_
Definition: tls_message_dumper.h:130
std::string name_
Definition: tls_message_dumper.h:131
TlsMessageDumper(SSL_CTX *ctx, std::string &&name)
Definition: tls_message_dumper.h:38
SSL * ssl_
Definition: tls_message_dumper.h:129
static void message_callback(int write_p, int version, int content_type, const void *buf, size_t len, SSL *, void *arg)
Definition: tls_message_dumper.h:121
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
Definition: buf0block_hint.cc:30
constexpr value_type ssl
Definition: classic_protocol_constants.h:49
Definition: gcs_xcom_synode.h:64
Definition: tls_keylog_dumper.h:32
struct result result
Definition: result.h:34
required uint64 version
Definition: replication_group_member_actions.proto:41
case opt name
Definition: sslopt-case.h:29
Definition: result.h:30