Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/x509/v3_purp.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2023 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
static X509_PURPOSE xstandard[] = {
46
    { X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
47
        check_purpose_ssl_client, "SSL client", "sslclient", NULL },
48
    { X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
49
        check_purpose_ssl_server, "SSL server", "sslserver", NULL },
50
    { X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
51
        check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL },
52
    { X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
53
        "S/MIME signing", "smimesign", NULL },
54
    { X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
55
        check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL },
56
    { X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
57
        "CRL signing", "crlsign", NULL },
58
    { X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check_purpose,
59
        "Any Purpose", "any",
60
        NULL },
61
    { X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, check_purpose_ocsp_helper,
62
        "OCSP helper", "ocsphelper", NULL },
63
    { X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
64
        check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign",
65
        NULL },
66
    { X509_PURPOSE_CODE_SIGN, X509_TRUST_OBJECT_SIGN, 0,
67
        check_purpose_code_sign, "Code signing", "codesign",
68
        NULL },
69
};
70
71
830
#define X509_PURPOSE_COUNT OSSL_NELEM(xstandard)
72
73
static STACK_OF(X509_PURPOSE) *xptable = NULL;
74
75
static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b)
76
0
{
77
0
    return (*a)->purpose - (*b)->purpose;
78
0
}
79
80
/*
81
 * As much as I'd like to make X509_check_purpose use a "const" X509* I really
82
 * can't because it does recalculate hashes and do other non-const things.
83
 * If id == -1 it just calls x509v3_cache_extensions() for its side-effect.
84
 * Returns 1 on success, 0 if x does not allow purpose, -1 on (internal) error.
85
 */
86
int X509_check_purpose(X509 *x, int id, int non_leaf)
87
332k
{
88
332k
    int idx;
89
332k
    const X509_PURPOSE *pt;
90
91
332k
    if (!ossl_x509v3_cache_extensions(x))
92
42.7k
        return -1;
93
289k
    if (id == -1)
94
289k
        return 1;
95
96
508
    idx = X509_PURPOSE_get_by_id(id);
97
508
    if (idx == -1)
98
0
        return -1;
99
508
    pt = X509_PURPOSE_get0(idx);
100
508
    return pt->check_purpose(pt, x, non_leaf);
101
508
}
102
103
int X509_PURPOSE_set(int *p, int purpose)
104
0
{
105
0
    if (X509_PURPOSE_get_by_id(purpose) == -1) {
106
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PURPOSE);
107
0
        return 0;
108
0
    }
109
0
    *p = purpose;
110
0
    return 1;
111
0
}
112
113
int X509_PURPOSE_get_count(void)
114
0
{
115
0
    if (!xptable)
116
0
        return X509_PURPOSE_COUNT;
117
0
    return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
118
0
}
119
120
X509_PURPOSE *X509_PURPOSE_get0(int idx)
121
68.9k
{
122
68.9k
    if (idx < 0)
123
68.0k
        return NULL;
124
830
    if (idx < (int)X509_PURPOSE_COUNT)
125
830
        return xstandard + idx;
126
0
    return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
127
830
}
128
129
int X509_PURPOSE_get_by_sname(const char *sname)
130
0
{
131
0
    int i;
132
0
    X509_PURPOSE *xptmp;
133
134
0
    for (i = 0; i < X509_PURPOSE_get_count(); i++) {
135
0
        xptmp = X509_PURPOSE_get0(i);
136
0
        if (strcmp(xptmp->sname, sname) == 0)
137
0
            return i;
138
0
    }
139
0
    return -1;
140
0
}
141
142
/* Returns -1 on error, else an index => 0 in standard/extended purpose table */
143
int X509_PURPOSE_get_by_id(int purpose)
144
68.9k
{
145
68.9k
    X509_PURPOSE tmp;
146
68.9k
    int idx;
147
148
68.9k
    if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX)
149
830
        return purpose - X509_PURPOSE_MIN;
150
68.0k
    if (xptable == NULL)
151
68.0k
        return -1;
152
0
    tmp.purpose = purpose;
153
0
    idx = sk_X509_PURPOSE_find(xptable, &tmp);
154
0
    if (idx < 0)
155
0
        return -1;
156
0
    return idx + X509_PURPOSE_COUNT;
157
0
}
158
159
int X509_PURPOSE_add(int id, int trust, int flags,
160
    int (*ck)(const X509_PURPOSE *, const X509 *, int),
161
    const char *name, const char *sname, void *arg)
162
0
{
163
0
    int idx;
164
0
    X509_PURPOSE *ptmp;
165
166
    /* This is set according to what we change: application can't set it */
167
0
    flags &= ~X509_PURPOSE_DYNAMIC;
168
    /* This will always be set for application modified trust entries */
169
0
    flags |= X509_PURPOSE_DYNAMIC_NAME;
170
    /* Get existing entry if any */
171
0
    idx = X509_PURPOSE_get_by_id(id);
172
    /* Need a new entry */
173
0
    if (idx == -1) {
174
0
        if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL)
175
0
            return 0;
176
0
        ptmp->flags = X509_PURPOSE_DYNAMIC;
177
0
    } else {
178
0
        ptmp = X509_PURPOSE_get0(idx);
179
0
    }
