/src/openssl/crypto/x509/v3_purp.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 1999-2025 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (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  |  | #include "x509_local.h"  | 
18  |  |  | 
19  |  | static int check_ssl_ca(const X509 *x);  | 
20  |  | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,  | 
21  |  |                                     int non_leaf);  | 
22  |  | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,  | 
23  |  |                                     int non_leaf);  | 
24  |  | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,  | 
25  |  |                                        int non_leaf);  | 
26  |  | static int purpose_smime(const X509 *x, int non_leaf);  | 
27  |  | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,  | 
28  |  |                                     int non_leaf);  | 
29  |  | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,  | 
30  |  |                                        int non_leaf);  | 
31  |  | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,  | 
32  |  |                                   int non_leaf);  | 
33  |  | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,  | 
34  |  |                                         int non_leaf);  | 
35  |  | static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x,  | 
36  |  |                                         int non_leaf);  | 
37  |  | static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x,  | 
38  |  |                             int non_leaf);  | 
39  |  | static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x,  | 
40  |  |                                      int non_leaf);  | 
41  |  |  | 
42  |  | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b);  | 
43  |  | static void xptable_free(X509_PURPOSE *p);  | 
44  |  |  | 
45  |  | /* note that the id must be unique and for the standard entries == idx + 1 */  | 
46  |  | static X509_PURPOSE xstandard[] = { | 
47  |  |     {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, | 
48  |  |      check_purpose_ssl_client, "SSL client", "sslclient", NULL},  | 
49  |  |     {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, | 
50  |  |      check_purpose_ssl_server, "SSL server", "sslserver", NULL},  | 
51  |  |     {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, | 
52  |  |      check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},  | 
53  |  |     {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, | 
54  |  |      "S/MIME signing", "smimesign", NULL},  | 
55  |  |     {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, | 
56  |  |      check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},  | 
57  |  |     {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, | 
58  |  |      "CRL signing", "crlsign", NULL},  | 
59  |  |     {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check_purpose, | 
60  |  |      "Any Purpose", "any",  | 
61  |  |      NULL},  | 
62  |  |     {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, check_purpose_ocsp_helper, | 
63  |  |      "OCSP helper", "ocsphelper", NULL},  | 
64  |  |     {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, | 
65  |  |      check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign",  | 
66  |  |      NULL},  | 
67  |  |     {X509_PURPOSE_CODE_SIGN, X509_TRUST_OBJECT_SIGN, 0, | 
68  |  |      check_purpose_code_sign, "Code signing", "codesign",  | 
69  |  |      NULL},  | 
70  |  | };  | 
71  |  |  | 
72  | 0  | #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard)  | 
73  |  |  | 
74  |  | /* the id must be unique, but there may be gaps and maybe table is not sorted */  | 
75  |  | static STACK_OF(X509_PURPOSE) *xptable = NULL;  | 
76  |  |  | 
77  |  | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b)  | 
78  | 0  | { | 
79  | 0  |     return (*a)->purpose - (*b)->purpose;  | 
80  | 0  | }  | 
81  |  |  | 
82  |  | /*  | 
83  |  |  * As much as I'd like to make X509_check_purpose use a "const" X509* I really  | 
84  |  |  * can't because it does recalculate hashes and do other non-const things.  | 
85  |  |  * If id == -1 it just calls x509v3_cache_extensions() for its side-effect.  | 
86  |  |  * Returns 1 on success, 0 if x does not allow purpose, -1 on (internal) error.  | 
87  |  |  */  | 
88  |  | int X509_check_purpose(X509 *x, int id, int non_leaf)  | 
89  | 0  | { | 
90  | 0  |     int idx;  | 
91  | 0  |     const X509_PURPOSE *pt;  | 
92  |  | 
  | 
93  | 0  |     if (!ossl_x509v3_cache_extensions(x))  | 
94  | 0  |         return -1;  | 
95  | 0  |     if (id == -1)  | 
96  | 0  |         return 1;  | 
97  |  |  | 
98  | 0  |     idx = X509_PURPOSE_get_by_id(id);  | 
99  | 0  |     if (idx == -1)  | 
100  | 0  |         return -1;  | 
101  | 0  |     pt = X509_PURPOSE_get0(idx);  | 
102  | 0  |     return pt->check_purpose(pt, x, non_leaf);  | 
103  | 0  | }  | 
104  |  |  | 
105  |  | /* resets to default (any) purpose if purpose == X509_PURPOSE_DEFAULT_ANY (0) */  | 
106  |  | int X509_PURPOSE_set(int *p, int purpose)  | 
107  | 0  | { | 
108  | 0  |     if (purpose != X509_PURPOSE_DEFAULT_ANY && X509_PURPOSE_get_by_id(purpose) == -1) { | 
109  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE);  | 
110  | 0  |         return 0;  | 
111  | 0  |     }  | 
112  | 0  |     *p = purpose;  | 
113  | 0  |     return 1;  | 
114  | 0  | }  | 
115  |  |  | 
116  |  | int X509_PURPOSE_get_count(void)  | 
117  | 0  | { | 
118  | 0  |     if (!xptable)  | 
119  | 0  |         return X509_PURPOSE_COUNT;  | 
120  | 0  |     return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;  | 
121  | 0  | }  | 
122  |  |  | 
123  |  | /* find smallest identifier not yet taken - note there might be gaps */  | 
124  |  | int X509_PURPOSE_get_unused_id(ossl_unused OSSL_LIB_CTX *libctx)  | 
125  | 0  | { | 
126  | 0  |     int id = X509_PURPOSE_MAX + 1;  | 
127  |  | 
  | 
128  | 0  |     while (X509_PURPOSE_get_by_id(id) != -1)  | 
129  | 0  |         id++;  | 
130  | 0  |     return id; /* is guaranteed to be unique and > X509_PURPOSE_MAX and != 0 */  | 
131  | 0  | }  | 
132  |  |  | 
133  |  | X509_PURPOSE *X509_PURPOSE_get0(int idx)  | 
134  | 0  | { | 
135  | 0  |     if (idx < 0)  | 
136  | 0  |         return NULL;  | 
137  | 0  |     if (idx < (int)X509_PURPOSE_COUNT)  | 
138  | 0  |         return xstandard + idx;  | 
139  | 0  |     return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);  | 
140  | 0  | }  | 
141  |  |  | 
142  |  | int X509_PURPOSE_get_by_sname(const char *sname)  | 
143  | 0  | { | 
144  | 0  |     int i;  | 
145  | 0  |     X509_PURPOSE *xptmp;  | 
146  |  | 
  | 
147  | 0  |     for (i = 0; i < X509_PURPOSE_get_count(); i++) { | 
148  | 0  |         xptmp = X509_PURPOSE_get0(i);  | 
149  | 0  |         if (strcmp(xptmp->sname, sname) == 0)  | 
150  | 0  |             return i;  | 
151  | 0  |     }  | 
152  | 0  |     return -1;  | 
153  | 0  | }  | 
154  |  |  | 
155  |  | /* Returns -1 on error, else an index => 0 in standard/extended purpose table */  | 
156  |  | int X509_PURPOSE_get_by_id(int purpose)  | 
157  | 0  | { | 
158  | 0  |     X509_PURPOSE tmp;  | 
159  | 0  |     int idx;  | 
160  |  | 
  | 
161  | 0  |     if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX)  | 
162  | 0  |         return purpose - X509_PURPOSE_MIN;  | 
163  | 0  |     if (xptable == NULL)  | 
164  | 0  |         return -1;  | 
165  | 0  |     tmp.purpose = purpose;  | 
166  | 0  |     idx = sk_X509_PURPOSE_find(xptable, &tmp);  | 
167  | 0  |     if (idx < 0)  | 
168  | 0  |         return -1;  | 
169  | 0  |     return idx + X509_PURPOSE_COUNT;  | 
170  | 0  | }  | 
171  |  |  | 
172  |  | /*  | 
173  |  |  * Add purpose entry identified by |sname|. |id| must be >= X509_PURPOSE_MIN.  | 
174  |  |  * May also be used to modify existing entry, including changing its id.  | 
175  |  |  */  | 
176  |  | int X509_PURPOSE_add(int id, int trust, int flags,  | 
177  |  |                      int (*ck) (const X509_PURPOSE *, const X509 *, int),  | 
178  |  |                      const char *name, const char *sname, void *arg)  | 
179  | 0  | { | 
180  | 0  |     int old_id = 0;  | 
181  | 0  |     int idx;  | 
182  | 0  |     X509_PURPOSE *ptmp;  | 
183  |  | 
  | 
184  | 0  |     if (id < X509_PURPOSE_MIN) { | 
185  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE);  | 
186  | 0  |         return 0;  | 
187  | 0  |     }  | 
188  | 0  |     if (trust < X509_TRUST_DEFAULT || name == NULL || sname == NULL || ck == NULL) { | 
189  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_INVALID_ARGUMENT);  | 
190  | 0  |         return 0;  | 
191  | 0  |     }  | 
192  |  |  | 
193  |  |     /* This is set according to what we change: application can't set it */  | 
194  | 0  |     flags &= ~X509_PURPOSE_DYNAMIC;  | 
195  |  |     /* This will always be set for application modified trust entries */  | 
196  | 0  |     flags |= X509_PURPOSE_DYNAMIC_NAME;  | 
197  |  |  | 
198  |  |     /* Get existing entry if any */  | 
199  | 0  |     idx = X509_PURPOSE_get_by_sname(sname);  | 
200  | 0  |     if (idx == -1) { /* Need a new entry */ | 
201  | 0  |         if (X509_PURPOSE_get_by_id(id) != -1) { | 
202  | 0  |             ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE);  | 
203  | 0  |             return 0;  | 
204  | 0  |         }  | 
205  | 0  |         if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL)  | 
206  | 0  |             return 0;  | 
207  | 0  |         ptmp->flags = X509_PURPOSE_DYNAMIC;  | 
208  | 0  |     } else { | 
209  | 0  |         ptmp = X509_PURPOSE_get0(idx);  | 
210  | 0  |         old_id = ptmp->purpose;  | 
211  | 0  |         if (id != old_id && X509_PURPOSE_get_by_id(id) != -1) { | 
212  | 0  |             ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE);  | 
213  | 0  |             return 0;  | 
214  | 0  |         }  | 
215  | 0  |     }  | 
216  |  |  | 
217  |  |     /* OPENSSL_free existing name if dynamic */  | 
218  | 0  |     if ((ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) { | 
219  | 0  |         OPENSSL_free(ptmp->name);  | 
220  | 0  |         OPENSSL_free(ptmp->sname);  | 
221  | 0  |     }  | 
222  |  |     /* Dup supplied name */  | 
223  | 0  |     ptmp->name = OPENSSL_strdup(name);  | 
224  | 0  |     ptmp->sname = OPENSSL_strdup(sname);  | 
225  | 0  |     if (ptmp->name == NULL || ptmp->sname == NULL)  | 
226  | 0  |         goto err;  | 
227  |  |     /* Keep the dynamic flag of existing entry */  | 
228  | 0  |     ptmp->flags &= X509_PURPOSE_DYNAMIC;  | 
229  |  |     /* Set all other flags */  | 
230  | 0  |     ptmp->flags |= flags;  | 
231  |  | 
  | 
232  | 0  |     ptmp->purpose = id;  | 
233  | 0  |     ptmp->trust = trust;  | 
234  | 0  |     ptmp->check_purpose = ck;  | 
235  | 0  |     ptmp->usr_data = arg;  | 
236  |  |  | 
237  |  |     /* If its a new entry manage the dynamic table */  | 
238  | 0  |     if (idx == -1) { | 
239  | 0  |         if (xptable == NULL  | 
240  | 0  |             && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) { | 
241  | 0  |             ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);  | 
242  | 0  |             goto err;  | 
243  | 0  |         }  | 
244  | 0  |         if (!sk_X509_PURPOSE_push(xptable, ptmp)) { | 
245  | 0  |             ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);  | 
246  | 0  |             goto err;  | 
247  | 0  |         }  | 
248  | 0  |     } else if (id != old_id) { | 
249  |  |         /* on changing existing entry id, make sure to reset 'sorted' */  | 
250  | 0  |         (void)sk_X509_PURPOSE_set(xptable, idx, ptmp);  | 
251  | 0  |     }  | 
252  | 0  |     return 1;  | 
253  | 0  |  err:  | 
254  | 0  |     if (idx == -1) { | 
255  | 0  |         OPENSSL_free(ptmp->name);  | 
256  | 0  |         OPENSSL_free(ptmp->sname);  | 
257  | 0  |         OPENSSL_free(ptmp);  | 
258  | 0  |     }  | 
259  | 0  |     return 0;  | 
260  | 0  | }  | 
261  |  |  | 
262  |  | static void xptable_free(X509_PURPOSE *p)  | 
263  | 0  | { | 
264  | 0  |     if (p == NULL)  | 
265  | 0  |         return;  | 
266  | 0  |     if ((p->flags & X509_PURPOSE_DYNAMIC) != 0) { | 
267  | 0  |         if ((p->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) { | 
268  | 0  |             OPENSSL_free(p->name);  | 
269  | 0  |             OPENSSL_free(p->sname);  | 
270  | 0  |         }  | 
271  | 0  |         OPENSSL_free(p);  | 
272  | 0  |     }  | 
273  | 0  | }  | 
274  |  |  | 
275  |  | void X509_PURPOSE_cleanup(void)  | 
276  | 0  | { | 
277  | 0  |     sk_X509_PURPOSE_pop_free(xptable, xptable_free);  | 
278  | 0  |     xptable = NULL;  | 
279  | 0  | }  | 
280  |  |  | 
281  |  | int X509_PURPOSE_get_id(const X509_PURPOSE *xp)  | 
282  | 0  | { | 
283  | 0  |     return xp->purpose;  | 
284  | 0  | }  | 
285  |  |  | 
286  |  | char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp)  | 
287  | 0  | { | 
288  | 0  |     return xp->name;  | 
289  | 0  | }  | 
290  |  |  | 
291  |  | char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp)  | 
292  | 0  | { | 
293  | 0  |     return xp->sname;  | 
294  | 0  | }  | 
295  |  |  | 
296  |  | int X509_PURPOSE_get_trust(const X509_PURPOSE *xp)  | 
297  | 0  | { | 
298  | 0  |     return xp->trust;  | 
299  | 0  | }  | 
300  |  |  | 
301  |  | static int nid_cmp(const int *a, const int *b)  | 
302  | 0  | { | 
303  | 0  |     return *a - *b;  | 
304  | 0  | }  | 
305  |  |  | 
306  |  | DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid);  | 
307  |  | IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid);  | 
308  |  |  | 
309  |  | int X509_supported_extension(X509_EXTENSION *ex)  | 
310  | 0  | { | 
311  |  |     /*  | 
312  |  |      * This table is a list of the NIDs of supported extensions: that is  | 
313  |  |      * those which are used by the verify process. If an extension is  | 
314  |  |      * critical and doesn't appear in this list then the verify process will  | 
315  |  |      * normally reject the certificate. The list must be kept in numerical  | 
316  |  |      * order because it will be searched using bsearch.  | 
317  |  |      */  | 
318  | 0  |     static const int supported_nids[] = { | 
319  | 0  |         NID_netscape_cert_type, /* 71 */  | 
320  | 0  |         NID_key_usage,          /* 83 */  | 
321  | 0  |         NID_subject_alt_name,   /* 85 */  | 
322  | 0  |         NID_basic_constraints,  /* 87 */  | 
323  | 0  |         NID_certificate_policies, /* 89 */  | 
324  | 0  |         NID_crl_distribution_points, /* 103 */  | 
325  | 0  |         NID_ext_key_usage,      /* 126 */  | 
326  | 0  | #ifndef OPENSSL_NO_RFC3779  | 
327  | 0  |         NID_sbgp_ipAddrBlock,   /* 290 */  | 
328  | 0  |         NID_sbgp_autonomousSysNum, /* 291 */  | 
329  | 0  | #endif  | 
330  | 0  |         NID_id_pkix_OCSP_noCheck, /* 369 */  | 
331  | 0  |         NID_policy_constraints, /* 401 */  | 
332  | 0  |         NID_proxyCertInfo,      /* 663 */  | 
333  | 0  |         NID_name_constraints,   /* 666 */  | 
334  | 0  |         NID_policy_mappings,    /* 747 */  | 
335  | 0  |         NID_inhibit_any_policy  /* 748 */  | 
336  | 0  |     };  | 
337  |  | 
  | 
338  | 0  |     int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));  | 
339  |  | 
  | 
