Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/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
834
#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
343k
{
88
343k
    int idx;
89
343k
    const X509_PURPOSE *pt;
90
91
343k
    if (!ossl_x509v3_cache_extensions(x))
92
43.8k
        return -1;
93
299k
    if (id == -1)
94
299k
        return 1;
95
96
507
    idx = X509_PURPOSE_get_by_id(id);
97
507
    if (idx == -1)
98
0
        return -1;
99
507
    pt = X509_PURPOSE_get0(idx);
100
507
    return pt->check_purpose(pt, x, non_leaf);
101
507
}
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
69.2k
{
122
69.2k
    if (idx < 0)
123
68.3k
        return NULL;
124
834
    if (idx < (int)X509_PURPOSE_COUNT)
125
834
        return xstandard + idx;
126
0
    return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
127
834
}
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
69.2k
{
145
69.2k
    X509_PURPOSE tmp;
146
69.2k
    int idx;
147
148
69.2k
    if (purpose >= X509_PURPOSE_MIN && purpose <= X509_PURPOSE_MAX)
149
834
        return purpose - X509_PURPOSE_MIN;
150
68.3k
    if (xptable == NULL)
151
68.3k
        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
236k
{
264
236k
    return *a - *b;
265
236k
}
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
101k
{
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
101k
    static const int supported_nids[] = {
280
101k
        NID_netscape_cert_type, /* 71 */
281
101k
        NID_key_usage, /* 83 */
282
101k
        NID_subject_alt_name, /* 85 */
283
101k
        NID_basic_constraints, /* 87 */
284
101k
        NID_certificate_policies, /* 89 */
285
101k
        NID_crl_distribution_points, /* 103 */
286
101k
        NID_ext_key_usage, /* 126 */
287
101k
#ifndef OPENSSL_NO_RFC3779
288
101k
        NID_sbgp_ipAddrBlock, /* 290 */
289
101k
        NID_sbgp_autonomousSysNum, /* 291 */
290
101k
#endif
291
101k
        NID_id_pkix_OCSP_noCheck, /* 369 */
292
101k
        NID_policy_constraints, /* 401 */
293
101k
        NID_proxyCertInfo, /* 663 */
294
101k
        NID_name_constraints, /* 666 */
295
101k
        NID_policy_mappings, /* 747 */
296
101k
        NID_inhibit_any_policy /* 748 */
297
101k
    };
298
299
101k
    int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
300
301
101k
    if (ex_nid == NID_undef)
302
5.28k
        return 0;
303
304
96.4k
    if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
305
95.2k
        return 1;
306
1.23k
    return 0;
307
96.4k
}
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.93k
{
312
1.93k
    const X509_NAME *iname = NULL;
313
1.93k
    int i;
314
315
1.93k
    if (dp->distpoint == NULL && sk_GENERAL_NAME_num(dp->CRLissuer) <= 0) {
316
58
        ERR_raise(ERR_LIB_X509, X509_R_INVALID_DISTPOINT);
317
58
        return 0;
318
58
    }
319
1.87k
    if (dp->reasons != NULL) {
320
31
        if (dp->reasons->length > 0)
321
31
            dp->dp_reasons = dp->reasons->data[0];
322
31
        if (dp->reasons->length > 1)
323
8
            dp->dp_reasons |= (dp->reasons->data[1] << 8);
324
31
        dp->dp_reasons &= CRLDP_ALL_REASONS;
325
1.84k
    } else {
326
1.84k
        dp->dp_reasons = CRLDP_ALL_REASONS;
327
1.84k
    }
328
1.87k
    if (dp->distpoint == NULL || dp->distpoint->type != 1)
329
1.84k
        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
59
    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
33
    if (iname == NULL)
347
31
        iname = X509_get_issuer_name(x);
348
33
    return DIST_POINT_set_dpname(dp->distpoint, iname) ? 1 : -1;
349
1.87k
}
350
351
/* Return 1 on success, 0 if x is invalid, -1 on (internal) error. */
352
static int setup_crldp(X509 *x)
353
214k
{
354
214k
    int i;
355
356
214k
    x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
357
214k
    if (x->crldp == NULL && i != -1)
358
2.05k
        return 0;
359
360
214k
    for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
361
1.93k
        int res = setup_dp(x, sk_DIST_POINT_value(x->crldp, i));
362
363
1.93k
        if (res < 1)
364
71
            return res;
365
1.93k
    }
366
212k
    return 1;
367
212k
}
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
186k
{
372
186k
    int subj_sig_nid;
373
374
186k
    if (issuer_key == NULL)
375
11.0k
        return X509_V_ERR_NO_ISSUER_PUBLIC_KEY;
376
175k
    if (OBJ_find_sigid_algs(OBJ_obj2nid(subject->cert_info.signature.algorithm),
377
175k
            NULL, &subj_sig_nid)
378
175k
        == 0)
379
7.47k
        return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM;
380
168k
    if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid))