180
181
    /* OPENSSL_free existing name if dynamic */
182
0
    if ((ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) {
183
0
        OPENSSL_free(ptmp->name);
184
0
        OPENSSL_free(ptmp->sname);
185
0
    }
186
    /* Dup supplied name */
187
0
    ptmp->name = OPENSSL_strdup(name);
188
0
    ptmp->sname = OPENSSL_strdup(sname);
189
0
    if (ptmp->name == NULL || ptmp->sname == NULL)
190
0
        goto err;
191
    /* Keep the dynamic flag of existing entry */
192
0
    ptmp->flags &= X509_PURPOSE_DYNAMIC;
193
    /* Set all other flags */
194
0
    ptmp->flags |= flags;
195
196
0
    ptmp->purpose = id;
197
0
    ptmp->trust = trust;
198
0
    ptmp->check_purpose = ck;
199
0
    ptmp->usr_data = arg;
200
201
    /* If its a new entry manage the dynamic table */
202
0
    if (idx == -1) {
203
0
        if (xptable == NULL
204
0
            && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) {
205
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
206
0
            goto err;
207
0
        }
208
0
        if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
209
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
210
0
            goto err;
211
0
        }
212
0
    }
213
0
    return 1;
214
0
err:
215
0
    if (idx == -1) {
216
0
        OPENSSL_free(ptmp->name);
217
0
        OPENSSL_free(ptmp->sname);
218
0
        OPENSSL_free(ptmp);
219
0
    }
220
0
    return 0;
221
0
}
222
223
static void xptable_free(X509_PURPOSE *p)
224
0
{
225
0
    if (p == NULL)
226
0
        return;
227
0
    if ((p->flags & X509_PURPOSE_DYNAMIC) != 0) {
228
0
        if ((p->flags & X509_PURPOSE_DYNAMIC_NAME) != 0) {
229
0
            OPENSSL_free(p->name);
230
0
            OPENSSL_free(p->sname);
231
0
        }
232
0
        OPENSSL_free(p);
233
0
    }
234
0
}
235
236
void X509_PURPOSE_cleanup(void)
237
0
{
238
0
    sk_X509_PURPOSE_pop_free(xptable, xptable_free);
239
0
    xptable = NULL;
240
0
}
241
242
int X509_PURPOSE_get_id(const X509_PURPOSE *xp)
243
0
{
244
0
    return xp->purpose;
245
0
}
246
247
char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp)
248
0
{
249
0
    return xp->name;
250
0
}
251
252
char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp)
253
0
{
254
0
    return xp->sname;
255
0
}
256
257
int X509_PURPOSE_get_trust(const X509_PURPOSE *xp)
258
0
{
259
0
    return xp->trust;
260
0
}
261
262
static int nid_cmp(const int *a, const int *b)
263
232k
{
264
232k
    return *a - *b;
265
232k
}
266
267
DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid);
268
IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid);
269
270
int X509_supported_extension(X509_EXTENSION *ex)
271
100k
{
272
    /*
273
     * This table is a list of the NIDs of supported extensions: that is
274
     * those which are used by the verify process. If an extension is
275
     * critical and doesn't appear in this list then the verify process will
276
     * normally reject the certificate. The list must be kept in numerical
277
     * order because it will be searched using bsearch.
278
     */
279
100k
    static const int supported_nids[] = {
280
100k
        NID_netscape_cert_type, /* 71 */
281
100k
        NID_key_usage, /* 83 */
282
100k
        NID_subject_alt_name, /* 85 */
283
100k
        NID_basic_constraints, /* 87 */
284
100k
        NID_certificate_policies, /* 89 */
285
100k
        NID_crl_distribution_points, /* 103 */
286
100k
        NID_ext_key_usage, /* 126 */
287
100k
#ifndef OPENSSL_NO_RFC3779
288
100k
        NID_sbgp_ipAddrBlock, /* 290 */
289
100k
        NID_sbgp_autonomousSysNum, /* 291 */
290
100k
#endif
291
100k
        NID_id_pkix_OCSP_noCheck, /* 369 */
292
100k
        NID_policy_constraints, /* 401 */
293
100k
        NID_proxyCertInfo, /* 663 */
294
100k
        NID_name_constraints, /* 666 */
295
100k
        NID_policy_mappings, /* 747 */
296
100k
        NID_inhibit_any_policy /* 748 */
297
100k
    };
298
299
100k
    int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
300
301
100k
    if (ex_nid == NID_undef)
302
6.22k
        return 0;
303
304
94.3k
    if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
305
93.0k
        return 1;
306
1.35k
    return 0;
307
94.3k
}
308
309
/* Returns 1 on success, 0 if x is invalid, -1 on (internal) error. */
310
static int setup_dp(const X509 *x, DIST_POINT *dp)
311
1.90k
{
312
1.90k
    const X509_NAME *iname = NULL;
313
1.90k
    int i;
314
315
1.90k
    if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) {
316
57
        ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT);
317
57
        return 0;
318
57
    }
