rendservice.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* Copyright 2004 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /* This module implements the hidden-service side of rendezvous functionality.
  5. */
  6. #include "or.h"
  7. /* Represents the mapping from a virtual port of a rendezvous service to
  8. * a real port on some IP.
  9. */
  10. typedef struct rend_service_port_config_t {
  11. uint16_t virtual_port;
  12. uint16_t real_port;
  13. uint32_t real_address;
  14. } rend_service_port_config_t;
  15. /* Represents a single hidden service running at this OP.
  16. */
  17. typedef struct rend_service_t {
  18. /* Fields specified in config file */
  19. char *directory; /* where in the filesystem it stores it */
  20. smartlist_t *ports;
  21. char *intro_nodes;
  22. char *intro_exclude_nodes;
  23. /* Other fields */
  24. crypto_pk_env_t *private_key;
  25. char service_id[REND_SERVICE_ID_LEN+1];
  26. char pk_digest[20];
  27. } rend_service_t;
  28. /* A list of rend_service_t.
  29. */
  30. static smartlist_t *rend_service_list = NULL;
  31. static void rend_service_free(rend_service_t *config)
  32. {
  33. int i;
  34. if (!config) return;
  35. tor_free(config->directory);
  36. for (i=0; i<config->ports->num_used; ++i) {
  37. tor_free(config->ports->list[i]);
  38. }
  39. smartlist_free(config->ports);
  40. if (config->private_key)
  41. crypto_free_pk_env(config->private_key);
  42. }
  43. static void rend_service_free_all(void)
  44. {
  45. int i;
  46. if (!rend_service_list) {
  47. rend_service_list = smartlist_create();
  48. return;
  49. }
  50. for (i=0; i < rend_service_list->num_used; ++i) {
  51. rend_service_free(rend_service_list->list[i]);
  52. }
  53. smartlist_free(rend_service_list);
  54. rend_service_list = smartlist_create();
  55. }
  56. static void add_service(rend_service_t *service)
  57. {
  58. int i;
  59. rend_service_port_config_t *p;
  60. struct in_addr addr;
  61. if (!service->ports->num_used) {
  62. log_fn(LOG_WARN, "Hidden service with no ports configured; ignoring.");
  63. rend_service_free(service);
  64. } else {
  65. smartlist_set_capacity(service->ports, service->ports->num_used);
  66. smartlist_add(rend_service_list, service);
  67. log_fn(LOG_INFO,"Configuring service with directory %s",service->directory);
  68. for (i = 0; i < service->ports->num_used; ++i) {
  69. p = (rend_service_port_config_t *) service->ports->list[i];
  70. addr.s_addr = htonl(p->real_address);
  71. log_fn(LOG_INFO,"Service maps port %d to %s:%d",
  72. p->virtual_port, inet_ntoa(addr), p->real_port);
  73. }
  74. }
  75. }
  76. /* Format: VirtualPort (IP|RealPort|IP:RealPort)?
  77. * IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
  78. */
  79. static rend_service_port_config_t *parse_port_config(const char *string)
  80. {
  81. int virtport, realport, r;
  82. struct in_addr addr;
  83. char *endptr, *colon, *addrstring;
  84. rend_service_port_config_t *result;
  85. virtport = (int) strtol(string, &endptr, 10);
  86. if (endptr == string) {
  87. log_fn(LOG_WARN, "Missing port in hidden service port configuration");
  88. return NULL;
  89. }
  90. if (virtport < 1 || virtport > 65535) {
  91. log_fn(LOG_WARN, "Port out of range in hidden service port configuration");
  92. return NULL;
  93. }
  94. string = endptr + strspn(endptr, " \t");
  95. if (!*string) {
  96. /* No addr:port part; use default. */
  97. realport = virtport;
  98. addr.s_addr = htonl(0x7F000001u);
  99. } else {
  100. colon = strchr(string, ':');
  101. if (colon) {
  102. /* Try to parse addr:port. */
  103. addrstring = tor_strndup(string, colon-string);
  104. r = tor_inet_aton(addrstring, &addr);
  105. tor_free(addrstring);
  106. if (!r) {
  107. log_fn(LOG_WARN,"Unparseable address in hidden service port configuration");
  108. return NULL;
  109. }
  110. realport = strtol(colon+1, &endptr, 10);
  111. if (*endptr) {
  112. log_fn(LOG_WARN,"Unparseable or missing port in hidden service port configuration.");
  113. return NULL;
  114. }
  115. } else if (strchr(string, '.') && tor_inet_aton(string, &addr)) {
  116. /* We have addr; use deafult port. */
  117. realport = virtport;
  118. } else {
  119. /* No addr:port, no addr -- must be port. */
  120. realport = strtol(string, &endptr, 10);
  121. if (*endptr) {
  122. log_fn(LOG_WARN, "Unparseable of missing port in hidden service port configuration.");
  123. return NULL;
  124. }
  125. addr.s_addr = htonl(0x7F000001u); /* Default to 127.0.0.1 */
  126. }
  127. }
  128. if (realport < 1 || realport > 65535) {
  129. log_fn(LOG_WARN, "Port out of range in hidden service port configuration.");
  130. return NULL;
  131. }
  132. result = tor_malloc(sizeof(rend_service_port_config_t));
  133. result->virtual_port = virtport;
  134. result->real_port = realport;
  135. result->real_address = (uint32_t) ntohl(addr.s_addr);
  136. return result;
  137. }
  138. /* Set up rend_service_list, based on the values of HiddenServiceDir and
  139. * HiddenServicePort in 'options'. Return 0 on success and -1 on
  140. * failure.
  141. */
  142. int rend_config_services(or_options_t *options)
  143. {
  144. struct config_line_t *line;
  145. rend_service_t *service = NULL;
  146. rend_service_port_config_t *portcfg;
  147. rend_service_free_all();
  148. for (line = options->RendConfigLines; line; line = line->next) {
  149. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  150. if (service)
  151. add_service(service);
  152. service = tor_malloc_zero(sizeof(rend_service_t));
  153. service->directory = tor_strdup(line->value);
  154. service->ports = smartlist_create();
  155. continue;
  156. }
  157. if (!service) {
  158. log_fn(LOG_WARN, "HiddenServicePort with no preceeding HiddenServiceDir directive");
  159. rend_service_free(service);
  160. return -1;
  161. }
  162. if (!strcasecmp(line->key, "HiddenServicePort")) {
  163. portcfg = parse_port_config(line->value);
  164. if (!portcfg) {
  165. rend_service_free(service);
  166. return -1;
  167. }
  168. smartlist_add(service->ports, portcfg);
  169. } else if (!strcasecmp(line->key, "HiddenServiceNodes")) {
  170. if (service->intro_nodes) {
  171. log_fn(LOG_WARN, "Got multiple HiddenServiceNodes lines for a single service");
  172. return -1;
  173. }
  174. service->intro_nodes = tor_strdup(line->value);
  175. } else {
  176. assert(!strcasecmp(line->key, "HiddenServiceExcludeNodes"));
  177. if (service->intro_exclude_nodes) {
  178. log_fn(LOG_WARN, "Got multiple HiddenServiceExcludedNodes lines for a single service");
  179. return -1;
  180. }
  181. service->intro_exclude_nodes = tor_strdup(line->value);
  182. }
  183. }
  184. if (service)
  185. add_service(service);
  186. return 0;
  187. }
  188. /* Load and/or generate private keys for all hidden services. Return 0 on
  189. * success, -1 on failure.
  190. */
  191. int rend_service_init_keys(void)
  192. {
  193. int i;
  194. rend_service_t *s;
  195. char fname[512];
  196. char buf[128];
  197. for (i=0; i < rend_service_list->num_used; ++i) {
  198. s = (rend_service_t*) rend_service_list->list[i];
  199. if (s->private_key)
  200. continue;
  201. /* Check/create directory */
  202. if (check_private_dir(s->directory, 1) < 0)
  203. return -1;
  204. /* Load key */
  205. if (strlcpy(fname,s->directory,512) >= 512 ||
  206. strlcat(fname,"/private_key",512) >= 512) {
  207. log_fn(LOG_WARN, "Directory name too long: '%s'", s->directory);
  208. return -1;
  209. }
  210. s->private_key = init_key_from_file(fname);
  211. if (!s->private_key)
  212. return -1;
  213. /* Create service file */
  214. if (rend_get_service_id(s->private_key, s->service_id)<0) {
  215. log_fn(LOG_WARN, "Couldn't encode service ID");
  216. return -1;
  217. }
  218. if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
  219. log_fn(LOG_WARN, "Couldn't compute hash of public key");
  220. return -1;
  221. }
  222. if (strlcpy(fname,s->directory,512) >= 512 ||
  223. strlcat(fname,"/hostname",512) >= 512) {
  224. log_fn(LOG_WARN, "Directory name too long: '%s'", s->directory);
  225. return -1;
  226. }
  227. sprintf(buf, "%s.onion\n", s->service_id);
  228. if (write_str_to_file(fname,buf)<0)
  229. return -1;
  230. }
  231. return 0;
  232. }
  233. static rend_service_t *
  234. rend_service_get_by_pk_digest(const char* digest)
  235. {
  236. int i;
  237. rend_service_t *s;
  238. for (i = 0; i < rend_service_list->num_used; ++i) {
  239. s = (rend_service_t*)rend_service_list->list[i];
  240. if (!memcmp(s->pk_digest, digest, 20))
  241. return s;
  242. }
  243. return NULL;
  244. }
  245. /******
  246. * Handle cells
  247. ******/
  248. /* Respond to an INTRODUCE2 cell by launching a circuit to the chosen
  249. * rendezvous points.
  250. */
  251. int
  252. rend_service_introduce(circuit_t *circuit, char *request, int request_len)
  253. {
  254. char *ptr, *rp_nickname, *r_cookie;
  255. char buf[RELAY_PAYLOAD_SIZE];
  256. char keys[20+CPATH_KEY_MATERIAL_LEN]; /* Holds KH, Df, Db, Kf, Kb */
  257. rend_service_t *service;
  258. int len, keylen;
  259. crypto_dh_env_t *dh = NULL;
  260. circuit_t *launched = NULL;
  261. crypt_path_t *cpath = NULL;
  262. if (circuit->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  263. log_fn(LOG_WARN, "Got an INTRODUCE2 over a non-introduction circuit.");
  264. return -1;
  265. }
  266. /* min key length plus digest length */
  267. if (request_len < 148) {
  268. log_fn(LOG_WARN, "Got a truncated INTRODUCE2 cell.");
  269. return -1;
  270. }
  271. /* first 20 bytes of request is service pk digest */
  272. service = rend_service_get_by_pk_digest(request);
  273. if (!service) {
  274. log_fn(LOG_WARN, "Got an INTRODUCE2 cell for an unrecognized service");
  275. return -1;
  276. }
  277. if (!memcmp(circuit->rend_service, request, 20)) {
  278. log_fn(LOG_WARN, "Got an INTRODUCE2 cell for the wrong service");
  279. return -1;
  280. }
  281. keylen = crypto_pk_keysize(service->private_key);
  282. if (request_len < keylen+20) {
  283. log_fn(LOG_WARN, "PK-encrypted portion of INTRODUCE2 cell was truncated");
  284. return -1;
  285. }
  286. /* Next N bytes is encrypted with service key */
  287. len = crypto_pk_private_hybrid_decrypt(
  288. service->private_key,request,request_len-20,buf, RSA_PKCS1_PADDING);
  289. if (len<0) {
  290. log_fn(LOG_WARN, "Couldn't decrypt INTRODUCE2 cell");
  291. return -1;
  292. }
  293. ptr=memchr(buf,0,len);
  294. if (!ptr || ptr == buf) {
  295. log_fn(LOG_WARN, "Couldn't find a null-terminated nickname in INTRODUCE2 cell");
  296. return -1;
  297. }
  298. if (strspn(buf,LEGAL_NICKNAME_CHARACTERS) != ptr-buf) {
  299. log_fn(LOG_WARN, "Nickname in INTRODUCE2 cell contains illegal character.");
  300. return -1;
  301. }
  302. /* Okay, now we know that the nickname is at the start of the buffer. */
  303. rp_nickname = buf;
  304. ++ptr;
  305. len -= (ptr-buf);
  306. if (len != 20+128) {
  307. log_fn(LOG_WARN, "Bad length for INTRODUCE2 cell.");
  308. return -1;
  309. }
  310. r_cookie = ptr;
  311. /* Try DH handshake... */
  312. dh = crypto_dh_new();
  313. if (!dh || crypto_dh_generate_public(dh)<0) {
  314. log_fn(LOG_WARN, "Couldn't build DH state or generate public key");
  315. goto err;
  316. }
  317. if (crypto_dh_compute_secret(dh, ptr+20, DH_KEY_LEN, keys,
  318. 20+CPATH_KEY_MATERIAL_LEN)<0) {
  319. log_fn(LOG_WARN, "Couldn't complete DH handshake");
  320. goto err;
  321. }
  322. /* Launch a circuit to alice's chosen rendezvous point.
  323. */
  324. launched = circuit_launch_new(CIRCUIT_PURPOSE_S_CONNECT_REND, rp_nickname);
  325. if (!launched) {
  326. log_fn(LOG_WARN, "Can't launch circuit to rendezvous point '%s'",
  327. rp_nickname);
  328. return -1;
  329. }
  330. assert(launched->build_state);
  331. /* Fill in the circuit's state. */
  332. memcpy(launched->rend_service, circuit->rend_service,CRYPTO_SHA1_DIGEST_LEN);
  333. memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
  334. launched->build_state->pending_final_cpath = cpath =
  335. tor_malloc_zero(sizeof(crypt_path_t));
  336. cpath->handshake_state = dh;
  337. dh = NULL;
  338. if (circuit_init_cpath_crypto(cpath,keys+20)<0)
  339. goto err;
  340. memcpy(cpath->handshake_digest, keys, 20);
  341. return 0;
  342. err:
  343. if (dh) crypto_dh_free(dh);
  344. if (launched) circuit_mark_for_close(launched);
  345. return -1;
  346. }
  347. /* Launch a circuit to serve as an introduction point.
  348. */
  349. static int
  350. rend_service_launch_establish_intro(rend_service_t *service, char *nickname)
  351. {
  352. circuit_t *launched;
  353. assert(service && nickname);
  354. launched = circuit_launch_new(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, nickname);
  355. if (!launched) {
  356. log_fn(LOG_WARN, "Can't launch circuit to establish introduction at '%s'",
  357. nickname);
  358. return -1;
  359. }
  360. memcpy(launched->rend_service, service->pk_digest, CRYPTO_SHA1_DIGEST_LEN);
  361. return 0;
  362. }
  363. /* Called when we're done building a circuit to an introduction point:
  364. * sends a RELAY_ESTABLISH_INTRO cell.
  365. */
  366. void
  367. rend_service_intro_is_ready(circuit_t *circuit)
  368. {
  369. rend_service_t *service;
  370. int len, r;
  371. char buf[RELAY_PAYLOAD_SIZE];
  372. char auth[CRYPTO_SHA1_DIGEST_LEN + 10];
  373. assert(circuit->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  374. assert(circuit->cpath);
  375. service = rend_service_get_by_pk_digest(circuit->rend_service);
  376. if (!service) {
  377. log_fn(LOG_WARN, "Internal error: unrecognized service ID on introduction circuit");
  378. goto err;
  379. }
  380. /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
  381. len = crypto_pk_asn1_encode(service->private_key, buf+2,
  382. RELAY_PAYLOAD_SIZE-2);
  383. set_uint16(buf, len);
  384. len += 2;
  385. memcpy(auth, circuit->cpath->prev->handshake_digest, CRYPTO_SHA1_DIGEST_LEN);
  386. memcpy(auth+CRYPTO_SHA1_DIGEST_LEN, "INTRODUCE", 9);
  387. if (crypto_SHA_digest(auth, CRYPTO_SHA1_DIGEST_LEN+9, buf+len))
  388. goto err;
  389. len += 20;
  390. r = crypto_pk_private_sign_digest(service->private_key, buf, len, buf+len);
  391. if (r<0) {
  392. log_fn(LOG_WARN, "Couldn't sign introduction request");
  393. goto err;
  394. }
  395. len += r;
  396. if (connection_edge_send_command(NULL, circuit,RELAY_COMMAND_ESTABLISH_INTRO,
  397. buf, len, circuit->cpath->prev)<0) {
  398. log_fn(LOG_WARN, "Couldn't send introduction request");
  399. goto err;
  400. }
  401. return;
  402. err:
  403. circuit_mark_for_close(circuit);
  404. }
  405. /* Called once a circuit to a rendezvous point is ready: sends a
  406. * RELAY_COMMAND_RENDEZVOUS1 cell.
  407. */
  408. void
  409. rend_service_rendezvous_is_ready(circuit_t *circuit)
  410. {
  411. rend_service_t *service;
  412. char buf[RELAY_PAYLOAD_SIZE];
  413. crypt_path_t *hop;
  414. assert(circuit->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  415. assert(circuit->cpath);
  416. assert(circuit->build_state);
  417. hop = circuit->build_state->pending_final_cpath;
  418. assert(hop);
  419. service = rend_service_get_by_pk_digest(circuit->rend_service);
  420. if (!service) {
  421. log_fn(LOG_WARN, "Internal error: unrecognized service ID on introduction circuit");
  422. goto err;
  423. }
  424. /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
  425. memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
  426. if (crypto_dh_get_public(hop->handshake_state,
  427. buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
  428. log_fn(LOG_WARN,"Couldn't get DH public key");
  429. goto err;
  430. }
  431. memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
  432. CRYPTO_SHA1_DIGEST_LEN);
  433. /* Send the cell */
  434. if (connection_edge_send_command(NULL, circuit, RELAY_COMMAND_RENDEZVOUS1,
  435. buf, REND_COOKIE_LEN+DH_KEY_LEN+1,
  436. circuit->cpath->prev)<0) {
  437. log_fn(LOG_WARN, "Couldn't send RENDEZVOUS1 cell");
  438. goto err;
  439. }
  440. /* Append the cpath entry. */
  441. onion_append_to_cpath(&circuit->cpath, hop);
  442. circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
  443. /* Change the circuit purpose. */
  444. circuit->purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
  445. return;
  446. err:
  447. circuit_mark_for_close(circuit);
  448. }
  449. /******
  450. * Manage introduction points
  451. ******/
  452. #define NUM_INTRO_POINTS 3
  453. int rend_services_init(void) {
  454. int i,j,r;
  455. routerinfo_t *router;
  456. routerlist_t *rl;
  457. rend_service_t *service;
  458. router_get_routerlist(&rl);
  459. for (i=0;i<rend_service_list->num_used;++i) {
  460. service = rend_service_list->list[i];
  461. /* The directory is now here. Pick three ORs as intro points. */
  462. for (j=0;j<rl->n_routers;j++) {
  463. router = rl->routers[j];
  464. //...
  465. // maybe built a smartlist of all of them, then pick at random
  466. // until you have three? or something smarter.
  467. }
  468. /* build a service descriptor out of them, and tuck it away
  469. * somewhere so we don't lose it */
  470. /* post it to the dirservers */
  471. //call router_post_to_dirservers(DIR_PURPOSE_UPLOAD_HIDSERV, desc, desc_len);
  472. // for each intro point,
  473. {
  474. //r = rend_service_launch_establish_intro(service, intro->nickname);
  475. //if (r<0) freak out
  476. }
  477. // anything else?
  478. }
  479. }
  480. /*
  481. Local Variables:
  482. mode:c
  483. indent-tabs-mode:nil
  484. c-basic-offset:2
  485. End:
  486. */