Skip to content

Commit c871332

Browse files
committed
Implement DTLSv1_get_timeout and DTLSv1_handle_timeout
wolfSSL_DTLSv1_get_timeout always returned 0 and reported the full timeout instead of the time left, and wolfSSL_DTLSv1_handle_timeout was a stub that did nothing. OpenSSL style event loops that wait for get_timeout and then call handle_timeout never retransmitted a lost flight. Reporting the time left needs to know when the flight went out, which the library did not record outside the non-blocking socket path in EmbedReceiveFrom. Add dtls_timer_start, set when a DTLS 1.2 message is stored for retransmission or the pool is resent, and when a DTLS 1.3 record is stored for retransmission or the buffered records are resent. It reads TimeNowInMilliseconds where the library builds it (TLS 1.3 tickets, PSK or DTLS 1.3) and LowResTimer otherwise. A whole second reading is rounded down by one second so the timer never expires early. A valid handshake message from the peer, which already resets the timeout, also restarts the timer while a flight waits, as the socket path re-arms its receive timeout on every read. A stateless server keeps no flight, so its state stays untouched. Dtls13RtxSendBuffered takes the timestamp after lastRtx, so the timer never expires inside DTLS13_MIN_RTX_INTERVAL of the last retransmission. get_timeout returns 1 with the time left while a flight waits for the peer and 0 otherwise. A DTLS 1.2 flight waits while it is stored and the handshake has not finished, a DTLS 1.3 flight while records are kept for retransmission. No timer runs while the object's own output is still pending or the handshake is suspended, so a blocked write or a slow asynchronous signature cannot trigger a retransmission of a partly sent flight. No timer runs once a fatal alert or close_notify closed the connection, so the stored flight is not resent after the alert. A timeout of 0 means no timeout, so no timer runs. handle_timeout returns 0 when no timer runs or it has not expired yet. Otherwise it calls wolfSSL_dtls_got_timeout and returns 1, or -1 when that fails, for example once the timeout reached its maximum. For DTLS 1.3 it clears the quick timeout flag first, since the full timeout expired, so the call sends any pending ACKs and retransmits instead of only sending the ACKs. Both return 0 when NO_ASN_TIME leaves no clock. The DTLS 1.3 quick timeout is not reported by get_timeout, so with these APIs the ACKs for a partly received flight go out with the next full timeout.
1 parent bd5eead commit c871332

7 files changed

Lines changed: 766 additions & 19 deletions

File tree

‎src/dtls13.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,8 +1039,10 @@ static int Dtls13SendOneFragmentRtx(WOLFSSL* ssl,
10391039
handshakeType, hashOutput, Dtls13SendNow(ssl, handshakeType));
10401040

10411041
if (rtxRecord != NULL) {
1042-
if (ret == 0 || ret == WC_NO_ERR_TRACE(WANT_WRITE))
1042+
if (ret == 0 || ret == WC_NO_ERR_TRACE(WANT_WRITE)) {
10431043
Dtls13RtxAddRecord(&ssl->dtls13Rtx, rtxRecord);
1044+
DtlsTimerStart(ssl);
1045+
}
10441046
else
10451047
Dtls13FreeRtxBufferRecord(ssl, rtxRecord);
10461048
}
@@ -1672,10 +1674,13 @@ static int Dtls13RtxSendBuffered(WOLFSSL* ssl)
16721674
#ifdef WOLFSSL_DEBUG_TLS
16731675
WOLFSSL_MSG("Avoid too fast retransmission");
16741676
#endif /* WOLFSSL_DEBUG_TLS */
1677+
DtlsTimerStart(ssl);
16751678
return 0;
16761679
}
16771680
ssl->dtls13Rtx.lastRtx = now;
16781681
#endif
1682+
/* Start the timer after lastRtx so it can't expire inside the interval. */
1683+
DtlsTimerStart(ssl);
16791684

16801685
r = ssl->dtls13Rtx.rtxRecords;
16811686
prevNext = &ssl->dtls13Rtx.rtxRecords;

‎src/internal.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12051,6 +12051,7 @@ int DtlsMsgPoolSave(WOLFSSL* ssl, const byte* data, word32 dataSz,
1205112051
cur->next = item;
1205212052
}
1205312053
ssl->dtls_tx_msg_list_sz++;
12054+
DtlsTimerStart(ssl);
1205412055
}
1205512056
else
1205612057
ret = MEMORY_E;
@@ -12073,6 +12074,37 @@ int DtlsMsgPoolTimeout(WOLFSSL* ssl)
1207312074
return result;
1207412075
}
1207512076

