@@ -8,6 +8,11 @@ mbedtls_ssl_srtp_profile DTLS_SRTP_SUPPORTED_PROFILES[] = {
8
8
MBEDTLS_TLS_SRTP_UNSET ,
9
9
};
10
10
11
+ #if MBEDTLS_VERSION_NUMBER >= 0x03000000
12
+ static mbedtls_ctr_drbg_context ctr_drbg ;
13
+ static mbedtls_entropy_context entropy ;
14
+ #endif
15
+
11
16
STATUS createDtlsSession (PDtlsSessionCallbacks pDtlsSessionCallbacks , TIMER_QUEUE_HANDLE timerQueueHandle , INT32 certificateBits ,
12
17
BOOL generateRSACertificate , PRtcCertificate pRtcCertificates , PDtlsSession * ppDtlsSession )
13
18
{
@@ -229,6 +234,32 @@ STATUS dtlsTransmissionTimerCallback(UINT32 timerID, UINT64 currentTime, UINT64
229
234
return retStatus ;
230
235
}
231
236
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
232
263
INT32 dtlsSessionKeyDerivationCallback (PVOID customData , const unsigned char * pMasterSecret , const unsigned char * pKeyBlock , ULONG maclen ,
233
264
ULONG keylen , ULONG ivlen , const unsigned char clientRandom [MAX_DTLS_RANDOM_BYTES_LEN ],
234
265
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
247
278
LEAVES ();
248
279
return 0 ;
249
280
}
281
+ #endif
250
282
251
283
STATUS dtlsSessionHandshakeInThread (PDtlsSession pDtlsSession , BOOL isServer )
252
284
{
@@ -287,11 +319,18 @@ STATUS dtlsSessionStart(PDtlsSession pDtlsSession, BOOL isServer)
287
319
}
288
320
mbedtls_ssl_conf_dtls_cookies (& pDtlsSession -> sslCtxConfig , NULL , NULL , NULL );
289
321
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
290
324
mbedtls_ssl_conf_export_keys_ext_cb (& pDtlsSession -> sslCtxConfig , dtlsSessionKeyDerivationCallback , pDtlsSession );
325
+ #endif
291
326
292
327
CHK (mbedtls_ssl_setup (& pDtlsSession -> sslCtx , & pDtlsSession -> sslCtxConfig ) == 0 , STATUS_SSL_CTX_CREATION_FAILED );
293
328
mbedtls_ssl_set_mtu (& pDtlsSession -> sslCtx , DEFAULT_MTU_SIZE_BYTES );
294
329
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
295
334
mbedtls_ssl_set_timer_cb (& pDtlsSession -> sslCtx , & pDtlsSession -> transmissionTimer , dtlsSessionSetTimerCallback , dtlsSessionGetTimerCallback );
296
335
297
336
// Start non-blocking handshaking
@@ -363,7 +402,11 @@ STATUS dtlsSessionProcessPacket(PDtlsSession pDtlsSession, PBYTE pData, PINT32 p
363
402
}
364
403
}
365
404
405
+ #if MBEDTLS_VERSION_NUMBER >= 0x03000000
406
+ if (pDtlsSession -> sslCtx .MBEDTLS_PRIVATE (state ) == MBEDTLS_SSL_HANDSHAKE_OVER ) {
407
+ #else
366
408
if (pDtlsSession -> sslCtx .state == MBEDTLS_SSL_HANDSHAKE_OVER ) {
409
+ #endif
367
410
CHK_STATUS (dtlsSessionChangeState (pDtlsSession , RTC_DTLS_TRANSPORT_STATE_CONNECTED ));
368
411
}
369
412
@@ -505,7 +548,11 @@ STATUS dtlsSessionPopulateKeyingMaterial(PDtlsSession pDtlsSession, PDtlsKeyingM
505
548
MEMCPY (pDtlsKeyingMaterial -> serverWriteKey + MAX_SRTP_MASTER_KEY_LEN , & keyingMaterialBuffer [offset ], MAX_SRTP_SALT_KEY_LEN );
506
549
507
550
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
508
554
switch (negotiatedSRTPProfile .chosen_dtls_srtp_profile ) {
555
+ #endif
509
556
case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80 :
510
557
pDtlsKeyingMaterial -> srtpProfile = KVS_SRTP_PROFILE_AES128_CM_HMAC_SHA1_80 ;
511
558
break ;
@@ -517,9 +564,11 @@ STATUS dtlsSessionPopulateKeyingMaterial(PDtlsSession pDtlsSession, PDtlsKeyingM
517
564
}
518
565
519
566
CleanUp :
567
+
520
568
if (locked ) {
521
569
MUTEX_UNLOCK (pDtlsSession -> sslLock );
522
570
}
571
+ CHK_LOG_ERR (retStatus );
523
572
524
573
LEAVES ();
525
574
return retStatus ;
@@ -561,15 +610,33 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
561
610
BOOL initialized = FALSE;
562
611
mbedtls_ecp_keypair * pSrcECP , * pDstECP ;
563
612
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
+
564
623
CHK (pCert != NULL && pKey != NULL && pDst != NULL , STATUS_NULL_ARG );
624
+ #if MBEDTLS_VERSION_NUMBER < 0x03000000
565
625
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
566
629
567
630
mbedtls_x509_crt_init (& pDst -> cert );
568
631
mbedtls_pk_init (& pDst -> privateKey );
569
632
initialized = TRUE;
570
633
571
634
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
572
636
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
573
640
574
641
switch (mbedtls_pk_get_type (pKey )) {
575
642
case MBEDTLS_PK_RSA :
@@ -579,9 +646,16 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
579
646
case MBEDTLS_PK_ECDSA :
580
647
pSrcECP = mbedtls_pk_ec (* pKey );
581
648
pDstECP = mbedtls_pk_ec (pDst -> privateKey );
649
+ #if MBEDTLS_VERSION_NUMBER < 0x03000000
582
650
CHK (mbedtls_ecp_group_copy (& pDstECP -> grp , & pSrcECP -> grp ) == 0 && mbedtls_ecp_copy (& pDstECP -> Q , & pSrcECP -> Q ) == 0 &&
583
651
mbedtls_mpi_copy (& pDstECP -> d , & pSrcECP -> d ) == 0 ,
584
652
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
585
659
break ;
586
660
default :
587
661
CHK (FALSE, STATUS_CERTIFICATE_GENERATION_FAILED );
@@ -598,6 +672,29 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
598
672
return retStatus ;
599
673
}
600
674
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
+
601
698
/**
602
699
* createCertificateAndKey generates a new certificate and a key
603
700
* 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)
732
829
pMdInfo = mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 );
733
830
CHK (pMdInfo != NULL , STATUS_INTERNAL_ERROR );
734
831
832
+ #if MBEDTLS_VERSION_NUMBER >= 0x03000000
833
+ sslRet = mbedtls_sha256 (pCert -> raw .p , pCert -> raw .len , fingerprint , 0 );
834
+ #else
735
835
sslRet = mbedtls_sha256_ret (pCert -> raw .p , pCert -> raw .len , fingerprint , 0 );
836
+ #endif
736
837
CHK (sslRet == 0 , STATUS_INTERNAL_ERROR );
737
838
738
839
size = mbedtls_md_get_size (pMdInfo );
0 commit comments