Skip to content

Commit 38fbb72

Browse files
committed
crypto/mbedtls: Add support for mbedtls 3.x
- Also clone mbedtls 3.6.x instead of 2.8.x to match with rainmaker's version
1 parent 339f24f commit 38fbb72

File tree

6 files changed

+133
-2
lines changed

6 files changed

+133
-2
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
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/build
2626
CMAKE_ARGS
2727
-DCMAKE_INSTALL_PREFIX=${OPEN_SRC_INSTALL_PREFIX}

src/source/Crypto/Crypto.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ typedef enum {
3535
#define KVS_RSA_F4 0x10001L
3636
#define KVS_MD5_DIGEST_LENGTH 16
3737
#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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,21 @@ 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(void *customData,
214+
mbedtls_ssl_key_export_type secret_type,
215+
const unsigned char *pMasterSecret,
216+
size_t pMasterSecretLen,
217+
const unsigned char clientRandom[MAX_DTLS_RANDOM_BYTES_LEN],
218+
const unsigned char serverRandom[MAX_DTLS_RANDOM_BYTES_LEN],
219+
mbedtls_tls_prf_types tlsProfile);
220+
#else
211221
INT32 dtlsSessionKeyDerivationCallback(PVOID, const unsigned char*, const unsigned char*, ULONG, ULONG, ULONG,
212222
const unsigned char[MAX_DTLS_RANDOM_BYTES_LEN], const unsigned char[MAX_DTLS_RANDOM_BYTES_LEN],
213223
mbedtls_tls_prf_types);
224+
#endif
225+
214226
#else
215227
#error "A Crypto implementation is required."
216228
#endif

src/source/Crypto/Dtls_mbedtls.c

Lines changed: 110 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,37 @@ STATUS dtlsTransmissionTimerCallback(UINT32 timerID, UINT64 currentTime, UINT64
229234
return retStatus;
230235
}
231236

237+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
238+
void dtlsSessionKeyDerivationCallback(void *customData,
239+
mbedtls_ssl_key_export_type secret_type,
240+
const unsigned char *pMasterSecret,
241+
size_t pMasterSecretLen,
242+
const unsigned char clientRandom[MAX_DTLS_RANDOM_BYTES_LEN],
243+
const unsigned char serverRandom[MAX_DTLS_RANDOM_BYTES_LEN],
244+
mbedtls_tls_prf_types tlsProfile)
245+
{
246+
ENTERS();
247+
248+
/* We're only interested in the TLS 1.2 master secret */
249+
if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
250+
printf("Secret type is not matching...\n");
251+
}
252+
253+
PDtlsSession pDtlsSession = (PDtlsSession) customData;
254+
PTlsKeys pKeys = &pDtlsSession->tlsKeys;
255+
256+
if (pMasterSecretLen != sizeof(pKeys->masterSecret)) {
257+
printf("Length check failed, pMasterSecretLen = %d, sizeof(pKeys->masterSecret) = %d\n",
258+
pMasterSecretLen, sizeof(pKeys->masterSecret));
259+
}
260+
261+
MEMCPY(pKeys->masterSecret, pMasterSecret, pMasterSecretLen);
262+
MEMCPY(pKeys->randBytes, clientRandom, MAX_DTLS_RANDOM_BYTES_LEN);
263+
MEMCPY(pKeys->randBytes + MAX_DTLS_RANDOM_BYTES_LEN, serverRandom, MAX_DTLS_RANDOM_BYTES_LEN);
264+
pKeys->tlsProfile = tlsProfile;
265+
LEAVES();
266+
}
267+
#else
232268
INT32 dtlsSessionKeyDerivationCallback(PVOID customData, const unsigned char* pMasterSecret, const unsigned char* pKeyBlock, ULONG maclen,
233269
ULONG keylen, ULONG ivlen, const unsigned char clientRandom[MAX_DTLS_RANDOM_BYTES_LEN],
234270
const unsigned char serverRandom[MAX_DTLS_RANDOM_BYTES_LEN], mbedtls_tls_prf_types tlsProfile)
@@ -247,6 +283,7 @@ INT32 dtlsSessionKeyDerivationCallback(PVOID customData, const unsigned char* pM
247283
LEAVES();
248284
return 0;
249285
}
286+
#endif
250287

