Merge branch 'tor-github/pr/1000'

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2019-05-08 08:02:28 -04:00
commit 3885e7b44b
22 changed files with 414 additions and 260 deletions

3
changes/bug30236 Normal file
View File

@ -0,0 +1,3 @@
o Code simplification and refactoring:
- Refactor and encapsulate parts of the codebase that manipulate
crypt_path_t objects. Resolves issue 30236.

View File

@ -54,8 +54,8 @@ problem function-size /src/app/main/main.c:sandbox_init_filter() 291
problem function-size /src/app/main/main.c:run_tor_main_loop() 105
problem function-size /src/app/main/ntmain.c:nt_service_install() 125
problem include-count /src/app/main/shutdown.c 52
problem file-size /src/core/mainloop/connection.c 5559
problem include-count /src/core/mainloop/connection.c 61
problem file-size /src/core/mainloop/connection.c 5560
problem include-count /src/core/mainloop/connection.c 62
problem function-size /src/core/mainloop/connection.c:connection_free_minimal() 185
problem function-size /src/core/mainloop/connection.c:connection_listener_new() 328
problem function-size /src/core/mainloop/connection.c:connection_handle_listener_read() 161
@ -79,11 +79,11 @@ problem function-size /src/core/or/channeltls.c:channel_tls_process_netinfo_cell
problem function-size /src/core/or/channeltls.c:channel_tls_process_certs_cell() 246
problem function-size /src/core/or/channeltls.c:channel_tls_process_authenticate_cell() 202
problem file-size /src/core/or/circuitbuild.c 3061
problem include-count /src/core/or/circuitbuild.c 53
problem include-count /src/core/or/circuitbuild.c 54
problem function-size /src/core/or/circuitbuild.c:get_unique_circ_id_by_chan() 128
problem function-size /src/core/or/circuitbuild.c:circuit_extend() 147
problem function-size /src/core/or/circuitbuild.c:choose_good_exit_server_general() 206
problem include-count /src/core/or/circuitlist.c 54
problem include-count /src/core/or/circuitlist.c 55
problem function-size /src/core/or/circuitlist.c:HT_PROTOTYPE() 128
problem function-size /src/core/or/circuitlist.c:circuit_free_() 143
problem function-size /src/core/or/circuitlist.c:circuit_find_to_cannibalize() 102
@ -245,7 +245,7 @@ problem function-size /src/feature/rend/rendmid.c:rend_mid_establish_intro_legac
problem function-size /src/feature/rend/rendparse.c:rend_parse_v2_service_descriptor() 187
problem function-size /src/feature/rend/rendparse.c:rend_decrypt_introduction_points() 104
problem function-size /src/feature/rend/rendparse.c:rend_parse_introduction_points() 131
problem file-size /src/feature/rend/rendservice.c 4510
problem file-size /src/feature/rend/rendservice.c 4511
problem function-size /src/feature/rend/rendservice.c:rend_service_prune_list_impl_() 107
problem function-size /src/feature/rend/rendservice.c:rend_config_service() 164
problem function-size /src/feature/rend/rendservice.c:rend_service_load_auth_keys() 178

View File

