Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/x509_vfy.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <ctype.h>
5 #include <limits.h>
6 #include <string.h>
7 #include <time.h>
8
9 #include <openssl/asn1.h>
10 #include <openssl/err.h>
11 #include <openssl/evp.h>
12 #include <openssl/mem.h>
13 #include <openssl/obj.h>
14 #include <openssl/thread.h>
15 #include <openssl/x509.h>
16
17 #include "../internal.h"
18 #include "internal.h"
19
20 static CRYPTO_EX_DATA_CLASS g_ex_data_class =
21 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
22
23 // CRL score values
24
25 // No unhandled critical extensions
26 #define CRL_SCORE_NOCRITICAL 0x100
27
28 // certificate is within CRL scope
29 #define CRL_SCORE_SCOPE 0x080
30
31 // CRL times valid
32 #define CRL_SCORE_TIME 0x040
33
34 // Issuer name matches certificate
35 #define CRL_SCORE_ISSUER_NAME 0x020
36
37 // If this score or above CRL is probably valid
38 #define CRL_SCORE_VALID \
39 (CRL_SCORE_NOCRITICAL | CRL_SCORE_TIME | CRL_SCORE_SCOPE)
40
41 // CRL issuer is certificate issuer
42 #define CRL_SCORE_ISSUER_CERT 0x018
43
44 // CRL issuer is on certificate path
45 #define CRL_SCORE_SAME_PATH 0x008
46
47 // CRL issuer matches CRL AKID
48 #define CRL_SCORE_AKID 0x004
49
50 static int null_callback(int ok, X509_STORE_CTX *e);
51 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
52 static int check_chain_extensions(X509_STORE_CTX *ctx);
53 static int check_name_constraints(X509_STORE_CTX *ctx);
54 static int check_id(X509_STORE_CTX *ctx);
55 static int check_trust(X509_STORE_CTX *ctx);
56 static int check_revocation(X509_STORE_CTX *ctx);
57 static int check_cert(X509_STORE_CTX *ctx);
58 static int check_policy(X509_STORE_CTX *ctx);
59
60 static int get_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
61 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl,
62 X509 *x);
63 static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x);
64 static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
65 int *pcrl_score);
66 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score);
67 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x);
68
69 static int internal_verify(X509_STORE_CTX *ctx);
70
71 static int null_callback(int ok, X509_STORE_CTX *e) { return ok; }
72 static int null_verify_custom_crit_oids_callback(X509_STORE_CTX *ctx,
73 X509 *x509,
74 STACK_OF(ASN1_OBJECT) *oids) {
75 // This returns 0 by default, so that the callback must be configured by the
76 // user when enabling the custom critical extensions feature.
77 return 0;
78 }
79
80 // cert_self_signed checks if |x| is self-signed. If |x| is valid, it returns
81 // one and sets |*out_is_self_signed| to the result. If |x| is invalid, it
82 // returns zero.
83 static int cert_self_signed(X509 *x, int *out_is_self_signed) {
84 if (!x509v3_cache_extensions(x)) {
85 return 0;
86 }
87 *out_is_self_signed = (x->ex_flags & EXFLAG_SS) != 0;
88 return 1;
89 }
90
91 static int call_verify_cb(int ok, X509_STORE_CTX *ctx) {
92 ok = ctx->verify_cb(ok, ctx);
93 // Historically, callbacks returning values like -1 would be treated as a mix
94 // of success or failure. Insert that callers check correctly.
95 //
96 // TODO(davidben): Also use this wrapper to constrain which errors may be
97 // suppressed, and ensure all |verify_cb| calls remember to fill in an error.
98 BSSL_CHECK(ok == 0 || ok == 1);
99 return ok;
100 }
101
102 // Given a certificate try and find an exact match in the store
103 static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x) {
104 STACK_OF(X509) *certs;
105 X509 *xtmp = NULL;
106 size_t i;
107 // Lookup all certs with matching subject name
108 certs = X509_STORE_CTX_get1_certs(ctx, X509_get_subject_name(x));
109 if (certs == NULL) {
110 return NULL;
111 }
112 // Look for exact match
113 for (i = 0; i < sk_X509_num(certs); i++) {
114 xtmp = sk_X509_value(certs, i);
115 if (!X509_cmp(xtmp, x)) {
116 break;
117 }
118 }
119 if (i < sk_X509_num(certs)) {
120 X509_up_ref(xtmp);
121 } else {
122 xtmp = NULL;
123 }
124 sk_X509_pop_free(certs, X509_free);
125 return xtmp;
126 }
127
128 int X509_verify_cert(X509_STORE_CTX *ctx) {
129 X509 *xtmp, *xtmp2, *chain_ss = NULL;
130 int bad_chain = 0;
131 X509_VERIFY_PARAM *param = ctx->param;
132 int i, ok = 0;
133 int j, retry, trust;
134 STACK_OF(X509) *sktmp = NULL;
135
136 if (ctx->cert == NULL) {
137 OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
138 ctx->error = X509_V_ERR_INVALID_CALL;
139 return -1;
140 }
141
142 if (ctx->chain != NULL) {
143 // This X509_STORE_CTX has already been used to verify a cert. We
144 // cannot do another one.
145 OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
146 ctx->error = X509_V_ERR_INVALID_CALL;
147 return -1;
148 }
149
150 if (ctx->param->flags &
151 (X509_V_FLAG_EXTENDED_CRL_SUPPORT | X509_V_FLAG_USE_DELTAS)) {
152 // We do not support indirect or delta CRLs. The flags still exist for
153 // compatibility with bindings libraries, but to ensure we do not
154 // inadvertently skip a CRL check that the caller expects, fail closed.
155 OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
156 ctx->error = X509_V_ERR_INVALID_CALL;
157 return -1;
158 }
159
160 // first we make sure the chain we are going to build is present and that
161 // the first entry is in place
162 ctx->chain = sk_X509_new_null();
163 if (ctx->chain == NULL || !sk_X509_push(ctx->chain, ctx->cert)) {
164 ctx->error = X509_V_ERR_OUT_OF_MEM;
165 goto end;
166 }
167 X509_up_ref(ctx->cert);
168 ctx->last_untrusted = 1;
169
170 // We use a temporary STACK so we can chop and hack at it.
171 if (ctx->untrusted != NULL && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
172 ctx->error = X509_V_ERR_OUT_OF_MEM;
173 goto end;
174 }
175
176 int num = (int)sk_X509_num(ctx->chain);
177 X509 *x = sk_X509_value(ctx->chain, num - 1);
178 // |param->depth| does not include the leaf certificate or the trust anchor,
179 // so the maximum size is 2 more.
180 int max_chain = param->depth >= INT_MAX - 2 ? INT_MAX : param->depth + 2;
181
182 for (;;) {
183 if (num >= max_chain) {
184 // FIXME: If this happens, we should take note of it and, if appropriate,
185 // use the X509_V_ERR_CERT_CHAIN_TOO_LONG error code later.
186 break;
187 }
188
189 int is_self_signed;
190 if (!cert_self_signed(x, &is_self_signed)) {
191 ctx->error = X509_V_ERR_INVALID_EXTENSION;
192 goto end;
193 }
194
195 // If we are self signed, we break
196 if (is_self_signed) {
197 break;
198 }
199 // If asked see if we can find issuer in trusted store first
200 if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
201 ok = get_issuer(&xtmp, ctx, x);
202 if (ok < 0) {
203 ctx->error = X509_V_ERR_STORE_LOOKUP;
204 goto end;
205 }
206 // If successful for now free up cert so it will be picked up
207 // again later.
208 if (ok > 0) {
209 X509_free(xtmp);
210 break;
211 }
212 }
213
214 // If we were passed a cert chain, use it first
215 if (sktmp != NULL) {
216 xtmp = find_issuer(ctx, sktmp, x);
217 if (xtmp != NULL) {
218 if (!sk_X509_push(ctx->chain, xtmp)) {
219 ctx->error = X509_V_ERR_OUT_OF_MEM;
220 ok = 0;
221 goto end;
222 }
223 X509_up_ref(xtmp);
224 (void)sk_X509_delete_ptr(sktmp, xtmp);
225 ctx->last_untrusted++;
226 x = xtmp;
227 num++;
228 // reparse the full chain for the next one
229 continue;
230 }
231 }
232 break;
233 }
234
235 // Remember how many untrusted certs we have
236 j = num;
237 // at this point, chain should contain a list of untrusted certificates.
238 // We now need to add at least one trusted one, if possible, otherwise we
239 // complain.
240
241 do {
242 // Examine last certificate in chain and see if it is self signed.
243 i = (int)sk_X509_num(ctx->chain);
244 x = sk_X509_value(ctx->chain, i - 1);
245
246 int is_self_signed;
247 if (!cert_self_signed(x, &is_self_signed)) {
248 ctx->error = X509_V_ERR_INVALID_EXTENSION;
249 goto end;
250 }
251
252 if (is_self_signed) {
253 // we have a self signed certificate
254 if (sk_X509_num(ctx->chain) == 1) {
255 // We have a single self signed certificate: see if we can
256 // find it in the store. We must have an exact match to avoid
257 // possible impersonation.
258 ok = get_issuer(&xtmp, ctx, x);
259 if ((ok <= 0) || X509_cmp(x, xtmp)) {
260 ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
261 ctx->current_cert = x;
262 ctx->error_depth = i - 1;
263 if (ok == 1) {
264 X509_free(xtmp);
265 }
266 bad_chain = 1;
267 ok = call_verify_cb(0, ctx);
268 if (!ok) {
269 goto end;
270 }
271 } else {
272 // We have a match: replace certificate with store
273 // version so we get any trust settings.
274 X509_free(x);
275 x = xtmp;
276 (void)sk_X509_set(ctx->chain, i - 1, x);
277 ctx->last_untrusted = 0;
278 }
279 } else {
280 // extract and save self signed certificate for later use
281 chain_ss = sk_X509_pop(ctx->chain);
282 ctx->last_untrusted--;
283 num--;
284 j--;
285 x = sk_X509_value(ctx->chain, num - 1);
286 }
287 }
288 // We now lookup certs from the certificate store
289 for (;;) {
290 if (num >= max_chain) {
291 // FIXME: If this happens, we should take note of it and, if
292 // appropriate, use the X509_V_ERR_CERT_CHAIN_TOO_LONG error code later.
293 break;
294 }
295 if (!cert_self_signed(x, &is_self_signed)) {
296 ctx->error = X509_V_ERR_INVALID_EXTENSION;
297 goto end;
298 }
299 // If we are self signed, we break
300 if (is_self_signed) {
301 break;
302 }
303 ok = get_issuer(&xtmp, ctx, x);
304
305 if (ok < 0) {
306 ctx->error = X509_V_ERR_STORE_LOOKUP;
307 goto end;
308 }
309 if (ok == 0) {
310 break;
311 }
312 x = xtmp;
313 if (!sk_X509_push(ctx->chain, x)) {
314 X509_free(xtmp);
315 ctx->error = X509_V_ERR_OUT_OF_MEM;
316 ok = 0;
317 goto end;
318 }
319
320 // OpenSSL 1.1.1 continuously re-checks for trust and breaks the loop
321 // as soon as trust has been established. 1.0.2 builds the chain with all
322 // possible certs first and only checks for trust if the final cert in
323 // the chain is self-signed.
324 // This caused additional unanticipated certs to be in the established
325 // certificate chain, particularly when |X509_V_FLAG_PARTIAL_CHAIN| was
326 // set. We try checking continuously for trust here for better 1.1.1
327 // compatibility.
328 trust = check_trust(ctx);
329 if (trust == X509_TRUST_TRUSTED || trust == X509_TRUST_REJECTED) {
330 break;
331 }
332
333 num++;
334 }
335
336 // we now have our chain, lets check it...
337 trust = check_trust(ctx);
338
339 // If explicitly rejected error
340 if (trust == X509_TRUST_REJECTED) {
341 ok = 0;
342 goto end;
343 }
344 // If it's not explicitly trusted then check if there is an alternative
345 // chain that could be used. We only do this if we haven't already
346 // checked via TRUSTED_FIRST and the user hasn't switched off alternate
347 // chain checking
348 retry = 0;
349 if (trust != X509_TRUST_TRUSTED &&
350 !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) &&
351 !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
352 while (j-- > 1) {
353 xtmp2 = sk_X509_value(ctx->chain, j - 1);
354 ok = get_issuer(&xtmp, ctx, xtmp2);
355 if (ok < 0) {
356 goto end;
357 }
358 // Check if we found an alternate chain
359 if (ok > 0) {
360 // Free up the found cert we'll add it again later
361 X509_free(xtmp);
362
363 // Dump all the certs above this point - we've found an
364 // alternate chain
365 while (num > j) {
366 xtmp = sk_X509_pop(ctx->chain);
367 X509_free(xtmp);
368 num--;
369 }
370 ctx->last_untrusted = (int)sk_X509_num(ctx->chain);
371 retry = 1;
372 break;
373 }
374 }
375 }
376 } while (retry);
377
378 // If not explicitly trusted then indicate error unless it's a single
379 // self signed certificate in which case we've indicated an error already
380 // and set bad_chain == 1
381 if (trust != X509_TRUST_TRUSTED && !bad_chain) {
382 if (chain_ss == NULL ||
383 !x509_check_issued_with_callback(ctx, x, chain_ss)) {
384 if (ctx->last_untrusted >= num) {
385 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
386 } else {
387 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
388 }
389 ctx->current_cert = x;
390 } else {
391 if (!sk_X509_push(ctx->chain, chain_ss)) {
392 ctx->error = X509_V_ERR_OUT_OF_MEM;
393 ok = 0;
394 goto end;
395 }
396 num++;
397 ctx->last_untrusted = num;
398 ctx->current_cert = chain_ss;
399 ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
400 chain_ss = NULL;
401 }
402
403 ctx->error_depth = num - 1;
404 bad_chain = 1;
405 ok = call_verify_cb(0, ctx);
406 if (!ok) {
407 goto end;
408 }
409 }
410
411 // We have the chain complete: now we need to check its purpose
412 ok = check_chain_extensions(ctx);
413
414 if (!ok) {
415 goto end;
416 }
417
418 ok = check_id(ctx);
419
420 if (!ok) {
421 goto end;
422 }
423
424 // Check revocation status: we do this after copying parameters because
425 // they may be needed for CRL signature verification.
426 ok = check_revocation(ctx);
427 if (!ok) {
428 goto end;
429 }
430
431 // At this point, we have a chain and need to verify it
432 ok = internal_verify(ctx);
433 if (!ok) {
434 goto end;
435 }
436
437 // Check name constraints
438
439 ok = check_name_constraints(ctx);
440 if (!ok) {
441 goto end;
442 }
443
444 // If we get this far evaluate policies
445 if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK)) {
446 ok = check_policy(ctx);
447 }
448
449 end:
450 if (sktmp != NULL) {
451 sk_X509_free(sktmp);
452 }
453 if (chain_ss != NULL) {
454 X509_free(chain_ss);
455 }
456
457 // Safety net, error returns must set ctx->error
458 if (ok <= 0 && ctx->error == X509_V_OK) {
459 ctx->error = X509_V_ERR_UNSPECIFIED;
460 }
461 return ok;
462 }
463
464 // Given a STACK_OF(X509) find the issuer of cert (if any)
465 //
466 // |find_issuer| will directly return the pointer of the corresponding index
467 // within |sk|. Callers of |find_issuer| should remember to bump the reference
468 // count of the returned |X509| if the call is successful.
469 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x) {
470 size_t i;
471 X509 *issuer, *candidate = NULL;
472 for (i = 0; i < sk_X509_num(sk); i++) {
473 issuer = sk_X509_value(sk, i);
474 if (x509_check_issued_with_callback(ctx, x, issuer)) {
475 candidate = issuer;
476 if (x509_check_cert_time(ctx, candidate, /*suppress_error*/ 1)) {
477 break;
478 }
479 }
480 }
481 return candidate;
482 }
483
484 // Given a possible certificate and issuer check them
485
486 int x509_check_issued_with_callback(X509_STORE_CTX *ctx, X509 *x,
487 X509 *issuer) {
488 int ret;
489 ret = X509_check_issued(issuer, x);
490 if (ret == X509_V_OK) {
491 return 1;
492 }
493 // If we haven't asked for issuer errors don't set ctx
494 if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK)) {
495 return 0;
496 }
497
498 ctx->error = ret;
499 ctx->current_cert = x;
500 ctx->current_issuer = issuer;
501 return call_verify_cb(0, ctx);
502 }
503
504 static int get_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) {
505 if (ctx->trusted_stack != NULL) {
506 // Ignore the store and use the configured stack instead.
507 *issuer = find_issuer(ctx, ctx->trusted_stack, x);
508 if (*issuer) {
509 X509_up_ref(*issuer);
510 return 1;
511 }
512 return 0;
513 }
514
515 return X509_STORE_CTX_get1_issuer(issuer, ctx, x);
516 }
517
518 static int check_custom_critical_extensions(X509_STORE_CTX *ctx, X509 *x) {
519 if (ctx->custom_crit_oids == NULL) {
520 // Fail if custom critical extensions are enabled, but none were set.
521 return 0;
522 }
523 size_t known_oid_count = sk_ASN1_OBJECT_num(ctx->custom_crit_oids);
524 if (known_oid_count == 0) {
525 return 0;
526 }
527
528 // Allocate |found_exts| to pass to the callback.
529 STACK_OF(ASN1_OBJECT) *found_exts = sk_ASN1_OBJECT_new_null();
530 if (found_exts == NULL) {
531 return 0;
532 }
533
534 // Iterate through all critical extensions of |x| and validate against the
535 // ones that aren't recognized by |X509_supported_extension|.
536 int last_pos = X509_get_ext_by_critical(x, 1, -1);
537 while (last_pos >= 0) {
538 const X509_EXTENSION *ext = X509_get_ext(x, last_pos);
539 if (!X509_supported_extension(ext)) {
540 int found = 0;
541
542 // Iterate through all set |custom_crit_oids|.
543 for (size_t i = 0; i < known_oid_count; i++) {
544 const ASN1_OBJECT *known_ext =
545 sk_ASN1_OBJECT_value(ctx->custom_crit_oids, i);
546 if (OBJ_cmp(ext->object, known_ext) == 0) {
547 // |sk_ASN1_OBJECT_value| returns a direct pointer.
548 ASN1_OBJECT *dup_obj = OBJ_dup(known_ext);
549 if (dup_obj == NULL || !sk_ASN1_OBJECT_push(found_exts, dup_obj)) {
550 ASN1_OBJECT_free(dup_obj);
551 sk_ASN1_OBJECT_pop_free(found_exts, ASN1_OBJECT_free);
552 return 0;
553 }
554 found = 1;
555 break;
556 }
557 }
558
559 if (!found) {
560 // If any critical extension isn't in our known list, return early.
561 sk_ASN1_OBJECT_pop_free(found_exts, ASN1_OBJECT_free);
562 return 0;
563 }
564 }
565 last_pos = X509_get_ext_by_critical(x, 1, last_pos);
566 }
567
568 // If we get here, all unknown critical extensions in |x| were
569 // properly handled and we pass the ones that were found to the caller.
570 if (!ctx->verify_custom_crit_oids(ctx, x, found_exts)) {
571 sk_ASN1_OBJECT_pop_free(found_exts, ASN1_OBJECT_free);
572 return 0;
573 }
574
575 sk_ASN1_OBJECT_pop_free(found_exts, ASN1_OBJECT_free);
576 return 1;
577 }
578
579 static int check_curve_decoded_by_name(X509 *x) {
580 EVP_PKEY *pkey = X509_get0_pubkey(x);
581 if (!pkey) {
582 return -1;
583 }
584
585 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC) {
586 return 1;
587 }
588
589 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
590 if (ec_key == NULL) {
591 return -1;
592 }
593
594 OPENSSL_BEGIN_ALLOW_DEPRECATED
595 int is_explicit = EC_KEY_decoded_from_explicit_params(ec_key);
596 OPENSSL_END_ALLOW_DEPRECATED
597
598 return !is_explicit;
599 }
600
601 // Check a certificate chains extensions for consistency with the supplied
602 // purpose
603
604 static int check_chain_extensions(X509_STORE_CTX *ctx) {
605 int ok = 0, plen = 0;
606 int purpose = ctx->param->purpose;
607
608 int num = (int)sk_X509_num(ctx->chain);
609
610 for (int i = 0; i < num; i++) {
611 X509 *x = sk_X509_value(ctx->chain, i);
612 if ( // OpenSSL's historic check for unknown critical extensions.
613 // |EXFLAG_CRITICAL| indicates an unsupported critical extension was
614 // found in |x| during the initial parsing of the certificate.
615 (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
616 (x->ex_flags & EXFLAG_CRITICAL)) &&
617 // AWS-LC specific logic for enabling custom unknown critical
618 // extensions.
619 !check_custom_critical_extensions(ctx, x)) {
620 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
621 ctx->error_depth = i;
622 ctx->current_cert = x;
623 ok = call_verify_cb(0, ctx);
624 if (!ok) {
625 goto end;
626 }
627 }
628
629 // The |num > 1| condition intentionally skips this check for single-certificate chains
630 // (e.g., a self-signed leaf or a partial chain with only the target certificate). This
631 // maintains consistency with OpenSSL 1.1.1 behavior.
632 if (num > 1 && (ctx->param->awslc_flags&AWSLC_V_ENABLE_EC_KEY_EXPLICIT_PARAMS) == 0) {
633 // In OpenSSL 1.1.1 this check was done if |X509_V_FLAG_X509_STRICT| was enabled.
634 // We did not perform this check explicitly before, but the behavior was baked into
635 // SPKI with explicit parameters not being parsed for elliptic curve key types.
636 //
637 // A conforming public CA should not issue certificate with explicitly encoded parameters for
638 // EC keys, but for privately operated CA's this could be doing so still. To be backwards
639 // compatible with OpenSSL 1.1.1 we have added support for parsing SPKI with explicit parameters,
640 // so we have added a new option for enabling this during chain validation.
641 int ret = check_curve_decoded_by_name(x);
642 if (ret < 0) {
643 ctx->error = X509_V_ERR_UNSPECIFIED;
644 ctx->error_depth = i;
645 ctx->current_cert = x;
646 ok = 0;
647 goto end;
648 } else if (ret == 0) {
649 ctx->error = X509_V_ERR_EC_KEY_EXPLICIT_PARAMS;
650 ctx->error_depth = i;
651 ctx->current_cert = x;
652 ok = 0;
653 goto end;
654 }
655 }
656
657 int must_be_ca = i > 0;
658 if (must_be_ca && !X509_check_ca(x)) {
659 ctx->error = X509_V_ERR_INVALID_CA;
660 ctx->error_depth = i;
661 ctx->current_cert = x;
662 ok = call_verify_cb(0, ctx);
663 if (!ok) {
664 goto end;
665 }
666 }
667 if (ctx->param->purpose > 0 &&
668 X509_check_purpose(x, purpose, must_be_ca) != 1) {
669 ctx->error = X509_V_ERR_INVALID_PURPOSE;
670 ctx->error_depth = i;
671 ctx->current_cert = x;
672 ok = call_verify_cb(0, ctx);
673 if (!ok) {
674 goto end;
675 }
676 }
677 // Check pathlen if not self issued
678 if (i > 1 && !(x->ex_flags & EXFLAG_SI) && x->ex_pathlen != -1 &&
679 plen - 1 > x->ex_pathlen) {
680 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
681 ctx->error_depth = i;
682 ctx->current_cert = x;
683 ok = call_verify_cb(0, ctx);
684 if (!ok) {
685 goto end;
686 }
687 }
688 // Increment path length if not self issued
689 if (!(x->ex_flags & EXFLAG_SI)) {
690 plen++;
691 }
692 }
693 ok = 1;
694 end:
695 return ok;
696 }
697
698 static int has_san_id(X509 *x, int type_id) {
699 int ret = 0;
700
701 GENERAL_NAMES *gs = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
702 if (gs == NULL) {
703 return 0;
704 }
705
706 for (size_t i = 0; i < sk_GENERAL_NAME_num(gs); i++) {
707 GENERAL_NAME *g = sk_GENERAL_NAME_value(gs, i);
708 if (g->type == type_id) {
709 ret = 1;
710 break;
711 }
712 }
713 GENERAL_NAMES_free(gs);
714 return ret;
715 }
716
717 static int check_name_constraints(X509_STORE_CTX *ctx) {
718 int i, j, rv;
719 // Check name constraints for all certificates
720 for (i = (int)sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
721 X509 *x = sk_X509_value(ctx->chain, i);
722 // Ignore self issued certs unless last in chain
723 if (i && (x->ex_flags & EXFLAG_SI)) {
724 continue;
725 }
726 // Check against constraints for all certificates higher in chain
727 // including trust anchor. Trust anchor not strictly speaking needed
728 // but if it includes constraints it is to be assumed it expects them
729 // to be obeyed.
730 for (j = (int)sk_X509_num(ctx->chain) - 1; j > i; j--) {
731 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
732 if (nc) {
733 rv = NAME_CONSTRAINTS_check(x, nc);
734 if (rv == X509_V_OK &&
735 (ctx->param->hostflags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT) ==
736 0 &&
737 ((ctx->param->hostflags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) !=
738 0 ||!has_san_id(x, GEN_DNS) )) {
739 rv = NAME_CONSTRAINTS_check_CN(x, nc);
740 }
741 switch (rv) {
742 case X509_V_OK:
743 continue;
744 case X509_V_ERR_OUT_OF_MEM:
745 ctx->error = rv;
746 return 0;
747 default:
748 ctx->error = rv;
749 ctx->error_depth = i;
750 ctx->current_cert = x;
751 if (!call_verify_cb(0, ctx)) {
752 return 0;
753 }
754 break;
755 }
756 }
757 }
758 }
759
760 return 1;
761 }
762
763 static int check_id_error(X509_STORE_CTX *ctx, int errcode) {
764 ctx->error = errcode;
765 ctx->current_cert = ctx->cert;
766 ctx->error_depth = 0;
767 return call_verify_cb(0, ctx);
768 }
769
770 static int check_hosts(X509 *x, X509_VERIFY_PARAM *param) {
771 size_t i;
772 size_t n = sk_OPENSSL_STRING_num(param->hosts);
773 char *name;
774
775 for (i = 0; i < n; ++i) {
776 name = sk_OPENSSL_STRING_value(param->hosts, i);
777 if (X509_check_host(x, name, strlen(name), param->hostflags, NULL) > 0) {
778 return 1;
779 }
780 }
781 return n == 0;
782 }
783
784 static int check_id(X509_STORE_CTX *ctx) {
785 X509_VERIFY_PARAM *vpm = ctx->param;
786 X509 *x = ctx->cert;
787 if (vpm->poison) {
788 if (!check_id_error(ctx, X509_V_ERR_INVALID_CALL)) {
789 return 0;
790 }
791 }
792 if (vpm->hosts && check_hosts(x, vpm) <= 0) {
793 if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH)) {
794 return 0;
795 }
796 }
797 if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) {
798 if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH)) {
799 return 0;
800 }
801 }
802 if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) {
803 if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH)) {
804 return 0;
805 }
806 }
807 return 1;
808 }
809
810 static int check_trust(X509_STORE_CTX *ctx) {
811 int ok;
812 X509 *x = NULL;
813 // Check all trusted certificates in chain
814 for (size_t i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
815 x = sk_X509_value(ctx->chain, i);
816 ok = X509_check_trust(x, ctx->param->trust, 0);
817 // If explicitly trusted return trusted
818 if (ok == X509_TRUST_TRUSTED) {
819 return X509_TRUST_TRUSTED;
820 }
821 // If explicitly rejected notify callback and reject if not
822 // overridden.
823 if (ok == X509_TRUST_REJECTED) {
824 ctx->error_depth = (int)i;
825 ctx->current_cert = x;
826 ctx->error = X509_V_ERR_CERT_REJECTED;
827 ok = call_verify_cb(0, ctx);
828 if (!ok) {
829 return X509_TRUST_REJECTED;
830 }
831 }
832 }
833 // If we accept partial chains and have at least one trusted certificate
834 // return success.
835 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
836 X509 *mx;
837 if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain)) {
838 return X509_TRUST_TRUSTED;
839 }
840 x = sk_X509_value(ctx->chain, 0);
841 mx = lookup_cert_match(ctx, x);
842 if (mx) {
843 (void)sk_X509_set(ctx->chain, 0, mx);
844 X509_free(x);
845 ctx->last_untrusted = 0;
846 return X509_TRUST_TRUSTED;
847 }
848 }
849
850 // If no trusted certs in chain at all return untrusted and allow
851 // standard (no issuer cert) etc errors to be indicated.
852 return X509_TRUST_UNTRUSTED;
853 }
854
855 static int check_revocation(X509_STORE_CTX *ctx) {
856 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK)) {
857 return 1;
858 }
859 int last;
860 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) {
861 last = (int)sk_X509_num(ctx->chain) - 1;
862 } else {
863 last = 0;
864 }
865 for (int i = 0; i <= last; i++) {
866 ctx->error_depth = i;
867 int ok = check_cert(ctx);
868 if (!ok) {
869 return ok;
870 }
871 }
872 return 1;
873 }
874
875 static int check_cert(X509_STORE_CTX *ctx) {
876 X509_CRL *crl = NULL;
877 int ok = 0, cnum = ctx->error_depth;
878 X509 *x = sk_X509_value(ctx->chain, cnum);
879 ctx->current_cert = x;
880 ctx->current_issuer = NULL;
881 ctx->current_crl_score = 0;
882
883 // Try to retrieve relevant CRL
884 ok = ctx->get_crl(ctx, &crl, x);
885 // If error looking up CRL, nothing we can do except notify callback
886 if (!ok) {
887 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
888 ok = call_verify_cb(0, ctx);
889 goto err;
890 }
891 ctx->current_crl = crl;
892 ok = ctx->check_crl(ctx, crl);
893 if (!ok) {
894 goto err;
895 }
896
897 ok = cert_crl(ctx, crl, x);
898 if (!ok) {
899 goto err;
900 }
901
902 err:
903 X509_CRL_free(crl);
904 ctx->current_crl = NULL;
905 return ok;
906 }
907
908 // Check CRL times against values in X509_STORE_CTX
909 static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) {
910 if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) {
911 return 1;
912 }
913
914 if (notify) {
915 ctx->current_crl = crl;
916 }
917 int64_t ptime;
918 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) {
919 ptime = ctx->param->check_time;
920 } else {
921 ptime = time(NULL);
922 }
923
924 int i = X509_cmp_time_posix(X509_CRL_get0_lastUpdate(crl), ptime);
925 if (i == 0) {
926 if (!notify) {
927 return 0;
928 }
929 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
930 if (!call_verify_cb(0, ctx)) {
931 return 0;
932 }
933 }
934
935 if (i > 0) {
936 if (!notify) {
937 return 0;
938 }
939 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
940 if (!call_verify_cb(0, ctx)) {
941 return 0;
942 }
943 }
944
945 if (X509_CRL_get0_nextUpdate(crl)) {
946 i = X509_cmp_time_posix(X509_CRL_get0_nextUpdate(crl), ptime);
947
948 if (i == 0) {
949 if (!notify) {
950 return 0;
951 }
952 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
953 if (!call_verify_cb(0, ctx)) {
954 return 0;
955 }
956 }
957 if (i < 0) {
958 if (!notify) {
959 return 0;
960 }
961 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
962 if (!call_verify_cb(0, ctx)) {
963 return 0;
964 }
965 }
966 }
967
968 if (notify) {
969 ctx->current_crl = NULL;
970 }
971
972 return 1;
973 }
974
975 static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 **pissuer,
976 int *pscore, STACK_OF(X509_CRL) *crls) {
977 int crl_score, best_score = *pscore;
978 X509 *x = ctx->current_cert;
979 X509_CRL *best_crl = NULL;
980 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
981
982 for (size_t i = 0; i < sk_X509_CRL_num(crls); i++) {
983 X509_CRL *crl = sk_X509_CRL_value(crls, i);
984 crl_score = get_crl_score(ctx, &crl_issuer, crl, x);
985 if (crl_score < best_score || crl_score == 0) {
986 continue;
987 }
988 // If current CRL is equivalent use it if it is newer
989 if (crl_score == best_score && best_crl != NULL) {
990 int day, sec;
991 if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl),
992 X509_CRL_get0_lastUpdate(crl)) == 0) {
993 continue;
994 }
995 // ASN1_TIME_diff never returns inconsistent signs for |day|
996 // and |sec|.
997 if (day <= 0 && sec <= 0) {
998 continue;
999 }
1000 }
1001 best_crl = crl;
1002 best_crl_issuer = crl_issuer;
1003 best_score = crl_score;
1004 }
1005
1006 if (best_crl) {
1007 if (*pcrl) {
1008 X509_CRL_free(*pcrl);
1009 }
1010 *pcrl = best_crl;
1011 *pissuer = best_crl_issuer;
1012 *pscore = best_score;
1013 X509_CRL_up_ref(best_crl);
1014 }
1015
1016 if (best_score >= CRL_SCORE_VALID) {
1017 return 1;
1018 }
1019
1020 return 0;
1021 }
1022
1023 // For a given CRL return how suitable it is for the supplied certificate
1024 // 'x'. The return value is a mask of several criteria. If the issuer is not
1025 // the certificate issuer this is returned in *pissuer.
1026 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl,
1027 X509 *x) {
1028 int crl_score = 0;
1029
1030 // First see if we can reject CRL straight away
1031
1032 // Invalid IDP cannot be processed
1033 if (crl->idp_flags & IDP_INVALID) {
1034 return 0;
1035 }
1036 // Reason codes and indirect CRLs are not supported.
1037 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS)) {
1038 return 0;
1039 }
1040 // We do not support indirect CRLs, so the issuer names must match.
1041 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
1042 return 0;
1043 }
1044 crl_score |= CRL_SCORE_ISSUER_NAME;
1045
1046 if (!(crl->flags & EXFLAG_CRITICAL)) {
1047 crl_score |= CRL_SCORE_NOCRITICAL;
1048 }
1049
1050 // Check expiry
1051 if (check_crl_time(ctx, crl, 0)) {
1052 crl_score |= CRL_SCORE_TIME;
1053 }
1054
1055 // Check authority key ID and locate certificate issuer
1056 if (!crl_akid_check(ctx, crl, pissuer, &crl_score)) {
1057 // If we can't locate certificate issuer at this point forget it
1058 return 0;
1059 }
1060
1061 // Check cert for matching CRL distribution points
1062 if (crl_crldp_check(x, crl, crl_score)) {
1063 crl_score |= CRL_SCORE_SCOPE;
1064 }
1065
1066 return crl_score;
1067 }
1068
1069 static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
1070 int *pcrl_score) {
1071 X509 *crl_issuer = NULL;
1072 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1073 int cidx = ctx->error_depth;
1074
1075 if ((size_t)cidx != sk_X509_num(ctx->chain) - 1) {
1076 cidx++;
1077 }
1078
1079 crl_issuer = sk_X509_value(ctx->chain, cidx);
1080
1081 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1082 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
1083 *pissuer = crl_issuer;
1084 return 1;
1085 }
1086
1087 for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) {
1088 crl_issuer = sk_X509_value(ctx->chain, cidx);
1089 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) {
1090 continue;
1091 }
1092 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1093 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
1094 *pissuer = crl_issuer;
1095 return 1;
1096 }
1097 }
1098
1099 return 0;
1100 }
1101
1102 // Check for match between two dist point names: three separate cases. 1.
1103 // Both are relative names and compare X509_NAME types. 2. One full, one
1104 // relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and
1105 // compare two GENERAL_NAMES. 4. One is NULL: automatic match.
1106 static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b) {
1107 X509_NAME *nm = NULL;
1108 GENERAL_NAMES *gens = NULL;
1109 GENERAL_NAME *gena, *genb;
1110 size_t i, j;
1111 if (!a || !b) {
1112 return 1;
1113 }
1114 if (a->type == 1) {
1115 if (!a->dpname) {
1116 return 0;
1117 }
1118 // Case 1: two X509_NAME
1119 if (b->type == 1) {
1120 if (!b->dpname) {
1121 return 0;
1122 }
1123 if (!X509_NAME_cmp(a->dpname, b->dpname)) {
1124 return 1;
1125 } else {
1126 return 0;
1127 }
1128 }
1129 // Case 2: set name and GENERAL_NAMES appropriately
1130 nm = a->dpname;
1131 gens = b->name.fullname;
1132 } else if (b->type == 1) {
1133 if (!b->dpname) {
1134 return 0;
1135 }
1136 // Case 2: set name and GENERAL_NAMES appropriately
1137 gens = a->name.fullname;
1138 nm = b->dpname;
1139 }
1140
1141 // Handle case 2 with one GENERAL_NAMES and one X509_NAME
1142 if (nm) {
1143 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1144 gena = sk_GENERAL_NAME_value(gens, i);
1145 if (gena->type != GEN_DIRNAME) {
1146 continue;
1147 }
1148 if (!X509_NAME_cmp(nm, gena->d.directoryName)) {
1149 return 1;
1150 }
1151 }
1152 return 0;
1153 }
1154
1155 // Else case 3: two GENERAL_NAMES
1156
1157 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1158 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1159 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1160 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1161 if (!GENERAL_NAME_cmp(gena, genb)) {
1162 return 1;
1163 }
1164 }
1165 }
1166
1167 return 0;
1168 }
1169
1170 // Check CRLDP and IDP. Return true when the CRL is a good
1171 // candidate CRL from which to check revocation of the certificate.
1172 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score) {
1173 if (crl->idp_flags & IDP_ONLYATTR) {
1174 return 0;
1175 }
1176 if (x->ex_flags & EXFLAG_CA) {
1177 if (crl->idp_flags & IDP_ONLYUSER) {
1178 return 0;
1179 }
1180 } else {
1181 if (crl->idp_flags & IDP_ONLYCA) {
1182 return 0;
1183 }
1184 }
1185 for (size_t i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1186 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1187 // Skip distribution points with a reasons field or a CRL issuer:
1188 //
1189 // We do not support CRLs partitioned by reason code. RFC 5280 requires CAs
1190 // include at least one DistributionPoint that covers all reasons.
1191 //
1192 // We also do not support indirect CRLs, and a CRL issuer can only match
1193 // indirect CRLs (RFC 5280, section 6.3.3, step b.1).
1194 if (dp->reasons != NULL || dp->CRLissuer != NULL) {
1195 continue;
1196 }
1197 // At this point we have already checked that the CRL issuer matches
1198 // the certificate issuer (and set CRL_SCORE_ISSUER_NAME);
1199
1200 // RFC 5280 Section 6.3.3 step b.2
1201 if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)){
1202 return 1;
1203 }
1204 }
1205
1206 // If the CRL does not specify an issuing distribution point, allow it to
1207 // match anything.
1208 // RFC5280 section 6.3.3 check (b).(2) does not prescribe what to do if the
1209 // CRL does not include an IDP. This fallback returns true if the CRL did not
1210 // include an IDP or an IDP without a DP. Such a CRL could still be a good
1211 // candidate CRL to check against although we cannot check if it matches the
1212 // DP in the certificate. In the event of multiple good candidate CRLs
1213 // (crl_crldp_check() returns 1 and get_crl_score() scores them high), some
1214 // without an IDP or with an IDP and without a DP and others matching the
1215 // certificate's DP, get_crl_sk() will pick the freshest one.
1216 return !crl->idp || !crl->idp->distpoint;
1217 }
1218
1219 // Retrieve CRL corresponding to current certificate.
1220 static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x) {
1221 int ok;
1222 X509 *issuer = NULL;
1223 int crl_score = 0;
1224 X509_CRL *crl = NULL;
1225 STACK_OF(X509_CRL) *skcrl;
1226 X509_NAME *nm = X509_get_issuer_name(x);
1227 ok = get_crl_sk(ctx, &crl, &issuer, &crl_score, ctx->crls);
1228 if (ok) {
1229 goto done;
1230 }
1231
1232 // Lookup CRLs from store
1233 skcrl = ctx->lookup_crls(ctx, nm);
1234
1235 // If no CRLs found and a near match from get_crl_sk use that
1236 if (!skcrl && crl) {
1237 goto done;
1238 }
1239
1240 get_crl_sk(ctx, &crl, &issuer, &crl_score, skcrl);
1241
1242 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1243
1244 done:
1245
1246 // If we got any kind of CRL use it and return success
1247 if (crl) {
1248 ctx->current_issuer = issuer;
1249 ctx->current_crl_score = crl_score;
1250 *pcrl = crl;
1251 return 1;
1252 }
1253
1254 return 0;
1255 }
1256
1257 // Check CRL validity
1258 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) {
1259 X509 *issuer = NULL;
1260 int cnum = ctx->error_depth;
1261 int chnum = (int)sk_X509_num(ctx->chain) - 1;
1262 // if we have an alternative CRL issuer cert use that
1263 if (ctx->current_issuer) {
1264 issuer = ctx->current_issuer;
1265 }
1266
1267 // Else find CRL issuer: if not last certificate then issuer is next
1268 // certificate in chain.
1269 else if (cnum < chnum) {
1270 issuer = sk_X509_value(ctx->chain, cnum + 1);
1271 } else {
1272 issuer = sk_X509_value(ctx->chain, chnum);
1273 // If not self signed, can't check signature
1274 if (!x509_check_issued_with_callback(ctx, issuer, issuer)) {
1275 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1276 if (!call_verify_cb(0, ctx)) {
1277 return 0;
1278 }
1279 }
1280 }
1281
1282 if (issuer) {
1283 // Check for cRLSign bit if keyUsage present
1284 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1285 !(issuer->ex_kusage & X509v3_KU_CRL_SIGN)) {
1286 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1287 if (!call_verify_cb(0, ctx)) {
1288 return 0;
1289 }
1290 }
1291
1292 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1293 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1294 if (!call_verify_cb(0, ctx)) {
1295 return 0;
1296 }
1297 }
1298
1299 if (crl->idp_flags & IDP_INVALID) {
1300 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1301 if (!call_verify_cb(0, ctx)) {
1302 return 0;
1303 }
1304 }
1305
1306 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1307 if (!check_crl_time(ctx, crl, 1)) {
1308 return 0;
1309 }
1310 }
1311
1312 // Attempt to get issuer certificate public key
1313 EVP_PKEY *ikey = X509_get0_pubkey(issuer);
1314 if (!ikey) {
1315 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1316 if (!call_verify_cb(0, ctx)) {
1317 return 0;
1318 }
1319 } else {
1320 // Verify CRL signature
1321 if (X509_CRL_verify(crl, ikey) <= 0) {
1322 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1323 if (!call_verify_cb(0, ctx)) {
1324 return 0;
1325 }
1326 }
1327 }
1328 }
1329
1330 return 1;
1331 }
1332
1333 // Check certificate against CRL
1334 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) {
1335 int ok;
1336 X509_REVOKED *rev;
1337 // The rules changed for this... previously if a CRL contained unhandled
1338 // critical extensions it could still be used to indicate a certificate
1339 // was revoked. This has since been changed since critical extension can
1340 // change the meaning of CRL entries.
1341 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
1342 (crl->flags & EXFLAG_CRITICAL)) {
1343 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1344 ok = call_verify_cb(0, ctx);
1345 if (!ok) {
1346 return 0;
1347 }
1348 }
1349 // Look for serial number of certificate in CRL.
1350 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1351 ctx->error = X509_V_ERR_CERT_REVOKED;
1352 ok = call_verify_cb(0, ctx);
1353 if (!ok) {
1354 return 0;
1355 }
1356 }
1357
1358 return 1;
1359 }
1360
1361 static int check_policy(X509_STORE_CTX *ctx) {
1362 X509 *current_cert = NULL;
1363 int ret = X509_policy_check(ctx->chain, ctx->param->policies,
1364 ctx->param->flags, &current_cert);
1365 if (ret != X509_V_OK) {
1366 ctx->current_cert = current_cert;
1367 ctx->error = ret;
1368 if (ret == X509_V_ERR_OUT_OF_MEM) {
1369 return 0;
1370 }
1371 return call_verify_cb(0, ctx);
1372 }
1373
1374 return 1;
1375 }
1376
1377 int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x509, int suppress_error) {
1378 if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) {
1379 return 1;
1380 }
1381
1382 int64_t ptime;
1383 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) {
1384 ptime = ctx->param->check_time;
1385 } else {
1386 ptime = time(NULL);
1387 }
1388
1389 int i = X509_cmp_time_posix(X509_get_notBefore(x509), ptime);
1390 if (i == 0) {
1391 if (suppress_error != 0) {
1392 return 0;
1393 }
1394 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1395 ctx->current_cert = x509;
1396 if (!call_verify_cb(0, ctx)) {
1397 return 0;
1398 }
1399 }
1400
1401 if (i > 0) {
1402 if (suppress_error != 0) {
1403 return 0;
1404 }
1405 ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1406 ctx->current_cert = x509;
1407 if (!call_verify_cb(0, ctx)) {
1408 return 0;
1409 }
1410 }
1411
1412 i = X509_cmp_time_posix(X509_get_notAfter(x509), ptime);
1413 if (i == 0) {
1414 if (suppress_error != 0) {
1415 return 0;
1416 }
1417 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1418 ctx->current_cert = x509;
1419 if (!call_verify_cb(0, ctx)) {
1420 return 0;
1421 }
1422 }
1423
1424 if (i < 0) {
1425 if (suppress_error != 0) {
1426 return 0;
1427 }
1428 ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1429 ctx->current_cert = x509;
1430 if (!call_verify_cb(0, ctx)) {
1431 return 0;
1432 }
1433 }
1434
1435 return 1;
1436 }
1437
1438 static int internal_verify(X509_STORE_CTX *ctx) {
1439 int ok = 0;
1440 X509 *xs, *xi;
1441
1442 int n = (int)sk_X509_num(ctx->chain);
1443 ctx->error_depth = n - 1;
1444 n--;
1445 xi = sk_X509_value(ctx->chain, n);
1446
1447 if (x509_check_issued_with_callback(ctx, xi, xi)) {
1448 xs = xi;
1449 } else {
1450 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
1451 xs = xi;
1452 goto check_cert;
1453 }
1454 if (n <= 0) {
1455 ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1456 ctx->current_cert = xi;
1457 ok = call_verify_cb(0, ctx);
1458 goto end;
1459 } else {
1460 n--;
1461 ctx->error_depth = n;
1462 xs = sk_X509_value(ctx->chain, n);
1463 }
1464 }
1465
1466 // ctx->error=0; not needed
1467 while (n >= 0) {
1468 ctx->error_depth = n;
1469
1470 // Skip signature check for self signed certificates unless
1471 // explicitly asked for. It doesn't add any security and just wastes
1472 // time.
1473 if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) {
1474 EVP_PKEY *pkey = X509_get0_pubkey(xi);
1475 if (pkey == NULL) {
1476 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1477 ctx->current_cert = xi;
1478 ok = call_verify_cb(0, ctx);
1479 if (!ok) {
1480 goto end;
1481 }
1482 } else if (X509_verify(xs, pkey) <= 0) {
1483 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1484 ctx->current_cert = xs;
1485 ok = call_verify_cb(0, ctx);
1486 if (!ok) {
1487 goto end;
1488 }
1489 }
1490 }
1491
1492 check_cert:
1493 ok = x509_check_cert_time(ctx, xs, /*suppress_error*/ 0);
1494 if (!ok) {
1495 goto end;
1496 }
1497
1498 // The last error (if any) is still in the error value
1499 ctx->current_issuer = xi;
1500 ctx->current_cert = xs;
1501 ok = call_verify_cb(1, ctx);
1502 if (!ok) {
1503 goto end;
1504 }
1505
1506 n--;
1507 if (n >= 0) {
1508 xi = xs;
1509 xs = sk_X509_value(ctx->chain, n);
1510 }
1511 }
1512
1513 // Check that the final certificate subject (leaf)
1514 // has a public key we actually support. Otherwise
1515 // we shouldn't consider the certificate okay. It is possible
1516 // for it to be reject sooner during |check_chain_extensions| as well
1517 // but disabling certain checks can result in an unuseable leaf certificate
1518 // making it to this point.
1519 //
1520 // This covers situations like invalid curves or points.
1521 EVP_PKEY *pkey = X509_get0_pubkey(xs);
1522 if(pkey == NULL) {
1523 ctx->error = X509_V_UNABLE_TO_GET_CERTS_PUBLIC_KEY;
1524 ctx->current_cert = xs;
1525 ok = 0;
1526 goto end;
1527 }
1528
1529 ok = 1;
1530 end:
1531 return ok;
1532 }
1533
1534 int X509_cmp_current_time(const ASN1_TIME *ctm) {
1535 return X509_cmp_time_posix(ctm, time(NULL));
1536 }
1537
1538 int X509_cmp_time(const ASN1_TIME *ctm, const time_t *cmp_time) {
1539 int64_t compare_time = (cmp_time == NULL) ? time(NULL) : *cmp_time;
1540 return X509_cmp_time_posix(ctm, compare_time);
1541 }
1542
1543 int X509_cmp_time_posix(const ASN1_TIME *ctm, int64_t cmp_time) {
1544 int64_t ctm_time;
1545 if (!ASN1_TIME_to_posix(ctm, &ctm_time)) {
1546 return 0;
1547 }
1548 // The return value 0 is reserved for errors.
1549 return (ctm_time - cmp_time <= 0) ? -1 : 1;
1550 }
1551
1552 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec) {
1553 return X509_time_adj(s, offset_sec, NULL);
1554 }
1555
1556 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, const time_t *in_tm) {
1557 return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1558 }
1559
1560 ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec,
1561 const time_t *in_tm) {
1562 int64_t t = 0;
1563
1564 if (in_tm) {
1565 t = *in_tm;
1566 } else {
1567 t = time(NULL);
1568 }
1569
1570 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1571 }
1572
1573 int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
1574 CRYPTO_EX_unused *unused,
1575 CRYPTO_EX_dup *dup_unused,
1576 CRYPTO_EX_free *free_func) {
1577 // This function is (usually) called only once, by
1578 // SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
1579 int index;
1580 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
1581 free_func)) {
1582 return -1;
1583 }
1584 return index;
1585 }
1586
1587 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data) {
1588 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1589 }
1590
1591 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx) {
1592 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1593 }
1594
1595 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx) { return ctx->error; }
1596
1597 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err) {
1598 ctx->error = err;
1599 }
1600
1601 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx) {
1602 return ctx->error_depth;
1603 }
1604
1605 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx) {
1606 return ctx->current_cert;
1607 }
1608
1609 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx) {
1610 return ctx->chain;
1611 }
1612
1613 STACK_OF(X509) *X509_STORE_CTX_get0_chain(X509_STORE_CTX *ctx) {
1614 return ctx->chain;
1615 }
1616
1617 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx) {
1618 if (!ctx->chain) {
1619 return NULL;
1620 }
1621 return X509_chain_up_ref(ctx->chain);
1622 }
1623
1624 X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx) {
1625 return ctx->current_issuer;
1626 }
1627
1628 X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx) {
1629 return ctx->current_crl;
1630 }
1631
1632 X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx) {
1633 // In OpenSSL, an |X509_STORE_CTX| sometimes has a parent context during CRL
1634 // path validation for indirect CRLs. We require the CRL to be issued
1635 // somewhere along the certificate path, so this is always NULL.
1636 return NULL;
1637 }
1638
1639 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x) { ctx->cert = x; }
1640
1641 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) {
1642 ctx->untrusted = sk;
1643 }
1644
1645 void X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) {
1646 X509_STORE_CTX_set_chain(ctx, sk);
1647 }
1648
1649 STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx) {
1650 return ctx->untrusted;
1651 }
1652
1653 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk) {
1654 ctx->crls = sk;
1655 }
1656
1657 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) {
1658 // If |purpose| is zero, this function historically silently did nothing.
1659 if (purpose == 0) {
1660 return 1;
1661 }
1662
1663 int idx = X509_PURPOSE_get_by_id(purpose);
1664 if (idx == -1) {
1665 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
1666 return 0;
1667 }
1668
1669 int trust = X509_PURPOSE_get_trust(X509_PURPOSE_get0(idx));
1670 if (!X509_STORE_CTX_set_trust(ctx, trust)) {
1671 return 0;
1672 }
1673
1674 if (ctx->param->purpose == 0) {
1675 ctx->param->purpose = purpose;
1676 }
1677 return 1;
1678 }
1679
1680 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust) {
1681 // If |trust| is zero, this function historically silently did nothing.
1682 if (trust == 0) {
1683 return 1;
1684 }
1685
1686 if (X509_TRUST_get_by_id(trust) == -1) {
1687 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID);
1688 return 0;
1689 }
1690
1691 if (ctx->param->trust == 0) {
1692 ctx->param->trust = trust;
1693 }
1694 return 1;
1695 }
1696
1697 X509_STORE_CTX *X509_STORE_CTX_new(void) {
1698 return OPENSSL_zalloc(sizeof(X509_STORE_CTX));
1699 }
1700
1701 void X509_STORE_CTX_free(X509_STORE_CTX *ctx) {
1702 if (ctx == NULL) {
1703 return;
1704 }
1705 X509_STORE_CTX_cleanup(ctx);
1706 OPENSSL_free(ctx);
1707 }
1708
1709 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
1710 STACK_OF(X509) *chain) {
1711 X509_STORE_CTX_cleanup(ctx);
1712
1713 ctx->ctx = store;
1714 ctx->cert = x509;
1715 ctx->untrusted = chain;
1716
1717 CRYPTO_new_ex_data(&ctx->ex_data);
1718
1719 if (store == NULL) {
1720 OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
1721 goto err;
1722 }
1723
1724 ctx->param = X509_VERIFY_PARAM_new();
1725 if (!ctx->param) {
1726 goto err;
1727 }
1728
1729 // Inherit callbacks and flags from X509_STORE.
1730
1731 ctx->verify_cb = store->verify_cb;
1732
1733 if (!X509_VERIFY_PARAM_inherit(ctx->param, store->param) ||
1734 !X509_VERIFY_PARAM_inherit(ctx->param,
1735 X509_VERIFY_PARAM_lookup("default"))) {
1736 goto err;
1737 }
1738
1739 if (store->verify_cb) {
1740 ctx->verify_cb = store->verify_cb;
1741 } else {
1742 ctx->verify_cb = null_callback;
1743 }
1744
1745 if (store->get_crl) {
1746 ctx->get_crl = store->get_crl;
1747 } else {
1748 ctx->get_crl = get_crl;
1749 }
1750
1751 if (store->check_crl) {
1752 ctx->check_crl = store->check_crl;
1753 } else {
1754 ctx->check_crl = check_crl;
1755 }
1756
1757 if (store->lookup_crls) {
1758 ctx->lookup_crls = store->lookup_crls;
1759 } else {
1760 ctx->lookup_crls = X509_STORE_get1_crls;
1761 }
1762
1763 ctx->verify_custom_crit_oids = null_verify_custom_crit_oids_callback;
1764
1765 return 1;
1766
1767 err:
1768 CRYPTO_free_ex_data(&g_ex_data_class, ctx, &ctx->ex_data);
1769 if (ctx->param != NULL) {
1770 X509_VERIFY_PARAM_free(ctx->param);
1771 }
1772
1773 OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
1774 return 0;
1775 }
1776
1777 // Set alternative lookup method: just a STACK of trusted certificates. This
1778 // avoids X509_STORE nastiness where it isn't needed.
1779
1780 void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx,
1781 STACK_OF(X509) *sk) {
1782 ctx->trusted_stack = sk;
1783 }
1784
1785 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) {
1786 X509_STORE_CTX_set0_trusted_stack(ctx, sk);
1787 }
1788
1789 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) {
1790 CRYPTO_free_ex_data(&g_ex_data_class, ctx, &(ctx->ex_data));
1791 X509_VERIFY_PARAM_free(ctx->param);
1792 sk_X509_pop_free(ctx->chain, X509_free);
1793 sk_ASN1_OBJECT_pop_free(ctx->custom_crit_oids, ASN1_OBJECT_free);
1794 OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
1795 }
1796
1797 void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth) {
1798 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1799 }
1800
1801 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags) {
1802 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
1803 }
1804
1805 void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx, unsigned long flags,
1806 int64_t t) {
1807 X509_VERIFY_PARAM_set_time_posix(ctx->param, t);
1808 }
1809
1810 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
1811 time_t t) {
1812 X509_STORE_CTX_set_time_posix(ctx, flags, t);
1813 }
1814
1815 X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx) {
1816 return ctx->cert;
1817 }
1818
1819 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
1820 int (*verify_cb)(int, X509_STORE_CTX *)) {
1821 ctx->verify_cb = verify_cb;
1822 }
1823
1824 int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name) {
1825 const X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_lookup(name);
1826 if (!param) {
1827 return 0;
1828 }
1829 return X509_VERIFY_PARAM_inherit(ctx->param, param);
1830 }
1831
1832 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx) {
1833 return ctx->param;
1834 }
1835
1836 void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param) {
1837 if (ctx->param) {
1838 X509_VERIFY_PARAM_free(ctx->param);
1839 }
1840 ctx->param = param;
1841 }
1842
1843 int X509_STORE_CTX_add_custom_crit_oid(X509_STORE_CTX *ctx, ASN1_OBJECT *oid) {
1844 GUARD_PTR(ctx);
1845 GUARD_PTR(oid);
1846
1847 ASN1_OBJECT *oid_dup = OBJ_dup(oid);
1848 if (oid_dup == NULL) {
1849 return 0;
1850 }
1851 if (ctx->custom_crit_oids == NULL) {
1852 ctx->custom_crit_oids = sk_ASN1_OBJECT_new_null();
1853 if (ctx->custom_crit_oids == NULL) {
1854 ASN1_OBJECT_free(oid_dup);
1855 return 0;
1856 }
1857 }
1858
1859 if (!sk_ASN1_OBJECT_push(ctx->custom_crit_oids, oid_dup)) {
1860 ASN1_OBJECT_free(oid_dup);
1861 return 0;
1862 }
1863 return 1;
1864 }
1865
1866 void X509_STORE_CTX_set_verify_crit_oids(
1867 X509_STORE_CTX *ctx,
1868 X509_STORE_CTX_verify_crit_oids_cb verify_custom_crit_oids) {
1869 ctx->verify_custom_crit_oids = verify_custom_crit_oids;
1870 }