381
2.38k
        || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss))
382
167k
        return X509_V_OK;
383
636
    return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH;
384
168k
}
385
386
8.99k
#define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)
387
#define ku_reject(x, usage) \
388
12.5k
    (((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
444k
{
403
444k
    BASIC_CONSTRAINTS *bs;
404
444k
    PROXY_CERT_INFO_EXTENSION *pci;
405
444k
    ASN1_BIT_STRING *usage;
406
444k
    ASN1_BIT_STRING *ns;
407
444k
    EXTENDED_KEY_USAGE *extusage;
408
444k
    int i;
409
444k
    int res;
410
411
444k
#ifdef tsan_ld_acq
412
    /* Fast lock-free check, see end of the function for details. */
413
444k
    if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
414
251k
        return (x->ex_flags & EXFLAG_INVALID) == 0;
415
193k
#endif
416
417
193k
    if (!CRYPTO_THREAD_write_lock(x->lock))
418
0
        return 0;
419
193k
    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
193k
    ERR_set_mark();
425
426
    /* Cache the SHA1 digest of the cert */
427
193k
    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
193k
    if (X509_get_version(x) == X509_VERSION_1)
432
2.45k
        x->ex_flags |= EXFLAG_V1;
433
434
    /* Handle basic constraints */
435
193k
    x->ex_pathlen = -1;
436
193k
    if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL)) != NULL) {
437
165k
        if (bs->ca)
438
28.2k
            x->ex_flags |= EXFLAG_CA;
439
165k
        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
3.04k
            if (bs->pathlen->type == V_ASN1_NEG_INTEGER) {
445
129
                ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN);
446
129
                x->ex_flags |= EXFLAG_INVALID;
447
2.91k
            } else {
448
2.91k
                x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
449
2.91k
            }
450
3.04k
        }
451
165k
        BASIC_CONSTRAINTS_free(bs);
452
165k
        x->ex_flags |= EXFLAG_BCONS;
453
165k
    } else if (i != -1) {
454
5.78k
        x->ex_flags |= EXFLAG_INVALID;
455
5.78k
    }
456
457
    /* Handle proxy certificates */
458
193k
    if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL)) != NULL) {
459
400
        if ((x->ex_flags & EXFLAG_CA) != 0
460
389
            || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
461
285
            || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
462
183
            x->ex_flags |= EXFLAG_INVALID;
463
183
        }
464
400
        if (pci->pcPathLengthConstraint != NULL)
465
180
            x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
466
220
        else
467
220
            x->ex_pcpathlen = -1;
468
400
        PROXY_CERT_INFO_EXTENSION_free(pci);
469
400
        x->ex_flags |= EXFLAG_PROXY;
470
193k
    } else if (i != -1) {
471
214
        x->ex_flags |= EXFLAG_INVALID;
472
214
    }
473
474
    /* Handle (basic) key usage */
475
193k
    if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL)) != NULL) {
476
126k
        x->ex_kusage = 0;
477
126k
        if (usage->length > 0) {
478
126k
            x->ex_kusage = usage->data[0];
479
126k
            if (usage->length > 1)
480
48
                x->ex_kusage |= usage->data[1] << 8;
481
126k
        }
482
126k
        x->ex_flags |= EXFLAG_KUSAGE;
483
126k
        ASN1_BIT_STRING_free(usage);
484
        /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */
485
126k
        if (x->ex_kusage == 0) {
486
1.11k
            ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE);
487
1.11k
            x->ex_flags |= EXFLAG_INVALID;
488
1.11k
        }
489
126k
    } else if (i != -1) {
490
1.44k
        x->ex_flags |= EXFLAG_INVALID;
491
1.44k
    }
492
493
    /* Handle extended key usage */
494
193k
    x->ex_xkusage = 0;
495
193k
    if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL)) != NULL) {
496
129k
        x->ex_flags |= EXFLAG_XKUSAGE;
497
259k
        for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
498
129k
            switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
499
127k
            case NID_server_auth:
500
127k
                x->ex_xkusage |= XKU_SSL_SERVER;
501
127k
                break;
502
123
            case NID_client_auth:
503
123
                x->ex_xkusage |= XKU_SSL_CLIENT;
504
123
                break;
505
116
            case NID_email_protect:
506
116
                x->ex_xkusage |= XKU_SMIME;
507
116
                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
27
            case NID_OCSP_sign:
516
27
                x->ex_xkusage |= XKU_OCSP_SIGN;
517
27
                break;
518
69
            case NID_time_stamp:
519
69
                x->ex_xkusage |= XKU_TIMESTAMP;
520
69
                break;
521
52
            case NID_dvcs:
522
52
                x->ex_xkusage |= XKU_DVCS;
523
52
                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
129k
            }