@ -6,6 +6,7 @@
#include "core/or/or.h"
#include "core/or/circuitlist.h"
#include "core/or/crypt_path.h"
#include "app/config/config.h"
#include "lib/crypt_ops/crypto_cipher.h"
#include "lib/crypt_ops/crypto_util.h"
@ -21,7 +22,7 @@
/** Update digest from the payload of cell. Assign integrity part to
* cell.
*/
static void
void
relay_set_digest(crypto_digest_t *digest, cell_t *cell)
{
char integrity[4];
@ -85,7 +86,7 @@ relay_digest_matches(crypto_digest_t *digest, cell_t *cell)
*
* Note that we use the same operation for encrypting and for decrypting.
*/
static void
void
relay_crypt_one_payload(crypto_cipher_t *cipher, uint8_t *in)
{
crypto_cipher_crypt_inplace(cipher, (char*) in, CELL_PAYLOAD_SIZE);
@ -152,18 +153,18 @@ relay_decrypt_cell(circuit_t *circ, cell_t *cell,
tor_assert(thishop);
/* decrypt one layer */
relay_crypt_one_payload(thishop->crypto.b_crypto, cell->payload);
cpath_crypt_cell(thishop, cell->payload, true);
relay_header_unpack(&rh, cell->payload);
if (rh.recognized == 0) {
/* it's possibly recognized. have to check digest to be sure. */
if (relay_digest_matches(thishop->crypto.b_digest, cell)) {
if (relay_digest_matches(cpath_get_incoming_digest(thishop), cell)) {
*recognized = 1;
*layer_hint = thishop;
/* This cell is for us. Keep a record of this cell because we will
* use it in the next SENDME cell. */
if (sendme_circuit_cell_is_next(thishop->deliver_window)) {
sendme_circuit_record_inbound_cell(thishop);
cpath_sendme_circuit_record_inbound_cell(thishop);
}
return 0;
}
@ -210,14 +211,14 @@ relay_encrypt_cell_outbound(cell_t *cell,
crypt_path_t *layer_hint)
{
crypt_path_t *thishop; /* counter for repeated crypts */
relay_set_digest(layer_hint->crypto.f_digest, cell);
cpath_set_cell_forward_digest(layer_hint, cell);
thishop = layer_hint;
/* moving from farthest to nearest hop */
do {
tor_assert(thishop);
log_debug(LD_OR,"encrypting a layer of the relay cell.");
relay_crypt_one_payload(thishop->crypto.f_crypto, cell->payload);
cpath_crypt_cell(thishop, cell->payload, false);
thishop = thishop->prev;
} while (thishop != circ->cpath->prev);

View File

@ -29,6 +29,11 @@ void relay_crypto_assert_ok(const relay_crypto_t *crypto);
uint8_t *relay_crypto_get_sendme_digest(relay_crypto_t *crypto);
void relay_crypto_record_sendme_digest(relay_crypto_t *crypto);
void
relay_crypt_one_payload(crypto_cipher_t *cipher, uint8_t *in);
void
relay_set_digest(crypto_digest_t *digest, cell_t *cell);
#endif /* !defined(TOR_RELAY_CRYPTO_H) */

View File

@ -39,6 +39,7 @@ LIBTOR_APP_A_SOURCES = \
src/core/or/circuitpadding.c \
src/core/or/circuitstats.c \
src/core/or/circuituse.c \
src/core/or/crypt_path.c \
src/core/or/command.c \
src/core/or/connection_edge.c \
src/core/or/connection_or.c \
@ -247,6 +248,7 @@ noinst_HEADERS += \
src/core/or/connection_edge.h \
src/core/or/connection_or.h \
src/core/or/connection_st.h \
src/core/or/crypt_path.h \
src/core/or/cpath_build_state_st.h \
src/core/or/crypt_path_reference_st.h \
src/core/or/crypt_path_st.h \

View File

@ -82,6 +82,7 @@
#include "core/or/policies.h"
#include "core/or/reasons.h"
#include "core/or/relay.h"
#include "core/or/crypt_path.h"
#include "core/proto/proto_http.h"
#include "core/proto/proto_socks.h"
#include "feature/client/dnsserv.h"
@ -5330,7 +5331,7 @@ assert_connection_ok(connection_t *conn, time_t now)
tor_assert(entry_conn->socks_request->has_finished);
if (!conn->marked_for_close) {
tor_assert(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
assert_cpath_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
cpath_assert_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
}
}
}

View File

@ -51,6 +51,7 @@
#include "core/or/ocirc_event.h"
#include "core/or/policies.h"
#include "core/or/relay.h"
#include "core/or/crypt_path.h"
#include "feature/client/bridges.h"
#include "feature/client/circpathbias.h"
#include "feature/client/entrynodes.h"
@ -90,8 +91,6 @@ static channel_t * channel_connect_for_circuit(const tor_addr_t *addr,
static int circuit_deliver_create_cell(circuit_t *circ,
const create_cell_t *create_cell,
int relayed);
static crypt_path_t *onion_next_hop_in_cpath(crypt_path_t *cpath);
STATIC int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
static int circuit_send_first_onion_skin(origin_circuit_t *circ);
static int circuit_build_no_more_hops(origin_circuit_t *circ);
static int circuit_send_intermediate_onion_skin(origin_circuit_t *circ,
@ -547,7 +546,7 @@ circuit_handle_first_hop(origin_circuit_t *circ)
int should_launch = 0;
const or_options_t *options = get_options();
firsthop = onion_next_hop_in_cpath(circ->cpath);
firsthop = cpath_get_next_non_open_hop(circ->cpath);
tor_assert(firsthop);
tor_assert(firsthop->extend_info);
@ -948,7 +947,7 @@ circuit_send_next_onion_skin(origin_circuit_t *circ)
tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
tor_assert(circ->base_.state == CIRCUIT_STATE_BUILDING);
crypt_path_t *hop = onion_next_hop_in_cpath(circ->cpath);
crypt_path_t *hop = cpath_get_next_non_open_hop(circ->cpath);
circuit_build_times_handle_completed_hop(circ);
circpad_machine_event_circ_added_hop(circ);
@ -1360,34 +1359,6 @@ circuit_extend(cell_t *cell, circuit_t *circ)
return 0;
}
/** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in key_data.
*
* If <b>is_hs_v3</b> is set, this cpath will be used for next gen hidden
* service circuits and <b>key_data</b> must be at least
* HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN bytes in length.
*
* If <b>is_hs_v3</b> is not set, key_data must contain CPATH_KEY_MATERIAL_LEN
* bytes, which are used as follows:
* - 20 to initialize f_digest
* - 20 to initialize b_digest
* - 16 to key f_crypto
* - 16 to key b_crypto
*
* (If 'reverse' is true, then f_XX and b_XX are swapped.)
*
* Return 0 if init was successful, else -1 if it failed.
*/
int
circuit_init_cpath_crypto(crypt_path_t *cpath,
const char *key_data, size_t key_data_len,
int reverse, int is_hs_v3)
{
tor_assert(cpath);
return relay_crypto_init(&cpath->crypto, key_data, key_data_len, reverse,
is_hs_v3);
}
/** A "created" cell <b>reply</b> came back to us on circuit <b>circ</b>.
* (The body of <b>reply</b> varies depending on what sort of handshake
* this is.)
@ -1413,7 +1384,7 @@ circuit_finish_handshake(origin_circuit_t *circ,
if (circ->cpath->state == CPATH_STATE_AWAITING_KEYS) {
hop = circ->cpath;
} else {
hop = onion_next_hop_in_cpath(circ->cpath);
hop = cpath_get_next_non_open_hop(circ->cpath);
if (!hop) { /* got an extended when we're all done? */
log_warn(LD_PROTOCOL,"got extended when circ already built? Closing.");
return - END_CIRC_REASON_TORPROTOCOL;
@ -1437,7 +1408,7 @@ circuit_finish_handshake(origin_circuit_t *circ,
onion_handshake_state_release(&hop->handshake_state);
if (circuit_init_cpath_crypto(hop, keys, sizeof(keys), 0, 0)<0) {
if (cpath_init_circuit_crypto(hop, keys, sizeof(keys), 0, 0)<0) {
return -END_CIRC_REASON_TORPROTOCOL;
}
@ -1489,7 +1460,7 @@ circuit_truncated(origin_circuit_t *circ, int reason)
}
layer->next = victim->next;
circuit_free_cpath_node(victim);
cpath_free(victim);
}
log_info(LD_CIRC, "finished");
@ -2308,7 +2279,7 @@ circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
state->chosen_exit = extend_info_dup(exit_ei);
++circ->build_state->desired_path_len;
onion_append_hop(&circ->cpath, exit_ei);
cpath_append_hop(&circ->cpath, exit_ei);
return 0;
}
@ -2373,47 +2344,6 @@ count_acceptable_nodes, (const smartlist_t *nodes, int direct))
return num;
}
/** Add <b>new_hop</b> to the end of the doubly-linked-list <b>head_ptr</b>.
* This function is used to extend cpath by another hop.
*/
void
onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop)
{
if (*head_ptr) {
new_hop->next = (*head_ptr);
new_hop->prev = (*head_ptr)->prev;
(*head_ptr)->prev->next = new_hop;
(*head_ptr)->prev = new_hop;
} else {
*head_ptr = new_hop;
new_hop->prev = new_hop->next = new_hop;
}
}
#ifdef TOR_UNIT_TESTS
/** Unittest helper function: Count number of hops in cpath linked list. */
unsigned int
cpath_get_n_hops(crypt_path_t **head_ptr)
{
unsigned int n_hops = 0;
crypt_path_t *tmp;
if (!*head_ptr) {
return 0;
}
tmp = *head_ptr;
do {
n_hops++;
tmp = tmp->next;
} while (tmp != *head_ptr);
return n_hops;
}
#endif /* defined(TOR_UNIT_TESTS) */
/**
* Build the exclude list for vanguard circuits.
*
@ -2688,20 +2618,6 @@ choose_good_entry_server(uint8_t purpose, cpath_build_state_t *state,
return choice;
}
/** Return the first non-open hop in cpath, or return NULL if all
* hops are open. */
static crypt_path_t *
onion_next_hop_in_cpath(crypt_path_t *cpath)
{
crypt_path_t *hop = cpath;
do {
if (hop->state != CPATH_STATE_OPEN)
return hop;
hop = hop->next;
} while (hop != cpath);
return NULL;
}
/** Choose a suitable next hop for the circuit <b>circ</b>.
* Append the hop info to circ->cpath.
*
@ -2758,33 +2674,11 @@ onion_extend_cpath(origin_circuit_t *circ)
extend_info_describe(info),
cur_len+1, build_state_get_exit_nickname(state));
onion_append_hop(&circ->cpath, info);
cpath_append_hop(&circ->cpath, info);
extend_info_free(info);
return 0;
}
/** Create a new hop, annotate it with information about its
* corresponding router <b>choice</b>, and append it to the
* end of the cpath <b>head_ptr</b>. */
STATIC int
onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
{
crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
/* link hop into the cpath, at the end. */
onion_append_to_cpath(head_ptr, hop);
hop->magic = CRYPT_PATH_MAGIC;
hop->state = CPATH_STATE_CLOSED;
hop->extend_info = extend_info_dup(choice);
hop->package_window = circuit_initial_package_window();
hop->deliver_window = CIRCWINDOW_START;
return 0;
}
/** Allocate a new extend_info object based on the various arguments. */
extend_info_t *
extend_info_new(const char *nickname,

View File

@ -34,9 +34,6 @@ int circuit_timeout_want_to_count_circ(const origin_circuit_t *circ);
int circuit_send_next_onion_skin(origin_circuit_t *circ);
void circuit_note_clock_jumped(int64_t seconds_elapsed, bool was_idle);
int circuit_extend(cell_t *cell, circuit_t *circ);
int circuit_init_cpath_crypto(crypt_path_t *cpath,
const char *key_data, size_t key_data_len,
int reverse, int is_hs_v3);
struct created_cell_t;
int circuit_finish_handshake(origin_circuit_t *circ,
const struct created_cell_t *created_cell);
@ -51,7 +48,6 @@ MOCK_DECL(int, circuit_all_predicted_ports_handled, (time_t now,
int circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *info);
int circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *info);
void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
extend_info_t *extend_info_new(const char *nickname,
const char *rsa_id_digest,
const struct ed25519_public_key_t *ed_id,
@ -93,11 +89,6 @@ STATIC int
onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit_ei,
int is_hs_v3_rp_circuit);
#if defined(TOR_UNIT_TESTS)
unsigned int cpath_get_n_hops(crypt_path_t **head_ptr);
#endif /* defined(TOR_UNIT_TESTS) */
#endif /* defined(CIRCUITBUILD_PRIVATE) */
#endif /* !defined(TOR_CIRCUITBUILD_H) */

View File

@ -63,6 +63,7 @@
#include "core/or/circuituse.h"
#include "core/or/circuitstats.h"
#include "core/or/circuitpadding.h"
#include "core/or/crypt_path.h"
#include "core/mainloop/connection.h"
#include "app/config/config.h"
#include "core/or/connection_edge.h"
@ -132,7 +133,6 @@ static smartlist_t *circuits_pending_other_guards = NULL;
* circuit_mark_for_close and which are waiting for circuit_about_to_free. */
static smartlist_t *circuits_pending_close = NULL;
static void circuit_free_cpath_node(crypt_path_t *victim);
static void cpath_ref_decref(crypt_path_reference_t *cpath_ref);
static void circuit_about_to_free_atexit(circuit_t *circ);
static void circuit_about_to_free(circuit_t *circ);
@ -1148,7 +1148,7 @@ circuit_free_(circuit_t *circ)
if (ocirc->build_state) {
extend_info_free(ocirc->build_state->chosen_exit);
circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
cpath_free(ocirc->build_state->pending_final_cpath);
cpath_ref_decref(ocirc->build_state->service_pending_final_cpath_ref);
}
tor_free(ocirc->build_state);
@ -1272,10 +1272,10 @@ circuit_clear_cpath(origin_circuit_t *circ)
while (cpath->next && cpath->next != head) {
victim = cpath;
cpath = victim->next;
circuit_free_cpath_node(victim);
cpath_free(victim);
}
circuit_free_cpath_node(cpath);
cpath_free(cpath);
circ->cpath = NULL;
}
@ -1332,29 +1332,13 @@ circuit_free_all(void)
HT_CLEAR(chan_circid_map, &chan_circid_map);
}
/** Deallocate space associated with the cpath node <b>victim</b>. */
static void
circuit_free_cpath_node(crypt_path_t *victim)
{
if (!victim)
return;
relay_crypto_clear(&victim->crypto);
onion_handshake_state_release(&victim->handshake_state);
crypto_dh_free(victim->rend_dh_handshake_state);
extend_info_free(victim->extend_info);
memwipe(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
tor_free(victim);
}
/** Release a crypt_path_reference_t*, which may be NULL. */
static void
cpath_ref_decref(crypt_path_reference_t *cpath_ref)
{
if (cpath_ref != NULL) {
if (--(cpath_ref->refcount) == 0) {
circuit_free_cpath_node(cpath_ref->cpath);
cpath_free(cpath_ref->cpath);
tor_free(cpath_ref);
}
}
@ -2785,59 +2769,6 @@ circuits_handle_oom(size_t current_allocation)
n_dirconns_killed);
}
/** Verify that cpath layer <b>cp</b> has all of its invariants
* correct. Trigger an assert if anything is invalid.
*/
void
assert_cpath_layer_ok(const crypt_path_t *cp)
{
// tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
// tor_assert(cp->port);
tor_assert(cp);
tor_assert(cp->magic == CRYPT_PATH_MAGIC);
switch (cp->state)
{
case CPATH_STATE_OPEN:
relay_crypto_assert_ok(&cp->crypto);
/* fall through */
case CPATH_STATE_CLOSED:
/*XXXX Assert that there's no handshake_state either. */
tor_assert(!cp->rend_dh_handshake_state);
break;
case CPATH_STATE_AWAITING_KEYS:
/* tor_assert(cp->dh_handshake_state); */
break;
default:
log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
tor_assert(0);
}
tor_assert(cp->package_window >= 0);
tor_assert(cp->deliver_window >= 0);
}
/** Verify that cpath <b>cp</b> has all of its invariants
* correct. Trigger an assert if anything is invalid.
*/
static void
assert_cpath_ok(const crypt_path_t *cp)
{
const crypt_path_t *start = cp;
do {
assert_cpath_layer_ok(cp);
/* layers must be in sequence of: "open* awaiting? closed*" */
if (cp != start) {
if (cp->state == CPATH_STATE_AWAITING_KEYS) {
tor_assert(cp->prev->state == CPATH_STATE_OPEN);
} else if (cp->state == CPATH_STATE_OPEN) {
tor_assert(cp->prev->state == CPATH_STATE_OPEN);
}
}
cp = cp->next;
tor_assert(cp);
} while (cp != start);
}
/** Verify that circuit <b>c</b> has all of its invariants
* correct. Trigger an assert if anything is invalid.
*/
@ -2899,7 +2830,7 @@ assert_circuit_ok,(const circuit_t *c))
!smartlist_contains(circuits_pending_chans, c));
}
if (origin_circ && origin_circ->cpath) {
assert_cpath_ok(origin_circ->cpath);
cpath_assert_ok(origin_circ->cpath);
}
if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
tor_assert(or_circ);

