Coverage Report

Created: 2025-12-10 06:24

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