Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 | |
Jesse Selover | b4810de | 2018-06-18 17:31:39 -0400 | [diff] [blame] | 57 | #include <ctype.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 58 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 59 | #include <time.h> |
| 60 | |
| 61 | #include <openssl/asn1.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 62 | #include <openssl/err.h> |
| 63 | #include <openssl/evp.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 64 | #include <openssl/mem.h> |
| 65 | #include <openssl/obj.h> |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame] | 66 | #include <openssl/thread.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 67 | #include <openssl/x509.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | |
Brian Smith | a039d70 | 2015-01-29 15:03:18 -0800 | [diff] [blame] | 69 | #include "../internal.h" |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 70 | #include "internal.h" |
Brian Smith | a039d70 | 2015-01-29 15:03:18 -0800 | [diff] [blame] | 71 | |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 72 | static CRYPTO_EX_DATA_CLASS g_ex_data_class = |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 73 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 74 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 75 | // CRL score values |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 76 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 77 | // No unhandled critical extensions |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 78 | #define CRL_SCORE_NOCRITICAL 0x100 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 79 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 80 | // certificate is within CRL scope |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 81 | #define CRL_SCORE_SCOPE 0x080 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 82 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 83 | // CRL times valid |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 84 | #define CRL_SCORE_TIME 0x040 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 85 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 86 | // Issuer name matches certificate |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 87 | #define CRL_SCORE_ISSUER_NAME 0x020 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 88 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 89 | // If this score or above CRL is probably valid |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 90 | #define CRL_SCORE_VALID \ |
| 91 | (CRL_SCORE_NOCRITICAL | CRL_SCORE_TIME | CRL_SCORE_SCOPE) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 92 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 93 | // CRL issuer is certificate issuer |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 94 | #define CRL_SCORE_ISSUER_CERT 0x018 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 95 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 96 | // CRL issuer is on certificate path |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 97 | #define CRL_SCORE_SAME_PATH 0x008 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 98 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 99 | // CRL issuer matches CRL AKID |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 100 | #define CRL_SCORE_AKID 0x004 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 101 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 102 | static int null_callback(int ok, X509_STORE_CTX *e); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 103 | static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x); |
| 104 | static int check_chain_extensions(X509_STORE_CTX *ctx); |
| 105 | static int check_name_constraints(X509_STORE_CTX *ctx); |
| 106 | static int check_id(X509_STORE_CTX *ctx); |
| 107 | static int check_trust(X509_STORE_CTX *ctx); |
| 108 | static int check_revocation(X509_STORE_CTX *ctx); |
| 109 | static int check_cert(X509_STORE_CTX *ctx); |
| 110 | static int check_policy(X509_STORE_CTX *ctx); |
| 111 | |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 112 | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl, |
| 113 | X509 *x); |
David Benjamin | d9a58a1 | 2023-11-12 14:04:00 -0500 | [diff] [blame] | 114 | static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x); |
David Benjamin | a4851dd | 2023-11-12 15:51:49 -0500 | [diff] [blame] | 115 | static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer, |
| 116 | int *pcrl_score); |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 117 | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score); |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 118 | static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | |
| 120 | static int internal_verify(X509_STORE_CTX *ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 122 | static int null_callback(int ok, X509_STORE_CTX *e) { return ok; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 124 | // cert_self_signed checks if |x| is self-signed. If |x| is valid, it returns |
| 125 | // one and sets |*out_is_self_signed| to the result. If |x| is invalid, it |
| 126 | // returns zero. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 127 | static int cert_self_signed(X509 *x, int *out_is_self_signed) { |
| 128 | if (!x509v3_cache_extensions(x)) { |
| 129 | return 0; |
| 130 | } |
| 131 | *out_is_self_signed = (x->ex_flags & EXFLAG_SS) != 0; |
| 132 | return 1; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 133 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 135 | // Given a certificate try and find an exact match in the store |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 137 | static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x) { |
| 138 | STACK_OF(X509) *certs; |
| 139 | X509 *xtmp = NULL; |
| 140 | size_t i; |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 141 | // Lookup all certs with matching subject name |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 142 | certs = X509_STORE_get1_certs(ctx, X509_get_subject_name(x)); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 143 | if (certs == NULL) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 144 | return NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 145 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 146 | // Look for exact match |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 147 | for (i = 0; i < sk_X509_num(certs); i++) { |
| 148 | xtmp = sk_X509_value(certs, i); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 149 | if (!X509_cmp(xtmp, x)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 150 | break; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 151 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 152 | } |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 153 | if (i < sk_X509_num(certs)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 154 | X509_up_ref(xtmp); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 155 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 156 | xtmp = NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 157 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 158 | sk_X509_pop_free(certs, X509_free); |
| 159 | return xtmp; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 160 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 161 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 162 | int X509_verify_cert(X509_STORE_CTX *ctx) { |
| 163 | X509 *x, *xtmp, *xtmp2, *chain_ss = NULL; |
| 164 | int bad_chain = 0; |
| 165 | X509_VERIFY_PARAM *param = ctx->param; |
| 166 | int depth, i, ok = 0; |
| 167 | int num, j, retry, trust; |
| 168 | STACK_OF(X509) *sktmp = NULL; |
Bob Beck | 6686352 | 2022-04-21 09:37:41 -0600 | [diff] [blame] | 169 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 170 | if (ctx->cert == NULL) { |
| 171 | OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); |
| 172 | ctx->error = X509_V_ERR_INVALID_CALL; |
| 173 | return -1; |
| 174 | } |
David Benjamin | f861499 | 2023-11-12 13:43:33 -0500 | [diff] [blame] | 175 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 176 | if (ctx->chain != NULL) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 177 | // This X509_STORE_CTX has already been used to verify a cert. We |
| 178 | // cannot do another one. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 179 | OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 180 | ctx->error = X509_V_ERR_INVALID_CALL; |
| 181 | return -1; |
| 182 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 183 | |
David Benjamin | f861499 | 2023-11-12 13:43:33 -0500 | [diff] [blame] | 184 | if (ctx->param->flags & |
| 185 | (X509_V_FLAG_EXTENDED_CRL_SUPPORT | X509_V_FLAG_USE_DELTAS)) { |
| 186 | // We do not support indirect or delta CRLs. The flags still exist for |
| 187 | // compatibility with bindings libraries, but to ensure we do not |
| 188 | // inadvertently skip a CRL check that the caller expects, fail closed. |
| 189 | OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 190 | ctx->error = X509_V_ERR_INVALID_CALL; |
| 191 | return -1; |
| 192 | } |
| 193 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 194 | // first we make sure the chain we are going to build is present and that |
| 195 | // the first entry is in place |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 196 | ctx->chain = sk_X509_new_null(); |
| 197 | if (ctx->chain == NULL || !sk_X509_push(ctx->chain, ctx->cert)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 198 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
| 199 | goto end; |
| 200 | } |
| 201 | X509_up_ref(ctx->cert); |
| 202 | ctx->last_untrusted = 1; |
| 203 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 204 | // We use a temporary STACK so we can chop and hack at it. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 205 | if (ctx->untrusted != NULL && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 206 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
| 207 | goto end; |
| 208 | } |
| 209 | |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 210 | num = (int)sk_X509_num(ctx->chain); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 211 | x = sk_X509_value(ctx->chain, num - 1); |
| 212 | depth = param->depth; |
| 213 | |
| 214 | for (;;) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 215 | // If we have enough, we break |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 216 | if (depth < num) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 217 | break; // FIXME: If this happens, we should take |
| 218 | // note of it and, if appropriate, use the |
| 219 | // X509_V_ERR_CERT_CHAIN_TOO_LONG error code |
| 220 | // later. |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 221 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 222 | |
| 223 | int is_self_signed; |
| 224 | if (!cert_self_signed(x, &is_self_signed)) { |
| 225 | ctx->error = X509_V_ERR_INVALID_EXTENSION; |
| 226 | goto end; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 227 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 228 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 229 | // If we are self signed, we break |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 230 | if (is_self_signed) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 231 | break; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 232 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 233 | // If asked see if we can find issuer in trusted store first |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 234 | if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) { |
| 235 | ok = ctx->get_issuer(&xtmp, ctx, x); |
| 236 | if (ok < 0) { |
| 237 | ctx->error = X509_V_ERR_STORE_LOOKUP; |
| 238 | goto end; |
| 239 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 240 | // If successful for now free up cert so it will be picked up |
| 241 | // again later. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 242 | if (ok > 0) { |
| 243 | X509_free(xtmp); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 244 | break; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 245 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 246 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 247 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 248 | // If we were passed a cert chain, use it first |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 249 | if (sktmp != NULL) { |
| 250 | xtmp = find_issuer(ctx, sktmp, x); |
| 251 | if (xtmp != NULL) { |
| 252 | if (!sk_X509_push(ctx->chain, xtmp)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 253 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
| 254 | ok = 0; |
| 255 | goto end; |
| 256 | } |
| 257 | X509_up_ref(xtmp); |
| 258 | (void)sk_X509_delete_ptr(sktmp, xtmp); |
| 259 | ctx->last_untrusted++; |
| 260 | x = xtmp; |
| 261 | num++; |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 262 | // reparse the full chain for the next one |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | } |
| 266 | break; |
| 267 | } |
David Benjamin | fd86eaa | 2020-06-17 15:13:16 -0400 | [diff] [blame] | 268 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 269 | // Remember how many untrusted certs we have |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 270 | j = num; |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 271 | // at this point, chain should contain a list of untrusted certificates. |
| 272 | // We now need to add at least one trusted one, if possible, otherwise we |
| 273 | // complain. |
David Benjamin | fd86eaa | 2020-06-17 15:13:16 -0400 | [diff] [blame] | 274 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 275 | do { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 276 | // Examine last certificate in chain and see if it is self signed. |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 277 | i = (int)sk_X509_num(ctx->chain); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 278 | x = sk_X509_value(ctx->chain, i - 1); |
| 279 | |
| 280 | int is_self_signed; |
| 281 | if (!cert_self_signed(x, &is_self_signed)) { |
| 282 | ctx->error = X509_V_ERR_INVALID_EXTENSION; |
| 283 | goto end; |
| 284 | } |
| 285 | |
| 286 | if (is_self_signed) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 287 | // we have a self signed certificate |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 288 | if (sk_X509_num(ctx->chain) == 1) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 289 | // We have a single self signed certificate: see if we can |
| 290 | // find it in the store. We must have an exact match to avoid |
| 291 | // possible impersonation. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 292 | ok = ctx->get_issuer(&xtmp, ctx, x); |
| 293 | if ((ok <= 0) || X509_cmp(x, xtmp)) { |
| 294 | ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; |
| 295 | ctx->current_cert = x; |
| 296 | ctx->error_depth = i - 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 297 | if (ok == 1) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 298 | X509_free(xtmp); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 299 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 300 | bad_chain = 1; |
| 301 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 302 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 303 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 304 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 305 | } else { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 306 | // We have a match: replace certificate with store |
| 307 | // version so we get any trust settings. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 308 | X509_free(x); |
| 309 | x = xtmp; |
| 310 | (void)sk_X509_set(ctx->chain, i - 1, x); |
| 311 | ctx->last_untrusted = 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 312 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 313 | } else { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 314 | // extract and save self signed certificate for later use |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 315 | chain_ss = sk_X509_pop(ctx->chain); |
| 316 | ctx->last_untrusted--; |
| 317 | num--; |
| 318 | j--; |
| 319 | x = sk_X509_value(ctx->chain, num - 1); |
| 320 | } |
| 321 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 322 | // We now lookup certs from the certificate store |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 323 | for (;;) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 324 | // If we have enough, we break |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 325 | if (depth < num) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 326 | break; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 327 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 328 | if (!cert_self_signed(x, &is_self_signed)) { |
| 329 | ctx->error = X509_V_ERR_INVALID_EXTENSION; |
| 330 | goto end; |
| 331 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 332 | // If we are self signed, we break |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 333 | if (is_self_signed) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 334 | break; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 335 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 336 | ok = ctx->get_issuer(&xtmp, ctx, x); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 338 | if (ok < 0) { |
| 339 | ctx->error = X509_V_ERR_STORE_LOOKUP; |
| 340 | goto end; |
| 341 | } |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 342 | if (ok == 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 343 | break; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 344 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 345 | x = xtmp; |
| 346 | if (!sk_X509_push(ctx->chain, x)) { |
| 347 | X509_free(xtmp); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 348 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
| 349 | ok = 0; |
| 350 | goto end; |
| 351 | } |
| 352 | num++; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 353 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 354 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 355 | // we now have our chain, lets check it... |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 356 | trust = check_trust(ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 357 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 358 | // If explicitly rejected error |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 359 | if (trust == X509_TRUST_REJECTED) { |
| 360 | ok = 0; |
| 361 | goto end; |
| 362 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 363 | // If it's not explicitly trusted then check if there is an alternative |
| 364 | // chain that could be used. We only do this if we haven't already |
| 365 | // checked via TRUSTED_FIRST and the user hasn't switched off alternate |
| 366 | // chain checking |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 367 | retry = 0; |
| 368 | if (trust != X509_TRUST_TRUSTED && |
| 369 | !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) && |
| 370 | !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) { |
| 371 | while (j-- > 1) { |
| 372 | xtmp2 = sk_X509_value(ctx->chain, j - 1); |
| 373 | ok = ctx->get_issuer(&xtmp, ctx, xtmp2); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 374 | if (ok < 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 375 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 376 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 377 | // Check if we found an alternate chain |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 378 | if (ok > 0) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 379 | // Free up the found cert we'll add it again later |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 380 | X509_free(xtmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 382 | // Dump all the certs above this point - we've found an |
| 383 | // alternate chain |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 384 | while (num > j) { |
| 385 | xtmp = sk_X509_pop(ctx->chain); |
| 386 | X509_free(xtmp); |
| 387 | num--; |
| 388 | } |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 389 | ctx->last_untrusted = (int)sk_X509_num(ctx->chain); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 390 | retry = 1; |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } while (retry); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 396 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 397 | // If not explicitly trusted then indicate error unless it's a single |
| 398 | // self signed certificate in which case we've indicated an error already |
| 399 | // and set bad_chain == 1 |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 400 | if (trust != X509_TRUST_TRUSTED && !bad_chain) { |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 401 | if (chain_ss == NULL || |
| 402 | !x509_check_issued_with_callback(ctx, x, chain_ss)) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 403 | if (ctx->last_untrusted >= num) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 404 | ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 405 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 406 | ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 407 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 408 | ctx->current_cert = x; |
| 409 | } else { |
| 410 | sk_X509_push(ctx->chain, chain_ss); |
| 411 | num++; |
| 412 | ctx->last_untrusted = num; |
| 413 | ctx->current_cert = chain_ss; |
| 414 | ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; |
| 415 | chain_ss = NULL; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 416 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 417 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 418 | ctx->error_depth = num - 1; |
| 419 | bad_chain = 1; |
| 420 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 421 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 422 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 423 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 424 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 426 | // We have the chain complete: now we need to check its purpose |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 427 | ok = check_chain_extensions(ctx); |
Martin Kreichgauer | b86be36 | 2017-08-14 15:55:48 -0700 | [diff] [blame] | 428 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 429 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 430 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 431 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 432 | |
| 433 | ok = check_id(ctx); |
| 434 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 435 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 436 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 437 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 438 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 439 | // Check revocation status: we do this after copying parameters because |
| 440 | // they may be needed for CRL signature verification. |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 441 | ok = check_revocation(ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 442 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 443 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 444 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 445 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 446 | // At this point, we have a chain and need to verify it |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 447 | ok = internal_verify(ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 448 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 449 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 450 | } |
David Benjamin | dd1ca99 | 2015-02-16 21:15:53 -0500 | [diff] [blame] | 451 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 452 | // Check name constraints |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 453 | ok = check_name_constraints(ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 454 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 455 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 456 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 457 | |
David Benjamin | 28226f5 | 2023-03-29 14:04:44 +0900 | [diff] [blame] | 458 | // If we get this far, evaluate policies. |
| 459 | if (!bad_chain) { |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 460 | ok = check_policy(ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 461 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 462 | |
| 463 | end: |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 464 | if (sktmp != NULL) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 465 | sk_X509_free(sktmp); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 466 | } |
| 467 | if (chain_ss != NULL) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 468 | X509_free(chain_ss); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 469 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 470 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 471 | // Safety net, error returns must set ctx->error |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 472 | if (ok <= 0 && ctx->error == X509_V_OK) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 473 | ctx->error = X509_V_ERR_UNSPECIFIED; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 474 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 475 | return ok; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 476 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 478 | // Given a STACK_OF(X509) find the issuer of cert (if any) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 479 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 480 | static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x) { |
| 481 | size_t i; |
| 482 | X509 *issuer; |
| 483 | for (i = 0; i < sk_X509_num(sk); i++) { |
| 484 | issuer = sk_X509_value(sk, i); |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 485 | if (x509_check_issued_with_callback(ctx, x, issuer)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 486 | return issuer; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 487 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 488 | } |
| 489 | return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 490 | } |
| 491 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 492 | // Given a possible certificate and issuer check them |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 494 | int x509_check_issued_with_callback(X509_STORE_CTX *ctx, X509 *x, |
| 495 | X509 *issuer) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 496 | int ret; |
| 497 | ret = X509_check_issued(issuer, x); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 498 | if (ret == X509_V_OK) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 499 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 500 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 501 | // If we haven't asked for issuer errors don't set ctx |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 502 | if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 503 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 504 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 505 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 506 | ctx->error = ret; |
| 507 | ctx->current_cert = x; |
| 508 | ctx->current_issuer = issuer; |
| 509 | return ctx->verify_cb(0, ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 510 | } |
| 511 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 512 | // Alternative lookup method: look from a STACK stored in other_ctx |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 513 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 514 | static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) { |
| 515 | *issuer = find_issuer(ctx, ctx->other_ctx, x); |
| 516 | if (*issuer) { |
| 517 | X509_up_ref(*issuer); |
| 518 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 519 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 520 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 521 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 522 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 523 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 524 | // Check a certificate chains extensions for consistency with the supplied |
| 525 | // purpose |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 526 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 527 | static int check_chain_extensions(X509_STORE_CTX *ctx) { |
David Benjamin | ca1690e | 2023-02-14 16:07:45 -0500 | [diff] [blame] | 528 | int ok = 0, plen = 0; |
David Benjamin | a4851dd | 2023-11-12 15:51:49 -0500 | [diff] [blame] | 529 | int purpose = ctx->param->purpose; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 530 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 531 | // Check all untrusted certificates |
David Benjamin | ca1690e | 2023-02-14 16:07:45 -0500 | [diff] [blame] | 532 | for (int i = 0; i < ctx->last_untrusted; i++) { |
| 533 | X509 *x = sk_X509_value(ctx->chain, i); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 534 | if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && |
| 535 | (x->ex_flags & EXFLAG_CRITICAL)) { |
| 536 | ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION; |
| 537 | ctx->error_depth = i; |
| 538 | ctx->current_cert = x; |
| 539 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 540 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 541 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 542 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 543 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | |
David Benjamin | ca1690e | 2023-02-14 16:07:45 -0500 | [diff] [blame] | 545 | int must_be_ca = i > 0; |
| 546 | if (must_be_ca && !X509_check_ca(x)) { |
| 547 | ctx->error = X509_V_ERR_INVALID_CA; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 548 | ctx->error_depth = i; |
| 549 | ctx->current_cert = x; |
| 550 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 551 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 552 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 553 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 554 | } |
David Benjamin | ca1690e | 2023-02-14 16:07:45 -0500 | [diff] [blame] | 555 | if (ctx->param->purpose > 0 && |
| 556 | X509_check_purpose(x, purpose, must_be_ca) != 1) { |
| 557 | ctx->error = X509_V_ERR_INVALID_PURPOSE; |
| 558 | ctx->error_depth = i; |
| 559 | ctx->current_cert = x; |
| 560 | ok = ctx->verify_cb(0, ctx); |
| 561 | if (!ok) { |
| 562 | goto end; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 563 | } |
| 564 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 565 | // Check pathlen if not self issued |
David Benjamin | ca1690e | 2023-02-14 16:07:45 -0500 | [diff] [blame] | 566 | if (i > 1 && !(x->ex_flags & EXFLAG_SI) && x->ex_pathlen != -1 && |
| 567 | plen > x->ex_pathlen + 1) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 568 | ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; |
| 569 | ctx->error_depth = i; |
| 570 | ctx->current_cert = x; |
| 571 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 572 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 573 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 574 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 575 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 576 | // Increment path length if not self issued |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 577 | if (!(x->ex_flags & EXFLAG_SI)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 578 | plen++; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 579 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 580 | } |
| 581 | ok = 1; |
| 582 | end: |
| 583 | return ok; |
| 584 | } |
| 585 | |
| 586 | static int reject_dns_name_in_common_name(X509 *x509) { |
David Benjamin | b6f47e8 | 2022-07-09 00:57:54 -0400 | [diff] [blame] | 587 | const X509_NAME *name = X509_get_subject_name(x509); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 588 | int i = -1; |
| 589 | for (;;) { |
| 590 | i = X509_NAME_get_index_by_NID(name, NID_commonName, i); |
| 591 | if (i == -1) { |
| 592 | return X509_V_OK; |
| 593 | } |
| 594 | |
David Benjamin | b6f47e8 | 2022-07-09 00:57:54 -0400 | [diff] [blame] | 595 | const X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i); |
| 596 | const ASN1_STRING *common_name = X509_NAME_ENTRY_get_data(entry); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 597 | unsigned char *idval; |
| 598 | int idlen = ASN1_STRING_to_UTF8(&idval, common_name); |
| 599 | if (idlen < 0) { |
| 600 | return X509_V_ERR_OUT_OF_MEM; |
| 601 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 602 | // Only process attributes that look like host names. Note it is |
| 603 | // important that this check be mirrored in |X509_check_host|. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 604 | int looks_like_dns = x509v3_looks_like_dns_name(idval, (size_t)idlen); |
| 605 | OPENSSL_free(idval); |
| 606 | if (looks_like_dns) { |
| 607 | return X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | static int check_name_constraints(X509_STORE_CTX *ctx) { |
| 613 | int i, j, rv; |
| 614 | int has_name_constraints = 0; |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 615 | // Check name constraints for all certificates |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 616 | for (i = (int)sk_X509_num(ctx->chain) - 1; i >= 0; i--) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 617 | X509 *x = sk_X509_value(ctx->chain, i); |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 618 | // Ignore self issued certs unless last in chain |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 619 | if (i && (x->ex_flags & EXFLAG_SI)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 620 | continue; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 621 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 622 | // Check against constraints for all certificates higher in chain |
| 623 | // including trust anchor. Trust anchor not strictly speaking needed |
| 624 | // but if it includes constraints it is to be assumed it expects them |
| 625 | // to be obeyed. |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 626 | for (j = (int)sk_X509_num(ctx->chain) - 1; j > i; j--) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 627 | NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc; |
| 628 | if (nc) { |
| 629 | has_name_constraints = 1; |
| 630 | rv = NAME_CONSTRAINTS_check(x, nc); |
| 631 | switch (rv) { |
| 632 | case X509_V_OK: |
| 633 | continue; |
| 634 | case X509_V_ERR_OUT_OF_MEM: |
| 635 | ctx->error = rv; |
| 636 | return 0; |
| 637 | default: |
| 638 | ctx->error = rv; |
| 639 | ctx->error_depth = i; |
| 640 | ctx->current_cert = x; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 641 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 642 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 643 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 644 | break; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 650 | // Name constraints do not match against the common name, but |
| 651 | // |X509_check_host| still implements the legacy behavior where, on |
| 652 | // certificates lacking a SAN list, DNS-like names in the common name are |
| 653 | // checked instead. |
| 654 | // |
| 655 | // While we could apply the name constraints to the common name, name |
| 656 | // constraints are rare enough that can hold such certificates to a higher |
| 657 | // standard. Note this does not make "DNS-like" heuristic failures any |
| 658 | // worse. A decorative common-name misidentified as a DNS name would fail |
| 659 | // the name constraint anyway. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 660 | X509 *leaf = sk_X509_value(ctx->chain, 0); |
| 661 | if (has_name_constraints && leaf->altname == NULL) { |
| 662 | rv = reject_dns_name_in_common_name(leaf); |
| 663 | switch (rv) { |
| 664 | case X509_V_OK: |
| 665 | break; |
| 666 | case X509_V_ERR_OUT_OF_MEM: |
| 667 | ctx->error = rv; |
| 668 | return 0; |
| 669 | default: |
| 670 | ctx->error = rv; |
| 671 | ctx->error_depth = i; |
| 672 | ctx->current_cert = leaf; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 673 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 674 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 675 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 676 | break; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return 1; |
| 681 | } |
| 682 | |
| 683 | static int check_id_error(X509_STORE_CTX *ctx, int errcode) { |
| 684 | ctx->error = errcode; |
| 685 | ctx->current_cert = ctx->cert; |
| 686 | ctx->error_depth = 0; |
| 687 | return ctx->verify_cb(0, ctx); |
| 688 | } |
| 689 | |
| 690 | static int check_hosts(X509 *x, X509_VERIFY_PARAM *param) { |
| 691 | size_t i; |
| 692 | size_t n = sk_OPENSSL_STRING_num(param->hosts); |
| 693 | char *name; |
| 694 | |
| 695 | if (param->peername != NULL) { |
| 696 | OPENSSL_free(param->peername); |
| 697 | param->peername = NULL; |
| 698 | } |
| 699 | for (i = 0; i < n; ++i) { |
| 700 | name = sk_OPENSSL_STRING_value(param->hosts, i); |
| 701 | if (X509_check_host(x, name, strlen(name), param->hostflags, |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 702 | ¶m->peername) > 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 703 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 704 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 705 | } |
| 706 | return n == 0; |
| 707 | } |
| 708 | |
| 709 | static int check_id(X509_STORE_CTX *ctx) { |
| 710 | X509_VERIFY_PARAM *vpm = ctx->param; |
| 711 | X509 *x = ctx->cert; |
| 712 | if (vpm->poison) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 713 | if (!check_id_error(ctx, X509_V_ERR_INVALID_CALL)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 714 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 715 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 716 | } |
| 717 | if (vpm->hosts && check_hosts(x, vpm) <= 0) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 718 | if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 719 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 720 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 721 | } |
| 722 | if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 723 | if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 724 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 725 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 726 | } |
| 727 | if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 728 | if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 729 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 730 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 731 | } |
| 732 | return 1; |
| 733 | } |
| 734 | |
| 735 | static int check_trust(X509_STORE_CTX *ctx) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 736 | int ok; |
| 737 | X509 *x = NULL; |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 738 | // Check all trusted certificates in chain |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 739 | for (size_t i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 740 | x = sk_X509_value(ctx->chain, i); |
| 741 | ok = X509_check_trust(x, ctx->param->trust, 0); |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 742 | // If explicitly trusted return trusted |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 743 | if (ok == X509_TRUST_TRUSTED) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 744 | return X509_TRUST_TRUSTED; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 745 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 746 | // If explicitly rejected notify callback and reject if not |
| 747 | // overridden. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 748 | if (ok == X509_TRUST_REJECTED) { |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 749 | ctx->error_depth = (int)i; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 750 | ctx->current_cert = x; |
| 751 | ctx->error = X509_V_ERR_CERT_REJECTED; |
| 752 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 753 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 754 | return X509_TRUST_REJECTED; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 755 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 756 | } |
| 757 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 758 | // If we accept partial chains and have at least one trusted certificate |
| 759 | // return success. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 760 | if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { |
| 761 | X509 *mx; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 762 | if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 763 | return X509_TRUST_TRUSTED; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 764 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 765 | x = sk_X509_value(ctx->chain, 0); |
| 766 | mx = lookup_cert_match(ctx, x); |
| 767 | if (mx) { |
| 768 | (void)sk_X509_set(ctx->chain, 0, mx); |
| 769 | X509_free(x); |
| 770 | ctx->last_untrusted = 0; |
| 771 | return X509_TRUST_TRUSTED; |
| 772 | } |
| 773 | } |
| 774 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 775 | // If no trusted certs in chain at all return untrusted and allow |
| 776 | // standard (no issuer cert) etc errors to be indicated. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 777 | return X509_TRUST_UNTRUSTED; |
| 778 | } |
| 779 | |
| 780 | static int check_revocation(X509_STORE_CTX *ctx) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 781 | if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 782 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 783 | } |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 784 | int last; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 785 | if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) { |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 786 | last = (int)sk_X509_num(ctx->chain) - 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 787 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 788 | last = 0; |
| 789 | } |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 790 | for (int i = 0; i <= last; i++) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 791 | ctx->error_depth = i; |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 792 | int ok = check_cert(ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 793 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 794 | return ok; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 795 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 796 | } |
| 797 | return 1; |
| 798 | } |
| 799 | |
| 800 | static int check_cert(X509_STORE_CTX *ctx) { |
David Benjamin | d9a58a1 | 2023-11-12 14:04:00 -0500 | [diff] [blame] | 801 | X509_CRL *crl = NULL; |
David Benjamin | 8cae3d0 | 2023-11-12 14:42:35 -0500 | [diff] [blame] | 802 | int ok = 0, cnum = ctx->error_depth; |
| 803 | X509 *x = sk_X509_value(ctx->chain, cnum); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 804 | ctx->current_cert = x; |
| 805 | ctx->current_issuer = NULL; |
| 806 | ctx->current_crl_score = 0; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 807 | |
David Benjamin | 8cae3d0 | 2023-11-12 14:42:35 -0500 | [diff] [blame] | 808 | // Try to retrieve relevant CRL |
| 809 | if (ctx->get_crl) { |
| 810 | ok = ctx->get_crl(ctx, &crl, x); |
| 811 | } else { |
| 812 | ok = get_crl(ctx, &crl, x); |
| 813 | } |
| 814 | // If error looking up CRL, nothing we can do except notify callback |
| 815 | if (!ok) { |
| 816 | ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; |
| 817 | ok = ctx->verify_cb(0, ctx); |
| 818 | goto err; |
| 819 | } |
| 820 | ctx->current_crl = crl; |
| 821 | ok = ctx->check_crl(ctx, crl); |
| 822 | if (!ok) { |
| 823 | goto err; |
| 824 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 825 | |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 826 | ok = cert_crl(ctx, crl, x); |
David Benjamin | 8cae3d0 | 2023-11-12 14:42:35 -0500 | [diff] [blame] | 827 | if (!ok) { |
| 828 | goto err; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 829 | } |
David Benjamin | d9a58a1 | 2023-11-12 14:04:00 -0500 | [diff] [blame] | 830 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 831 | err: |
| 832 | X509_CRL_free(crl); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 833 | ctx->current_crl = NULL; |
| 834 | return ok; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 835 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 836 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 837 | // Check CRL times against values in X509_STORE_CTX |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 838 | static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) { |
David Benjamin | eccd103 | 2022-08-12 15:12:29 -0400 | [diff] [blame] | 839 | if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) { |
| 840 | return 1; |
| 841 | } |
| 842 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 843 | if (notify) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 844 | ctx->current_crl = crl; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 845 | } |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 846 | int64_t ptime; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 847 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 848 | ptime = ctx->param->check_time; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 849 | } else { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 850 | ptime = time(NULL); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 851 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 852 | |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 853 | int i = X509_cmp_time_posix(X509_CRL_get0_lastUpdate(crl), ptime); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 854 | if (i == 0) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 855 | if (!notify) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 856 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 857 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 858 | ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 859 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 860 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 861 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | if (i > 0) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 865 | if (!notify) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 866 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 867 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 868 | ctx->error = X509_V_ERR_CRL_NOT_YET_VALID; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 869 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 870 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 871 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | if (X509_CRL_get0_nextUpdate(crl)) { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 875 | i = X509_cmp_time_posix(X509_CRL_get0_nextUpdate(crl), ptime); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 876 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 877 | if (i == 0) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 878 | if (!notify) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 879 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 880 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 881 | ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 882 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 883 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 884 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 885 | } |
David Benjamin | 9bed373 | 2023-11-12 14:51:23 -0500 | [diff] [blame] | 886 | if (i < 0) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 887 | if (!notify) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 888 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 889 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 890 | ctx->error = X509_V_ERR_CRL_HAS_EXPIRED; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 891 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 892 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 893 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 894 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 895 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 896 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 897 | if (notify) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 898 | ctx->current_crl = NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 899 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 900 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 901 | return 1; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 902 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 903 | |
David Benjamin | d9a58a1 | 2023-11-12 14:04:00 -0500 | [diff] [blame] | 904 | static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 **pissuer, |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 905 | int *pscore, STACK_OF(X509_CRL) *crls) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 906 | int crl_score, best_score = *pscore; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 907 | X509 *x = ctx->current_cert; |
David Benjamin | d9a58a1 | 2023-11-12 14:04:00 -0500 | [diff] [blame] | 908 | X509_CRL *best_crl = NULL; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 909 | X509 *crl_issuer = NULL, *best_crl_issuer = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 910 | |
David Benjamin | d9a58a1 | 2023-11-12 14:04:00 -0500 | [diff] [blame] | 911 | for (size_t i = 0; i < sk_X509_CRL_num(crls); i++) { |
| 912 | X509_CRL *crl = sk_X509_CRL_value(crls, i); |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 913 | crl_score = get_crl_score(ctx, &crl_issuer, crl, x); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 914 | if (crl_score < best_score || crl_score == 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 915 | continue; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 916 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 917 | // If current CRL is equivalent use it if it is newer |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 918 | if (crl_score == best_score && best_crl != NULL) { |
| 919 | int day, sec; |
| 920 | if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl), |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 921 | X509_CRL_get0_lastUpdate(crl)) == 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 922 | continue; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 923 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 924 | // ASN1_TIME_diff never returns inconsistent signs for |day| |
| 925 | // and |sec|. |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 926 | if (day <= 0 && sec <= 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 927 | continue; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 928 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 929 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 930 | best_crl = crl; |
| 931 | best_crl_issuer = crl_issuer; |
| 932 | best_score = crl_score; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 933 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 934 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 935 | if (best_crl) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 936 | if (*pcrl) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 937 | X509_CRL_free(*pcrl); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 938 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 939 | *pcrl = best_crl; |
| 940 | *pissuer = best_crl_issuer; |
| 941 | *pscore = best_score; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 942 | X509_CRL_up_ref(best_crl); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 943 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 944 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 945 | if (best_score >= CRL_SCORE_VALID) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 946 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 947 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 948 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 949 | return 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 950 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 951 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 952 | // For a given CRL return how suitable it is for the supplied certificate |
| 953 | // 'x'. The return value is a mask of several criteria. If the issuer is not |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 954 | // the certificate issuer this is returned in *pissuer. |
| 955 | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl, |
| 956 | X509 *x) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 957 | int crl_score = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 958 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 959 | // First see if we can reject CRL straight away |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 960 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 961 | // Invalid IDP cannot be processed |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 962 | if (crl->idp_flags & IDP_INVALID) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 963 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 964 | } |
David Benjamin | f861499 | 2023-11-12 13:43:33 -0500 | [diff] [blame] | 965 | // Reason codes and indirect CRLs are not supported. |
| 966 | if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS)) { |
| 967 | return 0; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 968 | } |
David Benjamin | 54eaf6b | 2023-11-12 15:00:31 -0500 | [diff] [blame] | 969 | // We do not support indirect CRLs, so the issuer names must match. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 970 | if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) { |
David Benjamin | 54eaf6b | 2023-11-12 15:00:31 -0500 | [diff] [blame] | 971 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 972 | } |
David Benjamin | 54eaf6b | 2023-11-12 15:00:31 -0500 | [diff] [blame] | 973 | crl_score |= CRL_SCORE_ISSUER_NAME; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 974 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 975 | if (!(crl->flags & EXFLAG_CRITICAL)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 976 | crl_score |= CRL_SCORE_NOCRITICAL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 977 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 978 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 979 | // Check expiry |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 980 | if (check_crl_time(ctx, crl, 0)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 981 | crl_score |= CRL_SCORE_TIME; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 982 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 983 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 984 | // Check authority key ID and locate certificate issuer |
David Benjamin | a4851dd | 2023-11-12 15:51:49 -0500 | [diff] [blame] | 985 | if (!crl_akid_check(ctx, crl, pissuer, &crl_score)) { |
| 986 | // If we can't locate certificate issuer at this point forget it |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 987 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 988 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 989 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 990 | // Check cert for matching CRL distribution points |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 991 | if (crl_crldp_check(x, crl, crl_score)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 992 | crl_score |= CRL_SCORE_SCOPE; |
| 993 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 994 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 995 | return crl_score; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 996 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 997 | |
David Benjamin | a4851dd | 2023-11-12 15:51:49 -0500 | [diff] [blame] | 998 | static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer, |
| 999 | int *pcrl_score) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1000 | X509 *crl_issuer = NULL; |
| 1001 | X509_NAME *cnm = X509_CRL_get_issuer(crl); |
| 1002 | int cidx = ctx->error_depth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1003 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1004 | if ((size_t)cidx != sk_X509_num(ctx->chain) - 1) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1005 | cidx++; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1006 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1007 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1008 | crl_issuer = sk_X509_value(ctx->chain, cidx); |
| 1009 | |
| 1010 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { |
David Benjamin | 54eaf6b | 2023-11-12 15:00:31 -0500 | [diff] [blame] | 1011 | *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT; |
| 1012 | *pissuer = crl_issuer; |
David Benjamin | a4851dd | 2023-11-12 15:51:49 -0500 | [diff] [blame] | 1013 | return 1; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1017 | crl_issuer = sk_X509_value(ctx->chain, cidx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1018 | if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1019 | continue; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1020 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1021 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1022 | *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH; |
| 1023 | *pissuer = crl_issuer; |
David Benjamin | a4851dd | 2023-11-12 15:51:49 -0500 | [diff] [blame] | 1024 | return 1; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1025 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1026 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1027 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1028 | return 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1029 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1030 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1031 | // Check for match between two dist point names: three separate cases. 1. |
| 1032 | // Both are relative names and compare X509_NAME types. 2. One full, one |
| 1033 | // relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and |
| 1034 | // compare two GENERAL_NAMES. 4. One is NULL: automatic match. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1035 | static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b) { |
| 1036 | X509_NAME *nm = NULL; |
| 1037 | GENERAL_NAMES *gens = NULL; |
| 1038 | GENERAL_NAME *gena, *genb; |
| 1039 | size_t i, j; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1040 | if (!a || !b) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1041 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1042 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1043 | if (a->type == 1) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1044 | if (!a->dpname) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1045 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1046 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1047 | // Case 1: two X509_NAME |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1048 | if (b->type == 1) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1049 | if (!b->dpname) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1050 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1051 | } |
| 1052 | if (!X509_NAME_cmp(a->dpname, b->dpname)) { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1053 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1054 | } else { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1055 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1056 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1057 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1058 | // Case 2: set name and GENERAL_NAMES appropriately |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1059 | nm = a->dpname; |
| 1060 | gens = b->name.fullname; |
| 1061 | } else if (b->type == 1) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1062 | if (!b->dpname) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1063 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1064 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1065 | // Case 2: set name and GENERAL_NAMES appropriately |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1066 | gens = a->name.fullname; |
| 1067 | nm = b->dpname; |
| 1068 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1069 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1070 | // Handle case 2 with one GENERAL_NAMES and one X509_NAME |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1071 | if (nm) { |
| 1072 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
| 1073 | gena = sk_GENERAL_NAME_value(gens, i); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1074 | if (gena->type != GEN_DIRNAME) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1075 | continue; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1076 | } |
| 1077 | if (!X509_NAME_cmp(nm, gena->d.directoryName)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1078 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1079 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1080 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1081 | return 0; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1082 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1083 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1084 | // Else case 3: two GENERAL_NAMES |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1085 | |
| 1086 | for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) { |
| 1087 | gena = sk_GENERAL_NAME_value(a->name.fullname, i); |
| 1088 | for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) { |
| 1089 | genb = sk_GENERAL_NAME_value(b->name.fullname, j); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1090 | if (!GENERAL_NAME_cmp(gena, genb)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1091 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1092 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | return 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1097 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1098 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1099 | // Check CRLDP and IDP |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 1100 | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1101 | if (crl->idp_flags & IDP_ONLYATTR) { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1102 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1103 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1104 | if (x->ex_flags & EXFLAG_CA) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1105 | if (crl->idp_flags & IDP_ONLYUSER) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1106 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1107 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1108 | } else { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1109 | if (crl->idp_flags & IDP_ONLYCA) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1110 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1111 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1112 | } |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 1113 | for (size_t i = 0; i < sk_DIST_POINT_num(x->crldp); i++) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1114 | DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i); |
David Benjamin | 54eaf6b | 2023-11-12 15:00:31 -0500 | [diff] [blame] | 1115 | // Skip distribution points with a reasons field or a CRL issuer: |
| 1116 | // |
| 1117 | // We do not support CRLs partitioned by reason code. RFC 5280 requires CAs |
| 1118 | // include at least one DistributionPoint that covers all reasons. |
| 1119 | // |
| 1120 | // We also do not support indirect CRLs, and a CRL issuer can only match |
| 1121 | // indirect CRLs (RFC 5280, section 6.3.3, step b.1). |
| 1122 | // support. |
| 1123 | if (dp->reasons != NULL && dp->CRLissuer != NULL && |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 1124 | (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint))) { |
| 1125 | return 1; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1126 | } |
| 1127 | } |
David Benjamin | 54eaf6b | 2023-11-12 15:00:31 -0500 | [diff] [blame] | 1128 | |
| 1129 | // If the CRL does not specify an issuing distribution point, allow it to |
| 1130 | // match anything. |
| 1131 | // |
| 1132 | // TODO(davidben): Does this match RFC 5280? It's hard to follow because RFC |
| 1133 | // 5280 starts from distribution points, while this starts from CRLs. |
| 1134 | return !crl->idp || !crl->idp->distpoint; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1135 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1136 | |
David Benjamin | 0c44813 | 2023-11-12 19:33:53 -0500 | [diff] [blame] | 1137 | // Retrieve CRL corresponding to current certificate. |
David Benjamin | d9a58a1 | 2023-11-12 14:04:00 -0500 | [diff] [blame] | 1138 | static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1139 | int ok; |
| 1140 | X509 *issuer = NULL; |
| 1141 | int crl_score = 0; |
David Benjamin | d9a58a1 | 2023-11-12 14:04:00 -0500 | [diff] [blame] | 1142 | X509_CRL *crl = NULL; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1143 | STACK_OF(X509_CRL) *skcrl; |
| 1144 | X509_NAME *nm = X509_get_issuer_name(x); |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 1145 | ok = get_crl_sk(ctx, &crl, &issuer, &crl_score, ctx->crls); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1146 | if (ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1147 | goto done; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1148 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1149 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1150 | // Lookup CRLs from store |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 1151 | skcrl = X509_STORE_get1_crls(ctx, nm); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1152 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1153 | // If no CRLs found and a near match from get_crl_sk use that |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1154 | if (!skcrl && crl) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1155 | goto done; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1156 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1157 | |
David Benjamin | 580c041 | 2023-11-12 14:28:51 -0500 | [diff] [blame] | 1158 | get_crl_sk(ctx, &crl, &issuer, &crl_score, skcrl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1159 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1160 | sk_X509_CRL_pop_free(skcrl, X509_CRL_free); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1161 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1162 | done: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1163 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1164 | // If we got any kind of CRL use it and return success |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1165 | if (crl) { |
| 1166 | ctx->current_issuer = issuer; |
| 1167 | ctx->current_crl_score = crl_score; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1168 | *pcrl = crl; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1169 | return 1; |
| 1170 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1171 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1172 | return 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1174 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1175 | // Check CRL validity |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1176 | static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) { |
| 1177 | X509 *issuer = NULL; |
| 1178 | EVP_PKEY *ikey = NULL; |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 1179 | int ok = 0; |
| 1180 | int cnum = ctx->error_depth; |
| 1181 | int chnum = (int)sk_X509_num(ctx->chain) - 1; |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1182 | // if we have an alternative CRL issuer cert use that |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1183 | if (ctx->current_issuer) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1184 | issuer = ctx->current_issuer; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1185 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1186 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1187 | // Else find CRL issuer: if not last certificate then issuer is next |
| 1188 | // certificate in chain. |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1189 | else if (cnum < chnum) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1190 | issuer = sk_X509_value(ctx->chain, cnum + 1); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1191 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1192 | issuer = sk_X509_value(ctx->chain, chnum); |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1193 | // If not self signed, can't check signature |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 1194 | if (!x509_check_issued_with_callback(ctx, issuer, issuer)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1195 | ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER; |
| 1196 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1197 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1198 | goto err; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1199 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | if (issuer) { |
David Benjamin | 0c44813 | 2023-11-12 19:33:53 -0500 | [diff] [blame] | 1204 | // Check for cRLSign bit if keyUsage present |
| 1205 | if ((issuer->ex_flags & EXFLAG_KUSAGE) && |
| 1206 | !(issuer->ex_kusage & KU_CRL_SIGN)) { |
| 1207 | ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN; |
| 1208 | ok = ctx->verify_cb(0, ctx); |
| 1209 | if (!ok) { |
| 1210 | goto err; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) { |
| 1215 | ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE; |
| 1216 | ok = ctx->verify_cb(0, ctx); |
| 1217 | if (!ok) { |
| 1218 | goto err; |
| 1219 | } |
| 1220 | } |
| 1221 | |
David Benjamin | 0c44813 | 2023-11-12 19:33:53 -0500 | [diff] [blame] | 1222 | if (crl->idp_flags & IDP_INVALID) { |
| 1223 | ctx->error = X509_V_ERR_INVALID_EXTENSION; |
| 1224 | ok = ctx->verify_cb(0, ctx); |
| 1225 | if (!ok) { |
| 1226 | goto err; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1227 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1228 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1229 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1230 | if (!(ctx->current_crl_score & CRL_SCORE_TIME)) { |
| 1231 | ok = check_crl_time(ctx, crl, 1); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1232 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1233 | goto err; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1234 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1235 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1236 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1237 | // Attempt to get issuer certificate public key |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1238 | ikey = X509_get_pubkey(issuer); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1239 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1240 | if (!ikey) { |
| 1241 | ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; |
| 1242 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1243 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1244 | goto err; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1245 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1246 | } else { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1247 | // Verify CRL signature |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1248 | if (X509_CRL_verify(crl, ikey) <= 0) { |
| 1249 | ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE; |
| 1250 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1251 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1252 | goto err; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1253 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | ok = 1; |
| 1259 | |
| 1260 | err: |
| 1261 | EVP_PKEY_free(ikey); |
| 1262 | return ok; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1263 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1264 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1265 | // Check certificate against CRL |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1266 | static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) { |
| 1267 | int ok; |
| 1268 | X509_REVOKED *rev; |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1269 | // The rules changed for this... previously if a CRL contained unhandled |
| 1270 | // critical extensions it could still be used to indicate a certificate |
| 1271 | // was revoked. This has since been changed since critical extension can |
| 1272 | // change the meaning of CRL entries. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1273 | if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && |
| 1274 | (crl->flags & EXFLAG_CRITICAL)) { |
| 1275 | ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION; |
| 1276 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1277 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1278 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1279 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1280 | } |
David Benjamin | 9177d65 | 2023-11-12 14:05:24 -0500 | [diff] [blame] | 1281 | // Look for serial number of certificate in CRL. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1282 | if (X509_CRL_get0_by_cert(crl, &rev, x)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1283 | ctx->error = X509_V_ERR_CERT_REVOKED; |
| 1284 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1285 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1286 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1287 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1288 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1289 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1290 | return 1; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1291 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1292 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1293 | static int check_policy(X509_STORE_CTX *ctx) { |
David Benjamin | 029d0e7 | 2022-12-26 19:38:40 -0500 | [diff] [blame] | 1294 | X509 *current_cert = NULL; |
| 1295 | int ret = X509_policy_check(ctx->chain, ctx->param->policies, |
| 1296 | ctx->param->flags, ¤t_cert); |
| 1297 | if (ret != X509_V_OK) { |
| 1298 | ctx->current_cert = current_cert; |
| 1299 | ctx->error = ret; |
| 1300 | if (ret == X509_V_ERR_OUT_OF_MEM) { |
| 1301 | return 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1302 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1303 | return ctx->verify_cb(0, ctx); |
| 1304 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1305 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1306 | if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) { |
| 1307 | ctx->current_cert = NULL; |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1308 | // Verification errors need to be "sticky", a callback may have allowed |
| 1309 | // an SSL handshake to continue despite an error, and we must then |
| 1310 | // remain in an error state. Therefore, we MUST NOT clear earlier |
| 1311 | // verification errors by setting the error to X509_V_OK. |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1312 | if (!ctx->verify_cb(2, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1313 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1314 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | return 1; |
| 1318 | } |
| 1319 | |
| 1320 | static int check_cert_time(X509_STORE_CTX *ctx, X509 *x) { |
David Benjamin | eccd103 | 2022-08-12 15:12:29 -0400 | [diff] [blame] | 1321 | if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) { |
| 1322 | return 1; |
| 1323 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1324 | |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1325 | int64_t ptime; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1326 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1327 | ptime = ctx->param->check_time; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1328 | } else { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1329 | ptime = time(NULL); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1330 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1331 | |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1332 | int i = X509_cmp_time_posix(X509_get_notBefore(x), ptime); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1333 | if (i == 0) { |
| 1334 | ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD; |
| 1335 | ctx->current_cert = x; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1336 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1337 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1338 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | if (i > 0) { |
| 1342 | ctx->error = X509_V_ERR_CERT_NOT_YET_VALID; |
| 1343 | ctx->current_cert = x; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1344 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1345 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1346 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1347 | } |
| 1348 | |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1349 | i = X509_cmp_time_posix(X509_get_notAfter(x), ptime); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1350 | if (i == 0) { |
| 1351 | ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD; |
| 1352 | ctx->current_cert = x; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1353 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1354 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1355 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | if (i < 0) { |
| 1359 | ctx->error = X509_V_ERR_CERT_HAS_EXPIRED; |
| 1360 | ctx->current_cert = x; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1361 | if (!ctx->verify_cb(0, ctx)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1362 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1363 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | return 1; |
| 1367 | } |
| 1368 | |
| 1369 | static int internal_verify(X509_STORE_CTX *ctx) { |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 1370 | int ok = 0; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1371 | X509 *xs, *xi; |
| 1372 | EVP_PKEY *pkey = NULL; |
| 1373 | |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 1374 | int n = (int)sk_X509_num(ctx->chain); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1375 | ctx->error_depth = n - 1; |
| 1376 | n--; |
| 1377 | xi = sk_X509_value(ctx->chain, n); |
| 1378 | |
David Benjamin | 998f511 | 2023-11-19 10:04:06 -0500 | [diff] [blame] | 1379 | if (x509_check_issued_with_callback(ctx, xi, xi)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1380 | xs = xi; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1381 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1382 | if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { |
| 1383 | xs = xi; |
| 1384 | goto check_cert; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1385 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1386 | if (n <= 0) { |
| 1387 | ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE; |
| 1388 | ctx->current_cert = xi; |
| 1389 | ok = ctx->verify_cb(0, ctx); |
| 1390 | goto end; |
David Benjamin | 046fc13 | 2021-08-01 15:20:53 -0400 | [diff] [blame] | 1391 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1392 | n--; |
| 1393 | ctx->error_depth = n; |
| 1394 | xs = sk_X509_value(ctx->chain, n); |
| 1395 | } |
| 1396 | } |
| 1397 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1398 | // ctx->error=0; not needed |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1399 | while (n >= 0) { |
| 1400 | ctx->error_depth = n; |
| 1401 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1402 | // Skip signature check for self signed certificates unless |
| 1403 | // explicitly asked for. It doesn't add any security and just wastes |
| 1404 | // time. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1405 | if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) { |
| 1406 | if ((pkey = X509_get_pubkey(xi)) == NULL) { |
| 1407 | ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; |
| 1408 | ctx->current_cert = xi; |
| 1409 | ok = ctx->verify_cb(0, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1410 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1411 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1412 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1413 | } else if (X509_verify(xs, pkey) <= 0) { |
| 1414 | ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE; |
| 1415 | ctx->current_cert = xs; |
| 1416 | ok = ctx->verify_cb(0, ctx); |
| 1417 | if (!ok) { |
| 1418 | EVP_PKEY_free(pkey); |
| 1419 | goto end; |
| 1420 | } |
| 1421 | } |
| 1422 | EVP_PKEY_free(pkey); |
| 1423 | pkey = NULL; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1424 | } |
David Benjamin | 046fc13 | 2021-08-01 15:20:53 -0400 | [diff] [blame] | 1425 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1426 | check_cert: |
| 1427 | ok = check_cert_time(ctx, xs); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1428 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1429 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1430 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1431 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1432 | // The last error (if any) is still in the error value |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1433 | ctx->current_issuer = xi; |
| 1434 | ctx->current_cert = xs; |
| 1435 | ok = ctx->verify_cb(1, ctx); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1436 | if (!ok) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1437 | goto end; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1438 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1439 | |
| 1440 | n--; |
| 1441 | if (n >= 0) { |
| 1442 | xi = xs; |
| 1443 | xs = sk_X509_value(ctx->chain, n); |
| 1444 | } |
| 1445 | } |
| 1446 | ok = 1; |
| 1447 | end: |
| 1448 | return ok; |
| 1449 | } |
| 1450 | |
| 1451 | int X509_cmp_current_time(const ASN1_TIME *ctm) { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1452 | return X509_cmp_time_posix(ctm, time(NULL)); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1453 | } |
| 1454 | |
David Benjamin | 899c1a7 | 2023-07-05 21:56:55 -0400 | [diff] [blame] | 1455 | int X509_cmp_time(const ASN1_TIME *ctm, const time_t *cmp_time) { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1456 | int64_t compare_time = (cmp_time == NULL) ? time(NULL) : *cmp_time; |
| 1457 | return X509_cmp_time_posix(ctm, compare_time); |
| 1458 | } |
| 1459 | |
| 1460 | int X509_cmp_time_posix(const ASN1_TIME *ctm, int64_t cmp_time) { |
Bob Beck | ccd665d | 2022-07-29 15:57:00 -0600 | [diff] [blame] | 1461 | int64_t ctm_time; |
| 1462 | if (!ASN1_TIME_to_posix(ctm, &ctm_time)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1463 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1464 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1465 | // The return value 0 is reserved for errors. |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1466 | return (ctm_time - cmp_time <= 0) ? -1 : 1; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec) { |
| 1470 | return X509_time_adj(s, offset_sec, NULL); |
| 1471 | } |
| 1472 | |
David Benjamin | 899c1a7 | 2023-07-05 21:56:55 -0400 | [diff] [blame] | 1473 | ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, const time_t *in_tm) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1474 | return X509_time_adj_ex(s, 0, offset_sec, in_tm); |
| 1475 | } |
| 1476 | |
| 1477 | ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec, |
David Benjamin | 899c1a7 | 2023-07-05 21:56:55 -0400 | [diff] [blame] | 1478 | const time_t *in_tm) { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1479 | int64_t t = 0; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1480 | |
| 1481 | if (in_tm) { |
| 1482 | t = *in_tm; |
| 1483 | } else { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1484 | t = time(NULL); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | return ASN1_TIME_adj(s, t, offset_day, offset_sec); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1488 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1489 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1490 | int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1491 | CRYPTO_EX_unused *unused, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 1492 | CRYPTO_EX_dup *dup_unused, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1493 | CRYPTO_EX_free *free_func) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1494 | // This function is (usually) called only once, by |
| 1495 | // SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1496 | int index; |
| 1497 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, |
| 1498 | free_func)) { |
| 1499 | return -1; |
| 1500 | } |
| 1501 | return index; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1502 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1503 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1504 | int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data) { |
| 1505 | return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1506 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1507 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1508 | void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx) { |
| 1509 | return CRYPTO_get_ex_data(&ctx->ex_data, idx); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1510 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1511 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1512 | int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx) { return ctx->error; } |
| 1513 | |
| 1514 | void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err) { |
| 1515 | ctx->error = err; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1516 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1517 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1518 | int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx) { |
| 1519 | return ctx->error_depth; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1520 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1521 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1522 | X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx) { |
| 1523 | return ctx->current_cert; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1524 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1525 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1526 | STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx) { |
| 1527 | return ctx->chain; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1528 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1529 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1530 | STACK_OF(X509) *X509_STORE_CTX_get0_chain(X509_STORE_CTX *ctx) { |
| 1531 | return ctx->chain; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1532 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1533 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1534 | STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1535 | if (!ctx->chain) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1536 | return NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1537 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1538 | return X509_chain_up_ref(ctx->chain); |
David Benjamin | 5cd0724 | 2020-03-23 21:28:27 -0400 | [diff] [blame] | 1539 | } |
| 1540 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1541 | X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx) { |
| 1542 | return ctx->current_issuer; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1543 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1544 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1545 | X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx) { |
| 1546 | return ctx->current_crl; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1547 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1548 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1549 | X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx) { |
David Benjamin | a4851dd | 2023-11-12 15:51:49 -0500 | [diff] [blame] | 1550 | // In OpenSSL, an |X509_STORE_CTX| sometimes has a parent context during CRL |
| 1551 | // path validation for indirect CRLs. We require the CRL to be issued |
| 1552 | // somewhere along the certificate path, so this is always NULL. |
| 1553 | return NULL; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1554 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1555 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1556 | void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x) { ctx->cert = x; } |
| 1557 | |
| 1558 | void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) { |
| 1559 | ctx->untrusted = sk; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1560 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1561 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1562 | STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx) { |
| 1563 | return ctx->untrusted; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1564 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1565 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1566 | void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk) { |
| 1567 | ctx->crls = sk; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1568 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1569 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1570 | int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) { |
| 1571 | return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0); |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1572 | } |
| 1573 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1574 | int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust) { |
| 1575 | return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1576 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1577 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1578 | // This function is used to set the X509_STORE_CTX purpose and trust values. |
| 1579 | // This is intended to be used when another structure has its own trust and |
| 1580 | // purpose values which (if set) will be inherited by the ctx. If they aren't |
| 1581 | // set then we will usually have a default purpose in mind which should then |
| 1582 | // be used to set the trust value. An example of this is SSL use: an SSL |
| 1583 | // structure will have its own purpose and trust settings which the |
| 1584 | // application can set: if they aren't set then we use the default of SSL |
| 1585 | // client/server. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1586 | |
| 1587 | int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1588 | int purpose, int trust) { |
| 1589 | int idx; |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1590 | // If purpose not set use default |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1591 | if (!purpose) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1592 | purpose = def_purpose; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1593 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1594 | // If we have a purpose then check it is valid |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1595 | if (purpose) { |
| 1596 | X509_PURPOSE *ptmp; |
| 1597 | idx = X509_PURPOSE_get_by_id(purpose); |
| 1598 | if (idx == -1) { |
| 1599 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID); |
| 1600 | return 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1601 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1602 | ptmp = X509_PURPOSE_get0(idx); |
| 1603 | if (ptmp->trust == X509_TRUST_DEFAULT) { |
| 1604 | idx = X509_PURPOSE_get_by_id(def_purpose); |
| 1605 | if (idx == -1) { |
| 1606 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID); |
| 1607 | return 0; |
| 1608 | } |
| 1609 | ptmp = X509_PURPOSE_get0(idx); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1610 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1611 | // If trust not set then get from purpose default |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1612 | if (!trust) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1613 | trust = ptmp->trust; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1614 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1615 | } |
| 1616 | if (trust) { |
| 1617 | idx = X509_TRUST_get_by_id(trust); |
| 1618 | if (idx == -1) { |
| 1619 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID); |
| 1620 | return 0; |
| 1621 | } |
| 1622 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1623 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1624 | if (purpose && !ctx->param->purpose) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1625 | ctx->param->purpose = purpose; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1626 | } |
| 1627 | if (trust && !ctx->param->trust) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1628 | ctx->param->trust = trust; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1629 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1630 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1633 | X509_STORE_CTX *X509_STORE_CTX_new(void) { |
David Benjamin | 698aa89 | 2023-11-18 17:57:01 -0500 | [diff] [blame] | 1634 | return OPENSSL_zalloc(sizeof(X509_STORE_CTX)); |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 1635 | } |
| 1636 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1637 | void X509_STORE_CTX_free(X509_STORE_CTX *ctx) { |
| 1638 | if (ctx == NULL) { |
| 1639 | return; |
| 1640 | } |
| 1641 | X509_STORE_CTX_cleanup(ctx); |
| 1642 | OPENSSL_free(ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1646 | STACK_OF(X509) *chain) { |
David Benjamin | 1685bd1 | 2023-11-21 12:01:38 -0500 | [diff] [blame] | 1647 | X509_STORE_CTX_cleanup(ctx); |
| 1648 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1649 | ctx->ctx = store; |
| 1650 | ctx->cert = x509; |
| 1651 | ctx->untrusted = chain; |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 1652 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1653 | CRYPTO_new_ex_data(&ctx->ex_data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1654 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1655 | if (store == NULL) { |
| 1656 | OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); |
| 1657 | goto err; |
| 1658 | } |
David Benjamin | ca3f243 | 2020-08-31 14:47:49 -0400 | [diff] [blame] | 1659 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1660 | ctx->param = X509_VERIFY_PARAM_new(); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1661 | if (!ctx->param) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1662 | goto err; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1663 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1664 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1665 | // Inherit callbacks and flags from X509_STORE. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1666 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1667 | ctx->verify_cb = store->verify_cb; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1668 | |
| 1669 | if (!X509_VERIFY_PARAM_inherit(ctx->param, store->param) || |
| 1670 | !X509_VERIFY_PARAM_inherit(ctx->param, |
| 1671 | X509_VERIFY_PARAM_lookup("default"))) { |
| 1672 | goto err; |
| 1673 | } |
| 1674 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1675 | if (store->get_issuer) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1676 | ctx->get_issuer = store->get_issuer; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1677 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1678 | ctx->get_issuer = X509_STORE_CTX_get1_issuer; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1679 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1680 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1681 | if (store->verify_cb) { |
David Benjamin | ca3f243 | 2020-08-31 14:47:49 -0400 | [diff] [blame] | 1682 | ctx->verify_cb = store->verify_cb; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1683 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1684 | ctx->verify_cb = null_callback; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1685 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1686 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1687 | if (store->get_crl) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1688 | ctx->get_crl = store->get_crl; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1689 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1690 | ctx->get_crl = NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1691 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1692 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1693 | if (store->check_crl) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1694 | ctx->check_crl = store->check_crl; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1695 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1696 | ctx->check_crl = check_crl; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1697 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1698 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1699 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1700 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1701 | err: |
| 1702 | CRYPTO_free_ex_data(&g_ex_data_class, ctx, &ctx->ex_data); |
| 1703 | if (ctx->param != NULL) { |
| 1704 | X509_VERIFY_PARAM_free(ctx->param); |
| 1705 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1706 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1707 | OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX)); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1708 | return 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1709 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1710 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 1711 | // Set alternative lookup method: just a STACK of trusted certificates. This |
| 1712 | // avoids X509_STORE nastiness where it isn't needed. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1713 | |
David Benjamin | 2135ac6 | 2022-08-12 15:33:43 -0400 | [diff] [blame] | 1714 | void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, |
| 1715 | STACK_OF(X509) *sk) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1716 | ctx->other_ctx = sk; |
| 1717 | ctx->get_issuer = get_issuer_sk; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1718 | } |
| 1719 | |
David Benjamin | 2135ac6 | 2022-08-12 15:33:43 -0400 | [diff] [blame] | 1720 | void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) { |
| 1721 | X509_STORE_CTX_set0_trusted_stack(ctx, sk); |
| 1722 | } |
| 1723 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1724 | void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) { |
David Benjamin | a4851dd | 2023-11-12 15:51:49 -0500 | [diff] [blame] | 1725 | X509_VERIFY_PARAM_free(ctx->param); |
| 1726 | ctx->param = NULL; |
| 1727 | sk_X509_pop_free(ctx->chain, X509_free); |
| 1728 | ctx->chain = NULL; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1729 | CRYPTO_free_ex_data(&g_ex_data_class, ctx, &(ctx->ex_data)); |
David Benjamin | 1685bd1 | 2023-11-21 12:01:38 -0500 | [diff] [blame] | 1730 | OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1731 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1732 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1733 | void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth) { |
| 1734 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1735 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1736 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1737 | void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags) { |
| 1738 | X509_VERIFY_PARAM_set_flags(ctx->param, flags); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1739 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1740 | |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1741 | void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx, unsigned long flags, |
David Benjamin | 2ba7634 | 2023-11-19 10:20:04 -0500 | [diff] [blame] | 1742 | int64_t t) { |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1743 | X509_VERIFY_PARAM_set_time_posix(ctx->param, t); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1744 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1745 | |
Bob Beck | 6e20b77 | 2023-02-08 11:32:19 -0700 | [diff] [blame] | 1746 | void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, |
| 1747 | time_t t) { |
| 1748 | X509_STORE_CTX_set_time_posix(ctx, flags, t); |
| 1749 | } |
| 1750 | |
| 1751 | X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx) { |
| 1752 | return ctx->cert; |
| 1753 | } |
David Benjamin | f05e3ea | 2018-05-10 17:35:11 -0400 | [diff] [blame] | 1754 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1755 | void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1756 | int (*verify_cb)(int, X509_STORE_CTX *)) { |
| 1757 | ctx->verify_cb = verify_cb; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1758 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1759 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1760 | int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name) { |
| 1761 | const X509_VERIFY_PARAM *param; |
| 1762 | param = X509_VERIFY_PARAM_lookup(name); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1763 | if (!param) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1764 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1765 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1766 | return X509_VERIFY_PARAM_inherit(ctx->param, param); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1767 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1768 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1769 | X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx) { |
| 1770 | return ctx->param; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1771 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1772 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1773 | void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1774 | if (ctx->param) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1775 | X509_VERIFY_PARAM_free(ctx->param); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 1776 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 1777 | ctx->param = param; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1778 | } |