Add X509_self_signed(), extending and improving documenation and tests
[openssl.git] / test / verify_extra_test.c
1 /*
2  * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <string.h>
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>
17 #include "testutil.h"
18
19 DEFINE_STACK_OF(X509)
20
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;
26
27 static X509 *load_cert_from_file(const char *filename)
28 {
29     X509 *cert = NULL;
30     BIO *bio;
31
32     bio = BIO_new_file(filename, "r");
33     if (bio != NULL)
34         cert = PEM_read_bio_X509(bio, NULL, 0, NULL);
35     BIO_free(bio);
36     return cert;
37 }
38
39 static STACK_OF(X509) *load_certs_from_file(const char *filename)
40 {
41     STACK_OF(X509) *certs;
42     BIO *bio;
43     X509 *x;
44
45     bio = BIO_new_file(filename, "r");
46
47     if (bio == NULL) {
48         return NULL;
49     }
50
51     certs = sk_X509_new_null();
52     if (certs == NULL) {
53         BIO_free(bio);
54         return NULL;
55     }
56
57     ERR_set_mark();
58     do {
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);
62             BIO_free(bio);
63             return NULL;
64         } else if (x == NULL) {
65             /*
66              * We probably just ran out of certs, so ignore any errors
67              * generated
68              */
69             ERR_pop_to_mark();
70         }
71     } while (x != NULL);
72
73     BIO_free(bio);
74
75     return certs;
76 }
77
78 /*
79  * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
80  *
81  * Chain is as follows:
82  *
83  * rootCA (self-signed)
84  *   |
85  * interCA
86  *   |
87  * subinterCA       subinterCA (self-signed)
88  *   |                   |
89  * leaf ------------------
90  *   |
91  * bad
92  *
93  * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
94  * leaf and bad have CA=FALSE
95  *
96  * subinterCA and subinterCA (ss) have the same subject name and keys
97  *
98  * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
99  * (roots.pem)
100  * leaf and subinterCA are in the untrusted list (untrusted.pem)
101  * bad is the certificate being verified (bad.pem)
102  *
103  * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
104  * CA=FALSE, and will therefore incorrectly verify bad
105  *
106  */
107 static int test_alt_chains_cert_forgery(void)
108 {
109     int ret = 0;
110     int i;
111     X509 *x = NULL;
112     STACK_OF(X509) *untrusted = NULL;
113     X509_STORE_CTX *sctx = NULL;
114     X509_STORE *store = NULL;
115     X509_LOOKUP *lookup = NULL;
116
117     store = X509_STORE_new();
118     if (store == NULL)
119         goto err;
120
121     lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
122     if (lookup == NULL)
123         goto err;
124     if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
125         goto err;
126
127     untrusted = load_certs_from_file(untrusted_f);
128
129     if ((x = load_cert_from_file(bad_f)) == NULL)
130         goto err;
131
132     sctx = X509_STORE_CTX_new();
133     if (sctx == NULL)
134         goto err;
135
136     if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
137         goto err;
138
139     i = X509_verify_cert(sctx);
140
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 */
143         ret = 1;
144     }
145  err:
146     X509_STORE_CTX_free(sctx);
147     X509_free(x);
148     sk_X509_pop_free(untrusted, X509_free);
149     X509_STORE_free(store);
150     return ret;
151 }
152
153 static int test_store_ctx(void)
154 {
155     X509_STORE_CTX *sctx = NULL;
156     X509 *x = NULL;
157     int testresult = 0, ret;
158
159     x = load_cert_from_file(bad_f);
160     if (x == NULL)
161         goto err;
162
163     sctx = X509_STORE_CTX_new();
164     if (sctx == NULL)
165         goto err;
166
167     if (!X509_STORE_CTX_init(sctx, NULL, x, NULL))
168         goto err;
169
170     /* Verifying a cert where we have no trusted certs should fail */
171     ret = X509_verify_cert(sctx);
172
173     if (ret == 0) {
174         /* This is the result we were expecting: Test passed */
175         testresult = 1;
176     }
177
178  err:
179     X509_STORE_CTX_free(sctx);
180     X509_free(x);
181     return testresult;
182 }
183
184 OPT_TEST_DECLARE_USAGE("roots.pem untrusted.pem bad.pem\n")
185
186 static int test_distinguishing_id(void)
187 {
188     X509 *x = NULL;
189     int ret = 0;
190     ASN1_OCTET_STRING *v = NULL, *v2 = NULL;
191     char *distid = "this is an ID";
192
193     x = load_cert_from_file(bad_f);
194     if (x == NULL)
195         goto err;
196
197     v = ASN1_OCTET_STRING_new();
198     if (v == NULL)
199         goto err;
200
201     if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid,
202                                (int)strlen(distid))) {
203         ASN1_OCTET_STRING_free(v);
204         goto err;
205     }
206
207     X509_set0_distinguishing_id(x, v);
208
209     v2 = X509_get0_distinguishing_id(x);
210     if (!TEST_ptr(v2)
211             || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0))
212         goto err;
213
214     ret = 1;
215  err:
216     X509_free(x);
217     return ret;
218 }
219
220 static int test_req_distinguishing_id(void)
221 {
222     X509_REQ *x = NULL;
223     BIO *bio = NULL;
224     int ret = 0;
225     ASN1_OCTET_STRING *v = NULL, *v2 = NULL;
226     char *distid = "this is an ID";
227
228     bio = BIO_new_file(req_f, "r");
229     if (bio == NULL)
230         goto err;
231
232     x = PEM_read_bio_X509_REQ(bio, NULL, 0, NULL);
233     if (x == NULL)
234         goto err;
235
236     v = ASN1_OCTET_STRING_new();
237     if (v == NULL)
238         goto err;
239
240     if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid,
241                                (int)strlen(distid))) {
242         ASN1_OCTET_STRING_free(v);
243         goto err;
244     }
245
246     X509_REQ_set0_distinguishing_id(x, v);
247
248     v2 = X509_REQ_get0_distinguishing_id(x);
249     if (!TEST_ptr(v2)
250             || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0))
251         goto err;
252
253     ret = 1;
254  err:
255     X509_REQ_free(x);
256     BIO_free(bio);
257     return ret;
258 }
259
260 static int test_self_signed(const char *filename, int expected)
261 {
262     X509 *cert;
263     int ret;
264
265     cert = load_cert_from_file(filename); /* may result in NULL */
266     ret = TEST_int_eq(X509_self_signed(cert, 1), expected);
267     X509_free(cert);
268     return ret;
269 }
270
271 static int test_self_signed_good(void)
272 {
273     return test_self_signed(root_f, 1);
274 }
275
276 static int test_self_signed_bad(void)
277 {
278     return test_self_signed(bad_f, 0);
279 }
280
281 static int test_self_signed_error(void)
282 {
283     return test_self_signed("nonexistent file name", -1);
284 }
285
286 int setup_tests(void)
287 {
288     if (!test_skip_common_options()) {
289         TEST_error("Error parsing test options\n");
290         return 0;
291     }
292
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)))
298         return 0;
299
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);
307     return 1;
308 }