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 static STACK_OF(X509) *load_certs_from_file(const char *filename)
19 STACK_OF(X509) *certs;
23 bio = BIO_new_file(filename, "r");
29 certs = sk_X509_new_null();
37 x = PEM_read_bio_X509(bio, NULL, 0, NULL);
38 if (x != NULL && !sk_X509_push(certs, x)) {
39 sk_X509_pop_free(certs, X509_free);
42 } else if (x == NULL) {
44 * We probably just ran out of certs, so ignore any errors
57 * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
59 * Chain is as follows:
61 * rootCA (self-signed)
65 * subinterCA subinterCA (self-signed)
67 * leaf ------------------
71 * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
72 * leaf and bad have CA=FALSE
74 * subinterCA and subinterCA (ss) have the same subject name and keys
76 * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
78 * leaf and subinterCA are in the untrusted list (untrusted.pem)
79 * bad is the certificate being verified (bad.pem)
81 * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
82 * CA=FALSE, and will therefore incorrectly verify bad
85 static int test_alt_chains_cert_forgery(const char *roots_f,
86 const char *untrusted_f,
92 STACK_OF(X509) *untrusted = NULL;
94 X509_STORE_CTX *sctx = NULL;
95 X509_STORE *store = NULL;
96 X509_LOOKUP *lookup = NULL;
98 store = X509_STORE_new();
102 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
105 if(!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
108 untrusted = load_certs_from_file(untrusted_f);
110 if ((bio = BIO_new_file(bad_f, "r")) == NULL)
113 if((x = PEM_read_bio_X509(bio, NULL, 0, NULL)) == NULL)
116 sctx = X509_STORE_CTX_new();
120 if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
123 i = X509_verify_cert(sctx);
125 if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) {
126 /* This is the result we were expecting: Test passed */
130 X509_STORE_CTX_free(sctx);
133 sk_X509_pop_free(untrusted, X509_free);
134 X509_STORE_free(store);
136 ERR_print_errors_fp(stderr);
140 int main(int argc, char **argv)
142 CRYPTO_set_mem_debug(1);
143 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
146 fprintf(stderr, "usage: verify_extra_test roots.pem untrusted.pem bad.pem\n");
150 if (!test_alt_chains_cert_forgery(argv[1], argv[2], argv[3])) {
151 fprintf(stderr, "Test alt chains cert forgery failed\n");
155 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
156 if (CRYPTO_mem_leaks_fp(stderr) <= 0)