32 #define TRACE_LEVEL TLS_TRACE_LEVEL
42 #if (TLS_SUPPORT == ENABLED)
67 if(context->txBufferLen == 0)
70 if(
length > context->txBufferMaxLen)
82 context->txBufferType = contentType;
84 context->txBufferLen =
length;
86 context->txBufferPos = 0;
94 else if(context->txBufferPos < context->txBufferLen)
97 n = context->txBufferLen - context->txBufferPos;
99 p = context->txBuffer + context->txBufferSize -
n;
104 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
106 n =
MIN(
n, context->maxFragLen);
109 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
114 context->encryptionEngine.hashAlgo != NULL)
119 n =
MIN(
n, context->encryptionEngine.recordSizeLimit);
129 context->txBufferPos +=
n;
135 context->txBufferLen = 0;
136 context->txBufferPos = 0;
172 if(context->rxBufferLen == 0)
176 context->rxBufferSize, &
n, &
type);
182 context->rxBufferType =
type;
184 context->rxBufferLen =
n;
186 context->rxBufferPos = 0;
193 if(context->rxBufferPos > 0)
196 osMemmove(context->rxBuffer, context->rxBuffer +
197 context->rxBufferPos, context->rxBufferLen);
200 context->rxBufferPos = 0;
204 error =
tlsReadRecord(context, context->rxBuffer + context->rxBufferLen,
205 context->rxBufferSize - context->rxBufferLen, &
n, &
type);
211 if(
type != context->rxBufferType)
221 context->rxBufferLen +=
n;
245 if(context->rxBufferLen <
n)
278 if(context->rxBufferLen <
sizeof(
TlsAlert))
295 n = context->rxBufferLen;
313 #if (TLS_MAX_WARNING_ALERTS > 0)
317 context->alertCount = 0;
320 #if (TLS_MAX_KEY_UPDATE_MESSAGES > 0)
324 context->keyUpdateCount = 0;
329 *
data = context->rxBuffer + context->rxBufferPos;
333 *contentType = context->rxBufferType;
355 uint16_t legacyVersion;
360 encryptionEngine = &context->encryptionEngine;
363 record = (
TlsRecord *) context->txBuffer;
372 if(context->txRecordLen == 0)
379 record->type = contentType;
380 record->version =
htons(legacyVersion);
392 encryptionEngine->hashAlgo != NULL)
404 context->txRecordPos = 0;
407 else if(context->txRecordPos < context->txRecordLen)
413 error = context->socketSendCallback(context->socketHandle,
414 context->txBuffer + context->txRecordPos,
415 context->txRecordLen - context->txRecordPos, &
n, 0);
421 context->txRecordPos +=
n;
432 context->txRecordLen = 0;
433 context->txRecordPos = 0;
472 if(context->rxRecordPos <
sizeof(
TlsRecord))
481 error = context->socketReceiveCallback(context->socketHandle,
482 data + context->rxRecordPos,
483 sizeof(
TlsRecord) - context->rxRecordPos, &
n, 0);
489 context->rxRecordPos +=
n;
492 if(context->rxRecordPos >=
sizeof(
TlsRecord))
514 else if(context->rxRecordPos < context->rxRecordLen)
517 if(size >= context->rxRecordLen)
523 error = context->socketReceiveCallback(context->socketHandle,
524 data + context->rxRecordPos,
525 context->rxRecordLen - context->rxRecordPos, &
n, 0);
531 context->rxRecordPos +=
n;
566 context->rxRecordLen = 0;
567 context->rxRecordPos = 0;
572 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
579 context->rxBufferLen == 0)
582 if(!context->updatedClientHelloReceived &&
583 context->earlyDataExtReceived)
586 context->earlyDataLen +=
ntohs(record->length);
590 if(context->earlyDataLen <= context->maxEarlyDataSize)
593 TRACE_INFO(
"Discarding early data (%" PRIu16
" bytes)...\r\n",
594 ntohs(record->length));
597 context->rxRecordLen = 0;
598 context->rxRecordPos = 0;
632 decryptionEngine = &context->decryptionEngine;
656 decryptionEngine->hashAlgo != NULL)
673 #if (TLS_MAX_CHANGE_CIPHER_SPEC_MESSAGES > 0)
675 context->changeCipherSpecCount = 0;
679 decryptionEngine->hashAlgo != NULL)
701 if(
ntohs(record->length) == 0)
710 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
712 if(context->recordSizeLimitExtReceived)
718 decryptionEngine->hashAlgo != NULL)
722 if(
ntohs(record->length) > decryptionEngine->recordSizeLimit)
728 #if (TLS_MAX_EMPTY_RECORDS > 0)
730 if(
ntohs(record->length) == 0)
733 context->emptyRecordCount++;
742 context->emptyRecordCount = 0;
760 #if (DTLS_SUPPORT == ENABLED)
788 #if (DTLS_SUPPORT == ENABLED)
817 #if (DTLS_SUPPORT == ENABLED)
845 #if (DTLS_SUPPORT == ENABLED)
876 #if (DTLS_SUPPORT == ENABLED)
906 const void *record, uint8_t *aad,
size_t *aadLen)
908 #if (DTLS_SUPPORT == ENABLED)
918 osMemcpy(aad, (
void *) &dtlsRecord->epoch, 2);
919 osMemcpy(aad + 2, &dtlsRecord->seqNum, 6);
920 osMemcpy(aad + 8, &dtlsRecord->type, 3);
921 osMemcpy(aad + 11, (
void *) &dtlsRecord->length, 2);
934 osMemcpy(aad, &encryptionEngine->seqNum, 8);
964 const void *record,
const uint8_t *recordIv, uint8_t *
nonce,
size_t *nonceLen)
970 if(encryptionEngine->recordIvLen != 0)
973 n = encryptionEngine->fixedIvLen + encryptionEngine->recordIvLen;
976 osMemcpy(
nonce, encryptionEngine->iv, encryptionEngine->fixedIvLen);
980 encryptionEngine->recordIvLen);
985 n = encryptionEngine->fixedIvLen;
987 #if (DTLS_SUPPORT == ENABLED)
1014 for(i = 0; i <
n; i++)
1016 nonce[i] ^= encryptionEngine->iv[i];
1036 seqNum->b[7] = temp & 0xFF;
1037 temp = (temp >> 8) +
seqNum->b[6];
1038 seqNum->b[6] = temp & 0xFF;
1039 temp = (temp >> 8) +
seqNum->b[5];
1040 seqNum->b[5] = temp & 0xFF;
1041 temp = (temp >> 8) +
seqNum->b[4];
1042 seqNum->b[4] = temp & 0xFF;
1043 temp = (temp >> 8) +
seqNum->b[3];
1044 seqNum->b[3] = temp & 0xFF;
1045 temp = (temp >> 8) +
seqNum->b[2];
1046 seqNum->b[2] = temp & 0xFF;
1047 temp = (temp >> 8) +
seqNum->b[1];
1048 seqNum->b[1] = temp & 0xFF;
1049 temp = (temp >> 8) +
seqNum->b[0];
1050 seqNum->b[0] = temp & 0xFF;