2 * Copyright 2015-2018 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
;
22 static STACK_OF(X509
) *load_certs_from_file(const char *filename
)
24 STACK_OF(X509
) *certs
;
28 bio
= BIO_new_file(filename
, "r");
34 certs
= sk_X509_new_null();
42 x
= PEM_read_bio_X509(bio
, NULL
, 0, NULL
);
43 if (x
!= NULL
&& !sk_X509_push(certs
, x
)) {
44 sk_X509_pop_free(certs
, X509_free
);
47 } else if (x
== NULL
) {
49 * We probably just ran out of certs, so ignore any errors
62 * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
64 * Chain is as follows:
66 * rootCA (self-signed)
70 * subinterCA subinterCA (self-signed)
72 * leaf ------------------
76 * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
77 * leaf and bad have CA=FALSE
79 * subinterCA and subinterCA (ss) have the same subject name and keys
81 * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
83 * leaf and subinterCA are in the untrusted list (untrusted.pem)
84 * bad is the certificate being verified (bad.pem)
86 * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
87 * CA=FALSE, and will therefore incorrectly verify bad
90 static int test_alt_chains_cert_forgery(void)
95 STACK_OF(X509
) *untrusted
= NULL
;
97 X509_STORE_CTX
*sctx
= NULL
;
98 X509_STORE
*store
= NULL
;
99 X509_LOOKUP
*lookup
= NULL
;
101 store
= X509_STORE_new();
105 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file());
108 if (!X509_LOOKUP_load_file(lookup
, roots_f
, X509_FILETYPE_PEM
))
111 untrusted
= load_certs_from_file(untrusted_f
);
113 if ((bio
= BIO_new_file(bad_f
, "r")) == NULL
)
116 if ((x
= PEM_read_bio_X509(bio
, NULL
, 0, NULL
)) == NULL
)
119 sctx
= X509_STORE_CTX_new();
123 if (!X509_STORE_CTX_init(sctx
, store
, x
, untrusted
))
126 i
= X509_verify_cert(sctx
);
128 if (i
== 0 && X509_STORE_CTX_get_error(sctx
) == X509_V_ERR_INVALID_CA
) {
129 /* This is the result we were expecting: Test passed */
133 X509_STORE_CTX_free(sctx
);
136 sk_X509_pop_free(untrusted
, X509_free
);
137 X509_STORE_free(store
);
141 static int test_store_ctx(void)
143 X509_STORE_CTX
*sctx
= NULL
;
146 int testresult
= 0, ret
;
148 bio
= BIO_new_file(bad_f
, "r");
152 x
= PEM_read_bio_X509(bio
, NULL
, 0, NULL
);
156 sctx
= X509_STORE_CTX_new();
160 if (!X509_STORE_CTX_init(sctx
, NULL
, x
, NULL
))
163 /* Verifying a cert where we have no trusted certs should fail */
164 ret
= X509_verify_cert(sctx
);
167 /* This is the result we were expecting: Test passed */
172 X509_STORE_CTX_free(sctx
);
178 int setup_tests(void)
180 if (!TEST_ptr(roots_f
= test_get_argument(0))
181 || !TEST_ptr(untrusted_f
= test_get_argument(1))
182 || !TEST_ptr(bad_f
= test_get_argument(2))) {
183 TEST_error("usage: verify_extra_test roots.pem untrusted.pem bad.pem\n");
187 ADD_TEST(test_alt_chains_cert_forgery
);
188 ADD_TEST(test_store_ctx
);