v3_purp.c 26 KB

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