3
#ifdef ENABLE_HTTPS
4
5
SSL_CTX *netdata_ssl_exporting_ctx =NULL;
6
-SSL_CTX *netdata_ssl_client_ctx =NULL;
7
-SSL_CTX *netdata_ssl_srv_ctx =NULL;
6
+SSL_CTX *netdata_ssl_streaming_sender_ctx =NULL;
7
+SSL_CTX *netdata_ssl_web_server_ctx =NULL;
8
const char *netdata_ssl_security_key =NULL;
9
const char *netdata_ssl_security_cert =NULL;
10
const char *tls_version=NULL;
11
const char *tls_ciphers=NULL;
12
-int netdata_ssl_validate_server = NETDATA_SSL_VALID_CERTIFICATE;
12
+bool netdata_ssl_validate_certificate = true;
13
+bool netdata_ssl_validate_certificate_sender = true;
14
+
15
+static SOCKET_PEERS netdata_ssl_peers(NETDATA_SSL *ssl) {
16
+ int sock_fd;
17
+
18
+ if(unlikely(!ssl->conn))
19
+ sock_fd = -1;
20
+ else
21
+ sock_fd = SSL_get_rfd(ssl->conn);
22
+
23
+ return socket_peers(sock_fd);
24
+}
25
+
26
+bool netdata_ssl_open(NETDATA_SSL *ssl, SSL_CTX *ctx, int fd) {
27
+ errno = 0;
28
+ ssl->ssl_errno = 0;
29
+
30
+ if(ssl->conn) {
31
+ if(!ctx || SSL_get_SSL_CTX(ssl->conn) != ctx) {
32
+ SSL_free(ssl->conn);
33
+ ssl->conn = NULL;
34
+ }
35
+ else if (SSL_clear(ssl->conn) == 0) {
36
+ netdata_ssl_log_error_queue("SSL_clear", ssl);
37
+ SSL_free(ssl->conn);
38
+ ssl->conn = NULL;
39
+ }
40
+ }
41
+
42
+ if(!ssl->conn) {
43
+ if(!ctx) {
44
+ internal_error(true, "SSL: not CTX given");
45
+ ssl->state = NETDATA_SSL_STATE_FAILED;
46
+ return false;
47
+ }
48
+
49
+ ssl->conn = SSL_new(ctx);
50
+ if (!ssl->conn) {
51
+ netdata_ssl_log_error_queue("SSL_new", ssl);
52
+ ssl->state = NETDATA_SSL_STATE_FAILED;
53
+ return false;
54
+ }
55
+ }
56
+
57
+ if(SSL_set_fd(ssl->conn, fd) != 1) {
58
+ netdata_ssl_log_error_queue("SSL_set_fd", ssl);
59
+ ssl->state = NETDATA_SSL_STATE_FAILED;
60
+ return false;
61
+ }
62
+
63
+ ssl->state = NETDATA_SSL_STATE_INIT;
64
+
65
+ ERR_clear_error();
66
+
67
+ return true;
68
+}
69
+
70
+void netdata_ssl_close(NETDATA_SSL *ssl) {
71
+ errno = 0;
72
+ ssl->ssl_errno = 0;
73
+
74
+ if(ssl->conn) {
75
+ if(SSL_connection(ssl)) {
76
+ int ret = SSL_shutdown(ssl->conn);
77
+ if(ret == 0)
78
+ SSL_shutdown(ssl->conn);
79
+ }
80
+
81
+ SSL_free(ssl->conn);
82
+
83
+ ERR_clear_error();
84
+ }
85
+
86
+ *ssl = NETDATA_SSL_UNSET_CONNECTION;
87
+}
88
+
89
+void netdata_ssl_log_error_queue(const char *call, NETDATA_SSL *ssl) {
90
+ error_limit_static_thread_var(erl, 1, 0);
91
+ unsigned long err;
92
+ while((err = ERR_get_error())) {
93
+ char *code;
94
+
95
+ switch (err) {
96
+ case SSL_ERROR_NONE:
97
+ code = "SSL_ERROR_NONE";
98
+ break;
99
+
100
+ case SSL_ERROR_SSL:
101
+ code = "SSL_ERROR_SSL";
102
+ ssl->state = NETDATA_SSL_STATE_FAILED;
103
+ break;
104
+
105
+ case SSL_ERROR_WANT_READ:
106
+ code = "SSL_ERROR_WANT_READ";
107
+ break;
108
+
109
+ case SSL_ERROR_WANT_WRITE:
110
+ code = "SSL_ERROR_WANT_WRITE";
111
+ break;
112
+
113
+ case SSL_ERROR_WANT_X509_LOOKUP:
114
+ code = "SSL_ERROR_WANT_X509_LOOKUP";
115
+ break;
116
+
117
+ case SSL_ERROR_SYSCALL:
118
+ code = "SSL_ERROR_SYSCALL";
119
+ ssl->state = NETDATA_SSL_STATE_FAILED;
120
+ break;
121
+
122
+ case SSL_ERROR_ZERO_RETURN:
123
+ code = "SSL_ERROR_ZERO_RETURN";
124
+ break;
125
+
126
+ case SSL_ERROR_WANT_CONNECT:
127
+ code = "SSL_ERROR_WANT_CONNECT";
128
+ break;
129
+
130
+ case SSL_ERROR_WANT_ACCEPT:
131
+ code = "SSL_ERROR_WANT_ACCEPT";
132
+ break;
133
+
134
+#ifdef SSL_ERROR_WANT_ASYNC
135
+ case SSL_ERROR_WANT_ASYNC:
136
+ code = "SSL_ERROR_WANT_ASYNC";
137
+ break;
138
+#endif
139
+
140
+#ifdef SSL_ERROR_WANT_ASYNC_JOB
141
+ case SSL_ERROR_WANT_ASYNC_JOB:
142
+ code = "SSL_ERROR_WANT_ASYNC_JOB";
143
+ break;
144
+#endif
145
+
146
+#ifdef SSL_ERROR_WANT_CLIENT_HELLO_CB
147
+ case SSL_ERROR_WANT_CLIENT_HELLO_CB:
148
+ code = "SSL_ERROR_WANT_CLIENT_HELLO_CB";
149
+ break;
150
+#endif
151
+
152
+#ifdef SSL_ERROR_WANT_RETRY_VERIFY
153
+ case SSL_ERROR_WANT_RETRY_VERIFY:
154
+ code = "SSL_ERROR_WANT_RETRY_VERIFY";
155
+ break;
156
+#endif
157
+
158
+ default:
159
+ code = "SSL_ERROR_UNKNOWN";
160
+ break;
161
+ }
162
+
163
+ char str[1024 + 1];
164
+ ERR_error_string_n(err, str, 1024);
165
+ str[1024] = '\0';
166
+ SOCKET_PEERS peers = netdata_ssl_peers(ssl);
167
+ error_limit(&erl, "SSL: %s() on socket local [[%s]:%d] <-> remote [[%s]:%d], returned error %lu (%s): %s",
168
+ call, peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, err, code, str);
169
+ }
170
+}
171
+
172
+static inline bool is_handshake_complete(NETDATA_SSL *ssl, const char *op) {
173
+ error_limit_static_thread_var(erl, 1, 0);
174
+
175
+ if(unlikely(!ssl->conn)) {
176
+ internal_error(true, "SSL: trying to %s on a NULL connection", op);
177
+ return false;
178
+ }
179
+
180
+ switch(ssl->state) {
181
+ case NETDATA_SSL_STATE_NOT_SSL: {
182
+ SOCKET_PEERS peers = netdata_ssl_peers(ssl);
183
+ error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on non-SSL connection",
184
+ peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
185
+ return false;
186
+ }
187
+
188
+ case NETDATA_SSL_STATE_INIT: {
189
+ SOCKET_PEERS peers = netdata_ssl_peers(ssl);
190
+ error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on an incomplete connection",
191
+ peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
192
+ return false;
193
+ }
194
+
195
+ case NETDATA_SSL_STATE_FAILED: {
196
+ SOCKET_PEERS peers = netdata_ssl_peers(ssl);
197
+ error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on a failed connection",
198
+ peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
199
+ return false;
200
+ }
201
+
202
+ case NETDATA_SSL_STATE_COMPLETE: {
203
+ return true;
204
+ }
205
+ }
206
+
207
+ return false;
208
+}
209
+
210
+/*
211
+ * netdata_ssl_read() should return the same as read():
212
+ *
213
+ * Positive value: The read() function succeeded and read some bytes. The exact number of bytes read is returned.
214
+ *
215
+ * Zero: For files and sockets, a return value of zero signifies end-of-file (EOF), meaning no more data is available
216
+ * for reading. For sockets, this usually means the other side has closed the connection.
217
+ *
218
+ * -1: An error occurred. The specific error can be found by examining the errno variable.
219
+ * EAGAIN or EWOULDBLOCK: The file descriptor is in non-blocking mode, and the read operation would block.
220
+ * (These are often the same value, but can be different on some systems.)
221
+ */
222
+
223
+ssize_t netdata_ssl_read(NETDATA_SSL *ssl, void *buf, size_t num) {
224
+ errno = 0;
225
+ ssl->ssl_errno = 0;
226
+
227
+ if(unlikely(!is_handshake_complete(ssl, "read")))
228
+ return -1;
229
+
230
+ int bytes = SSL_read(ssl->conn, buf, (int)num);
231
+
232
+ if(unlikely(bytes <= 0)) {
233
+ int err = SSL_get_error(ssl->conn, bytes);
234
+ netdata_ssl_log_error_queue("SSL_read", ssl);
235
+ if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
236
+ ssl->ssl_errno = err;
237
+ errno = EWOULDBLOCK;
238
+ }
239
+
240
+ bytes = -1; // according to read() or recv()
241
+ }
242
+
243
+ return bytes;
244
+}
245
+
246
+/*
247
+ * netdata_ssl_write() should return the same as write():
248
+ *
249
+ * Positive value: The write() function succeeded and wrote some bytes. The exact number of bytes written is returned.
250
+ *
251
+ * Zero: It's technically possible for write() to return zero, indicating that zero bytes were written. However, for a
252
+ * socket, this generally does not happen unless the size of the data to be written is zero.
253
+ *
254
+ * -1: An error occurred. The specific error can be found by examining the errno variable.
255
+ * EAGAIN or EWOULDBLOCK: The file descriptor is in non-blocking mode, and the write operation would block.
256
+ * (These are often the same value, but can be different on some systems.)
257
+ */
258
+
259
+ssize_t netdata_ssl_write(NETDATA_SSL *ssl, const void *buf, size_t num) {
260
+ errno = 0;
261
+ ssl->ssl_errno = 0;
262
+
263
+ if(unlikely(!is_handshake_complete(ssl, "write")))
264
+ return -1;
265
+
266
+ int bytes = SSL_write(ssl->conn, (uint8_t *)buf, (int)num);
267
+
268
+ if(unlikely(bytes <= 0)) {
269
+ int err = SSL_get_error(ssl->conn, bytes);
270
+ netdata_ssl_log_error_queue("SSL_write", ssl);
271
+ if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
272
+ ssl->ssl_errno = err;
273
+ errno = EWOULDBLOCK;
274
+ }
275
+
276
+ bytes = -1; // according to write() or send()
277
+ }
278
+
279
+ return bytes;
280
+}
281
+
282
+static inline bool is_handshake_initialized(NETDATA_SSL *ssl, const char *op) {
283
+ error_limit_static_thread_var(erl, 1, 0);
284
+
285
+ if(unlikely(!ssl->conn)) {
286
+ internal_error(true, "SSL: trying to %s on a NULL connection", op);
287
+ return false;
288
+ }
289
+
290
+ switch(ssl->state) {
291
+ case NETDATA_SSL_STATE_NOT_SSL: {
292
+ SOCKET_PEERS peers = netdata_ssl_peers(ssl);
293
+ error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on non-SSL connection",
294
+ peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
295
+ return false;
296
+ }
297
+
298
+ case NETDATA_SSL_STATE_INIT: {
299
+ return true;
300
+ }
301
+
302
+ case NETDATA_SSL_STATE_FAILED: {
303
+ SOCKET_PEERS peers = netdata_ssl_peers(ssl);
304
+ error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on a failed connection",
305
+ peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
306
+ return false;
307
+ }
308
+
309
+ case NETDATA_SSL_STATE_COMPLETE: {
310
+ SOCKET_PEERS peers = netdata_ssl_peers(ssl);
311
+ error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on an complete connection",
312
+ peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
313
+ return false;
314
+ }
315
+ }
316
+
317
+ return false;
318
+}
319
+
320
+#define WANT_READ_WRITE_TIMEOUT_MS 10
321
+
322
+static inline bool want_read_write_should_retry(NETDATA_SSL *ssl, int err) {
323
+ int ssl_errno = SSL_get_error(ssl->conn, err);
324
+ if(ssl_errno == SSL_ERROR_WANT_READ || ssl_errno == SSL_ERROR_WANT_WRITE) {
325
+ struct pollfd pfds[1] = { [0] = {
326
+ .fd = SSL_get_rfd(ssl->conn),
327
+ .events = (short)(((ssl_errno == SSL_ERROR_WANT_READ ) ? POLLIN : 0) |
328
+ ((ssl_errno == SSL_ERROR_WANT_WRITE) ? POLLOUT : 0)),
329
+ }};
330
+
331
+ if(poll(pfds, 1, WANT_READ_WRITE_TIMEOUT_MS) <= 0)
332
+ return false; // timeout (0) or error (<0)
333
+
334
+ return true; // we have activity, so we should retry
335
+ }
336
+
337
+ return false; // an unknown error
338
+}
339
+
340
+bool netdata_ssl_connect(NETDATA_SSL *ssl) {
341
+ errno = 0;
342
+ ssl->ssl_errno = 0;
343
+
344
+ if(unlikely(!is_handshake_initialized(ssl, "connect")))
345
+ return false;
346
+
347
+ SSL_set_connect_state(ssl->conn);
348
+
349
+ int err;
350
+ while ((err = SSL_connect(ssl->conn)) != 1) {
351
+ if(!want_read_write_should_retry(ssl, err))
352
+ break;
353
+ }
354
+
355
+ if (err != 1) {
356
+ netdata_ssl_log_error_queue("SSL_connect", ssl);
357
+ ssl->state = NETDATA_SSL_STATE_FAILED;
358
+ return false;
359
+ }
360
+
361
+ ssl->state = NETDATA_SSL_STATE_COMPLETE;
362
+ return true;
363
+}
364
+
365
+bool netdata_ssl_accept(NETDATA_SSL *ssl) {
366
+ errno = 0;
367
+ ssl->ssl_errno = 0;
368
+
369
+ if(unlikely(!is_handshake_initialized(ssl, "accept")))
370
+ return false;
371
+
372
+ SSL_set_accept_state(ssl->conn);
373
+
374
+ int err;
375
+ while ((err = SSL_accept(ssl->conn)) != 1) {
376
+ if(!want_read_write_should_retry(ssl, err))
377
+ break;
378
+ }
379
+
380
+ if (err != 1) {
381
+ netdata_ssl_log_error_queue("SSL_accept", ssl);
382
+ ssl->state = NETDATA_SSL_STATE_FAILED;
383
+ return false;
384
+ }
385
+
386
+ ssl->state = NETDATA_SSL_STATE_COMPLETE;
387
+ return true;
388
+}
389
390
/**
391
* Info Callback
396
* @param where the variable with the flags set.
397
* @param ret the return of the caller
398
*/
23
-static void security_info_callback(const SSL *ssl, int where, int ret __maybe_unused) {
399
+static void netdata_ssl_info_callback(const SSL *ssl, int where, int ret __maybe_unused) {
400
(void)ssl;
401
if (where & SSL_CB_ALERT) {
402
debug(D_WEB_CLIENT,"SSL INFO CALLBACK %s %s", SSL_alert_type_string(ret), SSL_alert_desc_string_long(ret));
408
*
409
* Starts the openssl library for the Netdata.
410
*/
35
-void security_openssl_library()
36
-{
411
+void netdata_ssl_initialize_openssl() {
412
+
413
#if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
414
# if (SSLEAY_VERSION_NUMBER >= OPENSSL_VERSION_097)
415
OPENSSL_config(NULL);
418
SSL_load_error_strings();
419
420
SSL_library_init();
421
+
422
#else
423
+
424
if (OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL) != 1) {
425
error("SSL library cannot be initialized.");
426
}
427
+
428
#endif
429
}
430
438
*
439
* @return it returns the version number.
440
*/
62
-int tls_select_version(const char *lversion) {
441
+static int netdata_ssl_select_tls_version(const char *lversion) {
442
if (!strcmp(lversion, "1") || !strcmp(lversion, "1.0"))
443
return TLS1_VERSION;
444
else if (!strcmp(lversion, "1.1"))
458
}
459
#endif
460
82
-/**
83
- * OpenSSL common options
84
- *
85
- * Clients and SERVER have common options, this function is responsible to set them in the context.
86
- *
87
- * @param ctx the initialized SSL context.
88
- * @param side 0 means server, and 1 client.
89
- */
90
-void security_openssl_common_options(SSL_CTX *ctx, int side) {
91
-#if OPENSSL_VERSION_NUMBER >= OPENSSL_VERSION_110
92
- if (!side) {
93
- int version = tls_select_version(tls_version) ;
94
-#endif
95
-#if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
96
- SSL_CTX_set_options (ctx,SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION);
97
-#else
98
- SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
99
- SSL_CTX_set_max_proto_version(ctx, version);
100
-
101
- if(tls_ciphers && strcmp(tls_ciphers, "none") != 0) {
102
- if (!SSL_CTX_set_cipher_list(ctx, tls_ciphers)) {
103
- error("SSL error. cannot set the cipher list");
104
- }
105
- }
106
- }
107
-#endif
108
-
109
- SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
110
-}
111
-
461
/**
462
* Initialize Openssl Client
463
*
465
*
466
* @return It returns the context on success or NULL otherwise
467
*/
119
-SSL_CTX * security_initialize_openssl_client() {
468
+SSL_CTX * netdata_ssl_create_client_ctx(unsigned long mode) {
469
SSL_CTX *ctx;
470
#if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
471
ctx = SSL_CTX_new(SSLv23_client_method());
487
#endif
488
}
489
490
+ if(mode)
491
+ SSL_CTX_set_mode(ctx, mode);
492
+
493
return ctx;
494
}
495
500
*
501
* @return It returns the context on success or NULL otherwise
502
*/
151
-static SSL_CTX * security_initialize_openssl_server() {
503
+static SSL_CTX * netdata_ssl_create_server_ctx(unsigned long mode) {
504
SSL_CTX *ctx;
505
char lerror[512];
506
static int netdata_id_context = 1;
523
524
SSL_CTX_use_certificate_chain_file(ctx, netdata_ssl_security_cert);
525
#endif
174
- security_openssl_common_options(ctx, 0);
526
+
527
+#if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
528
+ SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION);
529
+#else
530
+ SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
531
+ SSL_CTX_set_max_proto_version(ctx, netdata_ssl_select_tls_version(tls_version));
532
+
533
+ if(tls_ciphers && strcmp(tls_ciphers, "none") != 0) {
534
+ if (!SSL_CTX_set_cipher_list(ctx, tls_ciphers)) {
535
+ error("SSL error. cannot set the cipher list");
536
+ }
537
+ }
538
+#endif
539
540
SSL_CTX_use_PrivateKey_file(ctx, netdata_ssl_security_key,SSL_FILETYPE_PEM);
541
547
}
548
549
SSL_CTX_set_session_id_context(ctx,(void*)&netdata_id_context,(unsigned int)sizeof(netdata_id_context));
186
- SSL_CTX_set_info_callback(ctx,security_info_callback);
550
+ SSL_CTX_set_info_callback(ctx, netdata_ssl_info_callback);
551
552
#if (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_095)
553
SSL_CTX_set_verify_depth(ctx,1);
554
#endif
555
debug(D_WEB_CLIENT,"SSL GLOBAL CONTEXT STARTED\n");
556
557
+ SSL_CTX_set_mode(ctx, mode);
558
+
559
return ctx;
560
}
561
569
* NETDATA_SSL_CONTEXT_STREAMING - Starts the streaming context.
570
* NETDATA_SSL_CONTEXT_EXPORTING - Starts the OpenTSDB context
571
*/
206
-void security_start_ssl(int selector) {
572
+void netdata_ssl_initialize_ctx(int selector) {
573
static SPINLOCK sp = NETDATA_SPINLOCK_INITIALIZER;
574
netdata_spinlock_lock(&sp);
575
576
switch (selector) {
211
- case NETDATA_SSL_CONTEXT_SERVER: {
212
- if(!netdata_ssl_srv_ctx) {
577
+ case NETDATA_SSL_WEB_SERVER_CTX: {
578
+ if(!netdata_ssl_web_server_ctx) {
579
struct stat statbuf;
580
if (stat(netdata_ssl_security_key, &statbuf) || stat(netdata_ssl_security_cert, &statbuf))
581
info("To use encryption it is necessary to set \"ssl certificate\" and \"ssl key\" in [web] !\n");
582
else {
217
- netdata_ssl_srv_ctx = security_initialize_openssl_server();
218
- SSL_CTX_set_mode(netdata_ssl_srv_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
583
+ netdata_ssl_web_server_ctx = netdata_ssl_create_server_ctx(
584
+ SSL_MODE_ENABLE_PARTIAL_WRITE |
585
+ SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
586
+ // SSL_MODE_AUTO_RETRY |
587
+ 0);
588
+
589
+ if(netdata_ssl_web_server_ctx && !netdata_ssl_validate_certificate)
590
+ SSL_CTX_set_verify(netdata_ssl_web_server_ctx, SSL_VERIFY_NONE, NULL);
591
}
592
}
593
break;
594
}
595
224
- case NETDATA_SSL_CONTEXT_STREAMING: {
225
- if(!netdata_ssl_client_ctx) {
226
- netdata_ssl_client_ctx = security_initialize_openssl_client();
596
+ case NETDATA_SSL_STREAMING_SENDER_CTX: {
597
+ if(!netdata_ssl_streaming_sender_ctx) {
598
//This is necessary for the stream, because it is working sometimes with nonblock socket.
599
//It returns the bitmask after to change, there is not any description of errors in the documentation
229
- SSL_CTX_set_mode(netdata_ssl_client_ctx,
230
- SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
231
- SSL_MODE_AUTO_RETRY);
600
+ netdata_ssl_streaming_sender_ctx = netdata_ssl_create_client_ctx(
601
+ SSL_MODE_ENABLE_PARTIAL_WRITE |
602
+ SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
603
+ // SSL_MODE_AUTO_RETRY |
604
+ 0
605
+ );
606
+
607
+ if(netdata_ssl_streaming_sender_ctx && !netdata_ssl_validate_certificate_sender)
608
+ SSL_CTX_set_verify(netdata_ssl_streaming_sender_ctx, SSL_VERIFY_NONE, NULL);
609
}
610
break;
611
}
612
236
- case NETDATA_SSL_CONTEXT_EXPORTING: {
237
- if(!netdata_ssl_exporting_ctx)
238
- netdata_ssl_exporting_ctx = security_initialize_openssl_client();
613
+ case NETDATA_SSL_EXPORTING_CTX: {
614
+ if(!netdata_ssl_exporting_ctx) {
615
+ netdata_ssl_exporting_ctx = netdata_ssl_create_client_ctx(0);
616
+
617
+ if(netdata_ssl_exporting_ctx && !netdata_ssl_validate_certificate)
618
+ SSL_CTX_set_verify(netdata_ssl_exporting_ctx, SSL_VERIFY_NONE, NULL);
619
+ }
620
break;
621
}
622
}
629
*
630
* Clean all the allocated contexts from netdata.
631
*/
251
-void security_clean_openssl()
632
+void netdata_ssl_cleanup()
633
{
253
- if (netdata_ssl_srv_ctx) {
254
- SSL_CTX_free(netdata_ssl_srv_ctx);
634
+ if (netdata_ssl_web_server_ctx) {
635
+ SSL_CTX_free(netdata_ssl_web_server_ctx);
636
+ netdata_ssl_web_server_ctx = NULL;
637
}
638
257
- if (netdata_ssl_client_ctx) {
258
- SSL_CTX_free(netdata_ssl_client_ctx);
639
+ if (netdata_ssl_streaming_sender_ctx) {
640
+ SSL_CTX_free(netdata_ssl_streaming_sender_ctx);
641
+ netdata_ssl_streaming_sender_ctx = NULL;
642
}
643
644
if (netdata_ssl_exporting_ctx) {
645
SSL_CTX_free(netdata_ssl_exporting_ctx);
646
+ netdata_ssl_exporting_ctx = NULL;
647
}
648
649
#if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
651
#endif
652
}
653
270
-/**
271
- * Process accept
272
- *
273
- * Process the SSL handshake with the client case it is necessary.
274
- *
275
- * @param ssl is a pointer for the SSL structure
276
- * @param msg is a copy of the first 8 bytes of the initial message received
277
- *
278
- * @return it returns 0 case it performs the handshake, 8 case it is clean connection
279
- * and another integer power of 2 otherwise.
280
- */
281
-int security_process_accept(SSL *ssl,int msg) {
282
- int sock = SSL_get_fd(ssl);
283
- int test;
284
- if (msg > 0x17)
285
- {
286
- return NETDATA_SSL_NO_HANDSHAKE;
287
- }
288
-
289
- ERR_clear_error();
290
- if ((test = SSL_accept(ssl)) <= 0) {
291
- int sslerrno = SSL_get_error(ssl, test);
292
- switch(sslerrno) {
293
- case SSL_ERROR_WANT_READ:
294
- {
295
- error("SSL handshake did not finish and it wanna read on socket %d!", sock);
296
- return NETDATA_SSL_WANT_READ;
297
- }
298
- case SSL_ERROR_WANT_WRITE:
299
- {
300
- error("SSL handshake did not finish and it wanna read on socket %d!", sock);
301
- return NETDATA_SSL_WANT_WRITE;
302
- }
303
- case SSL_ERROR_NONE:
304
- case SSL_ERROR_SSL:
305
- case SSL_ERROR_SYSCALL:
306
- default:
307
- {
308
- u_long err;
309
- char buf[256];
310
- int counter = 0;
311
- while ((err = ERR_get_error()) != 0) {
312
- ERR_error_string_n(err, buf, sizeof(buf));
313
- error("%d SSL Handshake error (%s) on socket %d", counter++, ERR_error_string((long)SSL_get_error(ssl, test), NULL), sock);
314
- }
315
- return NETDATA_SSL_NO_HANDSHAKE;
316
- }
317
- }
318
- }
319
-
320
- if (SSL_is_init_finished(ssl))
321
- {
322
- debug(D_WEB_CLIENT_ACCESS,"SSL Handshake finished %s errno %d on socket fd %d", ERR_error_string((long)SSL_get_error(ssl, test), NULL), errno, sock);
323
- }
324
-
325
- return NETDATA_SSL_HANDSHAKE_COMPLETE;
326
-}
327
-
654
/**
655
* Test Certificate
656
*