aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_hs_intropoint.c
blob: 608988ba9ad30a8b0f7de78f0a3215f1b19f18ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* Copyright (c) 2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file test_hs_service.c
 * \brief Test hidden service functionality.
 */

#define HS_SERVICE_PRIVATE
#define HS_INTROPOINT_PRIVATE
#define RENDSERVICE_PRIVATE
#define CIRCUITLIST_PRIVATE

#include "test.h"
#include "crypto.h"

#include "or.h"
#include "ht.h"

#include "hs/cell_establish_intro.h"
#include "hs_service.h"
#include "hs_circuitmap.h"
#include "hs_intropoint.h"

#include "circuitlist.h"
#include "circuituse.h"
#include "rendservice.h"

/* Mock function to avoid networking in unittests */
static int
mock_send_intro_established_cell(or_circuit_t *circ)
{
  (void) circ;
  return 0;
}

/* Try sending an ESTABLISH_INTRO cell on a circuit that is already an intro
 * point. Should fail. */
static void
test_establish_intro_wrong_purpose(void *arg)
{
  int retval;
  hs_cell_establish_intro_t *establish_intro_cell = NULL;
  or_circuit_t *intro_circ = or_circuit_new(0,NULL);;
  uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  ssize_t cell_len = 0;
  uint8_t circuit_key_material[DIGEST_LEN] = {0};

  (void)arg;

  /* Get the auth key of the intro point */
  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  memcpy(intro_circ->rend_circ_nonce, circuit_key_material, DIGEST_LEN);

  /* Set a bad circuit purpose!! :) */
  circuit_change_purpose(TO_CIRCUIT(intro_circ), CIRCUIT_PURPOSE_INTRO_POINT);

  /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
     attempt to parse it. */
  establish_intro_cell = generate_establish_intro_cell(circuit_key_material,
                                           sizeof(circuit_key_material));
  tt_assert(establish_intro_cell);
  cell_len = get_establish_intro_payload(cell_body, sizeof(cell_body),
                                         establish_intro_cell);
  tt_int_op(cell_len, >, 0);

  /* Receive the cell */
  retval = hs_intro_received_establish_intro(intro_circ, cell_body, cell_len);
  tt_int_op(retval, ==, -1);

 done:
  hs_cell_establish_intro_free(establish_intro_cell);
  circuit_free(TO_CIRCUIT(intro_circ));
}

/* Prepare a circuit for accepting an ESTABLISH_INTRO cell */
static void
helper_prepare_circ_for_intro(or_circuit_t *circ,
                              uint8_t *circuit_key_material)
{
  /* Prepare the circuit for the incoming ESTABLISH_INTRO */
  circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
  memcpy(circ->rend_circ_nonce, circuit_key_material, DIGEST_LEN);
}

/* Send an empty ESTABLISH_INTRO cell. Should fail. */
static void
test_establish_intro_wrong_keytype(void *arg)
{
  int retval;
  or_circuit_t *intro_circ = or_circuit_new(0,NULL);;
  uint8_t circuit_key_material[DIGEST_LEN] = {0};

  (void)arg;

  /* Get the auth key of the intro point */
  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  helper_prepare_circ_for_intro(intro_circ, circuit_key_material);

  /* Receive the cell. Should fail. */
  retval = hs_intro_received_establish_intro(intro_circ, (uint8_t*)"", 0);
  tt_int_op(retval, ==, -1);

 done:
  circuit_free(TO_CIRCUIT(intro_circ));
}

/* Send an ESTABLISH_INTRO cell with an unknown auth key type. Should fail. */
static void
test_establish_intro_wrong_keytype2(void *arg)
{
  int retval;
  hs_cell_establish_intro_t *establish_intro_cell = NULL;
  or_circuit_t *intro_circ = or_circuit_new(0,NULL);;
  uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  ssize_t cell_len = 0;
  uint8_t circuit_key_material[DIGEST_LEN] = {0};

  (void)arg;

  /* Get the auth key of the intro point */
  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  helper_prepare_circ_for_intro(intro_circ, circuit_key_material);

  /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
     attempt to parse it. */
  establish_intro_cell = generate_establish_intro_cell(circuit_key_material,
                                           sizeof(circuit_key_material));
  tt_assert(establish_intro_cell);
  cell_len = get_establish_intro_payload(cell_body, sizeof(cell_body),
                                         establish_intro_cell);
  tt_int_op(cell_len, >, 0);

  /* Mutate the auth key type! :) */
  cell_body[0] = 42;

  /* Receive the cell. Should fail. */
  retval = hs_intro_received_establish_intro(intro_circ, cell_body, cell_len);
  tt_int_op(retval, ==, -1);

 done:
  hs_cell_establish_intro_free(establish_intro_cell);
  circuit_free(TO_CIRCUIT(intro_circ));
}

