Update more tests
[openssl.git] / test / verify_extra_test.c
1 /*
2  * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
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>
16 #include "testutil.h"
17 #include "test_main_custom.h"
18
19 static STACK_OF(X509) *load_certs_from_file(const char *filename)
20 {
21     STACK_OF(X509) *certs;
22     BIO *bio;
23     X509 *x;
24
25     bio = BIO_new_file(filename, "r");
26
27     if (bio == NULL) {
28         return NULL;
29     }
30
31     certs = sk_X509_new_null();
32     if (certs == NULL) {
33         BIO_free(bio);
34         return NULL;
35     }
36
37     ERR_set_mark();
38     do {
39         x = PEM_read_bio_X509(bio, NULL, 0, NULL);
40         if (x != NULL && !sk_X509_push(certs, x)) {
41             sk_X509_pop_free(certs, X509_free);
42             BIO_free(bio);
43             return NULL;
44         } else if (x == NULL) {
45             /*
46              * We probably just ran out of certs, so ignore any errors
47              * generated
48              */
49             ERR_pop_to_mark();
50         }
51     } while (x != NULL);
52
53     BIO_free(bio);
54
55     return certs;
56 }
57
58 /*
59  * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
60  *
61  * Chain is as follows:
62  *
63  * rootCA (self-signed)
64  *   |
65  * interCA
66  *   |
67  * subinterCA       subinterCA (self-signed)
68  *   |                   |
69  * leaf ------------------
70  *   |
71  * bad
72  *
73  * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
74  * leaf and bad have CA=FALSE
75  *
76  * subinterCA and subinterCA (ss) have the same subject name and keys
77  *
78  * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
79  * (roots.pem)
80  * leaf and subinterCA are in the untrusted list (untrusted.pem)
81  * bad is the certificate being verified (bad.pem)
82  *
83  * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
84  * CA=FALSE, and will therefore incorrectly verify bad
85  *
86  */
87 static int test_alt_chains_cert_forgery(const char *roots_f,
88                                         const char *untrusted_f,
89                                         const char *bad_f)
90 {
91     int ret = 0;
92     int i;
93     X509 *x = NULL;
94     STACK_OF(X509) *untrusted = NULL;
95     BIO *bio = NULL;
96     X509_STORE_CTX *sctx = NULL;
97     X509_STORE *store = NULL;
98     X509_LOOKUP *lookup = NULL;
99
100     store = X509_STORE_new();
101     if (store == NULL)
102         goto err;
103
104     lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
105     if (lookup == NULL)
106         goto err;
107     if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
108         goto err;
109
110     untrusted = load_certs_from_file(untrusted_f);
111
112     if ((bio = BIO_new_file(bad_f, "r")) == NULL)
113         goto err;
114
115     if ((x = PEM_read_bio_X509(bio, NULL, 0, NULL)) == NULL)
116         goto err;
117
118     sctx = X509_STORE_CTX_new();
119     if (sctx == NULL)
120         goto err;
121
122     if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
123         goto err;
124
125     i = X509_verify_cert(sctx);
126
127     if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) {
128         /* This is the result we were expecting: Test passed */
129         ret = 1;
130     }
131  err:
132     X509_STORE_CTX_free(sctx);
133     X509_free(x);
134     BIO_free(bio);
135     sk_X509_pop_free(untrusted, X509_free);
136     X509_STORE_free(store);
137     return ret;
138 }
139
140 int test_main(int argc, char **argv)
141 {
142     if (argc != 4) {
143         TEST_error("usage: verify_extra_test roots.pem untrusted.pem bad.pem\n");
144         return EXIT_FAILURE;
145     }
146
147     if (!TEST_true(test_alt_chains_cert_forgery(argv[1], argv[2], argv[3])))
148         return EXIT_FAILURE;
149     return EXIT_SUCCESS;
150 }