MySQL 26.10.0
Source Code Documentation
ssl_operation.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2026, 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_HTTP_SRC_TLS_SSL_OPERATION_H_
27#define ROUTER_SRC_HTTP_SRC_TLS_SSL_OPERATION_H_
28
29#include <openssl/bio.h>
30#include <openssl/err.h>
31#include <openssl/ssl.h>
32
33namespace net {
34namespace tls {
35
36class Operation {
37 public:
39
40 public:
41 virtual ~Operation() = default;
42
43 protected:
45 public:
46 AnalyzeOperation(BIO *bio, SSL *ssl)
47 : bio_{bio}, ssl_{ssl}, pending_{BIO_pending(bio)} {
48 ERR_clear_error();
49 }
50
51 Result check_ssl_result(int ssl_result) {
52 auto pending = BIO_pending(bio_);
53
54 Result op_result = ok;
55
56 if (ssl_result <= 0) {
57 auto error_cause = SSL_get_error(ssl_, ssl_result);
58
59 switch (error_cause) {
60 case SSL_ERROR_WANT_READ:
61 op_result = want_read;
62 break;
63
64 case SSL_ERROR_WANT_WRITE:
65 op_result = want_write;
66 break;
67
68 case SSL_ERROR_ZERO_RETURN:
69 return close;
70
71 case SSL_ERROR_SYSCALL:
72 if (pending > pending_) return want_write;
73 return fatal;
74
75 case SSL_ERROR_SSL:
76 return fatal;
77
78 default:
79 return fatal;
80 }
81 }
82
83 if (pending) {
84 op_result = want_write;
85 }
86
87 return op_result;
88 }
89
90 BIO *bio_;
91 SSL *ssl_;
93 };
94};
95
97 public:
98 static bool is_read_operation() { return true; }
99
100 static int read_ex(SSL *ssl, void *buf, size_t num,
101 size_t *out_number_of_bytes_io) {
102 return SSL_read_ex(ssl, buf, num, out_number_of_bytes_io);
103 }
104
105 static Result op(BIO *bio, SSL *ssl, void *buffer, const size_t buffer_size,
106 size_t *out_number_of_bytes_io) {
108 bio,
109 ssl,
110 };
111
112 if (!buffer_size) return ok;
113
114 return op.check_ssl_result(
115 read_ex(ssl, buffer, buffer_size, out_number_of_bytes_io));
116 }
117};
118
120 public:
121 static int write_ex(SSL *ssl, const void *buf, size_t num,
122 size_t *out_number_of_bytes_io) {
123 return SSL_write_ex(ssl, buf, num, out_number_of_bytes_io);
124 }
125
126 static bool is_read_operation() { return false; }
127
128 static Result op(BIO *bio, SSL *ssl, const void *buffer,
129 const size_t buffer_size, size_t *out_number_of_bytes_io) {
131 bio,
132 ssl,
133 };
134
135 if (!buffer_size) return ok;
136
137 return op.check_ssl_result(
138 write_ex(ssl, buffer, buffer_size, out_number_of_bytes_io));
139 }
140};
141
143 public:
144 static bool is_read_operation() { return false; }
145
146 static Result op(BIO *bio, SSL *ssl, [[maybe_unused]] const void *buffer,
147 [[maybe_unused]] const size_t buffer_size,
148 [[maybe_unused]] size_t *out_number_of_bytes_io) {
150 bio,
151 ssl,
152 };
153
154 return op.check_ssl_result(SSL_connect(ssl));
155 }
156};
157
158} // namespace tls
159} // namespace net
160
161#endif // ROUTER_SRC_HTTP_SRC_TLS_SSL_OPERATION_H_
Definition: ssl_operation.h:44
Result check_ssl_result(int ssl_result)
Definition: ssl_operation.h:51
AnalyzeOperation(BIO *bio, SSL *ssl)
Definition: ssl_operation.h:46
BIO * bio_
Definition: ssl_operation.h:90
int pending_
Definition: ssl_operation.h:92
SSL * ssl_
Definition: ssl_operation.h:91
Definition: ssl_operation.h:36
Result
Definition: ssl_operation.h:38
@ want_read
Definition: ssl_operation.h:38
@ fatal
Definition: ssl_operation.h:38
@ want_write
Definition: ssl_operation.h:38
@ ok
Definition: ssl_operation.h:38
@ close
Definition: ssl_operation.h:38
virtual ~Operation()=default
Definition: ssl_operation.h:142
static Result op(BIO *bio, SSL *ssl, const void *buffer, const size_t buffer_size, size_t *out_number_of_bytes_io)
Definition: ssl_operation.h:146
static bool is_read_operation()
Definition: ssl_operation.h:144
Definition: ssl_operation.h:96
static int read_ex(SSL *ssl, void *buf, size_t num, size_t *out_number_of_bytes_io)
Definition: ssl_operation.h:100
static Result op(BIO *bio, SSL *ssl, void *buffer, const size_t buffer_size, size_t *out_number_of_bytes_io)
Definition: ssl_operation.h:105
static bool is_read_operation()
Definition: ssl_operation.h:98
Definition: ssl_operation.h:119
static bool is_read_operation()
Definition: ssl_operation.h:126
static Result op(BIO *bio, SSL *ssl, const void *buffer, const size_t buffer_size, size_t *out_number_of_bytes_io)
Definition: ssl_operation.h:128
static int write_ex(SSL *ssl, const void *buf, size_t num, size_t *out_number_of_bytes_io)
Definition: ssl_operation.h:121
Definition: buf0block_hint.cc:30
constexpr value_type ssl
Definition: classic_protocol_constants.h:49
Definition: buffer.h:45
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
size_t buffer_size(const ConstBufferSequence &buffers) noexcept
Definition: buffer.h:313
Definition: tls_keylog_dumper.h:33