1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Functions to queue create cells,
10 * and parse and create the CREATE cell and its allies.
12 * This module has a few functions, all related to the CREATE/CREATED
13 * handshake that we use on links in order to create a circuit, and the
14 * related EXTEND/EXTENDED handshake that we use over circuits in order to
15 * extend them an additional hop.
17 * Clients invoke these functions when creating or extending a circuit,
18 * from circuitbuild.c.
20 * Relays invoke these functions when they receive a CREATE or EXTEND
21 * cell in command.c or relay.c, in order to queue the pending request.
22 * They also invoke them from cpuworker.c, which handles dispatching
23 * onionskin requests to different worker threads.
27 * This module also handles:
29 * <li> Queueing incoming onionskins on the relay side before passing
30 * them to worker threads.
31 * <li>Expiring onionskins on the relay side if they have waited for
33 * <li>Packaging private keys on the server side in order to pass
34 * them to worker threads.
35 * <li>Encoding and decoding CREATE, CREATED, CREATE2, and CREATED2 cells.
36 * <li>Encoding and decodign EXTEND, EXTENDED, EXTEND2, and EXTENDED2
41 #include "core/or/or.h"
43 #include "app/config/config.h"
44 #include "core/crypto/onion_crypto.h"
45 #include "core/crypto/onion_fast.h"
46 #include "core/crypto/onion_ntor.h"
47 #include "core/or/onion.h"
48 #include "feature/nodelist/networkstatus.h"
50 #include "core/or/cell_st.h"
53 #include "trunnel/ed25519_cert.h"
55 /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. If
56 * <b>unknown_ok</b> is true, allow cells with handshake types we don't
59 check_create_cell(const create_cell_t
*cell
, int unknown_ok
)
61 switch (cell
->cell_type
) {
64 case CELL_CREATE_FAST
:
65 if (cell
->handshake_type
!= ONION_HANDSHAKE_TYPE_FAST
)
74 switch (cell
->handshake_type
) {
75 case ONION_HANDSHAKE_TYPE_TAP
:
77 case ONION_HANDSHAKE_TYPE_FAST
:
78 if (cell
->handshake_len
!= CREATE_FAST_LEN
)
81 case ONION_HANDSHAKE_TYPE_NTOR
:
82 if (cell
->handshake_len
!= NTOR_ONIONSKIN_LEN
)
85 case ONION_HANDSHAKE_TYPE_NTOR_V3
:
86 /* ntor v3 has variable length fields that are checked
87 * elsewhere. Fall through to always valid here. */
97 /** Write the various parameters into the create cell. Separate from
98 * create_cell_parse() to make unit testing easier.
101 create_cell_init(create_cell_t
*cell_out
, uint8_t cell_type
,
102 uint16_t handshake_type
, uint16_t handshake_len
,
103 const uint8_t *onionskin
)
105 memset(cell_out
, 0, sizeof(*cell_out
));
107 cell_out
->cell_type
= cell_type
;
108 cell_out
->handshake_type
= handshake_type
;
109 cell_out
->handshake_len
= handshake_len
;
110 memcpy(cell_out
->onionskin
, onionskin
, handshake_len
);
113 /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
114 * <b>p_len</b> bytes long, and use it to fill the fields of
115 * <b>cell_out</b>. Return 0 on success and -1 on failure.
117 * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
118 * this function is also used for parsing those.
121 parse_create2_payload(create_cell_t
*cell_out
, const uint8_t *p
, size_t p_len
)
123 uint16_t handshake_type
, handshake_len
;
128 handshake_type
= ntohs(get_uint16(p
));
129 handshake_len
= ntohs(get_uint16(p
+2));
131 if (handshake_len
> CELL_PAYLOAD_SIZE
- 4 || handshake_len
> p_len
- 4)
133 if (handshake_type
== ONION_HANDSHAKE_TYPE_FAST
)
136 create_cell_init(cell_out
, CELL_CREATE2
, handshake_type
, handshake_len
,
141 /** Magic string which, in a CREATE or EXTEND cell, indicates that a seeming
142 * TAP payload is really an ntor payload. We'd do away with this if every
143 * relay supported EXTEND2, but we want to be able to extend from A to B with
144 * ntor even when A doesn't understand EXTEND2 and so can't generate a
147 #define NTOR_CREATE_MAGIC "ntorNTORntorNTOR"
149 /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
150 * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
151 * syntactically valid CREATE2 cells that we can't generate or react to.) */
153 create_cell_parse(create_cell_t
*cell_out
, const cell_t
*cell_in
)
155 switch (cell_in
->command
) {
158 case CELL_CREATE_FAST
:
159 create_cell_init(cell_out
, CELL_CREATE_FAST
, ONION_HANDSHAKE_TYPE_FAST
,
160 CREATE_FAST_LEN
, cell_in
->payload
);
163 if (parse_create2_payload(cell_out
, cell_in
->payload
,
164 CELL_PAYLOAD_SIZE
) < 0)
171 return check_create_cell(cell_out
, 0);
174 /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
176 check_created_cell(const created_cell_t
*cell
)
178 switch (cell
->cell_type
) {
181 case CELL_CREATED_FAST
:
182 if (cell
->handshake_len
!= CREATED_FAST_LEN
)
186 if (cell
->handshake_len
> RELAY_PAYLOAD_SIZE
-2)
194 /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
195 * <b>cell_out</b>. Return 0 on success, -1 on failure. */
197 created_cell_parse(created_cell_t
*cell_out
, const cell_t
*cell_in
)
199 memset(cell_out
, 0, sizeof(*cell_out
));
201 switch (cell_in
->command
) {
204 case CELL_CREATED_FAST
:
205 cell_out
->cell_type
= CELL_CREATED_FAST
;
206 cell_out
->handshake_len
= CREATED_FAST_LEN
;
207 memcpy(cell_out
->reply
, cell_in
->payload
, CREATED_FAST_LEN
);
211 const uint8_t *p
= cell_in
->payload
;
212 cell_out
->cell_type
= CELL_CREATED2
;
213 cell_out
->handshake_len
= ntohs(get_uint16(p
));
214 if (cell_out
->handshake_len
> CELL_PAYLOAD_SIZE
- 2)
216 memcpy(cell_out
->reply
, p
+2, cell_out
->handshake_len
);
221 return check_created_cell(cell_out
);
224 /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
226 check_extend_cell(const extend_cell_t
*cell
)
228 const bool is_extend2
= (cell
->cell_type
== RELAY_COMMAND_EXTEND2
);
230 if (tor_digest_is_zero((const char*)cell
->node_id
))
232 if (!tor_addr_port_is_valid_ap(&cell
->orport_ipv4
, 0)) {
233 /* EXTEND cells must have an IPv4 address. */
237 /* EXTEND2 cells must have at least one IP address.
238 * It can be IPv4 or IPv6. */
239 if (!tor_addr_port_is_valid_ap(&cell
->orport_ipv6
, 0)) {
243 if (cell
->create_cell
.cell_type
== CELL_CREATE
) {
245 } else if (cell
->create_cell
.cell_type
== CELL_CREATE2
) {
246 if (cell
->cell_type
!= RELAY_COMMAND_EXTEND2
)
249 /* In particular, no CREATE_FAST cells are allowed */
252 if (cell
->create_cell
.handshake_type
== ONION_HANDSHAKE_TYPE_FAST
||
253 cell
->create_cell
.handshake_type
== ONION_HANDSHAKE_TYPE_TAP
)
256 return check_create_cell(&cell
->create_cell
, 1);
260 create_cell_from_create2_cell_body(create_cell_t
*cell_out
,
261 const create2_cell_body_t
*cell
)
263 tor_assert(cell_out
);
265 memset(cell_out
, 0, sizeof(create_cell_t
));
266 if (BUG(cell
->handshake_len
> sizeof(cell_out
->onionskin
))) {
267 /* This should be impossible because there just isn't enough room in the
268 * input cell to make the handshake_len this large and provide a
269 * handshake_data to match. */
273 cell_out
->cell_type
= CELL_CREATE2
;
274 cell_out
->handshake_type
= cell
->handshake_type
;
275 cell_out
->handshake_len
= cell
->handshake_len
;
276 memcpy(cell_out
->onionskin
,
277 create2_cell_body_getconstarray_handshake_data(cell
),
278 cell
->handshake_len
);
283 extend_cell_from_extend2_cell_body(extend_cell_t
*cell_out
,
284 const extend2_cell_body_t
*cell
)
286 tor_assert(cell_out
);
288 int found_ipv4
= 0, found_ipv6
= 0, found_rsa_id
= 0, found_ed_id
= 0;
289 memset(cell_out
, 0, sizeof(*cell_out
));
290 tor_addr_make_unspec(&cell_out
->orport_ipv4
.addr
);
291 tor_addr_make_unspec(&cell_out
->orport_ipv6
.addr
);
292 cell_out
->cell_type
= RELAY_COMMAND_EXTEND2
;
295 for (i
= 0; i
< cell
->n_spec
; ++i
) {
296 const link_specifier_t
*ls
= extend2_cell_body_getconst_ls(cell
, i
);
297 switch (ls
->ls_type
) {
302 tor_addr_from_ipv4h(&cell_out
->orport_ipv4
.addr
, ls
->un_ipv4_addr
);
303 cell_out
->orport_ipv4
.port
= ls
->un_ipv4_port
;
309 tor_addr_from_ipv6_bytes(&cell_out
->orport_ipv6
.addr
,
311 cell_out
->orport_ipv6
.port
= ls
->un_ipv6_port
;
317 memcpy(cell_out
->node_id
, ls
->un_legacy_id
, 20);
323 memcpy(cell_out
->ed_pubkey
.pubkey
, ls
->un_ed25519_id
, 32);
326 /* Ignore this, whatever it is. */
331 /* EXTEND2 cells must have an RSA ID */
335 /* EXTEND2 cells must have at least one IP address */
336 if (!found_ipv4
&& !found_ipv6
)
339 return create_cell_from_create2_cell_body(&cell_out
->create_cell
,
343 /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
344 * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
345 * 0 on success, -1 on failure. */
347 extend_cell_parse
,(extend_cell_t
*cell_out
,
348 const uint8_t command
,
349 const uint8_t *payload
,
350 size_t payload_length
))
353 tor_assert(cell_out
);
356 if (payload_length
> RELAY_PAYLOAD_SIZE
)
360 case RELAY_COMMAND_EXTEND
:
363 case RELAY_COMMAND_EXTEND2
:
365 extend2_cell_body_t
*cell
= NULL
;
366 if (extend2_cell_body_parse(&cell
, payload
, payload_length
) < 0 ||
369 extend2_cell_body_free(cell
);
372 int r
= extend_cell_from_extend2_cell_body(cell_out
, cell
);
373 extend2_cell_body_free(cell
);
382 return check_extend_cell(cell_out
);
385 /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
387 check_extended_cell(const extended_cell_t
*cell
)
390 if (cell
->created_cell
.cell_type
== CELL_CREATED
) {
391 if (cell
->cell_type
!= RELAY_COMMAND_EXTENDED
)
393 } else if (cell
->created_cell
.cell_type
== CELL_CREATED2
) {
394 if (cell
->cell_type
!= RELAY_COMMAND_EXTENDED2
)
400 return check_created_cell(&cell
->created_cell
);
403 /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
404 * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
405 * 0 on success, -1 on failure. */
407 extended_cell_parse(extended_cell_t
*cell_out
,
408 const uint8_t command
, const uint8_t *payload
,
411 tor_assert(cell_out
);
414 memset(cell_out
, 0, sizeof(*cell_out
));
415 if (payload_len
> RELAY_PAYLOAD_SIZE
)
419 case RELAY_COMMAND_EXTENDED
:
421 case RELAY_COMMAND_EXTENDED2
:
423 cell_out
->cell_type
= RELAY_COMMAND_EXTENDED2
;
424 cell_out
->created_cell
.cell_type
= CELL_CREATED2
;
425 cell_out
->created_cell
.handshake_len
= ntohs(get_uint16(payload
));
426 if (cell_out
->created_cell
.handshake_len
> RELAY_PAYLOAD_SIZE
- 2 ||
427 cell_out
->created_cell
.handshake_len
> payload_len
- 2)
429 memcpy(cell_out
->created_cell
.reply
, payload
+2,
430 cell_out
->created_cell
.handshake_len
);
437 return check_extended_cell(cell_out
);
440 /** Fill <b>cell_out</b> with a correctly formatted version of the
441 * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
442 * failure. This is a cell we didn't originate if <b>relayed</b> is true. */
444 create_cell_format_impl(cell_t
*cell_out
, const create_cell_t
*cell_in
,
449 if (check_create_cell(cell_in
, relayed
) < 0)
452 memset(cell_out
->payload
, 0, sizeof(cell_out
->payload
));
453 cell_out
->command
= cell_in
->cell_type
;
455 p
= cell_out
->payload
;
456 space
= sizeof(cell_out
->payload
);
458 switch (cell_in
->cell_type
) {
460 if (BUG(cell_in
->handshake_type
== ONION_HANDSHAKE_TYPE_NTOR_V3
)) {
461 log_warn(LD_BUG
, "Create cells cannot contain ntorv3.");
465 if (cell_in
->handshake_type
== ONION_HANDSHAKE_TYPE_NTOR
) {
466 memcpy(p
, NTOR_CREATE_MAGIC
, 16);
471 case CELL_CREATE_FAST
:
472 tor_assert(cell_in
->handshake_len
<= space
);
473 memcpy(p
, cell_in
->onionskin
, cell_in
->handshake_len
);
476 tor_assert(cell_in
->handshake_len
<= sizeof(cell_out
->payload
)-4);
477 set_uint16(cell_out
->payload
, htons(cell_in
->handshake_type
));
478 set_uint16(cell_out
->payload
+2, htons(cell_in
->handshake_len
));
479 memcpy(cell_out
->payload
+ 4, cell_in
->onionskin
, cell_in
->handshake_len
);
489 create_cell_format(cell_t
*cell_out
, const create_cell_t
*cell_in
)
491 return create_cell_format_impl(cell_out
, cell_in
, 0);
495 create_cell_format_relayed(cell_t
*cell_out
, const create_cell_t
*cell_in
)
497 return create_cell_format_impl(cell_out
, cell_in
, 1);
500 /** Fill <b>cell_out</b> with a correctly formatted version of the
501 * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
504 created_cell_format(cell_t
*cell_out
, const created_cell_t
*cell_in
)
506 if (check_created_cell(cell_in
) < 0)
509 memset(cell_out
->payload
, 0, sizeof(cell_out
->payload
));
510 cell_out
->command
= cell_in
->cell_type
;
512 switch (cell_in
->cell_type
) {
514 case CELL_CREATED_FAST
:
515 tor_assert(cell_in
->handshake_len
<= sizeof(cell_out
->payload
));
516 memcpy(cell_out
->payload
, cell_in
->reply
, cell_in
->handshake_len
);
519 tor_assert(cell_in
->handshake_len
<= sizeof(cell_out
->payload
)-2);
520 set_uint16(cell_out
->payload
, htons(cell_in
->handshake_len
));
521 memcpy(cell_out
->payload
+ 2, cell_in
->reply
, cell_in
->handshake_len
);
529 /** Return true iff we are configured (by torrc or by the networkstatus
530 * parameters) to use Ed25519 identities in our Extend2 cells. */
532 should_include_ed25519_id_extend_cells(const networkstatus_t
*ns
,
533 const or_options_t
*options
)
535 if (options
->ExtendByEd25519ID
!= -1)
536 return options
->ExtendByEd25519ID
; /* The user has an opinion. */
538 return (int) networkstatus_get_param(ns
, "ExtendByEd25519ID",
544 /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
545 * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
546 * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
547 * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
549 extend_cell_format(uint8_t *command_out
, uint16_t *len_out
,
550 uint8_t *payload_out
, const extend_cell_t
*cell_in
)
553 if (check_extend_cell(cell_in
) < 0)
558 memset(p
, 0, RELAY_PAYLOAD_SIZE
);
560 switch (cell_in
->cell_type
) {
561 case RELAY_COMMAND_EXTEND
:
563 case RELAY_COMMAND_EXTEND2
:
565 uint8_t n_specifiers
= 1;
566 *command_out
= RELAY_COMMAND_EXTEND2
;
567 extend2_cell_body_t
*cell
= extend2_cell_body_new();
568 link_specifier_t
*ls
;
569 if (tor_addr_port_is_valid_ap(&cell_in
->orport_ipv4
, 0)) {
570 /* Maybe IPv4 specifier first. */
572 ls
= link_specifier_new();
573 extend2_cell_body_add_ls(cell
, ls
);
574 ls
->ls_type
= LS_IPV4
;
576 ls
->un_ipv4_addr
= tor_addr_to_ipv4h(&cell_in
->orport_ipv4
.addr
);
577 ls
->un_ipv4_port
= cell_in
->orport_ipv4
.port
;
581 ls
= link_specifier_new();
582 extend2_cell_body_add_ls(cell
, ls
);
583 ls
->ls_type
= LS_LEGACY_ID
;
584 ls
->ls_len
= DIGEST_LEN
;
585 memcpy(ls
->un_legacy_id
, cell_in
->node_id
, DIGEST_LEN
);
587 if (should_include_ed25519_id_extend_cells(NULL
, get_options()) &&
588 !ed25519_public_key_is_zero(&cell_in
->ed_pubkey
)) {
589 /* Then, maybe, the ed25519 id! */
591 ls
= link_specifier_new();
592 extend2_cell_body_add_ls(cell
, ls
);
593 ls
->ls_type
= LS_ED25519_ID
;
595 memcpy(ls
->un_ed25519_id
, cell_in
->ed_pubkey
.pubkey
, 32);
597 if (tor_addr_port_is_valid_ap(&cell_in
->orport_ipv6
, 0)) {
598 /* Then maybe IPv6 specifier. */
600 ls
= link_specifier_new();
601 extend2_cell_body_add_ls(cell
, ls
);
602 ls
->ls_type
= LS_IPV6
;
604 tor_addr_copy_ipv6_bytes(ls
->un_ipv6_addr
,
605 &cell_in
->orport_ipv6
.addr
);
606 ls
->un_ipv6_port
= cell_in
->orport_ipv6
.port
;
608 cell
->n_spec
= n_specifiers
;
610 /* Now, the handshake */
611 cell
->create2
= create2_cell_body_new();
612 cell
->create2
->handshake_type
= cell_in
->create_cell
.handshake_type
;
613 cell
->create2
->handshake_len
= cell_in
->create_cell
.handshake_len
;
614 create2_cell_body_setlen_handshake_data(cell
->create2
,
615 cell_in
->create_cell
.handshake_len
);
616 memcpy(create2_cell_body_getarray_handshake_data(cell
->create2
),
617 cell_in
->create_cell
.onionskin
,
618 cell_in
->create_cell
.handshake_len
);
620 ssize_t len_encoded
= extend2_cell_body_encode(
621 payload_out
, RELAY_PAYLOAD_SIZE
,
623 extend2_cell_body_free(cell
);
624 if (len_encoded
< 0 || len_encoded
> UINT16_MAX
)
626 *len_out
= (uint16_t) len_encoded
;
636 /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
637 * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
638 * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
639 * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
641 extended_cell_format(uint8_t *command_out
, uint16_t *len_out
,
642 uint8_t *payload_out
, const extended_cell_t
*cell_in
)
645 if (check_extended_cell(cell_in
) < 0)
649 memset(p
, 0, RELAY_PAYLOAD_SIZE
);
651 switch (cell_in
->cell_type
) {
652 case RELAY_COMMAND_EXTENDED
:
654 case RELAY_COMMAND_EXTENDED2
:
656 *command_out
= RELAY_COMMAND_EXTENDED2
;
657 *len_out
= 2 + cell_in
->created_cell
.handshake_len
;
658 set_uint16(payload_out
, htons(cell_in
->created_cell
.handshake_len
));
659 if (2+cell_in
->created_cell
.handshake_len
> RELAY_PAYLOAD_SIZE
)
661 memcpy(payload_out
+2, cell_in
->created_cell
.reply
,
662 cell_in
->created_cell
.handshake_len
);