connection_or.c 38 KB

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