32 #define TRACE_LEVEL TLS_TRACE_LEVEL
47 #if (TLS_SUPPORT == ENABLED && DTLS_SUPPORT == ENABLED)
78 context->retransmitCount = 0;
93 context->retransmitCount = 0;
104 context->retransmitCount = 0;
112 context->txBufferLen = 0;
142 if(context->rxBufferLen > 0)
147 else if(context->rxRecordLen > 0)
163 else if(context->rxDatagramLen > 0)
181 error =
dtlsReadDatagram(context, context->rxBuffer + context->rxFragQueueLen,
182 context->rxBufferSize - context->rxFragQueueLen, &context->rxDatagramLen);
188 context->rxDatagramPos = context->rxBufferSize - context->rxDatagramLen;
191 osMemmove(context->rxBuffer + context->rxDatagramPos,
192 context->rxBuffer + context->rxFragQueueLen, context->rxDatagramLen);
200 #if (TLS_MAX_WARNING_ALERTS > 0)
203 context->alertCount = 0;
207 *
data = context->rxBuffer + context->rxBufferPos;
209 *
length = context->rxBufferLen;
211 *contentType = context->rxBufferType;
240 if((context->txBufferLen +
n) > context->txBufferSize)
244 encryptionEngine = &context->encryptionEngine;
247 record = (
DtlsRecord *) (context->txBuffer + context->txBufferLen);
253 record->type = contentType;
255 record->epoch =
htons(encryptionEngine->epoch);
266 context->txBufferLen +=
n;
271 record->seqNum = encryptionEngine->dtlsSeqNum;
277 if((context->txBufferLen +
n) > context->txBufferSize)
282 encryptionEngine->hashAlgo != NULL)
302 TRACE_INFO(
"Sending UDP datagram (%u bytes)...\r\n",
n);
305 error = context->socketSendCallback(context->socketHandle, record,
n, &
n, 0);
330 decryptionEngine = &context->decryptionEngine;
333 if(context->rxDatagramLen <
sizeof(
DtlsRecord))
336 context->rxDatagramLen = 0;
342 record = (
DtlsRecord *) (context->rxBuffer + context->rxDatagramPos);
344 recordLen =
ntohs(record->length);
347 if((recordLen +
sizeof(
DtlsRecord)) > context->rxDatagramLen)
350 context->rxDatagramLen = 0;
360 context->rxRecordPos = context->rxDatagramPos +
sizeof(
DtlsRecord);
363 context->rxDatagramPos += recordLen +
sizeof(
DtlsRecord);
364 context->rxDatagramLen -= recordLen +
sizeof(
DtlsRecord);
372 if(
ntohs(record->epoch) != context->decryptionEngine.epoch)
383 decryptionEngine->hashAlgo != NULL)
400 recordLen =
ntohs(record->length);
407 context->rxRecordVersion =
ntohs(record->version);
411 context->rxRecordLen = recordLen;
439 context->rxRecordLen = 0;
463 context->rxRecordLen = 0;
489 context->txMsgSeq = 0;
499 if(context->decryptionEngine.epoch == 0)
504 context->encryptionEngine.dtlsSeqNum = context->decryptionEngine.dtlsSeqNum;
526 if(context->txBufferLen > 0)
581 context->rxFragQueueLen = 0;
595 context->rxFragQueueLen = 0;
600 context->rxBufferPos = 0;
612 context->txBufferLen = 0;
625 context->rxRecordLen = 0;
637 context->rxRecordLen = 0;
648 context->rxRecordLen = 0;
662 if(context->rxRecordLen <
sizeof(
TlsAlert))
665 context->rxRecordLen = 0;
677 context->txBufferLen = 0;
682 context->rxRecordLen = 0;
695 context->rxRecordLen = 0;
701 context->rxBufferLen = context->rxRecordLen;
703 context->rxBufferPos = 0;
706 osMemcpy(context->rxBuffer, context->rxBuffer + context->rxRecordPos,
707 context->rxRecordLen);
710 context->rxRecordLen = 0;
712 context->rxFragQueueLen = 0;
737 pmtu =
MIN(context->pmtu, context->txBufferSize - context->txBufferLen);
744 datagram = context->txBuffer + context->txBufferLen;
746 context->txDatagramLen = 0;
748 context->txBufferPos = 0;
752 while(context->txBufferPos < context->txBufferLen)
755 record = (
DtlsRecord *) (context->txBuffer + context->txBufferPos);
761 if(
ntohs(record->epoch) == context->encryptionEngine.epoch)
763 encryptionEngine = &context->encryptionEngine;
767 encryptionEngine = &context->prevEncryptionEngine;
786 if(context->txDatagramLen > 0)
794 if((context->txDatagramLen +
n) > pmtu)
797 TRACE_INFO(
"Sending UDP datagram (%u bytes)...\r\n", context->txDatagramLen);
800 error = context->socketSendCallback(context->socketHandle,
801 datagram, context->txDatagramLen, &
n, 0);
807 context->txDatagramLen = 0;
817 if((context->txBufferLen + context->txDatagramLen +
n) > context->txBufferSize)
822 osMemcpy(datagram + context->txDatagramLen, record,
826 record = (
DtlsRecord *) (datagram + context->txDatagramLen);
830 record->seqNum = encryptionEngine->dtlsSeqNum;
834 encryptionEngine->hashAlgo != NULL)
856 if(context->txDatagramLen > 0)
859 TRACE_INFO(
"Sending UDP datagram (%u bytes)...\r\n", context->txDatagramLen);
862 error = context->socketSendCallback(context->socketHandle, datagram,
863 context->txDatagramLen, &
n, 0);
869 context->txDatagramLen = 0;
875 context->retransmitCount++;
906 pmtu =
MIN(context->pmtu, context->txBufferSize - context->txBufferLen);
918 maxFragSize = pmtu -
n;
921 datagram = context->txBuffer + context->txBufferLen;
934 if(context->txDatagramLen > 0)
942 if((context->txDatagramLen +
n) > pmtu)
945 TRACE_INFO(
"Sending UDP datagram (%u bytes)...\r\n", context->txDatagramLen);
948 error = context->socketSendCallback(context->socketHandle,
949 datagram, context->txDatagramLen, &
n, 0);
955 context->txDatagramLen = 0;
961 record = (
DtlsRecord *) (datagram + context->txDatagramLen);
966 record->epoch =
htons(encryptionEngine->epoch);
967 record->seqNum = encryptionEngine->dtlsSeqNum;
974 fragment->msgType =
message->msgType;
976 STORE24BE(totalLength, fragment->length);
978 fragment->msgSeq =
message->msgSeq;
990 TRACE_DEBUG(
" msgType = %u\r\n", fragment->msgType);
998 encryptionEngine->hashAlgo != NULL)
1042 size_t prevFragOffset;
1043 size_t prevFragLength;
1056 while(pos < context->rxFragQueueLen)
1062 if(
message->msgType != fragment->msgType)
1078 if((context->rxFragQueueLen +
fragLength) > (context->rxBufferSize - context->rxDatagramLen))
1086 context->rxFragQueueLen - pos);
1096 prevFragOffset =
LOAD24BE(prevFragment->fragOffset);
1098 prevFragLength =
LOAD24BE(prevFragment->fragLength);
1104 while(pos < context->rxFragQueueLen)
1115 if(
fragOffset <= (prevFragOffset + prevFragLength))
1132 prevFragLength +=
n;
1134 STORE24BE(prevFragLength, prevFragment->fragLength);
1155 prevFragment = fragment;
1176 size_t size,
size_t *
length)
1188 error = context->socketReceiveCallback(context->socketHandle,
data,
1266 if(context->txBufferLen > 0)
1273 context->retransmitTimeout) >= 0)
1285 context->retransmitTimeout =
MIN(context->retransmitTimeout * 2,
1313 seqNum->b[5] = temp & 0xFF;
1314 temp = (temp >> 8) +
seqNum->b[4];
1315 seqNum->b[4] = temp & 0xFF;
1316 temp = (temp >> 8) +
seqNum->b[3];
1317 seqNum->b[3] = temp & 0xFF;
1318 temp = (temp >> 8) +
seqNum->b[2];
1319 seqNum->b[2] = temp & 0xFF;
1320 temp = (temp >> 8) +
seqNum->b[1];
1321 seqNum->b[1] = temp & 0xFF;
1322 temp = (temp >> 8) +
seqNum->b[0];
1323 seqNum->b[0] = temp & 0xFF;