Skip to content

Commit 5f2a76f

Browse files
committed
wolfSSL_X509_verify_cert: add host check from ctx->param
1 parent 3540d89 commit 5f2a76f

3 files changed

Lines changed: 92 additions & 1 deletion

File tree

src/x509_str.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,27 @@ int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
740740
wolfSSL_sk_X509_free(certsToUse);
741741
}
742742

743+
/* Enforce hostname / IP verification from X509_VERIFY_PARAM if set. */
744+
if (ret == WOLFSSL_SUCCESS && ctx->param != NULL) {
745+
if (ctx->param->hostName[0] != '\0') {
746+
if (wolfSSL_X509_check_host(ctx->current_cert,
747+
ctx->param->hostName,
748+
XSTRLEN(ctx->param->hostName),
749+
ctx->param->hostFlags, NULL) != WOLFSSL_SUCCESS) {
750+
ctx->error = X509_V_ERR_HOSTNAME_MISMATCH;
751+
ret = WOLFSSL_FAILURE;
752+
}
753+
}
754+
else if (ctx->param->ipasc[0] != '\0') {
755+
if (wolfSSL_X509_check_ip_asc(ctx->current_cert,
756+
ctx->param->ipasc,
757+
ctx->param->hostFlags) != WOLFSSL_SUCCESS) {
758+
ctx->error = X509_V_ERR_IP_ADDRESS_MISMATCH;
759+
ret = WOLFSSL_FAILURE;
760+
}
761+
}
762+
}
763+
743764
return ret == WOLFSSL_SUCCESS ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
744765
}
745766

tests/api/test_x509.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,74 @@ int test_x509_GetCAByAKID(void)
244244
return EXPECT_RESULT();
245245
}
246246

247+
/* Regression test: wolfSSL_X509_verify_cert() must honour the hostname set via
248+
* X509_VERIFY_PARAM_set1_host(). Before the fix the hostname was stored in
249+
* ctx->param->hostName but never consulted, so any chain-valid certificate
250+
* would pass regardless of hostname mismatch (RFC 6125 §6.4.1 violation).
251+
*
252+
* Uses existing PEM fixtures:
253+
* svrCertFile – CN=www.wolfssl.com, SAN DNS=example.com, SAN IP=127.0.0.1
254+
* caCertFile – CA that signed svrCertFile
255+
*/
256+
int test_x509_verify_cert_hostname_check(void)
257+
{
258+
EXPECT_DECLS;
259+
#if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
260+
WOLFSSL_X509_STORE* store = NULL;
261+
WOLFSSL_X509_STORE_CTX* ctx = NULL;
262+
WOLFSSL_X509* ca = NULL;
263+
WOLFSSL_X509* leaf = NULL;
264+
WOLFSSL_X509_VERIFY_PARAM* param = NULL;
265+
266+
ExpectNotNull(store = wolfSSL_X509_STORE_new());
267+
ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
268+
SSL_FILETYPE_PEM));
269+
ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, ca), WOLFSSL_SUCCESS);
270+
271+
ExpectNotNull(leaf = wolfSSL_X509_load_certificate_file(svrCertFile,
272+
SSL_FILETYPE_PEM));
273+
274+
/* Case 1: no hostname constraint – must succeed. */
275+
ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
276+
ExpectIntEQ(wolfSSL_X509_STORE_CTX_init(ctx, store, leaf, NULL),
277+
WOLFSSL_SUCCESS);
278+
ExpectIntEQ(wolfSSL_X509_verify_cert(ctx), WOLFSSL_SUCCESS);
279+
wolfSSL_X509_STORE_CTX_free(ctx);
280+
ctx = NULL;
281+
282+
/* Case 2: hostname matches a SAN DNS entry – must succeed. */
283+
ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
284+
ExpectIntEQ(wolfSSL_X509_STORE_CTX_init(ctx, store, leaf, NULL),
285+
WOLFSSL_SUCCESS);
286+
param = wolfSSL_X509_STORE_CTX_get0_param(ctx);
287+
ExpectNotNull(param);
288+
ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(param, "example.com",
289+
XSTRLEN("example.com")), WOLFSSL_SUCCESS);
290+
ExpectIntEQ(wolfSSL_X509_verify_cert(ctx), WOLFSSL_SUCCESS);
291+
wolfSSL_X509_STORE_CTX_free(ctx);
292+
ctx = NULL;
293+
294+
/* Case 3: hostname does not match – must FAIL with the right error code. */
295+
ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
296+
ExpectIntEQ(wolfSSL_X509_STORE_CTX_init(ctx, store, leaf, NULL),
297+
WOLFSSL_SUCCESS);
298+
param = wolfSSL_X509_STORE_CTX_get0_param(ctx);
299+
ExpectNotNull(param);
300+
ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(param, "wrong.com",
301+
XSTRLEN("wrong.com")), WOLFSSL_SUCCESS);
302+
ExpectIntNE(wolfSSL_X509_verify_cert(ctx), WOLFSSL_SUCCESS);
303+
ExpectIntEQ(wolfSSL_X509_STORE_CTX_get_error(ctx),
304+
X509_V_ERR_HOSTNAME_MISMATCH);
305+
wolfSSL_X509_STORE_CTX_free(ctx);
306+
ctx = NULL;
307+
308+
wolfSSL_X509_free(leaf);
309+
wolfSSL_X509_free(ca);
310+
wolfSSL_X509_STORE_free(store);
311+
#endif /* OPENSSL_EXTRA && !NO_FILESYSTEM && !NO_RSA */
312+
return EXPECT_RESULT();
313+
}
314+
247315
int test_x509_set_serialNumber(void)
248316
{
249317
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)

tests/api/test_x509.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
int test_x509_rfc2818_verification_callback(void);
2626
int test_x509_GetCAByAKID(void);
2727
int test_x509_set_serialNumber(void);
28+
int test_x509_verify_cert_hostname_check(void);
2829

2930
#define TEST_X509_DECLS \
3031
TEST_DECL_GROUP("x509", test_x509_rfc2818_verification_callback), \
3132
TEST_DECL_GROUP("x509", test_x509_GetCAByAKID), \
32-
TEST_DECL_GROUP("x509", test_x509_set_serialNumber)
33+
TEST_DECL_GROUP("x509", test_x509_set_serialNumber), \
34+
TEST_DECL_GROUP("x509", test_x509_verify_cert_hostname_check)
3335

3436
#endif /* WOLFCRYPT_TEST_X509_H */

0 commit comments

Comments
 (0)