v3_purp.c 27 KB

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