Tor 0.4.9.9
Loading...
Searching...
No Matches
hs_intropoint.c
Go to the documentation of this file.
1/* Copyright (c) 2016-2021, The Tor Project, Inc. */
2/* See LICENSE for licensing information */
3
4/**
5 * \file hs_intropoint.c
6 * \brief Implement next generation introductions point functionality
7 **/
8
9#define HS_INTROPOINT_PRIVATE
10
11#include "core/or/or.h"
12#include "app/config/config.h"
13#include "core/or/channel.h"
14#include "core/or/circuitlist.h"
15#include "core/or/circuituse.h"
16#include "core/or/relay.h"
17#include "core/or/relay_msg.h"
23
24/* Trunnel */
25#include "trunnel/ed25519_cert.h"
26#include "trunnel/extension.h"
27#include "trunnel/hs/cell_establish_intro.h"
28#include "trunnel/hs/cell_introduce1.h"
29
34#include "feature/hs/hs_dos.h"
36
37#include "core/or/or_circuit_st.h"
38
39/** Extract the authentication key from an ESTABLISH_INTRO or INTRODUCE1 using
40 * the given <b>cell_type</b> from <b>cell</b> and place it in
41 * <b>auth_key_out</b>. */
42STATIC void
44 unsigned int cell_type, const void *cell)
45{
46 size_t auth_key_len;
47 const uint8_t *key_array;
48
49 tor_assert(auth_key_out);
50 tor_assert(cell);
51
52 switch (cell_type) {
53 case RELAY_COMMAND_ESTABLISH_INTRO:
54 {
55 const trn_cell_establish_intro_t *c_cell = cell;
56 key_array = trn_cell_establish_intro_getconstarray_auth_key(c_cell);
57 auth_key_len = trn_cell_establish_intro_getlen_auth_key(c_cell);
58 break;
59 }
60 case RELAY_COMMAND_INTRODUCE1:
61 {
62 const trn_cell_introduce1_t *c_cell = cell;
63 key_array = trn_cell_introduce1_getconstarray_auth_key(cell);
64 auth_key_len = trn_cell_introduce1_getlen_auth_key(c_cell);
65 break;
66 }
67 default:
68 /* Getting here is really bad as it means we got a unknown cell type from
69 * this file where every call has an hardcoded value. */
70 tor_assert_unreached(); /* LCOV_EXCL_LINE */
71 }
72 tor_assert(key_array);
73 tor_assert(auth_key_len == sizeof(auth_key_out->pubkey));
74 memcpy(auth_key_out->pubkey, key_array, auth_key_len);
75}
76
77/** We received an ESTABLISH_INTRO <b>cell</b>. Verify its signature and MAC,
78 * given <b>circuit_key_material</b>. Return 0 on success else -1 on error. */
79STATIC int
80verify_establish_intro_cell(const trn_cell_establish_intro_t *cell,
81 const uint8_t *circuit_key_material,
82 size_t circuit_key_material_len)
83{
84 /* We only reach this function if the first byte of the cell is 0x02 which
85 * means that auth_key_type is of ed25519 type, hence this check should
86 * always pass. See hs_intro_received_establish_intro(). */
87 if (BUG(cell->auth_key_type != TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519)) {
88 return -1;
89 }
90
91 /* Make sure the auth key length is of the right size for this type. For
92 * EXTRA safety, we check both the size of the array and the length which
93 * must be the same. Safety first!*/
94 if (trn_cell_establish_intro_getlen_auth_key(cell) != ED25519_PUBKEY_LEN ||
95 trn_cell_establish_intro_get_auth_key_len(cell) != ED25519_PUBKEY_LEN) {
96 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
97 "ESTABLISH_INTRO auth key length is invalid");
98 return -1;
99 }
100
101 const uint8_t *msg = cell->start_cell;
102
103 /* Verify the sig */
104 {
105 ed25519_signature_t sig_struct;
106 const uint8_t *sig_array =
107 trn_cell_establish_intro_getconstarray_sig(cell);
108
109 /* Make sure the signature length is of the right size. For EXTRA safety,
110 * we check both the size of the array and the length which must be the
111 * same. Safety first!*/
112 if (trn_cell_establish_intro_getlen_sig(cell) != sizeof(sig_struct.sig) ||
113 trn_cell_establish_intro_get_sig_len(cell) != sizeof(sig_struct.sig)) {
114 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
115 "ESTABLISH_INTRO sig len is invalid");
116 return -1;
117 }
118 /* We are now sure that sig_len is of the right size. */
119 memcpy(sig_struct.sig, sig_array, cell->sig_len);
120
121 ed25519_public_key_t auth_key;
122 get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO, cell);
123
124 const size_t sig_msg_len = cell->end_sig_fields - msg;
125 int sig_mismatch = ed25519_checksig_prefixed(&sig_struct,
126 msg, sig_msg_len,
128 &auth_key);
129 if (sig_mismatch) {
130 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
131 "ESTABLISH_INTRO signature not as expected");
132 return -1;
133 }
134 }
135
136 /* Verify the MAC */
137 {
138 const size_t auth_msg_len = cell->end_mac_fields - msg;
139 uint8_t mac[DIGEST256_LEN];
140 crypto_mac_sha3_256(mac, sizeof(mac),
141 circuit_key_material, circuit_key_material_len,
142 msg, auth_msg_len);
143 if (tor_memneq(mac, cell->handshake_mac, sizeof(mac))) {
144 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
145 "ESTABLISH_INTRO handshake_auth not as expected");
146 return -1;
147 }
148 }
149
150 return 0;
151}
152
153/** Send an INTRO_ESTABLISHED cell to <b>circ</b>. */
154MOCK_IMPL(int,
156{
157 int ret;
158 uint8_t *encoded_cell = NULL;
159 ssize_t encoded_len, result_len;
160 trn_cell_intro_established_t *cell;
161 trn_extension_t *ext;
162
163 tor_assert(circ);
164
165 /* Build the cell payload. */
166 cell = trn_cell_intro_established_new();
167 ext = trn_extension_new();
168 trn_extension_set_num(ext, 0);
169 trn_cell_intro_established_set_extensions(cell, ext);
170 /* Encode the cell to binary format. */
171 encoded_len = trn_cell_intro_established_encoded_len(cell);
172 tor_assert(encoded_len > 0);
173 encoded_cell = tor_malloc_zero(encoded_len);
174 result_len = trn_cell_intro_established_encode(encoded_cell, encoded_len,
175 cell);
176 tor_assert(encoded_len == result_len);
177
178 ret = relay_send_command_from_edge(0, TO_CIRCUIT(circ),
179 RELAY_COMMAND_INTRO_ESTABLISHED,
180 (char *) encoded_cell, encoded_len,
181 NULL);
182 /* On failure, the above function will close the circuit. */
183 trn_cell_intro_established_free(cell);
184 tor_free(encoded_cell);
185 return ret;
186}
187
188/** Validate the cell DoS extension parameters. Return true iff they've been
189 * bound check and can be used. Else return false. See proposal 305 for
190 * details and reasons about this validation. */
191STATIC bool
192cell_dos_extension_parameters_are_valid(uint64_t intro2_rate_per_sec,
193 uint64_t intro2_burst_per_sec)
194{
195 bool ret = false;
196
197 /* Check that received value is not below the minimum. Don't check if minimum
198 is set to 0, since the param is a positive value and gcc will complain. */
199#if HS_CONFIG_V3_DOS_DEFENSE_RATE_PER_SEC_MIN > 0
200 if (intro2_rate_per_sec < HS_CONFIG_V3_DOS_DEFENSE_RATE_PER_SEC_MIN) {
201 log_fn(LOG_PROTOCOL_WARN, LD_REND,
202 "Intro point DoS defenses rate per second is "
203 "too small. Received value: %" PRIu64, intro2_rate_per_sec);
204 goto end;
205 }
206#endif /* HS_CONFIG_V3_DOS_DEFENSE_RATE_PER_SEC_MIN > 0 */
207
208 /* Check that received value is not above maximum */
209 if (intro2_rate_per_sec > HS_CONFIG_V3_DOS_DEFENSE_RATE_PER_SEC_MAX) {
210 log_fn(LOG_PROTOCOL_WARN, LD_REND,
211 "Intro point DoS defenses rate per second is "
212 "too big. Received value: %" PRIu64, intro2_rate_per_sec);
213 goto end;
214 }
215
216 /* Check that received value is not below the minimum */
217#if HS_CONFIG_V3_DOS_DEFENSE_BURST_PER_SEC_MIN > 0
218 if (intro2_burst_per_sec < HS_CONFIG_V3_DOS_DEFENSE_BURST_PER_SEC_MIN) {
219 log_fn(LOG_PROTOCOL_WARN, LD_REND,
220 "Intro point DoS defenses burst per second is "
221 "too small. Received value: %" PRIu64, intro2_burst_per_sec);
222 goto end;
223 }
224#endif /* HS_CONFIG_V3_DOS_DEFENSE_BURST_PER_SEC_MIN > 0 */
225
226 /* Check that received value is not above maximum */
227 if (intro2_burst_per_sec > HS_CONFIG_V3_DOS_DEFENSE_BURST_PER_SEC_MAX) {
228 log_fn(LOG_PROTOCOL_WARN, LD_REND,
229 "Intro point DoS defenses burst per second is "
230 "too big. Received value: %" PRIu64, intro2_burst_per_sec);
231 goto end;
232 }
233
234 /* In a rate limiting scenario, burst can never be smaller than the rate. At
235 * best it can be equal. */
236 if (intro2_burst_per_sec < intro2_rate_per_sec) {
237 log_info(LD_REND, "Intro point DoS defenses burst is smaller than rate. "
238 "Rate: %" PRIu64 " vs Burst: %" PRIu64,
239 intro2_rate_per_sec, intro2_burst_per_sec);
240 goto end;
241 }
242
243 /* Passing validation. */
244 ret = true;
245
246 end:
247 return ret;
248}
249
250/** Parse the cell DoS extension and apply defenses on the given circuit if
251 * validation passes. If the cell extension is malformed or contains unusable
252 * values, the DoS defenses is disabled on the circuit. */
253static void
255 const trn_extension_field_t *field,
256 or_circuit_t *circ)
257{
258 ssize_t ret;
259 uint64_t intro2_rate_per_sec = 0, intro2_burst_per_sec = 0;
260 trn_cell_extension_dos_t *dos = NULL;
261
262 tor_assert(field);
263 tor_assert(circ);
264
265 ret = trn_cell_extension_dos_parse(&dos,
266 trn_extension_field_getconstarray_field(field),
267 trn_extension_field_getlen_field(field));
268 if (ret < 0) {
269 goto end;
270 }
271
272 for (size_t i = 0; i < trn_cell_extension_dos_get_n_params(dos); i++) {
273 const trn_cell_extension_dos_param_t *param =
274 trn_cell_extension_dos_getconst_params(dos, i);
275 if (BUG(param == NULL)) {
276 goto end;
277 }
278
279 switch (trn_cell_extension_dos_param_get_type(param)) {
280 case TRUNNEL_DOS_PARAM_TYPE_INTRO2_RATE_PER_SEC:
281 intro2_rate_per_sec = trn_cell_extension_dos_param_get_value(param);
282 break;
283 case TRUNNEL_DOS_PARAM_TYPE_INTRO2_BURST_PER_SEC:
284 intro2_burst_per_sec = trn_cell_extension_dos_param_get_value(param);
285 break;
286 default:
287 goto end;
288 }
289 }
290
291 /* At this point, the extension is valid so any values out of it implies
292 * that it was set explicitly and thus flag the circuit that it should not
293 * look at the consensus for that reason for the defenses' values. */
295
296 /* A value of 0 is valid in the sense that we accept it but we still disable
297 * the defenses so return false. */
298 if (intro2_rate_per_sec == 0 || intro2_burst_per_sec == 0) {
299 log_info(LD_REND, "Intro point DoS defenses parameter set to 0. "
300 "Disabling INTRO2 DoS defenses on circuit id %u",
301 circ->p_circ_id);
303 goto end;
304 }
305
306 /* If invalid, we disable the defense on the circuit. */
307 if (!cell_dos_extension_parameters_are_valid(intro2_rate_per_sec,
308 intro2_burst_per_sec)) {
310 log_info(LD_REND, "Disabling INTRO2 DoS defenses on circuit id %u",
311 circ->p_circ_id);
312 goto end;
313 }
314
315 /* We passed validation, enable defenses and apply rate/burst. */
317
318 /* Initialize the INTRODUCE2 token bucket for the rate limiting. */
320 (uint32_t) intro2_rate_per_sec,
321 (uint32_t) intro2_burst_per_sec,
322 (uint32_t) monotime_coarse_absolute_sec());
323 log_info(LD_REND, "Intro point DoS defenses enabled. Rate is %" PRIu64
324 " and Burst is %" PRIu64,
325 intro2_rate_per_sec, intro2_burst_per_sec);
326
327 end:
328 trn_cell_extension_dos_free(dos);
329 return;
330}
331
332/** Parse every cell extension in the given ESTABLISH_INTRO cell. */
333static void
335 const trn_cell_establish_intro_t *parsed_cell,
336 or_circuit_t *circ)
337{
338 const trn_extension_t *extensions;
339
340 tor_assert(parsed_cell);
341 tor_assert(circ);
342
343 extensions = trn_cell_establish_intro_getconst_extensions(parsed_cell);
344 if (extensions == NULL) {
345 goto end;
346 }
347
348 /* Go over all extensions. */
349 for (size_t idx = 0; idx < trn_extension_get_num(extensions); idx++) {
350 const trn_extension_field_t *field =
351 trn_extension_getconst_fields(extensions, idx);
352 if (BUG(field == NULL)) {
353 /* The number of extensions should match the number of fields. */
354 break;
355 }
356
357 switch (trn_extension_field_get_field_type(field)) {
358 case TRUNNEL_CELL_EXTENSION_TYPE_DOS:
359 /* After this, the circuit should be set for DoS defenses. */
361 break;
362 default:
363 /* Unknown extension. Skip over. */
364 break;
365 }
366 }
367
368 end:
369 return;
370}
371
372/** We received an ESTABLISH_INTRO <b>parsed_cell</b> on <b>circ</b>. It's
373 * well-formed and passed our verifications. Perform appropriate actions to
374 * establish an intro point. */
375static int
377 const trn_cell_establish_intro_t *parsed_cell)
378{
379 /* Get the auth key of this intro point */
380 ed25519_public_key_t auth_key;
381 get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO,
382 parsed_cell);
383
384 /* Setup INTRODUCE2 defenses on the circuit. Must be done before parsing the
385 * cell extension that can possibly change the defenses' values. */
387
388 /* Handle cell extension if any. */
390
391 /* Then notify the hidden service that the intro point is established by
392 sending an INTRO_ESTABLISHED cell */
394 log_warn(LD_PROTOCOL, "Couldn't send INTRO_ESTABLISHED cell.");
395 return -1;
396 }
397
398 /* Associate intro point auth key with this circuit. */
400 /* Repurpose this circuit into an intro circuit. */
402
403 return 0;
404}
405
406/** We just received an ESTABLISH_INTRO cell in <b>circ</b> with payload in
407 * <b>request</b>. Handle it by making <b>circ</b> an intro circuit. Return 0
408 * if everything went well, or -1 if there were errors. */
409static int
410handle_establish_intro(or_circuit_t *circ, const uint8_t *request,
411 size_t request_len)
412{
413 int cell_ok, retval = -1;
414 trn_cell_establish_intro_t *parsed_cell = NULL;
415
416 tor_assert(circ);
417 tor_assert(request);
418
419 log_info(LD_REND, "Received an ESTABLISH_INTRO request on circuit %" PRIu32,
420 circ->p_circ_id);
421
422 /* Check that the circuit is in shape to become an intro point */
424 relay_increment_est_intro_action(EST_INTRO_UNSUITABLE_CIRCUIT);
425 goto err;
426 }
427
428 /* Parse the cell */
429 ssize_t parsing_result = trn_cell_establish_intro_parse(&parsed_cell,
430 request, request_len);
431 if (parsing_result < 0) {
432 relay_increment_est_intro_action(EST_INTRO_MALFORMED);
433 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
434 "Rejecting %s ESTABLISH_INTRO cell.",
435 parsing_result == -1 ? "invalid" : "truncated");
436 goto err;
437 }
438
439 cell_ok = verify_establish_intro_cell(parsed_cell,
440 (uint8_t *) circ->rend_circ_nonce,
441 sizeof(circ->rend_circ_nonce));
442 if (cell_ok < 0) {
443 relay_increment_est_intro_action(EST_INTRO_MALFORMED);
444 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
445 "Failed to verify ESTABLISH_INTRO cell.");
446 goto err;
447 }
448
449 /* This cell is legit. Take the appropriate actions. */
450 cell_ok = handle_verified_establish_intro_cell(circ, parsed_cell);
451 if (cell_ok < 0) {
452 relay_increment_est_intro_action(EST_INTRO_CIRCUIT_DEAD);
453 goto err;
454 }
455
456 relay_increment_est_intro_action(EST_INTRO_SUCCESS);
457 /* We are done! */
458 retval = 0;
459 goto done;
460
461 err:
462 /* When sending the intro establish ack, on error the circuit can be marked
463 * as closed so avoid a double close. */
464 if (!TO_CIRCUIT(circ)->marked_for_close) {
465 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
466 }
467
468 done:
469 trn_cell_establish_intro_free(parsed_cell);
470 return retval;
471}
472
473/** Return True if circuit is suitable for being an intro circuit. */
474static int
476 const char *log_cell_type_str)
477{
478 tor_assert(circ);
479 tor_assert(log_cell_type_str);
480
481 /* Basic circuit state sanity checks. */
482 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
483 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
484 "Rejecting %s on non-OR circuit.", log_cell_type_str);
485 return 0;
486 }
487
488 if (circ->base_.n_chan) {
489 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
490 "Rejecting %s on non-edge circuit.", log_cell_type_str);
491 return 0;
492 }
493
494 /* Suitable. */
495 return 1;
496}
497
498/** Return True if circuit is suitable for being service-side intro circuit. */
499int
504
505/** We just received an ESTABLISH_INTRO cell in <b>circ</b>. Pass it to the
506 * appropriate handler. */
507int
509 size_t request_len)
510{
511 tor_assert(circ);
512 tor_assert(request);
513
514 if (request_len == 0) {
515 relay_increment_est_intro_action(EST_INTRO_MALFORMED);
516 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Empty ESTABLISH_INTRO cell.");
517 goto err;
518 }
519
520 /* Using the first byte of the cell, figure out the version of
521 * ESTABLISH_INTRO and pass it to the appropriate cell handler */
522 const uint8_t first_byte = request[0];
523 switch (first_byte) {
524 case TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_LEGACY0:
525 case TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_LEGACY1:
526 /* Likely version 2 onion service which is now obsolete. Avoid a
527 * protocol warning considering they still exists on the network. */
528 relay_increment_est_intro_action(EST_INTRO_MALFORMED);
529 goto err;
530 case TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519:
531 return handle_establish_intro(circ, request, request_len);
532 default:
533 relay_increment_est_intro_action(EST_INTRO_MALFORMED);
534 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
535 "Unrecognized AUTH_KEY_TYPE %u.", first_byte);
536 goto err;
537 }
538
539 err:
540 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
541 return -1;
542}
543
544/** Send an INTRODUCE_ACK cell onto the circuit <b>circ</b> with the status
545 * value in <b>status</b>. Depending on the status, it can be ACK or a NACK.
546 * Return 0 on success else a negative value on error which will close the
547 * circuit. */
548static int
550{
551 int ret = -1;
552 uint8_t *encoded_cell = NULL;
553 ssize_t encoded_len, result_len;
554 trn_cell_introduce_ack_t *cell;
555 trn_extension_t *ext;
556
557 tor_assert(circ);
558
559 /* Setup the INTRODUCE_ACK cell. We have no extensions so the N_EXTENSIONS
560 * field is set to 0 by default with a new object. */
561 cell = trn_cell_introduce_ack_new();
562 ret = trn_cell_introduce_ack_set_status(cell, status);
563 /* We have no cell extensions in an INTRODUCE_ACK cell. */
564 ext = trn_extension_new();
565 trn_extension_set_num(ext, 0);
566 trn_cell_introduce_ack_set_extensions(cell, ext);
567 /* A wrong status is a very bad code flow error as this value is controlled
568 * by the code in this file and not an external input. This means we use a
569 * code that is not known by the trunnel ABI. */
570 tor_assert(ret == 0);
571 /* Encode the payload. We should never fail to get the encoded length. */
572 encoded_len = trn_cell_introduce_ack_encoded_len(cell);
573 tor_assert(encoded_len > 0);
574 encoded_cell = tor_malloc_zero(encoded_len);
575 result_len = trn_cell_introduce_ack_encode(encoded_cell, encoded_len, cell);
576 tor_assert(encoded_len == result_len);
577
578 ret = relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
579 RELAY_COMMAND_INTRODUCE_ACK,
580 (char *) encoded_cell, encoded_len,
581 NULL);
582 /* On failure, the above function will close the circuit. */
583 trn_cell_introduce_ack_free(cell);
584 tor_free(encoded_cell);
585 return ret;
586}
587
588/** Validate a parsed INTRODUCE1 <b>cell</b>. Return 0 if valid or else a
589 * negative value for an invalid cell that should be NACKed. */
590STATIC int
591validate_introduce1_parsed_cell(const trn_cell_introduce1_t *cell)
592{
593 size_t legacy_key_id_len;
594 const uint8_t *legacy_key_id;
595
596 tor_assert(cell);
597
598 /* This code path SHOULD NEVER be reached if the cell is a legacy type so
599 * safety net here. The legacy ID must be zeroes in this case. */
600 legacy_key_id_len = trn_cell_introduce1_getlen_legacy_key_id(cell);
601 legacy_key_id = trn_cell_introduce1_getconstarray_legacy_key_id(cell);
602 if (BUG(!fast_mem_is_zero((char *) legacy_key_id, legacy_key_id_len))) {
603 goto invalid;
604 }
605
606 /* The auth key of an INTRODUCE1 should be of type ed25519 thus leading to a
607 * known fixed length as well. */
608 if (trn_cell_introduce1_get_auth_key_type(cell) !=
609 TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519) {
610 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
611 "Rejecting invalid INTRODUCE1 cell auth key type. "
612 "Responding with NACK.");
613 goto invalid;
614 }
615 if (trn_cell_introduce1_get_auth_key_len(cell) != ED25519_PUBKEY_LEN ||
616 trn_cell_introduce1_getlen_auth_key(cell) != ED25519_PUBKEY_LEN) {
617 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
618 "Rejecting invalid INTRODUCE1 cell auth key length. "
619 "Responding with NACK.");
620 goto invalid;
621 }
622 if (trn_cell_introduce1_getlen_encrypted(cell) == 0) {
623 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
624 "Rejecting invalid INTRODUCE1 cell encrypted length. "
625 "Responding with NACK.");
626 goto invalid;
627 }
628
629 return 0;
630 invalid:
631 return -1;
632}
633
634/** We just received a non legacy INTRODUCE1 cell on <b>client_circ</b> with
635 * the payload in <b>request</b> of size <b>request_len</b>. Return 0 if
636 * everything went well, or -1 if an error occurred. This function is in charge
637 * of sending back an INTRODUCE_ACK cell and will close client_circ on error.
638 */
639STATIC int
640handle_introduce1(or_circuit_t *client_circ, const uint8_t *request,
641 size_t request_len)
642{
643 int ret = -1;
644 or_circuit_t *service_circ;
645 trn_cell_introduce1_t *parsed_cell = NULL;
646 uint16_t status = TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS;
647
648 tor_assert(client_circ);
649 tor_assert(request);
650
651 /* Make sure the cell we're going to send toward the onion service is
652 * small enough to fit in a CGO-style circuit. Otherwise we should fail
653 * it right here, rather than trying to send it toward the onion
654 * service, maybe discovering that it doesn't fit, and maybe tearing
655 * down that intro circuit.
656 *
657 * Note that we refuse this cell in that case even if it would fit
658 * (that is, even if the onion service side isn't using a CGO-style
659 * circuit), because we don't want to reveal to the client which kind
660 * of circuit the onion service made. */
661 if (request_len >
663 RELAY_COMMAND_INTRODUCE2)) {
664 relay_increment_intro1_action(INTRO1_MALFORMED);
665 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
666 "Rejecting over-long INTRODUCE1 cell. Responding with NACK.");
667 /* Inform client that the INTRODUCE1 has a bad format. */
668 status = TRUNNEL_HS_INTRO_ACK_STATUS_BAD_FORMAT;
669 goto send_ack;
670 }
671
672 /* Parse cell. Note that we can only parse the non encrypted section for
673 * which we'll use the authentication key to find the service introduction
674 * circuit and relay the cell on it. */
675 ssize_t cell_size = trn_cell_introduce1_parse(&parsed_cell, request,
676 request_len);
677 if (cell_size < 0) {
678 relay_increment_intro1_action(INTRO1_MALFORMED);
679 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
680 "Rejecting %s INTRODUCE1 cell. Responding with NACK.",
681 cell_size == -1 ? "invalid" : "truncated");
682 /* Inform client that the INTRODUCE1 has a bad format. */
683 status = TRUNNEL_HS_INTRO_ACK_STATUS_BAD_FORMAT;
684 goto send_ack;
685 }
686
687 /* Once parsed validate the cell format. */
688 if (validate_introduce1_parsed_cell(parsed_cell) < 0) {
689 relay_increment_intro1_action(INTRO1_MALFORMED);
690 /* Inform client that the INTRODUCE1 has bad format. */
691 status = TRUNNEL_HS_INTRO_ACK_STATUS_BAD_FORMAT;
692 goto send_ack;
693 }
694
695 /* Find introduction circuit through our circuit map. */
696 {
697 ed25519_public_key_t auth_key;
698 get_auth_key_from_cell(&auth_key, RELAY_COMMAND_INTRODUCE1, parsed_cell);
699 service_circ = hs_circuitmap_get_intro_circ_v3_relay_side(&auth_key);
700 if (service_circ == NULL) {
701 relay_increment_intro1_action(INTRO1_UNKNOWN_SERVICE);
702 char b64_key[ED25519_BASE64_LEN + 1];
703 ed25519_public_to_base64(b64_key, &auth_key);
704 log_info(LD_REND, "No intro circuit found for INTRODUCE1 cell "
705 "with auth key %s from circuit %" PRIu32 ". "
706 "Responding with NACK.",
707 safe_str(b64_key), client_circ->p_circ_id);
708 /* Inform the client that we don't know the requested service ID. */
709 status = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
710 goto send_ack;
711 }
712 }
713
714 /* Before sending, let's make sure this cell can be sent on the service
715 * circuit asking the DoS defenses. */
716 if (!hs_dos_can_send_intro2(service_circ)) {
717 relay_increment_intro1_action(INTRO1_RATE_LIMITED);
718 char *msg;
719 static ratelim_t rlimit = RATELIM_INIT(5 * 60);
720 if ((msg = rate_limit_log(&rlimit, approx_time()))) {
721 log_info(LD_PROTOCOL, "Can't relay INTRODUCE1 v3 cell due to DoS "
722 "limitations. Sending NACK to client.");
723 tor_free(msg);
724 }
725 status = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
726 goto send_ack;
727 }
728
729 /* Relay the cell to the service on its intro circuit with an INTRODUCE2
730 * cell which is the same exact payload. */
731 if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(service_circ),
732 RELAY_COMMAND_INTRODUCE2,
733 (char *) request, request_len, NULL)) {
734 relay_increment_intro1_action(INTRO1_CIRCUIT_DEAD);
735 /* Inform the client that we can't relay the cell. Use the unknown ID
736 * status code since it means that we do not know the service. */
737 status = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
738 goto send_ack;
739 }
740
741 relay_increment_intro1_action(INTRO1_SUCCESS);
742 /* Success! Send an INTRODUCE_ACK success status onto the client circuit. */
743 status = TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS;
744 ret = 0;
745
746 send_ack:
747 /* Send INTRODUCE_ACK or INTRODUCE_NACK to client */
748 if (send_introduce_ack_cell(client_circ, status) < 0) {
749 log_warn(LD_PROTOCOL, "Unable to send an INTRODUCE ACK status %d "
750 "to client.", status);
751 /* Circuit has been closed on failure of transmission. */
752 goto done;
753 }
754 done:
755 trn_cell_introduce1_free(parsed_cell);
756 return ret;
757}
758
759/** Return true iff the circuit <b>circ</b> is suitable for receiving an
760 * INTRODUCE1 cell. */
761STATIC int
763{
764 tor_assert(circ);
765
766 /* Is this circuit an intro point circuit? */
767 if (!circuit_is_suitable_intro_point(circ, "INTRODUCE1")) {
768 return 0;
769 }
770
771 if (circ->already_received_introduce1) {
772 relay_increment_intro1_action(INTRO1_CIRCUIT_REUSED);
773 log_fn(LOG_PROTOCOL_WARN, LD_REND,
774 "Blocking multiple introductions on the same circuit. "
775 "Someone might be trying to attack a hidden service through "
776 "this relay.");
777 return 0;
778 }
779
780 /* Disallow single hop client circuit. */
781 if (circ->p_chan && channel_is_client(circ->p_chan)) {
782 relay_increment_intro1_action(INTRO1_SINGLE_HOP);
783 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
784 "Single hop client was rejected while trying to introduce. "
785 "Closing circuit.");
786 return 0;
787 }
788
789 return 1;
790}
791
792/** We just received an INTRODUCE1 cell on <b>circ</b>. Figure out which type
793 * it is and pass it to the appropriate handler. Return 0 on success else a
794 * negative value and the circuit is closed. */
795int
796hs_intro_received_introduce1(or_circuit_t *circ, const uint8_t *request,
797 size_t request_len)
798{
799 tor_assert(circ);
800 tor_assert(request);
801
802 /* A cell that can't hold a DIGEST_LEN is invalid. */
803 if (request_len < DIGEST_LEN) {
804 relay_increment_intro1_action(INTRO1_MALFORMED);
805 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Invalid INTRODUCE1 cell length.");
806 goto err;
807 }
808
809 /* Make sure we have a circuit that can have an INTRODUCE1 cell on it. */
811 /* We do not send a NACK because the circuit is not suitable for any kind
812 * of response or transmission as it's a violation of the protocol. */
813 goto err;
814 }
815 /* Mark the circuit that we got this cell. None are allowed after this as a
816 * DoS mitigation since one circuit with one client can hammer a service. */
817 circ->already_received_introduce1 = 1;
818
819 /* Handle the cell. */
820 return handle_introduce1(circ, request, request_len);
821
822 err:
823 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
824 return -1;
825}
826
827/** Clear memory allocated by the given intropoint object ip (but don't free
828 * the object itself). */
829void
831{
832 if (ip == NULL) {
833 return;
834 }
835 tor_cert_free(ip->auth_key_cert);
836 SMARTLIST_FOREACH(ip->link_specifiers, link_specifier_t *, ls,
837 link_specifier_free(ls));
838 smartlist_free(ip->link_specifiers);
839 memset(ip, 0, sizeof(hs_intropoint_t));
840}
time_t approx_time(void)
Definition approx_time.c:32
int channel_is_client(const channel_t *chan)
Definition channel.c:2917
Header file for channel.c.
Header file for circuitlist.c.
#define CIRCUIT_PURPOSE_INTRO_POINT
Definition circuitlist.h:42
#define CIRCUIT_PURPOSE_OR
Definition circuitlist.h:39
void circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
Header file for circuituse.c.
Functions and types for monotonic times.
Header file for config.c.
void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out, const uint8_t *key, size_t key_len, const uint8_t *msg, size_t msg_len)
int ed25519_checksig_prefixed(const ed25519_signature_t *signature, const uint8_t *msg, size_t msg_len, const char *prefix_str, const ed25519_public_key_t *pubkey)
void ed25519_public_to_base64(char *output, const ed25519_public_key_t *pkey)
Header for crypto_format.c.
#define tor_memneq(a, b, sz)
Definition di_ops.h:21
#define DIGEST_LEN
#define DIGEST256_LEN
or_circuit_t * hs_circuitmap_get_intro_circ_v3_relay_side(const ed25519_public_key_t *auth_key)
void hs_circuitmap_register_intro_circ_v3_relay_side(or_circuit_t *circ, const ed25519_public_key_t *auth_key)
Header file for hs_circuitmap.c.
Header file containing common data for the whole HS subsystem.
#define ESTABLISH_INTRO_SIG_PREFIX
Definition hs_common.h:50
Header file containing configuration ABI/API for the HS subsystem.
Header file for hs_descriptor.c.
bool hs_dos_can_send_intro2(or_circuit_t *s_intro_circ)
Definition hs_dos.c:167
void hs_dos_setup_default_intro2_defenses(or_circuit_t *circ)
Definition hs_dos.c:138
Header file containing denial of service defenses for the HS subsystem for all versions.
STATIC bool cell_dos_extension_parameters_are_valid(uint64_t intro2_rate_per_sec, uint64_t intro2_burst_per_sec)
static int circuit_is_suitable_intro_point(const or_circuit_t *circ, const char *log_cell_type_str)
static int handle_establish_intro(or_circuit_t *circ, const uint8_t *request, size_t request_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)
void hs_intropoint_clear(hs_intropoint_t *ip)
int hs_intro_received_introduce1(or_circuit_t *circ, const uint8_t *request, size_t request_len)
STATIC int handle_introduce1(or_circuit_t *client_circ, const uint8_t *request, size_t request_len)
static void handle_establish_intro_cell_extensions(const trn_cell_establish_intro_t *parsed_cell, or_circuit_t *circ)
int hs_intro_send_intro_established_cell(or_circuit_t *circ)
int hs_intro_received_establish_intro(or_circuit_t *circ, const uint8_t *request, size_t request_len)
int hs_intro_circuit_is_suitable_for_establish_intro(const or_circuit_t *circ)
static int send_introduce_ack_cell(or_circuit_t *circ, uint16_t status)
STATIC int validate_introduce1_parsed_cell(const trn_cell_introduce1_t *cell)
STATIC void get_auth_key_from_cell(ed25519_public_key_t *auth_key_out, unsigned int cell_type, const void *cell)
static void handle_establish_intro_cell_dos_extension(const trn_extension_field_t *field, or_circuit_t *circ)
STATIC int circuit_is_suitable_for_introduce1(const or_circuit_t *circ)
static int handle_verified_establish_intro_cell(or_circuit_t *circ, const trn_cell_establish_intro_t *parsed_cell)
Header file for hs_intropoint.c.
#define log_fn(severity, domain, args,...)
Definition log.h:283
#define LD_REND
Definition log.h:84
#define LD_PROTOCOL
Definition log.h:72
#define tor_free(p)
Definition malloc.h:56
Master header file for Tor-specific functionality.
#define TO_CIRCUIT(x)
Definition or.h:951
@ RELAY_CELL_FORMAT_V1
Definition or.h:542
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition ratelim.c:42
Header file for relay.c.
Header for feature/relay/relay_metrics.c.
Header file for relay_msg.c.
static size_t relay_cell_max_payload_size(relay_cell_fmt_t format, uint8_t relay_command)
Definition relay_msg.h:65
Header file for rendmid.c.
Header file for rephist.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
uint8_t purpose
Definition circuit_st.h:112
channel_t * n_chan
Definition circuit_st.h:70
tor_cert_t * auth_key_cert
smartlist_t * link_specifiers
channel_t * p_chan
unsigned int introduce2_dos_defense_enabled
circid_t p_circ_id
unsigned int introduce2_dos_defense_explicit
token_bucket_ctr_t introduce2_bucket
char rend_circ_nonce[DIGEST_LEN]
#define STATIC
Definition testsupport.h:32
#define MOCK_IMPL(rv, funcname, arglist)
void token_bucket_ctr_init(token_bucket_ctr_t *bucket, uint32_t rate, uint32_t burst, uint32_t now_ts_sec)
#define tor_assert(expr)
Definition util_bug.h:103
int fast_mem_is_zero(const char *mem, size_t len)
Definition util_string.c:76
#define ED25519_BASE64_LEN
#define ED25519_PUBKEY_LEN