command.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. return;
  676. }
  677. } else {
  678. /* we're the server. If the client never authenticated, we have
  679. some housekeeping to do.*/
  680. if (!conn->handshake_state->authenticated) {
  681. tor_assert(tor_digest_is_zero(
  682. (const char*)conn->handshake_state->authenticated_peer_id));
  683. connection_or_set_circid_type(conn, NULL);
  684. connection_or_init_conn_from_address(conn,
  685. &conn->_base.addr,
  686. conn->_base.port,
  687. (const char*)conn->handshake_state->authenticated_peer_id,
  688. 0);
  689. }
  690. }
  691. }
  692. /* Decode the cell. */
  693. timestamp = ntohl(get_uint32(cell->payload));
  694. if (labs(now - conn->handshake_state->sent_versions_at) < 180) {
  695. apparent_skew = now - timestamp;
  696. }
  697. my_addr_type = (uint8_t) cell->payload[4];
  698. my_addr_len = (uint8_t) cell->payload[5];
  699. my_addr_ptr = (uint8_t*) cell->payload + 6;
  700. end = cell->payload + CELL_PAYLOAD_SIZE;
  701. cp = cell->payload + 6 + my_addr_len;
  702. if (cp >= end) {
  703. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  704. "Addresses too long in netinfo cell; closing connection.");
  705. connection_mark_for_close(TO_CONN(conn));
  706. return;
  707. } else if (my_addr_type == RESOLVED_TYPE_IPV4 && my_addr_len == 4) {
  708. my_apparent_addr = ntohl(get_uint32(my_addr_ptr));
  709. }
  710. n_other_addrs = (uint8_t) *cp++;
  711. while (n_other_addrs && cp < end-2) {
  712. /* Consider all the other addresses; if any matches, this connection is
  713. * "canonical." */
  714. tor_addr_t addr;
  715. const uint8_t *next =
  716. decode_address_from_payload(&addr, cp, (int)(end-cp));
  717. if (next == NULL) {
  718. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  719. "Bad address in netinfo cell; closing connection.");
  720. connection_mark_for_close(TO_CONN(conn));
  721. return;
  722. }
  723. if (tor_addr_eq(&addr, &conn->real_addr)) {
  724. conn->is_canonical = 1;
  725. break;
  726. }
  727. cp = next;
  728. --n_other_addrs;
  729. }
  730. /* Act on apparent skew. */
  731. /** Warn when we get a netinfo skew with at least this value. */
  732. #define NETINFO_NOTICE_SKEW 3600
  733. if (labs(apparent_skew) > NETINFO_NOTICE_SKEW &&
  734. router_get_by_id_digest(conn->identity_digest)) {
  735. char dbuf[64];
  736. int severity;
  737. /*XXXX be smarter about when everybody says we are skewed. */
  738. if (router_digest_is_trusted_dir(conn->identity_digest))
  739. severity = LOG_WARN;
  740. else
  741. severity = LOG_INFO;
  742. format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
  743. log_fn(severity, LD_GENERAL, "Received NETINFO cell with skewed time from "
  744. "server at %s:%d. It seems that our clock is %s by %s, or "
  745. "that theirs is %s. Tor requires an accurate clock to work: "
  746. "please check your time and date settings.",
  747. conn->_base.address, (int)conn->_base.port,
  748. apparent_skew>0 ? "ahead" : "behind", dbuf,
  749. apparent_skew>0 ? "behind" : "ahead");
  750. if (severity == LOG_WARN) /* only tell the controller if an authority */
  751. control_event_general_status(LOG_WARN,
  752. "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
  753. apparent_skew,
  754. conn->_base.address, conn->_base.port);
  755. }
  756. /* XXX maybe act on my_apparent_addr, if the source is sufficiently
  757. * trustworthy. */
  758. (void)my_apparent_addr;
  759. if (connection_or_set_state_open(conn)<0) {
  760. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got good NETINFO cell from %s:%d; but "
  761. "was unable to make the OR connection become open.",
  762. safe_str_client(conn->_base.address),
  763. conn->_base.port);
  764. connection_mark_for_close(TO_CONN(conn));
  765. } else {
  766. log_info(LD_OR, "Got good NETINFO cell from %s:%d; OR connection is now "
  767. "open, using protocol version %d. Its ID digest is %s",
  768. safe_str_client(conn->_base.address),
  769. conn->_base.port, (int)conn->link_proto,
  770. hex_str(conn->identity_digest, DIGEST_LEN));
  771. }
  772. assert_connection_ok(TO_CONN(conn),time(NULL));
  773. }
  774. /** Process a CERT cell from an OR connection.
  775. *
  776. * If the other side should not have sent us a CERT cell, or the cell is
  777. * malformed, or it is supposed to authenticate the TLS key but it doesn't,
  778. * then mark the connection.
  779. *
  780. * If the cell has a good cert chain and we're doing a v3 handshake, then
  781. * store the certificates in or_handshake_state. If this is the client side
  782. * of the connection, we then authenticate the server or mark the connection.
  783. * If it's the server side, wait for an AUTHENTICATE cell.
  784. */
  785. static void
  786. command_process_cert_cell(var_cell_t *cell, or_connection_t *conn)
  787. {
  788. #define ERR(s) \
  789. do { \
  790. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  791. "Received a bad CERT cell from %s:%d: %s", \
  792. safe_str(conn->_base.address), conn->_base.port, (s)); \
  793. connection_mark_for_close(TO_CONN(conn)); \
  794. goto err; \
  795. } while (0)
  796. tor_cert_t *link_cert = NULL;
  797. tor_cert_t *id_cert = NULL;
  798. tor_cert_t *auth_cert = NULL;
  799. uint8_t *ptr;
  800. int n_certs, i;
  801. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  802. ERR("We're not doing a v3 handshake!");
  803. if (conn->link_proto < 3)
  804. ERR("We're not using link protocol >= 3");
  805. if (conn->handshake_state->received_cert_cell)
  806. ERR("We already got one");
  807. if (conn->handshake_state->authenticated) {
  808. /* Should be unreachable, but let's make sure. */
  809. ERR("We're already authenticated!");
  810. }
  811. if (cell->payload_len < 1)
  812. ERR("It had no body");
  813. if (cell->circ_id)
  814. ERR("It had a nonzero circuit ID");
  815. n_certs = cell->payload[0];
  816. ptr = cell->payload + 1;
  817. for (i = 0; i < n_certs; ++i) {
  818. uint8_t cert_type;
  819. uint16_t cert_len;
  820. if (ptr + 3 > cell->payload + cell->payload_len) {
  821. goto truncated;
  822. }
  823. cert_type = *ptr;
  824. cert_len = ntohs(get_uint16(ptr+1));
  825. if (ptr + 3 + cert_len > cell->payload + cell->payload_len) {
  826. goto truncated;
  827. }
  828. if (cert_type == OR_CERT_TYPE_TLS_LINK ||
  829. cert_type == OR_CERT_TYPE_ID_1024 ||
  830. cert_type == OR_CERT_TYPE_AUTH_1024) {
  831. tor_cert_t *cert = tor_cert_decode(ptr + 3, cert_len);
  832. if (!cert) {
  833. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  834. "Received undecodable certificate in CERT cell from %s:%d",
  835. safe_str(conn->_base.address), conn->_base.port);
  836. } else {
  837. if (cert_type == OR_CERT_TYPE_TLS_LINK && !link_cert)
  838. link_cert = cert;
  839. else if (cert_type == OR_CERT_TYPE_ID_1024 && !id_cert)
  840. id_cert = cert;
  841. else if (cert_type == OR_CERT_TYPE_AUTH_1024 && !auth_cert)
  842. auth_cert = cert;
  843. else
  844. tor_cert_free(cert);
  845. }
  846. }
  847. ptr += 3 + cert_len;
  848. continue;
  849. truncated:
  850. ERR("It ends in the middle of a certificate");
  851. }
  852. if (conn->handshake_state->started_here) {
  853. if (! (id_cert && link_cert))
  854. ERR("The certs we wanted were missing");
  855. /* Okay. We should be able to check the certificates now. */
  856. if (! tor_tls_cert_matches_key(conn->tls, link_cert)) {
  857. ERR("The link certificate didn't match the TLS public key");
  858. }
  859. if (! tor_tls_cert_is_valid(link_cert, id_cert, 0))
  860. ERR("The link certificate was not valid");
  861. if (! tor_tls_cert_is_valid(id_cert, id_cert, 1))
  862. ERR("The ID certificate was not valid");
  863. conn->handshake_state->authenticated = 1;
  864. {
  865. crypto_pk_env_t *identity_rcvd = tor_tls_cert_get_key(id_cert);
  866. const digests_t *id_digests = tor_cert_get_id_digests(id_cert);
  867. memcpy(conn->handshake_state->authenticated_peer_id,
  868. id_digests->d[DIGEST_SHA1], DIGEST_LEN);
  869. connection_or_set_circid_type(conn, identity_rcvd);
  870. crypto_free_pk_env(identity_rcvd);
  871. }
  872. if (connection_or_client_learned_peer_id(conn,
  873. conn->handshake_state->authenticated_peer_id) < 0)
  874. ERR("Problem setting or checking peer id");
  875. log_info(LD_OR, "Got some good certifcates from %s:%d: Authenticated it.",
  876. safe_str(conn->_base.address), conn->_base.port);
  877. conn->handshake_state->id_cert = id_cert;
  878. id_cert = NULL;
  879. } else {
  880. if (! (id_cert && auth_cert))
  881. ERR("The certs we wanted were missing");
  882. /* Remember these certificates so we can check an AUTHENTICATE cell */
  883. conn->handshake_state->id_cert = id_cert;
  884. conn->handshake_state->auth_cert = auth_cert;
  885. if (! tor_tls_cert_is_valid(auth_cert, id_cert, 1))
  886. ERR("The authentication certificate was not valid");
  887. if (! tor_tls_cert_is_valid(id_cert, id_cert, 1))
  888. ERR("The ID certificate was not valid");
  889. log_info(LD_OR, "Got some good certifcates from %s:%d: "
  890. "Waiting for AUTHENTICATE.",
  891. safe_str(conn->_base.address), conn->_base.port);
  892. /* XXXX check more stuff? */
  893. id_cert = auth_cert = NULL;
  894. }
  895. conn->handshake_state->received_cert_cell = 1;
  896. err:
  897. tor_cert_free(id_cert);
  898. tor_cert_free(link_cert);
  899. tor_cert_free(auth_cert);
  900. #undef ERR
  901. }
  902. /** Process an AUTH_CHALLENGE cell from an OR connection.
  903. *
  904. * If we weren't supposed to get one (for example, because we're not the
  905. * originator of the connection), or it's ill-formed, or we aren't doing a v3
  906. * handshake, mark the connection. If the cell is well-formed but we don't
  907. * want to authenticate, just drop it. If the cell is well-formed *and* we
  908. * want to authenticate, send an AUTHENTICATE cell. */
  909. static void
  910. command_process_auth_challenge_cell(var_cell_t *cell, or_connection_t *conn)
  911. {
  912. int n_types, i, use_type = -1;
  913. uint8_t *cp;
  914. #define ERR(s) \
  915. do { \
  916. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  917. "Received a bad AUTH_CHALLENGE cell from %s:%d: %s", \
  918. safe_str(conn->_base.address), conn->_base.port, (s)); \
  919. connection_mark_for_close(TO_CONN(conn)); \
  920. return; \
  921. } while (0)
  922. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  923. ERR("We're not currently doing a v3 handshake");
  924. if (conn->link_proto < 3)
  925. ERR("We're not using link protocol >= 3");
  926. if (! conn->handshake_state->started_here)
  927. ERR("We didn't originate this connection");
  928. if (conn->handshake_state->received_auth_challenge)
  929. ERR("We already received one");
  930. if (! conn->handshake_state->received_cert_cell)
  931. ERR("We haven't gotten a CERTS cell yet");
  932. if (cell->payload_len < OR_AUTH_CHALLENGE_LEN + 2)
  933. ERR("It was too short");
  934. if (cell->circ_id)
  935. ERR("It had a nonzero circuit ID");
  936. n_types = ntohs(get_uint16(cell->payload + OR_AUTH_CHALLENGE_LEN));
  937. if (cell->payload_len < OR_AUTH_CHALLENGE_LEN + 2 + 2*n_types)
  938. ERR("It looks truncated");
  939. memcpy(conn->handshake_state->auth_challenge, cell->payload,
  940. OR_AUTH_CHALLENGE_LEN);
  941. /* Now see if there is an authentication type we can use */
  942. cp=cell->payload+OR_AUTH_CHALLENGE_LEN+2;
  943. for (i=0; i < n_types; ++i, cp += 2) {
  944. uint16_t authtype = ntohs(get_uint16(cp));
  945. if (authtype == AUTHTYPE_RSA_SHA256_TLSSECRET)
  946. use_type = authtype;
  947. }
  948. conn->handshake_state->received_auth_challenge = 1;
  949. if (use_type && public_server_mode(get_options())) {
  950. log_info(LD_OR, "Got an AUTH_CHALLENGE cell from %s:%d: Sending "
  951. "authentication",
  952. safe_str(conn->_base.address), conn->_base.port);
  953. if (connection_or_send_authenticate_cell(conn, use_type) < 0) {
  954. log_warn(LD_OR, "Couldn't send authenticate cell");
  955. connection_mark_for_close(TO_CONN(conn));
  956. return;
  957. }
  958. if (connection_or_send_netinfo(conn) < 0) {
  959. log_warn(LD_OR, "Couldn't send netinfo cell");
  960. connection_mark_for_close(TO_CONN(conn));
  961. return;
  962. }
  963. } else {
  964. log_info(LD_OR, "Got an AUTH_CHALLENGE cell from %s:%d: Not authenticating",
  965. safe_str(conn->_base.address), conn->_base.port);
  966. }
  967. #undef ERR
  968. }
  969. /** Process an AUTHENTICATE cell from an OR connection.
  970. *
  971. * If it's ill-formed or we weren't supposed to get one or we're not doing a
  972. * v3 handshake, then mark the connection. If it does not authenticate the
  973. * other side of the connection successfully (because it isn't signed right,
  974. * we didn't get a CERT cell, etc) mark the connection. Otherwise, accept
  975. * the identity of the router on the other side of the connection.
  976. */
  977. static void
  978. command_process_authenticate_cell(var_cell_t *cell, or_connection_t *conn)
  979. {
  980. uint8_t expected[V3_AUTH_FIXED_PART_LEN];
  981. const uint8_t *auth;
  982. int authlen;
  983. #define ERR(s) \
  984. do { \
  985. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  986. "Received a bad AUTHETNICATE cell from %s:%d: %s", \
  987. safe_str(conn->_base.address), conn->_base.port, (s)); \
  988. connection_mark_for_close(TO_CONN(conn)); \
  989. return; \
  990. } while (0)
  991. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  992. ERR("We're not doing a v3 handshake");
  993. if (conn->link_proto < 3)
  994. ERR("We're not using link protocol >= 3");
  995. if (conn->handshake_state->started_here)
  996. ERR("We originated this connection");
  997. if (conn->handshake_state->received_authenticate)
  998. ERR("We already got one!");
  999. if (conn->handshake_state->authenticated) {
  1000. /* Should be impossible given other checks */
  1001. ERR("The peer is already authenticated");
  1002. }
  1003. if (! conn->handshake_state->received_cert_cell)
  1004. ERR("We never got a cert cell");
  1005. if (conn->handshake_state->auth_cert == NULL)
  1006. ERR("We never got an authentication certificate");
  1007. if (conn->handshake_state->id_cert == NULL)
  1008. ERR("We never got an identity certificate");
  1009. if (cell->payload_len < 4)
  1010. ERR("Cell was way too short");
  1011. auth = cell->payload;
  1012. {
  1013. uint16_t type = ntohs(get_uint16(auth));
  1014. uint16_t len = ntohs(get_uint16(auth+2));
  1015. if (4 + len > cell->payload_len)
  1016. ERR("Authenticator was truncated");
  1017. if (type != AUTHTYPE_RSA_SHA256_TLSSECRET)
  1018. ERR("Authenticator type was not recognized");
  1019. auth += 4;
  1020. authlen = len;
  1021. }
  1022. if (authlen < V3_AUTH_BODY_LEN + 1)
  1023. ERR("Authenticator was too short");
  1024. if (connection_or_compute_authenticate_cell_body(
  1025. conn, expected, sizeof(expected), NULL, 1) < 0)
  1026. ERR("Couldn't compute expected AUTHENTICATE cell body");
  1027. if (tor_memneq(expected, auth, sizeof(expected)))
  1028. ERR("Some field in the AUTHENTICATE cell body was not as expected");
  1029. {
  1030. crypto_pk_env_t *pk = tor_tls_cert_get_key(
  1031. conn->handshake_state->auth_cert);
  1032. char d[DIGEST256_LEN];
  1033. char *signed_data;
  1034. size_t keysize;
  1035. int signed_len;
  1036. crypto_digest256(d, (char*)auth, V3_AUTH_BODY_LEN, DIGEST_SHA256);
  1037. keysize = crypto_pk_keysize(pk);
  1038. signed_data = tor_malloc(keysize);
  1039. signed_len = crypto_pk_public_checksig(pk, signed_data, keysize,
  1040. (char*)auth + V3_AUTH_BODY_LEN,
  1041. authlen - V3_AUTH_BODY_LEN);
  1042. if (signed_len < 0) {
  1043. tor_free(signed_data);
  1044. ERR("Signature wasn't valid");
  1045. }
  1046. if (signed_len < DIGEST256_LEN) {
  1047. tor_free(signed_data);
  1048. ERR("Not enough data was signed");
  1049. }
  1050. /* Note that we deliberately allow *more* than DIGEST256_LEN bytes here,
  1051. * in case they're later used to hold a SHA3 digest or something. */
  1052. if (tor_memneq(signed_data, d, DIGEST256_LEN)) {
  1053. tor_free(signed_data);
  1054. ERR("Signature did not match data to be signed.");
  1055. }
  1056. tor_free(signed_data);
  1057. }
  1058. /* Okay, we are authenticated. */
  1059. conn->handshake_state->received_authenticate = 1;
  1060. conn->handshake_state->authenticated = 1;
  1061. conn->handshake_state->digest_received_data = 0;
  1062. {
  1063. crypto_pk_env_t *identity_rcvd =
  1064. tor_tls_cert_get_key(conn->handshake_state->id_cert);
  1065. const digests_t *id_digests =
  1066. tor_cert_get_id_digests(conn->handshake_state->id_cert);
  1067. memcpy(conn->handshake_state->authenticated_peer_id,
  1068. id_digests->d[DIGEST_SHA1], DIGEST_LEN);
  1069. connection_or_set_circid_type(conn, identity_rcvd);
  1070. crypto_free_pk_env(identity_rcvd);
  1071. connection_or_init_conn_from_address(conn,
  1072. &conn->_base.addr,
  1073. conn->_base.port,
  1074. (const char*)conn->handshake_state->authenticated_peer_id,
  1075. 0);
  1076. log_info(LD_OR, "Got an AUTHENTICATE cell from %s:%d: Looks good.",
  1077. safe_str(conn->_base.address), conn->_base.port);
  1078. }
  1079. #undef ERR
  1080. }