File: ssl_acceptor_context_operator.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (347 lines) | stat: -rw-r--r-- 13,756 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "my_dbug.h"                                /* assert */
#include "mysql/components/services/log_builtins.h" /* LogErr */
#include "mysql/status_var.h"                       /* SHOW_VAR */
#include "mysqld_error.h"                           /* Error/Warning macros */
#include "sql/ssl_acceptor_context_status.h"        /* Status functions */

#include "sql/ssl_acceptor_context_operator.h"

Ssl_acceptor_context_container *mysql_main;
Ssl_acceptor_context_container *mysql_admin;

Ssl_acceptor_context_container::Ssl_acceptor_context_container(
    Ssl_acceptor_context_data *data)
    : lock_(nullptr) {
  lock_ = new Ssl_acceptor_context_data_lock(data);
}

Ssl_acceptor_context_container ::~Ssl_acceptor_context_container() {
  if (lock_ != nullptr) delete lock_;
  lock_ = nullptr;
}

void Ssl_acceptor_context_container::switch_data(
    Ssl_acceptor_context_data *new_data) {
  if (lock_ != nullptr) lock_->write_wait_and_delete(new_data);
}

bool TLS_channel::singleton_init(Ssl_acceptor_context_container **out,
                                 std::string channel, bool use_ssl_arg,
                                 Ssl_init_callback *callbacks, bool db_init) {
  if (out == nullptr || callbacks == nullptr) return true;
  *out = nullptr;
  /*
    No need to take the ssl_ctx_lock lock here since it's being called
    from singleton_init().
  */
  if (use_ssl_arg && callbacks->provision_certs()) return true;

  /*
    No real need for opt_use_ssl to be enabled in bootstrap mode,
    but we want the SSL material generation and/or validation
    (if supplied). So, we keep it on.

    We don't hush the option since it would indicate a failure
    in auto-generation, bad key material explicitly specified or
    auto-generation disabled explcitly while SSL is still on.
  */
  Ssl_acceptor_context_data *news =
      new Ssl_acceptor_context_data(channel, use_ssl_arg, callbacks);
  Ssl_acceptor_context_container *new_container =
      new Ssl_acceptor_context_container(news);
  if (news == nullptr || new_container == nullptr) {
    LogErr(WARNING_LEVEL, ER_SSL_LIBRARY_ERROR,
           "Error initializing the SSL context system structure");
    if (new_container) delete new_container;
    return true;
  }

  if (news->have_ssl() && callbacks->warn_self_signed_ca()) {
    /* This would delete Ssl_acceptor_context_data too */
    delete new_container;
    return true;
  }

  if (!db_init && news->have_ssl())
    LogErr(SYSTEM_LEVEL, ER_TLS_CONFIGURED_FOR_CHANNEL, channel.c_str());

  *out = new_container;
  return false;
}

void TLS_channel::singleton_deinit(Ssl_acceptor_context_container *container) {
  if (container == nullptr) return;
  delete container;
}

void TLS_channel::singleton_flush(Ssl_acceptor_context_container *container,
                                  std::string channel,
                                  Ssl_init_callback *callbacks,
                                  enum enum_ssl_init_error *error, bool force) {
  if (container == nullptr) return;
  Ssl_acceptor_context_data *news =
      new Ssl_acceptor_context_data(channel, true, callbacks, false, error);
  if (*error != SSL_INITERR_NOERROR && !force) {
    delete news;
    return;
  }
  (void)container->switch_data(news);
  return;
}

std::string Lock_and_access_ssl_acceptor_context::show_property(
    Ssl_acceptor_context_property_type property_type) {
  const Ssl_acceptor_context_data *data = read_lock_;
  return (data != nullptr ? data->show_property(property_type) : std::string{});
}

std::string Lock_and_access_ssl_acceptor_context::channel_name() {
  const Ssl_acceptor_context_data *data = read_lock_;
  return (data != nullptr ? data->channel_name() : std::string{});
}

bool Lock_and_access_ssl_acceptor_context::have_ssl() {
  const Ssl_acceptor_context_data *data = read_lock_;
  return (data != nullptr ? data->have_ssl() : false);
}

bool have_ssl() {
  if (mysql_main != nullptr) {
    Lock_and_access_ssl_acceptor_context context(mysql_main);
    if (context.have_ssl()) return true;
  }
  if (mysql_admin != nullptr) {
    Lock_and_access_ssl_acceptor_context context(mysql_admin);
    if (context.have_ssl()) return true;
  }
  return false;
}

/* Helpers */
static int show_long_status(SHOW_VAR *var, char *buff,
                            Ssl_acceptor_context_property_type property_type) {
  std::string property;
  if (mysql_main != nullptr) {
    Lock_and_access_ssl_acceptor_context main(mysql_main);
    property = main.show_property(property_type);
  }
  var->type = SHOW_LONG;
  var->value = buff;
  *((long *)buff) = std::stol(property);

  return 0;
}

static int show_char_status(SHOW_VAR *var, char *buff,
                            Ssl_acceptor_context_property_type property_type) {
  std::string property;
  if (mysql_main != nullptr) {
    Lock_and_access_ssl_acceptor_context main(mysql_main);
    property = main.show_property(property_type);
  }
  var->type = SHOW_CHAR;
  strncpy(buff, property.c_str(), SHOW_VAR_FUNC_BUFF_SIZE);
  buff[SHOW_VAR_FUNC_BUFF_SIZE - 1] = 0;
  var->value = buff;

  return 0;
}
/* Helpers end */

