Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/x509/v3_purp.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2026 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 "internal/numbers.h"
13
#include <openssl/x509v3.h>
14
#include <openssl/x509_vfy.h>
15
#include "crypto/x509.h"
16
#include "internal/tsan_assist.h"
17
#include "x509_local.h"
18
19
static int check_ssl_ca(const X509 *x);
20
static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
21
    int non_leaf);
22
static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
23
    int non_leaf);
24
static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
25
    int non_leaf);
26
static int purpose_smime(const X509 *x, int non_leaf);
27
static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
28
    int non_leaf);
29
static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
30
    int non_leaf);
31
static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
32
    int non_leaf);
33
static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
34
    int non_leaf);
35
static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x,
36
    int non_leaf);
37
static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x,
38
    int non_leaf);
39
static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x,
40
    int non_leaf);
41
42
static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b);
43
static void xptable_free(X509_PURPOSE *p);
44
45
/* note that the id must be unique and for the standard entries == idx + 1 */
46
static X509_PURPOSE xstandard[] = {
47
    { X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
48
        check_purpose_ssl_client, "SSL client", "sslclient", NULL },
49
    { X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
50
        check_purpose_ssl_server, "SSL server", "sslserver", NULL },
51
    { X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
52
        check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL },
53
    { X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
54
        "S/MIME signing", "smimesign", NULL },
55
    { X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
56
        check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL },
57
    { X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
58
        "CRL signing", "crlsign", NULL },
59
    { X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check_purpose,
60
        "Any Purpose", "any",
61
        NULL },
62
    { X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, check_purpose_ocsp_helper,
63
        "OCSP helper", "ocsphelper", NULL },
64
    { X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
65
        check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign",
66
        NULL },
67
    { X509_PURPOSE_CODE_SIGN, X509_TRUST_OBJECT_SIGN, 0,
68
        check_purpose_code_sign, "Code signing", "codesign",
69
        NULL },
70
};
71
72
823
#define X509_PURPOSE_COUNT OSSL_NELEM(xstandard)
73
74
/* the id must be unique, but there may be gaps and maybe table is not sorted */
75
static STACK_OF(X509_PURPOSE) *xptable = NULL;
76
77
static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b)
78
0
{
79
0
    return (*a)->purpose - (*b)->purpose;
80
0
}
81
82
int X509_check_purpose(const X509 *x, int id, int non_leaf)
83
181k
{
84
181k
    int idx;
85
181k
    const X509_PURPOSE *pt;
86
87
    /*
88
     * TODO: This cast can be dropped when https://github.com/openssl/openssl/pull/30067
89
     * gets merged
90
     */
91
181k
    if (!ossl_x509v3_cache_extensions((X509 *)x))
92
45.3k
        return -1;
93
136k
    if (id == -1)
94
136k
        return 1;
95
96
499
    idx = X509_PURPOSE_get_by_id(id);
97
499
    if (idx == -1)
98
0
        return -1;
99
499
    pt = X509_PURPOSE_get0(idx);
100
499
    return pt->check_purpose(pt, x, non_leaf);
101
499
}
102
103
/* resets to default (any) purpose if purpose == X509_PURPOSE_DEFAULT_ANY (0) */
104
int X509_PURPOSE_set(int *p, int purpose)
105
0
{
106
0
    if (purpose != X509_PURPOSE_DEFAULT_ANY && X509_PURPOSE_get_by_id(purpose) == -1) {
107
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE);
108
0
        return 0;
109
0
    }
110
0
    *p = purpose;
111
0
    return 1;
112
0
}
113
114
int X509_PURPOSE_get_count(void)
115
0
{
116
0
    if (!xptable)
117
0
        return X509_PURPOSE_COUNT;
118
0
    return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
119
0
}
120
121
/* find smallest identifier not yet taken - note there might be gaps */
122
int X509_PURPOSE_get_unused_id(ossl_unused OSSL_LIB_CTX *libctx)
123
0
{
124
0
    int id = X509_PURPOSE_MAX + 1;
125
126
0
    while (X509_PURPOSE_get_by_id(id) != -1)
127
0
        id++;
128
0
    return id; /* is guaranteed to be unique and > X509_PURPOSE_MAX and != 0 */
129
0
}
130
131
X509_PURPOSE *X509_PURPOSE_get0(int idx)
132
71.5k
{
133
71.5k
    if (idx < 0)
134
70.6k
        return NULL;
135
823
    if (idx < (int)X509_PURPOSE_COUNT)
136
823
        return xstandard + idx;
137
0
    return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
138
823
}
139
140
int X509_PURPOSE_get_by_sname(const char *sname)
141
0
{
142
0
    int i;
143
0
    X509_PURPOSE *xptmp;
144
145
0
    for (i = 0; i < X509_PURPOSE_get_count(); i++) {
146
0
        xptmp = X509_PURPOSE_get0(i);
147
0
        if (strcmp(xptmp->sname, sname) == 0)
148
0
            return i;
149
0
    }
150
0
    return -1;
151
0
}
152
153
/* Returns -1 on error, else an index => 0 in standard/extended purpose table */
154
int X509_PURPOSE_get_by_id(int purpose)
155
71.5k
{
156
71.5k
    X509_PURPOSE tmp;
157
71.5k
    int idx;
158
159
71.5k
    if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX)
160
823
        return purpose - X509_PURPOSE_MIN;
161
70.6k
    if (xptable == NULL)
162
70.6k
        return -1;
163
0
    tmp.purpose = purpose;
164
0
    idx = sk_X509_PURPOSE_find(xptable, &tmp);
165
0
    if (idx < 0)
166
0
        return -1;
167
0
    return idx + X509_PURPOSE_COUNT;
168
0
}
169
170
/*
171
 * Add purpose entry identified by |sname|. |id| must be >= X509_PURPOSE_MIN.
172
 * May also be used to modify existing entry, including changing its id.
173
 */
174
int X509_PURPOSE_add(int id, int trust, int flags,
175
    int (*ck)(const X509_PURPOSE *, const X509 *, int),
176
    const char *name, const char *sname, void *arg)
177
0
{
178
0
    int old_id = 0;
179
0
    int idx;
180
0
    X509_PURPOSE *ptmp;
181
182
0
    if (id < X509_PURPOSE_MIN) {
183
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE);
184
0
        return 0;
185
0
    }
186
0
    if (trust < X509_TRUST_DEFAULT || name == NULL || sname == NULL || ck == NULL) {
187
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_INVALID_ARGUMENT);
188
0
        return 0;
189
0
    }
190
191
    /* This is set according to what we change: application can't set it */
192
0
    flags &= ~X509_PURPOSE_DYNAMIC;
193
    /* This will always be set for application modified trust entries */
194
0
    flags |= X509_PURPOSE_DYNAMIC_NAME;
195
196
    /* Get existing entry if any */
