v3_purp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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-2018 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. x509v3_cache_extensions(x);
  122. /* Return if side-effect only call */
  123. if (id == -1)
  124. return 1;
  125. idx = X509_PURPOSE_get_by_id(id);
  126. if (idx == -1)
  127. return -1;
  128. pt = X509_PURPOSE_get0(idx);
  129. return pt->check_purpose(pt, x, ca);
  130. }
  131. int X509_PURPOSE_set(int *p, int purpose)
  132. {
  133. if (X509_PURPOSE_get_by_id(purpose) == -1) {
  134. X509V3err(X509V3_F_X509_PURPOSE_SET, X509V3_R_INVALID_PURPOSE);
  135. return 0;
  136. }
  137. *p = purpose;
  138. return 1;
  139. }
  140. int X509_PURPOSE_get_count(void)
  141. {
  142. if (!xptable)
  143. return X509_PURPOSE_COUNT;
  144. return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
  145. }
  146. X509_PURPOSE *X509_PURPOSE_get0(int idx)
  147. {
  148. if (idx < 0)
  149. return NULL;
  150. if (idx < (int)X509_PURPOSE_COUNT)
  151. return xstandard + idx;
  152. return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
  153. }
  154. int X509_PURPOSE_get_by_sname(char *sname)
  155. {
  156. int i;
  157. X509_PURPOSE *xptmp;
  158. for (i = 0; i < X509_PURPOSE_get_count(); i++) {
  159. xptmp = X509_PURPOSE_get0(i);
  160. if (!strcmp(xptmp->sname, sname))
  161. return i;
  162. }
  163. return -1;
  164. }
  165. int X509_PURPOSE_get_by_id(int purpose)
  166. {
  167. X509_PURPOSE tmp;
  168. int idx;
  169. if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
  170. return purpose - X509_PURPOSE_MIN;
  171. tmp.purpose = purpose;
  172. if (!xptable)
  173. return -1;
  174. idx = sk_X509_PURPOSE_find(xptable, &tmp);
  175. if (idx == -1)
  176. return -1;
  177. return idx + X509_PURPOSE_COUNT;
  178. }
  179. int X509_PURPOSE_add(int id, int trust, int flags,
  180. int (*ck) (const X509_PURPOSE *, const X509 *, int),
  181. char *name, char *sname, void *arg)
  182. {
  183. int idx;
  184. X509_PURPOSE *ptmp;
  185. /*
  186. * This is set according to what we change: application can't set it
  187. */
  188. flags &= ~X509_PURPOSE_DYNAMIC;
  189. /* This will always be set for application modified trust entries */
  190. flags |= X509_PURPOSE_DYNAMIC_NAME;
  191. /* Get existing entry if any */
  192. idx = X509_PURPOSE_get_by_id(id);
  193. /* Need a new entry */
  194. if (idx == -1) {
  195. if (!(ptmp = OPENSSL_malloc(sizeof(X509_PURPOSE)))) {
  196. X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
  197. return 0;
  198. }
  199. ptmp->flags = X509_PURPOSE_DYNAMIC;
  200. } else
  201. ptmp = X509_PURPOSE_get0(idx);
  202. /* OPENSSL_free existing name if dynamic */
  203. if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
  204. OPENSSL_free(ptmp->name);
  205. OPENSSL_free(ptmp->sname);
  206. }
  207. /* dup supplied name */
  208. ptmp->name = BUF_strdup(name);
  209. ptmp->sname = BUF_strdup(sname);
  210. if (!ptmp->name || !ptmp->sname) {
  211. X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
  212. return 0;
  213. }
  214. /* Keep the dynamic flag of existing entry */
  215. ptmp->flags &= X509_PURPOSE_DYNAMIC;
  216. /* Set all other flags */
  217. ptmp->flags |= flags;
  218. ptmp->purpose = id;
  219. ptmp->trust = trust;
  220. ptmp->check_purpose = ck;
  221. ptmp->usr_data = arg;
  222. /* If its a new entry manage the dynamic table */
  223. if (idx == -1) {
  224. if (!xptable && !(xptable = sk_X509_PURPOSE_new(xp_cmp))) {
  225. X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
  226. return 0;
  227. }
  228. if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
  229. X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
  230. return 0;
  231. }
  232. }
  233. return 1;
  234. }
  235. static void xptable_free(X509_PURPOSE *p)
  236. {
  237. if (!p)
  238. return;
  239. if (p->flags & X509_PURPOSE_DYNAMIC) {
  240. if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
  241. OPENSSL_free(p->name);
  242. OPENSSL_free(p->sname);
  243. }
  244. OPENSSL_free(p);
  245. }
  246. }
  247. void X509_PURPOSE_cleanup(void)
  248. {
  249. unsigned int i;
  250. sk_X509_PURPOSE_pop_free(xptable, xptable_free);
  251. for (i = 0; i < X509_PURPOSE_COUNT; i++)
  252. xptable_free(xstandard + i);
  253. xptable = NULL;
  254. }
  255. int X509_PURPOSE_get_id(X509_PURPOSE *xp)
  256. {
  257. return xp->purpose;
  258. }
  259. char *X509_PURPOSE_get0_name(X509_PURPOSE *xp)
  260. {
  261. return xp->name;
  262. }
  263. char *X509_PURPOSE_get0_sname(X509_PURPOSE *xp)
  264. {
  265. return xp->sname;
  266. }
  267. int X509_PURPOSE_get_trust(X509_PURPOSE *xp)
  268. {
  269. return xp->trust;
  270. }
  271. static int nid_cmp(const int *a, const int *b)
  272. {
  273. return *a - *b;
  274. }
  275. DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid);
  276. IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid);
  277. int X509_supported_extension(X509_EXTENSION *ex)
  278. {
  279. /*
  280. * This table is a list of the NIDs of supported extensions: that is
  281. * those which are used by the verify process. If an extension is
  282. * critical and doesn't appear in this list then the verify process will
  283. * normally reject the certificate. The list must be kept in numerical
  284. * order because it will be searched using bsearch.
  285. */
  286. static const int supported_nids[] = {
  287. NID_netscape_cert_type, /* 71 */
  288. NID_key_usage, /* 83 */
  289. NID_subject_alt_name, /* 85 */
  290. NID_basic_constraints, /* 87 */
  291. NID_certificate_policies, /* 89 */
  292. NID_crl_distribution_points, /* 103 */
  293. NID_ext_key_usage, /* 126 */
  294. #ifndef OPENSSL_NO_RFC3779
  295. NID_sbgp_ipAddrBlock, /* 290 */
  296. NID_sbgp_autonomousSysNum, /* 291 */
  297. #endif
  298. NID_policy_constraints, /* 401 */
  299. NID_proxyCertInfo, /* 663 */
  300. NID_name_constraints, /* 666 */
  301. NID_policy_mappings, /* 747 */
  302. NID_inhibit_any_policy /* 748 */
  303. };
  304. int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
  305. if (ex_nid == NID_undef)
  306. return 0;
  307. if (OBJ_bsearch_nid(&ex_nid, supported_nids,
  308. sizeof(supported_nids) / sizeof(int)))
  309. return 1;
  310. return 0;
  311. }
  312. static void setup_dp(X509 *x, DIST_POINT *dp)
  313. {
  314. X509_NAME *iname = NULL;
  315. int i;
  316. if (dp->reasons) {
  317. if (dp->reasons->length > 0)
  318. dp->dp_reasons = dp->reasons->data[0];
  319. if (dp->reasons->length > 1)
  320. dp->dp_reasons |= (dp->reasons->data[1] << 8);
  321. dp->dp_reasons &= CRLDP_ALL_REASONS;
  322. } else
  323. dp->dp_reasons = CRLDP_ALL_REASONS;
  324. if (!dp->distpoint || (dp->distpoint->type != 1))
  325. return;
  326. for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
  327. GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
  328. if (gen->type == GEN_DIRNAME) {
  329. iname = gen->d.directoryName;
  330. break;
  331. }
  332. }
  333. if (!iname)
  334. iname = X509_get_issuer_name(x);
  335. DIST_POINT_set_dpname(dp->distpoint, iname);
  336. }
  337. static void setup_crldp(X509 *x)
  338. {
  339. int i;
  340. x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL);
  341. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++)
  342. setup_dp(x, sk_DIST_POINT_value(x->crldp, i));
  343. }
  344. #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
  345. #define ku_reject(x, usage) \
  346. (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
  347. #define xku_reject(x, usage) \
  348. (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
  349. #define ns_reject(x, usage) \
  350. (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
  351. static void x509v3_cache_extensions(X509 *x)
  352. {
  353. BASIC_CONSTRAINTS *bs;
  354. PROXY_CERT_INFO_EXTENSION *pci;
  355. ASN1_BIT_STRING *usage;
  356. ASN1_BIT_STRING *ns;
  357. EXTENDED_KEY_USAGE *extusage;
  358. X509_EXTENSION *ex;
  359. int i;
  360. CRYPTO_w_lock(CRYPTO_LOCK_X509);
  361. if (x->ex_flags & EXFLAG_SET) {
  362. CRYPTO_w_unlock(CRYPTO_LOCK_X509);
  363. return;
  364. }
  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. CRYPTO_w_unlock(CRYPTO_LOCK_X509);
  491. }
  492. /*-
  493. * CA checks common to all purposes
  494. * return codes:
  495. * 0 not a CA
  496. * 1 is a CA
  497. * 2 basicConstraints absent so "maybe" a CA
  498. * 3 basicConstraints absent but self signed V1.
  499. * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
  500. */
  501. static int check_ca(const X509 *x)
  502. {
  503. /* keyUsage if present should allow cert signing */
  504. if (ku_reject(x, KU_KEY_CERT_SIGN))
  505. return 0;
  506. if (x->ex_flags & EXFLAG_BCONS) {
  507. if (x->ex_flags & EXFLAG_CA)
  508. return 1;
  509. /* If basicConstraints says not a CA then say so */
  510. else
  511. return 0;
  512. } else {
  513. /* we support V1 roots for... uh, I don't really know why. */
  514. if ((x->ex_flags & V1_ROOT) == V1_ROOT)
  515. return 3;
  516. /*
  517. * If key usage present it must have certSign so tolerate it
  518. */
  519. else if (x->ex_flags & EXFLAG_KUSAGE)
  520. return 4;
  521. /* Older certificates could have Netscape-specific CA types */
  522. else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
  523. return 5;
  524. /* can this still be regarded a CA certificate? I doubt it */
  525. return 0;
  526. }
  527. }
  528. int X509_check_ca(X509 *x)
  529. {
  530. x509v3_cache_extensions(x);
  531. return check_ca(x);
  532. }
  533. /* Check SSL CA: common checks for SSL client and server */
  534. static int check_ssl_ca(const X509 *x)
  535. {
  536. int ca_ret;
  537. ca_ret = check_ca(x);
  538. if (!ca_ret)
  539. return 0;
  540. /* check nsCertType if present */
  541. if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
  542. return ca_ret;
  543. else
  544. return 0;
  545. }
  546. static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
  547. int ca)
  548. {
  549. if (xku_reject(x, XKU_SSL_CLIENT))
  550. return 0;
  551. if (ca)
  552. return check_ssl_ca(x);
  553. /* We need to do digital signatures or key agreement */
  554. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
  555. return 0;
  556. /* nsCertType if present should allow SSL client use */
  557. if (ns_reject(x, NS_SSL_CLIENT))
  558. return 0;
  559. return 1;
  560. }
  561. /*
  562. * Key usage needed for TLS/SSL server: digital signature, encipherment or
  563. * key agreement. The ssl code can check this more thoroughly for individual
  564. * key types.
  565. */
  566. #define KU_TLS \
  567. KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
  568. static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  569. int ca)
  570. {
  571. if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
  572. return 0;
  573. if (ca)
  574. return check_ssl_ca(x);
  575. if (ns_reject(x, NS_SSL_SERVER))
  576. return 0;
  577. if (ku_reject(x, KU_TLS))
  578. return 0;
  579. return 1;
  580. }
  581. static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
  582. int ca)
  583. {
  584. int ret;
  585. ret = check_purpose_ssl_server(xp, x, ca);
  586. if (!ret || ca)
  587. return ret;
  588. /* We need to encipher or Netscape complains */
  589. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  590. return 0;
  591. return ret;
  592. }
  593. /* common S/MIME checks */
  594. static int purpose_smime(const X509 *x, int ca)
  595. {
  596. if (xku_reject(x, XKU_SMIME))
  597. return 0;
  598. if (ca) {
  599. int ca_ret;
  600. ca_ret = check_ca(x);
  601. if (!ca_ret)
  602. return 0;
  603. /* check nsCertType if present */
  604. if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
  605. return ca_ret;
  606. else
  607. return 0;
  608. }
  609. if (x->ex_flags & EXFLAG_NSCERT) {
  610. if (x->ex_nscert & NS_SMIME)
  611. return 1;
  612. /* Workaround for some buggy certificates */
  613. if (x->ex_nscert & NS_SSL_CLIENT)
  614. return 2;
  615. return 0;
  616. }
  617. return 1;
  618. }
  619. static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
  620. int ca)
  621. {
  622. int ret;
  623. ret = purpose_smime(x, ca);
  624. if (!ret || ca)
  625. return ret;
  626. if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
  627. return 0;
  628. return ret;
  629. }
  630. static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
  631. int ca)
  632. {
  633. int ret;
  634. ret = purpose_smime(x, ca);
  635. if (!ret || ca)
  636. return ret;
  637. if (ku_reject(x, KU_KEY_ENCIPHERMENT))
  638. return 0;
  639. return ret;
  640. }
  641. static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
  642. int ca)
  643. {
  644. if (ca) {
  645. int ca_ret;
  646. if ((ca_ret = check_ca(x)) != 2)
  647. return ca_ret;
  648. else
  649. return 0;
  650. }
  651. if (ku_reject(x, KU_CRL_SIGN))
  652. return 0;
  653. return 1;
  654. }
  655. /*
  656. * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
  657. * is valid. Additional checks must be made on the chain.
  658. */
  659. static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
  660. {
  661. /*
  662. * Must be a valid CA. Should we really support the "I don't know" value
  663. * (2)?
  664. */
  665. if (ca)
  666. return check_ca(x);
  667. /* leaf certificate is checked in OCSP_verify() */
  668. return 1;
  669. }
  670. static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
  671. int ca)
  672. {
  673. int i_ext;
  674. /* If ca is true we must return if this is a valid CA certificate. */
  675. if (ca)
  676. return check_ca(x);
  677. /*
  678. * Check the optional key usage field:
  679. * if Key Usage is present, it must be one of digitalSignature
  680. * and/or nonRepudiation (other values are not consistent and shall
  681. * be rejected).
  682. */
  683. if ((x->ex_flags & EXFLAG_KUSAGE)
  684. && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
  685. !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
  686. return 0;
  687. /* Only time stamp key usage is permitted and it's required. */
  688. if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
  689. return 0;
  690. /* Extended Key Usage MUST be critical */
  691. i_ext = X509_get_ext_by_NID((X509 *)x, NID_ext_key_usage, -1);
  692. if (i_ext >= 0) {
  693. X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
  694. if (!X509_EXTENSION_get_critical(ext))
  695. return 0;
  696. }
  697. return 1;
  698. }
  699. static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
  700. {
  701. return 1;
  702. }
  703. /*-
  704. * Various checks to see if one certificate issued the second.
  705. * This can be used to prune a set of possible issuer certificates
  706. * which have been looked up using some simple method such as by
  707. * subject name.
  708. * These are:
  709. * 1. Check issuer_name(subject) == subject_name(issuer)
  710. * 2. If akid(subject) exists check it matches issuer
  711. * 3. If key_usage(issuer) exists check it supports certificate signing
  712. * returns 0 for OK, positive for reason for mismatch, reasons match
  713. * codes for X509_verify_cert()
  714. */
  715. int X509_check_issued(X509 *issuer, X509 *subject)
  716. {
  717. if (X509_NAME_cmp(X509_get_subject_name(issuer),
  718. X509_get_issuer_name(subject)))
  719. return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
  720. x509v3_cache_extensions(issuer);
  721. x509v3_cache_extensions(subject);
  722. if (subject->akid) {
  723. int ret = X509_check_akid(issuer, subject->akid);
  724. if (ret != X509_V_OK)
  725. return ret;
  726. }
  727. if (subject->ex_flags & EXFLAG_PROXY) {
  728. if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
  729. return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
  730. } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
  731. return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
  732. return X509_V_OK;
  733. }
  734. int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
  735. {
  736. if (!akid)
  737. return X509_V_OK;
  738. /* Check key ids (if present) */
  739. if (akid->keyid && issuer->skid &&
  740. ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
  741. return X509_V_ERR_AKID_SKID_MISMATCH;
  742. /* Check serial number */
  743. if (akid->serial &&
  744. ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
  745. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  746. /* Check issuer name */
  747. if (akid->issuer) {
  748. /*
  749. * Ugh, for some peculiar reason AKID includes SEQUENCE OF
  750. * GeneralName. So look for a DirName. There may be more than one but
  751. * we only take any notice of the first.
  752. */
  753. GENERAL_NAMES *gens;
  754. GENERAL_NAME *gen;
  755. X509_NAME *nm = NULL;
  756. int i;
  757. gens = akid->issuer;
  758. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  759. gen = sk_GENERAL_NAME_value(gens, i);
  760. if (gen->type == GEN_DIRNAME) {
  761. nm = gen->d.dirn;
  762. break;
  763. }
  764. }
  765. if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
  766. return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  767. }
  768. return X509_V_OK;
  769. }