rendservice.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char rendservice_c_id[] =
  5. "$Id$";
  6. /**
  7. * \file rendservice.c
  8. * \brief The hidden-service side of rendezvous functionality.
  9. **/
  10. #include "or.h"
  11. static circuit_t *find_intro_circuit(routerinfo_t *router,
  12. const char *pk_digest);
  13. /** Represents the mapping from a virtual port of a rendezvous service to
  14. * a real port on some IP.
  15. */
  16. typedef struct rend_service_port_config_t {
  17. uint16_t virtual_port;
  18. uint16_t real_port;
  19. uint32_t real_addr;
  20. } rend_service_port_config_t;
  21. /** Try to maintain this many intro points per service if possible. */
  22. #define NUM_INTRO_POINTS 3
  23. /** If we can't build our intro circuits, don't retry for this long. */
  24. #define INTRO_CIRC_RETRY_PERIOD 60*5
  25. /** Don't try to build more than this many circuits before giving up
  26. * for a while.*/
  27. #define MAX_INTRO_CIRCS_PER_PERIOD 10
  28. /** How many times will a hidden service operator attempt to connect to
  29. * a requested rendezvous point before giving up? */
  30. #define MAX_REND_FAILURES 30
  31. /** How many seconds should we spend trying to connect to a requested
  32. * rendezvous point before giving up? */
  33. #define MAX_REND_TIMEOUT 30
  34. /** Represents a single hidden service running at this OP. */
  35. typedef struct rend_service_t {
  36. /** Fields specified in config file */
  37. char *directory; /**< where in the filesystem it stores it */
  38. smartlist_t *ports; /**< List of rend_service_port_config_t */
  39. char *intro_prefer_nodes; /**< comma-separated list of nicknames */
  40. char *intro_exclude_nodes; /**< comma-separated list of nicknames */
  41. /* Other fields */
  42. crypto_pk_env_t *private_key;
  43. char service_id[REND_SERVICE_ID_LEN+1];
  44. char pk_digest[DIGEST_LEN];
  45. smartlist_t *intro_nodes; /**< list of hexdigests for intro points we have,
  46. * or are trying to establish. */
  47. time_t intro_period_started;
  48. int n_intro_circuits_launched; /**< count of intro circuits we have
  49. * established in this period. */
  50. rend_service_descriptor_t *desc;
  51. time_t desc_is_dirty;
  52. time_t next_upload_time;
  53. } rend_service_t;
  54. /** A list of rend_service_t's for services run on this OP.
  55. */
  56. static smartlist_t *rend_service_list = NULL;
  57. /** Return the number of rendezvous services we have configured. */
  58. int
  59. num_rend_services(void)
  60. {
  61. if (!rend_service_list)
  62. return 0;
  63. return smartlist_len(rend_service_list);
  64. }
  65. /** Release the storage held by <b>service</b>.
  66. */
  67. static void
  68. rend_service_free(rend_service_t *service)
  69. {
  70. if (!service) return;
  71. tor_free(service->directory);
  72. SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p));
  73. smartlist_free(service->ports);
  74. if (service->private_key)
  75. crypto_free_pk_env(service->private_key);
  76. tor_free(service->intro_prefer_nodes);
  77. tor_free(service->intro_exclude_nodes);
  78. SMARTLIST_FOREACH(service->intro_nodes, void*, p, tor_free(p));
  79. smartlist_free(service->intro_nodes);
  80. if (service->desc)
  81. rend_service_descriptor_free(service->desc);
  82. tor_free(service);
  83. }
  84. /** Release all the storage held in rend_service_list.
  85. */
  86. void
  87. rend_service_free_all(void)
  88. {
  89. if (!rend_service_list) {
  90. return;
  91. }
  92. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr,
  93. rend_service_free(ptr));
  94. smartlist_free(rend_service_list);
  95. rend_service_list = NULL;
  96. }
  97. /** Validate <b>service</b> and add it to rend_service_list if possible.
  98. */
  99. static void
  100. add_service(rend_service_t *service)
  101. {
  102. int i;
  103. rend_service_port_config_t *p;
  104. struct in_addr addr;
  105. if (!service->intro_prefer_nodes)
  106. service->intro_prefer_nodes = tor_strdup("");
  107. if (!service->intro_exclude_nodes)
  108. service->intro_exclude_nodes = tor_strdup("");
  109. if (!smartlist_len(service->ports)) {
  110. warn(LD_CONFIG, "Hidden service with no ports configured; ignoring.");
  111. rend_service_free(service);
  112. } else {
  113. smartlist_set_capacity(service->ports, -1);
  114. smartlist_add(rend_service_list, service);
  115. debug(LD_REND,"Configuring service with directory \"%s\"",
  116. service->directory);
  117. for (i = 0; i < smartlist_len(service->ports); ++i) {
  118. char addrbuf[INET_NTOA_BUF_LEN];
  119. p = smartlist_get(service->ports, i);
  120. addr.s_addr = htonl(p->real_addr);
  121. tor_inet_ntoa(&addr, addrbuf, sizeof(addrbuf));
  122. debug(LD_REND,"Service maps port %d to %s:%d",
  123. p->virtual_port, addrbuf, p->real_port);
  124. }
  125. }
  126. }
  127. /** Parses a real-port to virtual-port mapping and returns a new
  128. * rend_service_port_config_t.
  129. *
  130. * The format is: VirtualPort (IP|RealPort|IP:RealPort)?
  131. *
  132. * IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
  133. */
  134. static rend_service_port_config_t *
  135. parse_port_config(const char *string)
  136. {
  137. smartlist_t *sl;
  138. int virtport;
  139. int realport;
  140. uint16_t p;
  141. uint32_t addr;
  142. const char *addrport;
  143. rend_service_port_config_t *result = NULL;
  144. sl = smartlist_create();
  145. smartlist_split_string(sl, string, " ",
  146. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  147. if (smartlist_len(sl) < 1 || smartlist_len(sl) > 2) {
  148. warn(LD_CONFIG, "Bad syntax in hidden service port configuration.");
  149. goto err;
  150. }
  151. virtport = atoi(smartlist_get(sl,0));
  152. if (virtport < 1 || virtport > 65535) {
  153. warn(LD_CONFIG, "Missing or invalid port in hidden service port "
  154. "configuration.");
  155. goto err;
  156. }
  157. if (smartlist_len(sl) == 1) {
  158. /* No addr:port part; use default. */
  159. realport = virtport;
  160. addr = 0x7F000001u; /* 127.0.0.1 */
  161. } else {
  162. addrport = smartlist_get(sl,1);
  163. if (strchr(addrport, ':') || strchr(addrport, '.')) {
  164. if (parse_addr_port(addrport, NULL, &addr, &p)<0) {
  165. warn(LD_CONFIG,"Unparseable address in hidden service port "
  166. "configuration.");
  167. goto err;
  168. }
  169. realport = p?p:virtport;
  170. } else {
  171. /* No addr:port, no addr -- must be port. */
  172. realport = atoi(addrport);
  173. if (realport < 1 || realport > 65535)
  174. goto err;
  175. addr = 0x7F000001u; /* Default to 127.0.0.1 */
  176. }
  177. }
  178. result = tor_malloc(sizeof(rend_service_port_config_t));
  179. result->virtual_port = virtport;
  180. result->real_port = realport;
  181. result->real_addr = addr;
  182. err:
  183. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  184. smartlist_free(sl);
  185. return result;
  186. }
  187. /** Set up rend_service_list, based on the values of HiddenServiceDir and
  188. * HiddenServicePort in <b>options</b>. Return 0 on success and -1 on
  189. * failure. (If <b>validate_only</b> is set, parse, warn and return as
  190. * normal, but don't actually change the configured services.)
  191. */
  192. int
  193. rend_config_services(or_options_t *options, int validate_only)
  194. {
  195. config_line_t *line;
  196. rend_service_t *service = NULL;
  197. rend_service_port_config_t *portcfg;
  198. if (!validate_only) {
  199. rend_service_free_all();
  200. rend_service_list = smartlist_create();
  201. }
  202. for (line = options->RendConfigLines; line; line = line->next) {
  203. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  204. if (service) {
  205. if (validate_only)
  206. rend_service_free(service);
  207. else
  208. add_service(service);
  209. }
  210. service = tor_malloc_zero(sizeof(rend_service_t));
  211. service->directory = tor_strdup(line->value);
  212. service->ports = smartlist_create();
  213. service->intro_nodes = smartlist_create();
  214. service->intro_period_started = time(NULL);
  215. continue;
  216. }
  217. if (!service) {
  218. warn(LD_CONFIG, "HiddenServicePort with no preceding HiddenServiceDir "
  219. "directive.");
  220. rend_service_free(service);
  221. return -1;
  222. }
  223. if (!strcasecmp(line->key, "HiddenServicePort")) {
  224. portcfg = parse_port_config(line->value);
  225. if (!portcfg) {
  226. rend_service_free(service);
  227. return -1;
  228. }
  229. smartlist_add(service->ports, portcfg);
  230. } else if (!strcasecmp(line->key, "HiddenServiceNodes")) {
  231. if (service->intro_prefer_nodes) {
  232. warn(LD_CONFIG, "Got multiple HiddenServiceNodes lines for a single "
  233. "service.");
  234. return -1;
  235. }
  236. service->intro_prefer_nodes = tor_strdup(line->value);
  237. } else {
  238. tor_assert(!strcasecmp(line->key, "HiddenServiceExcludeNodes"));
  239. if (service->intro_exclude_nodes) {
  240. warn(LD_CONFIG, "Got multiple HiddenServiceExcludedNodes lines for "
  241. "a single service.");
  242. return -1;
  243. }
  244. service->intro_exclude_nodes = tor_strdup(line->value);
  245. }
  246. }
  247. if (service) {
  248. if (validate_only)
  249. rend_service_free(service);
  250. else
  251. add_service(service);
  252. }
  253. return 0;
  254. }
  255. /** Replace the old value of <b>service</b>-\>desc with one that reflects
  256. * the other fields in service.
  257. */
  258. static void
  259. rend_service_update_descriptor(rend_service_t *service)
  260. {
  261. rend_service_descriptor_t *d;
  262. circuit_t *circ;
  263. int i,n;
  264. routerinfo_t *router;
  265. if (service->desc) {
  266. rend_service_descriptor_free(service->desc);
  267. service->desc = NULL;
  268. }
  269. d = service->desc = tor_malloc(sizeof(rend_service_descriptor_t));
  270. d->pk = crypto_pk_dup_key(service->private_key);
  271. d->timestamp = time(NULL);
  272. d->version = 1;
  273. n = smartlist_len(service->intro_nodes);
  274. d->n_intro_points = 0;
  275. d->intro_points = tor_malloc_zero(sizeof(char*)*n);
  276. d->intro_point_extend_info = tor_malloc_zero(sizeof(extend_info_t*)*n);
  277. d->protocols = (1<<2) | (1<<0); /* We support protocol 2 and protocol 0. */
  278. for (i=0; i < n; ++i) {
  279. router = router_get_by_nickname(smartlist_get(service->intro_nodes, i),1);
  280. if (!router) {
  281. info(LD_REND,"Router '%s' not found. Skipping.",
  282. (char*)smartlist_get(service->intro_nodes, i));
  283. continue;
  284. }
  285. circ = find_intro_circuit(router, service->pk_digest);
  286. if (circ && circ->purpose == CIRCUIT_PURPOSE_S_INTRO) {
  287. /* We have an entirely established intro circuit. */
  288. d->intro_points[d->n_intro_points] = tor_strdup(router->nickname);
  289. d->intro_point_extend_info[d->n_intro_points] =
  290. extend_info_from_router(router);
  291. d->n_intro_points++;
  292. }
  293. }
  294. }
  295. /** Load and/or generate private keys for all hidden services. Return 0 on
  296. * success, -1 on failure.
  297. */
  298. int
  299. rend_service_load_keys(void)
  300. {
  301. int i;
  302. rend_service_t *s;
  303. char fname[512];
  304. char buf[128];
  305. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  306. s = smartlist_get(rend_service_list,i);
  307. if (s->private_key)
  308. continue;
  309. info(LD_REND, "Loading hidden-service keys from \"%s\"", s->directory);
  310. /* Check/create directory */
  311. if (check_private_dir(s->directory, CPD_CREATE) < 0)
  312. return -1;
  313. /* Load key */
  314. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  315. strlcat(fname,"/private_key",sizeof(fname)) >= sizeof(fname)) {
  316. warn(LD_CONFIG, "Directory name too long: \"%s\".", s->directory);
  317. return -1;
  318. }
  319. s->private_key = init_key_from_file(fname);
  320. if (!s->private_key)
  321. return -1;
  322. /* Create service file */
  323. if (rend_get_service_id(s->private_key, s->service_id)<0) {
  324. warn(LD_BUG, "Internal error: couldn't encode service ID.");
  325. return -1;
  326. }
  327. if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
  328. warn(LD_BUG, "Bug: Couldn't compute hash of public key.");
  329. return -1;
  330. }
  331. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  332. strlcat(fname,"/hostname",sizeof(fname)) >= sizeof(fname)) {
  333. warn(LD_CONFIG, "Directory name too long: \"%s\".", s->directory);
  334. return -1;
  335. }
  336. tor_snprintf(buf, sizeof(buf),"%s.onion\n", s->service_id);
  337. if (write_str_to_file(fname,buf,0)<0)
  338. return -1;
  339. }
  340. return 0;
  341. }
  342. /** Return the service whose public key has a digest of <b>digest</b>. Return
  343. * NULL if no such service exists.
  344. */
  345. static rend_service_t *
  346. rend_service_get_by_pk_digest(const char* digest)
  347. {
  348. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
  349. if (!memcmp(s->pk_digest,digest,DIGEST_LEN)) return s);
  350. return NULL;
  351. }
  352. /** Return 1 if any virtual port in <b>service</b> wants a circuit
  353. * to have good uptime. Else return 0.
  354. */
  355. static int
  356. rend_service_requires_uptime(rend_service_t *service)
  357. {
  358. int i;
  359. rend_service_port_config_t *p;
  360. for (i=0; i < smartlist_len(service->ports); ++i) {
  361. p = smartlist_get(service->ports, i);
  362. if (smartlist_string_num_isin(get_options()->LongLivedPorts,
  363. p->virtual_port))
  364. return 1;
  365. }
  366. return 0;
  367. }
  368. /******
  369. * Handle cells
  370. ******/
  371. /** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
  372. * rendezvous point.
  373. */
  374. int
  375. rend_service_introduce(circuit_t *circuit, const char *request,
  376. size_t request_len)
  377. {
  378. char *ptr, *r_cookie;
  379. extend_info_t *extend_info = NULL;
  380. char buf[RELAY_PAYLOAD_SIZE];
  381. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN]; /* Holds KH, Df, Db, Kf, Kb */
  382. rend_service_t *service;
  383. int r, i;
  384. size_t len, keylen;
  385. crypto_dh_env_t *dh = NULL;
  386. circuit_t *launched = NULL;
  387. crypt_path_t *cpath = NULL;
  388. char serviceid[REND_SERVICE_ID_LEN+1];
  389. char hexcookie[9];
  390. int circ_needs_uptime;
  391. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  392. circuit->rend_pk_digest,10);
  393. info(LD_REND, "Received INTRODUCE2 cell for service %s on circ %d.",
  394. serviceid, circuit->n_circ_id);
  395. if (circuit->purpose != CIRCUIT_PURPOSE_S_INTRO) {
  396. warn(LD_PROTOCOL, "Got an INTRODUCE2 over a non-introduction circuit %d.",
  397. circuit->n_circ_id);
  398. return -1;
  399. }
  400. /* min key length plus digest length plus nickname length */
  401. if (request_len < DIGEST_LEN+REND_COOKIE_LEN+(MAX_NICKNAME_LEN+1)+
  402. DH_KEY_LEN+42) {
  403. warn(LD_PROTOCOL, "Got a truncated INTRODUCE2 cell on circ %d.",
  404. circuit->n_circ_id);
  405. return -1;
  406. }
  407. /* first DIGEST_LEN bytes of request is service pk digest */
  408. service = rend_service_get_by_pk_digest(request);
  409. if (!service) {
  410. warn(LD_REND, "Got an INTRODUCE2 cell for an unrecognized service %s.",
  411. serviceid);
  412. return -1;
  413. }
  414. if (memcmp(circuit->rend_pk_digest, request, DIGEST_LEN)) {
  415. base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request, 10);
  416. warn(LD_REND, "Got an INTRODUCE2 cell for the wrong service (%s).",
  417. serviceid);
  418. return -1;
  419. }
  420. keylen = crypto_pk_keysize(service->private_key);
  421. if (request_len < keylen+DIGEST_LEN) {
  422. warn(LD_PROTOCOL,
  423. "PK-encrypted portion of INTRODUCE2 cell was truncated.");
  424. return -1;
  425. }
  426. /* Next N bytes is encrypted with service key */
  427. r = crypto_pk_private_hybrid_decrypt(
  428. service->private_key,buf,request+DIGEST_LEN,request_len-DIGEST_LEN,
  429. PK_PKCS1_OAEP_PADDING,1);
  430. if (r<0) {
  431. warn(LD_PROTOCOL, "Couldn't decrypt INTRODUCE2 cell.");
  432. return -1;
  433. }
  434. len = r;
  435. if (*buf == 2) {
  436. /* Version 2 INTRODUCE2 cell. */
  437. int klen;
  438. extend_info = tor_malloc_zero(sizeof(extend_info_t));
  439. extend_info->addr = ntohl(get_uint32(buf+1));
  440. extend_info->port = ntohs(get_uint16(buf+5));
  441. memcpy(extend_info->identity_digest, buf+7, DIGEST_LEN);
  442. extend_info->nickname[0] = '$';
  443. base16_encode(extend_info->nickname+1, sizeof(extend_info->nickname)-1,
  444. extend_info->identity_digest, DIGEST_LEN);
  445. klen = ntohs(get_uint16(buf+7+DIGEST_LEN));
  446. if ((int)len != 7+DIGEST_LEN+2+klen+20+128) {
  447. warn(LD_PROTOCOL, "Bad length %u for version 2 INTRODUCE2 cell.",
  448. (int)len);
  449. goto err;
  450. }
  451. extend_info->onion_key = crypto_pk_asn1_decode(buf+7+DIGEST_LEN+2, klen);
  452. if (!extend_info->onion_key) {
  453. warn(LD_PROTOCOL,
  454. "Error decoding onion key in version 2 INTRODUCE2 cell.");
  455. goto err;
  456. }
  457. ptr = buf+7+DIGEST_LEN+2+klen;
  458. len -= 7+DIGEST_LEN+2+klen;
  459. } else {
  460. char *rp_nickname;
  461. size_t nickname_field_len;
  462. routerinfo_t *router;
  463. int version;
  464. if (*buf == 1) {
  465. rp_nickname = buf+1;
  466. nickname_field_len = MAX_HEX_NICKNAME_LEN+1;
  467. version = 1;
  468. } else {
  469. nickname_field_len = MAX_NICKNAME_LEN+1;
  470. rp_nickname = buf;
  471. version = 0;
  472. }
  473. /* XXX when 0.1.0.x is obsolete, change this to reject version < 2. */
  474. ptr=memchr(rp_nickname,0,nickname_field_len);
  475. if (!ptr || ptr == rp_nickname) {
  476. warn(LD_PROTOCOL,
  477. "Couldn't find a null-padded nickname in INTRODUCE2 cell.");
  478. return -1;
  479. }
  480. if ((version == 0 && !is_legal_nickname(rp_nickname)) ||
  481. (version == 1 && !is_legal_nickname_or_hexdigest(rp_nickname))) {
  482. warn(LD_PROTOCOL, "Bad nickname in INTRODUCE2 cell.");
  483. return -1;
  484. }
  485. /* Okay, now we know that a nickname is at the start of the buffer. */
  486. ptr = rp_nickname+nickname_field_len;
  487. len -= nickname_field_len;
  488. len -= rp_nickname - buf; /* also remove header space used by version, if
  489. * any */
  490. router = router_get_by_nickname(rp_nickname, 0);
  491. if (!router) {
  492. info(LD_REND, "Couldn't find router '%s' named in rendezvous cell.",
  493. rp_nickname);
  494. goto err;
  495. }
  496. extend_info = extend_info_from_router(router);
  497. }
  498. if (len != REND_COOKIE_LEN+DH_KEY_LEN) {
  499. warn(LD_PROTOCOL, "Bad length %u for INTRODUCE2 cell.", (int)len);
  500. return -1;
  501. }
  502. r_cookie = ptr;
  503. base16_encode(hexcookie,9,r_cookie,4);
  504. /* Try DH handshake... */
  505. dh = crypto_dh_new();
  506. if (!dh || crypto_dh_generate_public(dh)<0) {
  507. warn(LD_BUG,"Internal error: couldn't build DH state "
  508. "or generate public key.");
  509. goto err;
  510. }
  511. if (crypto_dh_compute_secret(dh, ptr+REND_COOKIE_LEN, DH_KEY_LEN, keys,
  512. DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  513. warn(LD_BUG, "Internal error: couldn't complete DH handshake");
  514. goto err;
  515. }
  516. circ_needs_uptime = rend_service_requires_uptime(service);
  517. /* help predict this next time */
  518. rep_hist_note_used_internal(time(NULL), circ_needs_uptime, 1);
  519. /* Launch a circuit to alice's chosen rendezvous point.
  520. */
  521. for (i=0;i<MAX_REND_FAILURES;i++) {
  522. launched = circuit_launch_by_extend_info(
  523. CIRCUIT_PURPOSE_S_CONNECT_REND, extend_info,
  524. circ_needs_uptime, 1, 1);
  525. if (launched)
  526. break;
  527. }
  528. if (!launched) { /* give up */
  529. warn(LD_REND, "Giving up launching first hop of circuit to rendezvous "
  530. "point '%s' for service %s.",
  531. extend_info->nickname, serviceid);
  532. goto err;
  533. }
  534. info(LD_REND,
  535. "Accepted intro; launching circuit to '%s' (cookie %s) for service %s.",
  536. extend_info->nickname, hexcookie, serviceid);
  537. tor_assert(launched->build_state);
  538. /* Fill in the circuit's state. */
  539. memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
  540. DIGEST_LEN);
  541. memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
  542. strlcpy(launched->rend_query, service->service_id,
  543. sizeof(launched->rend_query));
  544. launched->build_state->pending_final_cpath = cpath =
  545. tor_malloc_zero(sizeof(crypt_path_t));
  546. cpath->magic = CRYPT_PATH_MAGIC;
  547. launched->build_state->expiry_time = time(NULL) + MAX_REND_TIMEOUT;
  548. cpath->dh_handshake_state = dh;
  549. dh = NULL;
  550. if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
  551. goto err;
  552. memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
  553. if (extend_info) extend_info_free(extend_info);
  554. return 0;
  555. err:
  556. if (dh) crypto_dh_free(dh);
  557. if (launched) circuit_mark_for_close(launched);
  558. if (extend_info) extend_info_free(extend_info);
  559. return -1;
  560. }
  561. /** Called when we fail building a rendezvous circuit at some point other
  562. * than the last hop: launches a new circuit to the same rendezvous point.
  563. */
  564. void
  565. rend_service_relaunch_rendezvous(circuit_t *oldcirc)
  566. {
  567. circuit_t *newcirc;
  568. cpath_build_state_t *newstate, *oldstate;
  569. tor_assert(oldcirc->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  570. if (!oldcirc->build_state ||
  571. oldcirc->build_state->failure_count > MAX_REND_FAILURES ||
  572. oldcirc->build_state->expiry_time < time(NULL)) {
  573. info(LD_REND,"Attempt to build circuit to %s for rendezvous has failed "
  574. "too many times or expired; giving up.",
  575. oldcirc->build_state ? oldcirc->build_state->chosen_exit->nickname :
  576. "*unknown*");
  577. return;
  578. }
  579. oldstate = oldcirc->build_state;
  580. tor_assert(oldstate);
  581. if (oldstate->pending_final_cpath == NULL) {
  582. info(LD_REND,"Skipping relaunch of circ that failed on its first hop. "
  583. "Initiator will retry.");
  584. return;
  585. }
  586. info(LD_REND,"Reattempting rendezvous circuit to %s",
  587. oldstate->chosen_exit->nickname);
  588. newcirc = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND,
  589. oldstate->chosen_exit, 0, 1, 1);
  590. if (!newcirc) {
  591. warn(LD_REND,"Couldn't relaunch rendezvous circuit to %s.",
  592. oldstate->chosen_exit->nickname);
  593. return;
  594. }
  595. newstate = newcirc->build_state;
  596. tor_assert(newstate);
  597. newstate->failure_count = oldstate->failure_count+1;
  598. newstate->expiry_time = oldstate->expiry_time;
  599. newstate->pending_final_cpath = oldstate->pending_final_cpath;
  600. oldstate->pending_final_cpath = NULL;
  601. memcpy(newcirc->rend_query, oldcirc->rend_query, REND_SERVICE_ID_LEN+1);
  602. memcpy(newcirc->rend_pk_digest, oldcirc->rend_pk_digest, DIGEST_LEN);
  603. memcpy(newcirc->rend_cookie, oldcirc->rend_cookie, REND_COOKIE_LEN);
  604. }
  605. /** Launch a circuit to serve as an introduction point for the service
  606. * <b>service</b> at the introduction point <b>nickname</b>
  607. */
  608. static int
  609. rend_service_launch_establish_intro(rend_service_t *service,
  610. const char *nickname)
  611. {
  612. circuit_t *launched;
  613. info(LD_REND, "Launching circuit to introduction point %s for service %s",
  614. nickname, service->service_id);
  615. rep_hist_note_used_internal(time(NULL), 1, 0);
  616. ++service->n_intro_circuits_launched;
  617. launched = circuit_launch_by_nickname(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  618. nickname, 1, 0, 1);
  619. if (!launched) {
  620. info(LD_REND, "Can't launch circuit to establish introduction at '%s'.",
  621. nickname);
  622. return -1;
  623. }
  624. strlcpy(launched->rend_query, service->service_id,
  625. sizeof(launched->rend_query));
  626. memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
  627. if (launched->state == CIRCUIT_STATE_OPEN)
  628. rend_service_intro_has_opened(launched);
  629. return 0;
  630. }
  631. /** Called when we're done building a circuit to an introduction point:
  632. * sends a RELAY_ESTABLISH_INTRO cell.
  633. */
  634. void
  635. rend_service_intro_has_opened(circuit_t *circuit)
  636. {
  637. rend_service_t *service;
  638. size_t len;
  639. int r;
  640. char buf[RELAY_PAYLOAD_SIZE];
  641. char auth[DIGEST_LEN + 9];
  642. char serviceid[REND_SERVICE_ID_LEN+1];
  643. tor_assert(circuit->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  644. tor_assert(CIRCUIT_IS_ORIGIN(circuit));
  645. tor_assert(circuit->cpath);
  646. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  647. circuit->rend_pk_digest,10);
  648. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  649. if (!service) {
  650. warn(LD_REND, "Unrecognized service ID %s on introduction circuit %d.",
  651. serviceid, circuit->n_circ_id);
  652. goto err;
  653. }
  654. info(LD_REND,
  655. "Established circuit %d as introduction point for service %s",
  656. circuit->n_circ_id, serviceid);
  657. /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
  658. len = crypto_pk_asn1_encode(service->private_key, buf+2,
  659. RELAY_PAYLOAD_SIZE-2);
  660. set_uint16(buf, htons((uint16_t)len));
  661. len += 2;
  662. memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
  663. memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
  664. if (crypto_digest(buf+len, auth, DIGEST_LEN+9))
  665. goto err;
  666. len += 20;
  667. r = crypto_pk_private_sign_digest(service->private_key, buf+len, buf, len);
  668. if (r<0) {
  669. warn(LD_BUG, "Internal error: couldn't sign introduction request.");
  670. goto err;
  671. }
  672. len += r;
  673. if (connection_edge_send_command(NULL, circuit,RELAY_COMMAND_ESTABLISH_INTRO,
  674. buf, len, circuit->cpath->prev)<0) {
  675. info(LD_GENERAL,
  676. "Couldn't send introduction request for service %s on circuit %d",
  677. serviceid, circuit->n_circ_id);
  678. goto err;
  679. }
  680. return;
  681. err:
  682. circuit_mark_for_close(circuit);
  683. }
  684. /** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
  685. * live introduction point, and note that the service descriptor is
  686. * now out-of-date.*/
  687. int
  688. rend_service_intro_established(circuit_t *circuit, const char *request,
  689. size_t request_len)
  690. {
  691. rend_service_t *service;
  692. if (circuit->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  693. warn(LD_PROTOCOL, "received INTRO_ESTABLISHED cell on non-intro circuit.");
  694. goto err;
  695. }
  696. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  697. if (!service) {
  698. warn(LD_REND, "Unknown service on introduction circuit %d.",
  699. circuit->n_circ_id);
  700. goto err;
  701. }
  702. service->desc_is_dirty = time(NULL);
  703. circuit->purpose = CIRCUIT_PURPOSE_S_INTRO;
  704. return 0;
  705. err:
  706. circuit_mark_for_close(circuit);
  707. return -1;
  708. }
  709. /** Called once a circuit to a rendezvous point is established: sends a
  710. * RELAY_COMMAND_RENDEZVOUS1 cell.
  711. */
  712. void
  713. rend_service_rendezvous_has_opened(circuit_t *circuit)
  714. {
  715. rend_service_t *service;
  716. char buf[RELAY_PAYLOAD_SIZE];
  717. crypt_path_t *hop;
  718. char serviceid[REND_SERVICE_ID_LEN+1];
  719. char hexcookie[9];
  720. tor_assert(circuit->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  721. tor_assert(circuit->cpath);
  722. tor_assert(circuit->build_state);
  723. hop = circuit->build_state->pending_final_cpath;
  724. tor_assert(hop);
  725. base16_encode(hexcookie,9,circuit->rend_cookie,4);
  726. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  727. circuit->rend_pk_digest,10);
  728. info(LD_REND,
  729. "Done building circuit %d to rendezvous with cookie %s for service %s",
  730. circuit->n_circ_id, hexcookie, serviceid);
  731. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  732. if (!service) {
  733. warn(LD_GENERAL, "Internal error: unrecognized service ID on "
  734. "introduction circuit.");
  735. goto err;
  736. }
  737. /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
  738. memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
  739. if (crypto_dh_get_public(hop->dh_handshake_state,
  740. buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
  741. warn(LD_GENERAL,"Couldn't get DH public key.");
  742. goto err;
  743. }
  744. memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
  745. DIGEST_LEN);
  746. /* Send the cell */
  747. if (connection_edge_send_command(NULL, circuit, RELAY_COMMAND_RENDEZVOUS1,
  748. buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
  749. circuit->cpath->prev)<0) {
  750. warn(LD_GENERAL, "Couldn't send RENDEZVOUS1 cell.");
  751. goto err;
  752. }
  753. crypto_dh_free(hop->dh_handshake_state);
  754. hop->dh_handshake_state = NULL;
  755. /* Append the cpath entry. */
  756. hop->state = CPATH_STATE_OPEN;
  757. /* set the windows to default. these are the windows
  758. * that bob thinks alice has.
  759. */
  760. hop->package_window = CIRCWINDOW_START;
  761. hop->deliver_window = CIRCWINDOW_START;
  762. onion_append_to_cpath(&circuit->cpath, hop);
  763. circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
  764. /* Change the circuit purpose. */
  765. circuit->purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
  766. return;
  767. err:
  768. circuit_mark_for_close(circuit);
  769. }
  770. /*
  771. * Manage introduction points
  772. */
  773. /** Return the (possibly non-open) introduction circuit ending at
  774. * <b>router</b> for the service whose public key is <b>pk_digest</b>. Return
  775. * NULL if no such service is found.
  776. */
  777. static circuit_t *
  778. find_intro_circuit(routerinfo_t *router, const char *pk_digest)
  779. {
  780. circuit_t *circ = NULL;
  781. tor_assert(router);
  782. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  783. CIRCUIT_PURPOSE_S_INTRO))) {
  784. tor_assert(circ->cpath);
  785. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  786. router->nickname)) {
  787. return circ;
  788. }
  789. }
  790. circ = NULL;
  791. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  792. CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
  793. tor_assert(circ->cpath);
  794. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  795. router->nickname)) {
  796. return circ;
  797. }
  798. }
  799. return NULL;
  800. }
  801. /** Encode and sign an up-to-date service descriptor for <b>service</b>,
  802. * and upload it to all the dirservers.
  803. */
  804. static void
  805. upload_service_descriptor(rend_service_t *service, int version)
  806. {
  807. char *desc;
  808. size_t desc_len;
  809. /* Update the descriptor. */
  810. rend_service_update_descriptor(service);
  811. if (rend_encode_service_descriptor(service->desc,
  812. version,
  813. service->private_key,
  814. &desc, &desc_len)<0) {
  815. warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
  816. "not uploading.");
  817. return;
  818. }
  819. /* Post it to the dirservers */
  820. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC, desc, desc_len);
  821. tor_free(desc);
  822. service->desc_is_dirty = 0;
  823. }
  824. /* XXXX Make this longer once directories remember service descriptors across
  825. * restarts.*/
  826. #define MAX_SERVICE_PUBLICATION_INTERVAL (15*60)
  827. /** For every service, check how many intro points it currently has, and:
  828. * - Pick new intro points as necessary.
  829. * - Launch circuits to any new intro points.
  830. */
  831. void
  832. rend_services_introduce(void)
  833. {
  834. int i,j,r;
  835. routerinfo_t *router;
  836. rend_service_t *service;
  837. char *intro;
  838. int changed, prev_intro_nodes;
  839. smartlist_t *intro_routers, *exclude_routers;
  840. time_t now;
  841. intro_routers = smartlist_create();
  842. exclude_routers = smartlist_create();
  843. now = time(NULL);
  844. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  845. smartlist_clear(intro_routers);
  846. service = smartlist_get(rend_service_list, i);
  847. tor_assert(service);
  848. changed = 0;
  849. if (now > service->intro_period_started+INTRO_CIRC_RETRY_PERIOD) {
  850. /* One period has elapsed; we can try building circuits again. */
  851. service->intro_period_started = now;
  852. service->n_intro_circuits_launched = 0;
  853. } else if (service->n_intro_circuits_launched >=
  854. MAX_INTRO_CIRCS_PER_PERIOD) {
  855. /* We have failed too many times in this period; wait for the next
  856. * one before we try again. */
  857. continue;
  858. }
  859. /* Find out which introduction points we have in progress for this
  860. service. */
  861. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  862. intro = smartlist_get(service->intro_nodes, j);
  863. router = router_get_by_nickname(intro, 0);
  864. if (!router || !find_intro_circuit(router,service->pk_digest)) {
  865. info(LD_REND,"Giving up on %s as intro point for %s.",
  866. intro, service->service_id);
  867. tor_free(intro);
  868. smartlist_del(service->intro_nodes,j--);
  869. changed = 1;
  870. service->desc_is_dirty = now;
  871. }
  872. smartlist_add(intro_routers, router);
  873. }
  874. /* We have enough intro points, and the intro points we thought we had were
  875. * all connected.
  876. */
  877. if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS) {
  878. /* We have all our intro points! Start a fresh period and reset the
  879. * circuit count. */
  880. service->intro_period_started = now;
  881. service->n_intro_circuits_launched = 0;
  882. continue;
  883. }
  884. /* Remember how many introduction circuits we started with. */
  885. prev_intro_nodes = smartlist_len(service->intro_nodes);
  886. smartlist_add_all(exclude_routers, intro_routers);
  887. /* The directory is now here. Pick three ORs as intro points. */
  888. for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
  889. char *hex_digest;
  890. router = router_choose_random_node(service->intro_prefer_nodes,
  891. service->intro_exclude_nodes, exclude_routers, 1, 0,
  892. get_options()->_AllowUnverified & ALLOW_UNVERIFIED_INTRODUCTION,
  893. 0);
  894. if (!router) {
  895. warn(LD_REND, "Could only establish %d introduction points for %s.",
  896. smartlist_len(service->intro_nodes), service->service_id);
  897. break;
  898. }
  899. changed = 1;
  900. hex_digest = tor_malloc_zero(HEX_DIGEST_LEN+2);
  901. hex_digest[0] = '$';
  902. base16_encode(hex_digest+1, HEX_DIGEST_LEN+1,
  903. router->cache_info.identity_digest,
  904. DIGEST_LEN);
  905. smartlist_add(intro_routers, router);
  906. smartlist_add(exclude_routers, router);
  907. smartlist_add(service->intro_nodes, hex_digest);
  908. info(LD_REND, "Picked router %s as an intro point for %s.",
  909. router->nickname, service->service_id);
  910. }
  911. /* Reset exclude_routers, for the next time around the loop. */
  912. smartlist_clear(exclude_routers);
  913. /* If there's no need to launch new circuits, stop here. */
  914. if (!changed)
  915. continue;
  916. /* Establish new introduction points. */
  917. for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
  918. intro = smartlist_get(service->intro_nodes, j);
  919. r = rend_service_launch_establish_intro(service, intro);
  920. if (r<0) {
  921. warn(LD_REND, "Error launching circuit to node %s for service %s.",
  922. intro, service->service_id);
  923. }
  924. }
  925. }
  926. smartlist_free(intro_routers);
  927. smartlist_free(exclude_routers);
  928. }
  929. /** Regenerate and upload rendezvous service descriptors for all
  930. * services, if necessary. If the descriptor has been dirty enough
  931. * for long enough, definitely upload; else only upload when the
  932. * periodic timeout has expired.
  933. *
  934. * For the first upload, pick a random time between now and two periods
  935. * from now, and pick it independently for each service.
  936. */
  937. void
  938. rend_consider_services_upload(time_t now)
  939. {
  940. int i;
  941. rend_service_t *service;
  942. int rendpostperiod = get_options()->RendPostPeriod;
  943. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  944. service = smartlist_get(rend_service_list, i);
  945. if (!service->next_upload_time) { /* never been uploaded yet */
  946. service->next_upload_time =
  947. now + crypto_rand_int(2*rendpostperiod);
  948. }
  949. if (service->next_upload_time < now ||
  950. (service->desc_is_dirty &&
  951. service->desc_is_dirty < now-5)) {
  952. /* if it's time, or if the directory servers have a wrong service
  953. * descriptor and ours has been stable for 5 seconds, upload a
  954. * new one of each format. */
  955. upload_service_descriptor(service, 0);
  956. // XXXX011 upload_service_descriptor(service, 1);
  957. service->next_upload_time = now + rendpostperiod;
  958. }
  959. }
  960. }
  961. /** Log the status of introduction points for all rendezvous services
  962. * at log severity <b>severity</b>.
  963. */
  964. void
  965. rend_service_dump_stats(int severity)
  966. {
  967. int i,j;
  968. routerinfo_t *router;
  969. rend_service_t *service;
  970. char *nickname;
  971. circuit_t *circ;
  972. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  973. service = smartlist_get(rend_service_list, i);
  974. log(severity, LD_GENERAL, "Service configured in \"%s\":",
  975. service->directory);
  976. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  977. nickname = smartlist_get(service->intro_nodes, j);
  978. router = router_get_by_nickname(smartlist_get(service->intro_nodes,j),1);
  979. if (!router) {
  980. log(severity, LD_GENERAL, " Intro point at %s: unrecognized router",
  981. nickname);
  982. continue;
  983. }
  984. circ = find_intro_circuit(router, service->pk_digest);
  985. if (!circ) {
  986. log(severity, LD_GENERAL, " Intro point at %s: no circuit",nickname);
  987. continue;
  988. }
  989. log(severity, LD_GENERAL, " Intro point at %s: circuit is %s",nickname,
  990. circuit_state_to_string(circ->state));
  991. }
  992. }
  993. }
  994. /** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
  995. * 'circ', and look up the port and address based on conn-\>port.
  996. * Assign the actual conn-\>addr and conn-\>port. Return -1 if failure,
  997. * or 0 for success.
  998. */
  999. int
  1000. rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ)
  1001. {
  1002. rend_service_t *service;
  1003. int i;
  1004. rend_service_port_config_t *p;
  1005. char serviceid[REND_SERVICE_ID_LEN+1];
  1006. tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  1007. debug(LD_REND,"beginning to hunt for addr/port");
  1008. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  1009. circ->rend_pk_digest,10);
  1010. service = rend_service_get_by_pk_digest(circ->rend_pk_digest);
  1011. if (!service) {
  1012. warn(LD_REND, "Couldn't find any service associated with pk %s on "
  1013. "rendezvous circuit %d; closing.",
  1014. serviceid, circ->n_circ_id);
  1015. return -1;
  1016. }
  1017. for (i = 0; i < smartlist_len(service->ports); ++i) {
  1018. p = smartlist_get(service->ports, i);
  1019. if (conn->port == p->virtual_port) {
  1020. conn->addr = p->real_addr;
  1021. conn->port = p->real_port;
  1022. return 0;
  1023. }
  1024. }
  1025. info(LD_REND, "No virtual port mapping exists for port %d on service %s",
  1026. conn->port,serviceid);
  1027. return -1;
  1028. }