Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/x509/x509_trust.c
Line
Count
Source
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
310
#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
0
{
54
0
    return (*a)->trust - (*b)->trust;
55
0
}
56
57
int (*X509_TRUST_set_default(int (*trust)(int, X509 *, int)))(int, X509 *,
58
    int)
59
0
{
60
0
    int (*oldtrust)(int, X509 *, int);
61
0
    oldtrust = default_trust;
62
0
    default_trust = trust;
63
0
    return oldtrust;
64
0
}
65
66
int X509_check_trust(X509 *x, int id, int flags)
67
2.87k
{
68
2.87k
    X509_TRUST *pt;
69
2.87k
    int idx;
70
71
    /* We get this as a default value */
72
2.87k
    if (id == X509_TRUST_DEFAULT)
73
2.23k
        return obj_trust(NID_anyExtendedKeyUsage, x,
74
2.23k
            flags | X509_TRUST_DO_SS_COMPAT);
75
637
    idx = X509_TRUST_get_by_id(id);
76
637
    if (idx < 0)
77
327
        return default_trust(id, x, flags);
78
310
    pt = X509_TRUST_get0(idx);
79
310
    return pt->check_trust(pt, x, flags);
80
637
}
81
82
int X509_TRUST_get_count(void)
83
0
{
84
0
    if (!trtable)
85
0
        return X509_TRUST_COUNT;
86
0
    return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
87
0
}
88
89
X509_TRUST *X509_TRUST_get0(int idx)
90
310
{
91
310
    if (idx < 0)
92
0
        return NULL;
93
310
    if (idx < (int)X509_TRUST_COUNT)
94
310
        return trstandard + idx;
95
0
    return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
96
310
}
97
98
int X509_TRUST_get_by_id(int id)
99
1.29k
{
100
1.29k
    X509_TRUST tmp;
101
1.29k
    int idx;
102
103
1.29k
    if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
104
964
        return id - X509_TRUST_MIN;
105
327
    if (trtable == NULL)
106
327
        return -1;
107
0
    tmp.trust = id;
108
0
    idx = sk_X509_TRUST_find(trtable, &tmp);
109
0
    if (idx < 0)
110
0
        return -1;
111
0
    return idx + X509_TRUST_COUNT;
112
0
}
113
114
int X509_TRUST_set(int *t, int trust)
115
0
{
116
0
    if (X509_TRUST_get_by_id(trust) < 0) {
117
0
        ERR_raise(ERR_LIB_X509, X509_R_INVALID_TRUST);
118
0
        return 0;
119
0
    }
120
0
    *t = trust;
121
0
    return 1;
122
0
}
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
0
{
127
0
    int idx;
128
0
    X509_TRUST *trtmp;
129
    /*
130
     * This is set according to what we change: application can't set it
131
     */
132
0
    flags &= ~X509_TRUST_DYNAMIC;
133
    /* This will always be set for application modified trust entries */
134
0
    flags |= X509_TRUST_DYNAMIC_NAME;
135
    /* Get existing entry if any */
136
0
    idx = X509_TRUST_get_by_id(id);
137
    /* Need a new entry */
138
0
    if (idx < 0) {
139
0
        if ((trtmp = OPENSSL_malloc(sizeof(*trtmp))) == NULL) {
140
0
            ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
141
0
            return 0;
142
0
        }
143
0
        trtmp->flags = X509_TRUST_DYNAMIC;
144
0
    } else
145
0
        trtmp = X509_TRUST_get0(idx);
146
147
    /* OPENSSL_free existing name if dynamic */
148
0
    if (trtmp->flags & X509_TRUST_DYNAMIC_NAME)
149
0
        OPENSSL_free(trtmp->name);
150
    /* dup supplied name */
151
0
    if ((trtmp->name = OPENSSL_strdup(name)) == NULL) {
152
0
        ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
153
0
        goto err;
154
0
    }
155
    /* Keep the dynamic flag of existing entry */
156
0
    trtmp->flags &= X509_TRUST_DYNAMIC;
157
    /* Set all other flags */
158
0
    trtmp->flags |= flags;
159
160
0
    trtmp->trust = id;
161
0
    trtmp->check_trust = ck;
162
0
    trtmp->arg1 = arg1;
163
0
    trtmp->arg2 = arg2;
164
165
    /* If its a new entry manage the dynamic table */
166
0
    if (idx < 0) {
167
0
        if (trtable == NULL
168
0
            && (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL) {
169
0
            ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
170
0
            goto err;
171
0
            ;
172
0
        }
173
0
        if (!sk_X509_TRUST_push(trtable, trtmp)) {
174
0
            ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
175
0
            goto err;
176
0
        }
177
0
    }
178
0
    return 1;
179
0
err:
180
0
    if (idx < 0) {
181
0
        OPENSSL_free(trtmp->name);
182
0
        OPENSSL_free(trtmp);
183
0
    }
184
0
    return 0;
185
0
}
186
187
static void trtable_free(X509_TRUST *p)
188
0
{
189
0
    if (p == NULL)
190
0
        return;
191
0
    if (p->flags & X509_TRUST_DYNAMIC) {
192
0
        if (p->flags & X509_TRUST_DYNAMIC_NAME)
193
0
            OPENSSL_free(p->name);
194
0
        OPENSSL_free(p);
195
0
    }
196
0
}
197
198
void X509_TRUST_cleanup(void)
199
0
{
200
0
    sk_X509_TRUST_pop_free(trtable, trtable_free);
201
0
    trtable = NULL;
202
0
}
203
204
int X509_TRUST_get_flags(const X509_TRUST *xp)
205
0
{
206
0
    return xp->flags;
207
0
}
208
209
char *X509_TRUST_get0_name(const X509_TRUST *xp)
210
0
{
211
0
    return xp->name;
212
0
}
213
214
int X509_TRUST_get_trust(const X509_TRUST *xp)
215
0
{
216
0
    return xp->trust;
217
0
}
218
219
static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
220
0
{
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 and X509_TRUST_NO_SS_COMPAT is not set.
226
     */
227
0
    flags |= X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU;
228
0
    return obj_trust(trust->arg1, x, flags);
229
0
}
230
231
static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
232
0
{
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
0
    flags &= ~(X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU);
239
0
    return obj_trust(trust->arg1, x, flags);
240
0
}
241
242
static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
243
2.54k
{
244
    /* Call for side-effect of setting EXFLAG_SS for self-signed-certs */
245
2.54k
    if (X509_check_purpose(x, -1, 0) != 1)
246
5
        return X509_TRUST_UNTRUSTED;
247
2.54k
    if ((flags & X509_TRUST_NO_SS_COMPAT) == 0 && (x->ex_flags & EXFLAG_SS))
248
642
        return X509_TRUST_TRUSTED;
249
1.90k
    else
250
1.90k
        return X509_TRUST_UNTRUSTED;
251
2.54k
}
252
253
static int obj_trust(int id, X509 *x, int flags)
254
458
{
255
458
    X509_CERT_AUX *ax = x->aux;
256
458
    int i;
257
258
458
    if (ax && ax->reject) {
259
0
        for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
260
0
            ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->reject, i);
261
0
            int nid = OBJ_obj2nid(obj);
262
263
0
            if (nid == id || (nid == NID_anyExtendedKeyUsage && (flags & X509_TRUST_OK_ANY_EKU)))
264
0
                return X509_TRUST_REJECTED;
265
0
        }
266
0
    }
