rendservice.c 31 KB

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