2 * Copyright 1999-2020 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 "crypto/x509.h"
15 DEFINE_STACK_OF(X509_TRUST)
16 DEFINE_STACK_OF(ASN1_OBJECT)
18 static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b);
19 static void trtable_free(X509_TRUST *p);
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);
25 static int obj_trust(int id, X509 *x, int flags);
26 static int (*default_trust) (int id, X509 *x, int flags) = obj_trust;
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
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,
38 {X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth,
40 {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect,
42 {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign,
44 {X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign,
46 {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP,
48 {X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL}
51 #define X509_TRUST_COUNT OSSL_NELEM(trstandard)
53 static STACK_OF(X509_TRUST) *trtable = NULL;
55 static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b)
57 return (*a)->trust - (*b)->trust;
60 int (*X509_TRUST_set_default(int (*trust) (int, X509 *, int))) (int, X509 *,
62 int (*oldtrust) (int, X509 *, int);
63 oldtrust = default_trust;
64 default_trust = trust;
68 int X509_check_trust(X509 *x, int id, int flags)
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);
79 return default_trust(id, x, flags);
80 pt = X509_TRUST_get0(idx);
81 return pt->check_trust(pt, x, flags);
84 int X509_TRUST_get_count(void)
87 return X509_TRUST_COUNT;
88 return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
91 X509_TRUST *X509_TRUST_get0(int idx)
95 if (idx < (int)X509_TRUST_COUNT)
96 return trstandard + idx;
97 return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
100 int X509_TRUST_get_by_id(int id)
105 if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
106 return id - X509_TRUST_MIN;
110 idx = sk_X509_TRUST_find(trtable, &tmp);
113 return idx + X509_TRUST_COUNT;
116 int X509_TRUST_set(int *t, int trust)
118 if (X509_TRUST_get_by_id(trust) == -1) {
119 X509err(X509_F_X509_TRUST_SET, X509_R_INVALID_TRUST);
126 int X509_TRUST_add(int id, int flags, int (*ck) (X509_TRUST *, X509 *, int),
127 const char *name, int arg1, void *arg2)
132 * This is set according to what we change: application can't set it
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 */
141 if ((trtmp = OPENSSL_malloc(sizeof(*trtmp))) == NULL) {
142 X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
145 trtmp->flags = X509_TRUST_DYNAMIC;
147 trtmp = X509_TRUST_get0(idx);
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);
157 /* Keep the dynamic flag of existing entry */
158 trtmp->flags &= X509_TRUST_DYNAMIC;
159 /* Set all other flags */
160 trtmp->flags |= flags;
163 trtmp->check_trust = ck;
167 /* If its a new entry manage the dynamic table */
170 && (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL) {
171 X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
174 if (!sk_X509_TRUST_push(trtable, trtmp)) {
175 X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
182 OPENSSL_free(trtmp->name);
188 static void trtable_free(X509_TRUST *p)
192 if (p->flags & X509_TRUST_DYNAMIC) {
193 if (p->flags & X509_TRUST_DYNAMIC_NAME)
194 OPENSSL_free(p->name);
199 void X509_TRUST_cleanup(void)
201 sk_X509_TRUST_pop_free(trtable, trtable_free);
205 int X509_TRUST_get_flags(const X509_TRUST *xp)
210 char *X509_TRUST_get0_name(const X509_TRUST *xp)
215 int X509_TRUST_get_trust(const X509_TRUST *xp)
220 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
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.
228 flags |= X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU;
229 return obj_trust(trust->arg1, x, flags);
232 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
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.
239 flags &= ~(X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU);
240 return obj_trust(trust->arg1, x, flags);
243 static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
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;
251 return X509_TRUST_UNTRUSTED;
254 static int obj_trust(int id, X509 *x, int flags)
256 X509_CERT_AUX *ax = x->aux;
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);
264 if (nid == id || (nid == NID_anyExtendedKeyUsage &&
265 (flags & X509_TRUST_OK_ANY_EKU)))
266 return X509_TRUST_REJECTED;
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);
275 if (nid == id || (nid == NID_anyExtendedKeyUsage &&
276 (flags & X509_TRUST_OK_ANY_EKU)))
277 return X509_TRUST_TRUSTED;
280 * Reject when explicit trust EKU are set and none match.
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.
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.
290 * Therefore, failure to match any trusted purpose must trigger an
293 return X509_TRUST_REJECTED;
296 if ((flags & X509_TRUST_DO_SS_COMPAT) == 0)
297 return X509_TRUST_UNTRUSTED;
300 * Not rejected, and there is no list of accepted uses, try compat.
302 return trust_compat(NULL, x, flags);