]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssl/crypto/x509v3/v3_purp.c
Merge OpenSSL 1.1.1f.
[FreeBSD/FreeBSD.git] / crypto / openssl / crypto / x509v3 / v3_purp.c
1 /*
2  * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include "internal/numbers.h"
13 #include <openssl/x509v3.h>
14 #include <openssl/x509_vfy.h>
15 #include "crypto/x509.h"
16 #include "internal/tsan_assist.h"
17
18 static void x509v3_cache_extensions(X509 *x);
19
20 static int check_ssl_ca(const X509 *x);
21 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
22                                     int ca);
23 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
24                                     int ca);
25 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
26                                        int ca);
27 static int purpose_smime(const X509 *x, int ca);
28 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
29                                     int ca);
30 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
31                                        int ca);
32 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
33                                   int ca);
34 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
35                                         int ca);
36 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
37 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
38
39 static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b);
40 static void xptable_free(X509_PURPOSE *p);
41
42 static X509_PURPOSE xstandard[] = {
43     {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
44      check_purpose_ssl_client, "SSL client", "sslclient", NULL},
45     {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
46      check_purpose_ssl_server, "SSL server", "sslserver", NULL},
47     {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
48      check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
49     {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
50      "S/MIME signing", "smimesign", NULL},
51     {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
52      check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
53     {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
54      "CRL signing", "crlsign", NULL},
55     {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any",
56      NULL},
57     {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper,
58      "OCSP helper", "ocsphelper", NULL},
59     {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
60      check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign",
61      NULL},
62 };
63
64 #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard)
65
66 static STACK_OF(X509_PURPOSE) *xptable = NULL;
67
68 static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b)
69 {
70     return (*a)->purpose - (*b)->purpose;
71 }
72
73 /*
74  * As much as I'd like to make X509_check_purpose use a "const" X509* I
75  * really can't because it does recalculate hashes and do other non-const
76  * things.
77  */
78 int X509_check_purpose(X509 *x, int id, int ca)
79 {
80     int idx;
81     const X509_PURPOSE *pt;
82
83     x509v3_cache_extensions(x);
84     if (x->ex_flags & EXFLAG_INVALID)
85         return -1;
86
87     /* Return if side-effect only call */
88     if (id == -1)
89         return 1;
90     idx = X509_PURPOSE_get_by_id(id);
91     if (idx == -1)
92         return -1;
93     pt = X509_PURPOSE_get0(idx);
94     return pt->check_purpose(pt, x, ca);
95 }
96
97 int X509_PURPOSE_set(int *p, int purpose)
98 {
99     if (X509_PURPOSE_get_by_id(purpose) == -1) {
100         X509V3err(X509V3_F_X509_PURPOSE_SET, X509V3_R_INVALID_PURPOSE);
101         return 0;
102     }
103     *p = purpose;
104     return 1;
105 }
106
107 int X509_PURPOSE_get_count(void)
108 {
109     if (!xptable)
110         return X509_PURPOSE_COUNT;
111     return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
112 }
113
114 X509_PURPOSE *X509_PURPOSE_get0(int idx)
115 {
116     if (idx < 0)
117         return NULL;
118     if (idx < (int)X509_PURPOSE_COUNT)
119         return xstandard + idx;
120     return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
121 }
122
123 int X509_PURPOSE_get_by_sname(const char *sname)
124 {
125     int i;
126     X509_PURPOSE *xptmp;
127     for (i = 0; i < X509_PURPOSE_get_count(); i++) {
128         xptmp = X509_PURPOSE_get0(i);
129         if (strcmp(xptmp->sname, sname) == 0)
130             return i;
131     }
132     return -1;
133 }
134
135 int X509_PURPOSE_get_by_id(int purpose)
136 {
137     X509_PURPOSE tmp;
138     int idx;
139
140     if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
141         return purpose - X509_PURPOSE_MIN;
142     if (xptable == NULL)
143         return -1;
144     tmp.purpose = purpose;
145     idx = sk_X509_PURPOSE_find(xptable, &tmp);
146     if (idx < 0)
147         return -1;
148     return idx + X509_PURPOSE_COUNT;
149 }
150
151 int X509_PURPOSE_add(int id, int trust, int flags,
152                      int (*ck) (const X509_PURPOSE *, const X509 *, int),
153                      const char *name, const char *sname, void *arg)
154 {
155     int idx;
156     X509_PURPOSE *ptmp;
157     /*
158      * This is set according to what we change: application can't set it
159      */
160     flags &= ~X509_PURPOSE_DYNAMIC;
161     /* This will always be set for application modified trust entries */
162     flags |= X509_PURPOSE_DYNAMIC_NAME;
163     /* Get existing entry if any */
164     idx = X509_PURPOSE_get_by_id(id);
165     /* Need a new entry */
166     if (idx == -1) {
167         if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL) {
168             X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
169             return 0;
170         }
171         ptmp->flags = X509_PURPOSE_DYNAMIC;
172     } else
173         ptmp = X509_PURPOSE_get0(idx);
174
175     /* OPENSSL_free existing name if dynamic */
176     if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
177         OPENSSL_free(ptmp->name);
178         OPENSSL_free(ptmp->sname);
179     }
180     /* dup supplied name */
181     ptmp->name = OPENSSL_strdup(name);
182     ptmp->sname = OPENSSL_strdup(sname);
183     if (!ptmp->name || !ptmp->sname) {
184         X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
185         goto err;
186     }
187     /* Keep the dynamic flag of existing entry */
188     ptmp->flags &= X509_PURPOSE_DYNAMIC;
189     /* Set all other flags */
190     ptmp->flags |= flags;
191
192     ptmp->purpose = id;
193     ptmp->trust = trust;
194     ptmp->check_purpose = ck;
195     ptmp->usr_data = arg;
196
197     /* If its a new entry manage the dynamic table */
198     if (idx == -1) {
199         if (xptable == NULL
200             && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) {
201             X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
202             goto err;
203         }
204         if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
205             X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
206             goto err;
207         }
208     }
209     return 1;
210  err:
211     if (idx == -1) {
212         OPENSSL_free(ptmp->name);
213         OPENSSL_free(ptmp->sname);
214         OPENSSL_free(ptmp);
215     }
216     return 0;
217 }
218
219 static void xptable_free(X509_PURPOSE *p)
220 {
221     if (!p)
222         return;
223     if (p->flags & X509_PURPOSE_DYNAMIC) {
224         if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
225             OPENSSL_free(p->name);
226             OPENSSL_free(p->sname);
227         }
228         OPENSSL_free(p);
229     }
230 }
231
232 void X509_PURPOSE_cleanup(void)
233 {
234     sk_X509_PURPOSE_pop_free(xptable, xptable_free);
235     xptable = NULL;
236 }
237
238 int X509_PURPOSE_get_id(const X509_PURPOSE *xp)
239 {
240     return xp->purpose;
241 }
242
243 char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp)
244 {
245     return xp->name;
246 }
247
248 char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp)
249 {
250     return xp->sname;
251 }
252
253 int X509_PURPOSE_get_trust(const X509_PURPOSE *xp)
254 {
255     return xp->trust;
256 }
257
258 static int nid_cmp(const int *a, const int *b)
259 {
260     return *a - *b;
261 }
262
263 DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid);
264 IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid);
265
266 int X509_supported_extension(X509_EXTENSION *ex)
267 {
268     /*
269      * This table is a list of the NIDs of supported extensions: that is
270      * those which are used by the verify process. If an extension is
271      * critical and doesn't appear in this list then the verify process will
272      * normally reject the certificate. The list must be kept in numerical
273      * order because it will be searched using bsearch.
274      */
275
276     static const int supported_nids[] = {
277         NID_netscape_cert_type, /* 71 */
278         NID_key_usage,          /* 83 */
279         NID_subject_alt_name,   /* 85 */
280         NID_basic_constraints,  /* 87 */
281         NID_certificate_policies, /* 89 */
282         NID_crl_distribution_points, /* 103 */
283         NID_ext_key_usage,      /* 126 */
284 #ifndef OPENSSL_NO_RFC3779
285         NID_sbgp_ipAddrBlock,   /* 290 */
286         NID_sbgp_autonomousSysNum, /* 291 */
287 #endif
288         NID_policy_constraints, /* 401 */
289         NID_proxyCertInfo,      /* 663 */
290         NID_name_constraints,   /* 666 */
291         NID_policy_mappings,    /* 747 */
292         NID_inhibit_any_policy  /* 748 */
293     };
294
295     int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
296
297     if (ex_nid == NID_undef)
298         return 0;
299
300     if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
301         return 1;
302     return 0;
303 }
304
305 static int setup_dp(X509 *x, DIST_POINT *dp)
306 {
307     X509_NAME *iname = NULL;
308     int i;
309
310     if (dp->reasons) {
311         if (dp->reasons->length > 0)
312             dp->dp_reasons = dp->reasons->data[0];
313         if (dp->reasons->length > 1)
314             dp->dp_reasons |= (dp->reasons->data[1] << 8);
315         dp->dp_reasons &= CRLDP_ALL_REASONS;
316     } else
317         dp->dp_reasons = CRLDP_ALL_REASONS;
318     if (!dp->distpoint || (dp->distpoint->type != 1))
319         return 1;
320     for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
321         GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
322         if (gen->type == GEN_DIRNAME) {
323             iname = gen->d.directoryName;
324             break;
325         }
326     }
327     if (!iname)
328         iname = X509_get_issuer_name(x);
329
330     return DIST_POINT_set_dpname(dp->distpoint, iname);
331 }
332
333 static int setup_crldp(X509 *x)
334 {
335     int i;
336
337     x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
338     if (x->crldp == NULL && i != -1)
339         return 0;
340     for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
341         if (!setup_dp(x, sk_DIST_POINT_value(x->crldp, i)))
342             return 0;
343     }
344     return 1;
345 }
346
347 #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
348 #define ku_reject(x, usage) \
349         (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
350 #define xku_reject(x, usage) \
351         (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
352 #define ns_reject(x, usage) \
353         (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
354
355 static void x509v3_cache_extensions(X509 *x)
356 {
357     BASIC_CONSTRAINTS *bs;
358     PROXY_CERT_INFO_EXTENSION *pci;
359     ASN1_BIT_STRING *usage;
360     ASN1_BIT_STRING *ns;
361     EXTENDED_KEY_USAGE *extusage;
362     X509_EXTENSION *ex;
363     int i;
364
365 #ifdef tsan_ld_acq
366     /* fast lock-free check, see end of the function for details. */
367     if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
368         return;
369 #endif
370
371     CRYPTO_THREAD_write_lock(x->lock);
372     if (x->ex_flags & EXFLAG_SET) {
373         CRYPTO_THREAD_unlock(x->lock);
374         return;
375     }
376
377     if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL))
378         x->ex_flags |= EXFLAG_INVALID;
379     /* V1 should mean no extensions ... */
380     if (!X509_get_version(x))
381         x->ex_flags |= EXFLAG_V1;
382     /* Handle basic constraints */
383     if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL))) {
384         if (bs->ca)
385             x->ex_flags |= EXFLAG_CA;
386         if (bs->pathlen) {
387             if ((bs->pathlen->type == V_ASN1_NEG_INTEGER)
388                 || !bs->ca) {
389                 x->ex_flags |= EXFLAG_INVALID;
390                 x->ex_pathlen = 0;
391             } else
392                 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
393         } else
394             x->ex_pathlen = -1;
395         BASIC_CONSTRAINTS_free(bs);
396         x->ex_flags |= EXFLAG_BCONS;
397     } else if (i != -1) {
398         x->ex_flags |= EXFLAG_INVALID;
399     }
400     /* Handle proxy certificates */
401     if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) {
402         if (x->ex_flags & EXFLAG_CA
403             || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
404             || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
405             x->ex_flags |= EXFLAG_INVALID;
406         }
407         if (pci->pcPathLengthConstraint) {
408             x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
409         } else
410             x->ex_pcpathlen = -1;
411         PROXY_CERT_INFO_EXTENSION_free(pci);
412         x->ex_flags |= EXFLAG_PROXY;
413     } else if (i != -1) {
414         x->ex_flags |= EXFLAG_INVALID;
415     }
416     /* Handle key usage */
417     if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) {
418         if (usage->length > 0) {
419             x->ex_kusage = usage->data[0];
420             if (usage->length > 1)
421                 x->ex_kusage |= usage->data[1] << 8;
422         } else
423             x->ex_kusage = 0;
424         x->ex_flags |= EXFLAG_KUSAGE;
425         ASN1_BIT_STRING_free(usage);
426     } else if (i != -1) {
427         x->ex_flags |= EXFLAG_INVALID;
428     }
429     x->ex_xkusage = 0;
430     if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) {
431         x->ex_flags |= EXFLAG_XKUSAGE;
432         for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
433             switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
434             case NID_server_auth:
435                 x->ex_xkusage |= XKU_SSL_SERVER;
436                 break;
437
438             case NID_client_auth:
439                 x->ex_xkusage |= XKU_SSL_CLIENT;
440                 break;
441
442             case NID_email_protect:
443                 x->ex_xkusage |= XKU_SMIME;
444                 break;
445
446             case NID_code_sign:
447                 x->ex_xkusage |= XKU_CODE_SIGN;
448                 break;
449
450             case NID_ms_sgc:
451             case NID_ns_sgc:
452                 x->ex_xkusage |= XKU_SGC;
453                 break;
454
455             case NID_OCSP_sign:
456                 x->ex_xkusage |= XKU_OCSP_SIGN;
457                 break;
458
459             case NID_time_stamp:
460                 x->ex_xkusage |= XKU_TIMESTAMP;
461                 break;
462
463             case NID_dvcs:
464                 x->ex_xkusage |= XKU_DVCS;
465                 break;
466
467             case NID_anyExtendedKeyUsage:
468                 x->ex_xkusage |= XKU_ANYEKU;
469                 break;
470             }
471         }
472         sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
473     } else if (i != -1) {
474         x->ex_flags |= EXFLAG_INVALID;
475     }
476
477     if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) {
478         if (ns->length > 0)
479             x->ex_nscert = ns->data[0];
480         else
481             x->ex_nscert = 0;
482         x->ex_flags |= EXFLAG_NSCERT;
483         ASN1_BIT_STRING_free(ns);
484     } else if (i != -1) {
485         x->ex_flags |= EXFLAG_INVALID;
486     }
487     x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
488     if (x->skid == NULL && i != -1)
489         x->ex_flags |= EXFLAG_INVALID;
490     x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
491     if (x->akid == NULL && i != -1)
492         x->ex_flags |= EXFLAG_INVALID;
493     /* Does subject name match issuer ? */
494     if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
495         x->ex_flags |= EXFLAG_SI;
496         /* If SKID matches AKID also indicate self signed */
497         if (X509_check_akid(x, x->akid) == X509_V_OK &&
498             !ku_reject(x, KU_KEY_CERT_SIGN))
499             x->ex_flags |= EXFLAG_SS;
500     }
501     x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
502     if (x->altname == NULL && i != -1)
503         x->ex_flags |= EXFLAG_INVALID;
504     x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
505     if (x->nc == NULL && i != -1)
506         x->ex_flags |= EXFLAG_INVALID;
507     if (!setup_crldp(x))
508         x->ex_flags |= EXFLAG_INVALID;
509
510 #ifndef OPENSSL_NO_RFC3779
511     x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
512     if (x->rfc3779_addr == NULL && i != -1)
513         x->ex_flags |= EXFLAG_INVALID;
514     x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
515     if (x->rfc3779_asid == NULL && i != -1)
516         x->ex_flags |= EXFLAG_INVALID;
517 #endif
518     for (i = 0; i < X509_get_ext_count(x); i++) {
519         ex = X509_get_ext(x, i);
520         if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
521             == NID_freshest_crl)
522             x->ex_flags |= EXFLAG_FRESHEST;
523         if (!X509_EXTENSION_get_critical(ex))
524             continue;
525         if (!X509_supported_extension(ex)) {
526             x->ex_flags |= EXFLAG_CRITICAL;
527             break;
528         }
529     }
530     x509_init_sig_info(x);
531     x->ex_flags |= EXFLAG_SET;
532 #ifdef tsan_st_rel
533     tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
534     /*
535      * Above store triggers fast lock-free check in the beginning of the
536      * function. But one has to ensure that the structure is "stable", i.e.
537      * all stores are visible on all processors. Hence the release fence.
538      */
539 #endif
540     CRYPTO_THREAD_unlock(x->lock);
541 }
542
543 /*-
544  * CA checks common to all purposes
545  * return codes:
546  * 0 not a CA
547  * 1 is a CA
548  * 2 basicConstraints absent so "maybe" a CA
549  * 3 basicConstraints absent but self signed V1.
550  * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
551  */
552
553 static int check_ca(const X509 *x)
554 {
555     /* keyUsage if present should allow cert signing */
556     if (ku_reject(x, KU_KEY_CERT_SIGN))
557         return 0;
558     if (x->ex_flags & EXFLAG_BCONS) {
559         if (x->ex_flags & EXFLAG_CA)
560             return 1;
561         /* If basicConstraints says not a CA then say so */
562         else
563             return 0;
564     } else {
565         /* we support V1 roots for...  uh, I don't really know why. */
566         if ((x->ex_flags & V1_ROOT) == V1_ROOT)
567             return 3;
568         /*
569          * If key usage present it must have certSign so tolerate it
570          */
571         else if (x->ex_flags & EXFLAG_KUSAGE)
572             return 4;
573         /* Older certificates could have Netscape-specific CA types */
574         else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
575             return 5;
576         /* can this still be regarded a CA certificate?  I doubt it */
577         return 0;
578     }
579 }
580
581 void X509_set_proxy_flag(X509 *x)
582 {
583     x->ex_flags |= EXFLAG_PROXY;
584 }
585
586 void X509_set_proxy_pathlen(X509 *x, long l)
587 {
588     x->ex_pcpathlen = l;
589 }
590
591 int X509_check_ca(X509 *x)
592 {
593     x509v3_cache_extensions(x);
594
595     return check_ca(x);
596 }
597
598 /* Check SSL CA: common checks for SSL client and server */
599 static int check_ssl_ca(const X509 *x)
600 {
601     int ca_ret;
602     ca_ret = check_ca(x);
603     if (!ca_ret)
604         return 0;
605     /* check nsCertType if present */
606     if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
607         return ca_ret;
608     else
609         return 0;
610 }
611
612 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
613                                     int ca)
614 {
615     if (xku_reject(x, XKU_SSL_CLIENT))
616         return 0;
617     if (ca)
618         return check_ssl_ca(x);
619     /* We need to do digital signatures or key agreement */
620     if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
621         return 0;
622     /* nsCertType if present should allow SSL client use */
623     if (ns_reject(x, NS_SSL_CLIENT))
624         return 0;
625     return 1;
626 }
627
628 /*
629  * Key usage needed for TLS/SSL server: digital signature, encipherment or
630  * key agreement. The ssl code can check this more thoroughly for individual
631  * key types.
632  */
633 #define KU_TLS \
634         KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
635
636 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
637                                     int ca)
638 {
639     if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
640         return 0;
641     if (ca)
642         return check_ssl_ca(x);
643
644     if (ns_reject(x, NS_SSL_SERVER))
645         return 0;
646     if (ku_reject(x, KU_TLS))
647         return 0;
648
649     return 1;
650
651 }
652
653 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
654                                        int ca)
655 {
656     int ret;
657     ret = check_purpose_ssl_server(xp, x, ca);
658     if (!ret || ca)
659         return ret;
660     /* We need to encipher or Netscape complains */
661     if (ku_reject(x, KU_KEY_ENCIPHERMENT))
662         return 0;
663     return ret;
664 }
665
666 /* common S/MIME checks */
667 static int purpose_smime(const X509 *x, int ca)
668 {
669     if (xku_reject(x, XKU_SMIME))
670         return 0;
671     if (ca) {
672         int ca_ret;
673         ca_ret = check_ca(x);
674         if (!ca_ret)
675             return 0;
676         /* check nsCertType if present */
677         if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
678             return ca_ret;
679         else
680             return 0;
681     }
682     if (x->ex_flags & EXFLAG_NSCERT) {
683         if (x->ex_nscert & NS_SMIME)
684             return 1;
685         /* Workaround for some buggy certificates */
686         if (x->ex_nscert & NS_SSL_CLIENT)
687             return 2;
688         return 0;
689     }
690     return 1;
691 }
692
693 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
694                                     int ca)
695 {
696     int ret;
697     ret = purpose_smime(x, ca);
698     if (!ret || ca)
699         return ret;
700     if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
701         return 0;
702     return ret;
703 }
704
705 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
706                                        int ca)
707 {
708     int ret;
709     ret = purpose_smime(x, ca);
710     if (!ret || ca)
711         return ret;
712     if (ku_reject(x, KU_KEY_ENCIPHERMENT))
713         return 0;
714     return ret;
715 }
716
717 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
718                                   int ca)
719 {
720     if (ca) {
721         int ca_ret;
722         if ((ca_ret = check_ca(x)) != 2)
723             return ca_ret;
724         else
725             return 0;
726     }
727     if (ku_reject(x, KU_CRL_SIGN))
728         return 0;
729     return 1;
730 }
731
732 /*
733  * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
734  * is valid. Additional checks must be made on the chain.
735  */
736
737 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
738 {
739     /*
740      * Must be a valid CA.  Should we really support the "I don't know" value
741      * (2)?
742      */
743     if (ca)
744         return check_ca(x);
745     /* leaf certificate is checked in OCSP_verify() */
746     return 1;
747 }
748
749 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
750                                         int ca)
751 {
752     int i_ext;
753
754     /* If ca is true we must return if this is a valid CA certificate. */
755     if (ca)
756         return check_ca(x);
757
758     /*
759      * Check the optional key usage field:
760      * if Key Usage is present, it must be one of digitalSignature
761      * and/or nonRepudiation (other values are not consistent and shall
762      * be rejected).
763      */
764     if ((x->ex_flags & EXFLAG_KUSAGE)
765         && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
766             !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
767         return 0;
768
769     /* Only time stamp key usage is permitted and it's required. */
770     if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
771         return 0;
772
773     /* Extended Key Usage MUST be critical */
774     i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
775     if (i_ext >= 0) {
776         X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
777         if (!X509_EXTENSION_get_critical(ext))
778             return 0;
779     }
780
781     return 1;
782 }
783
784 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
785 {
786     return 1;
787 }
788
789 /*-
790  * Various checks to see if one certificate issued the second.
791  * This can be used to prune a set of possible issuer certificates
792  * which have been looked up using some simple method such as by
793  * subject name.
794  * These are:
795  * 1. Check issuer_name(subject) == subject_name(issuer)
796  * 2. If akid(subject) exists check it matches issuer
797  * 3. If key_usage(issuer) exists check it supports certificate signing
798  * returns 0 for OK, positive for reason for mismatch, reasons match
799  * codes for X509_verify_cert()
800  */
801
802 int X509_check_issued(X509 *issuer, X509 *subject)
803 {
804     if (X509_NAME_cmp(X509_get_subject_name(issuer),
805                       X509_get_issuer_name(subject)))
806         return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
807
808     x509v3_cache_extensions(issuer);
809     if (issuer->ex_flags & EXFLAG_INVALID)
810         return X509_V_ERR_UNSPECIFIED;
811     x509v3_cache_extensions(subject);
812     if (subject->ex_flags & EXFLAG_INVALID)
813         return X509_V_ERR_UNSPECIFIED;
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
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         int 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
868 uint32_t X509_get_extension_flags(X509 *x)
869 {
870     /* Call for side-effect of computing hash and caching extensions */
871     X509_check_purpose(x, -1, -1);
872     return x->ex_flags;
873 }
874
875 uint32_t X509_get_key_usage(X509 *x)
876 {
877     /* Call for side-effect of computing hash and caching extensions */
878     if (X509_check_purpose(x, -1, -1) != 1)
879         return 0;
880     if (x->ex_flags & EXFLAG_KUSAGE)
881         return x->ex_kusage;
882     return UINT32_MAX;
883 }
884
885 uint32_t X509_get_extended_key_usage(X509 *x)
886 {
887     /* Call for side-effect of computing hash and caching extensions */
888     if (X509_check_purpose(x, -1, -1) != 1)
889         return 0;
890     if (x->ex_flags & EXFLAG_XKUSAGE)
891         return x->ex_xkusage;
892     return UINT32_MAX;
893 }
894
895 const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
896 {
897     /* Call for side-effect of computing hash and caching extensions */
898     if (X509_check_purpose(x, -1, -1) != 1)
899         return NULL;
900     return x->skid;
901 }
902
903 const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
904 {
905     /* Call for side-effect of computing hash and caching extensions */
906     if (X509_check_purpose(x, -1, -1) != 1)
907         return NULL;
908     return (x->akid != NULL ? x->akid->keyid : NULL);
909 }
910
911 const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
912 {
913     /* Call for side-effect of computing hash and caching extensions */
914     if (X509_check_purpose(x, -1, -1) != 1)
915         return NULL;
916     return (x->akid != NULL ? x->akid->issuer : NULL);
917 }
918
919 const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
920 {
921     /* Call for side-effect of computing hash and caching extensions */
922     if (X509_check_purpose(x, -1, -1) != 1)
923         return NULL;
924     return (x->akid != NULL ? x->akid->serial : NULL);
925 }
926
927 long X509_get_pathlen(X509 *x)
928 {
929     /* Called for side effect of caching extensions */
930     if (X509_check_purpose(x, -1, -1) != 1
931             || (x->ex_flags & EXFLAG_BCONS) == 0)
932         return -1;
933     return x->ex_pathlen;
934 }
935
936 long X509_get_proxy_pathlen(X509 *x)
937 {
938     /* Called for side effect of caching extensions */
939     if (X509_check_purpose(x, -1, -1) != 1
940             || (x->ex_flags & EXFLAG_PROXY) == 0)
941         return -1;
942     return x->ex_pcpathlen;
943 }