forked from memcached/memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.c
268 lines (236 loc) · 8.35 KB
/
tls.c
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
#include "memcached.h"
#ifdef TLS
#include "tls.h"
#include <string.h>
#include <sysexits.h>
#include <sys/param.h>
#include <openssl/err.h>
#ifndef MAXPATHLEN
#define MAXPATHLEN 4096
#endif
static pthread_mutex_t ssl_ctx_lock = PTHREAD_MUTEX_INITIALIZER;
const unsigned ERROR_MSG_SIZE = 64;
const size_t SSL_ERROR_MSG_SIZE = 256;
void SSL_LOCK() {
pthread_mutex_lock(&(ssl_ctx_lock));
}
void SSL_UNLOCK(void) {
pthread_mutex_unlock(&(ssl_ctx_lock));
}
/*
* Reads decrypted data from the underlying BIO read buffers,
* which reads from the socket.
*/
ssize_t ssl_read(conn *c, void *buf, size_t count) {
assert (c != NULL);
/* TODO : document the state machine interactions for SSL_read with
non-blocking sockets/ SSL re-negotiations
*/
return SSL_read(c->ssl, buf, count);
}
/*
* SSL sendmsg implementation. Perform a SSL_write.
*/
ssize_t ssl_sendmsg(conn *c, struct msghdr *msg, int flags) {
assert (c != NULL);
size_t buf_remain = settings.ssl_wbuf_size;
size_t bytes = 0;
size_t to_copy;
int i;
// ssl_wbuf is pointing to the buffer allocated in the worker thread.
assert(c->ssl_wbuf);
// TODO: allocate a fix buffer in crawler/logger if they start using
// the sendmsg method. Also, set c->ssl_wbuf when the side thread
// start owning the connection and reset the pointer in
// conn_worker_readd.
// Currently this connection would not be served by a different thread
// than the one it's assigned.
assert(pthread_equal(c->thread->thread_id, pthread_self()) != 0);
char *bp = c->ssl_wbuf;
for (i = 0; i < msg->msg_iovlen; i++) {
size_t len = msg->msg_iov[i].iov_len;
to_copy = len < buf_remain ? len : buf_remain;
memcpy(bp + bytes, (void*)msg->msg_iov[i].iov_base, to_copy);
buf_remain -= to_copy;
bytes += to_copy;
if (buf_remain == 0)
break;
}
/* TODO : document the state machine interactions for SSL_write with
non-blocking sockets/ SSL re-negotiations
*/
return SSL_write(c->ssl, c->ssl_wbuf, bytes);
}
/*
* Writes data to the underlying BIO write buffers,
* which encrypt and write them to the socket.
*/
ssize_t ssl_write(conn *c, void *buf, size_t count) {
assert (c != NULL);
return SSL_write(c->ssl, buf, count);
}
/*
* Prints an SSL error into the buff, if there's any.
*/
static void print_ssl_error(char *buff, size_t len) {
unsigned long err;
if ((err = ERR_get_error()) != 0) {
ERR_error_string_n(err, buff, len);
}
}
/*
* Loads server certificates to the SSL context and validate them.
* @return whether certificates are successfully loaded and verified or not.
* @param error_msg contains the error when unsuccessful.
*/
static bool load_server_certificates(char **errmsg) {
bool success = false;
const size_t CRLF_NULLCHAR_LEN = 3;
char *error_msg = malloc(MAXPATHLEN + ERROR_MSG_SIZE +
SSL_ERROR_MSG_SIZE);
size_t errmax = MAXPATHLEN + ERROR_MSG_SIZE + SSL_ERROR_MSG_SIZE -
CRLF_NULLCHAR_LEN;
if (error_msg == NULL) {
*errmsg = NULL;
return false;
}
if (settings.ssl_ctx == NULL) {
snprintf(error_msg, errmax, "Error TLS not enabled\r\n");
*errmsg = error_msg;
return false;
}
char *ssl_err_msg = malloc(SSL_ERROR_MSG_SIZE);
if (ssl_err_msg == NULL) {
free(error_msg);
*errmsg = NULL;
return false;
}
bzero(ssl_err_msg, SSL_ERROR_MSG_SIZE);
size_t err_msg_size = 0;
SSL_LOCK();
if (!SSL_CTX_use_certificate_chain_file(settings.ssl_ctx,
settings.ssl_chain_cert)) {
print_ssl_error(ssl_err_msg, SSL_ERROR_MSG_SIZE);
err_msg_size = snprintf(error_msg, errmax, "Error loading the certificate chain: "
"%s : %s", settings.ssl_chain_cert, ssl_err_msg);
} else if (!SSL_CTX_use_PrivateKey_file(settings.ssl_ctx, settings.ssl_key,
settings.ssl_keyformat)) {
print_ssl_error(ssl_err_msg, SSL_ERROR_MSG_SIZE);
err_msg_size = snprintf(error_msg, errmax, "Error loading the key: %s : %s",
settings.ssl_key, ssl_err_msg);
} else if (!SSL_CTX_check_private_key(settings.ssl_ctx)) {
print_ssl_error(ssl_err_msg, SSL_ERROR_MSG_SIZE);
err_msg_size = snprintf(error_msg, errmax, "Error validating the certificate: %s",
ssl_err_msg);
} else if (settings.ssl_ca_cert) {
if (!SSL_CTX_load_verify_locations(settings.ssl_ctx,
settings.ssl_ca_cert, NULL)) {
print_ssl_error(ssl_err_msg, SSL_ERROR_MSG_SIZE);
err_msg_size = snprintf(error_msg, errmax,
"Error loading the CA certificate: %s : %s",
settings.ssl_ca_cert, ssl_err_msg);
} else {
SSL_CTX_set_client_CA_list(settings.ssl_ctx,
SSL_load_client_CA_file(settings.ssl_ca_cert));
success = true;
}
} else {
success = true;
}
SSL_UNLOCK();
free(ssl_err_msg);
if (success) {
settings.ssl_last_cert_refresh_time = current_time;
free(error_msg);
} else {
*errmsg = error_msg;
error_msg += (err_msg_size >= errmax ? errmax - 1: err_msg_size);
snprintf(error_msg, CRLF_NULLCHAR_LEN, "\r\n");
// Print if there are more errors and drain the queue.
ERR_print_errors_fp(stderr);
}
return success;
}
/*
* Verify SSL settings and initiates the SSL context.
*/
int ssl_init(void) {
assert(settings.ssl_enabled);
// SSL context for the process. All connections will share one
// process level context.
settings.ssl_ctx = SSL_CTX_new(TLS_server_method());
SSL_CTX_set_min_proto_version(settings.ssl_ctx, settings.ssl_min_version);
// The server certificate, private key and validations.
char *error_msg;
if (!load_server_certificates(&error_msg)) {
fprintf(stderr, "%s", error_msg);
free(error_msg);
exit(EX_USAGE);
}
// The verification mode of client certificate, default is SSL_VERIFY_PEER.
SSL_CTX_set_verify(settings.ssl_ctx, settings.ssl_verify_mode, NULL);
if (settings.ssl_ciphers && !SSL_CTX_set_cipher_list(settings.ssl_ctx,
settings.ssl_ciphers)) {
fprintf(stderr, "Error setting the provided cipher(s): %s\n",
settings.ssl_ciphers);
exit(EX_USAGE);
}
// Optional session caching; default disabled.
if (settings.ssl_session_cache) {
SSL_CTX_sess_set_new_cb(settings.ssl_ctx, ssl_new_session_callback);
SSL_CTX_set_session_cache_mode(settings.ssl_ctx, SSL_SESS_CACHE_SERVER);
SSL_CTX_set_session_id_context(settings.ssl_ctx,
(const unsigned char *) SESSION_ID_CONTEXT,
strlen(SESSION_ID_CONTEXT));
} else {
SSL_CTX_set_session_cache_mode(settings.ssl_ctx, SSL_SESS_CACHE_OFF);
}
return 0;
}
/*
* This method is registered with each SSL connection and abort the SSL session
* if a client initiates a renegotiation.
* TODO : Proper way to do this is to set SSL_OP_NO_RENEGOTIATION
* using the SSL_CTX_set_options but that option only available in
* openssl 1.1.0h or above.
*/
void ssl_callback(const SSL *s, int where, int ret) {
SSL* ssl = (SSL*)s;
if (SSL_in_before(ssl)) {
fprintf(stderr, "%d: SSL renegotiation is not supported, "
"closing the connection\n", SSL_get_fd(ssl));
SSL_set_shutdown(ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
return;
}
}
/*
* This method is invoked with every new successfully negotiated SSL session,
* when server-side session caching is enabled. Note that this method is not
* invoked when a session is reused.
*/
int ssl_new_session_callback(SSL *s, SSL_SESSION *sess) {
STATS_LOCK();
stats.ssl_new_sessions++;
STATS_UNLOCK();
return 0;
}
bool refresh_certs(char **errmsg) {
return load_server_certificates(errmsg);
}
const char *ssl_proto_text(int version) {
switch (version) {
case TLS1_VERSION:
return "tlsv1.0";
case TLS1_1_VERSION:
return "tlsv1.1";
case TLS1_2_VERSION:
return "tlsv1.2";
#if defined(TLS1_3_VERSION)
case TLS1_3_VERSION:
return "tlsv1.3";
#endif
default:
return "unknown";
}
}
#endif