1 /* Copyright (c) 2016-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
5 * \file hs_intropoint.c
6 * \brief Implement next generation introductions point functionality
9 #define HS_INTROPOINT_PRIVATE
13 #include "circuitlist.h"
14 #include "circuituse.h"
20 #include "ed25519_cert.h"
21 #include "hs/cell_common.h"
22 #include "hs/cell_establish_intro.h"
23 #include "hs/cell_introduce1.h"
25 #include "hs_circuitmap.h"
26 #include "hs_descriptor.h"
27 #include "hs_intropoint.h"
28 #include "hs_common.h"
30 /** Extract the authentication key from an ESTABLISH_INTRO or INTRODUCE1 using
31 * the given <b>cell_type</b> from <b>cell</b> and place it in
32 * <b>auth_key_out</b>. */
34 get_auth_key_from_cell(ed25519_public_key_t
*auth_key_out
,
35 unsigned int cell_type
, const void *cell
)
38 const uint8_t *key_array
;
40 tor_assert(auth_key_out
);
44 case RELAY_COMMAND_ESTABLISH_INTRO
:
46 const trn_cell_establish_intro_t
*c_cell
= cell
;
47 key_array
= trn_cell_establish_intro_getconstarray_auth_key(c_cell
);
48 auth_key_len
= trn_cell_establish_intro_getlen_auth_key(c_cell
);
51 case RELAY_COMMAND_INTRODUCE1
:
53 const trn_cell_introduce1_t
*c_cell
= cell
;
54 key_array
= trn_cell_introduce1_getconstarray_auth_key(cell
);
55 auth_key_len
= trn_cell_introduce1_getlen_auth_key(c_cell
);
59 /* Getting here is really bad as it means we got a unknown cell type from
60 * this file where every call has an hardcoded value. */
61 tor_assert(0); /* LCOV_EXCL_LINE */
63 tor_assert(key_array
);
64 tor_assert(auth_key_len
== sizeof(auth_key_out
->pubkey
));
65 memcpy(auth_key_out
->pubkey
, key_array
, auth_key_len
);
68 /** We received an ESTABLISH_INTRO <b>cell</b>. Verify its signature and MAC,
69 * given <b>circuit_key_material</b>. Return 0 on success else -1 on error. */
71 verify_establish_intro_cell(const trn_cell_establish_intro_t
*cell
,
72 const uint8_t *circuit_key_material
,
73 size_t circuit_key_material_len
)
75 /* We only reach this function if the first byte of the cell is 0x02 which
76 * means that auth_key_type is of ed25519 type, hence this check should
77 * always pass. See hs_intro_received_establish_intro(). */
78 if (BUG(cell
->auth_key_type
!= HS_INTRO_AUTH_KEY_TYPE_ED25519
)) {
82 /* Make sure the auth key length is of the right size for this type. For
83 * EXTRA safety, we check both the size of the array and the length which
84 * must be the same. Safety first!*/
85 if (trn_cell_establish_intro_getlen_auth_key(cell
) != ED25519_PUBKEY_LEN
||
86 trn_cell_establish_intro_get_auth_key_len(cell
) != ED25519_PUBKEY_LEN
) {
87 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
88 "ESTABLISH_INTRO auth key length is invalid");
92 const uint8_t *msg
= cell
->start_cell
;
96 ed25519_signature_t sig_struct
;
97 const uint8_t *sig_array
=
98 trn_cell_establish_intro_getconstarray_sig(cell
);
100 /* Make sure the signature length is of the right size. For EXTRA safety,
101 * we check both the size of the array and the length which must be the
102 * same. Safety first!*/
103 if (trn_cell_establish_intro_getlen_sig(cell
) != sizeof(sig_struct
.sig
) ||
104 trn_cell_establish_intro_get_sig_len(cell
) != sizeof(sig_struct
.sig
)) {
105 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
106 "ESTABLISH_INTRO sig len is invalid");
109 /* We are now sure that sig_len is of the right size. */
110 memcpy(sig_struct
.sig
, sig_array
, cell
->sig_len
);
112 ed25519_public_key_t auth_key
;
113 get_auth_key_from_cell(&auth_key
, RELAY_COMMAND_ESTABLISH_INTRO
, cell
);
115 const size_t sig_msg_len
= cell
->end_sig_fields
- msg
;
116 int sig_mismatch
= ed25519_checksig_prefixed(&sig_struct
,
118 ESTABLISH_INTRO_SIG_PREFIX
,
121 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
122 "ESTABLISH_INTRO signature not as expected");
129 const size_t auth_msg_len
= cell
->end_mac_fields
- msg
;
130 uint8_t mac
[DIGEST256_LEN
];
131 crypto_mac_sha3_256(mac
, sizeof(mac
),
132 circuit_key_material
, circuit_key_material_len
,
134 if (tor_memneq(mac
, cell
->handshake_mac
, sizeof(mac
))) {
135 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
136 "ESTABLISH_INTRO handshake_auth not as expected");
144 /* Send an INTRO_ESTABLISHED cell to <b>circ</b>. */
146 hs_intro_send_intro_established_cell
,(or_circuit_t
*circ
))
149 uint8_t *encoded_cell
= NULL
;
150 ssize_t encoded_len
, result_len
;
151 trn_cell_intro_established_t
*cell
;
152 trn_cell_extension_t
*ext
;
156 /* Build the cell payload. */
157 cell
= trn_cell_intro_established_new();
158 ext
= trn_cell_extension_new();
159 trn_cell_extension_set_num(ext
, 0);
160 trn_cell_intro_established_set_extensions(cell
, ext
);
161 /* Encode the cell to binary format. */
162 encoded_len
= trn_cell_intro_established_encoded_len(cell
);
163 tor_assert(encoded_len
> 0);
164 encoded_cell
= tor_malloc_zero(encoded_len
);
165 result_len
= trn_cell_intro_established_encode(encoded_cell
, encoded_len
,
167 tor_assert(encoded_len
== result_len
);
169 ret
= relay_send_command_from_edge(0, TO_CIRCUIT(circ
),
170 RELAY_COMMAND_INTRO_ESTABLISHED
,
171 (char *) encoded_cell
, encoded_len
,
173 /* On failure, the above function will close the circuit. */
174 trn_cell_intro_established_free(cell
);
175 tor_free(encoded_cell
);
179 /** We received an ESTABLISH_INTRO <b>parsed_cell</b> on <b>circ</b>. It's
180 * well-formed and passed our verifications. Perform appropriate actions to
181 * establish an intro point. */
183 handle_verified_establish_intro_cell(or_circuit_t
*circ
,
184 const trn_cell_establish_intro_t
*parsed_cell
)
186 /* Get the auth key of this intro point */
187 ed25519_public_key_t auth_key
;
188 get_auth_key_from_cell(&auth_key
, RELAY_COMMAND_ESTABLISH_INTRO
,
191 /* Then notify the hidden service that the intro point is established by
192 sending an INTRO_ESTABLISHED cell */
193 if (hs_intro_send_intro_established_cell(circ
)) {
194 log_warn(LD_PROTOCOL
, "Couldn't send INTRO_ESTABLISHED cell.");
198 /* Associate intro point auth key with this circuit. */
199 hs_circuitmap_register_intro_circ_v3_relay_side(circ
, &auth_key
);
200 /* Repurpose this circuit into an intro circuit. */
201 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_INTRO_POINT
);
206 /** We just received an ESTABLISH_INTRO cell in <b>circ</b> with payload in
207 * <b>request</b>. Handle it by making <b>circ</b> an intro circuit. Return 0
208 * if everything went well, or -1 if there were errors. */
210 handle_establish_intro(or_circuit_t
*circ
, const uint8_t *request
,
213 int cell_ok
, retval
= -1;
214 trn_cell_establish_intro_t
*parsed_cell
= NULL
;
219 log_info(LD_REND
, "Received an ESTABLISH_INTRO request on circuit %" PRIu32
,
222 /* Check that the circuit is in shape to become an intro point */
223 if (!hs_intro_circuit_is_suitable_for_establish_intro(circ
)) {
228 ssize_t parsing_result
= trn_cell_establish_intro_parse(&parsed_cell
,
229 request
, request_len
);
230 if (parsing_result
< 0) {
231 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
232 "Rejecting %s ESTABLISH_INTRO cell.",
233 parsing_result
== -1 ? "invalid" : "truncated");
237 cell_ok
= verify_establish_intro_cell(parsed_cell
,
238 (uint8_t *) circ
->rend_circ_nonce
,
239 sizeof(circ
->rend_circ_nonce
));
241 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
242 "Failed to verify ESTABLISH_INTRO cell.");
246 /* This cell is legit. Take the appropriate actions. */
247 cell_ok
= handle_verified_establish_intro_cell(circ
, parsed_cell
);
257 /* When sending the intro establish ack, on error the circuit can be marked
258 * as closed so avoid a double close. */
259 if (!TO_CIRCUIT(circ
)->marked_for_close
) {
260 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
264 trn_cell_establish_intro_free(parsed_cell
);
268 /* Return True if circuit is suitable for being an intro circuit. */
270 circuit_is_suitable_intro_point(const or_circuit_t
*circ
,
271 const char *log_cell_type_str
)
274 tor_assert(log_cell_type_str
);
276 /* Basic circuit state sanity checks. */
277 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
) {
278 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
279 "Rejecting %s on non-OR circuit.", log_cell_type_str
);
283 if (circ
->base_
.n_chan
) {
284 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
285 "Rejecting %s on non-edge circuit.", log_cell_type_str
);
293 /* Return True if circuit is suitable for being service-side intro circuit. */
295 hs_intro_circuit_is_suitable_for_establish_intro(const or_circuit_t
*circ
)
297 return circuit_is_suitable_intro_point(circ
, "ESTABLISH_INTRO");
300 /* We just received an ESTABLISH_INTRO cell in <b>circ</b>. Figure out of it's
301 * a legacy or a next gen cell, and pass it to the appropriate handler. */
303 hs_intro_received_establish_intro(or_circuit_t
*circ
, const uint8_t *request
,
309 if (request_len
== 0) {
310 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
, "Empty ESTABLISH_INTRO cell.");
314 /* Using the first byte of the cell, figure out the version of
315 * ESTABLISH_INTRO and pass it to the appropriate cell handler */
316 const uint8_t first_byte
= request
[0];
317 switch (first_byte
) {
318 case HS_INTRO_AUTH_KEY_TYPE_LEGACY0
:
319 case HS_INTRO_AUTH_KEY_TYPE_LEGACY1
:
320 return rend_mid_establish_intro_legacy(circ
, request
, request_len
);
321 case HS_INTRO_AUTH_KEY_TYPE_ED25519
:
322 return handle_establish_intro(circ
, request
, request_len
);
324 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
325 "Unrecognized AUTH_KEY_TYPE %u.", first_byte
);
330 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
334 /* Send an INTRODUCE_ACK cell onto the circuit <b>circ</b> with the status
335 * value in <b>status</b>. Depending on the status, it can be ACK or a NACK.
336 * Return 0 on success else a negative value on error which will close the
339 send_introduce_ack_cell(or_circuit_t
*circ
, hs_intro_ack_status_t status
)
342 uint8_t *encoded_cell
= NULL
;
343 ssize_t encoded_len
, result_len
;
344 trn_cell_introduce_ack_t
*cell
;
345 trn_cell_extension_t
*ext
;
349 /* Setup the INTRODUCE_ACK cell. We have no extensions so the N_EXTENSIONS
350 * field is set to 0 by default with a new object. */
351 cell
= trn_cell_introduce_ack_new();
352 ret
= trn_cell_introduce_ack_set_status(cell
, status
);
353 /* We have no cell extensions in an INTRODUCE_ACK cell. */
354 ext
= trn_cell_extension_new();
355 trn_cell_extension_set_num(ext
, 0);
356 trn_cell_introduce_ack_set_extensions(cell
, ext
);
357 /* A wrong status is a very bad code flow error as this value is controlled
358 * by the code in this file and not an external input. This means we use a
359 * code that is not known by the trunnel ABI. */
360 tor_assert(ret
== 0);
361 /* Encode the payload. We should never fail to get the encoded length. */
362 encoded_len
= trn_cell_introduce_ack_encoded_len(cell
);
363 tor_assert(encoded_len
> 0);
364 encoded_cell
= tor_malloc_zero(encoded_len
);
365 result_len
= trn_cell_introduce_ack_encode(encoded_cell
, encoded_len
, cell
);
366 tor_assert(encoded_len
== result_len
);
368 ret
= relay_send_command_from_edge(CONTROL_CELL_ID
, TO_CIRCUIT(circ
),
369 RELAY_COMMAND_INTRODUCE_ACK
,
370 (char *) encoded_cell
, encoded_len
,
372 /* On failure, the above function will close the circuit. */
373 trn_cell_introduce_ack_free(cell
);
374 tor_free(encoded_cell
);
378 /* Validate a parsed INTRODUCE1 <b>cell</b>. Return 0 if valid or else a
379 * negative value for an invalid cell that should be NACKed. */
381 validate_introduce1_parsed_cell(const trn_cell_introduce1_t
*cell
)
383 size_t legacy_key_id_len
;
384 const uint8_t *legacy_key_id
;
388 /* This code path SHOULD NEVER be reached if the cell is a legacy type so
389 * safety net here. The legacy ID must be zeroes in this case. */
390 legacy_key_id_len
= trn_cell_introduce1_getlen_legacy_key_id(cell
);
391 legacy_key_id
= trn_cell_introduce1_getconstarray_legacy_key_id(cell
);
392 if (BUG(!tor_mem_is_zero((char *) legacy_key_id
, legacy_key_id_len
))) {
396 /* The auth key of an INTRODUCE1 should be of type ed25519 thus leading to a
397 * known fixed length as well. */
398 if (trn_cell_introduce1_get_auth_key_type(cell
) !=
399 HS_INTRO_AUTH_KEY_TYPE_ED25519
) {
400 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
401 "Rejecting invalid INTRODUCE1 cell auth key type. "
402 "Responding with NACK.");
405 if (trn_cell_introduce1_get_auth_key_len(cell
) != ED25519_PUBKEY_LEN
||
406 trn_cell_introduce1_getlen_auth_key(cell
) != ED25519_PUBKEY_LEN
) {
407 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
408 "Rejecting invalid INTRODUCE1 cell auth key length. "
409 "Responding with NACK.");
412 if (trn_cell_introduce1_getlen_encrypted(cell
) == 0) {
413 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
414 "Rejecting invalid INTRODUCE1 cell encrypted length. "
415 "Responding with NACK.");
424 /* We just received a non legacy INTRODUCE1 cell on <b>client_circ</b> with
425 * the payload in <b>request</b> of size <b>request_len</b>. Return 0 if
426 * everything went well, or -1 if an error occurred. This function is in charge
427 * of sending back an INTRODUCE_ACK cell and will close client_circ on error.
430 handle_introduce1(or_circuit_t
*client_circ
, const uint8_t *request
,
434 or_circuit_t
*service_circ
;
435 trn_cell_introduce1_t
*parsed_cell
;
436 hs_intro_ack_status_t status
= HS_INTRO_ACK_STATUS_SUCCESS
;
438 tor_assert(client_circ
);
441 /* Parse cell. Note that we can only parse the non encrypted section for
442 * which we'll use the authentication key to find the service introduction
443 * circuit and relay the cell on it. */
444 ssize_t cell_size
= trn_cell_introduce1_parse(&parsed_cell
, request
,
447 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
448 "Rejecting %s INTRODUCE1 cell. Responding with NACK.",
449 cell_size
== -1 ? "invalid" : "truncated");
450 /* Inform client that the INTRODUCE1 has a bad format. */
451 status
= HS_INTRO_ACK_STATUS_BAD_FORMAT
;
455 /* Once parsed validate the cell format. */
456 if (validate_introduce1_parsed_cell(parsed_cell
) < 0) {
457 /* Inform client that the INTRODUCE1 has bad format. */
458 status
= HS_INTRO_ACK_STATUS_BAD_FORMAT
;
462 /* Find introduction circuit through our circuit map. */
464 ed25519_public_key_t auth_key
;
465 get_auth_key_from_cell(&auth_key
, RELAY_COMMAND_INTRODUCE1
, parsed_cell
);
466 service_circ
= hs_circuitmap_get_intro_circ_v3_relay_side(&auth_key
);
467 if (service_circ
== NULL
) {
468 char b64_key
[ED25519_BASE64_LEN
+ 1];
469 ed25519_public_to_base64(b64_key
, &auth_key
);
470 log_info(LD_REND
, "No intro circuit found for INTRODUCE1 cell "
471 "with auth key %s from circuit %" PRIu32
". "
472 "Responding with NACK.",
473 safe_str(b64_key
), client_circ
->p_circ_id
);
474 /* Inform the client that we don't know the requested service ID. */
475 status
= HS_INTRO_ACK_STATUS_UNKNOWN_ID
;
480 /* Relay the cell to the service on its intro circuit with an INTRODUCE2
481 * cell which is the same exact payload. */
482 if (relay_send_command_from_edge(CONTROL_CELL_ID
, TO_CIRCUIT(service_circ
),
483 RELAY_COMMAND_INTRODUCE2
,
484 (char *) request
, request_len
, NULL
)) {
485 log_warn(LD_PROTOCOL
, "Unable to send INTRODUCE2 cell to the service.");
486 /* Inform the client that we can't relay the cell. */
487 status
= HS_INTRO_ACK_STATUS_CANT_RELAY
;
491 /* Success! Send an INTRODUCE_ACK success status onto the client circuit. */
492 status
= HS_INTRO_ACK_STATUS_SUCCESS
;
496 /* Send INTRODUCE_ACK or INTRODUCE_NACK to client */
497 if (send_introduce_ack_cell(client_circ
, status
) < 0) {
498 log_warn(LD_PROTOCOL
, "Unable to send an INTRODUCE ACK status %d "
499 "to client.", status
);
500 /* Circuit has been closed on failure of transmission. */
503 if (status
!= HS_INTRO_ACK_STATUS_SUCCESS
) {
504 /* We just sent a NACK that is a non success status code so close the
505 * circuit because it's not useful to keep it open. Remember, a client can
506 * only send one INTRODUCE1 cell on a circuit. */
507 circuit_mark_for_close(TO_CIRCUIT(client_circ
), END_CIRC_REASON_INTERNAL
);
510 trn_cell_introduce1_free(parsed_cell
);
514 /* Identify if the encoded cell we just received is a legacy one or not. The
515 * <b>request</b> should be at least DIGEST_LEN bytes long. */
517 introduce1_cell_is_legacy(const uint8_t *request
)
521 /* If the first 20 bytes of the cell (DIGEST_LEN) are NOT zeroes, it
522 * indicates a legacy cell (v2). */
523 if (!tor_mem_is_zero((const char *) request
, DIGEST_LEN
)) {
527 /* Not a legacy cell. */
531 /* Return true iff the circuit <b>circ</b> is suitable for receiving an
532 * INTRODUCE1 cell. */
534 circuit_is_suitable_for_introduce1(const or_circuit_t
*circ
)
538 /* Is this circuit an intro point circuit? */
539 if (!circuit_is_suitable_intro_point(circ
, "INTRODUCE1")) {
543 if (circ
->already_received_introduce1
) {
544 log_fn(LOG_PROTOCOL_WARN
, LD_REND
,
545 "Blocking multiple introductions on the same circuit. "
546 "Someone might be trying to attack a hidden service through "
554 /* We just received an INTRODUCE1 cell on <b>circ</b>. Figure out which type
555 * it is and pass it to the appropriate handler. Return 0 on success else a
556 * negative value and the circuit is closed. */
558 hs_intro_received_introduce1(or_circuit_t
*circ
, const uint8_t *request
,
566 /* A cell that can't hold a DIGEST_LEN is invalid as we need to check if
567 * it's a legacy cell or not using the first DIGEST_LEN bytes. */
568 if (request_len
< DIGEST_LEN
) {
569 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
, "Invalid INTRODUCE1 cell length.");
573 /* Make sure we have a circuit that can have an INTRODUCE1 cell on it. */
574 if (!circuit_is_suitable_for_introduce1(circ
)) {
575 /* We do not send a NACK because the circuit is not suitable for any kind
576 * of response or transmission as it's a violation of the protocol. */
579 /* Mark the circuit that we got this cell. None are allowed after this as a
580 * DoS mitigation since one circuit with one client can hammer a service. */
581 circ
->already_received_introduce1
= 1;
583 /* We are sure here to have at least DIGEST_LEN bytes. */
584 if (introduce1_cell_is_legacy(request
)) {
585 /* Handle a legacy cell. */
586 ret
= rend_mid_introduce_legacy(circ
, request
, request_len
);
588 /* Handle a non legacy cell. */
589 ret
= handle_introduce1(circ
, request
, request_len
);
594 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
598 /* Clear memory allocated by the given intropoint object ip (but don't free the
601 hs_intropoint_clear(hs_intropoint_t
*ip
)
606 tor_cert_free(ip
->auth_key_cert
);
607 SMARTLIST_FOREACH(ip
->link_specifiers
, hs_desc_link_specifier_t
*, ls
,
608 hs_desc_link_specifier_free(ls
));
609 smartlist_free(ip
->link_specifiers
);
610 memset(ip
, 0, sizeof(hs_intropoint_t
));