Skip to content

Commit 8ae4ab5

Browse files
authored
Merge pull request #11856 from ohki-saito/fix/F8873
Fail SSL_CTRL_CHAIN when a chain certificate cannot be added
2 parents f8ea7ff + 649e0ee commit 8ae4ab5

3 files changed

Lines changed: 136 additions & 16 deletions

File tree

‎src/ssl.c‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8707,11 +8707,11 @@ long wolfSSL_CTX_ctrl(WOLFSSL_CTX* ctx, int cmd, long opt, void* pt)
87078707
case SSL_CTRL_CHAIN:
87088708
#ifdef SESSION_CERTS
87098709
{
8710-
/*
8711-
* We don't care about opt here because a copy of the certificate is
8712-
* stored anyway so increasing the reference counter is not necessary.
8713-
* Just check to make sure that it is set to one of the correct values.
8714-
*/
8710+
/* Build ctx->certChain (DER copies) from sk. A reference is taken on
8711+
* each certificate because wolfSSL_CTX_add_extra_chain_cert() consumes
8712+
* one on success. The ctx takes ownership of sk only on success; on
8713+
* failure no chain is set and the caller still owns sk.
8714+
* opt: 0 = set0, 1 = set1. */
87158715
WOLF_STACK_OF(WOLFSSL_X509)* sk = (WOLF_STACK_OF(WOLFSSL_X509)*) pt;
87168716
WOLFSSL_X509* x509;
87178717
int i;
@@ -8721,35 +8721,50 @@ long wolfSSL_CTX_ctrl(WOLFSSL_CTX* ctx, int cmd, long opt, void* pt)
87218721
}
87228722
/* Clear certificate chain */
87238723
FreeDer(&ctx->certChain);
8724+
ctx->certChainCnt = 0;
87248725
if (sk) {
87258726
for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) {
87268727
x509 = wolfSSL_sk_X509_value(sk, i);
8728+
if (x509 == NULL) {
8729+
ret = WOLFSSL_FAILURE;
8730+
break;
8731+
}
87278732
/* Prevent wolfSSL_CTX_add_extra_chain_cert from freeing cert */
87288733
if (wolfSSL_X509_up_ref(x509) != 1) {
87298734
WOLFSSL_MSG("Error increasing reference count");
8730-
continue;
8735+
ret = WOLFSSL_FAILURE;
8736+
break;
87318737
}
87328738
if (wolfSSL_CTX_add_extra_chain_cert(ctx, x509) !=
87338739
WOLFSSL_SUCCESS) {
87348740
WOLFSSL_MSG("Error adding certificate to context");
87358741
/* Decrease reference count on failure */
87368742
wolfSSL_X509_free(x509);
8737-
x509 = NULL;
8743+
ret = WOLFSSL_FAILURE;
8744+
break;
87388745
}
87398746
}
8747+
x509 = NULL;
87408748
}
8741-
/* Free previous chain */
8749+
8750+
/* Free previous chain. On failure the ctx is left with no chain */
87428751
wolfSSL_sk_X509_pop_free(ctx->x509Chain, NULL);
8743-
ctx->x509Chain = sk;
8744-
if (sk && opt == 1) {
8745-
/* up all refs when opt == 1 */
8746-
for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) {
8747-
x509 = wolfSSL_sk_X509_value(sk, i);
8748-
if (wolfSSL_X509_up_ref(x509) != 1) {
8749-
WOLFSSL_MSG("Error increasing reference count");
8750-
continue;
8752+
ctx->x509Chain = NULL;
8753+
if (ret == WOLFSSL_SUCCESS) {
8754+
ctx->x509Chain = sk;
8755+
if (sk && opt == 1) {
8756+
/* up all refs when opt == 1 */
8757+
for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) {
8758+
x509 = wolfSSL_sk_X509_value(sk, i);
8759+
if (wolfSSL_X509_up_ref(x509) != 1) {
8760+
WOLFSSL_MSG("Error increasing reference count");
8761+
continue;
8762+
}
87518763
}
87528764
}
8765+
} else {
8766+
FreeDer(&ctx->certChain);
8767+
ctx->certChainCnt = 0;
87538768
}
87548769
}
87558770
#else

