Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/v3_purp.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2025 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
0
#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
0
{
84
0
    int idx;
85
0
    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
0
    if (!ossl_x509v3_cache_extensions((X509 *)x))
92
0
        return -1;
93
0
    if (id == -1)
94
0
        return 1;
95
96
0
    idx = X509_PURPOSE_get_by_id(id);
97
0
    if (idx == -1)
98
0
        return -1;
99
0
    pt = X509_PURPOSE_get0(idx);
100
0
    return pt->check_purpose(pt, x, non_leaf);
101
0
}
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
0
{
133
0
    if (idx < 0)
134
0
        return NULL;
135
0
    if (idx < (int)X509_PURPOSE_COUNT)
136
0
        return xstandard + idx;
137
0
    return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
138
0
}
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
0
{
156
0
    X509_PURPOSE tmp;
157
0
    int idx;
158
159
0
    if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX)
160
0
        return purpose - X509_PURPOSE_MIN;
161
0
    if (xptable == NULL)
162
0
        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
0
{
301
0
    return *a - *b;
302
0
}
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
0
{
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
0
    static const int supported_nids[] = {
317
0
        NID_netscape_cert_type, /* 71 */
318
0
        NID_key_usage, /* 83 */
319
0
        NID_subject_alt_name, /* 85 */
320
0
        NID_basic_constraints, /* 87 */
321
0
        NID_certificate_policies, /* 89 */
322
0
        NID_crl_distribution_points, /* 103 */
323
0
        NID_ext_key_usage, /* 126 */
324
0
#ifndef OPENSSL_NO_RFC3779
325
0
        NID_sbgp_ipAddrBlock, /* 290 */
326
0
        NID_sbgp_autonomousSysNum, /* 291 */
327
0
#endif
328
0
        NID_id_pkix_OCSP_noCheck, /* 369 */
329
0
        NID_policy_constraints, /* 401 */
330
0
        NID_proxyCertInfo, /* 663 */
331
0
        NID_name_constraints, /* 666 */
332
0
        NID_policy_mappings, /* 747 */
333
0
        NID_inhibit_any_policy /* 748 */
334
0
    };
335
336
0
    int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
337
338
0
    if (ex_nid == NID_undef)
339
0
        return 0;
340
341
0
    if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
342
0
        return 1;
343
0
    return 0;
344
0
}
345
346
/* Returns 1 on success, 0 if x is invalid, -1 on (internal) error. */
347
static int setup_dp(const X509 *x, DIST_POINT *dp)
348
0
{
349
0
    const X509_NAME *iname = NULL;
350
0
    int i;
351
352
0
    if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) {
353
0
        ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT);
354
0
        return 0;
355
0
    }
356
0
    if (dp->reasons != NULL) {
357
0
        if (dp->reasons->length > 0)
358
0
            dp->dp_reasons = dp->reasons->data[0];
359
0
        if (dp->reasons->length > 1)
360
0
            dp->dp_reasons |= (dp->reasons->data[1] << 8);
361
0
        dp->dp_reasons &= CRLDP_ALL_REASONS;
362
0
    } else {
363
0
        dp->dp_reasons = CRLDP_ALL_REASONS;
364
0
    }
365
0
    if (dp->distpoint == NULL || dp->distpoint->type != 1)
366
0
        return 1;
367
368
    /* Handle name fragment given by nameRelativeToCRLIssuer */
369
    /*
370
     * Note that the below way of determining iname is not really compliant
371
     * with https://tools.ietf.org/html/rfc5280#section-4.2.1.13
372
     * According to it, sk_GENERAL_NAME_num(dp->CRLissuer) MUST be <= 1
373
     * and any CRLissuer could be of type different to GEN_DIRNAME.
374
     */
375
0
    for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
376
0
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
377
378
0
        if (gen->type == GEN_DIRNAME) {
379
0
            iname = gen->d.directoryName;
380
0
            break;
381
0
        }
382
0
    }
383
0
    if (iname == NULL)
384
0
        iname = X509_get_issuer_name(x);
385
0
    return DIST_POINT_set_dpname(dp->distpoint, iname) ? 1 : -1;
386
0
}
387
388
/* Return 1 on success, 0 if x is invalid, -1 on (internal) error. */
389
static int setup_crldp(const X509 *x, STACK_OF(DIST_POINT) **tmp_crldp)
390
0
{
391
0
    int i;
392
393
0
    *tmp_crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
394
0
    if (*tmp_crldp == NULL && i != -1)
395
0
        return 0;
396
397
0
    for (i = 0; i < sk_DIST_POINT_num(*tmp_crldp); i++) {
398
0
        int res = setup_dp(x, sk_DIST_POINT_value(*tmp_crldp, i));
399
400
0
        if (res < 1)
401
0
            return res;
402
0
    }
403
0
    return 1;
404
0
}
405
406
/* Check that issuer public key algorithm matches subject signature algorithm */
407
static int check_sig_alg_match(const EVP_PKEY *issuer_key, const X509 *subject)
408
0
{
409
0
    int subj_sig_nid;
410
411
0
    if (issuer_key == NULL)
412
0
        return X509_V_ERR_NO_ISSUER_PUBLIC_KEY;
413
0
    if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm),
