v3_purp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Copyright 1999-2018 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 "internal/x509_int.h"
  15. static void x509v3_cache_extensions(X509 *x);
  16. static int check_ssl_ca(const X509 *x);
  17. static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
  18. int ca);
  19. static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  20. int ca);
  21. static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  22. int ca);
  23. static int purpose_smime(const X509 *x, int ca);
  24. static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
  25. int ca);
  26. static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
  27. int ca);
  28. static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
  29. int ca);
  30. static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
  31. int ca);
  32. static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
  33. static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
  34. static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b);
  35. static void xptable_free(X509_PURPOSE *p);
  36. static X509_PURPOSE xstandard[] = {
  37. {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
  38. check_purpose_ssl_client, "SSL client", "sslclient", NULL},
  39. {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
  40. check_purpose_ssl_server, "SSL server", "sslserver", NULL},
  41. {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
  42. check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
  43. {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
  44. "S/MIME signing", "smimesign", NULL},
  45. {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
  46. check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
  47. {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
  48. "CRL signing", "crlsign", NULL},
  49. {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any",
  50. NULL},
  51. {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper,
  52. "OCSP helper", "ocsphelper", NULL},
  53. {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
  54. check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign",
  55. NULL},
  56. };
  57. #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard)
  58. static STACK_OF(X509_PURPOSE) *xptable = NULL;
  59. static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b)
  60. {
  61. return (*a)->purpose - (*b)->purpose;
  62. }
  63. /*
  64. * As much as I'd like to make X509_check_purpose use a "const" X509* I
  65. * really can't because it does recalculate hashes and do other non-const
  66. * things.
  67. */
  68. int X509_check_purpose(X509 *x, int id, int ca)
  69. {
  70. int idx;
  71. const X509_PURPOSE *pt;
  72. if (!(x->ex_flags & EXFLAG_SET)) {
  73. CRYPTO_THREAD_write_lock(x->lock);
  74. x509v3_cache_extensions(x);
  75. CRYPTO_THREAD_unlock(x->lock);
  76. }
  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. tmp.purpose = purpose;
  127. if (!xptable)
  128. return -1;
  129. idx = sk_X509_PURPOSE_find(xptable, &tmp);
  130. if (idx == -1)
  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 void 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;
  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. DIST_POINT_set_dpname(dp->distpoint, iname);
  295. }
  296. static void setup_crldp(X509 *x)
  297. {
  298. int i;
  299. x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL);
  300. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++)
  301. setup_dp(x, sk_DIST_POINT_value(x->crldp, i));
  302. }
  303. #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
  304. #define ku_reject(x, usage) \
  305. (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
  306. #define xku_reject(x, usage) \
  307. (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
  308. #define ns_reject(x, usage) \
  309. (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
  310. static void x509v3_cache_extensions(X509 *x)
  311. {
  312. BASIC_CONSTRAINTS *bs;
  313. PROXY_CERT_INFO_EXTENSION *pci;
  314. ASN1_BIT_STRING *usage;
  315. ASN1_BIT_STRING *ns;
  316. EXTENDED_KEY_USAGE *extusage;
  317. X509_EXTENSION *ex;
  318. int i;
  319. if (x->ex_flags & EXFLAG_SET)
  320. return;
  321. X509_digest(x, EVP_sha1(), x->sha1_hash, NULL);
  322. /* V1 should mean no extensions ... */
  323. if (!X509_get_version(x))
  324. x->ex_flags |= EXFLAG_V1;
  325. /* Handle basic constraints */
  326. if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {
  327. if (bs->ca)
  328. x->ex_flags |= EXFLAG_CA;
  329. if (bs->pathlen) {
  330. if ((bs->pathlen->type == V_ASN1_NEG_INTEGER)
  331. || !bs->ca) {
  332. x->ex_flags |= EXFLAG_INVALID;
  333. x->ex_pathlen = 0;
  334. } else
  335. x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
  336. } else
  337. x->ex_pathlen = -1;
  338. BASIC_CONSTRAINTS_free(bs);
  339. x->ex_flags |= EXFLAG_BCONS;
  340. }
  341. /* Handle proxy certificates */
  342. if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, NULL, NULL))) {
  343. if (x->ex_flags & EXFLAG_CA
  344. || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
  345. || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
  346. x->ex_flags |= EXFLAG_INVALID;
  347. }
  348. if (pci->pcPathLengthConstraint) {
  349. x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
  350. } else
  351. x->ex_pcpathlen = -1;
  352. PROXY_CERT_INFO_EXTENSION_free(pci);
  353. x->ex_flags |= EXFLAG_PROXY;
  354. }
  355. /* Handle key usage */
  356. if ((usage = X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {
  357. if (usage->length > 0) {
  358. x->ex_kusage = usage->data[0];
  359. if (usage->length > 1)
  360. x->ex_kusage |= usage->data[1] << 8;
  361. } else
  362. x->ex_kusage = 0;
  363. x->ex_flags |= EXFLAG_KUSAGE;
  364. ASN1_BIT_STRING_free(usage);
  365. }
  366. x->ex_xkusage = 0;
  367. if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {
  368. x->ex_flags |= EXFLAG_XKUSAGE;
  369. for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
  370. switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
  371. case NID_server_auth:
  372. x->ex_xkusage |= XKU_SSL_SERVER;
  373. break;
  374. case NID_client_auth:
  375. x->ex_xkusage |= XKU_SSL_CLIENT;
  376. break;
  377. case NID_email_protect:
  378. x->ex_xkusage |= XKU_SMIME;
  379. break;
  380. case NID_code_sign:
  381. x->ex_xkusage |= XKU_CODE_SIGN;
  382. break;
  383. case NID_ms_sgc:
  384. case NID_ns_sgc:
  385. x->ex_xkusage |= XKU_SGC;
  386. break;
  387. case NID_OCSP_sign:
  388. x->ex_xkusage |= XKU_OCSP_SIGN;
  389. break;
  390. case NID_time_stamp:
  391. x->ex_xkusage |= XKU_TIMESTAMP;
  392. break;
  393. case NID_dvcs:
  394. x->ex_xkusage |= XKU_DVCS;
  395. break;
  396. case NID_anyExtendedKeyUsage:
  397. x->ex_xkusage |= XKU_ANYEKU;
  398. break;
  399. }
  400. }
  401. sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
  402. }
  403. if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {
  404. if (ns->length > 0)
  405. x->ex_nscert = ns->data[0];
  406. else
  407. x->ex_nscert = 0;
  408. x->ex_flags |= EXFLAG_NSCERT;
  409. ASN1_BIT_STRING_free(ns);
  410. }
  411. x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL);
  412. x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL);
  413. /* Does subject name match issuer ? */
  414. if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
  415. x->ex_flags |= EXFLAG_SI;
  416. /* If SKID matches AKID also indicate self signed */
  417. if (X509_check_akid(x, x->akid) == X509_V_OK &&
  418. !ku_reject(x, KU_KEY_CERT_SIGN))
  419. x->ex_flags |= EXFLAG_SS;
  420. }
  421. x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  422. x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
  423. if (!x->nc && (i != -1))
  424. x->ex_flags |= EXFLAG_INVALID;
  425. setup_crldp(x);
  426. #ifndef OPENSSL_NO_RFC3779
  427. x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, NULL, NULL);
  428. x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum,
  429. NULL, NULL);
  430. #endif
  431. for (i = 0; i < X509_get_ext_count(x); i++) {
  432. ex = X509_get_ext(x, i);
  433. if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
  434. == NID_freshest_crl)
  435. x->ex_flags |= EXFLAG_FRESHEST;
  436. if (!X509_EXTENSION_get_critical(ex))
  437. continue;
  438. if (!X509_supported_extension(ex)) {
  439. x->ex_flags |= EXFLAG_CRITICAL;
  440. break;
  441. }
  442. }
  443. x509_init_sig_info(x);
  444. x->ex_flags |= EXFLAG_SET;
  445. }
  446. /*-
  447. * CA checks common to all purposes
  448. * return codes:
  449. * 0 not a CA
  450. * 1 is a CA
  451. * 2 basicConstraints absent so "maybe" a CA
  452. * 3 basicConstraints absent but self signed V1.
  453. * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
  454. */
  455. static int check_ca(const X509 *x)
  456. {
  457. /* keyUsage if present should allow cert signing */
  458. if (ku_reject(x, KU_KEY_CERT_SIGN))
  459. return 0;
  460. if (x->ex_flags & EXFLAG_BCONS) {
  461. if (x->ex_flags & EXFLAG_CA)
  462. return 1;
  463. /* If basicConstraints says not a CA then say so */
  464. else
  465. return 0;
  466. } else {
  467. /* we support V1 roots for... uh, I don't really know why. */
  468. if ((x->ex_flags & V1_ROOT) == V1_ROOT)
  469. return 3;
  470. /*
  471. * If key usage present it must have certSign so tolerate it
  472. */
  473. else if (x->ex_flags & EXFLAG_KUSAGE)
  474. return 4;
  475. /* Older certificates could have Netscape-specific CA types */
  476. else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
  477. return 5;
  478. /* can this still be regarded a CA certificate? I doubt it */
  479. return 0;
  480. }
  481. }
  482. void X509_set_proxy_flag(X509 *x)
  483. {
  484. x->ex_flags |= EXFLAG_PROXY;
  485. }
  486. void X509_set_proxy_pathlen(X509 *x, long l)
  487. {
  488. x->ex_pcpathlen = l;
  489. }
  490. int X509_check_ca(X509 *x)
  491. {
  492. if (!(x->ex_flags & EXFLAG_SET)) {
  493. CRYPTO_THREAD_write_lock(x->lock);
  494. x509v3_cache_extensions(x);
  495. CRYPTO_THREAD_unlock(x->lock);
  496. }
  497. return check_ca(x);
  498. }
  499. /* Check SSL CA: common checks for SSL client and server */
  500. static int check_ssl_ca(const X509 *x)
  501. {
  502. int ca_ret;
  503. ca_ret = check_ca(x);
  504. if (!ca_ret)
  505. return 0;
  506. /* check nsCertType if present */
  507. if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
  508. return ca_ret;
  509. else
  510. return 0;
  511. }
  512. static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
  513. int ca)
  514. {
  515. if (xku_reject(x, XKU_SSL_CLIENT))
  516. return 0;
  517. if (ca)
  518. return check_ssl_ca(x);
  519. /* We need to do digital signatures or key agreement */
  520. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
  521. return 0;
  522. /* nsCertType if present should allow SSL client use */
  523. if (ns_reject(x, NS_SSL_CLIENT))
  524. return 0;
  525. return 1;
  526. }
  527. /*
  528. * Key usage needed for TLS/SSL server: digital signature, encipherment or
  529. * key agreement. The ssl code can check this more thoroughly for individual
  530. * key types.
  531. */
  532. #define KU_TLS \
  533. KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
  534. static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  535. int ca)
  536. {
  537. if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
  538. return 0;
  539. if (ca)
  540. return check_ssl_ca(x);
  541. if (ns_reject(x, NS_SSL_SERVER))
  542. return 0;
  543. if (ku_reject(x, KU_TLS))
  544. return 0;
  545. return 1;
  546. }
  547. static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  548. int ca)
  549. {
  550. int ret;
  551. ret = check_purpose_ssl_server(xp, x, ca);
  552. if (!ret || ca)
  553. return ret;
  554. /* We need to encipher or Netscape complains */
  555. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  556. return 0;
  557. return ret;
  558. }
  559. /* common S/MIME checks */
  560. static int purpose_smime(const X509 *x, int ca)
  561. {
  562. if (xku_reject(x, XKU_SMIME))
  563. return 0;
  564. if (ca) {
  565. int ca_ret;
  566. ca_ret = check_ca(x);
  567. if (!ca_ret)
  568. return 0;
  569. /* check nsCertType if present */
  570. if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
  571. return ca_ret;
  572. else
  573. return 0;
  574. }
  575. if (x->ex_flags & EXFLAG_NSCERT) {
  576. if (x->ex_nscert & NS_SMIME)
  577. return 1;
  578. /* Workaround for some buggy certificates */
  579. if (x->ex_nscert & NS_SSL_CLIENT)
  580. return 2;
  581. return 0;
  582. }
  583. return 1;
  584. }
  585. static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
  586. int ca)
  587. {
  588. int ret;
  589. ret = purpose_smime(x, ca);
  590. if (!ret || ca)
  591. return ret;
  592. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
  593. return 0;
  594. return ret;
  595. }
  596. static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
  597. int ca)
  598. {
  599. int ret;
  600. ret = purpose_smime(x, ca);
  601. if (!ret || ca)
  602. return ret;
  603. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  604. return 0;
  605. return ret;
  606. }
  607. static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
  608. int ca)
  609. {
  610. if (ca) {
  611. int ca_ret;
  612. if ((ca_ret = check_ca(x)) != 2)
  613. return ca_ret;
  614. else
  615. return 0;
  616. }
  617. if (ku_reject(x, KU_CRL_SIGN))
  618. return 0;
  619. return 1;
  620. }
  621. /*
  622. * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
  623. * is valid. Additional checks must be made on the chain.
  624. */
  625. static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
  626. {
  627. /*
  628. * Must be a valid CA. Should we really support the "I don't know" value
  629. * (2)?
  630. */
  631. if (ca)
  632. return check_ca(x);
  633. /* leaf certificate is checked in OCSP_verify() */
  634. return 1;
  635. }
  636. static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
  637. int ca)
  638. {
  639. int i_ext;
  640. /* If ca is true we must return if this is a valid CA certificate. */
  641. if (ca)
  642. return check_ca(x);
  643. /*
  644. * Check the optional key usage field:
  645. * if Key Usage is present, it must be one of digitalSignature
  646. * and/or nonRepudiation (other values are not consistent and shall
  647. * be rejected).
  648. */
  649. if ((x->ex_flags & EXFLAG_KUSAGE)
  650. && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
  651. !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
  652. return 0;
  653. /* Only time stamp key usage is permitted and it's required. */
  654. if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
  655. return 0;
  656. /* Extended Key Usage MUST be critical */
  657. i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
  658. if (i_ext >= 0) {
  659. X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
  660. if (!X509_EXTENSION_get_critical(ext))
  661. return 0;
  662. }
  663. return 1;
  664. }
  665. static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
  666. {
  667. return 1;
  668. }
  669. /*-
  670. * Various checks to see if one certificate issued the second.
  671. * This can be used to prune a set of possible issuer certificates
  672. * which have been looked up using some simple method such as by
  673. * subject name.
  674. * These are:
  675. * 1. Check issuer_name(subject) == subject_name(issuer)
  676. * 2. If akid(subject) exists check it matches issuer
  677. * 3. If key_usage(issuer) exists check it supports certificate signing
  678. * returns 0 for OK, positive for reason for mismatch, reasons match
  679. * codes for X509_verify_cert()
  680. */
  681. int X509_check_issued(X509 *issuer, X509 *subject)
  682. {
  683. if (X509_NAME_cmp(X509_get_subject_name(issuer),
  684. X509_get_issuer_name(subject)))
  685. return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
  686. x509v3_cache_extensions(issuer);
  687. x509v3_cache_extensions(subject);
  688. if (subject->akid) {
  689. int ret = X509_check_akid(issuer, subject->akid);
  690. if (ret != X509_V_OK)
  691. return ret;
  692. }
  693. if (subject->ex_flags & EXFLAG_PROXY) {
  694. if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
  695. return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
  696. } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
  697. return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
  698. return X509_V_OK;
  699. }
  700. int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
  701. {
  702. if (!akid)
  703. return X509_V_OK;
  704. /* Check key ids (if present) */
  705. if (akid->keyid && issuer->skid &&
  706. ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
  707. return X509_V_ERR_AKID_SKID_MISMATCH;
  708. /* Check serial number */
  709. if (akid->serial &&
  710. ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
  711. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  712. /* Check issuer name */
  713. if (akid->issuer) {
  714. /*
  715. * Ugh, for some peculiar reason AKID includes SEQUENCE OF
  716. * GeneralName. So look for a DirName. There may be more than one but
  717. * we only take any notice of the first.
  718. */
  719. GENERAL_NAMES *gens;
  720. GENERAL_NAME *gen;
  721. X509_NAME *nm = NULL;
  722. int i;
  723. gens = akid->issuer;
  724. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  725. gen = sk_GENERAL_NAME_value(gens, i);
  726. if (gen->type == GEN_DIRNAME) {
  727. nm = gen->d.dirn;
  728. break;
  729. }
  730. }
  731. if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
  732. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  733. }
  734. return X509_V_OK;
  735. }
  736. uint32_t X509_get_extension_flags(X509 *x)
  737. {
  738. /* Call for side-effect of computing hash and caching extensions */
  739. X509_check_purpose(x, -1, -1);
  740. return x->ex_flags;
  741. }
  742. uint32_t X509_get_key_usage(X509 *x)
  743. {
  744. /* Call for side-effect of computing hash and caching extensions */
  745. X509_check_purpose(x, -1, -1);
  746. if (x->ex_flags & EXFLAG_KUSAGE)
  747. return x->ex_kusage;
  748. return UINT32_MAX;
  749. }
  750. uint32_t X509_get_extended_key_usage(X509 *x)
  751. {
  752. /* Call for side-effect of computing hash and caching extensions */
  753. X509_check_purpose(x, -1, -1);
  754. if (x->ex_flags & EXFLAG_XKUSAGE)
  755. return x->ex_xkusage;
  756. return UINT32_MAX;
  757. }
  758. const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
  759. {
  760. /* Call for side-effect of computing hash and caching extensions */
  761. X509_check_purpose(x, -1, -1);
  762. return x->skid;
  763. }
  764. const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
  765. {
  766. /* Call for side-effect of computing hash and caching extensions */
  767. X509_check_purpose(x, -1, -1);
  768. return (x->akid != NULL ? x->akid->keyid : NULL);
  769. }
  770. long X509_get_pathlen(X509 *x)
  771. {
  772. /* Called for side effect of caching extensions */
  773. if (X509_check_purpose(x, -1, -1) != 1
  774. || (x->ex_flags & EXFLAG_BCONS) == 0)
  775. return -1;
  776. return x->ex_pathlen;
  777. }
  778. long X509_get_proxy_pathlen(X509 *x)
  779. {
  780. /* Called for side effect of caching extensions */
  781. if (X509_check_purpose(x, -1, -1) != 1
  782. || (x->ex_flags & EXFLAG_PROXY) == 0)
  783. return -1;
  784. return x->ex_pcpathlen;
  785. }