]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssl/crypto/x509v3/v3_purp.c
Import PCG-C into sys/contrib
[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                 x->ex_flags |= EXFLAG_INVALID;
389                 x->ex_pathlen = 0;
390             } else {
391                 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
392                 if (!bs->ca && x->ex_pathlen != 0) {
393                     x->ex_flags |= EXFLAG_INVALID;
394                     x->ex_pathlen = 0;
395                 }
396             }
397         } else
398             x->ex_pathlen = -1;
399         BASIC_CONSTRAINTS_free(bs);
400         x->ex_flags |= EXFLAG_BCONS;
401     } else if (i != -1) {
402         x->ex_flags |= EXFLAG_INVALID;
403     }
404     /* Handle proxy certificates */
405     if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) {
406         if (x->ex_flags & EXFLAG_CA
407             || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
408             || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
409             x->ex_flags |= EXFLAG_INVALID;
410         }
411         if (pci->pcPathLengthConstraint) {
412             x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
413         } else
414             x->ex_pcpathlen = -1;
415         PROXY_CERT_INFO_EXTENSION_free(pci);
416         x->ex_flags |= EXFLAG_PROXY;
417     } else if (i != -1) {
418         x->ex_flags |= EXFLAG_INVALID;
419     }
420     /* Handle key usage */
421     if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) {
422         if (usage->length > 0) {
423             x->ex_kusage = usage->data[0];
424             if (usage->length > 1)
425                 x->ex_kusage |= usage->data[1] << 8;
426         } else
427             x->ex_kusage = 0;
428         x->ex_flags |= EXFLAG_KUSAGE;
429         ASN1_BIT_STRING_free(usage);
430     } else if (i != -1) {
431         x->ex_flags |= EXFLAG_INVALID;
432     }
433     x->ex_xkusage = 0;
434     if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) {
435         x->ex_flags |= EXFLAG_XKUSAGE;
436         for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
437             switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
438             case NID_server_auth:
439                 x->ex_xkusage |= XKU_SSL_SERVER;
440                 break;
441
442             case NID_client_auth:
443                 x->ex_xkusage |= XKU_SSL_CLIENT;
444                 break;
445
446             case NID_email_protect:
447                 x->ex_xkusage |= XKU_SMIME;
448                 break;
449
450             case NID_code_sign:
451                 x->ex_xkusage |= XKU_CODE_SIGN;
452                 break;
453
454             case NID_ms_sgc:
455             case NID_ns_sgc:
456                 x->ex_xkusage |= XKU_SGC;
457                 break;
458
459             case NID_OCSP_sign:
460                 x->ex_xkusage |= XKU_OCSP_SIGN;
461                 break;
462
463             case NID_time_stamp:
464                 x->ex_xkusage |= XKU_TIMESTAMP;
465                 break;
466
467             case NID_dvcs:
468                 x->ex_xkusage |= XKU_DVCS;
469                 break;
470
471             case NID_anyExtendedKeyUsage:
472                 x->ex_xkusage |= XKU_ANYEKU;
473                 break;
474             }
475         }
476         sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
477     } else if (i != -1) {
478         x->ex_flags |= EXFLAG_INVALID;
479     }
480
481     if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) {
482         if (ns->length > 0)
483             x->ex_nscert = ns->data[0];
484         else
485             x->ex_nscert = 0;
486         x->ex_flags |= EXFLAG_NSCERT;
487         ASN1_BIT_STRING_free(ns);
488     } else if (i != -1) {
489         x->ex_flags |= EXFLAG_INVALID;
490     }
491     x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
492     if (x->skid == NULL && i != -1)
493         x->ex_flags |= EXFLAG_INVALID;
494     x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
495     if (x->akid == NULL && i != -1)
496         x->ex_flags |= EXFLAG_INVALID;
497     /* Does subject name match issuer ? */
498     if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
499         x->ex_flags |= EXFLAG_SI;
500         /* If SKID matches AKID also indicate self signed */
501         if (X509_check_akid(x, x->akid) == X509_V_OK &&
502             !ku_reject(x, KU_KEY_CERT_SIGN))
503             x->ex_flags |= EXFLAG_SS;
504     }
505     x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
506     if (x->altname == NULL && i != -1)
507         x->ex_flags |= EXFLAG_INVALID;
508     x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
509     if (x->nc == NULL && i != -1)
510         x->ex_flags |= EXFLAG_INVALID;
511     if (!setup_crldp(x))
512         x->ex_flags |= EXFLAG_INVALID;
513
514 #ifndef OPENSSL_NO_RFC3779
515     x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
516     if (x->rfc3779_addr == NULL && i != -1)
517         x->ex_flags |= EXFLAG_INVALID;
518     x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
519     if (x->rfc3779_asid == NULL && i != -1)
520         x->ex_flags |= EXFLAG_INVALID;
521 #endif
522     for (i = 0; i < X509_get_ext_count(x); i++) {
523         ex = X509_get_ext(x, i);
524         if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
525             == NID_freshest_crl)
526             x->ex_flags |= EXFLAG_FRESHEST;
527         if (!X509_EXTENSION_get_critical(ex))
528             continue;
529         if (!X509_supported_extension(ex)) {
530             x->ex_flags |= EXFLAG_CRITICAL;
531             break;
532         }
533     }
534     x509_init_sig_info(x);
535     x->ex_flags |= EXFLAG_SET;
536 #ifdef tsan_st_rel
537     tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
538     /*
539      * Above store triggers fast lock-free check in the beginning of the
540      * function. But one has to ensure that the structure is "stable", i.e.
541      * all stores are visible on all processors. Hence the release fence.
542      */
543 #endif
544     CRYPTO_THREAD_unlock(x->lock);
545 }
546
547 /*-
548  * CA checks common to all purposes
549  * return codes:
550  * 0 not a CA
551  * 1 is a CA
552  * 2 Only possible in older versions of openSSL when basicConstraints are absent
553  *   new versions will not return this value. May be a CA
554  * 3 basicConstraints absent but self signed V1.
555  * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
556  * 5 Netscape specific CA Flags present
557  */
558
559 static int check_ca(const X509 *x)
560 {
561     /* keyUsage if present should allow cert signing */
562     if (ku_reject(x, KU_KEY_CERT_SIGN))
563         return 0;
564     if (x->ex_flags & EXFLAG_BCONS) {
565         if (x->ex_flags & EXFLAG_CA)
566             return 1;
567         /* If basicConstraints says not a CA then say so */
568         else
569             return 0;
570     } else {
571         /* we support V1 roots for...  uh, I don't really know why. */
572         if ((x->ex_flags & V1_ROOT) == V1_ROOT)
573             return 3;
574         /*
575          * If key usage present it must have certSign so tolerate it
576          */
577         else if (x->ex_flags & EXFLAG_KUSAGE)
578             return 4;
579         /* Older certificates could have Netscape-specific CA types */
580         else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
581             return 5;
582         /* can this still be regarded a CA certificate?  I doubt it */
583         return 0;
584     }
585 }
586
587 void X509_set_proxy_flag(X509 *x)
588 {
589     x->ex_flags |= EXFLAG_PROXY;
590 }
591
592 void X509_set_proxy_pathlen(X509 *x, long l)
593 {
594     x->ex_pcpathlen = l;
595 }
596
597 int X509_check_ca(X509 *x)
598 {
599     x509v3_cache_extensions(x);
600
601     return check_ca(x);
602 }
603
604 /* Check SSL CA: common checks for SSL client and server */
605 static int check_ssl_ca(const X509 *x)
606 {
607     int ca_ret;
608     ca_ret = check_ca(x);
609     if (!ca_ret)
610         return 0;
611     /* check nsCertType if present */
612     if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
613         return ca_ret;
614     else
615         return 0;
616 }
617
618 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
619                                     int ca)
620 {
621     if (xku_reject(x, XKU_SSL_CLIENT))
622         return 0;
623     if (ca)
624         return check_ssl_ca(x);
625     /* We need to do digital signatures or key agreement */
626     if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
627         return 0;
628     /* nsCertType if present should allow SSL client use */
629     if (ns_reject(x, NS_SSL_CLIENT))
630         return 0;
631     return 1;
632 }
633
634 /*
635  * Key usage needed for TLS/SSL server: digital signature, encipherment or
636  * key agreement. The ssl code can check this more thoroughly for individual
637  * key types.
638  */
639 #define KU_TLS \
640         KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
641
642 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
643                                     int ca)
644 {
645     if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
646         return 0;
647     if (ca)
648         return check_ssl_ca(x);
649
650     if (ns_reject(x, NS_SSL_SERVER))
651         return 0;
652     if (ku_reject(x, KU_TLS))
653         return 0;
654
655     return 1;
656
657 }
658
659 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
660                                        int ca)
661 {
662     int ret;
663     ret = check_purpose_ssl_server(xp, x, ca);
664     if (!ret || ca)
665         return ret;
666     /* We need to encipher or Netscape complains */
667     if (ku_reject(x, KU_KEY_ENCIPHERMENT))
668         return 0;
669     return ret;
670 }
671
672 /* common S/MIME checks */
673 static int purpose_smime(const X509 *x, int ca)
674 {
675     if (xku_reject(x, XKU_SMIME))
676         return 0;
677     if (ca) {
678         int ca_ret;
679         ca_ret = check_ca(x);
680         if (!ca_ret)
681             return 0;
682         /* check nsCertType if present */
683         if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
684             return ca_ret;
685         else
686             return 0;
687     }
688     if (x->ex_flags & EXFLAG_NSCERT) {
689         if (x->ex_nscert & NS_SMIME)
690             return 1;
691         /* Workaround for some buggy certificates */
692         if (x->ex_nscert & NS_SSL_CLIENT)
693             return 2;
694         return 0;
695     }
696     return 1;
697 }
698
699 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
700                                     int ca)
701 {
702     int ret;
703     ret = purpose_smime(x, ca);
704     if (!ret || ca)
705         return ret;
706     if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
707         return 0;
708     return ret;
709 }
710
711 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
712                                        int ca)
713 {
714     int ret;
715     ret = purpose_smime(x, ca);
716     if (!ret || ca)
717         return ret;
718     if (ku_reject(x, KU_KEY_ENCIPHERMENT))
719         return 0;
720     return ret;
721 }
722
723 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
724                                   int ca)
725 {
726     if (ca) {
727         int ca_ret;
728         if ((ca_ret = check_ca(x)) != 2)
729             return ca_ret;
730         else
731             return 0;
732     }
733     if (ku_reject(x, KU_CRL_SIGN))
734         return 0;
735     return 1;
736 }
737
738 /*
739  * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
740  * is valid. Additional checks must be made on the chain.
741  */
742
743 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
744 {
745     /*
746      * Must be a valid CA.  Should we really support the "I don't know" value
747      * (2)?
748      */
749     if (ca)
750         return check_ca(x);
751     /* leaf certificate is checked in OCSP_verify() */
752     return 1;
753 }
754
755 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
756                                         int ca)
757 {
758     int i_ext;
759
760     /* If ca is true we must return if this is a valid CA certificate. */
761     if (ca)
762         return check_ca(x);
763
764     /*
765      * Check the optional key usage field:
766      * if Key Usage is present, it must be one of digitalSignature
767      * and/or nonRepudiation (other values are not consistent and shall
768      * be rejected).
769      */
770     if ((x->ex_flags & EXFLAG_KUSAGE)
771         && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
772             !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
773         return 0;
774
775     /* Only time stamp key usage is permitted and it's required. */
776     if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
777         return 0;
778
779     /* Extended Key Usage MUST be critical */
780     i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
781     if (i_ext >= 0) {
782         X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
783         if (!X509_EXTENSION_get_critical(ext))
784             return 0;
785     }
786
787     return 1;
788 }
789
790 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
791 {
792     return 1;
793 }
794
795 /*-
796  * Various checks to see if one certificate issued the second.
797  * This can be used to prune a set of possible issuer certificates
798  * which have been looked up using some simple method such as by
799  * subject name.
800  * These are:
801  * 1. Check issuer_name(subject) == subject_name(issuer)
802  * 2. If akid(subject) exists check it matches issuer
803  * 3. If key_usage(issuer) exists check it supports certificate signing
804  * returns 0 for OK, positive for reason for mismatch, reasons match
805  * codes for X509_verify_cert()
806  */
807
808 int X509_check_issued(X509 *issuer, X509 *subject)
809 {
810     if (X509_NAME_cmp(X509_get_subject_name(issuer),
811                       X509_get_issuer_name(subject)))
812         return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
813
814     x509v3_cache_extensions(issuer);
815     if (issuer->ex_flags & EXFLAG_INVALID)
816         return X509_V_ERR_UNSPECIFIED;
817     x509v3_cache_extensions(subject);
818     if (subject->ex_flags & EXFLAG_INVALID)
819         return X509_V_ERR_UNSPECIFIED;
820
821     if (subject->akid) {
822         int ret = X509_check_akid(issuer, subject->akid);
823         if (ret != X509_V_OK)
824             return ret;
825     }
826
827     if (subject->ex_flags & EXFLAG_PROXY) {
828         if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
829             return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
830     } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
831         return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
832     return X509_V_OK;
833 }
834
835 int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
836 {
837
838     if (!akid)
839         return X509_V_OK;
840
841     /* Check key ids (if present) */
842     if (akid->keyid && issuer->skid &&
843         ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
844         return X509_V_ERR_AKID_SKID_MISMATCH;
845     /* Check serial number */
846     if (akid->serial &&
847         ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
848         return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
849     /* Check issuer name */
850     if (akid->issuer) {
851         /*
852          * Ugh, for some peculiar reason AKID includes SEQUENCE OF
853          * GeneralName. So look for a DirName. There may be more than one but
854          * we only take any notice of the first.
855          */
856         GENERAL_NAMES *gens;
857         GENERAL_NAME *gen;
858         X509_NAME *nm = NULL;
859         int i;
860         gens = akid->issuer;
861         for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
862             gen = sk_GENERAL_NAME_value(gens, i);
863             if (gen->type == GEN_DIRNAME) {
864                 nm = gen->d.dirn;
865                 break;
866             }
867         }
868         if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
869             return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
870     }
871     return X509_V_OK;
872 }
873
874 uint32_t X509_get_extension_flags(X509 *x)
875 {
876     /* Call for side-effect of computing hash and caching extensions */
877     X509_check_purpose(x, -1, -1);
878     return x->ex_flags;
879 }
880
881 uint32_t X509_get_key_usage(X509 *x)
882 {
883     /* Call for side-effect of computing hash and caching extensions */
884     if (X509_check_purpose(x, -1, -1) != 1)
885         return 0;
886     if (x->ex_flags & EXFLAG_KUSAGE)
887         return x->ex_kusage;
888     return UINT32_MAX;
889 }
890
891 uint32_t X509_get_extended_key_usage(X509 *x)
892 {
893     /* Call for side-effect of computing hash and caching extensions */
894     if (X509_check_purpose(x, -1, -1) != 1)
895         return 0;
896     if (x->ex_flags & EXFLAG_XKUSAGE)
897         return x->ex_xkusage;
898     return UINT32_MAX;
899 }
900
901 const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
902 {
903     /* Call for side-effect of computing hash and caching extensions */
904     if (X509_check_purpose(x, -1, -1) != 1)
905         return NULL;
906     return x->skid;
907 }
908
909 const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
910 {
911     /* Call for side-effect of computing hash and caching extensions */
912     if (X509_check_purpose(x, -1, -1) != 1)
913         return NULL;
914     return (x->akid != NULL ? x->akid->keyid : NULL);
915 }
916
917 const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
918 {
919     /* Call for side-effect of computing hash and caching extensions */
920     if (X509_check_purpose(x, -1, -1) != 1)
921         return NULL;
922     return (x->akid != NULL ? x->akid->issuer : NULL);
923 }
924
925 const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
926 {
927     /* Call for side-effect of computing hash and caching extensions */
928     if (X509_check_purpose(x, -1, -1) != 1)
929         return NULL;
930     return (x->akid != NULL ? x->akid->serial : NULL);
931 }
932
933 long X509_get_pathlen(X509 *x)
934 {
935     /* Called for side effect of caching extensions */
936     if (X509_check_purpose(x, -1, -1) != 1
937             || (x->ex_flags & EXFLAG_BCONS) == 0)
938         return -1;
939     return x->ex_pathlen;
940 }
941
942 long X509_get_proxy_pathlen(X509 *x)
943 {
944     /* Called for side effect of caching extensions */
945     if (X509_check_purpose(x, -1, -1) != 1
946             || (x->ex_flags & EXFLAG_PROXY) == 0)
947         return -1;
948     return x->ex_pcpathlen;
949 }