12077+
#ifndef NO_ASN_TIME
12078+
#if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || \
12079+
!defined(NO_PSK) || defined(WOLFSSL_DTLS13))
12080+
#define DTLS_TIMER_NOW() ((word32)TimeNowInMilliseconds())
12081+
#define DTLS_TIMER_RES 0
12082+
#else
12083+
#define DTLS_TIMER_NOW() (LowResTimer() * 1000)
12084+
#define DTLS_TIMER_RES 1000
12085+
#endif
12086+
12087+
/* Milliseconds since the flight was sent, rounded down to the clock tick. */
12088+
word32 DtlsTimerElapsedMs(const WOLFSSL* ssl)
12089+
{
12090+
word32 elapsed = DTLS_TIMER_NOW() - ssl->dtls_timer_start;
12091+
12092+
return (elapsed > DTLS_TIMER_RES) ? elapsed - DTLS_TIMER_RES : 0;
12093+
}
12094+
#endif /* !NO_ASN_TIME */
12095+
12096+
/* Start or restart the retransmission timer of the last flight. */
12097+
void DtlsTimerStart(WOLFSSL* ssl)
12098+
{
12099+
#ifndef NO_ASN_TIME
12100+
ssl->dtls_timer_start = DTLS_TIMER_NOW();
12101+
#else
12102+
(void)ssl;
12103+
#endif
12104+
}
12105+
#undef DTLS_TIMER_NOW
12106+
#undef DTLS_TIMER_RES
12107+
1207612108

1207712109
/* DtlsMsgPoolReset() deletes the stored transmit list. */
1207812110
void DtlsMsgPoolReset(WOLFSSL* ssl)
@@ -12283,6 +12315,8 @@ int DtlsMsgPoolSend(WOLFSSL* ssl, int sendOnlyFirstPacket)
1228312315

1228412316
if (ret == 0 && ssl->options.groupMessages)
1228512317
ret = SendBuffered(ssl);
12318+
if (ret == 0 || ret == WC_NO_ERR_TRACE(WANT_WRITE))
12319+
DtlsTimerStart(ssl);
1228612320
}
1228712321

1228812322
WOLFSSL_LEAVE("DtlsMsgPoolSend()", ret);
@@ -28298,6 +28332,8 @@ static int DoProcessReplyEx(WOLFSSL* ssl, int allowSocketErr)
2829828332
/* Reset timeout as we have received a valid
2829928333
* DTLS handshake message */
2830028334
ssl->dtls_timeout = ssl->dtls_timeout_init;
28335+
if (ssl->dtls_tx_msg_list != NULL)
28336+
DtlsTimerStart(ssl);
2830128337
}
2830228338
else if (DtlsShouldDropHandshake(ssl, ret)) {
2830328339
DropAndRestartProcessReply(ssl);
@@ -28344,6 +28380,8 @@ static int DoProcessReplyEx(WOLFSSL* ssl, int allowSocketErr)
2834428380
/* Reset timeout as we have received a valid
2834528381
* DTLS handshake message */
2834628382
ssl->dtls_timeout = ssl->dtls_timeout_init;
28383+
if (ssl->dtls13Rtx.rtxRecords != NULL)
28384+
DtlsTimerStart(ssl);
2834728385
}
2834828386
else if (DtlsShouldDropHandshake(ssl, ret)) {
2834928387
DropAndRestartProcessReply(ssl);

‎src/ssl_api_dtls.c‎

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,36 +1195,99 @@ void wolfSSL_dtls13_set_send_more_acks(WOLFSSL* ssl, int value)
11951195
}
11961196
#endif /* WOLFSSL_DTLS13 */
11971197