340  | 0  |     if (ex_nid == NID_undef)  | 
341  | 0  |         return 0;  | 
342  |  |  | 
343  | 0  |     if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))  | 
344  | 0  |         return 1;  | 
345  | 0  |     return 0;  | 
346  | 0  | }  | 
347  |  |  | 
348  |  | /* Returns 1 on success, 0 if x is invalid, -1 on (internal) error. */  | 
349  |  | static int setup_dp(const X509 *x, DIST_POINT *dp)  | 
350  | 0  | { | 
351  | 0  |     const X509_NAME *iname = NULL;  | 
352  | 0  |     int i;  | 
353  |  | 
  | 
354  | 0  |     if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) { | 
355  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT);  | 
356  | 0  |         return 0;  | 
357  | 0  |     }  | 
358  | 0  |     if (dp->reasons != NULL) { | 
359  | 0  |         if (dp->reasons->length > 0)  | 
360  | 0  |             dp->dp_reasons = dp->reasons->data[0];  | 
361  | 0  |         if (dp->reasons->length > 1)  | 
362  | 0  |             dp->dp_reasons |= (dp->reasons->data[1] << 8);  | 
363  | 0  |         dp->dp_reasons &= CRLDP_ALL_REASONS;  | 
364  | 0  |     } else { | 
365  | 0  |         dp->dp_reasons = CRLDP_ALL_REASONS;  | 
366  | 0  |     }  | 
367  | 0  |     if (dp->distpoint == NULL || dp->distpoint->type != 1)  | 
368  | 0  |         return 1;  | 
369  |  |  | 
370  |  |     /* Handle name fragment given by nameRelativeToCRLIssuer */  | 
371  |  |     /*  | 
372  |  |      * Note that the below way of determining iname is not really compliant  | 
373  |  |      * with https://tools.ietf.org/html/rfc5280#section-4.2.1.13  | 
374  |  |      * According to it, sk_GENERAL_NAME_num(dp->CRLissuer) MUST be <= 1  | 
375  |  |      * and any CRLissuer could be of type different to GEN_DIRNAME.  | 
376  |  |      */  | 
377  | 0  |     for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { | 
378  | 0  |         GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);  | 
379  |  | 
  | 
