v3_purp.c 26 KB

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