197
0
    idx = X509_PURPOSE_get_by_sname(sname);
198
0
    if (idx == -1) { /* Need a new entry */
199
0
        if (X509_PURPOSE_get_by_id(id) != -1) {
200
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE);
201
0
            return 0;
202
0
        }
203
0
        if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL)
204
0
            return 0;
205
0
        ptmp->flags = X509_PURPOSE_DYNAMIC;
206
0
    } else {
207
0
        ptmp = X509_PURPOSE_get0(idx);
208
0
        old_id = ptmp->purpose;
209
0
        if (id != old_id && X509_PURPOSE_get_by_id(id) != -1) {
210
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_PURPOSE_NOT_UNIQUE);
211
0
            return 0;
212
0
        }
213
0
    }
214
215
    /* OPENSSL_free existing name if dynamic */
216
0
    if ((ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) {
217
0
        OPENSSL_free(ptmp->name);
218
0
        OPENSSL_free(ptmp->sname);
219
0
    }
220
    /* Dup supplied name */
221
0
    ptmp->name = OPENSSL_strdup(name);
222
0
    ptmp->sname = OPENSSL_strdup(sname);
223
0
    if (ptmp->name == NULL || ptmp->sname == NULL)
224
0
        goto err;
225
    /* Keep the dynamic flag of existing entry */
226
0
    ptmp->flags &= X509_PURPOSE_DYNAMIC;
227
    /* Set all other flags */
228
0
    ptmp->flags |= flags;
229
230
0
    ptmp->purpose = id;
231
0
    ptmp->trust = trust;
232
0
    ptmp->check_purpose = ck;
233
0
    ptmp->usr_data = arg;
234
235
    /* If its a new entry manage the dynamic table */
236
0
    if (idx == -1) {
237
0
        if (xptable == NULL
238
0
            && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) {
239
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
240
0
            goto err;
241
0
        }
242
0
        if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
243
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
244
0
            goto err;
245
0
        }
246
0
    } else if (id != old_id) {
247
        /* on changing existing entry id, make sure to reset 'sorted' */
248
0
        (void)sk_X509_PURPOSE_set(xptable, idx, ptmp);
249
0
    }
250
0
    return 1;
251
0
err:
252
0
    if (idx == -1) {
253
0
        OPENSSL_free(ptmp->name);
254
0
        OPENSSL_free(ptmp->sname);
255
0
        OPENSSL_free(ptmp);
256
0
    }
257
0
    return 0;
258
0
}
259
260
static void xptable_free(X509_PURPOSE *p)
261
0
{
262
0
    if (p == NULL)
263
0
        return;
264
0
    if ((p->flags & X509_PURPOSE_DYNAMIC) != 0) {
265
0
        if ((p->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) {
266
0
            OPENSSL_free(p->name);
267
0
            OPENSSL_free(p->sname);
268
0
        }
269
0
        OPENSSL_free(p);
270
0
    }
271
0
}
272
273
void X509_PURPOSE_cleanup(void)
274
0
{
275
0
    sk_X509_PURPOSE_pop_free(xptable, xptable_free);
276
0
    xptable = NULL;
277
0
}
278
279
int X509_PURPOSE_get_id(const X509_PURPOSE *xp)
280
0
{
281
0
    return xp->purpose;
282
0
}
283
284
char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp)
285
0
{
286
0
    return xp->name;
287
0
}
288
289
char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp)
290
0
{
291
0
    return xp->sname;
292
0
}
293
294
int X509_PURPOSE_get_trust(const X509_PURPOSE *xp)
295
0
{
296
0
    return xp->trust;
297
0
}
298
299
static int nid_cmp(const int *a, const int *b)
300
132k
{
301
132k
    return *a - *b;
302
132k
}
303
304
DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid);
305
IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid);
306
307
int X509_supported_extension(const X509_EXTENSION *ex)
308
58.2k
{
309
    /*
310
     * This table is a list of the NIDs of supported extensions: that is
311
     * those which are used by the verify process. If an extension is
312
     * critical and doesn't appear in this list then the verify process will
313
     * normally reject the certificate. The list must be kept in numerical
314
     * order because it will be searched using bsearch.
315
     */
316
58.2k
    static const int supported_nids[] = {
317
58.2k
        NID_netscape_cert_type, /* 71 */
318
58.2k
        NID_key_usage, /* 83 */
319
58.2k
        NID_subject_alt_name, /* 85 */
320
58.2k
        NID_basic_constraints, /* 87 */
321
58.2k
        NID_certificate_policies, /* 89 */
322
58.2k
        NID_crl_distribution_points, /* 103 */
323
58.2k
        NID_ext_key_usage, /* 126 */
324
58.2k
#ifndef OPENSSL_NO_RFC3779
325
58.2k
        NID_sbgp_ipAddrBlock, /* 290 */
326
58.2k
        NID_sbgp_autonomousSysNum, /* 291 */
327
58.2k
#endif
328
58.2k
        NID_id_pkix_OCSP_noCheck, /* 369 */
329
58.2k
        NID_policy_constraints, /* 401 */
330
58.2k
        NID_proxyCertInfo, /* 663 */
331
58.2k
        NID_name_constraints, /* 666 */
332
58.2k
        NID_policy_mappings, /* 747 */
333
58.2k
        NID_inhibit_any_policy /* 748 */
334
58.2k
    };
335
336
58.2k
    int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
337
338
58.2k
    if (ex_nid == NID_undef)
339
5.20k
        return 0;
340
341
53.0k
    if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
342
51.6k
        return 1;
343
1.37k
    return 0;
344
53.0k
}
345
346
/*
347
 * Returns 1 on success, 0 if x is invalid.
348
 *
349
 * The full name of a nameRelativeToCRLIssuer distribution point is not
350
 * computed here.  Doing so for every parsed certificate cost an
351
 * X509_NAME_dup() of the issuer name per relative distribution point,
352
 * which a certificate with many such entries could turn into hundreds of
353
 * megabytes of heap on a plain TLS handshake, while the result is only
354
 * needed when a CRL is actually being matched against the certificate.
355
 * That name is now built on demand in the CRL checking code instead.
356
 */
357
static int setup_dp(DIST_POINT *dp)
358
2.50k
{
359
2.50k
    if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) {
360
50
        ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT);
361
50
        return 0;
362
50
    }
363
2.45k
    if (dp->reasons != NULL) {
364
36
        if (dp->reasons->length > 0)
365
34
            dp->dp_reasons = dp->reasons->data[0];
366
36
        if (dp->reasons->length > 1)
367
12
            dp->dp_reasons |= (dp->reasons->data[1] << 8);
368
36
        dp->dp_reasons &= CRLDP_ALL_REASONS;
369
2.41k
    } else {
370
2.41k
        dp->dp_reasons = CRLDP_ALL_REASONS;
371
2.41k
    }
372
2.45k
    return 1;
