• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <ctype.h>
58 #include <string.h>
59 #include <time.h>
60 
61 #include <openssl/asn1.h>
62 #include <openssl/err.h>
63 #include <openssl/evp.h>
64 #include <openssl/mem.h>
65 #include <openssl/obj.h>
66 #include <openssl/thread.h>
67 #include <openssl/x509.h>
68 #include <openssl/x509v3.h>
69 
70 #include "internal.h"
71 #include "../internal.h"
72 #include "../x509v3/internal.h"
73 
74 static CRYPTO_EX_DATA_CLASS g_ex_data_class =
75     CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
76 
77 /* CRL score values */
78 
79 /* No unhandled critical extensions */
80 
81 #define CRL_SCORE_NOCRITICAL    0x100
82 
83 /* certificate is within CRL scope */
84 
85 #define CRL_SCORE_SCOPE         0x080
86 
87 /* CRL times valid */
88 
89 #define CRL_SCORE_TIME          0x040
90 
91 /* Issuer name matches certificate */
92 
93 #define CRL_SCORE_ISSUER_NAME   0x020
94 
95 /* If this score or above CRL is probably valid */
96 
97 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
98 
99 /* CRL issuer is certificate issuer */
100 
101 #define CRL_SCORE_ISSUER_CERT   0x018
102 
103 /* CRL issuer is on certificate path */
104 
105 #define CRL_SCORE_SAME_PATH     0x008
106 
107 /* CRL issuer matches CRL AKID */
108 
109 #define CRL_SCORE_AKID          0x004
110 
111 /* Have a delta CRL with valid times */
112 
113 #define CRL_SCORE_TIME_DELTA    0x002
114 
115 static int null_callback(int ok, X509_STORE_CTX *e);
116 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
117 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
118 static int check_chain_extensions(X509_STORE_CTX *ctx);
119 static int check_name_constraints(X509_STORE_CTX *ctx);
120 static int check_id(X509_STORE_CTX *ctx);
121 static int check_trust(X509_STORE_CTX *ctx);
122 static int check_revocation(X509_STORE_CTX *ctx);
123 static int check_cert(X509_STORE_CTX *ctx);
124 static int check_policy(X509_STORE_CTX *ctx);
125 
126 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
127                          unsigned int *preasons, X509_CRL *crl, X509 *x);
128 static int get_crl_delta(X509_STORE_CTX *ctx,
129                          X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
130 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
131                          int *pcrl_score, X509_CRL *base,
132                          STACK_OF(X509_CRL) *crls);
133 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
134                            int *pcrl_score);
135 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
136                            unsigned int *preasons);
137 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
138 static int check_crl_chain(X509_STORE_CTX *ctx,
139                            STACK_OF(X509) *cert_path,
140                            STACK_OF(X509) *crl_path);
141 
142 static int internal_verify(X509_STORE_CTX *ctx);
143 
null_callback(int ok,X509_STORE_CTX * e)144 static int null_callback(int ok, X509_STORE_CTX *e)
145 {
146     return ok;
147 }
148 
149 /* cert_self_signed checks if |x| is self-signed. If |x| is valid, it returns
150  * one and sets |*out_is_self_signed| to the result. If |x| is invalid, it
151  * returns zero. */
cert_self_signed(X509 * x,int * out_is_self_signed)152 static int cert_self_signed(X509 *x, int *out_is_self_signed)
153 {
154     if (!x509v3_cache_extensions(x)) {
155         return 0;
156     }
157     *out_is_self_signed = (x->ex_flags & EXFLAG_SS) != 0;
158     return 1;
159 }
160 
161 /* Given a certificate try and find an exact match in the store */
162 
lookup_cert_match(X509_STORE_CTX * ctx,X509 * x)163 static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
164 {
165     STACK_OF(X509) *certs;
166     X509 *xtmp = NULL;
167     size_t i;
168     /* Lookup all certs with matching subject name */
169     certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
170     if (certs == NULL)
171         return NULL;
172     /* Look for exact match */
173     for (i = 0; i < sk_X509_num(certs); i++) {
174         xtmp = sk_X509_value(certs, i);
175         if (!X509_cmp(xtmp, x))
176             break;
177     }
178     if (i < sk_X509_num(certs))
179         X509_up_ref(xtmp);
180     else
181         xtmp = NULL;
182     sk_X509_pop_free(certs, X509_free);
183     return xtmp;
184 }
185 
X509_verify_cert(X509_STORE_CTX * ctx)186 int X509_verify_cert(X509_STORE_CTX *ctx)
187 {
188     X509 *x, *xtmp, *xtmp2, *chain_ss = NULL;
189     int bad_chain = 0;
190     X509_VERIFY_PARAM *param = ctx->param;
191     int depth, i, ok = 0;
192     int num, j, retry, trust;
193     int (*cb) (int xok, X509_STORE_CTX *xctx);
194     STACK_OF(X509) *sktmp = NULL;
195     if (ctx->cert == NULL) {
196         OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
197         ctx->error = X509_V_ERR_INVALID_CALL;
198         return -1;
199     }
200     if (ctx->chain != NULL) {
201         /*
202          * This X509_STORE_CTX has already been used to verify a cert. We
203          * cannot do another one.
204          */
205         OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
206         ctx->error = X509_V_ERR_INVALID_CALL;
207         return -1;
208     }
209 
210     cb = ctx->verify_cb;
211 
212     /*
213      * first we make sure the chain we are going to build is present and that
214      * the first entry is in place
215      */
216     ctx->chain = sk_X509_new_null();
217     if (ctx->chain == NULL || !sk_X509_push(ctx->chain, ctx->cert)) {
218         OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
219         ctx->error = X509_V_ERR_OUT_OF_MEM;
220         goto end;
221     }
222     X509_up_ref(ctx->cert);
223     ctx->last_untrusted = 1;
224 
225     /* We use a temporary STACK so we can chop and hack at it.
226      * sktmp = ctx->untrusted ++ ctx->ctx->additional_untrusted */
227     if (ctx->untrusted != NULL
228         && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
229         OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
230         ctx->error = X509_V_ERR_OUT_OF_MEM;
231         goto end;
232     }
233 
234     if (ctx->ctx->additional_untrusted != NULL) {
235         if (sktmp == NULL) {
236             sktmp = sk_X509_new_null();
237             if (sktmp == NULL) {
238                 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
239                 ctx->error = X509_V_ERR_OUT_OF_MEM;
240                 goto end;
241             }
242         }
243 
244         for (size_t k = 0; k < sk_X509_num(ctx->ctx->additional_untrusted);
245              k++) {
246             if (!sk_X509_push(sktmp,
247                               sk_X509_value(ctx->ctx->additional_untrusted,
248                               k))) {
249                 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
250                 ctx->error = X509_V_ERR_OUT_OF_MEM;
251                 goto end;
252             }
253         }
254     }
255 
256     num = sk_X509_num(ctx->chain);
257     x = sk_X509_value(ctx->chain, num - 1);
258     depth = param->depth;
259 
260     for (;;) {
261         /* If we have enough, we break */
262         if (depth < num)
263             break;              /* FIXME: If this happens, we should take
264                                  * note of it and, if appropriate, use the
265                                  * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
266                                  * later. */
267 
268         int is_self_signed;
269         if (!cert_self_signed(x, &is_self_signed)) {
270             ctx->error = X509_V_ERR_INVALID_EXTENSION;
271             goto end;
272         }
273 
274         /* If we are self signed, we break */
275         if (is_self_signed)
276             break;
277         /*
278          * If asked see if we can find issuer in trusted store first
279          */
280         if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
281             ok = ctx->get_issuer(&xtmp, ctx, x);
282             if (ok < 0) {
283                 ctx->error = X509_V_ERR_STORE_LOOKUP;
284                 goto end;
285             }
286             /*
287              * If successful for now free up cert so it will be picked up
288              * again later.
289              */
290             if (ok > 0) {
291                 X509_free(xtmp);
292                 break;
293             }
294         }
295 
296         /* If we were passed a cert chain, use it first */
297         if (sktmp != NULL) {
298             xtmp = find_issuer(ctx, sktmp, x);
299             if (xtmp != NULL) {
300                 if (!sk_X509_push(ctx->chain, xtmp)) {
301                     OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
302                     ctx->error = X509_V_ERR_OUT_OF_MEM;
303                     ok = 0;
304                     goto end;
305                 }
306                 X509_up_ref(xtmp);
307                 (void)sk_X509_delete_ptr(sktmp, xtmp);
308                 ctx->last_untrusted++;
309                 x = xtmp;
310                 num++;
311                 /*
312                  * reparse the full chain for the next one
313                  */
314                 continue;
315             }
316         }
317         break;
318     }
319 
320     /* Remember how many untrusted certs we have */
321     j = num;
322     /*
323      * at this point, chain should contain a list of untrusted certificates.
324      * We now need to add at least one trusted one, if possible, otherwise we
325      * complain.
326      */
327 
328     do {
329         /*
330          * Examine last certificate in chain and see if it is self signed.
331          */
332         i = sk_X509_num(ctx->chain);
333         x = sk_X509_value(ctx->chain, i - 1);
334 
335         int is_self_signed;
336         if (!cert_self_signed(x, &is_self_signed)) {
337             ctx->error = X509_V_ERR_INVALID_EXTENSION;
338             goto end;
339         }
340 
341         if (is_self_signed) {
342             /* we have a self signed certificate */
343             if (sk_X509_num(ctx->chain) == 1) {
344                 /*
345                  * We have a single self signed certificate: see if we can
346                  * find it in the store. We must have an exact match to avoid
347                  * possible impersonation.
348                  */
349                 ok = ctx->get_issuer(&xtmp, ctx, x);
350                 if ((ok <= 0) || X509_cmp(x, xtmp)) {
351                     ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
352                     ctx->current_cert = x;
353                     ctx->error_depth = i - 1;
354                     if (ok == 1)
355                         X509_free(xtmp);
356                     bad_chain = 1;
357                     ok = cb(0, ctx);
358                     if (!ok)
359                         goto end;
360                 } else {
361                     /*
362                      * We have a match: replace certificate with store
363                      * version so we get any trust settings.
364                      */
365                     X509_free(x);
366                     x = xtmp;
367                     (void)sk_X509_set(ctx->chain, i - 1, x);
368                     ctx->last_untrusted = 0;
369                 }
370             } else {
371                 /*
372                  * extract and save self signed certificate for later use
373                  */
374                 chain_ss = sk_X509_pop(ctx->chain);
375                 ctx->last_untrusted--;
376                 num--;
377                 j--;
378                 x = sk_X509_value(ctx->chain, num - 1);
379             }
380         }
381         /* We now lookup certs from the certificate store */
382         for (;;) {
383             /* If we have enough, we break */
384             if (depth < num)
385                 break;
386             if (!cert_self_signed(x, &is_self_signed)) {
387                 ctx->error = X509_V_ERR_INVALID_EXTENSION;
388                 goto end;
389             }
390             /* If we are self signed, we break */
391             if (is_self_signed)
392                 break;
393             ok = ctx->get_issuer(&xtmp, ctx, x);
394 
395             if (ok < 0) {
396                 ctx->error = X509_V_ERR_STORE_LOOKUP;
397                 goto end;
398             }
399             if (ok == 0)
400                 break;
401             x = xtmp;
402             if (!sk_X509_push(ctx->chain, x)) {
403                 X509_free(xtmp);
404                 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
405                 ctx->error = X509_V_ERR_OUT_OF_MEM;
406                 ok = 0;
407                 goto end;
408             }
409             num++;
410         }
411 
412         /* we now have our chain, lets check it... */
413         trust = check_trust(ctx);
414 
415         /* If explicitly rejected error */
416         if (trust == X509_TRUST_REJECTED) {
417             ok = 0;
418             goto end;
419         }
420         /*
421          * If it's not explicitly trusted then check if there is an alternative
422          * chain that could be used. We only do this if we haven't already
423          * checked via TRUSTED_FIRST and the user hasn't switched off alternate
424          * chain checking
425          */
426         retry = 0;
427         if (trust != X509_TRUST_TRUSTED
428             && !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
429             && !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
430             while (j-- > 1) {
431                 xtmp2 = sk_X509_value(ctx->chain, j - 1);
432                 ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
433                 if (ok < 0)
434                     goto end;
435                 /* Check if we found an alternate chain */
436                 if (ok > 0) {
437                     /*
438                      * Free up the found cert we'll add it again later
439                      */
440                     X509_free(xtmp);
441 
442                     /*
443                      * Dump all the certs above this point - we've found an
444                      * alternate chain
445                      */
446                     while (num > j) {
447                         xtmp = sk_X509_pop(ctx->chain);
448                         X509_free(xtmp);
449                         num--;
450                     }
451                     ctx->last_untrusted = sk_X509_num(ctx->chain);
452                     retry = 1;
453                     break;
454                 }
455             }
456         }
457     } while (retry);
458 
459     /*
460      * If not explicitly trusted then indicate error unless it's a single
461      * self signed certificate in which case we've indicated an error already
462      * and set bad_chain == 1
463      */
464     if (trust != X509_TRUST_TRUSTED && !bad_chain) {
465         if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
466             if (ctx->last_untrusted >= num)
467                 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
468             else
469                 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
470             ctx->current_cert = x;
471         } else {
472 
473             sk_X509_push(ctx->chain, chain_ss);
474             num++;
475             ctx->last_untrusted = num;
476             ctx->current_cert = chain_ss;
477             ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
478             chain_ss = NULL;
479         }
480 
481         ctx->error_depth = num - 1;
482         bad_chain = 1;
483         ok = cb(0, ctx);
484         if (!ok)
485             goto end;
486     }
487 
488     /* We have the chain complete: now we need to check its purpose */
489     ok = check_chain_extensions(ctx);
490 
491     if (!ok)
492         goto end;
493 
494     ok = check_id(ctx);
495 
496     if (!ok)
497         goto end;
498 
499     /*
500      * Check revocation status: we do this after copying parameters because
501      * they may be needed for CRL signature verification.
502      */
503 
504     ok = ctx->check_revocation(ctx);
505     if (!ok)
506         goto end;
507 
508     int err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
509                                       ctx->param->flags);
510     if (err != X509_V_OK) {
511         ctx->error = err;
512         ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
513         ok = cb(0, ctx);
514         if (!ok)
515             goto end;
516     }
517 
518     /* At this point, we have a chain and need to verify it */
519     if (ctx->verify != NULL)
520         ok = ctx->verify(ctx);
521     else
522         ok = internal_verify(ctx);
523     if (!ok)
524         goto end;
525 
526     /* Check name constraints */
527 
528     ok = check_name_constraints(ctx);
529     if (!ok)
530         goto end;
531 
532     /* If we get this far evaluate policies */
533     if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
534         ok = ctx->check_policy(ctx);
535 
536  end:
537     if (sktmp != NULL)
538         sk_X509_free(sktmp);
539     if (chain_ss != NULL)
540         X509_free(chain_ss);
541 
542     /* Safety net, error returns must set ctx->error */
543     if (ok <= 0 && ctx->error == X509_V_OK)
544         ctx->error = X509_V_ERR_UNSPECIFIED;
545     return ok;
546 }
547 
548 /*
549  * Given a STACK_OF(X509) find the issuer of cert (if any)
550  */
551 
find_issuer(X509_STORE_CTX * ctx,STACK_OF (X509)* sk,X509 * x)552 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
553 {
554     size_t i;
555     X509 *issuer;
556     for (i = 0; i < sk_X509_num(sk); i++) {
557         issuer = sk_X509_value(sk, i);
558         if (ctx->check_issued(ctx, x, issuer))
559             return issuer;
560     }
561     return NULL;
562 }
563 
564 /* Given a possible certificate and issuer check them */
565 
check_issued(X509_STORE_CTX * ctx,X509 * x,X509 * issuer)566 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
567 {
568     int ret;
569     ret = X509_check_issued(issuer, x);
570     if (ret == X509_V_OK)
571         return 1;
572     /* If we haven't asked for issuer errors don't set ctx */
573     if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
574         return 0;
575 
576     ctx->error = ret;
577     ctx->current_cert = x;
578     ctx->current_issuer = issuer;
579     return ctx->verify_cb(0, ctx);
580 }
581 
582 /* Alternative lookup method: look from a STACK stored in other_ctx */
583 
get_issuer_sk(X509 ** issuer,X509_STORE_CTX * ctx,X509 * x)584 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
585 {
586     *issuer = find_issuer(ctx, ctx->other_ctx, x);
587     if (*issuer) {
588         X509_up_ref(*issuer);
589         return 1;
590     } else
591         return 0;
592 }
593 
594 /*
595  * Check a certificate chains extensions for consistency with the supplied
596  * purpose
597  */
598 
check_chain_extensions(X509_STORE_CTX * ctx)599 static int check_chain_extensions(X509_STORE_CTX *ctx)
600 {
601     int i, ok = 0, plen = 0;
602     X509 *x;
603     int (*cb) (int xok, X509_STORE_CTX *xctx);
604     int proxy_path_length = 0;
605     int purpose;
606     int allow_proxy_certs;
607     cb = ctx->verify_cb;
608 
609     enum {
610         // ca_or_leaf allows either type of certificate so that direct use of
611         // self-signed certificates works.
612         ca_or_leaf,
613         must_be_ca,
614         must_not_be_ca,
615     } ca_requirement;
616 
617     /* CRL path validation */
618     if (ctx->parent) {
619         allow_proxy_certs = 0;
620         purpose = X509_PURPOSE_CRL_SIGN;
621     } else {
622         allow_proxy_certs =
623             ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
624         purpose = ctx->param->purpose;
625     }
626 
627     ca_requirement = ca_or_leaf;
628 
629     /* Check all untrusted certificates */
630     for (i = 0; i < ctx->last_untrusted; i++) {
631         int ret;
632         x = sk_X509_value(ctx->chain, i);
633         if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
634             && (x->ex_flags & EXFLAG_CRITICAL)) {
635             ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
636             ctx->error_depth = i;
637             ctx->current_cert = x;
638             ok = cb(0, ctx);
639             if (!ok)
640                 goto end;
641         }
642         if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
643             ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
644             ctx->error_depth = i;
645             ctx->current_cert = x;
646             ok = cb(0, ctx);
647             if (!ok)
648                 goto end;
649         }
650 
651         switch (ca_requirement) {
652         case ca_or_leaf:
653             ret = 1;
654             break;
655         case must_not_be_ca:
656             if (X509_check_ca(x)) {
657                 ret = 0;
658                 ctx->error = X509_V_ERR_INVALID_NON_CA;
659             } else
660                 ret = 1;
661             break;
662         case must_be_ca:
663             if (!X509_check_ca(x)) {
664                 ret = 0;
665                 ctx->error = X509_V_ERR_INVALID_CA;
666             } else
667                 ret = 1;
668             break;
669         default:
670             // impossible.
671             ret = 0;
672         }
673 
674         if (ret == 0) {
675             ctx->error_depth = i;
676             ctx->current_cert = x;
677             ok = cb(0, ctx);
678             if (!ok)
679                 goto end;
680         }
681         if (ctx->param->purpose > 0) {
682             ret = X509_check_purpose(x, purpose, ca_requirement == must_be_ca);
683             if (ret != 1) {
684                 ret = 0;
685                 ctx->error = X509_V_ERR_INVALID_PURPOSE;
686                 ctx->error_depth = i;
687                 ctx->current_cert = x;
688                 ok = cb(0, ctx);
689                 if (!ok)
690                     goto end;
691             }
692         }
693         /* Check pathlen if not self issued */
694         if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
695             && (x->ex_pathlen != -1)
696             && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
697             ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
698             ctx->error_depth = i;
699             ctx->current_cert = x;
700             ok = cb(0, ctx);
701             if (!ok)
702                 goto end;
703         }
704         /* Increment path length if not self issued */
705         if (!(x->ex_flags & EXFLAG_SI))
706             plen++;
707         /*
708          * If this certificate is a proxy certificate, the next certificate
709          * must be another proxy certificate or a EE certificate.  If not,
710          * the next certificate must be a CA certificate.
711          */
712         if (x->ex_flags & EXFLAG_PROXY) {
713             if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
714                 ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
715                 ctx->error_depth = i;
716                 ctx->current_cert = x;
717                 ok = cb(0, ctx);
718                 if (!ok)
719                     goto end;
720             }
721             proxy_path_length++;
722             ca_requirement = must_not_be_ca;
723         } else {
724             ca_requirement = must_be_ca;
725         }
726     }
727     ok = 1;
728  end:
729     return ok;
730 }
731 
reject_dns_name_in_common_name(X509 * x509)732 static int reject_dns_name_in_common_name(X509 *x509)
733 {
734     X509_NAME *name = X509_get_subject_name(x509);
735     int i = -1;
736     for (;;) {
737         i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
738         if (i == -1) {
739             return X509_V_OK;
740         }
741 
742         X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i);
743         ASN1_STRING *common_name = X509_NAME_ENTRY_get_data(entry);
744         unsigned char *idval;
745         int idlen = ASN1_STRING_to_UTF8(&idval, common_name);
746         if (idlen < 0) {
747             return X509_V_ERR_OUT_OF_MEM;
748         }
749         /* Only process attributes that look like host names. Note it is
750          * important that this check be mirrored in |X509_check_host|. */
751         int looks_like_dns = x509v3_looks_like_dns_name(idval, (size_t)idlen);
752         OPENSSL_free(idval);
753         if (looks_like_dns) {
754             return X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS;
755         }
756     }
757 }
758 
check_name_constraints(X509_STORE_CTX * ctx)759 static int check_name_constraints(X509_STORE_CTX *ctx)
760 {
761     int i, j, rv;
762     int has_name_constraints = 0;
763     /* Check name constraints for all certificates */
764     for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
765         X509 *x = sk_X509_value(ctx->chain, i);
766         /* Ignore self issued certs unless last in chain */
767         if (i && (x->ex_flags & EXFLAG_SI))
768             continue;
769         /*
770          * Check against constraints for all certificates higher in chain
771          * including trust anchor. Trust anchor not strictly speaking needed
772          * but if it includes constraints it is to be assumed it expects them
773          * to be obeyed.
774          */
775         for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
776             NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
777             if (nc) {
778                 has_name_constraints = 1;
779                 rv = NAME_CONSTRAINTS_check(x, nc);
780                 switch (rv) {
781                 case X509_V_OK:
782                     continue;
783                 case X509_V_ERR_OUT_OF_MEM:
784                     ctx->error = rv;
785                     return 0;
786                 default:
787                     ctx->error = rv;
788                     ctx->error_depth = i;
789                     ctx->current_cert = x;
790                     if (!ctx->verify_cb(0, ctx))
791                         return 0;
792                     break;
793                 }
794             }
795         }
796     }
797 
798     /* Name constraints do not match against the common name, but
799      * |X509_check_host| still implements the legacy behavior where, on
800      * certificates lacking a SAN list, DNS-like names in the common name are
801      * checked instead.
802      *
803      * While we could apply the name constraints to the common name, name
804      * constraints are rare enough that can hold such certificates to a higher
805      * standard. Note this does not make "DNS-like" heuristic failures any
806      * worse. A decorative common-name misidentified as a DNS name would fail
807      * the name constraint anyway. */
808     X509 *leaf = sk_X509_value(ctx->chain, 0);
809     if (has_name_constraints && leaf->altname == NULL) {
810         rv = reject_dns_name_in_common_name(leaf);
811         switch (rv) {
812         case X509_V_OK:
813             break;
814         case X509_V_ERR_OUT_OF_MEM:
815             ctx->error = rv;
816             return 0;
817         default:
818             ctx->error = rv;
819             ctx->error_depth = i;
820             ctx->current_cert = leaf;
821             if (!ctx->verify_cb(0, ctx))
822                 return 0;
823             break;
824         }
825     }
826 
827     return 1;
828 }
829 
check_id_error(X509_STORE_CTX * ctx,int errcode)830 static int check_id_error(X509_STORE_CTX *ctx, int errcode)
831 {
832     ctx->error = errcode;
833     ctx->current_cert = ctx->cert;
834     ctx->error_depth = 0;
835     return ctx->verify_cb(0, ctx);
836 }
837 
check_hosts(X509 * x,X509_VERIFY_PARAM * param)838 static int check_hosts(X509 *x, X509_VERIFY_PARAM *param)
839 {
840     size_t i;
841     size_t n = sk_OPENSSL_STRING_num(param->hosts);
842     char *name;
843 
844     if (param->peername != NULL) {
845         OPENSSL_free(param->peername);
846         param->peername = NULL;
847     }
848     for (i = 0; i < n; ++i) {
849         name = sk_OPENSSL_STRING_value(param->hosts, i);
850         if (X509_check_host(x, name, strlen(name), param->hostflags,
851                             &param->peername) > 0)
852             return 1;
853     }
854     return n == 0;
855 }
856 
check_id(X509_STORE_CTX * ctx)857 static int check_id(X509_STORE_CTX *ctx)
858 {
859     X509_VERIFY_PARAM *vpm = ctx->param;
860     X509 *x = ctx->cert;
861     if (vpm->poison) {
862         if (!check_id_error(ctx, X509_V_ERR_INVALID_CALL))
863             return 0;
864     }
865     if (vpm->hosts && check_hosts(x, vpm) <= 0) {
866         if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
867             return 0;
868     }
869     if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) {
870         if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
871             return 0;
872     }
873     if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) {
874         if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
875             return 0;
876     }
877     return 1;
878 }
879 
check_trust(X509_STORE_CTX * ctx)880 static int check_trust(X509_STORE_CTX *ctx)
881 {
882     size_t i;
883     int ok;
884     X509 *x = NULL;
885     int (*cb) (int xok, X509_STORE_CTX *xctx);
886     cb = ctx->verify_cb;
887     /* Check all trusted certificates in chain */
888     for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
889         x = sk_X509_value(ctx->chain, i);
890         ok = X509_check_trust(x, ctx->param->trust, 0);
891         /* If explicitly trusted return trusted */
892         if (ok == X509_TRUST_TRUSTED)
893             return X509_TRUST_TRUSTED;
894         /*
895          * If explicitly rejected notify callback and reject if not
896          * overridden.
897          */
898         if (ok == X509_TRUST_REJECTED) {
899             ctx->error_depth = i;
900             ctx->current_cert = x;
901             ctx->error = X509_V_ERR_CERT_REJECTED;
902             ok = cb(0, ctx);
903             if (!ok)
904                 return X509_TRUST_REJECTED;
905         }
906     }
907     /*
908      * If we accept partial chains and have at least one trusted certificate
909      * return success.
910      */
911     if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
912         X509 *mx;
913         if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain))
914             return X509_TRUST_TRUSTED;
915         x = sk_X509_value(ctx->chain, 0);
916         mx = lookup_cert_match(ctx, x);
917         if (mx) {
918             (void)sk_X509_set(ctx->chain, 0, mx);
919             X509_free(x);
920             ctx->last_untrusted = 0;
921             return X509_TRUST_TRUSTED;
922         }
923     }
924 
925     /*
926      * If no trusted certs in chain at all return untrusted and allow
927      * standard (no issuer cert) etc errors to be indicated.
928      */
929     return X509_TRUST_UNTRUSTED;
930 }
931 
check_revocation(X509_STORE_CTX * ctx)932 static int check_revocation(X509_STORE_CTX *ctx)
933 {
934     int i, last, ok;
935     if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
936         return 1;
937     if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
938         last = sk_X509_num(ctx->chain) - 1;
939     else {
940         /* If checking CRL paths this isn't the EE certificate */
941         if (ctx->parent)
942             return 1;
943         last = 0;
944     }
945     for (i = 0; i <= last; i++) {
946         ctx->error_depth = i;
947         ok = check_cert(ctx);
948         if (!ok)
949             return ok;
950     }
951     return 1;
952 }
953 
check_cert(X509_STORE_CTX * ctx)954 static int check_cert(X509_STORE_CTX *ctx)
955 {
956     X509_CRL *crl = NULL, *dcrl = NULL;
957     X509 *x;
958     int ok = 0, cnum;
959     unsigned int last_reasons;
960     cnum = ctx->error_depth;
961     x = sk_X509_value(ctx->chain, cnum);
962     ctx->current_cert = x;
963     ctx->current_issuer = NULL;
964     ctx->current_crl_score = 0;
965     ctx->current_reasons = 0;
966     while (ctx->current_reasons != CRLDP_ALL_REASONS) {
967         last_reasons = ctx->current_reasons;
968         /* Try to retrieve relevant CRL */
969         if (ctx->get_crl)
970             ok = ctx->get_crl(ctx, &crl, x);
971         else
972             ok = get_crl_delta(ctx, &crl, &dcrl, x);
973         /*
974          * If error looking up CRL, nothing we can do except notify callback
975          */
976         if (!ok) {
977             ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
978             ok = ctx->verify_cb(0, ctx);
979             goto err;
980         }
981         ctx->current_crl = crl;
982         ok = ctx->check_crl(ctx, crl);
983         if (!ok)
984             goto err;
985 
986         if (dcrl) {
987             ok = ctx->check_crl(ctx, dcrl);
988             if (!ok)
989                 goto err;
990             ok = ctx->cert_crl(ctx, dcrl, x);
991             if (!ok)
992                 goto err;
993         } else
994             ok = 1;
995 
996         /* Don't look in full CRL if delta reason is removefromCRL */
997         if (ok != 2) {
998             ok = ctx->cert_crl(ctx, crl, x);
999             if (!ok)
1000                 goto err;
1001         }
1002 
1003         X509_CRL_free(crl);
1004         X509_CRL_free(dcrl);
1005         crl = NULL;
1006         dcrl = NULL;
1007         /*
1008          * If reasons not updated we wont get anywhere by another iteration,
1009          * so exit loop.
1010          */
1011         if (last_reasons == ctx->current_reasons) {
1012             ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
1013             ok = ctx->verify_cb(0, ctx);
1014             goto err;
1015         }
1016     }
1017  err:
1018     X509_CRL_free(crl);
1019     X509_CRL_free(dcrl);
1020 
1021     ctx->current_crl = NULL;
1022     return ok;
1023 
1024 }
1025 
1026 /* Check CRL times against values in X509_STORE_CTX */
1027 
check_crl_time(X509_STORE_CTX * ctx,X509_CRL * crl,int notify)1028 static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
1029 {
1030     time_t *ptime;
1031     int i;
1032     if (notify)
1033         ctx->current_crl = crl;
1034     if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1035         ptime = &ctx->param->check_time;
1036     else
1037         ptime = NULL;
1038 
1039     i = X509_cmp_time(X509_CRL_get0_lastUpdate(crl), ptime);
1040     if (i == 0) {
1041         if (!notify)
1042             return 0;
1043         ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
1044         if (!ctx->verify_cb(0, ctx))
1045             return 0;
1046     }
1047 
1048     if (i > 0) {
1049         if (!notify)
1050             return 0;
1051         ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
1052         if (!ctx->verify_cb(0, ctx))
1053             return 0;
1054     }
1055 
1056     if (X509_CRL_get0_nextUpdate(crl)) {
1057         i = X509_cmp_time(X509_CRL_get0_nextUpdate(crl), ptime);
1058 
1059         if (i == 0) {
1060             if (!notify)
1061                 return 0;
1062             ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
1063             if (!ctx->verify_cb(0, ctx))
1064                 return 0;
1065         }
1066         /* Ignore expiry of base CRL is delta is valid */
1067         if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
1068             if (!notify)
1069                 return 0;
1070             ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
1071             if (!ctx->verify_cb(0, ctx))
1072                 return 0;
1073         }
1074     }
1075 
1076     if (notify)
1077         ctx->current_crl = NULL;
1078 
1079     return 1;
1080 }
1081 
get_crl_sk(X509_STORE_CTX * ctx,X509_CRL ** pcrl,X509_CRL ** pdcrl,X509 ** pissuer,int * pscore,unsigned int * preasons,STACK_OF (X509_CRL)* crls)1082 static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
1083                       X509 **pissuer, int *pscore, unsigned int *preasons,
1084                       STACK_OF(X509_CRL) *crls)
1085 {
1086     int crl_score, best_score = *pscore;
1087     size_t i;
1088     unsigned int reasons, best_reasons = 0;
1089     X509 *x = ctx->current_cert;
1090     X509_CRL *crl, *best_crl = NULL;
1091     X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
1092 
1093     for (i = 0; i < sk_X509_CRL_num(crls); i++) {
1094         crl = sk_X509_CRL_value(crls, i);
1095         reasons = *preasons;
1096         crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
1097         if (crl_score < best_score || crl_score == 0)
1098             continue;
1099         /* If current CRL is equivalent use it if it is newer */
1100         if (crl_score == best_score && best_crl != NULL) {
1101             int day, sec;
1102             if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl),
1103                                X509_CRL_get0_lastUpdate(crl)) == 0)
1104                 continue;
1105             /*
1106              * ASN1_TIME_diff never returns inconsistent signs for |day|
1107              * and |sec|.
1108              */
1109             if (day <= 0 && sec <= 0)
1110                 continue;
1111         }
1112         best_crl = crl;
1113         best_crl_issuer = crl_issuer;
1114         best_score = crl_score;
1115         best_reasons = reasons;
1116     }
1117 
1118     if (best_crl) {
1119         if (*pcrl)
1120             X509_CRL_free(*pcrl);
1121         *pcrl = best_crl;
1122         *pissuer = best_crl_issuer;
1123         *pscore = best_score;
1124         *preasons = best_reasons;
1125         X509_CRL_up_ref(best_crl);
1126         if (*pdcrl) {
1127             X509_CRL_free(*pdcrl);
1128             *pdcrl = NULL;
1129         }
1130         get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
1131     }
1132 
1133     if (best_score >= CRL_SCORE_VALID)
1134         return 1;
1135 
1136     return 0;
1137 }
1138 
1139 /*
1140  * Compare two CRL extensions for delta checking purposes. They should be
1141  * both present or both absent. If both present all fields must be identical.
1142  */
1143 
crl_extension_match(X509_CRL * a,X509_CRL * b,int nid)1144 static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
1145 {
1146     ASN1_OCTET_STRING *exta, *extb;
1147     int i;
1148     i = X509_CRL_get_ext_by_NID(a, nid, -1);
1149     if (i >= 0) {
1150         /* Can't have multiple occurrences */
1151         if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
1152             return 0;
1153         exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
1154     } else
1155         exta = NULL;
1156 
1157     i = X509_CRL_get_ext_by_NID(b, nid, -1);
1158 
1159     if (i >= 0) {
1160 
1161         if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
1162             return 0;
1163         extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
1164     } else
1165         extb = NULL;
1166 
1167     if (!exta && !extb)
1168         return 1;
1169 
1170     if (!exta || !extb)
1171         return 0;
1172 
1173     if (ASN1_OCTET_STRING_cmp(exta, extb))
1174         return 0;
1175 
1176     return 1;
1177 }
1178 
1179 /* See if a base and delta are compatible */
1180 
check_delta_base(X509_CRL * delta,X509_CRL * base)1181 static int check_delta_base(X509_CRL *delta, X509_CRL *base)
1182 {
1183     /* Delta CRL must be a delta */
1184     if (!delta->base_crl_number)
1185         return 0;
1186     /* Base must have a CRL number */
1187     if (!base->crl_number)
1188         return 0;
1189     /* Issuer names must match */
1190     if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
1191         return 0;
1192     /* AKID and IDP must match */
1193     if (!crl_extension_match(delta, base, NID_authority_key_identifier))
1194         return 0;
1195     if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
1196         return 0;
1197     /* Delta CRL base number must not exceed Full CRL number. */
1198     if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
1199         return 0;
1200     /* Delta CRL number must exceed full CRL number */
1201     if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
1202         return 1;
1203     return 0;
1204 }
1205 
1206 /*
1207  * For a given base CRL find a delta... maybe extend to delta scoring or
1208  * retrieve a chain of deltas...
1209  */
1210 
get_delta_sk(X509_STORE_CTX * ctx,X509_CRL ** dcrl,int * pscore,X509_CRL * base,STACK_OF (X509_CRL)* crls)1211 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
1212                          X509_CRL *base, STACK_OF(X509_CRL) *crls)
1213 {
1214     X509_CRL *delta;
1215     size_t i;
1216     if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
1217         return;
1218     if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
1219         return;
1220     for (i = 0; i < sk_X509_CRL_num(crls); i++) {
1221         delta = sk_X509_CRL_value(crls, i);
1222         if (check_delta_base(delta, base)) {
1223             if (check_crl_time(ctx, delta, 0))
1224                 *pscore |= CRL_SCORE_TIME_DELTA;
1225             X509_CRL_up_ref(delta);
1226             *dcrl = delta;
1227             return;
1228         }
1229     }
1230     *dcrl = NULL;
1231 }
1232 
1233 /*
1234  * For a given CRL return how suitable it is for the supplied certificate
1235  * 'x'. The return value is a mask of several criteria. If the issuer is not
1236  * the certificate issuer this is returned in *pissuer. The reasons mask is
1237  * also used to determine if the CRL is suitable: if no new reasons the CRL
1238  * is rejected, otherwise reasons is updated.
1239  */
1240 
get_crl_score(X509_STORE_CTX * ctx,X509 ** pissuer,unsigned int * preasons,X509_CRL * crl,X509 * x)1241 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
1242                          unsigned int *preasons, X509_CRL *crl, X509 *x)
1243 {
1244 
1245     int crl_score = 0;
1246     unsigned int tmp_reasons = *preasons, crl_reasons;
1247 
1248     /* First see if we can reject CRL straight away */
1249 
1250     /* Invalid IDP cannot be processed */
1251     if (crl->idp_flags & IDP_INVALID)
1252         return 0;
1253     /* Reason codes or indirect CRLs need extended CRL support */
1254     if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
1255         if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
1256             return 0;
1257     } else if (crl->idp_flags & IDP_REASONS) {
1258         /* If no new reasons reject */
1259         if (!(crl->idp_reasons & ~tmp_reasons))
1260             return 0;
1261     }
1262     /* Don't process deltas at this stage */
1263     else if (crl->base_crl_number)
1264         return 0;
1265     /* If issuer name doesn't match certificate need indirect CRL */
1266     if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
1267         if (!(crl->idp_flags & IDP_INDIRECT))
1268             return 0;
1269     } else
1270         crl_score |= CRL_SCORE_ISSUER_NAME;
1271 
1272     if (!(crl->flags & EXFLAG_CRITICAL))
1273         crl_score |= CRL_SCORE_NOCRITICAL;
1274 
1275     /* Check expiry */
1276     if (check_crl_time(ctx, crl, 0))
1277         crl_score |= CRL_SCORE_TIME;
1278 
1279     /* Check authority key ID and locate certificate issuer */
1280     crl_akid_check(ctx, crl, pissuer, &crl_score);
1281 
1282     /* If we can't locate certificate issuer at this point forget it */
1283 
1284     if (!(crl_score & CRL_SCORE_AKID))
1285         return 0;
1286 
1287     /* Check cert for matching CRL distribution points */
1288 
1289     if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
1290         /* If no new reasons reject */
1291         if (!(crl_reasons & ~tmp_reasons))
1292             return 0;
1293         tmp_reasons |= crl_reasons;
1294         crl_score |= CRL_SCORE_SCOPE;
1295     }
1296 
1297     *preasons = tmp_reasons;
1298 
1299     return crl_score;
1300 
1301 }
1302 
crl_akid_check(X509_STORE_CTX * ctx,X509_CRL * crl,X509 ** pissuer,int * pcrl_score)1303 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
1304                            X509 **pissuer, int *pcrl_score)
1305 {
1306     X509 *crl_issuer = NULL;
1307     X509_NAME *cnm = X509_CRL_get_issuer(crl);
1308     int cidx = ctx->error_depth;
1309     size_t i;
1310 
1311     if ((size_t)cidx != sk_X509_num(ctx->chain) - 1)
1312         cidx++;
1313 
1314     crl_issuer = sk_X509_value(ctx->chain, cidx);
1315 
1316     if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1317         if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1318             *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
1319             *pissuer = crl_issuer;
1320             return;
1321         }
1322     }
1323 
1324     for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) {
1325         crl_issuer = sk_X509_value(ctx->chain, cidx);
1326         if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1327             continue;
1328         if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1329             *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
1330             *pissuer = crl_issuer;
1331             return;
1332         }
1333     }
1334 
1335     /* Anything else needs extended CRL support */
1336 
1337     if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1338         return;
1339 
1340     /*
1341      * Otherwise the CRL issuer is not on the path. Look for it in the set of
1342      * untrusted certificates.
1343      */
1344     for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1345         crl_issuer = sk_X509_value(ctx->untrusted, i);
1346         if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1347             continue;
1348         if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1349             *pissuer = crl_issuer;
1350             *pcrl_score |= CRL_SCORE_AKID;
1351             return;
1352         }
1353     }
1354 
1355     for (i = 0; i < sk_X509_num(ctx->ctx->additional_untrusted); i++) {
1356         crl_issuer = sk_X509_value(ctx->ctx->additional_untrusted, i);
1357         if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1358             continue;
1359         if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1360             *pissuer = crl_issuer;
1361             *pcrl_score |= CRL_SCORE_AKID;
1362             return;
1363         }
1364     }
1365 }
1366 
1367 /*
1368  * Check the path of a CRL issuer certificate. This creates a new
1369  * X509_STORE_CTX and populates it with most of the parameters from the
1370  * parent. This could be optimised somewhat since a lot of path checking will
1371  * be duplicated by the parent, but this will rarely be used in practice.
1372  */
1373 
check_crl_path(X509_STORE_CTX * ctx,X509 * x)1374 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1375 {
1376     X509_STORE_CTX crl_ctx;
1377     int ret;
1378     /* Don't allow recursive CRL path validation */
1379     if (ctx->parent)
1380         return 0;
1381     if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
1382         return -1;
1383 
1384     crl_ctx.crls = ctx->crls;
1385     /* Copy verify params across */
1386     X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1387 
1388     crl_ctx.parent = ctx;
1389     crl_ctx.verify_cb = ctx->verify_cb;
1390 
1391     /* Verify CRL issuer */
1392     ret = X509_verify_cert(&crl_ctx);
1393 
1394     if (ret <= 0)
1395         goto err;
1396 
1397     /* Check chain is acceptable */
1398 
1399     ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1400  err:
1401     X509_STORE_CTX_cleanup(&crl_ctx);
1402     return ret;
1403 }
1404 
1405 /*
1406  * RFC 3280 says nothing about the relationship between CRL path and
1407  * certificate path, which could lead to situations where a certificate could
1408  * be revoked or validated by a CA not authorised to do so. RFC 5280 is more
1409  * strict and states that the two paths must end in the same trust anchor,
1410  * though some discussions remain... until this is resolved we use the
1411  * RFC 5280 version
1412  */
1413 
check_crl_chain(X509_STORE_CTX * ctx,STACK_OF (X509)* cert_path,STACK_OF (X509)* crl_path)1414 static int check_crl_chain(X509_STORE_CTX *ctx,
1415                            STACK_OF(X509) *cert_path,
1416                            STACK_OF(X509) *crl_path)
1417 {
1418     X509 *cert_ta, *crl_ta;
1419     cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1420     crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1421     if (!X509_cmp(cert_ta, crl_ta))
1422         return 1;
1423     return 0;
1424 }
1425 
1426 /*
1427  * Check for match between two dist point names: three separate cases. 1.
1428  * Both are relative names and compare X509_NAME types. 2. One full, one
1429  * relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and
1430  * compare two GENERAL_NAMES. 4. One is NULL: automatic match.
1431  */
1432 
idp_check_dp(DIST_POINT_NAME * a,DIST_POINT_NAME * b)1433 static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1434 {
1435     X509_NAME *nm = NULL;
1436     GENERAL_NAMES *gens = NULL;
1437     GENERAL_NAME *gena, *genb;
1438     size_t i, j;
1439     if (!a || !b)
1440         return 1;
1441     if (a->type == 1) {
1442         if (!a->dpname)
1443             return 0;
1444         /* Case 1: two X509_NAME */
1445         if (b->type == 1) {
1446             if (!b->dpname)
1447                 return 0;
1448             if (!X509_NAME_cmp(a->dpname, b->dpname))
1449                 return 1;
1450             else
1451                 return 0;
1452         }
1453         /* Case 2: set name and GENERAL_NAMES appropriately */
1454         nm = a->dpname;
1455         gens = b->name.fullname;
1456     } else if (b->type == 1) {
1457         if (!b->dpname)
1458             return 0;
1459         /* Case 2: set name and GENERAL_NAMES appropriately */
1460         gens = a->name.fullname;
1461         nm = b->dpname;
1462     }
1463 
1464     /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1465     if (nm) {
1466         for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1467             gena = sk_GENERAL_NAME_value(gens, i);
1468             if (gena->type != GEN_DIRNAME)
1469                 continue;
1470             if (!X509_NAME_cmp(nm, gena->d.directoryName))
1471                 return 1;
1472         }
1473         return 0;
1474     }
1475 
1476     /* Else case 3: two GENERAL_NAMES */
1477 
1478     for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1479         gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1480         for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1481             genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1482             if (!GENERAL_NAME_cmp(gena, genb))
1483                 return 1;
1484         }
1485     }
1486 
1487     return 0;
1488 
1489 }
1490 
crldp_check_crlissuer(DIST_POINT * dp,X509_CRL * crl,int crl_score)1491 static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1492 {
1493     size_t i;
1494     X509_NAME *nm = X509_CRL_get_issuer(crl);
1495     /* If no CRLissuer return is successful iff don't need a match */
1496     if (!dp->CRLissuer)
1497         return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
1498     for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1499         GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1500         if (gen->type != GEN_DIRNAME)
1501             continue;
1502         if (!X509_NAME_cmp(gen->d.directoryName, nm))
1503             return 1;
1504     }
1505     return 0;
1506 }
1507 
1508 /* Check CRLDP and IDP */
1509 
crl_crldp_check(X509 * x,X509_CRL * crl,int crl_score,unsigned int * preasons)1510 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
1511                            unsigned int *preasons)
1512 {
1513     size_t i;
1514     if (crl->idp_flags & IDP_ONLYATTR)
1515         return 0;
1516     if (x->ex_flags & EXFLAG_CA) {
1517         if (crl->idp_flags & IDP_ONLYUSER)
1518             return 0;
1519     } else {
1520         if (crl->idp_flags & IDP_ONLYCA)
1521             return 0;
1522     }
1523     *preasons = crl->idp_reasons;
1524     for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1525         DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1526         if (crldp_check_crlissuer(dp, crl, crl_score)) {
1527             if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1528                 *preasons &= dp->dp_reasons;
1529                 return 1;
1530             }
1531         }
1532     }
1533     if ((!crl->idp || !crl->idp->distpoint)
1534         && (crl_score & CRL_SCORE_ISSUER_NAME))
1535         return 1;
1536     return 0;
1537 }
1538 
1539 /*
1540  * Retrieve CRL corresponding to current certificate. If deltas enabled try
1541  * to find a delta CRL too
1542  */
1543 
get_crl_delta(X509_STORE_CTX * ctx,X509_CRL ** pcrl,X509_CRL ** pdcrl,X509 * x)1544 static int get_crl_delta(X509_STORE_CTX *ctx,
1545                          X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1546 {
1547     int ok;
1548     X509 *issuer = NULL;
1549     int crl_score = 0;
1550     unsigned int reasons;
1551     X509_CRL *crl = NULL, *dcrl = NULL;
1552     STACK_OF(X509_CRL) *skcrl;
1553     X509_NAME *nm = X509_get_issuer_name(x);
1554     reasons = ctx->current_reasons;
1555     ok = get_crl_sk(ctx, &crl, &dcrl,
1556                     &issuer, &crl_score, &reasons, ctx->crls);
1557 
1558     if (ok)
1559         goto done;
1560 
1561     /* Lookup CRLs from store */
1562 
1563     skcrl = ctx->lookup_crls(ctx, nm);
1564 
1565     /* If no CRLs found and a near match from get_crl_sk use that */
1566     if (!skcrl && crl)
1567         goto done;
1568 
1569     get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1570 
1571     sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1572 
1573  done:
1574 
1575     /* If we got any kind of CRL use it and return success */
1576     if (crl) {
1577         ctx->current_issuer = issuer;
1578         ctx->current_crl_score = crl_score;
1579         ctx->current_reasons = reasons;
1580         *pcrl = crl;
1581         *pdcrl = dcrl;
1582         return 1;
1583     }
1584 
1585     return 0;
1586 }
1587 
1588 /* Check CRL validity */
check_crl(X509_STORE_CTX * ctx,X509_CRL * crl)1589 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1590 {
1591     X509 *issuer = NULL;
1592     EVP_PKEY *ikey = NULL;
1593     int ok = 0, chnum, cnum;
1594     cnum = ctx->error_depth;
1595     chnum = sk_X509_num(ctx->chain) - 1;
1596     /* if we have an alternative CRL issuer cert use that */
1597     if (ctx->current_issuer)
1598         issuer = ctx->current_issuer;
1599 
1600     /*
1601      * Else find CRL issuer: if not last certificate then issuer is next
1602      * certificate in chain.
1603      */
1604     else if (cnum < chnum)
1605         issuer = sk_X509_value(ctx->chain, cnum + 1);
1606     else {
1607         issuer = sk_X509_value(ctx->chain, chnum);
1608         /* If not self signed, can't check signature */
1609         if (!ctx->check_issued(ctx, issuer, issuer)) {
1610             ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1611             ok = ctx->verify_cb(0, ctx);
1612             if (!ok)
1613                 goto err;
1614         }
1615     }
1616 
1617     if (issuer) {
1618         /*
1619          * Skip most tests for deltas because they have already been done
1620          */
1621         if (!crl->base_crl_number) {
1622             /* Check for cRLSign bit if keyUsage present */
1623             if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1624                 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1625                 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1626                 ok = ctx->verify_cb(0, ctx);
1627                 if (!ok)
1628                     goto err;
1629             }
1630 
1631             if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1632                 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1633                 ok = ctx->verify_cb(0, ctx);
1634                 if (!ok)
1635                     goto err;
1636             }
1637 
1638             if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1639                 if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
1640                     ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1641                     ok = ctx->verify_cb(0, ctx);
1642                     if (!ok)
1643                         goto err;
1644                 }
1645             }
1646 
1647             if (crl->idp_flags & IDP_INVALID) {
1648                 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1649                 ok = ctx->verify_cb(0, ctx);
1650                 if (!ok)
1651                     goto err;
1652             }
1653 
1654         }
1655 
1656         if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1657             ok = check_crl_time(ctx, crl, 1);
1658             if (!ok)
1659                 goto err;
1660         }
1661 
1662         /* Attempt to get issuer certificate public key */
1663         ikey = X509_get_pubkey(issuer);
1664 
1665         if (!ikey) {
1666             ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1667             ok = ctx->verify_cb(0, ctx);
1668             if (!ok)
1669                 goto err;
1670         } else {
1671             int rv;
1672             rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
1673             if (rv != X509_V_OK) {
1674                 ctx->error = rv;
1675                 ok = ctx->verify_cb(0, ctx);
1676                 if (!ok)
1677                     goto err;
1678             }
1679             /* Verify CRL signature */
1680             if (X509_CRL_verify(crl, ikey) <= 0) {
1681                 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1682                 ok = ctx->verify_cb(0, ctx);
1683                 if (!ok)
1684                     goto err;
1685             }
1686         }
1687     }
1688 
1689     ok = 1;
1690 
1691  err:
1692     EVP_PKEY_free(ikey);
1693     return ok;
1694 }
1695 
1696 /* Check certificate against CRL */
cert_crl(X509_STORE_CTX * ctx,X509_CRL * crl,X509 * x)1697 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1698 {
1699     int ok;
1700     X509_REVOKED *rev;
1701     /*
1702      * The rules changed for this... previously if a CRL contained unhandled
1703      * critical extensions it could still be used to indicate a certificate
1704      * was revoked. This has since been changed since critical extension can
1705      * change the meaning of CRL entries.
1706      */
1707     if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
1708         && (crl->flags & EXFLAG_CRITICAL)) {
1709         ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1710         ok = ctx->verify_cb(0, ctx);
1711         if (!ok)
1712             return 0;
1713     }
1714     /*
1715      * Look for serial number of certificate in CRL If found make sure reason
1716      * is not removeFromCRL.
1717      */
1718     if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1719         if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1720             return 2;
1721         ctx->error = X509_V_ERR_CERT_REVOKED;
1722         ok = ctx->verify_cb(0, ctx);
1723         if (!ok)
1724             return 0;
1725     }
1726 
1727     return 1;
1728 }
1729 
check_policy(X509_STORE_CTX * ctx)1730 static int check_policy(X509_STORE_CTX *ctx)
1731 {
1732     int ret;
1733     if (ctx->parent)
1734         return 1;
1735     ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1736                             ctx->param->policies, ctx->param->flags);
1737     if (ret == 0) {
1738         OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
1739         ctx->error = X509_V_ERR_OUT_OF_MEM;
1740         return 0;
1741     }
1742     /* Invalid or inconsistent extensions */
1743     if (ret == -1) {
1744         /*
1745          * Locate certificates with bad extensions and notify callback.
1746          */
1747         X509 *x;
1748         size_t i;
1749         for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1750             x = sk_X509_value(ctx->chain, i);
1751             if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1752                 continue;
1753             ctx->current_cert = x;
1754             ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1755             if (!ctx->verify_cb(0, ctx))
1756                 return 0;
1757         }
1758         return 1;
1759     }
1760     if (ret == -2) {
1761         ctx->current_cert = NULL;
1762         ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1763         return ctx->verify_cb(0, ctx);
1764     }
1765 
1766     if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1767         ctx->current_cert = NULL;
1768         /*
1769          * Verification errors need to be "sticky", a callback may have allowed
1770          * an SSL handshake to continue despite an error, and we must then
1771          * remain in an error state.  Therefore, we MUST NOT clear earlier
1772          * verification errors by setting the error to X509_V_OK.
1773          */
1774         if (!ctx->verify_cb(2, ctx))
1775             return 0;
1776     }
1777 
1778     return 1;
1779 }
1780 
check_cert_time(X509_STORE_CTX * ctx,X509 * x)1781 static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
1782 {
1783     time_t *ptime;
1784     int i;
1785 
1786     if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1787         ptime = &ctx->param->check_time;
1788     else
1789         ptime = NULL;
1790 
1791     i = X509_cmp_time(X509_get_notBefore(x), ptime);
1792     if (i == 0) {
1793         ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1794         ctx->current_cert = x;
1795         if (!ctx->verify_cb(0, ctx))
1796             return 0;
1797     }
1798 
1799     if (i > 0) {
1800         ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1801         ctx->current_cert = x;
1802         if (!ctx->verify_cb(0, ctx))
1803             return 0;
1804     }
1805 
1806     i = X509_cmp_time(X509_get_notAfter(x), ptime);
1807     if (i == 0) {
1808         ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1809         ctx->current_cert = x;
1810         if (!ctx->verify_cb(0, ctx))
1811             return 0;
1812     }
1813 
1814     if (i < 0) {
1815         ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1816         ctx->current_cert = x;
1817         if (!ctx->verify_cb(0, ctx))
1818             return 0;
1819     }
1820 
1821     return 1;
1822 }
1823 
internal_verify(X509_STORE_CTX * ctx)1824 static int internal_verify(X509_STORE_CTX *ctx)
1825 {
1826     int ok = 0, n;
1827     X509 *xs, *xi;
1828     EVP_PKEY *pkey = NULL;
1829     int (*cb) (int xok, X509_STORE_CTX *xctx);
1830 
1831     cb = ctx->verify_cb;
1832 
1833     n = sk_X509_num(ctx->chain);
1834     ctx->error_depth = n - 1;
1835     n--;
1836     xi = sk_X509_value(ctx->chain, n);
1837 
1838     if (ctx->check_issued(ctx, xi, xi))
1839         xs = xi;
1840     else {
1841         if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
1842             xs = xi;
1843             goto check_cert;
1844         }
1845         if (n <= 0) {
1846             ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1847             ctx->current_cert = xi;
1848             ok = cb(0, ctx);
1849             goto end;
1850         } else {
1851             n--;
1852             ctx->error_depth = n;
1853             xs = sk_X509_value(ctx->chain, n);
1854         }
1855     }
1856 
1857 /*      ctx->error=0;  not needed */
1858     while (n >= 0) {
1859         ctx->error_depth = n;
1860 
1861         /*
1862          * Skip signature check for self signed certificates unless
1863          * explicitly asked for. It doesn't add any security and just wastes
1864          * time.
1865          */
1866         if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) {
1867             if ((pkey = X509_get_pubkey(xi)) == NULL) {
1868                 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1869                 ctx->current_cert = xi;
1870                 ok = (*cb) (0, ctx);
1871                 if (!ok)
1872                     goto end;
1873             } else if (X509_verify(xs, pkey) <= 0) {
1874                 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1875                 ctx->current_cert = xs;
1876                 ok = (*cb) (0, ctx);
1877                 if (!ok) {
1878                     EVP_PKEY_free(pkey);
1879                     goto end;
1880                 }
1881             }
1882             EVP_PKEY_free(pkey);
1883             pkey = NULL;
1884         }
1885 
1886  check_cert:
1887         ok = check_cert_time(ctx, xs);
1888         if (!ok)
1889             goto end;
1890 
1891         /* The last error (if any) is still in the error value */
1892         ctx->current_issuer = xi;
1893         ctx->current_cert = xs;
1894         ok = (*cb) (1, ctx);
1895         if (!ok)
1896             goto end;
1897 
1898         n--;
1899         if (n >= 0) {
1900             xi = xs;
1901             xs = sk_X509_value(ctx->chain, n);
1902         }
1903     }
1904     ok = 1;
1905  end:
1906     return ok;
1907 }
1908 
X509_cmp_current_time(const ASN1_TIME * ctm)1909 int X509_cmp_current_time(const ASN1_TIME *ctm)
1910 {
1911     return X509_cmp_time(ctm, NULL);
1912 }
1913 
X509_cmp_time(const ASN1_TIME * ctm,time_t * cmp_time)1914 int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
1915 {
1916     static const size_t utctime_length = sizeof("YYMMDDHHMMSSZ") - 1;
1917     static const size_t generalizedtime_length = sizeof("YYYYMMDDHHMMSSZ") - 1;
1918     ASN1_TIME *asn1_cmp_time = NULL;
1919     int i, day, sec, ret = 0;
1920 
1921     /*
1922      * Note that ASN.1 allows much more slack in the time format than RFC 5280.
1923      * In RFC 5280, the representation is fixed:
1924      * UTCTime: YYMMDDHHMMSSZ
1925      * GeneralizedTime: YYYYMMDDHHMMSSZ
1926      *
1927      * We do NOT currently enforce the following RFC 5280 requirement:
1928      * "CAs conforming to this profile MUST always encode certificate
1929      *  validity dates through the year 2049 as UTCTime; certificate validity
1930      *  dates in 2050 or later MUST be encoded as GeneralizedTime."
1931      */
1932     switch (ctm->type) {
1933     case V_ASN1_UTCTIME:
1934         if (ctm->length != (int)(utctime_length))
1935             return 0;
1936         break;
1937     case V_ASN1_GENERALIZEDTIME:
1938         if (ctm->length != (int)(generalizedtime_length))
1939             return 0;
1940         break;
1941     default:
1942         return 0;
1943     }
1944 
1945     /**
1946      * Verify the format: the ASN.1 functions we use below allow a more
1947      * flexible format than what's mandated by RFC 5280.
1948      * Digit and date ranges will be verified in the conversion methods.
1949      */
1950     for (i = 0; i < ctm->length - 1; i++) {
1951         if (!isdigit(ctm->data[i]))
1952             return 0;
1953     }
1954     if (ctm->data[ctm->length - 1] != 'Z')
1955         return 0;
1956 
1957     /*
1958      * There is ASN1_UTCTIME_cmp_time_t but no
1959      * ASN1_GENERALIZEDTIME_cmp_time_t or ASN1_TIME_cmp_time_t,
1960      * so we go through ASN.1
1961      */
1962     asn1_cmp_time = X509_time_adj(NULL, 0, cmp_time);
1963     if (asn1_cmp_time == NULL)
1964         goto err;
1965     if (!ASN1_TIME_diff(&day, &sec, ctm, asn1_cmp_time))
1966         goto err;
1967 
1968     /*
1969      * X509_cmp_time comparison is <=.
1970      * The return value 0 is reserved for errors.
1971      */
1972     ret = (day >= 0 && sec >= 0) ? -1 : 1;
1973 
1974  err:
1975     ASN1_TIME_free(asn1_cmp_time);
1976     return ret;
1977 }
1978 
X509_gmtime_adj(ASN1_TIME * s,long offset_sec)1979 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec)
1980 {
1981     return X509_time_adj(s, offset_sec, NULL);
1982 }
1983 
X509_time_adj(ASN1_TIME * s,long offset_sec,time_t * in_tm)1984 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
1985 {
1986     return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1987 }
1988 
X509_time_adj_ex(ASN1_TIME * s,int offset_day,long offset_sec,time_t * in_tm)1989 ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
1990                             int offset_day, long offset_sec, time_t *in_tm)
1991 {
1992     time_t t = 0;
1993 
1994     if (in_tm) {
1995         t = *in_tm;
1996     } else {
1997         time(&t);
1998     }
1999 
2000     return ASN1_TIME_adj(s, t, offset_day, offset_sec);
2001 }
2002 
2003 /* Make a delta CRL as the diff between two full CRLs */
2004 
X509_CRL_diff(X509_CRL * base,X509_CRL * newer,EVP_PKEY * skey,const EVP_MD * md,unsigned int flags)2005 X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
2006                         EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
2007 {
2008     X509_CRL *crl = NULL;
2009     int i;
2010     size_t j;
2011     STACK_OF(X509_REVOKED) *revs = NULL;
2012     /* CRLs can't be delta already */
2013     if (base->base_crl_number || newer->base_crl_number) {
2014         OPENSSL_PUT_ERROR(X509, X509_R_CRL_ALREADY_DELTA);
2015         return NULL;
2016     }
2017     /* Base and new CRL must have a CRL number */
2018     if (!base->crl_number || !newer->crl_number) {
2019         OPENSSL_PUT_ERROR(X509, X509_R_NO_CRL_NUMBER);
2020         return NULL;
2021     }
2022     /* Issuer names must match */
2023     if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
2024         OPENSSL_PUT_ERROR(X509, X509_R_ISSUER_MISMATCH);
2025         return NULL;
2026     }
2027     /* AKID and IDP must match */
2028     if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
2029         OPENSSL_PUT_ERROR(X509, X509_R_AKID_MISMATCH);
2030         return NULL;
2031     }
2032     if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
2033         OPENSSL_PUT_ERROR(X509, X509_R_IDP_MISMATCH);
2034         return NULL;
2035     }
2036     /* Newer CRL number must exceed full CRL number */
2037     if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
2038         OPENSSL_PUT_ERROR(X509, X509_R_NEWER_CRL_NOT_NEWER);
2039         return NULL;
2040     }
2041     /* CRLs must verify */
2042     if (skey && (X509_CRL_verify(base, skey) <= 0 ||
2043                  X509_CRL_verify(newer, skey) <= 0)) {
2044         OPENSSL_PUT_ERROR(X509, X509_R_CRL_VERIFY_FAILURE);
2045         return NULL;
2046     }
2047     /* Create new CRL */
2048     crl = X509_CRL_new();
2049     if (!crl || !X509_CRL_set_version(crl, X509_CRL_VERSION_2))
2050         goto memerr;
2051     /* Set issuer name */
2052     if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
2053         goto memerr;
2054 
2055     if (!X509_CRL_set1_lastUpdate(crl, X509_CRL_get0_lastUpdate(newer)))
2056         goto memerr;
2057     if (!X509_CRL_set1_nextUpdate(crl, X509_CRL_get0_nextUpdate(newer)))
2058         goto memerr;
2059 
2060     /* Set base CRL number: must be critical */
2061 
2062     if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
2063         goto memerr;
2064 
2065     /*
2066      * Copy extensions across from newest CRL to delta: this will set CRL
2067      * number to correct value too.
2068      */
2069 
2070     for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
2071         X509_EXTENSION *ext;
2072         ext = X509_CRL_get_ext(newer, i);
2073         if (!X509_CRL_add_ext(crl, ext, -1))
2074             goto memerr;
2075     }
2076 
2077     /* Go through revoked entries, copying as needed */
2078 
2079     revs = X509_CRL_get_REVOKED(newer);
2080 
2081     for (j = 0; j < sk_X509_REVOKED_num(revs); j++) {
2082         X509_REVOKED *rvn, *rvtmp;
2083         rvn = sk_X509_REVOKED_value(revs, j);
2084         /*
2085          * Add only if not also in base. TODO: need something cleverer here
2086          * for some more complex CRLs covering multiple CAs.
2087          */
2088         if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber)) {
2089             rvtmp = X509_REVOKED_dup(rvn);
2090             if (!rvtmp)
2091                 goto memerr;
2092             if (!X509_CRL_add0_revoked(crl, rvtmp)) {
2093                 X509_REVOKED_free(rvtmp);
2094                 goto memerr;
2095             }
2096         }
2097     }
2098     /* TODO: optionally prune deleted entries */
2099 
2100     if (skey && md && !X509_CRL_sign(crl, skey, md))
2101         goto memerr;
2102 
2103     return crl;
2104 
2105  memerr:
2106     OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
2107     if (crl)
2108         X509_CRL_free(crl);
2109     return NULL;
2110 }
2111 
X509_STORE_CTX_get_ex_new_index(long argl,void * argp,CRYPTO_EX_unused * unused,CRYPTO_EX_dup * dup_unused,CRYPTO_EX_free * free_func)2112 int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
2113                                     CRYPTO_EX_unused * unused,
2114                                     CRYPTO_EX_dup *dup_unused,
2115                                     CRYPTO_EX_free *free_func)
2116 {
2117     /*
2118      * This function is (usually) called only once, by
2119      * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
2120      */
2121     int index;
2122     if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
2123                                  free_func)) {
2124         return -1;
2125     }
2126     return index;
2127 }
2128 
X509_STORE_CTX_set_ex_data(X509_STORE_CTX * ctx,int idx,void * data)2129 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
2130 {
2131     return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
2132 }
2133 
X509_STORE_CTX_get_ex_data(X509_STORE_CTX * ctx,int idx)2134 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
2135 {
2136     return CRYPTO_get_ex_data(&ctx->ex_data, idx);
2137 }
2138 
X509_STORE_CTX_get_error(X509_STORE_CTX * ctx)2139 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
2140 {
2141     return ctx->error;
2142 }
2143 
X509_STORE_CTX_set_error(X509_STORE_CTX * ctx,int err)2144 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
2145 {
2146     ctx->error = err;
2147 }
2148 
X509_STORE_CTX_get_error_depth(X509_STORE_CTX * ctx)2149 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
2150 {
2151     return ctx->error_depth;
2152 }
2153 
X509_STORE_CTX_get_current_cert(X509_STORE_CTX * ctx)2154 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
2155 {
2156     return ctx->current_cert;
2157 }
2158 
STACK_OF(X509)2159 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
2160 {
2161     return ctx->chain;
2162 }
2163 
STACK_OF(X509)2164 STACK_OF(X509) *X509_STORE_CTX_get0_chain(X509_STORE_CTX *ctx)
2165 {
2166     return ctx->chain;
2167 }
2168 
STACK_OF(X509)2169 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
2170 {
2171     if (!ctx->chain)
2172         return NULL;
2173     return X509_chain_up_ref(ctx->chain);
2174 }
2175 
X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX * ctx)2176 X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
2177 {
2178     return ctx->current_issuer;
2179 }
2180 
X509_STORE_CTX_get0_current_crl(X509_STORE_CTX * ctx)2181 X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
2182 {
2183     return ctx->current_crl;
2184 }
2185 
X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX * ctx)2186 X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
2187 {
2188     return ctx->parent;
2189 }
2190 
X509_STORE_CTX_set_cert(X509_STORE_CTX * ctx,X509 * x)2191 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
2192 {
2193     ctx->cert = x;
2194 }
2195 
X509_STORE_CTX_set_chain(X509_STORE_CTX * ctx,STACK_OF (X509)* sk)2196 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2197 {
2198     ctx->untrusted = sk;
2199 }
2200 
STACK_OF(X509)2201 STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx)
2202 {
2203     return ctx->untrusted;
2204 }
2205 
X509_STORE_CTX_set0_crls(X509_STORE_CTX * ctx,STACK_OF (X509_CRL)* sk)2206 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
2207 {
2208     ctx->crls = sk;
2209 }
2210 
X509_STORE_CTX_set_purpose(X509_STORE_CTX * ctx,int purpose)2211 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
2212 {
2213     return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
2214 }
2215 
X509_STORE_CTX_set_trust(X509_STORE_CTX * ctx,int trust)2216 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
2217 {
2218     return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
2219 }
2220 
2221 /*
2222  * This function is used to set the X509_STORE_CTX purpose and trust values.
2223  * This is intended to be used when another structure has its own trust and
2224  * purpose values which (if set) will be inherited by the ctx. If they aren't
2225  * set then we will usually have a default purpose in mind which should then
2226  * be used to set the trust value. An example of this is SSL use: an SSL
2227  * structure will have its own purpose and trust settings which the
2228  * application can set: if they aren't set then we use the default of SSL
2229  * client/server.
2230  */
2231 
X509_STORE_CTX_purpose_inherit(X509_STORE_CTX * ctx,int def_purpose,int purpose,int trust)2232 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
2233                                    int purpose, int trust)
2234 {
2235     int idx;
2236     /* If purpose not set use default */
2237     if (!purpose)
2238         purpose = def_purpose;
2239     /* If we have a purpose then check it is valid */
2240     if (purpose) {
2241         X509_PURPOSE *ptmp;
2242         idx = X509_PURPOSE_get_by_id(purpose);
2243         if (idx == -1) {
2244             OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
2245             return 0;
2246         }
2247         ptmp = X509_PURPOSE_get0(idx);
2248         if (ptmp->trust == X509_TRUST_DEFAULT) {
2249             idx = X509_PURPOSE_get_by_id(def_purpose);
2250             if (idx == -1) {
2251                 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
2252                 return 0;
2253             }
2254             ptmp = X509_PURPOSE_get0(idx);
2255         }
2256         /* If trust not set then get from purpose default */
2257         if (!trust)
2258             trust = ptmp->trust;
2259     }
2260     if (trust) {
2261         idx = X509_TRUST_get_by_id(trust);
2262         if (idx == -1) {
2263             OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID);
2264             return 0;
2265         }
2266     }
2267 
2268     if (purpose && !ctx->param->purpose)
2269         ctx->param->purpose = purpose;
2270     if (trust && !ctx->param->trust)
2271         ctx->param->trust = trust;
2272     return 1;
2273 }
2274 
X509_STORE_CTX_new(void)2275 X509_STORE_CTX *X509_STORE_CTX_new(void)
2276 {
2277     X509_STORE_CTX *ctx;
2278     ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
2279     if (!ctx) {
2280         OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
2281         return NULL;
2282     }
2283     X509_STORE_CTX_zero(ctx);
2284     return ctx;
2285 }
2286 
X509_STORE_CTX_zero(X509_STORE_CTX * ctx)2287 void X509_STORE_CTX_zero(X509_STORE_CTX *ctx)
2288 {
2289     OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
2290 }
2291 
X509_STORE_CTX_free(X509_STORE_CTX * ctx)2292 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
2293 {
2294     if (ctx == NULL) {
2295         return;
2296     }
2297     X509_STORE_CTX_cleanup(ctx);
2298     OPENSSL_free(ctx);
2299 }
2300 
X509_STORE_CTX_init(X509_STORE_CTX * ctx,X509_STORE * store,X509 * x509,STACK_OF (X509)* chain)2301 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
2302                         STACK_OF(X509) *chain)
2303 {
2304     X509_STORE_CTX_zero(ctx);
2305     ctx->ctx = store;
2306     ctx->cert = x509;
2307     ctx->untrusted = chain;
2308 
2309     CRYPTO_new_ex_data(&ctx->ex_data);
2310 
2311     if (store == NULL) {
2312         OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
2313         goto err;
2314     }
2315 
2316     ctx->param = X509_VERIFY_PARAM_new();
2317     if (!ctx->param)
2318         goto err;
2319 
2320     /*
2321      * Inherit callbacks and flags from X509_STORE.
2322      */
2323 
2324     ctx->verify_cb = store->verify_cb;
2325     ctx->cleanup = store->cleanup;
2326 
2327     if (!X509_VERIFY_PARAM_inherit(ctx->param, store->param) ||
2328         !X509_VERIFY_PARAM_inherit(ctx->param,
2329                                    X509_VERIFY_PARAM_lookup("default"))) {
2330         goto err;
2331     }
2332 
2333     if (store->check_issued)
2334         ctx->check_issued = store->check_issued;
2335     else
2336         ctx->check_issued = check_issued;
2337 
2338     if (store->get_issuer)
2339         ctx->get_issuer = store->get_issuer;
2340     else
2341         ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2342 
2343     if (store->verify_cb)
2344         ctx->verify_cb = store->verify_cb;
2345     else
2346         ctx->verify_cb = null_callback;
2347 
2348     if (store->verify)
2349         ctx->verify = store->verify;
2350     else
2351         ctx->verify = internal_verify;
2352 
2353     if (store->check_revocation)
2354         ctx->check_revocation = store->check_revocation;
2355     else
2356         ctx->check_revocation = check_revocation;
2357 
2358     if (store->get_crl)
2359         ctx->get_crl = store->get_crl;
2360     else
2361         ctx->get_crl = NULL;
2362 
2363     if (store->check_crl)
2364         ctx->check_crl = store->check_crl;
2365     else
2366         ctx->check_crl = check_crl;
2367 
2368     if (store->cert_crl)
2369         ctx->cert_crl = store->cert_crl;
2370     else
2371         ctx->cert_crl = cert_crl;
2372 
2373     if (store->lookup_certs)
2374         ctx->lookup_certs = store->lookup_certs;
2375     else
2376         ctx->lookup_certs = X509_STORE_get1_certs;
2377 
2378     if (store->lookup_crls)
2379         ctx->lookup_crls = store->lookup_crls;
2380     else
2381         ctx->lookup_crls = X509_STORE_get1_crls;
2382 
2383     ctx->check_policy = check_policy;
2384 
2385     return 1;
2386 
2387  err:
2388     CRYPTO_free_ex_data(&g_ex_data_class, ctx, &ctx->ex_data);
2389     if (ctx->param != NULL) {
2390         X509_VERIFY_PARAM_free(ctx->param);
2391     }
2392 
2393     OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
2394     OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
2395     return 0;
2396 }
2397 
2398 /*
2399  * Set alternative lookup method: just a STACK of trusted certificates. This
2400  * avoids X509_STORE nastiness where it isn't needed.
2401  */
2402 
X509_STORE_CTX_trusted_stack(X509_STORE_CTX * ctx,STACK_OF (X509)* sk)2403 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2404 {
2405     ctx->other_ctx = sk;
2406     ctx->get_issuer = get_issuer_sk;
2407 }
2408 
X509_STORE_CTX_cleanup(X509_STORE_CTX * ctx)2409 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2410 {
2411     /* We need to be idempotent because, unfortunately, |X509_STORE_CTX_free|
2412      * also calls this function. */
2413     if (ctx->cleanup != NULL) {
2414         ctx->cleanup(ctx);
2415         ctx->cleanup = NULL;
2416     }
2417     if (ctx->param != NULL) {
2418         if (ctx->parent == NULL)
2419             X509_VERIFY_PARAM_free(ctx->param);
2420         ctx->param = NULL;
2421     }
2422     if (ctx->tree != NULL) {
2423         X509_policy_tree_free(ctx->tree);
2424         ctx->tree = NULL;
2425     }
2426     if (ctx->chain != NULL) {
2427         sk_X509_pop_free(ctx->chain, X509_free);
2428         ctx->chain = NULL;
2429     }
2430     CRYPTO_free_ex_data(&g_ex_data_class, ctx, &(ctx->ex_data));
2431     OPENSSL_memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
2432 }
2433 
X509_STORE_CTX_set_depth(X509_STORE_CTX * ctx,int depth)2434 void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2435 {
2436     X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2437 }
2438 
X509_STORE_CTX_set_flags(X509_STORE_CTX * ctx,unsigned long flags)2439 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2440 {
2441     X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2442 }
2443 
X509_STORE_CTX_set_time(X509_STORE_CTX * ctx,unsigned long flags,time_t t)2444 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
2445                              time_t t)
2446 {
2447     X509_VERIFY_PARAM_set_time(ctx->param, t);
2448 }
2449 
X509_STORE_CTX_get0_cert(X509_STORE_CTX * ctx)2450 X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx)
2451 {
2452     return ctx->cert;
2453 }
2454 
X509_STORE_CTX_set_verify_cb(X509_STORE_CTX * ctx,int (* verify_cb)(int,X509_STORE_CTX *))2455 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2456                                   int (*verify_cb) (int, X509_STORE_CTX *))
2457 {
2458     ctx->verify_cb = verify_cb;
2459 }
2460 
X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX * ctx)2461 X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2462 {
2463     return ctx->tree;
2464 }
2465 
X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX * ctx)2466 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2467 {
2468     return ctx->explicit_policy;
2469 }
2470 
X509_STORE_CTX_set_default(X509_STORE_CTX * ctx,const char * name)2471 int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2472 {
2473     const X509_VERIFY_PARAM *param;
2474     param = X509_VERIFY_PARAM_lookup(name);
2475     if (!param)
2476         return 0;
2477     return X509_VERIFY_PARAM_inherit(ctx->param, param);
2478 }
2479 
X509_STORE_CTX_get0_param(X509_STORE_CTX * ctx)2480 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2481 {
2482     return ctx->param;
2483 }
2484 
X509_STORE_CTX_set0_param(X509_STORE_CTX * ctx,X509_VERIFY_PARAM * param)2485 void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2486 {
2487     if (ctx->param)
2488         X509_VERIFY_PARAM_free(ctx->param);
2489     ctx->param = param;
2490 }
2491