123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- #define HS_INTROPOINT_PRIVATE
- #include "or.h"
- #include "config.h"
- #include "circuitlist.h"
- #include "circuituse.h"
- #include "relay.h"
- #include "rendmid.h"
- #include "rephist.h"
- #include "ed25519_cert.h"
- #include "hs/cell_common.h"
- #include "hs/cell_establish_intro.h"
- #include "hs/cell_introduce1.h"
- #include "hs_circuitmap.h"
- #include "hs_descriptor.h"
- #include "hs_intropoint.h"
- #include "hs_common.h"
- STATIC void
- get_auth_key_from_cell(ed25519_public_key_t *auth_key_out,
- unsigned int cell_type, const void *cell)
- {
- size_t auth_key_len;
- const uint8_t *key_array;
- tor_assert(auth_key_out);
- tor_assert(cell);
- switch (cell_type) {
- case RELAY_COMMAND_ESTABLISH_INTRO:
- {
- const trn_cell_establish_intro_t *c_cell = cell;
- key_array = trn_cell_establish_intro_getconstarray_auth_key(c_cell);
- auth_key_len = trn_cell_establish_intro_getlen_auth_key(c_cell);
- break;
- }
- case RELAY_COMMAND_INTRODUCE1:
- {
- const trn_cell_introduce1_t *c_cell = cell;
- key_array = trn_cell_introduce1_getconstarray_auth_key(cell);
- auth_key_len = trn_cell_introduce1_getlen_auth_key(c_cell);
- break;
- }
- default:
-
- tor_assert(0);
- }
- tor_assert(key_array);
- tor_assert(auth_key_len == sizeof(auth_key_out->pubkey));
- memcpy(auth_key_out->pubkey, key_array, auth_key_len);
- }
- STATIC int
- verify_establish_intro_cell(const trn_cell_establish_intro_t *cell,
- const uint8_t *circuit_key_material,
- size_t circuit_key_material_len)
- {
-
- if (BUG(cell->auth_key_type != HS_INTRO_AUTH_KEY_TYPE_ED25519)) {
- return -1;
- }
-
- if (trn_cell_establish_intro_getlen_auth_key(cell) != ED25519_PUBKEY_LEN ||
- trn_cell_establish_intro_get_auth_key_len(cell) != ED25519_PUBKEY_LEN) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "ESTABLISH_INTRO auth key length is invalid");
- return -1;
- }
- const uint8_t *msg = cell->start_cell;
-
- {
- ed25519_signature_t sig_struct;
- const uint8_t *sig_array =
- trn_cell_establish_intro_getconstarray_sig(cell);
-
- if (trn_cell_establish_intro_getlen_sig(cell) != sizeof(sig_struct.sig) ||
- trn_cell_establish_intro_get_sig_len(cell) != sizeof(sig_struct.sig)) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "ESTABLISH_INTRO sig len is invalid");
- return -1;
- }
-
- memcpy(sig_struct.sig, sig_array, cell->sig_len);
- ed25519_public_key_t auth_key;
- get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO, cell);
- const size_t sig_msg_len = cell->end_sig_fields - msg;
- int sig_mismatch = ed25519_checksig_prefixed(&sig_struct,
- msg, sig_msg_len,
- ESTABLISH_INTRO_SIG_PREFIX,
- &auth_key);
- if (sig_mismatch) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "ESTABLISH_INTRO signature not as expected");
- return -1;
- }
- }
-
- {
- const size_t auth_msg_len = cell->end_mac_fields - msg;
- uint8_t mac[DIGEST256_LEN];
- crypto_mac_sha3_256(mac, sizeof(mac),
- circuit_key_material, circuit_key_material_len,
- msg, auth_msg_len);
- if (tor_memneq(mac, cell->handshake_mac, sizeof(mac))) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "ESTABLISH_INTRO handshake_auth not as expected");
- return -1;
- }
- }
- return 0;
- }
- MOCK_IMPL(int,
- hs_intro_send_intro_established_cell,(or_circuit_t *circ))
- {
- int ret;
- uint8_t *encoded_cell = NULL;
- ssize_t encoded_len, result_len;
- trn_cell_intro_established_t *cell;
- trn_cell_extension_t *ext;
- tor_assert(circ);
-
- cell = trn_cell_intro_established_new();
- ext = trn_cell_extension_new();
- trn_cell_extension_set_num(ext, 0);
- trn_cell_intro_established_set_extensions(cell, ext);
-
- encoded_len = trn_cell_intro_established_encoded_len(cell);
- tor_assert(encoded_len > 0);
- encoded_cell = tor_malloc_zero(encoded_len);
- result_len = trn_cell_intro_established_encode(encoded_cell, encoded_len,
- cell);
- tor_assert(encoded_len == result_len);
- ret = relay_send_command_from_edge(0, TO_CIRCUIT(circ),
- RELAY_COMMAND_INTRO_ESTABLISHED,
- (char *) encoded_cell, encoded_len,
- NULL);
-
- trn_cell_intro_established_free(cell);
- tor_free(encoded_cell);
- return ret;
- }
- static int
- handle_verified_establish_intro_cell(or_circuit_t *circ,
- const trn_cell_establish_intro_t *parsed_cell)
- {
-
- ed25519_public_key_t auth_key;
- get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO,
- parsed_cell);
-
- if (hs_intro_send_intro_established_cell(circ)) {
- log_warn(LD_PROTOCOL, "Couldn't send INTRO_ESTABLISHED cell.");
- return -1;
- }
-
- hs_circuitmap_register_intro_circ_v3_relay_side(circ, &auth_key);
-
- circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
- return 0;
- }
- static int
- handle_establish_intro(or_circuit_t *circ, const uint8_t *request,
- size_t request_len)
- {
- int cell_ok, retval = -1;
- trn_cell_establish_intro_t *parsed_cell = NULL;
- tor_assert(circ);
- tor_assert(request);
- log_info(LD_REND, "Received an ESTABLISH_INTRO request on circuit %" PRIu32,
- circ->p_circ_id);
-
- if (!hs_intro_circuit_is_suitable_for_establish_intro(circ)) {
- goto err;
- }
-
- ssize_t parsing_result = trn_cell_establish_intro_parse(&parsed_cell,
- request, request_len);
- if (parsing_result < 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Rejecting %s ESTABLISH_INTRO cell.",
- parsing_result == -1 ? "invalid" : "truncated");
- goto err;
- }
- cell_ok = verify_establish_intro_cell(parsed_cell,
- (uint8_t *) circ->rend_circ_nonce,
- sizeof(circ->rend_circ_nonce));
- if (cell_ok < 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Failed to verify ESTABLISH_INTRO cell.");
- goto err;
- }
-
- cell_ok = handle_verified_establish_intro_cell(circ, parsed_cell);
- if (cell_ok < 0) {
- goto err;
- }
-
- retval = 0;
- goto done;
- err:
-
- if (!TO_CIRCUIT(circ)->marked_for_close) {
- circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
- }
- done:
- trn_cell_establish_intro_free(parsed_cell);
- return retval;
- }
- static int
- circuit_is_suitable_intro_point(const or_circuit_t *circ,
- const char *log_cell_type_str)
- {
- tor_assert(circ);
- tor_assert(log_cell_type_str);
-
- if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Rejecting %s on non-OR circuit.", log_cell_type_str);
- return 0;
- }
- if (circ->base_.n_chan) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Rejecting %s on non-edge circuit.", log_cell_type_str);
- return 0;
- }
-
- return 1;
- }
- int
- hs_intro_circuit_is_suitable_for_establish_intro(const or_circuit_t *circ)
- {
- return circuit_is_suitable_intro_point(circ, "ESTABLISH_INTRO");
- }
- int
- hs_intro_received_establish_intro(or_circuit_t *circ, const uint8_t *request,
- size_t request_len)
- {
- tor_assert(circ);
- tor_assert(request);
- if (request_len == 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Empty ESTABLISH_INTRO cell.");
- goto err;
- }
-
- const uint8_t first_byte = request[0];
- switch (first_byte) {
- case HS_INTRO_AUTH_KEY_TYPE_LEGACY0:
- case HS_INTRO_AUTH_KEY_TYPE_LEGACY1:
- return rend_mid_establish_intro_legacy(circ, request, request_len);
- case HS_INTRO_AUTH_KEY_TYPE_ED25519:
- return handle_establish_intro(circ, request, request_len);
- default:
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Unrecognized AUTH_KEY_TYPE %u.", first_byte);
- goto err;
- }
- err:
- circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
- return -1;
- }
- static int
- send_introduce_ack_cell(or_circuit_t *circ, hs_intro_ack_status_t status)
- {
- int ret = -1;
- uint8_t *encoded_cell = NULL;
- ssize_t encoded_len, result_len;
- trn_cell_introduce_ack_t *cell;
- trn_cell_extension_t *ext;
- tor_assert(circ);
-
- cell = trn_cell_introduce_ack_new();
- ret = trn_cell_introduce_ack_set_status(cell, status);
-
- ext = trn_cell_extension_new();
- trn_cell_extension_set_num(ext, 0);
- trn_cell_introduce_ack_set_extensions(cell, ext);
-
- tor_assert(ret == 0);
-
- encoded_len = trn_cell_introduce_ack_encoded_len(cell);
- tor_assert(encoded_len > 0);
- encoded_cell = tor_malloc_zero(encoded_len);
- result_len = trn_cell_introduce_ack_encode(encoded_cell, encoded_len, cell);
- tor_assert(encoded_len == result_len);
- ret = relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
- RELAY_COMMAND_INTRODUCE_ACK,
- (char *) encoded_cell, encoded_len,
- NULL);
-
- trn_cell_introduce_ack_free(cell);
- tor_free(encoded_cell);
- return ret;
- }
- STATIC int
- validate_introduce1_parsed_cell(const trn_cell_introduce1_t *cell)
- {
- size_t legacy_key_id_len;
- const uint8_t *legacy_key_id;
- tor_assert(cell);
-
- legacy_key_id_len = trn_cell_introduce1_getlen_legacy_key_id(cell);
- legacy_key_id = trn_cell_introduce1_getconstarray_legacy_key_id(cell);
- if (BUG(!tor_mem_is_zero((char *) legacy_key_id, legacy_key_id_len))) {
- goto invalid;
- }
-
- if (trn_cell_introduce1_get_auth_key_type(cell) !=
- HS_INTRO_AUTH_KEY_TYPE_ED25519) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Rejecting invalid INTRODUCE1 cell auth key type. "
- "Responding with NACK.");
- goto invalid;
- }
- if (trn_cell_introduce1_get_auth_key_len(cell) != ED25519_PUBKEY_LEN ||
- trn_cell_introduce1_getlen_auth_key(cell) != ED25519_PUBKEY_LEN) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Rejecting invalid INTRODUCE1 cell auth key length. "
- "Responding with NACK.");
- goto invalid;
- }
- if (trn_cell_introduce1_getlen_encrypted(cell) == 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Rejecting invalid INTRODUCE1 cell encrypted length. "
- "Responding with NACK.");
- goto invalid;
- }
- return 0;
- invalid:
- return -1;
- }
- STATIC int
- handle_introduce1(or_circuit_t *client_circ, const uint8_t *request,
- size_t request_len)
- {
- int ret = -1;
- or_circuit_t *service_circ;
- trn_cell_introduce1_t *parsed_cell;
- hs_intro_ack_status_t status = HS_INTRO_ACK_STATUS_SUCCESS;
- tor_assert(client_circ);
- tor_assert(request);
-
- ssize_t cell_size = trn_cell_introduce1_parse(&parsed_cell, request,
- request_len);
- if (cell_size < 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Rejecting %s INTRODUCE1 cell. Responding with NACK.",
- cell_size == -1 ? "invalid" : "truncated");
-
- status = HS_INTRO_ACK_STATUS_BAD_FORMAT;
- goto send_ack;
- }
-
- if (validate_introduce1_parsed_cell(parsed_cell) < 0) {
-
- status = HS_INTRO_ACK_STATUS_BAD_FORMAT;
- goto send_ack;
- }
-
- {
- ed25519_public_key_t auth_key;
- get_auth_key_from_cell(&auth_key, RELAY_COMMAND_INTRODUCE1, parsed_cell);
- service_circ = hs_circuitmap_get_intro_circ_v3_relay_side(&auth_key);
- if (service_circ == NULL) {
- char b64_key[ED25519_BASE64_LEN + 1];
- ed25519_public_to_base64(b64_key, &auth_key);
- log_info(LD_REND, "No intro circuit found for INTRODUCE1 cell "
- "with auth key %s from circuit %" PRIu32 ". "
- "Responding with NACK.",
- safe_str(b64_key), client_circ->p_circ_id);
-
- status = HS_INTRO_ACK_STATUS_UNKNOWN_ID;
- goto send_ack;
- }
- }
-
- if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(service_circ),
- RELAY_COMMAND_INTRODUCE2,
- (char *) request, request_len, NULL)) {
- log_warn(LD_PROTOCOL, "Unable to send INTRODUCE2 cell to the service.");
-
- status = HS_INTRO_ACK_STATUS_CANT_RELAY;
- goto send_ack;
- }
-
- status = HS_INTRO_ACK_STATUS_SUCCESS;
- ret = 0;
- send_ack:
-
- if (send_introduce_ack_cell(client_circ, status) < 0) {
- log_warn(LD_PROTOCOL, "Unable to send an INTRODUCE ACK status %d "
- "to client.", status);
-
- goto done;
- }
- if (status != HS_INTRO_ACK_STATUS_SUCCESS) {
-
- circuit_mark_for_close(TO_CIRCUIT(client_circ), END_CIRC_REASON_INTERNAL);
- }
- done:
- trn_cell_introduce1_free(parsed_cell);
- return ret;
- }
- STATIC int
- introduce1_cell_is_legacy(const uint8_t *request)
- {
- tor_assert(request);
-
- if (!tor_mem_is_zero((const char *) request, DIGEST_LEN)) {
-
- return 1;
- }
-
- return 0;
- }
- STATIC int
- circuit_is_suitable_for_introduce1(const or_circuit_t *circ)
- {
- tor_assert(circ);
-
- if (!circuit_is_suitable_intro_point(circ, "INTRODUCE1")) {
- return 0;
- }
- if (circ->already_received_introduce1) {
- log_fn(LOG_PROTOCOL_WARN, LD_REND,
- "Blocking multiple introductions on the same circuit. "
- "Someone might be trying to attack a hidden service through "
- "this relay.");
- return 0;
- }
- return 1;
- }
- int
- hs_intro_received_introduce1(or_circuit_t *circ, const uint8_t *request,
- size_t request_len)
- {
- int ret;
- tor_assert(circ);
- tor_assert(request);
-
- if (request_len < DIGEST_LEN) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Invalid INTRODUCE1 cell length.");
- goto err;
- }
-
- if (!circuit_is_suitable_for_introduce1(circ)) {
-
- goto err;
- }
-
- circ->already_received_introduce1 = 1;
-
- if (introduce1_cell_is_legacy(request)) {
-
- ret = rend_mid_introduce_legacy(circ, request, request_len);
- } else {
-
- ret = handle_introduce1(circ, request, request_len);
- }
- return ret;
- err:
- circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
- return -1;
- }
- void
- hs_intropoint_clear(hs_intropoint_t *ip)
- {
- if (ip == NULL) {
- return;
- }
- tor_cert_free(ip->auth_key_cert);
- SMARTLIST_FOREACH(ip->link_specifiers, hs_desc_link_specifier_t *, ls,
- hs_desc_link_specifier_free(ls));
- smartlist_free(ip->link_specifiers);
- memset(ip, 0, sizeof(hs_intropoint_t));
- }
|