414
0
            NULL, &subj_sig_nid)
415
0
        == 0)
416
0
        return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM;
417
0
    if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid))
418
0
        || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss))
419
0
        return X509_V_OK;
420
0
    return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH;
421
0
}
422
423
0
#define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)
424
#define ku_reject(x, usage) \
425
0
    (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0)
426
#define xku_reject(x, usage) \
427
0
    (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0)
428
#define ns_reject(x, usage) \
429
0
    (((x)->ex_flags & EXFLAG_NSCERT) != 0 && ((x)->ex_nscert & (usage)) == 0)
430
431
/*
432
 * Cache info on various X.509v3 extensions and further derived information,
433
 * e.g., if cert 'x' is self-issued, in x->ex_flags and other internal fields.
434
 * x->sha1_hash is filled in, or else EXFLAG_NO_FINGERPRINT is set in x->flags.
435
 * X509_SIG_INFO_VALID is set in x->flags if x->siginf was filled successfully.
436
 * Set EXFLAG_INVALID and return 0 in case the certificate is invalid.
437
 *
438
 * This is usually called by side-effect on objects, and forces us to keep
439
 * mutable X509 objects around. We should really make this go away.
440
 * In the interest of being able to do so, this function explicitly takes
441
 * a const argument and casts away const.
442
 */
443
int ossl_x509v3_cache_extensions(const X509 *const_x)
444
0
{
445
0
    BASIC_CONSTRAINTS *bs;
446
0
    PROXY_CERT_INFO_EXTENSION *pci;
447
0
    ASN1_BIT_STRING *usage;
448
0
    ASN1_BIT_STRING *ns;
449
0
    EXTENDED_KEY_USAGE *extusage;
450
0
    int i;
451
0
    int res;
452
0
    uint32_t tmp_ex_flags;
453
0
    unsigned char tmp_sha1_hash[SHA_DIGEST_LENGTH];
454
0
    long tmp_ex_pathlen;
455
0
    long tmp_ex_pcpathlen;
456
0
    uint32_t tmp_ex_kusage;
457
0
    uint32_t tmp_ex_xkusage;
458
0
    uint32_t tmp_ex_nscert;
459
0
    ASN1_OCTET_STRING *tmp_skid;
460
0
    AUTHORITY_KEYID *tmp_akid;
461
0
    STACK_OF(GENERAL_NAME) *tmp_altname;
462
0
    NAME_CONSTRAINTS *tmp_nc;
463
0
    STACK_OF(DIST_POINT) *tmp_crldp = NULL;
464
0
    X509_SIG_INFO tmp_siginf;
465
466
0
#ifdef tsan_ld_acq
467
    /* Fast lock-free check, see end of the function for details. */
468
0
    if (tsan_ld_acq((TSAN_QUALIFIER int *)&const_x->ex_cached))
469
0
        return (const_x->ex_flags & EXFLAG_INVALID) == 0;
470
0
#endif
471
472
0
    if (!CRYPTO_THREAD_read_lock(const_x->lock))
473
0
        return 0;
474
0
    tmp_ex_flags = const_x->ex_flags;
475
0
    tmp_ex_pcpathlen = const_x->ex_pcpathlen;
476
0
    tmp_ex_kusage = const_x->ex_kusage;
477
0
    tmp_ex_nscert = const_x->ex_nscert;
478
479
0
    if ((tmp_ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */
480
0
        CRYPTO_THREAD_unlock(const_x->lock);
481
0
        return (tmp_ex_flags & EXFLAG_INVALID) == 0;
482
0
    }
483
484
0
    ERR_set_mark();
485
486
    /* Cache the SHA1 digest of the cert */
487
0
    if (!X509_digest(const_x, EVP_sha1(), tmp_sha1_hash, NULL))
488
0
        tmp_ex_flags |= EXFLAG_NO_FINGERPRINT;
489
490
    /* V1 should mean no extensions ... */
491
0
    if (X509_get_version(const_x) == X509_VERSION_1)
492
0
        tmp_ex_flags |= EXFLAG_V1;
493
494
    /* Handle basic constraints */
495
0
    tmp_ex_pathlen = -1;
496
0
    if ((bs = X509_get_ext_d2i(const_x, NID_basic_constraints, &i, NULL)) != NULL) {
497
0
        if (bs->ca)
498
0
            tmp_ex_flags |= EXFLAG_CA;
499
0
        if (bs->pathlen != NULL) {
500
            /*
501
             * The error case !bs->ca is checked by check_chain()
502
             * in case ctx->param->flags & X509_V_FLAG_X509_STRICT
503
             */
504
0
            if (bs->pathlen->type == V_ASN1_NEG_INTEGER) {
505
0
                ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN);
506
0
                tmp_ex_flags |= EXFLAG_INVALID;
507
0
            } else {
508
0
                tmp_ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
509
0
            }
510
0
        }
511
0
        BASIC_CONSTRAINTS_free(bs);
512
0
        tmp_ex_flags |= EXFLAG_BCONS;
513
0
    } else if (i != -1) {
514
0
        tmp_ex_flags |= EXFLAG_INVALID;
515
0
    }
516
517
    /* Handle proxy certificates */
518
0
    if ((pci = X509_get_ext_d2i(const_x, NID_proxyCertInfo, &i, NULL)) != NULL) {
519
0
        if ((tmp_ex_flags & EXFLAG_CA) != 0
520
0
            || X509_get_ext_by_NID(const_x, NID_subject_alt_name, -1) >= 0
521
0
            || X509_get_ext_by_NID(const_x, NID_issuer_alt_name, -1) >= 0) {
522
0
            tmp_ex_flags |= EXFLAG_INVALID;
523
0
        }
524
0
        if (pci->pcPathLengthConstraint != NULL)
525
0
            tmp_ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
526
0
        else
527
0
            tmp_ex_pcpathlen = -1;
528
0
        PROXY_CERT_INFO_EXTENSION_free(pci);
529
0
        tmp_ex_flags |= EXFLAG_PROXY;
530
0
    } else if (i != -1) {
531
0
        tmp_ex_flags |= EXFLAG_INVALID;
532
0
    }
533
534
    /* Handle (basic) key usage */
535
0
    if ((usage = X509_get_ext_d2i(const_x, NID_key_usage, &i, NULL)) != NULL) {
536
0
        tmp_ex_kusage = 0;
537
0
        if (usage->length > 0) {
538
0
            tmp_ex_kusage = usage->data[0];
539
0
            if (usage->length > 1)
540
0
                tmp_ex_kusage |= usage->data[1] << 8;
541
0
        }
542
0
        tmp_ex_flags |= EXFLAG_KUSAGE;
543
0
        ASN1_BIT_STRING_free(usage);
544
        /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */
545
0
        if (tmp_ex_kusage == 0) {
546
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE);
547
0
            tmp_ex_flags |= EXFLAG_INVALID;
548
0
        }
549
0
    } else if (i != -1) {
550
0
        tmp_ex_flags |= EXFLAG_INVALID;
551
0
    }
