v3_purp.c 30 KB

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