Skip to content

Commit

Permalink
Enable allowing unsupported critical extensions in runtime
Browse files Browse the repository at this point in the history
When compile time flag MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
is not set, certificate parsing fails if certificate contains unsupported critical extension.

This patch allows to modify this behavior in runtime.

Signed-off-by: Lev Stipakov <[email protected]>
  • Loading branch information
lstipakov committed Feb 19, 2020
1 parent 8d073c7 commit 4ba48dd
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
22 changes: 22 additions & 0 deletions include/mbedtls/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,10 @@ struct mbedtls_ssl_config
retransmission timeout (ms) */
#endif

uint32_t allowed_unsupported_critical_exts; /*!< Bit flags which represent runtime-enabled
unsupported critical extensions, e.g.
MBEDTLS_X509_EXT_NAME_CONSTRAINTS */

#if defined(MBEDTLS_SSL_RENEGOTIATION)
int renego_max_records; /*!< grace period for renegotiation */
unsigned char renego_period[8]; /*!< value of the record counters
Expand Down Expand Up @@ -3392,6 +3396,24 @@ void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
const unsigned char period[8] );
#endif /* MBEDTLS_SSL_RENEGOTIATION */

/**
* \brief Allows unsupported critical extensions
*
* Without compile-time flag MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
* mbedTLS fails certificate verification if certificate contains
* unsupported critical extensions.
*
* This method allows to modify behavior in runtime by providing
* bit flags which represent unsupported extensions (for example MBEDTLS_X509_EXT_NAME_CONSTRAINTS)
* which should be allowed despite missing above mentioned compile-time flag.
*
* \param conf SSL configuration
* \param exts Bit flags which represent runtime-enabled unsupported critical extensions,
* e.g. MBEDTLS_X509_EXT_NAME_CONSTRAINTS
*
*/
void mbedtls_ssl_conf_allow_unsupported_critical_exts( mbedtls_ssl_config *conf, uint32_t exts );

/**
* \brief Check if there is data already read from the
* underlying transport but not yet processed.
Expand Down
2 changes: 2 additions & 0 deletions include/mbedtls/x509_crt.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ typedef struct mbedtls_x509_crt
mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */

uint32_t allowed_unsupported_critical_exts; /**< Optional Bit flags which represent runtime-enabled unsupported critical extensions, e.g. MBEDTLS_X509_EXT_NAME_CONSTRAINTS */

struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
}
mbedtls_x509_crt;
Expand Down
11 changes: 11 additions & 0 deletions library/ssl_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,12 @@ static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
}

if (ssl->session_negotiate->peer_cert != NULL)
{
ssl->session_negotiate->peer_cert->allowed_unsupported_critical_exts =
ssl->conf->allowed_unsupported_critical_exts;
}

/* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
i += 3;

Expand Down Expand Up @@ -4670,6 +4676,11 @@ void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
}
#endif /* MBEDTLS_SSL_RENEGOTIATION */

void mbedtls_ssl_conf_allow_unsupported_critical_exts( mbedtls_ssl_config *conf, uint32_t exts )
{
conf->allowed_unsupported_critical_exts = exts;
}

