123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617 |
- #define HS_CLIENT_PRIVATE
- #include "or.h"
- #include "hs_circuit.h"
- #include "hs_ident.h"
- #include "connection_edge.h"
- #include "container.h"
- #include "rendclient.h"
- #include "hs_descriptor.h"
- #include "hs_cache.h"
- #include "hs_cell.h"
- #include "hs_ident.h"
- #include "config.h"
- #include "directory.h"
- #include "hs_client.h"
- #include "hs_control.h"
- #include "router.h"
- #include "routerset.h"
- #include "circuitlist.h"
- #include "circuituse.h"
- #include "connection.h"
- #include "nodelist.h"
- #include "circpathbias.h"
- #include "connection.h"
- #include "hs_ntor.h"
- #include "circuitbuild.h"
- #include "networkstatus.h"
- #include "reasons.h"
- static const char *
- fetch_status_to_string(hs_client_fetch_status_t status)
- {
- switch (status) {
- case HS_CLIENT_FETCH_ERROR:
- return "Internal error";
- case HS_CLIENT_FETCH_LAUNCHED:
- return "Descriptor fetch launched";
- case HS_CLIENT_FETCH_HAVE_DESC:
- return "Already have descriptor";
- case HS_CLIENT_FETCH_NO_HSDIRS:
- return "No more HSDir available to query";
- case HS_CLIENT_FETCH_NOT_ALLOWED:
- return "Fetching descriptors is not allowed";
- case HS_CLIENT_FETCH_MISSING_INFO:
- return "Missing directory information";
- case HS_CLIENT_FETCH_PENDING:
- return "Pending descriptor fetch";
- default:
- return "(Unknown client fetch status code)";
- }
- }
- static int
- fetch_status_should_close_socks(hs_client_fetch_status_t status)
- {
- switch (status) {
- case HS_CLIENT_FETCH_NO_HSDIRS:
-
- case HS_CLIENT_FETCH_ERROR:
-
- case HS_CLIENT_FETCH_NOT_ALLOWED:
-
- goto close;
- case HS_CLIENT_FETCH_MISSING_INFO:
- case HS_CLIENT_FETCH_HAVE_DESC:
- case HS_CLIENT_FETCH_PENDING:
- case HS_CLIENT_FETCH_LAUNCHED:
-
- goto no_close;
- }
- no_close:
- return 0;
- close:
- return 1;
- }
- static void
- cancel_descriptor_fetches(void)
- {
- smartlist_t *conns =
- connection_list_by_type_state(CONN_TYPE_DIR, DIR_PURPOSE_FETCH_HSDESC);
- SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
- const hs_ident_dir_conn_t *ident = TO_DIR_CONN(conn)->hs_ident;
- if (BUG(ident == NULL)) {
-
- continue;
- }
- log_debug(LD_REND, "Marking for close a directory connection fetching "
- "a hidden service descriptor for service %s.",
- safe_str_client(ed25519_fmt(&ident->identity_pk)));
- connection_mark_for_close(conn);
- } SMARTLIST_FOREACH_END(conn);
-
- smartlist_free(conns);
- log_info(LD_REND, "Hidden service client descriptor fetches cancelled.");
- }
- static void
- flag_all_conn_wait_desc(const ed25519_public_key_t *service_identity_pk)
- {
- tor_assert(service_identity_pk);
- smartlist_t *conns =
- connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_CIRCUIT_WAIT);
- SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
- edge_connection_t *edge_conn;
- if (BUG(!CONN_IS_EDGE(conn))) {
- continue;
- }
- edge_conn = TO_EDGE_CONN(conn);
- if (edge_conn->hs_ident &&
- ed25519_pubkey_eq(&edge_conn->hs_ident->identity_pk,
- service_identity_pk)) {
- connection_ap_mark_as_non_pending_circuit(TO_ENTRY_CONN(conn));
- conn->state = AP_CONN_STATE_RENDDESC_WAIT;
- }
- } SMARTLIST_FOREACH_END(conn);
- smartlist_free(conns);
- }
- static void
- purge_hid_serv_request(const ed25519_public_key_t *identity_pk)
- {
- char base64_blinded_pk[ED25519_BASE64_LEN + 1];
- ed25519_public_key_t blinded_pk;
- tor_assert(identity_pk);
-
- hs_build_blinded_pubkey(identity_pk, NULL, 0,
- hs_get_time_period_num(0), &blinded_pk);
- if (BUG(ed25519_public_to_base64(base64_blinded_pk, &blinded_pk) < 0)) {
- return;
- }
-
- hs_purge_hid_serv_from_last_hid_serv_requests(base64_blinded_pk);
- }
- static int
- directory_request_is_pending(const ed25519_public_key_t *identity_pk)
- {
- int ret = 0;
- smartlist_t *conns =
- connection_list_by_type_purpose(CONN_TYPE_DIR, DIR_PURPOSE_FETCH_HSDESC);
- SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
- const hs_ident_dir_conn_t *ident = TO_DIR_CONN(conn)->hs_ident;
- if (BUG(ident == NULL)) {
-
- continue;
- }
- if (!ed25519_pubkey_eq(identity_pk, &ident->identity_pk)) {
- continue;
- }
- ret = 1;
- break;
- } SMARTLIST_FOREACH_END(conn);
-
- smartlist_free(conns);
- return ret;
- }
- static void
- close_all_socks_conns_waiting_for_desc(const ed25519_public_key_t *identity_pk,
- hs_client_fetch_status_t status,
- int reason)
- {
- unsigned int count = 0;
- time_t now = approx_time();
- smartlist_t *conns =
- connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_RENDDESC_WAIT);
- SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
- entry_connection_t *entry_conn = TO_ENTRY_CONN(base_conn);
- const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
-
- if (!edge_conn->hs_ident ||
- !ed25519_pubkey_eq(identity_pk,
- &edge_conn->hs_ident->identity_pk)) {
- continue;
- }
- assert_connection_ok(base_conn, now);
-
- connection_mark_unattached_ap(entry_conn, reason);
- count++;
- } SMARTLIST_FOREACH_END(base_conn);
- if (count > 0) {
- char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
- hs_build_address(identity_pk, HS_VERSION_THREE, onion_address);
- log_notice(LD_REND, "Closed %u streams for service %s.onion "
- "for reason %s. Fetch status: %s.",
- count, safe_str_client(onion_address),
- stream_end_reason_to_string(reason),
- fetch_status_to_string(status));
- }
-
- smartlist_free(conns);
- }
- static void
- retry_all_socks_conn_waiting_for_desc(void)
- {
- smartlist_t *conns =
- connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_RENDDESC_WAIT);
- SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
- hs_client_fetch_status_t status;
- const edge_connection_t *edge_conn =
- ENTRY_TO_EDGE_CONN(TO_ENTRY_CONN(base_conn));
-
- if (edge_conn->hs_ident == NULL) {
- continue;
- }
-
- if (base_conn->marked_for_close) {
- continue;
- }
-
-
- status = hs_client_refetch_hsdesc(&edge_conn->hs_ident->identity_pk);
- if (BUG(status == HS_CLIENT_FETCH_HAVE_DESC)) {
-
- continue;
- }
-
- } SMARTLIST_FOREACH_END(base_conn);
-
- smartlist_free(conns);
- }
- static void
- note_connection_attempt_succeeded(const hs_ident_edge_conn_t *hs_conn_ident)
- {
- tor_assert(hs_conn_ident);
-
- purge_hid_serv_request(&hs_conn_ident->identity_pk);
-
- }
- static hs_client_fetch_status_t
- directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk,
- const routerstatus_t *hsdir)
- {
- uint64_t current_time_period = hs_get_time_period_num(0);
- ed25519_public_key_t blinded_pubkey;
- char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
- hs_ident_dir_conn_t hs_conn_dir_ident;
- int retval;
- tor_assert(hsdir);
- tor_assert(onion_identity_pk);
-
- hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
- current_time_period, &blinded_pubkey);
-
- retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
- if (BUG(retval < 0)) {
- return HS_CLIENT_FETCH_ERROR;
- }
-
- hs_ident_dir_conn_init(onion_identity_pk, &blinded_pubkey,
- &hs_conn_dir_ident);
-
- directory_request_t *req =
- directory_request_new(DIR_PURPOSE_FETCH_HSDESC);
- directory_request_set_routerstatus(req, hsdir);
- directory_request_set_indirection(req, DIRIND_ANONYMOUS);
- directory_request_set_resource(req, base64_blinded_pubkey);
- directory_request_fetch_set_hs_ident(req, &hs_conn_dir_ident);
- directory_initiate_request(req);
- directory_request_free(req);
- log_info(LD_REND, "Descriptor fetch request for service %s with blinded "
- "key %s to directory %s",
- safe_str_client(ed25519_fmt(onion_identity_pk)),
- safe_str_client(base64_blinded_pubkey),
- safe_str_client(routerstatus_describe(hsdir)));
-
- hs_control_desc_event_requested(onion_identity_pk, base64_blinded_pubkey,
- hsdir);
-
- memwipe(&blinded_pubkey, 0, sizeof(blinded_pubkey));
- memwipe(base64_blinded_pubkey, 0, sizeof(base64_blinded_pubkey));
- memwipe(&hs_conn_dir_ident, 0, sizeof(hs_conn_dir_ident));
- return HS_CLIENT_FETCH_LAUNCHED;
- }
- STATIC routerstatus_t *
- pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
- {
- int retval;
- char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
- uint64_t current_time_period = hs_get_time_period_num(0);
- smartlist_t *responsible_hsdirs;
- ed25519_public_key_t blinded_pubkey;
- routerstatus_t *hsdir_rs = NULL;
- tor_assert(onion_identity_pk);
- responsible_hsdirs = smartlist_new();
-
- hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
- current_time_period, &blinded_pubkey);
-
- retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
- if (BUG(retval < 0)) {
- return NULL;
- }
-
- hs_get_responsible_hsdirs(&blinded_pubkey, current_time_period,
- 0, 1, responsible_hsdirs);
- log_debug(LD_REND, "Found %d responsible HSDirs and about to pick one.",
- smartlist_len(responsible_hsdirs));
-
- hsdir_rs = hs_pick_hsdir(responsible_hsdirs, base64_blinded_pubkey);
- return hsdir_rs;
- }
- MOCK_IMPL(STATIC hs_client_fetch_status_t,
- fetch_v3_desc, (const ed25519_public_key_t *onion_identity_pk))
- {
- routerstatus_t *hsdir_rs =NULL;
- tor_assert(onion_identity_pk);
- hsdir_rs = pick_hsdir_v3(onion_identity_pk);
- if (!hsdir_rs) {
- log_info(LD_REND, "Couldn't pick a v3 hsdir.");
- return HS_CLIENT_FETCH_NO_HSDIRS;
- }
- return directory_launch_v3_desc_fetch(onion_identity_pk, hsdir_rs);
- }
- static int
- intro_circ_is_ok(const origin_circuit_t *circ)
- {
- int ret = 0;
- tor_assert(circ);
- if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
- TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
- TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACKED)) {
- ret = -1;
- }
- if (BUG(circ->hs_ident == NULL)) {
- ret = -1;
- }
- if (BUG(!hs_ident_intro_circ_is_valid(circ->hs_ident))) {
- ret = -1;
- }
-
- assert_circ_anonymity_ok(circ, get_options());
- return ret;
- }
- static const hs_desc_intro_point_t *
- find_desc_intro_point_by_ident(const hs_ident_circuit_t *ident,
- const hs_descriptor_t *desc)
- {
- const hs_desc_intro_point_t *intro_point = NULL;
- tor_assert(ident);
- tor_assert(desc);
- SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
- const hs_desc_intro_point_t *, ip) {
- if (ed25519_pubkey_eq(&ident->intro_auth_pk,
- &ip->auth_key_cert->signed_key)) {
- intro_point = ip;
- break;
- }
- } SMARTLIST_FOREACH_END(ip);
- return intro_point;
- }
- static hs_desc_intro_point_t *
- find_desc_intro_point_by_legacy_id(const char *legacy_id,
- const hs_descriptor_t *desc)
- {
- hs_desc_intro_point_t *ret_ip = NULL;
- tor_assert(legacy_id);
- tor_assert(desc);
-
- SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
- hs_desc_intro_point_t *, ip) {
- SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
- const hs_desc_link_specifier_t *, lspec) {
-
- if (lspec->type != LS_LEGACY_ID) {
- continue;
- }
- if (fast_memneq(legacy_id, lspec->u.legacy_id, DIGEST_LEN)) {
- break;
- }
-
- ret_ip = ip;
- goto end;
- } SMARTLIST_FOREACH_END(lspec);
- } SMARTLIST_FOREACH_END(ip);
- end:
- return ret_ip;
- }
- static int
- send_introduce1(origin_circuit_t *intro_circ,
- origin_circuit_t *rend_circ)
- {
- int status;
- char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
- const ed25519_public_key_t *service_identity_pk = NULL;
- const hs_desc_intro_point_t *ip;
- tor_assert(rend_circ);
- if (intro_circ_is_ok(intro_circ) < 0) {
- goto perm_err;
- }
- service_identity_pk = &intro_circ->hs_ident->identity_pk;
-
- hs_build_address(service_identity_pk, HS_VERSION_THREE, onion_address);
- log_info(LD_REND, "Sending INTRODUCE1 cell to service %s on circuit %u",
- safe_str_client(onion_address), TO_CIRCUIT(intro_circ)->n_circ_id);
-
- const hs_descriptor_t *desc =
- hs_cache_lookup_as_client(service_identity_pk);
- if (desc == NULL || !hs_client_any_intro_points_usable(service_identity_pk,
- desc)) {
- log_info(LD_REND, "Request to %s %s. Trying to fetch a new descriptor.",
- safe_str_client(onion_address),
- (desc) ? "didn't have usable intro points" :
- "didn't have a descriptor");
- hs_client_refetch_hsdesc(service_identity_pk);
-
- flag_all_conn_wait_desc(service_identity_pk);
-
- goto tran_err;
- }
-
- ip = find_desc_intro_point_by_ident(intro_circ->hs_ident, desc);
- if (BUG(ip == NULL)) {
-
- goto perm_err;
- }
-
- if (hs_circ_send_introduce1(intro_circ, rend_circ, ip,
- desc->subcredential) < 0) {
-
- tor_assert_nonfatal(TO_CIRCUIT(intro_circ)->marked_for_close);
- goto perm_err;
- }
-
- memcpy(&rend_circ->hs_ident->intro_enc_pk, &ip->enc_key,
- sizeof(rend_circ->hs_ident->intro_enc_pk));
- ed25519_pubkey_copy(&rend_circ->hs_ident->intro_auth_pk,
- &intro_circ->hs_ident->intro_auth_pk);
-
- circuit_change_purpose(TO_CIRCUIT(intro_circ),
- CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT);
-
- TO_CIRCUIT(intro_circ)->timestamp_dirty = time(NULL);
- pathbias_count_use_attempt(intro_circ);
-
- status = 0;
- goto end;
- perm_err:
-
- if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
- circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
- }
- circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
- status = -2;
- goto end;
- tran_err:
- status = -1;
- end:
- memwipe(onion_address, 0, sizeof(onion_address));
- return status;
- }
- static void
- setup_intro_circ_auth_key(origin_circuit_t *circ)
- {
- const hs_descriptor_t *desc;
- const hs_desc_intro_point_t *ip;
- tor_assert(circ);
- desc = hs_cache_lookup_as_client(&circ->hs_ident->identity_pk);
- if (BUG(desc == NULL)) {
-
- goto end;
- }
-
- ip = find_desc_intro_point_by_legacy_id(
- circ->build_state->chosen_exit->identity_digest, desc);
- if (ip) {
-
- ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
- &ip->auth_key_cert->signed_key);
- goto end;
- }
-
- tor_assert_nonfatal_unreached();
- end:
- return;
- }
- static void
- client_intro_circ_has_opened(origin_circuit_t *circ)
- {
- tor_assert(circ);
- tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
- log_info(LD_REND, "Introduction circuit %u has opened. Attaching streams.",
- (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
-
- setup_intro_circ_auth_key(circ);
- connection_ap_attach_pending(1);
- }
- static void
- client_rendezvous_circ_has_opened(origin_circuit_t *circ)
- {
- tor_assert(circ);
- tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
- const extend_info_t *rp_ei = circ->build_state->chosen_exit;
-
- if (rp_ei) {
- const node_t *rp_node = node_get_by_id(rp_ei->identity_digest);
- if (rp_node) {
- if (BUG(!node_supports_v3_rendezvous_point(rp_node))) {
- return;
- }
- }
- }
- log_info(LD_REND, "Rendezvous circuit has opened to %s.",
- safe_str_client(extend_info_describe(rp_ei)));
-
- hs_circ_send_establish_rendezvous(circ);
-
- if (!TO_CIRCUIT(circ)->marked_for_close) {
- hs_circuitmap_register_rend_circ_client_side(circ,
- circ->hs_ident->rendezvous_cookie);
- }
- }
- STATIC extend_info_t *
- desc_intro_point_to_extend_info(const hs_desc_intro_point_t *ip)
- {
- extend_info_t *ei;
- smartlist_t *lspecs = smartlist_new();
- tor_assert(ip);
-
- SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
- const hs_desc_link_specifier_t *, desc_lspec) {
- link_specifier_t *lspec = hs_desc_lspec_to_trunnel(desc_lspec);
- smartlist_add(lspecs, lspec);
- } SMARTLIST_FOREACH_END(desc_lspec);
-
- ei = hs_get_extend_info_from_lspecs(lspecs, &ip->onion_key, 0);
- SMARTLIST_FOREACH(lspecs, link_specifier_t *, ls, link_specifier_free(ls));
- smartlist_free(lspecs);
- return ei;
- }
- static int
- intro_point_is_usable(const ed25519_public_key_t *service_pk,
- const hs_desc_intro_point_t *ip)
- {
- const hs_cache_intro_state_t *state;
- tor_assert(service_pk);
- tor_assert(ip);
- state = hs_cache_client_intro_state_find(service_pk,
- &ip->auth_key_cert->signed_key);
- if (state == NULL) {
-
- goto usable;
- }
- if (state->error) {
- log_info(LD_REND, "Intro point with auth key %s had an error. Not usable",
- safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
- goto not_usable;
- }
- if (state->timed_out) {
- log_info(LD_REND, "Intro point with auth key %s timed out. Not usable",
- safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
- goto not_usable;
- }
- if (state->unreachable_count >= MAX_INTRO_POINT_REACHABILITY_FAILURES) {
- log_info(LD_REND, "Intro point with auth key %s unreachable. Not usable",
- safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
- goto not_usable;
- }
- usable:
- return 1;
- not_usable:
- return 0;
- }
- STATIC extend_info_t *
- client_get_random_intro(const ed25519_public_key_t *service_pk)
- {
- extend_info_t *ei = NULL, *ei_excluded = NULL;
- smartlist_t *usable_ips = NULL;
- const hs_descriptor_t *desc;
- const hs_desc_encrypted_data_t *enc_data;
- const or_options_t *options = get_options();
-
- char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
- tor_assert(service_pk);
- desc = hs_cache_lookup_as_client(service_pk);
-
- hs_build_address(service_pk,
- desc ? desc->plaintext_data.version : HS_VERSION_THREE,
- onion_address);
- if (desc == NULL || !hs_client_any_intro_points_usable(service_pk,
- desc)) {
- log_info(LD_REND, "Unable to randomly select an introduction point "
- "for service %s because descriptor %s. We can't connect.",
- safe_str_client(onion_address),
- (desc) ? "doesn't have any usable intro points"
- : "is missing (assuming v3 onion address)");
- goto end;
- }
- enc_data = &desc->encrypted_data;
- usable_ips = smartlist_new();
- smartlist_add_all(usable_ips, enc_data->intro_points);
- while (smartlist_len(usable_ips) != 0) {
- int idx;
- const hs_desc_intro_point_t *ip;
-
- idx = crypto_rand_int(smartlist_len(usable_ips));
- ip = smartlist_get(usable_ips, idx);
- smartlist_del(usable_ips, idx);
-
- if (!intro_point_is_usable(service_pk, ip)) {
- continue;
- }
-
- ei = desc_intro_point_to_extend_info(ip);
- if (ei == NULL) {
-
- log_info(LD_REND, "Unable to select introduction point with auth key %s "
- "for service %s, because we could not extend to it.",
- safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)),
- safe_str_client(onion_address));
- continue;
- }
-
- if (routerset_contains_extendinfo(options->ExcludeNodes, ei)) {
-
- if (ei_excluded) {
-
- extend_info_free(ei_excluded);
- }
- ei_excluded = ei;
- continue;
- }
-
- goto end;
- }
-
- ei = ei_excluded;
- if (options->StrictNodes) {
- log_warn(LD_REND, "Every introduction point for service %s is in the "
- "ExcludeNodes set and StrictNodes is set. We can't connect.",
- safe_str_client(onion_address));
- extend_info_free(ei);
- ei = NULL;
- } else {
- log_fn(LOG_PROTOCOL_WARN, LD_REND, "Every introduction point for service "
- "%s is unusable or we can't extend to it. We can't connect.",
- safe_str_client(onion_address));
- }
- end:
- smartlist_free(usable_ips);
- memwipe(onion_address, 0, sizeof(onion_address));
- return ei;
- }
- static int
- close_or_reextend_intro_circ(origin_circuit_t *intro_circ)
- {
- int ret = -1;
- const hs_descriptor_t *desc;
- origin_circuit_t *rend_circ;
- tor_assert(intro_circ);
- desc = hs_cache_lookup_as_client(&intro_circ->hs_ident->identity_pk);
- if (BUG(desc == NULL)) {
-
- goto close;
- }
-
- if (!hs_client_any_intro_points_usable(&intro_circ->hs_ident->identity_pk,
- desc)) {
- goto close;
- }
-
- if (hs_client_reextend_intro_circuit(intro_circ) < 0) {
- goto close;
- }
-
- ret = 0;
- goto end;
- close:
-
- if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
- circuit_change_purpose(TO_CIRCUIT(intro_circ),
- CIRCUIT_PURPOSE_C_INTRODUCE_ACKED);
- circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
- }
-
- rend_circ = hs_circuitmap_get_rend_circ_client_side(
- intro_circ->hs_ident->rendezvous_cookie);
-
- if (rend_circ) {
- circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_FINISHED);
- }
- end:
- return ret;
- }
- static void
- handle_introduce_ack_success(origin_circuit_t *intro_circ)
- {
- origin_circuit_t *rend_circ = NULL;
- tor_assert(intro_circ);
- log_info(LD_REND, "Received INTRODUCE_ACK ack! Informing rendezvous");
-
- uint8_t *rendezvous_cookie = intro_circ->hs_ident->rendezvous_cookie;
- rend_circ =
- hs_circuitmap_get_established_rend_circ_client_side(rendezvous_cookie);
- if (rend_circ == NULL) {
- log_warn(LD_REND, "Can't find any rendezvous circuit. Stopping");
- goto end;
- }
- assert_circ_anonymity_ok(rend_circ, get_options());
-
- if (TO_CIRCUIT(rend_circ)->purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
- goto end;
- }
- circuit_change_purpose(TO_CIRCUIT(rend_circ),
- CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED);
-
- TO_CIRCUIT(rend_circ)->timestamp_dirty = time(NULL);
- end:
-
- circuit_change_purpose(TO_CIRCUIT(intro_circ),
- CIRCUIT_PURPOSE_C_INTRODUCE_ACKED);
- circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
-
- return;
- }
- static void
- handle_introduce_ack_bad(origin_circuit_t *circ, int status)
- {
- tor_assert(circ);
- log_info(LD_REND, "Received INTRODUCE_ACK nack by %s. Reason: %u",
- safe_str_client(extend_info_describe(circ->build_state->chosen_exit)),
- status);
-
- circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_INTRODUCING);
-
- hs_cache_client_intro_state_note(&circ->hs_ident->identity_pk,
- &circ->hs_ident->intro_auth_pk,
- INTRO_POINT_FAILURE_GENERIC);
- }
- static int
- handle_introduce_ack(origin_circuit_t *circ, const uint8_t *payload,
- size_t payload_len)
- {
- int status, ret = -1;
- tor_assert(circ);
- tor_assert(circ->build_state);
- tor_assert(circ->build_state->chosen_exit);
- assert_circ_anonymity_ok(circ, get_options());
- tor_assert(payload);
- status = hs_cell_parse_introduce_ack(payload, payload_len);
- switch (status) {
- case HS_CELL_INTRO_ACK_SUCCESS:
- ret = 0;
- handle_introduce_ack_success(circ);
- goto end;
- case HS_CELL_INTRO_ACK_FAILURE:
- case HS_CELL_INTRO_ACK_BADFMT:
- case HS_CELL_INTRO_ACK_NORELAY:
- handle_introduce_ack_bad(circ, status);
-
- ret = close_or_reextend_intro_circ(circ);
- break;
- default:
- log_info(LD_PROTOCOL, "Unknown INTRODUCE_ACK status code %u from %s",
- status,
- safe_str_client(extend_info_describe(circ->build_state->chosen_exit)));
- break;
- }
- end:
- return ret;
- }
- STATIC int
- handle_rendezvous2(origin_circuit_t *circ, const uint8_t *payload,
- size_t payload_len)
- {
- int ret = -1;
- curve25519_public_key_t server_pk;
- uint8_t auth_mac[DIGEST256_LEN] = {0};
- uint8_t handshake_info[CURVE25519_PUBKEY_LEN + sizeof(auth_mac)] = {0};
- hs_ntor_rend_cell_keys_t keys;
- const hs_ident_circuit_t *ident;
- tor_assert(circ);
- tor_assert(payload);
-
- ident = circ->hs_ident;
- tor_assert(ident);
- if (hs_cell_parse_rendezvous2(payload, payload_len, handshake_info,
- sizeof(handshake_info)) < 0) {
- goto err;
- }
-
- memcpy(&server_pk, handshake_info, CURVE25519_PUBKEY_LEN);
- memcpy(auth_mac, handshake_info + CURVE25519_PUBKEY_LEN, sizeof(auth_mac));
-
- if (hs_ntor_client_get_rendezvous1_keys(&ident->intro_auth_pk,
- &ident->rendezvous_client_kp,
- &ident->intro_enc_pk, &server_pk,
- &keys) < 0) {
- log_info(LD_REND, "Unable to compute the rendezvous keys.");
- goto err;
- }
-
- if (!hs_ntor_client_rendezvous2_mac_is_good(&keys, auth_mac)) {
- log_info(LD_REND, "Invalid MAC in RENDEZVOUS2. Rejecting cell.");
- goto err;
- }
-
- if (hs_circuit_setup_e2e_rend_circ(circ, keys.ntor_key_seed,
- sizeof(keys.ntor_key_seed), 0) < 0) {
- log_info(LD_REND, "Unable to setup the e2e encryption.");
- goto err;
- }
-
- ret = 0;
- goto end;
- err:
- circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
- end:
- memwipe(&keys, 0, sizeof(keys));
- return ret;
- }
- static unsigned int
- can_client_refetch_desc(const ed25519_public_key_t *identity_pk,
- hs_client_fetch_status_t *status_out)
- {
- hs_client_fetch_status_t status;
- tor_assert(identity_pk);
-
- if (!get_options()->FetchHidServDescriptors) {
- log_warn(LD_REND, "We received an onion address for a hidden service "
- "descriptor but we are configured to not fetch.");
- status = HS_CLIENT_FETCH_NOT_ALLOWED;
- goto cannot;
- }
-
- if (!networkstatus_get_live_consensus(approx_time())) {
- log_info(LD_REND, "Can't fetch descriptor for service %s because we "
- "are missing a live consensus. Stalling connection.",
- safe_str_client(ed25519_fmt(identity_pk)));
- status = HS_CLIENT_FETCH_MISSING_INFO;
- goto cannot;
- }
- if (!router_have_minimum_dir_info()) {
- log_info(LD_REND, "Can't fetch descriptor for service %s because we "
- "dont have enough descriptors. Stalling connection.",
- safe_str_client(ed25519_fmt(identity_pk)));
- status = HS_CLIENT_FETCH_MISSING_INFO;
- goto cannot;
- }
-
- {
- const hs_descriptor_t *cached_desc = NULL;
- cached_desc = hs_cache_lookup_as_client(identity_pk);
- if (cached_desc && hs_client_any_intro_points_usable(identity_pk,
- cached_desc)) {
- log_info(LD_GENERAL, "We would fetch a v3 hidden service descriptor "
- "but we already have a usable descriptor.");
- status = HS_CLIENT_FETCH_HAVE_DESC;
- goto cannot;
- }
- }
-
- if (directory_request_is_pending(identity_pk)) {
- log_info(LD_REND, "Already a pending directory request. Waiting on it.");
- status = HS_CLIENT_FETCH_PENDING;
- goto cannot;
- }
-
- return 1;
- cannot:
- if (status_out) {
- *status_out = status;
- }
- return 0;
- }
- void
- hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
- {
- tor_assert(connection_edge_is_rendezvous_stream(conn));
- if (BUG(conn->rend_data && conn->hs_ident)) {
- log_warn(LD_BUG, "Stream had both rend_data and hs_ident..."
- "Prioritizing hs_ident");
- }
- if (conn->hs_ident) {
- note_connection_attempt_succeeded(conn->hs_ident);
- return;
- } else if (conn->rend_data) {
- rend_client_note_connection_attempt_ended(conn->rend_data);
- return;
- }
- }
- int
- hs_client_decode_descriptor(const char *desc_str,
- const ed25519_public_key_t *service_identity_pk,
- hs_descriptor_t **desc)
- {
- int ret;
- uint8_t subcredential[DIGEST256_LEN];
- ed25519_public_key_t blinded_pubkey;
- tor_assert(desc_str);
- tor_assert(service_identity_pk);
- tor_assert(desc);
-
- {
- uint64_t current_time_period = hs_get_time_period_num(0);
- hs_build_blinded_pubkey(service_identity_pk, NULL, 0, current_time_period,
- &blinded_pubkey);
- hs_get_subcredential(service_identity_pk, &blinded_pubkey, subcredential);
- }
-
- ret = hs_desc_decode_descriptor(desc_str, subcredential, desc);
- memwipe(subcredential, 0, sizeof(subcredential));
- if (ret < 0) {
- log_warn(LD_GENERAL, "Could not parse received descriptor as client.");
- if (get_options()->SafeLogging_ == SAFELOG_SCRUB_NONE) {
- log_warn(LD_GENERAL, "%s", escaped(desc_str));
- }
- goto err;
- }
-
- tor_cert_t *cert = (*desc)->plaintext_data.signing_key_cert;
- if (tor_cert_checksig(cert,
- &blinded_pubkey, approx_time()) < 0) {
- log_warn(LD_GENERAL, "Descriptor signing key certificate signature "
- "doesn't validate with computed blinded key: %s",
- tor_cert_describe_signature_status(cert));
- goto err;
- }
- return 0;
- err:
- return -1;
- }
- int
- hs_client_any_intro_points_usable(const ed25519_public_key_t *service_pk,
- const hs_descriptor_t *desc)
- {
- tor_assert(service_pk);
- tor_assert(desc);
- SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
- const hs_desc_intro_point_t *, ip) {
- if (intro_point_is_usable(service_pk, ip)) {
- goto usable;
- }
- } SMARTLIST_FOREACH_END(ip);
- return 0;
- usable:
- return 1;
- }
- int
- hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
- {
- hs_client_fetch_status_t status;
- tor_assert(identity_pk);
- if (!can_client_refetch_desc(identity_pk, &status)) {
- return status;
- }
-
- status = fetch_v3_desc(identity_pk);
- if (fetch_status_should_close_socks(status)) {
- close_all_socks_conns_waiting_for_desc(identity_pk, status,
- END_STREAM_REASON_RESOLVEFAILED);
-
- purge_hid_serv_request(identity_pk);
- }
- return status;
- }
- int
- hs_client_send_introduce1(origin_circuit_t *intro_circ,
- origin_circuit_t *rend_circ)
- {
- return (intro_circ->hs_ident) ? send_introduce1(intro_circ, rend_circ) :
- rend_client_send_introduction(intro_circ,
- rend_circ);
- }
- void
- hs_client_circuit_has_opened(origin_circuit_t *circ)
- {
- tor_assert(circ);
-
- switch (TO_CIRCUIT(circ)->purpose) {
- case CIRCUIT_PURPOSE_C_INTRODUCING:
- if (circ->hs_ident) {
- client_intro_circ_has_opened(circ);
- } else {
- rend_client_introcirc_has_opened(circ);
- }
- break;
- case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
- if (circ->hs_ident) {
- client_rendezvous_circ_has_opened(circ);
- } else {
- rend_client_rendcirc_has_opened(circ);
- }
- break;
- default:
- tor_assert_nonfatal_unreached();
- }
- }
- int
- hs_client_receive_rendezvous_acked(origin_circuit_t *circ,
- const uint8_t *payload, size_t payload_len)
- {
- tor_assert(circ);
- tor_assert(payload);
- (void) payload_len;
- if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
- log_warn(LD_PROTOCOL, "Got a RENDEZVOUS_ESTABLISHED but we were not "
- "expecting one. Closing circuit.");
- goto err;
- }
- log_info(LD_REND, "Received an RENDEZVOUS_ESTABLISHED. This circuit is "
- "now ready for rendezvous.");
- circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_REND_READY);
-
- TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
-
- pathbias_mark_use_success(circ);
-
- connection_ap_attach_pending(1);
- return 0;
- err:
- circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
- return -1;
- }
- void
- hs_client_desc_has_arrived(const hs_ident_dir_conn_t *ident)
- {
- time_t now = time(NULL);
- smartlist_t *conns = NULL;
- tor_assert(ident);
- conns = connection_list_by_type_state(CONN_TYPE_AP,
- AP_CONN_STATE_RENDDESC_WAIT);
- SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
- const hs_descriptor_t *desc;
- entry_connection_t *entry_conn = TO_ENTRY_CONN(base_conn);
- const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
-
- if (!edge_conn->hs_ident ||
- !ed25519_pubkey_eq(&ident->identity_pk,
- &edge_conn->hs_ident->identity_pk)) {
- continue;
- }
- assert_connection_ok(base_conn, now);
-
- desc = hs_cache_lookup_as_client(&ident->identity_pk);
- if (BUG(desc == NULL)) {
- goto end;
- }
- if (!hs_client_any_intro_points_usable(&ident->identity_pk, desc)) {
- log_info(LD_REND, "Hidden service descriptor is unusable. "
- "Closing streams.");
- connection_mark_unattached_ap(entry_conn,
- END_STREAM_REASON_RESOLVEFAILED);
-
- note_connection_attempt_succeeded(edge_conn->hs_ident);
- goto end;
- }
- log_info(LD_REND, "Descriptor has arrived. Launching circuits.");
-
- base_conn->timestamp_created = now;
- base_conn->timestamp_lastread = now;
- base_conn->timestamp_lastwritten = now;
-
- base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
- connection_ap_mark_as_pending_circuit(entry_conn);
- } SMARTLIST_FOREACH_END(base_conn);
- end:
-
- smartlist_free(conns);
- }
- extend_info_t *
- hs_client_get_random_intro_from_edge(const edge_connection_t *edge_conn)
- {
- tor_assert(edge_conn);
- return (edge_conn->hs_ident) ?
- client_get_random_intro(&edge_conn->hs_ident->identity_pk) :
- rend_client_get_random_intro(edge_conn->rend_data);
- }
- int
- hs_client_receive_introduce_ack(origin_circuit_t *circ,
- const uint8_t *payload, size_t payload_len)
- {
- int ret = -1;
- tor_assert(circ);
- tor_assert(payload);
- if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
- log_warn(LD_PROTOCOL, "Unexpected INTRODUCE_ACK on circuit %u.",
- (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
- circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
- goto end;
- }
- ret = (circ->hs_ident) ? handle_introduce_ack(circ, payload, payload_len) :
- rend_client_introduction_acked(circ, payload,
- payload_len);
-
- pathbias_mark_use_success(circ);
- end:
- return ret;
- }
- int
- hs_client_receive_rendezvous2(origin_circuit_t *circ,
- const uint8_t *payload, size_t payload_len)
- {
- int ret = -1;
- tor_assert(circ);
- tor_assert(payload);
-
- if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
- TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
- log_warn(LD_PROTOCOL, "Unexpected RENDEZVOUS2 cell on circuit %u. "
- "Closing circuit.",
- (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
- circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
- goto end;
- }
- log_info(LD_REND, "Got RENDEZVOUS2 cell from hidden service on circuit %u.",
- TO_CIRCUIT(circ)->n_circ_id);
- ret = (circ->hs_ident) ? handle_rendezvous2(circ, payload, payload_len) :
- rend_client_receive_rendezvous(circ, payload,
- payload_len);
- end:
- return ret;
- }
- int
- hs_client_reextend_intro_circuit(origin_circuit_t *circ)
- {
- int ret = -1;
- extend_info_t *ei;
- tor_assert(circ);
- ei = (circ->hs_ident) ?
- client_get_random_intro(&circ->hs_ident->identity_pk) :
- rend_client_get_random_intro(circ->rend_data);
- if (ei == NULL) {
- log_warn(LD_REND, "No usable introduction points left. Closing.");
- circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
- goto end;
- }
- if (circ->remaining_relay_early_cells) {
- log_info(LD_REND, "Re-extending circ %u, this time to %s.",
- (unsigned int) TO_CIRCUIT(circ)->n_circ_id,
- safe_str_client(extend_info_describe(ei)));
- ret = circuit_extend_to_new_exit(circ, ei);
- if (ret == 0) {
-
- TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
- }
- } else {
- log_info(LD_REND, "Closing intro circ %u (out of RELAY_EARLY cells).",
- (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
- circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
-
- ret = 0;
- }
- end:
- extend_info_free(ei);
- return ret;
- }
- void
- hs_client_free_all(void)
- {
-
- hs_purge_last_hid_serv_requests();
- }
- void
- hs_client_purge_state(void)
- {
-
- rend_client_purge_state();
-
- cancel_descriptor_fetches();
-
- hs_cache_client_intro_state_purge();
-
- hs_cache_purge_as_client();
-
- hs_purge_last_hid_serv_requests();
- log_info(LD_REND, "Hidden service client state has been purged.");
- }
- void
- hs_client_dir_info_changed(void)
- {
-
- retry_all_socks_conn_waiting_for_desc();
- }
|