]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x509/x509_trust.c
da29526d277182162b579a1415af69c445d12f7c
[thirdparty/openssl.git] / crypto / x509 / x509_trust.c
1 /*
2 * Copyright 1999-2022 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
52 static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b)
53 {
54 return (*a)->trust - (*b)->trust;
55 }
56
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 */
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
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
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
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 idx = sk_X509_TRUST_find(trtable, &tmp);
109 if (idx < 0)
110 return -1;
111 return idx + X509_TRUST_COUNT;
112 }
113
114 int X509_TRUST_set(int *t, int trust)
115 {
116 if (X509_TRUST_get_by_id(trust) < 0) {
117 ERR_raise(ERR_LIB_X509, X509_R_INVALID_TRUST);
118 return 0;
119 }
120 *t = trust;
121 return 1;
122 }
123
124 int X509_TRUST_add(int id, int flags, int (*ck) (X509_TRUST *, X509 *, int),
125 const char *name, int arg1, void *arg2)
126 {
127 int idx;
128 X509_TRUST *trtmp;
129 /*
130 * This is set according to what we change: application can't set it
131 */
132 flags &= ~X509_TRUST_DYNAMIC;
133 /* This will always be set for application modified trust entries */
134 flags |= X509_TRUST_DYNAMIC_NAME;
135 /* Get existing entry if any */
136 idx = X509_TRUST_get_by_id(id);
137 /* Need a new entry */
138 if (idx < 0) {
139 if ((trtmp = OPENSSL_malloc(sizeof(*trtmp))) == NULL) {
140 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
141 return 0;
142 }
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 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
153 goto err;
154 }
155 /* Keep the dynamic flag of existing entry */
156 trtmp->flags &= X509_TRUST_DYNAMIC;
157 /* Set all other flags */
158 trtmp->flags |= flags;
159
160 trtmp->trust = id;
161 trtmp->check_trust = ck;
162 trtmp->arg1 = arg1;
163 trtmp->arg2 = arg2;
164
165 /* If its a new entry manage the dynamic table */
166 if (idx < 0) {
167 if (trtable == NULL
168 && (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL) {
169 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
170 goto err;
171 }
172 if (!sk_X509_TRUST_push(trtable, trtmp)) {
173 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
174 goto err;
175 }
176 }
177 return 1;
178 err:
179 if (idx < 0) {
180 OPENSSL_free(trtmp->name);
181 OPENSSL_free(trtmp);
182 }
183 return 0;
184 }
185
186 static void trtable_free(X509_TRUST *p)
187 {
188 if (p == NULL)
189 return;
190 if (p->flags & X509_TRUST_DYNAMIC) {
191 if (p->flags & X509_TRUST_DYNAMIC_NAME)
192 OPENSSL_free(p->name);
193 OPENSSL_free(p);
194 }
195 }
196
197 void X509_TRUST_cleanup(void)
198 {
199 sk_X509_TRUST_pop_free(trtable, trtable_free);
200 trtable = NULL;
201 }
202
203 int X509_TRUST_get_flags(const X509_TRUST *xp)
204 {
205 return xp->flags;
206 }
207
208 char *X509_TRUST_get0_name(const X509_TRUST *xp)
209 {
210 return xp->name;
211 }
212
213 int X509_TRUST_get_trust(const X509_TRUST *xp)
214 {
215 return xp->trust;
216 }
217
218 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
219 {
220 /*
221 * Declare the chain verified if the desired trust OID is not rejected in
222 * any auxiliary trust info for this certificate, and the OID is either
223 * expressly trusted, or else either "anyEKU" is trusted, or the
224 * certificate is self-signed and X509_TRUST_NO_SS_COMPAT is not set.
225 */
226 flags |= X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU;
227 return obj_trust(trust->arg1, x, flags);
228 }
229
230 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
231 {
232 /*
233 * Declare the chain verified only if the desired trust OID is not
234 * rejected and is expressly trusted. Neither "anyEKU" nor "compat"
235 * trust in self-signed certificates apply.
236 */
237 flags &= ~(X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU);
238 return obj_trust(trust->arg1, x, flags);
239 }
240
241 static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
242 {
243 /* Call for side-effect of setting EXFLAG_SS for self-signed-certs */
244 if (X509_check_purpose(x, -1, 0) != 1)
245 return X509_TRUST_UNTRUSTED;
246 if ((flags & X509_TRUST_NO_SS_COMPAT) == 0 && (x->ex_flags & EXFLAG_SS))
247 return X509_TRUST_TRUSTED;
248 else
249 return X509_TRUST_UNTRUSTED;
250 }
251
252 static int obj_trust(int id, X509 *x, int flags)
253 {
254 X509_CERT_AUX *ax = x->aux;
255 int i;
256
257 if (ax != NULL && ax->reject != NULL) {
258 for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
259 ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->reject, i);
260 int nid = OBJ_obj2nid(obj);
261
262 if (nid == id || (nid == NID_anyExtendedKeyUsage &&
263 (flags & X509_TRUST_OK_ANY_EKU)))
264 return X509_TRUST_REJECTED;
265 }
266 }
267
268 if (ax != NULL && ax->trust != NULL) {
269 for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
270 ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->trust, i);
271 int nid = OBJ_obj2nid(obj);
272
273 if (nid == id || (nid == NID_anyExtendedKeyUsage &&
274 (flags & X509_TRUST_OK_ANY_EKU)))
275 return X509_TRUST_TRUSTED;
276 }
277 /*
278 * Reject when explicit trust EKU are set and none match.
279 *
280 * Returning untrusted is enough for for full chains that end in
281 * self-signed roots, because when explicit trust is specified it
282 * suppresses the default blanket trust of self-signed objects.
283 *
284 * But for partial chains, this is not enough, because absent a similar
285 * trust-self-signed policy, non matching EKUs are indistinguishable
286 * from lack of EKU constraints.
287 *
288 * Therefore, failure to match any trusted purpose must trigger an
289 * explicit reject.
290 */
291 return X509_TRUST_REJECTED;
292 }
293
294 if ((flags & X509_TRUST_DO_SS_COMPAT) == 0)
295 return X509_TRUST_UNTRUSTED;
296
297 /*
298 * Not rejected, and there is no list of accepted uses, try compat.
299 */
300 return trust_compat(NULL, x, flags);
301 }