531
129k
        }
532
129k
        sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
533
129k
    } else if (i != -1) {
534
5.46k
        x->ex_flags |= EXFLAG_INVALID;
535
5.46k
    }
536
537
    /* Handle legacy Netscape extension */
538
193k
    if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL)) != NULL) {
539
20
        if (ns->length > 0)
540
13
            x->ex_nscert = ns->data[0];
541
7
        else
542
7
            x->ex_nscert = 0;
543
20
        x->ex_flags |= EXFLAG_NSCERT;
544
20
        ASN1_BIT_STRING_free(ns);
545
193k
    } else if (i != -1) {
546
25
        x->ex_flags |= EXFLAG_INVALID;
547
25
    }
548
549
    /* Handle subject key identifier and issuer/authority key identifier */
550
193k
    x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
551
193k
    if (x->skid == NULL && i != -1)
552
3.03k
        x->ex_flags |= EXFLAG_INVALID;
553
554
193k
    x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
555
193k
    if (x->akid == NULL && i != -1)
556
6.24k
        x->ex_flags |= EXFLAG_INVALID;
557
558
    /* Check if subject name matches issuer */
559
193k
    if (X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)) == 0) {
560
151k
        x->ex_flags |= EXFLAG_SI; /* Cert is self-issued */
561
151k
        if (X509_check_akid(x, x->akid) == X509_V_OK /* SKID matches AKID */
562
150k
            /* .. and the signature alg matches the PUBKEY alg: */
563
150k
            && check_sig_alg_match(X509_get0_pubkey(x), x) == X509_V_OK)
564
143k
            x->ex_flags |= EXFLAG_SS; /* indicate self-signed */
565
        /* This is very related to ossl_x509_likely_issued(x, x) == X509_V_OK */
566
151k
    }
567
568
    /* Handle subject alternative names and various other extensions */
569
193k
    x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
570
193k
    if (x->altname == NULL && i != -1)
571
3.05k
        x->ex_flags |= EXFLAG_INVALID;
572
193k
    x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
573
193k
    if (x->nc == NULL && i != -1)
574
906
        x->ex_flags |= EXFLAG_INVALID;
575
576
    /* Handle CRL distribution point entries */
577
193k
    res = setup_crldp(x);
578
193k
    if (res == 0)
579
1.88k
        x->ex_flags |= EXFLAG_INVALID;
580
581
193k
#ifndef OPENSSL_NO_RFC3779
582
193k
    x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
583
193k
    if (x->rfc3779_addr == NULL && i != -1)
584
241
        x->ex_flags |= EXFLAG_INVALID;
585
193k
    x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
586
193k
    if (x->rfc3779_asid == NULL && i != -1)
587
115
        x->ex_flags |= EXFLAG_INVALID;
588
193k
#endif
589
991k
    for (i = 0; i < X509_get_ext_count(x); i++) {
590
803k
        X509_EXTENSION *ex = X509_get_ext(x, i);
591
803k
        int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
592
593
803k
        if (nid == NID_freshest_crl)
594
14.4k
            x->ex_flags |= EXFLAG_FRESHEST;
595
803k
        if (!X509_EXTENSION_get_critical(ex))
596
714k
            continue;
597
88.6k
        if (!X509_supported_extension(ex)) {
598
5.28k
            x->ex_flags |= EXFLAG_CRITICAL;
599
5.28k
            break;
600
5.28k
        }
601
83.3k
        switch (nid) {
602
51.4k
        case NID_basic_constraints:
603
51.4k
            x->ex_flags |= EXFLAG_BCONS_CRITICAL;
604
51.4k
            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
292
        case NID_subject_alt_name:
612
292
            x->ex_flags |= EXFLAG_SAN_CRITICAL;
613
292
            break;
614
31.6k
        default:
615
31.6k
            break;
616
83.3k
        }
617
83.3k
    }
618
619
    /* Set x->siginf, ignoring errors due to unsupported algos */
620
193k
    (void)ossl_x509_init_sig_info(x);
621
622
193k
    x->ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */
623
193k
#ifdef tsan_st_rel
624
193k
    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
193k
#endif
631
193k
    ERR_pop_to_mark();
632
633
193k
    if ((x->ex_flags & EXFLAG_INVALID) == 0) {
634
171k
        CRYPTO_THREAD_unlock(x->lock);
635
171k
        return 1;
636
171k
    }
637
21.9k
    CRYPTO_THREAD_unlock(x->lock);
638
21.9k
    ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE);
639
21.9k
    return 0;