319
1.85k
    if (dp->reasons != NULL) {
320
34
        if (dp->reasons->length > 0)
321
34
            dp->dp_reasons = dp->reasons->data[0];
322
34
        if (dp->reasons->length > 1)
323
10
            dp->dp_reasons |= (dp->reasons->data[1] << 8);
324
34
        dp->dp_reasons &= CRLDP_ALL_REASONS;
325
1.81k
    } else {
326
1.81k
        dp->dp_reasons = CRLDP_ALL_REASONS;
327
1.81k
    }
328
1.85k
    if (dp->distpoint == NULL || dp->distpoint->type != 1)
329
1.81k
        return 1;
330
331
    /* Handle name fragment given by nameRelativeToCRLIssuer */
332
    /*
333
     * Note that the below way of determining iname is not really compliant
334
     * with https://tools.ietf.org/html/rfc5280#section-4.2.1.13
335
     * According to it, sk_GENERAL_NAME_num(dp->CRLissuer) MUST be <= 1
336
     * and any CRLissuer could be of type different to GEN_DIRNAME.
337
     */
338
61
    for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
339
28
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
340
341
28
        if (gen->type == GEN_DIRNAME) {
342
2
            iname = gen->d.directoryName;
343
2
            break;
344
2
        }
345
28
    }
346
35
    if (iname == NULL)
347
33
        iname = X509_get_issuer_name(x);
348
35
    return DIST_POINT_set_dpname(dp->distpoint, iname) ? 1 : -1;
349
1.85k
}
350
351
/* Return 1 on success, 0 if x is invalid, -1 on (internal) error. */
352
static int setup_crldp(X509 *x)
353
210k
{
354
210k
    int i;
355
356
210k
    x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
357
210k
    if (x->crldp == NULL && i != -1)
358
1.99k
        return 0;
359
360
210k
    for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
361
1.90k
        int res = setup_dp(x, sk_DIST_POINT_value(x->crldp, i));
362
363
1.90k
        if (res < 1)
364
71
            return res;
365
1.90k
    }
366
208k
    return 1;
367
208k
}
368
369
/* Check that issuer public key algorithm matches subject signature algorithm */
370
static int check_sig_alg_match(const EVP_PKEY *issuer_key, const X509 *subject)
371
183k
{
372
183k
    int subj_sig_nid;
373
374
183k
    if (issuer_key == NULL)
375
11.5k
        return X509_V_ERR_NO_ISSUER_PUBLIC_KEY;
376
171k
    if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm),
377
171k
            NULL, &subj_sig_nid)
378
171k
        == 0)
379
8.75k
        return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM;
380
162k
    if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid))
381
2.30k
        || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss))
382
162k
        return X509_V_OK;
383
660
    return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH;
384
162k
}
385
386
10.4k
#define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)
387
#define ku_reject(x, usage) \
388
12.9k
    (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0)
389
#define xku_reject(x, usage) \
390
0
    (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0)
391
#define ns_reject(x, usage) \
392
0
    (((x)->ex_flags & EXFLAG_NSCERT) != 0 && ((x)->ex_nscert & (usage)) == 0)
393
394
/*
395
 * Cache info on various X.509v3 extensions and further derived information,
396
 * e.g., if cert 'x' is self-issued, in x->ex_flags and other internal fields.
397
 * x->sha1_hash is filled in, or else EXFLAG_NO_FINGERPRINT is set in x->flags.
398
 * X509_SIG_INFO_VALID is set in x->flags if x->siginf was filled successfully.
399
 * Set EXFLAG_INVALID and return 0 in case the certificate is invalid.
400
 */
401
int ossl_x509v3_cache_extensions(X509 *x)
402
438k
{
403
438k
    BASIC_CONSTRAINTS *bs;
404
438k
    PROXY_CERT_INFO_EXTENSION *pci;
405
438k
    ASN1_BIT_STRING *usage;
406
438k
    ASN1_BIT_STRING *ns;
407
438k
    EXTENDED_KEY_USAGE *extusage;
408
438k
    int i;
409
438k
    int res;
410
411
438k
#ifdef tsan_ld_acq
412
    /* Fast lock-free check, see end of the function for details. */
413
438k
    if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
414
247k
        return (x->ex_flags & EXFLAG_INVALID) == 0;
415
190k
#endif
416
417
190k
    if (!CRYPTO_THREAD_write_lock(x->lock))
418
0
        return 0;
419
190k
    if ((x->ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */
420
0
        CRYPTO_THREAD_unlock(x->lock);
421
0
        return (x->ex_flags & EXFLAG_INVALID) == 0;
422
0
    }
423
424
190k
    ERR_set_mark();
425
426
    /* Cache the SHA1 digest of the cert */
427
190k
    if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL))
428
0
        x->ex_flags |= EXFLAG_NO_FINGERPRINT;
429
430
    /* V1 should mean no extensions ... */
431
190k
    if (X509_get_version(x) == X509_VERSION_1)
432
2.78k
        x->ex_flags |= EXFLAG_V1;
433
434
    /* Handle basic constraints */
435
190k
    x->ex_pathlen = -1;
