Go to the documentation of this file.
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
45 #if (TLS_SUPPORT == ENABLED)
57 context->state = newState;
60 if(context->stateChangeCallback != NULL)
63 context->stateChangeCallback(context, newState);
212 if(context->prngAlgo != NULL && context->prngContext != NULL)
216 error = context->prngAlgo->read(context->prngContext,
random, 32);
224 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
273 if(context->prngAlgo != NULL && context->prngContext != NULL)
277 error = context->prngAlgo->read(context->prngContext, context->sessionId,
284 context->sessionIdLen =
length;
313 if(
version >= context->versionMin && version <= context->versionMax)
359 if(context->numCipherSuites > 0)
362 for(i = 0; i < context->numCipherSuites; i++)
370 if(i >= context->numCipherSuites)
380 context->version, context->transportProtocol))
392 context->keyExchMethod =
cipherSuite->keyExchMethod;
396 if(context->cipherSuite.prfHashAlgo == NULL)
405 context->cipherSuite.verifyDataLen = 12;
431 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
437 if(context->sessionIdLen == 0)
441 if(context->cipherSuite.identifier == 0)
448 session->
version = context->version;
449 session->
cipherSuite = context->cipherSuite.identifier;
458 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
463 #if (TLS_SNI_SUPPORT == ENABLED)
466 context->serverName != NULL)
503 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
509 if(context->ticket == NULL || context->ticketLen == 0)
513 if(context->cipherSuite.identifier == 0)
517 session->
version = context->version;
518 session->
cipherSuite = context->cipherSuite.identifier;
523 if(session->
ticket == NULL)
533 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
557 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
571 context->version = session->
version;
572 context->cipherSuite.identifier = session->
cipherSuite;
573 context->sessionIdLen = 0;
582 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
606 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
620 context->version = session->
version;
621 context->cipherSuite.identifier = session->
cipherSuite;
622 context->sessionIdLen = 0;
625 if(context->ticket != NULL)
627 osMemset(context->ticket, 0, context->ticketLen);
629 context->ticket = NULL;
630 context->ticketLen = 0;
636 if(context->ticket == NULL)
646 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
684 encryptionEngine->version = context->version;
690 #if (DTLS_SUPPORT == ENABLED)
693 encryptionEngine->epoch++;
700 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
704 if(entity == context->entity)
706 encryptionEngine->recordSizeLimit = context->recordSizeLimit;
710 encryptionEngine->recordSizeLimit =
MIN(context->rxBufferMaxLen,
715 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
719 encryptionEngine->encryptThenMac = context->etmExtReceived;
724 encryptionEngine->macKeyLen =
cipherSuite->macKeyLen;
725 encryptionEngine->encKeyLen =
cipherSuite->encKeyLen;
726 encryptionEngine->fixedIvLen =
cipherSuite->fixedIvLen;
727 encryptionEngine->recordIvLen =
cipherSuite->recordIvLen;
728 encryptionEngine->authTagLen =
cipherSuite->authTagLen;
731 encryptionEngine->cipherAlgo =
cipherSuite->cipherAlgo;
732 encryptionEngine->cipherMode =
cipherSuite->cipherMode;
733 encryptionEngine->hashAlgo =
cipherSuite->hashAlgo;
736 encryptionEngine->cipherContext = NULL;
738 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
740 encryptionEngine->hmacContext = &context->hmacContext;
743 #if (TLS_GCM_CIPHER_SUPPORT == ENABLED)
745 encryptionEngine->gcmContext = NULL;
748 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
758 p = context->keyBlock;
795 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
856 if(encryptionEngine->cipherContext != NULL)
859 error = cipherAlgo->
init(encryptionEngine->cipherContext,
881 #if (TLS_GCM_CIPHER_SUPPORT == ENABLED)
892 if(encryptionEngine->gcmContext != NULL)
895 error =
gcmInit(encryptionEngine->gcmContext, cipherAlgo,
896 encryptionEngine->cipherContext);
920 if(encryptionEngine->cipherContext != NULL)
923 encryptionEngine->cipherAlgo->deinit(encryptionEngine->cipherContext);
927 encryptionEngine->cipherContext = NULL;
930 #if (TLS_GCM_CIPHER_SUPPORT == ENABLED)
932 if(encryptionEngine->gcmContext != NULL)
939 encryptionEngine->gcmContext = NULL;
944 encryptionEngine->cipherAlgo = NULL;
946 encryptionEngine->hashAlgo = NULL;
1032 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1033 TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1069 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1070 TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1117 static const char_t *
const label[] =
1180 #if (TLS_MD5_SUPPORT == ENABLED)
1186 #if (TLS_SHA1_SUPPORT == ENABLED)
1192 #if (TLS_SHA224_SUPPORT == ENABLED)
1198 #if (TLS_SHA256_SUPPORT == ENABLED)
1204 #if (TLS_SHA384_SUPPORT == ENABLED)
1210 #if (TLS_SHA512_SUPPORT == ENABLED)
1216 #if (TLS_SM3_SUPPORT == ENABLED)
1248 #if (TLS_ECDH_SUPPORT == ENABLED)
1252 #if (TLS_SECP160K1_SUPPORT == ENABLED)
1258 #if (TLS_SECP160R1_SUPPORT == ENABLED)
1264 #if (TLS_SECP160R2_SUPPORT == ENABLED)
1270 #if (TLS_SECP192K1_SUPPORT == ENABLED)
1276 #if (TLS_SECP192R1_SUPPORT == ENABLED)
1282 #if (TLS_SECP224K1_SUPPORT == ENABLED)
1288 #if (TLS_SECP224R1_SUPPORT == ENABLED)
1294 #if (TLS_SECP256K1_SUPPORT == ENABLED)
1300 #if (TLS_SECP256R1_SUPPORT == ENABLED)
1306 #if (TLS_SECP384R1_SUPPORT == ENABLED)
1312 #if (TLS_SECP521R1_SUPPORT == ENABLED)
1318 #if (TLS_BRAINPOOLP256R1_SUPPORT == ENABLED)
1325 #if (TLS_BRAINPOOLP384R1_SUPPORT == ENABLED)
1332 #if (TLS_BRAINPOOLP512R1_SUPPORT == ENABLED)
1339 #if (TLS_SM2_SUPPORT == ENABLED)
1345 #if (TLS_X25519_SUPPORT == ENABLED)
1351 #if (TLS_X448_SUPPORT == ENABLED)
1365 if(context->numSupportedGroups > 0)
1368 for(i = 0; i < context->numSupportedGroups; i++)
1371 if(context->supportedGroups[i] == namedCurve)
1376 if(i >= context->numSupportedGroups)
1401 #if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
1407 #if (TLS_SECP160K1_SUPPORT == ENABLED)
1414 #if (TLS_SECP160R1_SUPPORT == ENABLED)
1421 #if (TLS_SECP160R2_SUPPORT == ENABLED)
1428 #if (TLS_SECP192K1_SUPPORT == ENABLED)
1435 #if (TLS_SECP192R1_SUPPORT == ENABLED)
1442 #if (TLS_SECP224K1_SUPPORT == ENABLED)
1449 #if (TLS_SECP224R1_SUPPORT == ENABLED)
1456 #if (TLS_SECP256K1_SUPPORT == ENABLED)
1463 #if (TLS_SECP256R1_SUPPORT == ENABLED)
1470 #if (TLS_SECP384R1_SUPPORT == ENABLED)
1477 #if (TLS_SECP521R1_SUPPORT == ENABLED)
1484 #if (TLS_BRAINPOOLP256R1_SUPPORT == ENABLED)
1491 #if (TLS_BRAINPOOLP384R1_SUPPORT == ENABLED)
1498 #if (TLS_BRAINPOOLP512R1_SUPPORT == ENABLED)
1505 #if (TLS_SM2_SUPPORT == ENABLED)
1540 if(encryptionEngine->hashAlgo != NULL)
1541 n += encryptionEngine->hashAlgo->digestSize;
1549 n += encryptionEngine->recordIvLen;
1554 n += encryptionEngine->cipherAlgo->blockSize -
1555 ((
payloadLen +
n) % encryptionEngine->cipherAlgo->blockSize);
1561 n += encryptionEngine->recordIvLen + encryptionEngine->authTagLen;
1566 n += encryptionEngine->authTagLen;
1594 for(i = 0; i <
length && valid; i++)
1599 if(
name[i] ==
'-' ||
name[i] ==
'.')
1603 else if(
name[i] >=
'0' &&
name[i] <=
'9')
1607 else if(
name[i] >=
'A' &&
name[i] <=
'Z')
1611 else if(
name[i] >=
'a' &&
name[i] <=
'z')
1615 else if(
name[i] ==
'_')
@ TLS_GROUP_BRAINPOOLP512R1_TLS13
#define TLS_MAX_RECORD_LENGTH
#define tlsAllocMem(size)
size_t ticketLen
Length of the session ticket.
const uint8_t tls11DowngradeRandom[8]
@ TLS_ALERT_UNEXPECTED_MESSAGE
@ TLS_GROUP_BRAINPOOLP256R1_TLS13
uint16_t cipherSuite
Cipher suite identifier.
error_t tlsSaveSessionTicket(const TlsContext *context, TlsSessionState *session)
Save session ticket.
const EcCurveInfo * ecGetCurveInfo(const uint8_t *oid, size_t length)
Get the elliptic curve that matches the specified OID.
@ TLS_ALERT_CERTIFICATE_REQUIRED
const HashAlgo * tlsGetHashAlgo(TlsHashAlgo hashAlgoId)
Get the hash algorithm that matches the specified identifier.
const uint8_t X25519_OID[3]
Arbitrary precision integer.
error_t tlsGenerateSessionId(TlsContext *context, size_t length)
Generate a random session identifier.
@ TLS_ALERT_ILLEGAL_PARAMETER
@ ERROR_VERSION_NOT_SUPPORTED
uint8_t * ticket
Session ticket.
@ ERROR_UNKNOWN_CERTIFICATE
@ ERROR_ILLEGAL_PARAMETER
error_t ecImport(const EcDomainParameters *params, EcPoint *r, const uint8_t *data, size_t length)
Convert an octet string to an EC point.
@ ERROR_DECRYPTION_FAILED
uint8_t secret[TLS_MASTER_SECRET_SIZE]
Master secret (TLS 1.2) or ticket PSK (TLS 1.3)
@ TLS_ALERT_UNSUPPORTED_EXTENSION
@ ERROR_UNEXPECTED_MESSAGE
@ TLS_ALERT_RECORD_OVERFLOW
error_t tlsSelectVersion(TlsContext *context, uint16_t version)
Set the TLS version to be used.
TlsConnectionEnd
TLS connection end.
systime_t timestamp
Time stamp to manage entry lifetime.
const uint8_t SECP224R1_OID[5]
error_t tlsSaveSessionId(const TlsContext *context, TlsSessionState *session)
Save session ID.
const uint8_t BRAINPOOLP512R1_OID[9]
Structure describing a cipher suite.
const uint8_t SECP160K1_OID[5]
size_t sessionIdLen
Length of the session identifier.
const uint8_t SECP256K1_OID[5]
error_t tlsWriteMpi(const Mpi *a, uint8_t *data, size_t *length)
Encode a multiple precision integer to an opaque vector.
TlsHashAlgo
Hash algorithms.
const uint8_t BRAINPOOLP384R1_OID[9]
const uint8_t tls12DowngradeRandom[8]
__weak_func error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
@ ERROR_UNSUPPORTED_CERTIFICATE
@ TLS_ALERT_DECRYPT_ERROR
error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format)
Octet string to integer conversion.
@ TLS_GROUP_BRAINPOOLP256R1
const uint8_t SECP256R1_OID[8]
int_t oidComp(const uint8_t *oid1, size_t oidLen1, const uint8_t *oid2, size_t oidLen2)
Compare object identifiers.
const uint8_t SECP224K1_OID[5]
const uint8_t SECP521R1_OID[5]
uint16_t cipherSuite
Cipher suite identifier.
Elliptic curve parameters.
error_t tlsSendAlert(TlsContext *context, uint8_t level, uint8_t description)
Send Alert message.
error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format)
Integer to octet string conversion.
#define osMemcpy(dest, src, length)
@ ERROR_UNSUPPORTED_EXTENSION
@ TLS_ALERT_BAD_RECORD_MAC
@ TLS_CONNECTION_END_SERVER
void tlsFreeEncryptionEngine(TlsEncryptionEngine *encryptionEngine)
Release encryption engine.
@ ERROR_FAILURE
Generic error code.
error_t tlsSelectCipherSuite(TlsContext *context, uint16_t identifier)
Set cipher suite.
@ TLS_ALERT_UNSUPPORTED_CERTIFICATE
bool_t tlsCheckDnsHostname(const char_t *name, size_t length)
DNS hostname verification.
@ ERROR_MISSING_EXTENSION
Handshake message processing (TLS client and server)
error_t tlsWriteEcPoint(const EcDomainParameters *params, const EcPoint *a, uint8_t *data, size_t *length)
Encode an EC point to an opaque vector.
@ TLS_ALERT_MISSING_EXTENSION
const uint8_t SECP160R1_OID[5]
size_t tlsComputeEncryptionOverhead(TlsEncryptionEngine *encryptionEngine, size_t payloadLen)
Compute overhead caused by encryption.
TlsNamedGroup tlsGetNamedCurve(const uint8_t *oid, size_t length)
Get the named curve that matches the specified OID.
@ TLS_GROUP_BRAINPOOLP512R1
@ TLS_ALERT_PROTOCOL_VERSION
const uint8_t SECP192R1_OID[8]
#define TLS_MASTER_SECRET_SIZE
@ TLS_GROUP_BRAINPOOLP384R1_TLS13
const uint8_t SECP384R1_OID[5]
uint8_t secret[TLS_MASTER_SECRET_SIZE]
Master secret.
const uint8_t X448_OID[3]
bool_t tlsIsCipherSuiteAcceptable(const TlsCipherSuiteInfo *cipherSuite, uint16_t minVersion, uint16_t maxVersion, TlsTransportProtocol transportProtocol)
Check whether a cipher suite can be used with a given protocol version.
error_t tlsRestoreSessionId(TlsContext *context, const TlsSessionState *session)
Restore a TLS session using session ID.
char_t * serverName
ServerName extension.
const uint8_t SECP192K1_OID[5]
error_t tlsReadMpi(Mpi *a, const uint8_t *data, size_t size, size_t *length)
Read a multiple precision integer from an opaque vector.
@ TLS_ALERT_BAD_CERTIFICATE
@ TLS_ALERT_INAPPROPRIATE_FALLBACK
#define TRACE_DEBUG_ARRAY(p, a, n)
@ ERROR_NO_APPLICATION_PROTOCOL
uint8_t sessionId[32]
Session identifier.
@ ERROR_INAPPROPRIATE_FALLBACK
@ TLS_ALERT_CERTIFICATE_EXPIRED
const TlsCipherSuiteInfo tlsSupportedCipherSuites[]
@ ERROR_CERTIFICATE_EXPIRED
@ TLS_ALERT_NO_APPLICATION_PROTOCOL
@ TLS_CONNECTION_END_CLIENT
const EcCurveInfo * tlsGetCurveInfo(TlsContext *context, uint16_t namedCurve)
Get the EC domain parameters that match the specified named curve.
Common interface for encryption algorithms.
TLS (Transport Layer Security)
uint16_t version
TLS protocol version.
error_t ecExport(const EcDomainParameters *params, const EcPoint *a, uint8_t *data, size_t *length)
Convert an EC point to an octet string.
const uint8_t SECP160R2_OID[5]
Common interface for hash algorithms.
__weak_func error_t tlsInitEncryptionEngine(TlsContext *context, TlsEncryptionEngine *encryptionEngine, TlsConnectionEnd entity, const uint8_t *secret)
Initialize encryption engine.
@ CIPHER_MODE_CHACHA20_POLY1305
void tlsProcessError(TlsContext *context, error_t errorCode)
Translate an error code to an alert message.
error_t tls13HkdfExpandLabel(TlsTransportProtocol transportProtocol, const HashAlgo *hash, const uint8_t *secret, size_t secretLen, const char_t *label, const uint8_t *context, size_t contextLen, uint8_t *output, size_t outputLen)
HKDF-Expand-Label function.
const char_t * tlsGetVersionName(uint16_t version)
Convert TLS version to string representation.
uint_t tlsGetNumSupportedCipherSuites(void)
Determine the number of cipher suites supported.
void tlsChangeState(TlsContext *context, TlsState newState)
Update TLS state.
TlsNamedGroup
Named groups.
#define osMemset(p, value, length)
@ TLS_ALERT_HANDSHAKE_FAILURE
@ ERROR_CERTIFICATE_REQUIRED
error_t tlsGenerateRandomValue(TlsContext *context, uint8_t *random)
Generate client or server random value.
@ TLS_ALERT_INTERNAL_ERROR
@ ERROR_INVALID_SIGNATURE
#define TlsEncryptionEngine
bool_t extendedMasterSecret
Extended master secret computation.
error_t tlsReadEcPoint(const EcDomainParameters *params, EcPoint *a, const uint8_t *data, size_t size, size_t *length)
Read an EC point from an opaque vector.
@ TLS_GROUP_BRAINPOOLP384R1
@ TLS_ALERT_CERTIFICATE_UNKNOWN
const uint8_t BRAINPOOLP256R1_OID[9]
uint_t mpiGetByteLength(const Mpi *a)
Get the actual length in bytes.
systime_t osGetSystemTime(void)
Retrieve system time.
error_t tlsRestoreSessionTicket(TlsContext *context, const TlsSessionState *session)
Restore a TLS session using session ticket.