3636#include <tests/api/api.h>
3737#include <tests/api/test_ossl_x509_str.h>
3838
39+ #if (defined(OPENSSL_ALL ) || defined(WOLFSSL_QT )) && \
40+ !defined(NO_RSA ) && !defined(NO_FILESYSTEM )
41+
42+ static int last_errcode [2 ];
43+ static int last_errdepth [2 ];
44+ static int err_index = 0 ;
45+
46+ static int X509Callback (int ok , X509_STORE_CTX * ctx )
47+ {
48+
49+ if (!ok ) {
50+ last_errcode [err_index ] = X509_STORE_CTX_get_error (ctx );
51+ last_errdepth [err_index ++ ] = X509_STORE_CTX_get_error_depth (ctx );
52+ }
53+ /* Always return OK to allow verification to continue.*/
54+ return 1 ;
55+ }
56+
57+ #endif
58+
3959int test_wolfSSL_X509_STORE_CTX_set_time (void )
4060{
4161 EXPECT_DECLS ;
@@ -161,6 +181,78 @@ int test_wolfSSL_X509_STORE_check_time(void)
161181 store = NULL ;
162182 wolfSSL_X509_free (cert );
163183 cert = NULL ;
184+
185+ #if (defined(OPENSSL_ALL ) || defined(WOLFSSL_QT )) && \
186+ !defined(NO_RSA ) && !defined(NO_FILESYSTEM )
187+
188+ err_index = 0 ;
189+
190+ ExpectNotNull (store = X509_STORE_new ());
191+ ExpectNotNull (ctx = X509_STORE_CTX_new ());
192+ ExpectNotNull (ca = wolfSSL_X509_load_certificate_file (caCertFile ,
193+ SSL_FILETYPE_PEM ));
194+ ExpectIntEQ (wolfSSL_X509_STORE_add_cert (store , ca ), WOLFSSL_SUCCESS );
195+
196+ X509_STORE_set_verify_cb (store , X509Callback );
197+
198+ ExpectNotNull (cert = wolfSSL_X509_load_certificate_file (expiredCertFile ,
199+ SSL_FILETYPE_PEM ));
200+
201+ ExpectIntEQ (X509_STORE_CTX_init (ctx , store , cert , NULL ), WOLFSSL_SUCCESS );
202+ ExpectIntEQ (X509_verify_cert (ctx ), WOLFSSL_SUCCESS );
203+ /* while verifying the certificate, it should have two errors */
204+ ExpectIntEQ (err_index , 2 );
205+ /* self-signed */
206+ ExpectIntEQ (last_errcode [err_index - 2 ],
207+ WOLFSSL_X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT );
208+ /* expired */
209+ ExpectIntEQ (last_errcode [err_index - 1 ],
210+ WOLFSSL_X509_V_ERR_CERT_HAS_EXPIRED );
211+
212+ X509_STORE_CTX_free (ctx );
213+ ctx = NULL ;
214+ X509_STORE_free (store );
215+ store = NULL ;
216+ X509_free (cert );
217+ cert = NULL ;
218+ X509_free (ca );
219+ ca = NULL ;
220+
221+ err_index = 0 ;
222+
223+ ExpectNotNull (store = X509_STORE_new ());
224+ /* Set NO_CHECK_TIME flag to skip time validation */
225+ ExpectIntEQ (X509_VERIFY_PARAM_set_flags (store -> param ,
226+ WOLFSSL_NO_CHECK_TIME ), WOLFSSL_SUCCESS );
227+ ExpectTrue ((store -> param -> flags & WOLFSSL_NO_CHECK_TIME ) ==
228+ WOLFSSL_NO_CHECK_TIME );
229+ ExpectNotNull (ctx = X509_STORE_CTX_new ());
230+ ExpectNotNull (ca = wolfSSL_X509_load_certificate_file (caCertFile ,
231+ SSL_FILETYPE_PEM ));
232+ ExpectIntEQ (wolfSSL_X509_STORE_add_cert (store , ca ), WOLFSSL_SUCCESS );
233+
234+ X509_STORE_set_verify_cb (store , X509Callback );
235+
236+ ExpectNotNull (cert = wolfSSL_X509_load_certificate_file (expiredCertFile ,
237+ SSL_FILETYPE_PEM ));
238+
239+ ExpectIntEQ (X509_STORE_CTX_init (ctx , store , cert , NULL ), WOLFSSL_SUCCESS );
240+ ExpectIntEQ (X509_verify_cert (ctx ), WOLFSSL_SUCCESS );
241+ /* while verifying the certificate, it should have an error */
242+ ExpectIntEQ (err_index , 1 );
243+ /* self-signed */
244+ ExpectIntEQ (last_errcode [err_index - 1 ],
245+ WOLFSSL_X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT );
246+ /* no expired because of no_check_time */
247+ X509_STORE_CTX_free (ctx );
248+ ctx = NULL ;
249+ X509_STORE_free (store );
250+ store = NULL ;
251+ X509_free (cert );
252+ cert = NULL ;
253+ X509_free (ca );
254+ ca = NULL ;
255+ #endif
164256#endif /* OPENSSL_EXTRA && !NO_FILESYSTEM && !NO_ASN_TIME && !NO_RSA */
165257 return EXPECT_RESULT ();
166258}
@@ -919,24 +1011,6 @@ int test_X509_STORE_untrusted(void)
9191011 return EXPECT_RESULT ();
9201012}
9211013
922- #if defined(OPENSSL_ALL ) && !defined(NO_RSA ) && !defined(NO_FILESYSTEM )
923-
924- static int last_errcode ;
925- static int last_errdepth ;
926-
927- static int X509Callback (int ok , X509_STORE_CTX * ctx )
928- {
929-
930- if (!ok ) {
931- last_errcode = X509_STORE_CTX_get_error (ctx );
932- last_errdepth = X509_STORE_CTX_get_error_depth (ctx );
933- }
934- /* Always return OK to allow verification to continue.*/
935- return 1 ;
936- }
937-
938- #endif
939-
9401014int test_X509_STORE_InvalidCa (void )
9411015{
9421016 EXPECT_DECLS ;
@@ -951,9 +1025,7 @@ int test_X509_STORE_InvalidCa(void)
9511025 X509 * cert = NULL ;
9521026 STACK_OF (X509 )* untrusted = NULL ;
9531027
954- last_errcode = 0 ;
955- last_errdepth = 0 ;
956-
1028+ err_index = 0 ;
9571029 ExpectTrue ((fp = XFOPEN (srvfile , "rb" ))
9581030 != XBADFILE );
9591031 ExpectNotNull (cert = PEM_read_X509 (fp , 0 , 0 , 0 ));
@@ -978,7 +1050,8 @@ int test_X509_STORE_InvalidCa(void)
9781050
9791051 ExpectIntEQ (X509_STORE_CTX_init (ctx , str , cert , untrusted ), 1 );
9801052 ExpectIntEQ (X509_verify_cert (ctx ), 1 );
981- ExpectIntEQ (last_errcode , X509_V_ERR_INVALID_CA );
1053+ ExpectIntEQ (err_index , 1 );
1054+ ExpectIntEQ (last_errcode [err_index - 1 ], X509_V_ERR_INVALID_CA );
9821055
9831056 X509_free (cert );
9841057 X509_STORE_free (str );
0 commit comments