rendservice.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /* Copyright 2004-2006 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. log_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. log_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. log_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. log_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. log_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. log_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. log_warn(LD_CONFIG, "%s with no preceding HiddenServiceDir directive",
  219. line->key);
  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. log_warn(LD_CONFIG,
  233. "Got multiple HiddenServiceNodes lines for a single "
  234. "service.");
  235. return -1;
  236. }
  237. service->intro_prefer_nodes = tor_strdup(line->value);
  238. } else {
  239. tor_assert(!strcasecmp(line->key, "HiddenServiceExcludeNodes"));
  240. if (service->intro_exclude_nodes) {
  241. log_warn(LD_CONFIG,
  242. "Got multiple HiddenServiceExcludedNodes lines for "
  243. "a single service.");
  244. return -1;
  245. }
  246. service->intro_exclude_nodes = tor_strdup(line->value);
  247. }
  248. }
  249. if (service) {
  250. if (validate_only)
  251. rend_service_free(service);
  252. else
  253. add_service(service);
  254. }
  255. return 0;
  256. }
  257. /** Replace the old value of <b>service</b>-\>desc with one that reflects
  258. * the other fields in service.
  259. */
  260. static void
  261. rend_service_update_descriptor(rend_service_t *service)
  262. {
  263. rend_service_descriptor_t *d;
  264. circuit_t *circ;
  265. int i,n;
  266. routerinfo_t *router;
  267. if (service->desc) {
  268. rend_service_descriptor_free(service->desc);
  269. service->desc = NULL;
  270. }
  271. d = service->desc = tor_malloc(sizeof(rend_service_descriptor_t));
  272. d->pk = crypto_pk_dup_key(service->private_key);
  273. d->timestamp = time(NULL);
  274. d->version = 1;
  275. n = smartlist_len(service->intro_nodes);
  276. d->n_intro_points = 0;
  277. d->intro_points = tor_malloc_zero(sizeof(char*)*n);
  278. d->intro_point_extend_info = tor_malloc_zero(sizeof(extend_info_t*)*n);
  279. d->protocols = (1<<2) | (1<<0); /* We support protocol 2 and protocol 0. */
  280. for (i=0; i < n; ++i) {
  281. router = router_get_by_nickname(smartlist_get(service->intro_nodes, i),1);
  282. if (!router) {
  283. log_info(LD_REND,"Router '%s' not found. Skipping.",
  284. (char*)smartlist_get(service->intro_nodes, i));
  285. continue;
  286. }
  287. circ = find_intro_circuit(router, service->pk_digest);
  288. if (circ && circ->purpose == CIRCUIT_PURPOSE_S_INTRO) {
  289. /* We have an entirely established intro circuit. */
  290. d->intro_points[d->n_intro_points] = tor_strdup(router->nickname);
  291. d->intro_point_extend_info[d->n_intro_points] =
  292. extend_info_from_router(router);
  293. d->n_intro_points++;
  294. }
  295. }
  296. }
  297. /** Load and/or generate private keys for all hidden services. Return 0 on
  298. * success, -1 on failure.
  299. */
  300. int
  301. rend_service_load_keys(void)
  302. {
  303. int i;
  304. rend_service_t *s;
  305. char fname[512];
  306. char buf[128];
  307. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  308. s = smartlist_get(rend_service_list,i);
  309. if (s->private_key)
  310. continue;
  311. log_info(LD_REND, "Loading hidden-service keys from \"%s\"",
  312. s->directory);
  313. /* Check/create directory */
  314. if (check_private_dir(s->directory, CPD_CREATE) < 0)
  315. return -1;
  316. /* Load key */
  317. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  318. strlcat(fname,"/private_key",sizeof(fname)) >= sizeof(fname)) {
  319. log_warn(LD_CONFIG, "Directory name too long: \"%s\".", s->directory);
  320. return -1;
  321. }
  322. s->private_key = init_key_from_file(fname);
  323. if (!s->private_key)
  324. return -1;
  325. /* Create service file */
  326. if (rend_get_service_id(s->private_key, s->service_id)<0) {
  327. log_warn(LD_BUG, "Internal error: couldn't encode service ID.");
  328. return -1;
  329. }
  330. if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
  331. log_warn(LD_BUG, "Bug: Couldn't compute hash of public key.");
  332. return -1;
  333. }
  334. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  335. strlcat(fname,"/hostname",sizeof(fname)) >= sizeof(fname)) {
  336. log_warn(LD_CONFIG, "Directory name too long: \"%s\".", s->directory);
  337. return -1;
  338. }
  339. tor_snprintf(buf, sizeof(buf),"%s.onion\n", s->service_id);
  340. if (write_str_to_file(fname,buf,0)<0)
  341. return -1;
  342. }
  343. return 0;
  344. }
  345. /** Return the service whose public key has a digest of <b>digest</b>. Return
  346. * NULL if no such service exists.
  347. */
  348. static rend_service_t *
  349. rend_service_get_by_pk_digest(const char* digest)
  350. {
  351. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
  352. if (!memcmp(s->pk_digest,digest,DIGEST_LEN)) return s);
  353. return NULL;
  354. }
  355. /** Return 1 if any virtual port in <b>service</b> wants a circuit
  356. * to have good uptime. Else return 0.
  357. */
  358. static int
  359. rend_service_requires_uptime(rend_service_t *service)
  360. {
  361. int i;
  362. rend_service_port_config_t *p;
  363. for (i=0; i < smartlist_len(service->ports); ++i) {
  364. p = smartlist_get(service->ports, i);
  365. if (smartlist_string_num_isin(get_options()->LongLivedPorts,
  366. p->virtual_port))
  367. return 1;
  368. }
  369. return 0;
  370. }
  371. /******
  372. * Handle cells
  373. ******/
  374. /** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
  375. * rendezvous point.
  376. */
  377. int
  378. rend_service_introduce(circuit_t *circuit, const char *request,
  379. size_t request_len)
  380. {
  381. char *ptr, *r_cookie;
  382. extend_info_t *extend_info = NULL;
  383. char buf[RELAY_PAYLOAD_SIZE];
  384. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN]; /* Holds KH, Df, Db, Kf, Kb */
  385. rend_service_t *service;
  386. int r, i;
  387. size_t len, keylen;
  388. crypto_dh_env_t *dh = NULL;
  389. circuit_t *launched = NULL;
  390. crypt_path_t *cpath = NULL;
  391. char serviceid[REND_SERVICE_ID_LEN+1];
  392. char hexcookie[9];
  393. int circ_needs_uptime;
  394. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  395. circuit->rend_pk_digest,10);
  396. log_info(LD_REND, "Received INTRODUCE2 cell for service %s on circ %d.",
  397. escaped(serviceid), circuit->n_circ_id);
  398. if (circuit->purpose != CIRCUIT_PURPOSE_S_INTRO) {
  399. log_warn(LD_PROTOCOL,
  400. "Got an INTRODUCE2 over a non-introduction circuit %d.",
  401. circuit->n_circ_id);
  402. return -1;
  403. }
  404. /* min key length plus digest length plus nickname length */
  405. if (request_len < DIGEST_LEN+REND_COOKIE_LEN+(MAX_NICKNAME_LEN+1)+
  406. DH_KEY_LEN+42) {
  407. log_warn(LD_PROTOCOL, "Got a truncated INTRODUCE2 cell on circ %d.",
  408. circuit->n_circ_id);
  409. return -1;
  410. }
  411. /* first DIGEST_LEN bytes of request is service pk digest */
  412. service = rend_service_get_by_pk_digest(request);
  413. if (!service) {
  414. log_warn(LD_REND, "Got an INTRODUCE2 cell for an unrecognized service %s.",
  415. escaped(serviceid));
  416. return -1;
  417. }
  418. if (memcmp(circuit->rend_pk_digest, request, DIGEST_LEN)) {
  419. base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request, 10);
  420. log_warn(LD_REND, "Got an INTRODUCE2 cell for the wrong service (%s).",
  421. escaped(serviceid));
  422. return -1;
  423. }
  424. keylen = crypto_pk_keysize(service->private_key);
  425. if (request_len < keylen+DIGEST_LEN) {
  426. log_warn(LD_PROTOCOL,
  427. "PK-encrypted portion of INTRODUCE2 cell was truncated.");
  428. return -1;
  429. }
  430. /* Next N bytes is encrypted with service key */
  431. r = crypto_pk_private_hybrid_decrypt(
  432. service->private_key,buf,request+DIGEST_LEN,request_len-DIGEST_LEN,
  433. PK_PKCS1_OAEP_PADDING,1);
  434. if (r<0) {
  435. log_warn(LD_PROTOCOL, "Couldn't decrypt INTRODUCE2 cell.");
  436. return -1;
  437. }
  438. len = r;
  439. if (*buf == 2) {
  440. /* Version 2 INTRODUCE2 cell. */
  441. int klen;
  442. extend_info = tor_malloc_zero(sizeof(extend_info_t));
  443. extend_info->addr = ntohl(get_uint32(buf+1));
  444. extend_info->port = ntohs(get_uint16(buf+5));
  445. memcpy(extend_info->identity_digest, buf+7, DIGEST_LEN);
  446. extend_info->nickname[0] = '$';
  447. base16_encode(extend_info->nickname+1, sizeof(extend_info->nickname)-1,
  448. extend_info->identity_digest, DIGEST_LEN);
  449. klen = ntohs(get_uint16(buf+7+DIGEST_LEN));
  450. if ((int)len != 7+DIGEST_LEN+2+klen+20+128) {
  451. log_warn(LD_PROTOCOL, "Bad length %u for version 2 INTRODUCE2 cell.",
  452. (int)len);
  453. goto err;
  454. }
  455. extend_info->onion_key = crypto_pk_asn1_decode(buf+7+DIGEST_LEN+2, klen);
  456. if (!extend_info->onion_key) {
  457. log_warn(LD_PROTOCOL,
  458. "Error decoding onion key in version 2 INTRODUCE2 cell.");
  459. goto err;
  460. }
  461. ptr = buf+7+DIGEST_LEN+2+klen;
  462. len -= 7+DIGEST_LEN+2+klen;
  463. } else {
  464. char *rp_nickname;
  465. size_t nickname_field_len;
  466. routerinfo_t *router;
  467. int version;
  468. if (*buf == 1) {
  469. rp_nickname = buf+1;
  470. nickname_field_len = MAX_HEX_NICKNAME_LEN+1;
  471. version = 1;
  472. } else {
  473. nickname_field_len = MAX_NICKNAME_LEN+1;
  474. rp_nickname = buf;
  475. version = 0;
  476. }
  477. /* XXX when 0.1.0.x is obsolete, change this to reject version < 2. */
  478. ptr=memchr(rp_nickname,0,nickname_field_len);
  479. if (!ptr || ptr == rp_nickname) {
  480. log_warn(LD_PROTOCOL,
  481. "Couldn't find a null-padded nickname in INTRODUCE2 cell.");
  482. return -1;
  483. }
  484. if ((version == 0 && !is_legal_nickname(rp_nickname)) ||
  485. (version == 1 && !is_legal_nickname_or_hexdigest(rp_nickname))) {
  486. log_warn(LD_PROTOCOL, "Bad nickname in INTRODUCE2 cell.");
  487. return -1;
  488. }
  489. /* Okay, now we know that a nickname is at the start of the buffer. */
  490. ptr = rp_nickname+nickname_field_len;
  491. len -= nickname_field_len;
  492. len -= rp_nickname - buf; /* also remove header space used by version, if
  493. * any */
  494. router = router_get_by_nickname(rp_nickname, 0);
  495. if (!router) {
  496. log_info(LD_REND, "Couldn't find router %s named in rendezvous cell.",
  497. escaped(rp_nickname));
  498. goto err;
  499. }
  500. extend_info = extend_info_from_router(router);
  501. }
  502. if (len != REND_COOKIE_LEN+DH_KEY_LEN) {
  503. log_warn(LD_PROTOCOL, "Bad length %u for INTRODUCE2 cell.", (int)len);
  504. return -1;
  505. }
  506. r_cookie = ptr;
  507. base16_encode(hexcookie,9,r_cookie,4);
  508. /* Try DH handshake... */
  509. dh = crypto_dh_new();
  510. if (!dh || crypto_dh_generate_public(dh)<0) {
  511. log_warn(LD_BUG,"Internal error: couldn't build DH state "
  512. "or generate public key.");
  513. goto err;
  514. }
  515. if (crypto_dh_compute_secret(dh, ptr+REND_COOKIE_LEN, DH_KEY_LEN, keys,
  516. DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  517. log_warn(LD_BUG, "Internal error: couldn't complete DH handshake");
  518. goto err;
  519. }
  520. circ_needs_uptime = rend_service_requires_uptime(service);
  521. /* help predict this next time */
  522. rep_hist_note_used_internal(time(NULL), circ_needs_uptime, 1);
  523. /* Launch a circuit to alice's chosen rendezvous point.
  524. */
  525. for (i=0;i<MAX_REND_FAILURES;i++) {
  526. launched = circuit_launch_by_extend_info(
  527. CIRCUIT_PURPOSE_S_CONNECT_REND, extend_info,
  528. circ_needs_uptime, 1, 1);
  529. if (launched)
  530. break;
  531. }
  532. if (!launched) { /* give up */
  533. log_warn(LD_REND, "Giving up launching first hop of circuit to rendezvous "
  534. "point '%s' for service %s.",
  535. extend_info->nickname, serviceid);
  536. goto err;
  537. }
  538. log_info(LD_REND,
  539. "Accepted intro; launching circuit to '%s' "
  540. "(cookie %s) for service %s.",
  541. extend_info->nickname, hexcookie, serviceid);
  542. tor_assert(launched->build_state);
  543. /* Fill in the circuit's state. */
  544. memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
  545. DIGEST_LEN);
  546. memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
  547. strlcpy(launched->rend_query, service->service_id,
  548. sizeof(launched->rend_query));
  549. launched->build_state->pending_final_cpath = cpath =
  550. tor_malloc_zero(sizeof(crypt_path_t));
  551. cpath->magic = CRYPT_PATH_MAGIC;
  552. launched->build_state->expiry_time = time(NULL) + MAX_REND_TIMEOUT;
  553. cpath->dh_handshake_state = dh;
  554. dh = NULL;
  555. if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
  556. goto err;
  557. memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
  558. if (extend_info) extend_info_free(extend_info);
  559. return 0;
  560. err:
  561. if (dh) crypto_dh_free(dh);
  562. if (launched) circuit_mark_for_close(launched, END_CIRC_AT_ORIGIN);
  563. if (extend_info) extend_info_free(extend_info);
  564. return -1;
  565. }
  566. /** Called when we fail building a rendezvous circuit at some point other
  567. * than the last hop: launches a new circuit to the same rendezvous point.
  568. */
  569. void
  570. rend_service_relaunch_rendezvous(circuit_t *oldcirc)
  571. {
  572. circuit_t *newcirc;
  573. cpath_build_state_t *newstate, *oldstate;
  574. tor_assert(oldcirc->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  575. if (!oldcirc->build_state ||
  576. oldcirc->build_state->failure_count > MAX_REND_FAILURES ||
  577. oldcirc->build_state->expiry_time < time(NULL)) {
  578. log_info(LD_REND,
  579. "Attempt to build circuit to %s for rendezvous has failed "
  580. "too many times or expired; giving up.",
  581. oldcirc->build_state ?
  582. oldcirc->build_state->chosen_exit->nickname : "*unknown*");
  583. return;
  584. }
  585. oldstate = oldcirc->build_state;
  586. tor_assert(oldstate);
  587. if (oldstate->pending_final_cpath == NULL) {
  588. log_info(LD_REND,"Skipping relaunch of circ that failed on its first hop. "
  589. "Initiator will retry.");
  590. return;
  591. }
  592. log_info(LD_REND,"Reattempting rendezvous circuit to '%s'",
  593. oldstate->chosen_exit->nickname);
  594. newcirc = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND,
  595. oldstate->chosen_exit, 0, 1, 1);
  596. if (!newcirc) {
  597. log_warn(LD_REND,"Couldn't relaunch rendezvous circuit to '%s'.",
  598. oldstate->chosen_exit->nickname);
  599. return;
  600. }
  601. newstate = newcirc->build_state;
  602. tor_assert(newstate);
  603. newstate->failure_count = oldstate->failure_count+1;
  604. newstate->expiry_time = oldstate->expiry_time;
  605. newstate->pending_final_cpath = oldstate->pending_final_cpath;
  606. oldstate->pending_final_cpath = NULL;
  607. memcpy(newcirc->rend_query, oldcirc->rend_query, REND_SERVICE_ID_LEN+1);
  608. memcpy(newcirc->rend_pk_digest, oldcirc->rend_pk_digest, DIGEST_LEN);
  609. memcpy(newcirc->rend_cookie, oldcirc->rend_cookie, REND_COOKIE_LEN);
  610. }
  611. /** Launch a circuit to serve as an introduction point for the service
  612. * <b>service</b> at the introduction point <b>nickname</b>
  613. */
  614. static int
  615. rend_service_launch_establish_intro(rend_service_t *service,
  616. const char *nickname)
  617. {
  618. circuit_t *launched;
  619. log_info(LD_REND,
  620. "Launching circuit to introduction point %s for service %s",
  621. nickname, service->service_id);
  622. rep_hist_note_used_internal(time(NULL), 1, 0);
  623. ++service->n_intro_circuits_launched;
  624. launched = circuit_launch_by_nickname(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  625. nickname, 1, 0, 1);
  626. if (!launched) {
  627. log_info(LD_REND,
  628. "Can't launch circuit to establish introduction at '%s'.",
  629. nickname);
  630. return -1;
  631. }
  632. strlcpy(launched->rend_query, service->service_id,
  633. sizeof(launched->rend_query));
  634. memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
  635. if (launched->state == CIRCUIT_STATE_OPEN)
  636. rend_service_intro_has_opened(launched);
  637. return 0;
  638. }
  639. /** Called when we're done building a circuit to an introduction point:
  640. * sends a RELAY_ESTABLISH_INTRO cell.
  641. */
  642. void
  643. rend_service_intro_has_opened(circuit_t *circuit)
  644. {
  645. rend_service_t *service;
  646. size_t len;
  647. int r;
  648. char buf[RELAY_PAYLOAD_SIZE];
  649. char auth[DIGEST_LEN + 9];
  650. char serviceid[REND_SERVICE_ID_LEN+1];
  651. tor_assert(circuit->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  652. tor_assert(CIRCUIT_IS_ORIGIN(circuit));
  653. tor_assert(circuit->cpath);
  654. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  655. circuit->rend_pk_digest,10);
  656. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  657. if (!service) {
  658. log_warn(LD_REND, "Unrecognized service ID %s on introduction circuit %d.",
  659. serviceid, circuit->n_circ_id);
  660. goto err;
  661. }
  662. log_info(LD_REND,
  663. "Established circuit %d as introduction point for service %s",
  664. circuit->n_circ_id, serviceid);
  665. /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
  666. len = crypto_pk_asn1_encode(service->private_key, buf+2,
  667. RELAY_PAYLOAD_SIZE-2);
  668. set_uint16(buf, htons((uint16_t)len));
  669. len += 2;
  670. memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
  671. memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
  672. if (crypto_digest(buf+len, auth, DIGEST_LEN+9))
  673. goto err;
  674. len += 20;
  675. r = crypto_pk_private_sign_digest(service->private_key, buf+len, buf, len);
  676. if (r<0) {
  677. log_warn(LD_BUG, "Internal error: couldn't sign introduction request.");
  678. goto err;
  679. }
  680. len += r;
  681. if (connection_edge_send_command(NULL, circuit,RELAY_COMMAND_ESTABLISH_INTRO,
  682. buf, len, circuit->cpath->prev)<0) {
  683. log_info(LD_GENERAL,
  684. "Couldn't send introduction request for service %s on circuit %d",
  685. serviceid, circuit->n_circ_id);
  686. goto err;
  687. }
  688. return;
  689. err:
  690. circuit_mark_for_close(circuit, END_CIRC_AT_ORIGIN);
  691. }
  692. /** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
  693. * live introduction point, and note that the service descriptor is
  694. * now out-of-date.*/
  695. int
  696. rend_service_intro_established(circuit_t *circuit, const char *request,
  697. size_t request_len)
  698. {
  699. rend_service_t *service;
  700. if (circuit->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  701. log_warn(LD_PROTOCOL,
  702. "received INTRO_ESTABLISHED cell on non-intro circuit.");
  703. goto err;
  704. }
  705. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  706. if (!service) {
  707. log_warn(LD_REND, "Unknown service on introduction circuit %d.",
  708. circuit->n_circ_id);
  709. goto err;
  710. }
  711. service->desc_is_dirty = time(NULL);
  712. circuit->purpose = CIRCUIT_PURPOSE_S_INTRO;
  713. return 0;
  714. err:
  715. circuit_mark_for_close(circuit, END_CIRC_AT_ORIGIN);
  716. return -1;
  717. }
  718. /** Called once a circuit to a rendezvous point is established: sends a
  719. * RELAY_COMMAND_RENDEZVOUS1 cell.
  720. */
  721. void
  722. rend_service_rendezvous_has_opened(circuit_t *circuit)
  723. {
  724. rend_service_t *service;
  725. char buf[RELAY_PAYLOAD_SIZE];
  726. crypt_path_t *hop;
  727. char serviceid[REND_SERVICE_ID_LEN+1];
  728. char hexcookie[9];
  729. tor_assert(circuit->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  730. tor_assert(circuit->cpath);
  731. tor_assert(circuit->build_state);
  732. hop = circuit->build_state->pending_final_cpath;
  733. tor_assert(hop);
  734. base16_encode(hexcookie,9,circuit->rend_cookie,4);
  735. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  736. circuit->rend_pk_digest,10);
  737. log_info(LD_REND,
  738. "Done building circuit %d to rendezvous with "
  739. "cookie %s for service %s",
  740. circuit->n_circ_id, hexcookie, serviceid);
  741. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  742. if (!service) {
  743. log_warn(LD_GENERAL, "Internal error: unrecognized service ID on "
  744. "introduction circuit.");
  745. goto err;
  746. }
  747. /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
  748. memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
  749. if (crypto_dh_get_public(hop->dh_handshake_state,
  750. buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
  751. log_warn(LD_GENERAL,"Couldn't get DH public key.");
  752. goto err;
  753. }
  754. memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
  755. DIGEST_LEN);
  756. /* Send the cell */
  757. if (connection_edge_send_command(NULL, circuit, RELAY_COMMAND_RENDEZVOUS1,
  758. buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
  759. circuit->cpath->prev)<0) {
  760. log_warn(LD_GENERAL, "Couldn't send RENDEZVOUS1 cell.");
  761. goto err;
  762. }
  763. crypto_dh_free(hop->dh_handshake_state);
  764. hop->dh_handshake_state = NULL;
  765. /* Append the cpath entry. */
  766. hop->state = CPATH_STATE_OPEN;
  767. /* set the windows to default. these are the windows
  768. * that bob thinks alice has.
  769. */
  770. hop->package_window = CIRCWINDOW_START;
  771. hop->deliver_window = CIRCWINDOW_START;
  772. onion_append_to_cpath(&circuit->cpath, hop);
  773. circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
  774. /* Change the circuit purpose. */
  775. circuit->purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
  776. return;
  777. err:
  778. circuit_mark_for_close(circuit, END_CIRC_AT_ORIGIN);
  779. }
  780. /*
  781. * Manage introduction points
  782. */
  783. /** Return the (possibly non-open) introduction circuit ending at
  784. * <b>router</b> for the service whose public key is <b>pk_digest</b>. Return
  785. * NULL if no such service is found.
  786. */
  787. static circuit_t *
  788. find_intro_circuit(routerinfo_t *router, const char *pk_digest)
  789. {
  790. circuit_t *circ = NULL;
  791. tor_assert(router);
  792. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  793. CIRCUIT_PURPOSE_S_INTRO))) {
  794. tor_assert(circ->cpath);
  795. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  796. router->nickname)) {
  797. return circ;
  798. }
  799. }
  800. circ = NULL;
  801. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  802. CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
  803. tor_assert(circ->cpath);
  804. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  805. router->nickname)) {
  806. return circ;
  807. }
  808. }
  809. return NULL;
  810. }
  811. /** Encode and sign an up-to-date service descriptor for <b>service</b>,
  812. * and upload it to all the dirservers.
  813. */
  814. static void
  815. upload_service_descriptor(rend_service_t *service, int version)
  816. {
  817. char *desc;
  818. size_t desc_len;
  819. /* Update the descriptor. */
  820. rend_service_update_descriptor(service);
  821. if (rend_encode_service_descriptor(service->desc,
  822. version,
  823. service->private_key,
  824. &desc, &desc_len)<0) {
  825. log_warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
  826. "not uploading.");
  827. return;
  828. }
  829. /* Post it to the dirservers */
  830. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC, desc, desc_len);
  831. tor_free(desc);
  832. service->desc_is_dirty = 0;
  833. }
  834. /* XXXX Make this longer once directories remember service descriptors across
  835. * restarts.*/
  836. #define MAX_SERVICE_PUBLICATION_INTERVAL (15*60)
  837. /** For every service, check how many intro points it currently has, and:
  838. * - Pick new intro points as necessary.
  839. * - Launch circuits to any new intro points.
  840. */
  841. void
  842. rend_services_introduce(void)
  843. {
  844. int i,j,r;
  845. routerinfo_t *router;
  846. rend_service_t *service;
  847. char *intro;
  848. int changed, prev_intro_nodes;
  849. smartlist_t *intro_routers, *exclude_routers;
  850. time_t now;
  851. intro_routers = smartlist_create();
  852. exclude_routers = smartlist_create();
  853. now = time(NULL);
  854. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  855. smartlist_clear(intro_routers);
  856. service = smartlist_get(rend_service_list, i);
  857. tor_assert(service);
  858. changed = 0;
  859. if (now > service->intro_period_started+INTRO_CIRC_RETRY_PERIOD) {
  860. /* One period has elapsed; we can try building circuits again. */
  861. service->intro_period_started = now;
  862. service->n_intro_circuits_launched = 0;
  863. } else if (service->n_intro_circuits_launched >=
  864. MAX_INTRO_CIRCS_PER_PERIOD) {
  865. /* We have failed too many times in this period; wait for the next
  866. * one before we try again. */
  867. continue;
  868. }
  869. /* Find out which introduction points we have in progress for this
  870. service. */
  871. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  872. intro = smartlist_get(service->intro_nodes, j);
  873. router = router_get_by_nickname(intro, 0);
  874. if (!router || !find_intro_circuit(router,service->pk_digest)) {
  875. log_info(LD_REND,"Giving up on %s as intro point for %s.",
  876. intro, service->service_id);
  877. tor_free(intro);
  878. smartlist_del(service->intro_nodes,j--);
  879. changed = 1;
  880. service->desc_is_dirty = now;
  881. }
  882. smartlist_add(intro_routers, router);
  883. }
  884. /* We have enough intro points, and the intro points we thought we had were
  885. * all connected.
  886. */
  887. if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS) {
  888. /* We have all our intro points! Start a fresh period and reset the
  889. * circuit count. */
  890. service->intro_period_started = now;
  891. service->n_intro_circuits_launched = 0;
  892. continue;
  893. }
  894. /* Remember how many introduction circuits we started with. */
  895. prev_intro_nodes = smartlist_len(service->intro_nodes);
  896. smartlist_add_all(exclude_routers, intro_routers);
  897. /* The directory is now here. Pick three ORs as intro points. */
  898. for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
  899. char *hex_digest;
  900. router = router_choose_random_node(service->intro_prefer_nodes,
  901. service->intro_exclude_nodes, exclude_routers, 1, 0, 0,
  902. get_options()->_AllowUnverified & ALLOW_UNVERIFIED_INTRODUCTION,
  903. 0);
  904. if (!router) {
  905. log_warn(LD_REND,
  906. "Could only establish %d introduction points for %s.",
  907. smartlist_len(service->intro_nodes), service->service_id);
  908. break;
  909. }
  910. changed = 1;
  911. hex_digest = tor_malloc_zero(HEX_DIGEST_LEN+2);
  912. hex_digest[0] = '$';
  913. base16_encode(hex_digest+1, HEX_DIGEST_LEN+1,
  914. router->cache_info.identity_digest,
  915. DIGEST_LEN);
  916. smartlist_add(intro_routers, router);
  917. smartlist_add(exclude_routers, router);
  918. smartlist_add(service->intro_nodes, hex_digest);
  919. log_info(LD_REND, "Picked router %s as an intro point for %s.",
  920. router->nickname, service->service_id);
  921. }
  922. /* Reset exclude_routers, for the next time around the loop. */
  923. smartlist_clear(exclude_routers);
  924. /* If there's no need to launch new circuits, stop here. */
  925. if (!changed)
  926. continue;
  927. /* Establish new introduction points. */
  928. for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
  929. intro = smartlist_get(service->intro_nodes, j);
  930. r = rend_service_launch_establish_intro(service, intro);
  931. if (r<0) {
  932. log_warn(LD_REND, "Error launching circuit to node %s for service %s.",
  933. intro, service->service_id);
  934. }
  935. }
  936. }
  937. smartlist_free(intro_routers);
  938. smartlist_free(exclude_routers);
  939. }
  940. /** Regenerate and upload rendezvous service descriptors for all
  941. * services, if necessary. If the descriptor has been dirty enough
  942. * for long enough, definitely upload; else only upload when the
  943. * periodic timeout has expired.
  944. *
  945. * For the first upload, pick a random time between now and two periods
  946. * from now, and pick it independently for each service.
  947. */
  948. void
  949. rend_consider_services_upload(time_t now)
  950. {
  951. int i;
  952. rend_service_t *service;
  953. int rendpostperiod = get_options()->RendPostPeriod;
  954. if (!get_options()->PublishHidServDescriptors)
  955. return;
  956. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  957. service = smartlist_get(rend_service_list, i);
  958. if (!service->next_upload_time) { /* never been uploaded yet */
  959. service->next_upload_time =
  960. now + crypto_rand_int(2*rendpostperiod);
  961. }
  962. if (service->next_upload_time < now ||
  963. (service->desc_is_dirty &&
  964. service->desc_is_dirty < now-5)) {
  965. /* if it's time, or if the directory servers have a wrong service
  966. * descriptor and ours has been stable for 5 seconds, upload a
  967. * new one of each format. */
  968. upload_service_descriptor(service, 0);
  969. // XXXX011 upload_service_descriptor(service, 1);
  970. service->next_upload_time = now + rendpostperiod;
  971. }
  972. }
  973. }
  974. /** Log the status of introduction points for all rendezvous services
  975. * at log severity <b>severity</b>.
  976. */
  977. void
  978. rend_service_dump_stats(int severity)
  979. {
  980. int i,j;
  981. routerinfo_t *router;
  982. rend_service_t *service;
  983. char *nickname;
  984. circuit_t *circ;
  985. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  986. service = smartlist_get(rend_service_list, i);
  987. log(severity, LD_GENERAL, "Service configured in \"%s\":",
  988. service->directory);
  989. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  990. nickname = smartlist_get(service->intro_nodes, j);
  991. router = router_get_by_nickname(smartlist_get(service->intro_nodes,j),1);
  992. if (!router) {
  993. log(severity, LD_GENERAL, " Intro point at %s: unrecognized router",
  994. nickname);
  995. continue;
  996. }
  997. circ = find_intro_circuit(router, service->pk_digest);
  998. if (!circ) {
  999. log(severity, LD_GENERAL, " Intro point at %s: no circuit",nickname);
  1000. continue;
  1001. }
  1002. log(severity, LD_GENERAL, " Intro point at %s: circuit is %s",nickname,
  1003. circuit_state_to_string(circ->state));
  1004. }
  1005. }
  1006. }
  1007. /** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
  1008. * 'circ', and look up the port and address based on conn-\>port.
  1009. * Assign the actual conn-\>addr and conn-\>port. Return -1 if failure,
  1010. * or 0 for success.
  1011. */
  1012. int
  1013. rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ)
  1014. {
  1015. rend_service_t *service;
  1016. int i;
  1017. rend_service_port_config_t *p;
  1018. char serviceid[REND_SERVICE_ID_LEN+1];
  1019. tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  1020. log_debug(LD_REND,"beginning to hunt for addr/port");
  1021. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  1022. circ->rend_pk_digest,10);
  1023. service = rend_service_get_by_pk_digest(circ->rend_pk_digest);
  1024. if (!service) {
  1025. log_warn(LD_REND, "Couldn't find any service associated with pk %s on "
  1026. "rendezvous circuit %d; closing.",
  1027. serviceid, circ->n_circ_id);
  1028. return -1;
  1029. }
  1030. for (i = 0; i < smartlist_len(service->ports); ++i) {
  1031. p = smartlist_get(service->ports, i);
  1032. if (conn->port == p->virtual_port) {
  1033. conn->addr = p->real_addr;
  1034. conn->port = p->real_port;
  1035. return 0;
  1036. }
  1037. }
  1038. log_info(LD_REND, "No virtual port mapping exists for port %d on service %s",
  1039. conn->port,serviceid);
  1040. return -1;
  1041. }