Skip to content

Commit 49c15c3

Browse files
committed
crypto/mbedtls: Add support for mbedtls 3.x
- mbedtls 2.8.x is getting out of support: https://github.com/Mbed-TLS/mbedtls/releases/tag/mbedtls-2.28.10 - Clone mbedtls 3.6.x instead of 2.8.x via CMake dependencies - Add related code to mbedtls usage keeping the 2.8.x support intact under mbedtsl version macros
1 parent cf817bc commit 49c15c3

File tree

6 files changed

+123
-5
lines changed

6 files changed

+123
-5
lines changed

CMake/Dependencies/libmbedtls-CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ message(STATUS "C flags here are ${CMAKE_C_FLAGS}")
2121
ExternalProject_Add(
2222
project_libmbedtls
2323
GIT_REPOSITORY https://github.com/ARMmbed/mbedtls.git
24-
GIT_TAG v2.28.8
24+
GIT_TAG v3.6.0
2525
GIT_PROGRESS TRUE
2626
GIT_SHALLOW TRUE
2727
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/build

src/source/Crypto/Crypto.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ typedef enum {
3232
KVS_SRTP_PROFILE_AES128_CM_HMAC_SHA1_32 = SRTP_AES128_CM_SHA1_32,
3333
} KVS_SRTP_PROFILE;
3434
#elif KVS_USE_MBEDTLS
35-
#define KVS_RSA_F4 0x10001L
36-
#define KVS_MD5_DIGEST_LENGTH 16
37-
#define KVS_SHA1_DIGEST_LENGTH 20
35+
#define KVS_RSA_F4 0x10001L
36+
#define KVS_MD5_DIGEST_LENGTH 16
37+
#define KVS_SHA1_DIGEST_LENGTH 20
38+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
39+
#define KVS_MD5_DIGEST(m, mlen, ob) mbedtls_md5((m), (mlen), (ob));
40+
#else
3841
#define KVS_MD5_DIGEST(m, mlen, ob) mbedtls_md5_ret((m), (mlen), (ob));
42+
#endif
3943
#define KVS_SHA1_HMAC(k, klen, m, mlen, ob, plen) \
4044
CHK(0 == mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), (k), (klen), (m), (mlen), (ob)), STATUS_HMAC_GENERATION_ERROR); \
4145
*(plen) = mbedtls_md_get_size(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1));

src/source/Crypto/Dtls.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,17 @@ INT32 dtlsSessionSendCallback(PVOID, const unsigned char*, ULONG);
208208
INT32 dtlsSessionReceiveCallback(PVOID, unsigned char*, ULONG);
209209
VOID dtlsSessionSetTimerCallback(PVOID, UINT32, UINT32);
210210
INT32 dtlsSessionGetTimerCallback(PVOID);
211+
212+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
213+
VOID dtlsSessionKeyDerivationCallback(PVOID customData, mbedtls_ssl_key_export_type secret_type, const unsigned char* pMasterSecret,
214+
SIZE_T pMasterSecretLen, const unsigned char clientRandom[MAX_DTLS_RANDOM_BYTES_LEN],
215+
const unsigned char serverRandom[MAX_DTLS_RANDOM_BYTES_LEN], mbedtls_tls_prf_types tlsProfile);
216+
#else
211217
INT32 dtlsSessionKeyDerivationCallback(PVOID, const unsigned char*, const unsigned char*, ULONG, ULONG, ULONG,
212218
const unsigned char[MAX_DTLS_RANDOM_BYTES_LEN], const unsigned char[MAX_DTLS_RANDOM_BYTES_LEN],
213219
mbedtls_tls_prf_types);
220+
#endif
221+
214222
#else
215223
#error "A Crypto implementation is required."
216224
#endif

src/source/Crypto/Dtls_mbedtls.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ mbedtls_ssl_srtp_profile DTLS_SRTP_SUPPORTED_PROFILES[] = {
88
MBEDTLS_TLS_SRTP_UNSET,
99
};
1010