380  | 0  |         if (gen->type == GEN_DIRNAME) { | 
381  | 0  |             iname = gen->d.directoryName;  | 
382  | 0  |             break;  | 
383  | 0  |         }  | 
384  | 0  |     }  | 
385  | 0  |     if (iname == NULL)  | 
386  | 0  |         iname = X509_get_issuer_name(x);  | 
387  | 0  |     return DIST_POINT_set_dpname(dp->distpoint, iname) ? 1 : -1;  | 
388  | 0  | }  | 
389  |  |  | 
390  |  | /* Return 1 on success, 0 if x is invalid, -1 on (internal) error. */  | 
391  |  | static int setup_crldp(X509 *x)  | 
392  | 0  | { | 
393  | 0  |     int i;  | 
394  |  | 
  | 
395  | 0  |     x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);  | 
396  | 0  |     if (x->crldp == NULL && i != -1)  | 
397  | 0  |         return 0;  | 
398  |  |  | 
399  | 0  |     for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) { | 
400  | 0  |         int res = setup_dp(x, sk_DIST_POINT_value(x->crldp, i));  | 
401  |  | 
  | 
402  | 0  |         if (res < 1)  | 
403  | 0  |             return res;  | 
404  | 0  |     }  | 
405  | 0  |     return 1;  | 
406  | 0  | }  | 
407  |  |  | 
408  |  | /* Check that issuer public key algorithm matches subject signature algorithm */  | 
409  |  | static int check_sig_alg_match(const EVP_PKEY *issuer_key, const X509 *subject)  | 
410  | 0  | { | 
411  | 0  |     int subj_sig_nid;  | 
412  |  | 
  | 
413  | 0  |     if (issuer_key == NULL)  | 
414  | 0  |         return X509_V_ERR_NO_ISSUER_PUBLIC_KEY;  | 
415  | 0  |     if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm),  | 
416  | 0  |                             NULL, &subj_sig_nid) == 0)  | 
417  | 0  |         return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM;  | 
418  | 0  |     if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid))  | 
419  | 0  |         || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss))  | 
420  | 0  |         return X509_V_OK;  | 
421  | 0  |     return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH;  | 
422  | 0  | }  | 
423  |  |  | 
424  | 0  | #define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)  | 
425  |  | #define ku_reject(x, usage) \  | 
426  | 0  |     (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0)  | 
427  |  | #define xku_reject(x, usage) \  | 
428  | 0  |     (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0)  | 
429  |  | #define ns_reject(x, usage) \  | 
430  | 0  |     (((x)->ex_flags & EXFLAG_NSCERT) != 0 && ((x)->ex_nscert & (usage)) == 0)  | 
431  |  |  | 
432  |  | /*  | 
433  |  |  * Cache info on various X.509v3 extensions and further derived information,  | 
434  |  |  * e.g., if cert 'x' is self-issued, in x->ex_flags and other internal fields.  | 
435  |  |  * x->sha1_hash is filled in, or else EXFLAG_NO_FINGERPRINT is set in x->flags.  | 
436  |  |  * X509_SIG_INFO_VALID is set in x->flags if x->siginf was filled successfully.  | 
437  |  |  * Set EXFLAG_INVALID and return 0 in case the certificate is invalid.  | 
438  |  |  */  | 
439  |  | int ossl_x509v3_cache_extensions(X509 *x)  | 
440  | 0  | { | 
441  | 0  |     BASIC_CONSTRAINTS *bs;  | 
442  | 0  |     PROXY_CERT_INFO_EXTENSION *pci;  | 
443  | 0  |     ASN1_BIT_STRING *usage;  | 
444  | 0  |     ASN1_BIT_STRING *ns;  | 
445  | 0  |     EXTENDED_KEY_USAGE *extusage;  | 
446  | 0  |     int i;  | 
447  | 0  |     int res;  | 
448  |  | 
  | 
449  | 0  | #ifdef tsan_ld_acq  | 
450  |  |     /* Fast lock-free check, see end of the function for details. */  | 
451  | 0  |     if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))  | 
452  | 0  |         return (x->ex_flags & EXFLAG_INVALID) == 0;  | 
453  | 0  | #endif  | 
454  |  |  | 
455  | 0  |     if (!CRYPTO_THREAD_write_lock(x->lock))  | 
456  | 0  |         return 0;  | 
457  | 0  |     if ((x->ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */ | 
458  | 0  |         CRYPTO_THREAD_unlock(x->lock);  | 
459  | 0  |         return (x->ex_flags & EXFLAG_INVALID) == 0;  | 
460  | 0  |     }  | 
461  |  |  | 
462  | 0  |     ERR_set_mark();  | 
463  |  |  | 
464  |  |     /* Cache the SHA1 digest of the cert */  | 
465  | 0  |     if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL))  | 
466  | 0  |         x->ex_flags |= EXFLAG_NO_FINGERPRINT;  | 
467  |  |  | 
468  |  |     /* V1 should mean no extensions ... */  | 
469  | 0  |     if (X509_get_version(x) == X509_VERSION_1)  | 
470  | 0  |         x->ex_flags |= EXFLAG_V1;  | 
471  |  |  | 
472  |  |     /* Handle basic constraints */  | 
473  | 0  |     x->ex_pathlen = -1;  | 
474  | 0  |     if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL)) != NULL) { | 
475  | 0  |         if (bs->ca)  | 
476  | 0  |             x->ex_flags |= EXFLAG_CA;  | 
477  | 0  |         if (bs->pathlen != NULL) { | 
478  |  |             /*  | 
479  |  |              * The error case !bs->ca is checked by check_chain()  | 
480  |  |              * in case ctx->param->flags & X509_V_FLAG_X509_STRICT  | 
481  |  |              */  | 
482  | 0  |             if (bs->pathlen->type == V_ASN1_NEG_INTEGER) { | 
483  | 0  |                 ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN);  | 
484  | 0  |                 x->ex_flags |= EXFLAG_INVALID;  | 
485  | 0  |             } else { | 
486  | 0  |                 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);  | 
487  | 0  |             }  | 
488  | 0  |         }  | 
489  | 0  |         BASIC_CONSTRAINTS_free(bs);  | 
490  | 0  |         x->ex_flags |= EXFLAG_BCONS;  | 
491  | 0  |     } else if (i != -1) { | 
492  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
493  | 0  |     }  | 
494  |  |  | 
495  |  |     /* Handle proxy certificates */  | 
496  | 0  |     if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL)) != NULL) { | 
497  | 0  |         if ((x->ex_flags & EXFLAG_CA) != 0  | 
498  | 0  |             || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0  | 
499  | 0  |             || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) { | 
500  | 0  |             x->ex_flags |= EXFLAG_INVALID;  | 
501  | 0  |         }  | 
502  | 0  |         if (pci->pcPathLengthConstraint != NULL)  | 
503  | 0  |             x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);  | 
504  | 0  |         else  | 
505  | 0  |             x->ex_pcpathlen = -1;  | 
506  | 0  |         PROXY_CERT_INFO_EXTENSION_free(pci);  | 
507  | 0  |         x->ex_flags |= EXFLAG_PROXY;  | 
508  | 0  |     } else if (i != -1) { | 
509  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
510  | 0  |     }  | 
511  |  |  | 
512  |  |     /* Handle (basic) key usage */  | 
513  | 0  |     if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL)) != NULL) { | 
514  | 0  |         x->ex_kusage = 0;  | 
515  | 0  |         if (usage->length > 0) { | 
516  | 0  |             x->ex_kusage = usage->data[0];  | 
517  | 0  |             if (usage->length > 1)  | 
518  | 0  |                 x->ex_kusage |= usage->data[1] << 8;  | 
519  | 0  |         }  | 
520  | 0  |         x->ex_flags |= EXFLAG_KUSAGE;  | 
521  | 0  |         ASN1_BIT_STRING_free(usage);  | 
522  |  |         /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */  | 
523  | 0  |         if (x->ex_kusage == 0) { | 
524  | 0  |             ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE);  | 
525  | 0  |             x->ex_flags |= EXFLAG_INVALID;  | 
526  | 0  |         }  | 
527  | 0  |     } else if (i != -1) { | 
528  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
529  | 0  |     }  | 
530  |  |  | 
531  |  |     /* Handle extended key usage */  | 
532  | 0  |     x->ex_xkusage = 0;  | 
533  | 0  |     if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL)) != NULL) { | 
534  | 0  |         x->ex_flags |= EXFLAG_XKUSAGE;  | 
535  | 0  |         for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) { | 
536  | 0  |             switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) { | 
537  | 0  |             case NID_server_auth:  | 
538  | 0  |                 x->ex_xkusage |= XKU_SSL_SERVER;  | 
539  | 0  |                 break;  | 
540  | 0  |             case NID_client_auth:  | 
541  | 0  |                 x->ex_xkusage |= XKU_SSL_CLIENT;  | 
542  | 0  |                 break;  | 
543  | 0  |             case NID_email_protect:  | 
544  | 0  |                 x->ex_xkusage |= XKU_SMIME;  | 
545  | 0  |                 break;  | 
546  | 0  |             case NID_code_sign:  | 
547  | 0  |                 x->ex_xkusage |= XKU_CODE_SIGN;  | 
548  | 0  |                 break;  | 
549  | 0  |             case NID_ms_sgc:  | 
550  | 0  |             case NID_ns_sgc:  | 
551  | 0  |                 x->ex_xkusage |= XKU_SGC;  | 
552  | 0  |                 break;  | 
553  | 0  |             case NID_OCSP_sign:  | 
554  | 0  |                 x->ex_xkusage |= XKU_OCSP_SIGN;  | 
555  | 0  |                 break;  | 
556  | 0  |             case NID_time_stamp:  | 
557  | 0  |                 x->ex_xkusage |= XKU_TIMESTAMP;  | 
558  | 0  |                 break;  | 
559  | 0  |             case NID_dvcs:  | 
560  | 0  |                 x->ex_xkusage |= XKU_DVCS;  | 
561  | 0  |                 break;  | 
562  | 0  |             case NID_anyExtendedKeyUsage:  | 
563  | 0  |                 x->ex_xkusage |= XKU_ANYEKU;  | 
564  | 0  |                 break;  | 
565  | 0  |             default:  | 
566  |  |                 /* Ignore unknown extended key usage */  | 
567  | 0  |                 break;  | 
568  | 0  |             }  | 
569  | 0  |         }  | 
570  | 0  |         sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);  | 
571  | 0  |     } else if (i != -1) { | 
572  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
573  | 0  |     }  | 
574  |  |  | 
575  |  |     /* Handle legacy Netscape extension */  | 
576  | 0  |     if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL)) != NULL) { | 
577  | 0  |         if (ns->length > 0)  | 
578  | 0  |             x->ex_nscert = ns->data[0];  | 
579  | 0  |         else  | 
580  | 0  |             x->ex_nscert = 0;  | 
581  | 0  |         x->ex_flags |= EXFLAG_NSCERT;  | 
582  | 0  |         ASN1_BIT_STRING_free(ns);  | 
583  | 0  |     } else if (i != -1) { | 
584  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
585  | 0  |     }  | 
586  |  |  | 
587  |  |     /* Handle subject key identifier and issuer/authority key identifier */  | 
588  | 0  |     x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);  | 
589  | 0  |     if (x->skid == NULL && i != -1)  | 
590  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
591  |  | 
  | 
