MySQL 8.3.0
Source Code Documentation
xcom_network_provider_native_lib.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2023, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef XCOM_NETWORK_PROVIDER_NATIVE_LIB_H
24#define XCOM_NETWORK_PROVIDER_NATIVE_LIB_H
25
26#include "xcom/result.h"
27#include "xcom/site_def.h"
28
29#ifndef XCOM_WITHOUT_OPENSSL
30#ifdef _WIN32
31/* In OpenSSL before 1.1.0, we need this first. */
32#include <Ws2tcpip.h>
33#include <winsock2.h>
34#endif /* _WIN32 */
35
36#include <openssl/err.h>
37#include <openssl/ssl.h>
38#endif /*! XCOM_WITHOUT_OPENSSL*/
39
40#define SYS_STRERROR_SIZE 512
41
43 public:
44 static result checked_create_socket(int domain, int type, int protocol);
45 static struct addrinfo *does_node_have_v4_address(struct addrinfo *retrieved);
46 static int timed_connect(int fd, struct sockaddr *sock_addr,
47 socklen_t sock_size);
48 static int timed_connect_sec(int fd, struct sockaddr *sock_addr,
49 socklen_t sock_size, int timeout);
50 static int timed_connect_msec(int fd, struct sockaddr *sock_addr,
51 socklen_t sock_size, int timeout);
52 static int allowlist_socket_accept(int fd, site_def const *xcom_config);
55
56 private:
57 static void init_server_addr(struct sockaddr **sock_addr, socklen_t *sock_len,
58 xcom_port port, int family);
59 static result xcom_checked_socket(int domain, int type, int protocol);
62 static void gcs_shutdown_socket(int *sock);
63 static result gcs_close_socket(int *sock);
64};
65
66#ifndef XCOM_WITHOUT_OPENSSL
67
68#ifndef SSL_SUCCESS
69#define SSL_SUCCESS 1
70#define SSL_ERROR 0
71#endif
72
74 public:
75 /*
76 Initialize the SSL.
77
78 server_key_file - Path of file that contains the server's X509 key in PEM
79 format.
80 server_cert_file - Path of file that contains the server's X509 certificate
81 in PEM format. client_key_file - Path of file that contains the client's
82 X509 key in PEM format. client_cert_file - Path of file that contains the
83 client's X509 certificate in PEM format. ca_file - Path of file
84 that contains list of trusted SSL CAs. ca_path - Path of directory
85 that contains trusted SSL CA certificates in PEM format. crl_file -
86 Path of file that contains certificate revocation lists. crl_path -
87 Path of directory that contains certificate revocation list files. cipher -
88 List of permitted ciphers to use for connection encryption. tls_version -
89 Protocols permitted for secure connections.
90
91 Note that only the server_key_file/server_cert_file and the client_key_file/
92 client_cert_file are required and the rest of the pointers can be NULL.
93 If the key is provided along with the certificate, either the key file or
94 the other can be omitted.
95
96 The caller can free the parameters after the call if this is necessary.
97
98 Return 0 if success 1 otherwise.
99 */
100 static int xcom_init_ssl(const char *server_key_file,
101 const char *server_cert_file,
102 const char *client_key_file,
103 const char *client_cert_file, const char *ca_file,
104 const char *ca_path, const char *crl_file,
105 const char *crl_path, const char *cipher,
106 const char *tls_version,
107 const char *tls_ciphersuites);
108
109 /*
110 Cleans Up the SSL Configuration freeing allocated memory.
111 */
112 static void xcom_cleanup_ssl();
113
114 /*
115 Destroy the SSL Configuration freeing allocated memory.
116 */
117 static void xcom_destroy_ssl();
118
119 /*
120 Verify whether the server certificate matches the host to which
121 the connection is attempted.
122 */
123 static int ssl_verify_server_cert(SSL *ssl, const char *server_hostname);
124};
125
126/*
127 Pointers to the SSL Context for the server and client
128 contexts respectively.
129*/
130extern SSL_CTX *server_ctx;
131extern SSL_CTX *client_ctx;
132
133#endif /* !XCOM_WITHOUT_OPENSSL */
134#endif // XCOM_NETWORK_PROVIDER_H
Definition: xcom_network_provider_native_lib.h:42
static result create_server_socket()
Definition: xcom_network_provider_native_lib.cc:125
static result announce_tcp(xcom_port port)
Definition: xcom_network_provider_native_lib.cc:206
static int timed_connect(int fd, struct sockaddr *sock_addr, socklen_t sock_size)
Definition: xcom_network_provider_native_lib.cc:452
static int timed_connect_msec(int fd, struct sockaddr *sock_addr, socklen_t sock_size, int timeout)
Definition: xcom_network_provider_native_lib.cc:340
static struct addrinfo * does_node_have_v4_address(struct addrinfo *retrieved)
Retrieves a node IPv4 address, if it exists.
Definition: xcom_network_provider_native_lib.cc:509
static void gcs_shutdown_socket(int *sock)
Definition: xcom_network_provider_native_lib.cc:310
static int timed_connect_sec(int fd, struct sockaddr *sock_addr, socklen_t sock_size, int timeout)
Definition: xcom_network_provider_native_lib.cc:459
static result gcs_close_socket(int *sock)
Definition: xcom_network_provider_native_lib.cc:316
static result checked_create_socket(int domain, int type, int protocol)
Definition: xcom_network_provider_native_lib.cc:467
static result xcom_checked_socket(int domain, int type, int protocol)
Wrapper function which retries and checks errors from socket.
Definition: xcom_network_provider_native_lib.cc:110
static int allowlist_socket_accept(int fd, site_def const *xcom_config)
Definition: xcom_network_provider_native_lib.cc:284
static result create_server_socket_v4()
Definition: xcom_network_provider_native_lib.cc:176
static result gcs_shut_close_socket(int *sock)
Definition: xcom_network_provider_native_lib.cc:327
static void init_server_addr(struct sockaddr **sock_addr, socklen_t *sock_len, xcom_port port, int family)
Initializes a sockaddr prepared to be used in bind()
Definition: xcom_network_provider_native_lib.cc:76
Definition: xcom_network_provider_native_lib.h:73
static void xcom_cleanup_ssl()
Definition: xcom_network_provider_ssl_native_lib.cc:544
static int ssl_verify_server_cert(SSL *ssl, const char *server_hostname)
Definition: xcom_network_provider_ssl_native_lib.cc:577
static void xcom_destroy_ssl()
Definition: xcom_network_provider_ssl_native_lib.cc:550
static int xcom_init_ssl(const char *server_key_file, const char *server_cert_file, const char *client_key_file, const char *client_cert_file, const char *ca_file, const char *ca_path, const char *crl_file, const char *crl_path, const char *cipher, const char *tls_version, const char *tls_ciphersuites)
Definition: xcom_network_provider_ssl_native_lib.cc:466
static MYSQL * sock
Definition: mysqlcheck.cc:56
constexpr value_type ssl
Definition: classic_protocol_constants.h:48
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:497
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:32
required string type
Definition: replication_group_member_actions.proto:33
struct sockaddr sockaddr
Definition: sock_probe_win32.h:62
Definition: result.h:29
Definition: site_struct.h:42
unsigned short xcom_port
Definition: xcom_common.h:45
SSL_CTX * client_ctx
Definition: xcom_network_provider_ssl_native_lib.cc:162
SSL_CTX * server_ctx
Definition: xcom_network_provider_ssl_native_lib.cc:161