373
2.50k
}
374
375
/* Return 1 on success, 0 if x is invalid. */
376
static int setup_crldp(const X509 *x, STACK_OF(DIST_POINT) **tmp_crldp)
377
126k
{
378
126k
    int i;
379
380
126k
    *tmp_crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
381
126k
    if (*tmp_crldp == NULL && i != -1)
382
2.06k
        return 0;
383
384
127k
    for (i = 0; i < sk_DIST_POINT_num(*tmp_crldp); i++) {
385
2.50k
        if (!setup_dp(sk_DIST_POINT_value(*tmp_crldp, i)))
386
50
            return 0;
387
2.50k
    }
388
124k
    return 1;
389
124k
}
390
391
/* Check that issuer public key algorithm matches subject signature algorithm */
392
static int check_sig_alg_match(const EVP_PKEY *issuer_key, const X509 *subject)
393
88.5k
{
394
88.5k
    int subj_sig_nid;
395
396
88.5k
    if (issuer_key == NULL)
397
6.97k
        return X509_V_ERR_NO_ISSUER_PUBLIC_KEY;
398
81.6k
    if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm),
399
81.6k
            NULL, &subj_sig_nid)
400
81.6k
        == 0)
401
7.82k
        return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM;
402
73.8k
    if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid))
403
1.86k
        || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss))
404
73.2k
        return X509_V_OK;
405
578
    return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH;
406
73.8k
}
407
408
14.2k
#define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)
409
#define ku_reject(x, usage) \
410
17.6k
    (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0)
411
#define xku_reject(x, usage) \
412
0
    (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0)
413
#define ns_reject(x, usage) \
414
0
    (((x)->ex_flags & EXFLAG_NSCERT) != 0 && ((x)->ex_nscert & (usage)) == 0)
415
416
/*
417
 * Cache info on various X.509v3 extensions and further derived information,
418
 * e.g., if cert 'x' is self-issued, in x->ex_flags and other internal fields.
419
 * x->sha1_hash is filled in, or else EXFLAG_NO_FINGERPRINT is set in x->flags.
420
 * X509_SIG_INFO_VALID is set in x->flags if x->siginf was filled successfully.
421
 * Set EXFLAG_INVALID and return 0 in case the certificate is invalid.
422
 *
423
 * This is usually called by side-effect on objects, and forces us to keep
424
 * mutable X509 objects around. We should really make this go away.
425
 * In the interest of being able to do so, this function explicitly takes
426
 * a const argument and casts away const.
427
 */
428
int ossl_x509v3_cache_extensions(const X509 *const_x)
429
43.5k
{
430
43.5k
    BASIC_CONSTRAINTS *bs;
431
43.5k
    PROXY_CERT_INFO_EXTENSION *pci;
432
43.5k
    ASN1_BIT_STRING *usage;
433
43.5k
    ASN1_BIT_STRING *ns;
434
43.5k
    EXTENDED_KEY_USAGE *extusage;
435
43.5k
    int i;
436
43.5k
    int res;
437
43.5k
    uint32_t tmp_ex_flags;
438
43.5k
    unsigned char tmp_sha1_hash[SHA_DIGEST_LENGTH];
439
43.5k
    long tmp_ex_pathlen;
440
43.5k
    long tmp_ex_pcpathlen;
441
43.5k
    uint32_t tmp_ex_kusage;
442
43.5k
    uint32_t tmp_ex_xkusage;
443
43.5k
    uint32_t tmp_ex_nscert;
444
43.5k
    ASN1_OCTET_STRING *tmp_skid = NULL;
445
43.5k
    AUTHORITY_KEYID *tmp_akid = NULL;
446
43.5k
    STACK_OF(GENERAL_NAME) *tmp_altname = NULL;
447
43.5k
    NAME_CONSTRAINTS *tmp_nc = NULL;
448
43.5k
    STACK_OF(DIST_POINT) *tmp_crldp = NULL;
449
43.5k
    X509_SIG_INFO tmp_siginf;
450
43.5k
#ifndef OPENSSL_NO_RFC3779
451
43.5k
    STACK_OF(IPAddressFamily) *tmp_rfc3779_addr = NULL;
452
43.5k
    struct ASIdentifiers_st *tmp_rfc3779_asid = NULL;
453
43.5k
#endif
454
43.5k
    int ret = 0;
455
456
43.5k
#ifdef tsan_ld_acq
457
    /* Fast lock-free check, see end of the function for details. */
458
43.5k
    if (tsan_ld_acq((TSAN_QUALIFIER int *)&const_x->ex_cached)) {
459
31.1k
        ret = (const_x->ex_flags & EXFLAG_INVALID) == 0;
460
31.1k
        goto done;
461
31.1k
    }
462
12.3k
#endif
463
464
12.3k
    if (!CRYPTO_THREAD_read_lock(const_x->lock))
465
0
        goto done;
466
12.3k
    tmp_ex_flags = const_x->ex_flags;
467
12.3k
    tmp_ex_pcpathlen = const_x->ex_pcpathlen;
468
12.3k
    tmp_ex_kusage = const_x->ex_kusage;
469
12.3k
    tmp_ex_nscert = const_x->ex_nscert;
470
471
12.3k
    if ((tmp_ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */
472
0
        CRYPTO_THREAD_unlock(const_x->lock);
473
0
        ret = (tmp_ex_flags & EXFLAG_INVALID) == 0;
474
0
        goto done;
475
0
    }
476
477
12.3k
    ERR_set_mark();
478
479
    /* Cache the SHA1 digest of the cert */
480
12.3k
    if (!X509_digest(const_x, EVP_sha1(), tmp_sha1_hash, NULL))
481
0
        tmp_ex_flags |= EXFLAG_NO_FINGERPRINT;
482
483
    /* V1 should mean no extensions ... */
484
12.3k
    if (X509_get_version(const_x) == X509_VERSION_1)
485
246
        tmp_ex_flags |= EXFLAG_V1;
486
487
    /* Handle basic constraints */
488
12.3k
    tmp_ex_pathlen = -1;
489
12.3k
    if ((bs = X509_get_ext_d2i(const_x, NID_basic_constraints, &i, NULL)) != NULL) {
490
8.71k
        if (bs->ca)
491
2.03k
            tmp_ex_flags |= EXFLAG_CA;
492
8.71k
        if (bs->pathlen != NULL) {
493
            /*
494
             * The error case !bs->ca is checked by check_chain()
495
             * in case ctx->param->flags & X509_V_FLAG_X509_STRICT
496
             */
497
631
            if (bs->pathlen->type == V_ASN1_NEG_INTEGER) {
498
51
                ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN);
499
51
                tmp_ex_flags |= EXFLAG_INVALID;
500
580
            } else {
501
580
                tmp_ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
502
580
            }
503
631
        }
504
8.71k
        BASIC_CONSTRAINTS_free(bs);
505
8.71k
        tmp_ex_flags |= EXFLAG_BCONS;
506
8.71k
    } else if (i != -1) {
507
652
        tmp_ex_flags |= EXFLAG_INVALID;
508
652
    }
509
510
    /* Handle proxy certificates */
511
12.3k
    if ((pci = X509_get_ext_d2i(const_x, NID_proxyCertInfo, &i, NULL)) != NULL) {
512
39
        if ((tmp_ex_flags & EXFLAG_CA) != 0
513
36
            || X509_get_ext_by_NID(const_x, NID_subject_alt_name, -1) >= 0
514
25
            || X509_get_ext_by_NID(const_x, NID_issuer_alt_name, -1) >= 0) {
515
20
            tmp_ex_flags |= EXFLAG_INVALID;
516
20
        }
517
39
        if (pci->pcPathLengthConstraint != NULL)
518
0
            tmp_ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
519
39
        else
520
39
            tmp_ex_pcpathlen = -1;
521
39
        PROXY_CERT_INFO_EXTENSION_free(pci);
522
39
        tmp_ex_flags |= EXFLAG_PROXY;
523
12.3k
    } else if (i != -1) {
524
31
        tmp_ex_flags |= EXFLAG_INVALID;
525
31
    }
526
527
    /* Handle (basic) key usage */
528
12.3k
    if ((usage = X509_get_ext_d2i(const_x, NID_key_usage, &i, NULL)) != NULL) {
529
4.92k
        tmp_ex_kusage = 0;
530
4.92k
        if (usage->length > 0) {
531
4.88k
            tmp_ex_kusage = usage->data[0];
532
4.88k
            if (usage->length > 1)
533
3
                tmp_ex_kusage |= usage->data[1] << 8;
534
4.88k
        }
535
4.92k
        tmp_ex_flags |= EXFLAG_KUSAGE;
536
4.92k
        ASN1_BIT_STRING_free(usage);
537
        /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */
538
4.92k
        if (tmp_ex_kusage == 0) {
539
260
            ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE);
540
260
            tmp_ex_flags |= EXFLAG_INVALID;
541
260
        }
542
7.47k
    } else if (i != -1) {
543
290
        tmp_ex_flags |= EXFLAG_INVALID;
544
290
    }
