32 #define TRACE_LEVEL TLS_TRACE_LEVEL
50 #if (TLS_SUPPORT == ENABLED && DTLS_SUPPORT == ENABLED)
103 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
127 context->retransmitCount = 0;
141 context->txBufferLen = 0;
144 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
147 context->numAckRecords = 0;
148 context->ackTimerRunning =
FALSE;
178 if(context->rxBufferLen > 0)
183 else if(context->rxRecordLen > 0)
199 else if(context->rxDatagramLen > 0)
201 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
228 error =
dtlsReadDatagram(context, context->rxBuffer + context->rxFragQueueLen,
229 context->rxBufferSize - context->rxFragQueueLen, &context->rxDatagramLen);
235 context->rxDatagramPos = context->rxBufferSize - context->rxDatagramLen;
238 osMemmove(context->rxBuffer + context->rxDatagramPos,
239 context->rxBuffer + context->rxFragQueueLen, context->rxDatagramLen);
247 #if (TLS_MAX_WARNING_ALERTS > 0)
251 context->alertCount = 0;
254 #if (TLS_MAX_KEY_UPDATE_MESSAGES > 0)
258 context->keyUpdateCount = 0;
263 *
data = context->rxBuffer + context->rxBufferPos;
265 *
length = context->rxBufferLen;
267 *contentType = context->rxBufferType;
289 uint16_t legacyVersion;
297 if((context->txBufferLen +
n) > context->txBufferSize)
300 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
306 encryptionEngine = &context->encryptionEngine[1];
311 encryptionEngine = &context->encryptionEngine[0];
315 record = (
DtlsRecord *) (context->txBuffer + context->txBufferLen);
326 record->type = contentType;
327 record->version =
htons(legacyVersion);
328 record->epoch =
htons(encryptionEngine->epoch);
339 context->txBufferLen +=
n;
344 record->seqNum = encryptionEngine->dtlsSeqNum;
348 encryptionEngine->hashAlgo != NULL)
350 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
359 if((context->txBufferLen +
n) > context->txBufferSize)
364 record->data,
ntohs(record->length), (uint8_t *) record, &
n);
377 if((context->txBufferLen +
n) > context->txBufferSize)
407 error = context->socketSendCallback(context->socketHandle, record,
n, &
n, 0);
432 decryptionEngine = &context->decryptionEngine[0];
435 if(context->rxDatagramLen <
sizeof(
DtlsRecord))
438 context->rxDatagramLen = 0;
444 record = (
DtlsRecord *) (context->rxBuffer + context->rxDatagramPos);
446 recordLen =
ntohs(record->length);
449 if((recordLen +
sizeof(
DtlsRecord)) > context->rxDatagramLen)
452 context->rxDatagramLen = 0;
462 context->rxRecordPos = context->rxDatagramPos +
sizeof(
DtlsRecord);
465 context->rxDatagramPos += recordLen +
sizeof(
DtlsRecord);
466 context->rxDatagramLen -= recordLen +
sizeof(
DtlsRecord);
474 if(
ntohs(record->epoch) != decryptionEngine->epoch)
478 if(context->replayDetectionEnabled)
489 decryptionEngine->hashAlgo != NULL)
506 recordLen =
ntohs(record->length);
513 context->rxRecordVersion =
ntohs(record->version);
517 context->rxRecordLen = recordLen;
521 context->rxRecordNum.epoch =
ntohs(record->epoch);
522 context->rxRecordNum.seqNum =
LOAD48BE(record->seqNum.b);
552 context->rxRecordLen = 0;
573 if(fragLen > context->rxRecordLen)
576 context->rxRecordLen = 0;
582 context->rxRecordPos += fragLen;
583 context->rxRecordLen -= fragLen;
602 context->txMsgSeq = 0;
612 if(context->decryptionEngine[0].epoch == 0)
617 context->encryptionEngine[0].dtlsSeqNum =
618 context->decryptionEngine[0].dtlsSeqNum;
627 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
639 if(context->rxRecordNum.epoch != 0)
648 if(context->rxRecordNum.epoch < 3)
655 if(context->rxRecordNum.epoch != 2)
671 context->txBufferLen = 0;
686 context->rxRecordNum.seqNum);
704 if(context->txBufferLen > 0)
758 context->rxFragQueueLen = 0;
774 context->rxFragQueueLen = 0;
779 context->rxBufferPos = 0;
794 context->txBufferLen = 0;
808 context->rxRecordLen = 0;
820 context->rxRecordLen = 0;
831 context->rxRecordLen = 0;
841 context->rxRecordLen = 0;
850 if(context->rxRecordLen <
sizeof(
TlsAlert))
853 context->rxRecordLen = 0;
871 context->txBufferLen = 0;
877 context->rxRecordLen = 0;
886 context->rxRecordLen = 0;
891 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
898 if(context->rxRecordNum.epoch < 3)
901 context->rxRecordLen = 0;
912 if(context->rxRecordLen <
sizeof(
Dtls13Ack))
915 context->rxRecordLen = 0;
924 context->rxRecordLen = 0;
930 context->rxBufferLen = context->rxRecordLen;
932 context->rxBufferPos = 0;
935 osMemcpy(context->rxBuffer, context->rxBuffer + context->rxRecordPos,
936 context->rxRecordLen);
939 context->rxRecordLen = 0;
941 context->rxFragQueueLen = 0;
967 pmtu =
MIN(context->pmtu, context->txBufferSize - context->txBufferLen);
974 datagram = context->txBuffer + context->txBufferLen;
976 context->txDatagramLen = 0;
978 context->txBufferPos = 0;
980 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
982 if(context->retransmitCount == 0)
985 osMemset(&context->encryptionEngine[0].retransmitState, 0,
988 osMemset(&context->encryptionEngine[1].retransmitState, 0,
991 osMemset(&context->encryptionEngine[2].retransmitState, 0,
996 context->encryptionEngine[0].retransmitState.index = 0;
997 context->encryptionEngine[1].retransmitState.index = 0;
998 context->encryptionEngine[2].retransmitState.index = 0;
1003 while(context->txBufferPos < context->txBufferLen)
1006 record = (
DtlsRecord *) (context->txBuffer + context->txBufferPos);
1016 if(context->encryptionEngine[i].epoch ==
ntohs(record->epoch))
1018 encryptionEngine = &context->encryptionEngine[i];
1024 if(encryptionEngine == NULL)
1027 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1029 if(context->retransmitCount == 0 ||
1036 if(encryptionEngine->retransmitState.index == 0)
1038 encryptionEngine->retransmitState.start =
1039 LOAD48BE(encryptionEngine->dtlsSeqNum.b);
1060 if(context->txDatagramLen > 0)
1068 if((context->txDatagramLen +
n) > pmtu)
1072 context->txDatagramLen);
1075 error = context->socketSendCallback(context->socketHandle,
1076 datagram, context->txDatagramLen, &
n, 0);
1082 context->txDatagramLen = 0;
1093 if((context->txBufferLen + context->txDatagramLen +
n) > context->txBufferSize)
1098 osMemcpy(datagram + context->txDatagramLen, record,
1102 record = (
DtlsRecord *) (datagram + context->txDatagramLen);
1106 record->seqNum = encryptionEngine->dtlsSeqNum;
1110 encryptionEngine->hashAlgo != NULL)
1132 if(context->txDatagramLen > 0)
1136 context->txDatagramLen);
1139 error = context->socketSendCallback(context->socketHandle, datagram,
1140 context->txDatagramLen, &
n, 0);
1146 context->txDatagramLen = 0;
1152 context->retransmitCount++;
1183 pmtu =
MIN(context->pmtu, context->txBufferSize - context->txBufferLen);
1185 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1189 encryptionEngine->hashAlgo != NULL))
1210 maxFragSize = pmtu -
n;
1213 datagram = context->txBuffer + context->txBufferLen;
1226 if(context->txDatagramLen > 0)
1228 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1232 encryptionEngine->hashAlgo != NULL))
1249 if((context->txDatagramLen +
n) > pmtu)
1253 context->txDatagramLen);
1256 error = context->socketSendCallback(context->socketHandle,
1257 datagram, context->txDatagramLen, &
n, 0);
1263 context->txDatagramLen = 0;
1269 record = (
DtlsRecord *) (datagram + context->txDatagramLen);
1274 encryptionEngine->hashAlgo != NULL))
1277 fragment = (
DtlsHandshake *) (datagram + context->txDatagramLen);
1284 record->epoch =
htons(encryptionEngine->epoch);
1285 record->seqNum = encryptionEngine->dtlsSeqNum;
1293 fragment->msgType =
message->msgType;
1297 fragment->msgSeq =
message->msgSeq;
1301 STORE24BE(fragLen, fragment->fragLength);
1309 TRACE_DEBUG(
" msgType = %u\r\n", fragment->msgType);
1317 encryptionEngine->hashAlgo != NULL)
1319 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1357 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1364 retransmitState = &encryptionEngine->retransmitState;
1367 if(context->retransmitCount == 0)
1370 retransmitState->
count++;
1372 retransmitState->
mask = (retransmitState->
mask << 1) | 1;
1378 if(retransmitState->
index < 32 &&
1379 (retransmitState->
mask & (1 << retransmitState->
index)) == 0)
1386 retransmitState->
index++;
1391 context->txDatagramLen +=
n;
1417 size_t prevFragOffset;
1431 while(pos < context->rxFragQueueLen)
1437 if(
message->msgType != fragment->msgType)
1453 if((context->rxFragQueueLen + fragLen) >
1454 (context->rxBufferSize - context->rxDatagramLen))
1463 osMemmove(context->rxBuffer + pos + fragLen, fragment,
1464 context->rxFragQueueLen - pos);
1469 context->rxFragQueueLen += fragLen;
1474 prevFragOffset =
LOAD24BE(prevFragment->fragOffset);
1476 prevFragLen =
LOAD24BE(prevFragment->fragLength);
1482 while(pos < context->rxFragQueueLen)
1489 fragLen =
LOAD24BE(fragment->fragLength);
1493 if(
fragOffset <= (prevFragOffset + prevFragLen))
1497 if((
fragOffset + fragLen) > (prevFragOffset + prevFragLen))
1505 n =
fragOffset + fragLen - prevFragOffset - prevFragLen;
1513 STORE24BE(prevFragLen, prevFragment->fragLength);
1521 osMemmove(fragment, fragment->data + fragLen,
1534 prevFragment = fragment;
1536 prevFragLen = fragLen;
1555 size_t size,
size_t *
length)
1567 error = context->socketReceiveCallback(context->socketHandle,
data,
1607 if((
time - context->startTime) >= context->timeout)
1644 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
1654 if(context->decryptionEngine[i].active)
1657 if(context->decryptionEngine[i].lifetime > 0)
1660 if((
time - context->decryptionEngine[i].timestamp) >=
1661 context->decryptionEngine[i].lifetime)
1664 context->decryptionEngine[i].epoch = 0;
1671 if(context->ackTimerRunning)
1678 if((
time - context->ackTimestamp) >= (context->retransmitTimeout / 4))
1683 context->ackTimerRunning =
FALSE;
1693 if(context->txBufferLen > 0)
1696 if((
time - context->retransmitTimestamp) >= context->retransmitTimeout)
1708 context->retransmitTimeout =
MIN(context->retransmitTimeout * 2,
1736 seqNum->b[5] = temp & 0xFF;
1737 temp = (temp >> 8) +
seqNum->b[4];
1738 seqNum->b[4] = temp & 0xFF;
1739 temp = (temp >> 8) +
seqNum->b[3];
1740 seqNum->b[3] = temp & 0xFF;
1741 temp = (temp >> 8) +
seqNum->b[2];
1742 seqNum->b[2] = temp & 0xFF;
1743 temp = (temp >> 8) +
seqNum->b[1];
1744 seqNum->b[1] = temp & 0xFF;
1745 temp = (temp >> 8) +
seqNum->b[0];
1746 seqNum->b[0] = temp & 0xFF;