security.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #include "../libnetdata.h"
  2. #ifdef ENABLE_HTTPS
  3. SSL_CTX *netdata_ssl_exporting_ctx =NULL;
  4. SSL_CTX *netdata_ssl_streaming_sender_ctx =NULL;
  5. SSL_CTX *netdata_ssl_web_server_ctx =NULL;
  6. const char *netdata_ssl_security_key =NULL;
  7. const char *netdata_ssl_security_cert =NULL;
  8. const char *tls_version=NULL;
  9. const char *tls_ciphers=NULL;
  10. bool netdata_ssl_validate_certificate = true;
  11. bool netdata_ssl_validate_certificate_sender = true;
  12. static SOCKET_PEERS netdata_ssl_peers(NETDATA_SSL *ssl) {
  13. int sock_fd;
  14. if(unlikely(!ssl->conn))
  15. sock_fd = -1;
  16. else
  17. sock_fd = SSL_get_rfd(ssl->conn);
  18. return socket_peers(sock_fd);
  19. }
  20. static void netdata_ssl_log_error_queue(const char *call, NETDATA_SSL *ssl, unsigned long err) {
  21. error_limit_static_thread_var(erl, 1, 0);
  22. if(err == SSL_ERROR_NONE)
  23. err = ERR_get_error();
  24. if(err == SSL_ERROR_NONE)
  25. return;
  26. do {
  27. char *code;
  28. switch (err) {
  29. case SSL_ERROR_SSL:
  30. code = "SSL_ERROR_SSL";
  31. ssl->state = NETDATA_SSL_STATE_FAILED;
  32. break;
  33. case SSL_ERROR_WANT_READ:
  34. code = "SSL_ERROR_WANT_READ";
  35. break;
  36. case SSL_ERROR_WANT_WRITE:
  37. code = "SSL_ERROR_WANT_WRITE";
  38. break;
  39. case SSL_ERROR_WANT_X509_LOOKUP:
  40. code = "SSL_ERROR_WANT_X509_LOOKUP";
  41. break;
  42. case SSL_ERROR_SYSCALL:
  43. code = "SSL_ERROR_SYSCALL";
  44. ssl->state = NETDATA_SSL_STATE_FAILED;
  45. break;
  46. case SSL_ERROR_ZERO_RETURN:
  47. code = "SSL_ERROR_ZERO_RETURN";
  48. break;
  49. case SSL_ERROR_WANT_CONNECT:
  50. code = "SSL_ERROR_WANT_CONNECT";
  51. break;
  52. case SSL_ERROR_WANT_ACCEPT:
  53. code = "SSL_ERROR_WANT_ACCEPT";
  54. break;
  55. #ifdef SSL_ERROR_WANT_ASYNC
  56. case SSL_ERROR_WANT_ASYNC:
  57. code = "SSL_ERROR_WANT_ASYNC";
  58. break;
  59. #endif
  60. #ifdef SSL_ERROR_WANT_ASYNC_JOB
  61. case SSL_ERROR_WANT_ASYNC_JOB:
  62. code = "SSL_ERROR_WANT_ASYNC_JOB";
  63. break;
  64. #endif
  65. #ifdef SSL_ERROR_WANT_CLIENT_HELLO_CB
  66. case SSL_ERROR_WANT_CLIENT_HELLO_CB:
  67. code = "SSL_ERROR_WANT_CLIENT_HELLO_CB";
  68. break;
  69. #endif
  70. #ifdef SSL_ERROR_WANT_RETRY_VERIFY
  71. case SSL_ERROR_WANT_RETRY_VERIFY:
  72. code = "SSL_ERROR_WANT_RETRY_VERIFY";
  73. break;
  74. #endif
  75. default:
  76. code = "SSL_ERROR_UNKNOWN";
  77. break;
  78. }
  79. char str[1024 + 1];
  80. ERR_error_string_n(err, str, 1024);
  81. str[1024] = '\0';
  82. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  83. error_limit(&erl, "SSL: %s() on socket local [[%s]:%d] <-> remote [[%s]:%d], returned error %lu (%s): %s",
  84. call, peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, err, code, str);
  85. } while((err = ERR_get_error()));
  86. }
  87. bool netdata_ssl_open(NETDATA_SSL *ssl, SSL_CTX *ctx, int fd) {
  88. errno = 0;
  89. ssl->ssl_errno = 0;
  90. if(ssl->conn) {
  91. if(!ctx || SSL_get_SSL_CTX(ssl->conn) != ctx) {
  92. SSL_free(ssl->conn);
  93. ssl->conn = NULL;
  94. }
  95. else if (SSL_clear(ssl->conn) == 0) {
  96. netdata_ssl_log_error_queue("SSL_clear", ssl, SSL_ERROR_NONE);
  97. SSL_free(ssl->conn);
  98. ssl->conn = NULL;
  99. }
  100. }
  101. if(!ssl->conn) {
  102. if(!ctx) {
  103. internal_error(true, "SSL: not CTX given");
  104. ssl->state = NETDATA_SSL_STATE_FAILED;
  105. return false;
  106. }
  107. ssl->conn = SSL_new(ctx);
  108. if (!ssl->conn) {
  109. netdata_ssl_log_error_queue("SSL_new", ssl, SSL_ERROR_NONE);
  110. ssl->state = NETDATA_SSL_STATE_FAILED;
  111. return false;
  112. }
  113. }
  114. if(SSL_set_fd(ssl->conn, fd) != 1) {
  115. netdata_ssl_log_error_queue("SSL_set_fd", ssl, SSL_ERROR_NONE);
  116. ssl->state = NETDATA_SSL_STATE_FAILED;
  117. return false;
  118. }
  119. ssl->state = NETDATA_SSL_STATE_INIT;
  120. ERR_clear_error();
  121. return true;
  122. }
  123. void netdata_ssl_close(NETDATA_SSL *ssl) {
  124. errno = 0;
  125. ssl->ssl_errno = 0;
  126. if(ssl->conn) {
  127. if(SSL_connection(ssl)) {
  128. int ret = SSL_shutdown(ssl->conn);
  129. if(ret == 0)
  130. SSL_shutdown(ssl->conn);
  131. }
  132. SSL_free(ssl->conn);
  133. ERR_clear_error();
  134. }
  135. *ssl = NETDATA_SSL_UNSET_CONNECTION;
  136. }
  137. static inline bool is_handshake_complete(NETDATA_SSL *ssl, const char *op) {
  138. error_limit_static_thread_var(erl, 1, 0);
  139. if(unlikely(!ssl->conn)) {
  140. internal_error(true, "SSL: trying to %s on a NULL connection", op);
  141. return false;
  142. }
  143. switch(ssl->state) {
  144. case NETDATA_SSL_STATE_NOT_SSL: {
  145. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  146. error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on non-SSL connection",
  147. peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
  148. return false;
  149. }
  150. case NETDATA_SSL_STATE_INIT: {
  151. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  152. error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on an incomplete connection",
  153. peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
  154. return false;
  155. }
  156. case NETDATA_SSL_STATE_FAILED: {
  157. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  158. error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on a failed connection",
  159. peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
  160. return false;
  161. }
  162. case NETDATA_SSL_STATE_COMPLETE: {
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. /*
  169. * netdata_ssl_read() should return the same as read():
  170. *
  171. * Positive value: The read() function succeeded and read some bytes. The exact number of bytes read is returned.
  172. *
  173. * Zero: For files and sockets, a return value of zero signifies end-of-file (EOF), meaning no more data is available
  174. * for reading. For sockets, this usually means the other side has closed the connection.
  175. *
  176. * -1: An error occurred. The specific error can be found by examining the errno variable.
  177. * EAGAIN or EWOULDBLOCK: The file descriptor is in non-blocking mode, and the read operation would block.
  178. * (These are often the same value, but can be different on some systems.)
  179. */
  180. ssize_t netdata_ssl_read(NETDATA_SSL *ssl, void *buf, size_t num) {
  181. errno = 0;
  182. ssl->ssl_errno = 0;
  183. if(unlikely(!is_handshake_complete(ssl, "read")))
  184. return -1;
  185. int bytes = SSL_read(ssl->conn, buf, (int)num);
  186. if(unlikely(bytes <= 0)) {
  187. int err = SSL_get_error(ssl->conn, bytes);
  188. if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
  189. ssl->ssl_errno = err;
  190. errno = EWOULDBLOCK;
  191. }
  192. else
  193. netdata_ssl_log_error_queue("SSL_read", ssl, err);
  194. bytes = -1; // according to read() or recv()
  195. }
  196. return bytes;
  197. }
  198. /*
  199. * netdata_ssl_write() should return the same as write():
  200. *
  201. * Positive value: The write() function succeeded and wrote some bytes. The exact number of bytes written is returned.
  202. *
  203. * Zero: It's technically possible for write() to return zero, indicating that zero bytes were written. However, for a
  204. * socket, this generally does not happen unless the size of the data to be written is zero.
  205. *
  206. * -1: An error occurred. The specific error can be found by examining the errno variable.
  207. * EAGAIN or EWOULDBLOCK: The file descriptor is in non-blocking mode, and the write operation would block.
  208. * (These are often the same value, but can be different on some systems.)
  209. */
  210. ssize_t netdata_ssl_write(NETDATA_SSL *ssl, const void *buf, size_t num) {
  211. errno = 0;
  212. ssl->ssl_errno = 0;
  213. if(unlikely(!is_handshake_complete(ssl, "write")))
  214. return -1;
  215. int bytes = SSL_write(ssl->conn, (uint8_t *)buf, (int)num);
  216. if(unlikely(bytes <= 0)) {
  217. int err = SSL_get_error(ssl->conn, bytes);
  218. if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
  219. ssl->ssl_errno = err;
  220. errno = EWOULDBLOCK;
  221. }
  222. else
  223. netdata_ssl_log_error_queue("SSL_write", ssl, err);
  224. bytes = -1; // according to write() or send()
  225. }
  226. return bytes;
  227. }
  228. static inline bool is_handshake_initialized(NETDATA_SSL *ssl, const char *op) {
  229. error_limit_static_thread_var(erl, 1, 0);
  230. if(unlikely(!ssl->conn)) {
  231. internal_error(true, "SSL: trying to %s on a NULL connection", op);
  232. return false;
  233. }
  234. switch(ssl->state) {
  235. case NETDATA_SSL_STATE_NOT_SSL: {
  236. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  237. error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on non-SSL connection",
  238. peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
  239. return false;
  240. }
  241. case NETDATA_SSL_STATE_INIT: {
  242. return true;
  243. }
  244. case NETDATA_SSL_STATE_FAILED: {
  245. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  246. error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on a failed connection",
  247. peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
  248. return false;
  249. }
  250. case NETDATA_SSL_STATE_COMPLETE: {
  251. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  252. error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on an complete connection",
  253. peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
  254. return false;
  255. }
  256. }
  257. return false;
  258. }
  259. #define WANT_READ_WRITE_TIMEOUT_MS 10
  260. static inline bool want_read_write_should_retry(NETDATA_SSL *ssl, int err) {
  261. int ssl_errno = SSL_get_error(ssl->conn, err);
  262. if(ssl_errno == SSL_ERROR_WANT_READ || ssl_errno == SSL_ERROR_WANT_WRITE) {
  263. struct pollfd pfds[1] = { [0] = {
  264. .fd = SSL_get_rfd(ssl->conn),
  265. .events = (short)(((ssl_errno == SSL_ERROR_WANT_READ ) ? POLLIN : 0) |
  266. ((ssl_errno == SSL_ERROR_WANT_WRITE) ? POLLOUT : 0)),
  267. }};
  268. if(poll(pfds, 1, WANT_READ_WRITE_TIMEOUT_MS) <= 0)
  269. return false; // timeout (0) or error (<0)
  270. return true; // we have activity, so we should retry
  271. }
  272. return false; // an unknown error
  273. }
  274. bool netdata_ssl_connect(NETDATA_SSL *ssl) {
  275. errno = 0;
  276. ssl->ssl_errno = 0;
  277. if(unlikely(!is_handshake_initialized(ssl, "connect")))
  278. return false;
  279. SSL_set_connect_state(ssl->conn);
  280. int err;
  281. while ((err = SSL_connect(ssl->conn)) != 1) {
  282. if(!want_read_write_should_retry(ssl, err))
  283. break;
  284. }
  285. if (err != 1) {
  286. err = SSL_get_error(ssl->conn, err);
  287. netdata_ssl_log_error_queue("SSL_connect", ssl, err);
  288. ssl->state = NETDATA_SSL_STATE_FAILED;
  289. return false;
  290. }
  291. ssl->state = NETDATA_SSL_STATE_COMPLETE;
  292. return true;
  293. }
  294. bool netdata_ssl_accept(NETDATA_SSL *ssl) {
  295. errno = 0;
  296. ssl->ssl_errno = 0;
  297. if(unlikely(!is_handshake_initialized(ssl, "accept")))
  298. return false;
  299. SSL_set_accept_state(ssl->conn);
  300. int err;
  301. while ((err = SSL_accept(ssl->conn)) != 1) {
  302. if(!want_read_write_should_retry(ssl, err))
  303. break;
  304. }
  305. if (err != 1) {
  306. err = SSL_get_error(ssl->conn, err);
  307. netdata_ssl_log_error_queue("SSL_accept", ssl, err);
  308. ssl->state = NETDATA_SSL_STATE_FAILED;
  309. return false;
  310. }
  311. ssl->state = NETDATA_SSL_STATE_COMPLETE;
  312. return true;
  313. }
  314. /**
  315. * Info Callback
  316. *
  317. * Function used as callback for the OpenSSL Library
  318. *
  319. * @param ssl a pointer to the SSL structure of the client
  320. * @param where the variable with the flags set.
  321. * @param ret the return of the caller
  322. */
  323. static void netdata_ssl_info_callback(const SSL *ssl, int where, int ret __maybe_unused) {
  324. (void)ssl;
  325. if (where & SSL_CB_ALERT) {
  326. debug(D_WEB_CLIENT,"SSL INFO CALLBACK %s %s", SSL_alert_type_string(ret), SSL_alert_desc_string_long(ret));
  327. }
  328. }
  329. /**
  330. * OpenSSL Library
  331. *
  332. * Starts the openssl library for the Netdata.
  333. */
  334. void netdata_ssl_initialize_openssl() {
  335. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  336. # if (SSLEAY_VERSION_NUMBER >= OPENSSL_VERSION_097)
  337. OPENSSL_config(NULL);
  338. # endif
  339. SSL_load_error_strings();
  340. SSL_library_init();
  341. #else
  342. if (OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL) != 1) {
  343. error("SSL library cannot be initialized.");
  344. }
  345. #endif
  346. }
  347. #if OPENSSL_VERSION_NUMBER >= OPENSSL_VERSION_110
  348. /**
  349. * TLS version
  350. *
  351. * Returns the TLS version depending of the user input.
  352. *
  353. * @param lversion is the user input.
  354. *
  355. * @return it returns the version number.
  356. */
  357. static int netdata_ssl_select_tls_version(const char *lversion) {
  358. if (!strcmp(lversion, "1") || !strcmp(lversion, "1.0"))
  359. return TLS1_VERSION;
  360. else if (!strcmp(lversion, "1.1"))
  361. return TLS1_1_VERSION;
  362. else if (!strcmp(lversion, "1.2"))
  363. return TLS1_2_VERSION;
  364. #if defined(TLS1_3_VERSION)
  365. else if (!strcmp(lversion, "1.3"))
  366. return TLS1_3_VERSION;
  367. #endif
  368. #if defined(TLS_MAX_VERSION)
  369. return TLS_MAX_VERSION;
  370. #else
  371. return TLS1_2_VERSION;
  372. #endif
  373. }
  374. #endif
  375. /**
  376. * Initialize Openssl Client
  377. *
  378. * Starts the client context with TLS 1.2.
  379. *
  380. * @return It returns the context on success or NULL otherwise
  381. */
  382. SSL_CTX * netdata_ssl_create_client_ctx(unsigned long mode) {
  383. SSL_CTX *ctx;
  384. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  385. ctx = SSL_CTX_new(SSLv23_client_method());
  386. #else
  387. ctx = SSL_CTX_new(TLS_client_method());
  388. #endif
  389. if(ctx) {
  390. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  391. SSL_CTX_set_options (ctx,SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION);
  392. #else
  393. SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
  394. # if defined(TLS_MAX_VERSION)
  395. SSL_CTX_set_max_proto_version(ctx, TLS_MAX_VERSION);
  396. # elif defined(TLS1_3_VERSION)
  397. SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
  398. # elif defined(TLS1_2_VERSION)
  399. SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION);
  400. # endif
  401. #endif
  402. }
  403. if(mode)
  404. SSL_CTX_set_mode(ctx, mode);
  405. return ctx;
  406. }
  407. /**
  408. * Initialize OpenSSL server
  409. *
  410. * Starts the server context with TLS 1.2 and load the certificate.
  411. *
  412. * @return It returns the context on success or NULL otherwise
  413. */
  414. static SSL_CTX * netdata_ssl_create_server_ctx(unsigned long mode) {
  415. SSL_CTX *ctx;
  416. char lerror[512];
  417. static int netdata_id_context = 1;
  418. //TO DO: Confirm the necessity to check return for other OPENSSL function
  419. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  420. ctx = SSL_CTX_new(SSLv23_server_method());
  421. if (!ctx) {
  422. error("Cannot create a new SSL context, netdata won't encrypt communication");
  423. return NULL;
  424. }
  425. SSL_CTX_use_certificate_file(ctx, netdata_ssl_security_cert, SSL_FILETYPE_PEM);
  426. #else
  427. ctx = SSL_CTX_new(TLS_server_method());
  428. if (!ctx) {
  429. error("Cannot create a new SSL context, netdata won't encrypt communication");
  430. return NULL;
  431. }
  432. SSL_CTX_use_certificate_chain_file(ctx, netdata_ssl_security_cert);
  433. #endif
  434. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  435. SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION);
  436. #else
  437. SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
  438. SSL_CTX_set_max_proto_version(ctx, netdata_ssl_select_tls_version(tls_version));
  439. if(tls_ciphers && strcmp(tls_ciphers, "none") != 0) {
  440. if (!SSL_CTX_set_cipher_list(ctx, tls_ciphers)) {
  441. error("SSL error. cannot set the cipher list");
  442. }
  443. }
  444. #endif
  445. SSL_CTX_use_PrivateKey_file(ctx, netdata_ssl_security_key,SSL_FILETYPE_PEM);
  446. if (!SSL_CTX_check_private_key(ctx)) {
  447. ERR_error_string_n(ERR_get_error(),lerror,sizeof(lerror));
  448. error("SSL cannot check the private key: %s",lerror);
  449. SSL_CTX_free(ctx);
  450. return NULL;
  451. }
  452. SSL_CTX_set_session_id_context(ctx,(void*)&netdata_id_context,(unsigned int)sizeof(netdata_id_context));
  453. SSL_CTX_set_info_callback(ctx, netdata_ssl_info_callback);
  454. #if (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_095)
  455. SSL_CTX_set_verify_depth(ctx,1);
  456. #endif
  457. debug(D_WEB_CLIENT,"SSL GLOBAL CONTEXT STARTED\n");
  458. SSL_CTX_set_mode(ctx, mode);
  459. return ctx;
  460. }
  461. /**
  462. * Start SSL
  463. *
  464. * Call the correct function to start the SSL context.
  465. *
  466. * @param selector informs the context that must be initialized, the following list has the valid values:
  467. * NETDATA_SSL_CONTEXT_SERVER - the server context
  468. * NETDATA_SSL_CONTEXT_STREAMING - Starts the streaming context.
  469. * NETDATA_SSL_CONTEXT_EXPORTING - Starts the OpenTSDB context
  470. */
  471. void netdata_ssl_initialize_ctx(int selector) {
  472. static SPINLOCK sp = NETDATA_SPINLOCK_INITIALIZER;
  473. netdata_spinlock_lock(&sp);
  474. switch (selector) {
  475. case NETDATA_SSL_WEB_SERVER_CTX: {
  476. if(!netdata_ssl_web_server_ctx) {
  477. struct stat statbuf;
  478. if (stat(netdata_ssl_security_key, &statbuf) || stat(netdata_ssl_security_cert, &statbuf))
  479. info("To use encryption it is necessary to set \"ssl certificate\" and \"ssl key\" in [web] !\n");
  480. else {
  481. netdata_ssl_web_server_ctx = netdata_ssl_create_server_ctx(
  482. SSL_MODE_ENABLE_PARTIAL_WRITE |
  483. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
  484. // SSL_MODE_AUTO_RETRY |
  485. 0);
  486. if(netdata_ssl_web_server_ctx && !netdata_ssl_validate_certificate)
  487. SSL_CTX_set_verify(netdata_ssl_web_server_ctx, SSL_VERIFY_NONE, NULL);
  488. }
  489. }
  490. break;
  491. }
  492. case NETDATA_SSL_STREAMING_SENDER_CTX: {
  493. if(!netdata_ssl_streaming_sender_ctx) {
  494. //This is necessary for the stream, because it is working sometimes with nonblock socket.
  495. //It returns the bitmask after to change, there is not any description of errors in the documentation
  496. netdata_ssl_streaming_sender_ctx = netdata_ssl_create_client_ctx(
  497. SSL_MODE_ENABLE_PARTIAL_WRITE |
  498. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
  499. // SSL_MODE_AUTO_RETRY |
  500. 0
  501. );
  502. if(netdata_ssl_streaming_sender_ctx && !netdata_ssl_validate_certificate_sender)
  503. SSL_CTX_set_verify(netdata_ssl_streaming_sender_ctx, SSL_VERIFY_NONE, NULL);
  504. }
  505. break;
  506. }
  507. case NETDATA_SSL_EXPORTING_CTX: {
  508. if(!netdata_ssl_exporting_ctx) {
  509. netdata_ssl_exporting_ctx = netdata_ssl_create_client_ctx(0);
  510. if(netdata_ssl_exporting_ctx && !netdata_ssl_validate_certificate)
  511. SSL_CTX_set_verify(netdata_ssl_exporting_ctx, SSL_VERIFY_NONE, NULL);
  512. }
  513. break;
  514. }
  515. }
  516. netdata_spinlock_unlock(&sp);
  517. }
  518. /**
  519. * Clean Open SSL
  520. *
  521. * Clean all the allocated contexts from netdata.
  522. */
  523. void netdata_ssl_cleanup()
  524. {
  525. if (netdata_ssl_web_server_ctx) {
  526. SSL_CTX_free(netdata_ssl_web_server_ctx);
  527. netdata_ssl_web_server_ctx = NULL;
  528. }
  529. if (netdata_ssl_streaming_sender_ctx) {
  530. SSL_CTX_free(netdata_ssl_streaming_sender_ctx);
  531. netdata_ssl_streaming_sender_ctx = NULL;
  532. }
  533. if (netdata_ssl_exporting_ctx) {
  534. SSL_CTX_free(netdata_ssl_exporting_ctx);
  535. netdata_ssl_exporting_ctx = NULL;
  536. }
  537. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  538. ERR_free_strings();
  539. #endif
  540. }
  541. /**
  542. * Test Certificate
  543. *
  544. * Check the certificate of Netdata parent
  545. *
  546. * @param ssl is the connection structure
  547. *
  548. * @return It returns 0 on success and -1 otherwise
  549. */
  550. int security_test_certificate(SSL *ssl) {
  551. X509* cert = SSL_get_peer_certificate(ssl);
  552. int ret;
  553. long status;
  554. if (!cert) {
  555. return -1;
  556. }
  557. status = SSL_get_verify_result(ssl);
  558. if((X509_V_OK != status))
  559. {
  560. char error[512];
  561. ERR_error_string_n(ERR_get_error(), error, sizeof(error));
  562. error("SSL RFC4158 check: We have a invalid certificate, the tests result with %ld and message %s", status, error);
  563. ret = -1;
  564. } else {
  565. ret = 0;
  566. }
  567. return ret;
  568. }
  569. /**
  570. * Location for context
  571. *
  572. * Case the user give us a directory with the certificates available and
  573. * the Netdata parent certificate, we use this function to validate the certificate.
  574. *
  575. * @param ctx the context where the path will be set.
  576. * @param file the file with Netdata parent certificate.
  577. * @param path the directory where the certificates are stored.
  578. *
  579. * @return It returns 0 on success and -1 otherwise.
  580. */
  581. int ssl_security_location_for_context(SSL_CTX *ctx, char *file, char *path) {
  582. int load_custom = 1, load_default = 1;
  583. if (file || path) {
  584. if(!SSL_CTX_load_verify_locations(ctx, file, path)) {
  585. info("Netdata can not verify custom CAfile or CApath for parent's SSL certificate, so it will use the default OpenSSL configuration to validate certificates!");
  586. load_custom = 0;
  587. }
  588. }
  589. if(!SSL_CTX_set_default_verify_paths(ctx)) {
  590. info("Can not verify default OpenSSL configuration to validate certificates!");
  591. load_default = 0;
  592. }
  593. if (load_custom == 0 && load_default == 0)
  594. return -1;
  595. return 0;
  596. }
  597. #endif