Go to the documentation of this file.
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
46 #if (TLS_SUPPORT == ENABLED)
58 context->state = newState;
61 if(context->stateChangeCallback != NULL)
64 context->stateChangeCallback(context, newState);
213 if(context->prngAlgo != NULL && context->prngContext != NULL)
217 error = context->prngAlgo->read(context->prngContext,
random, 32);
225 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
274 if(context->prngAlgo != NULL && context->prngContext != NULL)
278 error = context->prngAlgo->read(context->prngContext, context->sessionId,
285 context->sessionIdLen =
length;
314 if(
version >= context->versionMin && version <= context->versionMax)
360 if(context->numCipherSuites > 0)
363 for(i = 0; i < context->numCipherSuites; i++)
371 if(i >= context->numCipherSuites)
381 context->version, context->transportProtocol))
393 context->keyExchMethod =
cipherSuite->keyExchMethod;
397 if(context->cipherSuite.prfHashAlgo == NULL)
406 context->cipherSuite.verifyDataLen = 12;
432 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
438 if(context->sessionIdLen == 0)
442 if(context->cipherSuite.identifier == 0)
449 session->
version = context->version;
450 session->
cipherSuite = context->cipherSuite.identifier;
459 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
464 #if (TLS_SNI_SUPPORT == ENABLED)
467 context->serverName != NULL)
504 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
510 if(context->ticket == NULL || context->ticketLen == 0)
514 if(context->cipherSuite.identifier == 0)
518 session->
version = context->version;
519 session->
cipherSuite = context->cipherSuite.identifier;
524 if(session->
ticket == NULL)
534 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
558 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
572 context->version = session->
version;
573 context->cipherSuite.identifier = session->
cipherSuite;
574 context->sessionIdLen = 0;
583 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
607 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
621 context->version = session->
version;
622 context->cipherSuite.identifier = session->
cipherSuite;
623 context->sessionIdLen = 0;
626 if(context->ticket != NULL)
628 osMemset(context->ticket, 0, context->ticketLen);
630 context->ticket = NULL;
631 context->ticketLen = 0;
637 if(context->ticket == NULL)
647 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
685 encryptionEngine->version = context->version;
691 #if (DTLS_SUPPORT == ENABLED)
694 encryptionEngine->epoch++;
701 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
705 if(entity == context->entity)
707 encryptionEngine->recordSizeLimit = context->recordSizeLimit;
711 encryptionEngine->recordSizeLimit =
MIN(context->rxBufferMaxLen,
718 encryptionEngine->macKeyLen =
cipherSuite->macKeyLen;
719 encryptionEngine->encKeyLen =
cipherSuite->encKeyLen;
720 encryptionEngine->fixedIvLen =
cipherSuite->fixedIvLen;
721 encryptionEngine->recordIvLen =
cipherSuite->recordIvLen;
722 encryptionEngine->authTagLen =
cipherSuite->authTagLen;
725 encryptionEngine->cipherAlgo =
cipherSuite->cipherAlgo;
726 encryptionEngine->cipherMode =
cipherSuite->cipherMode;
727 encryptionEngine->hashAlgo =
cipherSuite->hashAlgo;
730 encryptionEngine->cipherContext = NULL;
732 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
734 encryptionEngine->hmacContext = &context->hmacContext;
737 #if (TLS_GCM_CIPHER_SUPPORT == ENABLED)
739 encryptionEngine->gcmContext = NULL;
742 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
752 p = context->keyBlock;
789 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
850 if(encryptionEngine->cipherContext != NULL)
853 error = cipherAlgo->
init(encryptionEngine->cipherContext,
875 #if (TLS_GCM_CIPHER_SUPPORT == ENABLED)
886 if(encryptionEngine->gcmContext != NULL)
889 error =
gcmInit(encryptionEngine->gcmContext, cipherAlgo,
890 encryptionEngine->cipherContext);
914 if(encryptionEngine->cipherContext != NULL)
917 encryptionEngine->cipherAlgo->deinit(encryptionEngine->cipherContext);
921 encryptionEngine->cipherContext = NULL;
924 #if (TLS_GCM_CIPHER_SUPPORT == ENABLED)
926 if(encryptionEngine->gcmContext != NULL)
933 encryptionEngine->gcmContext = NULL;
938 encryptionEngine->cipherAlgo = NULL;
940 encryptionEngine->hashAlgo = NULL;
1026 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1027 TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1063 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1064 TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1111 static const char_t *
const label[] =
1174 #if (TLS_MD5_SUPPORT == ENABLED)
1180 #if (TLS_SHA1_SUPPORT == ENABLED)
1186 #if (TLS_SHA224_SUPPORT == ENABLED)
1192 #if (TLS_SHA256_SUPPORT == ENABLED)
1198 #if (TLS_SHA384_SUPPORT == ENABLED)
1204 #if (TLS_SHA512_SUPPORT == ENABLED)
1236 #if (TLS_ECDH_SUPPORT == ENABLED)
1240 #if (TLS_SECP160K1_SUPPORT == ENABLED)
1246 #if (TLS_SECP160R1_SUPPORT == ENABLED)
1252 #if (TLS_SECP160R2_SUPPORT == ENABLED)
1258 #if (TLS_SECP192K1_SUPPORT == ENABLED)
1264 #if (TLS_SECP192R1_SUPPORT == ENABLED)
1270 #if (TLS_SECP224K1_SUPPORT == ENABLED)
1276 #if (TLS_SECP224R1_SUPPORT == ENABLED)
1282 #if (TLS_SECP256K1_SUPPORT == ENABLED)
1288 #if (TLS_SECP256R1_SUPPORT == ENABLED)
1294 #if (TLS_SECP384R1_SUPPORT == ENABLED)
1300 #if (TLS_SECP521R1_SUPPORT == ENABLED)
1306 #if (TLS_BRAINPOOLP256R1_SUPPORT == ENABLED)
1313 #if (TLS_BRAINPOOLP384R1_SUPPORT == ENABLED)
1320 #if (TLS_BRAINPOOLP512R1_SUPPORT == ENABLED)
1327 #if (TLS_X25519_SUPPORT == ENABLED)
1333 #if (TLS_X448_SUPPORT == ENABLED)
1347 if(context->numSupportedGroups > 0)
1350 for(i = 0; i < context->numSupportedGroups; i++)
1353 if(context->supportedGroups[i] == namedCurve)
1358 if(i >= context->numSupportedGroups)
1383 #if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
1389 #if (TLS_SECP160K1_SUPPORT == ENABLED)
1396 #if (TLS_SECP160R1_SUPPORT == ENABLED)
1403 #if (TLS_SECP160R2_SUPPORT == ENABLED)
1410 #if (TLS_SECP192K1_SUPPORT == ENABLED)
1417 #if (TLS_SECP192R1_SUPPORT == ENABLED)
1424 #if (TLS_SECP224K1_SUPPORT == ENABLED)
1431 #if (TLS_SECP224R1_SUPPORT == ENABLED)
1438 #if (TLS_SECP256K1_SUPPORT == ENABLED)
1445 #if (TLS_SECP256R1_SUPPORT == ENABLED)
1452 #if (TLS_SECP384R1_SUPPORT == ENABLED)
1459 #if (TLS_SECP521R1_SUPPORT == ENABLED)
1466 #if (TLS_BRAINPOOLP256R1_SUPPORT == ENABLED)
1473 #if (TLS_BRAINPOOLP384R1_SUPPORT == ENABLED)
1480 #if (TLS_BRAINPOOLP512R1_SUPPORT == ENABLED)
1515 if(encryptionEngine->hashAlgo != NULL)
1516 n += encryptionEngine->hashAlgo->digestSize;
1524 n += encryptionEngine->recordIvLen;
1529 n += encryptionEngine->cipherAlgo->blockSize -
1530 ((
payloadLen +
n) % encryptionEngine->cipherAlgo->blockSize);
1536 n += encryptionEngine->recordIvLen + encryptionEngine->authTagLen;
1541 n += encryptionEngine->authTagLen;
1569 for(i = 0; i <
length && valid; i++)
1574 if(
name[i] ==
'-' ||
name[i] ==
'.')
1578 else if(
name[i] >=
'0' &&
name[i] <=
'9')
1582 else if(
name[i] >=
'A' &&
name[i] <=
'Z')
1586 else if(
name[i] >=
'a' &&
name[i] <=
'z')
@ 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 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.
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.
const HashAlgo * tlsGetHashAlgo(uint8_t hashAlgoId)
Get the hash algorithm that matches the specified identifier.
@ ERROR_FAILURE
Generic error code.
error_t tlsSelectCipherSuite(TlsContext *context, uint16_t identifier)
Set cipher suite.
__start_packed struct @0 TlsSequenceNumber
Sequence number.
@ 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.
__start_packed struct @0 DtlsSequenceNumber
Sequence number.
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.