32 #define TRACE_LEVEL TLS_TRACE_LEVEL
47 #if (TLS_SUPPORT == ENABLED && DTLS_SUPPORT == ENABLED)
80 context->retransmitCount = 0;
95 context->retransmitCount = 0;
106 context->retransmitCount = 0;
114 context->txBufferLen = 0;
144 if(context->rxBufferLen > 0)
149 else if(context->rxRecordLen > 0)
165 else if(context->rxDatagramLen > 0)
183 error =
dtlsReadDatagram(context, context->rxBuffer + context->rxFragQueueLen,
184 context->rxBufferSize - context->rxFragQueueLen, &context->rxDatagramLen);
190 context->rxDatagramPos = context->rxBufferSize - context->rxDatagramLen;
193 osMemmove(context->rxBuffer + context->rxDatagramPos,
194 context->rxBuffer + context->rxFragQueueLen, context->rxDatagramLen);
202 #if (TLS_MAX_WARNING_ALERTS > 0)
206 context->alertCount = 0;
211 *
data = context->rxBuffer + context->rxBufferPos;
213 *
length = context->rxBufferLen;
215 *contentType = context->rxBufferType;
237 uint16_t legacyVersion;
245 if((context->txBufferLen +
n) > context->txBufferSize)
249 encryptionEngine = &context->encryptionEngine;
252 record = (
DtlsRecord *) (context->txBuffer + context->txBufferLen);
263 record->type = contentType;
264 record->version =
htons(legacyVersion);
265 record->epoch =
htons(encryptionEngine->epoch);
276 context->txBufferLen +=
n;
281 record->seqNum = encryptionEngine->dtlsSeqNum;
287 if((context->txBufferLen +
n) > context->txBufferSize)
292 encryptionEngine->hashAlgo != NULL)
315 error = context->socketSendCallback(context->socketHandle, record,
n, &
n, 0);
340 decryptionEngine = &context->decryptionEngine;
343 if(context->rxDatagramLen <
sizeof(
DtlsRecord))
346 context->rxDatagramLen = 0;
352 record = (
DtlsRecord *) (context->rxBuffer + context->rxDatagramPos);
354 recordLen =
ntohs(record->length);
357 if((recordLen +
sizeof(
DtlsRecord)) > context->rxDatagramLen)
360 context->rxDatagramLen = 0;
370 context->rxRecordPos = context->rxDatagramPos +
sizeof(
DtlsRecord);
373 context->rxDatagramPos += recordLen +
sizeof(
DtlsRecord);
374 context->rxDatagramLen -= recordLen +
sizeof(
DtlsRecord);
382 if(
ntohs(record->epoch) != context->decryptionEngine.epoch)
393 decryptionEngine->hashAlgo != NULL)
410 recordLen =
ntohs(record->length);
417 context->rxRecordVersion =
ntohs(record->version);
421 context->rxRecordLen = recordLen;
450 context->rxRecordLen = 0;
474 context->rxRecordLen = 0;
500 context->txMsgSeq = 0;
510 if(context->decryptionEngine.epoch == 0)
515 context->encryptionEngine.dtlsSeqNum = context->decryptionEngine.dtlsSeqNum;
537 if(context->txBufferLen > 0)
592 context->rxFragQueueLen = 0;
608 context->rxFragQueueLen = 0;
613 context->rxBufferPos = 0;
625 context->txBufferLen = 0;
629 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
636 context->txBufferLen = 0;
655 context->rxRecordLen = 0;
667 context->rxRecordLen = 0;
678 context->rxRecordLen = 0;
692 if(context->rxRecordLen <
sizeof(
TlsAlert))
695 context->rxRecordLen = 0;
707 context->txBufferLen = 0;
712 context->rxRecordLen = 0;
725 context->rxRecordLen = 0;
731 context->rxBufferLen = context->rxRecordLen;
733 context->rxBufferPos = 0;
736 osMemcpy(context->rxBuffer, context->rxBuffer + context->rxRecordPos,
737 context->rxRecordLen);
740 context->rxRecordLen = 0;
742 context->rxFragQueueLen = 0;
767 pmtu =
MIN(context->pmtu, context->txBufferSize - context->txBufferLen);
774 datagram = context->txBuffer + context->txBufferLen;
776 context->txDatagramLen = 0;
778 context->txBufferPos = 0;
782 while(context->txBufferPos < context->txBufferLen)
785 record = (
DtlsRecord *) (context->txBuffer + context->txBufferPos);
791 if(
ntohs(record->epoch) == context->encryptionEngine.epoch)
793 encryptionEngine = &context->encryptionEngine;
797 encryptionEngine = &context->prevEncryptionEngine;
816 if(context->txDatagramLen > 0)
824 if((context->txDatagramLen +
n) > pmtu)
828 context->txDatagramLen);
831 error = context->socketSendCallback(context->socketHandle,
832 datagram, context->txDatagramLen, &
n, 0);
838 context->txDatagramLen = 0;
848 if((context->txBufferLen + context->txDatagramLen +
n) > context->txBufferSize)
853 osMemcpy(datagram + context->txDatagramLen, record,
857 record = (
DtlsRecord *) (datagram + context->txDatagramLen);
861 record->seqNum = encryptionEngine->dtlsSeqNum;
865 encryptionEngine->hashAlgo != NULL)
887 if(context->txDatagramLen > 0)
891 context->txDatagramLen);
894 error = context->socketSendCallback(context->socketHandle, datagram,
895 context->txDatagramLen, &
n, 0);
901 context->txDatagramLen = 0;
907 context->retransmitCount++;
938 pmtu =
MIN(context->pmtu, context->txBufferSize - context->txBufferLen);
950 maxFragSize = pmtu -
n;
953 datagram = context->txBuffer + context->txBufferLen;
966 if(context->txDatagramLen > 0)
974 if((context->txDatagramLen +
n) > pmtu)
978 context->txDatagramLen);
981 error = context->socketSendCallback(context->socketHandle,
982 datagram, context->txDatagramLen, &
n, 0);
988 context->txDatagramLen = 0;
994 record = (
DtlsRecord *) (datagram + context->txDatagramLen);
999 record->epoch =
htons(encryptionEngine->epoch);
1000 record->seqNum = encryptionEngine->dtlsSeqNum;
1007 fragment->msgType =
message->msgType;
1011 fragment->msgSeq =
message->msgSeq;
1023 TRACE_DEBUG(
" msgType = %u\r\n", fragment->msgType);
1031 encryptionEngine->hashAlgo != NULL)
1075 size_t prevFragOffset;
1076 size_t prevFragLength;
1089 while(pos < context->rxFragQueueLen)
1095 if(
message->msgType != fragment->msgType)
1111 if((context->rxFragQueueLen +
fragLength) > (context->rxBufferSize - context->rxDatagramLen))
1119 context->rxFragQueueLen - pos);
1129 prevFragOffset =
LOAD24BE(prevFragment->fragOffset);
1131 prevFragLength =
LOAD24BE(prevFragment->fragLength);
1137 while(pos < context->rxFragQueueLen)
1148 if(
fragOffset <= (prevFragOffset + prevFragLength))
1165 prevFragLength +=
n;
1167 STORE24BE(prevFragLength, prevFragment->fragLength);
1188 prevFragment = fragment;
1209 size_t size,
size_t *
length)
1221 error = context->socketReceiveCallback(context->socketHandle,
data,
1299 if(context->txBufferLen > 0)
1306 context->retransmitTimeout) >= 0)
1318 context->retransmitTimeout =
MIN(context->retransmitTimeout * 2,
1346 seqNum->b[5] = temp & 0xFF;
1347 temp = (temp >> 8) +
seqNum->b[4];
1348 seqNum->b[4] = temp & 0xFF;
1349 temp = (temp >> 8) +
seqNum->b[3];
1350 seqNum->b[3] = temp & 0xFF;
1351 temp = (temp >> 8) +
seqNum->b[2];
1352 seqNum->b[2] = temp & 0xFF;
1353 temp = (temp >> 8) +
seqNum->b[1];
1354 seqNum->b[1] = temp & 0xFF;
1355 temp = (temp >> 8) +
seqNum->b[0];
1356 seqNum->b[0] = temp & 0xFF;