@@ -1074,6 +1074,56 @@ int test_X509_STORE_InvalidCa(void)
10741074 ExpectIntEQ (X509_STORE_CTX_init (ctx , str , cert , untrusted ), 1 );
10751075 ExpectIntEQ (X509_verify_cert (ctx ), 1 );
10761076 ExpectIntEQ (last_errcode , X509_V_ERR_INVALID_CA );
1077+ /* Defense in depth: ctx->error must not be clobbered back to X509_V_OK
1078+ * by the later successful verification of the intermediate against the
1079+ * trusted root. The worst-seen error must persist. */
1080+ ExpectIntEQ (X509_STORE_CTX_get_error (ctx ), X509_V_ERR_INVALID_CA );
1081+
1082+ X509_free (cert );
1083+ X509_STORE_free (str );
1084+ X509_STORE_CTX_free (ctx );
1085+ sk_X509_pop_free (untrusted , NULL );
1086+ #endif
1087+ return EXPECT_RESULT ();
1088+ }
1089+
1090+ int test_X509_STORE_InvalidCa_NoCallback (void )
1091+ {
1092+ EXPECT_DECLS ;
1093+ #if defined(OPENSSL_ALL ) && !defined(NO_RSA ) && !defined(NO_FILESYSTEM )
1094+ const char * filename = "./certs/intermediate/ca_false_intermediate/"
1095+ "test_int_not_cacert.pem" ;
1096+ const char * srvfile = "./certs/intermediate/ca_false_intermediate/"
1097+ "test_sign_bynoca_srv.pem" ;
1098+ X509_STORE_CTX * ctx = NULL ;
1099+ X509_STORE * str = NULL ;
1100+ XFILE fp = XBADFILE ;
1101+ X509 * cert = NULL ;
1102+ STACK_OF (X509 )* untrusted = NULL ;
1103+
1104+ ExpectTrue ((fp = XFOPEN (srvfile , "rb" ))
1105+ != XBADFILE );
1106+ ExpectNotNull (cert = PEM_read_X509 (fp , 0 , 0 , 0 ));
1107+ if (fp != XBADFILE ) {
1108+ XFCLOSE (fp );
1109+ fp = XBADFILE ;
1110+ }
1111+
1112+ ExpectNotNull (str = X509_STORE_new ());
1113+ ExpectNotNull (ctx = X509_STORE_CTX_new ());
1114+ ExpectNotNull (untrusted = sk_X509_new_null ());
1115+
1116+ /* Create cert chain stack with an intermediate that is CA:FALSE. */
1117+ ExpectIntEQ (test_X509_STORE_untrusted_load_cert_to_stack (filename ,
1118+ untrusted ), TEST_SUCCESS );
1119+
1120+ ExpectIntEQ (X509_STORE_load_locations (str ,
1121+ "./certs/intermediate/ca_false_intermediate/test_ca.pem" ,
1122+ NULL ), 1 );
1123+ ExpectIntEQ (X509_STORE_CTX_init (ctx , str , cert , untrusted ), 1 );
1124+ /* No verify callback: verification must fail on CA:FALSE issuer. */
1125+ ExpectIntNE (X509_verify_cert (ctx ), 1 );
1126+ ExpectIntEQ (X509_STORE_CTX_get_error (ctx ), X509_V_ERR_INVALID_CA );
10771127
10781128 X509_free (cert );
10791129 X509_STORE_free (str );
0 commit comments