diff --git a/plugins/imdtls/imdtls.c b/plugins/imdtls/imdtls.c index 88b45374f..65e2eb268 100644 --- a/plugins/imdtls/imdtls.c +++ b/plugins/imdtls/imdtls.c @@ -35,6 +35,7 @@ #include #include #include +#include // --- Include openssl headers as well #include @@ -98,6 +99,10 @@ struct dtlsClient_s { SSL *sslClient; /* DTSL (SSL) Client */ time_t lastActivityTime; /* Last Activity Time */ int clientfd; /* ClientFD */ + int peerVerified; /* post-handshake permitted-peer check completed */ + long verifiedRenegotiations; /* handshake generation covered by peerVerified */ + short pollEvents; /* readiness needed by the handshake */ + int drainPending; /* continue after the per-client read limit */ }; // Use typedef to define a type 'dtlsClient_t' based on 'struct dtlsClient_s' @@ -572,6 +577,32 @@ static void DTLScleanupSession(instanceConf_t *inst, int idx) { } inst->dtlsClients[idx]->sslClient = NULL; inst->dtlsClients[idx]->lastActivityTime = 0; + inst->dtlsClients[idx]->peerVerified = 0; + inst->dtlsClients[idx]->verifiedRenegotiations = 0; + inst->dtlsClients[idx]->pollEvents = POLLIN; + inst->dtlsClients[idx]->drainPending = 0; +} + +static int DTLSVerifySession(instanceConf_t *inst, int idx) { + dtlsClient_t *client = inst->dtlsClients[idx]; + if (!SSL_is_init_finished(client->sslClient)) { + client->peerVerified = 0; + return 0; + } + long renegotiations = SSL_total_renegotiations(client->sslClient); + if (client->peerVerified && client->verifiedRenegotiations == renegotiations) { + return 1; + } + + if (imdtls_verify_callback(1, client->sslClient) == 0) { + LogMsg(0, RS_RET_NO_ERRCODE, LOG_WARNING, "imdtls: Cert Verify FAILED for DTLS client idx (%d)", idx); + DTLScleanupSession(inst, idx); + return 0; + } + client->peerVerified = 1; + client->verifiedRenegotiations = renegotiations; + DBGPRINTF("imdtls: Cert Verify SUCCESS for DTLS client idx (%d)\n", idx); + return 1; } static void DTLSAcceptSession(instanceConf_t *inst, int idx) { @@ -587,7 +618,7 @@ static void DTLSAcceptSession(instanceConf_t *inst, int idx) { if (ret <= 0) { err = SSL_get_error(ssl, ret); if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { - // Non-blocking operation did not complete; retry later + inst->dtlsClients[idx]->pollEvents = (err == SSL_ERROR_WANT_WRITE) ? POLLIN | POLLOUT : POLLIN; DBGPRINTF("imdtls: SSL_accept didn't complete (%d). Will retry.\n", err); } else if (err == SSL_ERROR_SYSCALL) { DBGPRINTF("imdtls: SSL_accept failed SSL_ERROR_SYSCALL idx (%d), removing client.\n", idx); @@ -602,18 +633,12 @@ static void DTLSAcceptSession(instanceConf_t *inst, int idx) { } else { DBGPRINTF("imdtls: SSL_accept success idx (%d), adding client.\n", idx); inst->dtlsClients[idx]->lastActivityTime = time(NULL); - - int status = 1; - status = imdtls_verify_callback(status, ssl); - if (status == 0) { - LogMsg(0, RS_RET_NO_ERRCODE, LOG_WARNING, "imdtls: Cert Verify FAILED for DTLS client idx (%d)", idx); - DTLScleanupSession(inst, idx); - } else { - DBGPRINTF("imdtls: Cert Verify SUCCESS for DTLS client idx (%d)\n", idx); - } + inst->dtlsClients[idx]->pollEvents = POLLIN; + DTLSVerifySession(inst, idx); } } else { DBGPRINTF("imdtls: SSL_get_state for DTLS client idx (%d) is %d\n", idx, ret); + DTLSVerifySession(inst, idx); } } @@ -630,26 +655,44 @@ static void DTLSReadClient(instanceConf_t *inst, int idx, short revents) { int err; SSL *ssl = inst->dtlsClients[idx]->sslClient; DBGPRINTF("imdtls: DEBUG Check Client activity on index %d.\n", idx); - if (revents & POLLIN) { + if (revents & (POLLIN | POLLOUT)) { if (ssl == NULL) { DBGPRINTF("imdtls: DTLSHandleSessions MISSING SSL OBJ for index %d.\n", idx); return; } + if (!SSL_is_init_finished(ssl)) { + DTLSAcceptSession(inst, idx); + ssl = inst->dtlsClients[idx]->sslClient; + if (ssl == NULL || !SSL_is_init_finished(ssl)) { + return; + } + } + if (!DTLSVerifySession(inst, idx) || !(revents & inst->dtlsClients[idx]->pollEvents)) { + return; + } DBGPRINTF("imdtls: Read Client activity on index %d.\n", idx); char buf[MAX_DTLS_MSGSIZE]; int len = 0; + int reads = 0; + inst->dtlsClients[idx]->drainPending = 0; do { len = SSL_read(ssl, buf, sizeof(buf) - 1); if (len > 0) { + inst->dtlsClients[idx]->pollEvents = POLLIN; + if (!DTLSVerifySession(inst, idx)) { + return; + } buf[len] = '\0'; processMsg(inst, inst->dtlsClients[idx], buf, len); } else { err = SSL_get_error(ssl, len); if (err == SSL_ERROR_WANT_READ) { + inst->dtlsClients[idx]->pollEvents = POLLIN; DBGPRINTF("imdtls: SSL_ERROR_WANT_READ flush rbio on index %d.\n", idx); BIO *rbio = SSL_get_rbio(ssl); BIO_flush(rbio); } else if (err == SSL_ERROR_WANT_WRITE) { + inst->dtlsClients[idx]->pollEvents = POLLIN | POLLOUT; DBGPRINTF("imdtls: SSL_ERROR_WANT_WRITE flush wbio on index %d.\n", idx); BIO *wbio = SSL_get_wbio(ssl); BIO_flush(wbio); @@ -668,7 +711,15 @@ static void DTLSReadClient(instanceConf_t *inst, int idx, short revents) { break; } } - } while (len > 0); + ++reads; + } while (len > 0 && reads < 32); + /* OpenSSL can retain complete DTLS records after the datagram socket + * has been drained. Retry once on the next loop after the limit, + * even if poll reports no new socket data. A WANT_READ clears this + * flag, so an incomplete record cannot spin the event loop. */ + if (len > 0 && reads == 32) { + inst->dtlsClients[idx]->drainPending = 1; + } } } @@ -678,6 +729,8 @@ static void DTLSHandleSessions(instanceConf_t *inst) { int optval = 1; int fdcount = 0; int ret, err; + int pollTimeout = -1; + time_t now = time(NULL); memset(clientIdxByPollSlot, -1, sizeof(clientIdxByPollSlot)); memset(fds, 0, sizeof(fds)); fds[0].fd = inst->sockfd; @@ -696,13 +749,36 @@ static void DTLSHandleSessions(instanceConf_t *inst) { fdcount++; DBGPRINTF("imdtls: DTLSHandleSessions handle client %d (%d)\n", fdcount, clientfd); fds[fdcount].fd = clientfd; - fds[fdcount].events = POLLIN; + fds[fdcount].events = inst->dtlsClients[i]->pollEvents; clientIdxByPollSlot[fdcount] = i; + if (inst->dtlsClients[i]->drainPending) { + pollTimeout = 0; + } + + /* A nonblocking DTLS handshake needs its retransmission timer + * serviced even if no socket becomes ready. */ + if (!SSL_is_init_finished(inst->dtlsClients[i]->sslClient)) { + struct timeval tv; + if (DTLSv1_get_timeout(inst->dtlsClients[i]->sslClient, &tv)) { + long long ms = (long long)tv.tv_sec * 1000 + (tv.tv_usec + 999) / 1000; + int delay = ms > INT_MAX ? INT_MAX : (int)ms; + if (pollTimeout < 0 || delay < pollTimeout) { + pollTimeout = delay; + } + } + } + long long idleSeconds = + (long long)inst->timeout - (long long)difftime(now, inst->dtlsClients[i]->lastActivityTime); + long long idleMs = idleSeconds <= 0 ? 0 : idleSeconds * 1000; + int idleDelay = idleMs > INT_MAX ? INT_MAX : (int)idleMs; + if (pollTimeout < 0 || idleDelay < pollTimeout) { + pollTimeout = idleDelay; + } } } DBGPRINTF("imdtls: Waiting for poll (clients %d) ...\n", fdcount); - ret = poll(fds, fdcount + 1, -1); + ret = poll(fds, fdcount + 1, pollTimeout); if (glbl.GetGlobalInputTermState() == 1) { DBGPRINTF("imdtls: DTLSHandleSessions Terminate State\n"); return; /* terminate input! */ @@ -716,14 +792,25 @@ static void DTLSHandleSessions(instanceConf_t *inst) { DBGPRINTF("imdtls: DTLSHandleSessions handle client sockets (%d) \n", fdcount); for (int i = 1; i <= fdcount; ++i) { if (clientIdxByPollSlot[i] >= 0) { - DTLSReadClient(inst, clientIdxByPollSlot[i], fds[i].revents); + int idx = clientIdxByPollSlot[i]; + short revents = fds[i].revents | (inst->dtlsClients[idx]->drainPending ? POLLIN : 0); + DTLSReadClient(inst, idx, revents); } } // Check session timeouts for (int i = 0; i < MAX_DTLS_CLIENTS; ++i) { if (inst->dtlsClients[i]->sslClient != NULL) { - if (difftime(time(NULL), inst->dtlsClients[i]->lastActivityTime) > inst->timeout) { + if (!SSL_is_init_finished(inst->dtlsClients[i]->sslClient)) { + struct timeval tv; + if (DTLSv1_get_timeout(inst->dtlsClients[i]->sslClient, &tv) && tv.tv_sec == 0 && tv.tv_usec == 0 && + DTLSv1_handle_timeout(inst->dtlsClients[i]->sslClient) < 0) { + DBGPRINTF("imdtls: DTLS retransmission failed for client index %d.\n", i); + DTLScleanupSession(inst, i); + continue; + } + } + if (difftime(time(NULL), inst->dtlsClients[i]->lastActivityTime) >= inst->timeout) { DBGPRINTF("imdtls: Session timeout (%d) for client index %d.\n", i, inst->timeout); DTLScleanupSession(inst, i); continue; @@ -832,12 +919,25 @@ static void DTLSHandleSessions(instanceConf_t *inst) { int addedClient = 0; BIO_ctrl_set_connected(rbio, client_addr); DBGPRINTF("imdtls: BIO_connect succeeded.\n"); + /* BIO_connect can reset the socket's blocking mode. Set + * O_NONBLOCK only after the BIO has been connected. */ + int flags = fcntl(clientfd, F_GETFL, 0); + if (flags < 0 || fcntl(clientfd, F_SETFL, flags | O_NONBLOCK) < 0) { + LogError(0, NO_ERRCODE, "imdtls: unable to make client socket non-blocking"); + close(clientfd); + SSL_free(ssl); + break; + } // Add to DTLS Clients for (int i = 0; i < MAX_DTLS_CLIENTS; ++i) { if (inst->dtlsClients[i]->sslClient == NULL) { inst->dtlsClients[i]->sslClient = ssl; inst->dtlsClients[i]->lastActivityTime = time(NULL); + inst->dtlsClients[i]->peerVerified = 0; + inst->dtlsClients[i]->verifiedRenegotiations = 0; + inst->dtlsClients[i]->pollEvents = POLLIN; + inst->dtlsClients[i]->drainPending = 0; DBGPRINTF("imdtls: New Client added at idx %d.\n", i); DTLSAcceptSession(inst, i); addedClient = 1; @@ -857,10 +957,11 @@ static void DTLSHandleSessions(instanceConf_t *inst) { /* Retry on EAGAIN (recv timeout, no datagram) and on ret == 0 * with an empty error queue (no usable handshake data yet). */ if (err == SSL_ERROR_SYSCALL && (errno == EAGAIN || (ret == 0 && ERR_peek_error() == 0))) { - DBGPRINTF("imdtls: DTLSv1_listen RET %d (ERR %d / ERRNO %d), retry\n", ret, err, errno); - // Wait little and retry DTLSv1_listen - srSleep(0, 100000); - continue; + DBGPRINTF("imdtls: DTLSv1_listen RET %d (ERR %d / ERRNO %d), defer\n", ret, err, errno); + /* DTLSv1_listen is stateless across the cookie exchange. + * Return to poll instead of waiting here for a second flight. */ + SSL_free(ssl); + break; } else { DBGPRINTF("imdtls: DTLSv1_listen RET %d (ERR %d / ERRNO %d), abort\n", ret, err, errno); net_ossl.osslLastOpenSSLErrorMsg(NULL, err, ssl, LOG_WARNING, "DTLSHandleSessions",