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.
 
 
 
 
 
 

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