rendservice.c 30 KB

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