rendservice.c 50 KB

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