Coverage Report

Created: 2026-10-28 06:28

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-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
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
823
#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
181k
{
88
181k
    int idx;
89
181k
    const X509_PURPOSE *pt;
90
91
181k
    if (!ossl_x509v3_cache_extensions(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
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
71.5k
{
122
71.5k
    if (idx < 0)
123
70.6k
        return NULL;
124
823
    if (idx < (int)X509_PURPOSE_COUNT)
125
823
        return xstandard + idx;
126
0
    return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
127
823
}
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
71.5k
{
145
71.5k
    X509_PURPOSE tmp;
146
71.5k
    int idx;
147
148
71.5k
    if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX)
149
823
        return purpose - X509_PURPOSE_MIN;
150
70.6k
    if (xptable == NULL)
151
70.6k
        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
132k
{
264
132k
    return *a - *b;
265
132k
}
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
58.2k
{
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
58.2k
    static const int supported_nids[] = {
280
58.2k
        NID_netscape_cert_type, /* 71 */
281
58.2k
        NID_key_usage, /* 83 */
282
58.2k
        NID_subject_alt_name, /* 85 */
283
58.2k
        NID_basic_constraints, /* 87 */
284
58.2k
        NID_certificate_policies, /* 89 */
285
58.2k
        NID_crl_distribution_points, /* 103 */
286
58.2k
        NID_ext_key_usage, /* 126 */
287
58.2k
#ifndef OPENSSL_NO_RFC3779
288
58.2k
        NID_sbgp_ipAddrBlock, /* 290 */
289
58.2k
        NID_sbgp_autonomousSysNum, /* 291 */
290
58.2k
#endif
291
58.2k
        NID_id_pkix_OCSP_noCheck, /* 369 */
292
58.2k
        NID_policy_constraints, /* 401 */
293
58.2k
        NID_proxyCertInfo, /* 663 */
294
58.2k
        NID_name_constraints, /* 666 */
295
58.2k
        NID_policy_mappings, /* 747 */
296
58.2k
        NID_inhibit_any_policy /* 748 */
297
58.2k
    };
298
299
58.2k
    int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
300
301
58.2k
    if (ex_nid == NID_undef)
302
5.20k
        return 0;
303
304
53.0k
    if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
305
51.6k
        return 1;
306
1.37k
    return 0;
307
53.0k
}
308
309
/*
310
 * The full name of a nameRelativeToCRLIssuer distribution point is not
311
 * computed here.  Doing so for every parsed certificate cost an
312
 * X509_NAME_dup() of the issuer name per relative distribution point,
313
 * which a certificate with many such entries could turn into hundreds of
314
 * megabytes of heap on a plain TLS handshake, while the result is only
315
 * needed when a CRL is actually being matched against the certificate.
316
 * That name is now built on demand in the CRL checking code instead.
317
 */
318
static int setup_dp(DIST_POINT *dp)
319
2.50k
{
320
2.50k
    if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) {
321
50
        ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT);
322
50
        return 0;
323
50
    }
324
2.45k
    if (dp->reasons != NULL) {
325
36
        if (dp->reasons->length > 0)
326
34
            dp->dp_reasons = dp->reasons->data[0];
327
36
        if (dp->reasons->length > 1)
328
12
            dp->dp_reasons |= (dp->reasons->data[1] << 8);
329
36
        dp->dp_reasons &= CRLDP_ALL_REASONS;
330
2.41k
    } else {
331
2.41k
        dp->dp_reasons = CRLDP_ALL_REASONS;
332
2.41k
    }
333
2.45k
    return 1;
334
2.50k
}
335
336
/* Return 1 on success, 0 on error. */
337
static int setup_crldp(X509 *x)
338
126k
{
339
126k
    int i;
340
341
126k
    x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
342
126k
    if (x->crldp == NULL && i != -1)
343
2.06k
        return 0;
344
345
127k
    for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
346
2.50k
        if (!setup_dp(sk_DIST_POINT_value(x->crldp, i)))
347
50
            return 0;
348
2.50k
    }
349
124k
    return 1;
350
124k
}
351
352
/* Check that issuer public key algorithm matches subject signature algorithm */
353
static int check_sig_alg_match(const EVP_PKEY *issuer_key, const X509 *subject)
354
88.5k
{
355
88.5k
    int subj_sig_nid;
356
357
88.5k
    if (issuer_key == NULL)
358
6.97k
        return X509_V_ERR_NO_ISSUER_PUBLIC_KEY;
359
81.6k
    if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm),