267
268
458
    if (ax && ax->trust) {
269
0
        for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
270
0
            ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->trust, i);
271
0
            int nid = OBJ_obj2nid(obj);
272
273
0
            if (nid == id || (nid == NID_anyExtendedKeyUsage && (flags & X509_TRUST_OK_ANY_EKU)))
274
0
                return X509_TRUST_TRUSTED;
275
0
        }
276
        /*
277
         * Reject when explicit trust EKU are set and none match.
278
         *
279
         * Returning untrusted is enough for for full chains that end in
280
         * self-signed roots, because when explicit trust is specified it
281
         * suppresses the default blanket trust of self-signed objects.
282
         *
283
         * But for partial chains, this is not enough, because absent a similar
284
         * trust-self-signed policy, non matching EKUs are indistinguishable
285
         * from lack of EKU constraints.
286
         *
287
         * Therefore, failure to match any trusted purpose must trigger an
288
         * explicit reject.
289
         */
290
0
        return X509_TRUST_REJECTED;
291
0
    }
292
293
458
    if ((flags & X509_TRUST_DO_SS_COMPAT) == 0)
294
50
        return X509_TRUST_UNTRUSTED;
295
296
    /*
297
     * Not rejected, and there is no list of accepted uses, try compat.
298
     */
299
408
    return trust_compat(NULL, x, flags);
300
458
}