640
193k
}
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
6.51k
{
656
    /* keyUsage if present should allow cert signing */
657
6.51k
    if (ku_reject(x, KU_KEY_CERT_SIGN))
658
16
        return 0;
659
6.49k
    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
4.49k
    } else {
663
        /* We support V1 roots for...  uh, I don't really know why. */
664
4.49k
        if ((x->ex_flags & V1_ROOT) == V1_ROOT)
665
89
            return 3;
666
        /*
667
         * If key usage present it must have certSign so tolerate it
668
         */
669
4.40k
        else if ((x->ex_flags & EXFLAG_KUSAGE) != 0)
670
7
            return 4;
671
        /* Older certificates could have Netscape-specific CA types */
672
4.40k
        else if ((x->ex_flags & EXFLAG_NSCERT) != 0
673
10
            && (x->ex_nscert & NS_ANY_CA) != 0)
674
3
            return 5;
675
        /* Can this still be regarded a CA certificate?  I doubt it. */
676
4.39k
        return 0;
677
4.49k
    }
678
6.49k
}
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.1k
{
695
    /* Note 0 normally means "not a CA" - but in this case means error. */
696
11.1k
    if (!ossl_x509v3_cache_extensions(x))
697
2.91k
        return 0;
698
699
8.24k
    return check_ca(x);
700
11.1k
}
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
507
{
827
    /*
828
     * Must be a valid CA.  Should we really support the "I don't know" value
829
     * (2)?
830
     */
831
507
    if (non_leaf)
832
226
        return check_ca(x);
833
    /* Leaf certificate is checked in OCSP_verify() */
834
281
    return 1;
835
507
}
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
39.6k
{
953
39.6k
    int ret;
954
955
39.6k
    if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK)
956
35.0k
        return ret;
957
4.58k
    return ossl_x509_signing_allowed(issuer, subject);
958
39.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
79.2k
{
963
79.2k
    int ret;
964
965
79.2k
    if (X509_NAME_cmp(X509_get_subject_name(issuer),
966
79.2k
            X509_get_issuer_name(subject))
967
79.2k
        != 0)
968
47.2k
        return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
969
970
    /* set issuer->skid and subject->akid */
971
31.9k
    if (!ossl_x509v3_cache_extensions(issuer)
972
24.8k
        || !ossl_x509v3_cache_extensions(subject))
973
9.63k
        return X509_V_ERR_UNSPECIFIED;
974
975
22.3k
    ret = X509_check_akid(issuer, subject->akid);
976
22.3k
    if (ret != X509_V_OK)
977
2.79k
        return ret;
978
979
    /* Check if the subject signature alg matches the issuer's PUBKEY alg */
980
19.5k
    return check_sig_alg_match(X509_get0_pubkey(issuer), subject);
981
22.3k
}
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
6.05k
{
992
6.05k
    if ((subject->ex_flags & EXFLAG_PROXY) != 0) {
993
5
        if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
994
0
            return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
995
6.04k
    } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) {
996
114
        return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
997
114
    }
998
5.93k
    return X509_V_OK;
999
6.05k
}
1000
1001
int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid)
1002
196k
{
1003
196k
    if (akid == NULL)
1004
161k
        return X509_V_OK;
1005
1006
    /* Check key ids (if present) */
1007
34.6k
    if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
1008
2.80k
        return X509_V_ERR_AKID_SKID_MISMATCH;
1009
    /* Check serial number */
1010
31.8k
    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
30.4k
    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
402
        GENERAL_NAMES *gens = akid->issuer;
1020
402
        GENERAL_NAME *gen;
1021
402
        X509_NAME *nm = NULL;
1022
402
        int i;
1023
1024
712
        for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1025
479
            gen = sk_GENERAL_NAME_value(gens, i);
1026
479
            if (gen->type == GEN_DIRNAME) {
1027
169
                nm = gen->d.dirn;
1028
169
                break;
1029
169
            }
1030
479
        }
1031
402
        if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0)
1032
106
            return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1033
402
    }
1034
30.2k
    return X509_V_OK;
1035
30.4k
}
1036
1037
uint32_t X509_get_extension_flags(X509 *x)
1038
181k
{
1039
    /* Call for side-effect of computing hash and caching extensions */
1040
181k
    X509_check_purpose(x, -1, 0);
1041
181k
    return x->ex_flags;
1042
181k
}
1043
1044
uint32_t X509_get_key_usage(X509 *x)
1045
22.6k
{
1046
    /* Call for side-effect of computing hash and caching extensions */
1047
22.6k
    if (X509_check_purpose(x, -1, 0) != 1)
1048
0
        return 0;
1049
22.6k
    return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX;
1050
22.6k
}
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.62k
{
1062
    /* Call for side-effect of computing hash and caching extensions */
1063
5.62k
    if (X509_check_purpose(x, -1, 0) != 1)
1064
715
        return NULL;
1065
4.91k
    return x->skid;
1066
5.62k
}
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
}