360
81.6k
            NULL, &subj_sig_nid)
361
81.6k
        == 0)
362
7.82k
        return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM;
363
73.8k
    if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid))
364
1.86k
        || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss))
365
73.2k
        return X509_V_OK;
366
578
    return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH;
367
73.8k
}
368
369
14.2k
#define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)
370
#define ku_reject(x, usage) \
371
17.6k
    (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0)
372
#define xku_reject(x, usage) \
373
0
    (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0)
374
#define ns_reject(x, usage) \
375
0
    (((x)->ex_flags & EXFLAG_NSCERT) != 0 && ((x)->ex_nscert & (usage)) == 0)
376
377
/*
378
 * Cache info on various X.509v3 extensions and further derived information,
379
 * e.g., if cert 'x' is self-issued, in x->ex_flags and other internal fields.
380
 * x->sha1_hash is filled in, or else EXFLAG_NO_FINGERPRINT is set in x->flags.
381
 * X509_SIG_INFO_VALID is set in x->flags if x->siginf was filled successfully.
382
 * Set EXFLAG_INVALID and return 0 in case the certificate is invalid.
383
 */
384
int ossl_x509v3_cache_extensions(X509 *x)
385
200k
{
386
200k
    BASIC_CONSTRAINTS *bs;
387
200k
    PROXY_CERT_INFO_EXTENSION *pci;
388
200k
    ASN1_BIT_STRING *usage;
389
200k
    ASN1_BIT_STRING *ns;
390
200k
    EXTENDED_KEY_USAGE *extusage;
391
200k
    int i;
392
200k
    int res;
393
394
200k
#ifdef tsan_ld_acq
395
    /* Fast lock-free check, see end of the function for details. */
396
200k
    if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
397
123k
        return (x->ex_flags & EXFLAG_INVALID) == 0;
398
77.2k
#endif
399
400
77.2k
    if (!CRYPTO_THREAD_write_lock(x->lock))
401
0
        return 0;
402
77.2k
    if ((x->ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */
403
0
        CRYPTO_THREAD_unlock(x->lock);
404
0
        return (x->ex_flags & EXFLAG_INVALID) == 0;
405
0
    }
406
407
77.2k
    ERR_set_mark();
408
409
    /* Cache the SHA1 digest of the cert */
410
77.2k
    if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL))
411
0
        x->ex_flags |= EXFLAG_NO_FINGERPRINT;
412
413
    /* V1 should mean no extensions ... */
414
77.2k
    if (X509_get_version(x) == X509_VERSION_1)
415
1.80k
        x->ex_flags |= EXFLAG_V1;
416
417
    /* Handle basic constraints */
418
77.2k
    x->ex_pathlen = -1;
419
77.2k
    if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL)) != NULL) {
420
56.7k
        if (bs->ca)
421
11.6k
            x->ex_flags |= EXFLAG_CA;
422
56.7k
        if (bs->pathlen != NULL) {
423
            /*
424
             * The error case !bs->ca is checked by check_chain()
425
             * in case ctx->param->flags & X509_V_FLAG_X509_STRICT
426
             */
427
1.65k
            if (bs->pathlen->type == V_ASN1_NEG_INTEGER) {
428
74
                ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN);
429
74
                x->ex_flags |= EXFLAG_INVALID;
430
1.57k
            } else {
431
1.57k
                x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
432
1.57k
            }
433
1.65k
        }
434
56.7k
        BASIC_CONSTRAINTS_free(bs);
435
56.7k
        x->ex_flags |= EXFLAG_BCONS;
436
56.7k
    } else if (i != -1) {
437
5.68k
        x->ex_flags |= EXFLAG_INVALID;
438
5.68k
    }
439
440
    /* Handle proxy certificates */