552
553
    /* Handle extended key usage */
554
0
    tmp_ex_xkusage = 0;
555
0
    if ((extusage = X509_get_ext_d2i(const_x, NID_ext_key_usage, &i, NULL)) != NULL) {
556
0
        tmp_ex_flags |= EXFLAG_XKUSAGE;
557
0
        for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
558
0
            switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
559
0
            case NID_server_auth:
560
0
                tmp_ex_xkusage |= XKU_SSL_SERVER;
561
0
                break;
562
0
            case NID_client_auth:
563
0
                tmp_ex_xkusage |= XKU_SSL_CLIENT;
564
0
                break;
565
0
            case NID_email_protect:
566
0
                tmp_ex_xkusage |= XKU_SMIME;
567
0
                break;
568
0
            case NID_code_sign:
569
0
                tmp_ex_xkusage |= XKU_CODE_SIGN;
570
0
                break;
571
0
            case NID_ms_sgc:
572
0
            case NID_ns_sgc:
573
0
                tmp_ex_xkusage |= XKU_SGC;
574
0
                break;
575
0
            case NID_OCSP_sign:
576
0
                tmp_ex_xkusage |= XKU_OCSP_SIGN;
577
0
                break;
578
0
            case NID_time_stamp:
579
0
                tmp_ex_xkusage |= XKU_TIMESTAMP;
580
0
                break;
581
0
            case NID_dvcs:
582
0
                tmp_ex_xkusage |= XKU_DVCS;
583
0
                break;
584
0
            case NID_anyExtendedKeyUsage:
585
0
                tmp_ex_xkusage |= XKU_ANYEKU;
586
0
                break;
587
0
            default:
588
                /* Ignore unknown extended key usage */
589
0
                break;
590
0
            }
591
0
        }
592
0
        sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
593
0
    } else if (i != -1) {
594
0
        tmp_ex_flags |= EXFLAG_INVALID;
595
0
    }
596
597
    /* Handle legacy Netscape extension */
598
0
    if ((ns = X509_get_ext_d2i(const_x, NID_netscape_cert_type, &i, NULL)) != NULL) {
599
0
        if (ns->length > 0)
600
0
            tmp_ex_nscert = ns->data[0];
601
0
        else
602
0
            tmp_ex_nscert = 0;
603
0
        tmp_ex_flags |= EXFLAG_NSCERT;
604
0
        ASN1_BIT_STRING_free(ns);
605
0
    } else if (i != -1) {
606
0
        tmp_ex_flags |= EXFLAG_INVALID;
607
0
    }
