v3_purp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /* v3_purp.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  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_crl_distribution_points, /* 103 */
  296. NID_ext_key_usage, /* 126 */
  297. #ifndef OPENSSL_NO_RFC3779
  298. NID_sbgp_ipAddrBlock, /* 290 */
  299. NID_sbgp_autonomousSysNum, /* 291 */
  300. #endif
  301. NID_policy_constraints, /* 401 */
  302. NID_proxyCertInfo, /* 663 */
  303. NID_name_constraints, /* 666 */
  304. NID_policy_mappings, /* 747 */
  305. NID_inhibit_any_policy /* 748 */
  306. };
  307. int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
  308. if (ex_nid == NID_undef)
  309. return 0;
  310. if (OBJ_bsearch_nid(&ex_nid, supported_nids,
  311. sizeof(supported_nids) / sizeof(int)))
  312. return 1;
  313. return 0;
  314. }
  315. static void setup_dp(X509 *x, DIST_POINT *dp)
  316. {
  317. X509_NAME *iname = NULL;
  318. int i;
  319. if (dp->reasons) {
  320. if (dp->reasons->length > 0)
  321. dp->dp_reasons = dp->reasons->data[0];
  322. if (dp->reasons->length > 1)
  323. dp->dp_reasons |= (dp->reasons->data[1] << 8);
  324. dp->dp_reasons &= CRLDP_ALL_REASONS;
  325. } else
  326. dp->dp_reasons = CRLDP_ALL_REASONS;
  327. if (!dp->distpoint || (dp->distpoint->type != 1))
  328. return;
  329. for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
  330. GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
  331. if (gen->type == GEN_DIRNAME) {
  332. iname = gen->d.directoryName;
  333. break;
  334. }
  335. }
  336. if (!iname)
  337. iname = X509_get_issuer_name(x);
  338. DIST_POINT_set_dpname(dp->distpoint, iname);
  339. }
  340. static void setup_crldp(X509 *x)
  341. {
  342. int i;
  343. x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL);
  344. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++)
  345. setup_dp(x, sk_DIST_POINT_value(x->crldp, i));
  346. }
  347. #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
  348. #define ku_reject(x, usage) \
  349. (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
  350. #define xku_reject(x, usage) \
  351. (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
  352. #define ns_reject(x, usage) \
  353. (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
  354. static void x509v3_cache_extensions(X509 *x)
  355. {
  356. BASIC_CONSTRAINTS *bs;
  357. PROXY_CERT_INFO_EXTENSION *pci;
  358. ASN1_BIT_STRING *usage;
  359. ASN1_BIT_STRING *ns;
  360. EXTENDED_KEY_USAGE *extusage;
  361. X509_EXTENSION *ex;
  362. int i;
  363. if (x->ex_flags & EXFLAG_SET)
  364. return;
  365. #ifndef OPENSSL_NO_SHA
  366. X509_digest(x, EVP_sha1(), x->sha1_hash, NULL);
  367. #endif
  368. /* V1 should mean no extensions ... */
  369. if (!X509_get_version(x))
  370. x->ex_flags |= EXFLAG_V1;
  371. /* Handle basic constraints */
  372. if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {
  373. if (bs->ca)
  374. x->ex_flags |= EXFLAG_CA;
  375. if (bs->pathlen) {
  376. if ((bs->pathlen->type == V_ASN1_NEG_INTEGER)
  377. || !bs->ca) {
  378. x->ex_flags |= EXFLAG_INVALID;
  379. x->ex_pathlen = 0;
  380. } else
  381. x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
  382. } else
  383. x->ex_pathlen = -1;
  384. BASIC_CONSTRAINTS_free(bs);
  385. x->ex_flags |= EXFLAG_BCONS;
  386. }
  387. /* Handle proxy certificates */
  388. if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, NULL, NULL))) {
  389. if (x->ex_flags & EXFLAG_CA
  390. || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
  391. || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
  392. x->ex_flags |= EXFLAG_INVALID;
  393. }
  394. if (pci->pcPathLengthConstraint) {
  395. x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
  396. } else
  397. x->ex_pcpathlen = -1;
  398. PROXY_CERT_INFO_EXTENSION_free(pci);
  399. x->ex_flags |= EXFLAG_PROXY;
  400. }
  401. /* Handle key usage */
  402. if ((usage = X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {
  403. if (usage->length > 0) {
  404. x->ex_kusage = usage->data[0];
  405. if (usage->length > 1)
  406. x->ex_kusage |= usage->data[1] << 8;
  407. } else
  408. x->ex_kusage = 0;
  409. x->ex_flags |= EXFLAG_KUSAGE;
  410. ASN1_BIT_STRING_free(usage);
  411. }
  412. x->ex_xkusage = 0;
  413. if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {
  414. x->ex_flags |= EXFLAG_XKUSAGE;
  415. for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
  416. switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
  417. case NID_server_auth:
  418. x->ex_xkusage |= XKU_SSL_SERVER;
  419. break;
  420. case NID_client_auth:
  421. x->ex_xkusage |= XKU_SSL_CLIENT;
  422. break;
  423. case NID_email_protect:
  424. x->ex_xkusage |= XKU_SMIME;
  425. break;
  426. case NID_code_sign:
  427. x->ex_xkusage |= XKU_CODE_SIGN;
  428. break;
  429. case NID_ms_sgc:
  430. case NID_ns_sgc:
  431. x->ex_xkusage |= XKU_SGC;
  432. break;
  433. case NID_OCSP_sign:
  434. x->ex_xkusage |= XKU_OCSP_SIGN;
  435. break;
  436. case NID_time_stamp:
  437. x->ex_xkusage |= XKU_TIMESTAMP;
  438. break;
  439. case NID_dvcs:
  440. x->ex_xkusage |= XKU_DVCS;
  441. break;
  442. case NID_anyExtendedKeyUsage:
  443. x->ex_xkusage |= XKU_ANYEKU;
  444. break;
  445. }
  446. }
  447. sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
  448. }
  449. if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {
  450. if (ns->length > 0)
  451. x->ex_nscert = ns->data[0];
  452. else
  453. x->ex_nscert = 0;
  454. x->ex_flags |= EXFLAG_NSCERT;
  455. ASN1_BIT_STRING_free(ns);
  456. }
  457. x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL);
  458. x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL);
  459. /* Does subject name match issuer ? */
  460. if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
  461. x->ex_flags |= EXFLAG_SI;
  462. /* If SKID matches AKID also indicate self signed */
  463. if (X509_check_akid(x, x->akid) == X509_V_OK &&
  464. !ku_reject(x, KU_KEY_CERT_SIGN))
  465. x->ex_flags |= EXFLAG_SS;
  466. }
  467. x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  468. x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
  469. if (!x->nc && (i != -1))
  470. x->ex_flags |= EXFLAG_INVALID;
  471. setup_crldp(x);
  472. #ifndef OPENSSL_NO_RFC3779
  473. x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, NULL, NULL);
  474. x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum,
  475. NULL, NULL);
  476. #endif
  477. for (i = 0; i < X509_get_ext_count(x); i++) {
  478. ex = X509_get_ext(x, i);
  479. if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
  480. == NID_freshest_crl)
  481. x->ex_flags |= EXFLAG_FRESHEST;
  482. if (!X509_EXTENSION_get_critical(ex))
  483. continue;
  484. if (!X509_supported_extension(ex)) {
  485. x->ex_flags |= EXFLAG_CRITICAL;
  486. break;
  487. }
  488. }
  489. x->ex_flags |= EXFLAG_SET;
  490. }
  491. /*-
  492. * CA checks common to all purposes
  493. * return codes:
  494. * 0 not a CA
  495. * 1 is a CA
  496. * 2 basicConstraints absent so "maybe" a CA
  497. * 3 basicConstraints absent but self signed V1.
  498. * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
  499. */
  500. static int check_ca(const X509 *x)
  501. {
  502. /* keyUsage if present should allow cert signing */
  503. if (ku_reject(x, KU_KEY_CERT_SIGN))
  504. return 0;
  505. if (x->ex_flags & EXFLAG_BCONS) {
  506. if (x->ex_flags & EXFLAG_CA)
  507. return 1;
  508. /* If basicConstraints says not a CA then say so */
  509. else
  510. return 0;
  511. } else {
  512. /* we support V1 roots for... uh, I don't really know why. */
  513. if ((x->ex_flags & V1_ROOT) == V1_ROOT)
  514. return 3;
  515. /*
  516. * If key usage present it must have certSign so tolerate it
  517. */
  518. else if (x->ex_flags & EXFLAG_KUSAGE)
  519. return 4;
  520. /* Older certificates could have Netscape-specific CA types */
  521. else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
  522. return 5;
  523. /* can this still be regarded a CA certificate? I doubt it */
  524. return 0;
  525. }
  526. }
  527. int X509_check_ca(X509 *x)
  528. {
  529. if (!(x->ex_flags & EXFLAG_SET)) {
  530. CRYPTO_w_lock(CRYPTO_LOCK_X509);
  531. x509v3_cache_extensions(x);
  532. CRYPTO_w_unlock(CRYPTO_LOCK_X509);
  533. }
  534. return check_ca(x);
  535. }
  536. /* Check SSL CA: common checks for SSL client and server */
  537. static int check_ssl_ca(const X509 *x)
  538. {
  539. int ca_ret;
  540. ca_ret = check_ca(x);
  541. if (!ca_ret)
  542. return 0;
  543. /* check nsCertType if present */
  544. if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
  545. return ca_ret;
  546. else
  547. return 0;
  548. }
  549. static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
  550. int ca)
  551. {
  552. if (xku_reject(x, XKU_SSL_CLIENT))
  553. return 0;
  554. if (ca)
  555. return check_ssl_ca(x);
  556. /* We need to do digital signatures or key agreement */
  557. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
  558. return 0;
  559. /* nsCertType if present should allow SSL client use */
  560. if (ns_reject(x, NS_SSL_CLIENT))
  561. return 0;
  562. return 1;
  563. }
  564. /*
  565. * Key usage needed for TLS/SSL server: digital signature, encipherment or
  566. * key agreement. The ssl code can check this more thoroughly for individual
  567. * key types.
  568. */
  569. #define KU_TLS \
  570. KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
  571. static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  572. int ca)
  573. {
  574. if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
  575. return 0;
  576. if (ca)
  577. return check_ssl_ca(x);
  578. if (ns_reject(x, NS_SSL_SERVER))
  579. return 0;
  580. if (ku_reject(x, KU_TLS))
  581. return 0;
  582. return 1;
  583. }
  584. static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  585. int ca)
  586. {
  587. int ret;
  588. ret = check_purpose_ssl_server(xp, x, ca);
  589. if (!ret || ca)
  590. return ret;
  591. /* We need to encipher or Netscape complains */
  592. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  593. return 0;
  594. return ret;
  595. }
  596. /* common S/MIME checks */
  597. static int purpose_smime(const X509 *x, int ca)
  598. {
  599. if (xku_reject(x, XKU_SMIME))
  600. return 0;
  601. if (ca) {
  602. int ca_ret;
  603. ca_ret = check_ca(x);
  604. if (!ca_ret)
  605. return 0;
  606. /* check nsCertType if present */
  607. if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
  608. return ca_ret;
  609. else
  610. return 0;
  611. }
  612. if (x->ex_flags & EXFLAG_NSCERT) {
  613. if (x->ex_nscert & NS_SMIME)
  614. return 1;
  615. /* Workaround for some buggy certificates */
  616. if (x->ex_nscert & NS_SSL_CLIENT)
  617. return 2;
  618. return 0;
  619. }
  620. return 1;
  621. }
  622. static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
  623. int ca)
  624. {
  625. int ret;
  626. ret = purpose_smime(x, ca);
  627. if (!ret || ca)
  628. return ret;
  629. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
  630. return 0;
  631. return ret;
  632. }
  633. static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
  634. int ca)
  635. {
  636. int ret;
  637. ret = purpose_smime(x, ca);
  638. if (!ret || ca)
  639. return ret;
  640. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  641. return 0;
  642. return ret;
  643. }
  644. static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
  645. int ca)
  646. {
  647. if (ca) {
  648. int ca_ret;
  649. if ((ca_ret = check_ca(x)) != 2)
  650. return ca_ret;
  651. else
  652. return 0;
  653. }
  654. if (ku_reject(x, KU_CRL_SIGN))
  655. return 0;
  656. return 1;
  657. }
  658. /*
  659. * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
  660. * is valid. Additional checks must be made on the chain.
  661. */
  662. static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
  663. {
  664. /*
  665. * Must be a valid CA. Should we really support the "I don't know" value
  666. * (2)?
  667. */
  668. if (ca)
  669. return check_ca(x);
  670. /* leaf certificate is checked in OCSP_verify() */
  671. return 1;
  672. }
  673. static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
  674. int ca)
  675. {
  676. int i_ext;
  677. /* If ca is true we must return if this is a valid CA certificate. */
  678. if (ca)
  679. return check_ca(x);
  680. /*
  681. * Check the optional key usage field:
  682. * if Key Usage is present, it must be one of digitalSignature
  683. * and/or nonRepudiation (other values are not consistent and shall
  684. * be rejected).
  685. */
  686. if ((x->ex_flags & EXFLAG_KUSAGE)
  687. && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
  688. !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
  689. return 0;
  690. /* Only time stamp key usage is permitted and it's required. */
  691. if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
  692. return 0;
  693. /* Extended Key Usage MUST be critical */
  694. i_ext = X509_get_ext_by_NID((X509 *)x, NID_ext_key_usage, -1);
  695. if (i_ext >= 0) {
  696. X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
  697. if (!X509_EXTENSION_get_critical(ext))
  698. return 0;
  699. }
  700. return 1;
  701. }
  702. static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
  703. {
  704. return 1;
  705. }
  706. /*-
  707. * Various checks to see if one certificate issued the second.
  708. * This can be used to prune a set of possible issuer certificates
  709. * which have been looked up using some simple method such as by
  710. * subject name.
  711. * These are:
  712. * 1. Check issuer_name(subject) == subject_name(issuer)
  713. * 2. If akid(subject) exists check it matches issuer
  714. * 3. If key_usage(issuer) exists check it supports certificate signing
  715. * returns 0 for OK, positive for reason for mismatch, reasons match
  716. * codes for X509_verify_cert()
  717. */
  718. int X509_check_issued(X509 *issuer, X509 *subject)
  719. {
  720. if (X509_NAME_cmp(X509_get_subject_name(issuer),
  721. X509_get_issuer_name(subject)))
  722. return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
  723. x509v3_cache_extensions(issuer);
  724. x509v3_cache_extensions(subject);
  725. if (subject->akid) {
  726. int ret = X509_check_akid(issuer, subject->akid);
  727. if (ret != X509_V_OK)
  728. return ret;
  729. }
  730. if (subject->ex_flags & EXFLAG_PROXY) {
  731. if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
  732. return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
  733. } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
  734. return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
  735. return X509_V_OK;
  736. }
  737. int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
  738. {
  739. if (!akid)
  740. return X509_V_OK;
  741. /* Check key ids (if present) */
  742. if (akid->keyid && issuer->skid &&
  743. ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
  744. return X509_V_ERR_AKID_SKID_MISMATCH;
  745. /* Check serial number */
  746. if (akid->serial &&
  747. ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
  748. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  749. /* Check issuer name */
  750. if (akid->issuer) {
  751. /*
  752. * Ugh, for some peculiar reason AKID includes SEQUENCE OF
  753. * GeneralName. So look for a DirName. There may be more than one but
  754. * we only take any notice of the first.
  755. */
  756. GENERAL_NAMES *gens;
  757. GENERAL_NAME *gen;
  758. X509_NAME *nm = NULL;
  759. int i;
  760. gens = akid->issuer;
  761. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  762. gen = sk_GENERAL_NAME_value(gens, i);
  763. if (gen->type == GEN_DIRNAME) {
  764. nm = gen->d.dirn;
  765. break;
  766. }
  767. }
  768. if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
  769. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  770. }
  771. return X509_V_OK;
  772. }