View File

@ -228,7 +228,6 @@ int circuit_count_pending_on_channel(channel_t *chan);
#define circuit_mark_for_close(c, reason) \
circuit_mark_for_close_((c), (reason), __LINE__, SHORT_FILE__)
void assert_cpath_layer_ok(const crypt_path_t *cp);
MOCK_DECL(void, assert_circuit_ok,(const circuit_t *c));
void circuit_free_all(void);
void circuits_handle_oom(size_t current_allocation);

262
src/core/or/crypt_path.c Normal file
View File

@ -0,0 +1,262 @@
/*
* Copyright (c) 2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file crypt_path.c
*
* \brief Functions dealing with layered circuit encryption. This file aims to
* provide an API around the crypt_path_t structure which holds crypto
* information about a specific hop of a circuit.
*
* TODO: We should eventually move all functions dealing and manipulating
* crypt_path_t to this file, so that eventually we encapsulate more and more
* of crypt_path_t. Here are some more functions that can be moved here with
* some more effort:
*
* - circuit_list_path_impl()
* - Functions dealing with cpaths in HSv2 create_rend_cpath() and
* create_rend_cpath_legacy()
* - The cpath related parts of rend_service_receive_introduction() and
* rend_client_send_introduction().
**/
#define CRYPT_PATH_PRIVATE
#include "core/or/or.h"
#include "core/or/crypt_path.h"
#include "core/crypto/relay_crypto.h"
#include "core/crypto/onion_crypto.h"
#include "core/or/circuitbuild.h"
#include "core/or/circuitlist.h"
#include "lib/crypt_ops/crypto_dh.h"
#include "lib/crypt_ops/crypto_util.h"
#include "core/or/crypt_path_st.h"
#include "core/or/cell_st.h"
/** Add <b>new_hop</b> to the end of the doubly-linked-list <b>head_ptr</b>.
* This function is used to extend cpath by another hop.
*/
void
cpath_extend_linked_list(crypt_path_t **head_ptr, crypt_path_t *new_hop)
{
if (*head_ptr) {
new_hop->next = (*head_ptr);
new_hop->prev = (*head_ptr)->prev;
(*head_ptr)->prev->next = new_hop;
(*head_ptr)->prev = new_hop;
} else {
*head_ptr = new_hop;
new_hop->prev = new_hop->next = new_hop;
}
}
/** Create a new hop, annotate it with information about its
* corresponding router <b>choice</b>, and append it to the
* end of the cpath <b>head_ptr</b>. */
int
cpath_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
{
crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
/* link hop into the cpath, at the end. */
cpath_extend_linked_list(head_ptr, hop);
hop->magic = CRYPT_PATH_MAGIC;
hop->state = CPATH_STATE_CLOSED;
hop->extend_info = extend_info_dup(choice);
hop->package_window = circuit_initial_package_window();
hop->deliver_window = CIRCWINDOW_START;
return 0;
}
/** Verify that cpath <b>cp</b> has all of its invariants
* correct. Trigger an assert if anything is invalid.
*/
void
cpath_assert_ok(const crypt_path_t *cp)
{
const crypt_path_t *start = cp;
do {
cpath_assert_layer_ok(cp);
/* layers must be in sequence of: "open* awaiting? closed*" */
if (cp != start) {
if (cp->state == CPATH_STATE_AWAITING_KEYS) {
tor_assert(cp->prev->state == CPATH_STATE_OPEN);
} else if (cp->state == CPATH_STATE_OPEN) {
tor_assert(cp->prev->state == CPATH_STATE_OPEN);
}
}
cp = cp->next;
tor_assert(cp);
} while (cp != start);
}
/** Verify that cpath layer <b>cp</b> has all of its invariants
* correct. Trigger an assert if anything is invalid.
*/
void
cpath_assert_layer_ok(const crypt_path_t *cp)
{
// tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
// tor_assert(cp->port);
tor_assert(cp);
tor_assert(cp->magic == CRYPT_PATH_MAGIC);
switch (cp->state)
{
case CPATH_STATE_OPEN:
relay_crypto_assert_ok(&cp->pvt_crypto);
/* fall through */
case CPATH_STATE_CLOSED:
/*XXXX Assert that there's no handshake_state either. */
tor_assert(!cp->rend_dh_handshake_state);
break;
case CPATH_STATE_AWAITING_KEYS:
/* tor_assert(cp->dh_handshake_state); */
break;
default:
log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
tor_assert(0);
}
tor_assert(cp->package_window >= 0);
tor_assert(cp->deliver_window >= 0);
}
/** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in key_data.
*
* If <b>is_hs_v3</b> is set, this cpath will be used for next gen hidden
* service circuits and <b>key_data</b> must be at least
* HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN bytes in length.
*
* If <b>is_hs_v3</b> is not set, key_data must contain CPATH_KEY_MATERIAL_LEN
* bytes, which are used as follows:
* - 20 to initialize f_digest
* - 20 to initialize b_digest
* - 16 to key f_crypto
* - 16 to key b_crypto
*
* (If 'reverse' is true, then f_XX and b_XX are swapped.)
*
* Return 0 if init was successful, else -1 if it failed.
*/
int
cpath_init_circuit_crypto(crypt_path_t *cpath,
const char *key_data, size_t key_data_len,
int reverse, int is_hs_v3)
{
tor_assert(cpath);
return relay_crypto_init(&cpath->pvt_crypto, key_data, key_data_len,
reverse, is_hs_v3);
}
/** Deallocate space associated with the cpath node <b>victim</b>. */
void
cpath_free(crypt_path_t *victim)
{
if (!victim)
return;
relay_crypto_clear(&victim->pvt_crypto);
onion_handshake_state_release(&victim->handshake_state);
crypto_dh_free(victim->rend_dh_handshake_state);
extend_info_free(victim->extend_info);
memwipe(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
tor_free(victim);
}
/********************** cpath crypto API *******************************/
/** Encrypt or decrypt <b>payload</b> using the crypto of <b>cpath</b>. Actual
* operation decided by <b>is_decrypt</b>. */
void
cpath_crypt_cell(const crypt_path_t *cpath, uint8_t *payload, bool is_decrypt)
{
if (is_decrypt) {
relay_crypt_one_payload(cpath->pvt_crypto.b_crypto, payload);
} else {
relay_crypt_one_payload(cpath->pvt_crypto.f_crypto, payload);
}
}
/** Getter for the incoming digest of <b>cpath</b>. */
struct crypto_digest_t *
cpath_get_incoming_digest(const crypt_path_t *cpath)
{
return cpath->pvt_crypto.b_digest;
}
/** Set the right integrity digest on the outgoing <b>cell</b> based on the
* cell payload and update the forward digest of <b>cpath</b>. */
void
cpath_set_cell_forward_digest(crypt_path_t *cpath, cell_t *cell)
{
relay_set_digest(cpath->pvt_crypto.f_digest, cell);
}
/************ cpath sendme API ***************************/
/** Keep the current inbound cell digest for the next SENDME digest. This part
* is only done by the client as the circuit came back from the Exit. */
void
cpath_sendme_circuit_record_inbound_cell(crypt_path_t *cpath)
{
tor_assert(cpath);
relay_crypto_record_sendme_digest(&cpath->pvt_crypto);
}
/** Return the sendme_digest of this <b>cpath</b>. */
uint8_t *
cpath_get_sendme_digest(crypt_path_t *cpath)
{
return relay_crypto_get_sendme_digest(&cpath->pvt_crypto);
}
/************ other cpath functions ***************************/
/** Return the first non-open hop in cpath, or return NULL if all
* hops are open. */
crypt_path_t *
cpath_get_next_non_open_hop(crypt_path_t *cpath)
{
crypt_path_t *hop = cpath;
do {
if (hop->state != CPATH_STATE_OPEN)
return hop;
hop = hop->next;
} while (hop != cpath);
return NULL;
}
#ifdef TOR_UNIT_TESTS
/** Unittest helper function: Count number of hops in cpath linked list. */
unsigned int
cpath_get_n_hops(crypt_path_t **head_ptr)
{
unsigned int n_hops = 0;
crypt_path_t *tmp;
if (!*head_ptr) {
return 0;
}
tmp = *head_ptr;
do {
n_hops++;
tmp = tmp->next;
} while (tmp != *head_ptr);
return n_hops;
}
#endif /* defined(TOR_UNIT_TESTS) */

43
src/core/or/crypt_path.h Normal file
View File

@ -0,0 +1,43 @@
/**
* \file crypt_path.h
* \brief Header file for crypt_path.c.
**/
#ifndef CRYPT_PATH_H
#define CRYPT_PATH_H
void cpath_assert_layer_ok(const crypt_path_t *cp);
void cpath_assert_ok(const crypt_path_t *cp);
int cpath_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
int cpath_init_circuit_crypto(crypt_path_t *cpath,
const char *key_data, size_t key_data_len,
int reverse, int is_hs_v3);
void
cpath_free(crypt_path_t *victim);
void cpath_extend_linked_list(crypt_path_t **head_ptr, crypt_path_t *new_hop);
void
cpath_crypt_cell(const crypt_path_t *cpath, uint8_t *payload, bool is_decrypt);
struct crypto_digest_t *
cpath_get_incoming_digest(const crypt_path_t *cpath);
void
cpath_set_cell_forward_digest(crypt_path_t *cpath, cell_t *cell);
crypt_path_t *cpath_get_next_non_open_hop(crypt_path_t *cpath);
void cpath_sendme_circuit_record_inbound_cell(crypt_path_t *cpath);
uint8_t *cpath_get_sendme_digest(crypt_path_t *cpath);
#if defined(TOR_UNIT_TESTS)
unsigned int cpath_get_n_hops(crypt_path_t **head_ptr);
#endif /* defined(TOR_UNIT_TESTS) */
#endif

View File

@ -24,15 +24,24 @@ struct onion_handshake_state_t {
} u;
};
/** Macro to encapsulate private members of a struct.
*
* Renames 'x' to 'x_crypt_path_private_field'.
*/
#define CRYPT_PATH_PRIV_FIELD(x) x ## _crypt_path_private_field
#ifdef CRYPT_PATH_PRIVATE
/* Helper macro to access private members of a struct. */
#define pvt_crypto CRYPT_PATH_PRIV_FIELD(crypto)
#endif
/** Holds accounting information for a single step in the layered encryption
* performed by a circuit. Used only at the client edge of a circuit. */
struct crypt_path_t {
uint32_t magic;
/** Cryptographic state used for encrypting and authenticating relay
* cells to and from this hop. */
relay_crypto_t crypto;
/** Current state of the handshake as performed with the OR at this
* step. */
onion_handshake_state_t handshake_state;
@ -65,6 +74,12 @@ struct crypt_path_t {
* at this step? */
int deliver_window; /**< How many cells are we willing to deliver originating
* at this step? */
/*********************** Private members ****************************/
/** Private member: Cryptographic state used for encrypting and
* authenticating relay cells to and from this hop. */
relay_crypto_t CRYPT_PATH_PRIV_FIELD(crypto);
};
#endif

View File

@ -15,6 +15,7 @@
#include "core/crypto/relay_crypto.h"
#include "core/mainloop/connection.h"
#include "core/or/cell_st.h"
#include "core/or/crypt_path.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "core/or/or_circuit_st.h"
@ -299,15 +300,6 @@ sendme_circuit_record_outbound_cell(or_circuit_t *or_circ)
relay_crypto_record_sendme_digest(&or_circ->crypto);
}
/** Keep the current inbound cell digest for the next SENDME digest. This part
* is only done by the client as the circuit came back from the Exit. */
void
sendme_circuit_record_inbound_cell(crypt_path_t *cpath)
{
tor_assert(cpath);
relay_crypto_record_sendme_digest(&cpath->crypto);
}
/** Return true iff the next cell for the given cell window is expected to be
* a SENDME.
*
@ -387,7 +379,7 @@ sendme_circuit_consider_sending(circuit_t *circ, crypt_path_t *layer_hint)
log_debug(LD_CIRC,"Queuing circuit sendme.");
if (layer_hint) {
layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
digest = relay_crypto_get_sendme_digest(&layer_hint->crypto);
digest = cpath_get_sendme_digest(layer_hint);
} else {
circ->deliver_window += CIRCWINDOW_INCREMENT;
digest = relay_crypto_get_sendme_digest(&TO_OR_CIRCUIT(circ)->crypto);

View File

@ -36,7 +36,6 @@ int sendme_note_stream_data_packaged(edge_connection_t *conn);
/* Track cell digest. */
void sendme_record_cell_digest(circuit_t *circ);
void sendme_circuit_record_inbound_cell(crypt_path_t *cpath);
void sendme_circuit_record_outbound_cell(or_circuit_t *or_circ);
/* Circuit level information. */

