Coverage Report

Created: 2026-05-24 07:14

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
944
#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
345k
{
88
345k
    int idx;
89
345k
    const X509_PURPOSE *pt;
90
91
345k
    if (!ossl_x509v3_cache_extensions(x))
92
44.8k
        return -1;
93
300k
    if (id == -1)
94
300k
        return 1;
95
96
580
    idx = X509_PURPOSE_get_by_id(id);
97
580
    if (idx == -1)
98
0
        return -1;
99
580
    pt = X509_PURPOSE_get0(idx);
100
580
    return pt->check_purpose(pt, x, non_leaf);
101
580
}
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
76.1k
{
122
76.1k
    if (idx < 0)
123
75.2k
        return NULL;
124
944
    if (idx < (int)X509_PURPOSE_COUNT)
125
944
        return xstandard + idx;
126
0
    return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
127
944
}
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
76.1k
{
145
76.1k
    X509_PURPOSE tmp;
146
76.1k
    int idx;
147
148
76.1k
    if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX)
149
944
        return purpose - X509_PURPOSE_MIN;
150
75.2k
    if (xptable == NULL)
151
75.2k
        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
240k
{
264
240k
    return *a - *b;
265
240k
}
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
104k
{
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
104k
    static const int supported_nids[] = {
280
104k
        NID_netscape_cert_type, /* 71 */
281
104k
        NID_key_usage, /* 83 */
282
104k
        NID_subject_alt_name, /* 85 */
283
104k
        NID_basic_constraints, /* 87 */
284
104k
        NID_certificate_policies, /* 89 */
285
104k
        NID_crl_distribution_points, /* 103 */
286
104k
        NID_ext_key_usage, /* 126 */
287
104k
#ifndef OPENSSL_NO_RFC3779
288
104k
        NID_sbgp_ipAddrBlock, /* 290 */
289
104k
        NID_sbgp_autonomousSysNum, /* 291 */
290
104k
#endif
291
104k
        NID_id_pkix_OCSP_noCheck, /* 369 */
292
104k
        NID_policy_constraints, /* 401 */
293
104k
        NID_proxyCertInfo, /* 663 */
294
104k
        NID_name_constraints, /* 666 */
295
104k
        NID_policy_mappings, /* 747 */
296
104k
        NID_inhibit_any_policy /* 748 */
297
104k
    };
298
299
104k
    int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
300
301
104k
    if (ex_nid == NID_undef)
302
6.36k
        return 0;
303
304
98.1k
    if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
305
96.6k
        return 1;
306
1.47k
    return 0;
307
98.1k
}
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
62
        ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT);
317
62
        return 0;
318
62
    }
319
1.84k
    if (dp->reasons != NULL) {
320
39
        if (dp->reasons->length > 0)
321
39
            dp->dp_reasons = dp->reasons->data[0];
322
39
        if (dp->reasons->length > 1)
323
17
            dp->dp_reasons |= (dp->reasons->data[1] << 8);
324
39
        dp->dp_reasons &= CRLDP_ALL_REASONS;
325
1.80k
    } else {
326
1.80k
        dp->dp_reasons = CRLDP_ALL_REASONS;
327
1.80k
    }
328
1.84k
    if (dp->distpoint == NULL || dp->distpoint->type != 1)
329
1.80k
        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
81
    for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
339
36
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
340
341
36
        if (gen->type == GEN_DIRNAME) {
342
2
            iname = gen->d.directoryName;
343
2
            break;
344
2
        }
345
36
    }
346
47
    if (iname == NULL)
347
45
        iname = X509_get_issuer_name(x);
348
47
    return DIST_POINT_set_dpname(dp->distpoint, iname) ? 1 : -1;
349
1.84k
}
350
351
/* Return 1 on success, 0 if x is invalid, -1 on (internal) error. */
352
static int setup_crldp(X509 *x)
353
224k
{
354
224k
    int i;
355
356
224k
    x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
357
224k
    if (x->crldp == NULL && i != -1)
358
1.95k
        return 0;
359
360
223k
    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
80
            return res;
365
1.90k
    }
