From 6225fc7542140198f57d189272d1697876411d80 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Thu, 25 Feb 2021 10:28:58 +0000 Subject: [PATCH] cert_chain_verify_depth: Handle value of 0 If cert_chain_verify_depth is 0 and cert_chain_validation is set in coap_dtls_pki_t, then a Cert + its signing CA should be valid (no intermediary CAs). src/coap_gnutls.c: src/coap_mbedtls.c: src/coap_openssl.c: Correct set and check cert_chain_verify_depth. src/coap_openssl.c: Fix building on openssl-1.1.0 --- src/coap_gnutls.c | 2 +- src/coap_mbedtls.c | 2 +- src/coap_openssl.c | 28 +++++++++++++--------------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index aec14fc8..e15ad081 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -1357,7 +1357,7 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, if (setup_data->cert_chain_validation) { gnutls_certificate_set_verify_limits(*pki_credentials, 0, - setup_data->cert_chain_verify_depth); + setup_data->cert_chain_verify_depth + 2); } /* diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 6d24a8f9..53c93ad2 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -401,7 +401,7 @@ cert_verify_callback_mbedtls(void *data, mbedtls_x509_crt *crt, } } if (setup_data->cert_chain_validation && - depth > (setup_data->cert_chain_verify_depth)) { + depth > (setup_data->cert_chain_verify_depth + 1)) { *flags |= MBEDTLS_X509_BADCERT_OTHER; coap_log(LOG_WARNING, " %s: %s: '%s' depth %d\n", diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 5ca78936..a8f55cb8 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -578,7 +578,7 @@ static void coap_dtls_info_callback(const SSL *ssl, int where, int ret) { coap_log(LOG_WARNING, "* %s: %s:failed in %s\n", coap_session_str(session), pstr, SSL_state_string_long(ssl)); while ((e = ERR_get_error())) - coap_log(LOG_WARNING, "* %s: %s at %s:%s\n", + coap_log(LOG_WARNING, "* %s: %s at %s:%s\n", coap_session_str(session), ERR_reason_error_string(e), ERR_lib_error_string(e), ERR_func_error_string(e)); } @@ -1027,17 +1027,15 @@ setup_pki_server(SSL_CTX *ctx, BIO *in; X509 *x = NULL; char *rw_var = NULL; - if (role == COAP_DTLS_ROLE_SERVER) { - cert_names = SSL_load_client_CA_file(setup_data->pki_key.key.pem.ca_file); - if (cert_names != NULL) - SSL_CTX_set_client_CA_list(ctx, cert_names); - else { - coap_log(LOG_WARNING, - "*** setup_pki: (D)TLS: %s: Unable to configure " - "client CA File\n", - setup_data->pki_key.key.pem.ca_file); - return 0; - } + cert_names = SSL_load_client_CA_file(setup_data->pki_key.key.pem.ca_file); + if (cert_names != NULL) + SSL_CTX_set_client_CA_list(ctx, cert_names); + else { + coap_log(LOG_WARNING, + "*** setup_pki: (D)TLS: %s: Unable to configure " + "client CA File\n", + setup_data->pki_key.key.pem.ca_file); + return 0; } /* Add CA to the trusted root CA store */ @@ -1880,7 +1878,7 @@ tls_verify_call_back(int preverify_ok, X509_STORE_CTX *ctx) { break; } if (setup_data->cert_chain_validation && - depth > (setup_data->cert_chain_verify_depth)) { + depth > (setup_data->cert_chain_verify_depth + 1)) { preverify_ok = 0; err = X509_V_ERR_CERT_CHAIN_TOO_LONG; X509_STORE_CTX_set_error(ctx, err); @@ -1991,7 +1989,7 @@ tls_secret_call_back(SSL *ssl, /* Check CA Chain */ if (setup_data->cert_chain_validation) - SSL_set_verify_depth(ssl, setup_data->cert_chain_verify_depth + 1); + SSL_set_verify_depth(ssl, setup_data->cert_chain_verify_depth + 2); /* Certificate Revocation */ if (setup_data->check_cert_revocation) { @@ -2422,7 +2420,7 @@ is_x509: /* Check CA Chain */ if (setup_data->cert_chain_validation) - SSL_set_verify_depth(ssl, setup_data->cert_chain_verify_depth + 1); + SSL_set_verify_depth(ssl, setup_data->cert_chain_verify_depth + 2); /* Certificate Revocation */ if (setup_data->check_cert_revocation) {