command.c 47 KB

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