366
222k
    return 1;
367
222k
}
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
189k
{
372
189k
    int subj_sig_nid;
373
374
189k
    if (issuer_key == NULL)
375
11.0k
        return X509_V_ERR_NO_ISSUER_PUBLIC_KEY;
376
178k
    if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm),
377
178k
            NULL, &subj_sig_nid)
378
178k
        == 0)
379
8.84k
        return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM;
380
169k
    if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid))
381
2.81k
        || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss))
382
169k
        return X509_V_OK;
383
704
    return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH;
384
169k
}
385
386
12.1k
#define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)
387
#define ku_reject(x, usage) \
388
13.7k
    (((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
371k
{
403
371k
    BASIC_CONSTRAINTS *bs;
404
371k
    PROXY_CERT_INFO_EXTENSION *pci;
405
371k
    ASN1_BIT_STRING *usage;
406
371k
    ASN1_BIT_STRING *ns;
407
371k
    EXTENDED_KEY_USAGE *extusage;
408
371k
    int i;
409
371k
    int res;
410
411
371k
#ifdef tsan_ld_acq
412
    /* Fast lock-free check, see end of the function for details. */
413
371k
    if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
414
207k
        return (x->ex_flags & EXFLAG_INVALID) == 0;
415
164k
#endif
416
417
164k
    if (!CRYPTO_THREAD_write_lock(x->lock))
418
0
        return 0;
419
164k
    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
164k
    ERR_set_mark();
425
426
    /* Cache the SHA1 digest of the cert */
427
164k
    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
164k
    if (X509_get_version(x) == X509_VERSION_1)
432
2.59k
        x->ex_flags |= EXFLAG_V1;
433
434
    /* Handle basic constraints */
435
164k
    x->ex_pathlen = -1;
436
164k
    if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL)) != NULL) {
437
134k
        if (bs->ca)
438
22.4k
            x->ex_flags |= EXFLAG_CA;
439
134k
        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.29k
            if (bs->pathlen->type == V_ASN1_NEG_INTEGER) {
445
98
                ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN);
446
98
                x->ex_flags |= EXFLAG_INVALID;
447
2.19k
            } else {
448
2.19k
                x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
449
2.19k
            }
450
2.29k
        }
451
134k
        BASIC_CONSTRAINTS_free(bs);
452
134k
        x->ex_flags |= EXFLAG_BCONS;
453
134k
    } else if (i != -1) {
454
7.60k
        x->ex_flags |= EXFLAG_INVALID;
455
7.60k
    }
456
457
    /* Handle proxy certificates */
458
164k
    if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL)) != NULL) {
459
385
        if ((x->ex_flags & EXFLAG_CA) != 0
460
380
            || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
461
272
            || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
462
175
            x->ex_flags |= EXFLAG_INVALID;
463
175
        }
464
385
        if (pci->pcPathLengthConstraint != NULL)
465
199
            x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
466
186
        else
467
186
            x->ex_pcpathlen = -1;
468
385
        PROXY_CERT_INFO_EXTENSION_free(pci);
469
385
        x->ex_flags |= EXFLAG_PROXY;
470
163k
    } else if (i != -1) {
471
213
        x->ex_flags |= EXFLAG_INVALID;
472
213
    }
473
474
    /* Handle (basic) key usage */
475
164k
    if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL)) != NULL) {
476
105k
        x->ex_kusage = 0;
477
105k
        if (usage->length > 0) {
478
105k
            x->ex_kusage = usage->data[0];
479
105k
            if (usage->length > 1)
480
63
                x->ex_kusage |= usage->data[1] << 8;
481
105k
        }
482
105k
        x->ex_flags |= EXFLAG_KUSAGE;
483
105k
        ASN1_BIT_STRING_free(usage);
484
        /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */
485
105k
        if (x->ex_kusage == 0) {
486
1.03k
            ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE);
487
1.03k
            x->ex_flags |= EXFLAG_INVALID;
488
1.03k
        }
