You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

868 lines
26 KiB

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