From 3a881079d38f53f62cf4553dad6183aa6869ea34 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Mon, 11 Sep 2023 16:20:28 +0200 Subject: [PATCH] Fix async --- src/dtls13.c | 27 ++++++++-------- src/internal.c | 86 ++++++++++++++++++++++++++------------------------ 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/dtls13.c b/src/dtls13.c index 3ea608215..2a2e543cd 100644 --- a/src/dtls13.c +++ b/src/dtls13.c @@ -2404,7 +2404,11 @@ static int Dtls13WriteAckMessage(WOLFSSL* ssl, c16toa(msgSz, ackMessage); ackMessage += OPAQUE16_LEN; + WOLFSSL_MSG("write ack records"); + while (recordNumberList != NULL) { + WOLFSSL_MSG_EX("epoch %d seq %d", recordNumberList->epoch, + recordNumberList->seq); c64toa(&recordNumberList->epoch, ackMessage); ackMessage += OPAQUE64_LEN; c64toa(&recordNumberList->seq, ackMessage); @@ -2596,10 +2600,13 @@ int DoDtls13Ack(WOLFSSL* ssl, const byte* input, word32 inputSize, if (length % (DTLS13_RN_SIZE) != 0) return PARSE_ERROR; + WOLFSSL_MSG("read ack records"); + ackMessage = input + OPAQUE16_LEN; for (i = 0; i < length; i += DTLS13_RN_SIZE) { ato64(ackMessage + i, &epoch); ato64(ackMessage + i + OPAQUE64_LEN, &seq); + WOLFSSL_MSG_EX("epoch %d seq %d", epoch, seq); Dtls13RtxRemoveRecord(ssl, epoch, seq); } @@ -2670,14 +2677,13 @@ int SendDtls13Ack(WOLFSSL* ssl) if (ret != 0) return ret; + ret = Dtls13WriteAckMessage(ssl, ssl->dtls13Rtx.seenRecords, &length); + if (ret != 0) + return ret; + + output = GetOutputBuffer(ssl); + if (w64IsZero(ssl->dtls13EncryptEpoch->epochNumber)) { - - ret = Dtls13WriteAckMessage(ssl, ssl->dtls13Rtx.seenRecords, &length); - if (ret != 0) - return ret; - - output = GetOutputBuffer(ssl); - ret = Dtls13RlAddPlaintextHeader(ssl, output, ack, (word16)length); if (ret != 0) return ret; @@ -2685,13 +2691,6 @@ int SendDtls13Ack(WOLFSSL* ssl) ssl->buffers.outputBuffer.length += length + DTLS_RECORD_HEADER_SZ; } else { - - ret = Dtls13WriteAckMessage(ssl, ssl->dtls13Rtx.seenRecords, &length); - if (ret != 0) - return ret; - - output = GetOutputBuffer(ssl); - outputSize = ssl->buffers.outputBuffer.bufferSize - ssl->buffers.outputBuffer.idx - ssl->buffers.outputBuffer.length; diff --git a/src/internal.c b/src/internal.c index dd4c26e40..138d65667 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17040,8 +17040,23 @@ static int _DtlsUpdateWindow(WOLFSSL* ssl) next_hi, next_lo, window); } +static WC_INLINE int DtlsShouldUpdateWindow(int ret) +{ + switch (ret) { + case 0: +#ifdef WOLFSSL_ASYNC_CRYPT + case WC_PENDING_E: +#endif + case APP_DATA_READY: + return 1; + default: + return 0; + } +} + #ifdef WOLFSSL_DTLS13 -static WC_INLINE int Dtls13UpdateWindow(WOLFSSL* ssl) + +static int Dtls13UpdateWindow(WOLFSSL* ssl) { w64wrapper nextSeq, seq; w64wrapper diff64; @@ -17104,6 +17119,14 @@ static WC_INLINE int Dtls13UpdateWindow(WOLFSSL* ssl) return 0; } + +static WC_INLINE int Dtls13UpdateWindowRecordRecvd(WOLFSSL* ssl) +{ + int ret = Dtls13UpdateWindow(ssl); + if (ret != 0) + return ret; + return Dtls13RecordRecvd(ssl); +} #endif /* WOLFSSL_DTLS13 */ int DtlsMsgDrain(WOLFSSL* ssl) @@ -20805,7 +20828,8 @@ default: ssl->buffers.inputBuffer.buffer, &ssl->buffers.inputBuffer.idx, ssl->buffers.inputBuffer.length); - if (ret == 0 && ssl->options.dtlsStateful) { + if (DtlsShouldUpdateWindow(ret) && + ssl->options.dtlsStateful) { if (IsDtlsNotSctpMode(ssl)) _DtlsUpdateWindow(ssl); /* Reset timeout as we have received a valid @@ -20826,16 +20850,13 @@ default: ssl->buffers.inputBuffer.buffer, &ssl->buffers.inputBuffer.idx, ssl->buffers.inputBuffer.length); - if (ret == 0 && ssl->options.dtlsStateful) { - ret = Dtls13UpdateWindow(ssl); - if (ret != 0) { - WOLFSSL_ERROR(ret); - return ret; - } - ret = Dtls13RecordRecvd(ssl); - if (ret != 0) { - WOLFSSL_ERROR(ret); - return ret; + if (DtlsShouldUpdateWindow(ret) && + ssl->options.dtlsStateful) { + int updateRet = + Dtls13UpdateWindowRecordRecvd(ssl); + if (updateRet != 0) { + WOLFSSL_ERROR(updateRet); + return updateRet; } } #ifdef WOLFSSL_EARLY_DATA @@ -20960,12 +20981,7 @@ default: } #ifdef WOLFSSL_DTLS13 if (ssl->options.dtls) { - ret = Dtls13UpdateWindow(ssl); - if (ret != 0) { - WOLFSSL_ERROR(ret); - return ret; - } - ret = Dtls13RecordRecvd(ssl); + ret = Dtls13UpdateWindowRecordRecvd(ssl); if (ret != 0) { WOLFSSL_ERROR(ret); return ret; @@ -21126,16 +21142,10 @@ default: ssl->buffers.inputBuffer.buffer, &ssl->buffers.inputBuffer.idx, NO_SNIFF); #ifdef WOLFSSL_DTLS - if (ssl->options.dtls && - (ret == 0 || ret == APP_DATA_READY)) { + if (ssl->options.dtls && DtlsShouldUpdateWindow(ret)) { #ifdef WOLFSSL_DTLS13 if (IsAtLeastTLSv1_3(ssl->version)) { - int updateRet = Dtls13UpdateWindow(ssl); - if (updateRet != 0) { - WOLFSSL_ERROR(updateRet); - return updateRet; - } - updateRet = Dtls13RecordRecvd(ssl); + int updateRet = Dtls13UpdateWindowRecordRecvd(ssl); if (updateRet != 0) { WOLFSSL_ERROR(updateRet); return updateRet; @@ -21180,12 +21190,7 @@ default: if (ssl->options.dtls) { #ifdef WOLFSSL_DTLS13 if (IsAtLeastTLSv1_3(ssl->version)) { - ret = Dtls13UpdateWindow(ssl); - if (ret != 0) { - WOLFSSL_ERROR(ret); - return ret; - } - ret = Dtls13RecordRecvd(ssl); + ret = Dtls13UpdateWindowRecordRecvd(ssl); if (ret != 0) { WOLFSSL_ERROR(ret); return ret; @@ -21211,18 +21216,15 @@ default: ssl->keys.padSz, &processedSize); ssl->buffers.inputBuffer.idx += processedSize; ssl->buffers.inputBuffer.idx += ssl->keys.padSz; + if (DtlsShouldUpdateWindow(ret)) { + int updateRet = Dtls13UpdateWindowRecordRecvd(ssl); + if (updateRet != 0) { + WOLFSSL_ERROR(updateRet); + return updateRet; + } + } if (ret != 0) return ret; - ret = Dtls13UpdateWindow(ssl); - if (ret != 0) { - WOLFSSL_ERROR(ret); - return ret; - } - ret = Dtls13RecordRecvd(ssl); - if (ret != 0) { - WOLFSSL_ERROR(ret); - return ret; - } break; } FALL_THROUGH;