489
105k
    } else if (i != -1) {
490
1.45k
        x->ex_flags |= EXFLAG_INVALID;
491
1.45k
    }
492
493
    /* Handle extended key usage */
494
164k
    x->ex_xkusage = 0;
495
164k
    if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL)) != NULL) {
496
106k
        x->ex_flags |= EXFLAG_XKUSAGE;
497
212k
        for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
498
106k
            switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
499
103k
            case NID_server_auth:
500
103k
                x->ex_xkusage |= XKU_SSL_SERVER;
501
103k
                break;
502
133
            case NID_client_auth:
503
133
                x->ex_xkusage |= XKU_SSL_CLIENT;
504
133
                break;
505
100
            case NID_email_protect:
506
100
                x->ex_xkusage |= XKU_SMIME;
507
100
                break;
508
40
            case NID_code_sign:
509
40
                x->ex_xkusage |= XKU_CODE_SIGN;
510
40
                break;
511
2
            case NID_ms_sgc:
512
6
            case NID_ns_sgc:
513
6
                x->ex_xkusage |= XKU_SGC;
514
6
                break;
515
33
            case NID_OCSP_sign:
516
33
                x->ex_xkusage |= XKU_OCSP_SIGN;
517
33
                break;
518
75
            case NID_time_stamp:
519
75
                x->ex_xkusage |= XKU_TIMESTAMP;
520
75
                break;
521
61
            case NID_dvcs:
522
61
                x->ex_xkusage |= XKU_DVCS;
523
61
                break;
524
2
            case NID_anyExtendedKeyUsage:
525
2
                x->ex_xkusage |= XKU_ANYEKU;
526
2
                break;
527
2.32k
            default:
528
                /* Ignore unknown extended key usage */
529
2.32k
                break;
530
106k
            }
531
106k
        }
532
106k
        sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
533
106k
    } else if (i != -1) {
534
5.00k
        x->ex_flags |= EXFLAG_INVALID;
535
5.00k
    }
536
537
    /* Handle legacy Netscape extension */
538
164k
    if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL)) != NULL) {
539
25
        if (ns->length > 0)
540
16
            x->ex_nscert = ns->data[0];
541
9
        else
542
9
            x->ex_nscert = 0;
543
25
        x->ex_flags |= EXFLAG_NSCERT;
544
25
        ASN1_BIT_STRING_free(ns);
545
164k
    } else if (i != -1) {
546
21
        x->ex_flags |= EXFLAG_INVALID;
547
21
    }
548
549
    /* Handle subject key identifier and issuer/authority key identifier */
550
164k
    x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
551
164k
    if (x->skid == NULL && i != -1)
552
2.28k
        x->ex_flags |= EXFLAG_INVALID;
553
554
164k
    x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
555
164k
    if (x->akid == NULL && i != -1)
556
5.34k
        x->ex_flags |= EXFLAG_INVALID;
557
558
    /* Check if subject name matches issuer */
559
164k
    if (X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)) == 0) {
560
124k
        x->ex_flags |= EXFLAG_SI; /* Cert is self-issued */
561
124k
        if (X509_check_akid(x, x->akid) == X509_V_OK /* SKID matches AKID */
562
123k
            /* .. and the signature alg matches the PUBKEY alg: */
563
123k
            && check_sig_alg_match(X509_get0_pubkey(x), x) == X509_V_OK)
564
117k
            x->ex_flags |= EXFLAG_SS; /* indicate self-signed */
565
        /* This is very related to ossl_x509_likely_issued(x, x) == X509_V_OK */
566
124k
    }
567
568
    /* Handle subject alternative names and various other extensions */
569
164k
    x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
570
164k
    if (x->altname == NULL && i != -1)
