32 #define TRACE_LEVEL TLS_TRACE_LEVEL
43 #if (TLS_SUPPORT == ENABLED)
68 if(context->txBufferLen == 0)
71 if(
length > context->txBufferMaxLen)
83 context->txBufferType = contentType;
85 context->txBufferLen =
length;
87 context->txBufferPos = 0;
95 else if(context->txBufferPos < context->txBufferLen)
98 n = context->txBufferLen - context->txBufferPos;
100 p = context->txBuffer + context->txBufferSize -
n;
105 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
107 n =
MIN(
n, context->maxFragLen);
110 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
115 context->encryptionEngine.hashAlgo != NULL)
120 n =
MIN(
n, context->encryptionEngine.recordSizeLimit);
130 context->txBufferPos +=
n;
136 context->txBufferLen = 0;
137 context->txBufferPos = 0;
173 if(context->rxBufferLen == 0)
177 context->rxBufferSize, &
n, &
type);
183 context->rxBufferType =
type;
185 context->rxBufferLen =
n;
187 context->rxBufferPos = 0;
194 if(context->rxBufferPos > 0)
197 osMemmove(context->rxBuffer, context->rxBuffer +
198 context->rxBufferPos, context->rxBufferLen);
201 context->rxBufferPos = 0;
205 error =
tlsReadRecord(context, context->rxBuffer + context->rxBufferLen,
206 context->rxBufferSize - context->rxBufferLen, &
n, &
type);
212 if(
type != context->rxBufferType)
220 context->rxBufferLen +=
n;
244 if(context->rxBufferLen <
n)
277 if(context->rxBufferLen <
sizeof(
TlsAlert))
294 n = context->rxBufferLen;
312 #if (TLS_MAX_WARNING_ALERTS > 0)
315 context->alertCount = 0;
317 #if (TLS_MAX_KEY_UPDATE_MESSAGES > 0)
320 context->keyUpdateCount = 0;
324 *
data = context->rxBuffer + context->rxBufferPos;
328 *contentType = context->rxBufferType;
350 uint16_t legacyVersion;
355 encryptionEngine = &context->encryptionEngine;
358 record = (
TlsRecord *) context->txBuffer;
367 if(context->txRecordLen == 0)
374 record->type = contentType;
375 record->version =
htons(legacyVersion);
387 encryptionEngine->hashAlgo != NULL)
399 context->txRecordPos = 0;
402 else if(context->txRecordPos < context->txRecordLen)
408 error = context->socketSendCallback(context->socketHandle,
409 context->txBuffer + context->txRecordPos,
410 context->txRecordLen - context->txRecordPos, &
n, 0);
416 context->txRecordPos +=
n;
427 context->txRecordLen = 0;
428 context->txRecordPos = 0;
467 if(context->rxRecordPos <
sizeof(
TlsRecord))
476 error = context->socketReceiveCallback(context->socketHandle,
477 data + context->rxRecordPos,
478 sizeof(
TlsRecord) - context->rxRecordPos, &
n, 0);
484 context->rxRecordPos +=
n;
487 if(context->rxRecordPos >=
sizeof(
TlsRecord))
509 else if(context->rxRecordPos < context->rxRecordLen)
512 if(size >= context->rxRecordLen)
518 error = context->socketReceiveCallback(context->socketHandle,
519 data + context->rxRecordPos,
520 context->rxRecordLen - context->rxRecordPos, &
n, 0);
526 context->rxRecordPos +=
n;
561 context->rxRecordLen = 0;
562 context->rxRecordPos = 0;
567 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
574 context->rxBufferLen == 0)
577 if(!context->updatedClientHelloReceived &&
578 context->earlyDataExtReceived)
581 context->earlyDataLen +=
ntohs(record->length);
585 if(context->earlyDataLen <= context->maxEarlyDataSize)
588 TRACE_INFO(
"Discarding early data (%" PRIu16
" bytes)...\r\n",
589 ntohs(record->length));
592 context->rxRecordLen = 0;
593 context->rxRecordPos = 0;
627 decryptionEngine = &context->decryptionEngine;
651 decryptionEngine->hashAlgo != NULL)
668 #if (TLS_MAX_CHANGE_CIPHER_SPEC_MESSAGES > 0)
670 context->changeCipherSpecCount = 0;
674 decryptionEngine->hashAlgo != NULL)
696 if(
ntohs(record->length) == 0)
705 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
707 if(context->recordSizeLimitExtReceived)
713 decryptionEngine->hashAlgo != NULL)
717 if(
ntohs(record->length) > decryptionEngine->recordSizeLimit)
723 #if (TLS_MAX_EMPTY_RECORDS > 0)
725 if(
ntohs(record->length) == 0)
728 context->emptyRecordCount++;
737 context->emptyRecordCount = 0;
755 #if (DTLS_SUPPORT == ENABLED)
783 #if (DTLS_SUPPORT == ENABLED)
812 #if (DTLS_SUPPORT == ENABLED)
840 #if (DTLS_SUPPORT == ENABLED)
871 #if (DTLS_SUPPORT == ENABLED)
901 const void *record, uint8_t *aad,
size_t *aadLen)
903 #if (DTLS_SUPPORT == ENABLED)
913 osMemcpy(aad, (
void *) &dtlsRecord->epoch, 2);
914 osMemcpy(aad + 2, &dtlsRecord->seqNum, 6);
915 osMemcpy(aad + 8, &dtlsRecord->type, 3);
916 osMemcpy(aad + 11, (
void *) &dtlsRecord->length, 2);
929 osMemcpy(aad, &encryptionEngine->seqNum, 8);
959 const void *record,
const uint8_t *recordIv, uint8_t *nonce,
size_t *nonceLen)
965 if(encryptionEngine->recordIvLen != 0)
968 n = encryptionEngine->fixedIvLen + encryptionEngine->recordIvLen;
971 osMemcpy(nonce, encryptionEngine->iv, encryptionEngine->fixedIvLen);
974 osMemcpy(nonce + encryptionEngine->fixedIvLen, recordIv,
975 encryptionEngine->recordIvLen);
980 n = encryptionEngine->fixedIvLen;
982 #if (DTLS_SUPPORT == ENABLED)
993 osMemcpy(nonce +
n - 8, (
void *) &dtlsRecord->epoch, 2);
994 osMemcpy(nonce +
n - 6, &dtlsRecord->seqNum, 6);
1002 osMemcpy(nonce +
n - 8, &encryptionEngine->seqNum, 8);
1009 for(i = 0; i <
n; i++)
1011 nonce[i] ^= encryptionEngine->iv[i];
1031 seqNum->b[7] = temp & 0xFF;
1032 temp = (temp >> 8) +
seqNum->b[6];
1033 seqNum->b[6] = temp & 0xFF;
1034 temp = (temp >> 8) +
seqNum->b[5];
1035 seqNum->b[5] = temp & 0xFF;
1036 temp = (temp >> 8) +
seqNum->b[4];
1037 seqNum->b[4] = temp & 0xFF;
1038 temp = (temp >> 8) +
seqNum->b[3];
1039 seqNum->b[3] = temp & 0xFF;
1040 temp = (temp >> 8) +
seqNum->b[2];
1041 seqNum->b[2] = temp & 0xFF;
1042 temp = (temp >> 8) +
seqNum->b[1];
1043 seqNum->b[1] = temp & 0xFF;
1044 temp = (temp >> 8) +
seqNum->b[0];
1045 seqNum->b[0] = temp & 0xFF;