View File

@ -15,6 +15,7 @@
#include "core/or/circuituse.h"
#include "core/or/policies.h"
#include "core/or/relay.h"
#include "core/or/crypt_path.h"
#include "feature/client/circpathbias.h"
#include "feature/hs/hs_cell.h"
#include "feature/hs/hs_circuit.h"
@ -89,7 +90,7 @@ create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len,
cpath = tor_malloc_zero(sizeof(crypt_path_t));
cpath->magic = CRYPT_PATH_MAGIC;
if (circuit_init_cpath_crypto(cpath, (char*)keys, sizeof(keys),
if (cpath_init_circuit_crypto(cpath, (char*)keys, sizeof(keys),
is_service_side, 1) < 0) {
tor_free(cpath);
goto err;
@ -126,7 +127,7 @@ create_rend_cpath_legacy(origin_circuit_t *circ, const uint8_t *rend_cell_body)
goto err;
}
/* ... and set up cpath. */
if (circuit_init_cpath_crypto(hop,
if (cpath_init_circuit_crypto(hop,
keys+DIGEST_LEN, sizeof(keys)-DIGEST_LEN,
0, 0) < 0)
goto err;
@ -177,7 +178,7 @@ finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop,
circ->hs_circ_has_timed_out = 0;
/* Append the hop to the cpath of this circuit */
onion_append_to_cpath(&circ->cpath, hop);
cpath_extend_linked_list(&circ->cpath, hop);
/* In legacy code, 'pending_final_cpath' points to the final hop we just
* appended to the cpath. We set the original pointer to NULL so that we

View File

@ -18,6 +18,7 @@
#include "core/or/circuituse.h"
#include "core/or/policies.h"
#include "core/or/relay.h"
#include "core/or/crypt_path.h"
#include "feature/client/circpathbias.h"
#include "feature/control/control_events.h"
#include "feature/dirclient/dirclient.h"
@ -2163,7 +2164,7 @@ rend_service_receive_introduction(origin_circuit_t *circuit,
cpath->rend_dh_handshake_state = dh;
dh = NULL;
if (circuit_init_cpath_crypto(cpath,
if (cpath_init_circuit_crypto(cpath,
keys+DIGEST_LEN, sizeof(keys)-DIGEST_LEN,
1, 0)<0)
goto err;
@ -3547,7 +3548,7 @@ rend_service_rendezvous_has_opened(origin_circuit_t *circuit)
hop->package_window = circuit_initial_package_window();
hop->deliver_window = CIRCWINDOW_START;
onion_append_to_cpath(&circuit->cpath, hop);
cpath_extend_linked_list(&circuit->cpath, hop);
circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
/* Change the circuit purpose. */

View File

@ -2,6 +2,7 @@
#define TOR_TIMERS_PRIVATE
#define CIRCUITPADDING_PRIVATE
#define NETWORKSTATUS_PRIVATE
#define CRYPT_PATH_PRIVATE
#include "core/or/or.h"
#include "test.h"
@ -9,6 +10,7 @@
#include "core/or/connection_or.h"
#include "core/or/channel.h"
#include "core/or/channeltls.h"
#include "core/or/crypt_path.h"
#include <event.h>
#include "lib/evloop/compat_libevent.h"
#include "lib/time/compat_time.h"
@ -143,12 +145,12 @@ new_fake_orcirc(channel_t *nchan, channel_t *pchan)
circuit_set_n_circid_chan(circ, circ->n_circ_id, nchan);
memset(&tmp_cpath, 0, sizeof(tmp_cpath));
if (circuit_init_cpath_crypto(&tmp_cpath, whatevs_key,
if (cpath_init_circuit_crypto(&tmp_cpath, whatevs_key,
sizeof(whatevs_key), 0, 0)<0) {
log_warn(LD_BUG,"Circuit initialization failed");
return NULL;
}
orcirc->crypto = tmp_cpath.crypto;
orcirc->crypto = tmp_cpath.pvt_crypto;
return orcirc;
}
@ -1617,7 +1619,7 @@ simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
// Add a hop to cpath
crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
onion_append_to_cpath(&TO_ORIGIN_CIRCUIT(client)->cpath, hop);
cpath_extend_linked_list(&TO_ORIGIN_CIRCUIT(client)->cpath, hop);
hop->magic = CRYPT_PATH_MAGIC;
hop->state = CPATH_STATE_OPEN;
@ -1631,7 +1633,7 @@ simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
digest, NULL, NULL, NULL,
&addr, padding);
circuit_init_cpath_crypto(hop, whatevs_key, sizeof(whatevs_key), 0, 0);
cpath_init_circuit_crypto(hop, whatevs_key, sizeof(whatevs_key), 0, 0);
hop->package_window = circuit_initial_package_window();
hop->deliver_window = CIRCWINDOW_START;

View File

@ -28,7 +28,7 @@ origin_circuit_t *subtest_fourhop_circuit(struct timeval, int);
origin_circuit_t *add_opened_threehop(void);
origin_circuit_t *build_unopened_fourhop(struct timeval);
int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
int cpath_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
static int marked_for_close;
/* Mock function because we are not trying to test the close circuit that does
@ -57,9 +57,9 @@ add_opened_threehop(void)
or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
or_circ->build_state->desired_path_len = DEFAULT_ROUTE_LEN;
onion_append_hop(&or_circ->cpath, &fakehop);
onion_append_hop(&or_circ->cpath, &fakehop);
onion_append_hop(&or_circ->cpath, &fakehop);
cpath_append_hop(&or_circ->cpath, &fakehop);
cpath_append_hop(&or_circ->cpath, &fakehop);
cpath_append_hop(&or_circ->cpath, &fakehop);
or_circ->has_opened = 1;
TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
@ -82,10 +82,10 @@ build_unopened_fourhop(struct timeval circ_start_time)
or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
or_circ->build_state->desired_path_len = 4;
onion_append_hop(&or_circ->cpath, fakehop);
onion_append_hop(&or_circ->cpath, fakehop);
onion_append_hop(&or_circ->cpath, fakehop);
onion_append_hop(&or_circ->cpath, fakehop);
cpath_append_hop(&or_circ->cpath, fakehop);
cpath_append_hop(&or_circ->cpath, fakehop);
cpath_append_hop(&or_circ->cpath, fakehop);
cpath_append_hop(&or_circ->cpath, fakehop);
tor_free(fakehop);

View File

@ -14,6 +14,7 @@
#define CIRCUITBUILD_PRIVATE
#define CIRCUITLIST_PRIVATE
#define CONNECTION_PRIVATE
#define CRYPT_PATH_PRIVATE
#include "test/test.h"
#include "test/test_helpers.h"
@ -44,6 +45,7 @@
#include "core/or/cpath_build_state_st.h"
#include "core/or/crypt_path_st.h"
#include "core/or/crypt_path.h"
#include "feature/dircommon/dir_connection_st.h"
#include "core/or/entry_connection_st.h"
#include "core/or/extend_info_st.h"
@ -241,12 +243,14 @@ test_e2e_rend_circuit_setup_legacy(void *arg)
tt_int_op(retval, OP_EQ, 1);
/* Check the digest algo */
tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.f_digest),
tt_int_op(
crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.f_digest),
OP_EQ, DIGEST_SHA1);
tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.b_digest),
tt_int_op(
crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.b_digest),
OP_EQ, DIGEST_SHA1);
tt_assert(or_circ->cpath->crypto.f_crypto);
tt_assert(or_circ->cpath->crypto.b_crypto);
tt_assert(or_circ->cpath->pvt_crypto.f_crypto);
tt_assert(or_circ->cpath->pvt_crypto.b_crypto);
/* Ensure that circ purpose was changed */
tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_C_REND_JOINED);
@ -311,12 +315,14 @@ test_e2e_rend_circuit_setup(void *arg)
tt_int_op(retval, OP_EQ, 1);
/* Check that the crypt path has prop224 algorithm parameters */
tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.f_digest),
tt_int_op(
crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.f_digest),
OP_EQ, DIGEST_SHA3_256);
tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.b_digest),
tt_int_op(
crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.b_digest),
OP_EQ, DIGEST_SHA3_256);
tt_assert(or_circ->cpath->crypto.f_crypto);
tt_assert(or_circ->cpath->crypto.b_crypto);
tt_assert(or_circ->cpath->pvt_crypto.f_crypto);
tt_assert(or_circ->cpath->pvt_crypto.b_crypto);
/* Ensure that circ purpose was changed */
tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_C_REND_JOINED);

