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.
 
 
 
 
 
 

785 lines
23 KiB

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