-
Notifications
You must be signed in to change notification settings - Fork 947
wolfSSL_X509_verify_cert: add host check from ctx->param
#9952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -740,6 +740,27 @@ int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx) | |||||||
| wolfSSL_sk_X509_free(certsToUse); | ||||||||
| } | ||||||||
|
|
||||||||
| /* Enforce hostname / IP verification from X509_VERIFY_PARAM if set. */ | ||||||||
| if (ret == WOLFSSL_SUCCESS && ctx->param != NULL) { | ||||||||
| if (ctx->param->hostName[0] != '\0') { | ||||||||
| if (wolfSSL_X509_check_host(ctx->current_cert, | ||||||||
| ctx->param->hostName, | ||||||||
| XSTRLEN(ctx->param->hostName), | ||||||||
| ctx->param->hostFlags, NULL) != WOLFSSL_SUCCESS) { | ||||||||
| ctx->error = X509_V_ERR_HOSTNAME_MISMATCH; | ||||||||
| ret = WOLFSSL_FAILURE; | ||||||||
| } | ||||||||
| } | ||||||||
| else if (ctx->param->ipasc[0] != '\0') { | ||||||||
| if (wolfSSL_X509_check_ip_asc(ctx->current_cert, | ||||||||
| ctx->param->ipasc, | ||||||||
| ctx->param->hostFlags) != WOLFSSL_SUCCESS) { | ||||||||
| ctx->error = X509_V_ERR_IP_ADDRESS_MISMATCH; | ||||||||
|
||||||||
| ctx->error = X509_V_ERR_IP_ADDRESS_MISMATCH; | |
| ctx->error = X509_V_ERR_IP_ADDRESS_MISMATCH; | |
| ctx->error_depth = 0; |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -244,6 +244,74 @@ int test_x509_GetCAByAKID(void) | |||||||
| return EXPECT_RESULT(); | ||||||||
| } | ||||||||
|
|
||||||||
| /* Regression test: wolfSSL_X509_verify_cert() must honour the hostname set via | ||||||||
| * X509_VERIFY_PARAM_set1_host(). Before the fix the hostname was stored in | ||||||||
| * ctx->param->hostName but never consulted, so any chain-valid certificate | ||||||||
| * would pass regardless of hostname mismatch (RFC 6125 §6.4.1 violation). | ||||||||
| * | ||||||||
| * Uses existing PEM fixtures: | ||||||||
| * svrCertFile – CN=www.wolfssl.com, SAN DNS=example.com, SAN IP=127.0.0.1 | ||||||||
| * caCertFile – CA that signed svrCertFile | ||||||||
| */ | ||||||||
| int test_x509_verify_cert_hostname_check(void) | ||||||||
| { | ||||||||
| EXPECT_DECLS; | ||||||||
| #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) | ||||||||
| WOLFSSL_X509_STORE* store = NULL; | ||||||||
| WOLFSSL_X509_STORE_CTX* ctx = NULL; | ||||||||
| WOLFSSL_X509* ca = NULL; | ||||||||
| WOLFSSL_X509* leaf = NULL; | ||||||||
| WOLFSSL_X509_VERIFY_PARAM* param = NULL; | ||||||||
|
|
||||||||
| ExpectNotNull(store = wolfSSL_X509_STORE_new()); | ||||||||
| ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile, | ||||||||
| SSL_FILETYPE_PEM)); | ||||||||
| ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, ca), WOLFSSL_SUCCESS); | ||||||||
|
|
||||||||
| ExpectNotNull(leaf = wolfSSL_X509_load_certificate_file(svrCertFile, | ||||||||
| SSL_FILETYPE_PEM)); | ||||||||
|
|
||||||||
| /* Case 1: no hostname constraint – must succeed. */ | ||||||||
| ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new()); | ||||||||
| ExpectIntEQ(wolfSSL_X509_STORE_CTX_init(ctx, store, leaf, NULL), | ||||||||
| WOLFSSL_SUCCESS); | ||||||||
| ExpectIntEQ(wolfSSL_X509_verify_cert(ctx), WOLFSSL_SUCCESS); | ||||||||
| wolfSSL_X509_STORE_CTX_free(ctx); | ||||||||
| ctx = NULL; | ||||||||
|
|
||||||||
| /* Case 2: hostname matches a SAN DNS entry – must succeed. */ | ||||||||
| ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new()); | ||||||||
| ExpectIntEQ(wolfSSL_X509_STORE_CTX_init(ctx, store, leaf, NULL), | ||||||||
| WOLFSSL_SUCCESS); | ||||||||
| param = wolfSSL_X509_STORE_CTX_get0_param(ctx); | ||||||||
| ExpectNotNull(param); | ||||||||
| ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(param, "example.com", | ||||||||
| XSTRLEN("example.com")), WOLFSSL_SUCCESS); | ||||||||
| ExpectIntEQ(wolfSSL_X509_verify_cert(ctx), WOLFSSL_SUCCESS); | ||||||||
| wolfSSL_X509_STORE_CTX_free(ctx); | ||||||||
| ctx = NULL; | ||||||||
|
|
||||||||
| /* Case 3: hostname does not match – must FAIL with the right error code. */ | ||||||||
| ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new()); | ||||||||
| ExpectIntEQ(wolfSSL_X509_STORE_CTX_init(ctx, store, leaf, NULL), | ||||||||
| WOLFSSL_SUCCESS); | ||||||||
| param = wolfSSL_X509_STORE_CTX_get0_param(ctx); | ||||||||
| ExpectNotNull(param); | ||||||||
| ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(param, "wrong.com", | ||||||||
| XSTRLEN("wrong.com")), WOLFSSL_SUCCESS); | ||||||||
| ExpectIntNE(wolfSSL_X509_verify_cert(ctx), WOLFSSL_SUCCESS); | ||||||||
| ExpectIntEQ(wolfSSL_X509_STORE_CTX_get_error(ctx), | ||||||||
| X509_V_ERR_HOSTNAME_MISMATCH); | ||||||||
|
||||||||
| X509_V_ERR_HOSTNAME_MISMATCH); | |
| X509_V_ERR_HOSTNAME_MISMATCH); | |
| ExpectIntEQ(wolfSSL_X509_STORE_CTX_get_error_depth(ctx), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When hostname verification fails, this sets ctx->error but leaves ctx->error_depth unchanged (it may contain the depth from the last chain validation step). For OpenSSL-compatible behavior, hostname mismatch should report an error depth of 0 (leaf). Set error_depth explicitly (e.g., wolfSSL_X509_STORE_CTX_set_error_depth(ctx, 0)) or use SetupStoreCtxError_ex(ctx, X509_V_ERR_HOSTNAME_MISMATCH, 0).