-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathproxy_tls.cpp
490 lines (443 loc) · 15.2 KB
/
proxy_tls.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "proxysql.h"
#include "cpp.h"
#include <openssl/x509v3.h>
static long
get_file_size (const char *filename) {
FILE *fp;
fp = fopen (filename, "rb");
if (fp) {
long size;
if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
size = 0;
fclose (fp);
return size;
} else
return 0;
}
static char * load_file (const char *filename) {
FILE *fp;
char *buffer;
long size;
size = get_file_size (filename);
if (0 == size)
return NULL;
fp = fopen (filename, "rb");
if (! fp)
return NULL;
buffer = (char *)malloc (size + 1);
if (! buffer) {
fclose (fp);
return NULL;
}
buffer[size] = '\0';
if (size != (long)fread (buffer, 1, size, fp)) {
free (buffer);
buffer = NULL;
}
fclose (fp);
return buffer;
}
// absolute path of ssl files
static char *ssl_key_fp = NULL;
static char *ssl_cert_fp = NULL;
static char *ssl_ca_fp = NULL;
struct dh_st {
int pad;
int version;
BIGNUM *p;
BIGNUM *g;
long length;
BIGNUM *pub_key;
BIGNUM *priv_key;
int flags;
BN_MONT_CTX *method_mont_p;
BIGNUM *q;
BIGNUM *j;
unsigned char *seed;
int seedlen;
BIGNUM *counter;
int references;
CRYPTO_EX_DATA ex_data;
const DH_METHOD *meth;
ENGINE *engine;
CRYPTO_RWLOCK *lock;
};
int callback_ssl_verify_peer(int ok, X509_STORE_CTX* ctx) {
// for now only return 1
return 1;
}
X509 * generate_x509(EVP_PKEY *pkey, const unsigned char *cn, uint32_t serial, int days, X509 *ca_x509, EVP_PKEY *ca_pkey) {
int rc;
X509 * x = NULL;
X509_NAME * name= NULL;
X509_EXTENSION* ext = NULL;
X509V3_CTX v3_ctx;
if ((x = X509_new()) == NULL) {
proxy_error("Unable to run X509_new()\n");
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
X509_set_version(x, 2);
ASN1_INTEGER_set(X509_get_serialNumber(x), serial);
X509_gmtime_adj(X509_get_notBefore(x), 0);
X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days);
rc = X509_set_pubkey(x, pkey);
if (rc==0){
proxy_error("Unable to set pubkey: %s\n", ERR_error_string(ERR_get_error(),NULL));
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
name = X509_get_subject_name(x);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, cn, -1, -1, 0);
if (ca_x509) {
rc = X509_set_issuer_name(x, X509_get_subject_name(ca_x509));
} else {
rc = X509_set_issuer_name(x, name);
}
if (rc==0) {
proxy_error("Unable to set issuer: %s\n", ERR_error_string(ERR_get_error(),NULL));
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
// set the context
X509V3_set_ctx(&v3_ctx, ca_x509 ? ca_x509 : x, x, NULL, NULL, 0);
ext = X509V3_EXT_conf_nid(
NULL, &v3_ctx, NID_basic_constraints, ca_x509 ? "critical, CA:FALSE" : "critical, CA:TRUE");
if (ext) {
X509_add_ext(x, ext, -1);
X509_EXTENSION_free(ext);
} else {
proxy_error("Unable to set certificate extensions: %s\n", ERR_error_string(ERR_get_error(),NULL));
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
if (ca_pkey) {
rc = X509_sign(x, ca_pkey, EVP_sha256());
} else {
rc = X509_sign(x, pkey, EVP_sha256());
}
if (rc==0) {
proxy_error("Unable to X509 sign: %s\n", ERR_error_string(ERR_get_error(),NULL));
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
return x;
}
void write_x509(const char *filen, X509 *x) {
BIO * x509file = NULL;
x509file = BIO_new_file(filen, "w" );
if (!x509file ) {
proxy_error("Error on BIO_new_file\n");
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
if (!PEM_write_bio_X509( x509file, x)) {
proxy_error("Error on PEM_write_bio_X509 for %s\n", filen);
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
BIO_free_all( x509file );
}
void write_rsa_key(const char *filen, RSA *rsa) {
BIO* pOut = BIO_new_file(filen, "w");
if (!pOut) {
proxy_error("Error on BIO_new_file\n");
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
if (!PEM_write_bio_RSAPrivateKey( pOut, rsa, NULL, NULL, 0, NULL, NULL)) {
proxy_error("Error on PEM_write_bio_RSAPrivateKey for %s\n", filen);
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
BIO_free_all( pOut );
}
EVP_PKEY * proxy_key_read(const char *filen, bool bootstrap, std::string& msg) {
EVP_PKEY * pkey = NULL;
BIO * pIn = BIO_new_file(filen,"r");
if (!pIn) {
proxy_error("Error on BIO_new_file() while reading %s\n", filen);
if (bootstrap == true) {
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
} else {
msg = "Error on BIO_new_file() while reading " + std::string(filen);
return pkey;
}
}
pkey = PEM_read_bio_PrivateKey( pIn , NULL, NULL, NULL);
if (pkey == NULL) {
proxy_error("Error on PEM_read_bio_PrivateKey for %s\n", filen);
if (bootstrap == true) {
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
} else {
msg = "Error on PEM_read_bio_PrivateKey() for " + std::string(filen);
BIO_free(pIn);
return pkey;
}
}
BIO_free(pIn);
return pkey;
}
X509 * proxy_read_x509(const char *filen, bool bootstrap, std::string& msg) {
X509 * x = NULL;
BIO * x509file = NULL;
x509file = BIO_new_file(filen, "r" );
if (!x509file ) {
proxy_error("Error on BIO_new_file() while reading %s\n", filen);
if (bootstrap == true) {
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
} else {
msg = "Error on BIO_new_file() while reading " + std::string(filen);
return x;
}
}
x = PEM_read_bio_X509( x509file, NULL, NULL, NULL);
if (x == NULL) {
proxy_error("Error on PEM_read_bio_X509 for %s\n", filen);
if (bootstrap == true) {
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
} else {
msg = "Error on PEM_read_bio_X509() for " + std::string(filen);
BIO_free_all(x509file);
return x;
}
}
BIO_free_all( x509file );
return x;
}
// return 0 un success
int ssl_mkit(X509 **x509ca, X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days, bool bootstrap, std::string& msg) {
X509 *x1;
X509 *x2;
EVP_PKEY *pk;
RSA *rsa;
// relative path to datadir of ssl files
const char * ssl_key_rp = (const char *)"proxysql-key.pem";
const char * ssl_cert_rp = (const char *)"proxysql-cert.pem";
const char * ssl_ca_rp = (const char *)"proxysql-ca.pem";
// how many files exists ?
int nfiles = 0;
bool ssl_key_exists = true;
bool ssl_cert_exists = true;
bool ssl_ca_exists = true;
// check if files exists
if (bootstrap == true) {
ssl_key_fp = (char *)malloc(strlen(GloVars.datadir)+strlen(ssl_key_rp)+8);
sprintf(ssl_key_fp,"%s/%s",GloVars.datadir,ssl_key_rp);
}
if (access(ssl_key_fp, R_OK)) {
ssl_key_exists = false;
}
if (bootstrap == true) {
ssl_cert_fp = (char *)malloc(strlen(GloVars.datadir)+strlen(ssl_cert_rp)+8);
sprintf(ssl_cert_fp,"%s/%s",GloVars.datadir,ssl_cert_rp);
}
if (access(ssl_cert_fp, R_OK)) {
ssl_cert_exists = false;
}
if (bootstrap == true) {
ssl_ca_fp = (char *)malloc(strlen(GloVars.datadir)+strlen(ssl_ca_rp)+8);
sprintf(ssl_ca_fp,"%s/%s",GloVars.datadir,ssl_ca_rp);
}
if (access(ssl_ca_fp, R_OK)) {
ssl_ca_exists = false;
}
nfiles += (ssl_key_exists ? 1 : 0);
nfiles += (ssl_cert_exists ? 1 : 0);
nfiles += (ssl_ca_exists ? 1 : 0);
if (
(bootstrap == true && (nfiles != 0 && nfiles != 3))
||
(bootstrap == false && (nfiles != 3))
) {
if (bootstrap == true) {
proxy_error("Only some SSL files are present. Either all files are present, or none. Exiting.\n");
} else {
proxy_error("Aborting PROXYSQL RELOAD TLS because not all SSL files are present\n");
}
proxy_error("%s : %s\n" , ssl_key_rp, (ssl_key_exists ? (char *)"YES" : (char *)"NO"));
proxy_error("%s : %s\n" , ssl_cert_rp, (ssl_cert_exists ? (char *)"YES" : (char *)"NO"));
proxy_error("%s : %s\n" , ssl_ca_rp, (ssl_ca_exists ? (char *)"YES" : (char *)"NO"));
if (bootstrap == true) {
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
} else {
msg = "RELOAD TLS failed: " + std::to_string(nfiles) + " TLS files are present. Expected: 3";
return 1;
}
}
if (bootstrap == true && nfiles == 0) {
proxy_info("No SSL keys/certificates found in datadir (%s). Generating new keys/certificates.\n", GloVars.datadir);
if ((pkeyp == NULL) || (*pkeyp == NULL)) {
if ((pk = EVP_PKEY_new()) == NULL) {
proxy_error("Unable to run EVP_PKEY_new()\n");
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
} else
pk = *pkeyp;
rsa = RSA_new();
if (!rsa) {
proxy_error("Unable to run RSA_new()\n");
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
BIGNUM *e= BN_new();
if (!e) {
proxy_error("Unable to run BN_new()\n");
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
if (!BN_set_word(e, RSA_F4) || !RSA_generate_key_ex(rsa, bits, e, NULL)) {
RSA_free(rsa);
BN_free(e);
proxy_error("Unable to run BN_new()\n");
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
BN_free(e);
write_rsa_key(ssl_key_fp, rsa);
if (!EVP_PKEY_assign_RSA(pk, rsa)) {
proxy_error("Unable to run EVP_PKEY_assign_RSA()\n");
exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted
}
time_t t = time(NULL);
x1 = generate_x509(pk, (const unsigned char *)"ProxySQL_Auto_Generated_CA_Certificate", t, 3650, NULL, NULL);
write_x509(ssl_ca_fp, x1);
x2 = generate_x509(pk, (const unsigned char *)"ProxySQL_Auto_Generated_Server_Certificate", t, 3650, x1, pk);
write_x509(ssl_cert_fp, x2);
rsa = NULL;
} else {
proxy_info("SSL keys/certificates found in datadir (%s): loading them.\n", GloVars.datadir);
if (bootstrap == true) {
// during bootstrap we just call the reads
// if the read fails during bootstrap, proxysql immediately exists
pk = proxy_key_read(ssl_key_fp, bootstrap, msg);
x1 = proxy_read_x509(ssl_ca_fp, bootstrap, msg);
x2 = proxy_read_x509(ssl_cert_fp, bootstrap, msg);
} else {
pk = proxy_key_read(ssl_key_fp, bootstrap, msg);
if (pk) {
x1 = proxy_read_x509(ssl_ca_fp, bootstrap, msg);
if (x1) {
x2 = proxy_read_x509(ssl_cert_fp, bootstrap, msg);
}
}
// note that this is only relevant during PROXYSQL RELOAD TLS
if (pk == NULL || x1 == NULL || x2 == NULL) {
return 1;
}
}
}
*x509ca = x1;
*x509p = x2;
*pkeyp = pk;
if (bootstrap == true) {
if (SSL_CTX_set_dh_auto(GloVars.global.ssl_ctx, 1) == 0) {
proxy_error("Error in SSL while initializing DH: %s . Shutting down.\n",ERR_error_string(ERR_get_error(), NULL));
exit(EXIT_SUCCESS); // EXIT_SUCCESS to avoid a restart loop
}
} else {
SSL_METHOD *ssl_method;
ssl_method = (SSL_METHOD *)TLS_server_method();
GloVars.global.tmp_ssl_ctx = SSL_CTX_new(ssl_method);
if (SSL_CTX_set_dh_auto(GloVars.global.ssl_ctx, 1) == 0) {
proxy_error("Error in SSL while initializing DH: %s . Shutting down.\n",ERR_error_string(ERR_get_error(), NULL));
proxy_error("Aborting PROXYSQL RELOAD TLS. Error in SSL while initializing DH: %s\n",ERR_error_string(ERR_get_error(), NULL));
msg = "RELOAD TLS failed: Error initializing DH. ";
msg += ERR_error_string(ERR_get_error(), NULL);
return 1;
}
}
return 0;
}
int ProxySQL_create_or_load_TLS(bool bootstrap, std::string& msg) {
BIO *bio_err;
X509 *x509 = NULL;
X509 *x509ca = NULL;
EVP_PKEY *pkey = NULL;
int ret = 0;
bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
if (bootstrap == true) {
// this is legacy code, when keys are loaded only during bootstrap
if (ssl_mkit(&x509ca, &x509, &pkey, 2048, 0, 730, true, msg) != 0) {
proxy_error("Unable to initialize SSL. Shutting down...\n");
exit(EXIT_SUCCESS); // we exit gracefully to not be restarted
}
if ( SSL_CTX_use_certificate(GloVars.global.ssl_ctx, x509) <= 0 ) {
ERR_print_errors_fp(stderr);
proxy_error("Unable to use SSL certificate. Shutting down...\n");
exit(EXIT_SUCCESS); // we exit gracefully to not be restarted
}
if ( SSL_CTX_add_extra_chain_cert(GloVars.global.ssl_ctx, x509ca) <= 0 ) {
ERR_print_errors_fp(stderr);
proxy_error("Unable to use SSL CA chain. Shutting down...\n");
exit(EXIT_SUCCESS); // we exit gracefully to not be restarted
}
if ( SSL_CTX_use_PrivateKey(GloVars.global.ssl_ctx, pkey) <= 0 ) {
ERR_print_errors_fp(stderr);
proxy_error("Unable to use SSL key. Shutting down...\n");
exit(EXIT_SUCCESS); // we exit gracefully to not be restarted
}
if ( !SSL_CTX_check_private_key(GloVars.global.ssl_ctx) ) {
proxy_error("Private key does not match the public certificate\n");
exit(EXIT_SUCCESS); // we exit gracefully to not be restarted
}
GloVars.global.ssl_key_pem_mem = load_file(ssl_key_fp);
GloVars.global.ssl_cert_pem_mem = load_file(ssl_cert_fp);
// We set the locations for the certificates to be used for
// verifications purposes.
if (!SSL_CTX_load_verify_locations(GloVars.global.ssl_ctx, ssl_ca_fp, ssl_ca_fp)) {
proxy_error("Unable to load CA certificates location for verification. Shutting down\n");
exit(EXIT_SUCCESS); // we exit gracefully to not be restarted
}
} else {
// here we use global.tmp_ssl_ctx instead of global.ssl_ctx
// because we will try to swap at the end
if (ssl_mkit(&x509ca, &x509, &pkey, 2048, 0, 730, false, msg) == 0) { // 0 on success
if (SSL_CTX_use_certificate(GloVars.global.tmp_ssl_ctx, x509) == 1) { // 1 on success
if (SSL_CTX_add_extra_chain_cert(GloVars.global.tmp_ssl_ctx, x509ca) == 1) { // 1 on success
if (SSL_CTX_use_PrivateKey(GloVars.global.tmp_ssl_ctx, pkey) == 1) { // 1 on success
if (SSL_CTX_check_private_key(GloVars.global.tmp_ssl_ctx) == 1) { // 1 on success
if (SSL_CTX_load_verify_locations(GloVars.global.tmp_ssl_ctx, ssl_ca_fp, ssl_ca_fp) == 1) { // 1 on success
// take the mutex
std::lock_guard<std::mutex> lock(GloVars.global.ssl_mutex);
// note: we don't free the current SSL context, perhaps used by some connections
// swap the SSL context
GloVars.global.ssl_ctx = GloVars.global.tmp_ssl_ctx;
GloVars.global.tmp_ssl_ctx = NULL;
free(GloVars.global.ssl_key_pem_mem);
free(GloVars.global.ssl_cert_pem_mem);
GloVars.global.ssl_key_pem_mem = load_file(ssl_key_fp);
GloVars.global.ssl_cert_pem_mem = load_file(ssl_cert_fp);
} else {
proxy_error("Failed to load location of CA certificates for verification\n");
msg = "Unable to load CA certificates location for verification";
ret = 1;
}
} else {
proxy_error("Private key does not match the public certificate\n");
msg = "Private key does not match the public certificate";
ret = 1;
}
} else {
ERR_print_errors_fp(stderr);
proxy_error("Unable to use SSL key\n");
msg = "Unable to use SSL key";
ret = 1;
}
} else {
ERR_print_errors_fp(stderr);
proxy_error("Unable to use SSL CA chain\n");
msg = "Unable to use SSL CA chain";
ret = 1;
}
} else {
ERR_print_errors_fp(stderr);
proxy_error("Unable to use SSL certificate\n");
msg = "Unable to use SSL certificate";
ret = 1;
}
} else {
proxy_error("Unable to initialize SSL\n");
if (msg.length() == 0) {
msg = "Unable to initialize SSL";
}
ret = 1;
}
}
if (ret == 0) {
// https://github.com/sysown/proxysql/issues/4419
SSL_CTX_set_verify(GloVars.global.ssl_ctx, SSL_VERIFY_NONE, callback_ssl_verify_peer);
}
X509_free(x509);
EVP_PKEY_free(pkey);
BIO_free(bio_err);
return ret;
}