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