571
2.73k
        x->ex_flags |= EXFLAG_INVALID;
572
164k
    x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
573
164k
    if (x->nc == NULL && i != -1)
574
735
        x->ex_flags |= EXFLAG_INVALID;
575
576
    /* Handle CRL distribution point entries */
577
164k
    res = setup_crldp(x);
578
164k
    if (res == 0)
579
1.40k
        x->ex_flags |= EXFLAG_INVALID;
580
581
164k
#ifndef OPENSSL_NO_RFC3779
582
164k
    x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
583
164k
    if (x->rfc3779_addr == NULL && i != -1)
584
189
        x->ex_flags |= EXFLAG_INVALID;
585
164k
    x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
586
164k
    if (x->rfc3779_asid == NULL && i != -1)
587
91
        x->ex_flags |= EXFLAG_INVALID;
588
164k
#endif
589
826k
    for (i = 0; i < X509_get_ext_count(x); i++) {
590
667k
        X509_EXTENSION *ex = X509_get_ext(x, i);
591
667k
        int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
592
593
667k
        if (nid == NID_freshest_crl)
594
19.8k
            x->ex_flags |= EXFLAG_FRESHEST;
595
667k
        if (!X509_EXTENSION_get_critical(ex))
596
593k
            continue;
597
73.8k
        if (!X509_supported_extension(ex)) {
598
5.44k
            x->ex_flags |= EXFLAG_CRITICAL;
599
5.44k
            break;
600
5.44k
        }
601
68.4k
        switch (nid) {
602
42.1k
        case NID_basic_constraints:
603
42.1k
            x->ex_flags |= EXFLAG_BCONS_CRITICAL;
604
42.1k
            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
603
        case NID_subject_alt_name:
612
603
            x->ex_flags |= EXFLAG_SAN_CRITICAL;
613
603
            break;
614
25.7k
        default:
615
25.7k
            break;
616
68.4k
        }
617
68.4k
    }
618
619
    /* Set x->siginf, ignoring errors due to unsupported algos */
620
164k
    (void)ossl_x509_init_sig_info(x);
621
622
164k
    x->ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */
623
164k
#ifdef tsan_st_rel
624
164k
    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
164k
#endif
631
164k
    ERR_pop_to_mark();
632
633
164k
    if ((x->ex_flags & EXFLAG_INVALID) == 0) {
634
142k
        CRYPTO_THREAD_unlock(x->lock);
635
142k
        return 1;
636
142k
    }
637
21.3k
    CRYPTO_THREAD_unlock(x->lock);
638
21.3k
    ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE);
639
21.3k
    return 0;
640
164k
}
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
8.11k
{
656
    /* keyUsage if present should allow cert signing */
657
8.11k
    if (ku_reject(x, KU_KEY_CERT_SIGN))
658
18
        return 0;
659
8.10k
    if ((x->ex_flags & EXFLAG_BCONS) != 0) {
660
        /* If basicConstraints says not a CA then say so */
661
2.00k
        return (x->ex_flags & EXFLAG_CA) != 0;
662
6.09k
    } else {
663
        /* We support V1 roots for...  uh, I don't really know why. */
664
6.09k
        if ((x->ex_flags & V1_ROOT) == V1_ROOT)
665
119
            return 3;
666
        /*
667
         * If key usage present it must have certSign so tolerate it
668
         */
669
5.97k
        else if ((x->ex_flags & EXFLAG_KUSAGE) != 0)
670
17
            return 4;
671
        /* Older certificates could have Netscape-specific CA types */
672
5.95k
        else if ((x->ex_flags & EXFLAG_NSCERT) != 0
673
12
            && (x->ex_nscert & NS_ANY_CA) != 0)
674
5
            return 5;
675
        /* Can this still be regarded a CA certificate?  I doubt it. */
676
5.95k
        return 0;
677
6.09k
    }
678
8.10k
}
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.8k
{
695
    /* Note 0 normally means "not a CA" - but in this case means error. */
696
11.8k
    if (!ossl_x509v3_cache_extensions(x))
697
2.17k
        return 0;
698
699
9.71k
    return check_ca(x);
700
11.8k
}
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
580
{
827
    /*
828
     * Must be a valid CA.  Should we really support the "I don't know" value
829
     * (2)?
830
     */
831
580
    if (non_leaf)
832
263
        return check_ca(x);
833
    /* Leaf certificate is checked in OCSP_verify() */
834
317
    return 1;
835
580
}
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
38.6k
{
953
38.6k
    int ret;
954
955
38.6k
    if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK)
