2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/crypto.h>
12 #include <openssl/bio.h>
13 #include <openssl/x509.h>
14 #include <openssl/pem.h>
15 #include <openssl/err.h>
18 static const char *roots_f;
19 static const char *untrusted_f;
20 static const char *bad_f;
21 static const char *good_f;
23 static X509 *load_cert_pem(const char *file)
28 if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
30 if (TEST_int_gt(BIO_read_filename(bio, file), 0))
31 (void)TEST_ptr(cert = PEM_read_bio_X509(bio, NULL, NULL, NULL));
37 static STACK_OF(X509) *load_certs_from_file(const char *filename)
39 STACK_OF(X509) *certs;
43 bio = BIO_new_file(filename, "r");
49 certs = sk_X509_new_null();
57 x = PEM_read_bio_X509(bio, NULL, 0, NULL);
58 if (x != NULL && !sk_X509_push(certs, x)) {
59 sk_X509_pop_free(certs, X509_free);
62 } else if (x == NULL) {
64 * We probably just ran out of certs, so ignore any errors
77 * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
79 * Chain is as follows:
81 * rootCA (self-signed)
85 * subinterCA subinterCA (self-signed)
87 * leaf ------------------
91 * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
92 * leaf and bad have CA=FALSE
94 * subinterCA and subinterCA (ss) have the same subject name and keys
96 * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
98 * leaf and subinterCA are in the untrusted list (untrusted.pem)
99 * bad is the certificate being verified (bad.pem)
101 * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
102 * CA=FALSE, and will therefore incorrectly verify bad
105 static int test_alt_chains_cert_forgery(void)
110 STACK_OF(X509) *untrusted = NULL;
112 X509_STORE_CTX *sctx = NULL;
113 X509_STORE *store = NULL;
114 X509_LOOKUP *lookup = NULL;
116 store = X509_STORE_new();
120 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
123 if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
126 untrusted = load_certs_from_file(untrusted_f);
128 if ((bio = BIO_new_file(bad_f, "r")) == NULL)
131 if ((x = PEM_read_bio_X509(bio, NULL, 0, NULL)) == NULL)
134 sctx = X509_STORE_CTX_new();
138 if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
141 i = X509_verify_cert(sctx);
143 if (i != 0 || X509_STORE_CTX_get_error(sctx) != X509_V_ERR_INVALID_CA)
146 /* repeat with X509_V_FLAG_X509_STRICT */
147 X509_STORE_CTX_cleanup(sctx);
148 X509_STORE_set_flags(store, X509_V_FLAG_X509_STRICT);
150 if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
153 i = X509_verify_cert(sctx);
155 if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA)
156 /* This is the result we were expecting: Test passed */
160 X509_STORE_CTX_free(sctx);
163 sk_X509_pop_free(untrusted, X509_free);
164 X509_STORE_free(store);
168 static int test_store_ctx(void)
170 X509_STORE_CTX *sctx = NULL;
173 int testresult = 0, ret;
175 bio = BIO_new_file(bad_f, "r");
179 x = PEM_read_bio_X509(bio, NULL, 0, NULL);
183 sctx = X509_STORE_CTX_new();
187 if (!X509_STORE_CTX_init(sctx, NULL, x, NULL))
190 /* Verifying a cert where we have no trusted certs should fail */
191 ret = X509_verify_cert(sctx);
194 /* This is the result we were expecting: Test passed */
199 X509_STORE_CTX_free(sctx);
205 static int test_self_signed(const char *filename, int expected)
207 X509 *cert = load_cert_pem(filename);
208 STACK_OF(X509) *trusted = sk_X509_new_null();
209 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
213 && TEST_true(sk_X509_push(trusted, cert))
214 && TEST_true(X509_STORE_CTX_init(ctx, NULL, cert, NULL));
215 X509_STORE_CTX_set0_trusted_stack(ctx, trusted);
216 ret = ret && TEST_int_eq(X509_verify_cert(ctx), expected);
218 X509_STORE_CTX_free(ctx);
219 sk_X509_free(trusted);
224 static int test_self_signed_good(void)
226 return test_self_signed(good_f, 1);
229 static int test_self_signed_bad(void)
231 return test_self_signed(bad_f, 0);
234 int setup_tests(void)
236 if (!TEST_ptr(roots_f = test_get_argument(0))
237 || !TEST_ptr(untrusted_f = test_get_argument(1))
238 || !TEST_ptr(bad_f = test_get_argument(2))
239 || !TEST_ptr(good_f = test_get_argument(3))) {
240 TEST_error("usage: verify_extra_test roots.pem untrusted.pem bad.pem good.pem\n");
244 ADD_TEST(test_alt_chains_cert_forgery);
245 ADD_TEST(test_store_ctx);
246 ADD_TEST(test_self_signed_good);
247 ADD_TEST(test_self_signed_bad);