608
609
    /* Handle subject key identifier and issuer/authority key identifier */
610
0
    tmp_skid = X509_get_ext_d2i(const_x, NID_subject_key_identifier, &i, NULL);
611
0
    if (tmp_skid == NULL && i != -1)
612
0
        tmp_ex_flags |= EXFLAG_INVALID;
613
614
0
    tmp_akid = X509_get_ext_d2i(const_x, NID_authority_key_identifier, &i, NULL);
615
0
    if (tmp_akid == NULL && i != -1)
616
0
        tmp_ex_flags |= EXFLAG_INVALID;
617
618
    /* Check if subject name matches issuer */
619
0
    if (X509_NAME_cmp(X509_get_subject_name(const_x), X509_get_issuer_name(const_x)) == 0) {
620
0
        tmp_ex_flags |= EXFLAG_SI; /* Cert is self-issued */
621
0
        if (X509_check_akid(const_x, tmp_akid) == X509_V_OK /* SKID matches AKID */
622
0
            /* .. and the signature alg matches the PUBKEY alg: */
623
0
            && check_sig_alg_match(X509_get0_pubkey(const_x), const_x) == X509_V_OK)
624
0
            tmp_ex_flags |= EXFLAG_SS; /* indicate self-signed */
625
        /* This is very related to ossl_x509_likely_issued(x, x) == X509_V_OK */
626
0
    }
627
628
    /* Handle subject alternative names and various other extensions */
629
0
    tmp_altname = X509_get_ext_d2i(const_x, NID_subject_alt_name, &i, NULL);
630
0
    if (tmp_altname == NULL && i != -1)
631
0
        tmp_ex_flags |= EXFLAG_INVALID;
632
0
    tmp_nc = X509_get_ext_d2i(const_x, NID_name_constraints, &i, NULL);
633
0
    if (tmp_nc == NULL && i != -1)
634
0
        tmp_ex_flags |= EXFLAG_INVALID;
635
636
    /* Handle CRL distribution point entries */
637
0
    res = setup_crldp(const_x, &tmp_crldp);
638
0
    if (res == 0)
639
0
        tmp_ex_flags |= EXFLAG_INVALID;
640
641
0
#ifndef OPENSSL_NO_RFC3779
642
0
    STACK_OF(IPAddressFamily) *tmp_rfc3779_addr
643
0
        = X509_get_ext_d2i(const_x, NID_sbgp_ipAddrBlock, &i, NULL);
644
0
    if (tmp_rfc3779_addr == NULL && i != -1)
645
0
        tmp_ex_flags |= EXFLAG_INVALID;
646
647
0
    struct ASIdentifiers_st *tmp_rfc3779_asid
648
0
        = X509_get_ext_d2i(const_x, NID_sbgp_autonomousSysNum, &i, NULL);
649
0
    if (tmp_rfc3779_asid == NULL && i != -1)
650
0
        tmp_ex_flags |= EXFLAG_INVALID;
651
0
#endif
652
653
0
    for (i = 0; i < X509_get_ext_count(const_x); i++) {
654
0
        const X509_EXTENSION *ex = X509_get_ext(const_x, i);
655
0
        int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
656
657
0
        if (nid == NID_freshest_crl)
658
0
            tmp_ex_flags |= EXFLAG_FRESHEST;
659
0
        if (!X509_EXTENSION_get_critical(ex))
660
0
            continue;
661
0
        if (!X509_supported_extension(ex)) {
662
0
            tmp_ex_flags |= EXFLAG_CRITICAL;
663
0
            break;
664
0
        }
665
0
    }
666
667
    /* Set x->siginf, ignoring errors due to unsupported algos */
668
0
    (void)ossl_x509_init_sig_info(const_x, &tmp_siginf);
669
670
0
    tmp_ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */
671
0
    ERR_pop_to_mark();
672
673
0
    CRYPTO_THREAD_unlock(const_x->lock);
674
    /*
675
     * Now that we've done all the compute intensive work under read lock
676
     * do all the updating under a write lock
677
     */
678
0
    if (!CRYPTO_THREAD_write_lock(const_x->lock))
679
0
        return 0;
680
0
    ((X509 *)const_x)->ex_flags = tmp_ex_flags;
681
0
    ((X509 *)const_x)->ex_pathlen = tmp_ex_pathlen;
682
0
    ((X509 *)const_x)->ex_pcpathlen = tmp_ex_pcpathlen;
683
0
    if (!(tmp_ex_flags & EXFLAG_NO_FINGERPRINT))
684
0
        memcpy(((X509 *)const_x)->sha1_hash, tmp_sha1_hash, SHA_DIGEST_LENGTH);
685
0
    if (tmp_ex_flags & EXFLAG_KUSAGE)