436
190k
    if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL)) != NULL) {
437
160k
        if (bs->ca)
438
27.6k
            x->ex_flags |= EXFLAG_CA;
439
160k
        if (bs->pathlen != NULL) {
440
            /*
441
             * The error case !bs->ca is checked by check_chain()
442
             * in case ctx->param->flags & X509_V_FLAG_X509_STRICT
443
             */
444
2.89k
            if (bs->pathlen->type == V_ASN1_NEG_INTEGER) {
445
117
                ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN);
446
117
                x->ex_flags |= EXFLAG_INVALID;
447
2.78k
            } else {
448
2.78k
                x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
449
2.78k
            }
450
2.89k
        }
451
160k
        BASIC_CONSTRAINTS_free(bs);
452
160k
        x->ex_flags |= EXFLAG_BCONS;
453
160k
    } else if (i != -1) {
454
5.98k
        x->ex_flags |= EXFLAG_INVALID;
455
5.98k
    }
456
457
    /* Handle proxy certificates */
458
190k
    if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL)) != NULL) {
459
419
        if ((x->ex_flags & EXFLAG_CA) != 0
460
409
            || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
461
304
            || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
462
181
            x->ex_flags |= EXFLAG_INVALID;
463
181
        }
464
419
        if (pci->pcPathLengthConstraint != NULL)
465
231
            x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
466
188
        else
467
188
            x->ex_pcpathlen = -1;
468
419
        PROXY_CERT_INFO_EXTENSION_free(pci);
469
419
        x->ex_flags |= EXFLAG_PROXY;
470
190k
    } else if (i != -1) {
471
246
        x->ex_flags |= EXFLAG_INVALID;
472
246
    }
473
474
    /* Handle (basic) key usage */
475
190k
    if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL)) != NULL) {
476
123k
        x->ex_kusage = 0;
477
123k
        if (usage->length > 0) {
478
122k
            x->ex_kusage = usage->data[0];
479
122k
            if (usage->length > 1)
480
47
                x->ex_kusage |= usage->data[1] << 8;
481
122k
        }
482
123k
        x->ex_flags |= EXFLAG_KUSAGE;
483
123k
        ASN1_BIT_STRING_free(usage);
484
        /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */
485
123k
        if (x->ex_kusage == 0) {
486
960
            ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE);
487
960
            x->ex_flags |= EXFLAG_INVALID;
488
960
        }
489
123k
    } else if (i != -1) {
490
1.58k
        x->ex_flags |= EXFLAG_INVALID;
491
1.58k
    }
492
493
    /* Handle extended key usage */
494
190k
    x->ex_xkusage = 0;
495
190k
    if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL)) != NULL) {
496
126k
        x->ex_flags |= EXFLAG_XKUSAGE;
497
252k
        for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
498
126k
            switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
499
123k
            case NID_server_auth:
500
123k
                x->ex_xkusage |= XKU_SSL_SERVER;
501
123k
                break;
502
190
            case NID_client_auth:
503
190
                x->ex_xkusage |= XKU_SSL_CLIENT;
504
190
                break;
505
114
            case NID_email_protect:
506
114
                x->ex_xkusage |= XKU_SMIME;
507
114
                break;
508
41
            case NID_code_sign:
509
41
                x->ex_xkusage |= XKU_CODE_SIGN;
510
41
                break;
511
0
            case NID_ms_sgc:
512
0
            case NID_ns_sgc:
513
0
                x->ex_xkusage |= XKU_SGC;
514
0
                break;
515
30
            case NID_OCSP_sign:
516
30
                x->ex_xkusage |= XKU_OCSP_SIGN;
517
30
                break;
518
68
            case NID_time_stamp:
519
68
                x->ex_xkusage |= XKU_TIMESTAMP;
520
68
                break;
521
63
            case NID_dvcs:
522
63
                x->ex_xkusage |= XKU_DVCS;
523
63
                break;
524
1
            case NID_anyExtendedKeyUsage:
525
1
                x->ex_xkusage |= XKU_ANYEKU;
526
1
                break;
527
2.40k
            default:
528
                /* Ignore unknown extended key usage */
529
2.40k
                break;
530
126k
            }
531
126k
        }
532
126k
        sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
533
126k
    } else if (i != -1) {
534
5.64k
        x->ex_flags |= EXFLAG_INVALID;
535
5.64k
    }
536
537
    /* Handle legacy Netscape extension */
538
190k
    if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL)) != NULL) {
539
19
        if (ns->length > 0)
540
12
            x->ex_nscert = ns->data[0];
541
7
        else
542
7
            x->ex_nscert = 0;
543
19
        x->ex_flags |= EXFLAG_NSCERT;
544
19
        ASN1_BIT_STRING_free(ns);
545
190k
    } else if (i != -1) {
546
20
        x->ex_flags |= EXFLAG_INVALID;
547
20
    }
548
549
    /* Handle subject key identifier and issuer/authority key identifier */
550
190k
    x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
551
190k
    if (x->skid == NULL && i != -1)
552
2.87k
        x->ex_flags |= EXFLAG_INVALID;
553
554
190k
    x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
555
190k
    if (x->akid == NULL && i != -1)
556
6.18k
        x->ex_flags |= EXFLAG_INVALID;
557
558
    /* Check if subject name matches issuer */