View File

@ -21,6 +21,7 @@
#define STATEFILE_PRIVATE
#define TOR_CHANNEL_INTERNAL_
#define HS_CLIENT_PRIVATE
#define CRYPT_PATH_PRIVATE
#include "test/test.h"
#include "test/test_helpers.h"
@ -60,6 +61,7 @@
#include "core/or/cpath_build_state_st.h"
#include "core/or/crypt_path_st.h"
#include "core/or/crypt_path.h"
#include "feature/nodelist/networkstatus_st.h"
#include "feature/nodelist/node_st.h"
#include "core/or/origin_circuit_st.h"
@ -193,12 +195,14 @@ test_e2e_rend_circuit_setup(void *arg)
tt_int_op(retval, OP_EQ, 1);
/* Check the digest algo */
tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.f_digest),
tt_int_op(
crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.f_digest),
OP_EQ, DIGEST_SHA3_256);
tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.b_digest),
tt_int_op(
crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.b_digest),
OP_EQ, DIGEST_SHA3_256);
tt_assert(or_circ->cpath->crypto.f_crypto);
tt_assert(or_circ->cpath->crypto.b_crypto);
tt_assert(or_circ->cpath->pvt_crypto.f_crypto);
tt_assert(or_circ->cpath->pvt_crypto.b_crypto);
/* Ensure that circ purpose was changed */
tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);

