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