‎tests/api/test_ssl_cert.c‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,109 @@ int test_wolfSSL_get_peer_certificate_ref(void)
777777
#endif
778778
return EXPECT_RESULT();
779779
}
780+
/* Setting a chain via SSL_CTRL_CHAIN must fail if any certificate cannot be
781+
* added, instead of reporting success with inconsistent DER and X509 chains.
782+
*/
783+
int test_wolfSSL_CTX_set0_chain_bad_cert(void)
784+
{
785+
EXPECT_DECLS;
786+
#if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || \
787+
defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)) && \
788+
defined(SESSION_CERTS) && !defined(NO_CERTS) && !defined(NO_TLS) && \
789+
!defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
790+
defined(WOLFSSL_PEM_TO_DER) && \
791+
(!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
792+
WOLFSSL_CTX* ctx = NULL;
793+
WOLF_STACK_OF(WOLFSSL_X509)* chain = NULL;
794+
WOLF_STACK_OF(WOLFSSL_X509)* goodChain = NULL;
795+
WOLFSSL_X509* ca = NULL;
796+
WOLFSSL_X509* goodCa = NULL;
797+
WOLFSSL_X509* empty = NULL;
798+
int ret = 0;
799+
800+
#ifndef NO_WOLFSSL_CLIENT
801+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
802+
#else
803+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
804+
#endif
805+
/* Load the leaf first so the stack entries are added as chain certs. */
806+
ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
807+
WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
808+
809+
/* Set a valid chain first so the failure path must release it. */
810+
ExpectNotNull(goodChain = sk_X509_new_null());
811+
ExpectNotNull(goodCa = wolfSSL_X509_load_certificate_file(caCertFile,
812+
WOLFSSL_FILETYPE_PEM));
813+
ExpectIntGT(sk_X509_push(goodChain, goodCa), 0);
814+
if (EXPECT_FAIL()) {
815+
X509_free(goodCa);
816+
}
817+
ret = (int)SSL_CTX_set0_chain(ctx, goodChain);
818+
ExpectIntEQ(ret, WOLFSSL_SUCCESS);
819+
/* ctx owns goodChain on success */
820+
if (ret != WOLFSSL_SUCCESS) {
821+
sk_X509_pop_free(goodChain, X509_free);
822+
}
823+
824+
ExpectNotNull(chain = sk_X509_new_null());
825+
ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
826+
WOLFSSL_FILETYPE_PEM));
827+
ExpectIntGT(sk_X509_push(chain, ca), 0);
828+
if (EXPECT_FAIL()) {
829+
X509_free(ca);
830+
}
831+
/* An allocated but unpopulated X509 has no DER and cannot be added. */
832+
ExpectNotNull(empty = X509_new());
833+
ExpectIntGT(sk_X509_push(chain, empty), 0);
834+
if (EXPECT_FAIL()) {
835+
X509_free(empty);
836+
}
837+
838+
ret = (int)SSL_CTX_set0_chain(ctx, chain);
839+
ExpectIntNE(ret, WOLFSSL_SUCCESS);
840+
/* On failure, the ctx must not keep a partially built chain. */
841+
if (ctx != NULL) {
842+
ExpectNull(ctx->certChain);
843+
ExpectNull(ctx->x509Chain);
844+
ExpectIntEQ(ctx->certChainCnt, 0);
845+
}
846+
847+
/* The ctx takes ownership of the stack only on success. */
848+
if (ret != WOLFSSL_SUCCESS) {
849+
sk_X509_pop_free(chain, X509_free);
850+
}
851+
852+
/* A valid chain can be set again after the failure. */
853+
goodChain = NULL;
854+
goodCa = NULL;
855+
ExpectNotNull(goodChain = sk_X509_new_null());
856+
ExpectNotNull(goodCa = wolfSSL_X509_load_certificate_file(caCertFile,
857+
WOLFSSL_FILETYPE_PEM));
858+
ExpectIntGT(sk_X509_push(goodChain, goodCa), 0);
859+
if (EXPECT_FAIL()) {
860+
X509_free(goodCa);
861+
}
862+
ret = (int)SSL_CTX_set0_chain(ctx, goodChain);
863+
ExpectIntEQ(ret, WOLFSSL_SUCCESS);
864+
/* ctx owns goodChain on success */
865+
if (ret != WOLFSSL_SUCCESS) {
866+
sk_X509_pop_free(goodChain, X509_free);
867+
}
868+
if (ctx != NULL) {
869+
ExpectIntGT(ctx->certChainCnt, 0);
870+
}
871+
872+
/* Clearing the chain on success must also reset the chain count. */
873+
ExpectIntEQ((int)SSL_CTX_set0_chain(ctx, NULL), WOLFSSL_SUCCESS);
874+
if (ctx != NULL) {
875+
ExpectNull(ctx->certChain);
876+
ExpectIntEQ(ctx->certChainCnt, 0);
877+
}
878+
879+
wolfSSL_CTX_free(ctx);
880+
#endif
881+
return EXPECT_RESULT();
882+
}
780883

781884
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA) && \
782885
defined(WOLFSSL_DYNAMIC_PEER_CERT) && !defined(NO_RSA) && \

‎tests/api/test_ssl_cert.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int test_wolfSSL_peer_cert_copy_oom(void);
4646
int test_wolfSSL_get_peer_certificate_ref(void);
4747
int test_wolfSSL_peer_cert_on_demand(void);
4848
int test_wolfSSL_peer_cert_kept_decode_err(void);
49+
int test_wolfSSL_CTX_set0_chain_bad_cert(void);
4950
int test_wolfSSL_CTX_set_client_cert_cb(void);
5051
int test_wolfSSL_CTX_set_cert_cb(void);
5152
int test_wolfSSL_cert_setup_cb_ret(void);
@@ -98,6 +99,7 @@ int test_wolfSSL_SESSION_get0_peer_full_chain(void);
9899
TEST_DECL_GROUP("ssl_cert", test_wolfSSL_get_peer_certificate_ref), \
99100
TEST_DECL_GROUP("ssl_cert", test_wolfSSL_peer_cert_on_demand), \
100101
TEST_DECL_GROUP("ssl_cert", test_wolfSSL_peer_cert_kept_decode_err), \
102+
TEST_DECL_GROUP("ssl_cert", test_wolfSSL_CTX_set0_chain_bad_cert), \
101103
TEST_DECL_GROUP("ssl_cert", test_wolfSSL_CTX_set_client_cert_cb), \
102104
TEST_DECL_GROUP("ssl_cert", test_wolfSSL_CTX_set_cert_cb), \
103105
TEST_DECL_GROUP("ssl_cert", test_wolfSSL_cert_setup_cb_ret), \

0 commit comments

Comments
 (0)