x509_vpm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright 2004-2017 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 <openssl/crypto.h>
  12. #include <openssl/buffer.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/x509v3.h>
  15. #include "internal/x509_int.h"
  16. #include "x509_lcl.h"
  17. /* X509_VERIFY_PARAM functions */
  18. #define SET_HOST 0
  19. #define ADD_HOST 1
  20. static char *str_copy(const char *s)
  21. {
  22. return OPENSSL_strdup(s);
  23. }
  24. static void str_free(char *s)
  25. {
  26. OPENSSL_free(s);
  27. }
  28. static int int_x509_param_set_hosts(X509_VERIFY_PARAM *vpm, int mode,
  29. const char *name, size_t namelen)
  30. {
  31. char *copy;
  32. /*
  33. * Refuse names with embedded NUL bytes, except perhaps as final byte.
  34. * XXX: Do we need to push an error onto the error stack?
  35. */
  36. if (namelen == 0 || name == NULL)
  37. namelen = name ? strlen(name) : 0;
  38. else if (name && memchr(name, '\0', namelen > 1 ? namelen - 1 : namelen))
  39. return 0;
  40. if (namelen > 0 && name[namelen - 1] == '\0')
  41. --namelen;
  42. if (mode == SET_HOST) {
  43. sk_OPENSSL_STRING_pop_free(vpm->hosts, str_free);
  44. vpm->hosts = NULL;
  45. }
  46. if (name == NULL || namelen == 0)
  47. return 1;
  48. copy = OPENSSL_strndup(name, namelen);
  49. if (copy == NULL)
  50. return 0;
  51. if (vpm->hosts == NULL &&
  52. (vpm->hosts = sk_OPENSSL_STRING_new_null()) == NULL) {
  53. OPENSSL_free(copy);
  54. return 0;
  55. }
  56. if (!sk_OPENSSL_STRING_push(vpm->hosts, copy)) {
  57. OPENSSL_free(copy);
  58. if (sk_OPENSSL_STRING_num(vpm->hosts) == 0) {
  59. sk_OPENSSL_STRING_free(vpm->hosts);
  60. vpm->hosts = NULL;
  61. }
  62. return 0;
  63. }
  64. return 1;
  65. }
  66. X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void)
  67. {
  68. X509_VERIFY_PARAM *param;
  69. param = OPENSSL_zalloc(sizeof(*param));
  70. if (param == NULL)
  71. return NULL;
  72. param->trust = X509_TRUST_DEFAULT;
  73. /*
  74. * param->inh_flags = X509_VP_FLAG_DEFAULT;
  75. */
  76. param->inh_flags = 0;
  77. param->depth = -1;
  78. param->auth_level = -1; /* -1 means unset, 0 is explicit */
  79. return param;
  80. }
  81. void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
  82. {
  83. if (param == NULL)
  84. return;
  85. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  86. sk_OPENSSL_STRING_pop_free(param->hosts, str_free);
  87. OPENSSL_free(param->peername);
  88. OPENSSL_free(param->email);
  89. OPENSSL_free(param->ip);
  90. OPENSSL_free(param);
  91. }
  92. /*-
  93. * This function determines how parameters are "inherited" from one structure
  94. * to another. There are several different ways this can happen.
  95. *
  96. * 1. If a child structure needs to have its values initialized from a parent
  97. * they are simply copied across. For example SSL_CTX copied to SSL.
  98. * 2. If the structure should take on values only if they are currently unset.
  99. * For example the values in an SSL structure will take appropriate value
  100. * for SSL servers or clients but only if the application has not set new
  101. * ones.
  102. *
  103. * The "inh_flags" field determines how this function behaves.
  104. *
  105. * Normally any values which are set in the default are not copied from the
  106. * destination and verify flags are ORed together.
  107. *
  108. * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
  109. * to the destination. Effectively the values in "to" become default values
  110. * which will be used only if nothing new is set in "from".
  111. *
  112. * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
  113. * they are set or not. Flags is still Ored though.
  114. *
  115. * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
  116. * of ORed.
  117. *
  118. * If X509_VP_FLAG_LOCKED is set then no values are copied.
  119. *
  120. * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
  121. * after the next call.
  122. */
  123. /* Macro to test if a field should be copied from src to dest */
  124. #define test_x509_verify_param_copy(field, def) \
  125. (to_overwrite || \
  126. ((src->field != def) && (to_default || (dest->field == def))))
  127. /* Macro to test and copy a field if necessary */
  128. #define x509_verify_param_copy(field, def) \
  129. if (test_x509_verify_param_copy(field, def)) \
  130. dest->field = src->field
  131. int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
  132. const X509_VERIFY_PARAM *src)
  133. {
  134. unsigned long inh_flags;
  135. int to_default, to_overwrite;
  136. if (!src)
  137. return 1;
  138. inh_flags = dest->inh_flags | src->inh_flags;
  139. if (inh_flags & X509_VP_FLAG_ONCE)
  140. dest->inh_flags = 0;
  141. if (inh_flags & X509_VP_FLAG_LOCKED)
  142. return 1;
  143. if (inh_flags & X509_VP_FLAG_DEFAULT)
  144. to_default = 1;
  145. else
  146. to_default = 0;
  147. if (inh_flags & X509_VP_FLAG_OVERWRITE)
  148. to_overwrite = 1;
  149. else
  150. to_overwrite = 0;
  151. x509_verify_param_copy(purpose, 0);
  152. x509_verify_param_copy(trust, X509_TRUST_DEFAULT);
  153. x509_verify_param_copy(depth, -1);
  154. x509_verify_param_copy(auth_level, -1);
  155. /* If overwrite or check time not set, copy across */
  156. if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
  157. dest->check_time = src->check_time;
  158. dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
  159. /* Don't need to copy flag: that is done below */
  160. }
  161. if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
  162. dest->flags = 0;
  163. dest->flags |= src->flags;
  164. if (test_x509_verify_param_copy(policies, NULL)) {
  165. if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
  166. return 0;
  167. }
  168. /* Copy the host flags if and only if we're copying the host list */
  169. if (test_x509_verify_param_copy(hosts, NULL)) {
  170. sk_OPENSSL_STRING_pop_free(dest->hosts, str_free);
  171. dest->hosts = NULL;
  172. if (src->hosts) {
  173. dest->hosts =
  174. sk_OPENSSL_STRING_deep_copy(src->hosts, str_copy, str_free);
  175. if (dest->hosts == NULL)
  176. return 0;
  177. dest->hostflags = src->hostflags;
  178. }
  179. }
  180. if (test_x509_verify_param_copy(email, NULL)) {
  181. if (!X509_VERIFY_PARAM_set1_email(dest, src->email, src->emaillen))
  182. return 0;
  183. }
  184. if (test_x509_verify_param_copy(ip, NULL)) {
  185. if (!X509_VERIFY_PARAM_set1_ip(dest, src->ip, src->iplen))
  186. return 0;
  187. }
  188. return 1;
  189. }
  190. int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
  191. const X509_VERIFY_PARAM *from)
  192. {
  193. unsigned long save_flags = to->inh_flags;
  194. int ret;
  195. to->inh_flags |= X509_VP_FLAG_DEFAULT;
  196. ret = X509_VERIFY_PARAM_inherit(to, from);
  197. to->inh_flags = save_flags;
  198. return ret;
  199. }
  200. static int int_x509_param_set1(char **pdest, size_t *pdestlen,
  201. const char *src, size_t srclen)
  202. {
  203. void *tmp;
  204. if (src) {
  205. if (srclen == 0)
  206. srclen = strlen(src);
  207. tmp = OPENSSL_memdup(src, srclen);
  208. if (tmp == NULL)
  209. return 0;
  210. } else {
  211. tmp = NULL;
  212. srclen = 0;
  213. }
  214. OPENSSL_free(*pdest);
  215. *pdest = tmp;
  216. if (pdestlen != NULL)
  217. *pdestlen = srclen;
  218. return 1;
  219. }
  220. int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
  221. {
  222. OPENSSL_free(param->name);
  223. param->name = OPENSSL_strdup(name);
  224. if (param->name)
  225. return 1;
  226. return 0;
  227. }
  228. int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  229. {
  230. param->flags |= flags;
  231. if (flags & X509_V_FLAG_POLICY_MASK)
  232. param->flags |= X509_V_FLAG_POLICY_CHECK;
  233. return 1;
  234. }
  235. int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
  236. unsigned long flags)
  237. {
  238. param->flags &= ~flags;
  239. return 1;
  240. }
  241. unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
  242. {
  243. return param->flags;
  244. }
  245. uint32_t X509_VERIFY_PARAM_get_inh_flags(const X509_VERIFY_PARAM *param)
  246. {
  247. return param->inh_flags;
  248. }
  249. int X509_VERIFY_PARAM_set_inh_flags(X509_VERIFY_PARAM *param, uint32_t flags)
  250. {
  251. param->inh_flags = flags;
  252. return 1;
  253. }
  254. int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
  255. {
  256. return X509_PURPOSE_set(&param->purpose, purpose);
  257. }
  258. int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
  259. {
  260. return X509_TRUST_set(&param->trust, trust);
  261. }
  262. void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
  263. {
  264. param->depth = depth;
  265. }
  266. void X509_VERIFY_PARAM_set_auth_level(X509_VERIFY_PARAM *param, int auth_level)
  267. {
  268. param->auth_level = auth_level;
  269. }
  270. time_t X509_VERIFY_PARAM_get_time(const X509_VERIFY_PARAM *param)
  271. {
  272. return param->check_time;
  273. }
  274. void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
  275. {
  276. param->check_time = t;
  277. param->flags |= X509_V_FLAG_USE_CHECK_TIME;
  278. }
  279. int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
  280. ASN1_OBJECT *policy)
  281. {
  282. if (!param->policies) {
  283. param->policies = sk_ASN1_OBJECT_new_null();
  284. if (!param->policies)
  285. return 0;
  286. }
  287. if (!sk_ASN1_OBJECT_push(param->policies, policy))
  288. return 0;
  289. return 1;
  290. }
  291. int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
  292. STACK_OF(ASN1_OBJECT) *policies)
  293. {
  294. int i;
  295. ASN1_OBJECT *oid, *doid;
  296. if (!param)
  297. return 0;
  298. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  299. if (!policies) {
  300. param->policies = NULL;
  301. return 1;
  302. }
  303. param->policies = sk_ASN1_OBJECT_new_null();
  304. if (!param->policies)
  305. return 0;
  306. for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) {
  307. oid = sk_ASN1_OBJECT_value(policies, i);
  308. doid = OBJ_dup(oid);
  309. if (!doid)
  310. return 0;
  311. if (!sk_ASN1_OBJECT_push(param->policies, doid)) {
  312. ASN1_OBJECT_free(doid);
  313. return 0;
  314. }
  315. }
  316. param->flags |= X509_V_FLAG_POLICY_CHECK;
  317. return 1;
  318. }
  319. int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
  320. const char *name, size_t namelen)
  321. {
  322. return int_x509_param_set_hosts(param, SET_HOST, name, namelen);
  323. }
  324. int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
  325. const char *name, size_t namelen)
  326. {
  327. return int_x509_param_set_hosts(param, ADD_HOST, name, namelen);
  328. }
  329. void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
  330. unsigned int flags)
  331. {
  332. param->hostflags = flags;
  333. }
  334. char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *param)
  335. {
  336. return param->peername;
  337. }
  338. /*
  339. * Move peername from one param structure to another, freeing any name present
  340. * at the target. If the source is a NULL parameter structure, free and zero
  341. * the target peername.
  342. */
  343. void X509_VERIFY_PARAM_move_peername(X509_VERIFY_PARAM *to,
  344. X509_VERIFY_PARAM *from)
  345. {
  346. char *peername = (from != NULL) ? from->peername : NULL;
  347. if (to->peername != peername) {
  348. OPENSSL_free(to->peername);
  349. to->peername = peername;
  350. }
  351. if (from)
  352. from->peername = NULL;
  353. }
  354. int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
  355. const char *email, size_t emaillen)
  356. {
  357. return int_x509_param_set1(&param->email, &param->emaillen,
  358. email, emaillen);
  359. }
  360. int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
  361. const unsigned char *ip, size_t iplen)
  362. {
  363. if (iplen != 0 && iplen != 4 && iplen != 16)
  364. return 0;
  365. return int_x509_param_set1((char **)&param->ip, &param->iplen,
  366. (char *)ip, iplen);
  367. }
  368. int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc)
  369. {
  370. unsigned char ipout[16];
  371. size_t iplen;
  372. iplen = (size_t)a2i_ipadd(ipout, ipasc);
  373. if (iplen == 0)
  374. return 0;
  375. return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen);
  376. }
  377. int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
  378. {
  379. return param->depth;
  380. }
  381. int X509_VERIFY_PARAM_get_auth_level(const X509_VERIFY_PARAM *param)
  382. {
  383. return param->auth_level;
  384. }
  385. const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param)
  386. {
  387. return param->name;
  388. }
  389. #define vpm_empty_id NULL, 0U, NULL, NULL, 0, NULL, 0
  390. /*
  391. * Default verify parameters: these are used for various applications and can
  392. * be overridden by the user specified table. NB: the 'name' field *must* be
  393. * in alphabetical order because it will be searched using OBJ_search.
  394. */
  395. static const X509_VERIFY_PARAM default_table[] = {
  396. {
  397. "default", /* X509 default parameters */
  398. 0, /* Check time */
  399. 0, /* internal flags */
  400. X509_V_FLAG_TRUSTED_FIRST, /* flags */
  401. 0, /* purpose */
  402. 0, /* trust */
  403. 100, /* depth */
  404. -1, /* auth_level */
  405. NULL, /* policies */
  406. vpm_empty_id},
  407. {
  408. "pkcs7", /* S/MIME sign parameters */
  409. 0, /* Check time */
  410. 0, /* internal flags */
  411. 0, /* flags */
  412. X509_PURPOSE_SMIME_SIGN, /* purpose */
  413. X509_TRUST_EMAIL, /* trust */
  414. -1, /* depth */
  415. -1, /* auth_level */
  416. NULL, /* policies */
  417. vpm_empty_id},
  418. {
  419. "smime_sign", /* S/MIME sign parameters */
  420. 0, /* Check time */
  421. 0, /* internal flags */
  422. 0, /* flags */
  423. X509_PURPOSE_SMIME_SIGN, /* purpose */
  424. X509_TRUST_EMAIL, /* trust */
  425. -1, /* depth */
  426. -1, /* auth_level */
  427. NULL, /* policies */
  428. vpm_empty_id},
  429. {
  430. "ssl_client", /* SSL/TLS client parameters */
  431. 0, /* Check time */
  432. 0, /* internal flags */
  433. 0, /* flags */
  434. X509_PURPOSE_SSL_CLIENT, /* purpose */
  435. X509_TRUST_SSL_CLIENT, /* trust */
  436. -1, /* depth */
  437. -1, /* auth_level */
  438. NULL, /* policies */
  439. vpm_empty_id},
  440. {
  441. "ssl_server", /* SSL/TLS server parameters */
  442. 0, /* Check time */
  443. 0, /* internal flags */
  444. 0, /* flags */
  445. X509_PURPOSE_SSL_SERVER, /* purpose */
  446. X509_TRUST_SSL_SERVER, /* trust */
  447. -1, /* depth */
  448. -1, /* auth_level */
  449. NULL, /* policies */
  450. vpm_empty_id}
  451. };
  452. static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
  453. static int table_cmp(const X509_VERIFY_PARAM *a, const X509_VERIFY_PARAM *b)
  454. {
  455. return strcmp(a->name, b->name);
  456. }
  457. DECLARE_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM, table);
  458. IMPLEMENT_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM, table);
  459. static int param_cmp(const X509_VERIFY_PARAM *const *a,
  460. const X509_VERIFY_PARAM *const *b)
  461. {
  462. return strcmp((*a)->name, (*b)->name);
  463. }
  464. int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
  465. {
  466. int idx;
  467. X509_VERIFY_PARAM *ptmp;
  468. if (param_table == NULL) {
  469. param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
  470. if (param_table == NULL)
  471. return 0;
  472. } else {
  473. idx = sk_X509_VERIFY_PARAM_find(param_table, param);
  474. if (idx != -1) {
  475. ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
  476. X509_VERIFY_PARAM_free(ptmp);
  477. (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
  478. }
  479. }
  480. if (!sk_X509_VERIFY_PARAM_push(param_table, param))
  481. return 0;
  482. return 1;
  483. }
  484. int X509_VERIFY_PARAM_get_count(void)
  485. {
  486. int num = OSSL_NELEM(default_table);
  487. if (param_table)
  488. num += sk_X509_VERIFY_PARAM_num(param_table);
  489. return num;
  490. }
  491. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id)
  492. {
  493. int num = OSSL_NELEM(default_table);
  494. if (id < num)
  495. return default_table + id;
  496. return sk_X509_VERIFY_PARAM_value(param_table, id - num);
  497. }
  498. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
  499. {
  500. int idx;
  501. X509_VERIFY_PARAM pm;
  502. pm.name = (char *)name;
  503. if (param_table) {
  504. idx = sk_X509_VERIFY_PARAM_find(param_table, &pm);
  505. if (idx != -1)
  506. return sk_X509_VERIFY_PARAM_value(param_table, idx);
  507. }
  508. return OBJ_bsearch_table(&pm, default_table, OSSL_NELEM(default_table));
  509. }
  510. void X509_VERIFY_PARAM_table_cleanup(void)
  511. {
  512. sk_X509_VERIFY_PARAM_pop_free(param_table, X509_VERIFY_PARAM_free);
  513. param_table = NULL;
  514. }