/* Status functions for mysql_main TLS context */

int Ssl_mysql_main_status::show_ssl_ctx_sess_accept(THD *, SHOW_VAR *var,
                                                    char *buff) {
  return show_long_status(var, buff,
                          Ssl_acceptor_context_property_type::accepts);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_accept_good(THD *, SHOW_VAR *var,
                                                         char *buff) {
  return show_long_status(var, buff,
                          Ssl_acceptor_context_property_type::finished_accepts);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_connect_good(THD *, SHOW_VAR *var,
                                                          char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::finished_connects);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_accept_renegotiate(THD *,
                                                                SHOW_VAR *var,
                                                                char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::callback_cache_hits);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_connect_renegotiate(THD *,
                                                                 SHOW_VAR *var,
                                                                 char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::callback_cache_hits);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_cb_hits(THD *, SHOW_VAR *var,
                                                     char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::callback_cache_hits);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_hits(THD *, SHOW_VAR *var,
                                                  char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::session_cache_hits);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_cache_full(THD *, SHOW_VAR *var,
                                                        char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::session_cache_overflows);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_misses(THD *, SHOW_VAR *var,
                                                    char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::session_cache_misses);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_timeouts(THD *, SHOW_VAR *var,
                                                      char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::session_cache_timeouts);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_timeout(THD *, SHOW_VAR *var,
                                                     char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::session_cache_timeout);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_number(THD *, SHOW_VAR *var,
                                                    char *buff) {
  return show_long_status(
      var, buff,
      Ssl_acceptor_context_property_type::used_session_cache_entries);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_connect(THD *, SHOW_VAR *var,
                                                     char *buff) {
  return show_long_status(var, buff,
                          Ssl_acceptor_context_property_type::client_connects);
}

int Ssl_mysql_main_status::show_ssl_ctx_sess_get_cache_size(THD *,
                                                            SHOW_VAR *var,
                                                            char *buff) {
  return show_long_status(
      var, buff, Ssl_acceptor_context_property_type::session_cache_size);
}

int Ssl_mysql_main_status::show_ssl_ctx_get_verify_mode(THD *, SHOW_VAR *var,
                                                        char *buff) {
  return show_long_status(var, buff,
                          Ssl_acceptor_context_property_type::ctx_verify_mode);
}

int Ssl_mysql_main_status::show_ssl_ctx_get_verify_depth(THD *, SHOW_VAR *var,
                                                         char *buff) {
  return show_long_status(var, buff,
                          Ssl_acceptor_context_property_type::ctx_verify_depth);
}

int Ssl_mysql_main_status::show_ssl_ctx_get_session_cache_mode(THD *,
                                                               SHOW_VAR *var,
                                                               char *buff) {
  return show_char_status(
      var, buff, Ssl_acceptor_context_property_type::session_cache_mode);
}

int Ssl_mysql_main_status::show_ssl_get_server_not_before(THD *, SHOW_VAR *var,
                                                          char *buff) {
  return show_char_status(
      var, buff, Ssl_acceptor_context_property_type::server_not_before);
}

int Ssl_mysql_main_status::show_ssl_get_server_not_after(THD *, SHOW_VAR *var,
                                                         char *buff) {
  return show_char_status(var, buff,
                          Ssl_acceptor_context_property_type::server_not_after);
}

int Ssl_mysql_main_status::show_ssl_get_ssl_ca(THD *, SHOW_VAR *var,
                                               char *buff) {
  return show_char_status(var, buff,
                          Ssl_acceptor_context_property_type::current_tls_ca);
}

int Ssl_mysql_main_status::show_ssl_get_ssl_capath(THD *, SHOW_VAR *var,
                                                   char *buff) {
  return show_char_status(
      var, buff, Ssl_acceptor_context_property_type::current_tls_capath);
}

int Ssl_mysql_main_status::show_ssl_get_ssl_cert(THD *, SHOW_VAR *var,
                                                 char *buff) {
  return show_char_status(var, buff,
                          Ssl_acceptor_context_property_type::current_tls_cert);
}

int Ssl_mysql_main_status::show_ssl_get_ssl_key(THD *, SHOW_VAR *var,
                                                char *buff) {
  return show_char_status(var, buff,
                          Ssl_acceptor_context_property_type::current_tls_key);
}

int Ssl_mysql_main_status::show_ssl_get_ssl_cipher(THD *, SHOW_VAR *var,
                                                   char *buff) {
  return show_char_status(
      var, buff, Ssl_acceptor_context_property_type::current_tls_cipher);
}

int Ssl_mysql_main_status::show_ssl_get_tls_ciphersuites(THD *, SHOW_VAR *var,
                                                         char *buff) {
  return show_char_status(
      var, buff, Ssl_acceptor_context_property_type::current_tls_ciphersuites);
}

int Ssl_mysql_main_status::show_ssl_get_tls_version(THD *, SHOW_VAR *var,
                                                    char *buff) {
  return show_char_status(
      var, buff, Ssl_acceptor_context_property_type::current_tls_version);
}

int Ssl_mysql_main_status::show_ssl_get_ssl_crl(THD *, SHOW_VAR *var,
                                                char *buff) {
  return show_char_status(var, buff,
                          Ssl_acceptor_context_property_type::current_tls_crl);
}

int Ssl_mysql_main_status::show_ssl_get_ssl_crlpath(THD *, SHOW_VAR *var,
                                                    char *buff) {
  return show_char_status(
      var, buff, Ssl_acceptor_context_property_type::current_tls_crlpath);
}