2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/x509v3.h>
13 #include "internal/x509_int.h"
15 static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b);
16 static void trtable_free(X509_TRUST *p);
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);
22 static int obj_trust(int id, X509 *x, int flags);
23 static int (*default_trust) (int id, X509 *x, int flags) = obj_trust;
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
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,
35 {X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth,
37 {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect,
39 {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign,
41 {X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign,
43 {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP,
45 {X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL}
48 #define X509_TRUST_COUNT OSSL_NELEM(trstandard)
50 static STACK_OF(X509_TRUST) *trtable = NULL;
52 static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b)
54 return (*a)->trust - (*b)->trust;
57 int (*X509_TRUST_set_default(int (*trust) (int, X509 *, int))) (int, X509 *,
59 int (*oldtrust) (int, X509 *, int);
60 oldtrust = default_trust;
61 default_trust = trust;
65 int X509_check_trust(X509 *x, int id, int flags)
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);
76 return default_trust(id, x, flags);
77 pt = X509_TRUST_get0(idx);
78 return pt->check_trust(pt, x, flags);
81 int X509_TRUST_get_count(void)
84 return X509_TRUST_COUNT;
85 return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
88 X509_TRUST *X509_TRUST_get0(int idx)
92 if (idx < (int)X509_TRUST_COUNT)
93 return trstandard + idx;
94 return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
97 int X509_TRUST_get_by_id(int id)
102 if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
103 return id - X509_TRUST_MIN;
107 idx = sk_X509_TRUST_find(trtable, &tmp);
110 return idx + X509_TRUST_COUNT;
113 int X509_TRUST_set(int *t, int trust)
115 if (X509_TRUST_get_by_id(trust) == -1) {
116 X509err(X509_F_X509_TRUST_SET, X509_R_INVALID_TRUST);
123 int X509_TRUST_add(int id, int flags, int (*ck) (X509_TRUST *, X509 *, int),
124 const char *name, int arg1, void *arg2)
129 * This is set according to what we change: application can't set it
131 flags &= ~X509_TRUST_DYNAMIC;
132 /* This will always be set for application modified trust entries */
133 flags |= X509_TRUST_DYNAMIC_NAME;
134 /* Get existing entry if any */
135 idx = X509_TRUST_get_by_id(id);
136 /* Need a new entry */
138 if ((trtmp = OPENSSL_malloc(sizeof(*trtmp))) == NULL) {
139 X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
142 trtmp->flags = X509_TRUST_DYNAMIC;
144 trtmp = X509_TRUST_get0(idx);
146 /* OPENSSL_free existing name if dynamic */
147 if (trtmp->flags & X509_TRUST_DYNAMIC_NAME)
148 OPENSSL_free(trtmp->name);
149 /* dup supplied name */
150 if ((trtmp->name = OPENSSL_strdup(name)) == NULL) {
151 X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
154 /* Keep the dynamic flag of existing entry */
155 trtmp->flags &= X509_TRUST_DYNAMIC;
156 /* Set all other flags */
157 trtmp->flags |= flags;
160 trtmp->check_trust = ck;
164 /* If its a new entry manage the dynamic table */
167 && (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL) {
168 X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
171 if (!sk_X509_TRUST_push(trtable, trtmp)) {
172 X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
179 OPENSSL_free(trtmp->name);
185 static void trtable_free(X509_TRUST *p)
189 if (p->flags & X509_TRUST_DYNAMIC) {
190 if (p->flags & X509_TRUST_DYNAMIC_NAME)
191 OPENSSL_free(p->name);
196 void X509_TRUST_cleanup(void)
198 sk_X509_TRUST_pop_free(trtable, trtable_free);
202 int X509_TRUST_get_flags(const X509_TRUST *xp)
207 char *X509_TRUST_get0_name(const X509_TRUST *xp)
212 int X509_TRUST_get_trust(const X509_TRUST *xp)
217 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
220 * Declare the chain verified if the desired trust OID is not rejected in
221 * any auxiliary trust info for this certificate, and the OID is either
222 * expressly trusted, or else either "anyEKU" is trusted, or the
223 * certificate is self-signed.
225 flags |= X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU;
226 return obj_trust(trust->arg1, x, flags);
229 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
232 * Declare the chain verified only if the desired trust OID is not
233 * rejected and is expressly trusted. Neither "anyEKU" nor "compat"
234 * trust in self-signed certificates apply.
236 flags &= ~(X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU);
237 return obj_trust(trust->arg1, x, flags);
240 static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
242 /* Call for side-effect of computing hash and caching extensions */
243 X509_check_purpose(x, -1, 0);
244 if ((flags & X509_TRUST_NO_SS_COMPAT) == 0 && x->ex_flags & EXFLAG_SS)
245 return X509_TRUST_TRUSTED;
247 return X509_TRUST_UNTRUSTED;
250 static int obj_trust(int id, X509 *x, int flags)
252 X509_CERT_AUX *ax = x->aux;
255 if (ax && ax->reject) {
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);
260 if (nid == id || (nid == NID_anyExtendedKeyUsage &&
261 (flags & X509_TRUST_OK_ANY_EKU)))
262 return X509_TRUST_REJECTED;
266 if (ax && ax->trust) {
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);
271 if (nid == id || (nid == NID_anyExtendedKeyUsage &&
272 (flags & X509_TRUST_OK_ANY_EKU)))
273 return X509_TRUST_TRUSTED;
276 * Reject when explicit trust EKU are set and none match.
278 * Returning untrusted is enough for 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.
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.
286 * Therefore, failure to match any trusted purpose must trigger an
289 return X509_TRUST_REJECTED;
292 if ((flags & X509_TRUST_DO_SS_COMPAT) == 0)
293 return X509_TRUST_UNTRUSTED;
296 * Not rejected, and there is no list of accepted uses, try compat.
298 return trust_compat(NULL, x, flags);