1 /*
2 * Copyright 1999-2023 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 <openssl/x509v3.h>
13 #include "crypto/x509.h"
14
15 static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b);
16 static void trtable_free(X509_TRUST *p);
17
18 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags);
19 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags);
20 static int trust_compat(X509_TRUST *trust, X509 *x, int flags);
21
22 static int obj_trust(int id, X509 *x, int flags);
23 static int (*default_trust) (int id, X509 *x, int flags) = obj_trust;
24
25 /*
26 * WARNING: the following table should be kept in order of trust and without
27 * any gaps so we can just subtract the minimum trust value to get an index
28 * into the table
29 */
30
31 static X509_TRUST trstandard[] = {
32 {X509_TRUST_COMPAT, 0, trust_compat, "compatible", 0, NULL},
33 {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, "SSL Client", NID_client_auth,
34 NULL},
35 {X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth,
36 NULL},
37 {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect,
38 NULL},
39 {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign,
40 NULL},
41 {X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign,
42 NULL},
43 {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP,
44 NULL},
45 {X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL}
46 };
47
48 #define X509_TRUST_COUNT OSSL_NELEM(trstandard)
49
50 static STACK_OF(X509_TRUST) *trtable = NULL;
51
tr_cmp(const X509_TRUST * const * a,const X509_TRUST * const * b)52 static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b)
53 {
54 return (*a)->trust - (*b)->trust;
55 }
56
X509_TRUST_set_default(int (* trust)(int,X509 *,int))57 int (*X509_TRUST_set_default(int (*trust) (int, X509 *, int))) (int, X509 *,
58 int) {
59 int (*oldtrust) (int, X509 *, int);
60 oldtrust = default_trust;
61 default_trust = trust;
62 return oldtrust;
63 }
64
65 /* Returns X509_TRUST_TRUSTED, X509_TRUST_REJECTED, or X509_TRUST_UNTRUSTED */
X509_check_trust(X509 * x,int id,int flags)66 int X509_check_trust(X509 *x, int id, int flags)
67 {
68 X509_TRUST *pt;
69 int idx;
70
71 /* We get this as a default value */
72 if (id == X509_TRUST_DEFAULT)
73 return obj_trust(NID_anyExtendedKeyUsage, x,
74 flags | X509_TRUST_DO_SS_COMPAT);
75 idx = X509_TRUST_get_by_id(id);
76 if (idx < 0)
77 return default_trust(id, x, flags);
78 pt = X509_TRUST_get0(idx);
79 return pt->check_trust(pt, x, flags);
80 }
81
X509_TRUST_get_count(void)82 int X509_TRUST_get_count(void)
83 {
84 if (!trtable)
85 return X509_TRUST_COUNT;
86 return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
87 }
88
X509_TRUST_get0(int idx)89 X509_TRUST *X509_TRUST_get0(int idx)
90 {
91 if (idx < 0)
92 return NULL;
93 if (idx < (int)X509_TRUST_COUNT)
94 return trstandard + idx;
95 return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
96 }
97
X509_TRUST_get_by_id(int id)98 int X509_TRUST_get_by_id(int id)
99 {
100 X509_TRUST tmp;
101 int idx;
102
103 if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
104 return id - X509_TRUST_MIN;
105 if (trtable == NULL)
106 return -1;
107 tmp.trust = id;
108 /* Ideally, this would be done under lock */
109 sk_X509_TRUST_sort(trtable);
110 idx = sk_X509_TRUST_find(trtable, &tmp);
111 if (idx < 0)
112 return -1;
113 return idx + X509_TRUST_COUNT;
114 }
115
X509_TRUST_set(int * t,int trust)116 int X509_TRUST_set(int *t, int trust)
117 {
118 if (X509_TRUST_get_by_id(trust) < 0) {
119 ERR_raise(ERR_LIB_X509, X509_R_INVALID_TRUST);
120 return 0;
121 }
122 *t = trust;
123 return 1;
124 }
125
X509_TRUST_add(int id,int flags,int (* ck)(X509_TRUST *,X509 *,int),const char * name,int arg1,void * arg2)126 int X509_TRUST_add(int id, int flags, int (*ck) (X509_TRUST *, X509 *, int),
127 const char *name, int arg1, void *arg2)
128 {
129 int idx;
130 X509_TRUST *trtmp;
131 /*
132 * This is set according to what we change: application can't set it
133 */
134 flags &= ~X509_TRUST_DYNAMIC;
135 /* This will always be set for application modified trust entries */
136 flags |= X509_TRUST_DYNAMIC_NAME;
137 /* Get existing entry if any */
138 idx = X509_TRUST_get_by_id(id);
139 /* Need a new entry */
140 if (idx < 0) {
141 if ((trtmp = OPENSSL_malloc(sizeof(*trtmp))) == NULL)
142 return 0;
143 trtmp->flags = X509_TRUST_DYNAMIC;
144 } else
145 trtmp = X509_TRUST_get0(idx);
146
147 /* OPENSSL_free existing name if dynamic */
148 if (trtmp->flags & X509_TRUST_DYNAMIC_NAME)
149 OPENSSL_free(trtmp->name);
150 /* dup supplied name */
151 if ((trtmp->name = OPENSSL_strdup(name)) == NULL)
152 goto err;
153 /* Keep the dynamic flag of existing entry */
154 trtmp->flags &= X509_TRUST_DYNAMIC;
155 /* Set all other flags */
156 trtmp->flags |= flags;
157
158 trtmp->trust = id;
159 trtmp->check_trust = ck;
160 trtmp->arg1 = arg1;
161 trtmp->arg2 = arg2;
162
163 /* If its a new entry manage the dynamic table */
164 if (idx < 0) {
165 if (trtable == NULL
166 && (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL) {
167 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
168 goto err;
169 }
170 if (!sk_X509_TRUST_push(trtable, trtmp)) {
171 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
172 goto err;
173 }
174 }
175 return 1;
176 err:
177 if (idx < 0) {
178 OPENSSL_free(trtmp->name);
179 OPENSSL_free(trtmp);
180 }
181 return 0;
182 }
183
trtable_free(X509_TRUST * p)184 static void trtable_free(X509_TRUST *p)
185 {
186 if (p == NULL)
187 return;
188 if (p->flags & X509_TRUST_DYNAMIC) {
189 if (p->flags & X509_TRUST_DYNAMIC_NAME)
190 OPENSSL_free(p->name);
191 OPENSSL_free(p);
192 }
193 }
194
X509_TRUST_cleanup(void)195 void X509_TRUST_cleanup(void)
196 {
197 sk_X509_TRUST_pop_free(trtable, trtable_free);
198 trtable = NULL;
199 }
200
X509_TRUST_get_flags(const X509_TRUST * xp)201 int X509_TRUST_get_flags(const X509_TRUST *xp)
202 {
203 return xp->flags;
204 }
205
X509_TRUST_get0_name(const X509_TRUST * xp)206 char *X509_TRUST_get0_name(const X509_TRUST *xp)
207 {
208 return xp->name;
209 }
210
X509_TRUST_get_trust(const X509_TRUST * xp)211 int X509_TRUST_get_trust(const X509_TRUST *xp)
212 {
213 return xp->trust;
214 }
215
trust_1oidany(X509_TRUST * trust,X509 * x,int flags)216 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
217 {
218 /*
219 * Declare the chain verified if the desired trust OID is not rejected in
220 * any auxiliary trust info for this certificate, and the OID is either
221 * expressly trusted, or else either "anyEKU" is trusted, or the
222 * certificate is self-signed and X509_TRUST_NO_SS_COMPAT is not set.
223 */
224 flags |= X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU;
225 return obj_trust(trust->arg1, x, flags);
226 }
227
trust_1oid(X509_TRUST * trust,X509 * x,int flags)228 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
229 {
230 /*
231 * Declare the chain verified only if the desired trust OID is not
232 * rejected and is expressly trusted. Neither "anyEKU" nor "compat"
233 * trust in self-signed certificates apply.
234 */
235 flags &= ~(X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU);
236 return obj_trust(trust->arg1, x, flags);
237 }
238
trust_compat(X509_TRUST * trust,X509 * x,int flags)239 static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
240 {
241 /* Call for side-effect of setting EXFLAG_SS for self-signed-certs */
242 if (X509_check_purpose(x, -1, 0) != 1)
243 return X509_TRUST_UNTRUSTED;
244 if ((flags & X509_TRUST_NO_SS_COMPAT) == 0 && (x->ex_flags & EXFLAG_SS))
245 return X509_TRUST_TRUSTED;
246 else
247 return X509_TRUST_UNTRUSTED;
248 }
249
obj_trust(int id,X509 * x,int flags)250 static int obj_trust(int id, X509 *x, int flags)
251 {
252 X509_CERT_AUX *ax = x->aux;
253 int i;
254
255 if (ax != NULL && ax->reject != NULL) {
256 for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
257 ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->reject, i);
258 int nid = OBJ_obj2nid(obj);
259
260 if (nid == id || (nid == NID_anyExtendedKeyUsage &&
261 (flags & X509_TRUST_OK_ANY_EKU)))
262 return X509_TRUST_REJECTED;
263 }
264 }
265
266 if (ax != NULL && ax->trust != NULL) {
267 for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
268 ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->trust, i);
269 int nid = OBJ_obj2nid(obj);
270
271 if (nid == id || (nid == NID_anyExtendedKeyUsage &&
272 (flags & X509_TRUST_OK_ANY_EKU)))
273 return X509_TRUST_TRUSTED;
274 }
275 /*
276 * Reject when explicit trust EKU are set and none match.
277 *
278 * Returning untrusted is enough for full chains that end in
279 * self-signed roots, because when explicit trust is specified it
280 * suppresses the default blanket trust of self-signed objects.
281 *
282 * But for partial chains, this is not enough, because absent a similar
283 * trust-self-signed policy, non matching EKUs are indistinguishable
284 * from lack of EKU constraints.
285 *
286 * Therefore, failure to match any trusted purpose must trigger an
287 * explicit reject.
288 */
289 return X509_TRUST_REJECTED;
290 }
291
292 if ((flags & X509_TRUST_DO_SS_COMPAT) == 0)
293 return X509_TRUST_UNTRUSTED;
294
295 /*
296 * Not rejected, and there is no list of accepted uses, try compat.
297 */
298 return trust_compat(NULL, x, flags);
299 }
300