686
0
        ((X509 *)const_x)->ex_kusage = tmp_ex_kusage;
687
0
    ((X509 *)const_x)->ex_xkusage = tmp_ex_xkusage;
688
0
    if (tmp_ex_flags & EXFLAG_NSCERT)
689
0
        ((X509 *)const_x)->ex_nscert = tmp_ex_nscert;
690
0
    ASN1_OCTET_STRING_free(((X509 *)const_x)->skid);
691
0
    ((X509 *)const_x)->skid = tmp_skid;
692
0
    AUTHORITY_KEYID_free(((X509 *)const_x)->akid);
693
0
    ((X509 *)const_x)->akid = tmp_akid;
694
0
    sk_GENERAL_NAME_pop_free(((X509 *)const_x)->altname, GENERAL_NAME_free);
695
0
    ((X509 *)const_x)->altname = tmp_altname;
696
0
    NAME_CONSTRAINTS_free(((X509 *)const_x)->nc);
697
0
    ((X509 *)const_x)->nc = tmp_nc;
698
0
    sk_DIST_POINT_pop_free(((X509 *)const_x)->crldp, DIST_POINT_free);
699
0
    ((X509 *)const_x)->crldp = tmp_crldp;
700
0
#ifndef OPENSSL_NO_RFC3779
701
0
    sk_IPAddressFamily_pop_free(((X509 *)const_x)->rfc3779_addr, IPAddressFamily_free);
702
0
    ((X509 *)const_x)->rfc3779_addr = tmp_rfc3779_addr;
703
0
    ASIdentifiers_free(((X509 *)const_x)->rfc3779_asid);
704
0
    ((X509 *)const_x)->rfc3779_asid = tmp_rfc3779_asid;
705
0
#endif
706
0
    ((X509 *)const_x)->siginf = tmp_siginf;
707
708
0
#ifdef tsan_st_rel
709
0
    tsan_st_rel((TSAN_QUALIFIER int *)&const_x->ex_cached, 1);
710
    /*
711
     * Above store triggers fast lock-free check in the beginning of the
712
     * function. But one has to ensure that the structure is "stable", i.e.
713
     * all stores are visible on all processors. Hence the release fence.
714
     */
715
0
#endif
716
0
    CRYPTO_THREAD_unlock(const_x->lock);
717
0
    if (tmp_ex_flags & EXFLAG_INVALID) {
718
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE);
719
0
        return 0;
720
0
    }
721
0
    return 1;
722
0
}
723
724
/*-
725
 * CA checks common to all purposes
726
 * return codes:
727
 * 0 not a CA
728
 * 1 is a CA
729
 * 2 Only possible in older versions of openSSL when basicConstraints are absent
730
 *   new versions will not return this value. May be a CA
731
 * 3 basicConstraints absent but self-signed V1.
732
 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
733
 * 5 Netscape specific CA Flags present
734
 */
735
736
static int check_ca(const X509 *x)
737
0
{
738
    /* keyUsage if present should allow cert signing */
739
0
    if (ku_reject(x, KU_KEY_CERT_SIGN))
740
0
        return 0;
741
0
    if ((x->ex_flags & EXFLAG_BCONS) != 0) {
742
        /* If basicConstraints says not a CA then say so */
743
0
        return (x->ex_flags & EXFLAG_CA) != 0;
744
0
    } else {
745
        /* We support V1 roots for...  uh, I don't really know why. */
746
0
        if ((x->ex_flags & V1_ROOT) == V1_ROOT)
747
0
            return 3;
748
        /*
749
         * If key usage present it must have certSign so tolerate it
750
         */
751
0
        else if ((x->ex_flags & EXFLAG_KUSAGE) != 0)
752
0
            return 4;
753
        /* Older certificates could have Netscape-specific CA types */
754
0
        else if ((x->ex_flags & EXFLAG_NSCERT) != 0
755
0
            && (x->ex_nscert & NS_ANY_CA) != 0)
756
0
            return 5;
757
        /* Can this still be regarded a CA certificate?  I doubt it. */
758
0
        return 0;
759
0
    }
760
0
}
761
762
void X509_set_proxy_flag(X509 *x)
763
0
{
764
0
    if (CRYPTO_THREAD_write_lock(x->lock)) {
765
0
        x->ex_flags |= EXFLAG_PROXY;
766
0
        CRYPTO_THREAD_unlock(x->lock);
767
0
    }
768
0
}
769
770
void X509_set_proxy_pathlen(X509 *x, long l)
771
0
{
772
0
    x->ex_pcpathlen = l;
773
0
}
774
775
int X509_check_ca(const X509 *x)
776
0
{
777
    /* Note 0 normally means "not a CA" - but in this case means error. */
778
0
    if (!ossl_x509v3_cache_extensions(x))
779
0
        return 0;
780
781
0
    return check_ca(x);
782
0
}
783
784
/* Check SSL CA: common checks for SSL client and server. */
785
static int check_ssl_ca(const X509 *x)
786
0
{
787
0
    int ca_ret = check_ca(x);
788
789
0
    if (ca_ret == 0)
790
0
        return 0;
791
    /* Check nsCertType if present */
792
0
    return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0;
793
0
}
794
795
static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
796
    int non_leaf)
