forked from uroni/urbackup_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenSSLPipe.cpp
557 lines (466 loc) · 10.4 KB
/
OpenSSLPipe.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#ifdef WITH_OPENSSL
#include "OpenSSLPipe.h"
#include <openssl/ssl.h>
#include <openssl/conf.h>
#include <openssl/err.h>
#include <openssl/x509v3.h>
#include <openssl/bio.h>
#include "Server.h"
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <unistd.h>
#endif
OpenSSLPipe::OpenSSLPipe(CStreamPipe * bpipe)
: bpipe(bpipe), bbio(NULL), ctx(NULL),
has_error(false)
{
}
OpenSSLPipe::~OpenSSLPipe()
{
if (bbio != NULL)
BIO_free_all(bbio);
if (ctx != NULL)
SSL_CTX_free(ctx);
}
namespace
{
void log_ssl_err()
{
unsigned long err = ERR_get_error();
if (err != 0)
{
const char* const str = ERR_reason_error_string(err);
if (str)
{
Server->Log(std::string("OpenSSL error: ") + str, LL_WARNING);
}
}
}
const char* const PREFERRED_CIPHERS = "HIGH:!aNULL:!kRSA:!SRP:!PSK:!CAMELLIA:!RC4:!MD5:!DSS";
//ALL that shit just to set MSG_NOSIGNAL :(
BIO_METHOD* meth_socket_nosigpipe;
int bio_read(BIO *b, char * buf, int len)
{
if (buf == NULL || len <= 0)
return 0;
BIO_clear_retry_flags(b);
int fd = BIO_get_fd(b, NULL);
int rc = recv(fd, buf, len, MSG_NOSIGNAL);
if (rc <= 0)
{
#ifdef _WIN32
DWORD err = WSAGetLastError();
if (err == WSAEWOULDBLOCK || err == WSAEINTR)
{
BIO_set_retry_read(b);
}
#else
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
{
BIO_set_retry_read(b);
}
#endif
}
return rc;
}
int bio_read_ex(BIO *b, char * buf, size_t len, size_t *readbytes)
{
int rc = bio_read(b, buf, static_cast<int>(len));
if (rc > 0)
{
*readbytes = rc;
return 1;
}
else
{
return 0;
}
}
int bio_write(BIO* b, const char* buf, int len)
{
if (buf == NULL || len <= 0)
return 0;
BIO_clear_retry_flags(b);
int fd = BIO_get_fd(b, NULL);
int rc = send(fd, buf, len, MSG_NOSIGNAL);
if(rc<0)
{
#ifdef _WIN32
DWORD err = WSAGetLastError();
if (err == EINTR || err== WSAEWOULDBLOCK)
{
BIO_set_retry_write(b);
}
#else
if (errno == EINTR || errno == EAGAIN || errno== EWOULDBLOCK)
{
BIO_set_retry_write(b);
}
#endif
return rc;
}
return rc;
}
int bio_write_ex(BIO* b, const char* buf, size_t len, size_t *written)
{
int rc = bio_write(b, buf, static_cast<int>(len));
if (rc > 0)
{
*written = rc;
return 1;
}
else
{
return 0;
}
}
}
void OpenSSLPipe::init()
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
SSL_library_init();
SSL_load_error_strings();
OPENSSL_config(NULL);
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
const BIO_METHOD* smeth = BIO_s_socket();
#else
BIO_METHOD* smeth = (BIO_METHOD*)BIO_s_socket();
#endif
int s_socket_nosignpipe = BIO_get_new_index();
meth_socket_nosigpipe = BIO_meth_new(s_socket_nosignpipe, "socket_nosigpipe");
BIO_meth_set_callback_ctrl(meth_socket_nosigpipe, BIO_meth_get_callback_ctrl(smeth));
BIO_meth_set_create(meth_socket_nosigpipe, BIO_meth_get_create(smeth));
BIO_meth_set_ctrl(meth_socket_nosigpipe, BIO_meth_get_ctrl(smeth));
BIO_meth_set_destroy(meth_socket_nosigpipe, BIO_meth_get_destroy(smeth));
BIO_meth_set_gets(meth_socket_nosigpipe, BIO_meth_get_gets(smeth));
BIO_meth_set_puts(meth_socket_nosigpipe, BIO_meth_get_puts(smeth));
BIO_meth_set_read(meth_socket_nosigpipe, bio_read);
BIO_meth_set_write(meth_socket_nosigpipe, bio_write);
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
BIO_meth_set_read_ex(meth_socket_nosigpipe, bio_read_ex);
BIO_meth_set_write_ex(meth_socket_nosigpipe, bio_write_ex);
#endif
}
#ifndef S_ISDIR
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
bool OpenSSLPipe::ssl_connect(const std::string & p_hostname, int timeoutms)
{
const SSL_METHOD* method = SSLv23_method();
if (method == NULL)
{
log_ssl_err();
return false;
}
ctx = SSL_CTX_new(method);
if (ctx == NULL)
{
log_ssl_err();
return false;
}
SSL_CTX_set_verify_depth(ctx, 5);
const long flags = SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
SSL_CTX_set_options(ctx, flags);
std::string capath = Server->getServerParameter("capath");
if (!capath.empty())
{
struct stat sbuf;
if (stat(capath.c_str(), &sbuf)==0 &&
!S_ISDIR(sbuf.st_mode))
{
long res = SSL_CTX_load_verify_locations(ctx, capath.c_str(), NULL);
if (res != 1)
{
log_ssl_err();
return false;
}
}
else
{
long res = SSL_CTX_load_verify_locations(ctx, NULL, capath.c_str());
if (res != 1)
{
log_ssl_err();
return false;
}
}
}
else
{
#ifndef OPENSSL_SEARCH_CA
long res = SSL_CTX_set_default_verify_paths(ctx);
if (res != 1)
{
log_ssl_err();
return false;
}
#else
char* capaths[] = {
"/etc/pki/tls/certs/ca-bundle.crt",
"/etc/ssl/certs/ca-certificates.crt",
"/var/lib/ca-certificates/ca-bundle.pem",
"/system/etc/security/cacerts",
"/etc/ssl/certs",
"/etc/pki/tls/certs",
"/etc/openssl/certs",
"/var/ssl/certs",
NULL
};
bool found_ca = false;
for (size_t i=0;capaths[i]!=NULL;++i)
{
char* cafn = capaths[i];
struct stat sbuf;
if (stat(cafn, &sbuf) == 0)
{
if (S_ISDIR(sbuf.st_mode))
{
long res = SSL_CTX_load_verify_locations(ctx, NULL, cafn);
if (res == 1)
{
found_ca = true;
break;
}
}
else
{
long res = SSL_CTX_load_verify_locations(ctx, cafn, NULL);
if (res == 1)
{
found_ca = true;
break;
}
}
}
}
if (!found_ca)
{
Server->Log("No ca certificates found. Please specify certificate bundle path via capath parameter", LL_WARNING);
return false;
}
#endif
}
bbio = BIO_new_ssl(ctx, 1);
if (bbio == NULL)
{
log_ssl_err();
return false;
}
BIO* sbio = BIO_new(meth_socket_nosigpipe);
if (sbio == NULL)
{
log_ssl_err();
return false;
}
BIO_set_fd(sbio, static_cast<int>(bpipe->getSocket()), BIO_NOCLOSE);
BIO_push(bbio, sbio);
BIO_set_nbio(bbio, 1);
SSL *ssl = NULL;
BIO_get_ssl(bbio, &ssl);
if (ssl == NULL)
{
log_ssl_err();
return false;
}
long res = SSL_set_cipher_list(ssl, PREFERRED_CIPHERS);
if (res!=1)
{
log_ssl_err();
return false;
}
res = SSL_set_tlsext_host_name(ssl, p_hostname.c_str());
if (res != 1)
{
log_ssl_err();
return false;
}
#if OPENSSL_VERSION_NUMBER < 0x10100000L
X509_VERIFY_PARAM* param = SSL_get0_param(ssl);
X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
res = X509_VERIFY_PARAM_set1_host(param, p_hostname.c_str(), p_hostname.size());
#else
SSL_set_hostflags(ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
res = SSL_set1_host(ssl, p_hostname.c_str());
#endif
if (res != 1)
{
log_ssl_err();
return false;
}
SSL_set_verify(ssl, SSL_VERIFY_PEER, NULL);
int64 starttime = Server->getTimeMS();
do
{
res = BIO_do_handshake(bbio);
if (res != 1
&& !BIO_should_retry(bbio) )
{
log_ssl_err();
return false;
}
if (res == 1)
break;
int64 rtime = timeoutms - (Server->getTimeMS() - starttime);
if (rtime < 0)
rtime = 0;
if (timeoutms < 0)
rtime = -1;
if (!bpipe->isReadOrWritable(static_cast<int>(rtime)))
break;
} while (Server->getTimeMS() - starttime < timeoutms);
if (res != 1)
{
Server->Log("SSL connect timeout", LL_WARNING);
return false;
}
X509* cert = SSL_get_peer_certificate(ssl);
if (cert)
{
X509_free(cert);
}
if (cert == NULL)
{
Server->Log("Getting server certificate failed", LL_WARNING);
return false;
}
res = SSL_get_verify_result(ssl);
if (res != X509_V_OK)
{
const char* const str = ERR_reason_error_string(res);
if (str)
{
Server->Log("Verifying certificate of hostname " + p_hostname + " failed with OpenSSL error code " + str, LL_WARNING);
}
return false;
}
return true;
}
size_t OpenSSLPipe::Read(char * buffer, size_t bsize, int timeoutms)
{
bool retry=false;
do
{
if (BIO_pending(bbio)<=0)
{
if (!bpipe->isReadable(timeoutms))
{
return 0;
}
}
int rc = BIO_read(bbio, buffer, static_cast<int>(bsize));
if (rc <= 0)
{
if (!BIO_should_retry(bbio))
{
has_error = true;
return 0;
}
else
{
retry=true;
}
}
else
{
bpipe->doThrottle(rc, false, true);
return rc;
}
} while(retry);
return 0;
}
bool OpenSSLPipe::Write(const char * buffer, size_t bsize, int timeoutms, bool flush)
{
if (bsize == 0)
return true;
if (!bpipe->isWritable(timeoutms))
{
return false;
}
int rc = BIO_write(bbio, buffer, static_cast<int>(bsize));
if (rc <= 0)
{
if (!BIO_should_retry(bbio))
{
has_error = true;
}
return false;
}
else
{
if (rc < bsize)
{
bpipe->doThrottle(rc, true, true);
return Write(buffer + rc, bsize - rc, -1, flush);
}
return true;
}
}
size_t OpenSSLPipe::Read(std::string * ret, int timeoutms)
{
char buffer[8192];
size_t l = Read(buffer, 8192, timeoutms);
if (l>0)
{
ret->assign(buffer, l);
}
else
{
return 0;
}
return l;
}
bool OpenSSLPipe::Write(const std::string & str, int timeoutms, bool flush)
{
return Write(&str[0], str.size(), timeoutms, flush);
}
bool OpenSSLPipe::Flush(int timeoutms)
{
return bpipe->Flush(timeoutms);
}
bool OpenSSLPipe::isWritable(int timeoutms)
{
return bpipe->isWritable(timeoutms);
}
bool OpenSSLPipe::isReadable(int timeoutms)
{
return BIO_pending(bbio)>0 || bpipe->isReadable(timeoutms);
}
bool OpenSSLPipe::hasError(void)
{
return has_error || bpipe->hasError();
}
void OpenSSLPipe::shutdown(void)
{
bpipe->shutdown();
}
size_t OpenSSLPipe::getNumWaiters()
{
return bpipe->getNumWaiters();
}
size_t OpenSSLPipe::getNumElements(void)
{
return bpipe->getNumElements();
}
void OpenSSLPipe::addThrottler(IPipeThrottler * throttler)
{
bpipe->addThrottler(throttler);
}
void OpenSSLPipe::addOutgoingThrottler(IPipeThrottler * throttler)
{
bpipe->addOutgoingThrottler(throttler);
}
void OpenSSLPipe::addIncomingThrottler(IPipeThrottler * throttler)
{
bpipe->addIncomingThrottler(throttler);
}
_i64 OpenSSLPipe::getTransferedBytes(void)
{
return bpipe->getTransferedBytes();
}
void OpenSSLPipe::resetTransferedBytes(void)
{
bpipe->resetTransferedBytes();
}
#endif //WITH_OPENSSL