]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
7e06a675 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
3e4b43b9 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <time.h> | |
12 | #include <errno.h> | |
d9b8b89b | 13 | #include <limits.h> |
d02b48c6 | 14 | |
25f2138b | 15 | #include "crypto/ctype.h" |
b39fc560 | 16 | #include "internal/cryptlib.h" |
17f389bb | 17 | #include <openssl/crypto.h> |
ec577822 BM |
18 | #include <openssl/buffer.h> |
19 | #include <openssl/evp.h> | |
20 | #include <openssl/asn1.h> | |
21 | #include <openssl/x509.h> | |
11262391 | 22 | #include <openssl/x509v3.h> |
ec577822 | 23 | #include <openssl/objects.h> |
176db6dc | 24 | #include "internal/dane.h" |
25f2138b | 25 | #include "crypto/x509.h" |
706457b7 | 26 | #include "x509_local.h" |
d02b48c6 | 27 | |
d43c4497 DSH |
28 | /* CRL score values */ |
29 | ||
30 | /* No unhandled critical extensions */ | |
31 | ||
0f113f3e | 32 | #define CRL_SCORE_NOCRITICAL 0x100 |
d43c4497 DSH |
33 | |
34 | /* certificate is within CRL scope */ | |
35 | ||
0f113f3e | 36 | #define CRL_SCORE_SCOPE 0x080 |
d43c4497 DSH |
37 | |
38 | /* CRL times valid */ | |
39 | ||
0f113f3e | 40 | #define CRL_SCORE_TIME 0x040 |
d43c4497 DSH |
41 | |
42 | /* Issuer name matches certificate */ | |
43 | ||
0f113f3e | 44 | #define CRL_SCORE_ISSUER_NAME 0x020 |
d43c4497 DSH |
45 | |
46 | /* If this score or above CRL is probably valid */ | |
47 | ||
48 | #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE) | |
49 | ||
50 | /* CRL issuer is certificate issuer */ | |
51 | ||
0f113f3e | 52 | #define CRL_SCORE_ISSUER_CERT 0x018 |
d43c4497 DSH |
53 | |
54 | /* CRL issuer is on certificate path */ | |
55 | ||
0f113f3e | 56 | #define CRL_SCORE_SAME_PATH 0x008 |
d43c4497 DSH |
57 | |
58 | /* CRL issuer matches CRL AKID */ | |
59 | ||
0f113f3e | 60 | #define CRL_SCORE_AKID 0x004 |
d43c4497 DSH |
61 | |
62 | /* Have a delta CRL with valid times */ | |
63 | ||
0f113f3e | 64 | #define CRL_SCORE_TIME_DELTA 0x002 |
d43c4497 | 65 | |
d9b8b89b VD |
66 | static int build_chain(X509_STORE_CTX *ctx); |
67 | static int verify_chain(X509_STORE_CTX *ctx); | |
170b7358 | 68 | static int dane_verify(X509_STORE_CTX *ctx); |
0f113f3e | 69 | static int null_callback(int ok, X509_STORE_CTX *e); |
2f043896 DSH |
70 | static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer); |
71 | static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x); | |
67ecd65c | 72 | static int check_chain(X509_STORE_CTX *ctx); |
e9746e03 | 73 | static int check_name_constraints(X509_STORE_CTX *ctx); |
3bf15e29 | 74 | static int check_id(X509_STORE_CTX *ctx); |
d9b8b89b | 75 | static int check_trust(X509_STORE_CTX *ctx, int num_untrusted); |
b545dc67 DSH |
76 | static int check_revocation(X509_STORE_CTX *ctx); |
77 | static int check_cert(X509_STORE_CTX *ctx); | |
5d7c222d | 78 | static int check_policy(X509_STORE_CTX *ctx); |
c9a81b30 | 79 | static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x); |
170b7358 | 80 | static int check_dane_issuer(X509_STORE_CTX *ctx, int depth); |
fbb82a60 VD |
81 | static int check_key_level(X509_STORE_CTX *ctx, X509 *cert); |
82 | static int check_sig_level(X509_STORE_CTX *ctx, X509 *cert); | |
cccf532f | 83 | static int check_curve(X509 *cert); |
4b96839f DSH |
84 | |
85 | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, | |
0f113f3e | 86 | unsigned int *preasons, X509_CRL *crl, X509 *x); |
d43c4497 | 87 | static int get_crl_delta(X509_STORE_CTX *ctx, |
0f113f3e MC |
88 | X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x); |
89 | static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, | |
90 | int *pcrl_score, X509_CRL *base, | |
91 | STACK_OF(X509_CRL) *crls); | |
92 | static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer, | |
93 | int *pcrl_score); | |
4b96839f | 94 | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, |
0f113f3e | 95 | unsigned int *preasons); |
9d84d4ed DSH |
96 | static int check_crl_path(X509_STORE_CTX *ctx, X509 *x); |
97 | static int check_crl_chain(X509_STORE_CTX *ctx, | |
0f113f3e MC |
98 | STACK_OF(X509) *cert_path, |
99 | STACK_OF(X509) *crl_path); | |
4b96839f | 100 | |
d02b48c6 | 101 | static int internal_verify(X509_STORE_CTX *ctx); |
d02b48c6 | 102 | |
6b691a5c | 103 | static int null_callback(int ok, X509_STORE_CTX *e) |
0f113f3e MC |
104 | { |
105 | return ok; | |
106 | } | |
d02b48c6 | 107 | |
0d8dbb52 | 108 | /*- |
ade08735 | 109 | * Return 1 if given cert is considered self-signed, 0 if not, or -1 on error. |
0d8dbb52 DDO |
110 | * This actually verifies self-signedness only if requested. |
111 | * It calls X509v3_cache_extensions() | |
112 | * to match issuer and subject names (i.e., the cert being self-issued) and any | |
113 | * present authority key identifier to match the subject key identifier, etc. | |
ade08735 | 114 | */ |
6725682d | 115 | int X509_self_signed(X509 *cert, int verify_signature) |
0f113f3e | 116 | { |
0d8dbb52 | 117 | EVP_PKEY *pkey; |
0c56a648 | 118 | |
0d8dbb52 | 119 | if ((pkey = X509_get0_pubkey(cert)) == NULL) { /* handles cert == NULL */ |
9311d0c4 | 120 | ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); |
0d8dbb52 DDO |
121 | return -1; |
122 | } | |
6725682d | 123 | if (!x509v3_cache_extensions(cert)) |
0d8dbb52 DDO |
124 | return -1; |
125 | if ((cert->ex_flags & EXFLAG_SS) == 0) | |
0f113f3e | 126 | return 0; |
0d8dbb52 DDO |
127 | if (!verify_signature) |
128 | return 1; | |
6725682d | 129 | return X509_verify(cert, pkey); |
0d8dbb52 | 130 | } |
2dabd822 | 131 | |
0d8dbb52 | 132 | /* Given a certificate try and find an exact match in the store */ |
2dabd822 | 133 | static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x) |
0f113f3e MC |
134 | { |
135 | STACK_OF(X509) *certs; | |
136 | X509 *xtmp = NULL; | |
137 | int i; | |
138 | /* Lookup all certs with matching subject name */ | |
bf973d06 | 139 | ERR_set_mark(); |
0f113f3e | 140 | certs = ctx->lookup_certs(ctx, X509_get_subject_name(x)); |
bf973d06 | 141 | ERR_pop_to_mark(); |
0f113f3e MC |
142 | if (certs == NULL) |
143 | return NULL; | |
144 | /* Look for exact match */ | |
145 | for (i = 0; i < sk_X509_num(certs); i++) { | |
146 | xtmp = sk_X509_value(certs, i); | |
147 | if (!X509_cmp(xtmp, x)) | |
148 | break; | |
e9e7b5df | 149 | xtmp = NULL; |
0f113f3e | 150 | } |
e9e7b5df | 151 | if (xtmp != NULL && !X509_up_ref(xtmp)) |
0f113f3e MC |
152 | xtmp = NULL; |
153 | sk_X509_pop_free(certs, X509_free); | |
154 | return xtmp; | |
155 | } | |
2dabd822 | 156 | |
70dd3c65 VD |
157 | /*- |
158 | * Inform the verify callback of an error. | |
e99505b4 DDO |
159 | * If 'x' is not NULL it is the error cert, otherwise use the chain cert at |
160 | * 'depth' | |
161 | * If 'err' is not X509_V_OK, that's the error value, otherwise leave | |
70dd3c65 VD |
162 | * unchanged (presumably set by the caller). |
163 | * | |
164 | * Returns 0 to abort verification with an error, non-zero to continue. | |
165 | */ | |
166 | static int verify_cb_cert(X509_STORE_CTX *ctx, X509 *x, int depth, int err) | |
167 | { | |
168 | ctx->error_depth = depth; | |
169 | ctx->current_cert = (x != NULL) ? x : sk_X509_value(ctx->chain, depth); | |
170 | if (err != X509_V_OK) | |
171 | ctx->error = err; | |
172 | return ctx->verify_cb(0, ctx); | |
173 | } | |
174 | ||
6e5e118c DO |
175 | #define CHECK_CB(cond, ctx, cert, depth, err) \ |
176 | if ((cond) && verify_cb_cert(ctx, cert, depth, err) == 0) \ | |
177 | return 0 | |
178 | ||
70dd3c65 VD |
179 | /*- |
180 | * Inform the verify callback of an error, CRL-specific variant. Here, the | |
181 | * error depth and certificate are already set, we just specify the error | |
182 | * number. | |
183 | * | |
184 | * Returns 0 to abort verification with an error, non-zero to continue. | |
185 | */ | |
186 | static int verify_cb_crl(X509_STORE_CTX *ctx, int err) | |
187 | { | |
188 | ctx->error = err; | |
189 | return ctx->verify_cb(0, ctx); | |
190 | } | |
191 | ||
fbb82a60 VD |
192 | static int check_auth_level(X509_STORE_CTX *ctx) |
193 | { | |
194 | int i; | |
195 | int num = sk_X509_num(ctx->chain); | |
196 | ||
197 | if (ctx->param->auth_level <= 0) | |
198 | return 1; | |
199 | ||
200 | for (i = 0; i < num; ++i) { | |
201 | X509 *cert = sk_X509_value(ctx->chain, i); | |
202 | ||
203 | /* | |
204 | * We've already checked the security of the leaf key, so here we only | |
205 | * check the security of issuer keys. | |
206 | */ | |
6e5e118c DO |
207 | CHECK_CB(i > 0 && !check_key_level(ctx, cert), |
208 | ctx, cert, i, X509_V_ERR_CA_KEY_TOO_SMALL); | |
fbb82a60 VD |
209 | /* |
210 | * We also check the signature algorithm security of all certificates | |
211 | * except those of the trust anchor at index num-1. | |
212 | */ | |
6e5e118c DO |
213 | CHECK_CB(i < num - 1 && !check_sig_level(ctx, cert), |
214 | ctx, cert, i, X509_V_ERR_CA_MD_TOO_WEAK); | |
fbb82a60 VD |
215 | } |
216 | return 1; | |
217 | } | |
218 | ||
d9b8b89b VD |
219 | static int verify_chain(X509_STORE_CTX *ctx) |
220 | { | |
d9b8b89b VD |
221 | int err; |
222 | int ok; | |
223 | ||
224 | /* | |
225 | * Before either returning with an error, or continuing with CRL checks, | |
226 | * instantiate chain public key parameters. | |
227 | */ | |
228 | if ((ok = build_chain(ctx)) == 0 || | |
67ecd65c | 229 | (ok = check_chain(ctx)) == 0 || |
fbb82a60 | 230 | (ok = check_auth_level(ctx)) == 0 || |
d9b8b89b VD |
231 | (ok = check_id(ctx)) == 0 || 1) |
232 | X509_get_pubkey_parameters(NULL, ctx->chain); | |
233 | if (ok == 0 || (ok = ctx->check_revocation(ctx)) == 0) | |
234 | return ok; | |
235 | ||
236 | err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain, | |
237 | ctx->param->flags); | |
6e5e118c | 238 | CHECK_CB(err != X509_V_OK, ctx, NULL, ctx->error_depth, err); |
d9b8b89b VD |
239 | |
240 | /* Verify chain signatures and expiration times */ | |
241 | ok = (ctx->verify != NULL) ? ctx->verify(ctx) : internal_verify(ctx); | |
242 | if (!ok) | |
243 | return ok; | |
244 | ||
8545051c DB |
245 | if ((ok = check_name_constraints(ctx)) == 0) |
246 | return ok; | |
247 | ||
d9b8b89b VD |
248 | #ifndef OPENSSL_NO_RFC3779 |
249 | /* RFC 3779 path validation, now that CRL check has been done */ | |
9021a5df | 250 | if ((ok = X509v3_asid_validate_path(ctx)) == 0) |
d9b8b89b | 251 | return ok; |
9021a5df | 252 | if ((ok = X509v3_addr_validate_path(ctx)) == 0) |
d9b8b89b VD |
253 | return ok; |
254 | #endif | |
255 | ||
256 | /* If we get this far evaluate policies */ | |
257 | if (ctx->param->flags & X509_V_FLAG_POLICY_CHECK) | |
258 | ok = ctx->check_policy(ctx); | |
259 | return ok; | |
260 | } | |
261 | ||
6b691a5c | 262 | int X509_verify_cert(X509_STORE_CTX *ctx) |
0f113f3e | 263 | { |
b9aec69a | 264 | SSL_DANE *dane = ctx->dane; |
f3e235ed | 265 | int ret; |
d9b8b89b | 266 | |
0f113f3e | 267 | if (ctx->cert == NULL) { |
9311d0c4 | 268 | ERR_raise(ERR_LIB_X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); |
f3e235ed | 269 | ctx->error = X509_V_ERR_INVALID_CALL; |
0f113f3e MC |
270 | return -1; |
271 | } | |
d9b8b89b | 272 | |
aae41f8c MC |
273 | if (ctx->chain != NULL) { |
274 | /* | |
275 | * This X509_STORE_CTX has already been used to verify a cert. We | |
276 | * cannot do another one. | |
277 | */ | |
9311d0c4 | 278 | ERR_raise(ERR_LIB_X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
f3e235ed | 279 | ctx->error = X509_V_ERR_INVALID_CALL; |
aae41f8c MC |
280 | return -1; |
281 | } | |
0f113f3e | 282 | |
eeccc237 | 283 | if (!X509_add_cert_new(&ctx->chain, ctx->cert, X509_ADD_FLAG_UP_REF)) { |
f3e235ed | 284 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
d9b8b89b | 285 | return -1; |
0f113f3e | 286 | } |
d9b8b89b | 287 | ctx->num_untrusted = 1; |
5d7c222d | 288 | |
fbb82a60 | 289 | /* If the peer's public key is too weak, we can stop early. */ |
6e5e118c DO |
290 | CHECK_CB(!check_key_level(ctx, ctx->cert), |
291 | ctx, ctx->cert, 0, X509_V_ERR_EE_KEY_TOO_SMALL); | |
fbb82a60 | 292 | |
f3e235ed VD |
293 | if (DANETLS_ENABLED(dane)) |
294 | ret = dane_verify(ctx); | |
295 | else | |
296 | ret = verify_chain(ctx); | |
297 | ||
170b7358 | 298 | /* |
f3e235ed VD |
299 | * Safety-net. If we are returning an error, we must also set ctx->error, |
300 | * so that the chain is not considered verified should the error be ignored | |
301 | * (e.g. TLS with SSL_VERIFY_NONE). | |
170b7358 | 302 | */ |
f3e235ed VD |
303 | if (ret <= 0 && ctx->error == X509_V_OK) |
304 | ctx->error = X509_V_ERR_UNSPECIFIED; | |
305 | return ret; | |
0f113f3e MC |
306 | } |
307 | ||
3bed88a3 DDO |
308 | static int sk_X509_contains(STACK_OF(X509) *sk, X509 *cert) |
309 | { | |
310 | int i, n = sk_X509_num(sk); | |
311 | ||
312 | for (i = 0; i < n; i++) | |
313 | if (X509_cmp(sk_X509_value(sk, i), cert) == 0) | |
314 | return 1; | |
315 | return 0; | |
316 | } | |
317 | ||
0f113f3e | 318 | /* |
3bed88a3 DDO |
319 | * Find in given STACK_OF(X509) sk a non-expired issuer cert (if any) of given cert x. |
320 | * The issuer must not be the same as x and must not yet be in ctx->chain, where the | |
321 | * exceptional case x is self-issued and ctx->chain has just one element is allowed. | |
2f043896 | 322 | */ |
2f043896 DSH |
323 | static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x) |
324 | { | |
0f113f3e | 325 | int i; |
c53f7355 | 326 | X509 *issuer, *rv = NULL; |
fbb82a60 | 327 | |
0f113f3e | 328 | for (i = 0; i < sk_X509_num(sk); i++) { |
c53f7355 | 329 | issuer = sk_X509_value(sk, i); |
d18c7ad6 DDO |
330 | /* |
331 | * Below check 'issuer != x' is an optimization and safety precaution: | |
332 | * Candidate issuer cert cannot be the same as the subject cert 'x'. | |
333 | */ | |
3bed88a3 DDO |
334 | if (issuer != x && ctx->check_issued(ctx, x, issuer) |
335 | && (((x->ex_flags & EXFLAG_SI) != 0 && sk_X509_num(ctx->chain) == 1) | |
336 | || !sk_X509_contains(ctx->chain, issuer))) { | |
c53f7355 VD |
337 | rv = issuer; |
338 | if (x509_check_cert_time(ctx, rv, -1)) | |
339 | break; | |
340 | } | |
0f113f3e | 341 | } |
c53f7355 | 342 | return rv; |
2f043896 DSH |
343 | } |
344 | ||
3bed88a3 DDO |
345 | /* Check that the given certificate 'x' is issued by the certificate 'issuer' */ |
346 | static int check_issued(ossl_unused X509_STORE_CTX *ctx, X509 *x, X509 *issuer) | |
2f043896 | 347 | { |
3bed88a3 | 348 | return x509_likely_issued(issuer, x) == X509_V_OK; |
2f043896 DSH |
349 | } |
350 | ||
351 | /* Alternative lookup method: look from a STACK stored in other_ctx */ | |
2f043896 DSH |
352 | static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) |
353 | { | |
0f113f3e | 354 | *issuer = find_issuer(ctx, ctx->other_ctx, x); |
e9e7b5df BE |
355 | |
356 | if (*issuer == NULL || !X509_up_ref(*issuer)) | |
357 | goto err; | |
358 | ||
359 | return 1; | |
360 | ||
361 | err: | |
362 | *issuer = NULL; | |
363 | return 0; | |
2f043896 | 364 | } |
2f043896 | 365 | |
8cc86b81 DDO |
366 | static STACK_OF(X509) *lookup_certs_sk(X509_STORE_CTX *ctx, |
367 | const X509_NAME *nm) | |
c864e761 DSH |
368 | { |
369 | STACK_OF(X509) *sk = NULL; | |
370 | X509 *x; | |
371 | int i; | |
7fcdbd83 | 372 | |
c864e761 DSH |
373 | for (i = 0; i < sk_X509_num(ctx->other_ctx); i++) { |
374 | x = sk_X509_value(ctx->other_ctx, i); | |
375 | if (X509_NAME_cmp(nm, X509_get_subject_name(x)) == 0) { | |
eeccc237 | 376 | if (!X509_add_cert_new(&sk, x, X509_ADD_FLAG_UP_REF)) { |
bbc3c22c | 377 | sk_X509_pop_free(sk, X509_free); |
7fcdbd83 | 378 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
c864e761 DSH |
379 | return NULL; |
380 | } | |
c864e761 DSH |
381 | } |
382 | } | |
383 | return sk; | |
384 | } | |
385 | ||
0daccd4d VD |
386 | /* |
387 | * Check EE or CA certificate purpose. For trusted certificates explicit local | |
388 | * auxiliary trust can be used to override EKU-restrictions. | |
389 | */ | |
390 | static int check_purpose(X509_STORE_CTX *ctx, X509 *x, int purpose, int depth, | |
391 | int must_be_ca) | |
392 | { | |
0daccd4d VD |
393 | int tr_ok = X509_TRUST_UNTRUSTED; |
394 | ||
395 | /* | |
396 | * For trusted certificates we want to see whether any auxiliary trust | |
33cc5dde | 397 | * settings trump the purpose constraints. |
0daccd4d VD |
398 | * |
399 | * This is complicated by the fact that the trust ordinals in | |
400 | * ctx->param->trust are entirely independent of the purpose ordinals in | |
401 | * ctx->param->purpose! | |
402 | * | |
403 | * What connects them is their mutual initialization via calls from | |
404 | * X509_STORE_CTX_set_default() into X509_VERIFY_PARAM_lookup() which sets | |
405 | * related values of both param->trust and param->purpose. It is however | |
406 | * typically possible to infer associated trust values from a purpose value | |
407 | * via the X509_PURPOSE API. | |
408 | * | |
409 | * Therefore, we can only check for trust overrides when the purpose we're | |
410 | * checking is the same as ctx->param->purpose and ctx->param->trust is | |
33cc5dde | 411 | * also set. |
0daccd4d VD |
412 | */ |
413 | if (depth >= ctx->num_untrusted && purpose == ctx->param->purpose) | |
414 | tr_ok = X509_check_trust(x, ctx->param->trust, X509_TRUST_NO_SS_COMPAT); | |
415 | ||
33cc5dde VD |
416 | switch (tr_ok) { |
417 | case X509_TRUST_TRUSTED: | |
0daccd4d | 418 | return 1; |
33cc5dde VD |
419 | case X509_TRUST_REJECTED: |
420 | break; | |
421 | default: | |
422 | switch (X509_check_purpose(x, purpose, must_be_ca > 0)) { | |
423 | case 1: | |
424 | return 1; | |
425 | case 0: | |
426 | break; | |
427 | default: | |
428 | if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) == 0) | |
429 | return 1; | |
430 | } | |
431 | break; | |
432 | } | |
0daccd4d | 433 | |
70dd3c65 | 434 | return verify_cb_cert(ctx, x, depth, X509_V_ERR_INVALID_PURPOSE); |
0daccd4d VD |
435 | } |
436 | ||
0f113f3e MC |
437 | /* |
438 | * Check a certificate chains extensions for consistency with the supplied | |
439 | * purpose | |
11262391 DSH |
440 | */ |
441 | ||
67ecd65c | 442 | static int check_chain(X509_STORE_CTX *ctx) |
11262391 | 443 | { |
0daccd4d | 444 | int i, must_be_ca, plen = 0; |
0f113f3e | 445 | X509 *x; |
0f113f3e MC |
446 | int proxy_path_length = 0; |
447 | int purpose; | |
448 | int allow_proxy_certs; | |
0daccd4d | 449 | int num = sk_X509_num(ctx->chain); |
0f113f3e | 450 | |
35a1cc90 MC |
451 | /*- |
452 | * must_be_ca can have 1 of 3 values: | |
453 | * -1: we accept both CA and non-CA certificates, to allow direct | |
454 | * use of self-signed certificates (which are marked as CA). | |
455 | * 0: we only accept non-CA certificates. This is currently not | |
456 | * used, but the possibility is present for future extensions. | |
457 | * 1: we only accept CA certificates. This is currently used for | |
458 | * all certificates in the chain except the leaf certificate. | |
459 | */ | |
0f113f3e MC |
460 | must_be_ca = -1; |
461 | ||
462 | /* CRL path validation */ | |
463 | if (ctx->parent) { | |
464 | allow_proxy_certs = 0; | |
465 | purpose = X509_PURPOSE_CRL_SIGN; | |
466 | } else { | |
467 | allow_proxy_certs = | |
468 | ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS); | |
0f113f3e MC |
469 | purpose = ctx->param->purpose; |
470 | } | |
471 | ||
0daccd4d | 472 | for (i = 0; i < num; i++) { |
0f113f3e | 473 | int ret; |
1e41dadf | 474 | |
0f113f3e | 475 | x = sk_X509_value(ctx->chain, i); |
6e5e118c DO |
476 | CHECK_CB((ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) == 0 |
477 | && (x->ex_flags & EXFLAG_CRITICAL) != 0, | |
478 | ctx, x, i, X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION); | |
479 | CHECK_CB(!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY), | |
480 | ctx, x, i, X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED); | |
0f113f3e MC |
481 | ret = X509_check_ca(x); |
482 | switch (must_be_ca) { | |
483 | case -1: | |
6e5e118c DO |
484 | CHECK_CB((ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0 |
485 | && ret != 1 && ret != 0, | |
486 | ctx, x, i, X509_V_ERR_INVALID_CA); | |
0f113f3e MC |
487 | break; |
488 | case 0: | |
5811387b | 489 | CHECK_CB(ret != 0, ctx, x, i, X509_V_ERR_INVALID_NON_CA); |
0f113f3e MC |
490 | break; |
491 | default: | |
4d9e33ac | 492 | /* X509_V_FLAG_X509_STRICT is implicit for intermediate CAs */ |
6e5e118c DO |
493 | CHECK_CB(ret == 0 |
494 | || ((i + 1 < num | |
495 | || ctx->param->flags & X509_V_FLAG_X509_STRICT) | |
496 | && ret != 1), ctx, x, i, X509_V_ERR_INVALID_CA); | |
0f113f3e MC |
497 | break; |
498 | } | |
cccf532f TM |
499 | if (num > 1) { |
500 | /* Check for presence of explicit elliptic curve parameters */ | |
501 | ret = check_curve(x); | |
6e5e118c DO |
502 | CHECK_CB(ret < 0, ctx, x, i, X509_V_ERR_UNSPECIFIED); |
503 | CHECK_CB(ret == 0, ctx, x, i, X509_V_ERR_EC_KEY_EXPLICIT_PARAMS); | |
cccf532f | 504 | } |
d72c8b45 | 505 | /* |
e99505b4 | 506 | * Do the following set of checks only if strict checking is requested |
d72c8b45 DDO |
507 | * and not for self-issued (including self-signed) EE (non-CA) certs |
508 | * because RFC 5280 does not apply to them according RFC 6818 section 2. | |
509 | */ | |
510 | if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0 | |
e41a2c4c DDO |
511 | && num > 1) { /* |
512 | * this should imply | |
513 | * !(i == 0 && (x->ex_flags & EXFLAG_CA) == 0 | |
514 | * && (x->ex_flags & EXFLAG_SI) != 0) | |
515 | */ | |
1e41dadf DDO |
516 | /* Check Basic Constraints according to RFC 5280 section 4.2.1.9 */ |
517 | if (x->ex_pathlen != -1) { | |
3309c4b7 DO |
518 | CHECK_CB((x->ex_flags & EXFLAG_CA) == 0, |
519 | ctx, x, i, X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA); | |
520 | CHECK_CB((x->ex_kusage & KU_KEY_CERT_SIGN) == 0, ctx, x, i, | |
521 | X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN); | |
1e41dadf | 522 | } |
3309c4b7 DO |
523 | CHECK_CB((x->ex_flags & EXFLAG_CA) != 0 |
524 | && (x->ex_flags & EXFLAG_BCONS) != 0 | |
525 | && (x->ex_flags & EXFLAG_BCONS_CRITICAL) == 0, | |
526 | ctx, x, i, X509_V_ERR_CA_BCONS_NOT_CRITICAL); | |
d72c8b45 | 527 | /* Check Key Usage according to RFC 5280 section 4.2.1.3 */ |
bb377c8d | 528 | if ((x->ex_flags & EXFLAG_CA) != 0) { |
3309c4b7 DO |
529 | CHECK_CB((x->ex_flags & EXFLAG_KUSAGE) == 0, |
530 | ctx, x, i, X509_V_ERR_CA_CERT_MISSING_KEY_USAGE); | |
bb377c8d | 531 | } else { |
3309c4b7 DO |
532 | CHECK_CB((x->ex_kusage & KU_KEY_CERT_SIGN) != 0, ctx, x, i, |
533 | X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA); | |
bb377c8d | 534 | } |
1e41dadf | 535 | /* Check issuer is non-empty acc. to RFC 5280 section 4.1.2.4 */ |
3309c4b7 DO |
536 | CHECK_CB(X509_NAME_entry_count(X509_get_issuer_name(x)) == 0, |
537 | ctx, x, i, X509_V_ERR_ISSUER_NAME_EMPTY); | |
1e41dadf | 538 | /* Check subject is non-empty acc. to RFC 5280 section 4.1.2.6 */ |
3309c4b7 DO |
539 | CHECK_CB(((x->ex_flags & EXFLAG_CA) != 0 |
540 | || (x->ex_kusage & KU_CRL_SIGN) != 0 | |
541 | || x->altname == NULL | |
542 | ) && X509_NAME_entry_count(X509_get_subject_name(x)) == 0, | |
543 | ctx, x, i, X509_V_ERR_SUBJECT_NAME_EMPTY); | |
544 | CHECK_CB(X509_NAME_entry_count(X509_get_subject_name(x)) == 0 | |
545 | && x->altname != NULL | |
546 | && (x->ex_flags & EXFLAG_SAN_CRITICAL) == 0, | |
547 | ctx, x, i, X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL); | |
1e41dadf | 548 | /* Check SAN is non-empty according to RFC 5280 section 4.2.1.6 */ |
3309c4b7 DO |
549 | CHECK_CB(x->altname != NULL && sk_GENERAL_NAME_num(x->altname) <= 0, |
550 | ctx, x, i, X509_V_ERR_EMPTY_SUBJECT_ALT_NAME); | |
1e41dadf DDO |
551 | /* TODO add more checks on SAN entries */ |
552 | /* Check sig alg consistency acc. to RFC 5280 section 4.1.1.2 */ | |
3309c4b7 DO |
553 | CHECK_CB(X509_ALGOR_cmp(&x->sig_alg, &x->cert_info.signature) != 0, |
554 | ctx, x, i, X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY); | |
555 | CHECK_CB(x->akid != NULL | |
556 | && (x->ex_flags & EXFLAG_AKID_CRITICAL) != 0, | |
557 | ctx, x, i, X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL); | |
558 | CHECK_CB(x->skid != NULL | |
559 | && (x->ex_flags & EXFLAG_SKID_CRITICAL) != 0, | |
560 | ctx, x, i, X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL); | |
1e41dadf DDO |
561 | if (X509_get_version(x) >= 2) { /* at least X.509v3 */ |
562 | /* Check AKID presence acc. to RFC 5280 section 4.2.1.1 */ | |
3309c4b7 DO |
563 | CHECK_CB(i + 1 < num /* |
564 | * this means not last cert in chain, | |
565 | * taken as "generated by conforming CAs" | |
566 | */ | |
567 | && (x->akid == NULL || x->akid->keyid == NULL), ctx, | |
568 | x, i, X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER); | |
1e41dadf | 569 | /* Check SKID presence acc. to RFC 5280 section 4.2.1.2 */ |
3309c4b7 DO |
570 | CHECK_CB((x->ex_flags & EXFLAG_CA) != 0 && x->skid == NULL, |
571 | ctx, x, i, X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER); | |
82bdd641 | 572 | } else { |
3309c4b7 DO |
573 | CHECK_CB(sk_X509_EXTENSION_num(X509_get0_extensions(x)) > 0, |
574 | ctx, x, i, X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3); | |
1e41dadf | 575 | } |
fa86e2ee | 576 | } |
6e5e118c | 577 | |
70dd3c65 VD |
578 | /* check_purpose() makes the callback as needed */ |
579 | if (purpose > 0 && !check_purpose(ctx, x, purpose, i, must_be_ca)) | |
580 | return 0; | |
e99505b4 | 581 | /* Check path length */ |
6e5e118c DO |
582 | CHECK_CB(i > 1 && x->ex_pathlen != -1 |
583 | && plen > x->ex_pathlen + proxy_path_length, | |
584 | ctx, x, i, X509_V_ERR_PATH_LENGTH_EXCEEDED); | |
ade08735 | 585 | /* Increment path length if not a self-issued intermediate CA */ |
dc5831da | 586 | if (i > 0 && (x->ex_flags & EXFLAG_SI) == 0) |
0f113f3e MC |
587 | plen++; |
588 | /* | |
589 | * If this certificate is a proxy certificate, the next certificate | |
590 | * must be another proxy certificate or a EE certificate. If not, | |
591 | * the next certificate must be a CA certificate. | |
592 | */ | |
593 | if (x->ex_flags & EXFLAG_PROXY) { | |
ed17c7c1 RL |
594 | /* |
595 | * RFC3820, 4.1.3 (b)(1) stipulates that if pCPathLengthConstraint | |
596 | * is less than max_path_length, the former should be copied to | |
597 | * the latter, and 4.1.4 (a) stipulates that max_path_length | |
598 | * should be verified to be larger than zero and decrement it. | |
599 | * | |
600 | * Because we're checking the certs in the reverse order, we start | |
601 | * with verifying that proxy_path_length isn't larger than pcPLC, | |
602 | * and copy the latter to the former if it is, and finally, | |
603 | * increment proxy_path_length. | |
604 | */ | |
605 | if (x->ex_pcpathlen != -1) { | |
6e5e118c DO |
606 | CHECK_CB(proxy_path_length > x->ex_pcpathlen, |
607 | ctx, x, i, X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED); | |
ed17c7c1 | 608 | proxy_path_length = x->ex_pcpathlen; |
0f113f3e MC |
609 | } |
610 | proxy_path_length++; | |
611 | must_be_ca = 0; | |
612 | } else | |
613 | must_be_ca = 1; | |
614 | } | |
0daccd4d | 615 | return 1; |
11262391 DSH |
616 | } |
617 | ||
55a6250f VD |
618 | static int has_san_id(X509 *x, int gtype) |
619 | { | |
620 | int i; | |
621 | int ret = 0; | |
622 | GENERAL_NAMES *gs = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); | |
623 | ||
624 | if (gs == NULL) | |
625 | return 0; | |
626 | ||
627 | for (i = 0; i < sk_GENERAL_NAME_num(gs); i++) { | |
628 | GENERAL_NAME *g = sk_GENERAL_NAME_value(gs, i); | |
629 | ||
630 | if (g->type == gtype) { | |
631 | ret = 1; | |
632 | break; | |
633 | } | |
634 | } | |
635 | GENERAL_NAMES_free(gs); | |
636 | return ret; | |
637 | } | |
638 | ||
e9746e03 | 639 | static int check_name_constraints(X509_STORE_CTX *ctx) |
0f113f3e | 640 | { |
70dd3c65 VD |
641 | int i; |
642 | ||
0f113f3e MC |
643 | /* Check name constraints for all certificates */ |
644 | for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) { | |
70dd3c65 VD |
645 | X509 *x = sk_X509_value(ctx->chain, i); |
646 | int j; | |
647 | ||
ade08735 | 648 | /* Ignore self-issued certs unless last in chain */ |
0f113f3e MC |
649 | if (i && (x->ex_flags & EXFLAG_SI)) |
650 | continue; | |
c8223538 RL |
651 | |
652 | /* | |
653 | * Proxy certificates policy has an extra constraint, where the | |
654 | * certificate subject MUST be the issuer with a single CN entry | |
655 | * added. | |
656 | * (RFC 3820: 3.4, 4.1.3 (a)(4)) | |
657 | */ | |
658 | if (x->ex_flags & EXFLAG_PROXY) { | |
659 | X509_NAME *tmpsubject = X509_get_subject_name(x); | |
660 | X509_NAME *tmpissuer = X509_get_issuer_name(x); | |
661 | X509_NAME_ENTRY *tmpentry = NULL; | |
662 | int last_object_nid = 0; | |
663 | int err = X509_V_OK; | |
664 | int last_object_loc = X509_NAME_entry_count(tmpsubject) - 1; | |
665 | ||
666 | /* Check that there are at least two RDNs */ | |
667 | if (last_object_loc < 1) { | |
668 | err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION; | |
669 | goto proxy_name_done; | |
670 | } | |
671 | ||
672 | /* | |
673 | * Check that there is exactly one more RDN in subject as | |
674 | * there is in issuer. | |
675 | */ | |
676 | if (X509_NAME_entry_count(tmpsubject) | |
677 | != X509_NAME_entry_count(tmpissuer) + 1) { | |
678 | err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION; | |
679 | goto proxy_name_done; | |
680 | } | |
681 | ||
682 | /* | |
683 | * Check that the last subject component isn't part of a | |
e99505b4 | 684 | * multi-valued RDN |
c8223538 RL |
685 | */ |
686 | if (X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject, | |
687 | last_object_loc)) | |
688 | == X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject, | |
689 | last_object_loc - 1))) { | |
690 | err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION; | |
691 | goto proxy_name_done; | |
692 | } | |
693 | ||
694 | /* | |
695 | * Check that the last subject RDN is a commonName, and that | |
696 | * all the previous RDNs match the issuer exactly | |
697 | */ | |
698 | tmpsubject = X509_NAME_dup(tmpsubject); | |
699 | if (tmpsubject == NULL) { | |
9311d0c4 | 700 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
c8223538 RL |
701 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
702 | return 0; | |
703 | } | |
704 | ||
705 | tmpentry = | |
706 | X509_NAME_delete_entry(tmpsubject, last_object_loc); | |
707 | last_object_nid = | |
708 | OBJ_obj2nid(X509_NAME_ENTRY_get_object(tmpentry)); | |
709 | ||
710 | if (last_object_nid != NID_commonName | |
711 | || X509_NAME_cmp(tmpsubject, tmpissuer) != 0) { | |
712 | err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION; | |
713 | } | |
714 | ||
715 | X509_NAME_ENTRY_free(tmpentry); | |
716 | X509_NAME_free(tmpsubject); | |
717 | ||
718 | proxy_name_done: | |
6e5e118c | 719 | CHECK_CB(err != X509_V_OK, ctx, x, i, err); |
c8223538 RL |
720 | } |
721 | ||
0f113f3e MC |
722 | /* |
723 | * Check against constraints for all certificates higher in chain | |
724 | * including trust anchor. Trust anchor not strictly speaking needed | |
725 | * but if it includes constraints it is to be assumed it expects them | |
726 | * to be obeyed. | |
727 | */ | |
728 | for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) { | |
729 | NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc; | |
70dd3c65 | 730 | |
0f113f3e | 731 | if (nc) { |
70dd3c65 VD |
732 | int rv = NAME_CONSTRAINTS_check(x, nc); |
733 | ||
5bd5dcd4 | 734 | /* If EE certificate check commonName too */ |
55a6250f VD |
735 | if (rv == X509_V_OK && i == 0 |
736 | && (ctx->param->hostflags | |
737 | & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT) == 0 | |
738 | && ((ctx->param->hostflags | |
739 | & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) != 0 | |
740 | || !has_san_id(x, GEN_DNS))) | |
5bd5dcd4 DSH |
741 | rv = NAME_CONSTRAINTS_check_CN(x, nc); |
742 | ||
f3e235ed VD |
743 | switch (rv) { |
744 | case X509_V_OK: | |
745 | break; | |
746 | case X509_V_ERR_OUT_OF_MEM: | |
70dd3c65 | 747 | return 0; |
f3e235ed | 748 | default: |
6e5e118c | 749 | CHECK_CB(1, ctx, x, i, rv); |
f3e235ed VD |
750 | break; |
751 | } | |
0f113f3e MC |
752 | } |
753 | } | |
754 | } | |
755 | return 1; | |
756 | } | |
e9746e03 | 757 | |
3bf15e29 | 758 | static int check_id_error(X509_STORE_CTX *ctx, int errcode) |
0f113f3e | 759 | { |
70dd3c65 | 760 | return verify_cb_cert(ctx, ctx->cert, 0, errcode); |
0f113f3e | 761 | } |
3bf15e29 | 762 | |
9689a6ae | 763 | static int check_hosts(X509 *x, X509_VERIFY_PARAM *vpm) |
0f113f3e MC |
764 | { |
765 | int i; | |
9689a6ae | 766 | int n = sk_OPENSSL_STRING_num(vpm->hosts); |
0f113f3e MC |
767 | char *name; |
768 | ||
9689a6ae DSH |
769 | if (vpm->peername != NULL) { |
770 | OPENSSL_free(vpm->peername); | |
771 | vpm->peername = NULL; | |
a0724ef1 | 772 | } |
0f113f3e | 773 | for (i = 0; i < n; ++i) { |
9689a6ae DSH |
774 | name = sk_OPENSSL_STRING_value(vpm->hosts, i); |
775 | if (X509_check_host(x, name, 0, vpm->hostflags, &vpm->peername) > 0) | |
0f113f3e MC |
776 | return 1; |
777 | } | |
778 | return n == 0; | |
779 | } | |
8abffa4a | 780 | |
3bf15e29 | 781 | static int check_id(X509_STORE_CTX *ctx) |
0f113f3e MC |
782 | { |
783 | X509_VERIFY_PARAM *vpm = ctx->param; | |
0f113f3e | 784 | X509 *x = ctx->cert; |
9689a6ae | 785 | if (vpm->hosts && check_hosts(x, vpm) <= 0) { |
0f113f3e MC |
786 | if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH)) |
787 | return 0; | |
788 | } | |
9689a6ae | 789 | if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) { |
0f113f3e MC |
790 | if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH)) |
791 | return 0; | |
792 | } | |
9689a6ae | 793 | if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) { |
0f113f3e MC |
794 | if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH)) |
795 | return 0; | |
796 | } | |
797 | return 1; | |
798 | } | |
3bf15e29 | 799 | |
d9b8b89b | 800 | static int check_trust(X509_STORE_CTX *ctx, int num_untrusted) |
51630a37 | 801 | { |
70dd3c65 | 802 | int i; |
0f113f3e | 803 | X509 *x = NULL; |
d9b8b89b | 804 | X509 *mx; |
b9aec69a | 805 | SSL_DANE *dane = ctx->dane; |
d9b8b89b VD |
806 | int num = sk_X509_num(ctx->chain); |
807 | int trust; | |
808 | ||
bdcadca2 VD |
809 | /* |
810 | * Check for a DANE issuer at depth 1 or greater, if it is a DANE-TA(2) | |
811 | * match, we're done, otherwise we'll merely record the match depth. | |
812 | */ | |
813 | if (DANETLS_HAS_TA(dane) && num_untrusted > 0 && num_untrusted < num) { | |
170b7358 VD |
814 | switch (trust = check_dane_issuer(ctx, num_untrusted)) { |
815 | case X509_TRUST_TRUSTED: | |
816 | case X509_TRUST_REJECTED: | |
817 | return trust; | |
818 | } | |
819 | } | |
820 | ||
d9b8b89b VD |
821 | /* |
822 | * Check trusted certificates in chain at depth num_untrusted and up. | |
823 | * Note, that depths 0..num_untrusted-1 may also contain trusted | |
824 | * certificates, but the caller is expected to have already checked those, | |
825 | * and wants to incrementally check just any added since. | |
826 | */ | |
827 | for (i = num_untrusted; i < num; i++) { | |
0f113f3e | 828 | x = sk_X509_value(ctx->chain, i); |
d9b8b89b | 829 | trust = X509_check_trust(x, ctx->param->trust, 0); |
0f113f3e | 830 | /* If explicitly trusted return trusted */ |
d9b8b89b VD |
831 | if (trust == X509_TRUST_TRUSTED) |
832 | goto trusted; | |
833 | if (trust == X509_TRUST_REJECTED) | |
834 | goto rejected; | |
0f113f3e | 835 | } |
d9b8b89b | 836 | |
0f113f3e | 837 | /* |
d9b8b89b VD |
838 | * If we are looking at a trusted certificate, and accept partial chains, |
839 | * the chain is PKIX trusted. | |
0f113f3e | 840 | */ |
d9b8b89b VD |
841 | if (num_untrusted < num) { |
842 | if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) | |
843 | goto trusted; | |
844 | return X509_TRUST_UNTRUSTED; | |
845 | } | |
846 | ||
497ecc0d | 847 | if (num_untrusted == num && ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { |
d9b8b89b VD |
848 | /* |
849 | * Last-resort call with no new trusted certificates, check the leaf | |
850 | * for a direct trust store match. | |
851 | */ | |
bdcadca2 VD |
852 | i = 0; |
853 | x = sk_X509_value(ctx->chain, i); | |
0f113f3e | 854 | mx = lookup_cert_match(ctx, x); |
d9b8b89b VD |
855 | if (!mx) |
856 | return X509_TRUST_UNTRUSTED; | |
857 | ||
858 | /* | |
859 | * Check explicit auxiliary trust/reject settings. If none are set, | |
860 | * we'll accept X509_TRUST_UNTRUSTED when not self-signed. | |
861 | */ | |
862 | trust = X509_check_trust(mx, ctx->param->trust, 0); | |
863 | if (trust == X509_TRUST_REJECTED) { | |
864 | X509_free(mx); | |
865 | goto rejected; | |
0f113f3e | 866 | } |
d9b8b89b VD |
867 | |
868 | /* Replace leaf with trusted match */ | |
869 | (void) sk_X509_set(ctx->chain, 0, mx); | |
870 | X509_free(x); | |
871 | ctx->num_untrusted = 0; | |
872 | goto trusted; | |
0f113f3e MC |
873 | } |
874 | ||
875 | /* | |
876 | * If no trusted certs in chain at all return untrusted and allow | |
877 | * standard (no issuer cert) etc errors to be indicated. | |
878 | */ | |
879 | return X509_TRUST_UNTRUSTED; | |
d9b8b89b VD |
880 | |
881 | rejected: | |
6e5e118c DO |
882 | return verify_cb_cert(ctx, x, i, X509_V_ERR_CERT_REJECTED) == 0 |
883 | ? X509_TRUST_REJECTED : X509_TRUST_UNTRUSTED; | |
d9b8b89b VD |
884 | |
885 | trusted: | |
170b7358 VD |
886 | if (!DANETLS_ENABLED(dane)) |
887 | return X509_TRUST_TRUSTED; | |
888 | if (dane->pdpth < 0) | |
889 | dane->pdpth = num_untrusted; | |
890 | /* With DANE, PKIX alone is not trusted until we have both */ | |
891 | if (dane->mdpth >= 0) | |
892 | return X509_TRUST_TRUSTED; | |
893 | return X509_TRUST_UNTRUSTED; | |
51630a37 DSH |
894 | } |
895 | ||
b545dc67 | 896 | static int check_revocation(X509_STORE_CTX *ctx) |
0f113f3e | 897 | { |
4c9b0a03 | 898 | int i = 0, last = 0, ok = 0; |
0f113f3e MC |
899 | if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK)) |
900 | return 1; | |
901 | if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) | |
902 | last = sk_X509_num(ctx->chain) - 1; | |
903 | else { | |
904 | /* If checking CRL paths this isn't the EE certificate */ | |
905 | if (ctx->parent) | |
906 | return 1; | |
907 | last = 0; | |
908 | } | |
909 | for (i = 0; i <= last; i++) { | |
910 | ctx->error_depth = i; | |
911 | ok = check_cert(ctx); | |
912 | if (!ok) | |
913 | return ok; | |
914 | } | |
915 | return 1; | |
916 | } | |
b545dc67 DSH |
917 | |
918 | static int check_cert(X509_STORE_CTX *ctx) | |
0f113f3e MC |
919 | { |
920 | X509_CRL *crl = NULL, *dcrl = NULL; | |
70dd3c65 VD |
921 | int ok = 0; |
922 | int cnum = ctx->error_depth; | |
923 | X509 *x = sk_X509_value(ctx->chain, cnum); | |
924 | ||
0f113f3e MC |
925 | ctx->current_cert = x; |
926 | ctx->current_issuer = NULL; | |
927 | ctx->current_crl_score = 0; | |
928 | ctx->current_reasons = 0; | |
70dd3c65 | 929 | |
790555d6 RL |
930 | if (x->ex_flags & EXFLAG_PROXY) |
931 | return 1; | |
932 | ||
0f113f3e | 933 | while (ctx->current_reasons != CRLDP_ALL_REASONS) { |
70dd3c65 VD |
934 | unsigned int last_reasons = ctx->current_reasons; |
935 | ||
0f113f3e MC |
936 | /* Try to retrieve relevant CRL */ |
937 | if (ctx->get_crl) | |
938 | ok = ctx->get_crl(ctx, &crl, x); | |
939 | else | |
940 | ok = get_crl_delta(ctx, &crl, &dcrl, x); | |
941 | /* | |
942 | * If error looking up CRL, nothing we can do except notify callback | |
943 | */ | |
944 | if (!ok) { | |
70dd3c65 VD |
945 | ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL); |
946 | goto done; | |
0f113f3e MC |
947 | } |
948 | ctx->current_crl = crl; | |
949 | ok = ctx->check_crl(ctx, crl); | |
950 | if (!ok) | |
70dd3c65 | 951 | goto done; |
0f113f3e MC |
952 | |
953 | if (dcrl) { | |
954 | ok = ctx->check_crl(ctx, dcrl); | |
955 | if (!ok) | |
70dd3c65 | 956 | goto done; |
0f113f3e MC |
957 | ok = ctx->cert_crl(ctx, dcrl, x); |
958 | if (!ok) | |
70dd3c65 | 959 | goto done; |
0f113f3e MC |
960 | } else |
961 | ok = 1; | |
962 | ||
963 | /* Don't look in full CRL if delta reason is removefromCRL */ | |
964 | if (ok != 2) { | |
965 | ok = ctx->cert_crl(ctx, crl, x); | |
966 | if (!ok) | |
70dd3c65 | 967 | goto done; |
0f113f3e MC |
968 | } |
969 | ||
970 | X509_CRL_free(crl); | |
971 | X509_CRL_free(dcrl); | |
972 | crl = NULL; | |
973 | dcrl = NULL; | |
974 | /* | |
60250017 | 975 | * If reasons not updated we won't get anywhere by another iteration, |
0f113f3e MC |
976 | * so exit loop. |
977 | */ | |
978 | if (last_reasons == ctx->current_reasons) { | |
70dd3c65 VD |
979 | ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL); |
980 | goto done; | |
0f113f3e MC |
981 | } |
982 | } | |
70dd3c65 | 983 | done: |
0f113f3e MC |
984 | X509_CRL_free(crl); |
985 | X509_CRL_free(dcrl); | |
986 | ||
987 | ctx->current_crl = NULL; | |
988 | return ok; | |
0f113f3e | 989 | } |
b545dc67 | 990 | |
e1a27eb3 DSH |
991 | /* Check CRL times against values in X509_STORE_CTX */ |
992 | ||
993 | static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) | |
0f113f3e MC |
994 | { |
995 | time_t *ptime; | |
996 | int i; | |
70dd3c65 | 997 | |
0f113f3e MC |
998 | if (notify) |
999 | ctx->current_crl = crl; | |
1000 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) | |
1001 | ptime = &ctx->param->check_time; | |
d35ff2c0 DW |
1002 | else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) |
1003 | return 1; | |
0f113f3e MC |
1004 | else |
1005 | ptime = NULL; | |
1006 | ||
568ce3a5 | 1007 | i = X509_cmp_time(X509_CRL_get0_lastUpdate(crl), ptime); |
0f113f3e MC |
1008 | if (i == 0) { |
1009 | if (!notify) | |
1010 | return 0; | |
70dd3c65 | 1011 | if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD)) |
0f113f3e MC |
1012 | return 0; |
1013 | } | |
1014 | ||
1015 | if (i > 0) { | |
1016 | if (!notify) | |
1017 | return 0; | |
70dd3c65 | 1018 | if (!verify_cb_crl(ctx, X509_V_ERR_CRL_NOT_YET_VALID)) |
0f113f3e MC |
1019 | return 0; |
1020 | } | |
1021 | ||
568ce3a5 DSH |
1022 | if (X509_CRL_get0_nextUpdate(crl)) { |
1023 | i = X509_cmp_time(X509_CRL_get0_nextUpdate(crl), ptime); | |
0f113f3e MC |
1024 | |
1025 | if (i == 0) { | |
1026 | if (!notify) | |
1027 | return 0; | |
70dd3c65 | 1028 | if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD)) |
0f113f3e MC |
1029 | return 0; |
1030 | } | |
e99505b4 | 1031 | /* Ignore expiration of base CRL is delta is valid */ |
0f113f3e MC |
1032 | if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) { |
1033 | if (!notify) | |
1034 | return 0; | |
70dd3c65 | 1035 | if (!verify_cb_crl(ctx, X509_V_ERR_CRL_HAS_EXPIRED)) |
0f113f3e MC |
1036 | return 0; |
1037 | } | |
1038 | } | |
1039 | ||
1040 | if (notify) | |
1041 | ctx->current_crl = NULL; | |
1042 | ||
1043 | return 1; | |
1044 | } | |
e1a27eb3 | 1045 | |
d43c4497 | 1046 | static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl, |
0f113f3e MC |
1047 | X509 **pissuer, int *pscore, unsigned int *preasons, |
1048 | STACK_OF(X509_CRL) *crls) | |
1049 | { | |
1050 | int i, crl_score, best_score = *pscore; | |
1051 | unsigned int reasons, best_reasons = 0; | |
1052 | X509 *x = ctx->current_cert; | |
1053 | X509_CRL *crl, *best_crl = NULL; | |
1054 | X509 *crl_issuer = NULL, *best_crl_issuer = NULL; | |
1055 | ||
1056 | for (i = 0; i < sk_X509_CRL_num(crls); i++) { | |
1057 | crl = sk_X509_CRL_value(crls, i); | |
1058 | reasons = *preasons; | |
1059 | crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x); | |
8b7c51a0 | 1060 | if (crl_score < best_score || crl_score == 0) |
626aa248 DSH |
1061 | continue; |
1062 | /* If current CRL is equivalent use it if it is newer */ | |
8b7c51a0 | 1063 | if (crl_score == best_score && best_crl != NULL) { |
626aa248 | 1064 | int day, sec; |
568ce3a5 DSH |
1065 | if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl), |
1066 | X509_CRL_get0_lastUpdate(crl)) == 0) | |
626aa248 | 1067 | continue; |
e032117d DSH |
1068 | /* |
1069 | * ASN1_TIME_diff never returns inconsistent signs for |day| | |
1070 | * and |sec|. | |
1071 | */ | |
1072 | if (day <= 0 && sec <= 0) | |
626aa248 | 1073 | continue; |
0f113f3e | 1074 | } |
626aa248 DSH |
1075 | best_crl = crl; |
1076 | best_crl_issuer = crl_issuer; | |
1077 | best_score = crl_score; | |
1078 | best_reasons = reasons; | |
0f113f3e MC |
1079 | } |
1080 | ||
1081 | if (best_crl) { | |
222561fe | 1082 | X509_CRL_free(*pcrl); |
0f113f3e MC |
1083 | *pcrl = best_crl; |
1084 | *pissuer = best_crl_issuer; | |
1085 | *pscore = best_score; | |
1086 | *preasons = best_reasons; | |
65cbf983 | 1087 | X509_CRL_up_ref(best_crl); |
25aaa98a RS |
1088 | X509_CRL_free(*pdcrl); |
1089 | *pdcrl = NULL; | |
0f113f3e MC |
1090 | get_delta_sk(ctx, pdcrl, pscore, best_crl, crls); |
1091 | } | |
1092 | ||
1093 | if (best_score >= CRL_SCORE_VALID) | |
1094 | return 1; | |
1095 | ||
1096 | return 0; | |
1097 | } | |
1098 | ||
1099 | /* | |
1100 | * Compare two CRL extensions for delta checking purposes. They should be | |
d43c4497 DSH |
1101 | * both present or both absent. If both present all fields must be identical. |
1102 | */ | |
1103 | ||
1104 | static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid) | |
0f113f3e MC |
1105 | { |
1106 | ASN1_OCTET_STRING *exta, *extb; | |
1107 | int i; | |
1108 | i = X509_CRL_get_ext_by_NID(a, nid, -1); | |
1109 | if (i >= 0) { | |
1110 | /* Can't have multiple occurrences */ | |
1111 | if (X509_CRL_get_ext_by_NID(a, nid, i) != -1) | |
1112 | return 0; | |
1113 | exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i)); | |
1114 | } else | |
1115 | exta = NULL; | |
d43c4497 | 1116 | |
0f113f3e | 1117 | i = X509_CRL_get_ext_by_NID(b, nid, -1); |
d43c4497 | 1118 | |
0f113f3e | 1119 | if (i >= 0) { |
d43c4497 | 1120 | |
0f113f3e MC |
1121 | if (X509_CRL_get_ext_by_NID(b, nid, i) != -1) |
1122 | return 0; | |
1123 | extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i)); | |
1124 | } else | |
1125 | extb = NULL; | |
d43c4497 | 1126 | |
0f113f3e MC |
1127 | if (!exta && !extb) |
1128 | return 1; | |
d43c4497 | 1129 | |
0f113f3e MC |
1130 | if (!exta || !extb) |
1131 | return 0; | |
d43c4497 | 1132 | |
0f113f3e MC |
1133 | if (ASN1_OCTET_STRING_cmp(exta, extb)) |
1134 | return 0; | |
d43c4497 | 1135 | |
0f113f3e MC |
1136 | return 1; |
1137 | } | |
d43c4497 DSH |
1138 | |
1139 | /* See if a base and delta are compatible */ | |
1140 | ||
1141 | static int check_delta_base(X509_CRL *delta, X509_CRL *base) | |
0f113f3e MC |
1142 | { |
1143 | /* Delta CRL must be a delta */ | |
1144 | if (!delta->base_crl_number) | |
1145 | return 0; | |
1146 | /* Base must have a CRL number */ | |
1147 | if (!base->crl_number) | |
1148 | return 0; | |
1149 | /* Issuer names must match */ | |
1150 | if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta))) | |
1151 | return 0; | |
1152 | /* AKID and IDP must match */ | |
1153 | if (!crl_extension_match(delta, base, NID_authority_key_identifier)) | |
1154 | return 0; | |
1155 | if (!crl_extension_match(delta, base, NID_issuing_distribution_point)) | |
1156 | return 0; | |
1157 | /* Delta CRL base number must not exceed Full CRL number. */ | |
1158 | if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0) | |
1159 | return 0; | |
1160 | /* Delta CRL number must exceed full CRL number */ | |
1161 | if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0) | |
1162 | return 1; | |
1163 | return 0; | |
1164 | } | |
1165 | ||
1166 | /* | |
1167 | * For a given base CRL find a delta... maybe extend to delta scoring or | |
1168 | * retrieve a chain of deltas... | |
d43c4497 DSH |
1169 | */ |
1170 | ||
1171 | static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore, | |
0f113f3e MC |
1172 | X509_CRL *base, STACK_OF(X509_CRL) *crls) |
1173 | { | |
1174 | X509_CRL *delta; | |
1175 | int i; | |
1176 | if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS)) | |
1177 | return; | |
1178 | if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST)) | |
1179 | return; | |
1180 | for (i = 0; i < sk_X509_CRL_num(crls); i++) { | |
1181 | delta = sk_X509_CRL_value(crls, i); | |
1182 | if (check_delta_base(delta, base)) { | |
1183 | if (check_crl_time(ctx, delta, 0)) | |
1184 | *pscore |= CRL_SCORE_TIME_DELTA; | |
65cbf983 | 1185 | X509_CRL_up_ref(delta); |
0f113f3e MC |
1186 | *dcrl = delta; |
1187 | return; | |
1188 | } | |
1189 | } | |
1190 | *dcrl = NULL; | |
1191 | } | |
1192 | ||
1193 | /* | |
1194 | * For a given CRL return how suitable it is for the supplied certificate | |
1195 | * 'x'. The return value is a mask of several criteria. If the issuer is not | |
1196 | * the certificate issuer this is returned in *pissuer. The reasons mask is | |
1197 | * also used to determine if the CRL is suitable: if no new reasons the CRL | |
1198 | * is rejected, otherwise reasons is updated. | |
4b96839f DSH |
1199 | */ |
1200 | ||
1201 | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, | |
0f113f3e MC |
1202 | unsigned int *preasons, X509_CRL *crl, X509 *x) |
1203 | { | |
1204 | ||
1205 | int crl_score = 0; | |
1206 | unsigned int tmp_reasons = *preasons, crl_reasons; | |
1207 | ||
1208 | /* First see if we can reject CRL straight away */ | |
1209 | ||
1210 | /* Invalid IDP cannot be processed */ | |
1211 | if (crl->idp_flags & IDP_INVALID) | |
1212 | return 0; | |
1213 | /* Reason codes or indirect CRLs need extended CRL support */ | |
1214 | if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) { | |
1215 | if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS)) | |
1216 | return 0; | |
1217 | } else if (crl->idp_flags & IDP_REASONS) { | |
1218 | /* If no new reasons reject */ | |
1219 | if (!(crl->idp_reasons & ~tmp_reasons)) | |
1220 | return 0; | |
1221 | } | |
1222 | /* Don't process deltas at this stage */ | |
1223 | else if (crl->base_crl_number) | |
1224 | return 0; | |
1225 | /* If issuer name doesn't match certificate need indirect CRL */ | |
1226 | if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) { | |
1227 | if (!(crl->idp_flags & IDP_INDIRECT)) | |
1228 | return 0; | |
1229 | } else | |
1230 | crl_score |= CRL_SCORE_ISSUER_NAME; | |
1231 | ||
1232 | if (!(crl->flags & EXFLAG_CRITICAL)) | |
1233 | crl_score |= CRL_SCORE_NOCRITICAL; | |
1234 | ||
e99505b4 | 1235 | /* Check expiration */ |
0f113f3e MC |
1236 | if (check_crl_time(ctx, crl, 0)) |
1237 | crl_score |= CRL_SCORE_TIME; | |
1238 | ||
1239 | /* Check authority key ID and locate certificate issuer */ | |
1240 | crl_akid_check(ctx, crl, pissuer, &crl_score); | |
1241 | ||
1242 | /* If we can't locate certificate issuer at this point forget it */ | |
1243 | ||
1244 | if (!(crl_score & CRL_SCORE_AKID)) | |
1245 | return 0; | |
1246 | ||
1247 | /* Check cert for matching CRL distribution points */ | |
1248 | ||
1249 | if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) { | |
1250 | /* If no new reasons reject */ | |
1251 | if (!(crl_reasons & ~tmp_reasons)) | |
1252 | return 0; | |
1253 | tmp_reasons |= crl_reasons; | |
1254 | crl_score |= CRL_SCORE_SCOPE; | |
1255 | } | |
1256 | ||
1257 | *preasons = tmp_reasons; | |
1258 | ||
1259 | return crl_score; | |
1260 | ||
1261 | } | |
4b96839f DSH |
1262 | |
1263 | static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, | |
0f113f3e MC |
1264 | X509 **pissuer, int *pcrl_score) |
1265 | { | |
1266 | X509 *crl_issuer = NULL; | |
8cc86b81 | 1267 | const X509_NAME *cnm = X509_CRL_get_issuer(crl); |
0f113f3e MC |
1268 | int cidx = ctx->error_depth; |
1269 | int i; | |
1270 | ||
1271 | if (cidx != sk_X509_num(ctx->chain) - 1) | |
1272 | cidx++; | |
1273 | ||
1274 | crl_issuer = sk_X509_value(ctx->chain, cidx); | |
1275 | ||
1276 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { | |
1277 | if (*pcrl_score & CRL_SCORE_ISSUER_NAME) { | |
1278 | *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT; | |
1279 | *pissuer = crl_issuer; | |
1280 | return; | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) { | |
1285 | crl_issuer = sk_X509_value(ctx->chain, cidx); | |
1286 | if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) | |
1287 | continue; | |
1288 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { | |
1289 | *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH; | |
1290 | *pissuer = crl_issuer; | |
1291 | return; | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | /* Anything else needs extended CRL support */ | |
1296 | ||
1297 | if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) | |
1298 | return; | |
1299 | ||
1300 | /* | |
1301 | * Otherwise the CRL issuer is not on the path. Look for it in the set of | |
1302 | * untrusted certificates. | |
1303 | */ | |
1304 | for (i = 0; i < sk_X509_num(ctx->untrusted); i++) { | |
1305 | crl_issuer = sk_X509_value(ctx->untrusted, i); | |
1306 | if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) | |
1307 | continue; | |
1308 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { | |
1309 | *pissuer = crl_issuer; | |
1310 | *pcrl_score |= CRL_SCORE_AKID; | |
1311 | return; | |
1312 | } | |
1313 | } | |
1314 | } | |
1315 | ||
1316 | /* | |
1317 | * Check the path of a CRL issuer certificate. This creates a new | |
9d84d4ed | 1318 | * X509_STORE_CTX and populates it with most of the parameters from the |
0f113f3e MC |
1319 | * parent. This could be optimised somewhat since a lot of path checking will |
1320 | * be duplicated by the parent, but this will rarely be used in practice. | |
9d84d4ed DSH |
1321 | */ |
1322 | ||
1323 | static int check_crl_path(X509_STORE_CTX *ctx, X509 *x) | |
0f113f3e MC |
1324 | { |
1325 | X509_STORE_CTX crl_ctx; | |
1326 | int ret; | |
70dd3c65 | 1327 | |
0f113f3e MC |
1328 | /* Don't allow recursive CRL path validation */ |
1329 | if (ctx->parent) | |
1330 | return 0; | |
faa9dcd4 | 1331 | if (!X509_STORE_CTX_init(&crl_ctx, ctx->store, x, ctx->untrusted)) |
0f113f3e MC |
1332 | return -1; |
1333 | ||
1334 | crl_ctx.crls = ctx->crls; | |
1335 | /* Copy verify params across */ | |
1336 | X509_STORE_CTX_set0_param(&crl_ctx, ctx->param); | |
1337 | ||
1338 | crl_ctx.parent = ctx; | |
1339 | crl_ctx.verify_cb = ctx->verify_cb; | |
1340 | ||
1341 | /* Verify CRL issuer */ | |
1342 | ret = X509_verify_cert(&crl_ctx); | |
0f113f3e MC |
1343 | if (ret <= 0) |
1344 | goto err; | |
1345 | ||
1346 | /* Check chain is acceptable */ | |
0f113f3e MC |
1347 | ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain); |
1348 | err: | |
1349 | X509_STORE_CTX_cleanup(&crl_ctx); | |
1350 | return ret; | |
1351 | } | |
1352 | ||
1353 | /* | |
1354 | * RFC3280 says nothing about the relationship between CRL path and | |
1355 | * certificate path, which could lead to situations where a certificate could | |
e99505b4 | 1356 | * be revoked or validated by a CA not authorized to do so. RFC5280 is more |
0f113f3e MC |
1357 | * strict and states that the two paths must end in the same trust anchor, |
1358 | * though some discussions remain... until this is resolved we use the | |
1359 | * RFC5280 version | |
9d84d4ed DSH |
1360 | */ |
1361 | ||
1362 | static int check_crl_chain(X509_STORE_CTX *ctx, | |
0f113f3e MC |
1363 | STACK_OF(X509) *cert_path, |
1364 | STACK_OF(X509) *crl_path) | |
1365 | { | |
1366 | X509 *cert_ta, *crl_ta; | |
1367 | cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1); | |
1368 | crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1); | |
1369 | if (!X509_cmp(cert_ta, crl_ta)) | |
1370 | return 1; | |
1371 | return 0; | |
1372 | } | |
9d84d4ed | 1373 | |
3a83462d MC |
1374 | /*- |
1375 | * Check for match between two dist point names: three separate cases. | |
3e727a3b DSH |
1376 | * 1. Both are relative names and compare X509_NAME types. |
1377 | * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES. | |
1378 | * 3. Both are full names and compare two GENERAL_NAMES. | |
d0fff69d | 1379 | * 4. One is NULL: automatic match. |
3e727a3b DSH |
1380 | */ |
1381 | ||
3e727a3b | 1382 | static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b) |
0f113f3e MC |
1383 | { |
1384 | X509_NAME *nm = NULL; | |
1385 | GENERAL_NAMES *gens = NULL; | |
1386 | GENERAL_NAME *gena, *genb; | |
1387 | int i, j; | |
1388 | if (!a || !b) | |
1389 | return 1; | |
1390 | if (a->type == 1) { | |
1391 | if (!a->dpname) | |
1392 | return 0; | |
1393 | /* Case 1: two X509_NAME */ | |
1394 | if (b->type == 1) { | |
1395 | if (!b->dpname) | |
1396 | return 0; | |
1397 | if (!X509_NAME_cmp(a->dpname, b->dpname)) | |
1398 | return 1; | |
1399 | else | |
1400 | return 0; | |
1401 | } | |
1402 | /* Case 2: set name and GENERAL_NAMES appropriately */ | |
1403 | nm = a->dpname; | |
1404 | gens = b->name.fullname; | |
1405 | } else if (b->type == 1) { | |
1406 | if (!b->dpname) | |
1407 | return 0; | |
1408 | /* Case 2: set name and GENERAL_NAMES appropriately */ | |
1409 | gens = a->name.fullname; | |
1410 | nm = b->dpname; | |
1411 | } | |
1412 | ||
1413 | /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */ | |
1414 | if (nm) { | |
1415 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | |
1416 | gena = sk_GENERAL_NAME_value(gens, i); | |
1417 | if (gena->type != GEN_DIRNAME) | |
1418 | continue; | |
1419 | if (!X509_NAME_cmp(nm, gena->d.directoryName)) | |
1420 | return 1; | |
1421 | } | |
1422 | return 0; | |
1423 | } | |
1424 | ||
1425 | /* Else case 3: two GENERAL_NAMES */ | |
1426 | ||
1427 | for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) { | |
1428 | gena = sk_GENERAL_NAME_value(a->name.fullname, i); | |
1429 | for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) { | |
1430 | genb = sk_GENERAL_NAME_value(b->name.fullname, j); | |
1431 | if (!GENERAL_NAME_cmp(gena, genb)) | |
1432 | return 1; | |
1433 | } | |
1434 | } | |
1435 | ||
1436 | return 0; | |
1437 | ||
1438 | } | |
bc7535bc | 1439 | |
4b96839f | 1440 | static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score) |
0f113f3e MC |
1441 | { |
1442 | int i; | |
8cc86b81 | 1443 | const X509_NAME *nm = X509_CRL_get_issuer(crl); |
0f113f3e MC |
1444 | /* If no CRLissuer return is successful iff don't need a match */ |
1445 | if (!dp->CRLissuer) | |
1446 | return ! !(crl_score & CRL_SCORE_ISSUER_NAME); | |
1447 | for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { | |
1448 | GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); | |
1449 | if (gen->type != GEN_DIRNAME) | |
1450 | continue; | |
1451 | if (!X509_NAME_cmp(gen->d.directoryName, nm)) | |
1452 | return 1; | |
1453 | } | |
1454 | return 0; | |
1455 | } | |
d0fff69d | 1456 | |
4b96839f | 1457 | /* Check CRLDP and IDP */ |
bc7535bc | 1458 | |
4b96839f | 1459 | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, |
0f113f3e MC |
1460 | unsigned int *preasons) |
1461 | { | |
1462 | int i; | |
1463 | if (crl->idp_flags & IDP_ONLYATTR) | |
1464 | return 0; | |
1465 | if (x->ex_flags & EXFLAG_CA) { | |
1466 | if (crl->idp_flags & IDP_ONLYUSER) | |
1467 | return 0; | |
1468 | } else { | |
1469 | if (crl->idp_flags & IDP_ONLYCA) | |
1470 | return 0; | |
1471 | } | |
1472 | *preasons = crl->idp_reasons; | |
1473 | for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) { | |
1474 | DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i); | |
1475 | if (crldp_check_crlissuer(dp, crl, crl_score)) { | |
1476 | if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) { | |
1477 | *preasons &= dp->dp_reasons; | |
1478 | return 1; | |
1479 | } | |
1480 | } | |
1481 | } | |
1482 | if ((!crl->idp || !crl->idp->distpoint) | |
1483 | && (crl_score & CRL_SCORE_ISSUER_NAME)) | |
1484 | return 1; | |
1485 | return 0; | |
1486 | } | |
1487 | ||
1488 | /* | |
1489 | * Retrieve CRL corresponding to current certificate. If deltas enabled try | |
1490 | * to find a delta CRL too | |
b545dc67 | 1491 | */ |
0f113f3e | 1492 | |
d43c4497 | 1493 | static int get_crl_delta(X509_STORE_CTX *ctx, |
0f113f3e MC |
1494 | X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x) |
1495 | { | |
1496 | int ok; | |
1497 | X509 *issuer = NULL; | |
1498 | int crl_score = 0; | |
1499 | unsigned int reasons; | |
1500 | X509_CRL *crl = NULL, *dcrl = NULL; | |
1501 | STACK_OF(X509_CRL) *skcrl; | |
8cc86b81 | 1502 | const X509_NAME *nm = X509_get_issuer_name(x); |
70dd3c65 | 1503 | |
0f113f3e MC |
1504 | reasons = ctx->current_reasons; |
1505 | ok = get_crl_sk(ctx, &crl, &dcrl, | |
1506 | &issuer, &crl_score, &reasons, ctx->crls); | |
0f113f3e MC |
1507 | if (ok) |
1508 | goto done; | |
1509 | ||
1510 | /* Lookup CRLs from store */ | |
1511 | ||
1512 | skcrl = ctx->lookup_crls(ctx, nm); | |
1513 | ||
1514 | /* If no CRLs found and a near match from get_crl_sk use that */ | |
1515 | if (!skcrl && crl) | |
1516 | goto done; | |
1517 | ||
1518 | get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl); | |
1519 | ||
1520 | sk_X509_CRL_pop_free(skcrl, X509_CRL_free); | |
1521 | ||
1522 | done: | |
0f113f3e MC |
1523 | /* If we got any kind of CRL use it and return success */ |
1524 | if (crl) { | |
1525 | ctx->current_issuer = issuer; | |
1526 | ctx->current_crl_score = crl_score; | |
1527 | ctx->current_reasons = reasons; | |
1528 | *pcrl = crl; | |
1529 | *pdcrl = dcrl; | |
1530 | return 1; | |
1531 | } | |
0f113f3e MC |
1532 | return 0; |
1533 | } | |
b545dc67 DSH |
1534 | |
1535 | /* Check CRL validity */ | |
1536 | static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) | |
0f113f3e MC |
1537 | { |
1538 | X509 *issuer = NULL; | |
1539 | EVP_PKEY *ikey = NULL; | |
70dd3c65 VD |
1540 | int cnum = ctx->error_depth; |
1541 | int chnum = sk_X509_num(ctx->chain) - 1; | |
1542 | ||
ade08735 | 1543 | /* If we have an alternative CRL issuer cert use that */ |
0f113f3e MC |
1544 | if (ctx->current_issuer) |
1545 | issuer = ctx->current_issuer; | |
0f113f3e MC |
1546 | /* |
1547 | * Else find CRL issuer: if not last certificate then issuer is next | |
1548 | * certificate in chain. | |
1549 | */ | |
1550 | else if (cnum < chnum) | |
1551 | issuer = sk_X509_value(ctx->chain, cnum + 1); | |
1552 | else { | |
1553 | issuer = sk_X509_value(ctx->chain, chnum); | |
ade08735 | 1554 | /* If not self-issued, can't check signature */ |
70dd3c65 VD |
1555 | if (!ctx->check_issued(ctx, issuer, issuer) && |
1556 | !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER)) | |
1557 | return 0; | |
0f113f3e MC |
1558 | } |
1559 | ||
70dd3c65 VD |
1560 | if (issuer == NULL) |
1561 | return 1; | |
0f113f3e | 1562 | |
70dd3c65 VD |
1563 | /* |
1564 | * Skip most tests for deltas because they have already been done | |
1565 | */ | |
1566 | if (!crl->base_crl_number) { | |
1567 | /* Check for cRLSign bit if keyUsage present */ | |
1568 | if ((issuer->ex_flags & EXFLAG_KUSAGE) && | |
1569 | !(issuer->ex_kusage & KU_CRL_SIGN) && | |
1570 | !verify_cb_crl(ctx, X509_V_ERR_KEYUSAGE_NO_CRL_SIGN)) | |
1571 | return 0; | |
0f113f3e | 1572 | |
70dd3c65 VD |
1573 | if (!(ctx->current_crl_score & CRL_SCORE_SCOPE) && |
1574 | !verify_cb_crl(ctx, X509_V_ERR_DIFFERENT_CRL_SCOPE)) | |
1575 | return 0; | |
0f113f3e | 1576 | |
70dd3c65 VD |
1577 | if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH) && |
1578 | check_crl_path(ctx, ctx->current_issuer) <= 0 && | |
1579 | !verify_cb_crl(ctx, X509_V_ERR_CRL_PATH_VALIDATION_ERROR)) | |
1580 | return 0; | |
0f113f3e | 1581 | |
70dd3c65 VD |
1582 | if ((crl->idp_flags & IDP_INVALID) && |
1583 | !verify_cb_crl(ctx, X509_V_ERR_INVALID_EXTENSION)) | |
1584 | return 0; | |
1585 | } | |
0f113f3e | 1586 | |
70dd3c65 VD |
1587 | if (!(ctx->current_crl_score & CRL_SCORE_TIME) && |
1588 | !check_crl_time(ctx, crl, 1)) | |
1589 | return 0; | |
0f113f3e | 1590 | |
70dd3c65 VD |
1591 | /* Attempt to get issuer certificate public key */ |
1592 | ikey = X509_get0_pubkey(issuer); | |
0f113f3e | 1593 | |
70dd3c65 VD |
1594 | if (!ikey && |
1595 | !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY)) | |
1596 | return 0; | |
0f113f3e | 1597 | |
70dd3c65 VD |
1598 | if (ikey) { |
1599 | int rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags); | |
0f113f3e | 1600 | |
70dd3c65 VD |
1601 | if (rv != X509_V_OK && !verify_cb_crl(ctx, rv)) |
1602 | return 0; | |
1603 | /* Verify CRL signature */ | |
1604 | if (X509_CRL_verify(crl, ikey) <= 0 && | |
1605 | !verify_cb_crl(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE)) | |
1606 | return 0; | |
1607 | } | |
1608 | return 1; | |
0f113f3e | 1609 | } |
b545dc67 DSH |
1610 | |
1611 | /* Check certificate against CRL */ | |
1612 | static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) | |
0f113f3e | 1613 | { |
0f113f3e | 1614 | X509_REVOKED *rev; |
70dd3c65 | 1615 | |
0f113f3e MC |
1616 | /* |
1617 | * The rules changed for this... previously if a CRL contained unhandled | |
1618 | * critical extensions it could still be used to indicate a certificate | |
70dd3c65 | 1619 | * was revoked. This has since been changed since critical extensions can |
0f113f3e MC |
1620 | * change the meaning of CRL entries. |
1621 | */ | |
1622 | if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) | |
70dd3c65 VD |
1623 | && (crl->flags & EXFLAG_CRITICAL) && |
1624 | !verify_cb_crl(ctx, X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION)) | |
1625 | return 0; | |
0f113f3e | 1626 | /* |
70dd3c65 VD |
1627 | * Look for serial number of certificate in CRL. If found, make sure |
1628 | * reason is not removeFromCRL. | |
0f113f3e MC |
1629 | */ |
1630 | if (X509_CRL_get0_by_cert(crl, &rev, x)) { | |
1631 | if (rev->reason == CRL_REASON_REMOVE_FROM_CRL) | |
1632 | return 2; | |
70dd3c65 | 1633 | if (!verify_cb_crl(ctx, X509_V_ERR_CERT_REVOKED)) |
0f113f3e MC |
1634 | return 0; |
1635 | } | |
1636 | ||
1637 | return 1; | |
1638 | } | |
b545dc67 | 1639 | |
5d7c222d | 1640 | static int check_policy(X509_STORE_CTX *ctx) |
0f113f3e MC |
1641 | { |
1642 | int ret; | |
3921ded7 | 1643 | |
0f113f3e MC |
1644 | if (ctx->parent) |
1645 | return 1; | |
3921ded7 VD |
1646 | /* |
1647 | * With DANE, the trust anchor might be a bare public key, not a | |
1648 | * certificate! In that case our chain does not have the trust anchor | |
1649 | * certificate as a top-most element. This comports well with RFC5280 | |
1650 | * chain verification, since there too, the trust anchor is not part of the | |
1651 | * chain to be verified. In particular, X509_policy_check() does not look | |
1652 | * at the TA cert, but assumes that it is present as the top-most chain | |
1653 | * element. We therefore temporarily push a NULL cert onto the chain if it | |
1654 | * was verified via a bare public key, and pop it off right after the | |
1655 | * X509_policy_check() call. | |
1656 | */ | |
1657 | if (ctx->bare_ta_signed && !sk_X509_push(ctx->chain, NULL)) { | |
9311d0c4 | 1658 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
f3e235ed | 1659 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
3921ded7 VD |
1660 | return 0; |
1661 | } | |
0f113f3e MC |
1662 | ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain, |
1663 | ctx->param->policies, ctx->param->flags); | |
3921ded7 | 1664 | if (ctx->bare_ta_signed) |
225c9660 | 1665 | (void)sk_X509_pop(ctx->chain); |
3921ded7 | 1666 | |
895c2f84 | 1667 | if (ret == X509_PCY_TREE_INTERNAL) { |
9311d0c4 | 1668 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
f3e235ed | 1669 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
0f113f3e MC |
1670 | return 0; |
1671 | } | |
1672 | /* Invalid or inconsistent extensions */ | |
895c2f84 | 1673 | if (ret == X509_PCY_TREE_INVALID) { |
0f113f3e | 1674 | int i; |
70dd3c65 VD |
1675 | |
1676 | /* Locate certificates with bad extensions and notify callback. */ | |
0f113f3e | 1677 | for (i = 1; i < sk_X509_num(ctx->chain); i++) { |
70dd3c65 VD |
1678 | X509 *x = sk_X509_value(ctx->chain, i); |
1679 | ||
6e5e118c DO |
1680 | CHECK_CB((x->ex_flags & EXFLAG_INVALID_POLICY) != 0, |
1681 | ctx, x, i, X509_V_ERR_INVALID_POLICY_EXTENSION); | |
0f113f3e MC |
1682 | } |
1683 | return 1; | |
1684 | } | |
895c2f84 | 1685 | if (ret == X509_PCY_TREE_FAILURE) { |
0f113f3e MC |
1686 | ctx->current_cert = NULL; |
1687 | ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY; | |
1688 | return ctx->verify_cb(0, ctx); | |
1689 | } | |
895c2f84 | 1690 | if (ret != X509_PCY_TREE_VALID) { |
9311d0c4 | 1691 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
895c2f84 VD |
1692 | return 0; |
1693 | } | |
0f113f3e MC |
1694 | |
1695 | if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) { | |
1696 | ctx->current_cert = NULL; | |
f3e235ed VD |
1697 | /* |
1698 | * Verification errors need to be "sticky", a callback may have allowed | |
1699 | * an SSL handshake to continue despite an error, and we must then | |
1700 | * remain in an error state. Therefore, we MUST NOT clear earlier | |
1701 | * verification errors by setting the error to X509_V_OK. | |
1702 | */ | |
0f113f3e MC |
1703 | if (!ctx->verify_cb(2, ctx)) |
1704 | return 0; | |
1705 | } | |
1706 | ||
1707 | return 1; | |
1708 | } | |
5d7c222d | 1709 | |
70dd3c65 VD |
1710 | /*- |
1711 | * Check certificate validity times. | |
1712 | * If depth >= 0, invoke verification callbacks on error, otherwise just return | |
1713 | * the validation status. | |
1714 | * | |
1715 | * Return 1 on success, 0 otherwise. | |
1716 | */ | |
1717 | int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth) | |
0f113f3e MC |
1718 | { |
1719 | time_t *ptime; | |
1720 | int i; | |
1721 | ||
1722 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) | |
1723 | ptime = &ctx->param->check_time; | |
d35ff2c0 DW |
1724 | else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) |
1725 | return 1; | |
0f113f3e MC |
1726 | else |
1727 | ptime = NULL; | |
1728 | ||
568ce3a5 | 1729 | i = X509_cmp_time(X509_get0_notBefore(x), ptime); |
70dd3c65 VD |
1730 | if (i >= 0 && depth < 0) |
1731 | return 0; | |
6e5e118c DO |
1732 | CHECK_CB(i == 0, ctx, x, depth, X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD); |
1733 | CHECK_CB(i > 0, ctx, x, depth, X509_V_ERR_CERT_NOT_YET_VALID); | |
0f113f3e | 1734 | |
568ce3a5 | 1735 | i = X509_cmp_time(X509_get0_notAfter(x), ptime); |
70dd3c65 VD |
1736 | if (i <= 0 && depth < 0) |
1737 | return 0; | |
6e5e118c DO |
1738 | CHECK_CB(i == 0, ctx, x, depth, X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD); |
1739 | CHECK_CB(i < 0, ctx, x, depth, X509_V_ERR_CERT_HAS_EXPIRED); | |
0f113f3e MC |
1740 | return 1; |
1741 | } | |
e1a27eb3 | 1742 | |
0b670a21 | 1743 | /* verify the issuer signatures and cert times of ctx->chain */ |
6b691a5c | 1744 | static int internal_verify(X509_STORE_CTX *ctx) |
0f113f3e | 1745 | { |
70dd3c65 VD |
1746 | int n = sk_X509_num(ctx->chain) - 1; |
1747 | X509 *xi = sk_X509_value(ctx->chain, n); | |
1748 | X509 *xs; | |
0f113f3e | 1749 | |
170b7358 VD |
1750 | /* |
1751 | * With DANE-verified bare public key TA signatures, it remains only to | |
1752 | * check the timestamps of the top certificate. We report the issuer as | |
1753 | * NULL, since all we have is a bare key. | |
1754 | */ | |
1755 | if (ctx->bare_ta_signed) { | |
1756 | xs = xi; | |
1757 | xi = NULL; | |
0b670a21 | 1758 | goto check_cert_time; |
170b7358 VD |
1759 | } |
1760 | ||
0b670a21 DDO |
1761 | if (ctx->check_issued(ctx, xi, xi)) |
1762 | xs = xi; /* the typical case: last cert in the chain is self-issued */ | |
0f113f3e MC |
1763 | else { |
1764 | if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { | |
1765 | xs = xi; | |
0b670a21 | 1766 | goto check_cert_time; |
0f113f3e | 1767 | } |
02a25671 | 1768 | if (n <= 0) { |
6e5e118c | 1769 | CHECK_CB(1, ctx, xi, 0, X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE); |
02a25671 AK |
1770 | |
1771 | xs = xi; | |
1772 | goto check_cert_time; | |
1773 | } | |
1774 | ||
70dd3c65 VD |
1775 | n--; |
1776 | ctx->error_depth = n; | |
1777 | xs = sk_X509_value(ctx->chain, n); | |
0f113f3e MC |
1778 | } |
1779 | ||
d9b8b89b VD |
1780 | /* |
1781 | * Do not clear ctx->error=0, it must be "sticky", only the user's callback | |
1782 | * is allowed to reset errors (at its own peril). | |
1783 | */ | |
0f113f3e | 1784 | while (n >= 0) { |
0f113f3e | 1785 | /* |
0b670a21 DDO |
1786 | * For each iteration of this loop: |
1787 | * n is the subject depth | |
1788 | * xs is the subject cert, for which the signature is to be checked | |
1789 | * xi is the supposed issuer cert containing the public key to use | |
1790 | * Initially xs == xi if the last cert in the chain is self-issued. | |
1791 | * | |
ade08735 DDO |
1792 | * Skip signature check for self-signed certificates unless explicitly |
1793 | * asked for because it does not add any security and just wastes time. | |
0f113f3e | 1794 | */ |
0b670a21 DDO |
1795 | if (xs != xi || ((ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE) |
1796 | && (xi->ex_flags & EXFLAG_SS) != 0)) { | |
02369787 | 1797 | EVP_PKEY *pkey; |
0b670a21 DDO |
1798 | /* |
1799 | * If the issuer's public key is not available or its key usage | |
1800 | * does not support issuing the subject cert, report the issuer | |
1801 | * cert and its depth (rather than n, the depth of the subject). | |
1802 | */ | |
1803 | int issuer_depth = n + (xs == xi ? 0 : 1); | |
1804 | /* | |
1805 | * According to https://tools.ietf.org/html/rfc5280#section-6.1.4 | |
1806 | * step (n) we must check any given key usage extension in a CA cert | |
1807 | * when preparing the verification of a certificate issued by it. | |
1808 | * According to https://tools.ietf.org/html/rfc5280#section-4.2.1.3 | |
e99505b4 DDO |
1809 | * we must not verify a certificate signature if the key usage of |
1810 | * the CA certificate that issued the certificate prohibits signing. | |
0b670a21 DDO |
1811 | * In case the 'issuing' certificate is the last in the chain and is |
1812 | * not a CA certificate but a 'self-issued' end-entity cert (i.e., | |
1813 | * xs == xi && !(xi->ex_flags & EXFLAG_CA)) RFC 5280 does not apply | |
1814 | * (see https://tools.ietf.org/html/rfc6818#section-2) and thus | |
1815 | * we are free to ignore any key usage restrictions on such certs. | |
1816 | */ | |
1817 | int ret = xs == xi && (xi->ex_flags & EXFLAG_CA) == 0 | |
1818 | ? X509_V_OK : x509_signing_allowed(xi, xs); | |
02369787 | 1819 | |
6e5e118c | 1820 | CHECK_CB(ret != X509_V_OK, ctx, xi, issuer_depth, ret); |
c01ff880 | 1821 | if ((pkey = X509_get0_pubkey(xi)) == NULL) { |
6e5e118c DO |
1822 | CHECK_CB(1, ctx, xi, issuer_depth, |
1823 | X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY); | |
1824 | } else { | |
1825 | CHECK_CB(X509_verify(xs, pkey) <= 0, | |
1826 | ctx, xs, n, X509_V_ERR_CERT_SIGNATURE_FAILURE); | |
0f113f3e | 1827 | } |
0f113f3e MC |
1828 | } |
1829 | ||
3bed88a3 | 1830 | check_cert_time: /* in addition to RFC 5280, do also for trusted (root) cert */ |
70dd3c65 VD |
1831 | /* Calls verify callback as needed */ |
1832 | if (!x509_check_cert_time(ctx, xs, n)) | |
1833 | return 0; | |
0f113f3e | 1834 | |
70dd3c65 VD |
1835 | /* |
1836 | * Signal success at this depth. However, the previous error (if any) | |
1837 | * is retained. | |
1838 | */ | |
0f113f3e MC |
1839 | ctx->current_issuer = xi; |
1840 | ctx->current_cert = xs; | |
70dd3c65 VD |
1841 | ctx->error_depth = n; |
1842 | if (!ctx->verify_cb(1, ctx)) | |
1843 | return 0; | |
0f113f3e | 1844 | |
70dd3c65 | 1845 | if (--n >= 0) { |
0f113f3e MC |
1846 | xi = xs; |
1847 | xs = sk_X509_value(ctx->chain, n); | |
1848 | } | |
1849 | } | |
70dd3c65 | 1850 | return 1; |
0f113f3e | 1851 | } |
d02b48c6 | 1852 | |
91b73acb | 1853 | int X509_cmp_current_time(const ASN1_TIME *ctm) |
bbb72003 | 1854 | { |
0f113f3e | 1855 | return X509_cmp_time(ctm, NULL); |
bbb72003 DSH |
1856 | } |
1857 | ||
91b73acb | 1858 | int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time) |
0f113f3e | 1859 | { |
80770da3 EK |
1860 | static const size_t utctime_length = sizeof("YYMMDDHHMMSSZ") - 1; |
1861 | static const size_t generalizedtime_length = sizeof("YYYYMMDDHHMMSSZ") - 1; | |
1862 | ASN1_TIME *asn1_cmp_time = NULL; | |
1863 | int i, day, sec, ret = 0; | |
48102247 | 1864 | #ifdef CHARSET_EBCDIC |
1865 | const char upper_z = 0x5A; | |
1866 | #else | |
1867 | const char upper_z = 'Z'; | |
1868 | #endif | |
f48b83b4 | 1869 | /* |
80770da3 EK |
1870 | * Note that ASN.1 allows much more slack in the time format than RFC5280. |
1871 | * In RFC5280, the representation is fixed: | |
f48b83b4 EK |
1872 | * UTCTime: YYMMDDHHMMSSZ |
1873 | * GeneralizedTime: YYYYMMDDHHMMSSZ | |
80770da3 EK |
1874 | * |
1875 | * We do NOT currently enforce the following RFC 5280 requirement: | |
1876 | * "CAs conforming to this profile MUST always encode certificate | |
1877 | * validity dates through the year 2049 as UTCTime; certificate validity | |
1878 | * dates in 2050 or later MUST be encoded as GeneralizedTime." | |
f48b83b4 | 1879 | */ |
80770da3 EK |
1880 | switch (ctm->type) { |
1881 | case V_ASN1_UTCTIME: | |
1882 | if (ctm->length != (int)(utctime_length)) | |
0f113f3e | 1883 | return 0; |
80770da3 EK |
1884 | break; |
1885 | case V_ASN1_GENERALIZEDTIME: | |
1886 | if (ctm->length != (int)(generalizedtime_length)) | |
0f113f3e | 1887 | return 0; |
80770da3 EK |
1888 | break; |
1889 | default: | |
1890 | return 0; | |
0f113f3e MC |
1891 | } |
1892 | ||
80770da3 EK |
1893 | /** |
1894 | * Verify the format: the ASN.1 functions we use below allow a more | |
1895 | * flexible format than what's mandated by RFC 5280. | |
1896 | * Digit and date ranges will be verified in the conversion methods. | |
1897 | */ | |
1898 | for (i = 0; i < ctm->length - 1; i++) { | |
48102247 | 1899 | if (!ascii_isdigit(ctm->data[i])) |
f48b83b4 | 1900 | return 0; |
0f113f3e | 1901 | } |
48102247 | 1902 | if (ctm->data[ctm->length - 1] != upper_z) |
f48b83b4 | 1903 | return 0; |
0f113f3e | 1904 | |
80770da3 EK |
1905 | /* |
1906 | * There is ASN1_UTCTIME_cmp_time_t but no | |
1907 | * ASN1_GENERALIZEDTIME_cmp_time_t or ASN1_TIME_cmp_time_t, | |
1908 | * so we go through ASN.1 | |
1909 | */ | |
1910 | asn1_cmp_time = X509_time_adj(NULL, 0, cmp_time); | |
1911 | if (asn1_cmp_time == NULL) | |
1912 | goto err; | |
1913 | if (!ASN1_TIME_diff(&day, &sec, ctm, asn1_cmp_time)) | |
1914 | goto err; | |
0f113f3e | 1915 | |
80770da3 EK |
1916 | /* |
1917 | * X509_cmp_time comparison is <=. | |
1918 | * The return value 0 is reserved for errors. | |
1919 | */ | |
1920 | ret = (day >= 0 && sec >= 0) ? -1 : 1; | |
1921 | ||
1922 | err: | |
1923 | ASN1_TIME_free(asn1_cmp_time); | |
1924 | return ret; | |
0f113f3e | 1925 | } |
d02b48c6 | 1926 | |
25d7cd1d DDO |
1927 | /* |
1928 | * Return 0 if time should not be checked or reference time is in range, | |
1929 | * or else 1 if it is past the end, or -1 if it is before the start | |
1930 | */ | |
1931 | int X509_cmp_timeframe(const X509_VERIFY_PARAM *vpm, | |
1932 | const ASN1_TIME *start, const ASN1_TIME *end) | |
1933 | { | |
1934 | time_t ref_time; | |
1935 | time_t *time = NULL; | |
1936 | unsigned long flags = vpm == NULL ? 0 : X509_VERIFY_PARAM_get_flags(vpm); | |
1937 | ||
1938 | if ((flags & X509_V_FLAG_USE_CHECK_TIME) != 0) { | |
1939 | ref_time = X509_VERIFY_PARAM_get_time(vpm); | |
1940 | time = &ref_time; | |
1941 | } else if ((flags & X509_V_FLAG_NO_CHECK_TIME) != 0) { | |
1942 | return 0; /* this means ok */ | |
1943 | } /* else reference time is the current time */ | |
1944 | ||
1945 | if (end != NULL && X509_cmp_time(end, time) < 0) | |
1946 | return 1; | |
1947 | if (start != NULL && X509_cmp_time(start, time) > 0) | |
1948 | return -1; | |
1949 | return 0; | |
1950 | } | |
1951 | ||
284ef5f3 | 1952 | ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj) |
bbb72003 | 1953 | { |
0f113f3e | 1954 | return X509_time_adj(s, adj, NULL); |
bbb72003 DSH |
1955 | } |
1956 | ||
87d3a0cd | 1957 | ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm) |
0f113f3e MC |
1958 | { |
1959 | return X509_time_adj_ex(s, 0, offset_sec, in_tm); | |
1960 | } | |
87d3a0cd DSH |
1961 | |
1962 | ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, | |
0f113f3e MC |
1963 | int offset_day, long offset_sec, time_t *in_tm) |
1964 | { | |
1965 | time_t t; | |
1966 | ||
1967 | if (in_tm) | |
1968 | t = *in_tm; | |
1969 | else | |
1970 | time(&t); | |
1971 | ||
1972 | if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) { | |
1973 | if (s->type == V_ASN1_UTCTIME) | |
1974 | return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec); | |
1975 | if (s->type == V_ASN1_GENERALIZEDTIME) | |
1976 | return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec); | |
1977 | } | |
1978 | return ASN1_TIME_adj(s, t, offset_day, offset_sec); | |
1979 | } | |
d02b48c6 | 1980 | |
7e258a56 | 1981 | int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain) |
0f113f3e MC |
1982 | { |
1983 | EVP_PKEY *ktmp = NULL, *ktmp2; | |
1984 | int i, j; | |
1985 | ||
1986 | if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) | |
1987 | return 1; | |
1988 | ||
1989 | for (i = 0; i < sk_X509_num(chain); i++) { | |
c01ff880 | 1990 | ktmp = X509_get0_pubkey(sk_X509_value(chain, i)); |
0f113f3e | 1991 | if (ktmp == NULL) { |
9311d0c4 | 1992 | ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); |
0f113f3e MC |
1993 | return 0; |
1994 | } | |
1995 | if (!EVP_PKEY_missing_parameters(ktmp)) | |
1996 | break; | |
0f113f3e MC |
1997 | } |
1998 | if (ktmp == NULL) { | |
9311d0c4 | 1999 | ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN); |
0f113f3e MC |
2000 | return 0; |
2001 | } | |
2002 | ||
2003 | /* first, populate the other certs */ | |
2004 | for (j = i - 1; j >= 0; j--) { | |
c01ff880 | 2005 | ktmp2 = X509_get0_pubkey(sk_X509_value(chain, j)); |
0f113f3e | 2006 | EVP_PKEY_copy_parameters(ktmp2, ktmp); |
0f113f3e MC |
2007 | } |
2008 | ||
2009 | if (pkey != NULL) | |
2010 | EVP_PKEY_copy_parameters(pkey, ktmp); | |
0f113f3e MC |
2011 | return 1; |
2012 | } | |
d02b48c6 | 2013 | |
e99505b4 | 2014 | /* Make a delta CRL as the difference between two full CRLs */ |
2e8cb108 DSH |
2015 | |
2016 | X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer, | |
0f113f3e MC |
2017 | EVP_PKEY *skey, const EVP_MD *md, unsigned int flags) |
2018 | { | |
2019 | X509_CRL *crl = NULL; | |
2020 | int i; | |
2021 | STACK_OF(X509_REVOKED) *revs = NULL; | |
2022 | /* CRLs can't be delta already */ | |
2023 | if (base->base_crl_number || newer->base_crl_number) { | |
9311d0c4 | 2024 | ERR_raise(ERR_LIB_X509, X509_R_CRL_ALREADY_DELTA); |
0f113f3e MC |
2025 | return NULL; |
2026 | } | |
2027 | /* Base and new CRL must have a CRL number */ | |
2028 | if (!base->crl_number || !newer->crl_number) { | |
9311d0c4 | 2029 | ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_NUMBER); |
0f113f3e MC |
2030 | return NULL; |
2031 | } | |
2032 | /* Issuer names must match */ | |
2033 | if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) { | |
9311d0c4 | 2034 | ERR_raise(ERR_LIB_X509, X509_R_ISSUER_MISMATCH); |
0f113f3e MC |
2035 | return NULL; |
2036 | } | |
2037 | /* AKID and IDP must match */ | |
2038 | if (!crl_extension_match(base, newer, NID_authority_key_identifier)) { | |
9311d0c4 | 2039 | ERR_raise(ERR_LIB_X509, X509_R_AKID_MISMATCH); |
0f113f3e MC |
2040 | return NULL; |
2041 | } | |
2042 | if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) { | |
9311d0c4 | 2043 | ERR_raise(ERR_LIB_X509, X509_R_IDP_MISMATCH); |
0f113f3e MC |
2044 | return NULL; |
2045 | } | |
2046 | /* Newer CRL number must exceed full CRL number */ | |
2047 | if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) { | |
9311d0c4 | 2048 | ERR_raise(ERR_LIB_X509, X509_R_NEWER_CRL_NOT_NEWER); |
0f113f3e MC |
2049 | return NULL; |
2050 | } | |
2051 | /* CRLs must verify */ | |
2052 | if (skey && (X509_CRL_verify(base, skey) <= 0 || | |
2053 | X509_CRL_verify(newer, skey) <= 0)) { | |
9311d0c4 | 2054 | ERR_raise(ERR_LIB_X509, X509_R_CRL_VERIFY_FAILURE); |
0f113f3e MC |
2055 | return NULL; |
2056 | } | |
2057 | /* Create new CRL */ | |
2058 | crl = X509_CRL_new(); | |
90945fa3 | 2059 | if (crl == NULL || !X509_CRL_set_version(crl, 1)) |
0f113f3e MC |
2060 | goto memerr; |
2061 | /* Set issuer name */ | |
2062 | if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer))) | |
2063 | goto memerr; | |
2064 | ||
568ce3a5 | 2065 | if (!X509_CRL_set1_lastUpdate(crl, X509_CRL_get0_lastUpdate(newer))) |
0f113f3e | 2066 | goto memerr; |
568ce3a5 | 2067 | if (!X509_CRL_set1_nextUpdate(crl, X509_CRL_get0_nextUpdate(newer))) |
0f113f3e MC |
2068 | goto memerr; |
2069 | ||
2070 | /* Set base CRL number: must be critical */ | |
2071 | ||
2072 | if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0)) | |
2073 | goto memerr; | |
2074 | ||
2075 | /* | |
2076 | * Copy extensions across from newest CRL to delta: this will set CRL | |
2077 | * number to correct value too. | |
2078 | */ | |
2079 | ||
2080 | for (i = 0; i < X509_CRL_get_ext_count(newer); i++) { | |
2081 | X509_EXTENSION *ext; | |
2082 | ext = X509_CRL_get_ext(newer, i); | |
2083 | if (!X509_CRL_add_ext(crl, ext, -1)) | |
2084 | goto memerr; | |
2085 | } | |
2086 | ||
2087 | /* Go through revoked entries, copying as needed */ | |
2088 | ||
2089 | revs = X509_CRL_get_REVOKED(newer); | |
2090 | ||
2091 | for (i = 0; i < sk_X509_REVOKED_num(revs); i++) { | |
2092 | X509_REVOKED *rvn, *rvtmp; | |
2093 | rvn = sk_X509_REVOKED_value(revs, i); | |
2094 | /* | |
2095 | * Add only if not also in base. TODO: need something cleverer here | |
2096 | * for some more complex CRLs covering multiple CAs. | |
2097 | */ | |
34a42e14 | 2098 | if (!X509_CRL_get0_by_serial(base, &rvtmp, &rvn->serialNumber)) { |
0f113f3e MC |
2099 | rvtmp = X509_REVOKED_dup(rvn); |
2100 | if (!rvtmp) | |
2101 | goto memerr; | |
2102 | if (!X509_CRL_add0_revoked(crl, rvtmp)) { | |
2103 | X509_REVOKED_free(rvtmp); | |
2104 | goto memerr; | |
2105 | } | |
2106 | } | |
2107 | } | |
2108 | /* TODO: optionally prune deleted entries */ | |
2109 | ||
2110 | if (skey && md && !X509_CRL_sign(crl, skey, md)) | |
2111 | goto memerr; | |
2112 | ||
2113 | return crl; | |
2114 | ||
2115 | memerr: | |
9311d0c4 | 2116 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
222561fe | 2117 | X509_CRL_free(crl); |
0f113f3e MC |
2118 | return NULL; |
2119 | } | |
2120 | ||
6b691a5c | 2121 | int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data) |
0f113f3e MC |
2122 | { |
2123 | return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); | |
2124 | } | |
58964a49 | 2125 | |
8cc86b81 | 2126 | void *X509_STORE_CTX_get_ex_data(const X509_STORE_CTX *ctx, int idx) |
0f113f3e MC |
2127 | { |
2128 | return CRYPTO_get_ex_data(&ctx->ex_data, idx); | |
2129 | } | |
58964a49 | 2130 | |
8cc86b81 | 2131 | int X509_STORE_CTX_get_error(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2132 | { |
2133 | return ctx->error; | |
2134 | } | |
58964a49 | 2135 | |
6b691a5c | 2136 | void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err) |
0f113f3e MC |
2137 | { |
2138 | ctx->error = err; | |
2139 | } | |
58964a49 | 2140 | |
8cc86b81 | 2141 | int X509_STORE_CTX_get_error_depth(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2142 | { |
2143 | return ctx->error_depth; | |
2144 | } | |
58964a49 | 2145 | |
51227177 VD |
2146 | void X509_STORE_CTX_set_error_depth(X509_STORE_CTX *ctx, int depth) |
2147 | { | |
2148 | ctx->error_depth = depth; | |
2149 | } | |
2150 | ||
8cc86b81 | 2151 | X509 *X509_STORE_CTX_get_current_cert(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2152 | { |
2153 | return ctx->current_cert; | |
2154 | } | |
58964a49 | 2155 | |
c9654873 VD |
2156 | void X509_STORE_CTX_set_current_cert(X509_STORE_CTX *ctx, X509 *x) |
2157 | { | |
2158 | ctx->current_cert = x; | |
2159 | } | |
2160 | ||
8cc86b81 | 2161 | STACK_OF(X509) *X509_STORE_CTX_get0_chain(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2162 | { |
2163 | return ctx->chain; | |
2164 | } | |
58964a49 | 2165 | |
8cc86b81 | 2166 | STACK_OF(X509) *X509_STORE_CTX_get1_chain(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2167 | { |
2168 | if (!ctx->chain) | |
2169 | return NULL; | |
2170 | return X509_chain_up_ref(ctx->chain); | |
2171 | } | |
25f923dd | 2172 | |
8cc86b81 | 2173 | X509 *X509_STORE_CTX_get0_current_issuer(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2174 | { |
2175 | return ctx->current_issuer; | |
2176 | } | |
2008e714 | 2177 | |
8cc86b81 | 2178 | X509_CRL *X509_STORE_CTX_get0_current_crl(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2179 | { |
2180 | return ctx->current_crl; | |
2181 | } | |
2008e714 | 2182 | |
8cc86b81 | 2183 | X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2184 | { |
2185 | return ctx->parent; | |
2186 | } | |
2008e714 | 2187 | |
6b691a5c | 2188 | void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x) |
0f113f3e MC |
2189 | { |
2190 | ctx->cert = x; | |
2191 | } | |
58964a49 | 2192 | |
e1a27eb3 | 2193 | void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk) |
0f113f3e MC |
2194 | { |
2195 | ctx->crls = sk; | |
2196 | } | |
e1a27eb3 | 2197 | |
13938ace | 2198 | int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) |
0f113f3e | 2199 | { |
0daccd4d VD |
2200 | /* |
2201 | * XXX: Why isn't this function always used to set the associated trust? | |
2202 | * Should there even be a VPM->trust field at all? Or should the trust | |
2203 | * always be inferred from the purpose by X509_STORE_CTX_init(). | |
2204 | */ | |
0f113f3e MC |
2205 | return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0); |
2206 | } | |
11262391 | 2207 | |
bb7cd4e3 | 2208 | int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust) |
0f113f3e | 2209 | { |
0daccd4d VD |
2210 | /* |
2211 | * XXX: See above, this function would only be needed when the default | |
2212 | * trust for the purpose needs an override in a corner case. | |
2213 | */ | |
0f113f3e MC |
2214 | return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust); |
2215 | } | |
2216 | ||
2217 | /* | |
2218 | * This function is used to set the X509_STORE_CTX purpose and trust values. | |
2219 | * This is intended to be used when another structure has its own trust and | |
2220 | * purpose values which (if set) will be inherited by the ctx. If they aren't | |
2221 | * set then we will usually have a default purpose in mind which should then | |
2222 | * be used to set the trust value. An example of this is SSL use: an SSL | |
2223 | * structure will have its own purpose and trust settings which the | |
2224 | * application can set: if they aren't set then we use the default of SSL | |
2225 | * client/server. | |
13938ace DSH |
2226 | */ |
2227 | ||
2228 | int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose, | |
0f113f3e MC |
2229 | int purpose, int trust) |
2230 | { | |
2231 | int idx; | |
2232 | /* If purpose not set use default */ | |
12a765a5 | 2233 | if (purpose == 0) |
0f113f3e MC |
2234 | purpose = def_purpose; |
2235 | /* If we have a purpose then check it is valid */ | |
12a765a5 | 2236 | if (purpose != 0) { |
0f113f3e MC |
2237 | X509_PURPOSE *ptmp; |
2238 | idx = X509_PURPOSE_get_by_id(purpose); | |
2239 | if (idx == -1) { | |
9311d0c4 | 2240 | ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID); |
0f113f3e MC |
2241 | return 0; |
2242 | } | |
2243 | ptmp = X509_PURPOSE_get0(idx); | |
2244 | if (ptmp->trust == X509_TRUST_DEFAULT) { | |
2245 | idx = X509_PURPOSE_get_by_id(def_purpose); | |
0daccd4d VD |
2246 | /* |
2247 | * XXX: In the two callers above def_purpose is always 0, which is | |
2248 | * not a known value, so idx will always be -1. How is the | |
2249 | * X509_TRUST_DEFAULT case actually supposed to be handled? | |
2250 | */ | |
0f113f3e | 2251 | if (idx == -1) { |
9311d0c4 | 2252 | ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID); |
0f113f3e MC |
2253 | return 0; |
2254 | } | |
2255 | ptmp = X509_PURPOSE_get0(idx); | |
2256 | } | |
2257 | /* If trust not set then get from purpose default */ | |
2258 | if (!trust) | |
2259 | trust = ptmp->trust; | |
2260 | } | |
2261 | if (trust) { | |
2262 | idx = X509_TRUST_get_by_id(trust); | |
2263 | if (idx == -1) { | |
9311d0c4 | 2264 | ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_TRUST_ID); |
0f113f3e MC |
2265 | return 0; |
2266 | } | |
2267 | } | |
2268 | ||
2269 | if (purpose && !ctx->param->purpose) | |
2270 | ctx->param->purpose = purpose; | |
2271 | if (trust && !ctx->param->trust) | |
2272 | ctx->param->trust = trust; | |
2273 | return 1; | |
51630a37 DSH |
2274 | } |
2275 | ||
b4250010 | 2276 | X509_STORE_CTX *X509_STORE_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq) |
2f043896 | 2277 | { |
b51bce94 | 2278 | X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
b196e7d9 | 2279 | |
90945fa3 | 2280 | if (ctx == NULL) { |
9311d0c4 | 2281 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2282 | return NULL; |
2283 | } | |
1143c27b MC |
2284 | |
2285 | ctx->libctx = libctx; | |
2286 | if (propq != NULL) { | |
2287 | ctx->propq = OPENSSL_strdup(propq); | |
2288 | if (ctx->propq == NULL) { | |
2289 | OPENSSL_free(ctx); | |
9311d0c4 | 2290 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
1143c27b MC |
2291 | return NULL; |
2292 | } | |
2293 | } | |
2294 | ||
0f113f3e | 2295 | return ctx; |
2f043896 DSH |
2296 | } |
2297 | ||
1143c27b MC |
2298 | X509_STORE_CTX *X509_STORE_CTX_new(void) |
2299 | { | |
d8652be0 | 2300 | return X509_STORE_CTX_new_ex(NULL, NULL); |
1143c27b MC |
2301 | } |
2302 | ||
2303 | ||
2f043896 DSH |
2304 | void X509_STORE_CTX_free(X509_STORE_CTX *ctx) |
2305 | { | |
c001ce33 | 2306 | if (ctx == NULL) |
222561fe | 2307 | return; |
c001ce33 | 2308 | |
0f113f3e | 2309 | X509_STORE_CTX_cleanup(ctx); |
1143c27b MC |
2310 | |
2311 | /* libctx and propq survive X509_STORE_CTX_cleanup() */ | |
2312 | OPENSSL_free(ctx->propq); | |
2313 | ||
0f113f3e | 2314 | OPENSSL_free(ctx); |
2f043896 DSH |
2315 | } |
2316 | ||
79aa04ef | 2317 | int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, |
0f113f3e MC |
2318 | STACK_OF(X509) *chain) |
2319 | { | |
2320 | int ret = 1; | |
ecdaa1ae | 2321 | |
faa9dcd4 | 2322 | ctx->store = store; |
0f113f3e MC |
2323 | ctx->cert = x509; |
2324 | ctx->untrusted = chain; | |
2325 | ctx->crls = NULL; | |
d9b8b89b | 2326 | ctx->num_untrusted = 0; |
0f113f3e MC |
2327 | ctx->other_ctx = NULL; |
2328 | ctx->valid = 0; | |
2329 | ctx->chain = NULL; | |
2330 | ctx->error = 0; | |
2331 | ctx->explicit_policy = 0; | |
2332 | ctx->error_depth = 0; | |
2333 | ctx->current_cert = NULL; | |
2334 | ctx->current_issuer = NULL; | |
2335 | ctx->current_crl = NULL; | |
2336 | ctx->current_crl_score = 0; | |
2337 | ctx->current_reasons = 0; | |
2338 | ctx->tree = NULL; | |
2339 | ctx->parent = NULL; | |
919ba009 | 2340 | ctx->dane = NULL; |
170b7358 | 2341 | ctx->bare_ta_signed = 0; |
e29c73c9 VD |
2342 | /* Zero ex_data to make sure we're cleanup-safe */ |
2343 | memset(&ctx->ex_data, 0, sizeof(ctx->ex_data)); | |
0f113f3e | 2344 | |
7b7eb472 VD |
2345 | /* store->cleanup is always 0 in OpenSSL, if set must be idempotent */ |
2346 | if (store) | |
0f113f3e | 2347 | ctx->cleanup = store->cleanup; |
7b7eb472 | 2348 | else |
0f113f3e MC |
2349 | ctx->cleanup = 0; |
2350 | ||
0f113f3e MC |
2351 | if (store && store->check_issued) |
2352 | ctx->check_issued = store->check_issued; | |
2353 | else | |
2354 | ctx->check_issued = check_issued; | |
2355 | ||
2356 | if (store && store->get_issuer) | |
2357 | ctx->get_issuer = store->get_issuer; | |
2358 | else | |
2359 | ctx->get_issuer = X509_STORE_CTX_get1_issuer; | |
2360 | ||
2361 | if (store && store->verify_cb) | |
2362 | ctx->verify_cb = store->verify_cb; | |
2363 | else | |
2364 | ctx->verify_cb = null_callback; | |
2365 | ||
2366 | if (store && store->verify) | |
2367 | ctx->verify = store->verify; | |
2368 | else | |
2369 | ctx->verify = internal_verify; | |
2370 | ||
2371 | if (store && store->check_revocation) | |
2372 | ctx->check_revocation = store->check_revocation; | |
2373 | else | |
2374 | ctx->check_revocation = check_revocation; | |
2375 | ||
2376 | if (store && store->get_crl) | |
2377 | ctx->get_crl = store->get_crl; | |
311f2785 VD |
2378 | else |
2379 | ctx->get_crl = NULL; | |
0f113f3e MC |
2380 | |
2381 | if (store && store->check_crl) | |
2382 | ctx->check_crl = store->check_crl; | |
2383 | else | |
2384 | ctx->check_crl = check_crl; | |
2385 | ||
2386 | if (store && store->cert_crl) | |
2387 | ctx->cert_crl = store->cert_crl; | |
2388 | else | |
2389 | ctx->cert_crl = cert_crl; | |
2390 | ||
0a5fe2eb RL |
2391 | if (store && store->check_policy) |
2392 | ctx->check_policy = store->check_policy; | |
2393 | else | |
2394 | ctx->check_policy = check_policy; | |
2395 | ||
0f113f3e MC |
2396 | if (store && store->lookup_certs) |
2397 | ctx->lookup_certs = store->lookup_certs; | |
2398 | else | |
6ddbb4cd | 2399 | ctx->lookup_certs = X509_STORE_CTX_get1_certs; |
0f113f3e MC |
2400 | |
2401 | if (store && store->lookup_crls) | |
2402 | ctx->lookup_crls = store->lookup_crls; | |
2403 | else | |
6ddbb4cd | 2404 | ctx->lookup_crls = X509_STORE_CTX_get1_crls; |
0f113f3e | 2405 | |
ecdaa1ae | 2406 | ctx->param = X509_VERIFY_PARAM_new(); |
2407 | if (ctx->param == NULL) { | |
9311d0c4 | 2408 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
ecdaa1ae | 2409 | goto err; |
2410 | } | |
2411 | ||
2412 | /* | |
2413 | * Inherit callbacks and flags from X509_STORE if not set use defaults. | |
2414 | */ | |
2415 | if (store) | |
2416 | ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param); | |
2417 | else | |
2418 | ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE; | |
2419 | ||
2420 | if (ret) | |
2421 | ret = X509_VERIFY_PARAM_inherit(ctx->param, | |
2422 | X509_VERIFY_PARAM_lookup("default")); | |
2423 | ||
2424 | if (ret == 0) { | |
9311d0c4 | 2425 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
ecdaa1ae | 2426 | goto err; |
2427 | } | |
2428 | ||
0daccd4d VD |
2429 | /* |
2430 | * XXX: For now, continue to inherit trust from VPM, but infer from the | |
2431 | * purpose if this still yields the default value. | |
2432 | */ | |
2433 | if (ctx->param->trust == X509_TRUST_DEFAULT) { | |
2434 | int idx = X509_PURPOSE_get_by_id(ctx->param->purpose); | |
2435 | X509_PURPOSE *xp = X509_PURPOSE_get0(idx); | |
2436 | ||
2437 | if (xp != NULL) | |
2438 | ctx->param->trust = X509_PURPOSE_get_trust(xp); | |
2439 | } | |
2440 | ||
e29c73c9 VD |
2441 | if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, |
2442 | &ctx->ex_data)) | |
2443 | return 1; | |
9311d0c4 | 2444 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
ecdaa1ae | 2445 | |
d9b8b89b | 2446 | err: |
e29c73c9 VD |
2447 | /* |
2448 | * On error clean up allocated storage, if the store context was not | |
2449 | * allocated with X509_STORE_CTX_new() this is our last chance to do so. | |
2450 | */ | |
ecdaa1ae | 2451 | X509_STORE_CTX_cleanup(ctx); |
2452 | return 0; | |
0f113f3e MC |
2453 | } |
2454 | ||
2455 | /* | |
2456 | * Set alternative lookup method: just a STACK of trusted certificates. This | |
2457 | * avoids X509_STORE nastiness where it isn't needed. | |
2f043896 | 2458 | */ |
f0e0fd51 | 2459 | void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) |
2f043896 | 2460 | { |
0f113f3e MC |
2461 | ctx->other_ctx = sk; |
2462 | ctx->get_issuer = get_issuer_sk; | |
c864e761 | 2463 | ctx->lookup_certs = lookup_certs_sk; |
2f043896 DSH |
2464 | } |
2465 | ||
2466 | void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) | |
0f113f3e | 2467 | { |
e29c73c9 VD |
2468 | /* |
2469 | * We need to be idempotent because, unfortunately, free() also calls | |
2470 | * cleanup(), so the natural call sequence new(), init(), cleanup(), free() | |
2471 | * calls cleanup() for the same object twice! Thus we must zero the | |
2472 | * pointers below after they're freed! | |
2473 | */ | |
2474 | /* Seems to always be 0 in OpenSSL, do this at most once. */ | |
2475 | if (ctx->cleanup != NULL) { | |
0f113f3e | 2476 | ctx->cleanup(ctx); |
e29c73c9 VD |
2477 | ctx->cleanup = NULL; |
2478 | } | |
0f113f3e MC |
2479 | if (ctx->param != NULL) { |
2480 | if (ctx->parent == NULL) | |
2481 | X509_VERIFY_PARAM_free(ctx->param); | |
2482 | ctx->param = NULL; | |
2483 | } | |
222561fe RS |
2484 | X509_policy_tree_free(ctx->tree); |
2485 | ctx->tree = NULL; | |
2486 | sk_X509_pop_free(ctx->chain, X509_free); | |
2487 | ctx->chain = NULL; | |
0f113f3e | 2488 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data)); |
16f8d4eb | 2489 | memset(&ctx->ex_data, 0, sizeof(ctx->ex_data)); |
0f113f3e | 2490 | } |
13938ace | 2491 | |
5d7c222d | 2492 | void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth) |
0f113f3e MC |
2493 | { |
2494 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); | |
2495 | } | |
bbb72003 | 2496 | |
5d7c222d | 2497 | void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags) |
0f113f3e MC |
2498 | { |
2499 | X509_VERIFY_PARAM_set_flags(ctx->param, flags); | |
2500 | } | |
5d7c222d | 2501 | |
0f113f3e MC |
2502 | void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, |
2503 | time_t t) | |
2504 | { | |
2505 | X509_VERIFY_PARAM_set_time(ctx->param, t); | |
2506 | } | |
bbb72003 | 2507 | |
8cc86b81 | 2508 | X509 *X509_STORE_CTX_get0_cert(const X509_STORE_CTX *ctx) |
1060a50b RL |
2509 | { |
2510 | return ctx->cert; | |
2511 | } | |
2512 | ||
8cc86b81 | 2513 | STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(const X509_STORE_CTX *ctx) |
1060a50b RL |
2514 | { |
2515 | return ctx->untrusted; | |
2516 | } | |
2517 | ||
2518 | void X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) | |
2519 | { | |
2520 | ctx->untrusted = sk; | |
2521 | } | |
2522 | ||
2523 | void X509_STORE_CTX_set0_verified_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) | |
2524 | { | |
2525 | sk_X509_pop_free(ctx->chain, X509_free); | |
2526 | ctx->chain = sk; | |
2527 | } | |
2528 | ||
db089ad6 | 2529 | void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx, |
f0e0fd51 | 2530 | X509_STORE_CTX_verify_cb verify_cb) |
0f113f3e MC |
2531 | { |
2532 | ctx->verify_cb = verify_cb; | |
2533 | } | |
db089ad6 | 2534 | |
8cc86b81 | 2535 | X509_STORE_CTX_verify_cb X509_STORE_CTX_get_verify_cb(const X509_STORE_CTX *ctx) |
f0e0fd51 RS |
2536 | { |
2537 | return ctx->verify_cb; | |
2538 | } | |
2539 | ||
4a7b3a7b VD |
2540 | void X509_STORE_CTX_set_verify(X509_STORE_CTX *ctx, |
2541 | X509_STORE_CTX_verify_fn verify) | |
2542 | { | |
2543 | ctx->verify = verify; | |
2544 | } | |
2545 | ||
8cc86b81 | 2546 | X509_STORE_CTX_verify_fn X509_STORE_CTX_get_verify(const X509_STORE_CTX *ctx) |
f0e0fd51 | 2547 | { |
1060a50b | 2548 | return ctx->verify; |
f0e0fd51 RS |
2549 | } |
2550 | ||
8cc86b81 | 2551 | X509_STORE_CTX_get_issuer_fn X509_STORE_CTX_get_get_issuer(const X509_STORE_CTX *ctx) |
f0e0fd51 | 2552 | { |
1060a50b | 2553 | return ctx->get_issuer; |
f0e0fd51 RS |
2554 | } |
2555 | ||
8cc86b81 DDO |
2556 | X509_STORE_CTX_check_issued_fn |
2557 | X509_STORE_CTX_get_check_issued(const X509_STORE_CTX *ctx) | |
4dba585f | 2558 | { |
1060a50b | 2559 | return ctx->check_issued; |
4dba585f DSH |
2560 | } |
2561 | ||
8cc86b81 DDO |
2562 | X509_STORE_CTX_check_revocation_fn |
2563 | X509_STORE_CTX_get_check_revocation(const X509_STORE_CTX *ctx) | |
f0e0fd51 | 2564 | { |
1060a50b | 2565 | return ctx->check_revocation; |
f0e0fd51 RS |
2566 | } |
2567 | ||
8cc86b81 | 2568 | X509_STORE_CTX_get_crl_fn X509_STORE_CTX_get_get_crl(const X509_STORE_CTX *ctx) |
f0e0fd51 | 2569 | { |
1060a50b | 2570 | return ctx->get_crl; |
f0e0fd51 RS |
2571 | } |
2572 | ||
8cc86b81 | 2573 | X509_STORE_CTX_check_crl_fn X509_STORE_CTX_get_check_crl(const X509_STORE_CTX *ctx) |
f0e0fd51 | 2574 | { |
1060a50b RL |
2575 | return ctx->check_crl; |
2576 | } | |
2577 | ||
8cc86b81 | 2578 | X509_STORE_CTX_cert_crl_fn X509_STORE_CTX_get_cert_crl(const X509_STORE_CTX *ctx) |
1060a50b RL |
2579 | { |
2580 | return ctx->cert_crl; | |
2581 | } | |
2582 | ||
8cc86b81 DDO |
2583 | X509_STORE_CTX_check_policy_fn |
2584 | X509_STORE_CTX_get_check_policy(const X509_STORE_CTX *ctx) | |
1060a50b RL |
2585 | { |
2586 | return ctx->check_policy; | |
2587 | } | |
2588 | ||
8cc86b81 DDO |
2589 | X509_STORE_CTX_lookup_certs_fn |
2590 | X509_STORE_CTX_get_lookup_certs(const X509_STORE_CTX *ctx) | |
1060a50b RL |
2591 | { |
2592 | return ctx->lookup_certs; | |
2593 | } | |
2594 | ||
8cc86b81 DDO |
2595 | X509_STORE_CTX_lookup_crls_fn |
2596 | X509_STORE_CTX_get_lookup_crls(const X509_STORE_CTX *ctx) | |
1060a50b RL |
2597 | { |
2598 | return ctx->lookup_crls; | |
2599 | } | |
2600 | ||
8cc86b81 | 2601 | X509_STORE_CTX_cleanup_fn X509_STORE_CTX_get_cleanup(const X509_STORE_CTX *ctx) |
1060a50b RL |
2602 | { |
2603 | return ctx->cleanup; | |
f0e0fd51 RS |
2604 | } |
2605 | ||
8cc86b81 | 2606 | X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2607 | { |
2608 | return ctx->tree; | |
2609 | } | |
5d7c222d | 2610 | |
8cc86b81 | 2611 | int X509_STORE_CTX_get_explicit_policy(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2612 | { |
2613 | return ctx->explicit_policy; | |
2614 | } | |
5d7c222d | 2615 | |
8cc86b81 | 2616 | int X509_STORE_CTX_get_num_untrusted(const X509_STORE_CTX *ctx) |
7f3f41d8 | 2617 | { |
d9b8b89b | 2618 | return ctx->num_untrusted; |
7f3f41d8 MC |
2619 | } |
2620 | ||
5d7c222d | 2621 | int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name) |
0f113f3e MC |
2622 | { |
2623 | const X509_VERIFY_PARAM *param; | |
12a765a5 | 2624 | |
0f113f3e | 2625 | param = X509_VERIFY_PARAM_lookup(name); |
12a765a5 | 2626 | if (param == NULL) |
0f113f3e MC |
2627 | return 0; |
2628 | return X509_VERIFY_PARAM_inherit(ctx->param, param); | |
2629 | } | |
5d7c222d | 2630 | |
8cc86b81 | 2631 | X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(const X509_STORE_CTX *ctx) |
0f113f3e MC |
2632 | { |
2633 | return ctx->param; | |
2634 | } | |
5d7c222d DSH |
2635 | |
2636 | void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param) | |
0f113f3e | 2637 | { |
222561fe | 2638 | X509_VERIFY_PARAM_free(ctx->param); |
0f113f3e MC |
2639 | ctx->param = param; |
2640 | } | |
d9b8b89b | 2641 | |
b9aec69a | 2642 | void X509_STORE_CTX_set0_dane(X509_STORE_CTX *ctx, SSL_DANE *dane) |
919ba009 VD |
2643 | { |
2644 | ctx->dane = dane; | |
2645 | } | |
2646 | ||
170b7358 VD |
2647 | static unsigned char *dane_i2d( |
2648 | X509 *cert, | |
2649 | uint8_t selector, | |
2650 | unsigned int *i2dlen) | |
2651 | { | |
2652 | unsigned char *buf = NULL; | |
2653 | int len; | |
2654 | ||
2655 | /* | |
2656 | * Extract ASN.1 DER form of certificate or public key. | |
2657 | */ | |
2658 | switch (selector) { | |
2659 | case DANETLS_SELECTOR_CERT: | |
2660 | len = i2d_X509(cert, &buf); | |
2661 | break; | |
2662 | case DANETLS_SELECTOR_SPKI: | |
2663 | len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &buf); | |
2664 | break; | |
2665 | default: | |
9311d0c4 | 2666 | ERR_raise(ERR_LIB_X509, X509_R_BAD_SELECTOR); |
170b7358 VD |
2667 | return NULL; |
2668 | } | |
2669 | ||
2670 | if (len < 0 || buf == NULL) { | |
9311d0c4 | 2671 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
170b7358 VD |
2672 | return NULL; |
2673 | } | |
2674 | ||
2675 | *i2dlen = (unsigned int)len; | |
2676 | return buf; | |
2677 | } | |
2678 | ||
2679 | #define DANETLS_NONE 256 /* impossible uint8_t */ | |
2680 | ||
2681 | static int dane_match(X509_STORE_CTX *ctx, X509 *cert, int depth) | |
2682 | { | |
b9aec69a | 2683 | SSL_DANE *dane = ctx->dane; |
170b7358 VD |
2684 | unsigned usage = DANETLS_NONE; |
2685 | unsigned selector = DANETLS_NONE; | |
2686 | unsigned ordinal = DANETLS_NONE; | |
2687 | unsigned mtype = DANETLS_NONE; | |
2688 | unsigned char *i2dbuf = NULL; | |
2689 | unsigned int i2dlen = 0; | |
2690 | unsigned char mdbuf[EVP_MAX_MD_SIZE]; | |
2691 | unsigned char *cmpbuf = NULL; | |
2692 | unsigned int cmplen = 0; | |
2693 | int i; | |
2694 | int recnum; | |
2695 | int matched = 0; | |
2696 | danetls_record *t = NULL; | |
2697 | uint32_t mask; | |
2698 | ||
2699 | mask = (depth == 0) ? DANETLS_EE_MASK : DANETLS_TA_MASK; | |
2700 | ||
2701 | /* | |
2702 | * The trust store is not applicable with DANE-TA(2) | |
2703 | */ | |
2704 | if (depth >= ctx->num_untrusted) | |
2705 | mask &= DANETLS_PKIX_MASK; | |
2706 | ||
2707 | /* | |
2708 | * If we've previously matched a PKIX-?? record, no need to test any | |
02e112a8 | 2709 | * further PKIX-?? records, it remains to just build the PKIX chain. |
170b7358 VD |
2710 | * Had the match been a DANE-?? record, we'd be done already. |
2711 | */ | |
2712 | if (dane->mdpth >= 0) | |
2713 | mask &= ~DANETLS_PKIX_MASK; | |
2714 | ||
2715 | /*- | |
2716 | * https://tools.ietf.org/html/rfc7671#section-5.1 | |
2717 | * https://tools.ietf.org/html/rfc7671#section-5.2 | |
2718 | * https://tools.ietf.org/html/rfc7671#section-5.3 | |
2719 | * https://tools.ietf.org/html/rfc7671#section-5.4 | |
2720 | * | |
2721 | * We handle DANE-EE(3) records first as they require no chain building | |
2722 | * and no expiration or hostname checks. We also process digests with | |
2723 | * higher ordinals first and ignore lower priorities except Full(0) which | |
2724 | * is always processed (last). If none match, we then process PKIX-EE(1). | |
2725 | * | |
2726 | * NOTE: This relies on DANE usages sorting before the corresponding PKIX | |
2727 | * usages in SSL_dane_tlsa_add(), and also on descending sorting of digest | |
2728 | * priorities. See twin comment in ssl/ssl_lib.c. | |
2729 | * | |
2730 | * We expect that most TLSA RRsets will have just a single usage, so we | |
2731 | * don't go out of our way to cache multiple selector-specific i2d buffers | |
2732 | * across usages, but if the selector happens to remain the same as switch | |
2733 | * usages, that's OK. Thus, a set of "3 1 1", "3 0 1", "1 1 1", "1 0 1", | |
2734 | * records would result in us generating each of the certificate and public | |
2735 | * key DER forms twice, but more typically we'd just see multiple "3 1 1" | |
2736 | * or multiple "3 0 1" records. | |
2737 | * | |
2738 | * As soon as we find a match at any given depth, we stop, because either | |
2739 | * we've matched a DANE-?? record and the peer is authenticated, or, after | |
0d4fb843 | 2740 | * exhausting all DANE-?? records, we've matched a PKIX-?? record, which is |
170b7358 VD |
2741 | * sufficient for DANE, and what remains to do is ordinary PKIX validation. |
2742 | */ | |
2743 | recnum = (dane->umask & mask) ? sk_danetls_record_num(dane->trecs) : 0; | |
2744 | for (i = 0; matched == 0 && i < recnum; ++i) { | |
2745 | t = sk_danetls_record_value(dane->trecs, i); | |
2746 | if ((DANETLS_USAGE_BIT(t->usage) & mask) == 0) | |
2747 | continue; | |
2748 | if (t->usage != usage) { | |
2749 | usage = t->usage; | |
2750 | ||
2751 | /* Reset digest agility for each usage/selector pair */ | |
2752 | mtype = DANETLS_NONE; | |
2753 | ordinal = dane->dctx->mdord[t->mtype]; | |
2754 | } | |
2755 | if (t->selector != selector) { | |
2756 | selector = t->selector; | |
2757 | ||
2758 | /* Update per-selector state */ | |
2759 | OPENSSL_free(i2dbuf); | |
2760 | i2dbuf = dane_i2d(cert, selector, &i2dlen); | |
2761 | if (i2dbuf == NULL) | |
2762 | return -1; | |
2763 | ||
2764 | /* Reset digest agility for each usage/selector pair */ | |
2765 | mtype = DANETLS_NONE; | |
2766 | ordinal = dane->dctx->mdord[t->mtype]; | |
2767 | } else if (t->mtype != DANETLS_MATCHING_FULL) { | |
2768 | /*- | |
2769 | * Digest agility: | |
2770 | * | |
2771 | * <https://tools.ietf.org/html/rfc7671#section-9> | |
2772 | * | |
2773 | * For a fixed selector, after processing all records with the | |
2774 | * highest mtype ordinal, ignore all mtypes with lower ordinals | |
2775 | * other than "Full". | |
2776 | */ | |
2777 | if (dane->dctx->mdord[t->mtype] < ordinal) | |
2778 | continue; | |
2779 | } | |
2780 | ||
2781 | /* | |
2782 | * Each time we hit a (new selector or) mtype, re-compute the relevant | |
2783 | * digest, more complex caching is not worth the code space. | |
2784 | */ | |
2785 | if (t->mtype != mtype) { | |
2786 | const EVP_MD *md = dane->dctx->mdevp[mtype = t->mtype]; | |
2787 | cmpbuf = i2dbuf; | |
2788 | cmplen = i2dlen; | |
2789 | ||
2790 | if (md != NULL) { | |
dccd20d1 F |
2791 | cmpbuf = mdbuf; |
2792 | if (!EVP_Digest(i2dbuf, i2dlen, cmpbuf, &cmplen, md, 0)) { | |
2793 | matched = -1; | |
170b7358 VD |
2794 | break; |
2795 | } | |
2796 | } | |
2797 | } | |
2798 | ||
2799 | /* | |
2800 | * Squirrel away the certificate and depth if we have a match. Any | |
2801 | * DANE match is dispositive, but with PKIX we still need to build a | |
2802 | * full chain. | |
2803 | */ | |
2804 | if (cmplen == t->dlen && | |
2805 | memcmp(cmpbuf, t->data, cmplen) == 0) { | |
2806 | if (DANETLS_USAGE_BIT(usage) & DANETLS_DANE_MASK) | |
2807 | matched = 1; | |
2808 | if (matched || dane->mdpth < 0) { | |
2809 | dane->mdpth = depth; | |
2810 | dane->mtlsa = t; | |
2811 | OPENSSL_free(dane->mcert); | |
2812 | dane->mcert = cert; | |
2813 | X509_up_ref(cert); | |
2814 | } | |
2815 | break; | |
2816 | } | |
2817 | } | |
2818 | ||
2819 | /* Clear the one-element DER cache */ | |
2820 | OPENSSL_free(i2dbuf); | |
2821 | return matched; | |
2822 | } | |
2823 | ||
2824 | static int check_dane_issuer(X509_STORE_CTX *ctx, int depth) | |
2825 | { | |
b9aec69a | 2826 | SSL_DANE *dane = ctx->dane; |
170b7358 VD |
2827 | int matched = 0; |
2828 | X509 *cert; | |
2829 | ||
2830 | if (!DANETLS_HAS_TA(dane) || depth == 0) | |
2831 | return X509_TRUST_UNTRUSTED; | |
2832 | ||
2833 | /* | |
ade08735 | 2834 | * Record any DANE trust anchor matches, for the first depth to test, if |
170b7358 VD |
2835 | * there's one at that depth. (This'll be false for length 1 chains looking |
2836 | * for an exact match for the leaf certificate). | |
2837 | */ | |
2838 | cert = sk_X509_value(ctx->chain, depth); | |
2839 | if (cert != NULL && (matched = dane_match(ctx, cert, depth)) < 0) | |
2840 | return X509_TRUST_REJECTED; | |
2841 | if (matched > 0) { | |
2842 | ctx->num_untrusted = depth - 1; | |
2843 | return X509_TRUST_TRUSTED; | |
2844 | } | |
2845 | ||
2846 | return X509_TRUST_UNTRUSTED; | |
2847 | } | |
2848 | ||
2849 | static int check_dane_pkeys(X509_STORE_CTX *ctx) | |
2850 | { | |
b9aec69a | 2851 | SSL_DANE *dane = ctx->dane; |
170b7358 VD |
2852 | danetls_record *t; |
2853 | int num = ctx->num_untrusted; | |
2854 | X509 *cert = sk_X509_value(ctx->chain, num - 1); | |
2855 | int recnum = sk_danetls_record_num(dane->trecs); | |
2856 | int i; | |
2857 | ||
2858 | for (i = 0; i < recnum; ++i) { | |
2859 | t = sk_danetls_record_value(dane->trecs, i); | |
2860 | if (t->usage != DANETLS_USAGE_DANE_TA || | |
2861 | t->selector != DANETLS_SELECTOR_SPKI || | |
2862 | t->mtype != DANETLS_MATCHING_FULL || | |
6725682d | 2863 | X509_verify(cert, t->spki) <= 0) |
170b7358 VD |
2864 | continue; |
2865 | ||
c0a445a9 | 2866 | /* Clear any PKIX-?? matches that failed to extend to a full chain */ |
170b7358 VD |
2867 | X509_free(dane->mcert); |
2868 | dane->mcert = NULL; | |
2869 | ||
2870 | /* Record match via a bare TA public key */ | |
2871 | ctx->bare_ta_signed = 1; | |
2872 | dane->mdpth = num - 1; | |
2873 | dane->mtlsa = t; | |
2874 | ||
2875 | /* Prune any excess chain certificates */ | |
2876 | num = sk_X509_num(ctx->chain); | |
2877 | for (; num > ctx->num_untrusted; --num) | |
2878 | X509_free(sk_X509_pop(ctx->chain)); | |
2879 | ||
2880 | return X509_TRUST_TRUSTED; | |
2881 | } | |
2882 | ||
2883 | return X509_TRUST_UNTRUSTED; | |
2884 | } | |
2885 | ||
b9aec69a | 2886 | static void dane_reset(SSL_DANE *dane) |
170b7358 VD |
2887 | { |
2888 | /* | |
2889 | * Reset state to verify another chain, or clear after failure. | |
2890 | */ | |
2891 | X509_free(dane->mcert); | |
2892 | dane->mcert = NULL; | |
2893 | dane->mtlsa = NULL; | |
2894 | dane->mdpth = -1; | |
2895 | dane->pdpth = -1; | |
2896 | } | |
2897 | ||
6e328256 VD |
2898 | static int check_leaf_suiteb(X509_STORE_CTX *ctx, X509 *cert) |
2899 | { | |
2900 | int err = X509_chain_check_suiteb(NULL, cert, NULL, ctx->param->flags); | |
2901 | ||
6e5e118c DO |
2902 | CHECK_CB(err != X509_V_OK, ctx, cert, 0, err); |
2903 | return 1; | |
6e328256 VD |
2904 | } |
2905 | ||
170b7358 VD |
2906 | static int dane_verify(X509_STORE_CTX *ctx) |
2907 | { | |
2908 | X509 *cert = ctx->cert; | |
b9aec69a | 2909 | SSL_DANE *dane = ctx->dane; |
170b7358 VD |
2910 | int matched; |
2911 | int done; | |
2912 | ||
2913 | dane_reset(dane); | |
2914 | ||
89ff989d VD |
2915 | /*- |
2916 | * When testing the leaf certificate, if we match a DANE-EE(3) record, | |
2917 | * dane_match() returns 1 and we're done. If however we match a PKIX-EE(1) | |
2918 | * record, the match depth and matching TLSA record are recorded, but the | |
ade08735 | 2919 | * return value is 0, because we still need to find a PKIX trust anchor. |
89ff989d VD |
2920 | * Therefore, when DANE authentication is enabled (required), we're done |
2921 | * if: | |
2922 | * + matched < 0, internal error. | |
2923 | * + matched == 1, we matched a DANE-EE(3) record | |
2924 | * + matched == 0, mdepth < 0 (no PKIX-EE match) and there are no | |
2925 | * DANE-TA(2) or PKIX-TA(0) to test. | |
2926 | */ | |
170b7358 VD |
2927 | matched = dane_match(ctx, ctx->cert, 0); |
2928 | done = matched != 0 || (!DANETLS_HAS_TA(dane) && dane->mdpth < 0); | |
2929 | ||
2930 | if (done) | |
2931 | X509_get_pubkey_parameters(NULL, ctx->chain); | |
2932 | ||
2933 | if (matched > 0) { | |
70dd3c65 | 2934 | /* Callback invoked as needed */ |
6e328256 VD |
2935 | if (!check_leaf_suiteb(ctx, cert)) |
2936 | return 0; | |
5ae4ceb9 VD |
2937 | /* Callback invoked as needed */ |
2938 | if ((dane->flags & DANE_FLAG_NO_DANE_EE_NAMECHECKS) == 0 && | |
2939 | !check_id(ctx)) | |
2940 | return 0; | |
70dd3c65 | 2941 | /* Bypass internal_verify(), issue depth 0 success callback */ |
170b7358 VD |
2942 | ctx->error_depth = 0; |
2943 | ctx->current_cert = cert; | |
6e328256 | 2944 | return ctx->verify_cb(1, ctx); |
170b7358 VD |
2945 | } |
2946 | ||
2947 | if (matched < 0) { | |
2948 | ctx->error_depth = 0; | |
2949 | ctx->current_cert = cert; | |
2950 | ctx->error = X509_V_ERR_OUT_OF_MEM; | |
2951 | return -1; | |
2952 | } | |
2953 | ||
2954 | if (done) { | |
2955 | /* Fail early, TA-based success is not possible */ | |
6e328256 VD |
2956 | if (!check_leaf_suiteb(ctx, cert)) |
2957 | return 0; | |
70dd3c65 | 2958 | return verify_cb_cert(ctx, cert, 0, X509_V_ERR_DANE_NO_MATCH); |
170b7358 VD |
2959 | } |
2960 | ||
2961 | /* | |
2962 | * Chain verification for usages 0/1/2. TLSA record matching of depth > 0 | |
2963 | * certificates happens in-line with building the rest of the chain. | |
2964 | */ | |
2965 | return verify_chain(ctx); | |
2966 | } | |
2967 | ||
fbb82a60 VD |
2968 | /* Get issuer, without duplicate suppression */ |
2969 | static int get_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *cert) | |
2970 | { | |
2971 | STACK_OF(X509) *saved_chain = ctx->chain; | |
2972 | int ok; | |
2973 | ||
2974 | ctx->chain = NULL; | |
2975 | ok = ctx->get_issuer(issuer, ctx, cert); | |
2976 | ctx->chain = saved_chain; | |
2977 | ||
2978 | return ok; | |
2979 | } | |
2980 | ||
d9b8b89b VD |
2981 | static int build_chain(X509_STORE_CTX *ctx) |
2982 | { | |
b9aec69a | 2983 | SSL_DANE *dane = ctx->dane; |
d9b8b89b VD |
2984 | int num = sk_X509_num(ctx->chain); |
2985 | X509 *cert = sk_X509_value(ctx->chain, num - 1); | |
0d8dbb52 | 2986 | int self_signed; |
d9b8b89b VD |
2987 | STACK_OF(X509) *sktmp = NULL; |
2988 | unsigned int search; | |
170b7358 | 2989 | int may_trusted = 0; |
d9b8b89b VD |
2990 | int may_alternate = 0; |
2991 | int trust = X509_TRUST_UNTRUSTED; | |
2992 | int alt_untrusted = 0; | |
2993 | int depth; | |
2994 | int ok = 0; | |
2995 | int i; | |
2996 | ||
2997 | /* Our chain starts with a single untrusted element. */ | |
24664a3b | 2998 | if (!ossl_assert(num == 1 && ctx->num_untrusted == num)) { |
9311d0c4 | 2999 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
24664a3b MC |
3000 | ctx->error = X509_V_ERR_UNSPECIFIED; |
3001 | return 0; | |
3002 | } | |
d9b8b89b | 3003 | |
6725682d | 3004 | self_signed = X509_self_signed(cert, 0); |
02369787 | 3005 | if (self_signed < 0) { |
0c56a648 MC |
3006 | ctx->error = X509_V_ERR_UNSPECIFIED; |
3007 | return 0; | |
3008 | } | |
3009 | ||
d9b8b89b VD |
3010 | #define S_DOUNTRUSTED (1 << 0) /* Search untrusted chain */ |
3011 | #define S_DOTRUSTED (1 << 1) /* Search trusted store */ | |
3012 | #define S_DOALTERNATE (1 << 2) /* Retry with pruned alternate chain */ | |
3013 | /* | |
3014 | * Set up search policy, untrusted if possible, trusted-first if enabled. | |
170b7358 VD |
3015 | * If we're doing DANE and not doing PKIX-TA/PKIX-EE, we never look in the |
3016 | * trust_store, otherwise we might look there first. If not trusted-first, | |
3017 | * and alternate chains are not disabled, try building an alternate chain | |
3018 | * if no luck with untrusted first. | |
d9b8b89b VD |
3019 | */ |
3020 | search = (ctx->untrusted != NULL) ? S_DOUNTRUSTED : 0; | |
170b7358 VD |
3021 | if (DANETLS_HAS_PKIX(dane) || !DANETLS_HAS_DANE(dane)) { |
3022 | if (search == 0 || ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) | |
3023 | search |= S_DOTRUSTED; | |
3024 | else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) | |
3025 | may_alternate = 1; | |
3026 | may_trusted = 1; | |
3027 | } | |
d9b8b89b VD |
3028 | |
3029 | /* | |
3030 | * Shallow-copy the stack of untrusted certificates (with TLS, this is | |
3031 | * typically the content of the peer's certificate message) so can make | |
3032 | * multiple passes over it, while free to remove elements as we go. | |
3033 | */ | |
3034 | if (ctx->untrusted && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) { | |
9311d0c4 | 3035 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
f3e235ed | 3036 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
d9b8b89b VD |
3037 | return 0; |
3038 | } | |
3039 | ||
69664d6a | 3040 | /* |
ade08735 | 3041 | * If we got any "DANE-TA(2) Cert(0) Full(0)" trust anchors from DNS, add |
69664d6a VD |
3042 | * them to our working copy of the untrusted certificate stack. Since the |
3043 | * caller of X509_STORE_CTX_init() may have provided only a leaf cert with | |
3044 | * no corresponding stack of untrusted certificates, we may need to create | |
3045 | * an empty stack first. [ At present only the ssl library provides DANE | |
3046 | * support, and ssl_verify_cert_chain() always provides a non-null stack | |
3047 | * containing at least the leaf certificate, but we must be prepared for | |
3048 | * this to change. ] | |
3049 | */ | |
170b7358 | 3050 | if (DANETLS_ENABLED(dane) && dane->certs != NULL) { |
69664d6a | 3051 | if (sktmp == NULL && (sktmp = sk_X509_new_null()) == NULL) { |
9311d0c4 | 3052 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
f3e235ed | 3053 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
69664d6a VD |
3054 | return 0; |
3055 | } | |
eeccc237 DDO |
3056 | if (!X509_add_certs(sktmp, dane->certs, X509_ADD_FLAG_DEFAULT)) { |
3057 | sk_X509_free(sktmp); | |
3058 | ctx->error = X509_V_ERR_OUT_OF_MEM; | |
3059 | return 0; | |
170b7358 VD |
3060 | } |
3061 | } | |
3062 | ||
d9b8b89b VD |
3063 | /* |
3064 | * Still absurdly large, but arithmetically safe, a lower hard upper bound | |
3065 | * might be reasonable. | |
3066 | */ | |
3067 | if (ctx->param->depth > INT_MAX/2) | |
3068 | ctx->param->depth = INT_MAX/2; | |
3069 | ||
3070 | /* | |
ade08735 | 3071 | * Try to extend the chain until we reach an ultimately trusted issuer. |
d9b8b89b VD |
3072 | * Build chains up to one longer the limit, later fail if we hit the limit, |
3073 | * with an X509_V_ERR_CERT_CHAIN_TOO_LONG error code. | |
3074 | */ | |
3075 | depth = ctx->param->depth + 1; | |
3076 | ||
3077 | while (search != 0) { | |
3078 | X509 *x; | |
3079 | X509 *xtmp = NULL; | |
3080 | ||
3081 | /* | |
3082 | * Look in the trust store if enabled for first lookup, or we've run | |
fbb82a60 VD |
3083 | * out of untrusted issuers and search here is not disabled. When we |
3084 | * reach the depth limit, we stop extending the chain, if by that point | |
ade08735 | 3085 | * we've not found a trust anchor, any trusted chain would be too long. |
fbb82a60 VD |
3086 | * |
3087 | * The error reported to the application verify callback is at the | |
3088 | * maximal valid depth with the current certificate equal to the last | |
3089 | * not ultimately-trusted issuer. For example, with verify_depth = 0, | |
3090 | * the callback will report errors at depth=1 when the immediate issuer | |
3091 | * of the leaf certificate is not a trust anchor. No attempt will be | |
3092 | * made to locate an issuer for that certificate, since such a chain | |
3093 | * would be a-priori too long. | |
d9b8b89b VD |
3094 | */ |
3095 | if ((search & S_DOTRUSTED) != 0) { | |
d9b8b89b VD |
3096 | i = num = sk_X509_num(ctx->chain); |
3097 | if ((search & S_DOALTERNATE) != 0) { | |
3098 | /* | |
3099 | * As high up the chain as we can, look for an alternative | |
3100 | * trusted issuer of an untrusted certificate that currently | |
3101 | * has an untrusted issuer. We use the alt_untrusted variable | |
3102 | * to track how far up the chain we find the first match. It | |
3103 | * is only if and when we find a match, that we prune the chain | |
3104 | * and reset ctx->num_untrusted to the reduced count of | |
3105 | * untrusted certificates. While we're searching for such a | |
3106 | * match (which may never be found), it is neither safe nor | |
3107 | * wise to preemptively modify either the chain or | |
3108 | * ctx->num_untrusted. | |
3109 | * | |
3110 | * Note, like ctx->num_untrusted, alt_untrusted is a count of | |
3111 | * untrusted certificates, not a "depth". | |
3112 | */ | |
3113 | i = alt_untrusted; | |
3114 | } | |
3115 | x = sk_X509_value(ctx->chain, i-1); | |
3116 | ||
fbb82a60 | 3117 | ok = (depth < num) ? 0 : get_issuer(&xtmp, ctx, x); |
d9b8b89b VD |
3118 | |
3119 | if (ok < 0) { | |
3120 | trust = X509_TRUST_REJECTED; | |
f3e235ed | 3121 | ctx->error = X509_V_ERR_STORE_LOOKUP; |
d9b8b89b VD |
3122 | search = 0; |
3123 | continue; | |
3124 | } | |
3125 | ||
3126 | if (ok > 0) { | |
3127 | /* | |
3128 | * Alternative trusted issuer for a mid-chain untrusted cert? | |
3129 | * Pop the untrusted cert's successors and retry. We might now | |
3130 | * be able to complete a valid chain via the trust store. Note | |
ade08735 DDO |
3131 | * that despite the current trust store match we might still |
3132 | * fail complete the chain to a suitable trust anchor, in which | |
d9b8b89b VD |
3133 | * case we may prune some more untrusted certificates and try |
3134 | * again. Thus the S_DOALTERNATE bit may yet be turned on | |
3135 | * again with an even shorter untrusted chain! | |
170b7358 VD |
3136 | * |
3137 | * If in the process we threw away our matching PKIX-TA trust | |
3138 | * anchor, reset DANE trust. We might find a suitable trusted | |
3139 | * certificate among the ones from the trust store. | |
d9b8b89b VD |
3140 | */ |
3141 | if ((search & S_DOALTERNATE) != 0) { | |
02369787 | 3142 | if (!ossl_assert(num > i && i > 0 && !self_signed)) { |
9311d0c4 | 3143 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
24664a3b MC |
3144 | X509_free(xtmp); |
3145 | trust = X509_TRUST_REJECTED; | |
3146 | ctx->error = X509_V_ERR_UNSPECIFIED; | |
3147 | search = 0; | |
3148 | continue; | |
3149 | } | |
d9b8b89b VD |
3150 | search &= ~S_DOALTERNATE; |
3151 | for (; num > i; --num) | |
3152 | X509_free(sk_X509_pop(ctx->chain)); | |
3153 | ctx->num_untrusted = num; | |
170b7358 VD |
3154 | |
3155 | if (DANETLS_ENABLED(dane) && | |
3156 | dane->mdpth >= ctx->num_untrusted) { | |
3157 | dane->mdpth = -1; | |
3158 | X509_free(dane->mcert); | |
3159 | dane->mcert = NULL; | |
3160 | } | |
3161 | if (DANETLS_ENABLED(dane) && | |
3162 | dane->pdpth >= ctx->num_untrusted) | |
3163 | dane->pdpth = -1; | |
d9b8b89b VD |
3164 | } |
3165 | ||
3166 | /* | |
3167 | * Self-signed untrusted certificates get replaced by their | |
3168 | * trusted matching issuer. Otherwise, grow the chain. | |
3169 | */ | |
02369787 | 3170 | if (!self_signed) { |
d9b8b89b VD |
3171 | if (!sk_X509_push(ctx->chain, x = xtmp)) { |
3172 | X509_free(xtmp); | |
9311d0c4 | 3173 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
d9b8b89b | 3174 | trust = X509_TRUST_REJECTED; |
f3e235ed | 3175 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
d9b8b89b VD |
3176 | search = 0; |
3177 | continue; | |
3178 | } | |
6725682d | 3179 | self_signed = X509_self_signed(x, 0); |
02369787 | 3180 | if (self_signed < 0) { |
a268ed3a | 3181 | sk_X509_free(sktmp); |
0c56a648 MC |
3182 | ctx->error = X509_V_ERR_UNSPECIFIED; |
3183 | return 0; | |
3184 | } | |
d9b8b89b VD |
3185 | } else if (num == ctx->num_untrusted) { |
3186 | /* | |
3187 | * We have a self-signed certificate that has the same | |
3188 | * subject name (and perhaps keyid and/or serial number) as | |
ade08735 | 3189 | * a trust anchor. We must have an exact match to avoid |
d9b8b89b VD |
3190 | * possible impersonation via key substitution etc. |
3191 | */ | |
3192 | if (X509_cmp(x, xtmp) != 0) { | |
3193 | /* Self-signed untrusted mimic. */ | |
3194 | X509_free(xtmp); | |
3195 | ok = 0; | |
3196 | } else { | |
3197 | X509_free(x); | |
3198 | ctx->num_untrusted = --num; | |
3199 | (void) sk_X509_set(ctx->chain, num, x = xtmp); | |
3200 | } | |
3201 | } | |
3202 | ||
3203 | /* | |
e99505b4 | 3204 | * We've added a new trusted certificate to the chain, re-check |
d9b8b89b VD |
3205 | * trust. If not done, and not self-signed look deeper. |
3206 | * Whether or not we're doing "trusted first", we no longer | |
3207 | * look for untrusted certificates from the peer's chain. | |
170b7358 VD |
3208 | * |
3209 | * At this point ctx->num_trusted and num must reflect the | |
3210 | * correct number of untrusted certificates, since the DANE | |
3211 | * logic in check_trust() depends on distinguishing CAs from | |
3212 | * "the wire" from CAs from the trust store. In particular, the | |
3213 | * certificate at depth "num" should be the new trusted | |
3214 | * certificate with ctx->num_untrusted <= num. | |
d9b8b89b VD |
3215 | */ |
3216 | if (ok) { | |
24664a3b | 3217 | if (!ossl_assert(ctx->num_untrusted <= num)) { |
9311d0c4 | 3218 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
24664a3b MC |
3219 | trust = X509_TRUST_REJECTED; |
3220 | ctx->error = X509_V_ERR_UNSPECIFIED; | |
3221 | search = 0; | |
3222 | continue; | |
3223 | } | |
d9b8b89b VD |
3224 | search &= ~S_DOUNTRUSTED; |
3225 | switch (trust = check_trust(ctx, num)) { | |
3226 | case X509_TRUST_TRUSTED: | |
3227 | case X509_TRUST_REJECTED: | |
3228 | search = 0; | |
3229 | continue; | |
3230 | } | |
02369787 | 3231 | if (!self_signed) |
d9b8b89b VD |
3232 | continue; |
3233 | } | |
3234 | } | |
3235 | ||
3236 | /* | |
3237 | * No dispositive decision, and either self-signed or no match, if | |
3238 | * we were doing untrusted-first, and alt-chains are not disabled, | |
3239 | * do that, by repeatedly losing one untrusted element at a time, | |
3240 | * and trying to extend the shorted chain. | |
3241 | */ | |
3242 | if ((search & S_DOUNTRUSTED) == 0) { | |
3243 | /* Continue search for a trusted issuer of a shorter chain? */ | |
3244 | if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0) | |
3245 | continue; | |
3246 | /* Still no luck and no fallbacks left? */ | |
3247 | if (!may_alternate || (search & S_DOALTERNATE) != 0 || | |
3248 | ctx->num_untrusted < 2) | |
3249 | break; | |
3250 | /* Search for a trusted issuer of a shorter chain */ | |
3251 | search |= S_DOALTERNATE; | |
3252 | alt_untrusted = ctx->num_untrusted - 1; | |
02369787 | 3253 | self_signed = 0; |
d9b8b89b VD |
3254 | } |
3255 | } | |
3256 | ||
3257 | /* | |
3258 | * Extend chain with peer-provided certificates | |
3259 | */ | |
3260 | if ((search & S_DOUNTRUSTED) != 0) { | |
3261 | num = sk_X509_num(ctx->chain); | |
24664a3b | 3262 | if (!ossl_assert(num == ctx->num_untrusted)) { |
9311d0c4 | 3263 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
24664a3b MC |
3264 | trust = X509_TRUST_REJECTED; |
3265 | ctx->error = X509_V_ERR_UNSPECIFIED; | |
3266 | search = 0; | |
3267 | continue; | |
3268 | } | |
d9b8b89b | 3269 | x = sk_X509_value(ctx->chain, num-1); |
d9b8b89b VD |
3270 | |
3271 | /* | |
3272 | * Once we run out of untrusted issuers, we stop looking for more | |
3273 | * and start looking only in the trust store if enabled. | |
3274 | */ | |
02369787 DDO |
3275 | xtmp = (self_signed || depth < num) ? NULL |
3276 | : find_issuer(ctx, sktmp, x); | |
d9b8b89b VD |
3277 | if (xtmp == NULL) { |
3278 | search &= ~S_DOUNTRUSTED; | |
3279 | if (may_trusted) | |
3280 | search |= S_DOTRUSTED; | |
3281 | continue; | |
3282 | } | |
3283 | ||
fbb82a60 VD |
3284 | /* Drop this issuer from future consideration */ |
3285 | (void) sk_X509_delete_ptr(sktmp, xtmp); | |
3286 | ||
e9e7b5df | 3287 | if (!X509_up_ref(xtmp)) { |
9311d0c4 | 3288 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
e9e7b5df BE |
3289 | trust = X509_TRUST_REJECTED; |
3290 | ctx->error = X509_V_ERR_UNSPECIFIED; | |
3291 | search = 0; | |
3292 | continue; | |
3293 | } | |
3294 | ||
fbb82a60 | 3295 | if (!sk_X509_push(ctx->chain, xtmp)) { |
e9e7b5df | 3296 | X509_free(xtmp); |
9311d0c4 | 3297 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
d9b8b89b | 3298 | trust = X509_TRUST_REJECTED; |
f3e235ed | 3299 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
d9b8b89b VD |
3300 | search = 0; |
3301 | continue; | |
3302 | } | |
fbb82a60 | 3303 | |
e9e7b5df | 3304 | x = xtmp; |
d9b8b89b | 3305 | ++ctx->num_untrusted; |
7b424087 | 3306 | self_signed = X509_self_signed(x, 0); |
02369787 | 3307 | if (self_signed < 0) { |
2a4ad6a5 | 3308 | sk_X509_free(sktmp); |
02369787 | 3309 | ctx->error = X509_V_ERR_UNSPECIFIED; |
0c56a648 MC |
3310 | return 0; |
3311 | } | |
d9b8b89b | 3312 | |
170b7358 VD |
3313 | /* |
3314 | * Check for DANE-TA trust of the topmost untrusted certificate. | |
3315 | */ | |
3316 | switch (trust = check_dane_issuer(ctx, ctx->num_untrusted - 1)) { | |
3317 | case X509_TRUST_TRUSTED: | |
3318 | case X509_TRUST_REJECTED: | |
3319 | search = 0; | |
3320 | continue; | |
3321 | } | |
d9b8b89b VD |
3322 | } |
3323 | } | |
3324 | sk_X509_free(sktmp); | |
3325 | ||
3326 | /* | |
170b7358 VD |
3327 | * Last chance to make a trusted chain, either bare DANE-TA public-key |
3328 | * signers, or else direct leaf PKIX trust. | |
d9b8b89b | 3329 | */ |
497ecc0d VD |
3330 | num = sk_X509_num(ctx->chain); |
3331 | if (num <= depth) { | |
170b7358 VD |
3332 | if (trust == X509_TRUST_UNTRUSTED && DANETLS_HAS_DANE_TA(dane)) |
3333 | trust = check_dane_pkeys(ctx); | |
497ecc0d VD |
3334 | if (trust == X509_TRUST_UNTRUSTED && num == ctx->num_untrusted) |
3335 | trust = check_trust(ctx, num); | |
d9b8b89b VD |
3336 | } |
3337 | ||
3338 | switch (trust) { | |
3339 | case X509_TRUST_TRUSTED: | |
3340 | return 1; | |
3341 | case X509_TRUST_REJECTED: | |
70dd3c65 | 3342 | /* Callback already issued */ |
d9b8b89b VD |
3343 | return 0; |
3344 | case X509_TRUST_UNTRUSTED: | |
3345 | default: | |
3346 | num = sk_X509_num(ctx->chain); | |
6e5e118c DO |
3347 | CHECK_CB(num > depth, ctx, NULL, num-1, X509_V_ERR_CERT_CHAIN_TOO_LONG); |
3348 | CHECK_CB(DANETLS_ENABLED(dane) | |
3349 | && (!DANETLS_HAS_PKIX(dane) || dane->pdpth >= 0), | |
3350 | ctx, NULL, num-1, X509_V_ERR_DANE_NO_MATCH); | |
02369787 | 3351 | if (self_signed) |
70dd3c65 | 3352 | return verify_cb_cert(ctx, NULL, num-1, |
6e5e118c DO |
3353 | sk_X509_num(ctx->chain) == 1 |
3354 | ? X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT | |
3355 | : X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN); | |
70dd3c65 | 3356 | return verify_cb_cert(ctx, NULL, num-1, |
6e5e118c DO |
3357 | ctx->num_untrusted < num |
3358 | ? X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT | |
3359 | : X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY); | |
d9b8b89b VD |
3360 | } |
3361 | } | |
fbb82a60 VD |
3362 | |
3363 | static const int minbits_table[] = { 80, 112, 128, 192, 256 }; | |
3364 | static const int NUM_AUTH_LEVELS = OSSL_NELEM(minbits_table); | |
3365 | ||
3366 | /* | |
3367 | * Check whether the public key of ``cert`` meets the security level of | |
3368 | * ``ctx``. | |
3369 | * | |
3370 | * Returns 1 on success, 0 otherwise. | |
3371 | */ | |
3372 | static int check_key_level(X509_STORE_CTX *ctx, X509 *cert) | |
3373 | { | |
3374 | EVP_PKEY *pkey = X509_get0_pubkey(cert); | |
3375 | int level = ctx->param->auth_level; | |
3376 | ||
baba1545 KG |
3377 | /* |
3378 | * At security level zero, return without checking for a supported public | |
3379 | * key type. Some engines support key types not understood outside the | |
3380 | * engine, and we only need to understand the key when enforcing a security | |
3381 | * floor. | |
3382 | */ | |
3383 | if (level <= 0) | |
3384 | return 1; | |
3385 | ||
fbb82a60 VD |
3386 | /* Unsupported or malformed keys are not secure */ |
3387 | if (pkey == NULL) | |
3388 | return 0; | |
3389 | ||
fbb82a60 VD |
3390 | if (level > NUM_AUTH_LEVELS) |
3391 | level = NUM_AUTH_LEVELS; | |
3392 | ||
3393 | return EVP_PKEY_security_bits(pkey) >= minbits_table[level - 1]; | |
3394 | } | |
3395 | ||
cccf532f TM |
3396 | /* |
3397 | * Check whether the public key of ``cert`` does not use explicit params | |
3398 | * for an elliptic curve. | |
3399 | * | |
3400 | * Returns 1 on success, 0 if check fails, -1 for other errors. | |
3401 | */ | |
3402 | static int check_curve(X509 *cert) | |
3403 | { | |
3404 | #ifndef OPENSSL_NO_EC | |
3405 | EVP_PKEY *pkey = X509_get0_pubkey(cert); | |
3406 | ||
3407 | /* Unsupported or malformed key */ | |
3408 | if (pkey == NULL) | |
3409 | return -1; | |
3410 | ||
3411 | if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { | |
3412 | int ret; | |
3413 | ||
3414 | ret = EC_KEY_decoded_from_explicit_params(EVP_PKEY_get0_EC_KEY(pkey)); | |
3415 | return ret < 0 ? ret : !ret; | |
3416 | } | |
3417 | #endif | |
3418 | ||
3419 | return 1; | |
3420 | } | |
3421 | ||
fbb82a60 VD |
3422 | /* |
3423 | * Check whether the signature digest algorithm of ``cert`` meets the security | |
3424 | * level of ``ctx``. Should not be checked for trust anchors (whether | |
3425 | * self-signed or otherwise). | |
3426 | * | |
3427 | * Returns 1 on success, 0 otherwise. | |
3428 | */ | |
3429 | static int check_sig_level(X509_STORE_CTX *ctx, X509 *cert) | |
3430 | { | |
fbb82a60 VD |
3431 | int secbits = -1; |
3432 | int level = ctx->param->auth_level; | |
3433 | ||
3434 | if (level <= 0) | |
3435 | return 1; | |
3436 | if (level > NUM_AUTH_LEVELS) | |
3437 | level = NUM_AUTH_LEVELS; | |
3438 | ||
c3c8823c DSH |
3439 | if (!X509_get_signature_info(cert, NULL, NULL, &secbits, NULL)) |
3440 | return 0; | |
fbb82a60 VD |
3441 | |
3442 | return secbits >= minbits_table[level - 1]; | |
3443 | } |