592  | 0  |     x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);  | 
593  | 0  |     if (x->akid == NULL && i != -1)  | 
594  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
595  |  |  | 
596  |  |     /* Check if subject name matches issuer */  | 
597  | 0  |     if (X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)) == 0) { | 
598  | 0  |         x->ex_flags |= EXFLAG_SI; /* Cert is self-issued */  | 
599  | 0  |         if (X509_check_akid(x, x->akid) == X509_V_OK /* SKID matches AKID */  | 
600  | 0  |                 /* .. and the signature alg matches the PUBKEY alg: */  | 
601  | 0  |                 && check_sig_alg_match(X509_get0_pubkey(x), x) == X509_V_OK)  | 
602  | 0  |             x->ex_flags |= EXFLAG_SS; /* indicate self-signed */  | 
603  |  |         /* This is very related to ossl_x509_likely_issued(x, x) == X509_V_OK */  | 
604  | 0  |     }  | 
605  |  |  | 
606  |  |     /* Handle subject alternative names and various other extensions */  | 
607  | 0  |     x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);  | 
608  | 0  |     if (x->altname == NULL && i != -1)  | 
609  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
610  | 0  |     x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);  | 
611  | 0  |     if (x->nc == NULL && i != -1)  | 
612  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
613  |  |  | 
614  |  |     /* Handle CRL distribution point entries */  | 
615  | 0  |     res = setup_crldp(x);  | 
616  | 0  |     if (res == 0)  | 
617  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
618  |  | 
  | 