545
546
    /* Handle extended key usage */
547
12.3k
    tmp_ex_xkusage = 0;
548
12.3k
    if ((extusage = X509_get_ext_d2i(const_x, NID_ext_key_usage, &i, NULL)) != NULL) {
549
5.92k
        tmp_ex_flags |= EXFLAG_XKUSAGE;
550
11.8k
        for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
551
5.94k
            switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
552
5.46k
            case NID_server_auth:
553
5.46k
                tmp_ex_xkusage |= XKU_SSL_SERVER;
554
5.46k
                break;
555
17
            case NID_client_auth:
556
17
                tmp_ex_xkusage |= XKU_SSL_CLIENT;
557
17
                break;
558
12
            case NID_email_protect:
559
12
                tmp_ex_xkusage |= XKU_SMIME;
560
12
                break;
561
9
            case NID_code_sign:
562
9
                tmp_ex_xkusage |= XKU_CODE_SIGN;
563
9
                break;
564
0
            case NID_ms_sgc:
565
0
            case NID_ns_sgc:
566
0
                tmp_ex_xkusage |= XKU_SGC;
567
0
                break;
568
6
            case NID_OCSP_sign:
569
6
                tmp_ex_xkusage |= XKU_OCSP_SIGN;
570
6
                break;
571
7
            case NID_time_stamp:
572
7
                tmp_ex_xkusage |= XKU_TIMESTAMP;
573
7
                break;
574
3
            case NID_dvcs:
575
3
                tmp_ex_xkusage |= XKU_DVCS;
576
3
                break;
577
0
            case NID_anyExtendedKeyUsage:
578
0
                tmp_ex_xkusage |= XKU_ANYEKU;
579
0
                break;
580
418
            default:
581
                /* Ignore unknown extended key usage */
582
418
                break;
583
5.94k
            }
584
5.94k
        }
585
5.92k
        sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
586
6.47k
    } else if (i != -1) {
587
649
        tmp_ex_flags |= EXFLAG_INVALID;
588
649
    }
589
590
    /* Handle legacy Netscape extension */
591
12.3k
    if ((ns = X509_get_ext_d2i(const_x, NID_netscape_cert_type, &i, NULL)) != NULL) {
592
5
        if (ns->length > 0)
593
3
            tmp_ex_nscert = ns->data[0];
594
2
        else
595
2
            tmp_ex_nscert = 0;
596
5
        tmp_ex_flags |= EXFLAG_NSCERT;
597
5
        ASN1_BIT_STRING_free(ns);
598
12.3k
    } else if (i != -1) {
599
5
        tmp_ex_flags |= EXFLAG_INVALID;
600
5
    }
601
602
    /* Handle subject key identifier and issuer/authority key identifier */
603
12.3k
    tmp_skid = X509_get_ext_d2i(const_x, NID_subject_key_identifier, &i, NULL);
604
12.3k
    if (tmp_skid == NULL && i != -1)
605
267
        tmp_ex_flags |= EXFLAG_INVALID;
606
607
12.3k
    tmp_akid = X509_get_ext_d2i(const_x, NID_authority_key_identifier, &i, NULL);
608
12.3k
    if (tmp_akid == NULL && i != -1)
609
1.25k
        tmp_ex_flags |= EXFLAG_INVALID;
610
611
    /* Setting EXFLAG_SS is equivalent to ossl_x509_likely_issued(const_x, const_x) == X509_V_OK */
612
12.3k
    if (X509_NAME_cmp(X509_get_subject_name(const_x), X509_get_issuer_name(const_x)) == 0) {
613
5.99k
        tmp_ex_flags |= EXFLAG_SI; /* Certificate is self-issued: subject == issuer */
614
        /*
615
         * When the SKID is missing, which is rare for self-issued certs,
616
         * we could afford doing the (accurate) actual self-signature check, but
617
         * decided against it for efficiency reasons and according to RFC 5280,
618
         * CA certs MUST have an SKID and non-root certs MUST have an AKID.
619
         *
620
         * The cached const_x->skid is not populated until the write-lock
621
         * publication below, so the keyid comparison is done directly
622
         * against tmp_skid.  X509_check_akid() is retained for its serial
623
         * number and issuer name checks.
624
         */
625
5.99k
        if ((tmp_akid == NULL || tmp_akid->keyid == NULL || tmp_skid == NULL
626
920
                || ASN1_OCTET_STRING_cmp(tmp_akid->keyid, tmp_skid) == 0)
627
5.82k
            && X509_check_akid(const_x, tmp_akid) == X509_V_OK
628
5.72k
            && check_sig_alg_match(X509_get0_pubkey(const_x), const_x) == X509_V_OK) {
629
            /*
630
             * Assume self-signed if the signature alg matches the pkey alg and
631
             * AKID is missing or matches respective fields in the same cert
632
             * Not checking if any given key usage extension allows signing.
633
             */
634
5.02k
            tmp_ex_flags |= EXFLAG_SS;
635
5.02k
        }
636
5.99k
    }