11+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
12+
static mbedtls_ctr_drbg_context ctr_drbg;
13+
static mbedtls_entropy_context entropy;
14+
#endif
15+
1116
STATUS createDtlsSession(PDtlsSessionCallbacks pDtlsSessionCallbacks, TIMER_QUEUE_HANDLE timerQueueHandle, INT32 certificateBits,
1217
BOOL generateRSACertificate, PRtcCertificate pRtcCertificates, PDtlsSession* ppDtlsSession)
1318
{
@@ -229,6 +234,32 @@ STATUS dtlsTransmissionTimerCallback(UINT32 timerID, UINT64 currentTime, UINT64
229234
return retStatus;
230235
}
231236

237+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
238+
VOID dtlsSessionKeyDerivationCallback(PVOID customData, mbedtls_ssl_key_export_type secret_type, const unsigned char* pMasterSecret,
239+
SIZE_T pMasterSecretLen, const unsigned char clientRandom[MAX_DTLS_RANDOM_BYTES_LEN],
240+
const unsigned char serverRandom[MAX_DTLS_RANDOM_BYTES_LEN], mbedtls_tls_prf_types tlsProfile)
241+
{
242+
ENTERS();
243+
244+
/* We're only interested in the TLS 1.2 master secret */
245+
if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
246+
DLOGE("Secret type is not matching...");
247+
}
248+
249+
PDtlsSession pDtlsSession = (PDtlsSession) customData;
250+
PTlsKeys pKeys = &pDtlsSession->tlsKeys;
251+
252+
if (pMasterSecretLen != sizeof(pKeys->masterSecret)) {
253+
DLOGE("Length check failed, pMasterSecretLen = %d, sizeof(pKeys->masterSecret) = %d\n", pMasterSecretLen, sizeof(pKeys->masterSecret));
254+
}
255+
256+
MEMCPY(pKeys->masterSecret, pMasterSecret, pMasterSecretLen);
257+
MEMCPY(pKeys->randBytes, clientRandom, MAX_DTLS_RANDOM_BYTES_LEN);
258+
MEMCPY(pKeys->randBytes + MAX_DTLS_RANDOM_BYTES_LEN, serverRandom, MAX_DTLS_RANDOM_BYTES_LEN);
259+
pKeys->tlsProfile = tlsProfile;
260+
LEAVES();
261+
}
262+
#else
232263
INT32 dtlsSessionKeyDerivationCallback(PVOID customData, const unsigned char* pMasterSecret, const unsigned char* pKeyBlock, ULONG maclen,
233264
ULONG keylen, ULONG ivlen, const unsigned char clientRandom[MAX_DTLS_RANDOM_BYTES_LEN],
234265
const unsigned char serverRandom[MAX_DTLS_RANDOM_BYTES_LEN], mbedtls_tls_prf_types tlsProfile)
@@ -247,6 +278,7 @@ INT32 dtlsSessionKeyDerivationCallback(PVOID customData, const unsigned char* pM
247278
LEAVES();
248279
return 0;
249280
}
281+
#endif
250282

251283
STATUS dtlsSessionHandshakeInThread(PDtlsSession pDtlsSession, BOOL isServer)
252284
{
@@ -287,11 +319,18 @@ STATUS dtlsSessionStart(PDtlsSession pDtlsSession, BOOL isServer)
287319
}
288320
mbedtls_ssl_conf_dtls_cookies(&pDtlsSession->sslCtxConfig, NULL, NULL, NULL);
289321
CHK(mbedtls_ssl_conf_dtls_srtp_protection_profiles(&pDtlsSession->sslCtxConfig, DTLS_SRTP_SUPPORTED_PROFILES) == 0, STATUS_CREATE_SSL_FAILED);
322+
323+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
290324
mbedtls_ssl_conf_export_keys_ext_cb(&pDtlsSession->sslCtxConfig, dtlsSessionKeyDerivationCallback, pDtlsSession);
325+
#endif
291326

292327
CHK(mbedtls_ssl_setup(&pDtlsSession->sslCtx, &pDtlsSession->sslCtxConfig) == 0, STATUS_SSL_CTX_CREATION_FAILED);
293328
mbedtls_ssl_set_mtu(&pDtlsSession->sslCtx, DEFAULT_MTU_SIZE_BYTES);
294329
mbedtls_ssl_set_bio(&pDtlsSession->sslCtx, pDtlsSession, dtlsSessionSendCallback, dtlsSessionReceiveCallback, NULL);
330+
331+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
332+
mbedtls_ssl_set_export_keys_cb(&pDtlsSession->sslCtx, dtlsSessionKeyDerivationCallback, pDtlsSession);
333+
#endif
295334
mbedtls_ssl_set_timer_cb(&pDtlsSession->sslCtx, &pDtlsSession->transmissionTimer, dtlsSessionSetTimerCallback, dtlsSessionGetTimerCallback);
296335