619  | 0  | #ifndef OPENSSL_NO_RFC3779  | 
620  | 0  |     x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);  | 
621  | 0  |     if (x->rfc3779_addr == NULL && i != -1)  | 
622  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
623  | 0  |     x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);  | 
624  | 0  |     if (x->rfc3779_asid == NULL && i != -1)  | 
625  | 0  |         x->ex_flags |= EXFLAG_INVALID;  | 
626  | 0  | #endif  | 
627  | 0  |     for (i = 0; i < X509_get_ext_count(x); i++) { | 
628  | 0  |         X509_EXTENSION *ex = X509_get_ext(x, i);  | 
629  | 0  |         int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));  | 
630  |  | 
  | 
631  | 0  |         if (nid == NID_freshest_crl)  | 
632  | 0  |             x->ex_flags |= EXFLAG_FRESHEST;  | 
633  | 0  |         if (!X509_EXTENSION_get_critical(ex))  | 
634  | 0  |             continue;  | 
635  | 0  |         if (!X509_supported_extension(ex)) { | 
636  | 0  |             x->ex_flags |= EXFLAG_CRITICAL;  | 
637  | 0  |             break;  | 
638  | 0  |         }  | 
639  | 0  |         switch (nid) { | 
640  | 0  |         case NID_basic_constraints:  | 
641  | 0  |             x->ex_flags |= EXFLAG_BCONS_CRITICAL;  | 
642  | 0  |             break;  | 
643  | 0  |         case NID_authority_key_identifier:  | 
644  | 0  |             x->ex_flags |= EXFLAG_AKID_CRITICAL;  | 
645  | 0  |             break;  | 
646  | 0  |         case NID_subject_key_identifier:  | 
647  | 0  |             x->ex_flags |= EXFLAG_SKID_CRITICAL;  | 
648  | 0  |             break;  | 
649  | 0  |         case NID_subject_alt_name:  | 
650  | 0  |             x->ex_flags |= EXFLAG_SAN_CRITICAL;  | 
651  | 0  |             break;  | 
652  | 0  |         default:  | 
653  | 0  |             break;  | 
654  | 0  |         }  | 
655  | 0  |     }  | 
656  |  |  | 
657  |  |     /* Set x->siginf, ignoring errors due to unsupported algos */  | 
658  | 0  |     (void)ossl_x509_init_sig_info(x);  | 
659  |  | 
  | 
660  | 0  |     x->ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */  | 
661  | 0  | #ifdef tsan_st_rel  | 
662  | 0  |     tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);  | 
663  |  |     /*  | 
664  |  |      * Above store triggers fast lock-free check in the beginning of the  | 
665  |  |      * function. But one has to ensure that the structure is "stable", i.e.  | 
666  |  |      * all stores are visible on all processors. Hence the release fence.  | 
667  |  |      */  | 
668  | 0  | #endif  | 
669  | 0  |     ERR_pop_to_mark();  | 
670  |  | 
  | 
