12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142 |
- const char rendservice_c_id[] =
- "$Id$";
- #include "or.h"
- static circuit_t *find_intro_circuit(routerinfo_t *router,
- const char *pk_digest);
- typedef struct rend_service_port_config_t {
- uint16_t virtual_port;
- uint16_t real_port;
- uint32_t real_addr;
- } rend_service_port_config_t;
- #define NUM_INTRO_POINTS 3
- #define INTRO_CIRC_RETRY_PERIOD 60*5
- #define MAX_INTRO_CIRCS_PER_PERIOD 10
- #define MAX_REND_FAILURES 30
- #define MAX_REND_TIMEOUT 30
- typedef struct rend_service_t {
-
- char *directory;
- smartlist_t *ports;
- char *intro_prefer_nodes;
- char *intro_exclude_nodes;
-
- crypto_pk_env_t *private_key;
- char service_id[REND_SERVICE_ID_LEN+1];
- char pk_digest[DIGEST_LEN];
- smartlist_t *intro_nodes;
- time_t intro_period_started;
- int n_intro_circuits_launched;
- rend_service_descriptor_t *desc;
- time_t desc_is_dirty;
- time_t next_upload_time;
- } rend_service_t;
- static smartlist_t *rend_service_list = NULL;
- int
- num_rend_services(void)
- {
- if (!rend_service_list)
- return 0;
- return smartlist_len(rend_service_list);
- }
- static void
- rend_service_free(rend_service_t *service)
- {
- if (!service) return;
- tor_free(service->directory);
- SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p));
- smartlist_free(service->ports);
- if (service->private_key)
- crypto_free_pk_env(service->private_key);
- tor_free(service->intro_prefer_nodes);
- tor_free(service->intro_exclude_nodes);
- SMARTLIST_FOREACH(service->intro_nodes, void*, p, tor_free(p));
- smartlist_free(service->intro_nodes);
- if (service->desc)
- rend_service_descriptor_free(service->desc);
- tor_free(service);
- }
- void
- rend_service_free_all(void)
- {
- if (!rend_service_list) {
- return;
- }
- SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr,
- rend_service_free(ptr));
- smartlist_free(rend_service_list);
- rend_service_list = NULL;
- }
- static void
- add_service(rend_service_t *service)
- {
- int i;
- rend_service_port_config_t *p;
- struct in_addr addr;
- if (!service->intro_prefer_nodes)
- service->intro_prefer_nodes = tor_strdup("");
- if (!service->intro_exclude_nodes)
- service->intro_exclude_nodes = tor_strdup("");
- if (!smartlist_len(service->ports)) {
- warn(LD_CONFIG, "Hidden service with no ports configured; ignoring.");
- rend_service_free(service);
- } else {
- smartlist_set_capacity(service->ports, -1);
- smartlist_add(rend_service_list, service);
- debug(LD_REND,"Configuring service with directory \"%s\"",
- service->directory);
- for (i = 0; i < smartlist_len(service->ports); ++i) {
- char addrbuf[INET_NTOA_BUF_LEN];
- p = smartlist_get(service->ports, i);
- addr.s_addr = htonl(p->real_addr);
- tor_inet_ntoa(&addr, addrbuf, sizeof(addrbuf));
- debug(LD_REND,"Service maps port %d to %s:%d",
- p->virtual_port, addrbuf, p->real_port);
- }
- }
- }
- static rend_service_port_config_t *
- parse_port_config(const char *string)
- {
- smartlist_t *sl;
- int virtport;
- int realport;
- uint16_t p;
- uint32_t addr;
- const char *addrport;
- rend_service_port_config_t *result = NULL;
- sl = smartlist_create();
- smartlist_split_string(sl, string, " ",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- if (smartlist_len(sl) < 1 || smartlist_len(sl) > 2) {
- warn(LD_CONFIG, "Bad syntax in hidden service port configuration.");
- goto err;
- }
- virtport = atoi(smartlist_get(sl,0));
- if (virtport < 1 || virtport > 65535) {
- warn(LD_CONFIG, "Missing or invalid port in hidden service port "
- "configuration.");
- goto err;
- }
- if (smartlist_len(sl) == 1) {
-
- realport = virtport;
- addr = 0x7F000001u;
- } else {
- addrport = smartlist_get(sl,1);
- if (strchr(addrport, ':') || strchr(addrport, '.')) {
- if (parse_addr_port(addrport, NULL, &addr, &p)<0) {
- warn(LD_CONFIG,"Unparseable address in hidden service port "
- "configuration.");
- goto err;
- }
- realport = p?p:virtport;
- } else {
-
- realport = atoi(addrport);
- if (realport < 1 || realport > 65535)
- goto err;
- addr = 0x7F000001u;
- }
- }
- result = tor_malloc(sizeof(rend_service_port_config_t));
- result->virtual_port = virtport;
- result->real_port = realport;
- result->real_addr = addr;
- err:
- SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
- smartlist_free(sl);
- return result;
- }
- int
- rend_config_services(or_options_t *options, int validate_only)
- {
- config_line_t *line;
- rend_service_t *service = NULL;
- rend_service_port_config_t *portcfg;
- if (!validate_only) {
- rend_service_free_all();
- rend_service_list = smartlist_create();
- }
- for (line = options->RendConfigLines; line; line = line->next) {
- if (!strcasecmp(line->key, "HiddenServiceDir")) {
- if (service) {
- if (validate_only)
- rend_service_free(service);
- else
- add_service(service);
- }
- service = tor_malloc_zero(sizeof(rend_service_t));
- service->directory = tor_strdup(line->value);
- service->ports = smartlist_create();
- service->intro_nodes = smartlist_create();
- service->intro_period_started = time(NULL);
- continue;
- }
- if (!service) {
- warn(LD_CONFIG, "HiddenServicePort with no preceding HiddenServiceDir "
- "directive.");
- rend_service_free(service);
- return -1;
- }
- if (!strcasecmp(line->key, "HiddenServicePort")) {
- portcfg = parse_port_config(line->value);
- if (!portcfg) {
- rend_service_free(service);
- return -1;
- }
- smartlist_add(service->ports, portcfg);
- } else if (!strcasecmp(line->key, "HiddenServiceNodes")) {
- if (service->intro_prefer_nodes) {
- warn(LD_CONFIG, "Got multiple HiddenServiceNodes lines for a single "
- "service.");
- return -1;
- }
- service->intro_prefer_nodes = tor_strdup(line->value);
- } else {
- tor_assert(!strcasecmp(line->key, "HiddenServiceExcludeNodes"));
- if (service->intro_exclude_nodes) {
- warn(LD_CONFIG, "Got multiple HiddenServiceExcludedNodes lines for "
- "a single service.");
- return -1;
- }
- service->intro_exclude_nodes = tor_strdup(line->value);
- }
- }
- if (service) {
- if (validate_only)
- rend_service_free(service);
- else
- add_service(service);
- }
- return 0;
- }
- static void
- rend_service_update_descriptor(rend_service_t *service)
- {
- rend_service_descriptor_t *d;
- circuit_t *circ;
- int i,n;
- routerinfo_t *router;
- if (service->desc) {
- rend_service_descriptor_free(service->desc);
- service->desc = NULL;
- }
- d = service->desc = tor_malloc(sizeof(rend_service_descriptor_t));
- d->pk = crypto_pk_dup_key(service->private_key);
- d->timestamp = time(NULL);
- d->version = 1;
- n = smartlist_len(service->intro_nodes);
- d->n_intro_points = 0;
- d->intro_points = tor_malloc_zero(sizeof(char*)*n);
- d->intro_point_extend_info = tor_malloc_zero(sizeof(extend_info_t*)*n);
- d->protocols = (1<<2) | (1<<0);
- for (i=0; i < n; ++i) {
- router = router_get_by_nickname(smartlist_get(service->intro_nodes, i),1);
- if (!router) {
- info(LD_REND,"Router '%s' not found. Skipping.",
- (char*)smartlist_get(service->intro_nodes, i));
- continue;
- }
- circ = find_intro_circuit(router, service->pk_digest);
- if (circ && circ->purpose == CIRCUIT_PURPOSE_S_INTRO) {
-
- d->intro_points[d->n_intro_points] = tor_strdup(router->nickname);
- d->intro_point_extend_info[d->n_intro_points] =
- extend_info_from_router(router);
- d->n_intro_points++;
- }
- }
- }
- int
- rend_service_load_keys(void)
- {
- int i;
- rend_service_t *s;
- char fname[512];
- char buf[128];
- for (i=0; i < smartlist_len(rend_service_list); ++i) {
- s = smartlist_get(rend_service_list,i);
- if (s->private_key)
- continue;
- info(LD_REND, "Loading hidden-service keys from \"%s\"", s->directory);
-
- if (check_private_dir(s->directory, CPD_CREATE) < 0)
- return -1;
-
- if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
- strlcat(fname,"/private_key",sizeof(fname)) >= sizeof(fname)) {
- warn(LD_CONFIG, "Directory name too long: \"%s\".", s->directory);
- return -1;
- }
- s->private_key = init_key_from_file(fname);
- if (!s->private_key)
- return -1;
-
- if (rend_get_service_id(s->private_key, s->service_id)<0) {
- warn(LD_BUG, "Internal error: couldn't encode service ID.");
- return -1;
- }
- if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
- warn(LD_BUG, "Bug: Couldn't compute hash of public key.");
- return -1;
- }
- if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
- strlcat(fname,"/hostname",sizeof(fname)) >= sizeof(fname)) {
- warn(LD_CONFIG, "Directory name too long: \"%s\".", s->directory);
- return -1;
- }
- tor_snprintf(buf, sizeof(buf),"%s.onion\n", s->service_id);
- if (write_str_to_file(fname,buf,0)<0)
- return -1;
- }
- return 0;
- }
- static rend_service_t *
- rend_service_get_by_pk_digest(const char* digest)
- {
- SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
- if (!memcmp(s->pk_digest,digest,DIGEST_LEN)) return s);
- return NULL;
- }
- static int
- rend_service_requires_uptime(rend_service_t *service)
- {
- int i;
- rend_service_port_config_t *p;
- for (i=0; i < smartlist_len(service->ports); ++i) {
- p = smartlist_get(service->ports, i);
- if (smartlist_string_num_isin(get_options()->LongLivedPorts,
- p->virtual_port))
- return 1;
- }
- return 0;
- }
- int
- rend_service_introduce(circuit_t *circuit, const char *request,
- size_t request_len)
- {
- char *ptr, *r_cookie;
- extend_info_t *extend_info = NULL;
- char buf[RELAY_PAYLOAD_SIZE];
- char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
- rend_service_t *service;
- int r, i;
- size_t len, keylen;
- crypto_dh_env_t *dh = NULL;
- circuit_t *launched = NULL;
- crypt_path_t *cpath = NULL;
- char serviceid[REND_SERVICE_ID_LEN+1];
- char hexcookie[9];
- int circ_needs_uptime;
- base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
- circuit->rend_pk_digest,10);
- info(LD_REND, "Received INTRODUCE2 cell for service %s on circ %d.",
- serviceid, circuit->n_circ_id);
- if (circuit->purpose != CIRCUIT_PURPOSE_S_INTRO) {
- warn(LD_PROTOCOL, "Got an INTRODUCE2 over a non-introduction circuit %d.",
- circuit->n_circ_id);
- return -1;
- }
-
- if (request_len < DIGEST_LEN+REND_COOKIE_LEN+(MAX_NICKNAME_LEN+1)+
- DH_KEY_LEN+42) {
- warn(LD_PROTOCOL, "Got a truncated INTRODUCE2 cell on circ %d.",
- circuit->n_circ_id);
- return -1;
- }
-
- service = rend_service_get_by_pk_digest(request);
- if (!service) {
- warn(LD_REND, "Got an INTRODUCE2 cell for an unrecognized service %s.",
- serviceid);
- return -1;
- }
- if (memcmp(circuit->rend_pk_digest, request, DIGEST_LEN)) {
- base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request, 10);
- warn(LD_REND, "Got an INTRODUCE2 cell for the wrong service (%s).",
- serviceid);
- return -1;
- }
- keylen = crypto_pk_keysize(service->private_key);
- if (request_len < keylen+DIGEST_LEN) {
- warn(LD_PROTOCOL,
- "PK-encrypted portion of INTRODUCE2 cell was truncated.");
- return -1;
- }
-
- r = crypto_pk_private_hybrid_decrypt(
- service->private_key,buf,request+DIGEST_LEN,request_len-DIGEST_LEN,
- PK_PKCS1_OAEP_PADDING,1);
- if (r<0) {
- warn(LD_PROTOCOL, "Couldn't decrypt INTRODUCE2 cell.");
- return -1;
- }
- len = r;
- if (*buf == 2) {
-
- int klen;
- extend_info = tor_malloc_zero(sizeof(extend_info_t));
- extend_info->addr = ntohl(get_uint32(buf+1));
- extend_info->port = ntohs(get_uint16(buf+5));
- memcpy(extend_info->identity_digest, buf+7, DIGEST_LEN);
- extend_info->nickname[0] = '$';
- base16_encode(extend_info->nickname+1, sizeof(extend_info->nickname)-1,
- extend_info->identity_digest, DIGEST_LEN);
- klen = ntohs(get_uint16(buf+7+DIGEST_LEN));
- if ((int)len != 7+DIGEST_LEN+2+klen+20+128) {
- warn(LD_PROTOCOL, "Bad length %u for version 2 INTRODUCE2 cell.",
- (int)len);
- goto err;
- }
- extend_info->onion_key = crypto_pk_asn1_decode(buf+7+DIGEST_LEN+2, klen);
- if (!extend_info->onion_key) {
- warn(LD_PROTOCOL,
- "Error decoding onion key in version 2 INTRODUCE2 cell.");
- goto err;
- }
- ptr = buf+7+DIGEST_LEN+2+klen;
- len -= 7+DIGEST_LEN+2+klen;
- } else {
- char *rp_nickname;
- size_t nickname_field_len;
- routerinfo_t *router;
- int version;
- if (*buf == 1) {
- rp_nickname = buf+1;
- nickname_field_len = MAX_HEX_NICKNAME_LEN+1;
- version = 1;
- } else {
- nickname_field_len = MAX_NICKNAME_LEN+1;
- rp_nickname = buf;
- version = 0;
- }
-
- ptr=memchr(rp_nickname,0,nickname_field_len);
- if (!ptr || ptr == rp_nickname) {
- warn(LD_PROTOCOL,
- "Couldn't find a null-padded nickname in INTRODUCE2 cell.");
- return -1;
- }
- if ((version == 0 && !is_legal_nickname(rp_nickname)) ||
- (version == 1 && !is_legal_nickname_or_hexdigest(rp_nickname))) {
- warn(LD_PROTOCOL, "Bad nickname in INTRODUCE2 cell.");
- return -1;
- }
-
- ptr = rp_nickname+nickname_field_len;
- len -= nickname_field_len;
- len -= rp_nickname - buf;
- router = router_get_by_nickname(rp_nickname, 0);
- if (!router) {
- info(LD_REND, "Couldn't find router '%s' named in rendezvous cell.",
- rp_nickname);
- goto err;
- }
- extend_info = extend_info_from_router(router);
- }
- if (len != REND_COOKIE_LEN+DH_KEY_LEN) {
- warn(LD_PROTOCOL, "Bad length %u for INTRODUCE2 cell.", (int)len);
- return -1;
- }
- r_cookie = ptr;
- base16_encode(hexcookie,9,r_cookie,4);
-
- dh = crypto_dh_new();
- if (!dh || crypto_dh_generate_public(dh)<0) {
- warn(LD_BUG,"Internal error: couldn't build DH state "
- "or generate public key.");
- goto err;
- }
- if (crypto_dh_compute_secret(dh, ptr+REND_COOKIE_LEN, DH_KEY_LEN, keys,
- DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
- warn(LD_BUG, "Internal error: couldn't complete DH handshake");
- goto err;
- }
- circ_needs_uptime = rend_service_requires_uptime(service);
-
- rep_hist_note_used_internal(time(NULL), circ_needs_uptime, 1);
-
- for (i=0;i<MAX_REND_FAILURES;i++) {
- launched = circuit_launch_by_extend_info(
- CIRCUIT_PURPOSE_S_CONNECT_REND, extend_info,
- circ_needs_uptime, 1, 1);
- if (launched)
- break;
- }
- if (!launched) {
- warn(LD_REND, "Giving up launching first hop of circuit to rendezvous "
- "point '%s' for service %s.",
- extend_info->nickname, serviceid);
- goto err;
- }
- info(LD_REND,
- "Accepted intro; launching circuit to '%s' (cookie %s) for service %s.",
- extend_info->nickname, hexcookie, serviceid);
- tor_assert(launched->build_state);
-
- memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
- DIGEST_LEN);
- memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
- strlcpy(launched->rend_query, service->service_id,
- sizeof(launched->rend_query));
- launched->build_state->pending_final_cpath = cpath =
- tor_malloc_zero(sizeof(crypt_path_t));
- cpath->magic = CRYPT_PATH_MAGIC;
- launched->build_state->expiry_time = time(NULL) + MAX_REND_TIMEOUT;
- cpath->dh_handshake_state = dh;
- dh = NULL;
- if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
- goto err;
- memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
- if (extend_info) extend_info_free(extend_info);
- return 0;
- err:
- if (dh) crypto_dh_free(dh);
- if (launched) circuit_mark_for_close(launched);
- if (extend_info) extend_info_free(extend_info);
- return -1;
- }
- void
- rend_service_relaunch_rendezvous(circuit_t *oldcirc)
- {
- circuit_t *newcirc;
- cpath_build_state_t *newstate, *oldstate;
- tor_assert(oldcirc->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
- if (!oldcirc->build_state ||
- oldcirc->build_state->failure_count > MAX_REND_FAILURES ||
- oldcirc->build_state->expiry_time < time(NULL)) {
- info(LD_REND,"Attempt to build circuit to %s for rendezvous has failed "
- "too many times or expired; giving up.",
- oldcirc->build_state ? oldcirc->build_state->chosen_exit->nickname :
- "*unknown*");
- return;
- }
- oldstate = oldcirc->build_state;
- tor_assert(oldstate);
- if (oldstate->pending_final_cpath == NULL) {
- info(LD_REND,"Skipping relaunch of circ that failed on its first hop. "
- "Initiator will retry.");
- return;
- }
- info(LD_REND,"Reattempting rendezvous circuit to %s",
- oldstate->chosen_exit->nickname);
- newcirc = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND,
- oldstate->chosen_exit, 0, 1, 1);
- if (!newcirc) {
- warn(LD_REND,"Couldn't relaunch rendezvous circuit to %s.",
- oldstate->chosen_exit->nickname);
- return;
- }
- newstate = newcirc->build_state;
- tor_assert(newstate);
- newstate->failure_count = oldstate->failure_count+1;
- newstate->expiry_time = oldstate->expiry_time;
- newstate->pending_final_cpath = oldstate->pending_final_cpath;
- oldstate->pending_final_cpath = NULL;
- memcpy(newcirc->rend_query, oldcirc->rend_query, REND_SERVICE_ID_LEN+1);
- memcpy(newcirc->rend_pk_digest, oldcirc->rend_pk_digest, DIGEST_LEN);
- memcpy(newcirc->rend_cookie, oldcirc->rend_cookie, REND_COOKIE_LEN);
- }
- static int
- rend_service_launch_establish_intro(rend_service_t *service,
- const char *nickname)
- {
- circuit_t *launched;
- info(LD_REND, "Launching circuit to introduction point %s for service %s",
- nickname, service->service_id);
- rep_hist_note_used_internal(time(NULL), 1, 0);
- ++service->n_intro_circuits_launched;
- launched = circuit_launch_by_nickname(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
- nickname, 1, 0, 1);
- if (!launched) {
- info(LD_REND, "Can't launch circuit to establish introduction at '%s'.",
- nickname);
- return -1;
- }
- strlcpy(launched->rend_query, service->service_id,
- sizeof(launched->rend_query));
- memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
- if (launched->state == CIRCUIT_STATE_OPEN)
- rend_service_intro_has_opened(launched);
- return 0;
- }
- void
- rend_service_intro_has_opened(circuit_t *circuit)
- {
- rend_service_t *service;
- size_t len;
- int r;
- char buf[RELAY_PAYLOAD_SIZE];
- char auth[DIGEST_LEN + 9];
- char serviceid[REND_SERVICE_ID_LEN+1];
- tor_assert(circuit->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
- tor_assert(CIRCUIT_IS_ORIGIN(circuit));
- tor_assert(circuit->cpath);
- base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
- circuit->rend_pk_digest,10);
- service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
- if (!service) {
- warn(LD_REND, "Unrecognized service ID %s on introduction circuit %d.",
- serviceid, circuit->n_circ_id);
- goto err;
- }
- info(LD_REND,
- "Established circuit %d as introduction point for service %s",
- circuit->n_circ_id, serviceid);
-
- len = crypto_pk_asn1_encode(service->private_key, buf+2,
- RELAY_PAYLOAD_SIZE-2);
- set_uint16(buf, htons((uint16_t)len));
- len += 2;
- memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
- memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
- if (crypto_digest(buf+len, auth, DIGEST_LEN+9))
- goto err;
- len += 20;
- r = crypto_pk_private_sign_digest(service->private_key, buf+len, buf, len);
- if (r<0) {
- warn(LD_BUG, "Internal error: couldn't sign introduction request.");
- goto err;
- }
- len += r;
- if (connection_edge_send_command(NULL, circuit,RELAY_COMMAND_ESTABLISH_INTRO,
- buf, len, circuit->cpath->prev)<0) {
- info(LD_GENERAL,
- "Couldn't send introduction request for service %s on circuit %d",
- serviceid, circuit->n_circ_id);
- goto err;
- }
- return;
- err:
- circuit_mark_for_close(circuit);
- }
- int
- rend_service_intro_established(circuit_t *circuit, const char *request,
- size_t request_len)
- {
- rend_service_t *service;
- if (circuit->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
- warn(LD_PROTOCOL, "received INTRO_ESTABLISHED cell on non-intro circuit.");
- goto err;
- }
- service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
- if (!service) {
- warn(LD_REND, "Unknown service on introduction circuit %d.",
- circuit->n_circ_id);
- goto err;
- }
- service->desc_is_dirty = time(NULL);
- circuit->purpose = CIRCUIT_PURPOSE_S_INTRO;
- return 0;
- err:
- circuit_mark_for_close(circuit);
- return -1;
- }
- void
- rend_service_rendezvous_has_opened(circuit_t *circuit)
- {
- rend_service_t *service;
- char buf[RELAY_PAYLOAD_SIZE];
- crypt_path_t *hop;
- char serviceid[REND_SERVICE_ID_LEN+1];
- char hexcookie[9];
- tor_assert(circuit->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
- tor_assert(circuit->cpath);
- tor_assert(circuit->build_state);
- hop = circuit->build_state->pending_final_cpath;
- tor_assert(hop);
- base16_encode(hexcookie,9,circuit->rend_cookie,4);
- base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
- circuit->rend_pk_digest,10);
- info(LD_REND,
- "Done building circuit %d to rendezvous with cookie %s for service %s",
- circuit->n_circ_id, hexcookie, serviceid);
- service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
- if (!service) {
- warn(LD_GENERAL, "Internal error: unrecognized service ID on "
- "introduction circuit.");
- goto err;
- }
-
- memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
- if (crypto_dh_get_public(hop->dh_handshake_state,
- buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
- warn(LD_GENERAL,"Couldn't get DH public key.");
- goto err;
- }
- memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
- DIGEST_LEN);
-
- if (connection_edge_send_command(NULL, circuit, RELAY_COMMAND_RENDEZVOUS1,
- buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
- circuit->cpath->prev)<0) {
- warn(LD_GENERAL, "Couldn't send RENDEZVOUS1 cell.");
- goto err;
- }
- crypto_dh_free(hop->dh_handshake_state);
- hop->dh_handshake_state = NULL;
-
- hop->state = CPATH_STATE_OPEN;
-
- hop->package_window = CIRCWINDOW_START;
- hop->deliver_window = CIRCWINDOW_START;
- onion_append_to_cpath(&circuit->cpath, hop);
- circuit->build_state->pending_final_cpath = NULL;
-
- circuit->purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
- return;
- err:
- circuit_mark_for_close(circuit);
- }
- static circuit_t *
- find_intro_circuit(routerinfo_t *router, const char *pk_digest)
- {
- circuit_t *circ = NULL;
- tor_assert(router);
- while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
- CIRCUIT_PURPOSE_S_INTRO))) {
- tor_assert(circ->cpath);
- if (!strcasecmp(circ->build_state->chosen_exit->nickname,
- router->nickname)) {
- return circ;
- }
- }
- circ = NULL;
- while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
- CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
- tor_assert(circ->cpath);
- if (!strcasecmp(circ->build_state->chosen_exit->nickname,
- router->nickname)) {
- return circ;
- }
- }
- return NULL;
- }
- static void
- upload_service_descriptor(rend_service_t *service, int version)
- {
- char *desc;
- size_t desc_len;
-
- rend_service_update_descriptor(service);
- if (rend_encode_service_descriptor(service->desc,
- version,
- service->private_key,
- &desc, &desc_len)<0) {
- warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
- "not uploading.");
- return;
- }
-
- directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC, desc, desc_len);
- tor_free(desc);
- service->desc_is_dirty = 0;
- }
- #define MAX_SERVICE_PUBLICATION_INTERVAL (15*60)
- void
- rend_services_introduce(void)
- {
- int i,j,r;
- routerinfo_t *router;
- rend_service_t *service;
- char *intro;
- int changed, prev_intro_nodes;
- smartlist_t *intro_routers, *exclude_routers;
- time_t now;
- intro_routers = smartlist_create();
- exclude_routers = smartlist_create();
- now = time(NULL);
- for (i=0; i < smartlist_len(rend_service_list); ++i) {
- smartlist_clear(intro_routers);
- service = smartlist_get(rend_service_list, i);
- tor_assert(service);
- changed = 0;
- if (now > service->intro_period_started+INTRO_CIRC_RETRY_PERIOD) {
-
- service->intro_period_started = now;
- service->n_intro_circuits_launched = 0;
- } else if (service->n_intro_circuits_launched >=
- MAX_INTRO_CIRCS_PER_PERIOD) {
-
- continue;
- }
-
- for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
- intro = smartlist_get(service->intro_nodes, j);
- router = router_get_by_nickname(intro, 0);
- if (!router || !find_intro_circuit(router,service->pk_digest)) {
- info(LD_REND,"Giving up on %s as intro point for %s.",
- intro, service->service_id);
- tor_free(intro);
- smartlist_del(service->intro_nodes,j--);
- changed = 1;
- service->desc_is_dirty = now;
- }
- smartlist_add(intro_routers, router);
- }
-
- if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS) {
-
- service->intro_period_started = now;
- service->n_intro_circuits_launched = 0;
- continue;
- }
-
- prev_intro_nodes = smartlist_len(service->intro_nodes);
- smartlist_add_all(exclude_routers, intro_routers);
-
- for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
- char *hex_digest;
- router = router_choose_random_node(service->intro_prefer_nodes,
- service->intro_exclude_nodes, exclude_routers, 1, 0,
- get_options()->_AllowUnverified & ALLOW_UNVERIFIED_INTRODUCTION,
- 0);
- if (!router) {
- warn(LD_REND, "Could only establish %d introduction points for %s.",
- smartlist_len(service->intro_nodes), service->service_id);
- break;
- }
- changed = 1;
- hex_digest = tor_malloc_zero(HEX_DIGEST_LEN+2);
- hex_digest[0] = '$';
- base16_encode(hex_digest+1, HEX_DIGEST_LEN+1,
- router->cache_info.identity_digest,
- DIGEST_LEN);
- smartlist_add(intro_routers, router);
- smartlist_add(exclude_routers, router);
- smartlist_add(service->intro_nodes, hex_digest);
- info(LD_REND, "Picked router %s as an intro point for %s.",
- router->nickname, service->service_id);
- }
-
- smartlist_clear(exclude_routers);
-
- if (!changed)
- continue;
-
- for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
- intro = smartlist_get(service->intro_nodes, j);
- r = rend_service_launch_establish_intro(service, intro);
- if (r<0) {
- warn(LD_REND, "Error launching circuit to node %s for service %s.",
- intro, service->service_id);
- }
- }
- }
- smartlist_free(intro_routers);
- smartlist_free(exclude_routers);
- }
- void
- rend_consider_services_upload(time_t now)
- {
- int i;
- rend_service_t *service;
- int rendpostperiod = get_options()->RendPostPeriod;
- for (i=0; i < smartlist_len(rend_service_list); ++i) {
- service = smartlist_get(rend_service_list, i);
- if (!service->next_upload_time) {
- service->next_upload_time =
- now + crypto_rand_int(2*rendpostperiod);
- }
- if (service->next_upload_time < now ||
- (service->desc_is_dirty &&
- service->desc_is_dirty < now-5)) {
-
- upload_service_descriptor(service, 0);
-
- service->next_upload_time = now + rendpostperiod;
- }
- }
- }
- void
- rend_service_dump_stats(int severity)
- {
- int i,j;
- routerinfo_t *router;
- rend_service_t *service;
- char *nickname;
- circuit_t *circ;
- for (i=0; i < smartlist_len(rend_service_list); ++i) {
- service = smartlist_get(rend_service_list, i);
- log(severity, LD_GENERAL, "Service configured in \"%s\":",
- service->directory);
- for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
- nickname = smartlist_get(service->intro_nodes, j);
- router = router_get_by_nickname(smartlist_get(service->intro_nodes,j),1);
- if (!router) {
- log(severity, LD_GENERAL, " Intro point at %s: unrecognized router",
- nickname);
- continue;
- }
- circ = find_intro_circuit(router, service->pk_digest);
- if (!circ) {
- log(severity, LD_GENERAL, " Intro point at %s: no circuit",nickname);
- continue;
- }
- log(severity, LD_GENERAL, " Intro point at %s: circuit is %s",nickname,
- circuit_state_to_string(circ->state));
- }
- }
- }
- int
- rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ)
- {
- rend_service_t *service;
- int i;
- rend_service_port_config_t *p;
- char serviceid[REND_SERVICE_ID_LEN+1];
- tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
- debug(LD_REND,"beginning to hunt for addr/port");
- base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
- circ->rend_pk_digest,10);
- service = rend_service_get_by_pk_digest(circ->rend_pk_digest);
- if (!service) {
- warn(LD_REND, "Couldn't find any service associated with pk %s on "
- "rendezvous circuit %d; closing.",
- serviceid, circ->n_circ_id);
- return -1;
- }
- for (i = 0; i < smartlist_len(service->ports); ++i) {
- p = smartlist_get(service->ports, i);
- if (conn->port == p->virtual_port) {
- conn->addr = p->real_addr;
- conn->port = p->real_port;
- return 0;
- }
- }
- info(LD_REND, "No virtual port mapping exists for port %d on service %s",
- conn->port,serviceid);
- return -1;
- }
|