297336
// Start non-blocking handshaking
@@ -363,7 +402,11 @@ STATUS dtlsSessionProcessPacket(PDtlsSession pDtlsSession, PBYTE pData, PINT32 p
363402
}
364403
}
365404

405+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
406+
if (pDtlsSession->sslCtx.MBEDTLS_PRIVATE(state) == MBEDTLS_SSL_HANDSHAKE_OVER) {
407+
#else
366408
if (pDtlsSession->sslCtx.state == MBEDTLS_SSL_HANDSHAKE_OVER) {
409+
#endif
367410
CHK_STATUS(dtlsSessionChangeState(pDtlsSession, RTC_DTLS_TRANSPORT_STATE_CONNECTED));
368411
}
369412

@@ -505,7 +548,11 @@ STATUS dtlsSessionPopulateKeyingMaterial(PDtlsSession pDtlsSession, PDtlsKeyingM
505548
MEMCPY(pDtlsKeyingMaterial->serverWriteKey + MAX_SRTP_MASTER_KEY_LEN, &keyingMaterialBuffer[offset], MAX_SRTP_SALT_KEY_LEN);
506549

507550
mbedtls_ssl_get_dtls_srtp_negotiation_result(&pDtlsSession->sslCtx, &negotiatedSRTPProfile);
551+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
552+
switch (negotiatedSRTPProfile.MBEDTLS_PRIVATE(chosen_dtls_srtp_profile)) {
553+
#else
508554
switch (negotiatedSRTPProfile.chosen_dtls_srtp_profile) {
555+
#endif
509556
case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80:
510557
pDtlsKeyingMaterial->srtpProfile = KVS_SRTP_PROFILE_AES128_CM_HMAC_SHA1_80;
511558
break;
@@ -517,9 +564,11 @@ STATUS dtlsSessionPopulateKeyingMaterial(PDtlsSession pDtlsSession, PDtlsKeyingM
517564
}
518565

519566
CleanUp:
567+
520568
if (locked) {
521569
MUTEX_UNLOCK(pDtlsSession->sslLock);
522570
}
571+
CHK_LOG_ERR(retStatus);
523572

524573
LEAVES();
525574
return retStatus;
@@ -561,15 +610,33 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
561610
BOOL initialized = FALSE;
562611
mbedtls_ecp_keypair *pSrcECP, *pDstECP;
563612

613+
#if (MBEDTLS_VERSION_NUMBER >= 0x03000000)
614+
mbedtls_entropy_init(&entropy);
615+
mbedtls_ctr_drbg_init(&ctr_drbg);
616+
int mbedtls_ctr_ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
617+
if (mbedtls_ctr_ret != 0) {
618+
DLOGE("mbedtls_ctr_drbg_seed failed");
619+
CHK(mbedtls_ctr_ret == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
620+
}
621+
#endif
622+
564623
CHK(pCert != NULL && pKey != NULL && pDst != NULL, STATUS_NULL_ARG);
624+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
565625
CHK(mbedtls_pk_check_pair(&pCert->pk, pKey) == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
626+
#else
627+
CHK(mbedtls_pk_check_pair(&pCert->pk, pKey, mbedtls_ctr_drbg_random, &ctr_drbg) == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
628+
#endif
566629

567630
mbedtls_x509_crt_init(&pDst->cert);
568631
mbedtls_pk_init(&pDst->privateKey);
569632
initialized = TRUE;
570633

571634
CHK(mbedtls_x509_crt_parse_der(&pDst->cert, pCert->raw.p, pCert->raw.len) == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
635+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
572636
CHK(mbedtls_pk_setup(&pDst->privateKey, pKey->pk_info) == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
637+
#else
638+
CHK(mbedtls_pk_setup(&pDst->privateKey, pKey->MBEDTLS_PRIVATE(pk_info)) == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
639+
#endif
573640

574641
switch (mbedtls_pk_get_type(pKey)) {
575642
case MBEDTLS_PK_RSA:
@@ -579,9 +646,16 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
579646
case MBEDTLS_PK_ECDSA:
580647
pSrcECP = mbedtls_pk_ec(*pKey);
581648
pDstECP = mbedtls_pk_ec(pDst->privateKey);
649+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
582650
CHK(mbedtls_ecp_group_copy(&pDstECP->grp, &pSrcECP->grp) == 0 && mbedtls_ecp_copy(&pDstECP->Q, &pSrcECP->Q) == 0 &&
583651
mbedtls_mpi_copy(&pDstECP->d, &pSrcECP->d) == 0,
584652
STATUS_CERTIFICATE_GENERATION_FAILED);
653+
#else
654+
CHK(mbedtls_ecp_group_copy(&pDstECP->MBEDTLS_PRIVATE(grp), &pSrcECP->MBEDTLS_PRIVATE(grp)) == 0 &&
655+
mbedtls_ecp_copy(&pDstECP->MBEDTLS_PRIVATE(Q), &pSrcECP->MBEDTLS_PRIVATE(Q)) == 0 &&
656+
mbedtls_mpi_copy(&pDstECP->MBEDTLS_PRIVATE(d), &pSrcECP->MBEDTLS_PRIVATE(d)) == 0,
657+
STATUS_CERTIFICATE_GENERATION_FAILED);
658+
#endif
585659
break;
586660
default:
587661
CHK(FALSE, STATUS_CERTIFICATE_GENERATION_FAILED);
@@ -598,6 +672,29 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
598672
return retStatus;
599673
}
600674

675+
#if !(defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_DEPRECATED_REMOVED))
676+
int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert* ctx, const mbedtls_mpi* serial)
677+
{
678+
int ret;
679+
size_t tmp_len;
680+
681+
/* Ensure that the MPI value fits into the buffer */
682+
tmp_len = mbedtls_mpi_size(serial);
683+
if (tmp_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
684+
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
685+
}
686+
687+
ctx->MBEDTLS_PRIVATE(serial_len) = tmp_len;
688+
689+
ret = mbedtls_mpi_write_binary(serial, ctx->MBEDTLS_PRIVATE(serial), tmp_len);
690+
if (ret < 0) {
691+
return ret;
692+
}
693+
694+
return 0;
695+
}
696+
#endif // MBEDTLS_BIGNUM_C && !MBEDTLS_DEPRECATED_REMOVED
697+
601698
/**
602699
* createCertificateAndKey generates a new certificate and a key
603700
* If generateRSACertificate is true, RSA is going to be used for the key generation. Otherwise, ECDSA is going to be used.
@@ -732,7 +829,11 @@ STATUS dtlsCertificateFingerprint(mbedtls_x509_crt* pCert, PCHAR pBuff)
732829
pMdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
733830
CHK(pMdInfo != NULL, STATUS_INTERNAL_ERROR);
734831

832+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
833+
sslRet = mbedtls_sha256(pCert->raw.p, pCert->raw.len, fingerprint, 0);
834+
#else
735835
sslRet = mbedtls_sha256_ret(pCert->raw.p, pCert->raw.len, fingerprint, 0);
836+
#endif
736837
CHK(sslRet == 0, STATUS_INTERNAL_ERROR);
737838

738839
size = mbedtls_md_get_size(pMdInfo);

src/source/Crypto/Tls_mbedtls.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ STATUS tlsSessionProcessPacket(PTlsSession pTlsSession, PBYTE pData, UINT32 buff
171171
iterate = FALSE;
172172
}
173173
}
174-
174+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
175+
if (pTlsSession->sslCtx.MBEDTLS_PRIVATE(state) == MBEDTLS_SSL_HANDSHAKE_OVER) {
176+
#else
175177
if (pTlsSession->sslCtx.state == MBEDTLS_SSL_HANDSHAKE_OVER) {
178+
#endif
176179
tlsSessionChangeState(pTlsSession, TLS_SESSION_STATE_CONNECTED);
177180
}
178181

src/source/Include_i.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ extern "C" {
3939
#include <mbedtls/entropy.h>
4040
#include <mbedtls/ctr_drbg.h>
4141
#include <mbedtls/error.h>
42+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
4243
#include <mbedtls/certs.h>
44+
#endif
4345
#include <mbedtls/sha256.h>
4446
#include <mbedtls/md5.h>
4547
#endif

0 commit comments

Comments
 (0)