797
0
{
798
0
    if (xku_reject(x, XKU_SSL_CLIENT))
799
0
        return 0;
800
0
    if (non_leaf)
801
0
        return check_ssl_ca(x);
802
    /* We need to do digital signatures or key agreement */
803
0
    if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
804
0
        return 0;
805
    /* nsCertType if present should allow SSL client use */
806
0
    if (ns_reject(x, NS_SSL_CLIENT))
807
0
        return 0;
808
0
    return 1;
809
0
}
810
811
/*
812
 * Key usage needed for TLS/SSL server: digital signature, encipherment or
813
 * key agreement. The ssl code can check this more thoroughly for individual
814
 * key types.
815
 */
816
#define KU_TLS \
817
    KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT
818
819
static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
820
    int non_leaf)
821
0
{
822
0
    if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
823
0
        return 0;
824
0
    if (non_leaf)
825
0
        return check_ssl_ca(x);
826
827
0
    if (ns_reject(x, NS_SSL_SERVER))
828
0
        return 0;
829
0
    if (ku_reject(x, KU_TLS))
830
0
        return 0;
831
832
0
    return 1;
833
0
}
834
835
static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
836
    int non_leaf)
837
0
{
838
0
    int ret = check_purpose_ssl_server(xp, x, non_leaf);
839
840
0
    if (!ret || non_leaf)
841
0
        return ret;
842
    /* We need to encipher or Netscape complains */
843
0
    return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;
844
0
}
845
846
/* common S/MIME client checks */
847
static int purpose_smime(const X509 *x, int non_leaf)
848
0
{
849
0
    if (xku_reject(x, XKU_SMIME))
850
0
        return 0;
851
0
    if (non_leaf) {
852
0
        int ca_ret = check_ca(x);
853
854
0
        if (ca_ret == 0)
855
0
            return 0;
856
        /* Check nsCertType if present */
857
0
        if (ca_ret != 5 || (x->ex_nscert & NS_SMIME_CA) != 0)
858
0
            return ca_ret;
859
0
        else
860
0
            return 0;
861
0
    }
862
0
    if ((x->ex_flags & EXFLAG_NSCERT) != 0) {
863
0
        if ((x->ex_nscert & NS_SMIME) != 0)
864
0
            return 1;
865
        /* Workaround for some buggy certificates */
866
0
        return (x->ex_nscert & NS_SSL_CLIENT) != 0 ? 2 : 0;
867
0
    }
868
0
    return 1;
869
0
}
870
871
static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
872
    int non_leaf)
873
0
{
874
0
    int ret = purpose_smime(x, non_leaf);
875
876
0
    if (!ret || non_leaf)
877
0
        return ret;
878
0
    return ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION) ? 0 : ret;
879
0
}
880
881
static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
882
    int non_leaf)
883
0
{
884
0
    int ret = purpose_smime(x, non_leaf);
885
886
0
    if (!ret || non_leaf)
887
0
        return ret;
888
0
    return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;
889
0
}
890
891
static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
892
    int non_leaf)
893
0
{
894
0
    if (non_leaf) {
895
0
        int ca_ret = check_ca(x);
896
897
0
        return ca_ret == 2 ? 0 : ca_ret;
898
0
    }
899
0
    return !ku_reject(x, KU_CRL_SIGN);
900
0
}
901
902
/*
903
 * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
904
 * is valid. Additional checks must be made on the chain.
905
 */
906
static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x,
907
    int non_leaf)
908
0
{
909
    /*
910
     * Must be a valid CA.  Should we really support the "I don't know" value
911
     * (2)?
912
     */
913
0
    if (non_leaf)
914
0
        return check_ca(x);
915
    /* Leaf certificate is checked in OCSP_basic_verify() */
916
0
    return 1;
917
0
}
918
919
static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
920
    int non_leaf)
921
0
{
922
0
    int i_ext;
923
924
    /*
925
     * If non_leaf is true we must check if this is a valid CA certificate.
926
     * The extra requirements by the CA/Browser Forum are not checked.
927
     */
928
0
    if (non_leaf)
929
0
        return check_ca(x);
930
931
    /*
932
     * Key Usage is checked according to RFC 5280 and
933
     * Extended Key Usage attributes is checked according to RFC 3161.
934
     * The extra (and somewhat conflicting) CA/Browser Forum
935
     * Baseline Requirements for the Issuance and Management of
936
     * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,
937
     * Section 7.1.2.3: Code signing and Timestamp Certificate are not checked.
938
     */
939
    /*
940
     * Check the optional key usage field:
941
     * if Key Usage is present, it must be one of digitalSignature
942
     * and/or nonRepudiation (other values are not consistent and shall
943
     * be rejected).
944
     */
945
0
    if ((x->ex_flags & EXFLAG_KUSAGE) != 0
946
0
        && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
947
0
        return 0;
948
949
    /* Only timestamp key usage is permitted and it's required. */
950
0
    if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP)
951
0
        return 0;
952
953
    /* Extended Key Usage MUST be critical */
954
0
    i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
955
0
    if (i_ext >= 0
956
0
        && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext)))
