Coverage Report

Created: 2026-02-22 06:11

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