command.c 44 KB

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