2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include <openssl/crypto.h>
13 #include <openssl/bio.h>
14 #include <openssl/x509.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
21 static const char *root_f;
22 static const char *roots_f;
23 static const char *untrusted_f;
24 static const char *bad_f;
25 static const char *req_f;
27 static X509 *load_cert_from_file(const char *filename)
32 bio = BIO_new_file(filename, "r");
34 cert = PEM_read_bio_X509(bio, NULL, 0, NULL);
39 static STACK_OF(X509) *load_certs_from_file(const char *filename)
41 STACK_OF(X509) *certs;
45 bio = BIO_new_file(filename, "r");
51 certs = sk_X509_new_null();
59 x = PEM_read_bio_X509(bio, NULL, 0, NULL);
60 if (x != NULL && !sk_X509_push(certs, x)) {
61 sk_X509_pop_free(certs, X509_free);
64 } else if (x == NULL) {
66 * We probably just ran out of certs, so ignore any errors
79 * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
81 * Chain is as follows:
83 * rootCA (self-signed)
87 * subinterCA subinterCA (self-signed)
89 * leaf ------------------
93 * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
94 * leaf and bad have CA=FALSE
96 * subinterCA and subinterCA (ss) have the same subject name and keys
98 * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
100 * leaf and subinterCA are in the untrusted list (untrusted.pem)
101 * bad is the certificate being verified (bad.pem)
103 * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
104 * CA=FALSE, and will therefore incorrectly verify bad
107 static int test_alt_chains_cert_forgery(void)
112 STACK_OF(X509) *untrusted = NULL;
113 X509_STORE_CTX *sctx = NULL;
114 X509_STORE *store = NULL;
115 X509_LOOKUP *lookup = NULL;
117 store = X509_STORE_new();
121 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
124 if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
127 untrusted = load_certs_from_file(untrusted_f);
129 if ((x = load_cert_from_file(bad_f)) == NULL)
132 sctx = X509_STORE_CTX_new();
136 if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
139 i = X509_verify_cert(sctx);
141 if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) {
142 /* This is the result we were expecting: Test passed */
146 X509_STORE_CTX_free(sctx);
148 sk_X509_pop_free(untrusted, X509_free);
149 X509_STORE_free(store);
153 static int test_store_ctx(void)
155 X509_STORE_CTX *sctx = NULL;
157 int testresult = 0, ret;
159 x = load_cert_from_file(bad_f);
163 sctx = X509_STORE_CTX_new();
167 if (!X509_STORE_CTX_init(sctx, NULL, x, NULL))
170 /* Verifying a cert where we have no trusted certs should fail */
171 ret = X509_verify_cert(sctx);
174 /* This is the result we were expecting: Test passed */
179 X509_STORE_CTX_free(sctx);
184 OPT_TEST_DECLARE_USAGE("roots.pem untrusted.pem bad.pem\n")
186 static int test_distinguishing_id(void)
190 ASN1_OCTET_STRING *v = NULL, *v2 = NULL;
191 char *distid = "this is an ID";
193 x = load_cert_from_file(bad_f);
197 v = ASN1_OCTET_STRING_new();
201 if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid,
202 (int)strlen(distid))) {
203 ASN1_OCTET_STRING_free(v);
207 X509_set0_distinguishing_id(x, v);
209 v2 = X509_get0_distinguishing_id(x);
211 || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0))
220 static int test_req_distinguishing_id(void)
225 ASN1_OCTET_STRING *v = NULL, *v2 = NULL;
226 char *distid = "this is an ID";
228 bio = BIO_new_file(req_f, "r");
232 x = PEM_read_bio_X509_REQ(bio, NULL, 0, NULL);
236 v = ASN1_OCTET_STRING_new();
240 if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid,
241 (int)strlen(distid))) {
242 ASN1_OCTET_STRING_free(v);
246 X509_REQ_set0_distinguishing_id(x, v);
248 v2 = X509_REQ_get0_distinguishing_id(x);
250 || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0))
260 static int test_self_signed(const char *filename, int expected)
265 cert = load_cert_from_file(filename); /* may result in NULL */
266 ret = TEST_int_eq(X509_self_signed(cert, 1), expected);
271 static int test_self_signed_good(void)
273 return test_self_signed(root_f, 1);
276 static int test_self_signed_bad(void)
278 return test_self_signed(bad_f, 0);
281 static int test_self_signed_error(void)
283 return test_self_signed("nonexistent file name", -1);
286 int setup_tests(void)
288 if (!test_skip_common_options()) {
289 TEST_error("Error parsing test options\n");
293 if (!TEST_ptr(root_f = test_get_argument(0))
294 || !TEST_ptr(roots_f = test_get_argument(1))
295 || !TEST_ptr(untrusted_f = test_get_argument(2))
296 || !TEST_ptr(bad_f = test_get_argument(3))
297 || !TEST_ptr(req_f = test_get_argument(4)))
300 ADD_TEST(test_alt_chains_cert_forgery);
301 ADD_TEST(test_store_ctx);
302 ADD_TEST(test_distinguishing_id);
303 ADD_TEST(test_req_distinguishing_id);
304 ADD_TEST(test_self_signed_good);
305 ADD_TEST(test_self_signed_bad);
306 ADD_TEST(test_self_signed_error);