637
638
    /* Handle subject alternative names and various other extensions */
639
12.3k
    tmp_altname = X509_get_ext_d2i(const_x, NID_subject_alt_name, &i, NULL);
640
12.3k
    if (tmp_altname == NULL && i != -1)
641
363
        tmp_ex_flags |= EXFLAG_INVALID;
642
12.3k
    tmp_nc = X509_get_ext_d2i(const_x, NID_name_constraints, &i, NULL);
643
12.3k
    if (tmp_nc == NULL && i != -1)
644
199
        tmp_ex_flags |= EXFLAG_INVALID;
645
646
    /* Handle CRL distribution point entries */
647
12.3k
    res = setup_crldp(const_x, &tmp_crldp);
648
12.3k
    if (res == 0)
649
304
        tmp_ex_flags |= EXFLAG_INVALID;
650
651
12.3k
#ifndef OPENSSL_NO_RFC3779
652
12.3k
    tmp_rfc3779_addr = X509_get_ext_d2i(const_x, NID_sbgp_ipAddrBlock, &i, NULL);
653
12.3k
    if (tmp_rfc3779_addr == NULL && i != -1)
654
50
        tmp_ex_flags |= EXFLAG_INVALID;
655
656
12.3k
    tmp_rfc3779_asid = X509_get_ext_d2i(const_x, NID_sbgp_autonomousSysNum, &i, NULL);
657
12.3k
    if (tmp_rfc3779_asid == NULL && i != -1)
658
18
        tmp_ex_flags |= EXFLAG_INVALID;
659
12.3k
#endif
660
661
60.4k
    for (i = 0; i < X509_get_ext_count(const_x); i++) {
662
48.8k
        const X509_EXTENSION *ex = X509_get_ext(const_x, i);
663
48.8k
        int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
664
665
48.8k
        if (nid == NID_freshest_crl)
666
511
            tmp_ex_flags |= EXFLAG_FRESHEST;
667
48.8k
        if (!X509_EXTENSION_get_critical(ex))
668
42.6k
            continue;
669
6.15k
        if (!X509_supported_extension(ex)) {
670
746
            tmp_ex_flags |= EXFLAG_CRITICAL;
671
746
            break;
672
746
        }
673
6.15k
    }
674
675
    /* Set x->siginf, ignoring errors due to unsupported algos */
676
12.3k
    (void)ossl_x509_init_sig_info(const_x, &tmp_siginf);
677
678
12.3k
    tmp_ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */
679
12.3k
    ERR_pop_to_mark();
680
681
12.3k
    CRYPTO_THREAD_unlock(const_x->lock);
682
    /*
683
     * Now that we've done all the compute intensive work under read lock
684
     * do all the updating under a write lock
685
     */
686
12.3k
    if (!CRYPTO_THREAD_write_lock(const_x->lock))
687
0
        goto done;
688
689
    /* See if another thread updated this certificate before we got the write lock. */
690
12.3k
    if ((const_x->ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */
691
0
        CRYPTO_THREAD_unlock(const_x->lock);
692
0
        ret = (const_x->ex_flags & EXFLAG_INVALID) == 0;
693
0
        goto done;
694
0
    }
695
696
    /* Otherwise, we have the lock, set the cached fields in the cert. */
697
12.3k
    ((X509 *)const_x)->ex_flags = tmp_ex_flags;
698
12.3k
    ((X509 *)const_x)->ex_pathlen = tmp_ex_pathlen;
699
12.3k
    ((X509 *)const_x)->ex_pcpathlen = tmp_ex_pcpathlen;
700
12.3k
    if (!(tmp_ex_flags & EXFLAG_NO_FINGERPRINT))
701
12.3k
        memcpy(((X509 *)const_x)->sha1_hash, tmp_sha1_hash, SHA_DIGEST_LENGTH);
702
12.3k
    if (tmp_ex_flags & EXFLAG_KUSAGE)
703
4.92k
        ((X509 *)const_x)->ex_kusage = tmp_ex_kusage;
704
12.3k
    ((X509 *)const_x)->ex_xkusage = tmp_ex_xkusage;
705
12.3k
    if (tmp_ex_flags & EXFLAG_NSCERT)
706
5
        ((X509 *)const_x)->ex_nscert = tmp_ex_nscert;
707
12.3k
    ASN1_OCTET_STRING_free(((X509 *)const_x)->skid);
708
12.3k
    ((X509 *)const_x)->skid = tmp_skid;
709
12.3k
    tmp_skid = NULL;
710
12.3k
    AUTHORITY_KEYID_free(((X509 *)const_x)->akid);
711
12.3k
    ((X509 *)const_x)->akid = tmp_akid;
712
12.3k
    tmp_akid = NULL;
713
12.3k
    sk_GENERAL_NAME_pop_free(((X509 *)const_x)->altname, GENERAL_NAME_free);
714
12.3k
    ((X509 *)const_x)->altname = tmp_altname;
715
12.3k
    tmp_altname = NULL;
716
12.3k
    NAME_CONSTRAINTS_free(((X509 *)const_x)->nc);
717
12.3k
    ((X509 *)const_x)->nc = tmp_nc;
718
12.3k
    tmp_nc = NULL;
719
12.3k
    sk_DIST_POINT_pop_free(((X509 *)const_x)->crldp, DIST_POINT_free);
720
12.3k
    ((X509 *)const_x)->crldp = tmp_crldp;
721
12.3k
    tmp_crldp = NULL;
722
12.3k
#ifndef OPENSSL_NO_RFC3779
723
12.3k
    sk_IPAddressFamily_pop_free(((X509 *)const_x)->rfc3779_addr, IPAddressFamily_free);
724
12.3k
    ((X509 *)const_x)->rfc3779_addr = tmp_rfc3779_addr;
725
12.3k
    tmp_rfc3779_addr = NULL;
726
12.3k
    ASIdentifiers_free(((X509 *)const_x)->rfc3779_asid);
727
12.3k
    ((X509 *)const_x)->rfc3779_asid = tmp_rfc3779_asid;
728
12.3k
    tmp_rfc3779_asid = NULL;
729
12.3k
#endif
730
12.3k
    ((X509 *)const_x)->siginf = tmp_siginf;
731
732
12.3k
#ifdef tsan_st_rel
733
12.3k
    tsan_st_rel((TSAN_QUALIFIER int *)&const_x->ex_cached, 1);
734
    /*
735
     * Above store triggers fast lock-free check in the beginning of the
736
     * function. But one has to ensure that the structure is "stable", i.e.
737
     * all stores are visible on all processors. Hence the release fence.
738
     */
739
12.3k
#endif
740
12.3k
    CRYPTO_THREAD_unlock(const_x->lock);
741
12.3k
    if (tmp_ex_flags & EXFLAG_INVALID) {
742
3.15k
        ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE);
743
3.15k
        goto done;
744
3.15k
    }
