Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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