559
190k
    if (X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)) == 0) {
560
148k
        x->ex_flags |= EXFLAG_SI; /* Cert is self-issued */
561
148k
        if (X509_check_akid(x, x->akid) == X509_V_OK /* SKID matches AKID */
562
146k
            /* .. and the signature alg matches the PUBKEY alg: */
563
146k
            && check_sig_alg_match(X509_get0_pubkey(x), x) == X509_V_OK)
564
139k
            x->ex_flags |= EXFLAG_SS; /* indicate self-signed */
565
        /* This is very related to ossl_x509_likely_issued(x, x) == X509_V_OK */
566
148k
    }
567
568
    /* Handle subject alternative names and various other extensions */
569
190k
    x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
570
190k
    if (x->altname == NULL && i != -1)
571
3.13k
        x->ex_flags |= EXFLAG_INVALID;
572
190k
    x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
573
190k
    if (x->nc == NULL && i != -1)
574
868
        x->ex_flags |= EXFLAG_INVALID;
575
576
    /* Handle CRL distribution point entries */
577
190k
    res = setup_crldp(x);
578
190k
    if (res == 0)
579
1.81k
        x->ex_flags |= EXFLAG_INVALID;
580
581
190k
#ifndef OPENSSL_NO_RFC3779
582
190k
    x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
583
190k
    if (x->rfc3779_addr == NULL && i != -1)
584
234
        x->ex_flags |= EXFLAG_INVALID;
585
190k
    x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
586
190k
    if (x->rfc3779_asid == NULL && i != -1)
587
102
        x->ex_flags |= EXFLAG_INVALID;
588
190k
#endif
589
978k
    for (i = 0; i < X509_get_ext_count(x); i++) {
590
793k
        X509_EXTENSION *ex = X509_get_ext(x, i);
591
793k
        int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
592
593
793k
        if (nid == NID_freshest_crl)
594
16.3k
            x->ex_flags |= EXFLAG_FRESHEST;
595
793k
        if (!X509_EXTENSION_get_critical(ex))
596
705k
            continue;
597
88.1k
        if (!X509_supported_extension(ex)) {
598
6.39k
            x->ex_flags |= EXFLAG_CRITICAL;
599
6.39k
            break;
600
6.39k
        }
601
81.7k
        switch (nid) {
602
50.0k
        case NID_basic_constraints:
603
50.0k
            x->ex_flags |= EXFLAG_BCONS_CRITICAL;
604
50.0k
            break;
605
0
        case NID_authority_key_identifier:
606
0
            x->ex_flags |= EXFLAG_AKID_CRITICAL;
607
0
            break;
608
0
        case NID_subject_key_identifier:
609
0
            x->ex_flags |= EXFLAG_SKID_CRITICAL;
610
0
            break;
611
909
        case NID_subject_alt_name:
612
909
            x->ex_flags |= EXFLAG_SAN_CRITICAL;
613
909
            break;
614
30.7k
        default:
615
30.7k
            break;
616
81.7k
        }
617
81.7k
    }
618
619
    /* Set x->siginf, ignoring errors due to unsupported algos */
620
190k
    (void)ossl_x509_init_sig_info(x);
621
622
190k
    x->ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */
623
190k
#ifdef tsan_st_rel
624
190k
    tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
625
    /*
626
     * Above store triggers fast lock-free check in the beginning of the
627
     * function. But one has to ensure that the structure is "stable", i.e.
628
     * all stores are visible on all processors. Hence the release fence.
629
     */
630
190k
#endif
631
190k
    ERR_pop_to_mark();
632
633
190k
    if ((x->ex_flags & EXFLAG_INVALID) == 0) {
634
168k
        CRYPTO_THREAD_unlock(x->lock);
635
168k
        return 1;
636
168k
    }
637
22.0k
    CRYPTO_THREAD_unlock(x->lock);
638
22.0k
    ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE);
639
22.0k
    return 0;
640
190k
}
641
642
/*-
643
 * CA checks common to all purposes
644
 * return codes:
645
 * 0 not a CA
646
 * 1 is a CA
647
 * 2 Only possible in older versions of openSSL when basicConstraints are absent
648
 *   new versions will not return this value. May be a CA
649
 * 3 basicConstraints absent but self-signed V1.
650
 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
651
 * 5 Netscape specific CA Flags present
652
 */
