command.c 44 KB

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