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