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