v3_purp.c 30 KB

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