957
0
        return 0;
958
0
    return 1;
959
0
}
960
961
static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x,
962
    int non_leaf)
963
0
{
964
0
    int i_ext;
965
966
    /*
967
     * If non_leaf is true we must check if this is a valid CA certificate.
968
     * The extra requirements by the CA/Browser Forum are not checked.
969
     */
970
0
    if (non_leaf)
971
0
        return check_ca(x);
972
973
    /*
974
     * Check the key usage and extended key usage fields:
975
     *
976
     * Reference: CA/Browser Forum,
977
     * Baseline Requirements for the Issuance and Management of
978
     * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,
979
     * Section 7.1.2.3: Code signing and Timestamp Certificate
980
     *
981
     * Checking covers Key Usage and Extended Key Usage attributes.
982
     * The certificatePolicies, cRLDistributionPoints (CDP), and
983
     * authorityInformationAccess (AIA) extensions are so far not checked.
984
     */
985
    /* Key Usage */
986
0
    if ((x->ex_flags & EXFLAG_KUSAGE) == 0)
987
0
        return 0;
988
0
    if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0)
989
0
        return 0;
990
0
    if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0)
991
0
        return 0;
992
993
    /* Key Usage MUST be critical */
994
0
    i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1);
995
0
    if (i_ext < 0)
996
0
        return 0;
997
0
    if (i_ext >= 0) {
998
0
        const X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
999
0
        if (!X509_EXTENSION_get_critical(ext))
1000
0
            return 0;
1001
0
    }
1002
1003
    /* Extended Key Usage */
1004
0
    if ((x->ex_flags & EXFLAG_XKUSAGE) == 0)
1005
0
        return 0;
1006
0
    if ((x->ex_xkusage & XKU_CODE_SIGN) == 0)
1007
0
        return 0;
1008
0
    if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0)
1009
0
        return 0;
1010
1011
0
    return 1;
1012
0
}
1013
1014
static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x,
1015
    int non_leaf)
1016
0
{
1017
0
    return 1;
1018
0
}
1019
1020
/*-
1021
 * Various checks to see if one certificate potentially issued the second.
1022
 * This can be used to prune a set of possible issuer certificates which
1023
 * have been looked up using some simple method such as by subject name.
1024
 * These are:
1025
 * 1. issuer_name(subject) == subject_name(issuer)
1026
 * 2. If akid(subject) exists, it matches the respective issuer fields.
1027
 * 3. subject signature algorithm == issuer public key algorithm
1028
 * 4. If key_usage(issuer) exists, it allows for signing subject.
1029
 * Note that this does not include actually checking the signature.
1030
 * Returns 0 for OK, or positive for reason for mismatch
1031
 * where reason codes match those for X509_verify_cert().
1032
 */
1033
int X509_check_issued(const X509 *issuer, const X509 *subject)
1034
0
{
1035
0
    int ret;
1036
1037
0
    if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK)
1038
0
        return ret;
1039
0
    return ossl_x509_signing_allowed(issuer, subject);
1040
0
}
1041
1042
/* do the checks 1., 2., and 3. as described above for X509_check_issued() */
1043
int ossl_x509_likely_issued(const X509 *issuer, const X509 *subject)
1044
0
{
1045
0
    int ret;
1046
1047
0
    if (X509_NAME_cmp(X509_get_subject_name(issuer),
1048
0
            X509_get_issuer_name(subject))
1049
0
        != 0)
1050
0
        return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
1051
1052
    /* set issuer->skid and subject->akid */
1053
0
    if (!ossl_x509v3_cache_extensions(issuer)
1054
0
        || !ossl_x509v3_cache_extensions(subject))
1055
0
        return X509_V_ERR_UNSPECIFIED;
1056
1057
0
    ret = X509_check_akid(issuer, subject->akid);
1058
0
    if (ret != X509_V_OK)
1059
0
        return ret;
1060
1061
    /* Check if the subject signature alg matches the issuer's PUBKEY alg */
1062
0
    return check_sig_alg_match(X509_get0_pubkey(issuer), subject);
1063
0
}
1064
1065
/*-
1066
 * Check if certificate I<issuer> is allowed to issue certificate I<subject>
1067
 * according to the B<keyUsage> field of I<issuer> if present
1068
 * depending on any proxyCertInfo extension of I<subject>.
1069
 * Returns 0 for OK, or positive for reason for rejection
1070
 * where reason codes match those for X509_verify_cert().
1071
 */