653
654
static int check_ca(const X509 *x)
655
7.24k
{
656
    /* keyUsage if present should allow cert signing */
657
7.24k
    if (ku_reject(x, KU_KEY_CERT_SIGN))
658
15
        return 0;
659
7.22k
    if ((x->ex_flags & EXFLAG_BCONS) != 0) {
660
        /* If basicConstraints says not a CA then say so */
661
1.99k
        return (x->ex_flags & EXFLAG_CA) != 0;
662
5.23k
    } else {
663
        /* We support V1 roots for...  uh, I don't really know why. */
664
5.23k
        if ((x->ex_flags & V1_ROOT) == V1_ROOT)
665
105
            return 3;
666
        /*
667
         * If key usage present it must have certSign so tolerate it
668
         */
669
5.12k
        else if ((x->ex_flags & EXFLAG_KUSAGE) != 0)
670
10
            return 4;
671
        /* Older certificates could have Netscape-specific CA types */
672
5.11k
        else if ((x->ex_flags & EXFLAG_NSCERT) != 0
673
11
            && (x->ex_nscert & NS_ANY_CA) != 0)
674
4
            return 5;
675
        /* Can this still be regarded a CA certificate?  I doubt it. */
676
5.11k
        return 0;
677
5.23k
    }
678
7.22k
}
679
680
void X509_set_proxy_flag(X509 *x)
681
0
{
682
0
    if (CRYPTO_THREAD_write_lock(x->lock)) {
683
0
        x->ex_flags |= EXFLAG_PROXY;
684
0
        CRYPTO_THREAD_unlock(x->lock);
685
0
    }
686
0
}
687
688
void X509_set_proxy_pathlen(X509 *x, long l)
689
0
{
690
0
    x->ex_pcpathlen = l;
691
0
}
692
693
int X509_check_ca(X509 *x)
694
11.3k
{
695
    /* Note 0 normally means "not a CA" - but in this case means error. */
696
11.3k
    if (!ossl_x509v3_cache_extensions(x))
697
2.56k
        return 0;
698
699
8.82k
    return check_ca(x);
700
11.3k
}
701
702
/* Check SSL CA: common checks for SSL client and server. */
703
static int check_ssl_ca(const X509 *x)
704
0
{
705
0
    int ca_ret = check_ca(x);
706
707
0
    if (ca_ret == 0)
708
0
        return 0;
709
    /* Check nsCertType if present */
710
0
    return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0;
711
0
}
712
713
static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
714
    int non_leaf)
715
0
{
716
0
    if (xku_reject(x, XKU_SSL_CLIENT))
717
0
        return 0;
718
0
    if (non_leaf)
719
0
        return check_ssl_ca(x);
720
    /* We need to do digital signatures or key agreement */
721
0
    if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
722
0
        return 0;
723
    /* nsCertType if present should allow SSL client use */
724
0
    if (ns_reject(x, NS_SSL_CLIENT))
725
0
        return 0;
726
0
    return 1;
727
0
}
728
729
/*
730
 * Key usage needed for TLS/SSL server: digital signature, encipherment or
731
 * key agreement. The ssl code can check this more thoroughly for individual
732
 * key types.
733
 */
734
#define KU_TLS \
735
    KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT
736
737
static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
738
    int non_leaf)
739
0
{
740
0
    if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
741
0
        return 0;
742
0
    if (non_leaf)
743
0
        return check_ssl_ca(x);
744
745
0
    if (ns_reject(x, NS_SSL_SERVER))
746
0
        return 0;
747
0
    if (ku_reject(x, KU_TLS))
748
0
        return 0;
749
750
0
    return 1;
751
0
}
752
753
static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
754
    int non_leaf)
755
0
{
756
0
    int ret = check_purpose_ssl_server(xp, x, non_leaf);
757
758
0
    if (!ret || non_leaf)
759
0
        return ret;
760
    /* We need to encipher or Netscape complains */
761
0
    return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;
762
0
}
763
764
/* common S/MIME checks */
765
static int purpose_smime(const X509 *x, int non_leaf)
766
0
{
767
0
    if (xku_reject(x, XKU_SMIME))
768
0
        return 0;
769
0
    if (non_leaf) {
770
0
        int ca_ret = check_ca(x);
771
772
0
        if (ca_ret == 0)
773
0
            return 0;
774
        /* Check nsCertType if present */
775
0
        if (ca_ret != 5 || (x->ex_nscert & NS_SMIME_CA) != 0)
776
0
            return ca_ret;
777
0
        else
778
0
            return 0;
779
0
    }
780
0
    if ((x->ex_flags & EXFLAG_NSCERT) != 0) {
781
0
        if ((x->ex_nscert & NS_SMIME) != 0)
782
0
            return 1;
783
        /* Workaround for some buggy certificates */
784
0
        return (x->ex_nscert & NS_SSL_CLIENT) != 0 ? 2 : 0;
785
0
    }
786
0
    return 1;
787
0
}
788
789
static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
790
    int non_leaf)
791
0
{
792
0
    int ret = purpose_smime(x, non_leaf);
793
794
0
    if (!ret || non_leaf)
795
0
        return ret;
796
0
    return ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION) ? 0 : ret;
797
0
}
798
799
static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
800
    int non_leaf)
801
0
{
802
0
    int ret = purpose_smime(x, non_leaf);
803
804
0
    if (!ret || non_leaf)
805
0
        return ret;
806
0
    return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;
807
0
}
808
809
static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
810
    int non_leaf)
811
0
{
812
0
    if (non_leaf) {
813
0
        int ca_ret = check_ca(x);
814
815
0
        return ca_ret == 2 ? 0 : ca_ret;
816
0
    }
817
0
    return !ku_reject(x, KU_CRL_SIGN);
818
0
}
819
820
/*
821
 * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
822
 * is valid. Additional checks must be made on the chain.
823
 */
824
static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x,
825
    int non_leaf)
826
508
{
827
    /*
828
     * Must be a valid CA.  Should we really support the "I don't know" value
829
     * (2)?
830
     */
831
508
    if (non_leaf)
832
229
        return check_ca(x);
833
    /* Leaf certificate is checked in OCSP_verify() */
834
279
    return 1;
835
508
}
836
837
static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
838
    int non_leaf)
