security.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include "../libnetdata.h"
  2. #ifdef ENABLE_HTTPS
  3. SSL_CTX *netdata_exporting_ctx=NULL;
  4. SSL_CTX *netdata_client_ctx=NULL;
  5. SSL_CTX *netdata_srv_ctx=NULL;
  6. const char *security_key=NULL;
  7. const char *security_cert=NULL;
  8. const char *tls_version=NULL;
  9. const char *tls_ciphers=NULL;
  10. int netdata_validate_server = NETDATA_SSL_VALID_CERTIFICATE;
  11. /**
  12. * Info Callback
  13. *
  14. * Function used as callback for the OpenSSL Library
  15. *
  16. * @param ssl a pointer to the SSL structure of the client
  17. * @param where the variable with the flags set.
  18. * @param ret the return of the caller
  19. */
  20. static void security_info_callback(const SSL *ssl, int where, int ret __maybe_unused) {
  21. (void)ssl;
  22. if (where & SSL_CB_ALERT) {
  23. debug(D_WEB_CLIENT,"SSL INFO CALLBACK %s %s", SSL_alert_type_string(ret), SSL_alert_desc_string_long(ret));
  24. }
  25. }
  26. /**
  27. * OpenSSL Library
  28. *
  29. * Starts the openssl library for the Netdata.
  30. */
  31. void security_openssl_library()
  32. {
  33. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  34. # if (SSLEAY_VERSION_NUMBER >= OPENSSL_VERSION_097)
  35. OPENSSL_config(NULL);
  36. # endif
  37. SSL_load_error_strings();
  38. SSL_library_init();
  39. #else
  40. if (OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL) != 1) {
  41. error("SSL library cannot be initialized.");
  42. }
  43. #endif
  44. }
  45. #if OPENSSL_VERSION_NUMBER >= OPENSSL_VERSION_110
  46. /**
  47. * TLS version
  48. *
  49. * Returns the TLS version depending of the user input.
  50. *
  51. * @param lversion is the user input.
  52. *
  53. * @return it returns the version number.
  54. */
  55. int tls_select_version(const char *lversion) {
  56. if (!strcmp(lversion, "1") || !strcmp(lversion, "1.0"))
  57. return TLS1_VERSION;
  58. else if (!strcmp(lversion, "1.1"))
  59. return TLS1_1_VERSION;
  60. else if (!strcmp(lversion, "1.2"))
  61. return TLS1_2_VERSION;
  62. #if defined(TLS1_3_VERSION)
  63. else if (!strcmp(lversion, "1.3"))
  64. return TLS1_3_VERSION;
  65. #endif
  66. #if defined(TLS_MAX_VERSION)
  67. return TLS_MAX_VERSION;
  68. #else
  69. return TLS1_2_VERSION;
  70. #endif
  71. }
  72. #endif
  73. /**
  74. * OpenSSL common options
  75. *
  76. * Clients and SERVER have common options, this function is responsible to set them in the context.
  77. *
  78. * @param ctx the initialized SSL context.
  79. * @param side 0 means server, and 1 client.
  80. */
  81. void security_openssl_common_options(SSL_CTX *ctx, int side) {
  82. #if OPENSSL_VERSION_NUMBER >= OPENSSL_VERSION_110
  83. if (!side) {
  84. int version = tls_select_version(tls_version) ;
  85. #endif
  86. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  87. SSL_CTX_set_options (ctx,SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION);
  88. #else
  89. SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
  90. SSL_CTX_set_max_proto_version(ctx, version);
  91. if(tls_ciphers && strcmp(tls_ciphers, "none") != 0) {
  92. if (!SSL_CTX_set_cipher_list(ctx, tls_ciphers)) {
  93. error("SSL error. cannot set the cipher list");
  94. }
  95. }
  96. }
  97. #endif
  98. SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  99. }
  100. /**
  101. * Initialize Openssl Client
  102. *
  103. * Starts the client context with TLS 1.2.
  104. *
  105. * @return It returns the context on success or NULL otherwise
  106. */
  107. SSL_CTX * security_initialize_openssl_client() {
  108. SSL_CTX *ctx;
  109. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  110. ctx = SSL_CTX_new(SSLv23_client_method());
  111. #else
  112. ctx = SSL_CTX_new(TLS_client_method());
  113. #endif
  114. if(ctx) {
  115. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  116. SSL_CTX_set_options (ctx,SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION);
  117. #else
  118. SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
  119. # if defined(TLS_MAX_VERSION)
  120. SSL_CTX_set_max_proto_version(ctx, TLS_MAX_VERSION);
  121. # elif defined(TLS1_3_VERSION)
  122. SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
  123. # elif defined(TLS1_2_VERSION)
  124. SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION);
  125. # endif
  126. #endif
  127. }
  128. return ctx;
  129. }
  130. /**
  131. * Initialize OpenSSL server
  132. *
  133. * Starts the server context with TLS 1.2 and load the certificate.
  134. *
  135. * @return It returns the context on success or NULL otherwise
  136. */
  137. static SSL_CTX * security_initialize_openssl_server() {
  138. SSL_CTX *ctx;
  139. char lerror[512];
  140. static int netdata_id_context = 1;
  141. //TO DO: Confirm the necessity to check return for other OPENSSL function
  142. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  143. ctx = SSL_CTX_new(SSLv23_server_method());
  144. if (!ctx) {
  145. error("Cannot create a new SSL context, netdata won't encrypt communication");
  146. return NULL;
  147. }
  148. SSL_CTX_use_certificate_file(ctx, security_cert, SSL_FILETYPE_PEM);
  149. #else
  150. ctx = SSL_CTX_new(TLS_server_method());
  151. if (!ctx) {
  152. error("Cannot create a new SSL context, netdata won't encrypt communication");
  153. return NULL;
  154. }
  155. SSL_CTX_use_certificate_chain_file(ctx, security_cert);
  156. #endif
  157. security_openssl_common_options(ctx, 0);
  158. SSL_CTX_use_PrivateKey_file(ctx,security_key,SSL_FILETYPE_PEM);
  159. if (!SSL_CTX_check_private_key(ctx)) {
  160. ERR_error_string_n(ERR_get_error(),lerror,sizeof(lerror));
  161. error("SSL cannot check the private key: %s",lerror);
  162. SSL_CTX_free(ctx);
  163. return NULL;
  164. }
  165. SSL_CTX_set_session_id_context(ctx,(void*)&netdata_id_context,(unsigned int)sizeof(netdata_id_context));
  166. SSL_CTX_set_info_callback(ctx,security_info_callback);
  167. #if (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_095)
  168. SSL_CTX_set_verify_depth(ctx,1);
  169. #endif
  170. debug(D_WEB_CLIENT,"SSL GLOBAL CONTEXT STARTED\n");
  171. return ctx;
  172. }
  173. /**
  174. * Start SSL
  175. *
  176. * Call the correct function to start the SSL context.
  177. *
  178. * @param selector informs the context that must be initialized, the following list has the valid values:
  179. * NETDATA_SSL_CONTEXT_SERVER - the server context
  180. * NETDATA_SSL_CONTEXT_STREAMING - Starts the streaming context.
  181. * NETDATA_SSL_CONTEXT_EXPORTING - Starts the OpenTSDB context
  182. */
  183. void security_start_ssl(int selector) {
  184. switch (selector) {
  185. case NETDATA_SSL_CONTEXT_SERVER: {
  186. struct stat statbuf;
  187. if (stat(security_key, &statbuf) || stat(security_cert, &statbuf)) {
  188. info("To use encryption it is necessary to set \"ssl certificate\" and \"ssl key\" in [web] !\n");
  189. return;
  190. }
  191. netdata_srv_ctx = security_initialize_openssl_server();
  192. SSL_CTX_set_mode(netdata_srv_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
  193. break;
  194. }
  195. case NETDATA_SSL_CONTEXT_STREAMING: {
  196. netdata_client_ctx = security_initialize_openssl_client();
  197. //This is necessary for the stream, because it is working sometimes with nonblock socket.
  198. //It returns the bitmask after to change, there is not any description of errors in the documentation
  199. SSL_CTX_set_mode(netdata_client_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE |SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |SSL_MODE_AUTO_RETRY);
  200. break;
  201. }
  202. case NETDATA_SSL_CONTEXT_EXPORTING: {
  203. netdata_exporting_ctx = security_initialize_openssl_client();
  204. break;
  205. }
  206. }
  207. }
  208. /**
  209. * Clean Open SSL
  210. *
  211. * Clean all the allocated contexts from netdata.
  212. */
  213. void security_clean_openssl()
  214. {
  215. if (netdata_srv_ctx) {
  216. SSL_CTX_free(netdata_srv_ctx);
  217. }
  218. if (netdata_client_ctx) {
  219. SSL_CTX_free(netdata_client_ctx);
  220. }
  221. if (netdata_exporting_ctx) {
  222. SSL_CTX_free(netdata_exporting_ctx);
  223. }
  224. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  225. ERR_free_strings();
  226. #endif
  227. }
  228. /**
  229. * Process accept
  230. *
  231. * Process the SSL handshake with the client case it is necessary.
  232. *
  233. * @param ssl is a pointer for the SSL structure
  234. * @param msg is a copy of the first 8 bytes of the initial message received
  235. *
  236. * @return it returns 0 case it performs the handshake, 8 case it is clean connection
  237. * and another integer power of 2 otherwise.
  238. */
  239. int security_process_accept(SSL *ssl,int msg) {
  240. int sock = SSL_get_fd(ssl);
  241. int test;
  242. if (msg > 0x17)
  243. {
  244. return NETDATA_SSL_NO_HANDSHAKE;
  245. }
  246. ERR_clear_error();
  247. if ((test = SSL_accept(ssl)) <= 0) {
  248. int sslerrno = SSL_get_error(ssl, test);
  249. switch(sslerrno) {
  250. case SSL_ERROR_WANT_READ:
  251. {
  252. error("SSL handshake did not finish and it wanna read on socket %d!", sock);
  253. return NETDATA_SSL_WANT_READ;
  254. }
  255. case SSL_ERROR_WANT_WRITE:
  256. {
  257. error("SSL handshake did not finish and it wanna read on socket %d!", sock);
  258. return NETDATA_SSL_WANT_WRITE;
  259. }
  260. case SSL_ERROR_NONE:
  261. case SSL_ERROR_SSL:
  262. case SSL_ERROR_SYSCALL:
  263. default:
  264. {
  265. u_long err;
  266. char buf[256];
  267. int counter = 0;
  268. while ((err = ERR_get_error()) != 0) {
  269. ERR_error_string_n(err, buf, sizeof(buf));
  270. info("%d SSL Handshake error (%s) on socket %d ", counter++, ERR_error_string((long)SSL_get_error(ssl, test), NULL), sock);
  271. }
  272. return NETDATA_SSL_NO_HANDSHAKE;
  273. }
  274. }
  275. }
  276. if (SSL_is_init_finished(ssl))
  277. {
  278. 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);
  279. }
  280. return NETDATA_SSL_HANDSHAKE_COMPLETE;
  281. }
  282. /**
  283. * Test Certificate
  284. *
  285. * Check the certificate of Netdata parent
  286. *
  287. * @param ssl is the connection structure
  288. *
  289. * @return It returns 0 on success and -1 otherwise
  290. */
  291. int security_test_certificate(SSL *ssl) {
  292. X509* cert = SSL_get_peer_certificate(ssl);
  293. int ret;
  294. long status;
  295. if (!cert) {
  296. return -1;
  297. }
  298. status = SSL_get_verify_result(ssl);
  299. if((X509_V_OK != status))
  300. {
  301. char error[512];
  302. ERR_error_string_n(ERR_get_error(), error, sizeof(error));
  303. error("SSL RFC4158 check: We have a invalid certificate, the tests result with %ld and message %s", status, error);
  304. ret = -1;
  305. } else {
  306. ret = 0;
  307. }
  308. return ret;
  309. }
  310. /**
  311. * Location for context
  312. *
  313. * Case the user give us a directory with the certificates available and
  314. * the Netdata parent certificate, we use this function to validate the certificate.
  315. *
  316. * @param ctx the context where the path will be set.
  317. * @param file the file with Netdata parent certificate.
  318. * @param path the directory where the certificates are stored.
  319. *
  320. * @return It returns 0 on success and -1 otherwise.
  321. */
  322. int security_location_for_context(SSL_CTX *ctx, char *file, char *path) {
  323. struct stat statbuf;
  324. if (stat(file, &statbuf)) {
  325. info("Netdata does not have the parent's SSL certificate, so it will use the default OpenSSL configuration to validate certificates!");
  326. return 0;
  327. }
  328. ERR_clear_error();
  329. u_long err;
  330. char buf[256];
  331. if(!SSL_CTX_load_verify_locations(ctx, file, path)) {
  332. goto slfc;
  333. }
  334. if(!SSL_CTX_set_default_verify_paths(ctx)) {
  335. goto slfc;
  336. }
  337. return 0;
  338. slfc:
  339. while ((err = ERR_get_error()) != 0) {
  340. ERR_error_string_n(err, buf, sizeof(buf));
  341. error("Cannot set the directory for the certificates and the parent SSL certificate: %s",buf);
  342. }
  343. return -1;
  344. }
  345. #endif