441
77.2k
    if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL)) != NULL) {
442
258
        if ((x->ex_flags & EXFLAG_CA) != 0
443
253
            || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
444
192
            || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
445
104
            x->ex_flags |= EXFLAG_INVALID;
446
104
        }
447
258
        if (pci->pcPathLengthConstraint != NULL)
448
127
            x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
449
131
        else
450
131
            x->ex_pcpathlen = -1;
451
258
        PROXY_CERT_INFO_EXTENSION_free(pci);
452
258
        x->ex_flags |= EXFLAG_PROXY;
453
76.9k
    } else if (i != -1) {
454
118
        x->ex_flags |= EXFLAG_INVALID;
455
118
    }
456
457
    /* Handle (basic) key usage */
458
77.2k
    if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL)) != NULL) {
459
41.9k
        x->ex_kusage = 0;
460
41.9k
        if (usage->length > 0) {
461
41.8k
            x->ex_kusage = usage->data[0];
462
41.8k
            if (usage->length > 1)
463
51
                x->ex_kusage |= usage->data[1] << 8;
464
41.8k
        }
465
41.9k
        x->ex_flags |= EXFLAG_KUSAGE;
466
41.9k
        ASN1_BIT_STRING_free(usage);
467
        /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */
468
41.9k
        if (x->ex_kusage == 0) {
469
626
            ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE);
470
626
            x->ex_flags |= EXFLAG_INVALID;
471
626
        }
472
41.9k
    } else if (i != -1) {
473
1.18k
        x->ex_flags |= EXFLAG_INVALID;
474
1.18k
    }
475
476
    /* Handle extended key usage */
477
77.2k
    x->ex_xkusage = 0;
478
77.2k
    if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL)) != NULL) {
479
41.4k
        x->ex_flags |= EXFLAG_XKUSAGE;
480
83.1k
        for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
481
41.6k
            switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
482
39.7k
            case NID_server_auth:
483
39.7k
                x->ex_xkusage |= XKU_SSL_SERVER;
484
39.7k
                break;
485
114
            case NID_client_auth:
486
114
                x->ex_xkusage |= XKU_SSL_CLIENT;
487
114
                break;
488
60
            case NID_email_protect:
489
60
                x->ex_xkusage |= XKU_SMIME;
490
60
                break;
491
40
            case NID_code_sign:
492
40
                x->ex_xkusage |= XKU_CODE_SIGN;
493
40
                break;
494
3
            case NID_ms_sgc:
495
7
            case NID_ns_sgc:
496
7
                x->ex_xkusage |= XKU_SGC;
497
7
                break;
498
36
            case NID_OCSP_sign:
499
36
                x->ex_xkusage |= XKU_OCSP_SIGN;
500
36
                break;
501
50
            case NID_time_stamp:
502
50
                x->ex_xkusage |= XKU_TIMESTAMP;
503
50
                break;
504
37
            case NID_dvcs:
505
37
                x->ex_xkusage |= XKU_DVCS;
506
37
                break;
507
2
            case NID_anyExtendedKeyUsage:
508
2
                x->ex_xkusage |= XKU_ANYEKU;
509
2
                break;
510
1.55k
            default:
511
                /* Ignore unknown extended key usage */
512
1.55k
                break;
513
41.6k
            }
514
41.6k
        }
515
41.4k
        sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
516
41.4k
    } else if (i != -1) {
517
2.90k
        x->ex_flags |= EXFLAG_INVALID;
518
2.90k
    }
519
520
    /* Handle legacy Netscape extension */
521
77.2k
    if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL)) != NULL) {
522
32
        if (ns->length > 0)
523
22
            x->ex_nscert = ns->data[0];
524
10
        else
525
10
            x->ex_nscert = 0;
526
32
        x->ex_flags |= EXFLAG_NSCERT;
527
32
        ASN1_BIT_STRING_free(ns);
528
77.1k
    } else if (i != -1) {
529
18
        x->ex_flags |= EXFLAG_INVALID;
530
18
    }
531
532
    /* Handle subject key identifier and issuer/authority key identifier */
533
77.2k
    x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
534
77.2k
    if (x->skid == NULL && i != -1)
535
1.21k
        x->ex_flags |= EXFLAG_INVALID;
536
537
77.2k
    x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
538
77.2k
    if (x->akid == NULL && i != -1)
539
3.66k
        x->ex_flags |= EXFLAG_INVALID;
540
541
    /* Check if subject name matches issuer */
