rendservice.c 49 KB

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