v3_purp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. x->ex_flags |= EXFLAG_INVALID;
  345. x->ex_pathlen = 0;
  346. } else {
  347. x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
  348. if (!bs->ca && x->ex_pathlen != 0) {
  349. x->ex_flags |= EXFLAG_INVALID;
  350. x->ex_pathlen = 0;
  351. }
  352. }
  353. } else
  354. x->ex_pathlen = -1;
  355. BASIC_CONSTRAINTS_free(bs);
  356. x->ex_flags |= EXFLAG_BCONS;
  357. } else if (i != -1) {
  358. x->ex_flags |= EXFLAG_INVALID;
  359. }
  360. /* Handle proxy certificates */
  361. if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) {
  362. if (x->ex_flags & EXFLAG_CA
  363. || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
  364. || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
  365. x->ex_flags |= EXFLAG_INVALID;
  366. }
  367. if (pci->pcPathLengthConstraint) {
  368. x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
  369. } else
  370. x->ex_pcpathlen = -1;
  371. PROXY_CERT_INFO_EXTENSION_free(pci);
  372. x->ex_flags |= EXFLAG_PROXY;
  373. } else if (i != -1) {
  374. x->ex_flags |= EXFLAG_INVALID;
  375. }
  376. /* Handle key usage */
  377. if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) {
  378. if (usage->length > 0) {
  379. x->ex_kusage = usage->data[0];
  380. if (usage->length > 1)
  381. x->ex_kusage |= usage->data[1] << 8;
  382. } else
  383. x->ex_kusage = 0;
  384. x->ex_flags |= EXFLAG_KUSAGE;
  385. ASN1_BIT_STRING_free(usage);
  386. } else if (i != -1) {
  387. x->ex_flags |= EXFLAG_INVALID;
  388. }
  389. x->ex_xkusage = 0;
  390. if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) {
  391. x->ex_flags |= EXFLAG_XKUSAGE;
  392. for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
  393. switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
  394. case NID_server_auth:
  395. x->ex_xkusage |= XKU_SSL_SERVER;
  396. break;
  397. case NID_client_auth:
  398. x->ex_xkusage |= XKU_SSL_CLIENT;
  399. break;
  400. case NID_email_protect:
  401. x->ex_xkusage |= XKU_SMIME;
  402. break;
  403. case NID_code_sign:
  404. x->ex_xkusage |= XKU_CODE_SIGN;
  405. break;
  406. case NID_ms_sgc:
  407. case NID_ns_sgc:
  408. x->ex_xkusage |= XKU_SGC;
  409. break;
  410. case NID_OCSP_sign:
  411. x->ex_xkusage |= XKU_OCSP_SIGN;
  412. break;
  413. case NID_time_stamp:
  414. x->ex_xkusage |= XKU_TIMESTAMP;
  415. break;
  416. case NID_dvcs:
  417. x->ex_xkusage |= XKU_DVCS;
  418. break;
  419. case NID_anyExtendedKeyUsage:
  420. x->ex_xkusage |= XKU_ANYEKU;
  421. break;
  422. }
  423. }
  424. sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
  425. } else if (i != -1) {
  426. x->ex_flags |= EXFLAG_INVALID;
  427. }
  428. if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) {
  429. if (ns->length > 0)
  430. x->ex_nscert = ns->data[0];
  431. else
  432. x->ex_nscert = 0;
  433. x->ex_flags |= EXFLAG_NSCERT;
  434. ASN1_BIT_STRING_free(ns);
  435. } else if (i != -1) {
  436. x->ex_flags |= EXFLAG_INVALID;
  437. }
  438. x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
  439. if (x->skid == NULL && i != -1)
  440. x->ex_flags |= EXFLAG_INVALID;
  441. x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
  442. if (x->akid == NULL && i != -1)
  443. x->ex_flags |= EXFLAG_INVALID;
  444. /* Does subject name match issuer ? */
  445. if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
  446. x->ex_flags |= EXFLAG_SI;
  447. /* If SKID matches AKID also indicate self signed */
  448. if (X509_check_akid(x, x->akid) == X509_V_OK &&
  449. !ku_reject(x, KU_KEY_CERT_SIGN))
  450. x->ex_flags |= EXFLAG_SS;
  451. }
  452. x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
  453. if (x->altname == NULL && i != -1)
  454. x->ex_flags |= EXFLAG_INVALID;
  455. x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
  456. if (x->nc == NULL && i != -1)
  457. x->ex_flags |= EXFLAG_INVALID;
  458. if (!setup_crldp(x))
  459. x->ex_flags |= EXFLAG_INVALID;
  460. #ifndef OPENSSL_NO_RFC3779
  461. x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
  462. if (x->rfc3779_addr == NULL && i != -1)
  463. x->ex_flags |= EXFLAG_INVALID;
  464. x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
  465. if (x->rfc3779_asid == NULL && i != -1)
  466. x->ex_flags |= EXFLAG_INVALID;
  467. #endif
  468. for (i = 0; i < X509_get_ext_count(x); i++) {
  469. ex = X509_get_ext(x, i);
  470. if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
  471. == NID_freshest_crl)
  472. x->ex_flags |= EXFLAG_FRESHEST;
  473. if (!X509_EXTENSION_get_critical(ex))
  474. continue;
  475. if (!X509_supported_extension(ex)) {
  476. x->ex_flags |= EXFLAG_CRITICAL;
  477. break;
  478. }
  479. }
  480. x509_init_sig_info(x);
  481. x->ex_flags |= EXFLAG_SET;
  482. #ifdef tsan_st_rel
  483. tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
  484. /*
  485. * Above store triggers fast lock-free check in the beginning of the
  486. * function. But one has to ensure that the structure is "stable", i.e.
  487. * all stores are visible on all processors. Hence the release fence.
  488. */
  489. #endif
  490. CRYPTO_THREAD_unlock(x->lock);
  491. }
  492. /*-
  493. * CA checks common to all purposes
  494. * return codes:
  495. * 0 not a CA
  496. * 1 is a CA
  497. * 2 Only possible in older versions of openSSL when basicConstraints are absent
  498. * new versions will not return this value. May be a CA
  499. * 3 basicConstraints absent but self signed V1.
  500. * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
  501. * 5 Netscape specific CA Flags present
  502. */
  503. static int check_ca(const X509 *x)
  504. {
  505. /* keyUsage if present should allow cert signing */
  506. if (ku_reject(x, KU_KEY_CERT_SIGN))
  507. return 0;
  508. if (x->ex_flags & EXFLAG_BCONS) {
  509. if (x->ex_flags & EXFLAG_CA)
  510. return 1;
  511. /* If basicConstraints says not a CA then say so */
  512. else
  513. return 0;
  514. } else {
  515. /* we support V1 roots for... uh, I don't really know why. */
  516. if ((x->ex_flags & V1_ROOT) == V1_ROOT)
  517. return 3;
  518. /*
  519. * If key usage present it must have certSign so tolerate it
  520. */
  521. else if (x->ex_flags & EXFLAG_KUSAGE)
  522. return 4;
  523. /* Older certificates could have Netscape-specific CA types */
  524. else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
  525. return 5;
  526. /* can this still be regarded a CA certificate? I doubt it */
  527. return 0;
  528. }
  529. }
  530. void X509_set_proxy_flag(X509 *x)
  531. {
  532. x->ex_flags |= EXFLAG_PROXY;
  533. }
  534. void X509_set_proxy_pathlen(X509 *x, long l)
  535. {
  536. x->ex_pcpathlen = l;
  537. }
  538. int X509_check_ca(X509 *x)
  539. {
  540. x509v3_cache_extensions(x);
  541. return check_ca(x);
  542. }
  543. /* Check SSL CA: common checks for SSL client and server */
  544. static int check_ssl_ca(const X509 *x)
  545. {
  546. int ca_ret;
  547. ca_ret = check_ca(x);
  548. if (!ca_ret)
  549. return 0;
  550. /* check nsCertType if present */
  551. if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
  552. return ca_ret;
  553. else
  554. return 0;
  555. }
  556. static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
  557. int ca)
  558. {
  559. if (xku_reject(x, XKU_SSL_CLIENT))
  560. return 0;
  561. if (ca)
  562. return check_ssl_ca(x);
  563. /* We need to do digital signatures or key agreement */
  564. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
  565. return 0;
  566. /* nsCertType if present should allow SSL client use */
  567. if (ns_reject(x, NS_SSL_CLIENT))
  568. return 0;
  569. return 1;
  570. }
  571. /*
  572. * Key usage needed for TLS/SSL server: digital signature, encipherment or
  573. * key agreement. The ssl code can check this more thoroughly for individual
  574. * key types.
  575. */
  576. #define KU_TLS \
  577. KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
  578. static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  579. int ca)
  580. {
  581. if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
  582. return 0;
  583. if (ca)
  584. return check_ssl_ca(x);
  585. if (ns_reject(x, NS_SSL_SERVER))
  586. return 0;
  587. if (ku_reject(x, KU_TLS))
  588. return 0;
  589. return 1;
  590. }
  591. static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  592. int ca)
  593. {
  594. int ret;
  595. ret = check_purpose_ssl_server(xp, x, ca);
  596. if (!ret || ca)
  597. return ret;
  598. /* We need to encipher or Netscape complains */
  599. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  600. return 0;
  601. return ret;
  602. }
  603. /* common S/MIME checks */
  604. static int purpose_smime(const X509 *x, int ca)
  605. {
  606. if (xku_reject(x, XKU_SMIME))
  607. return 0;
  608. if (ca) {
  609. int ca_ret;
  610. ca_ret = check_ca(x);
  611. if (!ca_ret)
  612. return 0;
  613. /* check nsCertType if present */
  614. if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
  615. return ca_ret;
  616. else
  617. return 0;
  618. }
  619. if (x->ex_flags & EXFLAG_NSCERT) {
  620. if (x->ex_nscert & NS_SMIME)
  621. return 1;
  622. /* Workaround for some buggy certificates */
  623. if (x->ex_nscert & NS_SSL_CLIENT)
  624. return 2;
  625. return 0;
  626. }
  627. return 1;
  628. }
  629. static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
  630. int ca)
  631. {
  632. int ret;
  633. ret = purpose_smime(x, ca);
  634. if (!ret || ca)
  635. return ret;
  636. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
  637. return 0;
  638. return ret;
  639. }
  640. static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
  641. int ca)
  642. {
  643. int ret;
  644. ret = purpose_smime(x, ca);
  645. if (!ret || ca)
  646. return ret;
  647. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  648. return 0;
  649. return ret;
  650. }
  651. static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
  652. int ca)
  653. {
  654. if (ca) {
  655. int ca_ret;
  656. if ((ca_ret = check_ca(x)) != 2)
  657. return ca_ret;
  658. else
  659. return 0;
  660. }
  661. if (ku_reject(x, KU_CRL_SIGN))
  662. return 0;
  663. return 1;
  664. }
  665. /*
  666. * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
  667. * is valid. Additional checks must be made on the chain.
  668. */
  669. static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
  670. {
  671. /*
  672. * Must be a valid CA. Should we really support the "I don't know" value
  673. * (2)?
  674. */
  675. if (ca)
  676. return check_ca(x);
  677. /* leaf certificate is checked in OCSP_verify() */
  678. return 1;
  679. }
  680. static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
  681. int ca)
  682. {
  683. int i_ext;
  684. /* If ca is true we must return if this is a valid CA certificate. */
  685. if (ca)
  686. return check_ca(x);
  687. /*
  688. * Check the optional key usage field:
  689. * if Key Usage is present, it must be one of digitalSignature
  690. * and/or nonRepudiation (other values are not consistent and shall
  691. * be rejected).
  692. */
  693. if ((x->ex_flags & EXFLAG_KUSAGE)
  694. && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
  695. !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
  696. return 0;
  697. /* Only time stamp key usage is permitted and it's required. */
  698. if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
  699. return 0;
  700. /* Extended Key Usage MUST be critical */
  701. i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
  702. if (i_ext >= 0) {
  703. X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
  704. if (!X509_EXTENSION_get_critical(ext))
  705. return 0;
  706. }
  707. return 1;
  708. }
  709. static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
  710. {
  711. return 1;
  712. }
  713. /*-
  714. * Various checks to see if one certificate issued the second.
  715. * This can be used to prune a set of possible issuer certificates
  716. * which have been looked up using some simple method such as by
  717. * subject name.
  718. * These are:
  719. * 1. Check issuer_name(subject) == subject_name(issuer)
  720. * 2. If akid(subject) exists check it matches issuer
  721. * 3. If key_usage(issuer) exists check it supports certificate signing
  722. * returns 0 for OK, positive for reason for mismatch, reasons match
  723. * codes for X509_verify_cert()
  724. */
  725. int X509_check_issued(X509 *issuer, X509 *subject)
  726. {
  727. if (X509_NAME_cmp(X509_get_subject_name(issuer),
  728. X509_get_issuer_name(subject)))
  729. return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
  730. x509v3_cache_extensions(issuer);
  731. if (issuer->ex_flags & EXFLAG_INVALID)
  732. return X509_V_ERR_UNSPECIFIED;
  733. x509v3_cache_extensions(subject);
  734. if (subject->ex_flags & EXFLAG_INVALID)
  735. return X509_V_ERR_UNSPECIFIED;
  736. if (subject->akid) {
  737. int ret = X509_check_akid(issuer, subject->akid);
  738. if (ret != X509_V_OK)
  739. return ret;
  740. }
  741. if (subject->ex_flags & EXFLAG_PROXY) {
  742. if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
  743. return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
  744. } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
  745. return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
  746. return X509_V_OK;
  747. }
  748. int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
  749. {
  750. if (!akid)
  751. return X509_V_OK;
  752. /* Check key ids (if present) */
  753. if (akid->keyid && issuer->skid &&
  754. ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
  755. return X509_V_ERR_AKID_SKID_MISMATCH;
  756. /* Check serial number */
  757. if (akid->serial &&
  758. ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
  759. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  760. /* Check issuer name */
  761. if (akid->issuer) {
  762. /*
  763. * Ugh, for some peculiar reason AKID includes SEQUENCE OF
  764. * GeneralName. So look for a DirName. There may be more than one but
  765. * we only take any notice of the first.
  766. */
  767. GENERAL_NAMES *gens;
  768. GENERAL_NAME *gen;
  769. X509_NAME *nm = NULL;
  770. int i;
  771. gens = akid->issuer;
  772. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  773. gen = sk_GENERAL_NAME_value(gens, i);
  774. if (gen->type == GEN_DIRNAME) {
  775. nm = gen->d.dirn;
  776. break;
  777. }
  778. }
  779. if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
  780. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  781. }
  782. return X509_V_OK;
  783. }
  784. uint32_t X509_get_extension_flags(X509 *x)
  785. {
  786. /* Call for side-effect of computing hash and caching extensions */
  787. X509_check_purpose(x, -1, -1);
  788. return x->ex_flags;
  789. }
  790. uint32_t X509_get_key_usage(X509 *x)
  791. {
  792. /* Call for side-effect of computing hash and caching extensions */
  793. if (X509_check_purpose(x, -1, -1) != 1)
  794. return 0;
  795. if (x->ex_flags & EXFLAG_KUSAGE)
  796. return x->ex_kusage;
  797. return UINT32_MAX;
  798. }
  799. uint32_t X509_get_extended_key_usage(X509 *x)
  800. {
  801. /* Call for side-effect of computing hash and caching extensions */
  802. if (X509_check_purpose(x, -1, -1) != 1)
  803. return 0;
  804. if (x->ex_flags & EXFLAG_XKUSAGE)
  805. return x->ex_xkusage;
  806. return UINT32_MAX;
  807. }
  808. const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
  809. {
  810. /* Call for side-effect of computing hash and caching extensions */
  811. if (X509_check_purpose(x, -1, -1) != 1)
  812. return NULL;
  813. return x->skid;
  814. }
  815. const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
  816. {
  817. /* Call for side-effect of computing hash and caching extensions */
  818. if (X509_check_purpose(x, -1, -1) != 1)
  819. return NULL;
  820. return (x->akid != NULL ? x->akid->keyid : NULL);
  821. }
  822. const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
  823. {
  824. /* Call for side-effect of computing hash and caching extensions */
  825. if (X509_check_purpose(x, -1, -1) != 1)
  826. return NULL;
  827. return (x->akid != NULL ? x->akid->issuer : NULL);
  828. }
  829. const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
  830. {
  831. /* Call for side-effect of computing hash and caching extensions */
  832. if (X509_check_purpose(x, -1, -1) != 1)
  833. return NULL;
  834. return (x->akid != NULL ? x->akid->serial : NULL);
  835. }
  836. long X509_get_pathlen(X509 *x)
  837. {
  838. /* Called for side effect of caching extensions */
  839. if (X509_check_purpose(x, -1, -1) != 1
  840. || (x->ex_flags & EXFLAG_BCONS) == 0)
  841. return -1;
  842. return x->ex_pathlen;
  843. }
  844. long X509_get_proxy_pathlen(X509 *x)
  845. {
  846. /* Called for side effect of caching extensions */
  847. if (X509_check_purpose(x, -1, -1) != 1
  848. || (x->ex_flags & EXFLAG_PROXY) == 0)
  849. return -1;
  850. return x->ex_pcpathlen;
  851. }