Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 1 | /* |
Matt Caswell | 6ec5fce | 2018-05-01 13:34:30 +0100 | [diff] [blame] | 2 | * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 3 | * |
Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 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 |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 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> |
Rich Salz | 4afc606 | 2017-04-11 23:12:04 -0400 | [diff] [blame] | 16 | #include "testutil.h" |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 17 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 18 | static const char *roots_f; |
| 19 | static const char *untrusted_f; |
| 20 | static const char *bad_f; |
| 21 | |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 22 | static STACK_OF(X509) *load_certs_from_file(const char *filename) |
| 23 | { |
| 24 | STACK_OF(X509) *certs; |
| 25 | BIO *bio; |
| 26 | X509 *x; |
| 27 | |
| 28 | bio = BIO_new_file(filename, "r"); |
| 29 | |
| 30 | if (bio == NULL) { |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | certs = sk_X509_new_null(); |
| 35 | if (certs == NULL) { |
| 36 | BIO_free(bio); |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | ERR_set_mark(); |
| 41 | do { |
| 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); |
| 45 | BIO_free(bio); |
| 46 | return NULL; |
| 47 | } else if (x == NULL) { |
| 48 | /* |
| 49 | * We probably just ran out of certs, so ignore any errors |
| 50 | * generated |
| 51 | */ |
| 52 | ERR_pop_to_mark(); |
| 53 | } |
| 54 | } while (x != NULL); |
| 55 | |
| 56 | BIO_free(bio); |
| 57 | |
| 58 | return certs; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery) |
| 63 | * |
| 64 | * Chain is as follows: |
| 65 | * |
| 66 | * rootCA (self-signed) |
| 67 | * | |
| 68 | * interCA |
| 69 | * | |
| 70 | * subinterCA subinterCA (self-signed) |
| 71 | * | | |
| 72 | * leaf ------------------ |
| 73 | * | |
| 74 | * bad |
| 75 | * |
| 76 | * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE |
| 77 | * leaf and bad have CA=FALSE |
| 78 | * |
| 79 | * subinterCA and subinterCA (ss) have the same subject name and keys |
| 80 | * |
| 81 | * interCA (but not rootCA) and subinterCA (ss) are in the trusted store |
| 82 | * (roots.pem) |
| 83 | * leaf and subinterCA are in the untrusted list (untrusted.pem) |
| 84 | * bad is the certificate being verified (bad.pem) |
| 85 | * |
| 86 | * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has |
| 87 | * CA=FALSE, and will therefore incorrectly verify bad |
| 88 | * |
| 89 | */ |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 90 | static int test_alt_chains_cert_forgery(void) |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 91 | { |
| 92 | int ret = 0; |
| 93 | int i; |
| 94 | X509 *x = NULL; |
| 95 | STACK_OF(X509) *untrusted = NULL; |
| 96 | BIO *bio = NULL; |
| 97 | X509_STORE_CTX *sctx = NULL; |
| 98 | X509_STORE *store = NULL; |
| 99 | X509_LOOKUP *lookup = NULL; |
| 100 | |
| 101 | store = X509_STORE_new(); |
| 102 | if (store == NULL) |
| 103 | goto err; |
| 104 | |
| 105 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()); |
| 106 | if (lookup == NULL) |
| 107 | goto err; |
FdaSilvaYY | 28b86f3 | 2016-08-24 00:17:31 +0200 | [diff] [blame] | 108 | if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM)) |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 109 | goto err; |
| 110 | |
Richard Levitte | e9daa81 | 2015-09-04 17:04:33 +0200 | [diff] [blame] | 111 | untrusted = load_certs_from_file(untrusted_f); |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 112 | |
Richard Levitte | e9daa81 | 2015-09-04 17:04:33 +0200 | [diff] [blame] | 113 | if ((bio = BIO_new_file(bad_f, "r")) == NULL) |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 114 | goto err; |
| 115 | |
FdaSilvaYY | 28b86f3 | 2016-08-24 00:17:31 +0200 | [diff] [blame] | 116 | if ((x = PEM_read_bio_X509(bio, NULL, 0, NULL)) == NULL) |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 117 | goto err; |
| 118 | |
| 119 | sctx = X509_STORE_CTX_new(); |
| 120 | if (sctx == NULL) |
| 121 | goto err; |
| 122 | |
| 123 | if (!X509_STORE_CTX_init(sctx, store, x, untrusted)) |
| 124 | goto err; |
| 125 | |
| 126 | i = X509_verify_cert(sctx); |
| 127 | |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 128 | if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) { |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 129 | /* This is the result we were expecting: Test passed */ |
| 130 | ret = 1; |
| 131 | } |
| 132 | err: |
| 133 | X509_STORE_CTX_free(sctx); |
| 134 | X509_free(x); |
| 135 | BIO_free(bio); |
| 136 | sk_X509_pop_free(untrusted, X509_free); |
| 137 | X509_STORE_free(store); |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | |
Matt Caswell | 7f6dfa1 | 2018-04-18 14:22:36 +0100 | [diff] [blame] | 141 | static int test_store_ctx(void) |
| 142 | { |
| 143 | X509_STORE_CTX *sctx = NULL; |
| 144 | X509 *x = NULL; |
| 145 | BIO *bio = NULL; |
| 146 | int testresult = 0, ret; |
| 147 | |
| 148 | bio = BIO_new_file(bad_f, "r"); |
| 149 | if (bio == NULL) |
| 150 | goto err; |
| 151 | |
| 152 | x = PEM_read_bio_X509(bio, NULL, 0, NULL); |
| 153 | if (x == NULL) |
| 154 | goto err; |
| 155 | |
| 156 | sctx = X509_STORE_CTX_new(); |
| 157 | if (sctx == NULL) |
| 158 | goto err; |
| 159 | |
| 160 | if (!X509_STORE_CTX_init(sctx, NULL, x, NULL)) |
| 161 | goto err; |
| 162 | |
| 163 | /* Verifying a cert where we have no trusted certs should fail */ |
| 164 | ret = X509_verify_cert(sctx); |
| 165 | |
| 166 | if (ret == 0) { |
| 167 | /* This is the result we were expecting: Test passed */ |
| 168 | testresult = 1; |
| 169 | } |
| 170 | |
| 171 | err: |
| 172 | X509_STORE_CTX_free(sctx); |
| 173 | X509_free(x); |
| 174 | BIO_free(bio); |
| 175 | return testresult; |
| 176 | } |
| 177 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 178 | int setup_tests(void) |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 179 | { |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 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))) { |
Rich Salz | 4afc606 | 2017-04-11 23:12:04 -0400 | [diff] [blame] | 183 | TEST_error("usage: verify_extra_test roots.pem untrusted.pem bad.pem\n"); |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 184 | return 0; |
Richard Levitte | e9daa81 | 2015-09-04 17:04:33 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 187 | ADD_TEST(test_alt_chains_cert_forgery); |
Matt Caswell | 7f6dfa1 | 2018-04-18 14:22:36 +0100 | [diff] [blame] | 188 | ADD_TEST(test_store_ctx); |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 189 | return 1; |
Matt Caswell | 593e9c6 | 2015-07-02 00:15:56 +0100 | [diff] [blame] | 190 | } |