security.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "../libnetdata.h"
  2. #ifdef ENABLE_HTTPS
  3. SSL_CTX *netdata_ssl_exporting_ctx =NULL;
  4. SSL_CTX *netdata_ssl_client_ctx =NULL;
  5. SSL_CTX *netdata_ssl_srv_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. int netdata_ssl_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, netdata_ssl_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, netdata_ssl_security_cert);
  156. #endif
  157. security_openssl_common_options(ctx, 0);
  158. SSL_CTX_use_PrivateKey_file(ctx, netdata_ssl_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. static SPINLOCK sp = NETDATA_SPINLOCK_INITIALIZER;
  185. netdata_spinlock_lock(&sp);
  186. switch (selector) {
  187. case NETDATA_SSL_CONTEXT_SERVER: {
  188. if(!netdata_ssl_srv_ctx) {
  189. struct stat statbuf;
  190. if (stat(netdata_ssl_security_key, &statbuf) || stat(netdata_ssl_security_cert, &statbuf))
  191. info("To use encryption it is necessary to set \"ssl certificate\" and \"ssl key\" in [web] !\n");
  192. else {
  193. netdata_ssl_srv_ctx = security_initialize_openssl_server();
  194. SSL_CTX_set_mode(netdata_ssl_srv_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
  195. }
  196. }
  197. break;
  198. }
  199. case NETDATA_SSL_CONTEXT_STREAMING: {
  200. if(!netdata_ssl_client_ctx) {
  201. netdata_ssl_client_ctx = security_initialize_openssl_client();
  202. //This is necessary for the stream, because it is working sometimes with nonblock socket.
  203. //It returns the bitmask after to change, there is not any description of errors in the documentation
  204. SSL_CTX_set_mode(netdata_ssl_client_ctx,
  205. SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
  206. SSL_MODE_AUTO_RETRY);
  207. }
  208. break;
  209. }
  210. case NETDATA_SSL_CONTEXT_EXPORTING: {
  211. if(!netdata_ssl_exporting_ctx)
  212. netdata_ssl_exporting_ctx = security_initialize_openssl_client();
  213. break;
  214. }
  215. }
  216. netdata_spinlock_unlock(&sp);
  217. }
  218. /**
  219. * Clean Open SSL
  220. *
  221. * Clean all the allocated contexts from netdata.
  222. */
  223. void security_clean_openssl()
  224. {
  225. if (netdata_ssl_srv_ctx) {
  226. SSL_CTX_free(netdata_ssl_srv_ctx);
  227. }
  228. if (netdata_ssl_client_ctx) {
  229. SSL_CTX_free(netdata_ssl_client_ctx);
  230. }
  231. if (netdata_ssl_exporting_ctx) {
  232. SSL_CTX_free(netdata_ssl_exporting_ctx);
  233. }
  234. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110
  235. ERR_free_strings();
  236. #endif
  237. }
  238. /**
  239. * Process accept
  240. *
  241. * Process the SSL handshake with the client case it is necessary.
  242. *
  243. * @param ssl is a pointer for the SSL structure
  244. * @param msg is a copy of the first 8 bytes of the initial message received
  245. *
  246. * @return it returns 0 case it performs the handshake, 8 case it is clean connection
  247. * and another integer power of 2 otherwise.
  248. */
  249. int security_process_accept(SSL *ssl,int msg) {
  250. int sock = SSL_get_fd(ssl);
  251. int test;
  252. if (msg > 0x17)
  253. {
  254. return NETDATA_SSL_NO_HANDSHAKE;
  255. }
  256. ERR_clear_error();
  257. if ((test = SSL_accept(ssl)) <= 0) {
  258. int sslerrno = SSL_get_error(ssl, test);
  259. switch(sslerrno) {
  260. case SSL_ERROR_WANT_READ:
  261. {
  262. error("SSL handshake did not finish and it wanna read on socket %d!", sock);
  263. return NETDATA_SSL_WANT_READ;
  264. }
  265. case SSL_ERROR_WANT_WRITE:
  266. {
  267. error("SSL handshake did not finish and it wanna read on socket %d!", sock);
  268. return NETDATA_SSL_WANT_WRITE;
  269. }
  270. case SSL_ERROR_NONE:
  271. case SSL_ERROR_SSL:
  272. case SSL_ERROR_SYSCALL:
  273. default:
  274. {
  275. u_long err;
  276. char buf[256];
  277. int counter = 0;
  278. while ((err = ERR_get_error()) != 0) {
  279. ERR_error_string_n(err, buf, sizeof(buf));
  280. error("%d SSL Handshake error (%s) on socket %d", counter++, ERR_error_string((long)SSL_get_error(ssl, test), NULL), sock);
  281. }
  282. return NETDATA_SSL_NO_HANDSHAKE;
  283. }
  284. }
  285. }
  286. if (SSL_is_init_finished(ssl))
  287. {
  288. 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);
  289. }
  290. return NETDATA_SSL_HANDSHAKE_COMPLETE;
  291. }
  292. /**
  293. * Test Certificate
  294. *
  295. * Check the certificate of Netdata parent
  296. *
  297. * @param ssl is the connection structure
  298. *
  299. * @return It returns 0 on success and -1 otherwise
  300. */
  301. int security_test_certificate(SSL *ssl) {
  302. X509* cert = SSL_get_peer_certificate(ssl);
  303. int ret;
  304. long status;
  305. if (!cert) {
  306. return -1;
  307. }
  308. status = SSL_get_verify_result(ssl);
  309. if((X509_V_OK != status))
  310. {
  311. char error[512];
  312. ERR_error_string_n(ERR_get_error(), error, sizeof(error));
  313. error("SSL RFC4158 check: We have a invalid certificate, the tests result with %ld and message %s", status, error);
  314. ret = -1;
  315. } else {
  316. ret = 0;
  317. }
  318. return ret;
  319. }
  320. /**
  321. * Location for context
  322. *
  323. * Case the user give us a directory with the certificates available and
  324. * the Netdata parent certificate, we use this function to validate the certificate.
  325. *
  326. * @param ctx the context where the path will be set.
  327. * @param file the file with Netdata parent certificate.
  328. * @param path the directory where the certificates are stored.
  329. *
  330. * @return It returns 0 on success and -1 otherwise.
  331. */
  332. int ssl_security_location_for_context(SSL_CTX *ctx, char *file, char *path) {
  333. int load_custom = 1, load_default = 1;
  334. if (file || path) {
  335. if(!SSL_CTX_load_verify_locations(ctx, file, path)) {
  336. 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!");
  337. load_custom = 0;
  338. }
  339. }
  340. if(!SSL_CTX_set_default_verify_paths(ctx)) {
  341. info("Can not verify default OpenSSL configuration to validate certificates!");
  342. load_default = 0;
  343. }
  344. if (load_custom == 0 && load_default == 0)
  345. return -1;
  346. return 0;
  347. }
  348. #endif