671  | 0  |     if ((x->ex_flags & EXFLAG_INVALID) == 0) { | 
672  | 0  |         CRYPTO_THREAD_unlock(x->lock);  | 
673  | 0  |         return 1;  | 
674  | 0  |     }  | 
675  | 0  |     CRYPTO_THREAD_unlock(x->lock);  | 
676  | 0  |     ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE);  | 
677  | 0  |     return 0;  | 
678  | 0  | }  | 
679  |  |  | 
680  |  | /*-  | 
681  |  |  * CA checks common to all purposes  | 
682  |  |  * return codes:  | 
683  |  |  * 0 not a CA  | 
684  |  |  * 1 is a CA  | 
685  |  |  * 2 Only possible in older versions of openSSL when basicConstraints are absent  | 
686  |  |  *   new versions will not return this value. May be a CA  | 
687  |  |  * 3 basicConstraints absent but self-signed V1.  | 
688  |  |  * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.  | 
689  |  |  * 5 Netscape specific CA Flags present  | 
690  |  |  */  | 
691  |  |  | 
692  |  | static int check_ca(const X509 *x)  | 
693  | 0  | { | 
694  |  |     /* keyUsage if present should allow cert signing */  | 
695  | 0  |     if (ku_reject(x, KU_KEY_CERT_SIGN))  | 
696  | 0  |         return 0;  | 
697  | 0  |     if ((x->ex_flags & EXFLAG_BCONS) != 0) { | 
698  |  |         /* If basicConstraints says not a CA then say so */  | 
699  | 0  |         return (x->ex_flags & EXFLAG_CA) != 0;  | 
700  | 0  |     } else { | 
701  |  |         /* We support V1 roots for...  uh, I don't really know why. */  | 
702  | 0  |         if ((x->ex_flags & V1_ROOT) == V1_ROOT)  | 
703  | 0  |             return 3;  | 
704  |  |         /*  | 
705  |  |          * If key usage present it must have certSign so tolerate it  | 
706  |  |          */  | 
707  | 0  |         else if ((x->ex_flags & EXFLAG_KUSAGE) != 0)  | 
708  | 0  |             return 4;  | 
709  |  |         /* Older certificates could have Netscape-specific CA types */  | 
710  | 0  |         else if ((x->ex_flags & EXFLAG_NSCERT) != 0  | 
711  | 0  |                  && (x->ex_nscert & NS_ANY_CA) != 0)  | 
712  | 0  |             return 5;  | 
713  |  |         /* Can this still be regarded a CA certificate?  I doubt it. */  | 
714  | 0  |         return 0;  | 
715  | 0  |     }  | 
716  | 0  | }  | 
717  |  |  | 
718  |  | void X509_set_proxy_flag(X509 *x)  | 
719  | 0  | { | 
720  | 0  |     if (CRYPTO_THREAD_write_lock(x->lock)) { | 
721  | 0  |         x->ex_flags |= EXFLAG_PROXY;  | 
722  | 0  |         CRYPTO_THREAD_unlock(x->lock);  | 
723  | 0  |     }  | 
724  | 0  | }  | 
725  |  |  | 
726  |  | void X509_set_proxy_pathlen(X509 *x, long l)  | 
727  | 0  | { | 
728  | 0  |     x->ex_pcpathlen = l;  | 
729  | 0  | }  | 
730  |  |  | 
731  |  | int X509_check_ca(X509 *x)  | 
732  | 0  | { | 
733  |  |     /* Note 0 normally means "not a CA" - but in this case means error. */  | 
734  | 0  |     if (!ossl_x509v3_cache_extensions(x))  | 
735  | 0  |         return 0;  | 
736  |  |  | 
737  | 0  |     return check_ca(x);  | 
738  | 0  | }  | 
739  |  |  | 
740  |  | /* Check SSL CA: common checks for SSL client and server. */  | 
741  |  | static int check_ssl_ca(const X509 *x)  | 
742  | 0  | { | 
743  | 0  |     int ca_ret = check_ca(x);  | 
744  |  | 
  | 
745  | 0  |     if (ca_ret == 0)  | 
746  | 0  |         return 0;  | 
747  |  |     /* Check nsCertType if present */  | 
748  | 0  |     return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0;  | 
749  | 0  | }  | 
750  |  |  | 
751  |  | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,  | 
752  |  |                                     int non_leaf)  | 
753  | 0  | { | 
754  | 0  |     if (xku_reject(x, XKU_SSL_CLIENT))  | 
755  | 0  |         return 0;  | 
756  | 0  |     if (non_leaf)  | 
757  | 0  |         return check_ssl_ca(x);  | 
758  |  |     /* We need to do digital signatures or key agreement */  | 
759  | 0  |     if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))  | 
760  | 0  |         return 0;  | 
761  |  |     /* nsCertType if present should allow SSL client use */  | 
762  | 0  |     if (ns_reject(x, NS_SSL_CLIENT))  | 
763  | 0  |         return 0;  | 
764  | 0  |     return 1;  | 
765  | 0  | }  | 
766  |  |  | 
767  |  | /*  | 
768  |  |  * Key usage needed for TLS/SSL server: digital signature, encipherment or  | 
769  |  |  * key agreement. The ssl code can check this more thoroughly for individual  | 
770  |  |  * key types.  | 
771  |  |  */  | 
772  |  | #define KU_TLS \  | 
773  |  |     KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT  | 
774  |  |  | 
775  |  | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,  | 
776  |  |                                     int non_leaf)  | 
777  | 0  | { | 
778  | 0  |     if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))  | 
779  | 0  |         return 0;  | 
780  | 0  |     if (non_leaf)  | 
781  | 0  |         return check_ssl_ca(x);  | 
782  |  |  | 
783  | 0  |     if (ns_reject(x, NS_SSL_SERVER))  | 
784  | 0  |         return 0;  | 
785  | 0  |     if (ku_reject(x, KU_TLS))  | 
786  | 0  |         return 0;  | 
787  |  |  | 
788  | 0  |     return 1;  | 
789  |  | 
  | 
