command.c 48 KB

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