#if defined(MBEDTLS_SSL_SESSION_TICKETS)
#if defined(MBEDTLS_SSL_CLI_C)
void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Expand Down
10 changes: 8 additions & 2 deletions library/x509_crt.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ static int x509_get_crt_ext( unsigned char **p,
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t len;
unsigned char *end_ext_data, *end_ext_octet;
int is_supported;

if( *p == end )
return( 0 );
Expand Down Expand Up @@ -945,16 +946,20 @@ static int x509_get_crt_ext( unsigned char **p,
/*
* Detect supported extensions
*/
ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
ret = mbedtls_oid_get_x509_ext_type_supported( &extn_oid, &ext_type, &is_supported );

if( ret != 0 )
if( ( ret != 0 ) || ( is_supported == 0 ) )
{
/* No parser found, skip extension */
*p = end_ext_octet;

#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
if( is_critical )
{
/* Do not fail if extension is found, but unsupported and allowed in runtime */
if( ( ret == 0 ) && ( ext_type & crt->allowed_unsupported_critical_exts ) )
continue;

/* Data is marked as critical: fail */
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Expand Down Expand Up @@ -1346,6 +1351,7 @@ static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,

prev = crt;
mbedtls_x509_crt_init( crt->next );
crt->next->allowed_unsupported_critical_exts = crt->allowed_unsupported_critical_exts;
crt = crt->next;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/data_files/test-ca-nc.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----BEGIN CERTIFICATE-----
MIIDSzCCAjOgAwIBAgIJAJx/NjT4C4viMA0GCSqGSIb3DQEBCwUAMBMxETAPBgNV
BAMMCExlZXZpQ0E0MB4XDTE4MDEyNzE1MDczMloXDTI4MDEyNTE1MDczMlowEzER
MA8GA1UEAwwITGVldmlDQTQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB
AQDWN79RTlyFm5o0LVMSVjc68W0+gtl95xpaaD7IS6gDYjcbGnCwSefiq7y9rtck
OM1A5Bzhj5+iWbmZStUmeJUhSGgxP/FxuUaAV0fsBGJ5jDrzmbhzDkHsNxDMB2ks
XFyy4LfODcBs9TXxY43KUKuq/0meiT3WAaZWHMYle9vkQJM2l0RyH4IXHCHiIRwd
2wntin6T9QOFJOc2ietNb7KsXVne81wb7h9BVMsjCIAsbPpHa+PZQs1xFuxmRxCs
kpavnMy+SqevHhvqtvbHppcXYtZspTnkVoXWUdx3HHXgZMQKlAWlwyx57xpZBU2g
qksO+KCLVYOQMN9usmuMOpHHAgMBAAGjgaEwgZ4wHQYDVR0eAQH/BBMwEaAPMA2C
C2V4YW1wbGUuY29tMB0GA1UdDgQWBBR3T9IilPeRAFfLO8ocg216OBo+6DBDBgNV
HSMEPDA6gBR3T9IilPeRAFfLO8ocg216OBo+6KEXpBUwEzERMA8GA1UEAwwITGVl
dmlDQTSCCQCcfzY0+AuL4jAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkq
hkiG9w0BAQsFAAOCAQEAR086ciNM3ujSQNhhguqFHYGfDRRuAgOk4l7GXIfFa9te
B2KMLSwP367QaMwFxRrOoDvixIjzbpiiKB3cv+IXqGyfsRJw47XLwGK4FtSsXjst
m2M8W5iXBQ94XoLj9OKb4ZJWKI930S/PF7uuxICtWttYSoylfyMkiR45+1SLj2eF
X4EnXK3Q0H42v8LCDFqj9iNQ2WMLwA7kFPB+oOZxkFi2G0F3VuW+JZeBPQCpYdRO
0kQQ/gIZE6KEdscKHi9y6OfGSeRlDBMADky9NiZy7I3AcspLcmMQh/191/DnooNe
OwQ6w1HweApjB46bGyILpGUi9MZhvCnoLWg+cN3/wQ==
-----END CERTIFICATE-----
6 changes: 6 additions & 0 deletions tests/suites/test_suite_x509parse.data
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,12 @@ X509 File parse (trailing spaces, OK)
depends_on:MBEDTLS_ECDSA_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_SHA256_C:MBEDTLS_RSA_C
x509parse_crt_file:"data_files/server7_trailing_space.crt":0

X509 File parse (unsupported critical ext Name Constraints, fail)
x509parse_crt_file:"data_files/test-ca-nc.crt":MBEDTLS_ERR_X509_INVALID_EXTENSIONS + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG

X509 File parse (allowed unsupported critical ext Name Constraints, ok)
x509parse_crt_file_allow_exts:"data_files/test-ca-nc.crt":MBEDTLS_X509_EXT_NAME_CONSTRAINTS:0

X509 Get time (UTC no issues)
depends_on:MBEDTLS_X509_USE_C
x509_get_time:MBEDTLS_ASN1_UTC_TIME:"500101000000Z":0:1950:1:1:0:0:0
Expand Down
15 changes: 15 additions & 0 deletions tests/suites/test_suite_x509parse.function
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,21 @@ exit:
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
void x509parse_crt_file_allow_exts( char *crt_file, int exts, int result )
{
mbedtls_x509_crt crt;

mbedtls_x509_crt_init( &crt );
crt.allowed_unsupported_critical_exts = exts;

TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );

exit:
mbedtls_x509_crt_free( &crt );
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
void x509parse_crt( data_t * buf, char * result_str, int result )
{
Expand Down

0 comments on commit 4ba48dd

Please sign in to comment.