channeltls.c (84506B)
1 /* * Copyright (c) 2012-2021, The Tor Project, Inc. */ 2 /* See LICENSE for licensing information */ 3 4 /** 5 * \file channeltls.c 6 * 7 * \brief A concrete subclass of channel_t using or_connection_t to transfer 8 * cells between Tor instances. 9 * 10 * This module fills in the various function pointers in channel_t, to 11 * implement the channel_tls_t channels as used in Tor today. These channels 12 * are created from channel_tls_connect() and 13 * channel_tls_handle_incoming(). Each corresponds 1:1 to or_connection_t 14 * object, as implemented in connection_or.c. These channels transmit cells 15 * to the underlying or_connection_t by calling 16 * connection_or_write_*_cell_to_buf(), and receive cells from the underlying 17 * or_connection_t when connection_or_process_cells_from_inbuf() calls 18 * channel_tls_handle_*_cell(). 19 * 20 * Here we also implement the server (responder) side of the v3+ Tor link 21 * handshake, which uses CERTS and AUTHENTICATE cell to negotiate versions, 22 * exchange expected and observed IP and time information, and bootstrap a 23 * level of authentication higher than we have gotten on the raw TLS 24 * handshake. 25 * 26 * NOTE: Since there is currently only one type of channel, there are probably 27 * more than a few cases where functionality that is currently in 28 * channeltls.c, connection_or.c, and channel.c ought to be divided up 29 * differently. The right time to do this is probably whenever we introduce 30 * our next channel type. 31 **/ 32 33 /* 34 * Define this so channel.h gives us things only channel_t subclasses 35 * should touch. 36 */ 37 #define CHANNEL_OBJECT_PRIVATE 38 39 #define CHANNELTLS_PRIVATE 40 41 #include "core/or/or.h" 42 #include "core/or/channel.h" 43 #include "core/or/channeltls.h" 44 #include "core/or/circuitmux.h" 45 #include "core/or/circuitmux_ewma.h" 46 #include "core/or/command.h" 47 #include "core/or/dos.h" 48 #include "app/config/config.h" 49 #include "app/config/resolve_addr.h" 50 #include "core/mainloop/connection.h" 51 #include "core/or/connection_or.h" 52 #include "feature/relay/relay_handshake.h" 53 #include "feature/control/control.h" 54 #include "feature/client/entrynodes.h" 55 #include "trunnel/link_handshake.h" 56 #include "core/or/relay.h" 57 #include "feature/stats/rephist.h" 58 #include "feature/stats/geoip_stats.h" 59 #include "feature/relay/router.h" 60 #include "feature/relay/routermode.h" 61 #include "feature/nodelist/dirlist.h" 62 #include "core/or/scheduler.h" 63 #include "feature/nodelist/torcert.h" 64 #include "feature/nodelist/networkstatus.h" 65 #include "trunnel/channelpadding_negotiation.h" 66 #include "trunnel/netinfo.h" 67 #include "core/or/channelpadding.h" 68 #include "core/or/extendinfo.h" 69 #include "core/or/congestion_control_common.h" 70 71 #include "core/or/cell_st.h" 72 #include "core/or/cell_queue_st.h" 73 #include "core/or/or_connection_st.h" 74 #include "core/or/or_handshake_certs_st.h" 75 #include "core/or/or_handshake_state_st.h" 76 #include "feature/nodelist/routerinfo_st.h" 77 #include "core/or/var_cell_st.h" 78 #include "feature/relay/relay_find_addr.h" 79 80 #include "lib/tls/tortls.h" 81 #include "lib/tls/x509.h" 82 83 /** How many CELL_PADDING cells have we received, ever? */ 84 uint64_t stats_n_padding_cells_processed = 0; 85 /** How many CELL_VERSIONS cells have we received, ever? */ 86 uint64_t stats_n_versions_cells_processed = 0; 87 /** How many CELL_NETINFO cells have we received, ever? */ 88 uint64_t stats_n_netinfo_cells_processed = 0; 89 /** How many CELL_VPADDING cells have we received, ever? */ 90 uint64_t stats_n_vpadding_cells_processed = 0; 91 /** How many CELL_CERTS cells have we received, ever? */ 92 uint64_t stats_n_certs_cells_processed = 0; 93 /** How many CELL_AUTH_CHALLENGE cells have we received, ever? */ 94 uint64_t stats_n_auth_challenge_cells_processed = 0; 95 /** How many CELL_AUTHENTICATE cells have we received, ever? */ 96 uint64_t stats_n_authenticate_cells_processed = 0; 97 /** How many CELL_AUTHORIZE cells have we received, ever? */ 98 uint64_t stats_n_authorize_cells_processed = 0; 99 100 /** Active listener, if any */ 101 static channel_listener_t *channel_tls_listener = NULL; 102 103 /* channel_tls_t method declarations */ 104 105 static void channel_tls_close_method(channel_t *chan); 106 static const char * channel_tls_describe_transport_method(channel_t *chan); 107 static void channel_tls_free_method(channel_t *chan); 108 static double channel_tls_get_overhead_estimate_method(channel_t *chan); 109 static int channel_tls_get_remote_addr_method(const channel_t *chan, 110 tor_addr_t *addr_out); 111 static int 112 channel_tls_get_transport_name_method(channel_t *chan, char **transport_out); 113 static const char *channel_tls_describe_peer_method(const channel_t *chan); 114 static int channel_tls_has_queued_writes_method(channel_t *chan); 115 static int channel_tls_is_canonical_method(channel_t *chan); 116 static int 117 channel_tls_matches_extend_info_method(channel_t *chan, 118 extend_info_t *extend_info); 119 static int channel_tls_matches_target_method(channel_t *chan, 120 const tor_addr_t *target); 121 static int channel_tls_num_cells_writeable_method(channel_t *chan); 122 static size_t channel_tls_num_bytes_queued_method(channel_t *chan); 123 static int channel_tls_write_cell_method(channel_t *chan, 124 cell_t *cell); 125 static int channel_tls_write_packed_cell_method(channel_t *chan, 126 packed_cell_t *packed_cell); 127 static int channel_tls_write_var_cell_method(channel_t *chan, 128 var_cell_t *var_cell); 129 130 /* channel_listener_tls_t method declarations */ 131 132 static void channel_tls_listener_close_method(channel_listener_t *chan_l); 133 static const char * 134 channel_tls_listener_describe_transport_method(channel_listener_t *chan_l); 135 136 /** Handle incoming cells for the handshake stuff here rather than 137 * passing them on up. */ 138 139 static void channel_tls_process_versions_cell(var_cell_t *cell, 140 channel_tls_t *tlschan); 141 static void channel_tls_process_netinfo_cell(cell_t *cell, 142 channel_tls_t *tlschan); 143 static int command_allowed_before_handshake(uint8_t command); 144 static int enter_v3_handshake_with_cell(var_cell_t *cell, 145 channel_tls_t *tlschan); 146 static void channel_tls_process_padding_negotiate_cell(cell_t *cell, 147 channel_tls_t *chan); 148 149 /** 150 * Do parts of channel_tls_t initialization common to channel_tls_connect() 151 * and channel_tls_handle_incoming(). 152 */ 153 STATIC void 154 channel_tls_common_init(channel_tls_t *tlschan) 155 { 156 channel_t *chan; 157 158 tor_assert(tlschan); 159 160 chan = &(tlschan->base_); 161 channel_init(chan); 162 chan->magic = TLS_CHAN_MAGIC; 163 chan->state = CHANNEL_STATE_OPENING; 164 chan->close = channel_tls_close_method; 165 chan->describe_transport = channel_tls_describe_transport_method; 166 chan->free_fn = channel_tls_free_method; 167 chan->get_overhead_estimate = channel_tls_get_overhead_estimate_method; 168 chan->get_remote_addr = channel_tls_get_remote_addr_method; 169 chan->describe_peer = channel_tls_describe_peer_method; 170 chan->get_transport_name = channel_tls_get_transport_name_method; 171 chan->has_queued_writes = channel_tls_has_queued_writes_method; 172 chan->is_canonical = channel_tls_is_canonical_method; 173 chan->matches_extend_info = channel_tls_matches_extend_info_method; 174 chan->matches_target = channel_tls_matches_target_method; 175 chan->num_bytes_queued = channel_tls_num_bytes_queued_method; 176 chan->num_cells_writeable = channel_tls_num_cells_writeable_method; 177 chan->write_cell = channel_tls_write_cell_method; 178 chan->write_packed_cell = channel_tls_write_packed_cell_method; 179 chan->write_var_cell = channel_tls_write_var_cell_method; 180 181 chan->cmux = circuitmux_alloc(); 182 /* We only have one policy for now so always set it to EWMA. */ 183 circuitmux_set_policy(chan->cmux, &ewma_policy); 184 } 185 186 /** 187 * Start a new TLS channel. 188 * 189 * Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to 190 * handshake with an OR with identity digest <b>id_digest</b>, and wrap 191 * it in a channel_tls_t. 192 */ 193 channel_t * 194 channel_tls_connect(const tor_addr_t *addr, uint16_t port, 195 const char *id_digest, 196 const ed25519_public_key_t *ed_id) 197 { 198 channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan)); 199 channel_t *chan = &(tlschan->base_); 200 201 channel_tls_common_init(tlschan); 202 203 log_debug(LD_CHANNEL, 204 "In channel_tls_connect() for channel %p " 205 "(global id %"PRIu64 ")", 206 tlschan, 207 (chan->global_identifier)); 208 209 if (is_local_to_resolve_addr(addr)) { 210 log_debug(LD_CHANNEL, 211 "Marking new outgoing channel %"PRIu64 " at %p as local", 212 (chan->global_identifier), chan); 213 channel_mark_local(chan); 214 } else { 215 log_debug(LD_CHANNEL, 216 "Marking new outgoing channel %"PRIu64 " at %p as remote", 217 (chan->global_identifier), chan); 218 channel_mark_remote(chan); 219 } 220 221 channel_mark_outgoing(chan); 222 223 /* Set up or_connection stuff */ 224 tlschan->conn = connection_or_connect(addr, port, id_digest, ed_id, tlschan); 225 /* connection_or_connect() will fill in tlschan->conn */ 226 if (!(tlschan->conn)) { 227 chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR; 228 channel_change_state(chan, CHANNEL_STATE_ERROR); 229 goto err; 230 } 231 232 log_debug(LD_CHANNEL, 233 "Got orconn %p for channel with global id %"PRIu64, 234 tlschan->conn, (chan->global_identifier)); 235 236 goto done; 237 238 err: 239 circuitmux_free(chan->cmux); 240 tor_free(tlschan); 241 chan = NULL; 242 243 done: 244 /* If we got one, we should register it */ 245 if (chan) channel_register(chan); 246 247 return chan; 248 } 249 250 /** 251 * Return the current channel_tls_t listener. 252 * 253 * Returns the current channel listener for incoming TLS connections, or 254 * NULL if none has been established 255 */ 256 channel_listener_t * 257 channel_tls_get_listener(void) 258 { 259 return channel_tls_listener; 260 } 261 262 /** 263 * Start a channel_tls_t listener if necessary. 264 * 265 * Return the current channel_tls_t listener, or start one if we haven't yet, 266 * and return that. 267 */ 268 channel_listener_t * 269 channel_tls_start_listener(void) 270 { 271 channel_listener_t *listener; 272 273 if (!channel_tls_listener) { 274 listener = tor_malloc_zero(sizeof(*listener)); 275 channel_init_listener(listener); 276 listener->state = CHANNEL_LISTENER_STATE_LISTENING; 277 listener->close = channel_tls_listener_close_method; 278 listener->describe_transport = 279 channel_tls_listener_describe_transport_method; 280 281 channel_tls_listener = listener; 282 283 log_debug(LD_CHANNEL, 284 "Starting TLS channel listener %p with global id %"PRIu64, 285 listener, (listener->global_identifier)); 286 287 channel_listener_register(listener); 288 } else listener = channel_tls_listener; 289 290 return listener; 291 } 292 293 /** 294 * Free everything on shutdown. 295 * 296 * Not much to do here, since channel_free_all() takes care of a lot, but let's 297 * get rid of the listener. 298 */ 299 void 300 channel_tls_free_all(void) 301 { 302 channel_listener_t *old_listener = NULL; 303 304 log_debug(LD_CHANNEL, 305 "Shutting down TLS channels..."); 306 307 if (channel_tls_listener) { 308 /* 309 * When we close it, channel_tls_listener will get nulled out, so save 310 * a pointer so we can free it. 311 */ 312 old_listener = channel_tls_listener; 313 log_debug(LD_CHANNEL, 314 "Closing channel_tls_listener with ID %"PRIu64 315 " at %p.", 316 (old_listener->global_identifier), 317 old_listener); 318 channel_listener_unregister(old_listener); 319 channel_listener_mark_for_close(old_listener); 320 channel_listener_free(old_listener); 321 tor_assert(channel_tls_listener == NULL); 322 } 323 324 log_debug(LD_CHANNEL, 325 "Done shutting down TLS channels"); 326 } 327 328 /** 329 * Create a new channel around an incoming or_connection_t. 330 */ 331 channel_t * 332 channel_tls_handle_incoming(or_connection_t *orconn) 333 { 334 channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan)); 335 channel_t *chan = &(tlschan->base_); 336 337 tor_assert(orconn); 338 tor_assert(!(orconn->chan)); 339 340 channel_tls_common_init(tlschan); 341 342 /* Link the channel and orconn to each other */ 343 tlschan->conn = orconn; 344 orconn->chan = tlschan; 345 346 if (is_local_to_resolve_addr(&(TO_CONN(orconn)->addr))) { 347 log_debug(LD_CHANNEL, 348 "Marking new incoming channel %"PRIu64 " at %p as local", 349 (chan->global_identifier), chan); 350 channel_mark_local(chan); 351 } else { 352 log_debug(LD_CHANNEL, 353 "Marking new incoming channel %"PRIu64 " at %p as remote", 354 (chan->global_identifier), chan); 355 channel_mark_remote(chan); 356 } 357 358 channel_mark_incoming(chan); 359 360 /* Register it */ 361 channel_register(chan); 362 363 char *transport_name = NULL; 364 if (channel_tls_get_transport_name_method(TLS_CHAN_TO_BASE(orconn->chan), 365 &transport_name) < 0) { 366 transport_name = NULL; 367 } 368 /* Start tracking TLS connections in the DoS subsystem as soon as possible, 369 * so we can protect against attacks that use partially open connections. 370 */ 371 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, 372 &TO_CONN(orconn)->addr, transport_name, 373 time(NULL)); 374 dos_new_client_conn(orconn, transport_name); 375 tor_free(transport_name); 376 377 return chan; 378 } 379 380 /** 381 * Set the `potentially_used_for_bootstrapping` flag on the or_connection_t 382 * corresponding to the provided channel. 383 * 384 * This flag indicates that if the connection fails, it might be interesting 385 * to the bootstrapping subsystem. (The bootstrapping system only cares about 386 * channels that we have tried to use for our own circuits. Other channels 387 * may have been launched in response to EXTEND cells from somebody else, and 388 * if they fail, it won't necessarily indicate a bootstrapping problem.) 389 **/ 390 void 391 channel_mark_as_used_for_origin_circuit(channel_t *chan) 392 { 393 if (BUG(!chan)) 394 return; 395 if (chan->magic != TLS_CHAN_MAGIC) 396 return; 397 channel_tls_t *tlschan = channel_tls_from_base(chan); 398 if (BUG(!tlschan)) 399 return; 400 401 if (tlschan->conn) 402 tlschan->conn->potentially_used_for_bootstrapping = 1; 403 } 404 405 /********* 406 * Casts * 407 ********/ 408 409 /** 410 * Cast a channel_tls_t to a channel_t. 411 */ 412 channel_t * 413 channel_tls_to_base(channel_tls_t *tlschan) 414 { 415 if (!tlschan) return NULL; 416 417 return &(tlschan->base_); 418 } 419 420 /** 421 * Cast a channel_t to a channel_tls_t, with appropriate type-checking 422 * asserts. 423 */ 424 channel_tls_t * 425 channel_tls_from_base(channel_t *chan) 426 { 427 if (!chan) return NULL; 428 429 tor_assert(chan->magic == TLS_CHAN_MAGIC); 430 431 return (channel_tls_t *)(chan); 432 } 433 434 /** 435 * Cast a const channel_tls_t to a const channel_t. 436 */ 437 const channel_t * 438 channel_tls_to_base_const(const channel_tls_t *tlschan) 439 { 440 return channel_tls_to_base((channel_tls_t*) tlschan); 441 } 442 443 /** 444 * Cast a const channel_t to a const channel_tls_t, with appropriate 445 * type-checking asserts. 446 */ 447 const channel_tls_t * 448 channel_tls_from_base_const(const channel_t *chan) 449 { 450 return channel_tls_from_base((channel_t *)chan); 451 } 452 453 /******************************************** 454 * Method implementations for channel_tls_t * 455 *******************************************/ 456 457 /** 458 * Close a channel_tls_t. 459 * 460 * This implements the close method for channel_tls_t. 461 */ 462 static void 463 channel_tls_close_method(channel_t *chan) 464 { 465 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 466 467 tor_assert(tlschan); 468 469 if (tlschan->conn) connection_or_close_normally(tlschan->conn, 1); 470 else { 471 /* Weird - we'll have to change the state ourselves, I guess */ 472 log_info(LD_CHANNEL, 473 "Tried to close channel_tls_t %p with NULL conn", 474 tlschan); 475 channel_change_state(chan, CHANNEL_STATE_ERROR); 476 } 477 } 478 479 /** 480 * Describe the transport for a channel_tls_t. 481 * 482 * This returns the string "TLS channel on connection <id>" to the upper 483 * layer. 484 */ 485 static const char * 486 channel_tls_describe_transport_method(channel_t *chan) 487 { 488 static char *buf = NULL; 489 uint64_t id; 490 channel_tls_t *tlschan; 491 const char *rv = NULL; 492 493 tor_assert(chan); 494 495 tlschan = BASE_CHAN_TO_TLS(chan); 496 497 if (tlschan->conn) { 498 id = TO_CONN(tlschan->conn)->global_identifier; 499 500 if (buf) tor_free(buf); 501 tor_asprintf(&buf, 502 "TLS channel (connection %"PRIu64 ")", 503 (id)); 504 505 rv = buf; 506 } else { 507 rv = "TLS channel (no connection)"; 508 } 509 510 return rv; 511 } 512 513 /** 514 * Free a channel_tls_t. 515 * 516 * This is called by the generic channel layer when freeing a channel_tls_t; 517 * this happens either on a channel which has already reached 518 * CHANNEL_STATE_CLOSED or CHANNEL_STATE_ERROR from channel_run_cleanup() or 519 * on shutdown from channel_free_all(). In the latter case we might still 520 * have an orconn active (which connection_free_all() will get to later), 521 * so we should null out its channel pointer now. 522 */ 523 static void 524 channel_tls_free_method(channel_t *chan) 525 { 526 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 527 528 tor_assert(tlschan); 529 530 if (tlschan->conn) { 531 tlschan->conn->chan = NULL; 532 tlschan->conn = NULL; 533 } 534 } 535 536 /** 537 * Get an estimate of the average TLS overhead for the upper layer. 538 */ 539 static double 540 channel_tls_get_overhead_estimate_method(channel_t *chan) 541 { 542 double overhead = 1.0; 543 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 544 545 tor_assert(tlschan); 546 tor_assert(tlschan->conn); 547 548 /* Just return 1.0f if we don't have sensible data */ 549 if (tlschan->conn->bytes_xmitted > 0 && 550 tlschan->conn->bytes_xmitted_by_tls >= 551 tlschan->conn->bytes_xmitted) { 552 overhead = ((double)(tlschan->conn->bytes_xmitted_by_tls)) / 553 ((double)(tlschan->conn->bytes_xmitted)); 554 555 /* 556 * Never estimate more than 2.0; otherwise we get silly large estimates 557 * at the very start of a new TLS connection. 558 */ 559 if (overhead > 2.0) 560 overhead = 2.0; 561 } 562 563 log_debug(LD_CHANNEL, 564 "Estimated overhead ratio for TLS chan %"PRIu64 " is %f", 565 (chan->global_identifier), overhead); 566 567 return overhead; 568 } 569 570 /** 571 * Get the remote address of a channel_tls_t. 572 * 573 * This implements the get_remote_addr method for channel_tls_t; copy the 574 * remote endpoint of the channel to addr_out and return 1. (Always 575 * succeeds if this channel is attached to an OR connection.) 576 * 577 * Always returns the real address of the peer, not the canonical address. 578 */ 579 static int 580 channel_tls_get_remote_addr_method(const channel_t *chan, 581 tor_addr_t *addr_out) 582 { 583 const channel_tls_t *tlschan = CONST_BASE_CHAN_TO_TLS(chan); 584 585 tor_assert(tlschan); 586 tor_assert(addr_out); 587 588 if (tlschan->conn == NULL) { 589 tor_addr_make_unspec(addr_out); 590 return 0; 591 } 592 593 /* They want the real address, so give it to them. */ 594 tor_addr_copy(addr_out, &TO_CONN(tlschan->conn)->addr); 595 596 return 1; 597 } 598 599 /** 600 * Get the name of the pluggable transport used by a channel_tls_t. 601 * 602 * This implements the get_transport_name for channel_tls_t. If the 603 * channel uses a pluggable transport, copy its name to 604 * <b>transport_out</b> and return 0. If the channel did not use a 605 * pluggable transport, return -1. 606 */ 607 static int 608 channel_tls_get_transport_name_method(channel_t *chan, char **transport_out) 609 { 610 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 611 612 tor_assert(tlschan); 613 tor_assert(transport_out); 614 tor_assert(tlschan->conn); 615 616 if (!tlschan->conn->ext_or_transport) 617 return -1; 618 619 *transport_out = tor_strdup(tlschan->conn->ext_or_transport); 620 return 0; 621 } 622 623 /** 624 * Get a human-readable endpoint description of a channel_tls_t. 625 * 626 * This format is intended for logging, and may change in the future; 627 * nothing should parse or rely on its particular details. 628 */ 629 static const char * 630 channel_tls_describe_peer_method(const channel_t *chan) 631 { 632 const channel_tls_t *tlschan = CONST_BASE_CHAN_TO_TLS(chan); 633 tor_assert(tlschan); 634 635 if (tlschan->conn) { 636 return connection_describe_peer(TO_CONN(tlschan->conn)); 637 } else { 638 return "(No connection)"; 639 } 640 } 641 642 /** 643 * Tell the upper layer if we have queued writes. 644 * 645 * This implements the has_queued_writes method for channel_tls t_; it returns 646 * 1 iff we have queued writes on the outbuf of the underlying or_connection_t. 647 */ 648 static int 649 channel_tls_has_queued_writes_method(channel_t *chan) 650 { 651 size_t outbuf_len; 652 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 653 654 tor_assert(tlschan); 655 if (!(tlschan->conn)) { 656 log_info(LD_CHANNEL, 657 "something called has_queued_writes on a tlschan " 658 "(%p with ID %"PRIu64 " but no conn", 659 chan, (chan->global_identifier)); 660 } 661 662 outbuf_len = (tlschan->conn != NULL) ? 663 connection_get_outbuf_len(TO_CONN(tlschan->conn)) : 664 0; 665 666 return (outbuf_len > 0); 667 } 668 669 /** 670 * Tell the upper layer if we're canonical. 671 * 672 * This implements the is_canonical method for channel_tls_t: 673 * it returns whether this is a canonical channel. 674 */ 675 static int 676 channel_tls_is_canonical_method(channel_t *chan) 677 { 678 int answer = 0; 679 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 680 681 tor_assert(tlschan); 682 683 if (tlschan->conn) { 684 /* If this bit is set to 0, and link_proto is sufficiently old, then we 685 * can't actually _rely_ on this being a non-canonical channel. 686 * Nonetheless, we're going to believe that this is a non-canonical 687 * channel in this case, since nobody should be using these link protocols 688 * any more. */ 689 answer = tlschan->conn->is_canonical; 690 } 691 692 return answer; 693 } 694 695 /** 696 * Check if we match an extend_info_t. 697 * 698 * This implements the matches_extend_info method for channel_tls_t; the upper 699 * layer wants to know if this channel matches an extend_info_t. 700 * 701 * NOTE that this function only checks for an address/port match, and should 702 * be used only when no identify is available. 703 */ 704 static int 705 channel_tls_matches_extend_info_method(channel_t *chan, 706 extend_info_t *extend_info) 707 { 708 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 709 710 tor_assert(tlschan); 711 tor_assert(extend_info); 712 713 /* Never match if we have no conn */ 714 if (!(tlschan->conn)) { 715 log_info(LD_CHANNEL, 716 "something called matches_extend_info on a tlschan " 717 "(%p with ID %"PRIu64 " but no conn", 718 chan, (chan->global_identifier)); 719 return 0; 720 } 721 722 const tor_addr_port_t *orport = &tlschan->conn->canonical_orport; 723 // If the canonical address is set, then we'll allow matches based on that. 724 if (! tor_addr_is_unspec(&orport->addr)) { 725 if (extend_info_has_orport(extend_info, &orport->addr, orport->port)) { 726 return 1; 727 } 728 } 729 730 // We also want to match if the true address and port are listed in the 731 // extend info. 732 return extend_info_has_orport(extend_info, 733 &TO_CONN(tlschan->conn)->addr, 734 TO_CONN(tlschan->conn)->port); 735 } 736 737 /** 738 * Check if we match a target address; return true iff we do. 739 * 740 * This implements the matches_target method for channel_tls t_; the upper 741 * layer wants to know if this channel matches a target address when extending 742 * a circuit. 743 */ 744 static int 745 channel_tls_matches_target_method(channel_t *chan, 746 const tor_addr_t *target) 747 { 748 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 749 750 tor_assert(tlschan); 751 tor_assert(target); 752 753 /* Never match if we have no conn */ 754 if (!(tlschan->conn)) { 755 log_info(LD_CHANNEL, 756 "something called matches_target on a tlschan " 757 "(%p with ID %"PRIu64 " but no conn", 758 chan, (chan->global_identifier)); 759 return 0; 760 } 761 762 /* addr is the address this connection came from. 763 * canonical_orport is updated by connection_or_init_conn_from_address() 764 * to be the address in the descriptor. It may be tempting to 765 * allow either address to be allowed, but if we did so, it would 766 * enable someone who steals a relay's keys to covertly impersonate/MITM it 767 * from anywhere on the Internet! (Because they could make long-lived 768 * TLS connections from anywhere to all relays, and wait for them to 769 * be used for extends). 770 * 771 * An adversary who has stolen a relay's keys could also post a fake relay 772 * descriptor, but that attack is easier to detect. 773 */ 774 return tor_addr_eq(&TO_CONN(tlschan->conn)->addr, target); 775 } 776 777 /** 778 * Tell the upper layer how many bytes we have queued and not yet 779 * sent. 780 */ 781 static size_t 782 channel_tls_num_bytes_queued_method(channel_t *chan) 783 { 784 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 785 786 tor_assert(tlschan); 787 tor_assert(tlschan->conn); 788 789 return connection_get_outbuf_len(TO_CONN(tlschan->conn)); 790 } 791 792 /** 793 * Tell the upper layer how many cells we can accept to write. 794 * 795 * This implements the num_cells_writeable method for channel_tls_t; it 796 * returns an estimate of the number of cells we can accept with 797 * channel_tls_write_*_cell(). 798 */ 799 static int 800 channel_tls_num_cells_writeable_method(channel_t *chan) 801 { 802 size_t outbuf_len; 803 ssize_t n; 804 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 805 size_t cell_network_size; 806 807 tor_assert(tlschan); 808 tor_assert(tlschan->conn); 809 810 cell_network_size = get_cell_network_size(tlschan->conn->wide_circ_ids); 811 outbuf_len = connection_get_outbuf_len(TO_CONN(tlschan->conn)); 812 /* Get the number of cells */ 813 n = CEIL_DIV(or_conn_highwatermark() - outbuf_len, cell_network_size); 814 if (n < 0) n = 0; 815 #if SIZEOF_SIZE_T > SIZEOF_INT 816 if (n > INT_MAX) n = INT_MAX; 817 #endif 818 819 return (int)n; 820 } 821 822 /** 823 * Write a cell to a channel_tls_t. 824 * 825 * This implements the write_cell method for channel_tls_t; given a 826 * channel_tls_t and a cell_t, transmit the cell_t. 827 */ 828 static int 829 channel_tls_write_cell_method(channel_t *chan, cell_t *cell) 830 { 831 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 832 int written = 0; 833 834 tor_assert(tlschan); 835 tor_assert(cell); 836 837 if (tlschan->conn) { 838 connection_or_write_cell_to_buf(cell, tlschan->conn); 839 ++written; 840 } else { 841 log_info(LD_CHANNEL, 842 "something called write_cell on a tlschan " 843 "(%p with ID %"PRIu64 " but no conn", 844 chan, (chan->global_identifier)); 845 } 846 847 return written; 848 } 849 850 /** 851 * Write a packed cell to a channel_tls_t. 852 * 853 * This implements the write_packed_cell method for channel_tls_t; given a 854 * channel_tls_t and a packed_cell_t, transmit the packed_cell_t. 855 * 856 * Return 0 on success or negative value on error. The caller must free the 857 * packed cell. 858 */ 859 static int 860 channel_tls_write_packed_cell_method(channel_t *chan, 861 packed_cell_t *packed_cell) 862 { 863 tor_assert(chan); 864 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 865 size_t cell_network_size = get_cell_network_size(chan->wide_circ_ids); 866 867 tor_assert(tlschan); 868 tor_assert(packed_cell); 869 870 if (tlschan->conn) { 871 connection_buf_add(packed_cell->body, cell_network_size, 872 TO_CONN(tlschan->conn)); 873 } else { 874 log_info(LD_CHANNEL, 875 "something called write_packed_cell on a tlschan " 876 "(%p with ID %"PRIu64 " but no conn", 877 chan, (chan->global_identifier)); 878 return -1; 879 } 880 881 return 0; 882 } 883 884 /** 885 * Write a variable-length cell to a channel_tls_t. 886 * 887 * This implements the write_var_cell method for channel_tls_t; given a 888 * channel_tls_t and a var_cell_t, transmit the var_cell_t. 889 */ 890 static int 891 channel_tls_write_var_cell_method(channel_t *chan, var_cell_t *var_cell) 892 { 893 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); 894 int written = 0; 895 896 tor_assert(tlschan); 897 tor_assert(var_cell); 898 899 if (tlschan->conn) { 900 connection_or_write_var_cell_to_buf(var_cell, tlschan->conn); 901 ++written; 902 } else { 903 log_info(LD_CHANNEL, 904 "something called write_var_cell on a tlschan " 905 "(%p with ID %"PRIu64 " but no conn", 906 chan, (chan->global_identifier)); 907 } 908 909 return written; 910 } 911 912 /************************************************* 913 * Method implementations for channel_listener_t * 914 ************************************************/ 915 916 /** 917 * Close a channel_listener_t. 918 * 919 * This implements the close method for channel_listener_t. 920 */ 921 static void 922 channel_tls_listener_close_method(channel_listener_t *chan_l) 923 { 924 tor_assert(chan_l); 925 926 /* 927 * Listeners we just go ahead and change state through to CLOSED, but 928 * make sure to check if they're channel_tls_listener to NULL it out. 929 */ 930 if (chan_l == channel_tls_listener) 931 channel_tls_listener = NULL; 932 933 if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSING || 934 chan_l->state == CHANNEL_LISTENER_STATE_CLOSED || 935 chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) { 936 channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING); 937 } 938 939 if (chan_l->incoming_list) { 940 SMARTLIST_FOREACH_BEGIN(chan_l->incoming_list, 941 channel_t *, ichan) { 942 channel_mark_for_close(ichan); 943 } SMARTLIST_FOREACH_END(ichan); 944 945 smartlist_free(chan_l->incoming_list); 946 chan_l->incoming_list = NULL; 947 } 948 949 if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSED || 950 chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) { 951 channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSED); 952 } 953 } 954 955 /** 956 * Describe the transport for a channel_listener_t. 957 * 958 * This returns the string "TLS channel (listening)" to the upper 959 * layer. 960 */ 961 static const char * 962 channel_tls_listener_describe_transport_method(channel_listener_t *chan_l) 963 { 964 tor_assert(chan_l); 965 966 return "TLS channel (listening)"; 967 } 968 969 /******************************************************* 970 * Functions for handling events on an or_connection_t * 971 ******************************************************/ 972 973 /** 974 * Handle an orconn state change. 975 * 976 * This function will be called by connection_or.c when the or_connection_t 977 * associated with this channel_tls_t changes state. 978 */ 979 void 980 channel_tls_handle_state_change_on_orconn(channel_tls_t *chan, 981 or_connection_t *conn, 982 uint8_t state) 983 { 984 channel_t *base_chan; 985 986 tor_assert(chan); 987 tor_assert(conn); 988 tor_assert(conn->chan == chan); 989 tor_assert(chan->conn == conn); 990 991 base_chan = TLS_CHAN_TO_BASE(chan); 992 993 /* Make sure the base connection state makes sense - shouldn't be error 994 * or closed. */ 995 996 tor_assert(CHANNEL_IS_OPENING(base_chan) || 997 CHANNEL_IS_OPEN(base_chan) || 998 CHANNEL_IS_MAINT(base_chan) || 999 CHANNEL_IS_CLOSING(base_chan)); 1000 1001 /* Did we just go to state open? */ 1002 if (state == OR_CONN_STATE_OPEN) { 1003 /* 1004 * We can go to CHANNEL_STATE_OPEN from CHANNEL_STATE_OPENING or 1005 * CHANNEL_STATE_MAINT on this. 1006 */ 1007 channel_change_state_open(base_chan); 1008 /* We might have just become writeable; check and tell the scheduler */ 1009 if (connection_or_num_cells_writeable(conn) > 0) { 1010 scheduler_channel_wants_writes(base_chan); 1011 } 1012 } else { 1013 /* 1014 * Not open, so from CHANNEL_STATE_OPEN we go to CHANNEL_STATE_MAINT, 1015 * otherwise no change. 1016 */ 1017 if (CHANNEL_IS_OPEN(base_chan)) { 1018 channel_change_state(base_chan, CHANNEL_STATE_MAINT); 1019 } 1020 } 1021 } 1022 1023 #ifdef KEEP_TIMING_STATS 1024 1025 /** 1026 * Timing states wrapper. 1027 * 1028 * This is a wrapper function around the actual function that processes the 1029 * <b>cell</b> that just arrived on <b>chan</b>. Increment <b>*time</b> 1030 * by the number of microseconds used by the call to <b>*func(cell, chan)</b>. 1031 */ 1032 static void 1033 channel_tls_time_process_cell(cell_t *cell, channel_tls_t *chan, int *time, 1034 void (*func)(cell_t *, channel_tls_t *)) 1035 { 1036 struct timeval start, end; 1037 long time_passed; 1038 1039 tor_gettimeofday(&start); 1040 1041 (*func)(cell, chan); 1042 1043 tor_gettimeofday(&end); 1044 time_passed = tv_udiff(&start, &end) ; 1045 1046 if (time_passed > 10000) { /* more than 10ms */ 1047 log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000); 1048 } 1049 1050 if (time_passed < 0) { 1051 log_info(LD_GENERAL,"That call took us back in time!"); 1052 time_passed = 0; 1053 } 1054 1055 *time += time_passed; 1056 } 1057 #endif /* defined(KEEP_TIMING_STATS) */ 1058 1059 #ifdef KEEP_TIMING_STATS 1060 #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \ 1061 ++num ## tp; \ 1062 channel_tls_time_process_cell(cl, cn, & tp ## time , \ 1063 channel_tls_process_ ## tp ## _cell); \ 1064 } STMT_END 1065 #else /* !defined(KEEP_TIMING_STATS) */ 1066 #define PROCESS_CELL(tp, cl, cn) channel_tls_process_ ## tp ## _cell(cl, cn) 1067 #endif /* defined(KEEP_TIMING_STATS) */ 1068 1069 /** 1070 * Handle an incoming cell on a channel_tls_t. 1071 * 1072 * This is called from connection_or.c to handle an arriving cell; it checks 1073 * for cell types specific to the handshake for this transport protocol and 1074 * handles them, and queues all other cells to the channel_t layer, which 1075 * eventually will hand them off to command.c. 1076 * 1077 * The channel layer itself decides whether the cell should be queued or 1078 * can be handed off immediately to the upper-layer code. It is responsible 1079 * for copying in the case that it queues; we merely pass pointers through 1080 * which we get from connection_or_process_cells_from_inbuf(). 1081 */ 1082 void 1083 channel_tls_handle_cell(cell_t *cell, or_connection_t *conn) 1084 { 1085 channel_tls_t *chan; 1086 int handshaking; 1087 1088 tor_assert(cell); 1089 tor_assert(conn); 1090 1091 chan = conn->chan; 1092 1093 if (!chan) { 1094 log_warn(LD_CHANNEL, 1095 "Got a cell_t on an OR connection with no channel"); 1096 return; 1097 } 1098 1099 handshaking = (TO_CONN(conn)->state != OR_CONN_STATE_OPEN); 1100 1101 if (conn->base_.marked_for_close) 1102 return; 1103 1104 /* Reject all but VERSIONS and NETINFO when handshaking. */ 1105 /* (VERSIONS actually indicates a protocol warning: it's variable-length, 1106 * so if it reaches this function, we're on a v1 connection.) */ 1107 if (handshaking && cell->command != CELL_VERSIONS && 1108 cell->command != CELL_NETINFO) { 1109 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, 1110 "Received unexpected cell command %d in chan state %s / " 1111 "conn state %s; closing the connection.", 1112 (int)cell->command, 1113 channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state), 1114 conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state)); 1115 connection_or_close_for_error(conn, 0); 1116 return; 1117 } 1118 1119 if (conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) 1120 or_handshake_state_record_cell(conn, conn->handshake_state, cell, 1); 1121 1122 /* We note that we're on the internet whenever we read a cell. This is 1123 * a fast operation. */ 1124 entry_guards_note_internet_connectivity(get_guard_selection_info()); 1125 rep_hist_padding_count_read(PADDING_TYPE_TOTAL); 1126 1127 if (TLS_CHAN_TO_BASE(chan)->padding_enabled) 1128 rep_hist_padding_count_read(PADDING_TYPE_ENABLED_TOTAL); 1129 1130 switch (cell->command) { 1131 case CELL_PADDING: 1132 rep_hist_padding_count_read(PADDING_TYPE_CELL); 1133 if (TLS_CHAN_TO_BASE(chan)->padding_enabled) 1134 rep_hist_padding_count_read(PADDING_TYPE_ENABLED_CELL); 1135 ++stats_n_padding_cells_processed; 1136 /* do nothing */ 1137 break; 1138 case CELL_VERSIONS: 1139 /* A VERSIONS cell should always be a variable-length cell, and 1140 * so should never reach this function (which handles constant-sized 1141 * cells). But if the connection is using the (obsolete) v1 link 1142 * protocol, all cells will be treated as constant-sized, and so 1143 * it's possible we'll reach this code. 1144 */ 1145 log_fn(LOG_PROTOCOL_WARN, LD_CHANNEL, 1146 "Received unexpected VERSIONS cell on a channel using link " 1147 "protocol %d; ignoring.", conn->link_proto); 1148 break; 1149 case CELL_NETINFO: 1150 ++stats_n_netinfo_cells_processed; 1151 PROCESS_CELL(netinfo, cell, chan); 1152 break; 1153 case CELL_PADDING_NEGOTIATE: 1154 ++stats_n_netinfo_cells_processed; 1155 PROCESS_CELL(padding_negotiate, cell, chan); 1156 break; 1157 case CELL_CREATE: 1158 case CELL_CREATE_FAST: 1159 case CELL_CREATED: 1160 case CELL_CREATED_FAST: 1161 case CELL_RELAY: 1162 case CELL_RELAY_EARLY: 1163 case CELL_DESTROY: 1164 case CELL_CREATE2: 1165 case CELL_CREATED2: 1166 /* 1167 * These are all transport independent and we pass them up through the 1168 * channel_t mechanism. They are ultimately handled in command.c. 1169 */ 1170 channel_process_cell(TLS_CHAN_TO_BASE(chan), cell); 1171 break; 1172 default: 1173 log_fn(LOG_INFO, LD_PROTOCOL, 1174 "Cell of unknown type (%d) received in channeltls.c. " 1175 "Dropping.", 1176 cell->command); 1177 break; 1178 } 1179 } 1180 1181 /** 1182 * Handle an incoming variable-length cell on a channel_tls_t. 1183 * 1184 * Process a <b>var_cell</b> that was just received on <b>conn</b>. Keep 1185 * internal statistics about how many of each cell we've processed so far 1186 * this second, and the total number of microseconds it took to 1187 * process each type of cell. All the var_cell commands are handshake- 1188 * related and live below the channel_t layer, so no variable-length 1189 * cells ever get delivered in the current implementation, but I've left 1190 * the mechanism in place for future use. 1191 * 1192 * If we were handing them off to the upper layer, the channel_t queueing 1193 * code would be responsible for memory management, and we'd just be passing 1194 * pointers through from connection_or_process_cells_from_inbuf(). That 1195 * caller always frees them after this function returns, so this function 1196 * should never free var_cell. 1197 */ 1198 void 1199 channel_tls_handle_var_cell(var_cell_t *var_cell, or_connection_t *conn) 1200 { 1201 channel_tls_t *chan; 1202 1203 #ifdef KEEP_TIMING_STATS 1204 /* how many of each cell have we seen so far this second? needs better 1205 * name. */ 1206 static int num_versions = 0, num_certs = 0; 1207 static time_t current_second = 0; /* from previous calls to time */ 1208 time_t now = time(NULL); 1209 1210 if (current_second == 0) current_second = now; 1211 if (now > current_second) { /* the second has rolled over */ 1212 /* print stats */ 1213 log_info(LD_OR, 1214 "At end of second: %d versions (%d ms), %d certs (%d ms)", 1215 num_versions, versions_time / ((now - current_second) * 1000), 1216 num_certs, certs_time / ((now - current_second) * 1000)); 1217 1218 num_versions = num_certs = 0; 1219 versions_time = certs_time = 0; 1220 1221 /* remember which second it is, for next time */ 1222 current_second = now; 1223 } 1224 #endif /* defined(KEEP_TIMING_STATS) */ 1225 1226 tor_assert(var_cell); 1227 tor_assert(conn); 1228 1229 chan = conn->chan; 1230 1231 if (!chan) { 1232 log_warn(LD_CHANNEL, 1233 "Got a var_cell_t on an OR connection with no channel"); 1234 return; 1235 } 1236 1237 if (TO_CONN(conn)->marked_for_close) 1238 return; 1239 1240 switch (TO_CONN(conn)->state) { 1241 case OR_CONN_STATE_TLS_HANDSHAKING: 1242 /* If we're using bufferevents, it's entirely possible for us to 1243 * notice "hey, data arrived!" before we notice "hey, the handshake 1244 * finished!" And we need to be accepting both at once to handle both 1245 * the v2 and v3 handshakes. */ 1246 /* But that should be happening any longer've disabled bufferevents. */ 1247 tor_assert_nonfatal_unreached_once(); 1248 FALLTHROUGH_UNLESS_ALL_BUGS_ARE_FATAL; 1249 case OR_CONN_STATE_SERVER_VERSIONS_WAIT: 1250 if (!(command_allowed_before_handshake(var_cell->command))) { 1251 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, 1252 "Received a cell with command %d in unexpected " 1253 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; " 1254 "closing the connection.", 1255 (int)(var_cell->command), 1256 conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state), 1257 (int)(TO_CONN(conn)->state), 1258 channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state), 1259 (int)(TLS_CHAN_TO_BASE(chan)->state)); 1260 /* see above comment about CHANNEL_STATE_ERROR */ 1261 connection_or_close_for_error(conn, 0); 1262 return; 1263 } else { 1264 if (enter_v3_handshake_with_cell(var_cell, chan) < 0) 1265 return; 1266 } 1267 break; 1268 case OR_CONN_STATE_OR_HANDSHAKING_V3: 1269 if (var_cell->command != CELL_AUTHENTICATE) 1270 or_handshake_state_record_var_cell(conn, conn->handshake_state, 1271 var_cell, 1); 1272 break; /* Everything is allowed */ 1273 case OR_CONN_STATE_OPEN: 1274 if (conn->link_proto < 3) { 1275 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, 1276 "Received a variable-length cell with command %d in orconn " 1277 "state %s [%d], channel state %s [%d] with link protocol %d; " 1278 "ignoring it.", 1279 (int)(var_cell->command), 1280 conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state), 1281 (int)(TO_CONN(conn)->state), 1282 channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state), 1283 (int)(TLS_CHAN_TO_BASE(chan)->state), 1284 (int)(conn->link_proto)); 1285 return; 1286 } 1287 break; 1288 default: 1289 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, 1290 "Received var-length cell with command %d in unexpected " 1291 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; " 1292 "ignoring it.", 1293 (int)(var_cell->command), 1294 conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state), 1295 (int)(TO_CONN(conn)->state), 1296 channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state), 1297 (int)(TLS_CHAN_TO_BASE(chan)->state)); 1298 return; 1299 } 1300 1301 /* We note that we're on the internet whenever we read a cell. This is 1302 * a fast operation. */ 1303 entry_guards_note_internet_connectivity(get_guard_selection_info()); 1304 1305 /* Now handle the cell */ 1306 1307 switch (var_cell->command) { 1308 case CELL_VERSIONS: 1309 ++stats_n_versions_cells_processed; 1310 PROCESS_CELL(versions, var_cell, chan); 1311 break; 1312 case CELL_VPADDING: 1313 ++stats_n_vpadding_cells_processed; 1314 /* Do nothing */ 1315 break; 1316 case CELL_CERTS: 1317 ++stats_n_certs_cells_processed; 1318 PROCESS_CELL(certs, var_cell, chan); 1319 break; 1320 case CELL_AUTH_CHALLENGE: 1321 ++stats_n_auth_challenge_cells_processed; 1322 PROCESS_CELL(auth_challenge, var_cell, chan); 1323 break; 1324 case CELL_AUTHENTICATE: 1325 ++stats_n_authenticate_cells_processed; 1326 PROCESS_CELL(authenticate, var_cell, chan); 1327 break; 1328 case CELL_AUTHORIZE: 1329 ++stats_n_authorize_cells_processed; 1330 /* Ignored so far. */ 1331 break; 1332 default: 1333 log_fn(LOG_INFO, LD_PROTOCOL, 1334 "Variable-length cell of unknown type (%d) received.", 1335 (int)(var_cell->command)); 1336 break; 1337 } 1338 } 1339 1340 #undef PROCESS_CELL 1341 1342 /** 1343 * Update channel marks after connection_or.c has changed an address. 1344 * 1345 * This is called from connection_or_init_conn_from_address() after the 1346 * connection's _base.addr or real_addr fields have potentially been changed 1347 * so we can recalculate the local mark. Notably, this happens when incoming 1348 * connections are reverse-proxied and we only learn the real address of the 1349 * remote router by looking it up in the consensus after we finish the 1350 * handshake and know an authenticated identity digest. 1351 */ 1352 void 1353 channel_tls_update_marks(or_connection_t *conn) 1354 { 1355 channel_t *chan = NULL; 1356 1357 tor_assert(conn); 1358 tor_assert(conn->chan); 1359 1360 chan = TLS_CHAN_TO_BASE(conn->chan); 1361 1362 if (is_local_to_resolve_addr(&(TO_CONN(conn)->addr))) { 1363 if (!channel_is_local(chan)) { 1364 log_debug(LD_CHANNEL, 1365 "Marking channel %"PRIu64 " at %p as local", 1366 (chan->global_identifier), chan); 1367 channel_mark_local(chan); 1368 } 1369 } else { 1370 if (channel_is_local(chan)) { 1371 log_debug(LD_CHANNEL, 1372 "Marking channel %"PRIu64 " at %p as remote", 1373 (chan->global_identifier), chan); 1374 channel_mark_remote(chan); 1375 } 1376 } 1377 } 1378 1379 /** 1380 * Check if this cell type is allowed before the handshake is finished. 1381 * 1382 * Return true if <b>command</b> is a cell command that's allowed to start a 1383 * V3 handshake. 1384 */ 1385 static int 1386 command_allowed_before_handshake(uint8_t command) 1387 { 1388 switch (command) { 1389 case CELL_VERSIONS: 1390 case CELL_VPADDING: 1391 case CELL_AUTHORIZE: 1392 return 1; 1393 default: 1394 return 0; 1395 } 1396 } 1397 1398 /** 1399 * Start a V3 handshake on an incoming connection. 1400 * 1401 * Called when we as a server receive an appropriate cell while waiting 1402 * either for a cell or a TLS handshake. Set the connection's state to 1403 * "handshaking_v3', initializes the or_handshake_state field as needed, 1404 * and add the cell to the hash of incoming cells.) 1405 */ 1406 static int 1407 enter_v3_handshake_with_cell(var_cell_t *cell, channel_tls_t *chan) 1408 { 1409 int started_here = 0; 1410 1411 tor_assert(cell); 1412 tor_assert(chan); 1413 tor_assert(chan->conn); 1414 1415 started_here = connection_or_nonopen_was_started_here(chan->conn); 1416 1417 tor_assert(TO_CONN(chan->conn)->state == OR_CONN_STATE_TLS_HANDSHAKING || 1418 TO_CONN(chan->conn)->state == 1419 OR_CONN_STATE_SERVER_VERSIONS_WAIT); 1420 1421 if (started_here) { 1422 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1423 "Received a cell while TLS-handshaking, not in " 1424 "OR_HANDSHAKING_V3, on a connection we originated."); 1425 } 1426 connection_or_change_state(chan->conn, OR_CONN_STATE_OR_HANDSHAKING_V3); 1427 if (connection_init_or_handshake_state(chan->conn, started_here) < 0) { 1428 connection_or_close_for_error(chan->conn, 0); 1429 return -1; 1430 } 1431 or_handshake_state_record_var_cell(chan->conn, 1432 chan->conn->handshake_state, cell, 1); 1433 return 0; 1434 } 1435 1436 /** 1437 * Process a 'versions' cell. 1438 * 1439 * This function is called to handle an incoming VERSIONS cell; the current 1440 * link protocol version must be 0 to indicate that no version has yet been 1441 * negotiated. We compare the versions in the cell to the list of versions 1442 * we support, pick the highest version we have in common, and continue the 1443 * negotiation from there. 1444 */ 1445 static void 1446 channel_tls_process_versions_cell(var_cell_t *cell, channel_tls_t *chan) 1447 { 1448 int highest_supported_version = 0; 1449 int started_here = 0; 1450 1451 tor_assert(cell); 1452 tor_assert(chan); 1453 tor_assert(chan->conn); 1454 1455 if ((cell->payload_len % 2) == 1) { 1456 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1457 "Received a VERSION cell with odd payload length %d; " 1458 "closing connection.",cell->payload_len); 1459 connection_or_close_for_error(chan->conn, 0); 1460 return; 1461 } 1462 1463 started_here = connection_or_nonopen_was_started_here(chan->conn); 1464 1465 if (chan->conn->link_proto != 0 || 1466 (chan->conn->handshake_state && 1467 chan->conn->handshake_state->received_versions)) { 1468 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1469 "Received a VERSIONS cell on a connection with its version " 1470 "already set to %d; dropping", 1471 (int)(chan->conn->link_proto)); 1472 return; 1473 } 1474 switch (chan->conn->base_.state) 1475 { 1476 case OR_CONN_STATE_OR_HANDSHAKING_V3: 1477 break; 1478 case OR_CONN_STATE_TLS_HANDSHAKING: 1479 case OR_CONN_STATE_SERVER_VERSIONS_WAIT: 1480 default: 1481 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1482 "VERSIONS cell while in unexpected state"); 1483 return; 1484 } 1485 1486 tor_assert(chan->conn->handshake_state); 1487 1488 { 1489 int i; 1490 const uint8_t *cp = cell->payload; 1491 for (i = 0; i < cell->payload_len / 2; ++i, cp += 2) { 1492 uint16_t v = ntohs(get_uint16(cp)); 1493 if (is_or_protocol_version_known(v) && v > highest_supported_version) 1494 highest_supported_version = v; 1495 } 1496 } 1497 if (!highest_supported_version) { 1498 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1499 "Couldn't find a version in common between my version list and the " 1500 "list in the VERSIONS cell; closing connection."); 1501 connection_or_close_for_error(chan->conn, 0); 1502 return; 1503 } else if (highest_supported_version == 1) { 1504 /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS 1505 * cells. */ 1506 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1507 "Used version negotiation protocol to negotiate a v1 connection. " 1508 "That's crazily non-compliant. Closing connection."); 1509 connection_or_close_for_error(chan->conn, 0); 1510 return; 1511 } else if (highest_supported_version < 3 && 1512 chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) { 1513 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1514 "Negotiated link protocol 2 or lower after doing a v3 TLS " 1515 "handshake. Closing connection."); 1516 connection_or_close_for_error(chan->conn, 0); 1517 return; 1518 } 1519 1520 rep_hist_note_negotiated_link_proto(highest_supported_version, started_here); 1521 1522 chan->conn->link_proto = highest_supported_version; 1523 chan->conn->handshake_state->received_versions = 1; 1524 1525 if (chan->conn->link_proto == 2) { 1526 log_info(LD_OR, 1527 "Negotiated version %d on %s; sending NETINFO.", 1528 highest_supported_version, 1529 connection_describe(TO_CONN(chan->conn))); 1530 1531 if (connection_or_send_netinfo(chan->conn) < 0) { 1532 connection_or_close_for_error(chan->conn, 0); 1533 return; 1534 } 1535 } else { 1536 const int send_versions = !started_here; 1537 /* If we want to authenticate, send a CERTS cell */ 1538 const int send_certs = !started_here || public_server_mode(get_options()); 1539 /* If we're a host that got a connection, ask for authentication. */ 1540 const int send_chall = !started_here; 1541 /* If our certs cell will authenticate us, we can send a netinfo cell 1542 * right now. */ 1543 const int send_netinfo = !started_here; 1544 const int send_any = 1545 send_versions || send_certs || send_chall || send_netinfo; 1546 tor_assert(chan->conn->link_proto >= 3); 1547 1548 log_info(LD_OR, 1549 "Negotiated version %d with on %s; %s%s%s%s%s", 1550 highest_supported_version, 1551 connection_describe(TO_CONN(chan->conn)), 1552 send_any ? "Sending cells:" : "Waiting for CERTS cell", 1553 send_versions ? " VERSIONS" : "", 1554 send_certs ? " CERTS" : "", 1555 send_chall ? " AUTH_CHALLENGE" : "", 1556 send_netinfo ? " NETINFO" : ""); 1557 1558 #ifdef DISABLE_V3_LINKPROTO_SERVERSIDE 1559 if (1) { 1560 connection_or_close_normally(chan->conn, 1); 1561 return; 1562 } 1563 #endif /* defined(DISABLE_V3_LINKPROTO_SERVERSIDE) */ 1564 1565 if (send_versions) { 1566 if (connection_or_send_versions(chan->conn, 1) < 0) { 1567 log_warn(LD_OR, "Couldn't send versions cell"); 1568 connection_or_close_for_error(chan->conn, 0); 1569 return; 1570 } 1571 } 1572 1573 /* We set this after sending the versions cell. */ 1574 /*XXXXX symbolic const.*/ 1575 TLS_CHAN_TO_BASE(chan)->wide_circ_ids = 1576 chan->conn->link_proto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS; 1577 chan->conn->wide_circ_ids = TLS_CHAN_TO_BASE(chan)->wide_circ_ids; 1578 1579 TLS_CHAN_TO_BASE(chan)->padding_enabled = 1580 chan->conn->link_proto >= MIN_LINK_PROTO_FOR_CHANNEL_PADDING; 1581 1582 if (send_certs) { 1583 if (connection_or_send_certs_cell(chan->conn) < 0) { 1584 log_warn(LD_OR, "Couldn't send certs cell"); 1585 connection_or_close_for_error(chan->conn, 0); 1586 return; 1587 } 1588 } 1589 if (send_chall) { 1590 if (connection_or_send_auth_challenge_cell(chan->conn) < 0) { 1591 log_warn(LD_OR, "Couldn't send auth_challenge cell"); 1592 connection_or_close_for_error(chan->conn, 0); 1593 return; 1594 } 1595 } 1596 if (send_netinfo) { 1597 if (connection_or_send_netinfo(chan->conn) < 0) { 1598 log_warn(LD_OR, "Couldn't send netinfo cell"); 1599 connection_or_close_for_error(chan->conn, 0); 1600 return; 1601 } 1602 } 1603 } 1604 } 1605 1606 /** 1607 * Process a 'padding_negotiate' cell. 1608 * 1609 * This function is called to handle an incoming PADDING_NEGOTIATE cell; 1610 * enable or disable padding accordingly, and read and act on its timeout 1611 * value contents. 1612 */ 1613 static void 1614 channel_tls_process_padding_negotiate_cell(cell_t *cell, channel_tls_t *chan) 1615 { 1616 channelpadding_negotiate_t *negotiation; 1617 tor_assert(cell); 1618 tor_assert(chan); 1619 tor_assert(chan->conn); 1620 1621 if (chan->conn->link_proto < MIN_LINK_PROTO_FOR_CHANNEL_PADDING) { 1622 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1623 "Received a PADDING_NEGOTIATE cell on v%d connection; dropping.", 1624 chan->conn->link_proto); 1625 return; 1626 } 1627 1628 if (channelpadding_negotiate_parse(&negotiation, cell->payload, 1629 CELL_PAYLOAD_SIZE) < 0) { 1630 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1631 "Received malformed PADDING_NEGOTIATE cell on v%d connection; " 1632 "dropping.", chan->conn->link_proto); 1633 1634 return; 1635 } 1636 1637 channelpadding_update_padding_for_channel(TLS_CHAN_TO_BASE(chan), 1638 negotiation); 1639 1640 channelpadding_negotiate_free(negotiation); 1641 } 1642 1643 /** 1644 * Convert <b>netinfo_addr</b> into corresponding <b>tor_addr</b>. 1645 * Return 0 on success; on failure, return -1 and log a warning. 1646 */ 1647 static int 1648 tor_addr_from_netinfo_addr(tor_addr_t *tor_addr, 1649 const netinfo_addr_t *netinfo_addr) { 1650 tor_assert(tor_addr); 1651 tor_assert(netinfo_addr); 1652 1653 uint8_t type = netinfo_addr_get_addr_type(netinfo_addr); 1654 uint8_t len = netinfo_addr_get_len(netinfo_addr); 1655 1656 if (type == NETINFO_ADDR_TYPE_IPV4 && len == 4) { 1657 uint32_t ipv4 = netinfo_addr_get_addr_ipv4(netinfo_addr); 1658 tor_addr_from_ipv4h(tor_addr, ipv4); 1659 } else if (type == NETINFO_ADDR_TYPE_IPV6 && len == 16) { 1660 const uint8_t *ipv6_bytes = netinfo_addr_getconstarray_addr_ipv6( 1661 netinfo_addr); 1662 tor_addr_from_ipv6_bytes(tor_addr, ipv6_bytes); 1663 } else { 1664 log_fn(LOG_PROTOCOL_WARN, LD_OR, "Cannot read address from NETINFO " 1665 "- wrong type/length."); 1666 return -1; 1667 } 1668 1669 return 0; 1670 } 1671 1672 /** 1673 * Helper: compute the absolute value of a time_t. 1674 * 1675 * (we need this because labs() doesn't always work for time_t, since 1676 * long can be shorter than time_t.) 1677 */ 1678 static inline time_t 1679 time_abs(time_t val) 1680 { 1681 return (val < 0) ? -val : val; 1682 } 1683 1684 /** Return true iff the channel can process a NETINFO cell. For this to return 1685 * true, these channel conditions apply: 1686 * 1687 * 1. Link protocol is version 2 or higher (tor-spec.txt, NETINFO cells 1688 * section). 1689 * 1690 * 2. Underlying OR connection of the channel is either in v2 or v3 1691 * handshaking state. 1692 */ 1693 static bool 1694 can_process_netinfo_cell(const channel_tls_t *chan) 1695 { 1696 /* NETINFO cells can only be negotiated on link protocol 2 or higher. */ 1697 if (chan->conn->link_proto < 2) { 1698 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1699 "Received a NETINFO cell on %s connection; dropping.", 1700 chan->conn->link_proto == 0 ? "non-versioned" : "a v1"); 1701 return false; 1702 } 1703 1704 /* Can't process a NETINFO cell if the connection is not handshaking. */ 1705 if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3) { 1706 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1707 "Received a NETINFO cell on non-handshaking connection; dropping."); 1708 return false; 1709 } 1710 1711 /* Make sure we do have handshake state. */ 1712 tor_assert(chan->conn->handshake_state); 1713 tor_assert(chan->conn->handshake_state->received_versions); 1714 1715 return true; 1716 } 1717 1718 /** Mark the given channel endpoint as a client (which means either a tor 1719 * client or a tor bridge). 1720 * 1721 * This MUST be done on an _unauthenticated_ channel. It is a mistake to mark 1722 * an authenticated channel as a client. 1723 * 1724 * The following is done on the channel: 1725 * 1726 * 1. Marked as a client. 1727 * 2. Type of circuit ID type is set. 1728 * 3. The underlying OR connection is initialized with the address of the 1729 * endpoint. 1730 */ 1731 static void 1732 mark_channel_tls_endpoint_as_client(channel_tls_t *chan) 1733 { 1734 /* Ending up here for an authenticated link is a mistake. */ 1735 if (BUG(chan->conn->handshake_state->authenticated)) { 1736 return; 1737 } 1738 1739 tor_assert(tor_digest_is_zero( 1740 (const char*)(chan->conn->handshake_state-> 1741 authenticated_rsa_peer_id))); 1742 tor_assert(fast_mem_is_zero( 1743 (const char*)(chan->conn->handshake_state-> 1744 authenticated_ed25519_peer_id.pubkey), 32)); 1745 /* If the client never authenticated, it's a tor client or bridge 1746 * relay, and we must not use it for EXTEND requests (nor could we, as 1747 * there are no authenticated peer IDs) */ 1748 channel_mark_client(TLS_CHAN_TO_BASE(chan)); 1749 channel_set_circid_type(TLS_CHAN_TO_BASE(chan), NULL, 1750 chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS); 1751 1752 connection_or_init_conn_from_address(chan->conn, 1753 &(chan->conn->base_.addr), 1754 chan->conn->base_.port, 1755 /* zero, checked above */ 1756 (const char*)(chan->conn->handshake_state-> 1757 authenticated_rsa_peer_id), 1758 NULL, /* Ed25519 ID: Also checked as zero */ 1759 0); 1760 } 1761 1762 /** 1763 * Process a 'netinfo' cell 1764 * 1765 * This function is called to handle an incoming NETINFO cell; read and act 1766 * on its contents, and set the connection state to "open". 1767 */ 1768 static void 1769 channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *chan) 1770 { 1771 time_t timestamp; 1772 uint8_t my_addr_type; 1773 uint8_t my_addr_len; 1774 uint8_t n_other_addrs; 1775 time_t now = time(NULL); 1776 const routerinfo_t *me = router_get_my_routerinfo(); 1777 1778 time_t apparent_skew = 0; 1779 tor_addr_t my_apparent_addr = TOR_ADDR_NULL; 1780 int started_here = 0; 1781 const char *identity_digest = NULL; 1782 1783 tor_assert(cell); 1784 tor_assert(chan); 1785 tor_assert(chan->conn); 1786 1787 /* Make sure we can process a NETINFO cell. Link protocol and state 1788 * validation is done to make sure of it. */ 1789 if (!can_process_netinfo_cell(chan)) { 1790 return; 1791 } 1792 1793 started_here = connection_or_nonopen_was_started_here(chan->conn); 1794 identity_digest = chan->conn->identity_digest; 1795 1796 if (chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) { 1797 tor_assert(chan->conn->link_proto >= 3); 1798 if (started_here) { 1799 if (!(chan->conn->handshake_state->authenticated)) { 1800 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1801 "Got a NETINFO cell from server, " 1802 "but no authentication. Closing the connection."); 1803 connection_or_close_for_error(chan->conn, 0); 1804 return; 1805 } 1806 } else { 1807 /* We're the server. If the client never authenticated, we have some 1808 * housekeeping to do. 1809 * 1810 * It's a tor client or bridge relay, and we must not use it for EXTEND 1811 * requests (nor could we, as there are no authenticated peer IDs) */ 1812 if (!(chan->conn->handshake_state->authenticated)) { 1813 mark_channel_tls_endpoint_as_client(chan); 1814 } 1815 } 1816 } 1817 1818 /* Decode the cell. */ 1819 netinfo_cell_t *netinfo_cell = NULL; 1820 1821 ssize_t parsed = netinfo_cell_parse(&netinfo_cell, cell->payload, 1822 CELL_PAYLOAD_SIZE); 1823 1824 if (parsed < 0) { 1825 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1826 "Failed to parse NETINFO cell - closing connection."); 1827 connection_or_close_for_error(chan->conn, 0); 1828 return; 1829 } 1830 1831 timestamp = netinfo_cell_get_timestamp(netinfo_cell); 1832 1833 const netinfo_addr_t *my_addr = 1834 netinfo_cell_getconst_other_addr(netinfo_cell); 1835 1836 my_addr_type = netinfo_addr_get_addr_type(my_addr); 1837 my_addr_len = netinfo_addr_get_len(my_addr); 1838 1839 if ((now - chan->conn->handshake_state->sent_versions_at) < 180) { 1840 apparent_skew = now - timestamp; 1841 } 1842 /* We used to check: 1843 * if (my_addr_len >= CELL_PAYLOAD_SIZE - 6) { 1844 * 1845 * This is actually never going to happen, since my_addr_len is at most 255, 1846 * and CELL_PAYLOAD_LEN - 6 is 503. So we know that cp is < end. */ 1847 1848 if (tor_addr_from_netinfo_addr(&my_apparent_addr, my_addr) == -1) { 1849 connection_or_close_for_error(chan->conn, 0); 1850 netinfo_cell_free(netinfo_cell); 1851 return; 1852 } 1853 1854 if (my_addr_type == NETINFO_ADDR_TYPE_IPV4 && my_addr_len == 4) { 1855 if (!get_options()->BridgeRelay && me && 1856 tor_addr_eq(&my_apparent_addr, &me->ipv4_addr)) { 1857 TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer = 1; 1858 } 1859 } else if (my_addr_type == NETINFO_ADDR_TYPE_IPV6 && 1860 my_addr_len == 16) { 1861 if (!get_options()->BridgeRelay && me && 1862 !tor_addr_is_null(&me->ipv6_addr) && 1863 tor_addr_eq(&my_apparent_addr, &me->ipv6_addr)) { 1864 TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer = 1; 1865 } 1866 } 1867 1868 if (me) { 1869 /* We have a descriptor, so we are a relay: record the address that the 1870 * other side said we had. */ 1871 tor_addr_copy(&TLS_CHAN_TO_BASE(chan)->addr_according_to_peer, 1872 &my_apparent_addr); 1873 } 1874 1875 n_other_addrs = netinfo_cell_get_n_my_addrs(netinfo_cell); 1876 for (uint8_t i = 0; i < n_other_addrs; i++) { 1877 /* Consider all the other addresses; if any matches, this connection is 1878 * "canonical." */ 1879 1880 const netinfo_addr_t *netinfo_addr = 1881 netinfo_cell_getconst_my_addrs(netinfo_cell, i); 1882 1883 tor_addr_t addr; 1884 1885 if (tor_addr_from_netinfo_addr(&addr, netinfo_addr) == -1) { 1886 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1887 "Bad address in netinfo cell; Skipping."); 1888 continue; 1889 } 1890 /* A relay can connect from anywhere and be canonical, so 1891 * long as it tells you from where it came. This may sound a bit 1892 * concerning... but that's what "canonical" means: that the 1893 * address is one that the relay itself has claimed. The relay 1894 * might be doing something funny, but nobody else is doing a MITM 1895 * on the relay's TCP. 1896 */ 1897 if (tor_addr_eq(&addr, &TO_CONN(chan->conn)->addr)) { 1898 connection_or_set_canonical(chan->conn, 1); 1899 break; 1900 } 1901 } 1902 1903 netinfo_cell_free(netinfo_cell); 1904 1905 if (me && !TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer && 1906 channel_is_canonical(TLS_CHAN_TO_BASE(chan))) { 1907 const char *descr = channel_describe_peer( 1908 TLS_CHAN_TO_BASE(chan)); 1909 log_info(LD_OR, 1910 "We made a connection to a relay at %s (fp=%s) but we think " 1911 "they will not consider this connection canonical. They " 1912 "think we are at %s, but we think its %s.", 1913 safe_str(descr), 1914 safe_str(hex_str(identity_digest, DIGEST_LEN)), 1915 safe_str(tor_addr_is_null(&my_apparent_addr) ? 1916 "<none>" : fmt_and_decorate_addr(&my_apparent_addr)), 1917 safe_str(fmt_addr(&me->ipv4_addr))); 1918 } 1919 1920 /* Act on apparent skew. */ 1921 /** Warn when we get a netinfo skew with at least this value. */ 1922 #define NETINFO_NOTICE_SKEW 3600 1923 if (time_abs(apparent_skew) > NETINFO_NOTICE_SKEW && 1924 (started_here || 1925 connection_or_digest_is_known_relay(chan->conn->identity_digest))) { 1926 int trusted = router_digest_is_trusted_dir(chan->conn->identity_digest); 1927 clock_skew_warning(TO_CONN(chan->conn), apparent_skew, trusted, LD_GENERAL, 1928 "NETINFO cell", "OR"); 1929 } 1930 1931 /* Consider our apparent address as a possible suggestion for our address if 1932 * we were unable to resolve it previously. The endpoint address is passed 1933 * in order to make sure to never consider an address that is the same as 1934 * our endpoint. */ 1935 relay_address_new_suggestion(&my_apparent_addr, &TO_CONN(chan->conn)->addr, 1936 identity_digest); 1937 1938 if (! chan->conn->handshake_state->sent_netinfo) { 1939 /* If we were prepared to authenticate, but we never got an AUTH_CHALLENGE 1940 * cell, then we would not previously have sent a NETINFO cell. Do so 1941 * now. */ 1942 if (connection_or_send_netinfo(chan->conn) < 0) { 1943 connection_or_close_for_error(chan->conn, 0); 1944 return; 1945 } 1946 } 1947 1948 if (connection_or_set_state_open(chan->conn) < 0) { 1949 log_fn(LOG_PROTOCOL_WARN, LD_OR, 1950 "Got good NETINFO cell on %s; but " 1951 "was unable to make the OR connection become open.", 1952 connection_describe(TO_CONN(chan->conn))); 1953 connection_or_close_for_error(chan->conn, 0); 1954 } else { 1955 log_info(LD_OR, 1956 "Got good NETINFO cell on %s; OR connection is now " 1957 "open, using protocol version %d. Its ID digest is %s. " 1958 "Our address is apparently %s.", 1959 connection_describe(TO_CONN(chan->conn)), 1960 (int)(chan->conn->link_proto), 1961 hex_str(identity_digest, DIGEST_LEN), 1962 tor_addr_is_null(&my_apparent_addr) ? 1963 "<none>" : 1964 safe_str_client(fmt_and_decorate_addr(&my_apparent_addr))); 1965 } 1966 assert_connection_ok(TO_CONN(chan->conn),time(NULL)); 1967 } 1968 1969 /** Types of certificates that we know how to parse from CERTS cells. Each 1970 * type corresponds to a different encoding format. */ 1971 typedef enum cert_encoding_t { 1972 CERT_ENCODING_UNKNOWN, /**< We don't recognize this. */ 1973 CERT_ENCODING_X509, /**< It's an RSA key, signed with RSA, encoded in x509. 1974 * (Actually, it might not be RSA. We test that later.) */ 1975 CERT_ENCODING_ED25519, /**< It's something signed with an Ed25519 key, 1976 * encoded asa a tor_cert_t.*/ 1977 CERT_ENCODING_RSA_CROSSCERT, /**< It's an Ed key signed with an RSA key. */ 1978 } cert_encoding_t; 1979 1980 /** 1981 * Given one of the certificate type codes used in a CERTS cell, 1982 * return the corresponding cert_encoding_t that we should use to parse 1983 * the certificate. 1984 */ 1985 static cert_encoding_t 1986 certs_cell_typenum_to_cert_type(int typenum) 1987 { 1988 switch (typenum) { 1989 case CERTTYPE_RSA1024_ID_LINK: 1990 case CERTTYPE_RSA1024_ID_ID: 1991 case CERTTYPE_RSA1024_ID_AUTH: 1992 return CERT_ENCODING_X509; 1993 case CERTTYPE_ED_ID_SIGN: 1994 case CERTTYPE_ED_SIGN_LINK: 1995 case CERTTYPE_ED_SIGN_AUTH: 1996 return CERT_ENCODING_ED25519; 1997 case CERTTYPE_RSA1024_ID_EDID: 1998 return CERT_ENCODING_RSA_CROSSCERT; 1999 default: 2000 return CERT_ENCODING_UNKNOWN; 2001 } 2002 } 2003 2004 /** 2005 * Process a CERTS cell from a channel. 2006 * 2007 * This function is called to process an incoming CERTS cell on a 2008 * channel_tls_t: 2009 * 2010 * If the other side should not have sent us a CERTS cell, or the cell is 2011 * malformed, or it is supposed to authenticate the TLS key but it doesn't, 2012 * then mark the connection. 2013 * 2014 * If the cell has a good cert chain and we're doing a v3 handshake, then 2015 * store the certificates in or_handshake_state. If this is the client side 2016 * of the connection, we then authenticate the server or mark the connection. 2017 * If it's the server side, wait for an AUTHENTICATE cell. 2018 */ 2019 STATIC void 2020 channel_tls_process_certs_cell(var_cell_t *cell, channel_tls_t *chan) 2021 { 2022 #define MAX_CERT_TYPE_WANTED CERTTYPE_RSA1024_ID_EDID 2023 /* These arrays will be sparse, since a cert type can be at most one 2024 * of ed/x509 */ 2025 tor_x509_cert_t *x509_certs[MAX_CERT_TYPE_WANTED + 1]; 2026 tor_cert_t *ed_certs[MAX_CERT_TYPE_WANTED + 1]; 2027 uint8_t *rsa_ed_cc_cert = NULL; 2028 size_t rsa_ed_cc_cert_len = 0; 2029 2030 int n_certs, i; 2031 certs_cell_t *cc = NULL; 2032 2033 int send_netinfo = 0, started_here = 0; 2034 2035 memset(x509_certs, 0, sizeof(x509_certs)); 2036 memset(ed_certs, 0, sizeof(ed_certs)); 2037 tor_assert(cell); 2038 tor_assert(chan); 2039 tor_assert(chan->conn); 2040 2041 #define ERR(s) \ 2042 do { \ 2043 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \ 2044 "Received a bad CERTS cell on %s: %s", \ 2045 connection_describe(TO_CONN(chan->conn)), \ 2046 (s)); \ 2047 connection_or_close_for_error(chan->conn, 0); \ 2048 goto err; \ 2049 } while (0) 2050 2051 /* Can't use connection_or_nonopen_was_started_here(); its conn->tls 2052 * check looks like it breaks 2053 * test_link_handshake_recv_certs_ok_server(). */ 2054 started_here = chan->conn->handshake_state->started_here; 2055 2056 if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3) 2057 ERR("We're not doing a v3 handshake!"); 2058 if (chan->conn->link_proto < 3) 2059 ERR("We're not using link protocol >= 3"); 2060 if (chan->conn->handshake_state->received_certs_cell) 2061 ERR("We already got one"); 2062 if (chan->conn->handshake_state->authenticated) { 2063 /* Should be unreachable, but let's make sure. */ 2064 ERR("We're already authenticated!"); 2065 } 2066 if (cell->payload_len < 1) 2067 ERR("It had no body"); 2068 if (cell->circ_id) 2069 ERR("It had a nonzero circuit ID"); 2070 2071 if (certs_cell_parse(&cc, cell->payload, cell->payload_len) < 0) 2072 ERR("It couldn't be parsed."); 2073 2074 n_certs = cc->n_certs; 2075 2076 for (i = 0; i < n_certs; ++i) { 2077 certs_cell_cert_t *c = certs_cell_get_certs(cc, i); 2078 2079 uint16_t cert_type = c->cert_type; 2080 uint16_t cert_len = c->cert_len; 2081 uint8_t *cert_body = certs_cell_cert_getarray_body(c); 2082 2083 if (cert_type > MAX_CERT_TYPE_WANTED) 2084 continue; 2085 const cert_encoding_t ct = certs_cell_typenum_to_cert_type(cert_type); 2086 switch (ct) { 2087 default: 2088 case CERT_ENCODING_UNKNOWN: 2089 break; 2090 case CERT_ENCODING_X509: { 2091 tor_x509_cert_t *x509_cert = tor_x509_cert_decode(cert_body, cert_len); 2092 if (!x509_cert) { 2093 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, 2094 "Received undecodable certificate in CERTS cell on %s", 2095 connection_describe(TO_CONN(chan->conn))); 2096 } else { 2097 if (x509_certs[cert_type]) { 2098 tor_x509_cert_free(x509_cert); 2099 ERR("Duplicate x509 certificate"); 2100 } else { 2101 x509_certs[cert_type] = x509_cert; 2102 } 2103 } 2104 break; 2105 } 2106 case CERT_ENCODING_ED25519: { 2107 tor_cert_t *ed_cert = tor_cert_parse(cert_body, cert_len); 2108 if (!ed_cert) { 2109 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, 2110 "Received undecodable Ed certificate " 2111 "in CERTS cell on %s", 2112 connection_describe(TO_CONN(chan->conn))); 2113 } else { 2114 if (ed_certs[cert_type]) { 2115 tor_cert_free(ed_cert); 2116 ERR("Duplicate Ed25519 certificate"); 2117 } else { 2118 ed_certs[cert_type] = ed_cert; 2119 } 2120 } 2121 break; 2122 } 2123 2124 case CERT_ENCODING_RSA_CROSSCERT: { 2125 if (rsa_ed_cc_cert) { 2126 ERR("Duplicate RSA->Ed25519 crosscert"); 2127 } else { 2128 rsa_ed_cc_cert = tor_memdup(cert_body, cert_len); 2129 rsa_ed_cc_cert_len = cert_len; 2130 } 2131 break; 2132 } 2133 } 2134 } 2135 2136 /* Move the certificates we (might) want into the handshake_state->certs 2137 * structure. */ 2138 tor_x509_cert_t *id_cert = x509_certs[CERTTYPE_RSA1024_ID_ID]; 2139 tor_x509_cert_t *auth_cert = x509_certs[CERTTYPE_RSA1024_ID_AUTH]; 2140 tor_x509_cert_t *link_cert = x509_certs[CERTTYPE_RSA1024_ID_LINK]; 2141 chan->conn->handshake_state->certs->auth_cert = auth_cert; 2142 chan->conn->handshake_state->certs->link_cert = link_cert; 2143 chan->conn->handshake_state->certs->id_cert = id_cert; 2144 x509_certs[CERTTYPE_RSA1024_ID_ID] = 2145 x509_certs[CERTTYPE_RSA1024_ID_AUTH] = 2146 x509_certs[CERTTYPE_RSA1024_ID_LINK] = NULL; 2147 2148 tor_cert_t *ed_id_sign = ed_certs[CERTTYPE_ED_ID_SIGN]; 2149 tor_cert_t *ed_sign_link = ed_certs[CERTTYPE_ED_SIGN_LINK]; 2150 tor_cert_t *ed_sign_auth = ed_certs[CERTTYPE_ED_SIGN_AUTH]; 2151 chan->conn->handshake_state->certs->ed_id_sign = ed_id_sign; 2152 chan->conn->handshake_state->certs->ed_sign_link = ed_sign_link; 2153 chan->conn->handshake_state->certs->ed_sign_auth = ed_sign_auth; 2154 ed_certs[CERTTYPE_ED_ID_SIGN] = 2155 ed_certs[CERTTYPE_ED_SIGN_LINK] = 2156 ed_certs[CERTTYPE_ED_SIGN_AUTH] = NULL; 2157 2158 chan->conn->handshake_state->certs->ed_rsa_crosscert = rsa_ed_cc_cert; 2159 chan->conn->handshake_state->certs->ed_rsa_crosscert_len = 2160 rsa_ed_cc_cert_len; 2161 rsa_ed_cc_cert = NULL; 2162 2163 int severity; 2164 /* Note that this warns more loudly about time and validity if we were 2165 * _trying_ to connect to an authority, not necessarily if we _did_ connect 2166 * to one. */ 2167 if (started_here && 2168 router_digest_is_trusted_dir(TLS_CHAN_TO_BASE(chan)->identity_digest)) 2169 severity = LOG_WARN; 2170 else 2171 severity = LOG_PROTOCOL_WARN; 2172 2173 const ed25519_public_key_t *checked_ed_id = NULL; 2174 const common_digests_t *checked_rsa_id = NULL; 2175 or_handshake_certs_check_both(severity, 2176 chan->conn->handshake_state->certs, 2177 chan->conn->tls, 2178 time(NULL), 2179 &checked_ed_id, 2180 &checked_rsa_id); 2181 2182 if (!checked_rsa_id) 2183 ERR("Invalid certificate chain!"); 2184 2185 if (started_here) { 2186 /* No more information is needed. */ 2187 2188 chan->conn->handshake_state->authenticated = 1; 2189 chan->conn->handshake_state->authenticated_rsa = 1; 2190 { 2191 const common_digests_t *id_digests = checked_rsa_id; 2192 crypto_pk_t *identity_rcvd; 2193 if (!id_digests) 2194 ERR("Couldn't compute digests for key in ID cert"); 2195 2196 identity_rcvd = tor_tls_cert_get_key(id_cert); 2197 if (!identity_rcvd) { 2198 ERR("Couldn't get RSA key from ID cert."); 2199 } 2200 memcpy(chan->conn->handshake_state->authenticated_rsa_peer_id, 2201 id_digests->d[DIGEST_SHA1], DIGEST_LEN); 2202 channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd, 2203 chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS); 2204 crypto_pk_free(identity_rcvd); 2205 } 2206 2207 if (checked_ed_id) { 2208 chan->conn->handshake_state->authenticated_ed25519 = 1; 2209 memcpy(&chan->conn->handshake_state->authenticated_ed25519_peer_id, 2210 checked_ed_id, sizeof(ed25519_public_key_t)); 2211 } 2212 2213 log_debug(LD_HANDSHAKE, "calling client_learned_peer_id from " 2214 "process_certs_cell"); 2215 2216 if (connection_or_client_learned_peer_id(chan->conn, 2217 chan->conn->handshake_state->authenticated_rsa_peer_id, 2218 checked_ed_id) < 0) 2219 ERR("Problem setting or checking peer id"); 2220 2221 log_info(LD_HANDSHAKE, 2222 "Got some good certificates on %s: Authenticated it with " 2223 "RSA%s", 2224 connection_describe(TO_CONN(chan->conn)), 2225 checked_ed_id ? " and Ed25519" : ""); 2226 2227 if (!public_server_mode(get_options())) { 2228 /* If we initiated the connection and we are not a public server, we 2229 * aren't planning to authenticate at all. At this point we know who we 2230 * are talking to, so we can just send a netinfo now. */ 2231 send_netinfo = 1; 2232 } 2233 } else { 2234 /* We can't call it authenticated till we see an AUTHENTICATE cell. */ 2235 log_info(LD_OR, 2236 "Got some good RSA%s certificates on %s. " 2237 "Waiting for AUTHENTICATE.", 2238 checked_ed_id ? " and Ed25519" : "", 2239 connection_describe(TO_CONN(chan->conn))); 2240 /* XXXX check more stuff? */ 2241 } 2242 2243 chan->conn->handshake_state->received_certs_cell = 1; 2244 2245 if (send_netinfo) { 2246 if (connection_or_send_netinfo(chan->conn) < 0) { 2247 log_warn(LD_OR, "Couldn't send netinfo cell"); 2248 connection_or_close_for_error(chan->conn, 0); 2249 goto err; 2250 } 2251 } 2252 2253 err: 2254 for (unsigned u = 0; u < ARRAY_LENGTH(x509_certs); ++u) { 2255 tor_x509_cert_free(x509_certs[u]); 2256 } 2257 for (unsigned u = 0; u < ARRAY_LENGTH(ed_certs); ++u) { 2258 tor_cert_free(ed_certs[u]); 2259 } 2260 tor_free(rsa_ed_cc_cert); 2261 certs_cell_free(cc); 2262 #undef ERR 2263 } 2264 2265 /** 2266 * Process an AUTH_CHALLENGE cell from a channel_tls_t. 2267 * 2268 * This function is called to handle an incoming AUTH_CHALLENGE cell on a 2269 * channel_tls_t; if we weren't supposed to get one (for example, because we're 2270 * not the originator of the channel), or it's ill-formed, or we aren't doing 2271 * a v3 handshake, mark the channel. If the cell is well-formed but we don't 2272 * want to authenticate, just drop it. If the cell is well-formed *and* we 2273 * want to authenticate, send an AUTHENTICATE cell and then a NETINFO cell. 2274 */ 2275 STATIC void 2276 channel_tls_process_auth_challenge_cell(var_cell_t *cell, channel_tls_t *chan) 2277 { 2278 int n_types, i, use_type = -1; 2279 auth_challenge_cell_t *ac = NULL; 2280 2281 tor_assert(cell); 2282 tor_assert(chan); 2283 tor_assert(chan->conn); 2284 2285 #define ERR(s) \ 2286 do { \ 2287 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \ 2288 "Received a bad AUTH_CHALLENGE cell on %s: %s", \ 2289 connection_describe(TO_CONN(chan->conn)), \ 2290 (s)); \ 2291 connection_or_close_for_error(chan->conn, 0); \ 2292 goto done; \ 2293 } while (0) 2294 2295 if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3) 2296 ERR("We're not currently doing a v3 handshake"); 2297 if (chan->conn->link_proto < 3) 2298 ERR("We're not using link protocol >= 3"); 2299 if (!(chan->conn->handshake_state->started_here)) 2300 ERR("We didn't originate this connection"); 2301 if (chan->conn->handshake_state->received_auth_challenge) 2302 ERR("We already received one"); 2303 if (!(chan->conn->handshake_state->received_certs_cell)) 2304 ERR("We haven't gotten a CERTS cell yet"); 2305 if (cell->circ_id) 2306 ERR("It had a nonzero circuit ID"); 2307 2308 if (auth_challenge_cell_parse(&ac, cell->payload, cell->payload_len) < 0) 2309 ERR("It was not well-formed."); 2310 2311 n_types = ac->n_methods; 2312 2313 /* Now see if there is an authentication type we can use */ 2314 for (i = 0; i < n_types; ++i) { 2315 uint16_t authtype = auth_challenge_cell_get_methods(ac, i); 2316 if (authchallenge_type_is_supported(authtype)) { 2317 if (use_type == -1 || 2318 authchallenge_type_is_better(authtype, use_type)) { 2319 use_type = authtype; 2320 } 2321 } 2322 } 2323 2324 chan->conn->handshake_state->received_auth_challenge = 1; 2325 2326 if (! public_server_mode(get_options())) { 2327 /* If we're not a public server then we don't want to authenticate on a 2328 connection we originated, and we already sent a NETINFO cell when we 2329 got the CERTS cell. We have nothing more to do. */ 2330 goto done; 2331 } 2332 2333 if (use_type >= 0) { 2334 log_info(LD_OR, 2335 "Got an AUTH_CHALLENGE cell on %s: Sending " 2336 "authentication type %d", 2337 connection_describe(TO_CONN(chan->conn)), 2338 use_type); 2339 2340 if (connection_or_send_authenticate_cell(chan->conn, use_type) < 0) { 2341 log_warn(LD_OR, 2342 "Couldn't send authenticate cell"); 2343 connection_or_close_for_error(chan->conn, 0); 2344 goto done; 2345 } 2346 } else { 2347 log_info(LD_OR, 2348 "Got an AUTH_CHALLENGE cell on %s, but we don't " 2349 "know any of its authentication types. Not authenticating.", 2350 connection_describe(TO_CONN(chan->conn))); 2351 } 2352 2353 if (connection_or_send_netinfo(chan->conn) < 0) { 2354 log_warn(LD_OR, "Couldn't send netinfo cell"); 2355 connection_or_close_for_error(chan->conn, 0); 2356 goto done; 2357 } 2358 2359 done: 2360 auth_challenge_cell_free(ac); 2361 2362 #undef ERR 2363 } 2364 2365 /** 2366 * Process an AUTHENTICATE cell from a channel_tls_t. 2367 * 2368 * If it's ill-formed or we weren't supposed to get one or we're not doing a 2369 * v3 handshake, then mark the connection. If it does not authenticate the 2370 * other side of the connection successfully (because it isn't signed right, 2371 * we didn't get a CERTS cell, etc) mark the connection. Otherwise, accept 2372 * the identity of the router on the other side of the connection. 2373 */ 2374 STATIC void 2375 channel_tls_process_authenticate_cell(var_cell_t *cell, channel_tls_t *chan) 2376 { 2377 var_cell_t *expected_cell = NULL; 2378 const uint8_t *auth; 2379 int authlen; 2380 int authtype; 2381 int bodylen; 2382 2383 tor_assert(cell); 2384 tor_assert(chan); 2385 tor_assert(chan->conn); 2386 2387 #define ERR(s) \ 2388 do { \ 2389 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \ 2390 "Received a bad AUTHENTICATE cell on %s: %s", \ 2391 connection_describe(TO_CONN(chan->conn)), \ 2392 (s)); \ 2393 connection_or_close_for_error(chan->conn, 0); \ 2394 var_cell_free(expected_cell); \ 2395 return; \ 2396 } while (0) 2397 2398 if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3) 2399 ERR("We're not doing a v3 handshake"); 2400 if (chan->conn->link_proto < 3) 2401 ERR("We're not using link protocol >= 3"); 2402 if (chan->conn->handshake_state->started_here) 2403 ERR("We originated this connection"); 2404 if (chan->conn->handshake_state->received_authenticate) 2405 ERR("We already got one!"); 2406 if (chan->conn->handshake_state->authenticated) { 2407 /* Should be impossible given other checks */ 2408 ERR("The peer is already authenticated"); 2409 } 2410 if (!(chan->conn->handshake_state->received_certs_cell)) 2411 ERR("We never got a certs cell"); 2412 if (chan->conn->handshake_state->certs->id_cert == NULL) 2413 ERR("We never got an identity certificate"); 2414 if (cell->payload_len < 4) 2415 ERR("Cell was way too short"); 2416 2417 auth = cell->payload; 2418 { 2419 uint16_t type = ntohs(get_uint16(auth)); 2420 uint16_t len = ntohs(get_uint16(auth+2)); 2421 if (4 + len > cell->payload_len) 2422 ERR("Authenticator was truncated"); 2423 2424 if (! authchallenge_type_is_supported(type)) 2425 ERR("Authenticator type was not recognized"); 2426 authtype = type; 2427 2428 auth += 4; 2429 authlen = len; 2430 } 2431 2432 if (authlen < V3_AUTH_BODY_LEN + 1) 2433 ERR("Authenticator was too short"); 2434 2435 expected_cell = connection_or_compute_authenticate_cell_body( 2436 chan->conn, authtype, NULL, 1); 2437 if (! expected_cell) 2438 ERR("Couldn't compute expected AUTHENTICATE cell body"); 2439 2440 if (BUG(authtype != AUTHTYPE_ED25519_SHA256_RFC5705)) { 2441 /* We should have detected that we don't support this 2442 * authentication type earlier, when we called 2443 * authchallenge_type_is_supported(). */ 2444 ERR("Unsupported authentication type"); 2445 } else { 2446 /* Our earlier check had better have made sure we had room 2447 * for an ed25519 sig (inadvertently) */ 2448 tor_assert(V3_AUTH_BODY_LEN > ED25519_SIG_LEN); 2449 bodylen = authlen - ED25519_SIG_LEN; 2450 } 2451 if (expected_cell->payload_len != bodylen+4) { 2452 ERR("Expected AUTHENTICATE cell body len not as expected."); 2453 } 2454 2455 /* Length of random part. */ 2456 if (BUG(bodylen < 24)) { 2457 // LCOV_EXCL_START 2458 ERR("Bodylen is somehow less than 24, which should really be impossible"); 2459 // LCOV_EXCL_STOP 2460 } 2461 2462 if (tor_memneq(expected_cell->payload+4, auth, bodylen-24)) 2463 ERR("Some field in the AUTHENTICATE cell body was not as expected"); 2464 2465 { 2466 if (chan->conn->handshake_state->certs->ed_id_sign == NULL) 2467 ERR("We never got an Ed25519 identity certificate."); 2468 if (chan->conn->handshake_state->certs->ed_sign_auth == NULL) 2469 ERR("We never got an Ed25519 authentication certificate."); 2470 2471 const ed25519_public_key_t *authkey = 2472 &chan->conn->handshake_state->certs->ed_sign_auth->signed_key; 2473 ed25519_signature_t sig; 2474 tor_assert(authlen > ED25519_SIG_LEN); 2475 memcpy(&sig.sig, auth + authlen - ED25519_SIG_LEN, ED25519_SIG_LEN); 2476 if (ed25519_checksig(&sig, auth, authlen - ED25519_SIG_LEN, authkey)<0) { 2477 ERR("Ed25519 signature wasn't valid."); 2478 } 2479 } 2480 2481 /* Okay, we are authenticated. */ 2482 chan->conn->handshake_state->received_authenticate = 1; 2483 chan->conn->handshake_state->authenticated = 1; 2484 chan->conn->handshake_state->authenticated_rsa = 1; 2485 chan->conn->handshake_state->digest_received_data = 0; 2486 { 2487 tor_x509_cert_t *id_cert = chan->conn->handshake_state->certs->id_cert; 2488 crypto_pk_t *identity_rcvd = tor_tls_cert_get_key(id_cert); 2489 const common_digests_t *id_digests = tor_x509_cert_get_id_digests(id_cert); 2490 const ed25519_public_key_t *ed_identity_received = NULL; 2491 2492 { 2493 chan->conn->handshake_state->authenticated_ed25519 = 1; 2494 ed_identity_received = 2495 &chan->conn->handshake_state->certs->ed_id_sign->signing_key; 2496 memcpy(&chan->conn->handshake_state->authenticated_ed25519_peer_id, 2497 ed_identity_received, sizeof(ed25519_public_key_t)); 2498 } 2499 2500 /* This must exist; we checked key type when reading the cert. */ 2501 tor_assert(id_digests); 2502 2503 memcpy(chan->conn->handshake_state->authenticated_rsa_peer_id, 2504 id_digests->d[DIGEST_SHA1], DIGEST_LEN); 2505 2506 channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd, 2507 chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS); 2508 crypto_pk_free(identity_rcvd); 2509 2510 log_debug(LD_HANDSHAKE, 2511 "Calling connection_or_init_conn_from_address on %s " 2512 " from %s, with%s ed25519 id.", 2513 connection_describe(TO_CONN(chan->conn)), 2514 __func__, 2515 ed_identity_received ? "" : "out"); 2516 2517 connection_or_init_conn_from_address(chan->conn, 2518 &(chan->conn->base_.addr), 2519 chan->conn->base_.port, 2520 (const char*)(chan->conn->handshake_state-> 2521 authenticated_rsa_peer_id), 2522 ed_identity_received, 2523 0); 2524 2525 log_debug(LD_HANDSHAKE, 2526 "Got an AUTHENTICATE cell on %s, type %d: Looks good.", 2527 connection_describe(TO_CONN(chan->conn)), 2528 authtype); 2529 } 2530 2531 var_cell_free(expected_cell); 2532 2533 #undef ERR 2534 }