rendservice.c 32 KB

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