@@ -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 ) && \
0 commit comments