|
@@ -14,19 +14,24 @@
|
|
|
#include "test.h"
|
|
|
#include "log_test_helpers.h"
|
|
|
#include "crypto.h"
|
|
|
+#include "log_test_helpers.h"
|
|
|
|
|
|
#include "or.h"
|
|
|
#include "ht.h"
|
|
|
|
|
|
+
|
|
|
#include "hs/cell_establish_intro.h"
|
|
|
-#include "hs_common.h"
|
|
|
+#include "hs/cell_introduce1.h"
|
|
|
+#include "hs/cell_common.h"
|
|
|
#include "hs_service.h"
|
|
|
+#include "hs_common.h"
|
|
|
#include "hs_circuitmap.h"
|
|
|
#include "hs_intropoint.h"
|
|
|
|
|
|
#include "circuitlist.h"
|
|
|
#include "circuituse.h"
|
|
|
#include "rendservice.h"
|
|
|
+#include "relay.h"
|
|
|
|
|
|
|
|
|
static int
|
|
@@ -36,6 +41,80 @@ mock_send_intro_established_cell(or_circuit_t *circ)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+static int
|
|
|
+mock_relay_send_command_from_edge(streamid_t stream_id, circuit_t *circ,
|
|
|
+ uint8_t relay_command, const char *payload,
|
|
|
+ size_t payload_len, crypt_path_t *cpath_layer,
|
|
|
+ const char *filename, int lineno)
|
|
|
+{
|
|
|
+ (void) stream_id;
|
|
|
+ (void) circ;
|
|
|
+ (void) relay_command;
|
|
|
+ (void) payload;
|
|
|
+ (void) payload_len;
|
|
|
+ (void) cpath_layer;
|
|
|
+ (void) filename;
|
|
|
+ (void) lineno;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static or_circuit_t *
|
|
|
+helper_create_intro_circuit(void)
|
|
|
+{
|
|
|
+ or_circuit_t *circ = or_circuit_new(0, NULL);
|
|
|
+ tt_assert(circ);
|
|
|
+ circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
|
|
|
+ done:
|
|
|
+ return circ;
|
|
|
+}
|
|
|
+
|
|
|
+static hs_cell_introduce1_t *
|
|
|
+helper_create_introduce1_cell(void)
|
|
|
+{
|
|
|
+ hs_cell_introduce1_t *cell = NULL;
|
|
|
+ ed25519_keypair_t auth_key_kp;
|
|
|
+
|
|
|
+
|
|
|
+ if (ed25519_keypair_generate(&auth_key_kp, 0) < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ cell = hs_cell_introduce1_new();
|
|
|
+ tt_assert(cell);
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ size_t auth_key_len = sizeof(auth_key_kp.pubkey);
|
|
|
+ hs_cell_introduce1_set_auth_key_type(cell,
|
|
|
+ HS_INTRO_AUTH_KEY_TYPE_ED25519);
|
|
|
+ hs_cell_introduce1_set_auth_key_len(cell, auth_key_len);
|
|
|
+ hs_cell_introduce1_setlen_auth_key(cell, auth_key_len);
|
|
|
+ uint8_t *auth_key_ptr = hs_cell_introduce1_getarray_auth_key(cell);
|
|
|
+ memcpy(auth_key_ptr, auth_key_kp.pubkey.pubkey, auth_key_len);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ cell_extension_t *ext = cell_extension_new();
|
|
|
+ cell_extension_set_num(ext, 0);
|
|
|
+ hs_cell_introduce1_set_extensions(cell, ext);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ size_t enc_len = 128;
|
|
|
+ hs_cell_introduce1_setlen_encrypted(cell, enc_len);
|
|
|
+ uint8_t *enc_ptr = hs_cell_introduce1_getarray_encrypted(cell);
|
|
|
+ memset(enc_ptr, 'a', enc_len);
|
|
|
+ }
|
|
|
+
|
|
|
+ return cell;
|
|
|
+ err:
|
|
|
+ done:
|
|
|
+ hs_cell_introduce1_free(cell);
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
* point. Should fail. */
|
|
|
static void
|
|
@@ -453,7 +532,8 @@ test_intro_point_registration(void *arg)
|
|
|
the_hs_circuitmap = get_hs_circuitmap();
|
|
|
tt_assert(the_hs_circuitmap);
|
|
|
tt_int_op(1, ==, HT_SIZE(the_hs_circuitmap));
|
|
|
- get_auth_key_from_establish_intro_cell(&auth_key, establish_intro_cell);
|
|
|
+ get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO,
|
|
|
+ establish_intro_cell);
|
|
|
returned_intro_circ = hs_circuitmap_get_intro_circ_v3(&auth_key);
|
|
|
tt_ptr_op(intro_circ, ==, returned_intro_circ);
|
|
|
}
|
|
@@ -499,6 +579,242 @@ test_intro_point_registration(void *arg)
|
|
|
UNMOCK(hs_intro_send_intro_established_cell);
|
|
|
}
|
|
|
|
|
|
+static void
|
|
|
+test_introduce1_suitable_circuit(void *arg)
|
|
|
+{
|
|
|
+ int ret;
|
|
|
+ or_circuit_t *circ = NULL;
|
|
|
+
|
|
|
+ (void) arg;
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ circ = or_circuit_new(0, NULL);
|
|
|
+ circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
|
|
|
+ ret = circuit_is_suitable_for_introduce1(circ);
|
|
|
+ circuit_free(TO_CIRCUIT(circ));
|
|
|
+ tt_int_op(ret, OP_EQ, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ circ = or_circuit_new(0, NULL);
|
|
|
+ circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
|
|
|
+ ret = circuit_is_suitable_for_introduce1(circ);
|
|
|
+ circuit_free(TO_CIRCUIT(circ));
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ circ = or_circuit_new(0, NULL);
|
|
|
+ circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
|
|
|
+
|
|
|
+ circ->base_.n_chan = (channel_t *) circ;
|
|
|
+ ret = circuit_is_suitable_for_introduce1(circ);
|
|
|
+ circuit_free(TO_CIRCUIT(circ));
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * limit works correctly. */
|
|
|
+ {
|
|
|
+ circ = or_circuit_new(0, NULL);
|
|
|
+ circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
|
|
|
+ circ->already_received_introduce1 = 1;
|
|
|
+ ret = circuit_is_suitable_for_introduce1(circ);
|
|
|
+ circuit_free(TO_CIRCUIT(circ));
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ done:
|
|
|
+ ;
|
|
|
+}
|
|
|
+
|
|
|
+static void
|
|
|
+test_introduce1_is_legacy(void *arg)
|
|
|
+{
|
|
|
+ int ret;
|
|
|
+ uint8_t request[256];
|
|
|
+
|
|
|
+ (void) arg;
|
|
|
+
|
|
|
+
|
|
|
+ * first 20 bytes MUST BE non-zero else it's a v3 cell. */
|
|
|
+ memset(request, 'a', DIGEST_LEN);
|
|
|
+ memset(request + DIGEST_LEN, 0, sizeof(request) - DIGEST_LEN);
|
|
|
+ ret = introduce1_cell_is_legacy(request);
|
|
|
+ tt_int_op(ret, OP_EQ, 1);
|
|
|
+
|
|
|
+
|
|
|
+ memset(request, 0, DIGEST_LEN);
|
|
|
+ memset(request + DIGEST_LEN, 'a', sizeof(request) - DIGEST_LEN);
|
|
|
+ ret = introduce1_cell_is_legacy(request);
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+
|
|
|
+ done:
|
|
|
+ ;
|
|
|
+}
|
|
|
+
|
|
|
+static void
|
|
|
+test_introduce1_validation(void *arg)
|
|
|
+{
|
|
|
+ int ret;
|
|
|
+ hs_cell_introduce1_t *cell = NULL;
|
|
|
+
|
|
|
+ (void) arg;
|
|
|
+
|
|
|
+
|
|
|
+ * function of that parsed cell. */
|
|
|
+ cell = helper_create_introduce1_cell();
|
|
|
+
|
|
|
+
|
|
|
+ memset(cell->legacy_key_id, 'a', sizeof(cell->legacy_key_id));
|
|
|
+ tor_capture_bugs_(1);
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tor_end_capture_bugs_();
|
|
|
+ tt_int_op(ret, OP_EQ, -1);
|
|
|
+
|
|
|
+ memset(cell->legacy_key_id, 0, sizeof(cell->legacy_key_id));
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+
|
|
|
+
|
|
|
+ cell->auth_key_type = 42;
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, -1);
|
|
|
+
|
|
|
+ cell->auth_key_type = HS_INTRO_AUTH_KEY_TYPE_ED25519;
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+
|
|
|
+
|
|
|
+ cell->auth_key_len = 0;
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, -1);
|
|
|
+ cell->auth_key_len = UINT16_MAX;
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, -1);
|
|
|
+
|
|
|
+ cell->auth_key_len = sizeof(ed25519_public_key_t);
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+
|
|
|
+ hs_cell_introduce1_setlen_auth_key(cell, 3);
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, -1);
|
|
|
+
|
|
|
+ hs_cell_introduce1_setlen_auth_key(cell, sizeof(ed25519_public_key_t));
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+
|
|
|
+
|
|
|
+ hs_cell_introduce1_setlen_encrypted(cell, 0);
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, -1);
|
|
|
+
|
|
|
+ hs_cell_introduce1_setlen_encrypted(cell, 1);
|
|
|
+ ret = validate_introduce1_parsed_cell(cell);
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+
|
|
|
+ done:
|
|
|
+ hs_cell_introduce1_free(cell);
|
|
|
+}
|
|
|
+
|
|
|
+static void
|
|
|
+test_received_introduce1_handling(void *arg)
|
|
|
+{
|
|
|
+ int ret;
|
|
|
+ uint8_t *request = NULL, buf[128];
|
|
|
+ hs_cell_introduce1_t *cell = NULL;
|
|
|
+ or_circuit_t *circ = NULL;
|
|
|
+
|
|
|
+ (void) arg;
|
|
|
+
|
|
|
+ MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
|
|
|
+
|
|
|
+ hs_circuitmap_init();
|
|
|
+
|
|
|
+
|
|
|
+ * DIGEST_LEN size. */
|
|
|
+ {
|
|
|
+ circ = helper_create_intro_circuit();
|
|
|
+ ret = hs_intro_received_introduce1(circ, buf, DIGEST_LEN - 1);
|
|
|
+ tt_int_op(ret, OP_EQ, -1);
|
|
|
+ circuit_free(TO_CIRCUIT(circ));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * INTRODUCE1 cell so from now on we'll only test the handling of a cell. */
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ circ = helper_create_intro_circuit();
|
|
|
+ uint8_t test[2];
|
|
|
+ ret = handle_introduce1(circ, test, sizeof(test));
|
|
|
+ tor_free(circ->p_chan);
|
|
|
+ circuit_free(TO_CIRCUIT(circ));
|
|
|
+ tt_int_op(ret, OP_EQ, -1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ cell = helper_create_introduce1_cell();
|
|
|
+ size_t request_len = hs_cell_introduce1_encoded_len(cell);
|
|
|
+ tt_size_op(request_len, OP_GT, 0);
|
|
|
+ request = tor_malloc_zero(request_len);
|
|
|
+ ssize_t encoded_len = hs_cell_introduce1_encode(request, request_len, cell);
|
|
|
+ tt_size_op(encoded_len, OP_GT, 0);
|
|
|
+
|
|
|
+ circ = helper_create_intro_circuit();
|
|
|
+ or_circuit_t *service_circ = helper_create_intro_circuit();
|
|
|
+ circuit_change_purpose(TO_CIRCUIT(service_circ), CIRCUIT_PURPOSE_INTRO_POINT);
|
|
|
+
|
|
|
+ ed25519_public_key_t auth_key;
|
|
|
+ const uint8_t *cell_auth_key =
|
|
|
+ hs_cell_introduce1_getconstarray_auth_key(cell);
|
|
|
+ memcpy(auth_key.pubkey, cell_auth_key, ED25519_PUBKEY_LEN);
|
|
|
+ hs_circuitmap_register_intro_circ_v3(service_circ, &auth_key);
|
|
|
+ ret = hs_intro_received_introduce1(circ, request, request_len);
|
|
|
+ circuit_free(TO_CIRCUIT(circ));
|
|
|
+ circuit_free(TO_CIRCUIT(service_circ));
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ tor_free(request);
|
|
|
+ hs_cell_introduce1_free(cell);
|
|
|
+ cell = helper_create_introduce1_cell();
|
|
|
+ uint8_t *legacy_key_id = hs_cell_introduce1_getarray_legacy_key_id(cell);
|
|
|
+ memset(legacy_key_id, 'a', DIGEST_LEN);
|
|
|
+
|
|
|
+ size_t request_len = hs_cell_introduce1_encoded_len(cell) + 256;
|
|
|
+ tt_size_op(request_len, OP_GT, 0);
|
|
|
+ request = tor_malloc_zero(request_len + 256);
|
|
|
+ ssize_t encoded_len = hs_cell_introduce1_encode(request, request_len, cell);
|
|
|
+ tt_size_op(encoded_len, OP_GT, 0);
|
|
|
+
|
|
|
+ circ = helper_create_intro_circuit();
|
|
|
+ or_circuit_t *service_circ = helper_create_intro_circuit();
|
|
|
+ circuit_change_purpose(TO_CIRCUIT(service_circ), CIRCUIT_PURPOSE_INTRO_POINT);
|
|
|
+
|
|
|
+ uint8_t token[REND_TOKEN_LEN];
|
|
|
+ memcpy(token, legacy_key_id, sizeof(token));
|
|
|
+ hs_circuitmap_register_intro_circ_v2(service_circ, token);
|
|
|
+ ret = hs_intro_received_introduce1(circ, request, request_len);
|
|
|
+ circuit_free(TO_CIRCUIT(circ));
|
|
|
+ circuit_free(TO_CIRCUIT(service_circ));
|
|
|
+ tt_int_op(ret, OP_EQ, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ done:
|
|
|
+ hs_cell_introduce1_free(cell);
|
|
|
+ tor_free(request);
|
|
|
+ hs_circuitmap_free_all();
|
|
|
+ UNMOCK(relay_send_command_from_edge_);
|
|
|
+}
|
|
|
+
|
|
|
struct testcase_t hs_intropoint_tests[] = {
|
|
|
{ "intro_point_registration",
|
|
|
test_intro_point_registration, TT_FORK, NULL, NULL },
|
|
@@ -524,6 +840,18 @@ struct testcase_t hs_intropoint_tests[] = {
|
|
|
{ "receive_establish_intro_wrong_mac",
|
|
|
test_establish_intro_wrong_mac, TT_FORK, NULL, NULL },
|
|
|
|
|
|
+ { "introduce1_suitable_circuit",
|
|
|
+ test_introduce1_suitable_circuit, TT_FORK, NULL, NULL },
|
|
|
+
|
|
|
+ { "introduce1_is_legacy",
|
|
|
+ test_introduce1_is_legacy, TT_FORK, NULL, NULL },
|
|
|
+
|
|
|
+ { "introduce1_validation",
|
|
|
+ test_introduce1_validation, TT_FORK, NULL, NULL },
|
|
|
+
|
|
|
+ { "received_introduce1_handling",
|
|
|
+ test_received_introduce1_handling, TT_FORK, NULL, NULL },
|
|
|
+
|
|
|
END_OF_TESTCASES
|
|
|
};
|
|
|
|