rendservice.c 49 KB

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