956
34.8k
        return ret;
957
3.81k
    return ossl_x509_signing_allowed(issuer, subject);
958
38.6k
}
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
73.8k
{
963
73.8k
    int ret;
964
965
73.8k
    if (X509_NAME_cmp(X509_get_subject_name(issuer),
966
73.8k
            X509_get_issuer_name(subject))
967
73.8k
        != 0)
968
47.1k
        return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
969
970
    /* set issuer->skid and subject->akid */
971
26.7k
    if (!ossl_x509v3_cache_extensions(issuer)
972
20.7k
        || !ossl_x509v3_cache_extensions(subject))
973
7.31k
        return X509_V_ERR_UNSPECIFIED;
974
975
19.4k
    ret = X509_check_akid(issuer, subject->akid);
976
19.4k
    if (ret != X509_V_OK)
977
2.13k
        return ret;
978
979
    /* Check if the subject signature alg matches the issuer's PUBKEY alg */
980
17.2k
    return check_sig_alg_match(X509_get0_pubkey(issuer), subject);
981
19.4k
}
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.63k
{
992
5.63k
    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.62k
    } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) {
996
108
        return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
997
108
    }
998
5.52k
    return X509_V_OK;
999
5.63k
}
1000
1001
int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid)
1002
198k
{
1003
198k
    if (akid == NULL)
1004
166k
        return X509_V_OK;
1005
1006
    /* Check key ids (if present) */
1007
32.6k
    if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
1008
2.08k
        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.38k
        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
533
        GENERAL_NAMES *gens = akid->issuer;
1020
533
        GENERAL_NAME *gen;
1021
533
        X509_NAME *nm = NULL;
1022
533
        int i;
1023
1024
981
        for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1025
645
            gen = sk_GENERAL_NAME_value(gens, i);
1026
645
            if (gen->type == GEN_DIRNAME) {
1027
197
                nm = gen->d.dirn;
1028
197
                break;
1029
197
            }
1030
645
        }
1031
533
        if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0)
1032
114
            return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1033
533
    }
1034
29.0k
    return X509_V_OK;
1035
29.1k
}
1036
1037
uint32_t X509_get_extension_flags(X509 *x)
1038
183k
{
1039
    /* Call for side-effect of computing hash and caching extensions */
1040
183k
    X509_check_purpose(x, -1, 0);
1041
183k
    return x->ex_flags;
1042
183k
}
1043
1044
uint32_t X509_get_key_usage(X509 *x)
1045
23.3k
{
1046
    /* Call for side-effect of computing hash and caching extensions */
1047
23.3k
    if (X509_check_purpose(x, -1, 0) != 1)
1048
0
        return 0;
1049
23.3k
    return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX;
1050
23.3k
}
1051
1052
uint32_t X509_get_extended_key_usage(X509 *x)
1053
6
{
1054
    /* Call for side-effect of computing hash and caching extensions */
1055
6
    if (X509_check_purpose(x, -1, 0) != 1)
1056
0
        return 0;
1057
6
    return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX;
1058
6
}
1059
1060
const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
1061
5.32k
{
1062
    /* Call for side-effect of computing hash and caching extensions */
1063
5.32k
    if (X509_check_purpose(x, -1, 0) != 1)
1064
439
        return NULL;
1065
4.88k
    return x->skid;
1066
5.32k
}
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
}