32 #define TRACE_LEVEL TLS_TRACE_LEVEL
46 #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)
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)
798 context->txDatagramLen);
801 error = context->socketSendCallback(context->socketHandle,
802 datagram, context->txDatagramLen, &
n, 0);
808 context->txDatagramLen = 0;
818 if((context->txBufferLen + context->txDatagramLen +
n) > context->txBufferSize)
823 osMemcpy(datagram + context->txDatagramLen, record,
827 record = (
DtlsRecord *) (datagram + context->txDatagramLen);
831 record->seqNum = encryptionEngine->dtlsSeqNum;
835 encryptionEngine->hashAlgo != NULL)
857 if(context->txDatagramLen > 0)
861 context->txDatagramLen);
864 error = context->socketSendCallback(context->socketHandle, datagram,
865 context->txDatagramLen, &
n, 0);
871 context->txDatagramLen = 0;
877 context->retransmitCount++;
908 pmtu =
MIN(context->pmtu, context->txBufferSize - context->txBufferLen);
920 maxFragSize = pmtu -
n;
923 datagram = context->txBuffer + context->txBufferLen;
936 if(context->txDatagramLen > 0)
944 if((context->txDatagramLen +
n) > pmtu)
948 context->txDatagramLen);
951 error = context->socketSendCallback(context->socketHandle,
952 datagram, context->txDatagramLen, &
n, 0);
958 context->txDatagramLen = 0;
964 record = (
DtlsRecord *) (datagram + context->txDatagramLen);
969 record->epoch =
htons(encryptionEngine->epoch);
970 record->seqNum = encryptionEngine->dtlsSeqNum;
977 fragment->msgType =
message->msgType;
981 fragment->msgSeq =
message->msgSeq;
993 TRACE_DEBUG(
" msgType = %u\r\n", fragment->msgType);
1001 encryptionEngine->hashAlgo != NULL)
1045 size_t prevFragOffset;
1046 size_t prevFragLength;
1059 while(pos < context->rxFragQueueLen)
1065 if(
message->msgType != fragment->msgType)
1081 if((context->rxFragQueueLen +
fragLength) > (context->rxBufferSize - context->rxDatagramLen))
1089 context->rxFragQueueLen - pos);
1099 prevFragOffset =
LOAD24BE(prevFragment->fragOffset);
1101 prevFragLength =
LOAD24BE(prevFragment->fragLength);
1107 while(pos < context->rxFragQueueLen)
1118 if(
fragOffset <= (prevFragOffset + prevFragLength))
1135 prevFragLength +=
n;
1137 STORE24BE(prevFragLength, prevFragment->fragLength);
1158 prevFragment = fragment;
1179 size_t size,
size_t *
length)
1191 error = context->socketReceiveCallback(context->socketHandle,
data,
1269 if(context->txBufferLen > 0)
1276 context->retransmitTimeout) >= 0)
1288 context->retransmitTimeout =
MIN(context->retransmitTimeout * 2,
1316 seqNum->b[5] = temp & 0xFF;
1317 temp = (temp >> 8) +
seqNum->b[4];
1318 seqNum->b[4] = temp & 0xFF;
1319 temp = (temp >> 8) +
seqNum->b[3];
1320 seqNum->b[3] = temp & 0xFF;
1321 temp = (temp >> 8) +
seqNum->b[2];
1322 seqNum->b[2] = temp & 0xFF;
1323 temp = (temp >> 8) +
seqNum->b[1];
1324 seqNum->b[1] = temp & 0xFF;
1325 temp = (temp >> 8) +
seqNum->b[0];
1326 seqNum->b[0] = temp & 0xFF;