Merge pull request #3610 from cesanta/hostnoca

Allow hostname checking even when no CA cert was given
This commit is contained in:
Sergio R. Caprile authored and GitHub committed 2026-06-26 14:12:09 -03:00
commit ed19d5290e
8 files changed
+131 -48

No files matched your search

+53 -21
View File
@@ -20202,6 +20202,11 @@ void mg_tls_handshake(struct mg_connection *c) {
struct mg_tls *tls = (struct mg_tls *) c->tls;
int rc = mbedtls_ssl_handshake(&tls->ssl);
if (rc == 0) { // Success
if (tls->check_name && (mbedtls_ssl_get_verify_result(&tls->ssl) &
MBEDTLS_X509_BADCERT_CN_MISMATCH)) {
mg_error(c, "failed to verify hostname");
return;
} // ignore MBEDTLS_X509_BADCERT_NOT_TRUSTED, no CA cert given
MG_DEBUG(("%lu success", c->id));
c->is_tls_hs = 0;
mg_call(c, MG_EV_TLS_HS, NULL);
@@ -20223,6 +20228,7 @@ static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) {
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
struct mg_tls *tls = (struct mg_tls *) mg_calloc(1, sizeof(*tls));
int rc = 0;
bool check_name = false;
c->tls = tls;
if (c->tls == NULL) {
mg_error(c, "TLS OOM");
@@ -20253,24 +20259,30 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
mbedtls_ssl_conf_rng(&tls->conf, mg_mbed_rng, c);
#endif
if (c->is_client && opts->name.buf != NULL && opts->name.len > 0 &&
opts->name.buf[0] != '\0') {
char *host = mg_mprintf("%.*s", opts->name.len, opts->name.buf);
mbedtls_ssl_set_hostname(&tls->ssl, host);
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03030000 || \
MBEDTLS_VERSION_NUMBER >= 0x04000000
// NOTE: MBEDTLS_SSL_VERIFY_OPTIONAL is not supported for TLS1.3 on client
// side See https://github.com/Mbed-TLS/mbedtls/issues/7075
check_name = true;
#endif
MG_DEBUG(("%lu hostname verification: %s", c->id, host));
mg_free(host);
} else {
MG_DEBUG(("%lu skipping hostname verification", c->id));
mbedtls_ssl_set_hostname(&tls->ssl, NULL);
}
if (opts->ca.len == 0 || mg_strcmp(opts->ca, mg_str("*")) == 0) {
// NOTE: MBEDTLS_SSL_VERIFY_NONE is not supported for TLS1.3 on client side
// See https://github.com/Mbed-TLS/mbedtls/issues/7075
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE);
mbedtls_ssl_conf_authmode(&tls->conf, check_name // see set comment above
? MBEDTLS_SSL_VERIFY_OPTIONAL
: MBEDTLS_SSL_VERIFY_NONE);
tls->check_name = check_name; // host name set but no CA cert given
} else {
if (mg_load_cert(opts->ca, &tls->ca) == false) goto fail;
mbedtls_ssl_conf_ca_chain(&tls->conf, &tls->ca, NULL);
if (c->is_client) {
if (opts->name.buf != NULL && opts->name.buf[0] != '\0') {
char *host = mg_mprintf("%.*s", opts->name.len, opts->name.buf);
mbedtls_ssl_set_hostname(&tls->ssl, host);
MG_DEBUG(("%lu hostname verification: %s", c->id, host));
mg_free(host);
} else {
MG_DEBUG(("%lu skipping hostname verification", c->id));
mbedtls_ssl_set_hostname(&tls->ssl, NULL);
}
}
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
}
if (!mg_load_cert(opts->cert, &tls->cert)) goto fail;
@@ -20552,6 +20564,7 @@ void mg_tls_free(struct mg_connection *c) {
SSL_free(tls->ssl);
SSL_CTX_free(tls->ctx);
BIO_meth_free(tls->bm);
mg_free(tls->name);
mg_free(tls);
c->tls = NULL;
}
@@ -20559,6 +20572,7 @@ void mg_tls_free(struct mg_connection *c) {
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
struct mg_tls *tls = (struct mg_tls *) mg_calloc(1, sizeof(*tls));
const char *id = "mongoose";
bool check_name = false;
static unsigned char s_initialised = 0;
BIO *bio = NULL;
int rc;
@@ -20600,6 +20614,14 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
SSL_set_options(tls->ssl, SSL_OP_CIPHER_SERVER_PREFERENCE);
#endif
if (c->is_client && opts->name.buf != NULL && opts->name.len > 0 && opts->name.buf[0] != '\0') {
tls->name = mg_mprintf("%.*s", (int) opts->name.len, opts->name.buf);
if (tls->name == NULL) {
mg_error(c, "TLS OOM");
goto fail;
}
check_name = true;
}
#if MG_TLS == MG_TLS_WOLFSSL && !defined(OPENSSL_COMPATIBLE_DEFAULTS)
if (opts->ca.len == 0 || mg_strcmp(opts->ca, mg_str("*")) == 0) {
// Older versions require that either the CA is loaded or SSL_VERIFY_NONE
@@ -20608,7 +20630,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
}
#endif
if (opts->ca.buf != NULL && opts->ca.buf[0] != '\0') {
if (opts->ca.buf != NULL && opts->ca.len > 0 && opts->ca.buf[0] != '\0') {
SSL_set_verify(tls->ssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
NULL);
#if MG_TLS == MG_TLS_WOLFSSL
@@ -20630,6 +20652,8 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
goto fail;
}
#endif
} else {
tls->check_name = check_name; // host name set but no CA cert given
}
if (opts->cert.buf != NULL && opts->cert.buf[0] != '\0') {
@@ -20654,15 +20678,13 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
(void) SSL_set_ecdh_auto(tls->ssl, 1);
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (opts->name.len > 0) {
char *s = mg_mprintf("%.*s", (int) opts->name.len, opts->name.buf);
if (tls->name != NULL) {
#if MG_TLS != MG_TLS_WOLFSSL || LIBWOLFSSL_VERSION_HEX >= 0x05005002
SSL_set1_host(tls->ssl, s);
SSL_set1_host(tls->ssl, tls->name);
#else
X509_VERIFY_PARAM_set1_host(SSL_get0_param(tls->ssl), s, 0);
X509_VERIFY_PARAM_set1_host(SSL_get0_param(tls->ssl), tls->name, 0);
#endif
SSL_set_tlsext_host_name(tls->ssl, s);
mg_free(s);
SSL_set_tlsext_host_name(tls->ssl, tls->name);
}
#endif
#if MG_TLS == MG_TLS_WOLFSSL
@@ -20690,6 +20712,16 @@ void mg_tls_handshake(struct mg_connection *c) {
struct mg_tls *tls = (struct mg_tls *) c->tls;
int rc = c->is_client ? SSL_connect(tls->ssl) : SSL_accept(tls->ssl);
if (rc == 1) {
if (tls->check_name) { // host name set but no CA cert given
X509 *cert = SSL_get_peer_certificate(tls->ssl);
bool ok =
cert != NULL && X509_check_host(cert, tls->name, 0, 0, NULL) == 1;
X509_free(cert);
if (!ok) {
mg_error(c, "failed to verify hostname");
return;
}
}
MG_DEBUG(("%lu success", c->id));
c->is_tls_hs = 0;
mg_call(c, MG_EV_TLS_HS, NULL);