command.c 41 KB

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