542
77.2k
    if (X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)) == 0) {
543
49.9k
        x->ex_flags |= EXFLAG_SI; /* Cert is self-issued */
544
49.9k
        if (X509_check_akid(x, x->akid) == X509_V_OK /* SKID matches AKID */
545
49.2k
            /* .. and the signature alg matches the PUBKEY alg: */
546
49.2k
            && check_sig_alg_match(X509_get0_pubkey(x), x) == X509_V_OK)
547
45.3k
            x->ex_flags |= EXFLAG_SS; /* indicate self-signed */
548
        /* This is very related to ossl_x509_likely_issued(x, x) == X509_V_OK */
549
49.9k
    }
550
551
    /* Handle subject alternative names and various other extensions */
552
77.2k
    x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
553
77.2k
    if (x->altname == NULL && i != -1)
554
2.19k
        x->ex_flags |= EXFLAG_INVALID;
555
77.2k
    x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
556
77.2k
    if (x->nc == NULL && i != -1)
557
550
        x->ex_flags |= EXFLAG_INVALID;
558
559
    /* Handle CRL distribution point entries */
560
77.2k
    res = setup_crldp(x);
561
77.2k
    if (res == 0)
562
1.09k
        x->ex_flags |= EXFLAG_INVALID;
563
564
77.2k
#ifndef OPENSSL_NO_RFC3779
565
77.2k
    x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
566
77.2k
    if (x->rfc3779_addr == NULL && i != -1)
567
139
        x->ex_flags |= EXFLAG_INVALID;
568
77.2k
    x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
569
77.2k
    if (x->rfc3779_asid == NULL && i != -1)
570
58
        x->ex_flags |= EXFLAG_INVALID;
571
77.2k
#endif
572
378k
    for (i = 0; i < X509_get_ext_count(x); i++) {
573
304k
        X509_EXTENSION *ex = X509_get_ext(x, i);
574
304k
        int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
575
576
304k
        if (nid == NID_freshest_crl)
577
2.29k
            x->ex_flags |= EXFLAG_FRESHEST;
578
304k
        if (!X509_EXTENSION_get_critical(ex))
579
269k
            continue;
580
35.7k
        if (!X509_supported_extension(ex)) {
581
3.92k
            x->ex_flags |= EXFLAG_CRITICAL;
582
3.92k
            break;
583
3.92k
        }
584
31.8k
        switch (nid) {
585
19.2k
        case NID_basic_constraints:
586
19.2k
            x->ex_flags |= EXFLAG_BCONS_CRITICAL;
587
19.2k
            break;
588
0
        case NID_authority_key_identifier:
589
0
            x->ex_flags |= EXFLAG_AKID_CRITICAL;
590
0
            break;
591
0
        case NID_subject_key_identifier:
592
0
            x->ex_flags |= EXFLAG_SKID_CRITICAL;
593
0
            break;
594
848
        case NID_subject_alt_name:
595
848
            x->ex_flags |= EXFLAG_SAN_CRITICAL;
596
848
            break;
597
11.7k
        default:
598
11.7k
            break;
599
31.8k
        }
600
31.8k
    }
601
602
    /* Set x->siginf, ignoring errors due to unsupported algos */
603
77.2k
    (void)ossl_x509_init_sig_info(x);
604
605
77.2k
    x->ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */
606
77.2k
#ifdef tsan_st_rel
607
77.2k
    tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
608
    /*
609
     * Above store triggers fast lock-free check in the beginning of the
610
     * function. But one has to ensure that the structure is "stable", i.e.
611
     * all stores are visible on all processors. Hence the release fence.
612
     */
613
77.2k
#endif
614
77.2k
    ERR_pop_to_mark();
615
616
77.2k
    if ((x->ex_flags & EXFLAG_INVALID) == 0) {
617
62.8k
        CRYPTO_THREAD_unlock(x->lock);
618
62.8k
        return 1;
619
62.8k
    }
620
14.3k
    CRYPTO_THREAD_unlock(x->lock);
621
14.3k
    ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE);
622
14.3k
    return 0;
623
77.2k
}
624
625
/*-
626
 * CA checks common to all purposes
627
 * return codes:
628
 * 0 not a CA
629
 * 1 is a CA
630
 * 2 Only possible in older versions of openSSL when basicConstraints are absent
631
 *   new versions will not return this value. May be a CA
632
 * 3 basicConstraints absent but self-signed V1.
633
 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
634
 * 5 Netscape specific CA Flags present
635
 */
