command.c 49 KB

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