summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/x509/verify.c
diff options
context:
space:
mode:
authorbeck <>2021-08-28 15:13:50 +0000
committerbeck <>2021-08-28 15:13:50 +0000
commit288f0a07ac11201013bee70e00135bb96e86d4a7 (patch)
tree2490e9a604a73d4098218a4ddcb273c750b4d12f /src/regress/lib/libcrypto/x509/verify.c
parent9505ca1201c33485cd795200eac090ceba10e557 (diff)
downloadopenbsd-288f0a07ac11201013bee70e00135bb96e86d4a7.tar.gz
openbsd-288f0a07ac11201013bee70e00135bb96e86d4a7.tar.bz2
openbsd-288f0a07ac11201013bee70e00135bb96e86d4a7.zip
Add a pass using the modern vfy with by_dir roots, code by me, script to
generate certdirs by jsing, and make chicken sacrifies by tb. ok tb@ jsing@
Diffstat (limited to 'src/regress/lib/libcrypto/x509/verify.c')
-rw-r--r--src/regress/lib/libcrypto/x509/verify.c44
1 files changed, 30 insertions, 14 deletions
diff --git a/src/regress/lib/libcrypto/x509/verify.c b/src/regress/lib/libcrypto/x509/verify.c
index 259854ef12..74ba603a22 100644
--- a/src/regress/lib/libcrypto/x509/verify.c
+++ b/src/regress/lib/libcrypto/x509/verify.c
@@ -1,7 +1,7 @@
1/* $OpenBSD: verify.c,v 1.6 2021/08/27 16:15:42 beck Exp $ */ 1/* $OpenBSD: verify.c,v 1.7 2021/08/28 15:13:50 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org>
4 * Copyright (c) 2020 Bob Beck <beck@openbsd.org> 4 * Copyright (c) 2020-2021 Bob Beck <beck@openbsd.org>
5 * 5 *
6 * Permission to use, copy, modify, and distribute this software for any 6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
@@ -26,9 +26,10 @@
26#include <openssl/x509v3.h> 26#include <openssl/x509v3.h>
27#include <openssl/x509_verify.h> 27#include <openssl/x509_verify.h>
28 28
29#define MODE_MODERN_VFY 0 29#define MODE_MODERN_VFY 0
30#define MODE_LEGACY_VFY 1 30#define MODE_MODERN_VFY_DIR 1
31#define MODE_VERIFY 2 31#define MODE_LEGACY_VFY 2
32#define MODE_VERIFY 3
32 33
33static int verbose = 1; 34static int verbose = 1;
34 35
@@ -100,18 +101,20 @@ verify_cert_cb(int ok, X509_STORE_CTX *xsc)
100} 101}
101 102
102static void 103static void
103verify_cert(const char *roots_file, const char *bundle_file, int *chains, 104verify_cert(const char *roots_dir, const char *roots_file,
104 int mode) 105 const char *bundle_file, int *chains, int mode)
105{ 106{
106 STACK_OF(X509) *roots = NULL, *bundle = NULL; 107 STACK_OF(X509) *roots = NULL, *bundle = NULL;
107 X509_STORE_CTX *xsc = NULL; 108 X509_STORE_CTX *xsc = NULL;
109 X509_STORE *store = NULL;
110 int verify_err, use_dir;
108 unsigned long flags; 111 unsigned long flags;
109 X509 *leaf = NULL; 112 X509 *leaf = NULL;
110 int verify_err;
111 113
112 *chains = 0; 114 *chains = 0;
115 use_dir = (mode == MODE_MODERN_VFY_DIR);
113 116
114 if (!certs_from_file(roots_file, &roots)) 117 if (!use_dir && !certs_from_file(roots_file, &roots))
115 errx(1, "failed to load roots from '%s'", roots_file); 118 errx(1, "failed to load roots from '%s'", roots_file);
116 if (!certs_from_file(bundle_file, &bundle)) 119 if (!certs_from_file(bundle_file, &bundle))
117 errx(1, "failed to load bundle from '%s'", bundle_file); 120 errx(1, "failed to load bundle from '%s'", bundle_file);
@@ -121,10 +124,16 @@ verify_cert(const char *roots_file, const char *bundle_file, int *chains,
121 124
122 if ((xsc = X509_STORE_CTX_new()) == NULL) 125 if ((xsc = X509_STORE_CTX_new()) == NULL)
123 errx(1, "X509_STORE_CTX"); 126 errx(1, "X509_STORE_CTX");
124 if (!X509_STORE_CTX_init(xsc, NULL, leaf, bundle)) { 127 if (use_dir && (store = X509_STORE_new()) == NULL)
128 errx(1, "X509_STORE");
129 if (!X509_STORE_CTX_init(xsc, store, leaf, bundle)) {
125 ERR_print_errors_fp(stderr); 130 ERR_print_errors_fp(stderr);
126 errx(1, "failed to init store context"); 131 errx(1, "failed to init store context");
127 } 132 }
133 if (use_dir) {
134 if (!X509_STORE_load_locations(store, NULL, roots_dir))
135 errx(1, "failed to set by_dir directory of %s", roots_dir);
136 }
128 if (mode == MODE_LEGACY_VFY) { 137 if (mode == MODE_LEGACY_VFY) {
129 flags = X509_VERIFY_PARAM_get_flags(xsc->param); 138 flags = X509_VERIFY_PARAM_get_flags(xsc->param);
130 flags |= X509_V_FLAG_LEGACY_VERIFY; 139 flags |= X509_V_FLAG_LEGACY_VERIFY;
@@ -137,7 +146,8 @@ verify_cert(const char *roots_file, const char *bundle_file, int *chains,
137 146
138 if (verbose) 147 if (verbose)
139 X509_STORE_CTX_set_verify_cb(xsc, verify_cert_cb); 148 X509_STORE_CTX_set_verify_cb(xsc, verify_cert_cb);
140 X509_STORE_CTX_set0_trusted_stack(xsc, roots); 149 if (!use_dir)
150 X509_STORE_CTX_set0_trusted_stack(xsc, roots);
141 if (X509_verify_cert(xsc) == 1) { 151 if (X509_verify_cert(xsc) == 1) {
142 *chains = 1; /* XXX */ 152 *chains = 1; /* XXX */
143 goto done; 153 goto done;
@@ -154,6 +164,7 @@ verify_cert(const char *roots_file, const char *bundle_file, int *chains,
154 done: 164 done:
155 sk_X509_pop_free(roots, X509_free); 165 sk_X509_pop_free(roots, X509_free);
156 sk_X509_pop_free(bundle, X509_free); 166 sk_X509_pop_free(bundle, X509_free);
167 X509_STORE_free(store);
157 X509_STORE_CTX_free(xsc); 168 X509_STORE_CTX_free(xsc);
158 X509_free(leaf); 169 X509_free(leaf);
159} 170}
@@ -394,7 +405,7 @@ struct verify_cert_test verify_cert_tests[] = {
394static int 405static int
395verify_cert_test(const char *certs_path, int mode) 406verify_cert_test(const char *certs_path, int mode)
396{ 407{
397 char *roots_file, *bundle_file; 408 char *roots_file, *bundle_file, *roots_dir;
398 struct verify_cert_test *vct; 409 struct verify_cert_test *vct;
399 int failed = 0; 410 int failed = 0;
400 int chains; 411 int chains;
@@ -409,13 +420,15 @@ verify_cert_test(const char *certs_path, int mode)
409 if (asprintf(&bundle_file, "%s/%s/bundle.pem", certs_path, 420 if (asprintf(&bundle_file, "%s/%s/bundle.pem", certs_path,
410 vct->id) == -1) 421 vct->id) == -1)
411 errx(1, "asprintf"); 422 errx(1, "asprintf");
423 if (asprintf(&roots_dir, "./%s/roots", vct->id) == -1)
424 errx(1, "asprintf");
412 425
413 fprintf(stderr, "== Test %zu (%s)\n", i, vct->id); 426 fprintf(stderr, "== Test %zu (%s)\n", i, vct->id);
414 if (mode == MODE_VERIFY) 427 if (mode == MODE_VERIFY)
415 verify_cert_new(roots_file, bundle_file, &chains); 428 verify_cert_new(roots_file, bundle_file, &chains);
416 else 429 else
417 verify_cert(roots_file, bundle_file, &chains, mode); 430 verify_cert(roots_dir, roots_file, bundle_file, &chains, mode);
418 if ((mode == 2 && chains == vct->want_chains) || 431 if ((mode == MODE_VERIFY && chains == vct->want_chains) ||
419 (chains == 0 && vct->want_chains == 0) || 432 (chains == 0 && vct->want_chains == 0) ||
420 (chains == 1 && vct->want_chains > 0)) { 433 (chains == 1 && vct->want_chains > 0)) {
421 fprintf(stderr, "INFO: Succeeded with %d chains%s\n", 434 fprintf(stderr, "INFO: Succeeded with %d chains%s\n",
@@ -432,6 +445,7 @@ verify_cert_test(const char *certs_path, int mode)
432 445
433 free(roots_file); 446 free(roots_file);
434 free(bundle_file); 447 free(bundle_file);
448 free(roots_dir);
435 } 449 }
436 450
437 return failed; 451 return failed;
@@ -451,6 +465,8 @@ main(int argc, char **argv)
451 failed |= verify_cert_test(argv[1], MODE_LEGACY_VFY); 465 failed |= verify_cert_test(argv[1], MODE_LEGACY_VFY);
452 fprintf(stderr, "\n\nTesting modern x509_vfy\n"); 466 fprintf(stderr, "\n\nTesting modern x509_vfy\n");
453 failed |= verify_cert_test(argv[1], MODE_MODERN_VFY); 467 failed |= verify_cert_test(argv[1], MODE_MODERN_VFY);
468 fprintf(stderr, "\n\nTesting modern x509_vfy by_dir\n");
469 failed |= verify_cert_test(argv[1], MODE_MODERN_VFY_DIR);
454 fprintf(stderr, "\n\nTesting x509_verify\n"); 470 fprintf(stderr, "\n\nTesting x509_verify\n");
455 failed |= verify_cert_test(argv[1], MODE_VERIFY); 471 failed |= verify_cert_test(argv[1], MODE_VERIFY);
456 472