xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/x509/v3_purp.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /*
2  * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project
3  * 2001.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    [email protected].
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * ([email protected]).  This product includes software written by Tim
55  * Hudson ([email protected]). */
56 
57 #include <string.h>
58 
59 #include <openssl/digest.h>
60 #include <openssl/err.h>
61 #include <openssl/mem.h>
62 #include <openssl/obj.h>
63 #include <openssl/thread.h>
64 #include <openssl/x509.h>
65 
66 #include "../internal.h"
67 #include "internal.h"
68 
69 
70 struct x509_purpose_st {
71   int purpose;
72   int trust;  // Default trust ID
73   int (*check_purpose)(const struct x509_purpose_st *, const X509 *, int);
74   const char *sname;
75 } /* X509_PURPOSE */;
76 
77 #define V1_ROOT (EXFLAG_V1 | EXFLAG_SS)
78 #define ku_reject(x, usage) \
79   (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
80 #define xku_reject(x, usage) \
81   (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
82 
83 static int check_ca(const X509 *x);
84 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
85                                     int ca);
86 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
87                                     int ca);
88 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
89                                        int ca);
90 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
91                                     int ca);
92 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
93                                        int ca);
94 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
95                                   int ca);
96 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
97                                         int ca);
98 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
99 
100 // X509_TRUST_NONE is not a valid |X509_TRUST_*| constant. It is used by
101 // |X509_PURPOSE_ANY| to indicate that it has no corresponding trust type and
102 // cannot be used with |X509_STORE_CTX_set_purpose|.
103 #define X509_TRUST_NONE (-1)
104 
105 static const X509_PURPOSE xstandard[] = {
106     {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, check_purpose_ssl_client,
107      "sslclient"},
108     {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, check_purpose_ssl_server,
109      "sslserver"},
110     {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER,
111      check_purpose_ns_ssl_server, "nssslserver"},
112     {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, check_purpose_smime_sign,
113      "smimesign"},
114     {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, check_purpose_smime_encrypt,
115      "smimeencrypt"},
116     {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, check_purpose_crl_sign,
117      "crlsign"},
118     {X509_PURPOSE_ANY, X509_TRUST_NONE, no_check, "any"},
119     // |X509_PURPOSE_OCSP_HELPER| performs no actual checks. OpenSSL's OCSP
120     // implementation relied on the caller performing EKU and KU checks.
121     {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, no_check, "ocsphelper"},
122     {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, check_purpose_timestamp_sign,
123      "timestampsign"},
124 };
125 
X509_check_purpose(X509 * x,int id,int ca)126 int X509_check_purpose(X509 *x, int id, int ca) {
127   // This differs from OpenSSL, which uses -1 to indicate a fatal error and 0 to
128   // indicate an invalid certificate. BoringSSL uses 0 for both.
129   if (!x509v3_cache_extensions(x)) {
130     return 0;
131   }
132 
133   if (id == -1) {
134     return 1;
135   }
136   const X509_PURPOSE *pt = X509_PURPOSE_get0(id);
137   if (pt == NULL) {
138     return 0;
139   }
140   // Historically, |check_purpose| implementations other than |X509_PURPOSE_ANY|
141   // called |check_ca|. This is redundant with the |X509_V_ERR_INVALID_CA|
142   // logic, but |X509_check_purpose| is public API, so we preserve this
143   // behavior.
144   if (ca && id != X509_PURPOSE_ANY && !check_ca(x)) {
145     return 0;
146   }
147   return pt->check_purpose(pt, x, ca);
148 }
149 
X509_PURPOSE_get0(int id)150 const X509_PURPOSE *X509_PURPOSE_get0(int id) {
151   for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(xstandard); i++) {
152     if (xstandard[i].purpose == id) {
153       return &xstandard[i];
154     }
155   }
156   return NULL;
157 }
158 
X509_PURPOSE_get_by_sname(const char * sname)159 int X509_PURPOSE_get_by_sname(const char *sname) {
160   for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(xstandard); i++) {
161     if (strcmp(xstandard[i].sname, sname) == 0) {
162       return xstandard[i].purpose;
163     }
164   }
165   return -1;
166 }
167 
X509_PURPOSE_get_id(const X509_PURPOSE * xp)168 int X509_PURPOSE_get_id(const X509_PURPOSE *xp) { return xp->purpose; }
169 
X509_PURPOSE_get_trust(const X509_PURPOSE * xp)170 int X509_PURPOSE_get_trust(const X509_PURPOSE *xp) { return xp->trust; }
171 
X509_supported_extension(const X509_EXTENSION * ex)172 int X509_supported_extension(const X509_EXTENSION *ex) {
173   int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
174   return nid == NID_key_usage ||             //
175          nid == NID_subject_alt_name ||      //
176          nid == NID_basic_constraints ||     //
177          nid == NID_certificate_policies ||  //
178          nid == NID_ext_key_usage ||         //
179          nid == NID_policy_constraints ||    //
180          nid == NID_name_constraints ||      //
181          nid == NID_policy_mappings ||       //
182          nid == NID_inhibit_any_policy;
183 }
184 
setup_dp(X509 * x,DIST_POINT * dp)185 static int setup_dp(X509 *x, DIST_POINT *dp) {
186   if (!dp->distpoint || (dp->distpoint->type != 1)) {
187     return 1;
188   }
189   X509_NAME *iname = NULL;
190   for (size_t i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
191     GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
192     if (gen->type == GEN_DIRNAME) {
193       iname = gen->d.directoryName;
194       break;
195     }
196   }
197   if (!iname) {
198     iname = X509_get_issuer_name(x);
199   }
200 
201   return DIST_POINT_set_dpname(dp->distpoint, iname);
202 }
203 
setup_crldp(X509 * x)204 static int setup_crldp(X509 *x) {
205   int j;
206   x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &j, NULL);
207   if (x->crldp == NULL && j != -1) {
208     return 0;
209   }
210   for (size_t i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
211     if (!setup_dp(x, sk_DIST_POINT_value(x->crldp, i))) {
212       return 0;
213     }
214   }
215   return 1;
216 }
217 
x509v3_cache_extensions(X509 * x)218 int x509v3_cache_extensions(X509 *x) {
219   BASIC_CONSTRAINTS *bs;
220   ASN1_BIT_STRING *usage;
221   EXTENDED_KEY_USAGE *extusage;
222   size_t i;
223   int j;
224 
225   CRYPTO_MUTEX_lock_read(&x->lock);
226   const int is_set = x->ex_flags & EXFLAG_SET;
227   CRYPTO_MUTEX_unlock_read(&x->lock);
228 
229   if (is_set) {
230     return (x->ex_flags & EXFLAG_INVALID) == 0;
231   }
232 
233   CRYPTO_MUTEX_lock_write(&x->lock);
234   if (x->ex_flags & EXFLAG_SET) {
235     CRYPTO_MUTEX_unlock_write(&x->lock);
236     return (x->ex_flags & EXFLAG_INVALID) == 0;
237   }
238 
239   if (!X509_digest(x, EVP_sha256(), x->cert_hash, NULL)) {
240     x->ex_flags |= EXFLAG_INVALID;
241   }
242   // V1 should mean no extensions ...
243   if (X509_get_version(x) == X509_VERSION_1) {
244     x->ex_flags |= EXFLAG_V1;
245   }
246   // Handle basic constraints
247   if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &j, NULL))) {
248     if (bs->ca) {
249       x->ex_flags |= EXFLAG_CA;
250     }
251     if (bs->pathlen) {
252       if ((bs->pathlen->type == V_ASN1_NEG_INTEGER) || !bs->ca) {
253         x->ex_flags |= EXFLAG_INVALID;
254         x->ex_pathlen = 0;
255       } else {
256         // TODO(davidben): |ASN1_INTEGER_get| returns -1 on overflow,
257         // which currently acts as if the constraint isn't present. This
258         // works (an overflowing path length constraint may as well be
259         // infinity), but Chromium's verifier simply treats values above
260         // 255 as an error.
261         x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
262       }
263     } else {
264       x->ex_pathlen = -1;
265     }
266     BASIC_CONSTRAINTS_free(bs);
267     x->ex_flags |= EXFLAG_BCONS;
268   } else if (j != -1) {
269     x->ex_flags |= EXFLAG_INVALID;
270   }
271   // Handle key usage
272   if ((usage = X509_get_ext_d2i(x, NID_key_usage, &j, NULL))) {
273     if (usage->length > 0) {
274       x->ex_kusage = usage->data[0];
275       if (usage->length > 1) {
276         x->ex_kusage |= usage->data[1] << 8;
277       }
278     } else {
279       x->ex_kusage = 0;
280     }
281     x->ex_flags |= EXFLAG_KUSAGE;
282     ASN1_BIT_STRING_free(usage);
283   } else if (j != -1) {
284     x->ex_flags |= EXFLAG_INVALID;
285   }
286   x->ex_xkusage = 0;
287   if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &j, NULL))) {
288     x->ex_flags |= EXFLAG_XKUSAGE;
289     for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
290       switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
291         case NID_server_auth:
292           x->ex_xkusage |= XKU_SSL_SERVER;
293           break;
294 
295         case NID_client_auth:
296           x->ex_xkusage |= XKU_SSL_CLIENT;
297           break;
298 
299         case NID_email_protect:
300           x->ex_xkusage |= XKU_SMIME;
301           break;
302 
303         case NID_code_sign:
304           x->ex_xkusage |= XKU_CODE_SIGN;
305           break;
306 
307         case NID_ms_sgc:
308         case NID_ns_sgc:
309           x->ex_xkusage |= XKU_SGC;
310           break;
311 
312         case NID_OCSP_sign:
313           x->ex_xkusage |= XKU_OCSP_SIGN;
314           break;
315 
316         case NID_time_stamp:
317           x->ex_xkusage |= XKU_TIMESTAMP;
318           break;
319 
320         case NID_dvcs:
321           x->ex_xkusage |= XKU_DVCS;
322           break;
323 
324         case NID_anyExtendedKeyUsage:
325           x->ex_xkusage |= XKU_ANYEKU;
326           break;
327       }
328     }
329     sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
330   } else if (j != -1) {
331     x->ex_flags |= EXFLAG_INVALID;
332   }
333 
334   x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &j, NULL);
335   if (x->skid == NULL && j != -1) {
336     x->ex_flags |= EXFLAG_INVALID;
337   }
338   x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &j, NULL);
339   if (x->akid == NULL && j != -1) {
340     x->ex_flags |= EXFLAG_INVALID;
341   }
342   // Does subject name match issuer ?
343   if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
344     x->ex_flags |= EXFLAG_SI;
345     // If SKID matches AKID also indicate self signed
346     if (X509_check_akid(x, x->akid) == X509_V_OK &&
347         !ku_reject(x, X509v3_KU_KEY_CERT_SIGN)) {
348       x->ex_flags |= EXFLAG_SS;
349     }
350   }
351   x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &j, NULL);
352   if (x->altname == NULL && j != -1) {
353     x->ex_flags |= EXFLAG_INVALID;
354   }
355   x->nc = X509_get_ext_d2i(x, NID_name_constraints, &j, NULL);
356   if (x->nc == NULL && j != -1) {
357     x->ex_flags |= EXFLAG_INVALID;
358   }
359   if (!setup_crldp(x)) {
360     x->ex_flags |= EXFLAG_INVALID;
361   }
362 
363   for (j = 0; j < X509_get_ext_count(x); j++) {
364     const X509_EXTENSION *ex = X509_get_ext(x, j);
365     if (!X509_EXTENSION_get_critical(ex)) {
366       continue;
367     }
368     if (!X509_supported_extension(ex)) {
369       x->ex_flags |= EXFLAG_CRITICAL;
370       break;
371     }
372   }
373   x->ex_flags |= EXFLAG_SET;
374 
375   CRYPTO_MUTEX_unlock_write(&x->lock);
376   return (x->ex_flags & EXFLAG_INVALID) == 0;
377 }
378 
379 // check_ca returns one if |x| should be considered a CA certificate and zero
380 // otherwise.
check_ca(const X509 * x)381 static int check_ca(const X509 *x) {
382   // keyUsage if present should allow cert signing
383   if (ku_reject(x, X509v3_KU_KEY_CERT_SIGN)) {
384     return 0;
385   }
386   // Version 1 certificates are considered CAs and don't have extensions.
387   if ((x->ex_flags & V1_ROOT) == V1_ROOT) {
388     return 1;
389   }
390   // Otherwise, it's only a CA if basicConstraints says so.
391   return ((x->ex_flags & EXFLAG_BCONS) && (x->ex_flags & EXFLAG_CA));
392 }
393 
X509_check_ca(X509 * x)394 int X509_check_ca(X509 *x) {
395   if (!x509v3_cache_extensions(x)) {
396     return 0;
397   }
398   return check_ca(x);
399 }
400 
401 // check_purpose returns one if |x| is a valid part of a certificate path for
402 // extended key usage |required_xku| and at least one of key usages in
403 // |required_kus|. |ca| indicates whether |x| is a CA or end-entity certificate.
check_purpose(const X509 * x,int ca,int required_xku,int required_kus)404 static int check_purpose(const X509 *x, int ca, int required_xku,
405                          int required_kus) {
406   // Check extended key usage on the entire chain.
407   if (required_xku != 0 && xku_reject(x, required_xku)) {
408     return 0;
409   }
410 
411   // Check key usages only on the end-entity certificate.
412   return ca || !ku_reject(x, required_kus);
413 }
414 
check_purpose_ssl_client(const X509_PURPOSE * xp,const X509 * x,int ca)415 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
416                                     int ca) {
417   // We need to do digital signatures or key agreement.
418   //
419   // TODO(davidben): We do not implement any TLS client certificate modes based
420   // on key agreement.
421   return check_purpose(x, ca, XKU_SSL_CLIENT,
422                        X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_AGREEMENT);
423 }
424 
425 // Key usage needed for TLS/SSL server: digital signature, encipherment or
426 // key agreement. The ssl code can check this more thoroughly for individual
427 // key types.
428 #define X509v3_KU_TLS                                         \
429   (X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_ENCIPHERMENT | \
430    X509v3_KU_KEY_AGREEMENT)
431 
check_purpose_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)432 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
433                                     int ca) {
434   return check_purpose(x, ca, XKU_SSL_SERVER, X509v3_KU_TLS);
435 }
436 
check_purpose_ns_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)437 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
438                                        int ca) {
439   // We need to encipher or Netscape complains.
440   return check_purpose(x, ca, XKU_SSL_SERVER, X509v3_KU_KEY_ENCIPHERMENT);
441 }
442 
check_purpose_smime_sign(const X509_PURPOSE * xp,const X509 * x,int ca)443 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
444                                     int ca) {
445   return check_purpose(x, ca, XKU_SMIME,
446                        X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_NON_REPUDIATION);
447 }
448 
check_purpose_smime_encrypt(const X509_PURPOSE * xp,const X509 * x,int ca)449 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
450                                        int ca) {
451   return check_purpose(x, ca, XKU_SMIME, X509v3_KU_KEY_ENCIPHERMENT);
452 }
453 
check_purpose_crl_sign(const X509_PURPOSE * xp,const X509 * x,int ca)454 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
455                                   int ca) {
456   return check_purpose(x, ca, /*required_xku=*/0, X509v3_KU_CRL_SIGN);
457 }
458 
check_purpose_timestamp_sign(const X509_PURPOSE * xp,const X509 * x,int ca)459 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
460                                         int ca) {
461   if (ca) {
462     return 1;
463   }
464 
465   // Check the optional key usage field:
466   // if Key Usage is present, it must be one of digitalSignature
467   // and/or nonRepudiation (other values are not consistent and shall
468   // be rejected).
469   if ((x->ex_flags & EXFLAG_KUSAGE) &&
470       ((x->ex_kusage &
471         ~(X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)) ||
472        !(x->ex_kusage &
473          (X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)))) {
474     return 0;
475   }
476 
477   // Only time stamp key usage is permitted and it's required.
478   //
479   // TODO(davidben): Should we check EKUs up the chain like the other cases?
480   if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP) {
481     return 0;
482   }
483 
484   // Extended Key Usage MUST be critical
485   int i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
486   if (i_ext >= 0) {
487     const X509_EXTENSION *ext = X509_get_ext(x, i_ext);
488     if (!X509_EXTENSION_get_critical(ext)) {
489       return 0;
490     }
491   }
492 
493   return 1;
494 }
495 
no_check(const X509_PURPOSE * xp,const X509 * x,int ca)496 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca) { return 1; }
497 
X509_check_issued(X509 * issuer,X509 * subject)498 int X509_check_issued(X509 *issuer, X509 *subject) {
499   if (X509_NAME_cmp(X509_get_subject_name(issuer),
500                     X509_get_issuer_name(subject))) {
501     return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
502   }
503   if (!x509v3_cache_extensions(issuer) || !x509v3_cache_extensions(subject)) {
504     return X509_V_ERR_UNSPECIFIED;
505   }
506 
507   if (subject->akid) {
508     int ret = X509_check_akid(issuer, subject->akid);
509     if (ret != X509_V_OK) {
510       return ret;
511     }
512   }
513 
514   if (ku_reject(issuer, X509v3_KU_KEY_CERT_SIGN)) {
515     return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
516   }
517   return X509_V_OK;
518 }
519 
X509_check_akid(X509 * issuer,const AUTHORITY_KEYID * akid)520 int X509_check_akid(X509 *issuer, const AUTHORITY_KEYID *akid) {
521   if (!akid) {
522     return X509_V_OK;
523   }
524 
525   // Check key ids (if present)
526   if (akid->keyid && issuer->skid &&
527       ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid)) {
528     return X509_V_ERR_AKID_SKID_MISMATCH;
529   }
530   // Check serial number
531   if (akid->serial &&
532       ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial)) {
533     return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
534   }
535   // Check issuer name
536   if (akid->issuer) {
537     // Ugh, for some peculiar reason AKID includes SEQUENCE OF
538     // GeneralName. So look for a DirName. There may be more than one but
539     // we only take any notice of the first.
540     GENERAL_NAMES *gens;
541     GENERAL_NAME *gen;
542     X509_NAME *nm = NULL;
543     size_t i;
544     gens = akid->issuer;
545     for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
546       gen = sk_GENERAL_NAME_value(gens, i);
547       if (gen->type == GEN_DIRNAME) {
548         nm = gen->d.dirn;
549         break;
550       }
551     }
552     if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer))) {
553       return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
554     }
555   }
556   return X509_V_OK;
557 }
558 
X509_get_extension_flags(X509 * x)559 uint32_t X509_get_extension_flags(X509 *x) {
560   // Ignore the return value. On failure, |x->ex_flags| will include
561   // |EXFLAG_INVALID|.
562   x509v3_cache_extensions(x);
563   return x->ex_flags;
564 }
565 
X509_get_key_usage(X509 * x)566 uint32_t X509_get_key_usage(X509 *x) {
567   if (!x509v3_cache_extensions(x)) {
568     return 0;
569   }
570   if (x->ex_flags & EXFLAG_KUSAGE) {
571     return x->ex_kusage;
572   }
573   // If there is no extension, key usage is unconstrained, so set all bits to
574   // one. Note that, although we use |UINT32_MAX|, |ex_kusage| only contains the
575   // first 16 bits when the extension is present.
576   return UINT32_MAX;
577 }
578 
X509_get_extended_key_usage(X509 * x)579 uint32_t X509_get_extended_key_usage(X509 *x) {
580   if (!x509v3_cache_extensions(x)) {
581     return 0;
582   }
583   if (x->ex_flags & EXFLAG_XKUSAGE) {
584     return x->ex_xkusage;
585   }
586   // If there is no extension, extended key usage is unconstrained, so set all
587   // bits to one.
588   return UINT32_MAX;
589 }
590 
X509_get0_subject_key_id(X509 * x509)591 const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x509) {
592   if (!x509v3_cache_extensions(x509)) {
593     return NULL;
594   }
595   return x509->skid;
596 }
597 
X509_get0_authority_key_id(X509 * x509)598 const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x509) {
599   if (!x509v3_cache_extensions(x509)) {
600     return NULL;
601   }
602   return x509->akid != NULL ? x509->akid->keyid : NULL;
603 }
604 
X509_get0_authority_issuer(X509 * x509)605 const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x509) {
606   if (!x509v3_cache_extensions(x509)) {
607     return NULL;
608   }
609   return x509->akid != NULL ? x509->akid->issuer : NULL;
610 }
611 
X509_get0_authority_serial(X509 * x509)612 const ASN1_INTEGER *X509_get0_authority_serial(X509 *x509) {
613   if (!x509v3_cache_extensions(x509)) {
614     return NULL;
615   }
616   return x509->akid != NULL ? x509->akid->serial : NULL;
617 }
618 
X509_get_pathlen(X509 * x509)619 long X509_get_pathlen(X509 *x509) {
620   if (!x509v3_cache_extensions(x509) || (x509->ex_flags & EXFLAG_BCONS) == 0) {
621     return -1;
622   }
623   return x509->ex_pathlen;
624 }
625