37#define CHANNEL_OBJECT_PRIVATE
39#define CHANNELTLS_PRIVATE
47#include "core/or/dos.h"
55#include "trunnel/link_handshake.h"
65#include "trunnel/channelpadding_negotiation.h"
66#include "trunnel/netinfo.h"
67#include "core/or/channelpadding.h"
140 channel_tls_t *tlschan);
142 channel_tls_t *tlschan);
145 channel_tls_t *tlschan);
147 channel_tls_t *chan);
160 chan = &(tlschan->base_);
162 chan->
magic = TLS_CHAN_MAGIC;
195 const char *id_digest,
197 const struct circuit_guard_state_t *guard_state,
198 bool for_origin_circ)
200 channel_tls_t *tlschan = tor_malloc_zero(
sizeof(*tlschan));
206 "In channel_tls_connect() for channel %p "
207 "(global id %"PRIu64
")",
213 "Marking new outgoing channel %"PRIu64
" at %p as local",
218 "Marking new outgoing channel %"PRIu64
" at %p as remote",
245 tlschan->conn->chan = NULL;
246 tlschan->conn = NULL;
252 tlschan->conn = conn;
255 "Got orconn %p for channel with global id %"PRIu64,
266 circuitmux_free(chan->
cmux);
301 listener = tor_malloc_zero(
sizeof(*listener));
311 "Starting TLS channel listener %p with global id %"PRIu64,
332 "Shutting down TLS channels...");
341 "Closing channel_tls_listener with ID %"PRIu64
347 channel_listener_free(old_listener);
352 "Done shutting down TLS channels");
361 channel_tls_t *tlschan = tor_malloc_zero(
sizeof(*tlschan));
370 tlschan->conn = orconn;
371 orconn->
chan = tlschan;
375 "Marking new incoming channel %"PRIu64
" at %p as local",
380 "Marking new incoming channel %"PRIu64
" at %p as remote",
390 char *transport_name = NULL;
392 &transport_name) < 0) {
393 transport_name = NULL;
399 &
TO_CONN(orconn)->addr, transport_name,
401 dos_new_client_conn(orconn, transport_name);
422 if (chan->
magic != TLS_CHAN_MAGIC)
429 tlschan->conn->potentially_used_for_bootstrapping = 1;
442 if (!tlschan)
return NULL;
444 return &(tlschan->base_);
454 if (!chan)
return NULL;
458 return (channel_tls_t *)(chan);
492 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
500 "Tried to close channel_tls_t %p with NULL conn",
515 static char *buf = NULL;
517 channel_tls_t *tlschan;
518 const char *rv = NULL;
522 tlschan = BASE_CHAN_TO_TLS(chan);
525 id =
TO_CONN(tlschan->conn)->global_identifier;
529 "TLS channel (connection %"PRIu64
")",
534 rv =
"TLS channel (no connection)";
553 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
558 tlschan->conn->chan = NULL;
559 tlschan->conn = NULL;
569 double overhead = 1.0;
570 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
576 if (tlschan->conn->bytes_xmitted > 0 &&
577 tlschan->conn->bytes_xmitted_by_tls >=
578 tlschan->conn->bytes_xmitted) {
579 overhead = ((double)(tlschan->conn->bytes_xmitted_by_tls)) /
580 ((
double)(tlschan->conn->bytes_xmitted));
591 "Estimated overhead ratio for TLS chan %"PRIu64
" is %f",
610 const channel_tls_t *tlschan = CONST_BASE_CHAN_TO_TLS(chan);
615 if (tlschan->conn == NULL) {
637 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
643 if (!tlschan->conn->ext_or_transport)
646 *transport_out = tor_strdup(tlschan->conn->ext_or_transport);
659 const channel_tls_t *tlschan = CONST_BASE_CHAN_TO_TLS(chan);
665 return "(No connection)";
679 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
682 if (!(tlschan->conn)) {
684 "something called has_queued_writes on a tlschan "
685 "(%p with ID %"PRIu64
" but no conn",
689 outbuf_len = (tlschan->conn != NULL) ?
690 connection_get_outbuf_len(
TO_CONN(tlschan->conn)) :
693 return (outbuf_len > 0);
706 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
716 answer = tlschan->conn->is_canonical;
735 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
741 if (!(tlschan->conn)) {
743 "something called matches_extend_info on a tlschan "
744 "(%p with ID %"PRIu64
" but no conn",
775 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
781 if (!(tlschan->conn)) {
783 "something called matches_target on a tlschan "
784 "(%p with ID %"PRIu64
" but no conn",
811 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
816 return connection_get_outbuf_len(
TO_CONN(tlschan->conn));
831 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
832 size_t cell_network_size;
837 cell_network_size = get_cell_network_size(tlschan->conn->wide_circ_ids);
838 outbuf_len = connection_get_outbuf_len(
TO_CONN(tlschan->conn));
842#if SIZEOF_SIZE_T > SIZEOF_INT
843 if (n > INT_MAX) n = INT_MAX;
858 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
869 "something called write_cell on a tlschan "
870 "(%p with ID %"PRIu64
" but no conn",
891 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
892 size_t cell_network_size = get_cell_network_size(chan->wide_circ_ids);
898 connection_buf_add(packed_cell->
body, cell_network_size,
902 "something called write_packed_cell on a tlschan "
903 "(%p with ID %"PRIu64
" but no conn",
920 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
931 "something called write_var_cell on a tlschan "
932 "(%p with ID %"PRIu64
" but no conn",
970 } SMARTLIST_FOREACH_END(ichan);
993 return "TLS channel (listening)";
1018 base_chan = TLS_CHAN_TO_BASE(chan);
1024 CHANNEL_IS_OPEN(base_chan) ||
1025 CHANNEL_IS_MAINT(base_chan) ||
1026 CHANNEL_IS_CLOSING(base_chan));
1044 if (CHANNEL_IS_OPEN(base_chan)) {
1050#ifdef KEEP_TIMING_STATS
1060channel_tls_time_process_cell(
cell_t *cell, channel_tls_t *chan,
int *time,
1061 void (*func)(
cell_t *, channel_tls_t *))
1068 (*func)(cell, chan);
1071 time_passed =
tv_udiff(&start, &end) ;
1073 if (time_passed > 10000) {
1074 log_debug(
LD_OR,
"That call just took %ld ms.",time_passed/1000);
1077 if (time_passed < 0) {
1078 log_info(
LD_GENERAL,
"That call took us back in time!");
1082 *time += time_passed;
1086#ifdef KEEP_TIMING_STATS
1087#define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
1089 channel_tls_time_process_cell(cl, cn, & tp ## time , \
1090 channel_tls_process_ ## tp ## _cell); \
1093#define PROCESS_CELL(tp, cl, cn) channel_tls_process_ ## tp ## _cell(cl, cn)
1112 channel_tls_t *chan;
1122 "Got a cell_t on an OR connection with no channel");
1134 if (handshaking && cell->
command != CELL_VERSIONS &&
1135 cell->
command != CELL_NETINFO) {
1137 "Received unexpected cell command %d in chan state %s / "
1138 "conn state %s; closing the connection.",
1148 if (BUG(!chan->conn->handshake_state)) {
1150 "Received a cell but handshake state is missing on %s",
1163 if (TLS_CHAN_TO_BASE(chan)->padding_enabled)
1169 if (TLS_CHAN_TO_BASE(chan)->padding_enabled)
1182 "Received unexpected VERSIONS cell on a channel using link "
1187 PROCESS_CELL(netinfo, cell, chan);
1189 case CELL_PADDING_NEGOTIATE:
1191 PROCESS_CELL(padding_negotiate, cell, chan);
1194 case CELL_CREATE_FAST:
1196 case CELL_CREATED_FAST:
1198 case CELL_RELAY_EARLY:
1210 "Cell of unknown type (%d) received in channeltls.c. "
1237 channel_tls_t *chan;
1239#ifdef KEEP_TIMING_STATS
1242 static int num_versions = 0, num_certs = 0;
1244 time_t now = time(NULL);
1250 "At end of second: %d versions (%d ms), %d certs (%d ms)",
1254 num_versions = num_certs = 0;
1255 versions_time = certs_time = 0;
1269 "Got a var_cell_t on an OR connection with no channel");
1273 if (
TO_CONN(conn)->marked_for_close)
1276 switch (
TO_CONN(conn)->state) {
1283 tor_assert_nonfatal_unreached_once();
1288 "Received a cell with command %d in unexpected "
1289 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1290 "closing the connection.",
1295 (
int)(TLS_CHAN_TO_BASE(chan)->state));
1305 if (var_cell->
command != CELL_AUTHENTICATE) {
1307 if (BUG(!chan->conn->handshake_state)) {
1309 "Received a var cell but handshake state is missing on %s",
1321 "Received a variable-length cell with command %d in orconn "
1322 "state %s [%d], channel state %s [%d] with link protocol %d; "
1328 (
int)(TLS_CHAN_TO_BASE(chan)->state),
1335 "Received var-length cell with command %d in unexpected "
1336 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1342 (
int)(TLS_CHAN_TO_BASE(chan)->state));
1355 PROCESS_CELL(versions, var_cell, chan);
1363 PROCESS_CELL(certs, var_cell, chan);
1365 case CELL_AUTH_CHALLENGE:
1367 PROCESS_CELL(auth_challenge, var_cell, chan);
1369 case CELL_AUTHENTICATE:
1371 PROCESS_CELL(authenticate, var_cell, chan);
1373 case CELL_AUTHORIZE:
1379 "Variable-length cell of unknown type (%d) received.",
1405 chan = TLS_CHAN_TO_BASE(conn->
chan);
1410 "Marking channel %"PRIu64
" at %p as local",
1417 "Marking channel %"PRIu64
" at %p as remote",
1436 case CELL_AUTHORIZE:
1454 int started_here = 0;
1468 "Received a cell while TLS-handshaking, not in "
1469 "OR_HANDSHAKING_V3, on a connection we originated.");
1477 chan->conn->handshake_state, cell, 1);
1493 int highest_supported_version = 0;
1494 int started_here = 0;
1502 "Received a VERSION cell with odd payload length %d; "
1510 if (chan->conn->link_proto != 0 ||
1511 (chan->conn->handshake_state &&
1512 chan->conn->handshake_state->received_versions)) {
1514 "Received a VERSIONS cell on a connection with its version "
1515 "already set to %d; dropping",
1516 (
int)(chan->conn->link_proto));
1519 switch (chan->conn->base_.state)
1527 "VERSIONS cell while in unexpected state");
1535 const uint8_t *cp = cell->
payload;
1536 for (i = 0; i < cell->
payload_len / 2; ++i, cp += 2) {
1539 highest_supported_version = v;
1542 if (!highest_supported_version) {
1544 "Couldn't find a version in common between my version list and the "
1545 "list in the VERSIONS cell; closing connection.");
1548 }
else if (highest_supported_version == 1) {
1552 "Used version negotiation protocol to negotiate a v1 connection. "
1553 "That's crazily non-compliant. Closing connection.");
1556 }
else if (highest_supported_version < 3 &&
1559 "Negotiated link protocol 2 or lower after doing a v3 TLS "
1560 "handshake. Closing connection.");
1567 chan->conn->link_proto = highest_supported_version;
1568 chan->conn->handshake_state->received_versions = 1;
1570 if (chan->conn->link_proto == 2) {
1572 "Negotiated version %d on %s; sending NETINFO.",
1573 highest_supported_version,
1581 const int send_versions = !started_here;
1583 const int send_certs = !started_here || public_server_mode(
get_options());
1585 const int send_chall = !started_here;
1588 const int send_netinfo = !started_here;
1589 const int send_any =
1590 send_versions || send_certs || send_chall || send_netinfo;
1594 "Negotiated version %d with on %s; %s%s%s%s%s",
1595 highest_supported_version,
1597 send_any ?
"Sending cells:" :
"Waiting for CERTS cell",
1598 send_versions ?
" VERSIONS" :
"",
1599 send_certs ?
" CERTS" :
"",
1600 send_chall ?
" AUTH_CHALLENGE" :
"",
1601 send_netinfo ?
" NETINFO" :
"");
1603#ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
1610 if (send_versions) {
1612 log_warn(
LD_OR,
"Couldn't send versions cell");
1620 TLS_CHAN_TO_BASE(chan)->wide_circ_ids =
1621 chan->conn->link_proto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
1622 chan->conn->wide_circ_ids = TLS_CHAN_TO_BASE(chan)->wide_circ_ids;
1624 TLS_CHAN_TO_BASE(chan)->padding_enabled =
1625 chan->conn->link_proto >= MIN_LINK_PROTO_FOR_CHANNEL_PADDING;
1629 log_warn(
LD_OR,
"Couldn't send certs cell");
1636 log_warn(
LD_OR,
"Couldn't send auth_challenge cell");
1643 log_warn(
LD_OR,
"Couldn't send netinfo cell");
1661 channelpadding_negotiate_t *negotiation;
1666 if (chan->conn->link_proto < MIN_LINK_PROTO_FOR_CHANNEL_PADDING) {
1668 "Received a PADDING_NEGOTIATE cell on v%d connection; dropping.",
1669 chan->conn->link_proto);
1673 if (channelpadding_negotiate_parse(&negotiation, cell->
payload,
1676 "Received malformed PADDING_NEGOTIATE cell on v%d connection; "
1677 "dropping.", chan->conn->link_proto);
1685 channelpadding_negotiate_free(negotiation);
1694 const netinfo_addr_t *netinfo_addr) {
1698 uint8_t type = netinfo_addr_get_addr_type(netinfo_addr);
1699 uint8_t len = netinfo_addr_get_len(netinfo_addr);
1701 if (type == NETINFO_ADDR_TYPE_IPV4 && len == 4) {
1702 uint32_t ipv4 = netinfo_addr_get_addr_ipv4(netinfo_addr);
1704 }
else if (type == NETINFO_ADDR_TYPE_IPV6 && len == 16) {
1705 const uint8_t *ipv6_bytes = netinfo_addr_getconstarray_addr_ipv6(
1709 log_fn(LOG_PROTOCOL_WARN,
LD_OR,
"Cannot read address from NETINFO "
1710 "- wrong type/length.");
1726 return (val < 0) ? -val : val;
1742 if (chan->conn->link_proto < 2) {
1744 "Received a NETINFO cell on %s connection; dropping.",
1745 chan->conn->link_proto == 0 ?
"non-versioned" :
"a v1");
1752 "Received a NETINFO cell on non-handshaking connection; dropping.");
1758 tor_assert(chan->conn->handshake_state->received_versions);
1780 if (BUG(chan->conn->handshake_state->authenticated)) {
1785 (
const char*)(chan->conn->handshake_state->
1786 authenticated_rsa_peer_id)));
1788 (
const char*)(chan->conn->handshake_state->
1789 authenticated_ed25519_peer_id.pubkey), 32));
1795 chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
1798 &(chan->conn->base_.addr),
1799 chan->conn->base_.port,
1801 (
const char*)(chan->conn->handshake_state->
1802 authenticated_rsa_peer_id),
1817 uint8_t my_addr_type;
1818 uint8_t my_addr_len;
1819 uint8_t n_other_addrs;
1820 time_t now = time(NULL);
1823 time_t apparent_skew = 0;
1825 int started_here = 0;
1826 const char *identity_digest = NULL;
1839 identity_digest = chan->conn->identity_digest;
1844 if (!(chan->conn->handshake_state->authenticated)) {
1846 "Got a NETINFO cell from server, "
1847 "but no authentication. Closing the connection.");
1857 if (!(chan->conn->handshake_state->authenticated)) {
1864 netinfo_cell_t *netinfo_cell = NULL;
1866 ssize_t parsed = netinfo_cell_parse(&netinfo_cell, cell->
payload,
1871 "Failed to parse NETINFO cell - closing connection.");
1876 timestamp = netinfo_cell_get_timestamp(netinfo_cell);
1878 const netinfo_addr_t *my_addr =
1879 netinfo_cell_getconst_other_addr(netinfo_cell);
1881 my_addr_type = netinfo_addr_get_addr_type(my_addr);
1882 my_addr_len = netinfo_addr_get_len(my_addr);
1884 if ((now - chan->conn->handshake_state->sent_versions_at) < 180) {
1885 apparent_skew = now - timestamp;
1895 netinfo_cell_free(netinfo_cell);
1899 if (my_addr_type == NETINFO_ADDR_TYPE_IPV4 && my_addr_len == 4) {
1902 TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer = 1;
1904 }
else if (my_addr_type == NETINFO_ADDR_TYPE_IPV6 &&
1905 my_addr_len == 16) {
1909 TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer = 1;
1916 tor_addr_copy(&TLS_CHAN_TO_BASE(chan)->addr_according_to_peer,
1920 n_other_addrs = netinfo_cell_get_n_my_addrs(netinfo_cell);
1921 for (uint8_t i = 0; i < n_other_addrs; i++) {
1925 const netinfo_addr_t *netinfo_addr =
1926 netinfo_cell_getconst_my_addrs(netinfo_cell, i);
1932 "Bad address in netinfo cell; Skipping.");
1943 connection_or_set_canonical(chan->conn, 1);
1948 netinfo_cell_free(netinfo_cell);
1950 if (me && !TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer &&
1953 TLS_CHAN_TO_BASE(chan));
1955 "We made a connection to a relay at %s (fp=%s) but we think "
1956 "they will not consider this connection canonical. They "
1957 "think we are at %s, but we think its %s.",
1967#define NETINFO_NOTICE_SKEW 3600
1968 if (
time_abs(apparent_skew) > NETINFO_NOTICE_SKEW &&
1971 int trusted = router_digest_is_trusted_dir(chan->conn->identity_digest);
1973 "NETINFO cell",
"OR");
1983 if (! chan->conn->handshake_state->sent_netinfo) {
1995 "Got good NETINFO cell on %s; but "
1996 "was unable to make the OR connection become open.",
2001 "Got good NETINFO cell on %s; OR connection is now "
2002 "open, using protocol version %d. Its ID digest is %s. "
2003 "Our address is apparently %s.",
2005 (
int)(chan->conn->link_proto),
2034 case CERTTYPE_RSA1024_ID_LINK:
2035 case CERTTYPE_RSA1024_ID_ID:
2036 case CERTTYPE_RSA1024_ID_AUTH:
2038 case CERTTYPE_ED_ID_SIGN:
2039 case CERTTYPE_ED_SIGN_LINK:
2040 case CERTTYPE_ED_SIGN_AUTH:
2042 case CERTTYPE_RSA1024_ID_EDID:
2067#define MAX_CERT_TYPE_WANTED CERTTYPE_RSA1024_ID_EDID
2070 tor_x509_cert_t *x509_certs[MAX_CERT_TYPE_WANTED + 1];
2071 tor_cert_t *ed_certs[MAX_CERT_TYPE_WANTED + 1];
2072 uint8_t *rsa_ed_cc_cert = NULL;
2073 size_t rsa_ed_cc_cert_len = 0;
2076 certs_cell_t *cc = NULL;
2078 int send_netinfo = 0, started_here = 0;
2080 memset(x509_certs, 0,
sizeof(x509_certs));
2081 memset(ed_certs, 0,
sizeof(ed_certs));
2088 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2089 "Received a bad CERTS cell on %s: %s", \
2090 connection_describe(TO_CONN(chan->conn)), \
2092 connection_or_close_for_error(chan->conn, 0); \
2097 ERR(
"We're not doing a v3 handshake!");
2099 if (BUG(!chan->conn->handshake_state)) {
2100 ERR(
"Handshake state not present.");
2106 started_here = chan->conn->handshake_state->started_here;
2108 if (chan->conn->link_proto < 3)
2109 ERR(
"We're not using link protocol >= 3");
2110 if (chan->conn->handshake_state->received_certs_cell)
2111 ERR(
"We already got one");
2112 if (chan->conn->handshake_state->authenticated) {
2114 ERR(
"We're already authenticated!");
2117 ERR(
"It had no body");
2119 ERR(
"It had a nonzero circuit ID");
2122 ERR(
"It couldn't be parsed.");
2124 n_certs = cc->n_certs;
2126 for (i = 0; i < n_certs; ++i) {
2127 certs_cell_cert_t *c = certs_cell_get_certs(cc, i);
2129 uint16_t cert_type = c->cert_type;
2130 uint16_t cert_len = c->cert_len;
2131 uint8_t *cert_body = certs_cell_cert_getarray_body(c);
2133 if (cert_type > MAX_CERT_TYPE_WANTED)
2144 "Received undecodable certificate in CERTS cell on %s",
2147 if (x509_certs[cert_type]) {
2148 tor_x509_cert_free(x509_cert);
2149 ERR(
"Duplicate x509 certificate");
2151 x509_certs[cert_type] = x509_cert;
2160 "Received undecodable Ed certificate "
2161 "in CERTS cell on %s",
2164 if (ed_certs[cert_type]) {
2165 tor_cert_free(ed_cert);
2166 ERR(
"Duplicate Ed25519 certificate");
2168 ed_certs[cert_type] = ed_cert;
2175 if (rsa_ed_cc_cert) {
2176 ERR(
"Duplicate RSA->Ed25519 crosscert");
2178 rsa_ed_cc_cert = tor_memdup(cert_body, cert_len);
2179 rsa_ed_cc_cert_len = cert_len;
2188 tor_x509_cert_t *id_cert = x509_certs[CERTTYPE_RSA1024_ID_ID];
2189 tor_x509_cert_t *auth_cert = x509_certs[CERTTYPE_RSA1024_ID_AUTH];
2190 tor_x509_cert_t *link_cert = x509_certs[CERTTYPE_RSA1024_ID_LINK];
2191 chan->conn->handshake_state->certs->auth_cert = auth_cert;
2192 chan->conn->handshake_state->certs->link_cert = link_cert;
2193 chan->conn->handshake_state->certs->id_cert = id_cert;
2194 x509_certs[CERTTYPE_RSA1024_ID_ID] =
2195 x509_certs[CERTTYPE_RSA1024_ID_AUTH] =
2196 x509_certs[CERTTYPE_RSA1024_ID_LINK] = NULL;
2198 tor_cert_t *ed_id_sign = ed_certs[CERTTYPE_ED_ID_SIGN];
2199 tor_cert_t *ed_sign_link = ed_certs[CERTTYPE_ED_SIGN_LINK];
2200 tor_cert_t *ed_sign_auth = ed_certs[CERTTYPE_ED_SIGN_AUTH];
2201 chan->conn->handshake_state->certs->ed_id_sign = ed_id_sign;
2202 chan->conn->handshake_state->certs->ed_sign_link = ed_sign_link;
2203 chan->conn->handshake_state->certs->ed_sign_auth = ed_sign_auth;
2204 ed_certs[CERTTYPE_ED_ID_SIGN] =
2205 ed_certs[CERTTYPE_ED_SIGN_LINK] =
2206 ed_certs[CERTTYPE_ED_SIGN_AUTH] = NULL;
2208 chan->conn->handshake_state->certs->ed_rsa_crosscert = rsa_ed_cc_cert;
2209 chan->conn->handshake_state->certs->ed_rsa_crosscert_len =
2211 rsa_ed_cc_cert = NULL;
2218 router_digest_is_trusted_dir(TLS_CHAN_TO_BASE(chan)->identity_digest))
2221 severity = LOG_PROTOCOL_WARN;
2226 chan->conn->handshake_state->certs,
2232 if (!checked_rsa_id)
2233 ERR(
"Invalid certificate chain!");
2238 chan->conn->handshake_state->authenticated = 1;
2239 chan->conn->handshake_state->authenticated_rsa = 1;
2244 ERR(
"Couldn't compute digests for key in ID cert");
2247 if (!identity_rcvd) {
2248 ERR(
"Couldn't get RSA key from ID cert.");
2250 memcpy(chan->conn->handshake_state->authenticated_rsa_peer_id,
2253 chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
2254 crypto_pk_free(identity_rcvd);
2257 if (checked_ed_id) {
2258 chan->conn->handshake_state->authenticated_ed25519 = 1;
2259 memcpy(&chan->conn->handshake_state->authenticated_ed25519_peer_id,
2263 log_debug(
LD_HANDSHAKE,
"calling client_learned_peer_id from "
2264 "process_certs_cell");
2267 chan->conn->handshake_state->authenticated_rsa_peer_id,
2269 ERR(
"Problem setting or checking peer id");
2272 "Got some good certificates on %s: Authenticated it with "
2275 checked_ed_id ?
" and Ed25519" :
"");
2286 "Got some good RSA%s certificates on %s. "
2287 "Waiting for AUTHENTICATE.",
2288 checked_ed_id ?
" and Ed25519" :
"",
2293 chan->conn->handshake_state->received_certs_cell = 1;
2297 log_warn(
LD_OR,
"Couldn't send netinfo cell");
2304 for (
unsigned u = 0; u <
ARRAY_LENGTH(x509_certs); ++u) {
2305 tor_x509_cert_free(x509_certs[u]);
2307 for (
unsigned u = 0; u <
ARRAY_LENGTH(ed_certs); ++u) {
2308 tor_cert_free(ed_certs[u]);
2311 certs_cell_free(cc);
2328 int n_types, i, use_type = -1;
2329 auth_challenge_cell_t *ac = NULL;
2337 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2338 "Received a bad AUTH_CHALLENGE cell on %s: %s", \
2339 connection_describe(TO_CONN(chan->conn)), \
2341 connection_or_close_for_error(chan->conn, 0); \
2346 ERR(
"We're not currently doing a v3 handshake");
2347 if (chan->conn->link_proto < 3)
2348 ERR(
"We're not using link protocol >= 3");
2349 if (BUG(!chan->conn->handshake_state))
2350 ERR(
"Handshake state not present.");
2351 if (!(chan->conn->handshake_state->started_here))
2352 ERR(
"We didn't originate this connection");
2353 if (chan->conn->handshake_state->received_auth_challenge)
2354 ERR(
"We already received one");
2355 if (!(chan->conn->handshake_state->received_certs_cell))
2356 ERR(
"We haven't gotten a CERTS cell yet");
2358 ERR(
"It had a nonzero circuit ID");
2361 ERR(
"It was not well-formed.");
2363 n_types = ac->n_methods;
2366 for (i = 0; i < n_types; ++i) {
2367 uint16_t authtype = auth_challenge_cell_get_methods(ac, i);
2368 if (authchallenge_type_is_supported(authtype)) {
2369 if (use_type == -1 ||
2370 authchallenge_type_is_better(authtype, use_type)) {
2371 use_type = authtype;
2376 chan->conn->handshake_state->received_auth_challenge = 1;
2385 if (use_type >= 0) {
2387 "Got an AUTH_CHALLENGE cell on %s: Sending "
2388 "authentication type %d",
2394 "Couldn't send authenticate cell");
2400 "Got an AUTH_CHALLENGE cell on %s, but we don't "
2401 "know any of its authentication types. Not authenticating.",
2406 log_warn(
LD_OR,
"Couldn't send netinfo cell");
2412 auth_challenge_cell_free(ac);
2430 const uint8_t *auth;
2441 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2442 "Received a bad AUTHENTICATE cell on %s: %s", \
2443 connection_describe(TO_CONN(chan->conn)), \
2445 connection_or_close_for_error(chan->conn, 0); \
2446 var_cell_free(expected_cell); \
2451 ERR(
"We're not doing a v3 handshake");
2452 if (chan->conn->link_proto < 3)
2453 ERR(
"We're not using link protocol >= 3");
2454 if (BUG(!chan->conn->handshake_state))
2455 ERR(
"Handshake state not present.");
2456 if (chan->conn->handshake_state->started_here)
2457 ERR(
"We originated this connection");
2458 if (chan->conn->handshake_state->received_authenticate)
2459 ERR(
"We already got one!");
2460 if (chan->conn->handshake_state->authenticated) {
2462 ERR(
"The peer is already authenticated");
2464 if (!(chan->conn->handshake_state->received_certs_cell))
2465 ERR(
"We never got a certs cell");
2466 if (chan->conn->handshake_state->certs->id_cert == NULL)
2467 ERR(
"We never got an identity certificate");
2469 ERR(
"Cell was way too short");
2476 ERR(
"Authenticator was truncated");
2478 if (! authchallenge_type_is_supported(type))
2479 ERR(
"Authenticator type was not recognized");
2487 ERR(
"Authenticator was too short");
2490 chan->conn, authtype, NULL, 1);
2491 if (! expected_cell)
2492 ERR(
"Couldn't compute expected AUTHENTICATE cell body");
2498 ERR(
"Unsupported authentication type");
2506 ERR(
"Expected AUTHENTICATE cell body len not as expected.");
2510 if (BUG(bodylen < 24)) {
2512 ERR(
"Bodylen is somehow less than 24, which should really be impossible");
2517 ERR(
"Some field in the AUTHENTICATE cell body was not as expected");
2520 if (chan->conn->handshake_state->certs->ed_id_sign == NULL)
2521 ERR(
"We never got an Ed25519 identity certificate.");
2522 if (chan->conn->handshake_state->certs->ed_sign_auth == NULL)
2523 ERR(
"We never got an Ed25519 authentication certificate.");
2526 &chan->conn->handshake_state->certs->ed_sign_auth->signed_key;
2531 ERR(
"Ed25519 signature wasn't valid.");
2536 chan->conn->handshake_state->received_authenticate = 1;
2537 chan->conn->handshake_state->authenticated = 1;
2538 chan->conn->handshake_state->authenticated_rsa = 1;
2539 chan->conn->handshake_state->digest_received_data = 0;
2541 tor_x509_cert_t *id_cert = chan->conn->handshake_state->certs->id_cert;
2543 const common_digests_t *id_digests = tor_x509_cert_get_id_digests(id_cert);
2547 chan->conn->handshake_state->authenticated_ed25519 = 1;
2548 ed_identity_received =
2549 &chan->conn->handshake_state->certs->ed_id_sign->signing_key;
2550 memcpy(&chan->conn->handshake_state->authenticated_ed25519_peer_id,
2557 memcpy(chan->conn->handshake_state->authenticated_rsa_peer_id,
2561 chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
2562 crypto_pk_free(identity_rcvd);
2565 "Calling connection_or_init_conn_from_address on %s "
2566 " from %s, with%s ed25519 id.",
2569 ed_identity_received ?
"" :
"out");
2572 &(chan->conn->base_.addr),
2573 chan->conn->base_.port,
2574 (
const char*)(chan->conn->handshake_state->
2575 authenticated_rsa_peer_id),
2576 ed_identity_received,
2580 "Got an AUTHENTICATE cell on %s, type %d: Looks good.",
2585 var_cell_free(expected_cell);
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
void tor_addr_make_unspec(tor_addr_t *a)
int tor_addr_is_null(const tor_addr_t *addr)
void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const uint8_t *ipv6_bytes)
#define fmt_and_decorate_addr(a)
#define tor_addr_from_ipv4h(dest, v4addr)
#define tor_addr_eq(a, b)
static bool tor_addr_is_unspec(const tor_addr_t *a)
const char * hex_str(const char *from, size_t fromlen)
static uint16_t get_uint16(const void *cp)
Fixed-size cell structure.
void channel_set_circid_type(channel_t *chan, crypto_pk_t *identity_rcvd, int consider_identity)
void channel_listener_unregister(channel_listener_t *chan_l)
void channel_mark_local(channel_t *chan)
void channel_mark_client(channel_t *chan)
void channel_mark_incoming(channel_t *chan)
void channel_init_listener(channel_listener_t *chan_l)
void channel_listener_mark_for_close(channel_listener_t *chan_l)
void channel_process_cell(channel_t *chan, cell_t *cell)
void channel_change_state_open(channel_t *chan)
int channel_is_canonical(channel_t *chan)
const char * channel_state_to_string(channel_state_t state)
void channel_register(channel_t *chan)
void channel_listener_register(channel_listener_t *chan_l)
void channel_mark_remote(channel_t *chan)
int channel_is_local(channel_t *chan)
const char * channel_describe_peer(channel_t *chan)
void channel_mark_for_close(channel_t *chan)
void channel_listener_change_state(channel_listener_t *chan_l, channel_listener_state_t to_state)
void channel_mark_outgoing(channel_t *chan)
void channel_init(channel_t *chan)
void channel_note_establishment_cancelled(channel_t *chan)
void channel_change_state(channel_t *chan, channel_state_t to_state)
Header file for channel.c.
@ CHANNEL_LISTENER_STATE_ERROR
@ CHANNEL_LISTENER_STATE_LISTENING
@ CHANNEL_LISTENER_STATE_CLOSING
@ CHANNEL_LISTENER_STATE_CLOSED
int channelpadding_update_padding_for_channel(channel_t *chan, const channelpadding_negotiate_t *pad_vars)
static int channel_tls_get_remote_addr_method(const channel_t *chan, tor_addr_t *addr_out)
void channel_tls_free_all(void)
void channel_tls_handle_var_cell(var_cell_t *var_cell, or_connection_t *conn)
static channel_listener_t * channel_tls_listener
STATIC void channel_tls_process_authenticate_cell(var_cell_t *cell, channel_tls_t *chan)
static void channel_tls_listener_close_method(channel_listener_t *chan_l)
void channel_mark_as_used_for_origin_circuit(channel_t *chan)
static int tor_addr_from_netinfo_addr(tor_addr_t *tor_addr, const netinfo_addr_t *netinfo_addr)
static const char * channel_tls_describe_transport_method(channel_t *chan)
channel_listener_t * channel_tls_start_listener(void)
static int channel_tls_write_var_cell_method(channel_t *chan, var_cell_t *var_cell)
static int channel_tls_has_queued_writes_method(channel_t *chan)
static void mark_channel_tls_endpoint_as_client(channel_tls_t *chan)
static double channel_tls_get_overhead_estimate_method(channel_t *chan)
void channel_tls_handle_state_change_on_orconn(channel_tls_t *chan, or_connection_t *conn, uint8_t state)
void channel_tls_handle_cell(cell_t *cell, or_connection_t *conn)
static int command_allowed_before_handshake(uint8_t command)
static bool can_process_netinfo_cell(const channel_tls_t *chan)
uint64_t stats_n_padding_cells_processed
channel_listener_t * channel_tls_get_listener(void)
@ CERT_ENCODING_RSA_CROSSCERT
const channel_tls_t * channel_tls_from_base_const(const channel_t *chan)
channel_tls_t * channel_tls_from_base(channel_t *chan)
uint64_t stats_n_certs_cells_processed
static int channel_tls_write_cell_method(channel_t *chan, cell_t *cell)
STATIC void channel_tls_process_auth_challenge_cell(var_cell_t *cell, channel_tls_t *chan)
static void channel_tls_close_method(channel_t *chan)
static int channel_tls_num_cells_writeable_method(channel_t *chan)
static void channel_tls_process_padding_negotiate_cell(cell_t *cell, channel_tls_t *chan)
STATIC void channel_tls_process_certs_cell(var_cell_t *cell, channel_tls_t *chan)
channel_t * channel_tls_connect(const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, const struct circuit_guard_state_t *guard_state, bool for_origin_circ)
uint64_t stats_n_auth_challenge_cells_processed
void channel_tls_update_marks(or_connection_t *conn)
uint64_t stats_n_authorize_cells_processed
uint64_t stats_n_versions_cells_processed
static int enter_v3_handshake_with_cell(var_cell_t *cell, channel_tls_t *tlschan)
const channel_t * channel_tls_to_base_const(const channel_tls_t *tlschan)
uint64_t stats_n_authenticate_cells_processed
static size_t channel_tls_num_bytes_queued_method(channel_t *chan)
static const char * channel_tls_describe_peer_method(const channel_t *chan)
channel_t * channel_tls_handle_incoming(or_connection_t *orconn)
static void channel_tls_free_method(channel_t *chan)
static cert_encoding_t certs_cell_typenum_to_cert_type(int typenum)
uint64_t stats_n_vpadding_cells_processed
static int channel_tls_is_canonical_method(channel_t *chan)
static int channel_tls_matches_extend_info_method(channel_t *chan, extend_info_t *extend_info)
static void channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *tlschan)
channel_t * channel_tls_to_base(channel_tls_t *tlschan)
static int channel_tls_write_packed_cell_method(channel_t *chan, packed_cell_t *packed_cell)
static int channel_tls_get_transport_name_method(channel_t *chan, char **transport_out)
static const char * channel_tls_listener_describe_transport_method(channel_listener_t *chan_l)
static time_t time_abs(time_t val)
static void channel_tls_process_versions_cell(var_cell_t *cell, channel_tls_t *tlschan)
STATIC void channel_tls_common_init(channel_tls_t *tlschan)
static int channel_tls_matches_target_method(channel_t *chan, const tor_addr_t *target)
uint64_t stats_n_netinfo_cells_processed
Header file for channeltls.c.
void circuitmux_set_policy(circuitmux_t *cmux, const circuitmux_policy_t *pol)
circuitmux_t * circuitmux_alloc(void)
Header file for circuitmux.c.
Header file for circuitmux_ewma.c.
Header file for command.c.
const or_options_t * get_options(void)
tor_cmdline_mode_t command
Header file for config.c.
Public APIs for congestion control.
static uint32_t or_conn_highwatermark(void)
void clock_skew_warning(const connection_t *conn, long apparent_skew, int trusted, log_domain_mask_t domain, const char *received, const char *source)
void assert_connection_ok(connection_t *conn, time_t now)
const char * connection_describe_peer(const connection_t *conn)
const char * connection_describe(const connection_t *conn)
const char * conn_state_to_string(int type, int state)
Header file for connection.c.
int connection_or_nonopen_was_started_here(struct or_connection_t *conn)
int is_or_protocol_version_known(uint16_t v)
int connection_or_set_state_open(or_connection_t *conn)
void connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
int connection_or_send_versions(or_connection_t *conn, int v3_plus)
int connection_or_client_learned_peer_id(or_connection_t *conn, const uint8_t *rsa_peer_id, const ed25519_public_key_t *ed_peer_id)
void or_handshake_state_record_var_cell(or_connection_t *conn, or_handshake_state_t *state, const var_cell_t *cell, int incoming)
int connection_or_send_netinfo(or_connection_t *conn)
void connection_or_change_state(or_connection_t *conn, uint8_t state)
void or_handshake_state_record_cell(or_connection_t *conn, or_handshake_state_t *state, const cell_t *cell, int incoming)
int connection_or_digest_is_known_relay(const char *id_digest)
void connection_or_init_conn_from_address(or_connection_t *conn, const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, int started_here)
void connection_or_close_for_error(or_connection_t *orconn, int flush)
or_connection_t * connection_or_connect(const tor_addr_t *_addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, channel_tls_t *chan, bool for_origin_circ)
ssize_t connection_or_num_cells_writeable(or_connection_t *conn)
int connection_init_or_handshake_state(or_connection_t *conn, int started_here)
void connection_or_close_normally(or_connection_t *orconn, int flush)
void connection_or_write_var_cell_to_buf(const var_cell_t *cell, or_connection_t *conn)
Header file for connection_or.c.
Header file for control.c.
int ed25519_checksig(const ed25519_signature_t *signature, const uint8_t *msg, size_t len, const ed25519_public_key_t *pubkey)
#define tor_memneq(a, b, sz)
Header file for dirlist.c.
void entry_guards_note_internet_connectivity(guard_selection_t *gs)
struct entry_guard_handle_t * entry_guard_handle_from_state(const circuit_guard_state_t *state)
guard_selection_t * get_guard_selection_info(void)
Header file for circuitbuild.c.
bool extend_info_has_orport(const extend_info_t *ei, const tor_addr_t *addr, uint16_t port)
Header for core/or/extendinfo.c.
Header file for geoip_stats.c.
#define log_fn(severity, domain, args,...)
static time_t current_second
Header file for networkstatus.c.
Master header file for Tor-specific functionality.
#define CELL_PAYLOAD_SIZE
#define AUTHTYPE_ED25519_SHA256_RFC5705
OR handshake certs structure.
OR handshake state structure.
#define OR_CONN_STATE_SERVER_VERSIONS_WAIT
#define OR_CONN_STATE_TLS_HANDSHAKING
#define OR_CONN_STATE_OR_HANDSHAKING_V3
#define OR_CONN_STATE_OPEN
int tor_asprintf(char **strp, const char *fmt,...)
void relay_address_new_suggestion(const tor_addr_t *suggested_addr, const tor_addr_t *peer_addr, const char *identity_digest)
Header file for relay_find_addr.c.
int connection_or_send_certs_cell(or_connection_t *conn)
var_cell_t * connection_or_compute_authenticate_cell_body(or_connection_t *conn, const int authtype, const ed25519_keypair_t *ed_signing_key, int server)
int connection_or_send_authenticate_cell(or_connection_t *conn, int authtype)
int connection_or_send_auth_challenge_cell(or_connection_t *conn)
Header for feature/relay/relay_handshake.c.
void rep_hist_note_negotiated_link_proto(unsigned link_proto, int started_here)
void rep_hist_padding_count_read(padding_type_t type)
Header file for rephist.c.
@ PADDING_TYPE_ENABLED_CELL
@ PADDING_TYPE_ENABLED_TOTAL
bool is_local_to_resolve_addr(const tor_addr_t *addr)
: Return true iff the given addr is judged to be local to our resolved address.
Header file for resolve_addr.c.
const routerinfo_t * router_get_my_routerinfo(void)
Header file for router.c.
Router descriptor structure.
Header file for routermode.c.
void scheduler_channel_wants_writes(channel_t *chan)
Header file for scheduler*.c.
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
uint8_t payload[CELL_PAYLOAD_SIZE]
channel_listener_state_t state
const char *(* describe_transport)(channel_listener_t *)
uint64_t global_identifier
smartlist_t * incoming_list
void(* close)(channel_listener_t *)
void(* free_fn)(channel_t *)
int(* is_canonical)(channel_t *)
void(* close)(channel_t *)
enum channel_t::@9 reason_for_closing
int(* matches_extend_info)(channel_t *, extend_info_t *)
struct entry_guard_handle_t * establishment_guard
uint64_t global_identifier
int(* write_packed_cell)(channel_t *, packed_cell_t *)
const char *(* describe_transport)(channel_t *)
double(* get_overhead_estimate)(channel_t *)
int(* matches_target)(channel_t *, const tor_addr_t *)
const char *(* describe_peer)(const channel_t *)
int(* write_var_cell)(channel_t *, var_cell_t *)
int(* has_queued_writes)(channel_t *)
char d[N_COMMON_DIGEST_ALGORITHMS][DIGEST256_LEN]
uint16_t marked_for_close
or_handshake_state_t * handshake_state
char body[CELL_MAX_NETWORK_SIZE]
uint8_t payload[FLEXIBLE_ARRAY_MEMBER]
void tor_gettimeofday(struct timeval *timeval)
void or_handshake_certs_check_both(int severity, or_handshake_certs_t *certs, tor_tls_t *tls, time_t now, const ed25519_public_key_t **ed_id_out, const common_digests_t **rsa_id_out)
tor_cert_t * tor_cert_parse(const uint8_t *encoded, const size_t len)
long tv_udiff(const struct timeval *start, const struct timeval *end)
#define FALLTHROUGH_UNLESS_ALL_BUGS_ARE_FATAL
int fast_mem_is_zero(const char *mem, size_t len)
int tor_digest_is_zero(const char *digest)
Variable-length cell structure.
crypto_pk_t * tor_tls_cert_get_key(tor_x509_cert_t *cert)
tor_x509_cert_t * tor_x509_cert_decode(const uint8_t *certificate, size_t certificate_len)