636
637
static int check_ca(const X509 *x)
638
9.64k
{
639
    /* keyUsage if present should allow cert signing */
640
9.64k
    if (ku_reject(x, KU_KEY_CERT_SIGN))
641
76
        return 0;
642
9.56k
    if ((x->ex_flags & EXFLAG_BCONS) != 0) {
643
        /* If basicConstraints says not a CA then say so */
644
2.44k
        return (x->ex_flags & EXFLAG_CA) != 0;
645
7.12k
    } else {
646
        /* We support V1 roots for...  uh, I don't really know why. */
647
7.12k
        if ((x->ex_flags & V1_ROOT) == V1_ROOT)
648
94
            return 3;
649
        /*
650
         * If key usage present it must have certSign so tolerate it
651
         */
652
7.03k
        else if ((x->ex_flags & EXFLAG_KUSAGE) != 0)
653
35
            return 4;
654
        /* Older certificates could have Netscape-specific CA types */
655
6.99k
        else if ((x->ex_flags & EXFLAG_NSCERT) != 0
656
14
            && (x->ex_nscert & NS_ANY_CA) != 0)
657
6
            return 5;
658
        /* Can this still be regarded a CA certificate?  I doubt it. */
659
6.99k
        return 0;
660
7.12k
    }
661
9.56k
}
662
663
void X509_set_proxy_flag(X509 *x)
664
0
{
665
0
    if (CRYPTO_THREAD_write_lock(x->lock)) {
666
0
        x->ex_flags |= EXFLAG_PROXY;
667
0
        CRYPTO_THREAD_unlock(x->lock);
668
0
    }
669
0
}
670
671
void X509_set_proxy_pathlen(X509 *x, long l)
672
0
{
673
0
    x->ex_pcpathlen = l;
674
0
}
675
676
int X509_check_ca(X509 *x)
677
10.7k
{
678
    /* Note 0 normally means "not a CA" - but in this case means error. */
679
10.7k
    if (!ossl_x509v3_cache_extensions(x))
680
1.32k
        return 0;
681
682
9.42k
    return check_ca(x);
683
10.7k
}
684
685
/* Check SSL CA: common checks for SSL client and server. */
686
static int check_ssl_ca(const X509 *x)
687
0
{
688
0
    int ca_ret = check_ca(x);
689
690
0
    if (ca_ret == 0)
691
0
        return 0;
692
    /* Check nsCertType if present */
693
0
    return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0;
694
0
}
695
696
static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
697
    int non_leaf)
698
0
{
699
0
    if (xku_reject(x, XKU_SSL_CLIENT))
700
0
        return 0;
701
0
    if (non_leaf)
702
0
        return check_ssl_ca(x);
703
    /* We need to do digital signatures or key agreement */
704
0
    if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
705
0
        return 0;
706
    /* nsCertType if present should allow SSL client use */
707
0
    if (ns_reject(x, NS_SSL_CLIENT))
708
0
        return 0;
709
0
    return 1;
710
0
}
711
712
/*
713
 * Key usage needed for TLS/SSL server: digital signature, encipherment or
714
 * key agreement. The ssl code can check this more thoroughly for individual
715
 * key types.
716
 */
717
#define KU_TLS \
718
    KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT
719
720
static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
721
    int non_leaf)
722
0
{
723
0
    if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
724
0
        return 0;
725
0
    if (non_leaf)
726
0
        return check_ssl_ca(x);
727
728
0
    if (ns_reject(x, NS_SSL_SERVER))
729
0
        return 0;
730
0
    if (ku_reject(x, KU_TLS))
731
0
        return 0;
732
733
0
    return 1;
734
0
}
735
736
static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
737
    int non_leaf)