1072
int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject)
1073
0
{
1074
0
    if ((subject->ex_flags & EXFLAG_PROXY) != 0) {
1075
0
        if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
1076
0
            return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
1077
0
    } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) {
1078
0
        return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
1079
0
    }
1080
0
    return X509_V_OK;
1081
0
}
1082
1083
int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid)
1084
0
{
1085
0
    if (akid == NULL)
1086
0
        return X509_V_OK;
1087
1088
    /* Check key ids (if present) */
1089
0
    if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
1090
0
        return X509_V_ERR_AKID_SKID_MISMATCH;
1091
    /* Check serial number */
1092
0
    if (akid->serial && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial))
1093
0
        return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1094
    /* Check issuer name */
1095
0
    if (akid->issuer) {
1096
        /*
1097
         * Ugh, for some peculiar reason AKID includes SEQUENCE OF
1098
         * GeneralName. So look for a DirName. There may be more than one but
1099
         * we only take any notice of the first.
1100
         */
1101
0
        GENERAL_NAMES *gens = akid->issuer;
1102
0
        GENERAL_NAME *gen;
1103
0
        X509_NAME *nm = NULL;
1104
0
        int i;
1105
1106
0
        for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1107
0
            gen = sk_GENERAL_NAME_value(gens, i);
1108
0
            if (gen->type == GEN_DIRNAME) {
1109
0
                nm = gen->d.dirn;
1110
0
                break;
1111
0
            }
1112
0
        }
1113
0
        if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0)
1114
0
            return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1115
0
    }
1116
0
    return X509_V_OK;
1117
0
}
1118
1119
uint32_t X509_get_extension_flags(const X509 *x)
1120
0
{
1121
    /* Call for side-effect of computing hash and caching extensions */
1122
0
    X509_check_purpose(x, -1, 0);
1123
0
    return x->ex_flags;
1124
0
}
1125
1126
uint32_t X509_get_key_usage(const X509 *x)
1127
0
{
1128
    /* Call for side-effect of computing hash and caching extensions */
1129
0
    if (X509_check_purpose(x, -1, 0) != 1)
1130
0
        return 0;
1131
0
    return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX;
1132
0
}
1133
1134
uint32_t X509_get_extended_key_usage(const X509 *x)
1135
0
{
1136
    /* Call for side-effect of computing hash and caching extensions */
1137
0
    if (X509_check_purpose(x, -1, 0) != 1)
1138
0
        return 0;
1139
0
    return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX;
1140
0
}
1141
1142
const ASN1_OCTET_STRING *X509_get0_subject_key_id(const X509 *x)
1143
0
{
1144
    /* Call for side-effect of computing hash and caching extensions */
1145
0
    if (X509_check_purpose(x, -1, 0) != 1)
1146
0
        return NULL;
1147
0
    return x->skid;
1148
0
}
1149
1150
const ASN1_OCTET_STRING *X509_get0_authority_key_id(const X509 *x)
1151
0
{
1152
    /* Call for side-effect of computing hash and caching extensions */
1153
0
    if (X509_check_purpose(x, -1, 0) != 1)
1154
0
        return NULL;
1155
0
    return (x->akid != NULL ? x->akid->keyid : NULL);
1156
0
}
1157
1158
const GENERAL_NAMES *X509_get0_authority_issuer(const X509 *x)
1159
0
{
1160
    /* Call for side-effect of computing hash and caching extensions */
1161
0
    if (X509_check_purpose(x, -1, 0) != 1)
1162
0
        return NULL;
1163
0
    return (x->akid != NULL ? x->akid->issuer : NULL);
1164
0
}
1165
1166
const ASN1_INTEGER *X509_get0_authority_serial(const X509 *x)
1167
0
{
1168
    /* Call for side-effect of computing hash and caching extensions */
1169
0
    if (X509_check_purpose(x, -1, 0) != 1)
1170
0
        return NULL;
1171
0
    return (x->akid != NULL ? x->akid->serial : NULL);
1172
0
}
1173
1174
long X509_get_pathlen(const X509 *x)
1175
0
{
1176
    /* Called for side effect of caching extensions */
1177
0
    if (X509_check_purpose(x, -1, 0) != 1
1178
0
        || (x->ex_flags & EXFLAG_BCONS) == 0)
1179
0
        return -1;
1180
0
    return x->ex_pathlen;
1181
0
}
1182
1183
long X509_get_proxy_pathlen(const X509 *x)
1184
0
{
1185
    /* Called for side effect of caching extensions */
1186
0
    if (X509_check_purpose(x, -1, 0) != 1
1187
0
        || (x->ex_flags & EXFLAG_PROXY) == 0)
1188
0
        return -1;
1189
0
    return x->ex_pcpathlen;
1190
0
}