|
36 | 36 | #include <tests/api/api.h> |
37 | 37 | #include <tests/api/test_ossl_x509_str.h> |
38 | 38 |
|
| 39 | +#if defined(OPENSSL_ALL) && \ |
| 40 | + !defined(NO_RSA) && !defined(NO_FILESYSTEM) |
| 41 | + |
| 42 | +static int last_errcodes[10]; |
| 43 | +static int last_errdepths[10]; |
| 44 | +static int err_index = 0; |
| 45 | + |
| 46 | +static int X509CallbackCount(int ok, X509_STORE_CTX *ctx) |
| 47 | +{ |
| 48 | + if (!ok) { |
| 49 | + if (err_index < 10) { |
| 50 | + last_errcodes[err_index] = X509_STORE_CTX_get_error(ctx); |
| 51 | + last_errdepths[err_index] = X509_STORE_CTX_get_error_depth(ctx); |
| 52 | + err_index++; |
| 53 | + } else { |
| 54 | + /* Should not happen in test */ |
| 55 | + WOLFSSL_MSG("Error index overflow in X509CallbackCount"); |
| 56 | + err_index = 0; |
| 57 | + } |
| 58 | + } |
| 59 | + /* Always return OK to allow verification to continue.*/ |
| 60 | + return 1; |
| 61 | +} |
| 62 | +#endif |
| 63 | + |
39 | 64 | int test_wolfSSL_X509_STORE_CTX_set_time(void) |
40 | 65 | { |
41 | 66 | EXPECT_DECLS; |
@@ -161,6 +186,78 @@ int test_wolfSSL_X509_STORE_check_time(void) |
161 | 186 | store = NULL; |
162 | 187 | wolfSSL_X509_free(cert); |
163 | 188 | cert = NULL; |
| 189 | + |
| 190 | +#if defined(OPENSSL_ALL) && \ |
| 191 | + !defined(NO_RSA) && !defined(NO_FILESYSTEM) |
| 192 | + |
| 193 | + err_index = 0; |
| 194 | + |
| 195 | + ExpectNotNull(store = X509_STORE_new()); |
| 196 | + ExpectNotNull(ctx = X509_STORE_CTX_new()); |
| 197 | + ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile, |
| 198 | + SSL_FILETYPE_PEM)); |
| 199 | + ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, ca), WOLFSSL_SUCCESS); |
| 200 | + |
| 201 | + X509_STORE_set_verify_cb(store, X509CallbackCount); |
| 202 | + |
| 203 | + ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(expiredCertFile, |
| 204 | + SSL_FILETYPE_PEM)); |
| 205 | + |
| 206 | + ExpectIntEQ(X509_STORE_CTX_init(ctx, store, cert, NULL), WOLFSSL_SUCCESS); |
| 207 | + ExpectIntEQ(X509_verify_cert(ctx), WOLFSSL_SUCCESS); |
| 208 | + /* while verifying the certificate, it should have two errors */ |
| 209 | + ExpectIntEQ(err_index, 2); |
| 210 | + /* self-signed */ |
| 211 | + ExpectIntEQ(last_errcodes[err_index - 2], |
| 212 | + WOLFSSL_X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT); |
| 213 | + /* expired */ |
| 214 | + ExpectIntEQ(last_errcodes[err_index - 1], |
| 215 | + WOLFSSL_X509_V_ERR_CERT_HAS_EXPIRED); |
| 216 | + |
| 217 | + X509_STORE_CTX_free(ctx); |
| 218 | + ctx = NULL; |
| 219 | + X509_STORE_free(store); |
| 220 | + store = NULL; |
| 221 | + X509_free(cert); |
| 222 | + cert = NULL; |
| 223 | + X509_free(ca); |
| 224 | + ca = NULL; |
| 225 | + |
| 226 | + err_index = 0; |
| 227 | + |
| 228 | + ExpectNotNull(store = X509_STORE_new()); |
| 229 | + /* Set NO_CHECK_TIME flag to skip time validation */ |
| 230 | + ExpectIntEQ(X509_VERIFY_PARAM_set_flags(store->param, |
| 231 | + WOLFSSL_NO_CHECK_TIME), WOLFSSL_SUCCESS); |
| 232 | + ExpectTrue((store->param->flags & WOLFSSL_NO_CHECK_TIME) == |
| 233 | + WOLFSSL_NO_CHECK_TIME); |
| 234 | + ExpectNotNull(ctx = X509_STORE_CTX_new()); |
| 235 | + ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile, |
| 236 | + SSL_FILETYPE_PEM)); |
| 237 | + ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, ca), WOLFSSL_SUCCESS); |
| 238 | + |
| 239 | + X509_STORE_set_verify_cb(store, X509CallbackCount); |
| 240 | + |
| 241 | + ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(expiredCertFile, |
| 242 | + SSL_FILETYPE_PEM)); |
| 243 | + |
| 244 | + ExpectIntEQ(X509_STORE_CTX_init(ctx, store, cert, NULL), WOLFSSL_SUCCESS); |
| 245 | + ExpectIntEQ(X509_verify_cert(ctx), WOLFSSL_SUCCESS); |
| 246 | + /* while verifying the certificate, it should have an error */ |
| 247 | + ExpectIntEQ(err_index, 1); |
| 248 | + /* self-signed */ |
| 249 | + ExpectIntEQ(last_errcodes[err_index - 1], |
| 250 | + WOLFSSL_X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT); |
| 251 | + /* no expired because of no_check_time */ |
| 252 | + X509_STORE_CTX_free(ctx); |
| 253 | + ctx = NULL; |
| 254 | + X509_STORE_free(store); |
| 255 | + store = NULL; |
| 256 | + X509_free(cert); |
| 257 | + cert = NULL; |
| 258 | + X509_free(ca); |
| 259 | + ca = NULL; |
| 260 | +#endif |
164 | 261 | #endif /* OPENSSL_EXTRA && !NO_FILESYSTEM && !NO_ASN_TIME && !NO_RSA */ |
165 | 262 | return EXPECT_RESULT(); |
166 | 263 | } |
|
0 commit comments