790  | 0  | }  | 
791  |  |  | 
792  |  | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,  | 
793  |  |                                        int non_leaf)  | 
794  | 0  | { | 
795  | 0  |     int ret = check_purpose_ssl_server(xp, x, non_leaf);  | 
796  |  | 
  | 
797  | 0  |     if (!ret || non_leaf)  | 
798  | 0  |         return ret;  | 
799  |  |     /* We need to encipher or Netscape complains */  | 
800  | 0  |     return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;  | 
801  | 0  | }  | 
802  |  |  | 
803  |  | /* common S/MIME client checks */  | 
804  |  | static int purpose_smime(const X509 *x, int non_leaf)  | 
805  | 0  | { | 
806  | 0  |     if (xku_reject(x, XKU_SMIME))  | 
807  | 0  |         return 0;  | 
808  | 0  |     if (non_leaf) { | 
809  | 0  |         int ca_ret = check_ca(x);  | 
810  |  | 
  | 
811  | 0  |         if (ca_ret == 0)  | 
812  | 0  |             return 0;  | 
813  |  |         /* Check nsCertType if present */  | 
814  | 0  |         if (ca_ret != 5 || (x->ex_nscert & NS_SMIME_CA) != 0)  | 
815  | 0  |             return ca_ret;  | 
816  | 0  |         else  | 
817  | 0  |             return 0;  | 
818  | 0  |     }  | 
819  | 0  |     if ((x->ex_flags & EXFLAG_NSCERT) != 0) { | 
820  | 0  |         if ((x->ex_nscert & NS_SMIME) != 0)  | 
821  | 0  |             return 1;  | 
822  |  |         /* Workaround for some buggy certificates */  | 
823  | 0  |         return (x->ex_nscert & NS_SSL_CLIENT) != 0 ? 2 : 0;  | 
824  | 0  |     }  | 
825  | 0  |     return 1;  | 
826  | 0  | }  | 
827  |  |  | 
828  |  | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,  | 
829  |  |                                     int non_leaf)  | 
830  | 0  | { | 
831  | 0  |     int ret = purpose_smime(x, non_leaf);  | 
832  |  | 
  | 
833  | 0  |     if (!ret || non_leaf)  | 
834  | 0  |         return ret;  | 
835  | 0  |     return ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION) ? 0 : ret;  | 
836  | 0  | }  | 
837  |  |  | 
838  |  | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,  | 
839  |  |                                        int non_leaf)  | 
840  | 0  | { | 
841  | 0  |     int ret = purpose_smime(x, non_leaf);  | 
842  |  | 
  | 
843  | 0  |     if (!ret || non_leaf)  | 
844  | 0  |         return ret;  | 
845  | 0  |     return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;  | 
846  | 0  | }  | 
847  |  |  | 
848  |  | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,  | 
849  |  |                                   int non_leaf)  | 
850  | 0  | { | 
851  | 0  |     if (non_leaf) { | 
852  | 0  |         int ca_ret = check_ca(x);  | 
853  |  | 
  | 
854  | 0  |         return ca_ret == 2 ? 0 : ca_ret;  | 
855  | 0  |     }  | 
856  | 0  |     return !ku_reject(x, KU_CRL_SIGN);  | 
857  | 0  | }  | 
858  |  |  | 
859  |  | /*  | 
860  |  |  * OCSP helper: this is *not* a full OCSP check. It just checks that each CA  | 
861  |  |  * is valid. Additional checks must be made on the chain.  | 
862  |  |  */  | 
863  |  | static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x,  | 
864  |  |                                      int non_leaf)  | 
865  | 0  | { | 
866  |  |     /*  | 
867  |  |      * Must be a valid CA.  Should we really support the "I don't know" value  | 
868  |  |      * (2)?  | 
869  |  |      */  | 
870  | 0  |     if (non_leaf)  | 
871  | 0  |         return check_ca(x);  | 
872  |  |     /* Leaf certificate is checked in OCSP_basic_verify() */  | 
873  | 0  |     return 1;  | 
874  | 0  | }  | 
875  |  |  | 
876  |  | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,  | 
877  |  |                                         int non_leaf)  | 
878  | 0  | { | 
879  | 0  |     int i_ext;  | 
880  |  |  | 
881  |  |     /*  | 
882  |  |      * If non_leaf is true we must check if this is a valid CA certificate.  | 
883  |  |      * The extra requirements by the CA/Browser Forum are not checked.  | 
884  |  |      */  | 
885  | 0  |     if (non_leaf)  | 
886  | 0  |         return check_ca(x);  | 
887  |  |  | 
888  |  |     /*  | 
889  |  |      * Key Usage is checked according to RFC 5280 and  | 
890  |  |      * Extended Key Usage attributes is checked according to RFC 3161.  | 
891  |  |      * The extra (and somewhat conflicting) CA/Browser Forum  | 
892  |  |      * Baseline Requirements for the Issuance and Management of  | 
893  |  |      * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,  | 
894  |  |      * Section 7.1.2.3: Code signing and Timestamp Certificate are not checked.  | 
895  |  |      */  | 
896  |  |     /*  | 
897  |  |      * Check the optional key usage field:  | 
898  |  |      * if Key Usage is present, it must be one of digitalSignature  | 
899  |  |      * and/or nonRepudiation (other values are not consistent and shall  | 
900  |  |      * be rejected).  | 
901  |  |      */  | 
902  | 0  |     if ((x->ex_flags & EXFLAG_KUSAGE) != 0  | 
903  | 0  |         && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||  | 
904  | 0  |             !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))  | 
905  | 0  |         return 0;  | 
906  |  |  | 
907  |  |     /* Only timestamp key usage is permitted and it's required. */  | 
908  | 0  |     if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP)  | 
909  | 0  |         return 0;  | 
910  |  |  | 
911  |  |     /* Extended Key Usage MUST be critical */  | 
912  | 0  |     i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);  | 
913  | 0  |     if (i_ext >= 0  | 
914  | 0  |             && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext)))  | 
915  | 0  |         return 0;  | 
916  | 0  |     return 1;  | 
917  | 0  | }  | 
918  |  |  | 
919  |  | static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x,  | 
920  |  |                                    int non_leaf)  | 
921  | 0  | { | 
922  | 0  |     int i_ext;  | 
923  |  |  | 
924  |  |     /*  | 
925  |  |      * If non_leaf is true we must check if this is a valid CA certificate.  | 
926  |  |      * The extra requirements by the CA/Browser Forum are not checked.  | 
927  |  |      */  | 
928  | 0  |     if (non_leaf)  | 
929  | 0  |         return check_ca(x);  | 
930  |  |  | 
931  |  |     /*  | 
932  |  |      * Check the key usage and extended key usage fields:  | 
933  |  |      *  | 
934  |  |      * Reference: CA/Browser Forum,  | 
935  |  |      * Baseline Requirements for the Issuance and Management of  | 
936  |  |      * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,  | 
937  |  |      * Section 7.1.2.3: Code signing and Timestamp Certificate  | 
938  |  |      *  | 
939  |  |      * Checking covers Key Usage and Extended Key Usage attributes.  | 
940  |  |      * The certificatePolicies, cRLDistributionPoints (CDP), and  | 
941  |  |      * authorityInformationAccess (AIA) extensions are so far not checked.  | 
942  |  |      */  | 
943  |  |     /* Key Usage */  | 
944  | 0  |     if ((x->ex_flags & EXFLAG_KUSAGE) == 0)  | 
945  | 0  |         return 0;  | 
946  | 0  |     if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0)  | 
947  | 0  |         return 0;  | 
948  | 0  |     if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0)  | 
949  | 0  |         return 0;  | 
950  |  |  | 
951  |  |     /* Key Usage MUST be critical */  | 
952  | 0  |     i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1);  | 
953  | 0  |     if (i_ext < 0)  | 
954  | 0  |         return 0;  | 
955  | 0  |     if (i_ext >= 0) { | 
956  | 0  |         X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);  | 
957  | 0  |         if (!X509_EXTENSION_get_critical(ext))  | 
958  | 0  |             return 0;  | 
959  | 0  |     }  | 
960  |  |  | 
961  |  |     /* Extended Key Usage */  | 
962  | 0  |     if ((x->ex_flags & EXFLAG_XKUSAGE) == 0)  | 
963  | 0  |         return 0;  | 
964  | 0  |     if ((x->ex_xkusage & XKU_CODE_SIGN) == 0)  | 
965  | 0  |         return 0;  | 
966  | 0  |     if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0)  | 
967  | 0  |         return 0;  | 
968  |  |  | 
969  | 0  |     return 1;  | 
970  |  | 
  | 