1198-
/* Get the time left until the next DTLS timeout.
1198+
/* Get the milliseconds left until the retransmission timer expires.
11991199
*
1200-
* @param [in] ssl SSL/TLS object.
1201-
* @param [out] timeleft Time left until the next timeout.
1202-
* @return 0 always.
1200+
* @param [in] ssl SSL/TLS object.
1201+
* @param [out] left Milliseconds left, 0 once the timer expired.
1202+
* @return 1 when the timer runs.
1203+
* @return 0 when no timer runs.
12031204
*/
1204-
int wolfSSL_DTLSv1_get_timeout(WOLFSSL* ssl, WOLFSSL_TIMEVAL* timeleft)
1205+
static int DtlsTimerLeft(WOLFSSL* ssl, word32* left)
12051206
{
1206-
if (ssl && timeleft) {
1207-
XMEMSET(timeleft, 0, sizeof(WOLFSSL_TIMEVAL));
1208-
timeleft->tv_sec = ssl->dtls_timeout;
1209-
}
1207+
#ifndef NO_ASN_TIME
1208+
int running;
1209+
word32 timeout;
1210+
word32 elapsed;
1211+
1212+
if (!IsDtlsNotSctpMode(ssl) || ssl->dtls_timeout <= 0 ||
1213+
ssl->options.isClosed || ssl->buffers.outputBuffer.length > 0)
1214+
return 0;
1215+
#ifdef WOLFSSL_HAVE_HS_SUSPEND
1216+
if (IsHsSuspendErr(ssl->error))
1217+
return 0;
1218+
#endif
1219+
#ifdef WOLFSSL_DTLS13
1220+
if (IsAtLeastTLSv1_3(ssl->version))
1221+
running = ssl->dtls13Rtx.rtxRecords != NULL;
1222+
else
1223+
#endif
1224+
running = ssl->options.handShakeState != HANDSHAKE_DONE &&
1225+
(ssl->dtls_tx_msg_list != NULL || ssl->dtls_tx_msg != NULL);
1226+
if (!running)
1227+
return 0;
1228+
1229+
if (ssl->dtls_timeout > (int)(WOLFSSL_MAX_32BIT / 1000))
1230+
timeout = WOLFSSL_MAX_32BIT;
1231+
else
1232+
timeout = (word32)ssl->dtls_timeout * 1000;
1233+
elapsed = DtlsTimerElapsedMs(ssl);
1234+
*left = (elapsed < timeout) ? timeout - elapsed : 0;
1235+
return 1;
1236+
#else
1237+
(void)ssl;
1238+
(void)left;
12101239
return 0;
1240+
#endif
12111241
}
12121242

1213-
#ifndef NO_WOLFSSL_STUB
1214-
/* Handle a DTLS timeout.
1243+
/* Get the time left until the DTLS retransmission timer expires.
12151244
*
1216-
* Not implemented - stub for OpenSSL compatibility.
1245+
* @param [in] ssl SSL/TLS object.
1246+
* @param [out] timeleft Time left, zero once the timer expired.
1247+
* @return 1 when the timer runs.
1248+
* @return 0 when no timer runs, or ssl or timeleft is NULL.
1249+
*/
1250+
int wolfSSL_DTLSv1_get_timeout(WOLFSSL* ssl, WOLFSSL_TIMEVAL* timeleft)
1251+
{
1252+
word32 left = 0;
1253+
1254+
if (ssl == NULL || timeleft == NULL)
1255+
return 0;
1256+
1257+
XMEMSET(timeleft, 0, sizeof(WOLFSSL_TIMEVAL));
1258+
if (!DtlsTimerLeft(ssl, &left))
1259+
return 0;
1260+
1261+
timeleft->tv_sec = left / 1000;
1262+
timeleft->tv_usec = (left % 1000) * 1000;
1263+
return 1;
1264+
}
1265+
1266+
/* Retransmit the last flight when the DTLS retransmission timer expired.
12171267
*
12181268
* @param [in] ssl SSL/TLS object.
1219-
* @return 0 always.
1269+
* @return 1 when the timer expired and the flight was retransmitted.
1270+
* @return 0 when no timer runs or it has not expired yet.
1271+
* @return WOLFSSL_FATAL_ERROR when ssl is NULL or retransmitting failed.
12201272
*/
12211273
int wolfSSL_DTLSv1_handle_timeout(WOLFSSL* ssl)
12221274
{
1223-
WOLFSSL_STUB("SSL_DTLSv1_handle_timeout");
1224-
(void)ssl;
1225-
return 0;
1275+
word32 left = 0;
1276+
1277+
if (ssl == NULL)
1278+
return WOLFSSL_FATAL_ERROR;
1279+
if (!DtlsTimerLeft(ssl, &left) || left > 0)
1280+
return 0;
1281+
#ifdef WOLFSSL_DTLS13
1282+
ssl->dtls13FastTimeout = 0;
1283+
#endif
1284+
if (wolfSSL_dtls_got_timeout(ssl) != WOLFSSL_SUCCESS)
1285+
return WOLFSSL_FATAL_ERROR;
1286+
1287+
return 1;
12261288
}
12271289

1290+
#ifndef NO_WOLFSSL_STUB
12281291
/* Set the initial DTLS timeout duration.
12291292
*
12301293
* Not implemented - stub for OpenSSL compatibility.

0 commit comments

Comments
 (0)