839
0
{
840
0
    int i_ext;
841
842
    /*
843
     * If non_leaf is true we must check if this is a valid CA certificate.
844
     * The extra requirements by the CA/Browser Forum are not checked.
845
     */
846
0
    if (non_leaf)
847
0
        return check_ca(x);
848
849
    /*
850
     * Key Usage is checked according to RFC 5280 and
851
     * Extended Key Usage attributes is checked according to RFC 3161.
852
     * The extra (and somewhat conflicting) CA/Browser Forum
853
     * Baseline Requirements for the Issuance and Management of
854
     * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,
855
     * Section 7.1.2.3: Code signing and Timestamp Certificate are not checked.
856
     */
857
    /*
858
     * Check the optional key usage field:
859
     * if Key Usage is present, it must be one of digitalSignature
860
     * and/or nonRepudiation (other values are not consistent and shall
861
     * be rejected).
862
     */
863
0
    if ((x->ex_flags & EXFLAG_KUSAGE) != 0
864
0
        && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
865
0
        return 0;
866
867
    /* Only timestamp key usage is permitted and it's required. */
868
0
    if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP)
869
0
        return 0;
870
871
    /* Extended Key Usage MUST be critical */
872
0
    i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
873
0
    if (i_ext >= 0
874
0
        && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext)))
875
0
        return 0;
876
0
    return 1;
877
0
}
878
879
static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x,
880
    int non_leaf)
881
0
{
882
0
    int i_ext;
883
884
    /*
885
     * If non_leaf is true we must check if this is a valid CA certificate.
886
     * The extra requirements by the CA/Browser Forum are not checked.
887
     */
888
0
    if (non_leaf)
889
0
        return check_ca(x);
890
891
    /*
892
     * Check the key usage and extended key usage fields:
893
     *
894
     * Reference: CA/Browser Forum,
895
     * Baseline Requirements for the Issuance and Management of
896
     * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,
897
     * Section 7.1.2.3: Code signing and Timestamp Certificate
898
     *
899
     * Checking covers Key Usage and Extended Key Usage attributes.
900
     * The certificatePolicies, cRLDistributionPoints (CDP), and
901
     * authorityInformationAccess (AIA) extensions are so far not checked.
902
     */
903
    /* Key Usage */
904
0
    if ((x->ex_flags & EXFLAG_KUSAGE) == 0)
905
0
        return 0;
906
0
    if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0)
907
0
        return 0;
908
0
    if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0)
909
0
        return 0;
910
911
    /* Key Usage MUST be critical */
912
0
    i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1);
913
0
    if (i_ext < 0)
914
0
        return 0;
915
0
    if (i_ext >= 0) {
916
0
        X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
917
0
        if (!X509_EXTENSION_get_critical(ext))
918
0
            return 0;
919
0
    }
920
921
    /* Extended Key Usage */
922
0
    if ((x->ex_flags & EXFLAG_XKUSAGE) == 0)
923
0
        return 0;
924
0
    if ((x->ex_xkusage & XKU_CODE_SIGN) == 0)
925
0
        return 0;
926
0
    if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0)
927
0
        return 0;
928
929
0
    return 1;
930
0
}
931
932
static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x,
933
    int non_leaf)
934
0
{
935
0
    return 1;
936
0
}
937
938
/*-
939
 * Various checks to see if one certificate potentially issued the second.
940
 * This can be used to prune a set of possible issuer certificates which
941
 * have been looked up using some simple method such as by subject name.
942
 * These are:
943
 * 1. issuer_name(subject) == subject_name(issuer)
944
 * 2. If akid(subject) exists, it matches the respective issuer fields.
945
 * 3. subject signature algorithm == issuer public key algorithm
946
 * 4. If key_usage(issuer) exists, it allows for signing subject.
947
 * Note that this does not include actually checking the signature.
948
 * Returns 0 for OK, or positive for reason for mismatch
949
 * where reason codes match those for X509_verify_cert().
950
 */
951
int X509_check_issued(X509 *issuer, X509 *subject)
952
37.9k
{
953
37.9k
    int ret;
954
955
37.9k
    if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK)
956
33.8k
        return ret;
957
4.08k
    return ossl_x509_signing_allowed(issuer, subject);
958
37.9k
}
959
960
/* do the checks 1., 2., and 3. as described above for X509_check_issued() */
961
int ossl_x509_likely_issued(X509 *issuer, X509 *subject)
962
78.7k
{
963
78.7k
    int ret;
964
965
78.7k
    if (X509_NAME_cmp(X509_get_subject_name(issuer),
966
78.7k
            X509_get_issuer_name(subject))
967
78.7k
        != 0)
968
47.1k
        return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
969
970
    /* set issuer->skid and subject->akid */
971
31.5k
    if (!ossl_x509v3_cache_extensions(issuer)
972
24.7k
        || !ossl_x509v3_cache_extensions(subject))
973
8.86k
        return X509_V_ERR_UNSPECIFIED;
974
975
22.6k
    ret = X509_check_akid(issuer, subject->akid);
976
22.6k
    if (ret != X509_V_OK)
977
2.61k
        return ret;
978
979
    /* Check if the subject signature alg matches the issuer's PUBKEY alg */
980
20.0k
    return check_sig_alg_match(X509_get0_pubkey(issuer), subject);
981
22.6k
}
982
983
/*-
984
 * Check if certificate I<issuer> is allowed to issue certificate I<subject>
985
 * according to the B<keyUsage> field of I<issuer> if present
986
 * depending on any proxyCertInfo extension of I<subject>.
987
 * Returns 0 for OK, or positive for reason for rejection
988
 * where reason codes match those for X509_verify_cert().
989
 */