971  | 0  | }  | 
972  |  |  | 
973  |  | static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x,  | 
974  |  |                             int non_leaf)  | 
975  | 0  | { | 
976  | 0  |     return 1;  | 
977  | 0  | }  | 
978  |  |  | 
979  |  | /*-  | 
980  |  |  * Various checks to see if one certificate potentially issued the second.  | 
981  |  |  * This can be used to prune a set of possible issuer certificates which  | 
982  |  |  * have been looked up using some simple method such as by subject name.  | 
983  |  |  * These are:  | 
984  |  |  * 1. issuer_name(subject) == subject_name(issuer)  | 
985  |  |  * 2. If akid(subject) exists, it matches the respective issuer fields.  | 
986  |  |  * 3. subject signature algorithm == issuer public key algorithm  | 
987  |  |  * 4. If key_usage(issuer) exists, it allows for signing subject.  | 
988  |  |  * Note that this does not include actually checking the signature.  | 
989  |  |  * Returns 0 for OK, or positive for reason for mismatch  | 
990  |  |  * where reason codes match those for X509_verify_cert().  | 
991  |  |  */  | 
992  |  | int X509_check_issued(X509 *issuer, X509 *subject)  | 
993  | 0  | { | 
994  | 0  |     int ret;  | 
995  |  | 
  | 
996  | 0  |     if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK)  | 
997  | 0  |         return ret;  | 
998  | 0  |     return ossl_x509_signing_allowed(issuer, subject);  | 
999  | 0  | }  | 
1000  |  |  | 
1001  |  | /* do the checks 1., 2., and 3. as described above for X509_check_issued() */  | 
1002  |  | int ossl_x509_likely_issued(X509 *issuer, X509 *subject)  | 
1003  | 0  | { | 
1004  | 0  |     int ret;  | 
1005  |  | 
  | 
1006  | 0  |     if (X509_NAME_cmp(X509_get_subject_name(issuer),  | 
1007  | 0  |                       X509_get_issuer_name(subject)) != 0)  | 
1008  | 0  |         return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;  | 
1009  |  |  | 
1010  |  |     /* set issuer->skid and subject->akid */  | 
1011  | 0  |     if (!ossl_x509v3_cache_extensions(issuer)  | 
1012  | 0  |             || !ossl_x509v3_cache_extensions(subject))  | 
1013  | 0  |         return X509_V_ERR_UNSPECIFIED;  | 
1014  |  |  | 
1015  | 0  |     ret = X509_check_akid(issuer, subject->akid);  | 
1016  | 0  |     if (ret != X509_V_OK)  | 
1017  | 0  |         return ret;  | 
1018  |  |  | 
1019  |  |     /* Check if the subject signature alg matches the issuer's PUBKEY alg */  | 
1020  | 0  |     return check_sig_alg_match(X509_get0_pubkey(issuer), subject);  | 
1021  | 0  | }  | 
1022  |  |  | 
1023  |  | /*-  | 
1024  |  |  * Check if certificate I<issuer> is allowed to issue certificate I<subject>  | 
1025  |  |  * according to the B<keyUsage> field of I<issuer> if present  | 
1026  |  |  * depending on any proxyCertInfo extension of I<subject>.  | 
1027  |  |  * Returns 0 for OK, or positive for reason for rejection  | 
1028  |  |  * where reason codes match those for X509_verify_cert().  | 
1029  |  |  */  | 
1030  |  | int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject)  | 
1031  | 0  | { | 
1032  | 0  |     if ((subject->ex_flags & EXFLAG_PROXY) != 0) { | 
1033  | 0  |         if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))  | 
1034  | 0  |             return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;  | 
1035  | 0  |     } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) { | 
1036  | 0  |         return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;  | 
1037  | 0  |     }  | 
1038  | 0  |     return X509_V_OK;  | 
1039  | 0  | }  | 
1040  |  |  | 
1041  |  | int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid)  | 
1042  | 0  | { | 
1043  | 0  |     if (akid == NULL)  | 
1044  | 0  |         return X509_V_OK;  | 
1045  |  |  | 
1046  |  |     /* Check key ids (if present) */  | 
1047  | 0  |     if (akid->keyid && issuer->skid &&  | 
1048  | 0  |         ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))  | 
1049  | 0  |         return X509_V_ERR_AKID_SKID_MISMATCH;  | 
1050  |  |     /* Check serial number */  | 
1051  | 0  |     if (akid->serial &&  | 
1052  | 0  |         ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial))  | 
1053  | 0  |         return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;  | 
1054  |  |     /* Check issuer name */  | 
1055  | 0  |     if (akid->issuer) { | 
1056  |  |         /*  | 
1057  |  |          * Ugh, for some peculiar reason AKID includes SEQUENCE OF  | 
1058  |  |          * GeneralName. So look for a DirName. There may be more than one but  | 
1059  |  |          * we only take any notice of the first.  | 
1060  |  |          */  | 
1061  | 0  |         GENERAL_NAMES *gens = akid->issuer;  | 
1062  | 0  |         GENERAL_NAME *gen;  | 
1063  | 0  |         X509_NAME *nm = NULL;  | 
1064  | 0  |         int i;  | 
1065  |  | 
  | 
1066  | 0  |         for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | 
1067  | 0  |             gen = sk_GENERAL_NAME_value(gens, i);  | 
1068  | 0  |             if (gen->type == GEN_DIRNAME) { | 
1069  | 0  |                 nm = gen->d.dirn;  | 
1070  | 0  |                 break;  | 
1071  | 0  |             }  | 
1072  | 0  |         }  | 
1073  | 0  |         if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0)  | 
1074  | 0  |             return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;  | 
1075  | 0  |     }  | 
1076  | 0  |     return X509_V_OK;  | 
1077  | 0  | }  | 
1078  |  |  | 
1079  |  | uint32_t X509_get_extension_flags(X509 *x)  | 
1080  | 0  | { | 
1081  |  |     /* Call for side-effect of computing hash and caching extensions */  | 
1082  | 0  |     X509_check_purpose(x, -1, 0);  | 
1083  | 0  |     return x->ex_flags;  | 
1084  | 0  | }  | 
1085  |  |  | 
1086  |  | uint32_t X509_get_key_usage(X509 *x)  | 
1087  | 0  | { | 
1088  |  |     /* Call for side-effect of computing hash and caching extensions */  | 
1089  | 0  |     if (X509_check_purpose(x, -1, 0) != 1)  | 
1090  | 0  |         return 0;  | 
1091  | 0  |     return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX;  | 
1092  | 0  | }  | 
1093  |  |  | 
1094  |  | uint32_t X509_get_extended_key_usage(X509 *x)  | 
1095  | 0  | { | 
1096  |  |     /* Call for side-effect of computing hash and caching extensions */  | 
1097  | 0  |     if (X509_check_purpose(x, -1, 0) != 1)  | 
1098  | 0  |         return 0;  | 
1099  | 0  |     return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX;  | 
1100  | 0  | }  | 
1101  |  |  | 
1102  |  | const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)  | 
1103  | 0  | { | 
1104  |  |     /* Call for side-effect of computing hash and caching extensions */  | 
1105  | 0  |     if (X509_check_purpose(x, -1, 0) != 1)  | 
1106  | 0  |         return NULL;  | 
1107  | 0  |     return x->skid;  | 
1108  | 0  | }  | 
1109  |  |  | 
1110  |  | const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)  | 
1111  | 0  | { | 
1112  |  |     /* Call for side-effect of computing hash and caching extensions */  | 
1113  | 0  |     if (X509_check_purpose(x, -1, 0) != 1)  | 
1114  | 0  |         return NULL;  | 
1115  | 0  |     return (x->akid != NULL ? x->akid->keyid : NULL);  | 
1116  | 0  | }  | 
1117  |  |  | 
1118  |  | const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)  | 
1119  | 0  | { | 
1120  |  |     /* Call for side-effect of computing hash and caching extensions */  | 
1121  | 0  |     if (X509_check_purpose(x, -1, 0) != 1)  | 
1122  | 0  |         return NULL;  | 
1123  | 0  |     return (x->akid != NULL ? x->akid->issuer : NULL);  | 
1124  | 0  | }  | 
1125  |  |  | 
1126  |  | const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)  | 
1127  | 0  | { | 
1128  |  |     /* Call for side-effect of computing hash and caching extensions */  | 
1129  | 0  |     if (X509_check_purpose(x, -1, 0) != 1)  | 
1130  | 0  |         return NULL;  | 
1131  | 0  |     return (x->akid != NULL ? x->akid->serial : NULL);  | 
1132  | 0  | }  | 
1133  |  |  | 
1134  |  | long X509_get_pathlen(X509 *x)  | 
1135  | 0  | { | 
1136  |  |     /* Called for side effect of caching extensions */  | 
1137  | 0  |     if (X509_check_purpose(x, -1, 0) != 1  | 
1138  | 0  |             || (x->ex_flags & EXFLAG_BCONS) == 0)  | 
1139  | 0  |         return -1;  | 
1140  | 0  |     return x->ex_pathlen;  | 
1141  | 0  | }  | 
1142  |  |  | 
1143  |  | long X509_get_proxy_pathlen(X509 *x)  | 
1144  | 0  | { | 
1145  |  |     /* Called for side effect of caching extensions */  | 
1146  | 0  |     if (X509_check_purpose(x, -1, 0) != 1  | 
1147  | 0  |             || (x->ex_flags & EXFLAG_PROXY) == 0)  | 
1148  | 0  |         return -1;  | 
1149  | 0  |     return x->ex_pcpathlen;  | 
1150  | 0  | }  |