745
746
9.23k
    ret = 1;
747
748
43.5k
done:
749
43.5k
    ASN1_OCTET_STRING_free(tmp_skid);
750
43.5k
    AUTHORITY_KEYID_free(tmp_akid);
751
43.5k
    sk_GENERAL_NAME_pop_free(tmp_altname, GENERAL_NAME_free);
752
43.5k
    NAME_CONSTRAINTS_free(tmp_nc);
753
43.5k
    sk_DIST_POINT_pop_free(tmp_crldp, DIST_POINT_free);
754
43.5k
#ifndef OPENSSL_NO_RFC3779
755
43.5k
    sk_IPAddressFamily_pop_free(tmp_rfc3779_addr, IPAddressFamily_free);
756
43.5k
    ASIdentifiers_free(tmp_rfc3779_asid);
757
43.5k
#endif
758
43.5k
    return ret;
759
9.23k
}
760
761
/*-
762
 * CA checks common to all purposes
763
 * return codes:
764
 * 0 not a CA
765
 * 1 is a CA
766
 * 2 Only possible in older versions of openSSL when basicConstraints are absent
767
 *   new versions will not return this value. May be a CA
768
 * 3 basicConstraints absent but self-signed V1.
769
 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
770
 * 5 Netscape specific CA Flags present
771
 */
772
773
static int check_ca(const X509 *x)
774
9.64k
{
775
    /* keyUsage if present should allow cert signing */
776
9.64k
    if (ku_reject(x, KU_KEY_CERT_SIGN))
777
76
        return 0;
778
9.56k
    if ((x->ex_flags & EXFLAG_BCONS) != 0) {
779
        /* If basicConstraints says not a CA then say so */
780
2.44k
        return (x->ex_flags & EXFLAG_CA) != 0;
781
7.12k
    } else {
782
        /* We support V1 roots for...  uh, I don't really know why. */
783
7.12k
        if ((x->ex_flags & V1_ROOT) == V1_ROOT)
784
94
            return 3;
785
        /*
786
         * If key usage present it must have certSign so tolerate it
787
         */
788
7.03k
        else if ((x->ex_flags & EXFLAG_KUSAGE) != 0)
789
35
            return 4;
790
        /* Older certificates could have Netscape-specific CA types */
791
6.99k
        else if ((x->ex_flags & EXFLAG_NSCERT) != 0
792
14
            && (x->ex_nscert & NS_ANY_CA) != 0)
793
6
            return 5;
794
        /* Can this still be regarded a CA certificate?  I doubt it. */
795
6.99k
        return 0;
796
7.12k
    }
797
9.56k
}
798
799
void X509_set_proxy_flag(X509 *x)
800
0
{
801
0
    if (CRYPTO_THREAD_write_lock(x->lock)) {
802
0
        x->ex_flags |= EXFLAG_PROXY;
803
0
        CRYPTO_THREAD_unlock(x->lock);
804
0
    }
805
0
}
806
807
void X509_set_proxy_pathlen(X509 *x, long l)
808
0
{
809
0
    x->ex_pcpathlen = l;
810
0
}
811
812
int X509_check_ca(const X509 *x)
813
10.7k
{
814
    /* Note 0 normally means "not a CA" - but in this case means error. */
815
10.7k
    if (!ossl_x509v3_cache_extensions(x))
816
1.32k
        return 0;
817
818
9.42k
    return check_ca(x);
819
10.7k
}
820
821
/* Check SSL CA: common checks for SSL client and server. */
822
static int check_ssl_ca(const X509 *x)
823
0
{
824
0
    int ca_ret = check_ca(x);
825
826
0
    if (ca_ret == 0)
827
0
        return 0;
828
    /* Check nsCertType if present */
829
0
    return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0;
830
0
}
831
832
static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
833
    int non_leaf)
834
0
{
835
0
    if (xku_reject(x, XKU_SSL_CLIENT))
836
0
        return 0;
837
0
    if (non_leaf)
838
0
        return check_ssl_ca(x);
839
    /* We need to do digital signatures or key agreement */
840
0
    if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
841
0
        return 0;
842
    /* nsCertType if present should allow SSL client use */
843
0
    if (ns_reject(x, NS_SSL_CLIENT))
844
0
        return 0;
845
0
    return 1;
846
0
}
847
848
/*
849
 * Key usage needed for TLS/SSL server: digital signature, encipherment or
850
 * key agreement. The ssl code can check this more thoroughly for individual
851
 * key types.
852
 */
853
#define KU_TLS \
854
    KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT
855
856
static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
857
    int non_leaf)
858
0
{
859
0
    if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
860
0
        return 0;
861
0
    if (non_leaf)
862
0
        return check_ssl_ca(x);
863
864
0
    if (ns_reject(x, NS_SSL_SERVER))
865
0
        return 0;
866
0
    if (ku_reject(x, KU_TLS))
867
0
        return 0;
868
869
0
    return 1;
870
0
}
871
872
static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
873
    int non_leaf)
874
0
{
875
0
    int ret = check_purpose_ssl_server(xp, x, non_leaf);
876
877
0
    if (!ret || non_leaf)
878
0
        return ret;
879
    /* We need to encipher or Netscape complains */
880
0
    return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;
881
0
}
882
883
/* common S/MIME client checks */
884
static int purpose_smime(const X509 *x, int non_leaf)
885
0
{
886
0
    if (xku_reject(x, XKU_SMIME))
887
0
        return 0;
888
0
    if (non_leaf) {
889
0
        int ca_ret = check_ca(x);
890
891
0
        if (ca_ret == 0)
892
0
            return 0;
893
        /* Check nsCertType if present */
894
0
        if (ca_ret != 5 || (x->ex_nscert & NS_SMIME_CA) != 0)
895
0
            return ca_ret;
896
0
        else
897
0
            return 0;
898
0
    }
899
0
    if ((x->ex_flags & EXFLAG_NSCERT) != 0) {
900
0
        if ((x->ex_nscert & NS_SMIME) != 0)
901
0
            return 1;
902
        /* Workaround for some buggy certificates */
903
0
        return (x->ex_nscert & NS_SSL_CLIENT) != 0 ? 2 : 0;
904
0
    }
905
0
    return 1;
906
0
}
907
908
static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
909
    int non_leaf)
910
0
{
911
0
    int ret = purpose_smime(x, non_leaf);
912
913
0
    if (!ret || non_leaf)
914
0
        return ret;
915
0
    return ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION) ? 0 : ret;