990
int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject)
991
5.66k
{
992
5.66k
    if ((subject->ex_flags & EXFLAG_PROXY) != 0) {
993
7
        if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
994
0
            return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
995
5.65k
    } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) {
996
116
        return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
997
116
    }
998
5.54k
    return X509_V_OK;
999
5.66k
}
1000
1001
int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid)
1002
192k
{
1003
192k
    if (akid == NULL)
1004
159k
        return X509_V_OK;
1005
1006
    /* Check key ids (if present) */
1007
33.1k
    if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
1008
2.56k
        return X509_V_ERR_AKID_SKID_MISMATCH;
1009
    /* Check serial number */
1010
30.5k
    if (akid->serial && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial))
1011
1.40k
        return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1012
    /* Check issuer name */
1013
29.1k
    if (akid->issuer) {
1014
        /*
1015
         * Ugh, for some peculiar reason AKID includes SEQUENCE OF
1016
         * GeneralName. So look for a DirName. There may be more than one but
1017
         * we only take any notice of the first.
1018
         */
1019
472
        GENERAL_NAMES *gens = akid->issuer;
1020
472
        GENERAL_NAME *gen;
1021
472
        X509_NAME *nm = NULL;
1022
472
        int i;
1023
1024
799
        for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1025
502
            gen = sk_GENERAL_NAME_value(gens, i);
1026
502
            if (gen->type == GEN_DIRNAME) {
1027
175
                nm = gen->d.dirn;
1028
175
                break;
1029
175
            }
1030
502
        }
1031
472
        if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0)
1032
99
            return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1033
472
    }
1034
29.0k
    return X509_V_OK;
1035
29.1k
}
1036
1037
uint32_t X509_get_extension_flags(X509 *x)
1038
175k
{
1039
    /* Call for side-effect of computing hash and caching extensions */
1040
175k
    X509_check_purpose(x, -1, 0);
1041
175k
    return x->ex_flags;
1042
175k
}
1043
1044
uint32_t X509_get_key_usage(X509 *x)
1045
21.7k
{
1046
    /* Call for side-effect of computing hash and caching extensions */
1047
21.7k
    if (X509_check_purpose(x, -1, 0) != 1)
1048
0
        return 0;
1049
21.7k
    return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX;
1050
21.7k
}
1051
1052
uint32_t X509_get_extended_key_usage(X509 *x)
1053
7
{
1054
    /* Call for side-effect of computing hash and caching extensions */
1055
7
    if (X509_check_purpose(x, -1, 0) != 1)
1056
0
        return 0;
1057
7
    return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX;
1058
7
}
1059
1060
const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
1061
5.05k
{
1062
    /* Call for side-effect of computing hash and caching extensions */
1063
5.05k
    if (X509_check_purpose(x, -1, 0) != 1)
1064
500
        return NULL;
1065
4.55k
    return x->skid;
1066
5.05k
}
1067
1068
const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
1069
0
{
1070
    /* Call for side-effect of computing hash and caching extensions */
1071
0
    if (X509_check_purpose(x, -1, 0) != 1)
1072
0
        return NULL;
1073
0
    return (x->akid != NULL ? x->akid->keyid : NULL);
1074
0
}
1075
1076
const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
1077
0
{
1078
    /* Call for side-effect of computing hash and caching extensions */
1079
0
    if (X509_check_purpose(x, -1, 0) != 1)
1080
0
        return NULL;
1081
0
    return (x->akid != NULL ? x->akid->issuer : NULL);
1082
0
}
1083
1084
const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
1085
0
{
1086
    /* Call for side-effect of computing hash and caching extensions */
1087
0
    if (X509_check_purpose(x, -1, 0) != 1)
1088
0
        return NULL;
1089
0
    return (x->akid != NULL ? x->akid->serial : NULL);
1090
0
}
1091
1092
long X509_get_pathlen(X509 *x)
1093
0
{
1094
    /* Called for side effect of caching extensions */
1095
0
    if (X509_check_purpose(x, -1, 0) != 1
1096
0
        || (x->ex_flags & EXFLAG_BCONS) == 0)
1097
0
        return -1;
1098
0
    return x->ex_pathlen;
1099
0
}
1100
1101
long X509_get_proxy_pathlen(X509 *x)
1102
0
{
1103
    /* Called for side effect of caching extensions */
1104
0
    if (X509_check_purpose(x, -1, 0) != 1
1105
0
        || (x->ex_flags & EXFLAG_PROXY) == 0)
1106
0
        return -1;
1107
0
    return x->ex_pcpathlen;
1108
0
}