command.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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-2011, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file command.c
  8. * \brief Functions for processing incoming cells.
  9. **/
  10. /* In-points to command.c:
  11. *
  12. * - command_process_cell(), called from
  13. * connection_or_process_cells_from_inbuf() in connection_or.c
  14. */
  15. #include "or.h"
  16. #include "circuitbuild.h"
  17. #include "circuitlist.h"
  18. #include "command.h"
  19. #include "connection.h"
  20. #include "connection_or.h"
  21. #include "config.h"
  22. #include "control.h"
  23. #include "cpuworker.h"
  24. #include "hibernate.h"
  25. #include "nodelist.h"
  26. #include "onion.h"
  27. #include "relay.h"
  28. #include "router.h"
  29. #include "routerlist.h"
  30. /** How many CELL_PADDING cells have we received, ever? */
  31. uint64_t stats_n_padding_cells_processed = 0;
  32. /** How many CELL_CREATE cells have we received, ever? */
  33. uint64_t stats_n_create_cells_processed = 0;
  34. /** How many CELL_CREATED cells have we received, ever? */
  35. uint64_t stats_n_created_cells_processed = 0;
  36. /** How many CELL_RELAY cells have we received, ever? */
  37. uint64_t stats_n_relay_cells_processed = 0;
  38. /** How many CELL_DESTROY cells have we received, ever? */
  39. uint64_t stats_n_destroy_cells_processed = 0;
  40. /** How many CELL_VERSIONS cells have we received, ever? */
  41. uint64_t stats_n_versions_cells_processed = 0;
  42. /** How many CELL_NETINFO cells have we received, ever? */
  43. uint64_t stats_n_netinfo_cells_processed = 0;
  44. /** How many CELL_VPADDING cells have we received, ever? */
  45. uint64_t stats_n_vpadding_cells_processed = 0;
  46. /** How many CELL_CERTS cells have we received, ever? */
  47. uint64_t stats_n_cert_cells_processed = 0;
  48. /** How many CELL_AUTH_CHALLENGE cells have we received, ever? */
  49. uint64_t stats_n_auth_challenge_cells_processed = 0;
  50. /** How many CELL_AUTHENTICATE cells have we received, ever? */
  51. uint64_t stats_n_authenticate_cells_processed = 0;
  52. /* These are the main functions for processing cells */
  53. static void command_process_create_cell(cell_t *cell, or_connection_t *conn);
  54. static void command_process_created_cell(cell_t *cell, or_connection_t *conn);
  55. static void command_process_relay_cell(cell_t *cell, or_connection_t *conn);
  56. static void command_process_destroy_cell(cell_t *cell, or_connection_t *conn);
  57. static void command_process_versions_cell(var_cell_t *cell,
  58. or_connection_t *conn);
  59. static void command_process_netinfo_cell(cell_t *cell, or_connection_t *conn);
  60. static void command_process_cert_cell(var_cell_t *cell,
  61. or_connection_t *conn);
  62. static void command_process_auth_challenge_cell(var_cell_t *cell,
  63. or_connection_t *conn);
  64. static void command_process_authenticate_cell(var_cell_t *cell,
  65. or_connection_t *conn);
  66. #ifdef KEEP_TIMING_STATS
  67. /** This is a wrapper function around the actual function that processes the
  68. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  69. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  70. */
  71. static void
  72. command_time_process_cell(cell_t *cell, or_connection_t *conn, int *time,
  73. void (*func)(cell_t *, or_connection_t *))
  74. {
  75. struct timeval start, end;
  76. long time_passed;
  77. tor_gettimeofday(&start);
  78. (*func)(cell, conn);
  79. tor_gettimeofday(&end);
  80. time_passed = tv_udiff(&start, &end) ;
  81. if (time_passed > 10000) { /* more than 10ms */
  82. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  83. }
  84. if (time_passed < 0) {
  85. log_info(LD_GENERAL,"That call took us back in time!");
  86. time_passed = 0;
  87. }
  88. *time += time_passed;
  89. }
  90. #endif
  91. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  92. * statistics about how many of each cell we've processed so far
  93. * this second, and the total number of microseconds it took to
  94. * process each type of cell.
  95. */
  96. void
  97. command_process_cell(cell_t *cell, or_connection_t *conn)
  98. {
  99. int handshaking = (conn->_base.state != OR_CONN_STATE_OPEN);
  100. #ifdef KEEP_TIMING_STATS
  101. /* how many of each cell have we seen so far this second? needs better
  102. * name. */
  103. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  104. /* how long has it taken to process each type of cell? */
  105. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  106. static time_t current_second = 0; /* from previous calls to time */
  107. time_t now = time(NULL);
  108. if (now > current_second) { /* the second has rolled over */
  109. /* print stats */
  110. log_info(LD_OR,
  111. "At end of second: %d creates (%d ms), %d createds (%d ms), "
  112. "%d relays (%d ms), %d destroys (%d ms)",
  113. num_create, create_time/1000,
  114. num_created, created_time/1000,
  115. num_relay, relay_time/1000,
  116. num_destroy, destroy_time/1000);
  117. /* zero out stats */
  118. num_create = num_created = num_relay = num_destroy = 0;
  119. create_time = created_time = relay_time = destroy_time = 0;
  120. /* remember which second it is, for next time */
  121. current_second = now;
  122. }
  123. #endif
  124. #ifdef KEEP_TIMING_STATS
  125. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  126. ++num ## tp; \
  127. command_time_process_cell(cl, cn, & tp ## time , \
  128. command_process_ ## tp ## _cell); \
  129. } STMT_END
  130. #else
  131. #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
  132. #endif
  133. /* Reject all but VERSIONS and NETINFO when handshaking. */
  134. if (handshaking && cell->command != CELL_VERSIONS &&
  135. cell->command != CELL_NETINFO)
  136. return;
  137. /* XXXX VERSIONS should be impossible; it's variable-length. */
  138. if (conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3)
  139. or_handshake_state_record_cell(conn->handshake_state, cell, 1);
  140. switch (cell->command) {
  141. case CELL_PADDING:
  142. ++stats_n_padding_cells_processed;
  143. /* do nothing */
  144. break;
  145. case CELL_CREATE:
  146. case CELL_CREATE_FAST:
  147. ++stats_n_create_cells_processed;
  148. PROCESS_CELL(create, cell, conn);
  149. break;
  150. case CELL_CREATED:
  151. case CELL_CREATED_FAST:
  152. ++stats_n_created_cells_processed;
  153. PROCESS_CELL(created, cell, conn);
  154. break;
  155. case CELL_RELAY:
  156. case CELL_RELAY_EARLY:
  157. ++stats_n_relay_cells_processed;
  158. PROCESS_CELL(relay, cell, conn);
  159. break;
  160. case CELL_DESTROY:
  161. ++stats_n_destroy_cells_processed;
  162. PROCESS_CELL(destroy, cell, conn);
  163. break;
  164. case CELL_VERSIONS:
  165. tor_fragile_assert();
  166. break;
  167. case CELL_NETINFO:
  168. ++stats_n_netinfo_cells_processed;
  169. PROCESS_CELL(netinfo, cell, conn);
  170. break;
  171. default:
  172. log_fn(LOG_INFO, LD_PROTOCOL,
  173. "Cell of unknown type (%d) received. Dropping.", cell->command);
  174. break;
  175. }
  176. }
  177. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  178. * statistics about how many of each cell we've processed so far
  179. * this second, and the total number of microseconds it took to
  180. * process each type of cell.
  181. */
  182. void
  183. command_process_var_cell(var_cell_t *cell, or_connection_t *conn)
  184. {
  185. #ifdef KEEP_TIMING_STATS
  186. /* how many of each cell have we seen so far this second? needs better
  187. * name. */
  188. static int num_versions=0, num_cert=0;
  189. time_t now = time(NULL);
  190. if (now > current_second) { /* the second has rolled over */
  191. /* print stats */
  192. log_info(LD_OR,
  193. "At end of second: %d versions (%d ms), %d cert (%d ms)",
  194. num_versions, versions_time/1000,
  195. cert, cert_time/1000);
  196. num_versions = num_cert = 0;
  197. versions_time = cert_time = 0;
  198. /* remember which second it is, for next time */
  199. current_second = now;
  200. }
  201. #endif
  202. switch (conn->_base.state)
  203. {
  204. case OR_CONN_STATE_OR_HANDSHAKING_V2:
  205. if (cell->command != CELL_VERSIONS)
  206. return;
  207. break;
  208. case OR_CONN_STATE_TLS_HANDSHAKING:
  209. /* If we're using bufferevents, it's entirely possible for us to
  210. * notice "hey, data arrived!" before we notice "hey, the handshake
  211. * finished!" And we need to be accepting both at once to handle both
  212. * the v2 and v3 handshakes. */
  213. /* fall through */
  214. case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
  215. if (cell->command != CELL_VERSIONS)
  216. return; /*XXXX023 log*/
  217. break;
  218. case OR_CONN_STATE_OR_HANDSHAKING_V3:
  219. if (cell->command != CELL_AUTHENTICATE)
  220. or_handshake_state_record_var_cell(conn->handshake_state, cell, 1);
  221. break; /* Everything is allowed */
  222. case OR_CONN_STATE_OPEN:
  223. if (conn->link_proto < 3)
  224. return;
  225. default:
  226. /*XXXX023 log */
  227. return;
  228. }
  229. switch (cell->command) {
  230. case CELL_VERSIONS:
  231. ++stats_n_versions_cells_processed;
  232. PROCESS_CELL(versions, cell, conn);
  233. break;
  234. case CELL_VPADDING:
  235. ++stats_n_vpadding_cells_processed;
  236. PROCESS_CELL(versions, cell, conn);
  237. break;
  238. case CELL_CERT:
  239. ++stats_n_cert_cells_processed;
  240. PROCESS_CELL(cert, cell, conn);
  241. break;
  242. case CELL_AUTH_CHALLENGE:
  243. ++stats_n_auth_challenge_cells_processed;
  244. PROCESS_CELL(auth_challenge, cell, conn);
  245. break;
  246. case CELL_AUTHENTICATE:
  247. ++stats_n_authenticate_cells_processed;
  248. PROCESS_CELL(authenticate, cell, conn);
  249. break;
  250. default:
  251. log_fn(LOG_INFO, LD_PROTOCOL,
  252. "Variable-length cell of unknown type (%d) received.",
  253. cell->command);
  254. break;
  255. }
  256. }
  257. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a
  258. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  259. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  260. * picked up again when the cpuworker finishes decrypting it.
  261. */
  262. static void
  263. command_process_create_cell(cell_t *cell, or_connection_t *conn)
  264. {
  265. or_circuit_t *circ;
  266. int id_is_high;
  267. if (we_are_hibernating()) {
  268. log_info(LD_OR,
  269. "Received create cell but we're shutting down. Sending back "
  270. "destroy.");
  271. connection_or_send_destroy(cell->circ_id, conn,
  272. END_CIRC_REASON_HIBERNATING);
  273. return;
  274. }
  275. if (!server_mode(get_options())) {
  276. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  277. "Received create cell (type %d) from %s:%d, but we're a client. "
  278. "Sending back a destroy.",
  279. (int)cell->command, conn->_base.address, conn->_base.port);
  280. connection_or_send_destroy(cell->circ_id, conn,
  281. END_CIRC_REASON_TORPROTOCOL);
  282. return;
  283. }
  284. /* If the high bit of the circuit ID is not as expected, close the
  285. * circ. */
  286. id_is_high = cell->circ_id & (1<<15);
  287. if ((id_is_high && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
  288. (!id_is_high && conn->circ_id_type == CIRC_ID_TYPE_LOWER)) {
  289. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  290. "Received create cell with unexpected circ_id %d. Closing.",
  291. cell->circ_id);
  292. connection_or_send_destroy(cell->circ_id, conn,
  293. END_CIRC_REASON_TORPROTOCOL);
  294. return;
  295. }
  296. if (circuit_id_in_use_on_orconn(cell->circ_id, conn)) {
  297. const node_t *node = node_get_by_id(conn->identity_digest);
  298. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  299. "Received CREATE cell (circID %d) for known circ. "
  300. "Dropping (age %d).",
  301. cell->circ_id, (int)(time(NULL) - conn->_base.timestamp_created));
  302. if (node) {
  303. char *p = esc_for_log(node_get_platform(node));
  304. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  305. "Details: router %s, platform %s.",
  306. node_describe(node), p);
  307. tor_free(p);
  308. }
  309. return;
  310. }
  311. circ = or_circuit_new(cell->circ_id, conn);
  312. circ->_base.purpose = CIRCUIT_PURPOSE_OR;
  313. circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
  314. if (cell->command == CELL_CREATE) {
  315. char *onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  316. memcpy(onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  317. /* hand it off to the cpuworkers, and then return. */
  318. if (assign_onionskin_to_cpuworker(NULL, circ, onionskin) < 0) {
  319. #define WARN_HANDOFF_FAILURE_INTERVAL (6*60*60)
  320. static ratelim_t handoff_warning =
  321. RATELIM_INIT(WARN_HANDOFF_FAILURE_INTERVAL);
  322. char *m;
  323. if ((m = rate_limit_log(&handoff_warning, approx_time()))) {
  324. log_warn(LD_GENERAL,"Failed to hand off onionskin. Closing.%s",m);
  325. tor_free(m);
  326. }
  327. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  328. return;
  329. }
  330. log_debug(LD_OR,"success: handed off onionskin.");
  331. } else {
  332. /* This is a CREATE_FAST cell; we can handle it immediately without using
  333. * a CPU worker. */
  334. char keys[CPATH_KEY_MATERIAL_LEN];
  335. char reply[DIGEST_LEN*2];
  336. tor_assert(cell->command == CELL_CREATE_FAST);
  337. if (fast_server_handshake(cell->payload, (uint8_t*)reply,
  338. (uint8_t*)keys, sizeof(keys))<0) {
  339. log_warn(LD_OR,"Failed to generate key material. Closing.");
  340. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  341. return;
  342. }
  343. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  344. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  345. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  346. return;
  347. }
  348. }
  349. }
  350. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>.
  351. * Find the circuit
  352. * that it's intended for. If we're not the origin of the circuit, package
  353. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  354. * are the origin of the circuit, send it to circuit_finish_handshake() to
  355. * finish processing keys, and then call circuit_send_next_onion_skin() to
  356. * extend to the next hop in the circuit if necessary.
  357. */
  358. static void
  359. command_process_created_cell(cell_t *cell, or_connection_t *conn)
  360. {
  361. circuit_t *circ;
  362. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  363. if (!circ) {
  364. log_info(LD_OR,
  365. "(circID %d) unknown circ (probably got a destroy earlier). "
  366. "Dropping.", cell->circ_id);
  367. return;
  368. }
  369. if (circ->n_circ_id != cell->circ_id) {
  370. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  371. "got created cell from Tor client? Closing.");
  372. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  373. return;
  374. }
  375. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  376. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  377. int err_reason = 0;
  378. log_debug(LD_OR,"at OP. Finishing handshake.");
  379. if ((err_reason = circuit_finish_handshake(origin_circ, cell->command,
  380. cell->payload)) < 0) {
  381. log_warn(LD_OR,"circuit_finish_handshake failed.");
  382. circuit_mark_for_close(circ, -err_reason);
  383. return;
  384. }
  385. log_debug(LD_OR,"Moving to next skin.");
  386. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  387. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  388. /* XXX push this circuit_close lower */
  389. circuit_mark_for_close(circ, -err_reason);
  390. return;
  391. }
  392. } else { /* pack it into an extended relay cell, and send it. */
  393. log_debug(LD_OR,
  394. "Converting created cell to extended relay cell, sending.");
  395. relay_send_command_from_edge(0, circ, RELAY_COMMAND_EXTENDED,
  396. (char*)cell->payload, ONIONSKIN_REPLY_LEN,
  397. NULL);
  398. }
  399. }
  400. /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
  401. * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
  402. * circuit_receive_relay_cell() for actual processing.
  403. */
  404. static void
  405. command_process_relay_cell(cell_t *cell, or_connection_t *conn)
  406. {
  407. circuit_t *circ;
  408. int reason, direction;
  409. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  410. if (!circ) {
  411. log_debug(LD_OR,
  412. "unknown circuit %d on connection from %s:%d. Dropping.",
  413. cell->circ_id, conn->_base.address, conn->_base.port);
  414. return;
  415. }
  416. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  417. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  418. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  419. return;
  420. }
  421. if (CIRCUIT_IS_ORIGIN(circ)) {
  422. /* if we're a relay and treating connections with recent local
  423. * traffic better, then this is one of them. */
  424. conn->client_used = time(NULL);
  425. }
  426. if (!CIRCUIT_IS_ORIGIN(circ) &&
  427. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  428. direction = CELL_DIRECTION_OUT;
  429. else
  430. direction = CELL_DIRECTION_IN;
  431. /* If we have a relay_early cell, make sure that it's outbound, and we've
  432. * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
  433. if (cell->command == CELL_RELAY_EARLY) {
  434. if (direction == CELL_DIRECTION_IN) {
  435. /* Allow an unlimited number of inbound relay_early cells,
  436. * for hidden service compatibility. There isn't any way to make
  437. * a long circuit through inbound relay_early cells anyway. See
  438. * bug 1038. -RD */
  439. } else {
  440. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  441. if (or_circ->remaining_relay_early_cells == 0) {
  442. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  443. "Received too many RELAY_EARLY cells on circ %d from %s:%d."
  444. " Closing circuit.",
  445. cell->circ_id, safe_str(conn->_base.address),
  446. conn->_base.port);
  447. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  448. return;
  449. }
  450. --or_circ->remaining_relay_early_cells;
  451. }
  452. }
  453. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  454. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  455. "(%s) failed. Closing.",
  456. direction==CELL_DIRECTION_OUT?"forward":"backward");
  457. circuit_mark_for_close(circ, -reason);
  458. }
  459. }
  460. /** Process a 'destroy' <b>cell</b> that just arrived from
  461. * <b>conn</b>. Find the circ that it refers to (if any).
  462. *
  463. * If the circ is in state
  464. * onionskin_pending, then call onion_pending_remove() to remove it
  465. * from the pending onion list (note that if it's already being
  466. * processed by the cpuworker, it won't be in the list anymore; but
  467. * when the cpuworker returns it, the circuit will be gone, and the
  468. * cpuworker response will be dropped).
  469. *
  470. * Then mark the circuit for close (which marks all edges for close,
  471. * and passes the destroy cell onward if necessary).
  472. */
  473. static void
  474. command_process_destroy_cell(cell_t *cell, or_connection_t *conn)
  475. {
  476. circuit_t *circ;
  477. int reason;
  478. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  479. reason = (uint8_t)cell->payload[0];
  480. if (!circ) {
  481. log_info(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  482. cell->circ_id, conn->_base.address, conn->_base.port);
  483. return;
  484. }
  485. log_debug(LD_OR,"Received for circID %d.",cell->circ_id);
  486. if (!CIRCUIT_IS_ORIGIN(circ) &&
  487. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  488. /* the destroy came from behind */
  489. circuit_set_p_circid_orconn(TO_OR_CIRCUIT(circ), 0, NULL);
  490. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  491. } else { /* the destroy came from ahead */
  492. circuit_set_n_circid_orconn(circ, 0, NULL);
  493. if (CIRCUIT_IS_ORIGIN(circ)) {
  494. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  495. } else {
  496. char payload[1];
  497. log_debug(LD_OR, "Delivering 'truncated' back.");
  498. payload[0] = (char)reason;
  499. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  500. payload, sizeof(payload), NULL);
  501. }
  502. }
  503. }
  504. /** Process a 'versions' cell. The current link protocol version must be 0
  505. * to indicate that no version has yet been negotiated. We compare the
  506. * versions in the cell to the list of versions we support, pick the
  507. * highest version we have in common, and continue the negotiation from
  508. * there.
  509. */
  510. static void
  511. command_process_versions_cell(var_cell_t *cell, or_connection_t *conn)
  512. {
  513. int highest_supported_version = 0;
  514. const uint8_t *cp, *end;
  515. const int started_here = connection_or_nonopen_was_started_here(conn);
  516. if (conn->link_proto != 0 ||
  517. (conn->handshake_state && conn->handshake_state->received_versions)) {
  518. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  519. "Received a VERSIONS cell on a connection with its version "
  520. "already set to %d; dropping", (int) conn->link_proto);
  521. return;
  522. }
  523. switch (conn->_base.state)
  524. {
  525. case OR_CONN_STATE_OR_HANDSHAKING_V2:
  526. break;
  527. case OR_CONN_STATE_TLS_HANDSHAKING:
  528. case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
  529. if (started_here) {
  530. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  531. "Received a versions cell while TLS-handshaking not in "
  532. "OR_HANDSHAKING_V3 on a connection we originated.");
  533. }
  534. conn->_base.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  535. if (connection_init_or_handshake_state(conn, started_here) < 0) {
  536. connection_mark_for_close(TO_CONN(conn));
  537. return;
  538. }
  539. or_handshake_state_record_var_cell(conn->handshake_state, cell, 1);
  540. break;
  541. case OR_CONN_STATE_OR_HANDSHAKING_V3:
  542. break;
  543. default:
  544. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  545. "VERSIONS cell while in unexpected state");
  546. return;
  547. }
  548. tor_assert(conn->handshake_state);
  549. end = cell->payload + cell->payload_len;
  550. for (cp = cell->payload; cp+1 < end; ++cp) {
  551. uint16_t v = ntohs(get_uint16(cp));
  552. if (is_or_protocol_version_known(v) && v > highest_supported_version)
  553. highest_supported_version = v;
  554. }
  555. if (!highest_supported_version) {
  556. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  557. "Couldn't find a version in common between my version list and the "
  558. "list in the VERSIONS cell; closing connection.");
  559. connection_mark_for_close(TO_CONN(conn));
  560. return;
  561. } else if (highest_supported_version == 1) {
  562. /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS
  563. * cells. */
  564. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  565. "Used version negotiation protocol to negotiate a v1 connection. "
  566. "That's crazily non-compliant. Closing connection.");
  567. connection_mark_for_close(TO_CONN(conn));
  568. return;
  569. } else if (highest_supported_version < 3 &&
  570. conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
  571. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  572. "Negotiated link protocol 2 or lower after doing a v3 TLS "
  573. "handshake. Closing connection.");
  574. connection_mark_for_close(TO_CONN(conn));
  575. return;
  576. }
  577. conn->link_proto = highest_supported_version;
  578. conn->handshake_state->received_versions = 1;
  579. if (conn->link_proto == 2) {
  580. log_info(LD_OR, "Negotiated version %d with %s:%d; sending NETINFO.",
  581. highest_supported_version,
  582. safe_str_client(conn->_base.address),
  583. conn->_base.port);
  584. if (connection_or_send_netinfo(conn) < 0) {
  585. connection_mark_for_close(TO_CONN(conn));
  586. return;
  587. }
  588. } else {
  589. const int send_versions = !started_here;
  590. /* If we want to authenticate, send a CERTS cell */
  591. const int send_certs = !started_here || public_server_mode(get_options());
  592. /* If we're a relay that got a connection, ask for authentication. */
  593. const int send_chall = !started_here && public_server_mode(get_options());
  594. /* If our certs cell will authenticate us, or if we have no intention of
  595. * authenticating, send a netinfo cell right now. */
  596. const int send_netinfo =
  597. !(started_here && public_server_mode(get_options()));
  598. const int send_any =
  599. send_versions || send_certs || send_chall || send_netinfo;
  600. tor_assert(conn->link_proto >= 3);
  601. log_info(LD_OR, "Negotiated version %d with %s:%d; %s%s%s%s%s",
  602. highest_supported_version,
  603. safe_str_client(conn->_base.address),
  604. conn->_base.port,
  605. send_any ? "Sending cells:" : "Waiting for CERTS cell",
  606. send_versions ? " VERSIONS" : "",
  607. send_certs ? " CERTS" : "",
  608. send_chall ? " AUTH_CHALLENGE" : "",
  609. send_netinfo ? " NETINFO" : "");
  610. if (send_versions) {
  611. if (connection_or_send_versions(conn, 1) < 0) {
  612. log_warn(LD_OR, "Couldn't send versions cell");
  613. connection_mark_for_close(TO_CONN(conn));
  614. return;
  615. }
  616. }
  617. if (send_certs) {
  618. if (connection_or_send_cert_cell(conn) < 0) {
  619. log_warn(LD_OR, "Couldn't send cert cell");
  620. connection_mark_for_close(TO_CONN(conn));
  621. return;
  622. }
  623. }
  624. if (send_chall) {
  625. if (connection_or_send_auth_challenge_cell(conn) < 0) {
  626. log_warn(LD_OR, "Couldn't send auth_challenge cell");
  627. connection_mark_for_close(TO_CONN(conn));
  628. return;
  629. }
  630. }
  631. if (send_netinfo) {
  632. if (connection_or_send_netinfo(conn) < 0) {
  633. log_warn(LD_OR, "Couldn't send netinfo cell");
  634. connection_mark_for_close(TO_CONN(conn));
  635. return;
  636. }
  637. }
  638. }
  639. }
  640. /** Process a 'netinfo' cell: read and act on its contents, and set the
  641. * connection state to "open". */
  642. static void
  643. command_process_netinfo_cell(cell_t *cell, or_connection_t *conn)
  644. {
  645. time_t timestamp;
  646. uint8_t my_addr_type;
  647. uint8_t my_addr_len;
  648. const uint8_t *my_addr_ptr;
  649. const uint8_t *cp, *end;
  650. uint8_t n_other_addrs;
  651. time_t now = time(NULL);
  652. long apparent_skew = 0;
  653. uint32_t my_apparent_addr = 0;
  654. if (conn->link_proto < 2) {
  655. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  656. "Received a NETINFO cell on %s connection; dropping.",
  657. conn->link_proto == 0 ? "non-versioned" : "a v1");
  658. return;
  659. }
  660. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V2 &&
  661. conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3) {
  662. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  663. "Received a NETINFO cell on non-handshaking connection; dropping.");
  664. return;
  665. }
  666. tor_assert(conn->handshake_state &&
  667. conn->handshake_state->received_versions);
  668. if (conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
  669. tor_assert(conn->link_proto >= 3);
  670. if (conn->handshake_state->started_here) {
  671. if (!conn->handshake_state->authenticated) {
  672. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got a NETINFO cell from server, "
  673. "but no authentication. Closing the connection.");
  674. connection_mark_for_close(TO_CONN(conn));
  675. }
  676. }
  677. }
  678. /* Decode the cell. */
  679. timestamp = ntohl(get_uint32(cell->payload));
  680. if (labs(now - conn->handshake_state->sent_versions_at) < 180) {
  681. apparent_skew = now - timestamp;
  682. }
  683. my_addr_type = (uint8_t) cell->payload[4];
  684. my_addr_len = (uint8_t) cell->payload[5];
  685. my_addr_ptr = (uint8_t*) cell->payload + 6;
  686. end = cell->payload + CELL_PAYLOAD_SIZE;
  687. cp = cell->payload + 6 + my_addr_len;
  688. if (cp >= end) {
  689. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  690. "Addresses too long in netinfo cell; closing connection.");
  691. connection_mark_for_close(TO_CONN(conn));
  692. return;
  693. } else if (my_addr_type == RESOLVED_TYPE_IPV4 && my_addr_len == 4) {
  694. my_apparent_addr = ntohl(get_uint32(my_addr_ptr));
  695. }
  696. n_other_addrs = (uint8_t) *cp++;
  697. while (n_other_addrs && cp < end-2) {
  698. /* Consider all the other addresses; if any matches, this connection is
  699. * "canonical." */
  700. tor_addr_t addr;
  701. const uint8_t *next =
  702. decode_address_from_payload(&addr, cp, (int)(end-cp));
  703. if (next == NULL) {
  704. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  705. "Bad address in netinfo cell; closing connection.");
  706. connection_mark_for_close(TO_CONN(conn));
  707. return;
  708. }
  709. if (tor_addr_eq(&addr, &conn->real_addr)) {
  710. conn->is_canonical = 1;
  711. break;
  712. }
  713. cp = next;
  714. --n_other_addrs;
  715. }
  716. /* Act on apparent skew. */
  717. /** Warn when we get a netinfo skew with at least this value. */
  718. #define NETINFO_NOTICE_SKEW 3600
  719. if (labs(apparent_skew) > NETINFO_NOTICE_SKEW &&
  720. router_get_by_id_digest(conn->identity_digest)) {
  721. char dbuf[64];
  722. int severity;
  723. /*XXXX be smarter about when everybody says we are skewed. */
  724. if (router_digest_is_trusted_dir(conn->identity_digest))
  725. severity = LOG_WARN;
  726. else
  727. severity = LOG_INFO;
  728. format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
  729. log_fn(severity, LD_GENERAL, "Received NETINFO cell with skewed time from "
  730. "server at %s:%d. It seems that our clock is %s by %s, or "
  731. "that theirs is %s. Tor requires an accurate clock to work: "
  732. "please check your time and date settings.",
  733. conn->_base.address, (int)conn->_base.port,
  734. apparent_skew>0 ? "ahead" : "behind", dbuf,
  735. apparent_skew>0 ? "behind" : "ahead");
  736. if (severity == LOG_WARN) /* only tell the controller if an authority */
  737. control_event_general_status(LOG_WARN,
  738. "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
  739. apparent_skew,
  740. conn->_base.address, conn->_base.port);
  741. }
  742. /* XXX maybe act on my_apparent_addr, if the source is sufficiently
  743. * trustworthy. */
  744. (void)my_apparent_addr;
  745. if (connection_or_set_state_open(conn)<0)
  746. connection_mark_for_close(TO_CONN(conn));
  747. else
  748. log_info(LD_OR, "Got good NETINFO cell from %s:%d; OR connection is now "
  749. "open, using protocol version %d",
  750. safe_str_client(conn->_base.address),
  751. conn->_base.port, (int)conn->link_proto);
  752. assert_connection_ok(TO_CONN(conn),time(NULL));
  753. }
  754. /** Process a CERT cell from an OR connection.
  755. *
  756. * If the other side should not have sent us a CERT cell, or the cell is
  757. * malformed, or it is supposed to authenticate the TLS key but it doesn't,
  758. * then mark the connection.
  759. *
  760. * If the cell has a good cert chain and we're doing a v3 handshake, then
  761. * store the certificates in or_handshake_state. If this is the client side
  762. * of the connection, we then authenticate the server or mark the connection.
  763. * If it's the server side, wait for an AUTHENTICATE cell.
  764. */
  765. static void
  766. command_process_cert_cell(var_cell_t *cell, or_connection_t *conn)
  767. {
  768. #define ERR(s) \
  769. do { \
  770. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  771. "Received a bad CERT cell from %s:%d: %s", \
  772. conn->_base.address, conn->_base.port, (s)); \
  773. connection_mark_for_close(TO_CONN(conn)); \
  774. goto err; \
  775. } while (0)
  776. tor_cert_t *link_cert = NULL;
  777. tor_cert_t *id_cert = NULL;
  778. tor_cert_t *auth_cert = NULL;
  779. uint8_t *ptr;
  780. int n_certs, i;
  781. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  782. ERR("We're not doing a v3 handshake!");
  783. if (conn->link_proto < 3)
  784. ERR("We're not using link protocol >= 3");
  785. if (conn->handshake_state->received_cert_cell)
  786. ERR("We already got one");
  787. if (conn->handshake_state->authenticated) {
  788. /* Should be unreachable, but let's make sure. */
  789. ERR("We're already authenticated!");
  790. }
  791. if (cell->payload_len < 1)
  792. ERR("It had no body");
  793. if (cell->circ_id)
  794. ERR("It had a nonzero circuit ID");
  795. n_certs = cell->payload[0];
  796. ptr = cell->payload + 1;
  797. for (i = 0; i < n_certs; ++i) {
  798. uint8_t cert_type;
  799. uint16_t cert_len;
  800. if (ptr + 3 > cell->payload + cell->payload_len) {
  801. goto truncated;
  802. }
  803. cert_type = *ptr;
  804. cert_len = ntohs(get_uint16(ptr+1));
  805. if (ptr + 3 + cert_len > cell->payload + cell->payload_len) {
  806. goto truncated;
  807. }
  808. if (cert_type == OR_CERT_TYPE_TLS_LINK ||
  809. cert_type == OR_CERT_TYPE_ID_1024 ||
  810. cert_type == OR_CERT_TYPE_AUTH_1024) {
  811. tor_cert_t *cert = tor_cert_decode(ptr + 3, cert_len);
  812. if (!cert) {
  813. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  814. "Received undecodable certificate in CERT cell from %s:%d",
  815. conn->_base.address, conn->_base.port);
  816. } else {
  817. if (cert_type == OR_CERT_TYPE_TLS_LINK && !link_cert)
  818. link_cert = cert;
  819. else if (cert_type == OR_CERT_TYPE_ID_1024 && !id_cert)
  820. id_cert = cert;
  821. else if (cert_type == OR_CERT_TYPE_AUTH_1024 && !auth_cert)
  822. auth_cert = cert;
  823. else
  824. tor_cert_free(cert);
  825. }
  826. }
  827. ptr += 3 + cert_len;
  828. continue;
  829. truncated:
  830. ERR("It ends in the middle of a certificate");
  831. }
  832. if (conn->handshake_state->started_here) {
  833. if (! (id_cert && link_cert))
  834. ERR("The certs we wanted were missing");
  835. /* Okay. We should be able to check the certificates now. */
  836. if (! tor_tls_cert_matches_key(conn->tls, link_cert)) {
  837. ERR("The link certificate didn't match the TLS public key");
  838. }
  839. if (! tor_tls_cert_is_valid(link_cert, id_cert, 0))
  840. ERR("The link certificate was not valid");
  841. if (! tor_tls_cert_is_valid(id_cert, id_cert, 1))
  842. ERR("The ID certificate was not valid");
  843. conn->handshake_state->authenticated = 1;
  844. {
  845. crypto_pk_env_t *identity_rcvd = tor_tls_cert_get_key(id_cert);
  846. const digests_t *id_digests = tor_cert_get_id_digests(id_cert);
  847. memcpy(conn->handshake_state->authenticated_peer_id,
  848. id_digests->d[DIGEST_SHA1], DIGEST_LEN);
  849. connection_or_set_circid_type(conn, identity_rcvd);
  850. crypto_free_pk_env(identity_rcvd);
  851. }
  852. if (connection_or_client_learned_peer_id(conn,
  853. conn->handshake_state->authenticated_peer_id) < 0)
  854. ERR("Problem setting or checking peer id");
  855. conn->handshake_state->id_cert = id_cert;
  856. id_cert = NULL;
  857. } else {
  858. if (! (id_cert && auth_cert))
  859. ERR("The certs we wanted were missing");
  860. /* Remember these certificates so we can check an AUTHENTICATE cell */
  861. conn->handshake_state->id_cert = id_cert;
  862. conn->handshake_state->auth_cert = auth_cert;
  863. if (! tor_tls_cert_is_valid(auth_cert, id_cert, 1))
  864. ERR("The authentication certificate was not valid");
  865. if (! tor_tls_cert_is_valid(id_cert, id_cert, 1))
  866. ERR("The ID certificate was not valid");
  867. /* XXXX check more stuff? */
  868. id_cert = auth_cert = NULL;
  869. }
  870. conn->handshake_state->received_cert_cell = 1;
  871. err:
  872. tor_cert_free(id_cert);
  873. tor_cert_free(link_cert);
  874. tor_cert_free(auth_cert);
  875. #undef ERR
  876. }
  877. /** Process an AUTH_CHALLENGE cell from an OR connection.
  878. *
  879. * If we weren't supposed to get one (for example, because we're not the
  880. * originator of the connection), or it's ill-formed, or we aren't doing a v3
  881. * handshake, mark the connection. If the cell is well-formed but we don't
  882. * want to authenticate, just drop it. If the cell is well-formed *and* we
  883. * want to authenticate, send an AUTHENTICATE cell. */
  884. static void
  885. command_process_auth_challenge_cell(var_cell_t *cell, or_connection_t *conn)
  886. {
  887. int n_types, i, use_type = -1;
  888. uint8_t *cp;
  889. #define ERR(s) \
  890. do { \
  891. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  892. "Received a bad AUTH_CHALLENGE cell from %s:%d: %s", \
  893. conn->_base.address, conn->_base.port, (s)); \
  894. connection_mark_for_close(TO_CONN(conn)); \
  895. return; \
  896. } while (0)
  897. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  898. ERR("We're not currently doing a v3 handshake");
  899. if (conn->link_proto < 3)
  900. ERR("We're not using link protocol >= 3");
  901. if (! conn->handshake_state->started_here)
  902. ERR("We didn't originate this connection");
  903. if (conn->handshake_state->received_auth_challenge)
  904. ERR("We already received one");
  905. if (! conn->handshake_state->received_cert_cell)
  906. ERR("We haven't gotten a CERTS cell yet");
  907. if (cell->payload_len < OR_AUTH_CHALLENGE_LEN + 2)
  908. ERR("It was too short");
  909. if (cell->circ_id)
  910. ERR("It had a nonzero circuit ID");
  911. n_types = ntohs(get_uint16(cell->payload + OR_AUTH_CHALLENGE_LEN));
  912. if (cell->payload_len < OR_AUTH_CHALLENGE_LEN + 2 + 2*n_types)
  913. ERR("It looks truncated");
  914. memcpy(conn->handshake_state->auth_challenge, cell->payload,
  915. OR_AUTH_CHALLENGE_LEN);
  916. /* Now see if there is an authentication type we can use */
  917. cp=cell->payload+OR_AUTH_CHALLENGE_LEN+2;
  918. for (i=0; i < n_types; ++i, cp += 2) {
  919. uint16_t authtype = ntohs(get_uint16(cp));
  920. if (authtype == AUTHTYPE_RSA_SHA256_TLSSECRET)
  921. use_type = authtype;
  922. }
  923. conn->handshake_state->received_auth_challenge = 1;
  924. if (use_type && public_server_mode(get_options())) {
  925. if (connection_or_send_authenticate_cell(conn, use_type) < 0) {
  926. log_warn(LD_OR, "Couldn't send authenticate cell");
  927. connection_mark_for_close(TO_CONN(conn));
  928. return;
  929. }
  930. if (connection_or_send_netinfo(conn) < 0) {
  931. log_warn(LD_OR, "Couldn't send netinfo cell");
  932. connection_mark_for_close(TO_CONN(conn));
  933. return;
  934. }
  935. }
  936. #undef ERR
  937. }
  938. /** Process an AUTHENTICATE cell from an OR connection.
  939. *
  940. * If it's ill-formed or we weren't supposed to get one or we're not doing a
  941. * v3 handshake, then mark the connection. If it does not authenticate the
  942. * other side of the connection successfully (because it isn't signed right,
  943. * we didn't get a CERT cell, etc) mark the connection. Otherwise, accept
  944. * the identity of the router on the other side of the connection.
  945. */
  946. static void
  947. command_process_authenticate_cell(var_cell_t *cell, or_connection_t *conn)
  948. {
  949. uint8_t expected[V3_AUTH_FIXED_PART_LEN];
  950. const uint8_t *auth;
  951. int authlen;
  952. #define ERR(s) \
  953. do { \
  954. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  955. "Received a bad AUTHETNICATE cell from %s:%d: %s", \
  956. conn->_base.address, conn->_base.port, (s)); \
  957. connection_mark_for_close(TO_CONN(conn)); \
  958. return; \
  959. } while (0)
  960. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  961. ERR("We're not doing a v3 handshake");
  962. if (conn->link_proto < 3)
  963. ERR("We're not using link protocol >= 3");
  964. if (conn->handshake_state->started_here)
  965. ERR("We originated this connection");
  966. if (conn->handshake_state->received_authenticate)
  967. ERR("We already got one!");
  968. if (conn->handshake_state->authenticated) {
  969. /* Should be impossible given other checks */
  970. ERR("The peer is already authenticated");
  971. }
  972. if (! conn->handshake_state->received_cert_cell)
  973. ERR("We never got a cert cell");
  974. if (conn->handshake_state->auth_cert == NULL)
  975. ERR("We never got an authentication certificate");
  976. if (conn->handshake_state->id_cert == NULL)
  977. ERR("We never got an identity certificate");
  978. if (cell->payload_len < 4)
  979. ERR("Cell was way too short");
  980. auth = cell->payload;
  981. {
  982. uint16_t type = ntohs(get_uint16(auth));
  983. uint16_t len = ntohs(get_uint16(auth+2));
  984. if (4 + len > cell->payload_len)
  985. ERR("Authenticator was truncated");
  986. if (type != AUTHTYPE_RSA_SHA256_TLSSECRET)
  987. ERR("Authenticator type was not recognized");
  988. auth += 4;
  989. authlen = len;
  990. }
  991. if (authlen < V3_AUTH_BODY_LEN + 1)
  992. ERR("Authenticator was too short");
  993. if (connection_or_compute_authenticate_cell_body(
  994. conn, expected, sizeof(expected), NULL, 1) < 0)
  995. ERR("Couldn't compute expected AUTHENTICATE cell body");
  996. if (tor_memneq(expected, auth, sizeof(expected)))
  997. ERR("Some field in the AUTHENTICATE cell body was not as expected");
  998. {
  999. crypto_pk_env_t *pk = tor_tls_cert_get_key(
  1000. conn->handshake_state->auth_cert);
  1001. char d[DIGEST256_LEN];
  1002. char *signed_data;
  1003. size_t keysize;
  1004. int signed_len;
  1005. crypto_digest256(d, (char*)auth, V3_AUTH_BODY_LEN, DIGEST_SHA256);
  1006. keysize = crypto_pk_keysize(pk);
  1007. signed_data = tor_malloc(keysize);
  1008. signed_len = crypto_pk_public_checksig(pk, signed_data, keysize,
  1009. (char*)auth + V3_AUTH_BODY_LEN,
  1010. authlen - V3_AUTH_BODY_LEN);
  1011. if (signed_len < 0) {
  1012. tor_free(signed_data);
  1013. ERR("Signature wasn't valid");
  1014. }
  1015. if (signed_len < DIGEST256_LEN) {
  1016. tor_free(signed_data);
  1017. ERR("Not enough data was signed");
  1018. }
  1019. /* Note that we deliberately allow *more* than DIGEST256_LEN bytes here,
  1020. * in case they're later used to hold a SHA3 digest or something. */
  1021. if (tor_memneq(signed_data, d, DIGEST256_LEN)) {
  1022. tor_free(signed_data);
  1023. ERR("Signature did not match data to be signed.");
  1024. }
  1025. tor_free(signed_data);
  1026. }
  1027. /* Okay, we are authenticated. */
  1028. conn->handshake_state->received_authenticate = 1;
  1029. conn->handshake_state->authenticated = 1;
  1030. conn->handshake_state->digest_received_data = 0;
  1031. {
  1032. crypto_pk_env_t *identity_rcvd =
  1033. tor_tls_cert_get_key(conn->handshake_state->id_cert);
  1034. const digests_t *id_digests =
  1035. tor_cert_get_id_digests(conn->handshake_state->id_cert);
  1036. memcpy(conn->handshake_state->authenticated_peer_id,
  1037. id_digests->d[DIGEST_SHA1], DIGEST_LEN);
  1038. connection_or_set_circid_type(conn, identity_rcvd);
  1039. crypto_free_pk_env(identity_rcvd);
  1040. connection_or_init_conn_from_address(conn,
  1041. &conn->_base.addr,
  1042. conn->_base.port,
  1043. (const char*)conn->handshake_state->authenticated_peer_id,
  1044. 0);
  1045. }
  1046. #undef ERR
  1047. }