916
0
}
917
918
static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
919
    int non_leaf)
920
0
{
921
0
    int ret = purpose_smime(x, non_leaf);
922
923
0
    if (!ret || non_leaf)
924
0
        return ret;
925
0
    return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;
926
0
}
927
928
static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
929
    int non_leaf)
930
0
{
931
0
    if (non_leaf) {
932
0
        int ca_ret = check_ca(x);
933
934
0
        return ca_ret == 2 ? 0 : ca_ret;
935
0
    }
936
0
    return !ku_reject(x, KU_CRL_SIGN);
937
0
}
938
939
/*
940
 * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
941
 * is valid. Additional checks must be made on the chain.
942
 */
943
static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x,
944
    int non_leaf)
945
499
{
946
    /*
947
     * Must be a valid CA.  Should we really support the "I don't know" value
948
     * (2)?
949
     */
950
499
    if (non_leaf)
951
219
        return check_ca(x);
952
    /* Leaf certificate is checked in OCSP_basic_verify() */
953
280
    return 1;
954
499
}
955
956
static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
957
    int non_leaf)
958
0
{
959
0
    int i_ext;
960
961
    /*
962
     * If non_leaf is true we must check if this is a valid CA certificate.
963
     * The extra requirements by the CA/Browser Forum are not checked.
964
     */
965
0
    if (non_leaf)
966
0
        return check_ca(x);
967
968
    /*
969
     * Key Usage is checked according to RFC 5280 and
970
     * Extended Key Usage attributes is checked according to RFC 3161.
971
     * The extra (and somewhat conflicting) CA/Browser Forum
972
     * Baseline Requirements for the Issuance and Management of
973
     * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,
974
     * Section 7.1.2.3: Code signing and Timestamp Certificate are not checked.
975
     */
976
    /*
977
     * Check the optional key usage field:
978
     * if Key Usage is present, it must be one of digitalSignature
979
     * and/or nonRepudiation (other values are not consistent and shall
980
     * be rejected).
981
     */
982
0
    if ((x->ex_flags & EXFLAG_KUSAGE) != 0
983
0
        && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
984
0
        return 0;
985
986
    /* Only timestamp key usage is permitted and it's required. */
987
0
    if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP)
988
0
        return 0;
989
990
    /* Extended Key Usage MUST be critical */
991
0
    i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
992
0
    if (i_ext >= 0
993
0
        && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext)))
994
0
        return 0;
995
0
    return 1;
996
0
}
997
998
static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x,
999
    int non_leaf)
1000
0
{
1001
0
    int i_ext;
1002
1003
    /*
1004
     * If non_leaf is true we must check if this is a valid CA certificate.
1005
     * The extra requirements by the CA/Browser Forum are not checked.
1006
     */
1007
0
    if (non_leaf)
1008
0
        return check_ca(x);
1009
1010
    /*
1011
     * Check the key usage and extended key usage fields:
1012
     *
1013
     * Reference: CA/Browser Forum,
1014
     * Baseline Requirements for the Issuance and Management of
1015
     * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,
1016
     * Section 7.1.2.3: Code signing and Timestamp Certificate
1017
     *
1018
     * Checking covers Key Usage and Extended Key Usage attributes.
1019
     * The certificatePolicies, cRLDistributionPoints (CDP), and
1020
     * authorityInformationAccess (AIA) extensions are so far not checked.
1021
     */
1022
    /* Key Usage */
1023
0
    if ((x->ex_flags & EXFLAG_KUSAGE) == 0)
1024
0
        return 0;
1025
0
    if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0)
1026
0
        return 0;
1027
0
    if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0)
1028
0
        return 0;
1029
1030
    /* Key Usage MUST be critical */
1031
0
    i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1);
1032
0
    if (i_ext < 0)
1033
0
        return 0;
1034
0
    if (i_ext >= 0) {
1035
0
        const X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
1036
0
        if (!X509_EXTENSION_get_critical(ext))
1037
0
            return 0;
1038
0
    }
1039
1040
    /* Extended Key Usage */
1041
0
    if ((x->ex_flags & EXFLAG_XKUSAGE) == 0)
1042
0
        return 0;
1043
0
    if ((x->ex_xkusage & XKU_CODE_SIGN) == 0)
1044
0
        return 0;
1045
0
    if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0)
1046
0
        return 0;
1047
1048
0
    return 1;
1049
0
}
1050
1051
static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x,
1052
    int non_leaf)
1053
0
{
1054
0
    return 1;
1055
0
}
1056
1057
/*-
1058
 * Various checks to see if one certificate potentially issued the second.
1059
 * This can be used to prune a set of possible issuer certificates which
1060
 * have been looked up using some simple method such as by subject name.
1061
 * These are:
1062
 * 1. issuer_name(subject) == subject_name(issuer)
1063
 * 2. If akid(subject) exists, it matches the respective issuer fields.
1064
 * 3. subject signature algorithm == issuer public key algorithm
1065
 * 4. If key_usage(issuer) exists, it allows for signing subject.
1066
 * Note that this does not include actually checking the signature.
1067
 * Returns 0 for OK, or positive for reason for mismatch
1068
 * where reason codes match those for X509_verify_cert().
1069
 */
1070
int X509_check_issued(const X509 *issuer, const X509 *subject)
1071
39.0k
{
1072
39.0k
    int ret;
1073
1074
39.0k
    if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK)
1075
34.5k
        return ret;
1076
4.51k
    return ossl_x509_signing_allowed(issuer, subject);
1077
39.0k
}
1078
1079
/*
1080
 * Do the checks 1., 2., and 3. as described above for X509_check_issued().
1081
 * These are very similar to a section of ossl_x509v3_cache_extensions().
1082
 * If |issuer| equals |subject| (such that self-signature should be checked),
1083
 * use the EXFLAG_SS result of ossl_x509v3_cache_extensions().
1084
 */
1085
int ossl_x509_likely_issued(const X509 *issuer, const X509 *subject)
1086
31.7k
{
1087
31.7k
    int ret;
1088
1089
31.7k
    if (X509_NAME_cmp(X509_get_subject_name(issuer),
1090
31.7k
            X509_get_issuer_name(subject))
1091
31.7k
        != 0)
1092
16.0k
        return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
1093
1094
    /* set issuer->skid, subject->akid, and subject->ex_flags */
1095
15.7k
    if (!ossl_x509v3_cache_extensions(issuer)
1096
11.0k
        || !ossl_x509v3_cache_extensions(subject))
1097
5.31k
        return X509_V_ERR_UNSPECIFIED;
1098
1099
10.4k
    if (issuer == subject
1100
3.95k
        || (X509_NAME_cmp(X509_get_issuer_name(issuer), X509_get_issuer_name(subject)) == 0
1101
2.37k
            && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), X509_get0_serialNumber(subject)) == 0))
