Try to extract as many descriptors as possible from truncated http responses. (when...
[tor.git] / src / or / connection_or.c
blob466d97d6800dbf48374f3ed802c30019a0f00e86
1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
5 /* $Id$ */
6 const char connection_or_c_id[] = "$Id$";
8 /**
9 * \file connection_or.c
10 * \brief Functions to handle OR connections, TLS handshaking, and
11 * cells on the network.
12 **/
14 #include "or.h"
16 /** How much clock skew do we tolerate when checking certificates for
17 * known routers? (sec) */
18 #define TIGHT_CERT_ALLOW_SKEW (90*60)
20 static int connection_tls_finish_handshake(connection_t *conn);
21 static int connection_or_process_cells_from_inbuf(connection_t *conn);
23 /**************************************************************/
25 /** Pack the cell_t host-order structure <b>src</b> into network-order
26 * in the buffer <b>dest</b>. See tor-spec.txt for details about the
27 * wire format.
29 static void
30 cell_pack(char *dest, const cell_t *src)
32 *(uint16_t*)dest = htons(src->circ_id);
33 *(uint8_t*)(dest+2) = src->command;
34 memcpy(dest+3, src->payload, CELL_PAYLOAD_SIZE);
37 /** Unpack the network-order buffer <b>src</b> into a host-order
38 * cell_t structure <b>dest</b>.
40 static void
41 cell_unpack(cell_t *dest, const char *src)
43 dest->circ_id = ntohs(*(uint16_t*)(src));
44 dest->command = *(uint8_t*)(src+2);
45 memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
48 int
49 connection_or_reached_eof(connection_t *conn)
51 log_fn(LOG_INFO,"OR connection reached EOF. Closing.");
52 connection_mark_for_close(conn);
53 return 0;
56 /** Read conn's inbuf. If the http response from the proxy is all
57 * here, make sure it's good news, and begin the tls handshake. If
58 * it's bad news, close the connection and return -1. Else return 0
59 * and hope for better luck next time.
61 static int
62 connection_or_read_proxy_response(connection_t *conn)
64 char *headers;
65 char *reason=NULL;
66 int status_code;
67 time_t date_header;
68 int compression;
70 switch (fetch_from_buf_http(conn->inbuf,
71 &headers, MAX_HEADERS_SIZE,
72 NULL, NULL, 10000, 0)) {
73 case -1: /* overflow */
74 log_fn(LOG_WARN,"Your https proxy sent back an oversized response. Closing.");
75 return -1;
76 case 0:
77 log_fn(LOG_INFO,"https proxy response not all here yet. Waiting.");
78 return 0;
79 /* case 1, fall through */
82 if (parse_http_response(headers, &status_code, &date_header,
83 &compression, &reason) < 0) {
84 log_fn(LOG_WARN,"Unparseable headers (connecting to '%s'). Closing.",
85 conn->address);
86 tor_free(headers);
87 return -1;
89 if (!reason) reason = tor_strdup("[no reason given]");
91 if (status_code == 200) {
92 log_fn(LOG_INFO,
93 "HTTPS connect to '%s' successful! (200 \"%s\") Starting TLS.",
94 conn->address, reason);
95 tor_free(reason);
96 if (connection_tls_start_handshake(conn, 0) < 0) {
97 /* TLS handshaking error of some kind. */
98 connection_mark_for_close(conn);
100 return -1;
102 return 0;
104 /* else, bad news on the status code */
105 log_fn(LOG_WARN,"The https proxy sent back an unexpected status code %d (\"%s\"). Closing.",
106 status_code, reason);
107 tor_free(reason);
108 connection_mark_for_close(conn);
109 return -1;
112 /** Handle any new bytes that have come in on connection <b>conn</b>.
113 * If conn is in 'open' state, hand it to
114 * connection_or_process_cells_from_inbuf()
115 * (else do nothing).
118 connection_or_process_inbuf(connection_t *conn)
120 tor_assert(conn);
121 tor_assert(conn->type == CONN_TYPE_OR);
123 switch (conn->state) {
124 case OR_CONN_STATE_PROXY_READING:
125 return connection_or_read_proxy_response(conn);
126 case OR_CONN_STATE_OPEN:
127 return connection_or_process_cells_from_inbuf(conn);
128 default:
129 return 0; /* don't do anything */
133 /** Connection <b>conn</b> has finished writing and has no bytes left on
134 * its outbuf.
136 * Otherwise it's in state "open": stop writing and return.
138 * If <b>conn</b> is broken, mark it for close and return -1, else
139 * return 0.
142 connection_or_finished_flushing(connection_t *conn)
144 tor_assert(conn);
145 tor_assert(conn->type == CONN_TYPE_OR);
147 assert_connection_ok(conn,0);
149 switch (conn->state) {
150 case OR_CONN_STATE_PROXY_FLUSHING:
151 log_fn(LOG_DEBUG,"finished sending CONNECT to proxy.");
152 conn->state = OR_CONN_STATE_PROXY_READING;
153 connection_stop_writing(conn);
154 break;
155 case OR_CONN_STATE_OPEN:
156 connection_stop_writing(conn);
157 break;
158 default:
159 log_fn(LOG_WARN,"BUG: called in unexpected state %d.", conn->state);
160 tor_fragile_assert();
161 return -1;
163 return 0;
166 /** Connected handler for OR connections: begin the TLS handshake.
169 connection_or_finished_connecting(connection_t *conn)
171 tor_assert(conn);
172 tor_assert(conn->type == CONN_TYPE_OR);
173 tor_assert(conn->state == OR_CONN_STATE_CONNECTING);
175 log_fn(LOG_INFO,"OR connect() to router at %s:%u finished.",
176 conn->address,conn->port);
178 if (get_options()->HttpsProxy) {
179 char buf[1024];
180 char addrbuf[INET_NTOA_BUF_LEN];
181 struct in_addr in;
182 char *base64_authenticator=NULL;
183 const char *authenticator = get_options()->HttpsProxyAuthenticator;
185 in.s_addr = htonl(conn->addr);
186 tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
188 if (authenticator) {
189 base64_authenticator = alloc_http_authenticator(authenticator);
190 if (!base64_authenticator)
191 log_fn(LOG_WARN, "Encoding https authenticator failed");
193 if (base64_authenticator) {
194 tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
195 "Proxy-Authorization: Basic %s\r\n\r\n", addrbuf,
196 conn->port, base64_authenticator);
197 tor_free(base64_authenticator);
198 } else {
199 tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
200 addrbuf, conn->port);
202 connection_write_to_buf(buf, strlen(buf), conn);
203 conn->state = OR_CONN_STATE_PROXY_FLUSHING;
204 return 0;
207 if (connection_tls_start_handshake(conn, 0) < 0) {
208 /* TLS handshaking error of some kind. */
209 connection_mark_for_close(conn);
210 return -1;
212 return 0;
215 /** Initialize <b>conn</b> to include all the relevant data from <b>router</b>.
216 * This function is called either from connection_or_connect(), if
217 * we initiated the connect, or from connection_tls_finish_handshake()
218 * if the other side initiated it.
220 static void
221 connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router)
223 or_options_t *options = get_options();
225 conn->addr = router->addr;
226 conn->port = router->or_port;
227 conn->receiver_bucket = conn->bandwidth = (int)options->BandwidthBurst;
228 conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
229 crypto_pk_get_digest(conn->identity_pkey, conn->identity_digest);
230 conn->nickname = tor_strdup(router->nickname);
231 tor_free(conn->address);
232 conn->address = tor_strdup(router->address);
235 /** If we don't necessarily know the router we're connecting to, but we
236 * have an addr/port/id_digest, then fill in as much as we can. Start
237 * by checking to see if this describes a router we know. */
238 static void
239 connection_or_init_conn_from_address(connection_t *conn,
240 uint32_t addr, uint16_t port,
241 const char *id_digest)
243 const char *n;
244 or_options_t *options = get_options();
245 routerinfo_t *r = router_get_by_digest(id_digest);
246 if (r) {
247 connection_or_init_conn_from_router(conn,r);
248 return;
250 conn->addr = addr;
251 conn->port = port;
252 /* This next part isn't really right, but it's good enough for now. */
253 conn->receiver_bucket = conn->bandwidth = (int)options->BandwidthBurst;
254 memcpy(conn->identity_digest, id_digest, DIGEST_LEN);
255 /* If we're an authoritative directory server, we may know a
256 * nickname for this router. */
257 n = dirserv_get_nickname_by_digest(id_digest);
258 if (n) {
259 conn->nickname = tor_strdup(n);
260 } else {
261 conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
262 conn->nickname[0] = '$';
263 base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
264 conn->identity_digest, DIGEST_LEN);
266 tor_free(conn->address);
267 conn->address = tor_dup_addr(addr);
270 /** "update an OR connection nickname on the fly"
271 * Actually, nobody calls this. Should we remove it? */
272 void
273 connection_or_update_nickname(connection_t *conn)
275 routerinfo_t *r;
276 const char *n;
278 tor_assert(conn);
279 tor_assert(conn->type == CONN_TYPE_OR);
280 n = dirserv_get_nickname_by_digest(conn->identity_digest);
281 if (n) {
282 if (!conn->nickname || strcmp(conn->nickname, n)) {
283 tor_free(conn->nickname);
284 conn->nickname = tor_strdup(n);
286 return;
288 r = router_get_by_digest(conn->identity_digest);
289 if (r && r->is_verified) {
290 if (!conn->nickname || strcmp(conn->nickname, r->nickname)) {
291 tor_free(conn->nickname);
292 conn->nickname = tor_strdup(r->nickname);
294 return;
296 if (conn->nickname[0] != '$') {
297 tor_free(conn->nickname);
298 conn->nickname = tor_malloc(HEX_DIGEST_LEN+1);
299 base16_encode(conn->nickname, HEX_DIGEST_LEN+1,
300 conn->identity_digest, DIGEST_LEN);
304 /** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
305 * handshake with an OR with identity digest <b>id_digest</b>.
307 * If <b>id_digest</b> is me, do nothing. If we're already connected to it,
308 * return that connection. If the connect() is in progress, set the
309 * new conn's state to 'connecting' and return it. If connect() succeeds,
310 * call connection_tls_start_handshake() on it.
312 * This function is called from router_retry_connections(), for
313 * ORs connecting to ORs, and circuit_establish_circuit(), for
314 * OPs connecting to ORs.
316 * Return the launched conn, or NULL if it failed.
318 connection_t *
319 connection_or_connect(uint32_t addr, uint16_t port, const char *id_digest)
321 connection_t *conn;
322 routerinfo_t *me;
323 or_options_t *options = get_options();
325 tor_assert(id_digest);
327 if (server_mode(options) && (me=router_get_my_routerinfo()) &&
328 !memcmp(me->identity_digest, id_digest,DIGEST_LEN)) {
329 log_fn(LOG_INFO,"Client asked me to connect to myself. Refusing.");
330 return NULL;
333 conn = connection_new(CONN_TYPE_OR);
335 /* set up conn so it's got all the data we need to remember */
336 connection_or_init_conn_from_address(conn, addr, port, id_digest);
337 conn->state = OR_CONN_STATE_CONNECTING;
338 control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED);
340 if (options->HttpsProxy) {
341 /* we shouldn't connect directly. use the https proxy instead. */
342 addr = options->HttpsProxyAddr;
343 port = options->HttpsProxyPort;
346 switch (connection_connect(conn, conn->address, addr, port)) {
347 case -1:
348 if (!options->HttpsProxy)
349 router_mark_as_down(conn->identity_digest);
350 helper_node_set_status(conn->identity_digest, 0);
351 control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
352 connection_free(conn);
353 return NULL;
354 case 0:
355 connection_watch_events(conn, EV_READ | EV_WRITE);
356 /* writable indicates finish, readable indicates broken link,
357 error indicates broken link on windows */
358 return conn;
359 /* case 1: fall through */
362 if (connection_or_finished_connecting(conn) < 0) {
363 /* already marked for close */
364 return NULL;
366 return conn;
369 /** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
370 * we initiated the connection, else it's 1.
372 * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and pass
373 * <b>conn</b> to connection_tls_continue_handshake().
375 * Return -1 if <b>conn</b> is broken, else return 0.
378 connection_tls_start_handshake(connection_t *conn, int receiving)
380 conn->state = OR_CONN_STATE_HANDSHAKING;
381 conn->tls = tor_tls_new(conn->s, receiving, 0);
382 if (!conn->tls) {
383 log_fn(LOG_WARN,"tor_tls_new failed. Closing.");
384 return -1;
386 connection_start_reading(conn);
387 log_fn(LOG_DEBUG,"starting the handshake");
388 if (connection_tls_continue_handshake(conn) < 0) {
389 return -1;
391 return 0;
394 /** Move forward with the tls handshake. If it finishes, hand
395 * <b>conn</b> to connection_tls_finish_handshake().
397 * Return -1 if <b>conn</b> is broken, else return 0.
400 connection_tls_continue_handshake(connection_t *conn)
402 check_no_tls_errors();
403 switch (tor_tls_handshake(conn->tls)) {
404 case TOR_TLS_ERROR:
405 case TOR_TLS_CLOSE:
406 log_fn(LOG_INFO,"tls error. breaking.");
407 return -1;
408 case TOR_TLS_DONE:
409 return connection_tls_finish_handshake(conn);
410 case TOR_TLS_WANTWRITE:
411 connection_start_writing(conn);
412 log_fn(LOG_DEBUG,"wanted write");
413 return 0;
414 case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
415 log_fn(LOG_DEBUG,"wanted read");
416 return 0;
418 return 0;
421 static char ZERO_DIGEST[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
423 /** Return 1 if we initiated this connection, or 0 if it started
424 * out as an incoming connection.
426 * This is implemented for now by checking to see if
427 * conn-\>identity_digest is set or not. Perhaps we should add a flag
428 * one day so we're clearer.
431 connection_or_nonopen_was_started_here(connection_t *conn)
433 tor_assert(sizeof(ZERO_DIGEST) == DIGEST_LEN);
434 tor_assert(conn->type == CONN_TYPE_OR);
436 if (!memcmp(ZERO_DIGEST, conn->identity_digest, DIGEST_LEN))
437 return 0;
438 else
439 return 1;
442 /** Conn just completed its handshake. Return 0 if all is well, and
443 * return -1 if he is lying, broken, or otherwise something is wrong.
445 * Make sure he sent a correctly formed certificate. If it has a
446 * recognized (approved) nickname, make sure his identity key matches
447 * it. If I initiated the connection, make sure it's the right guy.
449 * If we return 0, write a hash of the identity key into digest_rcvd,
450 * which must have DIGEST_LEN space in it. (If we return -1 this
451 * buffer is undefined.)
453 * As side effects,
454 * 1) Set conn->circ_id_type according to tor-spec.txt
455 * 2) If we're an authdirserver and we initiated the connection: drop all
456 * descriptors that claim to be on that IP/port but that aren't
457 * this guy; and note that this guy is reachable.
459 static int
460 connection_or_check_valid_handshake(connection_t *conn, char *digest_rcvd)
462 routerinfo_t *router;
463 crypto_pk_env_t *identity_rcvd=NULL;
464 char nickname[MAX_NICKNAME_LEN+1];
465 or_options_t *options = get_options();
466 int severity = (authdir_mode(options) || !server_mode(options))
467 ? LOG_WARN : LOG_INFO;
469 check_no_tls_errors();
470 if (! tor_tls_peer_has_cert(conn->tls)) {
471 log_fn(LOG_INFO,"Peer didn't send a cert! Closing.");
472 return -1;
474 check_no_tls_errors();
475 if (tor_tls_get_peer_cert_nickname(conn->tls, nickname, sizeof(nickname))) {
476 log_fn(LOG_WARN,"Other side (%s:%d) has a cert without a valid nickname. Closing.",
477 conn->address, conn->port);
478 return -1;
480 check_no_tls_errors();
481 log_fn(LOG_DEBUG, "Other side (%s:%d) claims to be router '%s'",
482 conn->address, conn->port, nickname);
484 if (tor_tls_verify(conn->tls, &identity_rcvd) < 0) {
485 log_fn(LOG_WARN,"Other side, which claims to be router '%s' (%s:%d), has a cert but it's invalid. Closing.",
486 nickname, conn->address, conn->port);
487 return -1;
489 check_no_tls_errors();
490 log_fn(LOG_DEBUG,"The router's cert is valid.");
491 crypto_pk_get_digest(identity_rcvd, digest_rcvd);
493 if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
494 conn->circ_id_type = CIRC_ID_TYPE_LOWER;
495 } else {
496 conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
498 crypto_free_pk_env(identity_rcvd);
500 router = router_get_by_nickname(nickname, 0);
501 if (router && /* we know this nickname */
502 router->is_named && /* make sure it's the right guy */
503 memcmp(digest_rcvd, router->identity_digest, DIGEST_LEN) != 0) {
504 log_fn(severity,
505 "Identity key not as expected for router claiming to be '%s' (%s:%d)",
506 nickname, conn->address, conn->port);
507 return -1;
510 if (connection_or_nonopen_was_started_here(conn)) {
511 int as_advertised = 1;
512 if (memcmp(digest_rcvd, conn->identity_digest, DIGEST_LEN)) {
513 /* I was aiming for a particular digest. I didn't get it! */
514 char seen[HEX_DIGEST_LEN+1];
515 char expected[HEX_DIGEST_LEN+1];
516 base16_encode(seen, sizeof(seen), digest_rcvd, DIGEST_LEN);
517 base16_encode(expected, sizeof(expected), conn->identity_digest, DIGEST_LEN);
518 log_fn(severity,
519 "Identity key not as expected for router at %s:%d: wanted %s but got %s",
520 conn->address, conn->port, expected, seen);
521 helper_node_set_status(conn->identity_digest, 0);
522 control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
523 as_advertised = 0;
525 if (authdir_mode(options)) {
526 /* We initiated this connection to address:port. Drop all routers
527 * with the same address:port and a different key or nickname.
529 dirserv_orconn_tls_done(conn->address, conn->port,
530 digest_rcvd, nickname, as_advertised);
532 if (!as_advertised)
533 return -1;
535 return 0;
538 /** The tls handshake is finished.
540 * Make sure we are happy with the person we just handshaked with.
542 * If he initiated the connection, make sure he's not already connected,
543 * then initialize conn from the information in router.
545 * If I'm not a server, set bandwidth to the default OP bandwidth.
547 * If all is successful, call circuit_n_conn_done() to handle events
548 * that have been pending on the tls handshake completion. Also set the
549 * directory to be dirty (only matters if I'm an authdirserver).
551 static int
552 connection_tls_finish_handshake(connection_t *conn)
554 char digest_rcvd[DIGEST_LEN];
556 log_fn(LOG_DEBUG,"tls handshake done. verifying.");
557 if (connection_or_check_valid_handshake(conn, digest_rcvd) < 0)
558 return -1;
560 if (!connection_or_nonopen_was_started_here(conn)) {
561 connection_t *c;
562 if ((c=connection_get_by_identity_digest(digest_rcvd, CONN_TYPE_OR))) {
563 log_fn(LOG_INFO,"Router '%s' is already connected on fd %d. Dropping fd %d.",
564 c->nickname, c->s, conn->s);
565 return -1;
567 connection_or_init_conn_from_address(conn,conn->addr,conn->port,digest_rcvd);
570 if (!server_mode(get_options())) { /* If I'm an OP... */
571 conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
574 directory_set_dirty();
575 conn->state = OR_CONN_STATE_OPEN;
576 connection_watch_events(conn, EV_READ);
577 circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
578 rep_hist_note_connect_succeeded(conn->identity_digest, time(NULL));
579 helper_node_set_status(conn->identity_digest, 1);
580 control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED);
581 return 0;
584 /** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s
585 * outbuf.
587 * (Commented out) If it's an OR conn, and an entire TLS record is
588 * ready, then try to flush the record now.
590 void
591 connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn)
593 char networkcell[CELL_NETWORK_SIZE];
594 char *n = networkcell;
596 tor_assert(cell);
597 tor_assert(conn);
598 tor_assert(connection_speaks_cells(conn));
600 cell_pack(n, cell);
602 connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
604 #define MIN_TLS_FLUSHLEN 15872
605 /* openssl tls record size is 16383, this is close. The goal here is to
606 * push data out as soon as we know there's enough for a tls record, so
607 * during periods of high load we won't read the entire megabyte from
608 * input before pushing any data out. It also has the feature of not
609 * growing huge outbufs unless something is slow. */
610 if (conn->outbuf_flushlen-CELL_NETWORK_SIZE < MIN_TLS_FLUSHLEN &&
611 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
612 int extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
613 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
614 if (connection_handle_write(conn) < 0) {
615 if (!conn->marked_for_close) {
616 /* this connection is broken. remove it. */
617 log_fn(LOG_WARN,"Bug: unhandled error on write for OR conn (fd %d); removing",
618 conn->s);
619 tor_fragile_assert();
620 conn->has_sent_end = 1; /* otherwise we cry wolf about duplicate close */
621 /* XXX do we need a close-immediate here, so we don't try to flush? */
622 connection_mark_for_close(conn);
624 return;
626 if (extra) {
627 conn->outbuf_flushlen += extra;
628 connection_start_writing(conn);
633 /** Process cells from <b>conn</b>'s inbuf.
635 * Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
636 * and hand it to command_process_cell().
638 * Always return 0.
640 static int
641 connection_or_process_cells_from_inbuf(connection_t *conn)
643 char buf[CELL_NETWORK_SIZE];
644 cell_t cell;
646 loop:
647 log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object).",
648 conn->s,(int)buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls));
649 if (buf_datalen(conn->inbuf) < CELL_NETWORK_SIZE) /* entire response available? */
650 return 0; /* not yet */
652 connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, conn);
654 /* retrieve cell info from buf (create the host-order struct from the
655 * network-order string) */
656 cell_unpack(&cell, buf);
658 command_process_cell(&cell, conn);
660 goto loop; /* process the remainder of the buffer */