59
59
#define TLS_HANDSHAKE_TIMEOUT_MAX 201000
60
60
61
61
//#define TLS_SEC_PROT_LIB_TLS_DEBUG // Enable mbed TLS debug traces
62
- #define TLS_SEC_PROT_LIB_CERT_VALID_SKIP // Skip certificate validation
63
62
64
- typedef int tls_sec_prot_lib_crt_verify_cb (mbedtls_x509_crt * crt , uint32_t * flags );
63
+ typedef int tls_sec_prot_lib_crt_verify_cb (tls_security_t * sec , mbedtls_x509_crt * crt , uint32_t * flags );
65
64
66
65
struct tls_security_s {
67
66
mbedtls_ssl_config conf ; /**< mbed TLS SSL configuration */
@@ -75,6 +74,7 @@ struct tls_security_s {
75
74
mbedtls_x509_crt owncert ; /**< Own certificate(s) */
76
75
mbedtls_pk_context pkey ; /**< Private key for own certificate */
77
76
void * handle ; /**< Handle provided in callbacks (defined by library user) */
77
+ bool ext_cert_valid : 1 ; /**< Extended certificate validation enabled */
78
78
tls_sec_prot_lib_crt_verify_cb * crt_verify ; /**< Verify function for top certificate */
79
79
tls_sec_prot_lib_send * send ; /**< Send callback */
80
80
tls_sec_prot_lib_receive * receive ; /**< Receive callback */
@@ -98,10 +98,10 @@ static int tls_sec_prot_lib_x509_crt_verify(void *ctx, mbedtls_x509_crt *crt, in
98
98
static int8_t tls_sec_prot_lib_subject_alternative_name_validate (mbedtls_x509_crt * crt );
99
99
static int8_t tls_sec_prot_lib_extended_key_usage_validate (mbedtls_x509_crt * crt );
100
100
#ifdef HAVE_PAE_AUTH
101
- static int tls_sec_prot_lib_x509_crt_idevid_ldevid_verify (mbedtls_x509_crt * crt , uint32_t * flags );
101
+ static int tls_sec_prot_lib_x509_crt_idevid_ldevid_verify (tls_security_t * sec , mbedtls_x509_crt * crt , uint32_t * flags );
102
102
#endif
103
103
#ifdef HAVE_PAE_SUPP
104
- static int tls_sec_prot_lib_x509_crt_server_verify (mbedtls_x509_crt * crt , uint32_t * flags );
104
+ static int tls_sec_prot_lib_x509_crt_server_verify (tls_security_t * sec , mbedtls_x509_crt * crt , uint32_t * flags );
105
105
#endif
106
106
#ifdef TLS_SEC_PROT_LIB_TLS_DEBUG
107
107
static void tls_sec_prot_lib_debug (void * ctx , int level , const char * file , int line , const char * string );
@@ -115,6 +115,17 @@ static void *tls_sec_prot_lib_mem_calloc(size_t count, size_t size);
115
115
static void tls_sec_prot_lib_mem_free (void * ptr );
116
116
#endif
117
117
118
+ #if defined(HAVE_PAE_AUTH ) && defined(HAVE_PAE_SUPP )
119
+ #define is_server_is_set (is_server == true)
120
+ #define is_server_is_not_set (is_server == false)
121
+ #elif defined(HAVE_PAE_AUTH )
122
+ #define is_server_is_set true
123
+ #define is_server_is_not_set false
124
+ #elif defined(HAVE_PAE_SUPP )
125
+ #define is_server_is_set false
126
+ #define is_server_is_not_set true
127
+ #endif
128
+
118
129
int8_t tls_sec_prot_lib_init (tls_security_t * sec )
119
130
{
120
131
const char * pers = "ws_tls" ;
@@ -123,7 +134,6 @@ int8_t tls_sec_prot_lib_init(tls_security_t *sec)
123
134
mbedtls_platform_set_calloc_free (tls_sec_prot_lib_mem_calloc , tls_sec_prot_lib_mem_free );
124
135
#endif
125
136
126
-
127
137
mbedtls_ssl_init (& sec -> ssl );
128
138
mbedtls_ssl_config_init (& sec -> conf );
129
139
mbedtls_ctr_drbg_init (& sec -> ctr_drbg );
@@ -282,22 +292,18 @@ static int tls_sec_prot_lib_configure_certificates(tls_security_t *sec, const se
282
292
// Certificate verify required on both client and server
283
293
mbedtls_ssl_conf_authmode (& sec -> conf , MBEDTLS_SSL_VERIFY_REQUIRED );
284
294
295
+ // Get extended certificate validation setting
296
+ sec -> ext_cert_valid = sec_prot_certs_ext_certificate_validation_get (certs );
297
+
285
298
return 0 ;
286
299
}
287
300
288
- #if defined(HAVE_PAE_AUTH ) && defined(HAVE_PAE_SUPP )
289
- #define is_server_is_set (is_server == true)
290
- #define is_server_is_not_set (is_server == false)
291
- #elif defined(HAVE_PAE_AUTH )
292
- #define is_server_is_set true
293
- #define is_server_is_not_set false
294
- #elif defined(HAVE_PAE_SUPP )
295
- #define is_server_is_set false
296
- #define is_server_is_not_set true
297
- #endif
298
-
299
301
int8_t tls_sec_prot_lib_connect (tls_security_t * sec , bool is_server , const sec_prot_certs_t * certs )
300
302
{
303
+ #if !defined(HAVE_PAE_SUPP ) || !defined(HAVE_PAE_AUTH )
304
+ (void ) is_server ;
305
+ #endif
306
+
301
307
if (!sec ) {
302
308
return -1 ;
303
309
}
@@ -313,6 +319,7 @@ int8_t tls_sec_prot_lib_connect(tls_security_t *sec, bool is_server, const sec_p
313
319
}
314
320
#endif
315
321
322
+
316
323
if ((mbedtls_ssl_config_defaults (& sec -> conf ,
317
324
is_server_is_set ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT ,
318
325
MBEDTLS_SSL_TRANSPORT_STREAM , 0 )) != 0 ) {
@@ -470,23 +477,20 @@ static int tls_sec_prot_lib_ssl_export_keys(void *p_expkey, const unsigned char
470
477
471
478
static int tls_sec_prot_lib_x509_crt_verify (void * ctx , mbedtls_x509_crt * crt , int certificate_depth , uint32_t * flags )
472
479
{
473
- /* MD/PK forced also by configuration flags and dynamic settings but still verified
474
- here to prevent invalid configurations/certificates */
480
+ tls_security_t * sec = (tls_security_t * ) ctx ;
481
+
482
+ /* MD/PK forced by configuration flags and dynamic settings but traced also here
483
+ to prevent invalid configurations/certificates */
475
484
if (crt -> sig_md != MBEDTLS_MD_SHA256 ) {
476
485
tr_error ("Invalid signature md algorithm" );
477
- * flags |= MBEDTLS_X509_BADCRL_BAD_MD ;
478
- return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ;
479
486
}
480
487
if (crt -> sig_pk != MBEDTLS_PK_ECDSA ) {
481
488
tr_error ("Invalid signature pk algorithm" );
482
- * flags |= MBEDTLS_X509_BADCRL_BAD_PK ;
483
- return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ;
484
489
}
485
490
486
491
// Verify top certificate of the chain
487
492
if (certificate_depth == 0 ) {
488
- tls_security_t * sec = (tls_security_t * )ctx ;
489
- return sec -> crt_verify (crt , flags );
493
+ return sec -> crt_verify (sec , crt , flags );
490
494
}
491
495
492
496
// No further checks for intermediate and root certificates at the moment
@@ -528,29 +532,30 @@ static int8_t tls_sec_prot_lib_extended_key_usage_validate(mbedtls_x509_crt *crt
528
532
{
529
533
// Extended key usage must be present
530
534
if (mbedtls_x509_crt_check_extended_key_usage (crt , MBEDTLS_OID_WISUN_FAN , sizeof (MBEDTLS_OID_WISUN_FAN ) - 1 ) != 0 ) {
535
+ tr_error ("invalid extended key usage" );
531
536
return -1 ; // FAIL
532
537
}
533
538
return 0 ;
534
539
}
535
540
536
541
#ifdef HAVE_PAE_AUTH
537
- static int tls_sec_prot_lib_x509_crt_idevid_ldevid_verify (mbedtls_x509_crt * crt , uint32_t * flags )
542
+ static int tls_sec_prot_lib_x509_crt_idevid_ldevid_verify (tls_security_t * sec , mbedtls_x509_crt * crt , uint32_t * flags )
538
543
{
539
544
// For both IDevID and LDevId both subject alternative name or extended key usage must be valid
540
545
if (tls_sec_prot_lib_subject_alternative_name_validate (crt ) < 0 ||
541
546
tls_sec_prot_lib_extended_key_usage_validate (crt ) < 0 ) {
542
547
tr_error ("invalid cert" );
543
- #ifndef TLS_SEC_PROT_LIB_CERT_VALID_SKIP
544
- * flags |= MBEDTLS_X509_BADCERT_OTHER ;
545
- return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ;
546
- #endif
548
+ if ( sec -> ext_cert_valid ) {
549
+ * flags |= MBEDTLS_X509_BADCERT_OTHER ;
550
+ return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ;
551
+ }
547
552
}
548
553
return 0 ;
549
554
}
550
555
#endif
551
556
552
557
#ifdef HAVE_PAE_SUPP
553
- static int tls_sec_prot_lib_x509_crt_server_verify (mbedtls_x509_crt * crt , uint32_t * flags )
558
+ static int tls_sec_prot_lib_x509_crt_server_verify (tls_security_t * sec , mbedtls_x509_crt * crt , uint32_t * flags )
554
559
{
555
560
int8_t sane_res = tls_sec_prot_lib_subject_alternative_name_validate (crt );
556
561
int8_t ext_key_res = tls_sec_prot_lib_extended_key_usage_validate (crt );
@@ -560,10 +565,10 @@ static int tls_sec_prot_lib_x509_crt_server_verify(mbedtls_x509_crt *crt, uint32
560
565
// Then both subject alternative name and extended key usage must be valid
561
566
if (sane_res < 0 || ext_key_res < 0 ) {
562
567
tr_error ("invalid cert" );
563
- #ifndef TLS_SEC_PROT_LIB_CERT_VALID_SKIP
564
- * flags |= MBEDTLS_X509_BADCERT_OTHER ;
565
- return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ;
566
- #endif
568
+ if ( sec -> ext_cert_valid ) {
569
+ * flags |= MBEDTLS_X509_BADCERT_OTHER ;
570
+ return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ;
571
+ }
567
572
}
568
573
}
569
574
@@ -656,4 +661,3 @@ uint16_t tls_sec_prot_lib_size(void)
656
661
}
657
662
#endif /* WS_MBEDTLS_SECURITY_ENABLED */
658
663
#endif /* HAVE_WS */
659
-
0 commit comments