251288
STATUS dtlsSessionHandshakeInThread(PDtlsSession pDtlsSession, BOOL isServer)
252289
{
@@ -287,11 +324,18 @@ STATUS dtlsSessionStart(PDtlsSession pDtlsSession, BOOL isServer)
287324
}
288325
mbedtls_ssl_conf_dtls_cookies(&pDtlsSession->sslCtxConfig, NULL, NULL, NULL);
289326
CHK(mbedtls_ssl_conf_dtls_srtp_protection_profiles(&pDtlsSession->sslCtxConfig, DTLS_SRTP_SUPPORTED_PROFILES) == 0, STATUS_CREATE_SSL_FAILED);
327+
328+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
290329
mbedtls_ssl_conf_export_keys_ext_cb(&pDtlsSession->sslCtxConfig, dtlsSessionKeyDerivationCallback, pDtlsSession);
330+
#endif
291331

292332
CHK(mbedtls_ssl_setup(&pDtlsSession->sslCtx, &pDtlsSession->sslCtxConfig) == 0, STATUS_SSL_CTX_CREATION_FAILED);
293333
mbedtls_ssl_set_mtu(&pDtlsSession->sslCtx, DEFAULT_MTU_SIZE_BYTES);
294334
mbedtls_ssl_set_bio(&pDtlsSession->sslCtx, pDtlsSession, dtlsSessionSendCallback, dtlsSessionReceiveCallback, NULL);
335+
336+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
337+
mbedtls_ssl_set_export_keys_cb(&pDtlsSession->sslCtx, dtlsSessionKeyDerivationCallback, pDtlsSession);
338+
#endif
295339
mbedtls_ssl_set_timer_cb(&pDtlsSession->sslCtx, &pDtlsSession->transmissionTimer, dtlsSessionSetTimerCallback, dtlsSessionGetTimerCallback);
296340

297341
// Start non-blocking handshaking
@@ -363,7 +407,11 @@ STATUS dtlsSessionProcessPacket(PDtlsSession pDtlsSession, PBYTE pData, PINT32 p
363407
}
364408
}
365409

