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.
 
 
 
 
 
 

845 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_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
  77. int ca);
  78. static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  79. int ca);
  80. static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  81. int ca);
  82. static int purpose_smime(const X509 *x, int ca);
  83. static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
  84. int ca);
  85. static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
  86. int ca);
  87. static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
  88. int ca);
  89. static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
  90. int ca);
  91. static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
  92. static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
  93. static int xp_cmp(const X509_PURPOSE **a, const X509_PURPOSE **b);
  94. static void xptable_free(X509_PURPOSE *p);
  95. static X509_PURPOSE xstandard[] = {
  96. {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
  97. check_purpose_ssl_client, (char *)"SSL client", (char *)"sslclient",
  98. NULL},
  99. {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
  100. check_purpose_ssl_server, (char *)"SSL server", (char *)"sslserver",
  101. NULL},
  102. {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
  103. check_purpose_ns_ssl_server, (char *)"Netscape SSL server",
  104. (char *)"nssslserver", NULL},
  105. {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
  106. (char *)"S/MIME signing", (char *)"smimesign", NULL},
  107. {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
  108. check_purpose_smime_encrypt, (char *)"S/MIME encryption",
  109. (char *)"smimeencrypt", NULL},
  110. {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
  111. (char *)"CRL signing", (char *)"crlsign", NULL},
  112. {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, (char *)"Any Purpose",
  113. (char *)"any", NULL},
  114. {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper,
  115. (char *)"OCSP helper", (char *)"ocsphelper", NULL},
  116. {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
  117. check_purpose_timestamp_sign, (char *)"Time Stamp signing",
  118. (char *)"timestampsign", NULL},
  119. };
  120. #define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE))
  121. static STACK_OF(X509_PURPOSE) *xptable = NULL;
  122. static int xp_cmp(const X509_PURPOSE **a, const X509_PURPOSE **b)
  123. {
  124. return (*a)->purpose - (*b)->purpose;
  125. }
  126. /*
  127. * As much as I'd like to make X509_check_purpose use a "const" X509* I
  128. * really can't because it does recalculate hashes and do other non-const
  129. * things.
  130. */
  131. int X509_check_purpose(X509 *x, int id, int ca)
  132. {
  133. int idx;
  134. const X509_PURPOSE *pt;
  135. x509v3_cache_extensions(x);
  136. if (id == -1)
  137. return 1;
  138. idx = X509_PURPOSE_get_by_id(id);
  139. if (idx == -1)
  140. return -1;
  141. pt = X509_PURPOSE_get0(idx);
  142. return pt->check_purpose(pt, x, ca);
  143. }
  144. int X509_PURPOSE_set(int *p, int purpose)
  145. {
  146. if (X509_PURPOSE_get_by_id(purpose) == -1) {
  147. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_PURPOSE);
  148. return 0;
  149. }
  150. *p = purpose;
  151. return 1;
  152. }
  153. int X509_PURPOSE_get_count(void)
  154. {
  155. if (!xptable)
  156. return X509_PURPOSE_COUNT;
  157. return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
  158. }
  159. X509_PURPOSE *X509_PURPOSE_get0(int idx)
  160. {
  161. if (idx < 0)
  162. return NULL;
  163. if (idx < (int)X509_PURPOSE_COUNT)
  164. return xstandard + idx;
  165. return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
  166. }
  167. int X509_PURPOSE_get_by_sname(char *sname)
  168. {
  169. int i;
  170. X509_PURPOSE *xptmp;
  171. for (i = 0; i < X509_PURPOSE_get_count(); i++) {
  172. xptmp = X509_PURPOSE_get0(i);
  173. if (!strcmp(xptmp->sname, sname))
  174. return i;
  175. }
  176. return -1;
  177. }
  178. int X509_PURPOSE_get_by_id(int purpose)
  179. {
  180. X509_PURPOSE tmp;
  181. size_t idx;
  182. if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
  183. return purpose - X509_PURPOSE_MIN;
  184. tmp.purpose = purpose;
  185. if (!xptable)
  186. return -1;
  187. sk_X509_PURPOSE_sort(xptable);
  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. /* check_ca returns one if |x| should be considered a CA certificate and zero
  506. * otherwise. */
  507. static int check_ca(const X509 *x)
  508. {
  509. /* keyUsage if present should allow cert signing */
  510. if (ku_reject(x, KU_KEY_CERT_SIGN))
  511. return 0;
  512. /* Version 1 certificates are considered CAs and don't have extensions. */
  513. if ((x->ex_flags & V1_ROOT) == V1_ROOT) {
  514. return 1;
  515. }
  516. /* Otherwise, it's only a CA if basicConstraints says so. */
  517. return ((x->ex_flags & EXFLAG_BCONS) &&
  518. (x->ex_flags & EXFLAG_CA));
  519. }
  520. int X509_check_ca(X509 *x)
  521. {
  522. x509v3_cache_extensions(x);
  523. return check_ca(x);
  524. }
  525. static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
  526. int ca)
  527. {
  528. if (xku_reject(x, XKU_SSL_CLIENT))
  529. return 0;
  530. if (ca)
  531. return check_ca(x);
  532. /* We need to do digital signatures or key agreement */
  533. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
  534. return 0;
  535. /* nsCertType if present should allow SSL client use */
  536. if (ns_reject(x, NS_SSL_CLIENT))
  537. return 0;
  538. return 1;
  539. }
  540. /*
  541. * Key usage needed for TLS/SSL server: digital signature, encipherment or
  542. * key agreement. The ssl code can check this more thoroughly for individual
  543. * key types.
  544. */
  545. #define KU_TLS \
  546. (KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT)
  547. static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  548. int ca)
  549. {
  550. if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
  551. return 0;
  552. if (ca)
  553. return check_ca(x);
  554. if (ns_reject(x, NS_SSL_SERVER))
  555. return 0;
  556. if (ku_reject(x, KU_TLS))
  557. return 0;
  558. return 1;
  559. }
  560. static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  561. int ca)
  562. {
  563. int ret;
  564. ret = check_purpose_ssl_server(xp, x, ca);
  565. if (!ret || ca)
  566. return ret;
  567. /* We need to encipher or Netscape complains */
  568. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  569. return 0;
  570. return ret;
  571. }
  572. /* purpose_smime returns one if |x| is a valid S/MIME leaf (|ca| is zero) or CA
  573. * (|ca| is one) certificate, and zero otherwise. */
  574. static int purpose_smime(const X509 *x, int ca)
  575. {
  576. if (xku_reject(x, XKU_SMIME))
  577. return 0;
  578. if (ca) {
  579. /* check nsCertType if present */
  580. if ((x->ex_flags & EXFLAG_NSCERT) &&
  581. (x->ex_nscert & NS_SMIME_CA) == 0) {
  582. return 0;
  583. }
  584. return check_ca(x);
  585. }
  586. if (x->ex_flags & EXFLAG_NSCERT) {
  587. return (x->ex_nscert & NS_SMIME) == NS_SMIME;
  588. }
  589. return 1;
  590. }
  591. static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
  592. int ca)
  593. {
  594. int ret;
  595. ret = purpose_smime(x, ca);
  596. if (!ret || ca)
  597. return ret;
  598. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
  599. return 0;
  600. return ret;
  601. }
  602. static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
  603. int ca)
  604. {
  605. int ret;
  606. ret = purpose_smime(x, ca);
  607. if (!ret || ca)
  608. return ret;
  609. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  610. return 0;
  611. return ret;
  612. }
  613. static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
  614. int ca)
  615. {
  616. if (ca) {
  617. return check_ca(x);
  618. }
  619. if (ku_reject(x, KU_CRL_SIGN))
  620. return 0;
  621. return 1;
  622. }
  623. /*
  624. * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
  625. * is valid. Additional checks must be made on the chain.
  626. */
  627. static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
  628. {
  629. if (ca)
  630. return check_ca(x);
  631. /* leaf certificate is checked in OCSP_verify() */
  632. return 1;
  633. }
  634. static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
  635. int ca)
  636. {
  637. int i_ext;
  638. /* If ca is true we must return if this is a valid CA certificate. */
  639. if (ca)
  640. return check_ca(x);
  641. /*
  642. * Check the optional key usage field:
  643. * if Key Usage is present, it must be one of digitalSignature
  644. * and/or nonRepudiation (other values are not consistent and shall
  645. * be rejected).
  646. */
  647. if ((x->ex_flags & EXFLAG_KUSAGE)
  648. && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
  649. !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
  650. return 0;
  651. /* Only time stamp key usage is permitted and it's required. */
  652. if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
  653. return 0;
  654. /* Extended Key Usage MUST be critical */
  655. i_ext = X509_get_ext_by_NID((X509 *)x, NID_ext_key_usage, -1);
  656. if (i_ext >= 0) {
  657. X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
  658. if (!X509_EXTENSION_get_critical(ext))
  659. return 0;
  660. }
  661. return 1;
  662. }
  663. static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
  664. {
  665. return 1;
  666. }
  667. /*
  668. * Various checks to see if one certificate issued the second. This can be
  669. * used to prune a set of possible issuer certificates which have been looked
  670. * up using some simple method such as by subject name. These are: 1. Check
  671. * issuer_name(subject) == subject_name(issuer) 2. If akid(subject) exists
  672. * check it matches issuer 3. If key_usage(issuer) exists check it supports
  673. * certificate signing returns 0 for OK, positive for reason for mismatch,
  674. * reasons match codes for X509_verify_cert()
  675. */
  676. int X509_check_issued(X509 *issuer, X509 *subject)
  677. {
  678. if (X509_NAME_cmp(X509_get_subject_name(issuer),
  679. X509_get_issuer_name(subject)))
  680. return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
  681. x509v3_cache_extensions(issuer);
  682. x509v3_cache_extensions(subject);
  683. if (subject->akid) {
  684. int ret = X509_check_akid(issuer, subject->akid);
  685. if (ret != X509_V_OK)
  686. return ret;
  687. }
  688. if (subject->ex_flags & EXFLAG_PROXY) {
  689. if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
  690. return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
  691. } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
  692. return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
  693. return X509_V_OK;
  694. }
  695. int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
  696. {
  697. if (!akid)
  698. return X509_V_OK;
  699. /* Check key ids (if present) */
  700. if (akid->keyid && issuer->skid &&
  701. ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
  702. return X509_V_ERR_AKID_SKID_MISMATCH;
  703. /* Check serial number */
  704. if (akid->serial &&
  705. ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
  706. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  707. /* Check issuer name */
  708. if (akid->issuer) {
  709. /*
  710. * Ugh, for some peculiar reason AKID includes SEQUENCE OF
  711. * GeneralName. So look for a DirName. There may be more than one but
  712. * we only take any notice of the first.
  713. */
  714. GENERAL_NAMES *gens;
  715. GENERAL_NAME *gen;
  716. X509_NAME *nm = NULL;
  717. size_t i;
  718. gens = akid->issuer;
  719. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  720. gen = sk_GENERAL_NAME_value(gens, i);
  721. if (gen->type == GEN_DIRNAME) {
  722. nm = gen->d.dirn;
  723. break;
  724. }
  725. }
  726. if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
  727. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  728. }
  729. return X509_V_OK;
  730. }
  731. uint32_t X509_get_extension_flags(X509 *x)
  732. {
  733. /* Call for side-effect of computing hash and caching extensions */
  734. X509_check_purpose(x, -1, -1);
  735. return x->ex_flags;
  736. }
  737. uint32_t X509_get_key_usage(X509 *x)
  738. {
  739. /* Call for side-effect of computing hash and caching extensions */
  740. X509_check_purpose(x, -1, -1);
  741. if (x->ex_flags & EXFLAG_KUSAGE)
  742. return x->ex_kusage;
  743. return UINT32_MAX;
  744. }
  745. uint32_t X509_get_extended_key_usage(X509 *x)
  746. {
  747. /* Call for side-effect of computing hash and caching extensions */
  748. X509_check_purpose(x, -1, -1);
  749. if (x->ex_flags & EXFLAG_XKUSAGE)
  750. return x->ex_xkusage;
  751. return UINT32_MAX;
  752. }