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>
17 #include "test_main_custom.h"
19 static STACK_OF(X509) *load_certs_from_file(const char *filename)
21 STACK_OF(X509) *certs;
25 bio = BIO_new_file(filename, "r");
31 certs = sk_X509_new_null();
39 x = PEM_read_bio_X509(bio, NULL, 0, NULL);
40 if (x != NULL && !sk_X509_push(certs, x)) {
41 sk_X509_pop_free(certs, X509_free);
44 } else if (x == NULL) {
46 * We probably just ran out of certs, so ignore any errors
59 * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
61 * Chain is as follows:
63 * rootCA (self-signed)
67 * subinterCA subinterCA (self-signed)
69 * leaf ------------------
73 * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
74 * leaf and bad have CA=FALSE
76 * subinterCA and subinterCA (ss) have the same subject name and keys
78 * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
80 * leaf and subinterCA are in the untrusted list (untrusted.pem)
81 * bad is the certificate being verified (bad.pem)
83 * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
84 * CA=FALSE, and will therefore incorrectly verify bad
87 static int test_alt_chains_cert_forgery(const char *roots_f,
88 const char *untrusted_f,
94 STACK_OF(X509) *untrusted = NULL;
96 X509_STORE_CTX *sctx = NULL;
97 X509_STORE *store = NULL;
98 X509_LOOKUP *lookup = NULL;
100 store = X509_STORE_new();
104 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
107 if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
110 untrusted = load_certs_from_file(untrusted_f);
112 if ((bio = BIO_new_file(bad_f, "r")) == NULL)
115 if ((x = PEM_read_bio_X509(bio, NULL, 0, NULL)) == NULL)
118 sctx = X509_STORE_CTX_new();
122 if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
125 i = X509_verify_cert(sctx);
127 if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) {
128 /* This is the result we were expecting: Test passed */
132 X509_STORE_CTX_free(sctx);
135 sk_X509_pop_free(untrusted, X509_free);
136 X509_STORE_free(store);
140 int test_main(int argc, char **argv)
143 TEST_error("usage: verify_extra_test roots.pem untrusted.pem bad.pem\n");
147 if (!TEST_true(test_alt_chains_cert_forgery(argv[1], argv[2], argv[3])))