738
0
{
739
0
    int ret = check_purpose_ssl_server(xp, x, non_leaf);
740
741
0
    if (!ret || non_leaf)
742
0
        return ret;
743
    /* We need to encipher or Netscape complains */
744
0
    return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;
745
0
}
746
747
/* common S/MIME checks */
748
static int purpose_smime(const X509 *x, int non_leaf)
749
0
{
750
0
    if (xku_reject(x, XKU_SMIME))
751
0
        return 0;
752
0
    if (non_leaf) {
753
0
        int ca_ret = check_ca(x);
754
755
0
        if (ca_ret == 0)
756
0
            return 0;
757
        /* Check nsCertType if present */
758
0
        if (ca_ret != 5 || (x->ex_nscert & NS_SMIME_CA) != 0)
759
0
            return ca_ret;
760
0
        else
761
0
            return 0;
762
0
    }
763
0
    if ((x->ex_flags & EXFLAG_NSCERT) != 0) {
764
0
        if ((x->ex_nscert & NS_SMIME) != 0)
765
0
            return 1;
766
        /* Workaround for some buggy certificates */
767
0
        return (x->ex_nscert & NS_SSL_CLIENT) != 0 ? 2 : 0;
768
0
    }
769
0
    return 1;
770
0
}
771
772
static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
773
    int non_leaf)
774
0
{
775
0
    int ret = purpose_smime(x, non_leaf);
776
777
0
    if (!ret || non_leaf)
778
0
        return ret;
779
0
    return ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION) ? 0 : ret;
780
0
}
781
782
static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
783
    int non_leaf)
784
0
{
785
0
    int ret = purpose_smime(x, non_leaf);
786
787
0
    if (!ret || non_leaf)
788
0
        return ret;
789
0
    return ku_reject(x, KU_KEY_ENCIPHERMENT) ? 0 : ret;
790
0
}
791
792
static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
793
    int non_leaf)
794
0
{
795
0
    if (non_leaf) {
796
0
        int ca_ret = check_ca(x);
797
798
0
        return ca_ret == 2 ? 0 : ca_ret;
799
0
    }
800
0
    return !ku_reject(x, KU_CRL_SIGN);
801
0
}
802
803
/*
804
 * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
805
 * is valid. Additional checks must be made on the chain.
806
 */
807
static int check_purpose_ocsp_helper(const X509_PURPOSE *xp, const X509 *x,
808
    int non_leaf)
809
499
{
810
    /*
811
     * Must be a valid CA.  Should we really support the "I don't know" value
812
     * (2)?
813
     */
814
499
    if (non_leaf)
815
219
        return check_ca(x);
816
    /* Leaf certificate is checked in OCSP_verify() */
817
280
    return 1;
818
499
}
819
820
static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
821
    int non_leaf)
822
0
{
823
0
    int i_ext;
824
825
    /*
826
     * If non_leaf is true we must check if this is a valid CA certificate.
827
     * The extra requirements by the CA/Browser Forum are not checked.
828
     */
829
0
    if (non_leaf)
830
0
        return check_ca(x);
831
832
    /*
833
     * Key Usage is checked according to RFC 5280 and
834
     * Extended Key Usage attributes is checked according to RFC 3161.
835
     * The extra (and somewhat conflicting) CA/Browser Forum
836
     * Baseline Requirements for the Issuance and Management of
837
     * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,
838
     * Section 7.1.2.3: Code signing and Timestamp Certificate are not checked.
839
     */
840
    /*
841
     * Check the optional key usage field:
842
     * if Key Usage is present, it must be one of digitalSignature
843
     * and/or nonRepudiation (other values are not consistent and shall
844
     * be rejected).
845
     */
846
0
    if ((x->ex_flags & EXFLAG_KUSAGE) != 0
847
0
        && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
848
0
        return 0;
849
850
    /* Only timestamp key usage is permitted and it's required. */
851
0
    if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP)
852
0
        return 0;
853
854
    /* Extended Key Usage MUST be critical */
855
0
    i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
856
0
    if (i_ext >= 0
857
0
        && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext)))
858
0
        return 0;
859
0
    return 1;
860
0
}
861
862
static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x,
863
    int non_leaf)