1102
        /*
1103
         * At this point, we can assume that issuer and subject
1104
         * are semantically the same cert because they are identical
1105
         * or at least have the same issuer and serial number,
1106
         * which (for any sane cert issuer) implies equality of the two certs.
1107
         * In this case, for consistency with chain building and validation,
1108
         * we make our issuance judgment depend on the presence of EXFLAG_SS.
1109
         * This is used for corrected chain building in the corner case of
1110
         * a self-issued but not actually self-signed trust anchor cert
1111
         * without subject and issuer key identifiers (i.e., no SKID and AKID).
1112
         */
1113
6.56k
        return (issuer->ex_flags & EXFLAG_SS) != 0
1114
6.56k
            ? X509_V_OK
1115
6.56k
            : X509_V_ERR_CERT_SIGNATURE_FAILURE;
1116
1117
3.88k
    ret = X509_check_akid(issuer, subject->akid);
1118
3.88k
    if (ret != X509_V_OK)
1119
529
        return ret;
1120
1121
    /* Check if the subject signature alg matches the issuer's PUBKEY alg */
1122
3.35k
    return check_sig_alg_match(X509_get0_pubkey(issuer), subject);
1123
3.88k
}
1124
1125
/*-
1126
 * Check if certificate I<issuer> is allowed to issue certificate I<subject>
1127
 * according to the B<keyUsage> field of I<issuer> if present
1128
 * depending on any proxyCertInfo extension of I<subject>.
1129
 * Returns 0 for OK, or positive for reason for rejection
1130
 * where reason codes match those for X509_verify_cert().
1131
 */
1132
int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject)
1133
8.00k
{
1134
8.00k
    if ((subject->ex_flags & EXFLAG_PROXY) != 0) {
1135
4
        if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
1136
0
            return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
1137
7.99k
    } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) {
1138
199
        return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
1139
199
    }
1140
7.80k
    return X509_V_OK;
1141
8.00k
}
1142
1143
/*
1144
 * check if all sub-fields of the authority key identifier information akid,
1145
 * as far as present, match the respective subjectKeyIdentifier extension (if
1146
 * present in issuer), serialNumber field, and issuer fields of issuer.
1147
 * returns X509_V_OK also if akid is NULL because this means no restriction.
1148
 */
1149
int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid)
1150
94.9k
{
1151
94.9k
    if (akid == NULL)
1152
73.8k
        return X509_V_OK;
1153
1154
    /* Check key ids (if present) */
1155
21.0k
    if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
1156
1.47k
        return X509_V_ERR_AKID_SKID_MISMATCH;
1157
    /* Check serial number */
1158
19.5k
    if (akid->serial && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial))
1159
1.14k
        return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1160
    /* Check issuer name */
1161
18.4k
    if (akid->issuer) {
1162
        /*
1163
         * Ugh, for some peculiar reason AKID includes SEQUENCE OF
1164
         * GeneralName. So look for a DirName. There may be more than one but
1165
         * we only take any notice of the first.
1166
         */
1167
358
        GENERAL_NAMES *gens = akid->issuer;
1168
358
        GENERAL_NAME *gen;
1169
358
        X509_NAME *nm = NULL;
1170
358
        int i;
1171
1172
650
        for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1173
447
            gen = sk_GENERAL_NAME_value(gens, i);
1174
447
            if (gen->type == GEN_DIRNAME) {
1175
155
                nm = gen->d.dirn;
1176
155
                break;
1177
155
            }
1178
447
        }
1179
358
        if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0)
1180
106
            return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1181
358
    }
1182
18.3k
    return X509_V_OK;
1183
18.4k
}
1184
1185
uint32_t X509_get_extension_flags(const X509 *x)
1186
32.9k
{
1187
    /* Call for side-effect of computing hash and caching extensions */
1188
32.9k
    X509_check_purpose(x, -1, 0);
1189
32.9k
    return x->ex_flags;
1190
32.9k
}
1191
1192
uint32_t X509_get_key_usage(const X509 *x)
1193
24.0k
{
1194
    /* Call for side-effect of computing hash and caching extensions */
1195
24.0k
    if (X509_check_purpose(x, -1, 0) != 1)
1196
0
        return 0;
1197
24.0k
    return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX;
1198
24.0k
}
1199
1200
uint32_t X509_get_extended_key_usage(const X509 *x)
1201
8
{
1202
    /* Call for side-effect of computing hash and caching extensions */
1203
8
    if (X509_check_purpose(x, -1, 0) != 1)
1204
0
        return 0;
1205
8
    return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX;
1206
8
}
1207
1208
const ASN1_OCTET_STRING *X509_get0_subject_key_id(const X509 *x)
1209
6.90k
{
1210
    /* Call for side-effect of computing hash and caching extensions */
1211
6.90k
    if (X509_check_purpose(x, -1, 0) != 1)
1212
808
        return NULL;
1213
6.09k
    return x->skid;
1214
6.90k
}
1215
1216
const ASN1_OCTET_STRING *X509_get0_authority_key_id(const X509 *x)
1217
0
{
1218
    /* Call for side-effect of computing hash and caching extensions */
1219
0
    if (X509_check_purpose(x, -1, 0) != 1)
1220
0
        return NULL;
1221
0
    return (x->akid != NULL ? x->akid->keyid : NULL);
1222
0
}
1223
1224
const GENERAL_NAMES *X509_get0_authority_issuer(const X509 *x)
1225
0
{
1226
    /* Call for side-effect of computing hash and caching extensions */
1227
0
    if (X509_check_purpose(x, -1, 0) != 1)
1228
0
        return NULL;
1229
0
    return (x->akid != NULL ? x->akid->issuer : NULL);
1230
0
}
1231
1232
const ASN1_INTEGER *X509_get0_authority_serial(const X509 *x)
1233
0
{
1234
    /* Call for side-effect of computing hash and caching extensions */
1235
0
    if (X509_check_purpose(x, -1, 0) != 1)
1236
0
        return NULL;
1237
0
    return (x->akid != NULL ? x->akid->serial : NULL);
1238
0
}
1239
1240
long X509_get_pathlen(const X509 *x)
1241
0
{
1242
    /* Called for side effect of caching extensions */
1243
0
    if (X509_check_purpose(x, -1, 0) != 1
1244
0
        || (x->ex_flags & EXFLAG_BCONS) == 0)
1245
0
        return -1;
1246
0
    return x->ex_pathlen;
1247
0
}
1248
1249
long X509_get_proxy_pathlen(const X509 *x)
1250
0
{
1251
    /* Called for side effect of caching extensions */
1252
0
    if (X509_check_purpose(x, -1, 0) != 1
1253
0
        || (x->ex_flags & EXFLAG_PROXY) == 0)
1254
0
        return -1;
1255
0
    return x->ex_pcpathlen;
1256
0
}