• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* v3_purp.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4  * 2001.
5  */
6 /* ====================================================================
7  * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com). */
57 
58 #include <stdio.h>
59 
60 #include <string.h>
61 
62 #include <openssl/buf.h>
63 #include <openssl/err.h>
64 #include <openssl/digest.h>
65 #include <openssl/mem.h>
66 #include <openssl/obj.h>
67 #include <openssl/thread.h>
68 #include <openssl/x509_vfy.h>
69 #include <openssl/x509v3.h>
70 
71 #include "../internal.h"
72 
73 #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
74 #define ku_reject(x, usage) \
75         (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
76 #define xku_reject(x, usage) \
77         (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
78 #define ns_reject(x, usage) \
79         (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
80 
81 static void x509v3_cache_extensions(X509 *x);
82 
83 static int check_ssl_ca(const X509 *x);
84 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
85                                     int ca);
86 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
87                                     int ca);
88 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
89                                        int ca);
90 static int purpose_smime(const X509 *x, int ca);
91 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
92                                     int ca);
93 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
94                                        int ca);
95 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
96                                   int ca);
97 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
98                                         int ca);
99 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
100 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
101 
102 static int xp_cmp(const X509_PURPOSE **a, const X509_PURPOSE **b);
103 static void xptable_free(X509_PURPOSE *p);
104 
105 static X509_PURPOSE xstandard[] = {
106     {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
107      check_purpose_ssl_client, (char *)"SSL client", (char *)"sslclient",
108      NULL},
109     {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
110      check_purpose_ssl_server, (char *)"SSL server", (char *)"sslserver",
111      NULL},
112     {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
113      check_purpose_ns_ssl_server, (char *)"Netscape SSL server",
114      (char *)"nssslserver", NULL},
115     {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
116      (char *)"S/MIME signing", (char *)"smimesign", NULL},
117     {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
118      check_purpose_smime_encrypt, (char *)"S/MIME encryption",
119      (char *)"smimeencrypt", NULL},
120     {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
121      (char *)"CRL signing", (char *)"crlsign", NULL},
122     {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, (char *)"Any Purpose",
123      (char *)"any", NULL},
124     {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper,
125      (char *)"OCSP helper", (char *)"ocsphelper", NULL},
126     {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
127      check_purpose_timestamp_sign, (char *)"Time Stamp signing",
128      (char *)"timestampsign", NULL},
129 };
130 
131 #define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE))
132 
133 static STACK_OF(X509_PURPOSE) *xptable = NULL;
134 
xp_cmp(const X509_PURPOSE ** a,const X509_PURPOSE ** b)135 static int xp_cmp(const X509_PURPOSE **a, const X509_PURPOSE **b)
136 {
137     return (*a)->purpose - (*b)->purpose;
138 }
139 
140 /*
141  * As much as I'd like to make X509_check_purpose use a "const" X509* I
142  * really can't because it does recalculate hashes and do other non-const
143  * things.
144  */
X509_check_purpose(X509 * x,int id,int ca)145 int X509_check_purpose(X509 *x, int id, int ca)
146 {
147     int idx;
148     const X509_PURPOSE *pt;
149     x509v3_cache_extensions(x);
150     if (id == -1)
151         return 1;
152     idx = X509_PURPOSE_get_by_id(id);
153     if (idx == -1)
154         return -1;
155     pt = X509_PURPOSE_get0(idx);
156     return pt->check_purpose(pt, x, ca);
157 }
158 
X509_PURPOSE_set(int * p,int purpose)159 int X509_PURPOSE_set(int *p, int purpose)
160 {
161     if (X509_PURPOSE_get_by_id(purpose) == -1) {
162         OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_PURPOSE);
163         return 0;
164     }
165     *p = purpose;
166     return 1;
167 }
168 
X509_PURPOSE_get_count(void)169 int X509_PURPOSE_get_count(void)
170 {
171     if (!xptable)
172         return X509_PURPOSE_COUNT;
173     return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
174 }
175 
X509_PURPOSE_get0(int idx)176 X509_PURPOSE *X509_PURPOSE_get0(int idx)
177 {
178     if (idx < 0)
179         return NULL;
180     if (idx < (int)X509_PURPOSE_COUNT)
181         return xstandard + idx;
182     return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
183 }
184 
X509_PURPOSE_get_by_sname(char * sname)185 int X509_PURPOSE_get_by_sname(char *sname)
186 {
187     int i;
188     X509_PURPOSE *xptmp;
189     for (i = 0; i < X509_PURPOSE_get_count(); i++) {
190         xptmp = X509_PURPOSE_get0(i);
191         if (!strcmp(xptmp->sname, sname))
192             return i;
193     }
194     return -1;
195 }
196 
X509_PURPOSE_get_by_id(int purpose)197 int X509_PURPOSE_get_by_id(int purpose)
198 {
199     X509_PURPOSE tmp;
200     size_t idx;
201 
202     if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
203         return purpose - X509_PURPOSE_MIN;
204     tmp.purpose = purpose;
205     if (!xptable)
206         return -1;
207 
208     if (!sk_X509_PURPOSE_find(xptable, &idx, &tmp))
209         return -1;
210     return idx + X509_PURPOSE_COUNT;
211 }
212 
X509_PURPOSE_add(int id,int trust,int flags,int (* ck)(const X509_PURPOSE *,const X509 *,int),char * name,char * sname,void * arg)213 int X509_PURPOSE_add(int id, int trust, int flags,
214                      int (*ck) (const X509_PURPOSE *, const X509 *, int),
215                      char *name, char *sname, void *arg)
216 {
217     int idx;
218     X509_PURPOSE *ptmp;
219     char *name_dup, *sname_dup;
220 
221     /*
222      * This is set according to what we change: application can't set it
223      */
224     flags &= ~X509_PURPOSE_DYNAMIC;
225     /* This will always be set for application modified trust entries */
226     flags |= X509_PURPOSE_DYNAMIC_NAME;
227     /* Get existing entry if any */
228     idx = X509_PURPOSE_get_by_id(id);
229     /* Need a new entry */
230     if (idx == -1) {
231         if (!(ptmp = OPENSSL_malloc(sizeof(X509_PURPOSE)))) {
232             OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
233             return 0;
234         }
235         ptmp->flags = X509_PURPOSE_DYNAMIC;
236     } else
237         ptmp = X509_PURPOSE_get0(idx);
238 
239     /* Duplicate the supplied names. */
240     name_dup = BUF_strdup(name);
241     sname_dup = BUF_strdup(sname);
242     if (name_dup == NULL || sname_dup == NULL) {
243         OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
244         if (name_dup != NULL)
245             OPENSSL_free(name_dup);
246         if (sname_dup != NULL)
247             OPENSSL_free(sname_dup);
248         if (idx == -1)
249             OPENSSL_free(ptmp);
250         return 0;
251     }
252 
253     /* OPENSSL_free existing name if dynamic */
254     if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
255         OPENSSL_free(ptmp->name);
256         OPENSSL_free(ptmp->sname);
257     }
258     /* dup supplied name */
259     ptmp->name = name_dup;
260     ptmp->sname = sname_dup;
261     /* Keep the dynamic flag of existing entry */
262     ptmp->flags &= X509_PURPOSE_DYNAMIC;
263     /* Set all other flags */
264     ptmp->flags |= flags;
265 
266     ptmp->purpose = id;
267     ptmp->trust = trust;
268     ptmp->check_purpose = ck;
269     ptmp->usr_data = arg;
270 
271     /* If its a new entry manage the dynamic table */
272     if (idx == -1) {
273         if (!xptable && !(xptable = sk_X509_PURPOSE_new(xp_cmp))) {
274             OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
275             xptable_free(ptmp);
276             return 0;
277         }
278         if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
279             OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
280             xptable_free(ptmp);
281             return 0;
282         }
283     }
284     return 1;
285 }
286 
xptable_free(X509_PURPOSE * p)287 static void xptable_free(X509_PURPOSE *p)
288 {
289     if (!p)
290         return;
291     if (p->flags & X509_PURPOSE_DYNAMIC) {
292         if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
293             OPENSSL_free(p->name);
294             OPENSSL_free(p->sname);
295         }
296         OPENSSL_free(p);
297     }
298 }
299 
X509_PURPOSE_cleanup(void)300 void X509_PURPOSE_cleanup(void)
301 {
302     unsigned int i;
303     sk_X509_PURPOSE_pop_free(xptable, xptable_free);
304     for (i = 0; i < X509_PURPOSE_COUNT; i++)
305         xptable_free(xstandard + i);
306     xptable = NULL;
307 }
308 
X509_PURPOSE_get_id(X509_PURPOSE * xp)309 int X509_PURPOSE_get_id(X509_PURPOSE *xp)
310 {
311     return xp->purpose;
312 }
313 
X509_PURPOSE_get0_name(X509_PURPOSE * xp)314 char *X509_PURPOSE_get0_name(X509_PURPOSE *xp)
315 {
316     return xp->name;
317 }
318 
X509_PURPOSE_get0_sname(X509_PURPOSE * xp)319 char *X509_PURPOSE_get0_sname(X509_PURPOSE *xp)
320 {
321     return xp->sname;
322 }
323 
X509_PURPOSE_get_trust(X509_PURPOSE * xp)324 int X509_PURPOSE_get_trust(X509_PURPOSE *xp)
325 {
326     return xp->trust;
327 }
328 
nid_cmp(const void * void_a,const void * void_b)329 static int nid_cmp(const void *void_a, const void *void_b)
330 {
331     const int *a = void_a, *b = void_b;
332 
333     return *a - *b;
334 }
335 
X509_supported_extension(X509_EXTENSION * ex)336 int X509_supported_extension(X509_EXTENSION *ex)
337 {
338     /*
339      * This table is a list of the NIDs of supported extensions: that is
340      * those which are used by the verify process. If an extension is
341      * critical and doesn't appear in this list then the verify process will
342      * normally reject the certificate. The list must be kept in numerical
343      * order because it will be searched using bsearch.
344      */
345 
346     static const int supported_nids[] = {
347         NID_netscape_cert_type, /* 71 */
348         NID_key_usage,          /* 83 */
349         NID_subject_alt_name,   /* 85 */
350         NID_basic_constraints,  /* 87 */
351         NID_certificate_policies, /* 89 */
352         NID_ext_key_usage,      /* 126 */
353         NID_policy_constraints, /* 401 */
354         NID_proxyCertInfo,      /* 663 */
355         NID_name_constraints,   /* 666 */
356         NID_policy_mappings,    /* 747 */
357         NID_inhibit_any_policy  /* 748 */
358     };
359 
360     int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
361 
362     if (ex_nid == NID_undef)
363         return 0;
364 
365     if (bsearch
366         (&ex_nid, supported_nids, sizeof(supported_nids) / sizeof(int),
367          sizeof(int), nid_cmp) != NULL)
368         return 1;
369     return 0;
370 }
371 
setup_dp(X509 * x,DIST_POINT * dp)372 static void setup_dp(X509 *x, DIST_POINT *dp)
373 {
374     X509_NAME *iname = NULL;
375     size_t i;
376     if (dp->reasons) {
377         if (dp->reasons->length > 0)
378             dp->dp_reasons = dp->reasons->data[0];
379         if (dp->reasons->length > 1)
380             dp->dp_reasons |= (dp->reasons->data[1] << 8);
381         dp->dp_reasons &= CRLDP_ALL_REASONS;
382     } else
383         dp->dp_reasons = CRLDP_ALL_REASONS;
384     if (!dp->distpoint || (dp->distpoint->type != 1))
385         return;
386     for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
387         GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
388         if (gen->type == GEN_DIRNAME) {
389             iname = gen->d.directoryName;
390             break;
391         }
392     }
393     if (!iname)
394         iname = X509_get_issuer_name(x);
395 
396     DIST_POINT_set_dpname(dp->distpoint, iname);
397 
398 }
399 
setup_crldp(X509 * x)400 static void setup_crldp(X509 *x)
401 {
402     size_t i;
403     x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL);
404     for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++)
405         setup_dp(x, sk_DIST_POINT_value(x->crldp, i));
406 }
407 
x509v3_cache_extensions(X509 * x)408 static void x509v3_cache_extensions(X509 *x)
409 {
410     BASIC_CONSTRAINTS *bs;
411     PROXY_CERT_INFO_EXTENSION *pci;
412     ASN1_BIT_STRING *usage;
413     ASN1_BIT_STRING *ns;
414     EXTENDED_KEY_USAGE *extusage;
415     X509_EXTENSION *ex;
416     size_t i;
417     int j;
418 
419     CRYPTO_MUTEX_lock_read(&x->lock);
420     const int is_set = x->ex_flags & EXFLAG_SET;
421     CRYPTO_MUTEX_unlock_read(&x->lock);
422 
423     if (is_set) {
424         return;
425     }
426 
427     CRYPTO_MUTEX_lock_write(&x->lock);
428     if (x->ex_flags & EXFLAG_SET) {
429         CRYPTO_MUTEX_unlock_write(&x->lock);
430         return;
431     }
432 
433     X509_digest(x, EVP_sha1(), x->sha1_hash, NULL);
434     /* V1 should mean no extensions ... */
435     if (!X509_get_version(x))
436         x->ex_flags |= EXFLAG_V1;
437     /* Handle basic constraints */
438     if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {
439         if (bs->ca)
440             x->ex_flags |= EXFLAG_CA;
441         if (bs->pathlen) {
442             if ((bs->pathlen->type == V_ASN1_NEG_INTEGER)
443                 || !bs->ca) {
444                 x->ex_flags |= EXFLAG_INVALID;
445                 x->ex_pathlen = 0;
446             } else
447                 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
448         } else
449             x->ex_pathlen = -1;
450         BASIC_CONSTRAINTS_free(bs);
451         x->ex_flags |= EXFLAG_BCONS;
452     }
453     /* Handle proxy certificates */
454     if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, NULL, NULL))) {
455         if (x->ex_flags & EXFLAG_CA
456             || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
457             || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
458             x->ex_flags |= EXFLAG_INVALID;
459         }
460         if (pci->pcPathLengthConstraint) {
461             x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
462         } else
463             x->ex_pcpathlen = -1;
464         PROXY_CERT_INFO_EXTENSION_free(pci);
465         x->ex_flags |= EXFLAG_PROXY;
466     }
467     /* Handle key usage */
468     if ((usage = X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {
469         if (usage->length > 0) {
470             x->ex_kusage = usage->data[0];
471             if (usage->length > 1)
472                 x->ex_kusage |= usage->data[1] << 8;
473         } else
474             x->ex_kusage = 0;
475         x->ex_flags |= EXFLAG_KUSAGE;
476         ASN1_BIT_STRING_free(usage);
477     }
478     x->ex_xkusage = 0;
479     if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {
480         x->ex_flags |= EXFLAG_XKUSAGE;
481         for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
482             switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
483             case NID_server_auth:
484                 x->ex_xkusage |= XKU_SSL_SERVER;
485                 break;
486 
487             case NID_client_auth:
488                 x->ex_xkusage |= XKU_SSL_CLIENT;
489                 break;
490 
491             case NID_email_protect:
492                 x->ex_xkusage |= XKU_SMIME;
493                 break;
494 
495             case NID_code_sign:
496                 x->ex_xkusage |= XKU_CODE_SIGN;
497                 break;
498 
499             case NID_ms_sgc:
500             case NID_ns_sgc:
501                 x->ex_xkusage |= XKU_SGC;
502                 break;
503 
504             case NID_OCSP_sign:
505                 x->ex_xkusage |= XKU_OCSP_SIGN;
506                 break;
507 
508             case NID_time_stamp:
509                 x->ex_xkusage |= XKU_TIMESTAMP;
510                 break;
511 
512             case NID_dvcs:
513                 x->ex_xkusage |= XKU_DVCS;
514                 break;
515 
516             case NID_anyExtendedKeyUsage:
517                 x->ex_xkusage |= XKU_ANYEKU;
518                 break;
519             }
520         }
521         sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
522     }
523 
524     if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {
525         if (ns->length > 0)
526             x->ex_nscert = ns->data[0];
527         else
528             x->ex_nscert = 0;
529         x->ex_flags |= EXFLAG_NSCERT;
530         ASN1_BIT_STRING_free(ns);
531     }
532     x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL);
533     x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL);
534     /* Does subject name match issuer ? */
535     if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
536         x->ex_flags |= EXFLAG_SI;
537         /* If SKID matches AKID also indicate self signed */
538         if (X509_check_akid(x, x->akid) == X509_V_OK &&
539             !ku_reject(x, KU_KEY_CERT_SIGN))
540             x->ex_flags |= EXFLAG_SS;
541     }
542     x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
543     x->nc = X509_get_ext_d2i(x, NID_name_constraints, &j, NULL);
544     if (!x->nc && (j != -1))
545         x->ex_flags |= EXFLAG_INVALID;
546     setup_crldp(x);
547 
548     for (j = 0; j < X509_get_ext_count(x); j++) {
549         ex = X509_get_ext(x, j);
550         if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
551             == NID_freshest_crl)
552             x->ex_flags |= EXFLAG_FRESHEST;
553         if (!X509_EXTENSION_get_critical(ex))
554             continue;
555         if (!X509_supported_extension(ex)) {
556             x->ex_flags |= EXFLAG_CRITICAL;
557             break;
558         }
559     }
560     x->ex_flags |= EXFLAG_SET;
561 
562     CRYPTO_MUTEX_unlock_write(&x->lock);
563 }
564 
565 /*
566  * CA checks common to all purposes return codes: 0 not a CA 1 is a CA 2
567  * basicConstraints absent so "maybe" a CA 3 basicConstraints absent but self
568  * signed V1. 4 basicConstraints absent but keyUsage present and keyCertSign
569  * asserted.
570  */
571 
check_ca(const X509 * x)572 static int check_ca(const X509 *x)
573 {
574     /* keyUsage if present should allow cert signing */
575     if (ku_reject(x, KU_KEY_CERT_SIGN))
576         return 0;
577     if (x->ex_flags & EXFLAG_BCONS) {
578         if (x->ex_flags & EXFLAG_CA)
579             return 1;
580         /* If basicConstraints says not a CA then say so */
581         else
582             return 0;
583     } else {
584         /* we support V1 roots for...  uh, I don't really know why. */
585         if ((x->ex_flags & V1_ROOT) == V1_ROOT)
586             return 3;
587         /*
588          * If key usage present it must have certSign so tolerate it
589          */
590         else if (x->ex_flags & EXFLAG_KUSAGE)
591             return 4;
592         /* Older certificates could have Netscape-specific CA types */
593         else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
594             return 5;
595         /* can this still be regarded a CA certificate?  I doubt it */
596         return 0;
597     }
598 }
599 
X509_check_ca(X509 * x)600 int X509_check_ca(X509 *x)
601 {
602     x509v3_cache_extensions(x);
603     return check_ca(x);
604 }
605 
606 /* Check SSL CA: common checks for SSL client and server */
check_ssl_ca(const X509 * x)607 static int check_ssl_ca(const X509 *x)
608 {
609     int ca_ret;
610     ca_ret = check_ca(x);
611     if (!ca_ret)
612         return 0;
613     /* check nsCertType if present */
614     if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
615         return ca_ret;
616     else
617         return 0;
618 }
619 
check_purpose_ssl_client(const X509_PURPOSE * xp,const X509 * x,int ca)620 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
621                                     int ca)
622 {
623     if (xku_reject(x, XKU_SSL_CLIENT))
624         return 0;
625     if (ca)
626         return check_ssl_ca(x);
627     /* We need to do digital signatures or key agreement */
628     if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
629         return 0;
630     /* nsCertType if present should allow SSL client use */
631     if (ns_reject(x, NS_SSL_CLIENT))
632         return 0;
633     return 1;
634 }
635 
636 /*
637  * Key usage needed for TLS/SSL server: digital signature, encipherment or
638  * key agreement. The ssl code can check this more thoroughly for individual
639  * key types.
640  */
641 #define KU_TLS \
642         (KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT)
643 
check_purpose_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)644 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
645                                     int ca)
646 {
647     if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
648         return 0;
649     if (ca)
650         return check_ssl_ca(x);
651 
652     if (ns_reject(x, NS_SSL_SERVER))
653         return 0;
654     if (ku_reject(x, KU_TLS))
655         return 0;
656 
657     return 1;
658 
659 }
660 
check_purpose_ns_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)661 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
662                                        int ca)
663 {
664     int ret;
665     ret = check_purpose_ssl_server(xp, x, ca);
666     if (!ret || ca)
667         return ret;
668     /* We need to encipher or Netscape complains */
669     if (ku_reject(x, KU_KEY_ENCIPHERMENT))
670         return 0;
671     return ret;
672 }
673 
674 /* common S/MIME checks */
purpose_smime(const X509 * x,int ca)675 static int purpose_smime(const X509 *x, int ca)
676 {
677     if (xku_reject(x, XKU_SMIME))
678         return 0;
679     if (ca) {
680         int ca_ret;
681         ca_ret = check_ca(x);
682         if (!ca_ret)
683             return 0;
684         /* check nsCertType if present */
685         if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
686             return ca_ret;
687         else
688             return 0;
689     }
690     if (x->ex_flags & EXFLAG_NSCERT) {
691         if (x->ex_nscert & NS_SMIME)
692             return 1;
693         /* Workaround for some buggy certificates */
694         if (x->ex_nscert & NS_SSL_CLIENT)
695             return 2;
696         return 0;
697     }
698     return 1;
699 }
700 
check_purpose_smime_sign(const X509_PURPOSE * xp,const X509 * x,int ca)701 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
702                                     int ca)
703 {
704     int ret;
705     ret = purpose_smime(x, ca);
706     if (!ret || ca)
707         return ret;
708     if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
709         return 0;
710     return ret;
711 }
712 
check_purpose_smime_encrypt(const X509_PURPOSE * xp,const X509 * x,int ca)713 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
714                                        int ca)
715 {
716     int ret;
717     ret = purpose_smime(x, ca);
718     if (!ret || ca)
719         return ret;
720     if (ku_reject(x, KU_KEY_ENCIPHERMENT))
721         return 0;
722     return ret;
723 }
724 
check_purpose_crl_sign(const X509_PURPOSE * xp,const X509 * x,int ca)725 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
726                                   int ca)
727 {
728     if (ca) {
729         int ca_ret;
730         if ((ca_ret = check_ca(x)) != 2)
731             return ca_ret;
732         else
733             return 0;
734     }
735     if (ku_reject(x, KU_CRL_SIGN))
736         return 0;
737     return 1;
738 }
739 
740 /*
741  * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
742  * is valid. Additional checks must be made on the chain.
743  */
744 
ocsp_helper(const X509_PURPOSE * xp,const X509 * x,int ca)745 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
746 {
747     /*
748      * Must be a valid CA.  Should we really support the "I don't know" value
749      * (2)?
750      */
751     if (ca)
752         return check_ca(x);
753     /* leaf certificate is checked in OCSP_verify() */
754     return 1;
755 }
756 
check_purpose_timestamp_sign(const X509_PURPOSE * xp,const X509 * x,int ca)757 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
758                                         int ca)
759 {
760     int i_ext;
761 
762     /* If ca is true we must return if this is a valid CA certificate. */
763     if (ca)
764         return check_ca(x);
765 
766     /*
767      * Check the optional key usage field:
768      * if Key Usage is present, it must be one of digitalSignature
769      * and/or nonRepudiation (other values are not consistent and shall
770      * be rejected).
771      */
772     if ((x->ex_flags & EXFLAG_KUSAGE)
773         && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
774             !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
775         return 0;
776 
777     /* Only time stamp key usage is permitted and it's required. */
778     if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
779         return 0;
780 
781     /* Extended Key Usage MUST be critical */
782     i_ext = X509_get_ext_by_NID((X509 *)x, NID_ext_key_usage, -1);
783     if (i_ext >= 0) {
784         X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
785         if (!X509_EXTENSION_get_critical(ext))
786             return 0;
787     }
788 
789     return 1;
790 }
791 
no_check(const X509_PURPOSE * xp,const X509 * x,int ca)792 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
793 {
794     return 1;
795 }
796 
797 /*
798  * Various checks to see if one certificate issued the second. This can be
799  * used to prune a set of possible issuer certificates which have been looked
800  * up using some simple method such as by subject name. These are: 1. Check
801  * issuer_name(subject) == subject_name(issuer) 2. If akid(subject) exists
802  * check it matches issuer 3. If key_usage(issuer) exists check it supports
803  * certificate signing returns 0 for OK, positive for reason for mismatch,
804  * reasons match codes for X509_verify_cert()
805  */
806 
X509_check_issued(X509 * issuer,X509 * subject)807 int X509_check_issued(X509 *issuer, X509 *subject)
808 {
809     if (X509_NAME_cmp(X509_get_subject_name(issuer),
810                       X509_get_issuer_name(subject)))
811         return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
812     x509v3_cache_extensions(issuer);
813     x509v3_cache_extensions(subject);
814 
815     if (subject->akid) {
816         int ret = X509_check_akid(issuer, subject->akid);
817         if (ret != X509_V_OK)
818             return ret;
819     }
820 
821     if (subject->ex_flags & EXFLAG_PROXY) {
822         if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
823             return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
824     } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
825         return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
826     return X509_V_OK;
827 }
828 
X509_check_akid(X509 * issuer,AUTHORITY_KEYID * akid)829 int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
830 {
831 
832     if (!akid)
833         return X509_V_OK;
834 
835     /* Check key ids (if present) */
836     if (akid->keyid && issuer->skid &&
837         ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
838         return X509_V_ERR_AKID_SKID_MISMATCH;
839     /* Check serial number */
840     if (akid->serial &&
841         ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
842         return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
843     /* Check issuer name */
844     if (akid->issuer) {
845         /*
846          * Ugh, for some peculiar reason AKID includes SEQUENCE OF
847          * GeneralName. So look for a DirName. There may be more than one but
848          * we only take any notice of the first.
849          */
850         GENERAL_NAMES *gens;
851         GENERAL_NAME *gen;
852         X509_NAME *nm = NULL;
853         size_t i;
854         gens = akid->issuer;
855         for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
856             gen = sk_GENERAL_NAME_value(gens, i);
857             if (gen->type == GEN_DIRNAME) {
858                 nm = gen->d.dirn;
859                 break;
860             }
861         }
862         if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
863             return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
864     }
865     return X509_V_OK;
866 }
867