| 1 | #include "../libnetdata.h" |
| 2 | |
| 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 | |
| 13 | static SOCKET_PEERS netdata_ssl_peers(NETDATA_SSL *ssl) { |
| 14 | int sock_fd; |
| 15 | |
| 16 | if(unlikely(!ssl->conn)) |
| 17 | sock_fd = -1; |
| 18 | else |
| 19 | sock_fd = SSL_get_rfd(ssl->conn); |
| 20 | |
| 21 | return socket_peers(sock_fd); |
| 22 | } |
| 23 | |
| 24 | static void netdata_ssl_log_error_queue(const char *call, NETDATA_SSL *ssl, unsigned long err) { |
| 25 | nd_log_limit_static_thread_var(erl, 1, 0); |
| 26 | |
| 27 | if (err == SSL_ERROR_NONE) |
| 28 | err = ERR_get_error(); |
| 29 | |
| 30 | if (err == SSL_ERROR_NONE) |
| 31 | return; |
| 32 | |
| 33 | SOCKET_PEERS peers = netdata_ssl_peers(ssl); |
| 34 | const char *ssl_state = ssl->conn ? SSL_state_string_long(ssl->conn) : "No SSL connection"; |
| 35 | const char *cipher = ssl->conn ? SSL_get_cipher(ssl->conn) : "Unknown"; |
| 36 | const char *alpn_proto = NULL; |
| 37 | unsigned int alpn_len = 0; |
| 38 | |
| 39 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 40 | SSL_get0_alpn_selected(ssl->conn, (const unsigned char **)&alpn_proto, &alpn_len); |
| 41 | #endif |
| 42 | |
| 43 | do { |
| 44 | char *err_code; |
| 45 | switch (err) { |
| 46 | case SSL_ERROR_SSL: |
| 47 | err_code = "SSL_ERROR_SSL"; |
| 48 | ssl->state = NETDATA_SSL_STATE_FAILED; |
| 49 | break; |
| 50 | |
| 51 | case SSL_ERROR_WANT_READ: |
| 52 | err_code = "SSL_ERROR_WANT_READ"; |
| 53 | break; |
| 54 | |
| 55 | case SSL_ERROR_WANT_WRITE: |
| 56 | err_code = "SSL_ERROR_WANT_WRITE"; |
| 57 | break; |
| 58 | |
| 59 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 60 | err_code = "SSL_ERROR_WANT_X509_LOOKUP"; |
| 61 | break; |
| 62 | |
| 63 | case SSL_ERROR_SYSCALL: |
| 64 | err_code = "SSL_ERROR_SYSCALL"; |
| 65 | ssl->state = NETDATA_SSL_STATE_FAILED; |
| 66 | break; |
| 67 | |
| 68 | case SSL_ERROR_ZERO_RETURN: |
| 69 | err_code = "SSL_ERROR_ZERO_RETURN"; |
| 70 | ssl->state = NETDATA_SSL_STATE_FAILED; |
| 71 | break; |
| 72 | |
| 73 | case SSL_ERROR_WANT_CONNECT: |
| 74 | err_code = "SSL_ERROR_WANT_CONNECT"; |
| 75 | break; |
| 76 | |
| 77 | case SSL_ERROR_WANT_ACCEPT: |
| 78 | err_code = "SSL_ERROR_WANT_ACCEPT"; |
| 79 | break; |
| 80 | |
| 81 | #ifdef SSL_ERROR_WANT_ASYNC |
| 82 | case SSL_ERROR_WANT_ASYNC: |
| 83 | err_code = "SSL_ERROR_WANT_ASYNC"; |
| 84 | break; |
| 85 | #endif |
| 86 | |
| 87 | #ifdef SSL_ERROR_WANT_ASYNC_JOB |
| 88 | case SSL_ERROR_WANT_ASYNC_JOB: |
| 89 | err_code = "SSL_ERROR_WANT_ASYNC_JOB"; |
| 90 | break; |
| 91 | #endif |
| 92 | |
| 93 | #ifdef SSL_ERROR_WANT_CLIENT_HELLO_CB |
| 94 | case SSL_ERROR_WANT_CLIENT_HELLO_CB: |
| 95 | err_code = "SSL_ERROR_WANT_CLIENT_HELLO_CB"; |
| 96 | break; |
| 97 | #endif |
| 98 | |
| 99 | #ifdef SSL_ERROR_WANT_RETRY_VERIFY |
| 100 | case SSL_ERROR_WANT_RETRY_VERIFY: |
| 101 | err_code = "SSL_ERROR_WANT_RETRY_VERIFY"; |
| 102 | break; |
| 103 | #endif |
| 104 | |
| 105 | default: |
| 106 | err_code = "SSL_ERROR_UNKNOWN"; |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | const char *reason = ERR_reason_error_string(err); |
| 111 | int reason_code = ERR_GET_REASON(err); |
| 112 | |
| 113 | char err_str[1024 + 1]; |
| 114 | ERR_error_string_n(err, err_str, 1024); |
| 115 | |
| 116 | // Extract TLS Alert Information |
| 117 | const char *alert_type = "None"; |
| 118 | const char *alert_desc = "None"; |
| 119 | |
| 120 | if (ERR_GET_LIB(err) == ERR_LIB_SSL) { // Ensure it's an SSL error |
| 121 | alert_type = SSL_alert_type_string_long(reason_code); |
| 122 | alert_desc = SSL_alert_desc_string_long(reason_code); |
| 123 | } |
| 124 | |
| 125 | nd_log_limit(&erl, NDLS_DAEMON, NDLP_ERR, |
| 126 | "SSL ERROR: %s() on socket " |
| 127 | "local [[%s]:%d] <-> remote [[%s]:%d], " |
| 128 | "State [%s], Cipher: [%s], ALPN: [%.*s], " |
| 129 | "Error [%lu, %s, %s], " |
| 130 | "Reason [%d, %s], " |
| 131 | "Alert [%s, %s], " |
| 132 | "Errno [%d]", |
| 133 | call, |
| 134 | peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, |
| 135 | ssl_state, cipher, (int)alpn_len, alpn_proto ? alpn_proto : "None", |
| 136 | err, err_code, err_str, |
| 137 | reason_code, reason ? reason : "Unknown", |
| 138 | alert_type, alert_desc, |
| 139 | errno); |
| 140 | |
| 141 | } while ((err = ERR_get_error())); |
| 142 | } |
| 143 | |
| 144 | bool netdata_ssl_open_ext(NETDATA_SSL *ssl, SSL_CTX *ctx, int fd, const unsigned char *alpn_protos, unsigned int alpn_protos_len) { |
| 145 | errno = 0; |
| 146 | ssl->ssl_errno = 0; |
| 147 | |
| 148 | if(ssl->conn) { |
| 149 | if(!ctx || SSL_get_SSL_CTX(ssl->conn) != ctx) { |
| 150 | SSL_free(ssl->conn); |
| 151 | ssl->conn = NULL; |
| 152 | } |
| 153 | else if (SSL_clear(ssl->conn) == 0) { |
| 154 | netdata_ssl_log_error_queue("SSL_clear", ssl, SSL_ERROR_NONE); |
| 155 | SSL_free(ssl->conn); |
| 156 | ssl->conn = NULL; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if(!ssl->conn) { |
| 161 | if(!ctx) { |
| 162 | internal_error(true, "SSL: not CTX given"); |
| 163 | ssl->state = NETDATA_SSL_STATE_FAILED; |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | ssl->conn = SSL_new(ctx); |
| 168 | if (!ssl->conn) { |
| 169 | netdata_ssl_log_error_queue("SSL_new", ssl, SSL_ERROR_NONE); |
| 170 | ssl->state = NETDATA_SSL_STATE_FAILED; |
| 171 | return false; |
| 172 | } |
| 173 | if (alpn_protos && alpn_protos_len > 0) |
| 174 | SSL_set_alpn_protos(ssl->conn, alpn_protos, alpn_protos_len); |
| 175 | } |
| 176 | |
| 177 | if(SSL_set_fd(ssl->conn, fd) != 1) { |
| 178 | netdata_ssl_log_error_queue("SSL_set_fd", ssl, SSL_ERROR_NONE); |
| 179 | ssl->state = NETDATA_SSL_STATE_FAILED; |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | ssl->state = NETDATA_SSL_STATE_INIT; |
| 184 | |
| 185 | ERR_clear_error(); |
| 186 | |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | bool netdata_ssl_open(NETDATA_SSL *ssl, SSL_CTX *ctx, int fd) { |
| 191 | return netdata_ssl_open_ext(ssl, ctx, fd, NULL, 0); |
| 192 | } |
| 193 | |
| 194 | ALWAYS_INLINE |
| 195 | void netdata_ssl_close(NETDATA_SSL *ssl) { |
| 196 | errno = 0; |
| 197 | ssl->ssl_errno = 0; |
| 198 | |
| 199 | if(ssl->conn) { |
| 200 | if(SSL_connection(ssl)) { |
| 201 | int ret = SSL_shutdown(ssl->conn); |
| 202 | if(ret == 0) |
| 203 | SSL_shutdown(ssl->conn); |
| 204 | } |
| 205 | |
| 206 | SSL_free(ssl->conn); |
| 207 | |
| 208 | ERR_clear_error(); |
| 209 | } |
| 210 | |
| 211 | *ssl = NETDATA_SSL_UNSET_CONNECTION; |
| 212 | } |
| 213 | |
| 214 | ALWAYS_INLINE |
| 215 | static bool is_handshake_complete(NETDATA_SSL *ssl, const char *op) { |
| 216 | nd_log_limit_static_thread_var(erl, 1, 0); |
| 217 | |
| 218 | if(unlikely(!ssl->conn)) { |
| 219 | internal_error(true, "SSL: trying to %s on a NULL connection", op); |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | switch(ssl->state) { |
| 224 | case NETDATA_SSL_STATE_NOT_SSL: { |
| 225 | SOCKET_PEERS peers = netdata_ssl_peers(ssl); |
| 226 | nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING, |
| 227 | "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on non-SSL connection", |
| 228 | peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op); |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | case NETDATA_SSL_STATE_INIT: { |
| 233 | SOCKET_PEERS peers = netdata_ssl_peers(ssl); |
| 234 | nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING, |
| 235 | "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on an incomplete connection", |
| 236 | peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | case NETDATA_SSL_STATE_FAILED: { |
| 241 | SOCKET_PEERS peers = netdata_ssl_peers(ssl); |
| 242 | nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING, |
| 243 | "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on a failed connection", |
| 244 | peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | case NETDATA_SSL_STATE_COMPLETE: { |
| 249 | return true; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * netdata_ssl_read() should return the same as read(): |
| 258 | * |
| 259 | * Positive value: The read() function succeeded and read some bytes. The exact number of bytes read is returned. |
| 260 | * |
| 261 | * Zero: For files and sockets, a return value of zero signifies end-of-file (EOF), meaning no more data is available |
| 262 | * for reading. For sockets, this usually means the other side has closed the connection. |
| 263 | * |
| 264 | * -1: An error occurred. The specific error can be found by examining the errno variable. |
| 265 | * EAGAIN or EWOULDBLOCK: The file descriptor is in non-blocking mode, and the read operation would block. |
| 266 | * (These are often the same value, but can be different on some systems.) |
| 267 | */ |
| 268 | |
| 269 | ALWAYS_INLINE |
| 270 | ssize_t netdata_ssl_pending(NETDATA_SSL *ssl) { |
| 271 | return SSL_pending(ssl->conn); |
| 272 | } |
| 273 | |
| 274 | ALWAYS_INLINE |
| 275 | bool netdata_ssl_has_pending(NETDATA_SSL *ssl) { |
| 276 | // this call was added on OpenSSL 1.1.0 |
| 277 | // however, it is more accurate than SSL_pending() |
| 278 | // unfortunately it does not exists in libressl. |
| 279 | // return SSL_has_pending(ssl->conn); |
| 280 | |
| 281 | return SSL_pending(ssl->conn) > 0; |
| 282 | } |
| 283 | |
| 284 | ALWAYS_INLINE |
| 285 | ssize_t netdata_ssl_read(NETDATA_SSL *ssl, void *buf, size_t num) { |
| 286 | errno = 0; |
| 287 | ssl->ssl_errno = 0; |
| 288 | |
| 289 | if(unlikely(!is_handshake_complete(ssl, "read"))) { |
| 290 | errno = ENOTCONN; |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | int bytes = SSL_read(ssl->conn, buf, (int)num); |
| 295 | |
| 296 | if(unlikely(bytes <= 0)) { |
| 297 | int err = SSL_get_error(ssl->conn, bytes); |
| 298 | if (err == SSL_ERROR_ZERO_RETURN) { |
| 299 | ssl->ssl_errno = err; |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { |
| 304 | ssl->ssl_errno = err; |
| 305 | errno = EWOULDBLOCK; |
| 306 | } |
| 307 | else { |
| 308 | // For SSL_ERROR_SYSCALL, errno contains the underlying socket error |
| 309 | // (e.g., ECONNRESET). Save it before calling netdata_ssl_log_error_queue() |
| 310 | // which may corrupt errno through subsequent function calls. |
| 311 | int saved_errno = errno; |
| 312 | netdata_ssl_log_error_queue("SSL_read", ssl, err); |
| 313 | errno = saved_errno; |
| 314 | } |
| 315 | |
| 316 | bytes = -1; // according to read() or recv() |
| 317 | } |
| 318 | |
| 319 | return bytes; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * netdata_ssl_peek() - peek at incoming SSL data without consuming it |
| 324 | * |
| 325 | * This function is identical to netdata_ssl_read() but uses SSL_peek() |
| 326 | * instead of SSL_read(), leaving the data in the SSL buffer for a |
| 327 | * subsequent read operation. Useful for probing connection status. |
| 328 | * |
| 329 | * Returns: |
| 330 | * > 0: Number of bytes available to peek |
| 331 | * 0: Connection closed (SSL_ERROR_ZERO_RETURN) |
| 332 | * -1: Error (check errno: EWOULDBLOCK means no data available) |
| 333 | */ |
| 334 | ALWAYS_INLINE |
| 335 | ssize_t netdata_ssl_peek(NETDATA_SSL *ssl, void *buf, size_t num) { |
| 336 | errno = 0; |
| 337 | ssl->ssl_errno = 0; |
| 338 | |
| 339 | if(unlikely(!is_handshake_complete(ssl, "peek"))) { |
| 340 | errno = ENOTCONN; |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | int bytes = SSL_peek(ssl->conn, buf, (int)num); |
| 345 | |
| 346 | if(unlikely(bytes <= 0)) { |
| 347 | int err = SSL_get_error(ssl->conn, bytes); |
| 348 | if (err == SSL_ERROR_ZERO_RETURN) { |
| 349 | ssl->ssl_errno = err; |
| 350 | return 0; // Connection closed |
| 351 | } |
| 352 | |
| 353 | if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { |
| 354 | ssl->ssl_errno = err; |
| 355 | errno = EWOULDBLOCK; |
| 356 | } |
| 357 | else { |
| 358 | // For SSL_ERROR_SYSCALL, errno contains the underlying socket error |
| 359 | // (e.g., ECONNRESET). Save it before calling netdata_ssl_log_error_queue() |
| 360 | // which may corrupt errno through subsequent function calls. |
| 361 | int saved_errno = errno; |
| 362 | netdata_ssl_log_error_queue("SSL_peek", ssl, err); |
| 363 | errno = saved_errno; |
| 364 | } |
| 365 | |
| 366 | bytes = -1; |
| 367 | } |
| 368 | |
| 369 | return bytes; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * netdata_ssl_write() should return the same as write(): |
| 374 | * |
| 375 | * Positive value: The write() function succeeded and wrote some bytes. The exact number of bytes written is returned. |
| 376 | * |
| 377 | * Zero: It's technically possible for write() to return zero, indicating that zero bytes were written. However, for a |
| 378 | * socket, this generally does not happen unless the size of the data to be written is zero. |
| 379 | * |
| 380 | * -1: An error occurred. The specific error can be found by examining the errno variable. |
| 381 | * EAGAIN or EWOULDBLOCK: The file descriptor is in non-blocking mode, and the write operation would block. |
| 382 | * (These are often the same value, but can be different on some systems.) |
| 383 | */ |
| 384 | |
| 385 | ALWAYS_INLINE |
| 386 | ssize_t netdata_ssl_write(NETDATA_SSL *ssl, const void *buf, size_t num) { |
| 387 | errno = 0; |
| 388 | ssl->ssl_errno = 0; |
| 389 | |
| 390 | if(unlikely(!is_handshake_complete(ssl, "write"))) { |
| 391 | errno = ENOTCONN; |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | int bytes = SSL_write(ssl->conn, (uint8_t *)buf, (int)num); |
| 396 | |
| 397 | if(unlikely(bytes <= 0)) { |
| 398 | int err = SSL_get_error(ssl->conn, bytes); |
| 399 | if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { |
| 400 | ssl->ssl_errno = err; |
| 401 | errno = EWOULDBLOCK; |
| 402 | } |
| 403 | else { |
| 404 | // For SSL_ERROR_SYSCALL, errno contains the underlying socket error |
| 405 | // (e.g., ECONNRESET). Save it before calling netdata_ssl_log_error_queue() |
| 406 | // which may corrupt errno through subsequent function calls. |
| 407 | int saved_errno = errno; |
| 408 | netdata_ssl_log_error_queue("SSL_write", ssl, err); |
| 409 | errno = saved_errno; |
| 410 | } |
| 411 | |
| 412 | bytes = -1; // according to write() or send() |
| 413 | } |
| 414 | |
| 415 | return bytes; |
| 416 | } |
| 417 | |
| 418 | static inline bool is_handshake_initialized(NETDATA_SSL *ssl, const char *op) { |
| 419 | nd_log_limit_static_thread_var(erl, 1, 0); |
| 420 | |
| 421 | if(unlikely(!ssl->conn)) { |
| 422 | internal_error(true, "SSL: trying to %s on a NULL connection", op); |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | switch(ssl->state) { |
| 427 | case NETDATA_SSL_STATE_NOT_SSL: { |
| 428 | SOCKET_PEERS peers = netdata_ssl_peers(ssl); |
| 429 | nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING, |
| 430 | "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on non-SSL connection", |
| 431 | peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op); |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | case NETDATA_SSL_STATE_INIT: { |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | case NETDATA_SSL_STATE_FAILED: { |
| 440 | SOCKET_PEERS peers = netdata_ssl_peers(ssl); |
| 441 | nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING, |
| 442 | "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on a failed connection", |
| 443 | peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op); |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | case NETDATA_SSL_STATE_COMPLETE: { |
| 448 | SOCKET_PEERS peers = netdata_ssl_peers(ssl); |
| 449 | nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING, |
| 450 | "SSL: on socket local [[%s]:%d] <-> remote [[%s]:%d], attempt to %s on an complete connection", |
| 451 | peers.local.ip, peers.local.port, peers.peer.ip, peers.peer.port, op); |
| 452 | return false; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | #define WANT_READ_WRITE_TIMEOUT_MS 10 |
| 460 | |
| 461 | ALWAYS_INLINE |
| 462 | static bool want_read_write_should_retry(NETDATA_SSL *ssl, int err) { |
| 463 | int ssl_errno = SSL_get_error(ssl->conn, err); |
| 464 | if(ssl_errno == SSL_ERROR_WANT_READ || ssl_errno == SSL_ERROR_WANT_WRITE) { |
| 465 | struct pollfd pfds[1] = { [0] = { |
| 466 | .fd = SSL_get_rfd(ssl->conn), |
| 467 | .events = (short)(((ssl_errno == SSL_ERROR_WANT_READ ) ? POLLIN : 0) | |
| 468 | ((ssl_errno == SSL_ERROR_WANT_WRITE) ? POLLOUT : 0)), |
| 469 | }}; |
| 470 | |
| 471 | if(poll(pfds, 1, WANT_READ_WRITE_TIMEOUT_MS) <= 0) |
| 472 | return false; // timeout (0) or error (<0) |
| 473 | |
| 474 | return true; // we have activity, so we should retry |
| 475 | } |
| 476 | |
| 477 | return false; // an unknown error |
| 478 | } |
| 479 | |
| 480 | bool netdata_ssl_connect(NETDATA_SSL *ssl) { |
| 481 | errno = 0; |
| 482 | ssl->ssl_errno = 0; |
| 483 | |
| 484 | if(unlikely(!is_handshake_initialized(ssl, "connect"))) |
| 485 | return false; |
| 486 | |
| 487 | SSL_set_connect_state(ssl->conn); |
| 488 | |
| 489 | int err; |
| 490 | while ((err = SSL_connect(ssl->conn)) != 1) { |
| 491 | if(!want_read_write_should_retry(ssl, err)) |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | if (err != 1) { |
| 496 | err = SSL_get_error(ssl->conn, err); |
| 497 | netdata_ssl_log_error_queue("SSL_connect", ssl, err); |
| 498 | ssl->state = NETDATA_SSL_STATE_FAILED; |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | ssl->state = NETDATA_SSL_STATE_COMPLETE; |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | bool netdata_ssl_accept(NETDATA_SSL *ssl) { |
| 507 | errno = 0; |
| 508 | ssl->ssl_errno = 0; |
| 509 | |
| 510 | if(unlikely(!is_handshake_initialized(ssl, "accept"))) |
| 511 | return false; |
| 512 | |
| 513 | SSL_set_accept_state(ssl->conn); |
| 514 | |
| 515 | int err; |
| 516 | while ((err = SSL_accept(ssl->conn)) != 1) { |
| 517 | if(!want_read_write_should_retry(ssl, err)) |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | if (err != 1) { |
| 522 | err = SSL_get_error(ssl->conn, err); |
| 523 | netdata_ssl_log_error_queue("SSL_accept", ssl, err); |
| 524 | ssl->state = NETDATA_SSL_STATE_FAILED; |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | ssl->state = NETDATA_SSL_STATE_COMPLETE; |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Info Callback |
| 534 | * |
| 535 | * Function used as callback for the OpenSSL Library |
| 536 | * |
| 537 | * @param ssl a pointer to the SSL structure of the client |
| 538 | * @param where the variable with the flags set. |
| 539 | * @param ret the return of the caller |
| 540 | */ |
| 541 | static void netdata_ssl_info_callback(const SSL *ssl, int where, int ret __maybe_unused) { |
| 542 | (void)ssl; |
| 543 | if (where & SSL_CB_ALERT) { |
| 544 | netdata_log_debug(D_WEB_CLIENT,"SSL INFO CALLBACK %s %s", SSL_alert_type_string(ret), SSL_alert_desc_string_long(ret)); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * OpenSSL Library |
| 550 | * |
| 551 | * Starts the openssl library for the Netdata. |
| 552 | */ |
| 553 | void netdata_ssl_initialize_openssl() { |
| 554 | |
| 555 | #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110 |
| 556 | # if (SSLEAY_VERSION_NUMBER >= OPENSSL_VERSION_097) |
| 557 | OPENSSL_config(NULL); |
| 558 | # endif |
| 559 | |
| 560 | SSL_load_error_strings(); |
| 561 | |
| 562 | SSL_library_init(); |
| 563 | |
| 564 | #else |
| 565 | |
| 566 | if (OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL) != 1) { |
| 567 | netdata_log_error("SSL library cannot be initialized."); |
| 568 | } |
| 569 | |
| 570 | #endif |
| 571 | } |
| 572 | |
| 573 | #if OPENSSL_VERSION_NUMBER >= OPENSSL_VERSION_110 |
| 574 | /** |
| 575 | * TLS version |
| 576 | * |
| 577 | * Returns the TLS version depending of the user input. |
| 578 | * |
| 579 | * @param lversion is the user input. |
| 580 | * |
| 581 | * @return it returns the version number. |
| 582 | */ |
| 583 | static int netdata_ssl_select_tls_version(const char *lversion) { |
| 584 | if (!strcmp(lversion, "1") || !strcmp(lversion, "1.0")) |
| 585 | return TLS1_VERSION; |
| 586 | else if (!strcmp(lversion, "1.1")) |
| 587 | return TLS1_1_VERSION; |
| 588 | else if (!strcmp(lversion, "1.2")) |
| 589 | return TLS1_2_VERSION; |
| 590 | #if defined(TLS1_3_VERSION) |
| 591 | else if (!strcmp(lversion, "1.3")) |
| 592 | return TLS1_3_VERSION; |
| 593 | #endif |
| 594 | |
| 595 | #if defined(TLS_MAX_VERSION) |
| 596 | return TLS_MAX_VERSION; |
| 597 | #else |
| 598 | return TLS1_2_VERSION; |
| 599 | #endif |
| 600 | } |
| 601 | #endif |
| 602 | |
| 603 | /** |
| 604 | * Initialize Openssl Client |
| 605 | * |
| 606 | * Starts the client context with TLS 1.2. |
| 607 | * |
| 608 | * @return It returns the context on success or NULL otherwise |
| 609 | */ |
| 610 | SSL_CTX * netdata_ssl_create_client_ctx(unsigned long mode) { |
| 611 | SSL_CTX *ctx; |
| 612 | #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110 |
| 613 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 614 | #else |
| 615 | ctx = SSL_CTX_new(TLS_client_method()); |
| 616 | #endif |
| 617 | if(ctx) { |
| 618 | #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110 |
| 619 | SSL_CTX_set_options (ctx,SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION); |
| 620 | #else |
| 621 | SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 622 | # if defined(TLS_MAX_VERSION) |
| 623 | SSL_CTX_set_max_proto_version(ctx, TLS_MAX_VERSION); |
| 624 | # elif defined(TLS1_3_VERSION) |
| 625 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 626 | # elif defined(TLS1_2_VERSION) |
| 627 | SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION); |
| 628 | # endif |
| 629 | #endif |
| 630 | } |
| 631 | |
| 632 | if(mode) |
| 633 | SSL_CTX_set_mode(ctx, mode); |
| 634 | |
| 635 | return ctx; |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Initialize OpenSSL server |
| 640 | * |
| 641 | * Starts the server context with TLS 1.2 and load the certificate. |
| 642 | * |
| 643 | * @return It returns the context on success or NULL otherwise |
| 644 | */ |
| 645 | static SSL_CTX * netdata_ssl_create_server_ctx(unsigned long mode) { |
| 646 | SSL_CTX *ctx; |
| 647 | char lerror[512]; |
| 648 | static int netdata_id_context = 1; |
| 649 | |
| 650 | //TO DO: Confirm the necessity to check return for other OPENSSL function |
| 651 | #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110 |
| 652 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 653 | if (!ctx) { |
| 654 | netdata_log_error("Cannot create a new SSL context, netdata won't encrypt communication"); |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | SSL_CTX_use_certificate_file(ctx, netdata_ssl_security_cert, SSL_FILETYPE_PEM); |
| 659 | #else |
| 660 | ctx = SSL_CTX_new(TLS_server_method()); |
| 661 | if (!ctx) { |
| 662 | netdata_log_error("Cannot create a new SSL context, netdata won't encrypt communication"); |
| 663 | return NULL; |
| 664 | } |
| 665 | |
| 666 | SSL_CTX_use_certificate_chain_file(ctx, netdata_ssl_security_cert); |
| 667 | #endif |
| 668 | |
| 669 | #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110 |
| 670 | SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION); |
| 671 | #else |
| 672 | SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 673 | SSL_CTX_set_max_proto_version(ctx, netdata_ssl_select_tls_version(tls_version)); |
| 674 | |
| 675 | if(tls_ciphers && strcmp(tls_ciphers, "none") != 0) { |
| 676 | if (!SSL_CTX_set_cipher_list(ctx, tls_ciphers)) { |
| 677 | netdata_log_error("SSL error. cannot set the cipher list"); |
| 678 | } |
| 679 | } |
| 680 | #endif |
| 681 | |
| 682 | SSL_CTX_use_PrivateKey_file(ctx, netdata_ssl_security_key,SSL_FILETYPE_PEM); |
| 683 | |
| 684 | if (!SSL_CTX_check_private_key(ctx)) { |
| 685 | ERR_error_string_n(ERR_get_error(),lerror,sizeof(lerror)); |
| 686 | netdata_log_error("SSL cannot check the private key: %s",lerror); |
| 687 | SSL_CTX_free(ctx); |
| 688 | return NULL; |
| 689 | } |
| 690 | |
| 691 | SSL_CTX_set_session_id_context(ctx,(void*)&netdata_id_context,(unsigned int)sizeof(netdata_id_context)); |
| 692 | SSL_CTX_set_info_callback(ctx, netdata_ssl_info_callback); |
| 693 | |
| 694 | #if (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_095) |
| 695 | SSL_CTX_set_verify_depth(ctx,1); |
| 696 | #endif |
| 697 | netdata_log_debug(D_WEB_CLIENT,"SSL GLOBAL CONTEXT STARTED\n"); |
| 698 | |
| 699 | SSL_CTX_set_mode(ctx, mode); |
| 700 | |
| 701 | return ctx; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Start SSL |
| 706 | * |
| 707 | * Call the correct function to start the SSL context. |
| 708 | * |
| 709 | * @param selector informs the context that must be initialized, the following list has the valid values: |
| 710 | * NETDATA_SSL_CONTEXT_SERVER - the server context |
| 711 | * NETDATA_SSL_CONTEXT_STREAMING - Starts the streaming context. |
| 712 | * NETDATA_SSL_CONTEXT_EXPORTING - Starts the OpenTSDB context |
| 713 | */ |
| 714 | void netdata_ssl_initialize_ctx(int selector) { |
| 715 | static SPINLOCK sp = SPINLOCK_INITIALIZER; |
| 716 | spinlock_lock(&sp); |
| 717 | |
| 718 | switch (selector) { |
| 719 | case NETDATA_SSL_WEB_SERVER_CTX: { |
| 720 | if(!netdata_ssl_web_server_ctx) { |
| 721 | struct stat statbuf; |
| 722 | if (stat(netdata_ssl_security_key, &statbuf) || stat(netdata_ssl_security_cert, &statbuf)) |
| 723 | netdata_log_info("To use encryption it is necessary to set \"ssl certificate\" and \"ssl key\" in [web] !\n"); |
| 724 | else { |
| 725 | netdata_ssl_web_server_ctx = netdata_ssl_create_server_ctx( |
| 726 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 727 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 728 | // SSL_MODE_AUTO_RETRY | |
| 729 | 0); |
| 730 | |
| 731 | if(netdata_ssl_web_server_ctx && !netdata_ssl_validate_certificate) |
| 732 | SSL_CTX_set_verify(netdata_ssl_web_server_ctx, SSL_VERIFY_NONE, NULL); |
| 733 | } |
| 734 | } |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | case NETDATA_SSL_STREAMING_SENDER_CTX: { |
| 739 | if(!netdata_ssl_streaming_sender_ctx) { |
| 740 | //This is necessary for the stream, because it is working sometimes with nonblock socket. |
| 741 | //It returns the bitmask after to change, there is not any description of errors in the documentation |
| 742 | netdata_ssl_streaming_sender_ctx = netdata_ssl_create_client_ctx( |
| 743 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 744 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 745 | // SSL_MODE_AUTO_RETRY | |
| 746 | 0 |
| 747 | ); |
| 748 | |
| 749 | if(netdata_ssl_streaming_sender_ctx && !netdata_ssl_validate_certificate_sender) |
| 750 | SSL_CTX_set_verify(netdata_ssl_streaming_sender_ctx, SSL_VERIFY_NONE, NULL); |
| 751 | } |
| 752 | break; |
| 753 | } |
| 754 | |
| 755 | case NETDATA_SSL_EXPORTING_CTX: { |
| 756 | if(!netdata_ssl_exporting_ctx) { |
| 757 | netdata_ssl_exporting_ctx = netdata_ssl_create_client_ctx(0); |
| 758 | |
| 759 | if(netdata_ssl_exporting_ctx && !netdata_ssl_validate_certificate) |
| 760 | SSL_CTX_set_verify(netdata_ssl_exporting_ctx, SSL_VERIFY_NONE, NULL); |
| 761 | } |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | spinlock_unlock(&sp); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Clean Open SSL |
| 771 | * |
| 772 | * Clean all the allocated contexts from netdata. |
| 773 | */ |
| 774 | void netdata_ssl_cleanup() |
| 775 | { |
| 776 | if (netdata_ssl_web_server_ctx) { |
| 777 | SSL_CTX_free(netdata_ssl_web_server_ctx); |
| 778 | netdata_ssl_web_server_ctx = NULL; |
| 779 | } |
| 780 | |
| 781 | if (netdata_ssl_streaming_sender_ctx) { |
| 782 | SSL_CTX_free(netdata_ssl_streaming_sender_ctx); |
| 783 | netdata_ssl_streaming_sender_ctx = NULL; |
| 784 | } |
| 785 | |
| 786 | if (netdata_ssl_exporting_ctx) { |
| 787 | SSL_CTX_free(netdata_ssl_exporting_ctx); |
| 788 | netdata_ssl_exporting_ctx = NULL; |
| 789 | } |
| 790 | |
| 791 | #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110 |
| 792 | ERR_free_strings(); |
| 793 | #endif |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Test Certificate |
| 798 | * |
| 799 | * Check the certificate of Netdata parent |
| 800 | * |
| 801 | * @param ssl is the connection structure |
| 802 | * |
| 803 | * @return It returns 0 on success and -1 otherwise |
| 804 | */ |
| 805 | int security_test_certificate(SSL *ssl) { |
| 806 | X509* cert = SSL_get_peer_certificate(ssl); |
| 807 | int ret; |
| 808 | long status; |
| 809 | if (!cert) { |
| 810 | return -1; |
| 811 | } |
| 812 | |
| 813 | status = SSL_get_verify_result(ssl); |
| 814 | if((X509_V_OK != status)) |
| 815 | { |
| 816 | char error[512]; |
| 817 | ERR_error_string_n(ERR_get_error(), error, sizeof(error)); |
| 818 | netdata_log_error("SSL RFC4158 check: We have a invalid certificate, the tests result with %ld and message %s", status, error); |
| 819 | ret = -1; |
| 820 | } else { |
| 821 | ret = 0; |
| 822 | } |
| 823 | |
| 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Location for context |
| 829 | * |
| 830 | * Case the user give us a directory with the certificates available and |
| 831 | * the Netdata parent certificate, we use this function to validate the certificate. |
| 832 | * |
| 833 | * @param ctx the context where the path will be set. |
| 834 | * @param file the file with Netdata parent certificate. |
| 835 | * @param path the directory where the certificates are stored. |
| 836 | * |
| 837 | * @return It returns 0 on success and -1 otherwise. |
| 838 | */ |
| 839 | int ssl_security_location_for_context(SSL_CTX *ctx, const char *file, const char *path) { |
| 840 | if(file && !*file) file = NULL; |
| 841 | if(path && !*path) path = NULL; |
| 842 | |
| 843 | int load_custom = 1, load_default = 1; |
| 844 | if (file || path) { |
| 845 | if(!SSL_CTX_load_verify_locations(ctx, file, path)) { |
| 846 | 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!"); |
| 847 | load_custom = 0; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | if(!SSL_CTX_set_default_verify_paths(ctx)) { |
| 852 | netdata_log_info("Can not verify default OpenSSL configuration to validate certificates!"); |
| 853 | load_default = 0; |
| 854 | } |
| 855 | |
| 856 | if (load_custom == 0 && load_default == 0) |
| 857 | return -1; |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | void netdata_ssl_log_verify_error(X509_STORE_CTX *ctx) { |
| 863 | int err = X509_STORE_CTX_get_error(ctx); |
| 864 | int depth = X509_STORE_CTX_get_error_depth(ctx); |
| 865 | |
| 866 | BIO *bio = NULL; |
| 867 | const char *subject = NULL; |
| 868 | int subject_len = 0; |
| 869 | |
| 870 | X509 *cert = X509_STORE_CTX_get_current_cert(ctx); |
| 871 | if (cert) { |
| 872 | X509_NAME *name = X509_get_subject_name(cert); |
| 873 | if (name) { |
| 874 | bio = BIO_new(BIO_s_mem()); |
| 875 | if (bio && X509_NAME_print_ex(bio, name, 0, XN_FLAG_ONELINE) >= 0) { |
| 876 | BUF_MEM *bptr = NULL; |
| 877 | /* Optional defensive check: |
| 878 | * BIO_get_mem_ptr() returns 1 on success, 0 on failure. */ |
| 879 | if (BIO_get_mem_ptr(bio, &bptr) > 0 && bptr && bptr->data && bptr->length > 0) { |
| 880 | subject = bptr->data; |
| 881 | subject_len = (int)bptr->length; |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | if (subject && subject_len > 0) { |
| 888 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 889 | "SSL: certificate verify error %d:%s at depth %d, subject: %.*s", |
| 890 | err, X509_verify_cert_error_string(err), depth, subject_len, subject); |
| 891 | } else { |
| 892 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 893 | "SSL: certificate verify error %d:%s at depth %d", |
| 894 | err, X509_verify_cert_error_string(err), depth); |
| 895 | } |
| 896 | |
| 897 | BIO_free(bio); |
| 898 | } |