v3_purp.c 35 KB

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