/* Send a legit ESTABLISH_INTRO cell but slightly change the signature. Should
 * fail. */
static void
test_establish_intro_wrong_sig(void *arg)
{
  int retval;
  hs_cell_establish_intro_t *establish_intro_cell = NULL;
  or_circuit_t *intro_circ = or_circuit_new(0,NULL);;
  uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  ssize_t cell_len = 0;
  uint8_t circuit_key_material[DIGEST_LEN] = {0};

  (void)arg;

  /* Get the auth key of the intro point */
  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  helper_prepare_circ_for_intro(intro_circ, circuit_key_material);

  /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
     attempt to parse it. */
  establish_intro_cell = generate_establish_intro_cell(circuit_key_material,
                                           sizeof(circuit_key_material));
  tt_assert(establish_intro_cell);
  cell_len = get_establish_intro_payload(cell_body, sizeof(cell_body),
                                         establish_intro_cell);
  tt_int_op(cell_len, >, 0);

  /* Mutate the last byte (signature)! :) */
  cell_body[cell_len-1]++;

  /* Receive the cell. Should fail. */
  retval = hs_intro_received_establish_intro(intro_circ, cell_body, cell_len);
  tt_int_op(retval, ==, -1);

 done:
  hs_cell_establish_intro_free(establish_intro_cell);
  circuit_free(TO_CIRCUIT(intro_circ));
}

/* Helper function: Send a well-formed v3 ESTABLISH_INTRO cell to
 * <b>intro_circ</b>. Return the cell. */
static hs_cell_establish_intro_t *
helper_establish_intro_v3(or_circuit_t *intro_circ)
{
  int retval;
  hs_cell_establish_intro_t *establish_intro_cell = NULL;
  uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  ssize_t cell_len = 0;
  uint8_t circuit_key_material[DIGEST_LEN] = {0};

  tt_assert(intro_circ);

  /* Prepare the circuit for the incoming ESTABLISH_INTRO */
  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  helper_prepare_circ_for_intro(intro_circ, circuit_key_material);

  /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
     attempt to parse it. */
  establish_intro_cell = generate_establish_intro_cell(circuit_key_material,
                                           sizeof(circuit_key_material));
  tt_assert(establish_intro_cell);
  cell_len = get_establish_intro_payload(cell_body, sizeof(cell_body),
                                         establish_intro_cell);
  tt_int_op(cell_len, >, 0);

  /* Receive the cell */
  retval = hs_intro_received_establish_intro(intro_circ, cell_body, cell_len);
  tt_int_op(retval, ==, 0);

 done:
  return establish_intro_cell;
}

/* Helper function: Send a well-formed v2 ESTABLISH_INTRO cell to
 * <b>intro_circ</b>. Return the public key advertised in the cell. */
static crypto_pk_t *
helper_establish_intro_v2(or_circuit_t *intro_circ)
{
  crypto_pk_t *key1 = NULL;
  int retval;
  uint8_t cell_body[RELAY_PAYLOAD_SIZE];
  ssize_t cell_len = 0;
  uint8_t circuit_key_material[DIGEST_LEN] = {0};

  tt_assert(intro_circ);

  /* Prepare the circuit for the incoming ESTABLISH_INTRO */
  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  helper_prepare_circ_for_intro(intro_circ, circuit_key_material);

  /* Send legacy establish_intro */
  key1 = pk_generate(0);

  /* Use old circuit_key_material why not */
  cell_len = encode_establish_intro_cell_legacy((char*)cell_body,
                                                key1,
                                                (char *) circuit_key_material);
  tt_int_op(cell_len, >, 0);

  /* Receive legacy establish_intro */
  retval = hs_intro_received_establish_intro(intro_circ,
                                       cell_body, cell_len);
  tt_int_op(retval, ==, 0);

 done:
  return key1;
}

