connection_or.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /* $Id$ */
  7. const char connection_or_c_id[] =
  8. "$Id$";
  9. /**
  10. * \file connection_or.c
  11. * \brief Functions to handle OR connections, TLS handshaking, and
  12. * cells on the network.
  13. **/
  14. #include "or.h"
  15. static int connection_tls_finish_handshake(or_connection_t *conn);
  16. static int connection_or_process_cells_from_inbuf(or_connection_t *conn);
  17. static int connection_or_send_versions(or_connection_t *conn);
  18. static int connection_init_or_handshake_state(or_connection_t *conn,
  19. int started_here);
  20. static int connection_or_check_valid_tls_handshake(or_connection_t *conn,
  21. int started_here,
  22. char *digest_rcvd_out);
  23. /**************************************************************/
  24. /** Map from identity digest of connected OR or desired OR to a connection_t
  25. * with that identity digest. If there is more than one such connection_t,
  26. * they form a linked list, with next_with_same_id as the next pointer. */
  27. static digestmap_t *orconn_identity_map = NULL;
  28. /** If conn is listed in orconn_identity_map, remove it, and clear
  29. * conn->identity_digest. Otherwise do nothing. */
  30. void
  31. connection_or_remove_from_identity_map(or_connection_t *conn)
  32. {
  33. or_connection_t *tmp;
  34. tor_assert(conn);
  35. if (!orconn_identity_map)
  36. return;
  37. tmp = digestmap_get(orconn_identity_map, conn->identity_digest);
  38. if (!tmp) {
  39. if (!tor_digest_is_zero(conn->identity_digest)) {
  40. log_warn(LD_BUG, "Didn't find connection '%s' on identity map when "
  41. "trying to remove it.",
  42. conn->nickname ? conn->nickname : "NULL");
  43. }
  44. return;
  45. }
  46. if (conn == tmp) {
  47. if (conn->next_with_same_id)
  48. digestmap_set(orconn_identity_map, conn->identity_digest,
  49. conn->next_with_same_id);
  50. else
  51. digestmap_remove(orconn_identity_map, conn->identity_digest);
  52. } else {
  53. while (tmp->next_with_same_id) {
  54. if (tmp->next_with_same_id == conn) {
  55. tmp->next_with_same_id = conn->next_with_same_id;
  56. break;
  57. }
  58. tmp = tmp->next_with_same_id;
  59. }
  60. }
  61. memset(conn->identity_digest, 0, DIGEST_LEN);
  62. conn->next_with_same_id = NULL;
  63. }
  64. /** Remove all entries from the identity-to-orconn map, and clear
  65. * all identities in OR conns.*/
  66. void
  67. connection_or_clear_identity_map(void)
  68. {
  69. smartlist_t *conns = get_connection_array();
  70. SMARTLIST_FOREACH(conns, connection_t *, conn,
  71. {
  72. if (conn->type == CONN_TYPE_OR) {
  73. or_connection_t *or_conn = TO_OR_CONN(conn);
  74. memset(or_conn->identity_digest, 0, DIGEST_LEN);
  75. or_conn->next_with_same_id = NULL;
  76. }
  77. });
  78. if (orconn_identity_map) {
  79. digestmap_free(orconn_identity_map, NULL);
  80. orconn_identity_map = NULL;
  81. }
  82. }
  83. /** Change conn->identity_digest to digest, and add conn into
  84. * orconn_digest_map. */
  85. static void
  86. connection_or_set_identity_digest(or_connection_t *conn, const char *digest)
  87. {
  88. or_connection_t *tmp;
  89. tor_assert(conn);
  90. tor_assert(digest);
  91. if (!orconn_identity_map)
  92. orconn_identity_map = digestmap_new();
  93. if (!memcmp(conn->identity_digest, digest, DIGEST_LEN))
  94. return;
  95. /* If the identity was set previously, remove the old mapping. */
  96. if (! tor_digest_is_zero(conn->identity_digest))
  97. connection_or_remove_from_identity_map(conn);
  98. memcpy(conn->identity_digest, digest, DIGEST_LEN);
  99. /* If we're setting the ID to zero, don't add a mapping. */
  100. if (tor_digest_is_zero(digest))
  101. return;
  102. tmp = digestmap_set(orconn_identity_map, digest, conn);
  103. conn->next_with_same_id = tmp;
  104. #if 1
  105. /* Testing code to check for bugs in representation. */
  106. for (; tmp; tmp = tmp->next_with_same_id) {
  107. tor_assert(!memcmp(tmp->identity_digest, digest, DIGEST_LEN));
  108. tor_assert(tmp != conn);
  109. }
  110. #endif
  111. }
  112. /** Pack the cell_t host-order structure <b>src</b> into network-order
  113. * in the buffer <b>dest</b>. See tor-spec.txt for details about the
  114. * wire format.
  115. *
  116. * Note that this function doesn't touch <b>dst</b>-\>next: the caller
  117. * should set it or clear it as appropriate.
  118. */
  119. void
  120. cell_pack(packed_cell_t *dst, const cell_t *src)
  121. {
  122. char *dest = dst->body;
  123. *(uint16_t*)dest = htons(src->circ_id);
  124. *(uint8_t*)(dest+2) = src->command;
  125. memcpy(dest+3, src->payload, CELL_PAYLOAD_SIZE);
  126. }
  127. /** Unpack the network-order buffer <b>src</b> into a host-order
  128. * cell_t structure <b>dest</b>.
  129. */
  130. static void
  131. cell_unpack(cell_t *dest, const char *src)
  132. {
  133. dest->circ_id = ntohs(*(uint16_t*)(src));
  134. dest->command = *(uint8_t*)(src+2);
  135. memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
  136. }
  137. /** DOCDOC */
  138. void
  139. var_cell_pack_header(const var_cell_t *cell, char *hdr_out)
  140. {
  141. *(uint16_t*)(hdr_out) = htons(cell->circ_id);
  142. *(uint8_t*)(hdr_out+2) = cell->command;
  143. set_uint16(hdr_out+3, htons(cell->payload_len));
  144. }
  145. /* DOCDOC*/
  146. var_cell_t *
  147. var_cell_new(uint16_t payload_len)
  148. {
  149. var_cell_t *cell = tor_malloc(sizeof(var_cell_t)+payload_len-1);
  150. cell->payload_len = payload_len;
  151. cell->command = 0;
  152. cell->circ_id = 0;
  153. return cell;
  154. }
  155. /** DOCDOC */
  156. void
  157. var_cell_free(var_cell_t *cell)
  158. {
  159. tor_free(cell);
  160. }
  161. int
  162. connection_or_reached_eof(or_connection_t *conn)
  163. {
  164. log_info(LD_OR,"OR connection reached EOF. Closing.");
  165. connection_mark_for_close(TO_CONN(conn));
  166. return 0;
  167. }
  168. /** Read conn's inbuf. If the http response from the proxy is all
  169. * here, make sure it's good news, and begin the tls handshake. If
  170. * it's bad news, close the connection and return -1. Else return 0
  171. * and hope for better luck next time.
  172. */
  173. static int
  174. connection_or_read_proxy_response(or_connection_t *or_conn)
  175. {
  176. char *headers;
  177. char *reason=NULL;
  178. int status_code;
  179. time_t date_header;
  180. connection_t *conn = TO_CONN(or_conn);
  181. switch (fetch_from_buf_http(conn->inbuf,
  182. &headers, MAX_HEADERS_SIZE,
  183. NULL, NULL, 10000, 0)) {
  184. case -1: /* overflow */
  185. log_warn(LD_PROTOCOL,
  186. "Your https proxy sent back an oversized response. Closing.");
  187. return -1;
  188. case 0:
  189. log_info(LD_OR,"https proxy response not all here yet. Waiting.");
  190. return 0;
  191. /* case 1, fall through */
  192. }
  193. if (parse_http_response(headers, &status_code, &date_header,
  194. NULL, &reason) < 0) {
  195. log_warn(LD_OR,
  196. "Unparseable headers from proxy (connecting to '%s'). Closing.",
  197. conn->address);
  198. tor_free(headers);
  199. return -1;
  200. }
  201. if (!reason) reason = tor_strdup("[no reason given]");
  202. if (status_code == 200) {
  203. log_info(LD_OR,
  204. "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
  205. conn->address, escaped(reason));
  206. tor_free(reason);
  207. if (connection_tls_start_handshake(or_conn, 0) < 0) {
  208. /* TLS handshaking error of some kind. */
  209. connection_mark_for_close(conn);
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. /* else, bad news on the status code */
  215. log_warn(LD_OR,
  216. "The https proxy sent back an unexpected status code %d (%s). "
  217. "Closing.",
  218. status_code, escaped(reason));
  219. tor_free(reason);
  220. connection_mark_for_close(conn);
  221. return -1;
  222. }
  223. /** Handle any new bytes that have come in on connection <b>conn</b>.
  224. * If conn is in 'open' state, hand it to
  225. * connection_or_process_cells_from_inbuf()
  226. * (else do nothing).
  227. */
  228. int
  229. connection_or_process_inbuf(or_connection_t *conn)
  230. {
  231. tor_assert(conn);
  232. switch (conn->_base.state) {
  233. case OR_CONN_STATE_PROXY_READING:
  234. return connection_or_read_proxy_response(conn);
  235. case OR_CONN_STATE_OPEN:
  236. return connection_or_process_cells_from_inbuf(conn);
  237. default:
  238. return 0; /* don't do anything */
  239. }
  240. }
  241. /** When adding cells to an OR connection's outbuf, keep adding until the
  242. * outbuf is at least this long, or we run out of cells. */
  243. #define OR_CONN_HIGHWATER (32*1024)
  244. /** Add cells to an OR connection's outbuf whenever the outbuf's data length
  245. * drops below this size. */
  246. #define OR_CONN_LOWWATER (16*1024)
  247. /** Called whenever we have flushed some data on an or_conn: add more data
  248. * from active circuits. */
  249. int
  250. connection_or_flushed_some(or_connection_t *conn)
  251. {
  252. size_t datalen = buf_datalen(conn->_base.outbuf);
  253. /* If we're under the low water mark, add cells until we're just over the
  254. * high water mark. */
  255. if (datalen < OR_CONN_LOWWATER) {
  256. int n = (OR_CONN_HIGHWATER - datalen + CELL_NETWORK_SIZE-1)
  257. / CELL_NETWORK_SIZE;
  258. while (conn->active_circuits && n > 0) {
  259. int flushed = connection_or_flush_from_first_active_circuit(conn, 1);
  260. n -= flushed;
  261. }
  262. }
  263. return 0;
  264. }
  265. /** Connection <b>conn</b> has finished writing and has no bytes left on
  266. * its outbuf.
  267. *
  268. * Otherwise it's in state "open": stop writing and return.
  269. *
  270. * If <b>conn</b> is broken, mark it for close and return -1, else
  271. * return 0.
  272. */
  273. int
  274. connection_or_finished_flushing(or_connection_t *conn)
  275. {
  276. tor_assert(conn);
  277. assert_connection_ok(TO_CONN(conn),0);
  278. switch (conn->_base.state) {
  279. case OR_CONN_STATE_PROXY_FLUSHING:
  280. log_debug(LD_OR,"finished sending CONNECT to proxy.");
  281. conn->_base.state = OR_CONN_STATE_PROXY_READING;
  282. connection_stop_writing(TO_CONN(conn));
  283. break;
  284. case OR_CONN_STATE_OPEN:
  285. connection_stop_writing(TO_CONN(conn));
  286. break;
  287. default:
  288. log_err(LD_BUG,"Called in unexpected state %d.", conn->_base.state);
  289. tor_fragile_assert();
  290. return -1;
  291. }
  292. return 0;
  293. }
  294. /** Connected handler for OR connections: begin the TLS handshake.
  295. */
  296. int
  297. connection_or_finished_connecting(or_connection_t *or_conn)
  298. {
  299. connection_t *conn;
  300. tor_assert(or_conn);
  301. conn = TO_CONN(or_conn);
  302. tor_assert(conn->state == OR_CONN_STATE_CONNECTING);
  303. log_debug(LD_OR,"OR connect() to router at %s:%u finished.",
  304. conn->address,conn->port);
  305. if (get_options()->HttpsProxy) {
  306. char buf[1024];
  307. char addrbuf[INET_NTOA_BUF_LEN];
  308. struct in_addr in;
  309. char *base64_authenticator=NULL;
  310. const char *authenticator = get_options()->HttpsProxyAuthenticator;
  311. in.s_addr = htonl(conn->addr);
  312. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  313. if (authenticator) {
  314. base64_authenticator = alloc_http_authenticator(authenticator);
  315. if (!base64_authenticator)
  316. log_warn(LD_OR, "Encoding https authenticator failed");
  317. }
  318. if (base64_authenticator) {
  319. tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
  320. "Proxy-Authorization: Basic %s\r\n\r\n", addrbuf,
  321. conn->port, base64_authenticator);
  322. tor_free(base64_authenticator);
  323. } else {
  324. tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
  325. addrbuf, conn->port);
  326. }
  327. connection_write_to_buf(buf, strlen(buf), conn);
  328. conn->state = OR_CONN_STATE_PROXY_FLUSHING;
  329. return 0;
  330. }
  331. if (connection_tls_start_handshake(or_conn, 0) < 0) {
  332. /* TLS handshaking error of some kind. */
  333. connection_mark_for_close(conn);
  334. return -1;
  335. }
  336. return 0;
  337. }
  338. /** If we don't necessarily know the router we're connecting to, but we
  339. * have an addr/port/id_digest, then fill in as much as we can. Start
  340. * by checking to see if this describes a router we know. */
  341. static void
  342. connection_or_init_conn_from_address(or_connection_t *conn,
  343. uint32_t addr, uint16_t port,
  344. const char *id_digest,
  345. int started_here)
  346. {
  347. or_options_t *options = get_options();
  348. routerinfo_t *r = router_get_by_digest(id_digest);
  349. conn->bandwidthrate = (int)options->BandwidthRate;
  350. conn->read_bucket = conn->bandwidthburst = (int)options->BandwidthBurst;
  351. connection_or_set_identity_digest(conn, id_digest);
  352. conn->_base.addr = addr;
  353. conn->_base.port = port;
  354. conn->real_addr = addr;
  355. if (r) {
  356. if (conn->_base.addr == r->addr)
  357. conn->is_canonical = 1;
  358. if (!started_here) {
  359. /* Override the addr/port, so our log messages will make sense.
  360. * This is dangerous, since if we ever try looking up a conn by
  361. * its actual addr/port, we won't remember. Careful! */
  362. /* XXXX020 this is stupid, and it's the reason we need real_addr to
  363. * track is_canonical properly. */
  364. conn->_base.addr = r->addr;
  365. conn->_base.port = r->or_port;
  366. }
  367. conn->nickname = tor_strdup(r->nickname);
  368. tor_free(conn->_base.address);
  369. conn->_base.address = tor_strdup(r->address);
  370. } else {
  371. const char *n;
  372. /* If we're an authoritative directory server, we may know a
  373. * nickname for this router. */
  374. n = dirserv_get_nickname_by_digest(id_digest);
  375. if (n) {
  376. conn->nickname = tor_strdup(n);
  377. } else {
  378. conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
  379. conn->nickname[0] = '$';
  380. base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
  381. conn->identity_digest, DIGEST_LEN);
  382. }
  383. tor_free(conn->_base.address);
  384. conn->_base.address = tor_dup_addr(addr);
  385. }
  386. }
  387. /** Return the best connection of type OR with the
  388. * digest <b>digest</b> that we have, or NULL if we have none.
  389. *
  390. * 1) Don't return it if it's marked for close.
  391. * 2) If there are any open conns, ignore non-open conns.
  392. * 3) If there are any non-obsolete conns, ignore obsolete conns.
  393. * 4) Then if there are any non-empty conns, ignore empty conns.
  394. * 5) Of the remaining conns, prefer newer conns.
  395. */
  396. or_connection_t *
  397. connection_or_get_by_identity_digest(const char *digest)
  398. {
  399. int newer;
  400. or_connection_t *conn, *best=NULL;
  401. if (!orconn_identity_map)
  402. return NULL;
  403. conn = digestmap_get(orconn_identity_map, digest);
  404. for (; conn; conn = conn->next_with_same_id) {
  405. tor_assert(conn->_base.magic == OR_CONNECTION_MAGIC);
  406. tor_assert(conn->_base.type == CONN_TYPE_OR);
  407. tor_assert(!memcmp(conn->identity_digest, digest, DIGEST_LEN));
  408. if (conn->_base.marked_for_close)
  409. continue;
  410. if (!best) {
  411. best = conn; /* whatever it is, it's better than nothing. */
  412. continue;
  413. }
  414. if (best->_base.state == OR_CONN_STATE_OPEN &&
  415. conn->_base.state != OR_CONN_STATE_OPEN)
  416. continue; /* avoid non-open conns if we can */
  417. newer = best->_base.timestamp_created < conn->_base.timestamp_created;
  418. if (best->is_canonical && !conn->is_canonical)
  419. continue; /* A canonical connection is best. */
  420. if (!best->_base.or_is_obsolete && conn->_base.or_is_obsolete)
  421. continue; /* We never prefer obsolete over non-obsolete connections. */
  422. if (
  423. /* We prefer non-obsolete connections: */
  424. (best->_base.or_is_obsolete && !conn->_base.or_is_obsolete) ||
  425. /* If both have circuits we prefer the newer: */
  426. (best->n_circuits && conn->n_circuits && newer) ||
  427. /* If neither has circuits we prefer the newer: */
  428. (!best->n_circuits && !conn->n_circuits && newer) ||
  429. /* We prefer connections with circuits: */
  430. (!best->n_circuits && conn->n_circuits)) {
  431. best = conn;
  432. };
  433. }
  434. return best;
  435. }
  436. /** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
  437. * handshake with an OR with identity digest <b>id_digest</b>.
  438. *
  439. * If <b>id_digest</b> is me, do nothing. If we're already connected to it,
  440. * return that connection. If the connect() is in progress, set the
  441. * new conn's state to 'connecting' and return it. If connect() succeeds,
  442. * call connection_tls_start_handshake() on it.
  443. *
  444. * This function is called from router_retry_connections(), for
  445. * ORs connecting to ORs, and circuit_establish_circuit(), for
  446. * OPs connecting to ORs.
  447. *
  448. * Return the launched conn, or NULL if it failed.
  449. */
  450. or_connection_t *
  451. connection_or_connect(uint32_t addr, uint16_t port, const char *id_digest)
  452. {
  453. or_connection_t *conn;
  454. or_options_t *options = get_options();
  455. tor_assert(id_digest);
  456. if (server_mode(options) && router_digest_is_me(id_digest)) {
  457. log_info(LD_PROTOCOL,"Client asked me to connect to myself. Refusing.");
  458. return NULL;
  459. }
  460. conn = TO_OR_CONN(connection_new(CONN_TYPE_OR, AF_INET));
  461. /* set up conn so it's got all the data we need to remember */
  462. connection_or_init_conn_from_address(conn, addr, port, id_digest, 1);
  463. conn->_base.state = OR_CONN_STATE_CONNECTING;
  464. control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED, 0);
  465. if (options->HttpsProxy) {
  466. /* we shouldn't connect directly. use the https proxy instead. */
  467. addr = options->HttpsProxyAddr;
  468. port = options->HttpsProxyPort;
  469. }
  470. switch (connection_connect(TO_CONN(conn), conn->_base.address, addr, port)) {
  471. case -1:
  472. /* If the connection failed immediately, and we're using
  473. * an https proxy, our https proxy is down. Don't blame the
  474. * Tor server. */
  475. if (!options->HttpsProxy) {
  476. entry_guard_register_connect_status(conn->identity_digest, 0,
  477. time(NULL));
  478. router_set_status(conn->identity_digest, 0);
  479. }
  480. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED,
  481. END_OR_CONN_REASON_TCP_REFUSED);
  482. connection_free(TO_CONN(conn));
  483. return NULL;
  484. case 0:
  485. connection_watch_events(TO_CONN(conn), EV_READ | EV_WRITE);
  486. /* writable indicates finish, readable indicates broken link,
  487. error indicates broken link on windows */
  488. return conn;
  489. /* case 1: fall through */
  490. }
  491. if (connection_or_finished_connecting(conn) < 0) {
  492. /* already marked for close */
  493. return NULL;
  494. }
  495. return conn;
  496. }
  497. /** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
  498. * we initiated the connection, else it's 1.
  499. *
  500. * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and
  501. * pass <b>conn</b> to connection_tls_continue_handshake().
  502. *
  503. * Return -1 if <b>conn</b> is broken, else return 0.
  504. */
  505. int
  506. connection_tls_start_handshake(or_connection_t *conn, int receiving)
  507. {
  508. conn->_base.state = OR_CONN_STATE_TLS_HANDSHAKING;
  509. conn->tls = tor_tls_new(conn->_base.s, receiving);
  510. if (!conn->tls) {
  511. log_warn(LD_BUG,"tor_tls_new failed. Closing.");
  512. return -1;
  513. }
  514. connection_start_reading(TO_CONN(conn));
  515. log_debug(LD_OR,"starting TLS handshake on fd %d", conn->_base.s);
  516. note_crypto_pk_op(receiving ? TLS_HANDSHAKE_S : TLS_HANDSHAKE_C);
  517. if (connection_tls_continue_handshake(conn) < 0) {
  518. return -1;
  519. }
  520. return 0;
  521. }
  522. /*DOCDOC*/
  523. static void
  524. connection_or_tls_renegotiated_cb(tor_tls_t *tls, void *_conn)
  525. {
  526. or_connection_t *conn = _conn;
  527. char id_digest[DIGEST_LEN];
  528. if (connection_or_check_valid_tls_handshake(conn,
  529. !tor_tls_is_server(tls),
  530. id_digest) < 0)
  531. return;
  532. connection_or_init_conn_from_address(conn, conn->_base.addr,
  533. conn->_base.port, id_digest, 0);
  534. }
  535. /** Move forward with the tls handshake. If it finishes, hand
  536. * <b>conn</b> to connection_tls_finish_handshake().
  537. *
  538. * Return -1 if <b>conn</b> is broken, else return 0.
  539. */
  540. int
  541. connection_tls_continue_handshake(or_connection_t *conn)
  542. {
  543. int result;
  544. check_no_tls_errors();
  545. again:
  546. if (conn->_base.state == OR_CONN_STATE_TLS_RENEGOTIATING)
  547. result = tor_tls_renegotiate(conn->tls);
  548. else
  549. result = tor_tls_handshake(conn->tls);
  550. switch (result) {
  551. CASE_TOR_TLS_ERROR_ANY:
  552. log_info(LD_OR,"tls error [%s]. breaking connection.",
  553. tor_tls_err_to_string(result));
  554. return -1;
  555. case TOR_TLS_DONE:
  556. if (! tor_tls_used_v1_handshake(conn->tls)) {
  557. if (!tor_tls_is_server(conn->tls)) {
  558. if (conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING) {
  559. conn->_base.state = OR_CONN_STATE_TLS_RENEGOTIATING;
  560. goto again;
  561. }
  562. } else {
  563. /* improved handshake, but not a client. */
  564. tor_tls_set_renegotiate_callback(conn->tls,
  565. connection_or_tls_renegotiated_cb,
  566. conn);
  567. }
  568. }
  569. return connection_tls_finish_handshake(conn);
  570. case TOR_TLS_WANTWRITE:
  571. connection_start_writing(TO_CONN(conn));
  572. log_debug(LD_OR,"wanted write");
  573. return 0;
  574. case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
  575. log_debug(LD_OR,"wanted read");
  576. return 0;
  577. case TOR_TLS_CLOSE:
  578. log_info(LD_OR,"tls closed. breaking connection.");
  579. return -1;
  580. }
  581. return 0;
  582. }
  583. /** Return 1 if we initiated this connection, or 0 if it started
  584. * out as an incoming connection.
  585. */
  586. int
  587. connection_or_nonopen_was_started_here(or_connection_t *conn)
  588. {
  589. tor_assert(conn->_base.type == CONN_TYPE_OR);
  590. if (!conn->tls)
  591. return 1; /* it's still in proxy states or something */
  592. if (conn->handshake_state)
  593. return conn->handshake_state->started_here;
  594. return !tor_tls_is_server(conn->tls);
  595. }
  596. /** <b>Conn</b> just completed its handshake. Return 0 if all is well, and
  597. * return -1 if he is lying, broken, or otherwise something is wrong.
  598. *
  599. * If we initiated this connection (<b>started_here</b> is true), make sure
  600. * the other side sent sent a correctly formed certificate. If I initiated the
  601. * connection, make sure it's the right guy.
  602. *
  603. * Otherwise (if we _didn't_ initiate this connection), it's okay for
  604. * the certificate to be weird or absent.
  605. *
  606. * If we return 0, and the certificate is as expected, write a hash of the
  607. * identity key into digest_rcvd, which must have DIGEST_LEN space in it. (If
  608. * we return -1 this buffer is undefined.) If the certificate is invalid
  609. * or missing on an incoming connection, we return 0 and set digest_rcvd to
  610. * DIGEST_LEN 0 bytes.
  611. *
  612. * As side effects,
  613. * 1) Set conn->circ_id_type according to tor-spec.txt.
  614. * 2) If we're an authdirserver and we initiated the connection: drop all
  615. * descriptors that claim to be on that IP/port but that aren't
  616. * this guy; and note that this guy is reachable.
  617. */
  618. static int
  619. connection_or_check_valid_tls_handshake(or_connection_t *conn,
  620. int started_here,
  621. char *digest_rcvd_out)
  622. {
  623. crypto_pk_env_t *identity_rcvd=NULL;
  624. or_options_t *options = get_options();
  625. int severity = server_mode(options) ? LOG_PROTOCOL_WARN : LOG_WARN;
  626. const char *safe_address =
  627. started_here ? conn->_base.address : safe_str(conn->_base.address);
  628. const char *conn_type = started_here ? "outgoing" : "incoming";
  629. int has_cert = 0, has_identity=0;
  630. check_no_tls_errors();
  631. has_cert = tor_tls_peer_has_cert(conn->tls);
  632. if (started_here && !has_cert) {
  633. log_info(LD_PROTOCOL,"Tried connecting to router at %s:%d, but it didn't "
  634. "send a cert! Closing.",
  635. safe_address, conn->_base.port);
  636. return -1;
  637. } else if (!has_cert) {
  638. log_debug(LD_PROTOCOL,"Got incoming connection with no certificate. "
  639. "That's ok.");
  640. }
  641. check_no_tls_errors();
  642. if (has_cert) {
  643. int v = tor_tls_verify_v1(started_here?severity:LOG_INFO,
  644. conn->tls, &identity_rcvd);
  645. if (started_here && v<0) {
  646. log_fn(severity,LD_OR,"Tried connecting to router at %s:%d: It"
  647. " has a cert but it's invalid. Closing.",
  648. safe_address, conn->_base.port);
  649. return -1;
  650. } else if (v<0) {
  651. log_info(LD_PROTOCOL,"Incoming connection gave us an invalid cert "
  652. "chain; ignoring.");
  653. } else {
  654. log_debug(LD_OR,"The certificate seems to be valid on %s connection "
  655. "with %s:%d", conn_type, safe_address, conn->_base.port);
  656. }
  657. check_no_tls_errors();
  658. }
  659. if (identity_rcvd) {
  660. has_identity = 1;
  661. crypto_pk_get_digest(identity_rcvd, digest_rcvd_out);
  662. if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
  663. conn->circ_id_type = CIRC_ID_TYPE_LOWER;
  664. } else {
  665. conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
  666. }
  667. crypto_free_pk_env(identity_rcvd);
  668. } else {
  669. memset(digest_rcvd_out, 0, DIGEST_LEN);
  670. conn->circ_id_type = CIRC_ID_TYPE_NEITHER;
  671. }
  672. if (started_here && tor_digest_is_zero(conn->identity_digest)) {
  673. memcpy(conn->identity_digest, digest_rcvd_out, DIGEST_LEN);
  674. tor_free(conn->nickname);
  675. conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
  676. conn->nickname[0] = '$';
  677. base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
  678. conn->identity_digest, DIGEST_LEN);
  679. log_info(LD_OR, "Connected to router %s at %s:%d without knowing "
  680. "its key. Hoping for the best.",
  681. conn->nickname, conn->_base.address, conn->_base.port);
  682. }
  683. if (started_here) {
  684. int as_advertised = 1;
  685. tor_assert(has_cert);
  686. tor_assert(has_identity);
  687. if (memcmp(digest_rcvd_out, conn->identity_digest, DIGEST_LEN)) {
  688. /* I was aiming for a particular digest. I didn't get it! */
  689. char seen[HEX_DIGEST_LEN+1];
  690. char expected[HEX_DIGEST_LEN+1];
  691. base16_encode(seen, sizeof(seen), digest_rcvd_out, DIGEST_LEN);
  692. base16_encode(expected, sizeof(expected), conn->identity_digest,
  693. DIGEST_LEN);
  694. log_fn(severity, LD_OR,
  695. "Tried connecting to router at %s:%d, but identity key was not "
  696. "as expected: wanted %s but got %s.",
  697. conn->_base.address, conn->_base.port, expected, seen);
  698. entry_guard_register_connect_status(conn->identity_digest,0,time(NULL));
  699. router_set_status(conn->identity_digest, 0);
  700. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED,
  701. END_OR_CONN_REASON_OR_IDENTITY);
  702. as_advertised = 0;
  703. }
  704. if (authdir_mode_tests_reachability(options)) {
  705. /* We initiated this connection to address:port. Drop all routers
  706. * with the same address:port and a different key.
  707. */
  708. dirserv_orconn_tls_done(conn->_base.address, conn->_base.port,
  709. digest_rcvd_out, as_advertised);
  710. }
  711. if (!as_advertised)
  712. return -1;
  713. }
  714. return 0;
  715. }
  716. #if 0
  717. /** DOCDOC */
  718. int
  719. connection_or_finish_or_handshake(or_connection_t *conn)
  720. {
  721. char id_digest[DIGEST_LEN];
  722. tor_assert(conn);
  723. tor_assert(conn->handshake_state);
  724. tor_assert(conn->link_proto >= 2);
  725. tor_assert(conn->handshake_state->received_versions != 0);
  726. tor_assert(conn->handshake_state->received_netinfo != 0);
  727. tor_assert(conn->handshake_state->received_certs != 0);
  728. if (connection_or_check_valid_tls_handshake(conn,
  729. conn->handshake_state->started_here,
  730. id_digest) < 0)
  731. return -1;
  732. connection_or_init_conn_from_address(conn, conn->_base.addr,
  733. conn->_base.port, id_digest, 0);
  734. if (connection_or_act_on_netinfo(conn)<0)
  735. return -1;
  736. return connection_or_set_state_open(conn);
  737. }
  738. #endif
  739. /** The tls handshake is finished.
  740. *
  741. * Make sure we are happy with the person we just handshaked with.
  742. *
  743. * If he initiated the connection, make sure he's not already connected,
  744. * then initialize conn from the information in router.
  745. *
  746. * If all is successful, call circuit_n_conn_done() to handle events
  747. * that have been pending on the <tls handshake completion. Also set the
  748. * directory to be dirty (only matters if I'm an authdirserver).
  749. */
  750. static int
  751. connection_tls_finish_handshake(or_connection_t *conn)
  752. {
  753. char digest_rcvd[DIGEST_LEN];
  754. int started_here = connection_or_nonopen_was_started_here(conn);
  755. log_debug(LD_OR,"tls handshake done. verifying.");
  756. directory_set_dirty();
  757. if (tor_tls_used_v1_handshake(conn->tls)) {
  758. conn->link_proto = 1;
  759. if (connection_or_check_valid_tls_handshake(conn, started_here,
  760. digest_rcvd) < 0)
  761. return -1;
  762. if (!started_here) {
  763. connection_or_init_conn_from_address(conn,conn->_base.addr,
  764. conn->_base.port, digest_rcvd, 0);
  765. }
  766. return connection_or_set_state_open(conn);
  767. } else {
  768. if (started_here) {
  769. if (connection_or_check_valid_tls_handshake(conn, started_here,
  770. digest_rcvd) < 0)
  771. return -1;
  772. }
  773. conn->_base.state = OR_CONN_STATE_OR_HANDSHAKING;
  774. if (connection_init_or_handshake_state(conn, started_here) < 0)
  775. return -1;
  776. return connection_or_send_versions(conn);
  777. }
  778. }
  779. /** DOCDOC */
  780. static int
  781. connection_init_or_handshake_state(or_connection_t *conn, int started_here)
  782. {
  783. or_handshake_state_t *s;
  784. s = conn->handshake_state = tor_malloc_zero(sizeof(or_handshake_state_t));
  785. s->started_here = started_here ? 1 : 0;
  786. if (tor_tls_get_random_values(conn->tls,
  787. conn->handshake_state->client_random,
  788. conn->handshake_state->server_random) < 0)
  789. return -1;
  790. if (started_here) {
  791. if (tor_tls_get_cert_digests(conn->tls,
  792. s->client_cert_digest,
  793. s->server_cert_digest)<0)
  794. return -1;
  795. } else {
  796. if (tor_tls_get_cert_digests(conn->tls,
  797. s->server_cert_digest,
  798. s->client_cert_digest)<0)
  799. return -1;
  800. }
  801. return 0;
  802. }
  803. /** DOCDOC */
  804. void
  805. or_handshake_state_free(or_handshake_state_t *state)
  806. {
  807. tor_assert(state);
  808. if (state->signing_key)
  809. crypto_free_pk_env(state->signing_key);
  810. if (state->identity_key)
  811. crypto_free_pk_env(state->identity_key);
  812. memset(state, 0xBE, sizeof(or_handshake_state_t));
  813. tor_free(state);
  814. }
  815. /**DOCDOC*/
  816. int
  817. connection_or_set_state_open(or_connection_t *conn)
  818. {
  819. int started_here = connection_or_nonopen_was_started_here(conn);
  820. conn->_base.state = OR_CONN_STATE_OPEN;
  821. control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED, 0);
  822. if (started_here) {
  823. rep_hist_note_connect_succeeded(conn->identity_digest, time(NULL));
  824. if (entry_guard_register_connect_status(conn->identity_digest, 1,
  825. time(NULL)) < 0) {
  826. /* pending circs get closed in circuit_about_to_close_connection() */
  827. return -1;
  828. }
  829. router_set_status(conn->identity_digest, 1);
  830. }
  831. if (conn->handshake_state) {
  832. or_handshake_state_free(conn->handshake_state);
  833. conn->handshake_state = NULL;
  834. }
  835. connection_watch_events(TO_CONN(conn), EV_READ);
  836. circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
  837. return 0;
  838. }
  839. /** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s outbuf.
  840. * For cells that use or affect a circuit, this should only be called by
  841. * connection_or_flush_from_first_active_circuit()
  842. */
  843. void
  844. connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
  845. {
  846. packed_cell_t networkcell;
  847. tor_assert(cell);
  848. tor_assert(conn);
  849. cell_pack(&networkcell, cell);
  850. connection_write_to_buf(networkcell.body, CELL_NETWORK_SIZE, TO_CONN(conn));
  851. if (cell->command != CELL_PADDING)
  852. conn->timestamp_last_added_nonpadding = time(NULL);
  853. }
  854. /**DOCDOC*/
  855. void
  856. connection_or_write_var_cell_to_buf(const var_cell_t *cell,
  857. or_connection_t *conn)
  858. {
  859. char hdr[VAR_CELL_HEADER_SIZE];
  860. tor_assert(cell);
  861. tor_assert(conn);
  862. var_cell_pack_header(cell, hdr);
  863. connection_write_to_buf(hdr, sizeof(hdr), TO_CONN(conn));
  864. connection_write_to_buf(cell->payload, cell->payload_len, TO_CONN(conn));
  865. if (cell->command != CELL_PADDING)
  866. conn->timestamp_last_added_nonpadding = time(NULL);
  867. }
  868. /** DOCDOC */
  869. static int
  870. connection_fetch_var_cell_from_buf(or_connection_t *conn, var_cell_t **out)
  871. {
  872. return fetch_var_cell_from_buf(conn->_base.inbuf, out);
  873. }
  874. /** Process cells from <b>conn</b>'s inbuf.
  875. *
  876. * Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
  877. * and hand it to command_process_cell().
  878. *
  879. * Always return 0.
  880. */
  881. static int
  882. connection_or_process_cells_from_inbuf(or_connection_t *conn)
  883. {
  884. var_cell_t *var_cell;
  885. while (1) {
  886. log_debug(LD_OR,
  887. "%d: starting, inbuf_datalen %d (%d pending in tls object).",
  888. conn->_base.s,(int)buf_datalen(conn->_base.inbuf),
  889. tor_tls_get_pending_bytes(conn->tls));
  890. if (connection_fetch_var_cell_from_buf(conn, &var_cell)) {
  891. if (!var_cell)
  892. return 0; /* not yet. */
  893. command_process_var_cell(var_cell, conn);
  894. var_cell_free(var_cell);
  895. } else {
  896. char buf[CELL_NETWORK_SIZE];
  897. cell_t cell;
  898. if (buf_datalen(conn->_base.inbuf) < CELL_NETWORK_SIZE) /* whole response
  899. available? */
  900. return 0; /* not yet */
  901. connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, TO_CONN(conn));
  902. /* retrieve cell info from buf (create the host-order struct from the
  903. * network-order string) */
  904. cell_unpack(&cell, buf);
  905. command_process_cell(&cell, conn);
  906. }
  907. }
  908. }
  909. /** Write a destroy cell with circ ID <b>circ_id</b> and reason <b>reason</b>
  910. * onto OR connection <b>conn</b>. Don't perform range-checking on reason:
  911. * we may want to propagate reasons from other cells.
  912. *
  913. * Return 0.
  914. */
  915. int
  916. connection_or_send_destroy(uint16_t circ_id, or_connection_t *conn, int reason)
  917. {
  918. cell_t cell;
  919. tor_assert(conn);
  920. memset(&cell, 0, sizeof(cell_t));
  921. cell.circ_id = circ_id;
  922. cell.command = CELL_DESTROY;
  923. cell.payload[0] = (uint8_t) reason;
  924. log_debug(LD_OR,"Sending destroy (circID %d).", circ_id);
  925. #if 0
  926. /* XXXX020 Actually, don't kill the cell queue: it may have data that we're
  927. * waiting to flush. We need to do something more sensible here. */
  928. /* Clear the cell queue on the circuit, so that our destroy cell will
  929. * be the very next thing written.*/
  930. circ = circuit_get_by_circid_orconn(circ_id, conn);
  931. circuit_clear_cell_queue(circ, conn);
  932. #endif
  933. connection_or_write_cell_to_buf(&cell, conn);
  934. return 0;
  935. }
  936. /** DOCDOC */
  937. static int
  938. connection_or_send_versions(or_connection_t *conn)
  939. {
  940. var_cell_t *cell;
  941. uint16_t versions[] = { 1, 2 };
  942. int n_versions = sizeof(versions) / sizeof(uint8_t);
  943. int i;
  944. tor_assert(conn->handshake_state &&
  945. !conn->handshake_state->sent_versions_at);
  946. /*XXXX020 docdoc 2-byte versions */
  947. cell = var_cell_new(n_versions * 2);
  948. cell->command = CELL_VERSIONS;
  949. for (i = 0; i < n_versions; ++i) {
  950. uint16_t v = versions[i];
  951. set_uint16(cell->payload+(2*i), htons(v));
  952. }
  953. connection_or_write_var_cell_to_buf(cell, conn);
  954. conn->handshake_state->sent_versions_at = time(NULL);
  955. var_cell_free(cell);
  956. return 0;
  957. }
  958. /** DOCDOC */
  959. int
  960. connection_or_send_netinfo(or_connection_t *conn)
  961. {
  962. cell_t cell;
  963. time_t now = time(NULL);
  964. routerinfo_t *me;
  965. memset(&cell, 0, sizeof(cell_t));
  966. cell.command = CELL_NETINFO;
  967. /* Their address. */
  968. set_uint32(cell.payload, htonl(now));
  969. cell.payload[4] = RESOLVED_TYPE_IPV4;
  970. cell.payload[5] = 4;
  971. set_uint32(cell.payload+6, htonl(conn->_base.addr));
  972. /* My address. */
  973. if ((me = router_get_my_routerinfo())) {
  974. cell.payload[10] = 1; /* only one address is supported. */
  975. cell.payload[11] = RESOLVED_TYPE_IPV4;
  976. cell.payload[12] = 4;
  977. set_uint32(cell.payload+13, htonl(me->addr));
  978. } else {
  979. cell.payload[10] = 0;
  980. }
  981. connection_or_write_cell_to_buf(&cell, conn);
  982. return 0;
  983. }
  984. #if 0
  985. #define LINK_AUTH_STRING "Tor initiator certificate verification"
  986. /** DOCDOC */
  987. int
  988. connection_or_compute_link_auth_hmac(or_connection_t *conn,
  989. char *hmac_out)
  990. {
  991. char buf[64 + 2*TOR_TLS_RANDOM_LEN + 2*DIGEST_LEN];
  992. char *cp;
  993. or_handshake_state_t *s;
  994. tor_assert(conn);
  995. tor_assert(conn->handshake_state);
  996. tor_assert(conn->tls);
  997. s = conn->handshake_state;
  998. /* Fill the buffer. */
  999. strlcpy(buf, LINK_AUTH_STRING, sizeof(buf));
  1000. cp = buf+strlen(buf);
  1001. ++cp; /* Skip the NUL */
  1002. memcpy(cp, s->client_random, TOR_TLS_RANDOM_LEN);
  1003. cp += TOR_TLS_RANDOM_LEN;
  1004. memcpy(cp, s->server_random, TOR_TLS_RANDOM_LEN);
  1005. cp += TOR_TLS_RANDOM_LEN;
  1006. memcpy(cp, s->client_cert_digest, DIGEST_LEN);
  1007. cp += DIGEST_LEN;
  1008. memcpy(cp, s->server_cert_digest, DIGEST_LEN);
  1009. cp += DIGEST_LEN;
  1010. tor_assert(cp < buf+sizeof(buf));
  1011. if (tor_tls_hmac_with_master_secret(conn->tls, hmac_out, buf, cp-buf) < 0)
  1012. return -1;
  1013. return 0;
  1014. }
  1015. /**DOCDOC*/
  1016. int
  1017. connection_or_send_cert(or_connection_t *conn)
  1018. {
  1019. size_t conn_cert_len = 0, id_cert_len = 0, total_len = 0;
  1020. char *id_cert = NULL, *conn_cert = NULL;
  1021. var_cell_t *cell;
  1022. char *cp;
  1023. /* If we're a client, we can send no cert at all. XXXXX020 */
  1024. /* DOCDOC length of cert before cert. */
  1025. tor_assert(conn);
  1026. tor_assert(conn->handshake_state);
  1027. tor_assert(conn->handshake_state->received_versions == 1);
  1028. if (conn->handshake_state->started_here)
  1029. conn_cert = tor_tls_encode_my_certificate(conn->tls, &conn_cert_len, 1);
  1030. id_cert = tor_tls_encode_my_certificate(conn->tls, &id_cert_len, 0);
  1031. tor_assert(id_cert);
  1032. total_len = id_cert_len + conn_cert_len + conn_cert ? 4 : 2;
  1033. cell = var_cell_new(total_len);
  1034. cell->command = CELL_VERSIONS;
  1035. cp = cell->payload;
  1036. if (conn_cert) {
  1037. set_uint16(cp, htons(conn_cert_len));
  1038. cp += 2;
  1039. memcpy(cp, conn_cert, conn_cert_len);
  1040. cp += conn_cert_len;
  1041. }
  1042. set_uint16(cp, htons(id_cert_len));
  1043. cp += 2;
  1044. memcpy(cp, id_cert, id_cert_len);
  1045. cp += id_cert_len;
  1046. tor_assert(cp == cell->payload + total_len);
  1047. connection_or_write_var_cell_to_buf(cell, conn);
  1048. tor_free(conn_cert);
  1049. tor_free(id_cert);
  1050. var_cell_free(cell);
  1051. return 0;
  1052. }
  1053. /**DOCDOC*/
  1054. int
  1055. connection_or_send_link_auth(or_connection_t *conn)
  1056. {
  1057. cell_t cell;
  1058. char hmac[DIGEST_LEN];
  1059. crypto_pk_env_t *key;
  1060. int r, len;
  1061. tor_assert(conn);
  1062. tor_assert(conn->tls);
  1063. tor_assert(conn->handshake_state);
  1064. tor_assert(conn->handshake_state->started_here == 1);
  1065. tor_assert(conn->handshake_state->received_certs == 1);
  1066. memset(&cell, 0, sizeof(cell));
  1067. cell.command = CELL_LINK_AUTH;
  1068. key = tor_tls_dup_private_key(conn->tls);
  1069. connection_or_compute_link_auth_hmac(conn, hmac);
  1070. cell.payload[2] = 0x00; /* Signature version */
  1071. r = crypto_pk_private_sign(key, cell.payload+3, hmac, sizeof(hmac));
  1072. crypto_free_pk_env(key);
  1073. if (r<0)
  1074. return -1;
  1075. len = r + 1;
  1076. set_uint16(cell.payload, htons(len));
  1077. connection_or_write_cell_to_buf(&cell, conn);
  1078. return 0;
  1079. }
  1080. #endif