2 * Copyright 2015-2016 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 STACK_OF(X509) *load_certs_from_file(const char *filename)
20 STACK_OF(X509) *certs;
24 bio = BIO_new_file(filename, "r");
30 certs = sk_X509_new_null();
38 x = PEM_read_bio_X509(bio, NULL, 0, NULL);
39 if (x != NULL && !sk_X509_push(certs, x)) {
40 sk_X509_pop_free(certs, X509_free);
43 } else if (x == NULL) {
45 * We probably just ran out of certs, so ignore any errors
58 * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
60 * Chain is as follows:
62 * rootCA (self-signed)
66 * subinterCA subinterCA (self-signed)
68 * leaf ------------------
72 * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
73 * leaf and bad have CA=FALSE
75 * subinterCA and subinterCA (ss) have the same subject name and keys
77 * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
79 * leaf and subinterCA are in the untrusted list (untrusted.pem)
80 * bad is the certificate being verified (bad.pem)
82 * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
83 * CA=FALSE, and will therefore incorrectly verify bad
86 static int test_alt_chains_cert_forgery(const char *roots_f,
87 const char *untrusted_f,
93 STACK_OF(X509) *untrusted = NULL;
95 X509_STORE_CTX *sctx = NULL;
96 X509_STORE *store = NULL;
97 X509_LOOKUP *lookup = NULL;
99 store = X509_STORE_new();
103 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
106 if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
109 untrusted = load_certs_from_file(untrusted_f);
111 if ((bio = BIO_new_file(bad_f, "r")) == NULL)
114 if ((x = PEM_read_bio_X509(bio, NULL, 0, NULL)) == NULL)
117 sctx = X509_STORE_CTX_new();
121 if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
124 i = X509_verify_cert(sctx);
126 if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) {
127 /* This is the result we were expecting: Test passed */
131 X509_STORE_CTX_free(sctx);
134 sk_X509_pop_free(untrusted, X509_free);
135 X509_STORE_free(store);
139 int test_main(int argc, char **argv)
142 TEST_error("usage: verify_extra_test roots.pem untrusted.pem bad.pem\n");
146 if (!TEST_true(test_alt_chains_cert_forgery(argv[1], argv[2], argv[3])))