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