410+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
411+
if (pDtlsSession->sslCtx.MBEDTLS_PRIVATE(state) == MBEDTLS_SSL_HANDSHAKE_OVER) {
412+
#else
366413
if (pDtlsSession->sslCtx.state == MBEDTLS_SSL_HANDSHAKE_OVER) {
414+
#endif
367415
CHK_STATUS(dtlsSessionChangeState(pDtlsSession, RTC_DTLS_TRANSPORT_STATE_CONNECTED));
368416
}
369417

@@ -504,8 +552,14 @@ STATUS dtlsSessionPopulateKeyingMaterial(PDtlsSession pDtlsSession, PDtlsKeyingM
504552

505553
MEMCPY(pDtlsKeyingMaterial->serverWriteKey + MAX_SRTP_MASTER_KEY_LEN, &keyingMaterialBuffer[offset], MAX_SRTP_SALT_KEY_LEN);
506554

555+
DLOGI("calling mbedtls_ssl_get_dtls_srtp_negotiation_result");
556+
507557
mbedtls_ssl_get_dtls_srtp_negotiation_result(&pDtlsSession->sslCtx, &negotiatedSRTPProfile);
558+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
559+
switch (negotiatedSRTPProfile.MBEDTLS_PRIVATE(chosen_dtls_srtp_profile)) {
560+
#else
508561
switch (negotiatedSRTPProfile.chosen_dtls_srtp_profile) {
562+
#endif
509563
case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80:
510564
pDtlsKeyingMaterial->srtpProfile = KVS_SRTP_PROFILE_AES128_CM_HMAC_SHA1_80;
511565
break;
@@ -520,6 +574,7 @@ STATUS dtlsSessionPopulateKeyingMaterial(PDtlsSession pDtlsSession, PDtlsKeyingM
520574
if (locked) {
521575
MUTEX_UNLOCK(pDtlsSession->sslLock);
522576
}
577+
CHK_LOG_ERR(retStatus);
523578

524579
LEAVES();
525580
return retStatus;
@@ -561,15 +616,35 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
561616
BOOL initialized = FALSE;
562617
mbedtls_ecp_keypair *pSrcECP, *pDstECP;
563618

619+
int ret = 0;
620+
#if (MBEDTLS_VERSION_NUMBER >= 0x03000000)
621+
mbedtls_entropy_init(&entropy);
622+
mbedtls_ctr_drbg_init(&ctr_drbg);
623+
int mbedtls_ctr_ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
624+
if (mbedtls_ctr_ret != 0) {
625+
printf("mbedtls_ctr_drbg_seed failed\n");
626+
goto CleanUp;
627+
}
628+
#endif
629+
564630
CHK(pCert != NULL && pKey != NULL && pDst != NULL, STATUS_NULL_ARG);
631+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
565632
CHK(mbedtls_pk_check_pair(&pCert->pk, pKey) == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
633+
#else
634+
ret = mbedtls_pk_check_pair(&pCert->pk, pKey, mbedtls_ctr_drbg_random, &ctr_drbg);
635+
CHK(ret == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
636+
#endif
566637

567638
mbedtls_x509_crt_init(&pDst->cert);
568639
mbedtls_pk_init(&pDst->privateKey);
569640
initialized = TRUE;
570641

571642
CHK(mbedtls_x509_crt_parse_der(&pDst->cert, pCert->raw.p, pCert->raw.len) == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
643+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
572644
CHK(mbedtls_pk_setup(&pDst->privateKey, pKey->pk_info) == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
645+
#else
646+
CHK(mbedtls_pk_setup(&pDst->privateKey, pKey->MBEDTLS_PRIVATE(pk_info)) == 0, STATUS_CERTIFICATE_GENERATION_FAILED);
647+
#endif
573648

574649
switch (mbedtls_pk_get_type(pKey)) {
575650
case MBEDTLS_PK_RSA:
@@ -579,9 +654,16 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
579654
case MBEDTLS_PK_ECDSA:
580655
pSrcECP = mbedtls_pk_ec(*pKey);
581656
pDstECP = mbedtls_pk_ec(pDst->privateKey);
657+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
582658
CHK(mbedtls_ecp_group_copy(&pDstECP->grp, &pSrcECP->grp) == 0 && mbedtls_ecp_copy(&pDstECP->Q, &pSrcECP->Q) == 0 &&
583659
mbedtls_mpi_copy(&pDstECP->d, &pSrcECP->d) == 0,
584660
STATUS_CERTIFICATE_GENERATION_FAILED);
661+
#else
662+
CHK(mbedtls_ecp_group_copy(&pDstECP->MBEDTLS_PRIVATE(grp), &pSrcECP->MBEDTLS_PRIVATE(grp)) == 0 &&
663+
mbedtls_ecp_copy(&pDstECP->MBEDTLS_PRIVATE(Q), &pSrcECP->MBEDTLS_PRIVATE(Q)) == 0 &&
664+
mbedtls_mpi_copy(&pDstECP->MBEDTLS_PRIVATE(d), &pSrcECP->MBEDTLS_PRIVATE(d)) == 0,
665+
STATUS_CERTIFICATE_GENERATION_FAILED);
666+
#endif
585667
break;
586668
default:
587669
CHK(FALSE, STATUS_CERTIFICATE_GENERATION_FAILED);
@@ -598,6 +680,30 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
598680
return retStatus;
599681
}
600682

683+
#if !(defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_DEPRECATED_REMOVED))
684+
int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx,
685+
const mbedtls_mpi *serial)
686+
{
687+
int ret;
688+
size_t tmp_len;
689+
690+
/* Ensure that the MPI value fits into the buffer */
691+
tmp_len = mbedtls_mpi_size(serial);
692+
if (tmp_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
693+
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
694+
}
695+
696+
ctx->MBEDTLS_PRIVATE(serial_len) = tmp_len;
697+
698+
ret = mbedtls_mpi_write_binary(serial, ctx->MBEDTLS_PRIVATE(serial), tmp_len);
699+
if (ret < 0) {
700+
return ret;
701+
}
702+
703+
return 0;
704+
}
705+
#endif // MBEDTLS_BIGNUM_C && !MBEDTLS_DEPRECATED_REMOVED
706+
601707
/**
602708
* createCertificateAndKey generates a new certificate and a key
603709
* If generateRSACertificate is true, RSA is going to be used for the key generation. Otherwise, ECDSA is going to be used.
@@ -732,7 +838,11 @@ STATUS dtlsCertificateFingerprint(mbedtls_x509_crt* pCert, PCHAR pBuff)
732838
pMdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
733839
CHK(pMdInfo != NULL, STATUS_INTERNAL_ERROR);
734840

841+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
842+
sslRet = mbedtls_sha256(pCert->raw.p, pCert->raw.len, fingerprint, 0);
843+
#else
735844
sslRet = mbedtls_sha256_ret(pCert->raw.p, pCert->raw.len, fingerprint, 0);
845+
#endif
736846
CHK(sslRet == 0, STATUS_INTERNAL_ERROR);
737847

738848
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)