864
0
{
865
0
    int i_ext;
866
867
    /*
868
     * If non_leaf is true we must check if this is a valid CA certificate.
869
     * The extra requirements by the CA/Browser Forum are not checked.
870
     */
871
0
    if (non_leaf)
872
0
        return check_ca(x);
873
874
    /*
875
     * Check the key usage and extended key usage fields:
876
     *
877
     * Reference: CA/Browser Forum,
878
     * Baseline Requirements for the Issuance and Management of
879
     * Publicly‐Trusted Code Signing Certificates, Version 3.0.0,
880
     * Section 7.1.2.3: Code signing and Timestamp Certificate
881
     *
882
     * Checking covers Key Usage and Extended Key Usage attributes.
883
     * The certificatePolicies, cRLDistributionPoints (CDP), and
884
     * authorityInformationAccess (AIA) extensions are so far not checked.
885
     */
886
    /* Key Usage */
887
0
    if ((x->ex_flags & EXFLAG_KUSAGE) == 0)
888
0
        return 0;
889
0
    if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0)
890
0
        return 0;
891
0
    if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0)
892
0
        return 0;
893
894
    /* Key Usage MUST be critical */
895
0
    i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1);
896
0
    if (i_ext < 0)
897
0
        return 0;
898
0
    if (i_ext >= 0) {
899
0
        X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
900
0
        if (!X509_EXTENSION_get_critical(ext))
901
0
            return 0;
902
0
    }
903
904
    /* Extended Key Usage */
905
0
    if ((x->ex_flags & EXFLAG_XKUSAGE) == 0)
906
0
        return 0;
907
0
    if ((x->ex_xkusage & XKU_CODE_SIGN) == 0)
908
0
        return 0;
909
0
    if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0)
910
0
        return 0;
911
912
0
    return 1;
913
0
}
914
915
static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x,
916
    int non_leaf)
917
0
{
918
0
    return 1;
919
0
}
920
921
/*-
922
 * Various checks to see if one certificate potentially issued the second.
923
 * This can be used to prune a set of possible issuer certificates which
924
 * have been looked up using some simple method such as by subject name.
925
 * These are:
926
 * 1. issuer_name(subject) == subject_name(issuer)
927
 * 2. If akid(subject) exists, it matches the respective issuer fields.
928
 * 3. subject signature algorithm == issuer public key algorithm
929
 * 4. If key_usage(issuer) exists, it allows for signing subject.
930
 * Note that this does not include actually checking the signature.
931
 * Returns 0 for OK, or positive for reason for mismatch
932
 * where reason codes match those for X509_verify_cert().
933
 */
934
int X509_check_issued(X509 *issuer, X509 *subject)
935
39.0k
{
936
39.0k
    int ret;
937
938
39.0k
    if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK)
939
34.5k
        return ret;
940
4.51k
    return ossl_x509_signing_allowed(issuer, subject);
941
39.0k
}
942
943
/* do the checks 1., 2., and 3. as described above for X509_check_issued() */
944
int ossl_x509_likely_issued(X509 *issuer, X509 *subject)
945
40.0k
{
946
40.0k
    int ret;
947
948
40.0k
    if (X509_NAME_cmp(X509_get_subject_name(issuer),
949
40.0k
            X509_get_issuer_name(subject))
950
40.0k
        != 0)
951
25.4k
        return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
952
953
    /* set issuer->skid and subject->akid */
954
14.6k
    if (!ossl_x509v3_cache_extensions(issuer)
955
11.2k
        || !ossl_x509v3_cache_extensions(subject))
956
3.83k
        return X509_V_ERR_UNSPECIFIED;
957
958
10.7k
    ret = X509_check_akid(issuer, subject->akid);
959
10.7k
    if (ret != X509_V_OK)
960
1.20k
        return ret;
961
962
    /* Check if the subject signature alg matches the issuer's PUBKEY alg */
963
9.57k
    return check_sig_alg_match(X509_get0_pubkey(issuer), subject);
964
10.7k
}
965
966
/*-
967
 * Check if certificate I<issuer> is allowed to issue certificate I<subject>
968
 * according to the B<keyUsage> field of I<issuer> if present
969
 * depending on any proxyCertInfo extension of I<subject>.
970
 * Returns 0 for OK, or positive for reason for rejection
971
 * where reason codes match those for X509_verify_cert().
972
 */
973
int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject)
974
8.00k
{
975
8.00k
    if ((subject->ex_flags & EXFLAG_PROXY) != 0) {
976
4
        if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
977
0
            return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
978
7.99k
    } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) {
979
199
        return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
980
199
    }
981
7.80k
    return X509_V_OK;
