v3_purp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. static void x509v3_cache_extensions(X509 *x);
  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 ca);
  20. static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  21. int ca);
  22. static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  23. int ca);
  24. static int purpose_smime(const X509 *x, int ca);
  25. static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
  26. int ca);
  27. static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
  28. int ca);
  29. static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
  30. int ca);
  31. static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
  32. int ca);
  33. static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
  34. static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
  35. static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b);
  36. static void xptable_free(X509_PURPOSE *p);
  37. static X509_PURPOSE xstandard[] = {
  38. {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
  39. check_purpose_ssl_client, "SSL client", "sslclient", NULL},
  40. {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
  41. check_purpose_ssl_server, "SSL server", "sslserver", NULL},
  42. {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
  43. check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
  44. {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
  45. "S/MIME signing", "smimesign", NULL},
  46. {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
  47. check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
  48. {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
  49. "CRL signing", "crlsign", NULL},
  50. {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any",
  51. NULL},
  52. {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper,
  53. "OCSP helper", "ocsphelper", NULL},
  54. {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
  55. check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign",
  56. NULL},
  57. };
  58. #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard)
  59. static STACK_OF(X509_PURPOSE) *xptable = NULL;
  60. static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b)
  61. {
  62. return (*a)->purpose - (*b)->purpose;
  63. }
  64. /*
  65. * As much as I'd like to make X509_check_purpose use a "const" X509* I
  66. * really can't because it does recalculate hashes and do other non-const
  67. * things.
  68. */
  69. int X509_check_purpose(X509 *x, int id, int ca)
  70. {
  71. int idx;
  72. const X509_PURPOSE *pt;
  73. x509v3_cache_extensions(x);
  74. if (x->ex_flags & EXFLAG_INVALID)
  75. return -1;
  76. /* Return if side-effect only call */
  77. if (id == -1)
  78. return 1;
  79. idx = X509_PURPOSE_get_by_id(id);
  80. if (idx == -1)
  81. return -1;
  82. pt = X509_PURPOSE_get0(idx);
  83. return pt->check_purpose(pt, x, ca);
  84. }
  85. int X509_PURPOSE_set(int *p, int purpose)
  86. {
  87. if (X509_PURPOSE_get_by_id(purpose) == -1) {
  88. X509V3err(X509V3_F_X509_PURPOSE_SET, X509V3_R_INVALID_PURPOSE);
  89. return 0;
  90. }
  91. *p = purpose;
  92. return 1;
  93. }
  94. int X509_PURPOSE_get_count(void)
  95. {
  96. if (!xptable)
  97. return X509_PURPOSE_COUNT;
  98. return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
  99. }
  100. X509_PURPOSE *X509_PURPOSE_get0(int idx)
  101. {
  102. if (idx < 0)
  103. return NULL;
  104. if (idx < (int)X509_PURPOSE_COUNT)
  105. return xstandard + idx;
  106. return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
  107. }
  108. int X509_PURPOSE_get_by_sname(const char *sname)
  109. {
  110. int i;
  111. X509_PURPOSE *xptmp;
  112. for (i = 0; i < X509_PURPOSE_get_count(); i++) {
  113. xptmp = X509_PURPOSE_get0(i);
  114. if (strcmp(xptmp->sname, sname) == 0)
  115. return i;
  116. }
  117. return -1;
  118. }
  119. int X509_PURPOSE_get_by_id(int purpose)
  120. {
  121. X509_PURPOSE tmp;
  122. int idx;
  123. if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
  124. return purpose - X509_PURPOSE_MIN;
  125. if (xptable == NULL)
  126. return -1;
  127. tmp.purpose = purpose;
  128. idx = sk_X509_PURPOSE_find(xptable, &tmp);
  129. if (idx < 0)
  130. return -1;
  131. return idx + X509_PURPOSE_COUNT;
  132. }
  133. int X509_PURPOSE_add(int id, int trust, int flags,
  134. int (*ck) (const X509_PURPOSE *, const X509 *, int),
  135. const char *name, const char *sname, void *arg)
  136. {
  137. int idx;
  138. X509_PURPOSE *ptmp;
  139. /*
  140. * This is set according to what we change: application can't set it
  141. */
  142. flags &= ~X509_PURPOSE_DYNAMIC;
  143. /* This will always be set for application modified trust entries */
  144. flags |= X509_PURPOSE_DYNAMIC_NAME;
  145. /* Get existing entry if any */
  146. idx = X509_PURPOSE_get_by_id(id);
  147. /* Need a new entry */
  148. if (idx == -1) {
  149. if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL) {
  150. X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
  151. return 0;
  152. }
  153. ptmp->flags = X509_PURPOSE_DYNAMIC;
  154. } else
  155. ptmp = X509_PURPOSE_get0(idx);
  156. /* OPENSSL_free existing name if dynamic */
  157. if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
  158. OPENSSL_free(ptmp->name);
  159. OPENSSL_free(ptmp->sname);
  160. }
  161. /* dup supplied name */
  162. ptmp->name = OPENSSL_strdup(name);
  163. ptmp->sname = OPENSSL_strdup(sname);
  164. if (!ptmp->name || !ptmp->sname) {
  165. X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
  166. goto err;
  167. }
  168. /* Keep the dynamic flag of existing entry */
  169. ptmp->flags &= X509_PURPOSE_DYNAMIC;
  170. /* Set all other flags */
  171. ptmp->flags |= flags;
  172. ptmp->purpose = id;
  173. ptmp->trust = trust;
  174. ptmp->check_purpose = ck;
  175. ptmp->usr_data = arg;
  176. /* If its a new entry manage the dynamic table */
  177. if (idx == -1) {
  178. if (xptable == NULL
  179. && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) {
  180. X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
  181. goto err;
  182. }
  183. if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
  184. X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
  185. goto err;
  186. }
  187. }
  188. return 1;
  189. err:
  190. if (idx == -1) {
  191. OPENSSL_free(ptmp->name);
  192. OPENSSL_free(ptmp->sname);
  193. OPENSSL_free(ptmp);
  194. }
  195. return 0;
  196. }
  197. static void xptable_free(X509_PURPOSE *p)
  198. {
  199. if (!p)
  200. return;
  201. if (p->flags & X509_PURPOSE_DYNAMIC) {
  202. if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
  203. OPENSSL_free(p->name);
  204. OPENSSL_free(p->sname);
  205. }
  206. OPENSSL_free(p);
  207. }
  208. }
  209. void X509_PURPOSE_cleanup(void)
  210. {
  211. sk_X509_PURPOSE_pop_free(xptable, xptable_free);
  212. xptable = NULL;
  213. }
  214. int X509_PURPOSE_get_id(const X509_PURPOSE *xp)
  215. {
  216. return xp->purpose;
  217. }
  218. char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp)
  219. {
  220. return xp->name;
  221. }
  222. char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp)
  223. {
  224. return xp->sname;
  225. }
  226. int X509_PURPOSE_get_trust(const X509_PURPOSE *xp)
  227. {
  228. return xp->trust;
  229. }
  230. static int nid_cmp(const int *a, const int *b)
  231. {
  232. return *a - *b;
  233. }
  234. DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid);
  235. IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid);
  236. int X509_supported_extension(X509_EXTENSION *ex)
  237. {
  238. /*
  239. * This table is a list of the NIDs of supported extensions: that is
  240. * those which are used by the verify process. If an extension is
  241. * critical and doesn't appear in this list then the verify process will
  242. * normally reject the certificate. The list must be kept in numerical
  243. * order because it will be searched using bsearch.
  244. */
  245. static const int supported_nids[] = {
  246. NID_netscape_cert_type, /* 71 */
  247. NID_key_usage, /* 83 */
  248. NID_subject_alt_name, /* 85 */
  249. NID_basic_constraints, /* 87 */
  250. NID_certificate_policies, /* 89 */
  251. NID_crl_distribution_points, /* 103 */
  252. NID_ext_key_usage, /* 126 */
  253. #ifndef OPENSSL_NO_RFC3779
  254. NID_sbgp_ipAddrBlock, /* 290 */
  255. NID_sbgp_autonomousSysNum, /* 291 */
  256. #endif
  257. NID_policy_constraints, /* 401 */
  258. NID_proxyCertInfo, /* 663 */
  259. NID_name_constraints, /* 666 */
  260. NID_policy_mappings, /* 747 */
  261. NID_inhibit_any_policy /* 748 */
  262. };
  263. int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
  264. if (ex_nid == NID_undef)
  265. return 0;
  266. if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
  267. return 1;
  268. return 0;
  269. }
  270. static int setup_dp(X509 *x, DIST_POINT *dp)
  271. {
  272. X509_NAME *iname = NULL;
  273. int i;
  274. if (dp->reasons) {
  275. if (dp->reasons->length > 0)
  276. dp->dp_reasons = dp->reasons->data[0];
  277. if (dp->reasons->length > 1)
  278. dp->dp_reasons |= (dp->reasons->data[1] << 8);
  279. dp->dp_reasons &= CRLDP_ALL_REASONS;
  280. } else
  281. dp->dp_reasons = CRLDP_ALL_REASONS;
  282. if (!dp->distpoint || (dp->distpoint->type != 1))
  283. return 1;
  284. for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
  285. GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
  286. if (gen->type == GEN_DIRNAME) {
  287. iname = gen->d.directoryName;
  288. break;
  289. }
  290. }
  291. if (!iname)
  292. iname = X509_get_issuer_name(x);
  293. return DIST_POINT_set_dpname(dp->distpoint, iname);
  294. }
  295. static int setup_crldp(X509 *x)
  296. {
  297. int i;
  298. x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
  299. if (x->crldp == NULL && i != -1)
  300. return 0;
  301. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
  302. if (!setup_dp(x, sk_DIST_POINT_value(x->crldp, i)))
  303. return 0;
  304. }
  305. return 1;
  306. }
  307. #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
  308. #define ku_reject(x, usage) \
  309. (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
  310. #define xku_reject(x, usage) \
  311. (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
  312. #define ns_reject(x, usage) \
  313. (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
  314. static void x509v3_cache_extensions(X509 *x)
  315. {
  316. BASIC_CONSTRAINTS *bs;
  317. PROXY_CERT_INFO_EXTENSION *pci;
  318. ASN1_BIT_STRING *usage;
  319. ASN1_BIT_STRING *ns;
  320. EXTENDED_KEY_USAGE *extusage;
  321. X509_EXTENSION *ex;
  322. int i;
  323. #ifdef tsan_ld_acq
  324. /* fast lock-free check, see end of the function for details. */
  325. if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
  326. return;
  327. #endif
  328. CRYPTO_THREAD_write_lock(x->lock);
  329. if (x->ex_flags & EXFLAG_SET) {
  330. CRYPTO_THREAD_unlock(x->lock);
  331. return;
  332. }
  333. if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL))
  334. x->ex_flags |= EXFLAG_INVALID;
  335. /* V1 should mean no extensions ... */
  336. if (!X509_get_version(x))
  337. x->ex_flags |= EXFLAG_V1;
  338. /* Handle basic constraints */
  339. if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL))) {
  340. if (bs->ca)
  341. x->ex_flags |= EXFLAG_CA;
  342. if (bs->pathlen) {
  343. if ((bs->pathlen->type == V_ASN1_NEG_INTEGER)
  344. || !bs->ca) {
  345. x->ex_flags |= EXFLAG_INVALID;
  346. x->ex_pathlen = 0;
  347. } else
  348. x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
  349. } else
  350. x->ex_pathlen = -1;
  351. BASIC_CONSTRAINTS_free(bs);
  352. x->ex_flags |= EXFLAG_BCONS;
  353. } else if (i != -1) {
  354. x->ex_flags |= EXFLAG_INVALID;
  355. }
  356. /* Handle proxy certificates */
  357. if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) {
  358. if (x->ex_flags & EXFLAG_CA
  359. || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
  360. || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
  361. x->ex_flags |= EXFLAG_INVALID;
  362. }
  363. if (pci->pcPathLengthConstraint) {
  364. x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
  365. } else
  366. x->ex_pcpathlen = -1;
  367. PROXY_CERT_INFO_EXTENSION_free(pci);
  368. x->ex_flags |= EXFLAG_PROXY;
  369. } else if (i != -1) {
  370. x->ex_flags |= EXFLAG_INVALID;
  371. }
  372. /* Handle key usage */
  373. if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) {
  374. if (usage->length > 0) {
  375. x->ex_kusage = usage->data[0];
  376. if (usage->length > 1)
  377. x->ex_kusage |= usage->data[1] << 8;
  378. } else
  379. x->ex_kusage = 0;
  380. x->ex_flags |= EXFLAG_KUSAGE;
  381. ASN1_BIT_STRING_free(usage);
  382. } else if (i != -1) {
  383. x->ex_flags |= EXFLAG_INVALID;
  384. }
  385. x->ex_xkusage = 0;
  386. if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) {
  387. x->ex_flags |= EXFLAG_XKUSAGE;
  388. for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
  389. switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
  390. case NID_server_auth:
  391. x->ex_xkusage |= XKU_SSL_SERVER;
  392. break;
  393. case NID_client_auth:
  394. x->ex_xkusage |= XKU_SSL_CLIENT;
  395. break;
  396. case NID_email_protect:
  397. x->ex_xkusage |= XKU_SMIME;
  398. break;
  399. case NID_code_sign:
  400. x->ex_xkusage |= XKU_CODE_SIGN;
  401. break;
  402. case NID_ms_sgc:
  403. case NID_ns_sgc:
  404. x->ex_xkusage |= XKU_SGC;
  405. break;
  406. case NID_OCSP_sign:
  407. x->ex_xkusage |= XKU_OCSP_SIGN;
  408. break;
  409. case NID_time_stamp:
  410. x->ex_xkusage |= XKU_TIMESTAMP;
  411. break;
  412. case NID_dvcs:
  413. x->ex_xkusage |= XKU_DVCS;
  414. break;
  415. case NID_anyExtendedKeyUsage:
  416. x->ex_xkusage |= XKU_ANYEKU;
  417. break;
  418. }
  419. }
  420. sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
  421. } else if (i != -1) {
  422. x->ex_flags |= EXFLAG_INVALID;
  423. }
  424. if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) {
  425. if (ns->length > 0)
  426. x->ex_nscert = ns->data[0];
  427. else
  428. x->ex_nscert = 0;
  429. x->ex_flags |= EXFLAG_NSCERT;
  430. ASN1_BIT_STRING_free(ns);
  431. } else if (i != -1) {
  432. x->ex_flags |= EXFLAG_INVALID;
  433. }
  434. x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
  435. if (x->skid == NULL && i != -1)
  436. x->ex_flags |= EXFLAG_INVALID;
  437. x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
  438. if (x->akid == NULL && i != -1)
  439. x->ex_flags |= EXFLAG_INVALID;
  440. /* Does subject name match issuer ? */
  441. if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
  442. x->ex_flags |= EXFLAG_SI;
  443. /* If SKID matches AKID also indicate self signed */
  444. if (X509_check_akid(x, x->akid) == X509_V_OK &&
  445. !ku_reject(x, KU_KEY_CERT_SIGN))
  446. x->ex_flags |= EXFLAG_SS;
  447. }
  448. x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
  449. if (x->altname == NULL && i != -1)
  450. x->ex_flags |= EXFLAG_INVALID;
  451. x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
  452. if (x->nc == NULL && i != -1)
  453. x->ex_flags |= EXFLAG_INVALID;
  454. if (!setup_crldp(x))
  455. x->ex_flags |= EXFLAG_INVALID;
  456. #ifndef OPENSSL_NO_RFC3779
  457. x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
  458. if (x->rfc3779_addr == NULL && i != -1)
  459. x->ex_flags |= EXFLAG_INVALID;
  460. x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
  461. if (x->rfc3779_asid == NULL && i != -1)
  462. x->ex_flags |= EXFLAG_INVALID;
  463. #endif
  464. for (i = 0; i < X509_get_ext_count(x); i++) {
  465. ex = X509_get_ext(x, i);
  466. if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
  467. == NID_freshest_crl)
  468. x->ex_flags |= EXFLAG_FRESHEST;
  469. if (!X509_EXTENSION_get_critical(ex))
  470. continue;
  471. if (!X509_supported_extension(ex)) {
  472. x->ex_flags |= EXFLAG_CRITICAL;
  473. break;
  474. }
  475. }
  476. x509_init_sig_info(x);
  477. x->ex_flags |= EXFLAG_SET;
  478. #ifdef tsan_st_rel
  479. tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
  480. /*
  481. * Above store triggers fast lock-free check in the beginning of the
  482. * function. But one has to ensure that the structure is "stable", i.e.
  483. * all stores are visible on all processors. Hence the release fence.
  484. */
  485. #endif
  486. CRYPTO_THREAD_unlock(x->lock);
  487. }
  488. /*-
  489. * CA checks common to all purposes
  490. * return codes:
  491. * 0 not a CA
  492. * 1 is a CA
  493. * 2 basicConstraints absent so "maybe" a CA
  494. * 3 basicConstraints absent but self signed V1.
  495. * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
  496. */
  497. static int check_ca(const X509 *x)
  498. {
  499. /* keyUsage if present should allow cert signing */
  500. if (ku_reject(x, KU_KEY_CERT_SIGN))
  501. return 0;
  502. if (x->ex_flags & EXFLAG_BCONS) {
  503. if (x->ex_flags & EXFLAG_CA)
  504. return 1;
  505. /* If basicConstraints says not a CA then say so */
  506. else
  507. return 0;
  508. } else {
  509. /* we support V1 roots for... uh, I don't really know why. */
  510. if ((x->ex_flags & V1_ROOT) == V1_ROOT)
  511. return 3;
  512. /*
  513. * If key usage present it must have certSign so tolerate it
  514. */
  515. else if (x->ex_flags & EXFLAG_KUSAGE)
  516. return 4;
  517. /* Older certificates could have Netscape-specific CA types */
  518. else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
  519. return 5;
  520. /* can this still be regarded a CA certificate? I doubt it */
  521. return 0;
  522. }
  523. }
  524. void X509_set_proxy_flag(X509 *x)
  525. {
  526. x->ex_flags |= EXFLAG_PROXY;
  527. }
  528. void X509_set_proxy_pathlen(X509 *x, long l)
  529. {
  530. x->ex_pcpathlen = l;
  531. }
  532. int X509_check_ca(X509 *x)
  533. {
  534. x509v3_cache_extensions(x);
  535. return check_ca(x);
  536. }
  537. /* Check SSL CA: common checks for SSL client and server */
  538. static int check_ssl_ca(const X509 *x)
  539. {
  540. int ca_ret;
  541. ca_ret = check_ca(x);
  542. if (!ca_ret)
  543. return 0;
  544. /* check nsCertType if present */
  545. if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
  546. return ca_ret;
  547. else
  548. return 0;
  549. }
  550. static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
  551. int ca)
  552. {
  553. if (xku_reject(x, XKU_SSL_CLIENT))
  554. return 0;
  555. if (ca)
  556. return check_ssl_ca(x);
  557. /* We need to do digital signatures or key agreement */
  558. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
  559. return 0;
  560. /* nsCertType if present should allow SSL client use */
  561. if (ns_reject(x, NS_SSL_CLIENT))
  562. return 0;
  563. return 1;
  564. }
  565. /*
  566. * Key usage needed for TLS/SSL server: digital signature, encipherment or
  567. * key agreement. The ssl code can check this more thoroughly for individual
  568. * key types.
  569. */
  570. #define KU_TLS \
  571. KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
  572. static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  573. int ca)
  574. {
  575. if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
  576. return 0;
  577. if (ca)
  578. return check_ssl_ca(x);
  579. if (ns_reject(x, NS_SSL_SERVER))
  580. return 0;
  581. if (ku_reject(x, KU_TLS))
  582. return 0;
  583. return 1;
  584. }
  585. static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  586. int ca)
  587. {
  588. int ret;
  589. ret = check_purpose_ssl_server(xp, x, ca);
  590. if (!ret || ca)
  591. return ret;
  592. /* We need to encipher or Netscape complains */
  593. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  594. return 0;
  595. return ret;
  596. }
  597. /* common S/MIME checks */
  598. static int purpose_smime(const X509 *x, int ca)
  599. {
  600. if (xku_reject(x, XKU_SMIME))
  601. return 0;
  602. if (ca) {
  603. int ca_ret;
  604. ca_ret = check_ca(x);
  605. if (!ca_ret)
  606. return 0;
  607. /* check nsCertType if present */
  608. if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
  609. return ca_ret;
  610. else
  611. return 0;
  612. }
  613. if (x->ex_flags & EXFLAG_NSCERT) {
  614. if (x->ex_nscert & NS_SMIME)
  615. return 1;
  616. /* Workaround for some buggy certificates */
  617. if (x->ex_nscert & NS_SSL_CLIENT)
  618. return 2;
  619. return 0;
  620. }
  621. return 1;
  622. }
  623. static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
  624. int ca)
  625. {
  626. int ret;
  627. ret = purpose_smime(x, ca);
  628. if (!ret || ca)
  629. return ret;
  630. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
  631. return 0;
  632. return ret;
  633. }
  634. static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
  635. int ca)
  636. {
  637. int ret;
  638. ret = purpose_smime(x, ca);
  639. if (!ret || ca)
  640. return ret;
  641. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  642. return 0;
  643. return ret;
  644. }
  645. static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
  646. int ca)
  647. {
  648. if (ca) {
  649. int ca_ret;
  650. if ((ca_ret = check_ca(x)) != 2)
  651. return ca_ret;
  652. else
  653. return 0;
  654. }
  655. if (ku_reject(x, KU_CRL_SIGN))
  656. return 0;
  657. return 1;
  658. }
  659. /*
  660. * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
  661. * is valid. Additional checks must be made on the chain.
  662. */
  663. static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
  664. {
  665. /*
  666. * Must be a valid CA. Should we really support the "I don't know" value
  667. * (2)?
  668. */
  669. if (ca)
  670. return check_ca(x);
  671. /* leaf certificate is checked in OCSP_verify() */
  672. return 1;
  673. }
  674. static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
  675. int ca)
  676. {
  677. int i_ext;
  678. /* If ca is true we must return if this is a valid CA certificate. */
  679. if (ca)
  680. return check_ca(x);
  681. /*
  682. * Check the optional key usage field:
  683. * if Key Usage is present, it must be one of digitalSignature
  684. * and/or nonRepudiation (other values are not consistent and shall
  685. * be rejected).
  686. */
  687. if ((x->ex_flags & EXFLAG_KUSAGE)
  688. && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
  689. !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
  690. return 0;
  691. /* Only time stamp key usage is permitted and it's required. */
  692. if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
  693. return 0;
  694. /* Extended Key Usage MUST be critical */
  695. i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
  696. if (i_ext >= 0) {
  697. X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
  698. if (!X509_EXTENSION_get_critical(ext))
  699. return 0;
  700. }
  701. return 1;
  702. }
  703. static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
  704. {
  705. return 1;
  706. }
  707. /*-
  708. * Various checks to see if one certificate issued the second.
  709. * This can be used to prune a set of possible issuer certificates
  710. * which have been looked up using some simple method such as by
  711. * subject name.
  712. * These are:
  713. * 1. Check issuer_name(subject) == subject_name(issuer)
  714. * 2. If akid(subject) exists check it matches issuer
  715. * 3. If key_usage(issuer) exists check it supports certificate signing
  716. * returns 0 for OK, positive for reason for mismatch, reasons match
  717. * codes for X509_verify_cert()
  718. */
  719. int X509_check_issued(X509 *issuer, X509 *subject)
  720. {
  721. if (X509_NAME_cmp(X509_get_subject_name(issuer),
  722. X509_get_issuer_name(subject)))
  723. return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
  724. x509v3_cache_extensions(issuer);
  725. if (issuer->ex_flags & EXFLAG_INVALID)
  726. return X509_V_ERR_UNSPECIFIED;
  727. x509v3_cache_extensions(subject);
  728. if (subject->ex_flags & EXFLAG_INVALID)
  729. return X509_V_ERR_UNSPECIFIED;
  730. if (subject->akid) {
  731. int ret = X509_check_akid(issuer, subject->akid);
  732. if (ret != X509_V_OK)
  733. return ret;
  734. }
  735. if (subject->ex_flags & EXFLAG_PROXY) {
  736. if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
  737. return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
  738. } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
  739. return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
  740. return X509_V_OK;
  741. }
  742. int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
  743. {
  744. if (!akid)
  745. return X509_V_OK;
  746. /* Check key ids (if present) */
  747. if (akid->keyid && issuer->skid &&
  748. ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
  749. return X509_V_ERR_AKID_SKID_MISMATCH;
  750. /* Check serial number */
  751. if (akid->serial &&
  752. ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
  753. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  754. /* Check issuer name */
  755. if (akid->issuer) {
  756. /*
  757. * Ugh, for some peculiar reason AKID includes SEQUENCE OF
  758. * GeneralName. So look for a DirName. There may be more than one but
  759. * we only take any notice of the first.
  760. */
  761. GENERAL_NAMES *gens;
  762. GENERAL_NAME *gen;
  763. X509_NAME *nm = NULL;
  764. int i;
  765. gens = akid->issuer;
  766. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  767. gen = sk_GENERAL_NAME_value(gens, i);
  768. if (gen->type == GEN_DIRNAME) {
  769. nm = gen->d.dirn;
  770. break;
  771. }
  772. }
  773. if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
  774. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  775. }
  776. return X509_V_OK;
  777. }
  778. uint32_t X509_get_extension_flags(X509 *x)
  779. {
  780. /* Call for side-effect of computing hash and caching extensions */
  781. X509_check_purpose(x, -1, -1);
  782. return x->ex_flags;
  783. }
  784. uint32_t X509_get_key_usage(X509 *x)
  785. {
  786. /* Call for side-effect of computing hash and caching extensions */
  787. if (X509_check_purpose(x, -1, -1) != 1)
  788. return 0;
  789. if (x->ex_flags & EXFLAG_KUSAGE)
  790. return x->ex_kusage;
  791. return UINT32_MAX;
  792. }
  793. uint32_t X509_get_extended_key_usage(X509 *x)
  794. {
  795. /* Call for side-effect of computing hash and caching extensions */
  796. if (X509_check_purpose(x, -1, -1) != 1)
  797. return 0;
  798. if (x->ex_flags & EXFLAG_XKUSAGE)
  799. return x->ex_xkusage;
  800. return UINT32_MAX;
  801. }
  802. const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
  803. {
  804. /* Call for side-effect of computing hash and caching extensions */
  805. if (X509_check_purpose(x, -1, -1) != 1)
  806. return NULL;
  807. return x->skid;
  808. }
  809. const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
  810. {
  811. /* Call for side-effect of computing hash and caching extensions */
  812. if (X509_check_purpose(x, -1, -1) != 1)
  813. return NULL;
  814. return (x->akid != NULL ? x->akid->keyid : NULL);
  815. }
  816. const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
  817. {
  818. /* Call for side-effect of computing hash and caching extensions */
  819. if (X509_check_purpose(x, -1, -1) != 1)
  820. return NULL;
  821. return (x->akid != NULL ? x->akid->issuer : NULL);
  822. }
  823. const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
  824. {
  825. /* Call for side-effect of computing hash and caching extensions */
  826. if (X509_check_purpose(x, -1, -1) != 1)
  827. return NULL;
  828. return (x->akid != NULL ? x->akid->serial : NULL);
  829. }
  830. long X509_get_pathlen(X509 *x)
  831. {
  832. /* Called for side effect of caching extensions */
  833. if (X509_check_purpose(x, -1, -1) != 1
  834. || (x->ex_flags & EXFLAG_BCONS) == 0)
  835. return -1;
  836. return x->ex_pathlen;
  837. }
  838. long X509_get_proxy_pathlen(X509 *x)
  839. {
  840. /* Called for side effect of caching extensions */
  841. if (X509_check_purpose(x, -1, -1) != 1
  842. || (x->ex_flags & EXFLAG_PROXY) == 0)
  843. return -1;
  844. return x->ex_pcpathlen;
  845. }