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