/** Successfuly register a v2 intro point and a v3 intro point. Ensure that HS
 *  circuitmap is maintained properly. */
static void
test_intro_point_registration(void *arg)
{
  int retval;
  hs_circuitmap_ht *the_hs_circuitmap = NULL;

  or_circuit_t *intro_circ = NULL;
  hs_cell_establish_intro_t *establish_intro_cell = NULL;
  ed25519_public_key_t auth_key;

  crypto_pk_t *legacy_auth_key = NULL;
  or_circuit_t *legacy_intro_circ = NULL;

  or_circuit_t *returned_intro_circ = NULL;

  (void) arg;

  MOCK(hs_intro_send_intro_established_cell, mock_send_intro_established_cell);

  hs_circuitmap_init();

  /* Check that the circuitmap is currently empty */
  {
    the_hs_circuitmap = get_hs_circuitmap();
    tt_assert(the_hs_circuitmap);
    tt_int_op(0, ==, HT_SIZE(the_hs_circuitmap));
    /* Do a circuitmap query in any case */
    returned_intro_circ = hs_circuitmap_get_intro_circ_v3(&auth_key);
    tt_ptr_op(returned_intro_circ, ==, NULL);
  }

  /* Create a v3 intro point */
  {
    intro_circ = or_circuit_new(0, NULL);
    tt_assert(intro_circ);
    establish_intro_cell = helper_establish_intro_v3(intro_circ);

    /* Check that the intro point was registered on the HS circuitmap */
    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);
    returned_intro_circ = hs_circuitmap_get_intro_circ_v3(&auth_key);
    tt_ptr_op(intro_circ, ==, returned_intro_circ);
  }

  /* Create a v2 intro point */
  {
    char key_digest[DIGEST_LEN];

    legacy_intro_circ = or_circuit_new(1, NULL);
    tt_assert(legacy_intro_circ);
    legacy_auth_key = helper_establish_intro_v2(legacy_intro_circ);
    tt_assert(legacy_auth_key);

    /* Check that the circuitmap now has two elements */
    the_hs_circuitmap = get_hs_circuitmap();
    tt_assert(the_hs_circuitmap);
    tt_int_op(2, ==, HT_SIZE(the_hs_circuitmap));

    /* Check that the new element is our legacy intro circuit. */
    retval = crypto_pk_get_digest(legacy_auth_key, key_digest);
    tt_int_op(retval, ==, 0);
    returned_intro_circ= hs_circuitmap_get_intro_circ_v2((uint8_t*)key_digest);
    tt_ptr_op(legacy_intro_circ, ==, returned_intro_circ);
  }

  /* XXX Continue test and try to register a second v3 intro point with the
   * same auth key. Make sure that old intro circuit gets closed. */

 done:
  crypto_pk_free(legacy_auth_key);
  circuit_free(TO_CIRCUIT(intro_circ));
  circuit_free(TO_CIRCUIT(legacy_intro_circ));
  hs_cell_establish_intro_free(establish_intro_cell);

  { /* Test circuitmap free_all function. */
    the_hs_circuitmap = get_hs_circuitmap();
    tt_assert(the_hs_circuitmap);
    hs_circuitmap_free_all();
    the_hs_circuitmap = get_hs_circuitmap();
    tt_assert(!the_hs_circuitmap);
  }

  UNMOCK(hs_intro_send_intro_established_cell);
}

struct testcase_t hs_intropoint_tests[] = {
  { "intro_point_registration",
    test_intro_point_registration, TT_FORK, NULL, NULL },

  { "receive_establish_intro_wrong_keytype",
    test_establish_intro_wrong_keytype, TT_FORK, NULL, NULL },

  { "receive_establish_intro_wrong_keytype2",
    test_establish_intro_wrong_keytype2, TT_FORK, NULL, NULL },

  { "receive_establish_intro_wrong_purpose",
    test_establish_intro_wrong_purpose, TT_FORK, NULL, NULL },

  { "receive_establish_intro_wrong_sig",
    test_establish_intro_wrong_sig, TT_FORK, NULL, NULL },

  END_OF_TESTCASES
};