security.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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_ZERO_RETURN) {
  189. ssl->ssl_errno = err;
  190. return 0;
  191. }
  192. if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
  193. ssl->ssl_errno = err;
  194. errno = EWOULDBLOCK;
  195. }
  196. else
  197. netdata_ssl_log_error_queue("SSL_read", ssl, err);
  198. bytes = -1; // according to read() or recv()
  199. }
  200. return bytes;
  201. }
  202. /*
  203. * netdata_ssl_write() should return the same as write():
  204. *
  205. * Positive value: The write() function succeeded and wrote some bytes. The exact number of bytes written is returned.
  206. *
  207. * Zero: It's technically possible for write() to return zero, indicating that zero bytes were written. However, for a
  208. * socket, this generally does not happen unless the size of the data to be written is zero.
  209. *
  210. * -1: An error occurred. The specific error can be found by examining the errno variable.
  211. * EAGAIN or EWOULDBLOCK: The file descriptor is in non-blocking mode, and the write operation would block.
  212. * (These are often the same value, but can be different on some systems.)
  213. */
  214. ssize_t netdata_ssl_write(NETDATA_SSL *ssl, const void *buf, size_t num) {
  215. errno = 0;
  216. ssl->ssl_errno = 0;
  217. if(unlikely(!is_handshake_complete(ssl, "write")))
  218. return -1;
  219. int bytes = SSL_write(ssl->conn, (uint8_t *)buf, (int)num);
  220. if(unlikely(bytes <= 0)) {
  221. int err = SSL_get_error(ssl->conn, bytes);
  222. if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
  223. ssl->ssl_errno = err;
  224. errno = EWOULDBLOCK;
  225. }
  226. else
  227. netdata_ssl_log_error_queue("SSL_write", ssl, err);
  228. bytes = -1; // according to write() or send()
  229. }
  230. return bytes;
  231. }
  232. static inline bool is_handshake_initialized(NETDATA_SSL *ssl, const char *op) {
  233. error_limit_static_thread_var(erl, 1, 0);
  234. if(unlikely(!ssl->conn)) {
  235. internal_error(true, "SSL: trying to %s on a NULL connection", op);
  236. return false;
  237. }
  238. switch(ssl->state) {
  239. case NETDATA_SSL_STATE_NOT_SSL: {
  240. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  241. error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on non-SSL connection",
  242. peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
  243. return false;
  244. }
  245. case NETDATA_SSL_STATE_INIT: {
  246. return true;
  247. }
  248. case NETDATA_SSL_STATE_FAILED: {
  249. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  250. error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on a failed connection",
  251. peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
  252. return false;
  253. }
  254. case NETDATA_SSL_STATE_COMPLETE: {
  255. SOCKET_PEERS peers = netdata_ssl_peers(ssl);
  256. error_limit(&erl, "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on an complete connection",
  257. peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op);
  258. return false;
  259. }
  260. }
  261. return false;
  262. }
  263. #define WANT_READ_WRITE_TIMEOUT_MS 10
  264. static inline bool want_read_write_should_retry(NETDATA_SSL *ssl, int err) {
  265. int ssl_errno = SSL_get_error(ssl->conn, err);
  266. if(ssl_errno == SSL_ERROR_WANT_READ || ssl_errno == SSL_ERROR_WANT_WRITE) {
  267. struct pollfd pfds[1] = { [0] = {
  268. .fd = SSL_get_rfd(ssl->conn),
  269. .events = (short)(((ssl_errno == SSL_ERROR_WANT_READ ) ? POLLIN : 0) |
  270. ((ssl_errno == SSL_ERROR_WANT_WRITE) ? POLLOUT : 0)),
  271. }};
  272. if(poll(pfds, 1, WANT_READ_WRITE_TIMEOUT_MS) <= 0)
  273. return false; // timeout (0) or error (<0)
  274. return true; // we have activity, so we should retry
  275. }
  276. return false; // an unknown error
  277. }
  278. bool netdata_ssl_connect(NETDATA_SSL *ssl) {
  279. errno = 0;
  280. ssl->ssl_errno = 0;
  281. if(unlikely(!is_handshake_initialized(ssl, "connect")))
  282. return false;
  283. SSL_set_connect_state(ssl->conn);
  284. int err;
  285. while ((err = SSL_connect(ssl->conn)) != 1) {
  286. if(!want_read_write_should_retry(ssl, err))
  287. break;
  288. }
  289. if (err != 1) {
  290. err = SSL_get_error(ssl->conn, err);
  291. netdata_ssl_log_error_queue("SSL_connect", ssl, err);
  292. ssl->state = NETDATA_SSL_STATE_FAILED;
  293. return false;
  294. }
  295. ssl->state = NETDATA_SSL_STATE_COMPLETE;
  296. return true;
  297. }
  298. bool netdata_ssl_accept(NETDATA_SSL *ssl) {
  299. errno = 0;
  300. ssl->ssl_errno = 0;
  301. if(unlikely(!is_handshake_initialized(ssl, "accept")))
  302. return false;
  303. SSL_set_accept_state(ssl->conn);
  304. int err;
  305. while ((err = SSL_accept(ssl->conn)) != 1) {
  306. if(!want_read_write_should_retry(ssl, err))
  307. break;
  308. }
  309. if (err != 1) {
  310. err = SSL_get_error(ssl->conn, err);
  311. netdata_ssl_log_error_queue("SSL_accept", ssl, err);
  312. ssl->state = NETDATA_SSL_STATE_FAILED;
  313. return false;
  314. }
  315. ssl->state = NETDATA_SSL_STATE_COMPLETE;
  316. return true;
  317. }
  318. /**
  319. * Info Callback
  320. *
  321. * Function used as callback for the OpenSSL Library
  322. *
  323. * @param ssl a pointer to the SSL structure of the client
  324. * @param where the variable with the flags set.
  325. * @param ret the return of the caller
  326. */
  327. static void netdata_ssl_info_callback(const SSL *ssl, int where, int ret __maybe_unused) {
  328. (void)ssl;
  329. if (where & SSL_CB_ALERT) {
  330. netdata_log_debug(D_WEB_CLIENT,"SSL INFO CALLBACK %s %s", SSL_alert_type_string(ret), SSL_alert_desc_string_long(ret));
  331. }
  332. }
  333. /**
  334. * OpenSSL Library
  335. *
  336. * Starts the openssl library for the Netdata.
  337. */
  338. void netdata_ssl_initialize_openssl() {
  339. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  340. # if (SSLEAY_VERSION_NUMBER >= OPENSSL_VERSION_097)
  341. OPENSSL_config(NULL);
  342. # endif
  343. SSL_load_error_strings();
  344. SSL_library_init();
  345. #else
  346. if (OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL) != 1) {
  347. netdata_log_error("SSL library cannot be initialized.");
  348. }
  349. #endif
  350. }
  351. #if OPENSSL_VERSION_NUMBER >= OPENSSL_VERSION_110
  352. /**
  353. * TLS version
  354. *
  355. * Returns the TLS version depending of the user input.
  356. *
  357. * @param lversion is the user input.
  358. *
  359. * @return it returns the version number.
  360. */
  361. static int netdata_ssl_select_tls_version(const char *lversion) {
  362. if (!strcmp(lversion, "1") || !strcmp(lversion, "1.0"))
  363. return TLS1_VERSION;
  364. else if (!strcmp(lversion, "1.1"))
  365. return TLS1_1_VERSION;
  366. else if (!strcmp(lversion, "1.2"))
  367. return TLS1_2_VERSION;
  368. #if defined(TLS1_3_VERSION)
  369. else if (!strcmp(lversion, "1.3"))
  370. return TLS1_3_VERSION;
  371. #endif
  372. #if defined(TLS_MAX_VERSION)
  373. return TLS_MAX_VERSION;
  374. #else
  375. return TLS1_2_VERSION;
  376. #endif
  377. }
  378. #endif
  379. /**
  380. * Initialize Openssl Client
  381. *
  382. * Starts the client context with TLS 1.2.
  383. *
  384. * @return It returns the context on success or NULL otherwise
  385. */
  386. SSL_CTX * netdata_ssl_create_client_ctx(unsigned long mode) {
  387. SSL_CTX *ctx;
  388. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  389. ctx = SSL_CTX_new(SSLv23_client_method());
  390. #else
  391. ctx = SSL_CTX_new(TLS_client_method());
  392. #endif
  393. if(ctx) {
  394. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  395. SSL_CTX_set_options (ctx,SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION);
  396. #else
  397. SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
  398. # if defined(TLS_MAX_VERSION)
  399. SSL_CTX_set_max_proto_version(ctx, TLS_MAX_VERSION);
  400. # elif defined(TLS1_3_VERSION)
  401. SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
  402. # elif defined(TLS1_2_VERSION)
  403. SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION);
  404. # endif
  405. #endif
  406. }
  407. if(mode)
  408. SSL_CTX_set_mode(ctx, mode);
  409. return ctx;
  410. }
  411. /**
  412. * Initialize OpenSSL server
  413. *
  414. * Starts the server context with TLS 1.2 and load the certificate.
  415. *
  416. * @return It returns the context on success or NULL otherwise
  417. */
  418. static SSL_CTX * netdata_ssl_create_server_ctx(unsigned long mode) {
  419. SSL_CTX *ctx;
  420. char lerror[512];
  421. static int netdata_id_context = 1;
  422. //TO DO: Confirm the necessity to check return for other OPENSSL function
  423. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  424. ctx = SSL_CTX_new(SSLv23_server_method());
  425. if (!ctx) {
  426. netdata_log_error("Cannot create a new SSL context, netdata won't encrypt communication");
  427. return NULL;
  428. }
  429. SSL_CTX_use_certificate_file(ctx, netdata_ssl_security_cert, SSL_FILETYPE_PEM);
  430. #else
  431. ctx = SSL_CTX_new(TLS_server_method());
  432. if (!ctx) {
  433. netdata_log_error("Cannot create a new SSL context, netdata won't encrypt communication");
  434. return NULL;
  435. }
  436. SSL_CTX_use_certificate_chain_file(ctx, netdata_ssl_security_cert);
  437. #endif
  438. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  439. SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION);
  440. #else
  441. SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
  442. SSL_CTX_set_max_proto_version(ctx, netdata_ssl_select_tls_version(tls_version));
  443. if(tls_ciphers && strcmp(tls_ciphers, "none") != 0) {
  444. if (!SSL_CTX_set_cipher_list(ctx, tls_ciphers)) {
  445. netdata_log_error("SSL error. cannot set the cipher list");
  446. }
  447. }
  448. #endif
  449. SSL_CTX_use_PrivateKey_file(ctx, netdata_ssl_security_key,SSL_FILETYPE_PEM);
  450. if (!SSL_CTX_check_private_key(ctx)) {
  451. ERR_error_string_n(ERR_get_error(),lerror,sizeof(lerror));
  452. netdata_log_error("SSL cannot check the private key: %s",lerror);
  453. SSL_CTX_free(ctx);
  454. return NULL;
  455. }
  456. SSL_CTX_set_session_id_context(ctx,(void*)&netdata_id_context,(unsigned int)sizeof(netdata_id_context));
  457. SSL_CTX_set_info_callback(ctx, netdata_ssl_info_callback);
  458. #if (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_095)
  459. SSL_CTX_set_verify_depth(ctx,1);
  460. #endif
  461. netdata_log_debug(D_WEB_CLIENT,"SSL GLOBAL CONTEXT STARTED\n");
  462. SSL_CTX_set_mode(ctx, mode);
  463. return ctx;
  464. }
  465. /**
  466. * Start SSL
  467. *
  468. * Call the correct function to start the SSL context.
  469. *
  470. * @param selector informs the context that must be initialized, the following list has the valid values:
  471. * NETDATA_SSL_CONTEXT_SERVER - the server context
  472. * NETDATA_SSL_CONTEXT_STREAMING - Starts the streaming context.
  473. * NETDATA_SSL_CONTEXT_EXPORTING - Starts the OpenTSDB context
  474. */
  475. void netdata_ssl_initialize_ctx(int selector) {
  476. static SPINLOCK sp = NETDATA_SPINLOCK_INITIALIZER;
  477. spinlock_lock(&sp);
  478. switch (selector) {
  479. case NETDATA_SSL_WEB_SERVER_CTX: {
  480. if(!netdata_ssl_web_server_ctx) {
  481. struct stat statbuf;
  482. if (stat(netdata_ssl_security_key, &statbuf) || stat(netdata_ssl_security_cert, &statbuf))
  483. netdata_log_info("To use encryption it is necessary to set \"ssl certificate\" and \"ssl key\" in [web] !\n");
  484. else {
  485. netdata_ssl_web_server_ctx = netdata_ssl_create_server_ctx(
  486. SSL_MODE_ENABLE_PARTIAL_WRITE |
  487. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
  488. // SSL_MODE_AUTO_RETRY |
  489. 0);
  490. if(netdata_ssl_web_server_ctx && !netdata_ssl_validate_certificate)
  491. SSL_CTX_set_verify(netdata_ssl_web_server_ctx, SSL_VERIFY_NONE, NULL);
  492. }
  493. }
  494. break;
  495. }
  496. case NETDATA_SSL_STREAMING_SENDER_CTX: {
  497. if(!netdata_ssl_streaming_sender_ctx) {
  498. //This is necessary for the stream, because it is working sometimes with nonblock socket.
  499. //It returns the bitmask after to change, there is not any description of errors in the documentation
  500. netdata_ssl_streaming_sender_ctx = netdata_ssl_create_client_ctx(
  501. SSL_MODE_ENABLE_PARTIAL_WRITE |
  502. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
  503. // SSL_MODE_AUTO_RETRY |
  504. 0
  505. );
  506. if(netdata_ssl_streaming_sender_ctx && !netdata_ssl_validate_certificate_sender)
  507. SSL_CTX_set_verify(netdata_ssl_streaming_sender_ctx, SSL_VERIFY_NONE, NULL);
  508. }
  509. break;
  510. }
  511. case NETDATA_SSL_EXPORTING_CTX: {
  512. if(!netdata_ssl_exporting_ctx) {
  513. netdata_ssl_exporting_ctx = netdata_ssl_create_client_ctx(0);
  514. if(netdata_ssl_exporting_ctx && !netdata_ssl_validate_certificate)
  515. SSL_CTX_set_verify(netdata_ssl_exporting_ctx, SSL_VERIFY_NONE, NULL);
  516. }
  517. break;
  518. }
  519. }
  520. spinlock_unlock(&sp);
  521. }
  522. /**
  523. * Clean Open SSL
  524. *
  525. * Clean all the allocated contexts from netdata.
  526. */
  527. void netdata_ssl_cleanup()
  528. {
  529. if (netdata_ssl_web_server_ctx) {
  530. SSL_CTX_free(netdata_ssl_web_server_ctx);
  531. netdata_ssl_web_server_ctx = NULL;
  532. }
  533. if (netdata_ssl_streaming_sender_ctx) {
  534. SSL_CTX_free(netdata_ssl_streaming_sender_ctx);
  535. netdata_ssl_streaming_sender_ctx = NULL;
  536. }
  537. if (netdata_ssl_exporting_ctx) {
  538. SSL_CTX_free(netdata_ssl_exporting_ctx);
  539. netdata_ssl_exporting_ctx = NULL;
  540. }
  541. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  542. ERR_free_strings();
  543. #endif
  544. }
  545. /**
  546. * Test Certificate
  547. *
  548. * Check the certificate of Netdata parent
  549. *
  550. * @param ssl is the connection structure
  551. *
  552. * @return It returns 0 on success and -1 otherwise
  553. */
  554. int security_test_certificate(SSL *ssl) {
  555. X509* cert = SSL_get_peer_certificate(ssl);
  556. int ret;
  557. long status;
  558. if (!cert) {
  559. return -1;
  560. }
  561. status = SSL_get_verify_result(ssl);
  562. if((X509_V_OK != status))
  563. {
  564. char error[512];
  565. ERR_error_string_n(ERR_get_error(), error, sizeof(error));
  566. netdata_log_error("SSL RFC4158 check: We have a invalid certificate, the tests result with %ld and message %s", status, error);
  567. ret = -1;
  568. } else {
  569. ret = 0;
  570. }
  571. return ret;
  572. }
  573. /**
  574. * Location for context
  575. *
  576. * Case the user give us a directory with the certificates available and
  577. * the Netdata parent certificate, we use this function to validate the certificate.
  578. *
  579. * @param ctx the context where the path will be set.
  580. * @param file the file with Netdata parent certificate.
  581. * @param path the directory where the certificates are stored.
  582. *
  583. * @return It returns 0 on success and -1 otherwise.
  584. */
  585. int ssl_security_location_for_context(SSL_CTX *ctx, char *file, char *path) {
  586. int load_custom = 1, load_default = 1;
  587. if (file || path) {
  588. if(!SSL_CTX_load_verify_locations(ctx, file, path)) {
  589. netdata_log_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!");
  590. load_custom = 0;
  591. }
  592. }
  593. if(!SSL_CTX_set_default_verify_paths(ctx)) {
  594. netdata_log_info("Can not verify default OpenSSL configuration to validate certificates!");
  595. load_default = 0;
  596. }
  597. if (load_custom == 0 && load_default == 0)
  598. return -1;
  599. return 0;
  600. }
  601. #endif