f7d89bfd8362f22dfe2c23bc2e5199c1e09df014
[openssl.git] / crypto / x509 / x509_trs.c
1 /*
2  * Copyright 1999-2016 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 <openssl/x509v3.h>
13 #include "internal/x509_int.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 int X509_check_trust(X509 *x, int id, int flags)
66 {
67     X509_TRUST *pt;
68     int idx;
69
70     /* We get this as a default value */
71     if (id == X509_TRUST_DEFAULT)
72         return obj_trust(NID_anyExtendedKeyUsage, x,
73                          flags | X509_TRUST_DO_SS_COMPAT);
74     idx = X509_TRUST_get_by_id(id);
75     if (idx == -1)
76         return default_trust(id, x, flags);
77     pt = X509_TRUST_get0(idx);
78     return pt->check_trust(pt, x, flags);
79 }
80
81 int X509_TRUST_get_count(void)
82 {
83     if (!trtable)
84         return X509_TRUST_COUNT;
85     return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
86 }
87
88 X509_TRUST *X509_TRUST_get0(int idx)
89 {
90     if (idx < 0)
91         return NULL;
92     if (idx < (int)X509_TRUST_COUNT)
93         return trstandard + idx;
94     return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
95 }
96
97 int X509_TRUST_get_by_id(int id)
98 {
99     X509_TRUST tmp;
100     int idx;
101     if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
102         return id - X509_TRUST_MIN;
103     tmp.trust = id;
104     if (!trtable)
105         return -1;
106     idx = sk_X509_TRUST_find(trtable, &tmp);
107     if (idx == -1)
108         return -1;
109     return idx + X509_TRUST_COUNT;
110 }
111
112 int X509_TRUST_set(int *t, int trust)
113 {
114     if (X509_TRUST_get_by_id(trust) == -1) {
115         X509err(X509_F_X509_TRUST_SET, X509_R_INVALID_TRUST);
116         return 0;
117     }
118     *t = trust;
119     return 1;
120 }
121
122 int X509_TRUST_add(int id, int flags, int (*ck) (X509_TRUST *, X509 *, int),
123                    const char *name, int arg1, void *arg2)
124 {
125     int idx;
126     X509_TRUST *trtmp;
127     /*
128      * This is set according to what we change: application can't set it
129      */
130     flags &= ~X509_TRUST_DYNAMIC;
131     /* This will always be set for application modified trust entries */
132     flags |= X509_TRUST_DYNAMIC_NAME;
133     /* Get existing entry if any */
134     idx = X509_TRUST_get_by_id(id);
135     /* Need a new entry */
136     if (idx == -1) {
137         if ((trtmp = OPENSSL_malloc(sizeof(*trtmp))) == NULL) {
138             X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
139             return 0;
140         }
141         trtmp->flags = X509_TRUST_DYNAMIC;
142     } else
143         trtmp = X509_TRUST_get0(idx);
144
145     /* OPENSSL_free existing name if dynamic */
146     if (trtmp->flags & X509_TRUST_DYNAMIC_NAME)
147         OPENSSL_free(trtmp->name);
148     /* dup supplied name */
149     if ((trtmp->name = OPENSSL_strdup(name)) == NULL) {
150         X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
151         goto err;
152     }
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 == -1) {
165         if (trtable == NULL
166             && (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL) {
167             X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
168             goto err;;
169         }
170         if (!sk_X509_TRUST_push(trtable, trtmp)) {
171             X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
172             goto err;
173         }
174     }
175     return 1;
176  err:
177     if (idx == -1) {
178         OPENSSL_free(trtmp->name);
179         OPENSSL_free(trtmp);
180     }
181     return 0;
182 }
183
184 static void trtable_free(X509_TRUST *p)
185 {
186     if (!p)
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
195 void X509_TRUST_cleanup(void)
196 {
197     unsigned int i;
198     for (i = 0; i < X509_TRUST_COUNT; i++)
199         trtable_free(trstandard + i);
200     sk_X509_TRUST_pop_free(trtable, trtable_free);
201     trtable = NULL;
202 }
203
204 int X509_TRUST_get_flags(const X509_TRUST *xp)
205 {
206     return xp->flags;
207 }
208
209 char *X509_TRUST_get0_name(const X509_TRUST *xp)
210 {
211     return xp->name;
212 }
213
214 int X509_TRUST_get_trust(const X509_TRUST *xp)
215 {
216     return xp->trust;
217 }
218
219 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
220 {
221     /*
222      * Declare the chain verified if the desired trust OID is not rejected in
223      * any auxiliary trust info for this certificate, and the OID is either
224      * expressly trusted, or else either "anyEKU" is trusted, or the
225      * certificate is self-signed.
226      */
227     flags |= X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU;
228     return obj_trust(trust->arg1, x, flags);
229 }
230
231 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
232 {
233     /*
234      * Declare the chain verified only if the desired trust OID is not
235      * rejected and is expressly trusted.  Neither "anyEKU" nor "compat"
236      * trust in self-signed certificates apply.
237      */
238     flags &= ~(X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU);
239     return obj_trust(trust->arg1, x, flags);
240 }
241
242 static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
243 {
244     /* Call for side-effect of computing hash and caching extensions */
245     X509_check_purpose(x, -1, 0);
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 && ax->reject) {
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 && ax->trust) {
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 }