1c7da899bSchristos /*
2d3425df3Schristos * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
3c7da899bSchristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5c7da899bSchristos * this file except in compliance with the License. You can obtain a copy
6c7da899bSchristos * in the file LICENSE in the source distribution or at
7c7da899bSchristos * https://www.openssl.org/source/license.html
8c7da899bSchristos */
9c7da899bSchristos
10c7da899bSchristos #include <stdio.h>
11*b0d17251Schristos #include <string.h>
12c7da899bSchristos #include <openssl/crypto.h>
13c7da899bSchristos #include <openssl/bio.h>
14c7da899bSchristos #include <openssl/x509.h>
15d3425df3Schristos #include <openssl/x509v3.h>
16c7da899bSchristos #include <openssl/pem.h>
17c7da899bSchristos #include <openssl/err.h>
1813d40330Schristos #include "testutil.h"
1913d40330Schristos
20d3425df3Schristos static const char *certs_dir;
21*b0d17251Schristos static char *root_f = NULL;
22d3425df3Schristos static char *roots_f = NULL;
23d3425df3Schristos static char *untrusted_f = NULL;
24d3425df3Schristos static char *bad_f = NULL;
25*b0d17251Schristos static char *req_f = NULL;
26d3425df3Schristos static char *sroot_cert = NULL;
27d3425df3Schristos static char *ca_cert = NULL;
28d3425df3Schristos static char *ee_cert = NULL;
29f7bc30e0Schristos
30*b0d17251Schristos #define load_cert_from_file(file) load_cert_pem(file, NULL)
31c7da899bSchristos
32f7bc30e0Schristos /*-
33c7da899bSchristos * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
34c7da899bSchristos *
35c7da899bSchristos * Chain is as follows:
36c7da899bSchristos *
37c7da899bSchristos * rootCA (self-signed)
38c7da899bSchristos * |
39c7da899bSchristos * interCA
40c7da899bSchristos * |
41c7da899bSchristos * subinterCA subinterCA (self-signed)
42c7da899bSchristos * | |
43c7da899bSchristos * leaf ------------------
44c7da899bSchristos * |
45c7da899bSchristos * bad
46c7da899bSchristos *
47c7da899bSchristos * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
48c7da899bSchristos * leaf and bad have CA=FALSE
49c7da899bSchristos *
50c7da899bSchristos * subinterCA and subinterCA (ss) have the same subject name and keys
51c7da899bSchristos *
52c7da899bSchristos * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
53c7da899bSchristos * (roots.pem)
54c7da899bSchristos * leaf and subinterCA are in the untrusted list (untrusted.pem)
55c7da899bSchristos * bad is the certificate being verified (bad.pem)
56c7da899bSchristos *
57c7da899bSchristos * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
58c7da899bSchristos * CA=FALSE, and will therefore incorrectly verify bad
59c7da899bSchristos *
60c7da899bSchristos */
test_alt_chains_cert_forgery(void)6113d40330Schristos static int test_alt_chains_cert_forgery(void)
62c7da899bSchristos {
63c7da899bSchristos int ret = 0;
64c7da899bSchristos int i;
65c7da899bSchristos X509 *x = NULL;
66c7da899bSchristos STACK_OF(X509) *untrusted = NULL;
67c7da899bSchristos X509_STORE_CTX *sctx = NULL;
68c7da899bSchristos X509_STORE *store = NULL;
69c7da899bSchristos X509_LOOKUP *lookup = NULL;
70c7da899bSchristos
71c7da899bSchristos store = X509_STORE_new();
72c7da899bSchristos if (store == NULL)
73c7da899bSchristos goto err;
74c7da899bSchristos
75c7da899bSchristos lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
76c7da899bSchristos if (lookup == NULL)
77c7da899bSchristos goto err;
78c7da899bSchristos if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
79c7da899bSchristos goto err;
80c7da899bSchristos
81*b0d17251Schristos untrusted = load_certs_pem(untrusted_f);
82c7da899bSchristos
83*b0d17251Schristos if ((x = load_cert_from_file(bad_f)) == NULL)
84c7da899bSchristos goto err;
85c7da899bSchristos
86c7da899bSchristos sctx = X509_STORE_CTX_new();
87c7da899bSchristos if (sctx == NULL)
88c7da899bSchristos goto err;
89c7da899bSchristos
90c7da899bSchristos if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
91c7da899bSchristos goto err;
92c7da899bSchristos
93c7da899bSchristos i = X509_verify_cert(sctx);
94c7da899bSchristos
95*b0d17251Schristos if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) {
96c7da899bSchristos /* This is the result we were expecting: Test passed */
97c7da899bSchristos ret = 1;
98*b0d17251Schristos }
99c7da899bSchristos err:
100c7da899bSchristos X509_STORE_CTX_free(sctx);
101c7da899bSchristos X509_free(x);
102c7da899bSchristos sk_X509_pop_free(untrusted, X509_free);
103c7da899bSchristos X509_STORE_free(store);
104c7da899bSchristos return ret;
105c7da899bSchristos }
106c7da899bSchristos
test_distinguishing_id(void)107*b0d17251Schristos static int test_distinguishing_id(void)
108132cc1c4Schristos {
109132cc1c4Schristos X509 *x = NULL;
110*b0d17251Schristos int ret = 0;
111*b0d17251Schristos ASN1_OCTET_STRING *v = NULL, *v2 = NULL;
112*b0d17251Schristos char *distid = "this is an ID";
113132cc1c4Schristos
114*b0d17251Schristos x = load_cert_from_file(bad_f);
115132cc1c4Schristos if (x == NULL)
116132cc1c4Schristos goto err;
117132cc1c4Schristos
118*b0d17251Schristos v = ASN1_OCTET_STRING_new();
119*b0d17251Schristos if (v == NULL)
120132cc1c4Schristos goto err;
121132cc1c4Schristos
122*b0d17251Schristos if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid,
123*b0d17251Schristos (int)strlen(distid))) {
124*b0d17251Schristos ASN1_OCTET_STRING_free(v);
125132cc1c4Schristos goto err;
126132cc1c4Schristos }
127132cc1c4Schristos
128*b0d17251Schristos X509_set0_distinguishing_id(x, v);
129*b0d17251Schristos
130*b0d17251Schristos v2 = X509_get0_distinguishing_id(x);
131*b0d17251Schristos if (!TEST_ptr(v2)
132*b0d17251Schristos || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0))
133*b0d17251Schristos goto err;
134*b0d17251Schristos
135*b0d17251Schristos ret = 1;
136132cc1c4Schristos err:
137132cc1c4Schristos X509_free(x);
138*b0d17251Schristos return ret;
139132cc1c4Schristos }
140132cc1c4Schristos
test_req_distinguishing_id(void)141*b0d17251Schristos static int test_req_distinguishing_id(void)
142f7bc30e0Schristos {
143*b0d17251Schristos X509_REQ *x = NULL;
144*b0d17251Schristos BIO *bio = NULL;
145*b0d17251Schristos int ret = 0;
146*b0d17251Schristos ASN1_OCTET_STRING *v = NULL, *v2 = NULL;
147*b0d17251Schristos char *distid = "this is an ID";
148*b0d17251Schristos
149*b0d17251Schristos bio = BIO_new_file(req_f, "r");
150*b0d17251Schristos if (bio == NULL)
151*b0d17251Schristos goto err;
152*b0d17251Schristos
153*b0d17251Schristos x = PEM_read_bio_X509_REQ(bio, NULL, 0, NULL);
154*b0d17251Schristos if (x == NULL)
155*b0d17251Schristos goto err;
156*b0d17251Schristos
157*b0d17251Schristos v = ASN1_OCTET_STRING_new();
158*b0d17251Schristos if (v == NULL)
159*b0d17251Schristos goto err;
160*b0d17251Schristos
161*b0d17251Schristos if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid,
162*b0d17251Schristos (int)strlen(distid))) {
163*b0d17251Schristos ASN1_OCTET_STRING_free(v);
164*b0d17251Schristos goto err;
165*b0d17251Schristos }
166*b0d17251Schristos
167*b0d17251Schristos X509_REQ_set0_distinguishing_id(x, v);
168*b0d17251Schristos
169*b0d17251Schristos v2 = X509_REQ_get0_distinguishing_id(x);
170*b0d17251Schristos if (!TEST_ptr(v2)
171*b0d17251Schristos || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0))
172*b0d17251Schristos goto err;
173*b0d17251Schristos
174*b0d17251Schristos ret = 1;
175*b0d17251Schristos err:
176*b0d17251Schristos X509_REQ_free(x);
177*b0d17251Schristos BIO_free(bio);
178*b0d17251Schristos return ret;
179*b0d17251Schristos }
180*b0d17251Schristos
test_self_signed(const char * filename,int use_trusted,int expected)181*b0d17251Schristos static int test_self_signed(const char *filename, int use_trusted, int expected)
182*b0d17251Schristos {
183*b0d17251Schristos X509 *cert = load_cert_from_file(filename); /* may result in NULL */
184f7bc30e0Schristos STACK_OF(X509) *trusted = sk_X509_new_null();
185f7bc30e0Schristos X509_STORE_CTX *ctx = X509_STORE_CTX_new();
186f7bc30e0Schristos int ret;
187f7bc30e0Schristos
188*b0d17251Schristos ret = TEST_int_eq(X509_self_signed(cert, 1), expected);
189*b0d17251Schristos
190*b0d17251Schristos if (cert != NULL) {
191*b0d17251Schristos if (use_trusted)
192*b0d17251Schristos ret = ret && TEST_true(sk_X509_push(trusted, cert));
193*b0d17251Schristos ret = ret && TEST_true(X509_STORE_CTX_init(ctx, NULL, cert, NULL));
194f7bc30e0Schristos X509_STORE_CTX_set0_trusted_stack(ctx, trusted);
195f7bc30e0Schristos ret = ret && TEST_int_eq(X509_verify_cert(ctx), expected);
196*b0d17251Schristos }
197f7bc30e0Schristos
198f7bc30e0Schristos X509_STORE_CTX_free(ctx);
199f7bc30e0Schristos sk_X509_free(trusted);
200f7bc30e0Schristos X509_free(cert);
201f7bc30e0Schristos return ret;
202f7bc30e0Schristos }
203f7bc30e0Schristos
test_self_signed_good(void)204f7bc30e0Schristos static int test_self_signed_good(void)
205f7bc30e0Schristos {
206*b0d17251Schristos return test_self_signed(root_f, 1, 1);
207f7bc30e0Schristos }
208f7bc30e0Schristos
test_self_signed_bad(void)209f7bc30e0Schristos static int test_self_signed_bad(void)
210f7bc30e0Schristos {
211*b0d17251Schristos return test_self_signed(bad_f, 1, 0);
212*b0d17251Schristos }
213*b0d17251Schristos
test_self_signed_error(void)214*b0d17251Schristos static int test_self_signed_error(void)
215*b0d17251Schristos {
216*b0d17251Schristos return test_self_signed("nonexistent file name", 1, -1);
217*b0d17251Schristos }
218*b0d17251Schristos
test_store_ctx(void)219*b0d17251Schristos static int test_store_ctx(void)
220*b0d17251Schristos {
221*b0d17251Schristos /* Verifying a cert where we have no trusted certs should fail */
222*b0d17251Schristos return test_self_signed(bad_f, 0, 0);
223f7bc30e0Schristos }
224f7bc30e0Schristos
do_test_purpose(int purpose,int expected)225d3425df3Schristos static int do_test_purpose(int purpose, int expected)
226d3425df3Schristos {
227*b0d17251Schristos X509 *eecert = load_cert_from_file(ee_cert); /* may result in NULL */
228*b0d17251Schristos X509 *untrcert = load_cert_from_file(ca_cert);
229*b0d17251Schristos X509 *trcert = load_cert_from_file(sroot_cert);
230d3425df3Schristos STACK_OF(X509) *trusted = sk_X509_new_null();
231d3425df3Schristos STACK_OF(X509) *untrusted = sk_X509_new_null();
232d3425df3Schristos X509_STORE_CTX *ctx = X509_STORE_CTX_new();
233d3425df3Schristos int testresult = 0;
234d3425df3Schristos
235d3425df3Schristos if (!TEST_ptr(eecert)
236d3425df3Schristos || !TEST_ptr(untrcert)
237d3425df3Schristos || !TEST_ptr(trcert)
238d3425df3Schristos || !TEST_ptr(trusted)
239d3425df3Schristos || !TEST_ptr(untrusted)
240d3425df3Schristos || !TEST_ptr(ctx))
241d3425df3Schristos goto err;
242d3425df3Schristos
243d3425df3Schristos
244d3425df3Schristos if (!TEST_true(sk_X509_push(trusted, trcert)))
245d3425df3Schristos goto err;
246d3425df3Schristos trcert = NULL;
247d3425df3Schristos if (!TEST_true(sk_X509_push(untrusted, untrcert)))
248d3425df3Schristos goto err;
249d3425df3Schristos untrcert = NULL;
250d3425df3Schristos
251d3425df3Schristos if (!TEST_true(X509_STORE_CTX_init(ctx, NULL, eecert, untrusted)))
252d3425df3Schristos goto err;
253d3425df3Schristos
254d3425df3Schristos if (!TEST_true(X509_STORE_CTX_set_purpose(ctx, purpose)))
255d3425df3Schristos goto err;
256d3425df3Schristos
257d3425df3Schristos /*
258d3425df3Schristos * X509_STORE_CTX_set0_trusted_stack() is bady named. Despite the set0 name
259d3425df3Schristos * we are still responsible for freeing trusted after we have finished with
260d3425df3Schristos * it.
261d3425df3Schristos */
262d3425df3Schristos X509_STORE_CTX_set0_trusted_stack(ctx, trusted);
263d3425df3Schristos
264d3425df3Schristos if (!TEST_int_eq(X509_verify_cert(ctx), expected))
265d3425df3Schristos goto err;
266d3425df3Schristos
267d3425df3Schristos testresult = 1;
268d3425df3Schristos err:
269d3425df3Schristos sk_X509_pop_free(trusted, X509_free);
270d3425df3Schristos sk_X509_pop_free(untrusted, X509_free);
271d3425df3Schristos X509_STORE_CTX_free(ctx);
272d3425df3Schristos X509_free(eecert);
273d3425df3Schristos X509_free(untrcert);
274d3425df3Schristos X509_free(trcert);
275d3425df3Schristos return testresult;
276d3425df3Schristos }
277d3425df3Schristos
test_purpose_ssl_client(void)278d3425df3Schristos static int test_purpose_ssl_client(void)
279d3425df3Schristos {
280d3425df3Schristos return do_test_purpose(X509_PURPOSE_SSL_CLIENT, 0);
281d3425df3Schristos }
282d3425df3Schristos
test_purpose_ssl_server(void)283d3425df3Schristos static int test_purpose_ssl_server(void)
284d3425df3Schristos {
285d3425df3Schristos return do_test_purpose(X509_PURPOSE_SSL_SERVER, 1);
286d3425df3Schristos }
287d3425df3Schristos
test_purpose_any(void)288d3425df3Schristos static int test_purpose_any(void)
289d3425df3Schristos {
290d3425df3Schristos return do_test_purpose(X509_PURPOSE_ANY, 1);
291d3425df3Schristos }
292d3425df3Schristos
293*b0d17251Schristos OPT_TEST_DECLARE_USAGE("certs-dir\n")
294*b0d17251Schristos
setup_tests(void)29513d40330Schristos int setup_tests(void)
296c7da899bSchristos {
297*b0d17251Schristos if (!test_skip_common_options()) {
298*b0d17251Schristos TEST_error("Error parsing test options\n");
299c7da899bSchristos return 0;
300c7da899bSchristos }
30113d40330Schristos
302*b0d17251Schristos if (!TEST_ptr(certs_dir = test_get_argument(0)))
303*b0d17251Schristos return 0;
304*b0d17251Schristos
305*b0d17251Schristos if (!TEST_ptr(root_f = test_mk_file_path(certs_dir, "rootCA.pem"))
306*b0d17251Schristos || !TEST_ptr(roots_f = test_mk_file_path(certs_dir, "roots.pem"))
307d3425df3Schristos || !TEST_ptr(untrusted_f = test_mk_file_path(certs_dir, "untrusted.pem"))
308d3425df3Schristos || !TEST_ptr(bad_f = test_mk_file_path(certs_dir, "bad.pem"))
309*b0d17251Schristos || !TEST_ptr(req_f = test_mk_file_path(certs_dir, "sm2-csr.pem"))
310d3425df3Schristos || !TEST_ptr(sroot_cert = test_mk_file_path(certs_dir, "sroot-cert.pem"))
311d3425df3Schristos || !TEST_ptr(ca_cert = test_mk_file_path(certs_dir, "ca-cert.pem"))
312d3425df3Schristos || !TEST_ptr(ee_cert = test_mk_file_path(certs_dir, "ee-cert.pem")))
313d3425df3Schristos goto err;
314d3425df3Schristos
31513d40330Schristos ADD_TEST(test_alt_chains_cert_forgery);
31613d40330Schristos ADD_TEST(test_store_ctx);
317*b0d17251Schristos ADD_TEST(test_distinguishing_id);
318*b0d17251Schristos ADD_TEST(test_req_distinguishing_id);
319f7bc30e0Schristos ADD_TEST(test_self_signed_good);
320f7bc30e0Schristos ADD_TEST(test_self_signed_bad);
321*b0d17251Schristos ADD_TEST(test_self_signed_error);
322d3425df3Schristos ADD_TEST(test_purpose_ssl_client);
323d3425df3Schristos ADD_TEST(test_purpose_ssl_server);
324d3425df3Schristos ADD_TEST(test_purpose_any);
32513d40330Schristos return 1;
326d3425df3Schristos err:
327d3425df3Schristos cleanup_tests();
328d3425df3Schristos return 0;
329d3425df3Schristos }
330d3425df3Schristos
cleanup_tests(void)331d3425df3Schristos void cleanup_tests(void)
332d3425df3Schristos {
333*b0d17251Schristos OPENSSL_free(root_f);
334d3425df3Schristos OPENSSL_free(roots_f);
335d3425df3Schristos OPENSSL_free(untrusted_f);
336d3425df3Schristos OPENSSL_free(bad_f);
337*b0d17251Schristos OPENSSL_free(req_f);
338d3425df3Schristos OPENSSL_free(sroot_cert);
339d3425df3Schristos OPENSSL_free(ca_cert);
340d3425df3Schristos OPENSSL_free(ee_cert);
34113d40330Schristos }
342