View File

@ -3,6 +3,8 @@
* Copyright (c) 2007-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define CRYPT_PATH_PRIVATE
#include "core/or/or.h"
#include "core/or/circuitbuild.h"
#define CIRCUITLIST_PRIVATE
@ -10,7 +12,7 @@
#include "lib/crypt_ops/crypto_rand.h"
#include "core/or/relay.h"
#include "core/crypto/relay_crypto.h"
#include "core/or/crypt_path.h"
#include "core/or/cell_st.h"
#include "core/or/or_circuit_st.h"
#include "core/or/origin_circuit_st.h"
@ -49,10 +51,10 @@ testing_circuitset_setup(const struct testcase_t *testcase)
cs->origin_circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
for (i=0; i<3; ++i) {
crypt_path_t *hop = tor_malloc_zero(sizeof(*hop));
relay_crypto_init(&hop->crypto, KEY_MATERIAL[i], sizeof(KEY_MATERIAL[i]),
0, 0);
relay_crypto_init(&hop->pvt_crypto, KEY_MATERIAL[i],
sizeof(KEY_MATERIAL[i]), 0, 0);
hop->state = CPATH_STATE_OPEN;
onion_append_to_cpath(&cs->origin_circ->cpath, hop);
cpath_extend_linked_list(&cs->origin_circ->cpath, hop);
tt_ptr_op(hop, OP_EQ, cs->origin_circ->cpath->prev);
}