rendservice.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char rendservice_c_id[] =
  6. "$Id$";
  7. /**
  8. * \file rendservice.c
  9. * \brief The hidden-service side of rendezvous functionality.
  10. **/
  11. #include "or.h"
  12. static origin_circuit_t *find_intro_circuit(rend_intro_point_t *intro,
  13. const char *pk_digest,
  14. int desc_version);
  15. /** Represents the mapping from a virtual port of a rendezvous service to
  16. * a real port on some IP.
  17. */
  18. typedef struct rend_service_port_config_t {
  19. uint16_t virtual_port;
  20. uint16_t real_port;
  21. uint32_t real_addr;
  22. } rend_service_port_config_t;
  23. /** Try to maintain this many intro points per service if possible. */
  24. #define NUM_INTRO_POINTS 3
  25. /** If we can't build our intro circuits, don't retry for this long. */
  26. #define INTRO_CIRC_RETRY_PERIOD (60*5)
  27. /** Don't try to build more than this many circuits before giving up
  28. * for a while.*/
  29. #define MAX_INTRO_CIRCS_PER_PERIOD 10
  30. /** How many times will a hidden service operator attempt to connect to
  31. * a requested rendezvous point before giving up? */
  32. #define MAX_REND_FAILURES 30
  33. /** How many seconds should we spend trying to connect to a requested
  34. * rendezvous point before giving up? */
  35. #define MAX_REND_TIMEOUT 30
  36. /** Represents a single hidden service running at this OP. */
  37. typedef struct rend_service_t {
  38. /* Fields specified in config file */
  39. char *directory; /**< where in the filesystem it stores it */
  40. smartlist_t *ports; /**< List of rend_service_port_config_t */
  41. char *intro_prefer_nodes; /**< comma-separated list of nicknames */
  42. char *intro_exclude_nodes; /**< comma-separated list of nicknames */
  43. int descriptor_version; /**< Rendezvous descriptor version that will be
  44. * published. */
  45. /* Other fields */
  46. crypto_pk_env_t *private_key; /**< Permanent hidden-service key. */
  47. char service_id[REND_SERVICE_ID_LEN_BASE32+1]; /**< Onion address without
  48. * '.onion' */
  49. char pk_digest[DIGEST_LEN]; /**< Hash of permanent hidden-service key. */
  50. smartlist_t *intro_nodes; /**< List of rend_intro_point_t's we have,
  51. * or are trying to establish. */
  52. time_t intro_period_started; /**< Start of the current period to build
  53. * introduction points. */
  54. int n_intro_circuits_launched; /**< count of intro circuits we have
  55. * established in this period. */
  56. rend_service_descriptor_t *desc; /**< Current hidden service descriptor. */
  57. time_t desc_is_dirty; /**< Time at which changes to the hidden service
  58. * descriptor content occurred, or 0 if it's
  59. * up-to-date. */
  60. time_t next_upload_time; /**< Scheduled next hidden service descriptor
  61. * upload time. */
  62. } rend_service_t;
  63. /** A list of rend_service_t's for services run on this OP.
  64. */
  65. static smartlist_t *rend_service_list = NULL;
  66. /** Return the number of rendezvous services we have configured. */
  67. int
  68. num_rend_services(void)
  69. {
  70. if (!rend_service_list)
  71. return 0;
  72. return smartlist_len(rend_service_list);
  73. }
  74. /** Release the storage held by <b>service</b>.
  75. */
  76. static void
  77. rend_service_free(rend_service_t *service)
  78. {
  79. if (!service) return;
  80. tor_free(service->directory);
  81. SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p));
  82. smartlist_free(service->ports);
  83. if (service->private_key)
  84. crypto_free_pk_env(service->private_key);
  85. if (service->intro_nodes) {
  86. SMARTLIST_FOREACH(service->intro_nodes, rend_intro_point_t *, intro,
  87. rend_intro_point_free(intro););
  88. smartlist_free(service->intro_nodes);
  89. }
  90. tor_free(service->intro_prefer_nodes);
  91. tor_free(service->intro_exclude_nodes);
  92. if (service->desc)
  93. rend_service_descriptor_free(service->desc);
  94. tor_free(service);
  95. }
  96. /** Release all the storage held in rend_service_list.
  97. */
  98. void
  99. rend_service_free_all(void)
  100. {
  101. if (!rend_service_list) {
  102. return;
  103. }
  104. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr,
  105. rend_service_free(ptr));
  106. smartlist_free(rend_service_list);
  107. rend_service_list = NULL;
  108. }
  109. /** Validate <b>service</b> and add it to rend_service_list if possible.
  110. */
  111. static void
  112. rend_add_service(rend_service_t *service)
  113. {
  114. int i;
  115. rend_service_port_config_t *p;
  116. struct in_addr addr;
  117. if (!service->intro_prefer_nodes)
  118. service->intro_prefer_nodes = tor_strdup("");
  119. if (!service->intro_exclude_nodes)
  120. service->intro_exclude_nodes = tor_strdup("");
  121. service->intro_nodes = smartlist_create();
  122. /* If the service is configured to publish unversioned (v0) and versioned
  123. * descriptors (v2 or higher), split it up into two separate services. */
  124. if (service->descriptor_version == -1) {
  125. rend_service_t *v0_service = tor_malloc_zero(sizeof(rend_service_t));
  126. v0_service->directory = tor_strdup(service->directory);
  127. v0_service->ports = smartlist_create();
  128. SMARTLIST_FOREACH(service->ports, rend_service_port_config_t *, p, {
  129. rend_service_port_config_t *copy =
  130. tor_malloc_zero(sizeof(rend_service_port_config_t));
  131. memcpy(copy, p, sizeof(rend_service_port_config_t));
  132. smartlist_add(v0_service->ports, copy);
  133. });
  134. v0_service->intro_prefer_nodes = tor_strdup(service->intro_prefer_nodes);
  135. v0_service->intro_exclude_nodes = tor_strdup(service->intro_exclude_nodes);
  136. v0_service->intro_period_started = service->intro_period_started;
  137. v0_service->descriptor_version = 0; /* Unversioned descriptor. */
  138. rend_add_service(v0_service);
  139. service->descriptor_version = 2; /* Versioned descriptor. */
  140. }
  141. if (!smartlist_len(service->ports)) {
  142. log_warn(LD_CONFIG, "Hidden service with no ports configured; ignoring.");
  143. rend_service_free(service);
  144. } else {
  145. smartlist_add(rend_service_list, service);
  146. log_debug(LD_REND,"Configuring service with directory \"%s\"",
  147. service->directory);
  148. for (i = 0; i < smartlist_len(service->ports); ++i) {
  149. char addrbuf[INET_NTOA_BUF_LEN];
  150. p = smartlist_get(service->ports, i);
  151. addr.s_addr = htonl(p->real_addr);
  152. tor_inet_ntoa(&addr, addrbuf, sizeof(addrbuf));
  153. log_debug(LD_REND,"Service maps port %d to %s:%d",
  154. p->virtual_port, addrbuf, p->real_port);
  155. }
  156. }
  157. }
  158. /** Parses a real-port to virtual-port mapping and returns a new
  159. * rend_service_port_config_t.
  160. *
  161. * The format is: VirtualPort (IP|RealPort|IP:RealPort)?
  162. *
  163. * IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
  164. */
  165. static rend_service_port_config_t *
  166. parse_port_config(const char *string)
  167. {
  168. smartlist_t *sl;
  169. int virtport;
  170. int realport;
  171. uint16_t p;
  172. uint32_t addr;
  173. const char *addrport;
  174. rend_service_port_config_t *result = NULL;
  175. sl = smartlist_create();
  176. smartlist_split_string(sl, string, " ",
  177. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  178. if (smartlist_len(sl) < 1 || smartlist_len(sl) > 2) {
  179. log_warn(LD_CONFIG, "Bad syntax in hidden service port configuration.");
  180. goto err;
  181. }
  182. virtport = (int)tor_parse_long(smartlist_get(sl,0), 10, 1, 65535, NULL,NULL);
  183. if (!virtport) {
  184. log_warn(LD_CONFIG, "Missing or invalid port %s in hidden service port "
  185. "configuration", escaped(smartlist_get(sl,0)));
  186. goto err;
  187. }
  188. if (smartlist_len(sl) == 1) {
  189. /* No addr:port part; use default. */
  190. realport = virtport;
  191. addr = 0x7F000001u; /* 127.0.0.1 */
  192. } else {
  193. addrport = smartlist_get(sl,1);
  194. if (strchr(addrport, ':') || strchr(addrport, '.')) {
  195. if (parse_addr_port(LOG_WARN, addrport, NULL, &addr, &p)<0) {
  196. log_warn(LD_CONFIG,"Unparseable address in hidden service port "
  197. "configuration.");
  198. goto err;
  199. }
  200. realport = p?p:virtport;
  201. } else {
  202. /* No addr:port, no addr -- must be port. */
  203. realport = (int)tor_parse_long(addrport, 10, 1, 65535, NULL, NULL);
  204. if (!realport) {
  205. log_warn(LD_CONFIG,"Unparseable or out-of-range port %s in hidden "
  206. "service port configuration.", escaped(addrport));
  207. goto err;
  208. }
  209. addr = 0x7F000001u; /* Default to 127.0.0.1 */
  210. }
  211. }
  212. result = tor_malloc(sizeof(rend_service_port_config_t));
  213. result->virtual_port = virtport;
  214. result->real_port = realport;
  215. result->real_addr = addr;
  216. err:
  217. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  218. smartlist_free(sl);
  219. return result;
  220. }
  221. /** Set up rend_service_list, based on the values of HiddenServiceDir and
  222. * HiddenServicePort in <b>options</b>. Return 0 on success and -1 on
  223. * failure. (If <b>validate_only</b> is set, parse, warn and return as
  224. * normal, but don't actually change the configured services.)
  225. */
  226. int
  227. rend_config_services(or_options_t *options, int validate_only)
  228. {
  229. config_line_t *line;
  230. rend_service_t *service = NULL;
  231. rend_service_port_config_t *portcfg;
  232. if (!validate_only) {
  233. rend_service_free_all();
  234. rend_service_list = smartlist_create();
  235. }
  236. for (line = options->RendConfigLines; line; line = line->next) {
  237. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  238. if (service) {
  239. if (validate_only)
  240. rend_service_free(service);
  241. else
  242. rend_add_service(service);
  243. }
  244. service = tor_malloc_zero(sizeof(rend_service_t));
  245. service->directory = tor_strdup(line->value);
  246. service->ports = smartlist_create();
  247. service->intro_period_started = time(NULL);
  248. service->descriptor_version = -1; /**< All descriptor versions. */
  249. continue;
  250. }
  251. if (!service) {
  252. log_warn(LD_CONFIG, "%s with no preceding HiddenServiceDir directive",
  253. line->key);
  254. rend_service_free(service);
  255. return -1;
  256. }
  257. if (!strcasecmp(line->key, "HiddenServicePort")) {
  258. portcfg = parse_port_config(line->value);
  259. if (!portcfg) {
  260. rend_service_free(service);
  261. return -1;
  262. }
  263. smartlist_add(service->ports, portcfg);
  264. } else if (!strcasecmp(line->key, "HiddenServiceNodes")) {
  265. if (service->intro_prefer_nodes) {
  266. log_warn(LD_CONFIG,
  267. "Got multiple HiddenServiceNodes lines for a single "
  268. "service.");
  269. rend_service_free(service);
  270. return -1;
  271. }
  272. service->intro_prefer_nodes = tor_strdup(line->value);
  273. } else if (!strcasecmp(line->key, "HiddenServiceExcludeNodes")) {
  274. if (service->intro_exclude_nodes) {
  275. log_warn(LD_CONFIG,
  276. "Got multiple HiddenServiceExcludedNodes lines for "
  277. "a single service.");
  278. rend_service_free(service);
  279. return -1;
  280. }
  281. service->intro_exclude_nodes = tor_strdup(line->value);
  282. } else {
  283. smartlist_t *versions;
  284. char *version_str;
  285. int i, version, ver_ok=1, versions_bitmask = 0;
  286. tor_assert(!strcasecmp(line->key, "HiddenServiceVersion"));
  287. versions = smartlist_create();
  288. smartlist_split_string(versions, line->value, ",",
  289. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  290. for (i = 0; i < smartlist_len(versions); i++) {
  291. version_str = smartlist_get(versions, i);
  292. if (strlen(version_str) != 1 || strspn(version_str, "02") != 1) {
  293. log_warn(LD_CONFIG,
  294. "HiddenServiceVersion can only be 0 and/or 2.");
  295. SMARTLIST_FOREACH(versions, char *, cp, tor_free(cp));
  296. smartlist_free(versions);
  297. rend_service_free(service);
  298. return -1;
  299. }
  300. version = (int)tor_parse_long(version_str, 10, 0, INT_MAX, &ver_ok,
  301. NULL);
  302. if (!ver_ok)
  303. continue;
  304. versions_bitmask |= 1 << version;
  305. }
  306. /* If exactly one version is set, change descriptor_version to that
  307. * value; otherwise leave it at -1. */
  308. if (versions_bitmask == 1 << 0) service->descriptor_version = 0;
  309. if (versions_bitmask == 1 << 2) service->descriptor_version = 2;
  310. SMARTLIST_FOREACH(versions, char *, cp, tor_free(cp));
  311. smartlist_free(versions);
  312. }
  313. }
  314. if (service) {
  315. if (validate_only)
  316. rend_service_free(service);
  317. else
  318. rend_add_service(service);
  319. }
  320. return 0;
  321. }
  322. /** Replace the old value of <b>service</b>-\>desc with one that reflects
  323. * the other fields in service.
  324. */
  325. static void
  326. rend_service_update_descriptor(rend_service_t *service)
  327. {
  328. rend_service_descriptor_t *d;
  329. origin_circuit_t *circ;
  330. int i;
  331. if (service->desc) {
  332. rend_service_descriptor_free(service->desc);
  333. service->desc = NULL;
  334. }
  335. d = service->desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  336. d->pk = crypto_pk_dup_key(service->private_key);
  337. d->timestamp = time(NULL);
  338. d->version = service->descriptor_version;
  339. d->intro_nodes = smartlist_create();
  340. /* Whoever understands descriptor version 2 also understands intro
  341. * protocol 2. So we only support 2. */
  342. d->protocols = 1 << 2;
  343. for (i = 0; i < smartlist_len(service->intro_nodes); ++i) {
  344. rend_intro_point_t *intro_svc = smartlist_get(service->intro_nodes, i);
  345. rend_intro_point_t *intro_desc;
  346. circ = find_intro_circuit(intro_svc, service->pk_digest, d->version);
  347. if (!circ || circ->_base.purpose != CIRCUIT_PURPOSE_S_INTRO)
  348. continue;
  349. /* We have an entirely established intro circuit. */
  350. intro_desc = tor_malloc_zero(sizeof(rend_intro_point_t));
  351. intro_desc->extend_info = extend_info_dup(intro_svc->extend_info);
  352. if (intro_svc->intro_key)
  353. intro_desc->intro_key = crypto_pk_dup_key(intro_svc->intro_key);
  354. smartlist_add(d->intro_nodes, intro_desc);
  355. }
  356. }
  357. /** Load and/or generate private keys for all hidden services. Return 0 on
  358. * success, -1 on failure.
  359. */
  360. int
  361. rend_service_load_keys(void)
  362. {
  363. int i;
  364. rend_service_t *s;
  365. char fname[512];
  366. char buf[128];
  367. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  368. s = smartlist_get(rend_service_list,i);
  369. if (s->private_key)
  370. continue;
  371. log_info(LD_REND, "Loading hidden-service keys from \"%s\"",
  372. s->directory);
  373. /* Check/create directory */
  374. if (check_private_dir(s->directory, CPD_CREATE) < 0)
  375. return -1;
  376. /* Load key */
  377. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  378. strlcat(fname,PATH_SEPARATOR"private_key",sizeof(fname))
  379. >= sizeof(fname)) {
  380. log_warn(LD_CONFIG, "Directory name too long to store key file: \"%s\".",
  381. s->directory);
  382. return -1;
  383. }
  384. s->private_key = init_key_from_file(fname, 1, LOG_ERR);
  385. if (!s->private_key)
  386. return -1;
  387. /* Create service file */
  388. if (rend_get_service_id(s->private_key, s->service_id)<0) {
  389. log_warn(LD_BUG, "Internal error: couldn't encode service ID.");
  390. return -1;
  391. }
  392. if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
  393. log_warn(LD_BUG, "Couldn't compute hash of public key.");
  394. return -1;
  395. }
  396. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  397. strlcat(fname,PATH_SEPARATOR"hostname",sizeof(fname))
  398. >= sizeof(fname)) {
  399. log_warn(LD_CONFIG, "Directory name too long to store hostname file:"
  400. " \"%s\".", s->directory);
  401. return -1;
  402. }
  403. tor_snprintf(buf, sizeof(buf),"%s.onion\n", s->service_id);
  404. if (write_str_to_file(fname,buf,0)<0)
  405. return -1;
  406. }
  407. return 0;
  408. }
  409. /** Return the service whose public key has a digest of <b>digest</b> and
  410. * which publishes the given descriptor <b>version</b>. Return NULL if no
  411. * such service exists.
  412. */
  413. static rend_service_t *
  414. rend_service_get_by_pk_digest_and_version(const char* digest,
  415. uint8_t version)
  416. {
  417. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
  418. if (!memcmp(s->pk_digest,digest,DIGEST_LEN) &&
  419. s->descriptor_version == version) return s);
  420. return NULL;
  421. }
  422. /** Return 1 if any virtual port in <b>service</b> wants a circuit
  423. * to have good uptime. Else return 0.
  424. */
  425. static int
  426. rend_service_requires_uptime(rend_service_t *service)
  427. {
  428. int i;
  429. rend_service_port_config_t *p;
  430. for (i=0; i < smartlist_len(service->ports); ++i) {
  431. p = smartlist_get(service->ports, i);
  432. if (smartlist_string_num_isin(get_options()->LongLivedPorts,
  433. p->virtual_port))
  434. return 1;
  435. }
  436. return 0;
  437. }
  438. /******
  439. * Handle cells
  440. ******/
  441. /** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
  442. * rendezvous point.
  443. */
  444. int
  445. rend_service_introduce(origin_circuit_t *circuit, const char *request,
  446. size_t request_len)
  447. {
  448. char *ptr, *r_cookie;
  449. extend_info_t *extend_info = NULL;
  450. char buf[RELAY_PAYLOAD_SIZE];
  451. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN]; /* Holds KH, Df, Db, Kf, Kb */
  452. rend_service_t *service;
  453. int r, i;
  454. size_t len, keylen;
  455. crypto_dh_env_t *dh = NULL;
  456. origin_circuit_t *launched = NULL;
  457. crypt_path_t *cpath = NULL;
  458. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  459. char hexcookie[9];
  460. int circ_needs_uptime;
  461. int reason = END_CIRC_REASON_TORPROTOCOL;
  462. crypto_pk_env_t *intro_key;
  463. char intro_key_digest[DIGEST_LEN];
  464. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  465. circuit->rend_pk_digest, REND_SERVICE_ID_LEN);
  466. log_info(LD_REND, "Received INTRODUCE2 cell for service %s on circ %d.",
  467. escaped(serviceid), circuit->_base.n_circ_id);
  468. if (circuit->_base.purpose != CIRCUIT_PURPOSE_S_INTRO) {
  469. log_warn(LD_PROTOCOL,
  470. "Got an INTRODUCE2 over a non-introduction circuit %d.",
  471. circuit->_base.n_circ_id);
  472. return -1;
  473. }
  474. /* min key length plus digest length plus nickname length */
  475. if (request_len < DIGEST_LEN+REND_COOKIE_LEN+(MAX_NICKNAME_LEN+1)+
  476. DH_KEY_LEN+42) {
  477. log_warn(LD_PROTOCOL, "Got a truncated INTRODUCE2 cell on circ %d.",
  478. circuit->_base.n_circ_id);
  479. return -1;
  480. }
  481. /* look up service depending on circuit. */
  482. service = rend_service_get_by_pk_digest_and_version(
  483. circuit->rend_pk_digest, circuit->rend_desc_version);
  484. if (!service) {
  485. log_warn(LD_REND, "Got an INTRODUCE2 cell for an unrecognized service %s.",
  486. escaped(serviceid));
  487. return -1;
  488. }
  489. /* if descriptor version is 2, use intro key instead of service key. */
  490. if (circuit->rend_desc_version == 0) {
  491. intro_key = service->private_key;
  492. } else {
  493. intro_key = circuit->intro_key;
  494. }
  495. /* first DIGEST_LEN bytes of request is intro or service pk digest */
  496. crypto_pk_get_digest(intro_key, intro_key_digest);
  497. if (memcmp(intro_key_digest, request, DIGEST_LEN)) {
  498. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  499. request, REND_SERVICE_ID_LEN);
  500. log_warn(LD_REND, "Got an INTRODUCE2 cell for the wrong service (%s).",
  501. escaped(serviceid));
  502. return -1;
  503. }
  504. keylen = crypto_pk_keysize(intro_key);
  505. if (request_len < keylen+DIGEST_LEN) {
  506. log_warn(LD_PROTOCOL,
  507. "PK-encrypted portion of INTRODUCE2 cell was truncated.");
  508. return -1;
  509. }
  510. /* Next N bytes is encrypted with service key */
  511. note_crypto_pk_op(REND_SERVER);
  512. r = crypto_pk_private_hybrid_decrypt(
  513. intro_key,buf,request+DIGEST_LEN,request_len-DIGEST_LEN,
  514. PK_PKCS1_OAEP_PADDING,1);
  515. if (r<0) {
  516. log_warn(LD_PROTOCOL, "Couldn't decrypt INTRODUCE2 cell.");
  517. return -1;
  518. }
  519. len = r;
  520. if (*buf == 2) {
  521. /* Version 2 INTRODUCE2 cell. */
  522. int klen;
  523. extend_info = tor_malloc_zero(sizeof(extend_info_t));
  524. extend_info->addr = ntohl(get_uint32(buf+1));
  525. extend_info->port = ntohs(get_uint16(buf+5));
  526. memcpy(extend_info->identity_digest, buf+7, DIGEST_LEN);
  527. extend_info->nickname[0] = '$';
  528. base16_encode(extend_info->nickname+1, sizeof(extend_info->nickname)-1,
  529. extend_info->identity_digest, DIGEST_LEN);
  530. klen = ntohs(get_uint16(buf+7+DIGEST_LEN));
  531. if ((int)len != 7+DIGEST_LEN+2+klen+20+128) {
  532. log_warn(LD_PROTOCOL, "Bad length %u for version 2 INTRODUCE2 cell.",
  533. (int)len);
  534. reason = END_CIRC_REASON_TORPROTOCOL;
  535. goto err;
  536. }
  537. extend_info->onion_key = crypto_pk_asn1_decode(buf+7+DIGEST_LEN+2, klen);
  538. if (!extend_info->onion_key) {
  539. log_warn(LD_PROTOCOL,
  540. "Error decoding onion key in version 2 INTRODUCE2 cell.");
  541. reason = END_CIRC_REASON_TORPROTOCOL;
  542. goto err;
  543. }
  544. ptr = buf+7+DIGEST_LEN+2+klen;
  545. len -= 7+DIGEST_LEN+2+klen;
  546. } else {
  547. char *rp_nickname;
  548. size_t nickname_field_len;
  549. routerinfo_t *router;
  550. int version;
  551. if (*buf == 1) {
  552. rp_nickname = buf+1;
  553. nickname_field_len = MAX_HEX_NICKNAME_LEN+1;
  554. version = 1;
  555. } else {
  556. nickname_field_len = MAX_NICKNAME_LEN+1;
  557. rp_nickname = buf;
  558. version = 0;
  559. }
  560. ptr=memchr(rp_nickname,0,nickname_field_len);
  561. if (!ptr || ptr == rp_nickname) {
  562. log_warn(LD_PROTOCOL,
  563. "Couldn't find a nul-padded nickname in INTRODUCE2 cell.");
  564. return -1;
  565. }
  566. if ((version == 0 && !is_legal_nickname(rp_nickname)) ||
  567. (version == 1 && !is_legal_nickname_or_hexdigest(rp_nickname))) {
  568. log_warn(LD_PROTOCOL, "Bad nickname in INTRODUCE2 cell.");
  569. return -1;
  570. }
  571. /* Okay, now we know that a nickname is at the start of the buffer. */
  572. ptr = rp_nickname+nickname_field_len;
  573. len -= nickname_field_len;
  574. len -= rp_nickname - buf; /* also remove header space used by version, if
  575. * any */
  576. router = router_get_by_nickname(rp_nickname, 0);
  577. if (!router) {
  578. log_info(LD_REND, "Couldn't find router %s named in introduce2 cell.",
  579. escaped_safe_str(rp_nickname));
  580. /* XXXX Add a no-such-router reason? */
  581. reason = END_CIRC_REASON_TORPROTOCOL;
  582. goto err;
  583. }
  584. extend_info = extend_info_from_router(router);
  585. }
  586. if (len != REND_COOKIE_LEN+DH_KEY_LEN) {
  587. log_warn(LD_PROTOCOL, "Bad length %u for INTRODUCE2 cell.", (int)len);
  588. reason = END_CIRC_REASON_TORPROTOCOL;
  589. goto err;
  590. }
  591. r_cookie = ptr;
  592. base16_encode(hexcookie,9,r_cookie,4);
  593. /* Try DH handshake... */
  594. dh = crypto_dh_new();
  595. if (!dh || crypto_dh_generate_public(dh)<0) {
  596. log_warn(LD_BUG,"Internal error: couldn't build DH state "
  597. "or generate public key.");
  598. reason = END_CIRC_REASON_INTERNAL;
  599. goto err;
  600. }
  601. if (crypto_dh_compute_secret(dh, ptr+REND_COOKIE_LEN, DH_KEY_LEN, keys,
  602. DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  603. log_warn(LD_BUG, "Internal error: couldn't complete DH handshake");
  604. reason = END_CIRC_REASON_INTERNAL;
  605. goto err;
  606. }
  607. circ_needs_uptime = rend_service_requires_uptime(service);
  608. /* help predict this next time */
  609. rep_hist_note_used_internal(time(NULL), circ_needs_uptime, 1);
  610. /* Launch a circuit to alice's chosen rendezvous point.
  611. */
  612. for (i=0;i<MAX_REND_FAILURES;i++) {
  613. int flags = CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_IS_INTERNAL;
  614. if (circ_needs_uptime) flags |= CIRCLAUNCH_NEED_UPTIME;
  615. launched = circuit_launch_by_extend_info(
  616. CIRCUIT_PURPOSE_S_CONNECT_REND, extend_info, flags);
  617. if (launched)
  618. break;
  619. }
  620. if (!launched) { /* give up */
  621. log_warn(LD_REND, "Giving up launching first hop of circuit to rendezvous "
  622. "point %s for service %s.",
  623. escaped_safe_str(extend_info->nickname), serviceid);
  624. reason = END_CIRC_REASON_CONNECTFAILED;
  625. goto err;
  626. }
  627. log_info(LD_REND,
  628. "Accepted intro; launching circuit to %s "
  629. "(cookie %s) for service %s.",
  630. escaped_safe_str(extend_info->nickname), hexcookie, serviceid);
  631. tor_assert(launched->build_state);
  632. /* Fill in the circuit's state. */
  633. memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
  634. DIGEST_LEN);
  635. memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
  636. strlcpy(launched->rend_query, service->service_id,
  637. sizeof(launched->rend_query));
  638. launched->rend_desc_version = service->descriptor_version;
  639. launched->build_state->pending_final_cpath = cpath =
  640. tor_malloc_zero(sizeof(crypt_path_t));
  641. cpath->magic = CRYPT_PATH_MAGIC;
  642. launched->build_state->expiry_time = time(NULL) + MAX_REND_TIMEOUT;
  643. cpath->dh_handshake_state = dh;
  644. dh = NULL;
  645. if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
  646. goto err;
  647. memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
  648. if (extend_info) extend_info_free(extend_info);
  649. return 0;
  650. err:
  651. if (dh) crypto_dh_free(dh);
  652. if (launched)
  653. circuit_mark_for_close(TO_CIRCUIT(launched), reason);
  654. if (extend_info) extend_info_free(extend_info);
  655. return -1;
  656. }
  657. /** Called when we fail building a rendezvous circuit at some point other
  658. * than the last hop: launches a new circuit to the same rendezvous point.
  659. */
  660. void
  661. rend_service_relaunch_rendezvous(origin_circuit_t *oldcirc)
  662. {
  663. origin_circuit_t *newcirc;
  664. cpath_build_state_t *newstate, *oldstate;
  665. tor_assert(oldcirc->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  666. if (!oldcirc->build_state ||
  667. oldcirc->build_state->failure_count > MAX_REND_FAILURES ||
  668. oldcirc->build_state->expiry_time < time(NULL)) {
  669. log_info(LD_REND,
  670. "Attempt to build circuit to %s for rendezvous has failed "
  671. "too many times or expired; giving up.",
  672. oldcirc->build_state ?
  673. oldcirc->build_state->chosen_exit->nickname : "*unknown*");
  674. return;
  675. }
  676. oldstate = oldcirc->build_state;
  677. tor_assert(oldstate);
  678. if (oldstate->pending_final_cpath == NULL) {
  679. log_info(LD_REND,"Skipping relaunch of circ that failed on its first hop. "
  680. "Initiator will retry.");
  681. return;
  682. }
  683. log_info(LD_REND,"Reattempting rendezvous circuit to '%s'",
  684. oldstate->chosen_exit->nickname);
  685. newcirc = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND,
  686. oldstate->chosen_exit,
  687. CIRCLAUNCH_NEED_CAPACITY|CIRCLAUNCH_IS_INTERNAL);
  688. if (!newcirc) {
  689. log_warn(LD_REND,"Couldn't relaunch rendezvous circuit to '%s'.",
  690. oldstate->chosen_exit->nickname);
  691. return;
  692. }
  693. newstate = newcirc->build_state;
  694. tor_assert(newstate);
  695. newstate->failure_count = oldstate->failure_count+1;
  696. newstate->expiry_time = oldstate->expiry_time;
  697. newstate->pending_final_cpath = oldstate->pending_final_cpath;
  698. oldstate->pending_final_cpath = NULL;
  699. memcpy(newcirc->rend_query, oldcirc->rend_query,
  700. REND_SERVICE_ID_LEN_BASE32+1);
  701. memcpy(newcirc->rend_pk_digest, oldcirc->rend_pk_digest,
  702. DIGEST_LEN);
  703. memcpy(newcirc->rend_cookie, oldcirc->rend_cookie,
  704. REND_COOKIE_LEN);
  705. newcirc->rend_desc_version = oldcirc->rend_desc_version;
  706. }
  707. /** Launch a circuit to serve as an introduction point for the service
  708. * <b>service</b> at the introduction point <b>nickname</b>
  709. */
  710. static int
  711. rend_service_launch_establish_intro(rend_service_t *service,
  712. rend_intro_point_t *intro)
  713. {
  714. origin_circuit_t *launched;
  715. log_info(LD_REND,
  716. "Launching circuit to introduction point %s for service %s",
  717. escaped_safe_str(intro->extend_info->nickname),
  718. service->service_id);
  719. rep_hist_note_used_internal(time(NULL), 1, 0);
  720. ++service->n_intro_circuits_launched;
  721. launched = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  722. intro->extend_info,
  723. CIRCLAUNCH_NEED_UPTIME|CIRCLAUNCH_IS_INTERNAL);
  724. if (!launched) {
  725. log_info(LD_REND,
  726. "Can't launch circuit to establish introduction at %s.",
  727. escaped_safe_str(intro->extend_info->nickname));
  728. return -1;
  729. }
  730. strlcpy(launched->rend_query, service->service_id,
  731. sizeof(launched->rend_query));
  732. memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
  733. launched->rend_desc_version = service->descriptor_version;
  734. if (service->descriptor_version == 2)
  735. launched->intro_key = crypto_pk_dup_key(intro->intro_key);
  736. if (launched->_base.state == CIRCUIT_STATE_OPEN)
  737. rend_service_intro_has_opened(launched);
  738. return 0;
  739. }
  740. /** Called when we're done building a circuit to an introduction point:
  741. * sends a RELAY_ESTABLISH_INTRO cell.
  742. */
  743. void
  744. rend_service_intro_has_opened(origin_circuit_t *circuit)
  745. {
  746. rend_service_t *service;
  747. size_t len;
  748. int r;
  749. char buf[RELAY_PAYLOAD_SIZE];
  750. char auth[DIGEST_LEN + 9];
  751. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  752. int reason = END_CIRC_REASON_TORPROTOCOL;
  753. crypto_pk_env_t *intro_key;
  754. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  755. tor_assert(circuit->cpath);
  756. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  757. circuit->rend_pk_digest, REND_SERVICE_ID_LEN);
  758. service = rend_service_get_by_pk_digest_and_version(
  759. circuit->rend_pk_digest, circuit->rend_desc_version);
  760. if (!service) {
  761. log_warn(LD_REND, "Unrecognized service ID %s on introduction circuit %d.",
  762. serviceid, circuit->_base.n_circ_id);
  763. reason = END_CIRC_REASON_NOSUCHSERVICE;
  764. goto err;
  765. }
  766. log_info(LD_REND,
  767. "Established circuit %d as introduction point for service %s",
  768. circuit->_base.n_circ_id, serviceid);
  769. /* If the introduction point will not be used in an unversioned
  770. * descriptor, use the intro key instead of the service key in
  771. * ESTABLISH_INTRO. */
  772. if (service->descriptor_version == 0)
  773. intro_key = service->private_key;
  774. else
  775. intro_key = circuit->intro_key;
  776. /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
  777. r = crypto_pk_asn1_encode(intro_key, buf+2,
  778. RELAY_PAYLOAD_SIZE-2);
  779. if (r < 0) {
  780. log_warn(LD_BUG, "Internal error; failed to establish intro point.");
  781. reason = END_CIRC_REASON_INTERNAL;
  782. goto err;
  783. }
  784. len = r;
  785. set_uint16(buf, htons((uint16_t)len));
  786. len += 2;
  787. memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
  788. memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
  789. if (crypto_digest(buf+len, auth, DIGEST_LEN+9))
  790. goto err;
  791. len += 20;
  792. note_crypto_pk_op(REND_SERVER);
  793. r = crypto_pk_private_sign_digest(intro_key, buf+len, buf, len);
  794. if (r<0) {
  795. log_warn(LD_BUG, "Internal error: couldn't sign introduction request.");
  796. reason = END_CIRC_REASON_INTERNAL;
  797. goto err;
  798. }
  799. len += r;
  800. if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
  801. RELAY_COMMAND_ESTABLISH_INTRO,
  802. buf, len, circuit->cpath->prev)<0) {
  803. log_info(LD_GENERAL,
  804. "Couldn't send introduction request for service %s on circuit %d",
  805. serviceid, circuit->_base.n_circ_id);
  806. reason = END_CIRC_REASON_INTERNAL;
  807. goto err;
  808. }
  809. return;
  810. err:
  811. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  812. }
  813. /** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
  814. * live introduction point, and note that the service descriptor is
  815. * now out-of-date.*/
  816. int
  817. rend_service_intro_established(origin_circuit_t *circuit, const char *request,
  818. size_t request_len)
  819. {
  820. rend_service_t *service;
  821. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  822. (void) request;
  823. (void) request_len;
  824. if (circuit->_base.purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  825. log_warn(LD_PROTOCOL,
  826. "received INTRO_ESTABLISHED cell on non-intro circuit.");
  827. goto err;
  828. }
  829. service = rend_service_get_by_pk_digest_and_version(
  830. circuit->rend_pk_digest, circuit->rend_desc_version);
  831. if (!service) {
  832. log_warn(LD_REND, "Unknown service on introduction circuit %d.",
  833. circuit->_base.n_circ_id);
  834. goto err;
  835. }
  836. service->desc_is_dirty = time(NULL);
  837. circuit->_base.purpose = CIRCUIT_PURPOSE_S_INTRO;
  838. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32 + 1,
  839. circuit->rend_pk_digest, REND_SERVICE_ID_LEN);
  840. log_info(LD_REND,
  841. "Received INTRO_ESTABLISHED cell on circuit %d for service %s",
  842. circuit->_base.n_circ_id, serviceid);
  843. return 0;
  844. err:
  845. circuit_mark_for_close(TO_CIRCUIT(circuit), END_CIRC_REASON_TORPROTOCOL);
  846. return -1;
  847. }
  848. /** Called once a circuit to a rendezvous point is established: sends a
  849. * RELAY_COMMAND_RENDEZVOUS1 cell.
  850. */
  851. void
  852. rend_service_rendezvous_has_opened(origin_circuit_t *circuit)
  853. {
  854. rend_service_t *service;
  855. char buf[RELAY_PAYLOAD_SIZE];
  856. crypt_path_t *hop;
  857. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  858. char hexcookie[9];
  859. int reason;
  860. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  861. tor_assert(circuit->cpath);
  862. tor_assert(circuit->build_state);
  863. hop = circuit->build_state->pending_final_cpath;
  864. tor_assert(hop);
  865. base16_encode(hexcookie,9,circuit->rend_cookie,4);
  866. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  867. circuit->rend_pk_digest, REND_SERVICE_ID_LEN);
  868. log_info(LD_REND,
  869. "Done building circuit %d to rendezvous with "
  870. "cookie %s for service %s",
  871. circuit->_base.n_circ_id, hexcookie, serviceid);
  872. service = rend_service_get_by_pk_digest_and_version(
  873. circuit->rend_pk_digest, circuit->rend_desc_version);
  874. if (!service) {
  875. log_warn(LD_GENERAL, "Internal error: unrecognized service ID on "
  876. "introduction circuit.");
  877. reason = END_CIRC_REASON_INTERNAL;
  878. goto err;
  879. }
  880. /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
  881. memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
  882. if (crypto_dh_get_public(hop->dh_handshake_state,
  883. buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
  884. log_warn(LD_GENERAL,"Couldn't get DH public key.");
  885. reason = END_CIRC_REASON_INTERNAL;
  886. goto err;
  887. }
  888. memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
  889. DIGEST_LEN);
  890. /* Send the cell */
  891. if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit),
  892. RELAY_COMMAND_RENDEZVOUS1,
  893. buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
  894. circuit->cpath->prev)<0) {
  895. log_warn(LD_GENERAL, "Couldn't send RENDEZVOUS1 cell.");
  896. reason = END_CIRC_REASON_INTERNAL;
  897. goto err;
  898. }
  899. crypto_dh_free(hop->dh_handshake_state);
  900. hop->dh_handshake_state = NULL;
  901. /* Append the cpath entry. */
  902. hop->state = CPATH_STATE_OPEN;
  903. /* set the windows to default. these are the windows
  904. * that bob thinks alice has.
  905. */
  906. hop->package_window = CIRCWINDOW_START;
  907. hop->deliver_window = CIRCWINDOW_START;
  908. onion_append_to_cpath(&circuit->cpath, hop);
  909. circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
  910. /* Change the circuit purpose. */
  911. circuit->_base.purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
  912. return;
  913. err:
  914. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  915. }
  916. /*
  917. * Manage introduction points
  918. */
  919. /** Return the (possibly non-open) introduction circuit ending at
  920. * <b>intro</b> for the service whose public key is <b>pk_digest</b> and
  921. * which publishes descriptor of version <b>desc_version</b>. Return
  922. * NULL if no such service is found.
  923. */
  924. static origin_circuit_t *
  925. find_intro_circuit(rend_intro_point_t *intro, const char *pk_digest,
  926. int desc_version)
  927. {
  928. origin_circuit_t *circ = NULL;
  929. tor_assert(intro);
  930. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  931. CIRCUIT_PURPOSE_S_INTRO))) {
  932. if (!strcasecmp(circ->build_state->chosen_exit->identity_digest,
  933. intro->extend_info->identity_digest) &&
  934. circ->rend_desc_version == desc_version) {
  935. return circ;
  936. }
  937. }
  938. circ = NULL;
  939. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  940. CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
  941. if (!strcasecmp(circ->build_state->chosen_exit->identity_digest,
  942. intro->extend_info->identity_digest) &&
  943. circ->rend_desc_version == desc_version) {
  944. return circ;
  945. }
  946. }
  947. return NULL;
  948. }
  949. /** Determine the responsible hidden service directories for the
  950. * rend_encoded_v2_service_descriptor_t's in <b>descs</b> and upload them;
  951. * <b>service_id</b> and <b>seconds_valid</b> are only passed for logging
  952. * purposes. */
  953. static void
  954. directory_post_to_hs_dir(smartlist_t *descs, const char *service_id,
  955. int seconds_valid)
  956. {
  957. int i, j;
  958. smartlist_t *responsible_dirs = smartlist_create();
  959. routerstatus_t *hs_dir;
  960. for (i = 0; i < smartlist_len(descs); i++) {
  961. rend_encoded_v2_service_descriptor_t *desc = smartlist_get(descs, i);
  962. /* Determine responsible dirs. */
  963. if (hid_serv_get_responsible_directories(responsible_dirs,
  964. desc->desc_id) < 0) {
  965. log_warn(LD_REND, "Could not determine the responsible hidden service "
  966. "directories to post descriptors to.");
  967. smartlist_free(responsible_dirs);
  968. return;
  969. }
  970. for (j = 0; j < smartlist_len(responsible_dirs); j++) {
  971. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  972. hs_dir = smartlist_get(responsible_dirs, j);
  973. /* Send publish request. */
  974. directory_initiate_command_routerstatus(hs_dir,
  975. DIR_PURPOSE_UPLOAD_RENDDESC_V2,
  976. ROUTER_PURPOSE_GENERAL,
  977. 1, NULL, desc->desc_str,
  978. strlen(desc->desc_str), 0);
  979. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  980. desc->desc_id, DIGEST_LEN);
  981. log_info(LD_REND, "Sending publish request for v2 descriptor for "
  982. "service '%s' with descriptor ID '%s' with validity "
  983. "of %d seconds to hidden service directory '%s' on "
  984. "port %d.",
  985. safe_str(service_id),
  986. safe_str(desc_id_base32),
  987. seconds_valid,
  988. hs_dir->nickname,
  989. hs_dir->dir_port);
  990. }
  991. smartlist_clear(responsible_dirs);
  992. }
  993. smartlist_free(responsible_dirs);
  994. }
  995. /** Encode and sign up-to-date v0 and/or v2 service descriptors for
  996. * <b>service</b>, and upload it/them to all the dirservers/to the
  997. * responsible hidden service directories.
  998. */
  999. static void
  1000. upload_service_descriptor(rend_service_t *service)
  1001. {
  1002. time_t now = time(NULL);
  1003. int rendpostperiod;
  1004. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  1005. int uploaded = 0;
  1006. /* Update the descriptor. */
  1007. rend_service_update_descriptor(service);
  1008. rendpostperiod = get_options()->RendPostPeriod;
  1009. /* Upload unversioned (v0) descriptor? */
  1010. if (service->descriptor_version == 0 &&
  1011. get_options()->PublishHidServDescriptors) {
  1012. char *desc;
  1013. size_t desc_len;
  1014. /* Encode the descriptor. */
  1015. if (rend_encode_service_descriptor(service->desc,
  1016. service->private_key,
  1017. &desc, &desc_len)<0) {
  1018. log_warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
  1019. "not uploading.");
  1020. return;
  1021. }
  1022. /* Post it to the dirservers */
  1023. rend_get_service_id(service->desc->pk, serviceid);
  1024. log_info(LD_REND, "Sending publish request for hidden service %s",
  1025. serviceid);
  1026. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC,
  1027. ROUTER_PURPOSE_GENERAL,
  1028. HIDSERV_AUTHORITY, desc, desc_len, 0);
  1029. tor_free(desc);
  1030. service->next_upload_time = now + rendpostperiod;
  1031. uploaded = 1;
  1032. }
  1033. /* Upload v2 descriptor? */
  1034. if (service->descriptor_version == 2 &&
  1035. get_options()->PublishHidServDescriptors) {
  1036. networkstatus_t *c = networkstatus_get_latest_consensus();
  1037. if (c && smartlist_len(c->routerstatus_list) > 0) {
  1038. int seconds_valid;
  1039. smartlist_t *descs = smartlist_create();
  1040. int i;
  1041. /* Encode the current descriptor. */
  1042. seconds_valid = rend_encode_v2_descriptors(descs, service->desc, now,
  1043. NULL, 0);
  1044. if (seconds_valid < 0) {
  1045. log_warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
  1046. "not uploading.");
  1047. smartlist_free(descs);
  1048. return;
  1049. }
  1050. /* Post the current descriptors to the hidden service directories. */
  1051. rend_get_service_id(service->desc->pk, serviceid);
  1052. log_info(LD_REND, "Sending publish request for hidden service %s",
  1053. serviceid);
  1054. directory_post_to_hs_dir(descs, serviceid, seconds_valid);
  1055. /* Free memory for descriptors. */
  1056. for (i = 0; i < smartlist_len(descs); i++)
  1057. rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
  1058. smartlist_clear(descs);
  1059. /* Update next upload time. */
  1060. if (seconds_valid - REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
  1061. > rendpostperiod)
  1062. service->next_upload_time = now + rendpostperiod;
  1063. else if (seconds_valid < REND_TIME_PERIOD_OVERLAPPING_V2_DESCS)
  1064. service->next_upload_time = now + seconds_valid + 1;
  1065. else
  1066. service->next_upload_time = now + seconds_valid -
  1067. REND_TIME_PERIOD_OVERLAPPING_V2_DESCS + 1;
  1068. /* Post also the next descriptors, if necessary. */
  1069. if (seconds_valid < REND_TIME_PERIOD_OVERLAPPING_V2_DESCS) {
  1070. seconds_valid = rend_encode_v2_descriptors(descs, service->desc,
  1071. now, NULL, 1);
  1072. if (seconds_valid < 0) {
  1073. log_warn(LD_BUG, "Internal error: couldn't encode service "
  1074. "descriptor; not uploading.");
  1075. smartlist_free(descs);
  1076. return;
  1077. }
  1078. directory_post_to_hs_dir(descs, serviceid, seconds_valid);
  1079. /* Free memory for descriptors. */
  1080. for (i = 0; i < smartlist_len(descs); i++)
  1081. rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
  1082. }
  1083. smartlist_free(descs);
  1084. uploaded = 1;
  1085. log_info(LD_REND, "Successfully uploaded v2 rend descriptors!");
  1086. }
  1087. }
  1088. /* If not uploaded, try again in one minute. */
  1089. if (!uploaded)
  1090. service->next_upload_time = now + 60;
  1091. /* Unmark dirty flag of this service. */
  1092. service->desc_is_dirty = 0;
  1093. }
  1094. /** For every service, check how many intro points it currently has, and:
  1095. * - Pick new intro points as necessary.
  1096. * - Launch circuits to any new intro points.
  1097. */
  1098. void
  1099. rend_services_introduce(void)
  1100. {
  1101. int i,j,r;
  1102. routerinfo_t *router;
  1103. rend_service_t *service;
  1104. rend_intro_point_t *intro;
  1105. int changed, prev_intro_nodes;
  1106. smartlist_t *intro_routers, *exclude_routers;
  1107. time_t now;
  1108. intro_routers = smartlist_create();
  1109. exclude_routers = smartlist_create();
  1110. now = time(NULL);
  1111. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  1112. smartlist_clear(intro_routers);
  1113. service = smartlist_get(rend_service_list, i);
  1114. tor_assert(service);
  1115. changed = 0;
  1116. if (now > service->intro_period_started+INTRO_CIRC_RETRY_PERIOD) {
  1117. /* One period has elapsed; we can try building circuits again. */
  1118. service->intro_period_started = now;
  1119. service->n_intro_circuits_launched = 0;
  1120. } else if (service->n_intro_circuits_launched >=
  1121. MAX_INTRO_CIRCS_PER_PERIOD) {
  1122. /* We have failed too many times in this period; wait for the next
  1123. * one before we try again. */
  1124. continue;
  1125. }
  1126. /* Find out which introduction points we have in progress for this
  1127. service. */
  1128. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  1129. intro = smartlist_get(service->intro_nodes, j);
  1130. router = router_get_by_digest(intro->extend_info->identity_digest);
  1131. if (!router || !find_intro_circuit(intro, service->pk_digest,
  1132. service->descriptor_version)) {
  1133. log_info(LD_REND,"Giving up on %s as intro point for %s.",
  1134. intro->extend_info->nickname, service->service_id);
  1135. rend_intro_point_free(intro);
  1136. smartlist_del(service->intro_nodes,j--);
  1137. changed = 1;
  1138. service->desc_is_dirty = now;
  1139. }
  1140. smartlist_add(intro_routers, router);
  1141. }
  1142. /* We have enough intro points, and the intro points we thought we had were
  1143. * all connected.
  1144. */
  1145. if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS) {
  1146. /* We have all our intro points! Start a fresh period and reset the
  1147. * circuit count. */
  1148. service->intro_period_started = now;
  1149. service->n_intro_circuits_launched = 0;
  1150. continue;
  1151. }
  1152. /* Remember how many introduction circuits we started with. */
  1153. prev_intro_nodes = smartlist_len(service->intro_nodes);
  1154. smartlist_add_all(exclude_routers, intro_routers);
  1155. /* The directory is now here. Pick three ORs as intro points. */
  1156. for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
  1157. router = router_choose_random_node(service->intro_prefer_nodes,
  1158. service->intro_exclude_nodes, exclude_routers, 1, 0, 0,
  1159. get_options()->_AllowInvalid & ALLOW_INVALID_INTRODUCTION,
  1160. 0, 0);
  1161. if (!router) {
  1162. log_warn(LD_REND,
  1163. "Could only establish %d introduction points for %s.",
  1164. smartlist_len(service->intro_nodes), service->service_id);
  1165. break;
  1166. }
  1167. changed = 1;
  1168. smartlist_add(intro_routers, router);
  1169. smartlist_add(exclude_routers, router);
  1170. intro = tor_malloc_zero(sizeof(rend_intro_point_t));
  1171. intro->extend_info = extend_info_from_router(router);
  1172. if (service->descriptor_version == 2) {
  1173. intro->intro_key = crypto_new_pk_env();
  1174. tor_assert(!crypto_pk_generate_key(intro->intro_key));
  1175. }
  1176. smartlist_add(service->intro_nodes, intro);
  1177. log_info(LD_REND, "Picked router %s as an intro point for %s.",
  1178. router->nickname, service->service_id);
  1179. }
  1180. /* Reset exclude_routers, for the next time around the loop. */
  1181. smartlist_clear(exclude_routers);
  1182. /* If there's no need to launch new circuits, stop here. */
  1183. if (!changed)
  1184. continue;
  1185. /* Establish new introduction points. */
  1186. for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
  1187. intro = smartlist_get(service->intro_nodes, j);
  1188. r = rend_service_launch_establish_intro(service, intro);
  1189. if (r<0) {
  1190. log_warn(LD_REND, "Error launching circuit to node %s for service %s.",
  1191. intro->extend_info->nickname, service->service_id);
  1192. }
  1193. }
  1194. }
  1195. smartlist_free(intro_routers);
  1196. smartlist_free(exclude_routers);
  1197. }
  1198. /** Regenerate and upload rendezvous service descriptors for all
  1199. * services, if necessary. If the descriptor has been dirty enough
  1200. * for long enough, definitely upload; else only upload when the
  1201. * periodic timeout has expired.
  1202. *
  1203. * For the first upload, pick a random time between now and two periods
  1204. * from now, and pick it independently for each service.
  1205. */
  1206. void
  1207. rend_consider_services_upload(time_t now)
  1208. {
  1209. int i;
  1210. rend_service_t *service;
  1211. int rendpostperiod = get_options()->RendPostPeriod;
  1212. if (!get_options()->PublishHidServDescriptors)
  1213. return;
  1214. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  1215. service = smartlist_get(rend_service_list, i);
  1216. if (!service->next_upload_time) { /* never been uploaded yet */
  1217. service->next_upload_time =
  1218. now + crypto_rand_int(2*rendpostperiod);
  1219. }
  1220. if (service->next_upload_time < now ||
  1221. (service->desc_is_dirty &&
  1222. service->desc_is_dirty < now-30)) {
  1223. /* if it's time, or if the directory servers have a wrong service
  1224. * descriptor and ours has been stable for 30 seconds, upload a
  1225. * new one of each format. */
  1226. upload_service_descriptor(service);
  1227. }
  1228. }
  1229. }
  1230. /** Log the status of introduction points for all rendezvous services
  1231. * at log severity <b>severity</b>.
  1232. */
  1233. void
  1234. rend_service_dump_stats(int severity)
  1235. {
  1236. int i,j;
  1237. rend_service_t *service;
  1238. rend_intro_point_t *intro;
  1239. const char *safe_name;
  1240. origin_circuit_t *circ;
  1241. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  1242. service = smartlist_get(rend_service_list, i);
  1243. log(severity, LD_GENERAL, "Service configured in \"%s\":",
  1244. service->directory);
  1245. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  1246. intro = smartlist_get(service->intro_nodes, j);
  1247. safe_name = safe_str(intro->extend_info->nickname);
  1248. circ = find_intro_circuit(intro, service->pk_digest,
  1249. service->descriptor_version);
  1250. if (!circ) {
  1251. log(severity, LD_GENERAL, " Intro point %d at %s: no circuit",
  1252. j, safe_name);
  1253. continue;
  1254. }
  1255. log(severity, LD_GENERAL, " Intro point %d at %s: circuit is %s",
  1256. j, safe_name, circuit_state_to_string(circ->_base.state));
  1257. }
  1258. }
  1259. }
  1260. /** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
  1261. * 'circ', and look up the port and address based on conn-\>port.
  1262. * Assign the actual conn-\>addr and conn-\>port. Return -1 if failure,
  1263. * or 0 for success.
  1264. */
  1265. int
  1266. rend_service_set_connection_addr_port(edge_connection_t *conn,
  1267. origin_circuit_t *circ)
  1268. {
  1269. rend_service_t *service;
  1270. char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
  1271. smartlist_t *matching_ports;
  1272. rend_service_port_config_t *chosen_port;
  1273. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  1274. log_debug(LD_REND,"beginning to hunt for addr/port");
  1275. base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
  1276. circ->rend_pk_digest, REND_SERVICE_ID_LEN);
  1277. service = rend_service_get_by_pk_digest_and_version(circ->rend_pk_digest,
  1278. circ->rend_desc_version);
  1279. if (!service) {
  1280. log_warn(LD_REND, "Couldn't find any service associated with pk %s on "
  1281. "rendezvous circuit %d; closing.",
  1282. serviceid, circ->_base.n_circ_id);
  1283. return -1;
  1284. }
  1285. matching_ports = smartlist_create();
  1286. SMARTLIST_FOREACH(service->ports, rend_service_port_config_t *, p,
  1287. {
  1288. if (conn->_base.port == p->virtual_port) {
  1289. smartlist_add(matching_ports, p);
  1290. }
  1291. });
  1292. chosen_port = smartlist_choose(matching_ports);
  1293. smartlist_free(matching_ports);
  1294. if (chosen_port) {
  1295. conn->_base.addr = chosen_port->real_addr;
  1296. conn->_base.port = chosen_port->real_port;
  1297. return 0;
  1298. }
  1299. log_info(LD_REND, "No virtual port mapping exists for port %d on service %s",
  1300. conn->_base.port,serviceid);
  1301. return -1;
  1302. }