v3_purp.c 26 KB

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