command.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char command_c_id[] =
  7. "$Id$";
  8. /**
  9. * \file command.c
  10. * \brief Functions for processing incoming cells.
  11. **/
  12. /* In-points to command.c:
  13. *
  14. * - command_process_cell(), called from
  15. * connection_or_process_cells_from_inbuf() in connection_or.c
  16. */
  17. #include "or.h"
  18. /** Keep statistics about how many of each type of cell we've received. */
  19. uint64_t stats_n_padding_cells_processed = 0;
  20. uint64_t stats_n_create_cells_processed = 0;
  21. uint64_t stats_n_created_cells_processed = 0;
  22. uint64_t stats_n_relay_cells_processed = 0;
  23. uint64_t stats_n_destroy_cells_processed = 0;
  24. uint64_t stats_n_versions_cells_processed = 0;
  25. uint64_t stats_n_netinfo_cells_processed = 0;
  26. uint64_t stats_n_cert_cells_processed = 0;
  27. uint64_t stats_n_link_auth_cells_processed = 0;
  28. /* These are the main functions for processing cells */
  29. static void command_process_create_cell(cell_t *cell, or_connection_t *conn);
  30. static void command_process_created_cell(cell_t *cell, or_connection_t *conn);
  31. static void command_process_relay_cell(cell_t *cell, or_connection_t *conn);
  32. static void command_process_destroy_cell(cell_t *cell, or_connection_t *conn);
  33. static void command_process_versions_cell(cell_t *cell, or_connection_t *conn);
  34. static void command_process_netinfo_cell(cell_t *cell, or_connection_t *conn);
  35. static void command_process_cert_cell(cell_t *cell, or_connection_t *conn);
  36. static void command_process_link_auth_cell(cell_t *cell,or_connection_t *conn);
  37. #ifdef KEEP_TIMING_STATS
  38. /** This is a wrapper function around the actual function that processes the
  39. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  40. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  41. */
  42. static void
  43. command_time_process_cell(cell_t *cell, or_connection_t *conn, int *time,
  44. void (*func)(cell_t *, or_connection_t *))
  45. {
  46. struct timeval start, end;
  47. long time_passed;
  48. tor_gettimeofday(&start);
  49. (*func)(cell, conn);
  50. tor_gettimeofday(&end);
  51. time_passed = tv_udiff(&start, &end) ;
  52. if (time_passed > 10000) { /* more than 10ms */
  53. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  54. }
  55. if (time_passed < 0) {
  56. log_info(LD_GENERAL,"That call took us back in time!");
  57. time_passed = 0;
  58. }
  59. *time += time_passed;
  60. }
  61. #endif
  62. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  63. * statistics about how many of each cell we've processed so far
  64. * this second, and the total number of microseconds it took to
  65. * process each type of cell.
  66. */
  67. void
  68. command_process_cell(cell_t *cell, or_connection_t *conn)
  69. {
  70. #ifdef KEEP_TIMING_STATS
  71. /* how many of each cell have we seen so far this second? needs better
  72. * name. */
  73. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  74. /* how long has it taken to process each type of cell? */
  75. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  76. static time_t current_second = 0; /* from previous calls to time */
  77. time_t now = time(NULL);
  78. if (now > current_second) { /* the second has rolled over */
  79. /* print stats */
  80. log_info(LD_OR,
  81. "At end of second: %d creates (%d ms), %d createds (%d ms), "
  82. "%d relays (%d ms), %d destroys (%d ms)",
  83. num_create, create_time/1000,
  84. num_created, created_time/1000,
  85. num_relay, relay_time/1000,
  86. num_destroy, destroy_time/1000);
  87. /* zero out stats */
  88. num_create = num_created = num_relay = num_destroy = 0;
  89. create_time = created_time = relay_time = destroy_time = 0;
  90. /* remember which second it is, for next time */
  91. current_second = now;
  92. }
  93. #endif
  94. #ifdef KEEP_TIMING_STATS
  95. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  96. ++num ## tp; \
  97. command_time_process_cell(cl, cn, & tp ## time , \
  98. command_process_ ## tp ## _cell); \
  99. } STMT_END
  100. #else
  101. #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
  102. #endif
  103. /*XXXX020 reject all but VERSIONS, NETINFO, CERT, LINK_AUTH when
  104. * handshaking. */
  105. switch (cell->command) {
  106. case CELL_PADDING:
  107. ++stats_n_padding_cells_processed;
  108. /* do nothing */
  109. break;
  110. case CELL_CREATE:
  111. case CELL_CREATE_FAST:
  112. ++stats_n_create_cells_processed;
  113. PROCESS_CELL(create, cell, conn);
  114. break;
  115. case CELL_CREATED:
  116. case CELL_CREATED_FAST:
  117. ++stats_n_created_cells_processed;
  118. PROCESS_CELL(created, cell, conn);
  119. break;
  120. case CELL_RELAY:
  121. ++stats_n_relay_cells_processed;
  122. PROCESS_CELL(relay, cell, conn);
  123. break;
  124. case CELL_DESTROY:
  125. ++stats_n_destroy_cells_processed;
  126. PROCESS_CELL(destroy, cell, conn);
  127. break;
  128. case CELL_VERSIONS:
  129. ++stats_n_versions_cells_processed;
  130. PROCESS_CELL(versions, cell, conn);
  131. break;
  132. case CELL_NETINFO:
  133. ++stats_n_netinfo_cells_processed;
  134. PROCESS_CELL(netinfo, cell, conn);
  135. break;
  136. case CELL_CERT:
  137. ++stats_n_cert_cells_processed;
  138. PROCESS_CELL(cert, cell, conn);
  139. break;
  140. case CELL_LINK_AUTH:
  141. ++stats_n_link_auth_cells_processed;
  142. PROCESS_CELL(link_auth, cell, conn);
  143. break;
  144. default:
  145. log_fn(LOG_INFO, LD_PROTOCOL,
  146. "Cell of unknown type (%d) received. Dropping.", cell->command);
  147. break;
  148. }
  149. }
  150. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a
  151. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  152. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  153. * picked up again when the cpuworker finishes decrypting it.
  154. */
  155. static void
  156. command_process_create_cell(cell_t *cell, or_connection_t *conn)
  157. {
  158. or_circuit_t *circ;
  159. int id_is_high;
  160. if (we_are_hibernating()) {
  161. log_info(LD_OR,
  162. "Received create cell but we're shutting down. Sending back "
  163. "destroy.");
  164. connection_or_send_destroy(cell->circ_id, conn,
  165. END_CIRC_REASON_HIBERNATING);
  166. return;
  167. }
  168. if (!server_mode(get_options())) {
  169. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  170. "Received create cell (type %d) from %s:%d, but we're a client. "
  171. "Sending back a destroy.",
  172. (int)cell->command, conn->_base.address, conn->_base.port);
  173. connection_or_send_destroy(cell->circ_id, conn,
  174. END_CIRC_REASON_TORPROTOCOL);
  175. return;
  176. }
  177. /* If the high bit of the circuit ID is not as expected, close the
  178. * circ. */
  179. id_is_high = cell->circ_id & (1<<15);
  180. if ((id_is_high && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
  181. (!id_is_high && conn->circ_id_type == CIRC_ID_TYPE_LOWER)) {
  182. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  183. "Received create cell with unexpected circ_id %d. Closing.",
  184. cell->circ_id);
  185. connection_or_send_destroy(cell->circ_id, conn,
  186. END_CIRC_REASON_TORPROTOCOL);
  187. return;
  188. }
  189. if (circuit_get_by_circid_orconn(cell->circ_id, conn)) {
  190. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  191. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  192. "Received CREATE cell (circID %d) for known circ. "
  193. "Dropping (age %d).",
  194. cell->circ_id, (int)(time(NULL) - conn->_base.timestamp_created));
  195. if (router)
  196. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  197. "Details: nickname \"%s\", platform %s.",
  198. router->nickname, escaped(router->platform));
  199. return;
  200. }
  201. circ = or_circuit_new(cell->circ_id, conn);
  202. circ->_base.purpose = CIRCUIT_PURPOSE_OR;
  203. circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
  204. if (cell->command == CELL_CREATE) {
  205. circ->_base.onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  206. memcpy(circ->_base.onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  207. /* hand it off to the cpuworkers, and then return. */
  208. if (assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  209. log_warn(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  210. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  211. return;
  212. }
  213. log_debug(LD_OR,"success: handed off onionskin.");
  214. } else {
  215. /* This is a CREATE_FAST cell; we can handle it immediately without using
  216. * a CPU worker. */
  217. char keys[CPATH_KEY_MATERIAL_LEN];
  218. char reply[DIGEST_LEN*2];
  219. tor_assert(cell->command == CELL_CREATE_FAST);
  220. if (fast_server_handshake(cell->payload, reply, keys, sizeof(keys))<0) {
  221. log_warn(LD_OR,"Failed to generate key material. Closing.");
  222. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  223. return;
  224. }
  225. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  226. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  227. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  228. return;
  229. }
  230. }
  231. }
  232. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>.
  233. * Find the circuit
  234. * that it's intended for. If we're not the origin of the circuit, package
  235. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  236. * are the origin of the circuit, send it to circuit_finish_handshake() to
  237. * finish processing keys, and then call circuit_send_next_onion_skin() to
  238. * extend to the next hop in the circuit if necessary.
  239. */
  240. static void
  241. command_process_created_cell(cell_t *cell, or_connection_t *conn)
  242. {
  243. circuit_t *circ;
  244. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  245. if (!circ) {
  246. log_info(LD_OR,
  247. "(circID %d) unknown circ (probably got a destroy earlier). "
  248. "Dropping.", cell->circ_id);
  249. return;
  250. }
  251. if (circ->n_circ_id != cell->circ_id) {
  252. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  253. "got created cell from Tor client? Closing.");
  254. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  255. return;
  256. }
  257. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  258. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  259. int err_reason = 0;
  260. log_debug(LD_OR,"at OP. Finishing handshake.");
  261. if ((err_reason = circuit_finish_handshake(origin_circ, cell->command,
  262. cell->payload)) < 0) {
  263. log_warn(LD_OR,"circuit_finish_handshake failed.");
  264. circuit_mark_for_close(circ, -err_reason);
  265. return;
  266. }
  267. log_debug(LD_OR,"Moving to next skin.");
  268. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  269. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  270. /* XXX push this circuit_close lower */
  271. circuit_mark_for_close(circ, -err_reason);
  272. return;
  273. }
  274. } else { /* pack it into an extended relay cell, and send it. */
  275. log_debug(LD_OR,
  276. "Converting created cell to extended relay cell, sending.");
  277. relay_send_command_from_edge(0, circ, RELAY_COMMAND_EXTENDED,
  278. cell->payload, ONIONSKIN_REPLY_LEN,
  279. NULL);
  280. }
  281. }
  282. /** Process a 'relay' <b>cell</b> that just arrived from <b>conn</b>. Make sure
  283. * it came in with a recognized circ_id. Pass it on to
  284. * circuit_receive_relay_cell() for actual processing.
  285. */
  286. static void
  287. command_process_relay_cell(cell_t *cell, or_connection_t *conn)
  288. {
  289. circuit_t *circ;
  290. int reason, direction;
  291. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  292. if (!circ) {
  293. log_debug(LD_OR,
  294. "unknown circuit %d on connection from %s:%d. Dropping.",
  295. cell->circ_id, conn->_base.address, conn->_base.port);
  296. return;
  297. }
  298. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  299. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  300. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  301. return;
  302. }
  303. if (CIRCUIT_IS_ORIGIN(circ)) {
  304. /* if we're a server and treating connections with recent local
  305. * traffic better, then this is one of them. */
  306. conn->client_used = time(NULL);
  307. }
  308. if (!CIRCUIT_IS_ORIGIN(circ) &&
  309. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  310. direction = CELL_DIRECTION_OUT;
  311. else
  312. direction = CELL_DIRECTION_IN;
  313. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  314. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  315. "(%s) failed. Closing.",
  316. direction==CELL_DIRECTION_OUT?"forward":"backward");
  317. circuit_mark_for_close(circ, -reason);
  318. }
  319. }
  320. /** Process a 'destroy' <b>cell</b> that just arrived from
  321. * <b>conn</b>. Find the circ that it refers to (if any).
  322. *
  323. * If the circ is in state
  324. * onionskin_pending, then call onion_pending_remove() to remove it
  325. * from the pending onion list (note that if it's already being
  326. * processed by the cpuworker, it won't be in the list anymore; but
  327. * when the cpuworker returns it, the circuit will be gone, and the
  328. * cpuworker response will be dropped).
  329. *
  330. * Then mark the circuit for close (which marks all edges for close,
  331. * and passes the destroy cell onward if necessary).
  332. */
  333. static void
  334. command_process_destroy_cell(cell_t *cell, or_connection_t *conn)
  335. {
  336. circuit_t *circ;
  337. int reason;
  338. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  339. reason = (uint8_t)cell->payload[0];
  340. if (!circ) {
  341. log_info(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  342. cell->circ_id, conn->_base.address, conn->_base.port);
  343. return;
  344. }
  345. log_debug(LD_OR,"Received for circID %d.",cell->circ_id);
  346. if (!CIRCUIT_IS_ORIGIN(circ) &&
  347. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  348. /* the destroy came from behind */
  349. circuit_set_p_circid_orconn(TO_OR_CIRCUIT(circ), 0, NULL);
  350. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  351. } else { /* the destroy came from ahead */
  352. circuit_set_n_circid_orconn(circ, 0, NULL);
  353. if (CIRCUIT_IS_ORIGIN(circ)) {
  354. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  355. } else {
  356. char payload[1];
  357. log_debug(LD_OR, "Delivering 'truncated' back.");
  358. payload[0] = (char)reason;
  359. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  360. payload, sizeof(payload), NULL);
  361. }
  362. }
  363. }
  364. /** Process a 'versions' cell. The current link protocol version must be 0
  365. * to indicate that no version has yet been negotiated. DOCDOC say more. */
  366. static void
  367. command_process_versions_cell(cell_t *cell, or_connection_t *conn)
  368. {
  369. uint16_t versionslen;
  370. int highest_supported_version = 0;
  371. const char *cp, *end;
  372. if (conn->link_proto != 0 ||
  373. conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING ||
  374. (conn->handshake_state && conn->handshake_state->received_versions)) {
  375. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  376. "Received a VERSIONS cell on a connection with its version "
  377. "already set to %d; dropping", (int) conn->link_proto);
  378. return;
  379. }
  380. tor_assert(conn->handshake_state);
  381. versionslen = ntohs(get_uint16(cell->payload));
  382. end = cell->payload + 2 + versionslen;
  383. if (end > cell->payload + CELL_PAYLOAD_SIZE)
  384. end = cell->payload + CELL_PAYLOAD_SIZE; /*XXXX020 warn?*/
  385. for (cp = cell->payload + 2; cp < end; ++cp) {
  386. uint8_t v = *cp;
  387. if (v == 1) {
  388. if (v > highest_supported_version)
  389. highest_supported_version = v;
  390. }
  391. }
  392. if (!highest_supported_version) {
  393. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  394. "Couldn't find a version in common; defaulting to v1.");
  395. /*XXXX020 just break the connection?*/
  396. conn->link_proto = 1;
  397. return;
  398. }
  399. conn->link_proto = highest_supported_version;
  400. conn->handshake_state->received_versions = 1;
  401. if (highest_supported_version >= 2)
  402. connection_or_send_netinfo(conn);
  403. }
  404. /** Process a 'netinfo' cell. DOCDOC say more. */
  405. static void
  406. command_process_netinfo_cell(cell_t *cell, or_connection_t *conn)
  407. {
  408. time_t timestamp;
  409. uint8_t my_addr_type;
  410. uint8_t my_addr_len;
  411. const char *my_addr_ptr;
  412. const char *cp, *end;
  413. uint8_t n_other_addrs;
  414. time_t now = time(NULL);
  415. if (conn->link_proto < 2) {
  416. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  417. "Received a NETINFO cell on %s connection; dropping.",
  418. conn->link_proto == 0 ? "non-versioned" : "a v1");
  419. return;
  420. }
  421. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  422. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  423. "Received a NETINFO cell on a non-handshaking; dropping.");
  424. return;
  425. }
  426. tor_assert(conn->handshake_state &&
  427. conn->handshake_state->received_versions);
  428. if (conn->handshake_state->received_netinfo) {
  429. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  430. "Received a duplicate NETINFO cell; dropping.");
  431. return;
  432. }
  433. /* Decode the cell. */
  434. timestamp = ntohl(get_uint32(cell->payload));
  435. if (abs(now - conn->handshake_state->sent_versions_at) < 180) {
  436. conn->handshake_state->apparent_skew = now - timestamp;
  437. }
  438. my_addr_type = (uint8_t) cell->payload[4];
  439. my_addr_len = (uint8_t) cell->payload[5];
  440. my_addr_ptr = cell->payload + 6;
  441. end = cell->payload + CELL_PAYLOAD_SIZE;
  442. cp = cell->payload + 6 + my_addr_len;
  443. if (cp >= end) {
  444. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  445. "Address too long in netinfo cell; dropping.");
  446. /*XXXX020 reject and break OR conn! */
  447. return;
  448. } else if (my_addr_type == RESOLVED_TYPE_IPV4 && my_addr_len == 4) {
  449. conn->handshake_state->my_apparent_addr = ntohl(get_uint32(my_addr_ptr));
  450. }
  451. n_other_addrs = (uint8_t) *cp++;
  452. while (n_other_addrs && cp < end-2) {
  453. /* Consider all the other addresses; if any matches, this connection is
  454. * "canonical." */
  455. uint8_t other_addr_type = (uint8_t) *cp++;
  456. uint8_t other_addr_len = (uint8_t) *cp++;
  457. if (cp + other_addr_len >= end)
  458. break; /*XXXX020 protocol warn. */
  459. if (other_addr_type == RESOLVED_TYPE_IPV4 && other_addr_len == 4) {
  460. uint32_t addr = ntohl(get_uint32(cp));
  461. if (addr == conn->real_addr) {
  462. conn->handshake_state->apparently_canonical = 1;
  463. break;
  464. }
  465. }
  466. cp += other_addr_len;
  467. --n_other_addrs;
  468. }
  469. conn->handshake_state->received_netinfo = 1;
  470. }
  471. /*XXXX020 move to connection_or.c */
  472. /** DOCDOC Called when we're done authenticating; act on stuff we
  473. * learned in netinfo. */
  474. void
  475. connection_or_act_on_netinfo(or_connection_t *conn)
  476. {
  477. long delta;
  478. if (!conn->handshake_state)
  479. return;
  480. tor_assert(conn->handshake_state->authenticated != 0);
  481. delta = conn->handshake_state->apparent_skew;
  482. /*XXXX020 magic number 3600 */
  483. if (abs(delta) > 3600 &&
  484. router_get_by_digest(conn->identity_digest)) {
  485. char dbuf[64];
  486. /*XXXX020 not always warn!*/
  487. format_time_interval(dbuf, sizeof(dbuf), delta);
  488. log_fn(LOG_WARN, LD_HTTP, "Received NETINFO cell with skewed time from "
  489. "server at %s:%d. It seems that our clock is %s by %s, or "
  490. "that theirs is %s. Tor requires an accurate clock to work: "
  491. "please check your time and date settings.",
  492. conn->_base.address, (int)conn->_base.port,
  493. delta>0 ? "ahead" : "behind", dbuf,
  494. delta>0 ? "behind" : "ahead");
  495. control_event_general_status(LOG_WARN,
  496. "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
  497. delta, conn->_base.address, conn->_base.port);
  498. }
  499. /* XXX020 possibly, learn my address from my_apparent_addr */
  500. if (conn->handshake_state->apparently_canonical) {
  501. conn->is_canonical = 1;
  502. }
  503. }
  504. static void
  505. command_process_cert_cell(cell_t *cell, or_connection_t *conn)
  506. {
  507. (void) cell;
  508. (void) conn;
  509. /* Parse certs. */
  510. /* Verify that identity cert has signed peer cert in SSL, or
  511. * peer cert in the cell. */
  512. /* Verify that identity cert is self-signed. */
  513. /* Learn ID digest. */
  514. /* Learn cert digests. */
  515. /* Remember peer cert public key. */
  516. /* set received_certs. */
  517. }
  518. #define LINK_AUTH_STRING "Tor initiator certificate verification"
  519. /** DOCDOC */
  520. static void
  521. command_process_link_auth_cell(cell_t *cell, or_connection_t *conn)
  522. {
  523. or_handshake_state_t *s;
  524. char hmac[DIGEST_LEN];
  525. size_t sig_len;
  526. const char *sig;
  527. char *checked = NULL;
  528. int checked_len;
  529. tor_assert(conn);
  530. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  531. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  532. "Received a LINK_AUTH cell on connection in the wrong state; "
  533. "dropping.");
  534. return;
  535. }
  536. s = conn->handshake_state;
  537. tor_assert(s);
  538. if (s->started_here) {
  539. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  540. "Got a LINK_AUTH cell from a server; closing the connection.");
  541. goto err;
  542. }
  543. if (!s->received_netinfo || !s->received_versions || !s->received_certs) {
  544. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got a LINK_AUTH cell too early; "
  545. "closing the connection");
  546. goto err;
  547. }
  548. if (cell->payload[0] != 0x00) {
  549. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Unrecognized LINK_AUTH signature "
  550. "version; closing the connection");
  551. goto err;
  552. }
  553. connection_or_compute_link_auth_hmac(conn, hmac);
  554. tor_assert(s->signing_key);
  555. /*XXXX020 these two are wrong; fix when protocol is revised. */
  556. sig = cell->payload+1;
  557. sig_len = 128;
  558. checked = tor_malloc(crypto_pk_keysize(s->signing_key));
  559. checked_len = crypto_pk_public_checksig(s->signing_key,checked,sig,sig_len);
  560. if (checked_len < 0) {
  561. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad signature on LINK_AUTH cell; "
  562. "closing the connection");
  563. goto err;
  564. }
  565. if (checked_len != DIGEST_LEN) {
  566. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad length (%d) of signed material in "
  567. "LINK_AUTH cell; closing the connection", checked_len);
  568. goto err;
  569. }
  570. if (memcmp(checked, hmac, DIGEST_LEN) != 0) {
  571. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad signed data in LINK_AUTH cell; "
  572. "closing the connection.");
  573. goto err;
  574. }
  575. /* Okay, we're authenticated. */
  576. s->authenticated = 1;
  577. /* XXXX020 act on being authenticated: */
  578. return;
  579. err:
  580. tor_free(checked);
  581. connection_mark_for_close(TO_CONN(conn));
  582. }