rendservice.c 30 KB

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