@@ -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,37 @@ STATUS dtlsTransmissionTimerCallback(UINT32 timerID, UINT64 currentTime, UINT64
229
234
return retStatus ;
230
235
}
231
236
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
232
268
INT32 dtlsSessionKeyDerivationCallback (PVOID customData , const unsigned char * pMasterSecret , const unsigned char * pKeyBlock , ULONG maclen ,
233
269
ULONG keylen , ULONG ivlen , const unsigned char clientRandom [MAX_DTLS_RANDOM_BYTES_LEN ],
234
270
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
247
283
LEAVES ();
248
284
return 0 ;
249
285
}
286
+ #endif
250
287
251
288
STATUS dtlsSessionHandshakeInThread (PDtlsSession pDtlsSession , BOOL isServer )
252
289
{
@@ -287,11 +324,18 @@ STATUS dtlsSessionStart(PDtlsSession pDtlsSession, BOOL isServer)
287
324
}
288
325
mbedtls_ssl_conf_dtls_cookies (& pDtlsSession -> sslCtxConfig , NULL , NULL , NULL );
289
326
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
290
329
mbedtls_ssl_conf_export_keys_ext_cb (& pDtlsSession -> sslCtxConfig , dtlsSessionKeyDerivationCallback , pDtlsSession );
330
+ #endif
291
331
292
332
CHK (mbedtls_ssl_setup (& pDtlsSession -> sslCtx , & pDtlsSession -> sslCtxConfig ) == 0 , STATUS_SSL_CTX_CREATION_FAILED );
293
333
mbedtls_ssl_set_mtu (& pDtlsSession -> sslCtx , DEFAULT_MTU_SIZE_BYTES );
294
334
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
295
339
mbedtls_ssl_set_timer_cb (& pDtlsSession -> sslCtx , & pDtlsSession -> transmissionTimer , dtlsSessionSetTimerCallback , dtlsSessionGetTimerCallback );
296
340
297
341
// Start non-blocking handshaking
@@ -363,7 +407,11 @@ STATUS dtlsSessionProcessPacket(PDtlsSession pDtlsSession, PBYTE pData, PINT32 p
363
407
}
364
408
}
365
409
410
+ #if MBEDTLS_VERSION_NUMBER >= 0x03000000
411
+ if (pDtlsSession -> sslCtx .MBEDTLS_PRIVATE (state ) == MBEDTLS_SSL_HANDSHAKE_OVER ) {
412
+ #else
366
413
if (pDtlsSession -> sslCtx .state == MBEDTLS_SSL_HANDSHAKE_OVER ) {
414
+ #endif
367
415
CHK_STATUS (dtlsSessionChangeState (pDtlsSession , RTC_DTLS_TRANSPORT_STATE_CONNECTED ));
368
416
}
369
417
@@ -504,8 +552,14 @@ STATUS dtlsSessionPopulateKeyingMaterial(PDtlsSession pDtlsSession, PDtlsKeyingM
504
552
505
553
MEMCPY (pDtlsKeyingMaterial -> serverWriteKey + MAX_SRTP_MASTER_KEY_LEN , & keyingMaterialBuffer [offset ], MAX_SRTP_SALT_KEY_LEN );
506
554
555
+ DLOGI ("calling mbedtls_ssl_get_dtls_srtp_negotiation_result" );
556
+
507
557
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
508
561
switch (negotiatedSRTPProfile .chosen_dtls_srtp_profile ) {
562
+ #endif
509
563
case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80 :
510
564
pDtlsKeyingMaterial -> srtpProfile = KVS_SRTP_PROFILE_AES128_CM_HMAC_SHA1_80 ;
511
565
break ;
@@ -520,6 +574,7 @@ STATUS dtlsSessionPopulateKeyingMaterial(PDtlsSession pDtlsSession, PDtlsKeyingM
520
574
if (locked ) {
521
575
MUTEX_UNLOCK (pDtlsSession -> sslLock );
522
576
}
577
+ CHK_LOG_ERR (retStatus );
523
578
524
579
LEAVES ();
525
580
return retStatus ;
@@ -561,15 +616,35 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
561
616
BOOL initialized = FALSE;
562
617
mbedtls_ecp_keypair * pSrcECP , * pDstECP ;
563
618
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
+
564
630
CHK (pCert != NULL && pKey != NULL && pDst != NULL , STATUS_NULL_ARG );
631
+ #if MBEDTLS_VERSION_NUMBER < 0x03000000
565
632
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
566
637
567
638
mbedtls_x509_crt_init (& pDst -> cert );
568
639
mbedtls_pk_init (& pDst -> privateKey );
569
640
initialized = TRUE;
570
641
571
642
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
572
644
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
573
648
574
649
switch (mbedtls_pk_get_type (pKey )) {
575
650
case MBEDTLS_PK_RSA :
@@ -579,9 +654,16 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
579
654
case MBEDTLS_PK_ECDSA :
580
655
pSrcECP = mbedtls_pk_ec (* pKey );
581
656
pDstECP = mbedtls_pk_ec (pDst -> privateKey );
657
+ #if MBEDTLS_VERSION_NUMBER < 0x03000000
582
658
CHK (mbedtls_ecp_group_copy (& pDstECP -> grp , & pSrcECP -> grp ) == 0 && mbedtls_ecp_copy (& pDstECP -> Q , & pSrcECP -> Q ) == 0 &&
583
659
mbedtls_mpi_copy (& pDstECP -> d , & pSrcECP -> d ) == 0 ,
584
660
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
585
667
break ;
586
668
default :
587
669
CHK (FALSE, STATUS_CERTIFICATE_GENERATION_FAILED );
@@ -598,6 +680,30 @@ STATUS copyCertificateAndKey(mbedtls_x509_crt* pCert, mbedtls_pk_context* pKey,
598
680
return retStatus ;
599
681
}
600
682
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
+
601
707
/**
602
708
* createCertificateAndKey generates a new certificate and a key
603
709
* 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)
732
838
pMdInfo = mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 );
733
839
CHK (pMdInfo != NULL , STATUS_INTERNAL_ERROR );
734
840
841
+ #if MBEDTLS_VERSION_NUMBER >= 0x03000000
842
+ sslRet = mbedtls_sha256 (pCert -> raw .p , pCert -> raw .len , fingerprint , 0 );
843
+ #else
735
844
sslRet = mbedtls_sha256_ret (pCert -> raw .p , pCert -> raw .len , fingerprint , 0 );
845
+ #endif
736
846
CHK (sslRet == 0 , STATUS_INTERNAL_ERROR );
737
847
738
848
size = mbedtls_md_get_size (pMdInfo );
0 commit comments