982
8.00k
}
983
984
int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid)
985
94.9k
{
986
94.9k
    if (akid == NULL)
987
73.8k
        return X509_V_OK;
988
989
    /* Check key ids (if present) */
990
21.0k
    if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
991
1.47k
        return X509_V_ERR_AKID_SKID_MISMATCH;
992
    /* Check serial number */
993
19.5k
    if (akid->serial && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial))
994
1.14k
        return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
995
    /* Check issuer name */
996
18.4k
    if (akid->issuer) {
997
        /*
998
         * Ugh, for some peculiar reason AKID includes SEQUENCE OF
999
         * GeneralName. So look for a DirName. There may be more than one but
1000
         * we only take any notice of the first.
1001
         */
1002
358
        GENERAL_NAMES *gens = akid->issuer;
1003
358
        GENERAL_NAME *gen;
1004
358
        X509_NAME *nm = NULL;
1005
358
        int i;
1006
1007
650
        for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1008
447
            gen = sk_GENERAL_NAME_value(gens, i);
1009
447
            if (gen->type == GEN_DIRNAME) {
1010
155
                nm = gen->d.dirn;
1011
155
                break;
1012
155
            }
1013
447
        }
1014
358
        if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0)
1015
106
            return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1016
358
    }
1017
18.3k
    return X509_V_OK;
1018
18.4k
}
1019
1020
uint32_t X509_get_extension_flags(X509 *x)
1021
32.9k
{
1022
    /* Call for side-effect of computing hash and caching extensions */
1023
32.9k
    X509_check_purpose(x, -1, 0);
1024
32.9k
    return x->ex_flags;
1025
32.9k
}
1026
1027
uint32_t X509_get_key_usage(X509 *x)
1028
24.0k
{
1029
    /* Call for side-effect of computing hash and caching extensions */
1030
24.0k
    if (X509_check_purpose(x, -1, 0) != 1)
1031
0
        return 0;
1032
24.0k
    return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX;
1033
24.0k
}
1034
1035
uint32_t X509_get_extended_key_usage(X509 *x)
1036
8
{
1037
    /* Call for side-effect of computing hash and caching extensions */
1038
8
    if (X509_check_purpose(x, -1, 0) != 1)
1039
0
        return 0;
1040
8
    return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX;
1041
8
}
1042
1043
const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
1044
6.90k
{
1045
    /* Call for side-effect of computing hash and caching extensions */
1046
6.90k
    if (X509_check_purpose(x, -1, 0) != 1)
1047
808
        return NULL;
1048
6.09k
    return x->skid;
1049
6.90k
}
1050
1051
const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
1052
0
{
1053
    /* Call for side-effect of computing hash and caching extensions */
1054
0
    if (X509_check_purpose(x, -1, 0) != 1)
1055
0
        return NULL;
1056
0
    return (x->akid != NULL ? x->akid->keyid : NULL);
1057
0
}
1058
1059
const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
1060
0
{
1061
    /* Call for side-effect of computing hash and caching extensions */
1062
0
    if (X509_check_purpose(x, -1, 0) != 1)
1063
0
        return NULL;
1064
0
    return (x->akid != NULL ? x->akid->issuer : NULL);
1065
0
}
1066
1067
const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
1068
0
{
1069
    /* Call for side-effect of computing hash and caching extensions */
1070
0
    if (X509_check_purpose(x, -1, 0) != 1)
1071
0
        return NULL;
1072
0
    return (x->akid != NULL ? x->akid->serial : NULL);
1073
0
}
1074
1075
long X509_get_pathlen(X509 *x)
1076
0
{
1077
    /* Called for side effect of caching extensions */
1078
0
    if (X509_check_purpose(x, -1, 0) != 1
1079
0
        || (x->ex_flags & EXFLAG_BCONS) == 0)
1080
0
        return -1;
1081
0
    return x->ex_pathlen;
1082
0
}
1083
1084
long X509_get_proxy_pathlen(X509 *x)
1085
0
{
1086
    /* Called for side effect of caching extensions */
1087
0
    if (X509_check_purpose(x, -1, 0) != 1
1088
0
        || (x->ex_flags & EXFLAG_PROXY) == 0)
1089
0
        return -1;
1090
0
    return x->ex_pcpathlen;
1091
0
}