xref: /freebsd/crypto/openssl/crypto/x509/v3_purp.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
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_X509V3, 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)
417         == 0)
418         return X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM;
419     if (EVP_PKEY_is_a(issuer_key, OBJ_nid2sn(subj_sig_nid))
420         || (EVP_PKEY_is_a(issuer_key, "RSA") && subj_sig_nid == NID_rsassaPss))
421         return X509_V_OK;
422     return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH;
423 }
424 
425 #define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)
426 #define ku_reject(x, usage) \
427     (((x)->ex_flags & EXFLAG_KUSAGE) != 0 && ((x)->ex_kusage & (usage)) == 0)
428 #define xku_reject(x, usage) \
429     (((x)->ex_flags & EXFLAG_XKUSAGE) != 0 && ((x)->ex_xkusage & (usage)) == 0)
430 #define ns_reject(x, usage) \
431     (((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  */
ossl_x509v3_cache_extensions(X509 * x)440 int ossl_x509v3_cache_extensions(X509 *x)
441 {
442     BASIC_CONSTRAINTS *bs;
443     PROXY_CERT_INFO_EXTENSION *pci;
444     ASN1_BIT_STRING *usage;
445     ASN1_BIT_STRING *ns;
446     EXTENDED_KEY_USAGE *extusage;
447     int i;
448     int res;
449 
450 #ifdef tsan_ld_acq
451     /* Fast lock-free check, see end of the function for details. */
452     if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
453         return (x->ex_flags & EXFLAG_INVALID) == 0;
454 #endif
455 
456     if (!CRYPTO_THREAD_write_lock(x->lock))
457         return 0;
458     if ((x->ex_flags & EXFLAG_SET) != 0) { /* Cert has already been processed */
459         CRYPTO_THREAD_unlock(x->lock);
460         return (x->ex_flags & EXFLAG_INVALID) == 0;
461     }
462 
463     ERR_set_mark();
464 
465     /* Cache the SHA1 digest of the cert */
466     if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL))
467         x->ex_flags |= EXFLAG_NO_FINGERPRINT;
468 
469     /* V1 should mean no extensions ... */
470     if (X509_get_version(x) == X509_VERSION_1)
471         x->ex_flags |= EXFLAG_V1;
472 
473     /* Handle basic constraints */
474     x->ex_pathlen = -1;
475     if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL)) != NULL) {
476         if (bs->ca)
477             x->ex_flags |= EXFLAG_CA;
478         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             if (bs->pathlen->type == V_ASN1_NEG_INTEGER) {
484                 ERR_raise(ERR_LIB_X509V3, X509V3_R_NEGATIVE_PATHLEN);
485                 x->ex_flags |= EXFLAG_INVALID;
486             } else {
487                 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
488             }
489         }
490         BASIC_CONSTRAINTS_free(bs);
491         x->ex_flags |= EXFLAG_BCONS;
492     } else if (i != -1) {
493         x->ex_flags |= EXFLAG_INVALID;
494     }
495 
496     /* Handle proxy certificates */
497     if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL)) != NULL) {
498         if ((x->ex_flags & EXFLAG_CA) != 0
499             || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
500             || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
501             x->ex_flags |= EXFLAG_INVALID;
502         }
503         if (pci->pcPathLengthConstraint != NULL)
504             x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
505         else
506             x->ex_pcpathlen = -1;
507         PROXY_CERT_INFO_EXTENSION_free(pci);
508         x->ex_flags |= EXFLAG_PROXY;
509     } else if (i != -1) {
510         x->ex_flags |= EXFLAG_INVALID;
511     }
512 
513     /* Handle (basic) key usage */
514     if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL)) != NULL) {
515         x->ex_kusage = 0;
516         if (usage->length > 0) {
517             x->ex_kusage = usage->data[0];
518             if (usage->length > 1)
519                 x->ex_kusage |= usage->data[1] << 8;
520         }
521         x->ex_flags |= EXFLAG_KUSAGE;
522         ASN1_BIT_STRING_free(usage);
523         /* Check for empty key usage according to RFC 5280 section 4.2.1.3 */
524         if (x->ex_kusage == 0) {
525             ERR_raise(ERR_LIB_X509V3, X509V3_R_EMPTY_KEY_USAGE);
526             x->ex_flags |= EXFLAG_INVALID;
527         }
528     } else if (i != -1) {
529         x->ex_flags |= EXFLAG_INVALID;
530     }
531 
532     /* Handle extended key usage */
533     x->ex_xkusage = 0;
534     if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL)) != NULL) {
535         x->ex_flags |= EXFLAG_XKUSAGE;
536         for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
537             switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
538             case NID_server_auth:
539                 x->ex_xkusage |= XKU_SSL_SERVER;
540                 break;
541             case NID_client_auth:
542                 x->ex_xkusage |= XKU_SSL_CLIENT;
543                 break;
544             case NID_email_protect:
545                 x->ex_xkusage |= XKU_SMIME;
546                 break;
547             case NID_code_sign:
548                 x->ex_xkusage |= XKU_CODE_SIGN;
549                 break;
550             case NID_ms_sgc:
551             case NID_ns_sgc:
552                 x->ex_xkusage |= XKU_SGC;
553                 break;
554             case NID_OCSP_sign:
555                 x->ex_xkusage |= XKU_OCSP_SIGN;
556                 break;
557             case NID_time_stamp:
558                 x->ex_xkusage |= XKU_TIMESTAMP;
559                 break;
560             case NID_dvcs:
561                 x->ex_xkusage |= XKU_DVCS;
562                 break;
563             case NID_anyExtendedKeyUsage:
564                 x->ex_xkusage |= XKU_ANYEKU;
565                 break;
566             default:
567                 /* Ignore unknown extended key usage */
568                 break;
569             }
570         }
571         sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
572     } else if (i != -1) {
573         x->ex_flags |= EXFLAG_INVALID;
574     }
575 
576     /* Handle legacy Netscape extension */
577     if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL)) != NULL) {
578         if (ns->length > 0)
579             x->ex_nscert = ns->data[0];
580         else
581             x->ex_nscert = 0;
582         x->ex_flags |= EXFLAG_NSCERT;
583         ASN1_BIT_STRING_free(ns);
584     } else if (i != -1) {
585         x->ex_flags |= EXFLAG_INVALID;
586     }
587 
588     /* Handle subject key identifier and issuer/authority key identifier */
589     x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
590     if (x->skid == NULL && i != -1)
591         x->ex_flags |= EXFLAG_INVALID;
592 
593     x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
594     if (x->akid == NULL && i != -1)
595         x->ex_flags |= EXFLAG_INVALID;
596 
597     /* Check if subject name matches issuer */
598     if (X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)) == 0) {
599         x->ex_flags |= EXFLAG_SI; /* Cert is self-issued */
600         if (X509_check_akid(x, x->akid) == X509_V_OK /* SKID matches AKID */
601             /* .. and the signature alg matches the PUBKEY alg: */
602             && check_sig_alg_match(X509_get0_pubkey(x), x) == X509_V_OK)
603             x->ex_flags |= EXFLAG_SS; /* indicate self-signed */
604         /* This is very related to ossl_x509_likely_issued(x, x) == X509_V_OK */
605     }
606 
607     /* Handle subject alternative names and various other extensions */
608     x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
609     if (x->altname == NULL && i != -1)
610         x->ex_flags |= EXFLAG_INVALID;
611     x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
612     if (x->nc == NULL && i != -1)
613         x->ex_flags |= EXFLAG_INVALID;
614 
615     /* Handle CRL distribution point entries */
616     res = setup_crldp(x);
617     if (res == 0)
618         x->ex_flags |= EXFLAG_INVALID;
619 
620 #ifndef OPENSSL_NO_RFC3779
621     x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
622     if (x->rfc3779_addr == NULL && i != -1)
623         x->ex_flags |= EXFLAG_INVALID;
624     x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
625     if (x->rfc3779_asid == NULL && i != -1)
626         x->ex_flags |= EXFLAG_INVALID;
627 #endif
628     for (i = 0; i < X509_get_ext_count(x); i++) {
629         X509_EXTENSION *ex = X509_get_ext(x, i);
630         int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
631 
632         if (nid == NID_freshest_crl)
633             x->ex_flags |= EXFLAG_FRESHEST;
634         if (!X509_EXTENSION_get_critical(ex))
635             continue;
636         if (!X509_supported_extension(ex)) {
637             x->ex_flags |= EXFLAG_CRITICAL;
638             break;
639         }
640         switch (nid) {
641         case NID_basic_constraints:
642             x->ex_flags |= EXFLAG_BCONS_CRITICAL;
643             break;
644         case NID_authority_key_identifier:
645             x->ex_flags |= EXFLAG_AKID_CRITICAL;
646             break;
647         case NID_subject_key_identifier:
648             x->ex_flags |= EXFLAG_SKID_CRITICAL;
649             break;
650         case NID_subject_alt_name:
651             x->ex_flags |= EXFLAG_SAN_CRITICAL;
652             break;
653         default:
654             break;
655         }
656     }
657 
658     /* Set x->siginf, ignoring errors due to unsupported algos */
659     (void)ossl_x509_init_sig_info(x);
660 
661     x->ex_flags |= EXFLAG_SET; /* Indicate that cert has been processed */
662 #ifdef tsan_st_rel
663     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 #endif
670     ERR_pop_to_mark();
671 
672     if ((x->ex_flags & EXFLAG_INVALID) == 0) {
673         CRYPTO_THREAD_unlock(x->lock);
674         return 1;
675     }
676     CRYPTO_THREAD_unlock(x->lock);
677     ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_CERTIFICATE);
678     return 0;
679 }
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 
check_ca(const X509 * x)693 static int check_ca(const X509 *x)
694 {
695     /* keyUsage if present should allow cert signing */
696     if (ku_reject(x, KU_KEY_CERT_SIGN))
697         return 0;
698     if ((x->ex_flags & EXFLAG_BCONS) != 0) {
699         /* If basicConstraints says not a CA then say so */
700         return (x->ex_flags & EXFLAG_CA) != 0;
701     } else {
702         /* We support V1 roots for...  uh, I don't really know why. */
703         if ((x->ex_flags & V1_ROOT) == V1_ROOT)
704             return 3;
705         /*
706          * If key usage present it must have certSign so tolerate it
707          */
708         else if ((x->ex_flags & EXFLAG_KUSAGE) != 0)
709             return 4;
710         /* Older certificates could have Netscape-specific CA types */
711         else if ((x->ex_flags & EXFLAG_NSCERT) != 0
712             && (x->ex_nscert & NS_ANY_CA) != 0)
713             return 5;
714         /* Can this still be regarded a CA certificate?  I doubt it. */
715         return 0;
716     }
717 }
718 
X509_set_proxy_flag(X509 * x)719 void X509_set_proxy_flag(X509 *x)
720 {
721     if (CRYPTO_THREAD_write_lock(x->lock)) {
722         x->ex_flags |= EXFLAG_PROXY;
723         CRYPTO_THREAD_unlock(x->lock);
724     }
725 }
726 
X509_set_proxy_pathlen(X509 * x,long l)727 void X509_set_proxy_pathlen(X509 *x, long l)
728 {
729     x->ex_pcpathlen = l;
730 }
731 
X509_check_ca(X509 * x)732 int X509_check_ca(X509 *x)
733 {
734     /* Note 0 normally means "not a CA" - but in this case means error. */
735     if (!ossl_x509v3_cache_extensions(x))
736         return 0;
737 
738     return check_ca(x);
739 }
740 
741 /* Check SSL CA: common checks for SSL client and server. */
check_ssl_ca(const X509 * x)742 static int check_ssl_ca(const X509 *x)
743 {
744     int ca_ret = check_ca(x);
745 
746     if (ca_ret == 0)
747         return 0;
748     /* Check nsCertType if present */
749     return ca_ret != 5 || (x->ex_nscert & NS_SSL_CA) != 0;
750 }
751 
check_purpose_ssl_client(const X509_PURPOSE * xp,const X509 * x,int non_leaf)752 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
753     int non_leaf)
754 {
755     if (xku_reject(x, XKU_SSL_CLIENT))
756         return 0;
757     if (non_leaf)
758         return check_ssl_ca(x);
759     /* We need to do digital signatures or key agreement */
760     if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
761         return 0;
762     /* nsCertType if present should allow SSL client use */
763     if (ns_reject(x, NS_SSL_CLIENT))
764         return 0;
765     return 1;
766 }
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 
check_purpose_ssl_server(const X509_PURPOSE * xp,const X509 * x,int non_leaf)776 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
777     int non_leaf)
778 {
779     if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
780         return 0;
781     if (non_leaf)
782         return check_ssl_ca(x);
783 
784     if (ns_reject(x, NS_SSL_SERVER))
785         return 0;
786     if (ku_reject(x, KU_TLS))
787         return 0;
788 
789     return 1;
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)) || !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
904         return 0;
905 
906     /* Only timestamp key usage is permitted and it's required. */
907     if ((x->ex_flags & EXFLAG_XKUSAGE) == 0 || x->ex_xkusage != XKU_TIMESTAMP)
908         return 0;
909 
910     /* Extended Key Usage MUST be critical */
911     i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
912     if (i_ext >= 0
913         && !X509_EXTENSION_get_critical(X509_get_ext((X509 *)x, i_ext)))
914         return 0;
915     return 1;
916 }
917 
check_purpose_code_sign(const X509_PURPOSE * xp,const X509 * x,int non_leaf)918 static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *x,
919     int non_leaf)
920 {
921     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     if (non_leaf)
928         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     if ((x->ex_flags & EXFLAG_KUSAGE) == 0)
944         return 0;
945     if ((x->ex_kusage & KU_DIGITAL_SIGNATURE) == 0)
946         return 0;
947     if ((x->ex_kusage & (KU_KEY_CERT_SIGN | KU_CRL_SIGN)) != 0)
948         return 0;
949 
950     /* Key Usage MUST be critical */
951     i_ext = X509_get_ext_by_NID(x, NID_key_usage, -1);
952     if (i_ext < 0)
953         return 0;
954     if (i_ext >= 0) {
955         X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
956         if (!X509_EXTENSION_get_critical(ext))
957             return 0;
958     }
959 
960     /* Extended Key Usage */
961     if ((x->ex_flags & EXFLAG_XKUSAGE) == 0)
962         return 0;
963     if ((x->ex_xkusage & XKU_CODE_SIGN) == 0)
964         return 0;
965     if ((x->ex_xkusage & (XKU_ANYEKU | XKU_SSL_SERVER)) != 0)
966         return 0;
967 
968     return 1;
969 }
970 
no_check_purpose(const X509_PURPOSE * xp,const X509 * x,int non_leaf)971 static int no_check_purpose(const X509_PURPOSE *xp, const X509 *x,
972     int non_leaf)
973 {
974     return 1;
975 }
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  */
X509_check_issued(X509 * issuer,X509 * subject)990 int X509_check_issued(X509 *issuer, X509 *subject)
991 {
992     int ret;
993 
994     if ((ret = ossl_x509_likely_issued(issuer, subject)) != X509_V_OK)
995         return ret;
996     return ossl_x509_signing_allowed(issuer, subject);
997 }
998 
999 /* do the checks 1., 2., and 3. as described above for X509_check_issued() */
ossl_x509_likely_issued(X509 * issuer,X509 * subject)1000 int ossl_x509_likely_issued(X509 *issuer, X509 *subject)
1001 {
1002     int ret;
1003 
1004     if (X509_NAME_cmp(X509_get_subject_name(issuer),
1005             X509_get_issuer_name(subject))
1006         != 0)
1007         return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
1008 
1009     /* set issuer->skid and subject->akid */
1010     if (!ossl_x509v3_cache_extensions(issuer)
1011         || !ossl_x509v3_cache_extensions(subject))
1012         return X509_V_ERR_UNSPECIFIED;
1013 
1014     ret = X509_check_akid(issuer, subject->akid);
1015     if (ret != X509_V_OK)
1016         return ret;
1017 
1018     /* Check if the subject signature alg matches the issuer's PUBKEY alg */
1019     return check_sig_alg_match(X509_get0_pubkey(issuer), subject);
1020 }
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  */
ossl_x509_signing_allowed(const X509 * issuer,const X509 * subject)1029 int ossl_x509_signing_allowed(const X509 *issuer, const X509 *subject)
1030 {
1031     if ((subject->ex_flags & EXFLAG_PROXY) != 0) {
1032         if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
1033             return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
1034     } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) {
1035         return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
1036     }
1037     return X509_V_OK;
1038 }
1039 
X509_check_akid(const X509 * issuer,const AUTHORITY_KEYID * akid)1040 int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid)
1041 {
1042     if (akid == NULL)
1043         return X509_V_OK;
1044 
1045     /* Check key ids (if present) */
1046     if (akid->keyid && issuer->skid && ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
1047         return X509_V_ERR_AKID_SKID_MISMATCH;
1048     /* Check serial number */
1049     if (akid->serial && ASN1_INTEGER_cmp(X509_get0_serialNumber(issuer), akid->serial))
1050         return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1051     /* Check issuer name */
1052     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         GENERAL_NAMES *gens = akid->issuer;
1059         GENERAL_NAME *gen;
1060         X509_NAME *nm = NULL;
1061         int i;
1062 
1063         for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1064             gen = sk_GENERAL_NAME_value(gens, i);
1065             if (gen->type == GEN_DIRNAME) {
1066                 nm = gen->d.dirn;
1067                 break;
1068             }
1069         }
1070         if (nm != NULL && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)) != 0)
1071             return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
1072     }
1073     return X509_V_OK;
1074 }
1075 
X509_get_extension_flags(X509 * x)1076 uint32_t X509_get_extension_flags(X509 *x)
1077 {
1078     /* Call for side-effect of computing hash and caching extensions */
1079     X509_check_purpose(x, -1, 0);
1080     return x->ex_flags;
1081 }
1082 
X509_get_key_usage(X509 * x)1083 uint32_t X509_get_key_usage(X509 *x)
1084 {
1085     /* Call for side-effect of computing hash and caching extensions */
1086     if (X509_check_purpose(x, -1, 0) != 1)
1087         return 0;
1088     return (x->ex_flags & EXFLAG_KUSAGE) != 0 ? x->ex_kusage : UINT32_MAX;
1089 }
1090 
X509_get_extended_key_usage(X509 * x)1091 uint32_t X509_get_extended_key_usage(X509 *x)
1092 {
1093     /* Call for side-effect of computing hash and caching extensions */
1094     if (X509_check_purpose(x, -1, 0) != 1)
1095         return 0;
1096     return (x->ex_flags & EXFLAG_XKUSAGE) != 0 ? x->ex_xkusage : UINT32_MAX;
1097 }
1098 
X509_get0_subject_key_id(X509 * x)1099 const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
1100 {
1101     /* Call for side-effect of computing hash and caching extensions */
1102     if (X509_check_purpose(x, -1, 0) != 1)
1103         return NULL;
1104     return x->skid;
1105 }
1106 
X509_get0_authority_key_id(X509 * x)1107 const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
1108 {
1109     /* Call for side-effect of computing hash and caching extensions */
1110     if (X509_check_purpose(x, -1, 0) != 1)
1111         return NULL;
1112     return (x->akid != NULL ? x->akid->keyid : NULL);
1113 }
1114 
X509_get0_authority_issuer(X509 * x)1115 const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
1116 {
1117     /* Call for side-effect of computing hash and caching extensions */
1118     if (X509_check_purpose(x, -1, 0) != 1)
1119         return NULL;
1120     return (x->akid != NULL ? x->akid->issuer : NULL);
1121 }
1122 
X509_get0_authority_serial(X509 * x)1123 const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
1124 {
1125     /* Call for side-effect of computing hash and caching extensions */
1126     if (X509_check_purpose(x, -1, 0) != 1)
1127         return NULL;
1128     return (x->akid != NULL ? x->akid->serial : NULL);
1129 }
1130 
X509_get_pathlen(X509 * x)1131 long X509_get_pathlen(X509 *x)
1132 {
1133     /* Called for side effect of caching extensions */
1134     if (X509_check_purpose(x, -1, 0) != 1
1135         || (x->ex_flags & EXFLAG_BCONS) == 0)
1136         return -1;
1137     return x->ex_pathlen;
1138 }
1139 
X509_get_proxy_pathlen(X509 * x)1140 long X509_get_proxy_pathlen(X509 *x)
1141 {
1142     /* Called for side effect of caching extensions */
1143     if (X509_check_purpose(x, -1, 0) != 1
1144         || (x->ex_flags & EXFLAG_PROXY) == 0)
1145         return -1;
1146     return x->ex_pcpathlen;
1147 }
1148