channeltls.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. /* * Copyright (c) 2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file channeltls.c
  5. * \brief channel_t concrete subclass using or_connection_t
  6. **/
  7. /*
  8. * Define this so channel.h gives us things only channel_t subclasses
  9. * should touch.
  10. */
  11. #define _TOR_CHANNEL_INTERNAL
  12. #include "or.h"
  13. #include "channel.h"
  14. #include "channeltls.h"
  15. #include "config.h"
  16. #include "connection.h"
  17. #include "connection_or.h"
  18. #include "control.h"
  19. #include "relay.h"
  20. #include "router.h"
  21. #include "routerlist.h"
  22. /** How many CELL_PADDING cells have we received, ever? */
  23. uint64_t stats_n_padding_cells_processed = 0;
  24. /** How many CELL_VERSIONS cells have we received, ever? */
  25. uint64_t stats_n_versions_cells_processed = 0;
  26. /** How many CELL_NETINFO cells have we received, ever? */
  27. uint64_t stats_n_netinfo_cells_processed = 0;
  28. /** How many CELL_VPADDING cells have we received, ever? */
  29. uint64_t stats_n_vpadding_cells_processed = 0;
  30. /** How many CELL_CERTS cells have we received, ever? */
  31. uint64_t stats_n_certs_cells_processed = 0;
  32. /** How many CELL_AUTH_CHALLENGE cells have we received, ever? */
  33. uint64_t stats_n_auth_challenge_cells_processed = 0;
  34. /** How many CELL_AUTHENTICATE cells have we received, ever? */
  35. uint64_t stats_n_authenticate_cells_processed = 0;
  36. /** How many CELL_AUTHORIZE cells have we received, ever? */
  37. uint64_t stats_n_authorize_cells_processed = 0;
  38. /** Active listener, if any */
  39. channel_listener_t *channel_tls_listener = NULL;
  40. /* channel_tls_t method declarations */
  41. static void channel_tls_close_method(channel_t *chan);
  42. static const char * channel_tls_describe_transport_method(channel_t *chan);
  43. static int
  44. channel_tls_get_remote_addr_method(channel_t *chan, tor_addr_t *addr_out);
  45. static const char *
  46. channel_tls_get_remote_descr_method(channel_t *chan, int req);
  47. static int channel_tls_has_queued_writes_method(channel_t *chan);
  48. static int channel_tls_is_canonical_method(channel_t *chan, int req);
  49. static int
  50. channel_tls_matches_extend_info_method(channel_t *chan,
  51. extend_info_t *extend_info);
  52. static int channel_tls_matches_target_method(channel_t *chan,
  53. const tor_addr_t *target);
  54. static int channel_tls_write_cell_method(channel_t *chan,
  55. cell_t *cell);
  56. static int channel_tls_write_packed_cell_method(channel_t *chan,
  57. packed_cell_t *packed_cell);
  58. static int channel_tls_write_var_cell_method(channel_t *chan,
  59. var_cell_t *var_cell);
  60. /* channel_listener_tls_t method declarations */
  61. static void channel_tls_listener_close_method(channel_listener_t *chan_l);
  62. static const char *
  63. channel_tls_listener_describe_transport_method(channel_listener_t *chan_l);
  64. /** Handle incoming cells for the handshake stuff here rather than
  65. * passing them on up. */
  66. static void channel_tls_process_versions_cell(var_cell_t *cell,
  67. channel_tls_t *tlschan);
  68. static void channel_tls_process_netinfo_cell(cell_t *cell,
  69. channel_tls_t *tlschan);
  70. static void channel_tls_process_certs_cell(var_cell_t *cell,
  71. channel_tls_t *tlschan);
  72. static void channel_tls_process_auth_challenge_cell(var_cell_t *cell,
  73. channel_tls_t *tlschan);
  74. static void channel_tls_process_authenticate_cell(var_cell_t *cell,
  75. channel_tls_t *tlschan);
  76. static int command_allowed_before_handshake(uint8_t command);
  77. static int enter_v3_handshake_with_cell(var_cell_t *cell,
  78. channel_tls_t *tlschan);
  79. /**
  80. * Start a new TLS channel
  81. *
  82. * Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
  83. * handshake with an OR with identity digest <b>id_digest</b>, and wrap
  84. * it in a channel_tls_t.
  85. */
  86. channel_t *
  87. channel_tls_connect(const tor_addr_t *addr, uint16_t port,
  88. const char *id_digest)
  89. {
  90. channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
  91. channel_t *chan = &(tlschan->_base);
  92. channel_init(chan);
  93. chan->magic = TLS_CHAN_MAGIC;
  94. chan->state = CHANNEL_STATE_OPENING;
  95. chan->close = channel_tls_close_method;
  96. chan->describe_transport = channel_tls_describe_transport_method;
  97. chan->get_remote_addr = channel_tls_get_remote_addr_method;
  98. chan->get_remote_descr = channel_tls_get_remote_descr_method;
  99. chan->has_queued_writes = channel_tls_has_queued_writes_method;
  100. chan->is_canonical = channel_tls_is_canonical_method;
  101. chan->matches_extend_info = channel_tls_matches_extend_info_method;
  102. chan->matches_target = channel_tls_matches_target_method;
  103. chan->write_cell = channel_tls_write_cell_method;
  104. chan->write_packed_cell = channel_tls_write_packed_cell_method;
  105. chan->write_var_cell = channel_tls_write_var_cell_method;
  106. log_debug(LD_CHANNEL,
  107. "In channel_tls_connect() for channel %p "
  108. "(global id " U64_FORMAT ")",
  109. tlschan,
  110. U64_PRINTF_ARG(chan->global_identifier));
  111. if (is_local_addr(addr)) channel_mark_local(chan);
  112. channel_mark_outgoing(chan);
  113. chan->active_circuit_pqueue = smartlist_new();
  114. chan->active_circuit_pqueue_last_recalibrated = cell_ewma_get_tick();
  115. /* Set up or_connection stuff */
  116. tlschan->conn = connection_or_connect(addr, port, id_digest, tlschan);
  117. /* connection_or_connect() will fill in tlschan->conn */
  118. if (!(tlschan->conn)) {
  119. chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR;
  120. channel_change_state(chan, CHANNEL_STATE_ERROR);
  121. goto err;
  122. }
  123. log_debug(LD_CHANNEL,
  124. "Got orconn %p for channel with global id " U64_FORMAT,
  125. tlschan->conn, U64_PRINTF_ARG(chan->global_identifier));
  126. goto done;
  127. err:
  128. smartlist_free(chan->active_circuit_pqueue);
  129. tor_free(tlschan);
  130. chan = NULL;
  131. done:
  132. /* If we got one, we should register it */
  133. if (chan) channel_register(chan);
  134. return chan;
  135. }
  136. /**
  137. * Return the current channel_tls_t listener
  138. *
  139. * Returns the current channel listener for incoming TLS connections, or
  140. * NULL if none has been established
  141. */
  142. channel_listener_t *
  143. channel_tls_get_listener(void)
  144. {
  145. return channel_tls_listener;
  146. }
  147. /**
  148. * Start a channel_tls_t listener if necessary
  149. *
  150. * Return the current channel_tls_t listener, or start one if we haven't yet,
  151. * and return that.
  152. */
  153. channel_listener_t *
  154. channel_tls_start_listener(void)
  155. {
  156. channel_listener_t *listener;
  157. if (!channel_tls_listener) {
  158. listener = tor_malloc_zero(sizeof(*listener));
  159. channel_init_listener(listener);
  160. listener->state = CHANNEL_LISTENER_STATE_LISTENING;
  161. listener->close = channel_tls_listener_close_method;
  162. listener->describe_transport =
  163. channel_tls_listener_describe_transport_method;
  164. channel_tls_listener = listener;
  165. log_debug(LD_CHANNEL,
  166. "Starting TLS channel listener %p with global id " U64_FORMAT,
  167. listener, U64_PRINTF_ARG(listener->global_identifier));
  168. channel_listener_register(listener);
  169. } else listener = channel_tls_listener;
  170. return listener;
  171. }
  172. /**
  173. * Free everything on shutdown
  174. *
  175. * Not much to do here, since channel_free_all() takes care of a lot, but let's
  176. * get rid of the listener.
  177. */
  178. void
  179. channel_tls_free_all(void)
  180. {
  181. log_debug(LD_CHANNEL,
  182. "Shutting down TLS channels...");
  183. if (channel_tls_listener) {
  184. channel_listener_unregister(channel_tls_listener);
  185. channel_listener_mark_for_close(channel_tls_listener);
  186. channel_listener_free(channel_tls_listener);
  187. channel_tls_listener = NULL;
  188. }
  189. log_debug(LD_CHANNEL,
  190. "Done shutting down TLS channels");
  191. }
  192. /**
  193. * Create a new channel around an incoming or_connection_t
  194. */
  195. channel_t *
  196. channel_tls_handle_incoming(or_connection_t *orconn)
  197. {
  198. channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
  199. channel_t *chan = &(tlschan->_base);
  200. tor_assert(orconn);
  201. tor_assert(!(orconn->chan));
  202. channel_init(chan);
  203. chan->magic = TLS_CHAN_MAGIC;
  204. chan->state = CHANNEL_STATE_OPENING;
  205. chan->close = channel_tls_close_method;
  206. chan->describe_transport = channel_tls_describe_transport_method;
  207. chan->get_remote_descr = channel_tls_get_remote_descr_method;
  208. chan->has_queued_writes = channel_tls_has_queued_writes_method;
  209. chan->is_canonical = channel_tls_is_canonical_method;
  210. chan->matches_extend_info = channel_tls_matches_extend_info_method;
  211. chan->matches_target = channel_tls_matches_target_method;
  212. chan->write_cell = channel_tls_write_cell_method;
  213. chan->write_packed_cell = channel_tls_write_packed_cell_method;
  214. chan->write_var_cell = channel_tls_write_var_cell_method;
  215. /* Link the channel and orconn to each other */
  216. tlschan->conn = orconn;
  217. orconn->chan = tlschan;
  218. if (is_local_addr(&(TO_CONN(orconn)->addr))) channel_mark_local(chan);
  219. channel_mark_incoming(chan);
  220. chan->active_circuit_pqueue = smartlist_new();
  221. chan->active_circuit_pqueue_last_recalibrated = cell_ewma_get_tick();
  222. /* If we got one, we should register it */
  223. if (chan) channel_register(chan);
  224. return chan;
  225. }
  226. /*********
  227. * Casts *
  228. ********/
  229. /**
  230. * Cast a channel_tls_t to a channel_t.
  231. */
  232. channel_t *
  233. channel_tls_to_base(channel_tls_t *tlschan)
  234. {
  235. if (!tlschan) return NULL;
  236. return &(tlschan->_base);
  237. }
  238. /**
  239. * Cast a channel_t to a channel_tls_t, with appropriate type-checking
  240. * asserts.
  241. */
  242. channel_tls_t *
  243. channel_tls_from_base(channel_t *chan)
  244. {
  245. if (!chan) return NULL;
  246. tor_assert(chan->magic == TLS_CHAN_MAGIC);
  247. return (channel_tls_t *)(chan);
  248. }
  249. /********************************************
  250. * Method implementations for channel_tls_t *
  251. *******************************************/
  252. /**
  253. * Close a channel_tls_t
  254. *
  255. * This implements the close method for channel_tls_t
  256. */
  257. static void
  258. channel_tls_close_method(channel_t *chan)
  259. {
  260. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  261. tor_assert(tlschan);
  262. if (tlschan->conn) connection_or_close_normally(tlschan->conn, 1);
  263. else {
  264. /* Weird - we'll have to change the state ourselves, I guess */
  265. log_info(LD_CHANNEL,
  266. "Tried to close channel_tls_t %p with NULL conn",
  267. tlschan);
  268. channel_change_state(chan, CHANNEL_STATE_ERROR);
  269. }
  270. }
  271. /**
  272. * Describe the transport for a channel_tls_t
  273. *
  274. * This returns the string "TLS channel on connection <id>" to the upper
  275. * layer.
  276. */
  277. static const char *
  278. channel_tls_describe_transport_method(channel_t *chan)
  279. {
  280. static char *buf = NULL;
  281. uint64_t id;
  282. channel_tls_t *tlschan;
  283. const char *rv = NULL;
  284. tor_assert(chan);
  285. tlschan = BASE_CHAN_TO_TLS(chan);
  286. if (tlschan->conn) {
  287. id = TO_CONN(tlschan->conn)->global_identifier;
  288. if (buf) tor_free(buf);
  289. tor_asprintf(&buf,
  290. "TLS channel (connection " U64_FORMAT ")",
  291. U64_PRINTF_ARG(id));
  292. rv = buf;
  293. } else {
  294. rv = "TLS channel (no connection)";
  295. }
  296. return rv;
  297. }
  298. /**
  299. * Get the remote address of a channel_tls_t
  300. *
  301. * This implements the get_remote_addr method for channel_tls_t; copy the
  302. * remote endpoint of the channel to addr_out and return 1 (always
  303. * succeeds for this transport).
  304. */
  305. static int
  306. channel_tls_get_remote_addr_method(channel_t *chan, tor_addr_t *addr_out)
  307. {
  308. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  309. tor_assert(tlschan);
  310. tor_assert(addr_out);
  311. tor_assert(tlschan->conn);
  312. tor_addr_copy(addr_out, &(TO_CONN(tlschan->conn)->addr));
  313. return 1;
  314. }
  315. /**
  316. * Get endpoint description of a channel_tls_t
  317. *
  318. * This implements the get_remote_descr method for channel_tls_t; it returns
  319. * a text description of the remote endpoint of the channel suitable for use
  320. * in log messages. The req parameter is 0 for the canonical address or 1 for
  321. * the actual address seen.
  322. */
  323. static const char *
  324. channel_tls_get_remote_descr_method(channel_t *chan, int req)
  325. {
  326. #define MAX_DESCR_LEN 32
  327. static char buf[MAX_DESCR_LEN + 1];
  328. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  329. connection_t *conn;
  330. const char *answer = NULL;
  331. char *addr_str;
  332. tor_assert(tlschan);
  333. tor_assert(tlschan->conn);
  334. conn = TO_CONN(tlschan->conn);
  335. switch (req) {
  336. case 0:
  337. /* Canonical address */
  338. tor_snprintf(buf, MAX_DESCR_LEN + 1,
  339. "%s:%u", conn->address, conn->port);
  340. answer = buf;
  341. break;
  342. case 1:
  343. /* Actual address */
  344. addr_str = tor_dup_addr(&(tlschan->conn->real_addr));
  345. tor_snprintf(buf, MAX_DESCR_LEN + 1,
  346. "%s:%u", addr_str, conn->port);
  347. tor_free(addr_str);
  348. answer = buf;
  349. break;
  350. default:
  351. /* Something's broken in channel.c */
  352. tor_assert(1);
  353. }
  354. return answer;
  355. }
  356. /**
  357. * Tell the upper layer if we have queued writes
  358. *
  359. * This implements the has_queued_writes method for channel_tls _t; it returns
  360. * 1 iff we have queued writes on the outbuf of the underlying or_connection_t.
  361. */
  362. static int
  363. channel_tls_has_queued_writes_method(channel_t *chan)
  364. {
  365. size_t outbuf_len;
  366. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  367. tor_assert(tlschan);
  368. tor_assert(tlschan->conn);
  369. outbuf_len = connection_get_outbuf_len(TO_CONN(tlschan->conn));
  370. return (outbuf_len > 0);
  371. }
  372. /**
  373. * Tell the upper layer if we're canonical
  374. *
  375. * This implements the is_canonical method for channel_tls_t; if req is zero,
  376. * it returns whether this is a canonical channel, and if it is one it returns
  377. * whether that can be relied upon.
  378. */
  379. static int
  380. channel_tls_is_canonical_method(channel_t *chan, int req)
  381. {
  382. int answer = 0;
  383. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  384. tor_assert(tlschan);
  385. tor_assert(tlschan->conn);
  386. switch (req) {
  387. case 0:
  388. answer = tlschan->conn->is_canonical;
  389. break;
  390. case 1:
  391. /*
  392. * Is the is_canonical bit reliable? In protocols version 2 and up
  393. * we get the canonical address from a NETINFO cell, but in older
  394. * versions it might be based on an obsolete descriptor.
  395. */
  396. answer = (tlschan->conn->link_proto >= 2);
  397. break;
  398. default:
  399. /* This shouldn't happen; channel.c is broken if it does */
  400. tor_assert(1);
  401. }
  402. return answer;
  403. }
  404. /**
  405. * Check if we match an extend_info_t
  406. *
  407. * This implements the matches_extend_info method for channel_tls_t; the upper
  408. * layer wants to know if this channel matches an extend_info_t.
  409. */
  410. static int
  411. channel_tls_matches_extend_info_method(channel_t *chan,
  412. extend_info_t *extend_info)
  413. {
  414. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  415. tor_assert(tlschan);
  416. tor_assert(extend_info);
  417. return (tor_addr_eq(&(extend_info->addr),
  418. &(TO_CONN(tlschan->conn)->addr)) &&
  419. (extend_info->port == TO_CONN(tlschan->conn)->port));
  420. }
  421. /**
  422. * Check if we match a target address
  423. *
  424. * This implements the matches_target method for channel_tls _t; the upper
  425. * layer wants to know if this channel matches a target address when extending
  426. * a circuit.
  427. */
  428. static int
  429. channel_tls_matches_target_method(channel_t *chan,
  430. const tor_addr_t *target)
  431. {
  432. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  433. tor_assert(tlschan);
  434. tor_assert(target);
  435. tor_assert(tlschan->conn);
  436. return tor_addr_compare(&(tlschan->conn->real_addr),
  437. target, CMP_EXACT);
  438. }
  439. /**
  440. * Write a cell to a channel_tls_t
  441. *
  442. * This implements the write_cell method for channel_tls_t; given a
  443. * channel_tls_t and a cell_t, transmit the cell_t.
  444. */
  445. static int
  446. channel_tls_write_cell_method(channel_t *chan, cell_t *cell)
  447. {
  448. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  449. tor_assert(tlschan);
  450. tor_assert(cell);
  451. tor_assert(tlschan->conn);
  452. connection_or_write_cell_to_buf(cell, tlschan->conn);
  453. return 1;
  454. }
  455. /**
  456. * Write a packed cell to a channel_tls_t
  457. *
  458. * This implements the write_packed_cell method for channel_tls_t; given a
  459. * channel_tls_t and a packed_cell_t, transmit the packed_cell_t.
  460. */
  461. static int
  462. channel_tls_write_packed_cell_method(channel_t *chan,
  463. packed_cell_t *packed_cell)
  464. {
  465. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  466. tor_assert(tlschan);
  467. tor_assert(packed_cell);
  468. tor_assert(tlschan->conn);
  469. connection_write_to_buf(packed_cell->body, CELL_NETWORK_SIZE,
  470. TO_CONN(tlschan->conn));
  471. /* This is where the cell is finished; used to be done from relay.c */
  472. packed_cell_free(packed_cell);
  473. return 1;
  474. }
  475. /**
  476. * Write a variable-length cell to a channel_tls_t
  477. *
  478. * This implements the write_var_cell method for channel_tls_t; given a
  479. * channel_tls_t and a var_cell_t, transmit the var_cell_t.
  480. */
  481. static int
  482. channel_tls_write_var_cell_method(channel_t *chan, var_cell_t *var_cell)
  483. {
  484. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  485. tor_assert(tlschan);
  486. tor_assert(var_cell);
  487. tor_assert(tlschan->conn);
  488. connection_or_write_var_cell_to_buf(var_cell, tlschan->conn);
  489. return 1;
  490. }
  491. /*************************************************
  492. * Method implementations for channel_listener_t *
  493. ************************************************/
  494. /**
  495. * Close a channel_listener_t
  496. *
  497. * This implements the close method for channel_listener_t
  498. */
  499. static void
  500. channel_tls_listener_close_method(channel_listener_t *chan_l)
  501. {
  502. tor_assert(chan_l);
  503. /*
  504. * Listeners we just go ahead and change state through to CLOSED, but
  505. * make sure to check if they're channel_tls_listener to NULL it out.
  506. */
  507. if (chan_l == channel_tls_listener)
  508. channel_tls_listener = NULL;
  509. if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  510. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  511. chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) {
  512. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
  513. }
  514. if (chan_l->incoming_list) {
  515. SMARTLIST_FOREACH_BEGIN(chan_l->incoming_list,
  516. channel_t *, ichan) {
  517. channel_mark_for_close(ichan);
  518. } SMARTLIST_FOREACH_END(ichan);
  519. smartlist_free(chan_l->incoming_list);
  520. chan_l->incoming_list = NULL;
  521. }
  522. if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  523. chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) {
  524. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSED);
  525. }
  526. }
  527. /**
  528. * Describe the transport for a channel_listener_t
  529. *
  530. * This returns the string "TLS channel (listening)" to the upper
  531. * layer.
  532. */
  533. static const char *
  534. channel_tls_listener_describe_transport_method(channel_listener_t *chan_l)
  535. {
  536. tor_assert(chan_l);
  537. return "TLS channel (listening)";
  538. }
  539. /*******************************************************
  540. * Functions for handling events on an or_connection_t *
  541. ******************************************************/
  542. /**
  543. * Handle an orconn state change
  544. *
  545. * This function will be called by connection_or.c when the or_connection_t
  546. * associated with this channel_tls_t changes state.
  547. */
  548. void
  549. channel_tls_handle_state_change_on_orconn(channel_tls_t *chan,
  550. or_connection_t *conn,
  551. uint8_t old_state,
  552. uint8_t state)
  553. {
  554. channel_t *base_chan;
  555. tor_assert(chan);
  556. tor_assert(conn);
  557. tor_assert(conn->chan == chan);
  558. tor_assert(chan->conn == conn);
  559. /* -Werror appeasement */
  560. tor_assert(old_state == old_state);
  561. base_chan = TLS_CHAN_TO_BASE(chan);
  562. /* Make sure the base connection state makes sense - shouldn't be error,
  563. * closed or listening. */
  564. tor_assert(base_chan->state == CHANNEL_STATE_OPENING ||
  565. base_chan->state == CHANNEL_STATE_OPEN ||
  566. base_chan->state == CHANNEL_STATE_MAINT ||
  567. base_chan->state == CHANNEL_STATE_CLOSING);
  568. /* Did we just go to state open? */
  569. if (state == OR_CONN_STATE_OPEN) {
  570. /*
  571. * We can go to CHANNEL_STATE_OPEN from CHANNEL_STATE_OPENING or
  572. * CHANNEL_STATE_MAINT on this.
  573. */
  574. channel_change_state(base_chan, CHANNEL_STATE_OPEN);
  575. } else {
  576. /*
  577. * Not open, so from CHANNEL_STATE_OPEN we go to CHANNEL_STATE_MAINT,
  578. * otherwise no change.
  579. */
  580. if (base_chan->state == CHANNEL_STATE_OPEN) {
  581. channel_change_state(base_chan, CHANNEL_STATE_MAINT);
  582. }
  583. }
  584. }
  585. /**
  586. * Flush cells from a channel_tls_t
  587. *
  588. * Try to flush up to about num_cells cells, and return how many we flushed.
  589. */
  590. ssize_t
  591. channel_tls_flush_some_cells(channel_tls_t *chan, ssize_t num_cells)
  592. {
  593. ssize_t flushed = 0;
  594. tor_assert(chan);
  595. if (flushed >= num_cells) goto done;
  596. /*
  597. * If channel_tls_t ever buffers anything below the channel_t layer, flush
  598. * that first here.
  599. */
  600. flushed += channel_flush_some_cells(TLS_CHAN_TO_BASE(chan),
  601. num_cells - flushed);
  602. /*
  603. * If channel_tls_t ever buffers anything below the channel_t layer, check
  604. * how much we actually got and push it on down here.
  605. */
  606. done:
  607. return flushed;
  608. }
  609. /**
  610. * Check if a channel_tls_t has anything to flush
  611. *
  612. * Return true if there is any more to flush on this channel (cells in queue
  613. * or active circuits).
  614. */
  615. int
  616. channel_tls_more_to_flush(channel_tls_t *chan)
  617. {
  618. tor_assert(chan);
  619. /*
  620. * If channel_tls_t ever buffers anything below channel_t, the
  621. * check for that should go here first.
  622. */
  623. return channel_more_to_flush(TLS_CHAN_TO_BASE(chan));
  624. }
  625. #ifdef KEEP_TIMING_STATS
  626. /**
  627. * Timing states wrapper
  628. *
  629. * This is a wrapper function around the actual function that processes the
  630. * <b>cell</b> that just arrived on <b>chan</b>. Increment <b>*time</b>
  631. * by the number of microseconds used by the call to <b>*func(cell, chan)</b>.
  632. */
  633. static void
  634. channel_tls_time_process_cell(cell_t *cell, channel_tls_t *chan, int *time,
  635. void (*func)(cell_t *, channel_tls_t *))
  636. {
  637. struct timeval start, end;
  638. long time_passed;
  639. tor_gettimeofday(&start);
  640. (*func)(cell, chan);
  641. tor_gettimeofday(&end);
  642. time_passed = tv_udiff(&start, &end) ;
  643. if (time_passed > 10000) { /* more than 10ms */
  644. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  645. }
  646. if (time_passed < 0) {
  647. log_info(LD_GENERAL,"That call took us back in time!");
  648. time_passed = 0;
  649. }
  650. *time += time_passed;
  651. }
  652. #endif
  653. /**
  654. * Handle an incoming cell on a channel_tls_t
  655. *
  656. * This is called from connection_or.c to handle an arriving cell; it checks
  657. * for cell types specific to the handshake for this transport protocol and
  658. * handles them, and queues all other cells to the channel_t layer, which
  659. * eventually will hand them off to command.c.
  660. */
  661. void
  662. channel_tls_handle_cell(cell_t *cell, or_connection_t *conn)
  663. {
  664. channel_tls_t *chan;
  665. int handshaking;
  666. #ifdef KEEP_TIMING_STATS
  667. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  668. ++num ## tp; \
  669. channel_tls_time_process_cell(cl, cn, & tp ## time , \
  670. channel_tls_process_ ## tp ## _cell); \
  671. } STMT_END
  672. #else
  673. #define PROCESS_CELL(tp, cl, cn) channel_tls_process_ ## tp ## _cell(cl, cn)
  674. #endif
  675. tor_assert(cell);
  676. tor_assert(conn);
  677. chan = conn->chan;
  678. if (!chan) {
  679. log_warn(LD_CHANNEL,
  680. "Got a cell_t on an OR connection with no channel");
  681. return;
  682. }
  683. handshaking = (TO_CONN(conn)->state != OR_CONN_STATE_OPEN);
  684. if (conn->_base.marked_for_close)
  685. return;
  686. /* Reject all but VERSIONS and NETINFO when handshaking. */
  687. /* (VERSIONS should actually be impossible; it's variable-length.) */
  688. if (handshaking && cell->command != CELL_VERSIONS &&
  689. cell->command != CELL_NETINFO) {
  690. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  691. "Received unexpected cell command %d in chan state %s / "
  692. "conn state %s; closing the connection.",
  693. (int)cell->command,
  694. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  695. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state));
  696. connection_or_close_for_error(conn, 0);
  697. return;
  698. }
  699. if (conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3)
  700. or_handshake_state_record_cell(conn->handshake_state, cell, 1);
  701. switch (cell->command) {
  702. case CELL_PADDING:
  703. ++stats_n_padding_cells_processed;
  704. /* do nothing */
  705. break;
  706. case CELL_VERSIONS:
  707. tor_fragile_assert();
  708. break;
  709. case CELL_NETINFO:
  710. ++stats_n_netinfo_cells_processed;
  711. PROCESS_CELL(netinfo, cell, chan);
  712. break;
  713. case CELL_CREATE:
  714. case CELL_CREATE_FAST:
  715. case CELL_CREATED:
  716. case CELL_CREATED_FAST:
  717. case CELL_RELAY:
  718. case CELL_RELAY_EARLY:
  719. case CELL_DESTROY:
  720. /*
  721. * These are all transport independent and we pass them up through the
  722. * channel_t mechanism. They are ultimately handled in command.c.
  723. */
  724. channel_queue_cell(TLS_CHAN_TO_BASE(chan), cell);
  725. break;
  726. default:
  727. log_fn(LOG_INFO, LD_PROTOCOL,
  728. "Cell of unknown type (%d) received in channeltls.c. "
  729. "Dropping.",
  730. cell->command);
  731. break;
  732. }
  733. }
  734. /**
  735. * Handle an incoming variable-length cell on a channel_tls_t
  736. *
  737. * Process a <b>var_cell</b> that was just received on <b>conn</b>. Keep
  738. * internal statistics about how many of each cell we've processed so far
  739. * this second, and the total number of microseconds it took to
  740. * process each type of cell. All the var_cell commands are handshake-
  741. * related and live below the channel_t layer, so no variable-length
  742. * cells ever get delivered in the current implementation, but I've left
  743. * the mechanism in place for future use.
  744. */
  745. void
  746. channel_tls_handle_var_cell(var_cell_t *var_cell, or_connection_t *conn)
  747. {
  748. channel_tls_t *chan;
  749. int handshaking;
  750. #ifdef KEEP_TIMING_STATS
  751. /* how many of each cell have we seen so far this second? needs better
  752. * name. */
  753. static int num_versions = 0, num_certs = 0;
  754. static time_t current_second = 0; /* from previous calls to time */
  755. time_t now = time(NULL);
  756. if (current_second == 0) current_second = now;
  757. if (now > current_second) { /* the second has rolled over */
  758. /* print stats */
  759. log_info(LD_OR,
  760. "At end of second: %d versions (%d ms), %d certs (%d ms)",
  761. num_versions, versions_time / ((now - current_second) * 1000),
  762. num_certs, certs_time / ((now - current_second) * 1000));
  763. num_versions = num_certs = 0;
  764. versions_time = certs_time = 0;
  765. /* remember which second it is, for next time */
  766. current_second = now;
  767. }
  768. #endif
  769. tor_assert(var_cell);
  770. tor_assert(conn);
  771. chan = conn->chan;
  772. if (!chan) {
  773. log_warn(LD_CHANNEL,
  774. "Got a var_cell_t on an OR connection with no channel");
  775. return;
  776. }
  777. handshaking = (TO_CONN(conn)->state != OR_CONN_STATE_OPEN);
  778. if (TO_CONN(conn)->marked_for_close)
  779. return;
  780. switch (TO_CONN(conn)->state) {
  781. case OR_CONN_STATE_OR_HANDSHAKING_V2:
  782. if (var_cell->command != CELL_VERSIONS) {
  783. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  784. "Received a cell with command %d in unexpected "
  785. "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
  786. "closing the connection.",
  787. (int)(var_cell->command),
  788. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
  789. TO_CONN(conn)->state,
  790. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  791. (int)(TLS_CHAN_TO_BASE(chan)->state));
  792. /*
  793. * The code in connection_or.c will tell channel_t to close for
  794. * error; it will go to CHANNEL_STATE_CLOSING, and then to
  795. * CHANNEL_STATE_ERROR when conn is closed.
  796. */
  797. connection_or_close_for_error(conn, 0);
  798. return;
  799. }
  800. break;
  801. case OR_CONN_STATE_TLS_HANDSHAKING:
  802. /* If we're using bufferevents, it's entirely possible for us to
  803. * notice "hey, data arrived!" before we notice "hey, the handshake
  804. * finished!" And we need to be accepting both at once to handle both
  805. * the v2 and v3 handshakes. */
  806. /* fall through */
  807. case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
  808. if (!(command_allowed_before_handshake(var_cell->command))) {
  809. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  810. "Received a cell with command %d in unexpected "
  811. "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
  812. "closing the connection.",
  813. (int)(var_cell->command),
  814. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
  815. (int)(TO_CONN(conn)->state),
  816. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  817. (int)(TLS_CHAN_TO_BASE(chan)->state));
  818. /* see above comment about CHANNEL_STATE_ERROR */
  819. connection_or_close_for_error(conn, 0);
  820. return;
  821. } else {
  822. if (enter_v3_handshake_with_cell(var_cell, chan) < 0)
  823. return;
  824. }
  825. break;
  826. case OR_CONN_STATE_OR_HANDSHAKING_V3:
  827. if (var_cell->command != CELL_AUTHENTICATE)
  828. or_handshake_state_record_var_cell(conn->handshake_state, var_cell, 1);
  829. break; /* Everything is allowed */
  830. case OR_CONN_STATE_OPEN:
  831. if (conn->link_proto < 3) {
  832. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  833. "Received a variable-length cell with command %d in orconn "
  834. "state %s [%d], channel state %s [%d] with link protocol %d; "
  835. "ignoring it.",
  836. (int)(var_cell->command),
  837. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
  838. (int)(TO_CONN(conn)->state),
  839. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  840. (int)(TLS_CHAN_TO_BASE(chan)->state),
  841. (int)(conn->link_proto));
  842. return;
  843. }
  844. break;
  845. default:
  846. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  847. "Received var-length cell with command %d in unexpected "
  848. "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
  849. "ignoring it.",
  850. (int)(var_cell->command),
  851. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
  852. (int)(TO_CONN(conn)->state),
  853. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  854. (int)(TLS_CHAN_TO_BASE(chan)->state));
  855. return;
  856. }
  857. /* Now handle the cell */
  858. switch (var_cell->command) {
  859. case CELL_VERSIONS:
  860. ++stats_n_versions_cells_processed;
  861. PROCESS_CELL(versions, var_cell, chan);
  862. break;
  863. case CELL_VPADDING:
  864. ++stats_n_vpadding_cells_processed;
  865. /* Do nothing */
  866. break;
  867. case CELL_CERTS:
  868. ++stats_n_certs_cells_processed;
  869. PROCESS_CELL(certs, var_cell, chan);
  870. break;
  871. case CELL_AUTH_CHALLENGE:
  872. ++stats_n_auth_challenge_cells_processed;
  873. PROCESS_CELL(auth_challenge, var_cell, chan);
  874. break;
  875. case CELL_AUTHENTICATE:
  876. ++stats_n_authenticate_cells_processed;
  877. PROCESS_CELL(authenticate, var_cell, chan);
  878. break;
  879. case CELL_AUTHORIZE:
  880. ++stats_n_authorize_cells_processed;
  881. /* Ignored so far. */
  882. break;
  883. default:
  884. log_fn(LOG_INFO, LD_PROTOCOL,
  885. "Variable-length cell of unknown type (%d) received.",
  886. (int)(var_cell->command));
  887. break;
  888. }
  889. }
  890. /**
  891. * Check if this cell type is allowed before the handshake is finished
  892. *
  893. * Return true if <b>command</b> is a cell command that's allowed to start a
  894. * V3 handshake.
  895. */
  896. static int
  897. command_allowed_before_handshake(uint8_t command)
  898. {
  899. switch (command) {
  900. case CELL_VERSIONS:
  901. case CELL_VPADDING:
  902. case CELL_AUTHORIZE:
  903. return 1;
  904. default:
  905. return 0;
  906. }
  907. }
  908. /**
  909. * Start a V3 handshake on an incoming connection
  910. *
  911. * Called when we as a server receive an appropriate cell while waiting
  912. * either for a cell or a TLS handshake. Set the connection's state to
  913. * "handshaking_v3', initializes the or_handshake_state field as needed,
  914. * and add the cell to the hash of incoming cells.)
  915. */
  916. static int
  917. enter_v3_handshake_with_cell(var_cell_t *cell, channel_tls_t *chan)
  918. {
  919. int started_here = 0;
  920. tor_assert(cell);
  921. tor_assert(chan);
  922. tor_assert(chan->conn);
  923. started_here = connection_or_nonopen_was_started_here(chan->conn);
  924. tor_assert(TO_CONN(chan->conn)->state == OR_CONN_STATE_TLS_HANDSHAKING ||
  925. TO_CONN(chan->conn)->state ==
  926. OR_CONN_STATE_TLS_SERVER_RENEGOTIATING);
  927. if (started_here) {
  928. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  929. "Received a cell while TLS-handshaking, not in "
  930. "OR_HANDSHAKING_V3, on a connection we originated.");
  931. }
  932. chan->conn->_base.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  933. if (connection_init_or_handshake_state(chan->conn, started_here) < 0) {
  934. connection_or_close_for_error(chan->conn, 0);
  935. return -1;
  936. }
  937. or_handshake_state_record_var_cell(chan->conn->handshake_state, cell, 1);
  938. return 0;
  939. }
  940. /**
  941. * Process a 'versions' cell.
  942. *
  943. * This function is called to handle an incoming VERSIONS cell; the current
  944. * link protocol version must be 0 to indicate that no version has yet been
  945. * negotiated. We compare the versions in the cell to the list of versions
  946. * we support, pick the highest version we have in common, and continue the
  947. * negotiation from there.
  948. */
  949. static void
  950. channel_tls_process_versions_cell(var_cell_t *cell, channel_tls_t *chan)
  951. {
  952. int highest_supported_version = 0;
  953. const uint8_t *cp, *end;
  954. int started_here = 0;
  955. tor_assert(cell);
  956. tor_assert(chan);
  957. tor_assert(chan->conn);
  958. started_here = connection_or_nonopen_was_started_here(chan->conn);
  959. if (chan->conn->link_proto != 0 ||
  960. (chan->conn->handshake_state &&
  961. chan->conn->handshake_state->received_versions)) {
  962. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  963. "Received a VERSIONS cell on a connection with its version "
  964. "already set to %d; dropping",
  965. (int)(chan->conn->link_proto));
  966. return;
  967. }
  968. switch (chan->conn->_base.state)
  969. {
  970. case OR_CONN_STATE_OR_HANDSHAKING_V2:
  971. case OR_CONN_STATE_OR_HANDSHAKING_V3:
  972. break;
  973. case OR_CONN_STATE_TLS_HANDSHAKING:
  974. case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
  975. default:
  976. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  977. "VERSIONS cell while in unexpected state");
  978. return;
  979. }
  980. tor_assert(chan->conn->handshake_state);
  981. end = cell->payload + cell->payload_len;
  982. for (cp = cell->payload; cp+1 < end; ++cp) {
  983. uint16_t v = ntohs(get_uint16(cp));
  984. if (is_or_protocol_version_known(v) && v > highest_supported_version)
  985. highest_supported_version = v;
  986. }
  987. if (!highest_supported_version) {
  988. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  989. "Couldn't find a version in common between my version list and the "
  990. "list in the VERSIONS cell; closing connection.");
  991. connection_or_close_for_error(chan->conn, 0);
  992. return;
  993. } else if (highest_supported_version == 1) {
  994. /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS
  995. * cells. */
  996. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  997. "Used version negotiation protocol to negotiate a v1 connection. "
  998. "That's crazily non-compliant. Closing connection.");
  999. connection_or_close_for_error(chan->conn, 0);
  1000. return;
  1001. } else if (highest_supported_version < 3 &&
  1002. chan->conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
  1003. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1004. "Negotiated link protocol 2 or lower after doing a v3 TLS "
  1005. "handshake. Closing connection.");
  1006. connection_or_close_for_error(chan->conn, 0);
  1007. return;
  1008. }
  1009. chan->conn->link_proto = highest_supported_version;
  1010. chan->conn->handshake_state->received_versions = 1;
  1011. if (chan->conn->link_proto == 2) {
  1012. log_info(LD_OR,
  1013. "Negotiated version %d with %s:%d; sending NETINFO.",
  1014. highest_supported_version,
  1015. safe_str_client(chan->conn->_base.address),
  1016. chan->conn->_base.port);
  1017. if (connection_or_send_netinfo(chan->conn) < 0) {
  1018. connection_or_close_for_error(chan->conn, 0);
  1019. return;
  1020. }
  1021. } else {
  1022. const int send_versions = !started_here;
  1023. /* If we want to authenticate, send a CERTS cell */
  1024. const int send_certs = !started_here || public_server_mode(get_options());
  1025. /* If we're a relay that got a connection, ask for authentication. */
  1026. const int send_chall = !started_here && public_server_mode(get_options());
  1027. /* If our certs cell will authenticate us, we can send a netinfo cell
  1028. * right now. */
  1029. const int send_netinfo = !started_here;
  1030. const int send_any =
  1031. send_versions || send_certs || send_chall || send_netinfo;
  1032. tor_assert(chan->conn->link_proto >= 3);
  1033. log_info(LD_OR,
  1034. "Negotiated version %d with %s:%d; %s%s%s%s%s",
  1035. highest_supported_version,
  1036. safe_str_client(chan->conn->_base.address),
  1037. chan->conn->_base.port,
  1038. send_any ? "Sending cells:" : "Waiting for CERTS cell",
  1039. send_versions ? " VERSIONS" : "",
  1040. send_certs ? " CERTS" : "",
  1041. send_chall ? " AUTH_CHALLENGE" : "",
  1042. send_netinfo ? " NETINFO" : "");
  1043. #ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
  1044. if (1) {
  1045. connection_or_close_normally(chan->conn, 1);
  1046. return;
  1047. }
  1048. #endif
  1049. if (send_versions) {
  1050. if (connection_or_send_versions(chan->conn, 1) < 0) {
  1051. log_warn(LD_OR, "Couldn't send versions cell");
  1052. connection_or_close_for_error(chan->conn, 0);
  1053. return;
  1054. }
  1055. }
  1056. if (send_certs) {
  1057. if (connection_or_send_certs_cell(chan->conn) < 0) {
  1058. log_warn(LD_OR, "Couldn't send certs cell");
  1059. connection_or_close_for_error(chan->conn, 0);
  1060. return;
  1061. }
  1062. }
  1063. if (send_chall) {
  1064. if (connection_or_send_auth_challenge_cell(chan->conn) < 0) {
  1065. log_warn(LD_OR, "Couldn't send auth_challenge cell");
  1066. connection_or_close_for_error(chan->conn, 0);
  1067. return;
  1068. }
  1069. }
  1070. if (send_netinfo) {
  1071. if (connection_or_send_netinfo(chan->conn) < 0) {
  1072. log_warn(LD_OR, "Couldn't send netinfo cell");
  1073. connection_or_close_for_error(chan->conn, 0);
  1074. return;
  1075. }
  1076. }
  1077. }
  1078. }
  1079. /**
  1080. * Process a 'netinfo' cell
  1081. *
  1082. * This function is called to handle an incoming NETINFO cell; read and act
  1083. * on its contents, and set the connection state to "open".
  1084. */
  1085. static void
  1086. channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *chan)
  1087. {
  1088. time_t timestamp;
  1089. uint8_t my_addr_type;
  1090. uint8_t my_addr_len;
  1091. const uint8_t *my_addr_ptr;
  1092. const uint8_t *cp, *end;
  1093. uint8_t n_other_addrs;
  1094. time_t now = time(NULL);
  1095. long apparent_skew = 0;
  1096. tor_addr_t my_apparent_addr = TOR_ADDR_NULL;
  1097. tor_assert(cell);
  1098. tor_assert(chan);
  1099. tor_assert(chan->conn);
  1100. if (chan->conn->link_proto < 2) {
  1101. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1102. "Received a NETINFO cell on %s connection; dropping.",
  1103. chan->conn->link_proto == 0 ? "non-versioned" : "a v1");
  1104. return;
  1105. }
  1106. if (chan->conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V2 &&
  1107. chan->conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3) {
  1108. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1109. "Received a NETINFO cell on non-handshaking connection; dropping.");
  1110. return;
  1111. }
  1112. tor_assert(chan->conn->handshake_state &&
  1113. chan->conn->handshake_state->received_versions);
  1114. if (chan->conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
  1115. tor_assert(chan->conn->link_proto >= 3);
  1116. if (chan->conn->handshake_state->started_here) {
  1117. if (!(chan->conn->handshake_state->authenticated)) {
  1118. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1119. "Got a NETINFO cell from server, "
  1120. "but no authentication. Closing the connection.");
  1121. connection_or_close_for_error(chan->conn, 0);
  1122. return;
  1123. }
  1124. } else {
  1125. /* we're the server. If the client never authenticated, we have
  1126. some housekeeping to do.*/
  1127. if (!(chan->conn->handshake_state->authenticated)) {
  1128. tor_assert(tor_digest_is_zero(
  1129. (const char*)(chan->conn->handshake_state->
  1130. authenticated_peer_id)));
  1131. channel_set_circid_type(TLS_CHAN_TO_BASE(chan), NULL);
  1132. connection_or_init_conn_from_address(chan->conn,
  1133. &(chan->conn->_base.addr),
  1134. chan->conn->_base.port,
  1135. (const char*)(chan->conn->handshake_state->
  1136. authenticated_peer_id),
  1137. 0);
  1138. }
  1139. }
  1140. }
  1141. /* Decode the cell. */
  1142. timestamp = ntohl(get_uint32(cell->payload));
  1143. if (labs(now - chan->conn->handshake_state->sent_versions_at) < 180) {
  1144. apparent_skew = now - timestamp;
  1145. }
  1146. my_addr_type = (uint8_t) cell->payload[4];
  1147. my_addr_len = (uint8_t) cell->payload[5];
  1148. my_addr_ptr = (uint8_t*) cell->payload + 6;
  1149. end = cell->payload + CELL_PAYLOAD_SIZE;
  1150. cp = cell->payload + 6 + my_addr_len;
  1151. if (cp >= end) {
  1152. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1153. "Addresses too long in netinfo cell; closing connection.");
  1154. connection_or_close_for_error(chan->conn, 0);
  1155. return;
  1156. } else if (my_addr_type == RESOLVED_TYPE_IPV4 && my_addr_len == 4) {
  1157. tor_addr_from_ipv4n(&my_apparent_addr, get_uint32(my_addr_ptr));
  1158. } else if (my_addr_type == RESOLVED_TYPE_IPV6 && my_addr_len == 16) {
  1159. tor_addr_from_ipv6_bytes(&my_apparent_addr, (const char *) my_addr_ptr);
  1160. }
  1161. n_other_addrs = (uint8_t) *cp++;
  1162. while (n_other_addrs && cp < end-2) {
  1163. /* Consider all the other addresses; if any matches, this connection is
  1164. * "canonical." */
  1165. tor_addr_t addr;
  1166. const uint8_t *next =
  1167. decode_address_from_payload(&addr, cp, (int)(end-cp));
  1168. if (next == NULL) {
  1169. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1170. "Bad address in netinfo cell; closing connection.");
  1171. connection_or_close_for_error(chan->conn, 0);
  1172. return;
  1173. }
  1174. if (tor_addr_eq(&addr, &(chan->conn->real_addr))) {
  1175. chan->conn->is_canonical = 1;
  1176. break;
  1177. }
  1178. cp = next;
  1179. --n_other_addrs;
  1180. }
  1181. /* Act on apparent skew. */
  1182. /** Warn when we get a netinfo skew with at least this value. */
  1183. #define NETINFO_NOTICE_SKEW 3600
  1184. if (labs(apparent_skew) > NETINFO_NOTICE_SKEW &&
  1185. router_get_by_id_digest(chan->conn->identity_digest)) {
  1186. char dbuf[64];
  1187. int severity;
  1188. /*XXXX be smarter about when everybody says we are skewed. */
  1189. if (router_digest_is_trusted_dir(chan->conn->identity_digest))
  1190. severity = LOG_WARN;
  1191. else
  1192. severity = LOG_INFO;
  1193. format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
  1194. log_fn(severity, LD_GENERAL,
  1195. "Received NETINFO cell with skewed time from "
  1196. "server at %s:%d. It seems that our clock is %s by %s, or "
  1197. "that theirs is %s. Tor requires an accurate clock to work: "
  1198. "please check your time and date settings.",
  1199. chan->conn->_base.address,
  1200. (int)(chan->conn->_base.port),
  1201. apparent_skew > 0 ? "ahead" : "behind",
  1202. dbuf,
  1203. apparent_skew > 0 ? "behind" : "ahead");
  1204. if (severity == LOG_WARN) /* only tell the controller if an authority */
  1205. control_event_general_status(LOG_WARN,
  1206. "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
  1207. apparent_skew,
  1208. chan->conn->_base.address,
  1209. chan->conn->_base.port);
  1210. }
  1211. /* XXX maybe act on my_apparent_addr, if the source is sufficiently
  1212. * trustworthy. */
  1213. if (connection_or_set_state_open(chan->conn) < 0) {
  1214. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1215. "Got good NETINFO cell from %s:%d; but "
  1216. "was unable to make the OR connection become open.",
  1217. safe_str_client(chan->conn->_base.address),
  1218. chan->conn->_base.port);
  1219. connection_or_close_for_error(chan->conn, 0);
  1220. } else {
  1221. log_info(LD_OR,
  1222. "Got good NETINFO cell from %s:%d; OR connection is now "
  1223. "open, using protocol version %d. Its ID digest is %s. "
  1224. "Our address is apparently %s.",
  1225. safe_str_client(chan->conn->_base.address),
  1226. chan->conn->_base.port,
  1227. (int)(chan->conn->link_proto),
  1228. hex_str(TLS_CHAN_TO_BASE(chan)->identity_digest,
  1229. DIGEST_LEN),
  1230. tor_addr_is_null(&my_apparent_addr) ?
  1231. "<none>" : fmt_and_decorate_addr(&my_apparent_addr));
  1232. }
  1233. assert_connection_ok(TO_CONN(chan->conn),time(NULL));
  1234. }
  1235. /**
  1236. * Process a CERTS cell from a channel.
  1237. *
  1238. * This function is called to process an incoming CERTS cell on a
  1239. * channel_tls_t:
  1240. *
  1241. * If the other side should not have sent us a CERTS cell, or the cell is
  1242. * malformed, or it is supposed to authenticate the TLS key but it doesn't,
  1243. * then mark the connection.
  1244. *
  1245. * If the cell has a good cert chain and we're doing a v3 handshake, then
  1246. * store the certificates in or_handshake_state. If this is the client side
  1247. * of the connection, we then authenticate the server or mark the connection.
  1248. * If it's the server side, wait for an AUTHENTICATE cell.
  1249. */
  1250. static void
  1251. channel_tls_process_certs_cell(var_cell_t *cell, channel_tls_t *chan)
  1252. {
  1253. tor_cert_t *link_cert = NULL;
  1254. tor_cert_t *id_cert = NULL;
  1255. tor_cert_t *auth_cert = NULL;
  1256. uint8_t *ptr;
  1257. int n_certs, i;
  1258. int send_netinfo = 0;
  1259. tor_assert(cell);
  1260. tor_assert(chan);
  1261. tor_assert(chan->conn);
  1262. #define ERR(s) \
  1263. do { \
  1264. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  1265. "Received a bad CERTS cell from %s:%d: %s", \
  1266. safe_str(chan->conn->_base.address), \
  1267. chan->conn->_base.port, (s)); \
  1268. connection_or_close_for_error(chan->conn, 0); \
  1269. return; \
  1270. } while (0)
  1271. if (chan->conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  1272. ERR("We're not doing a v3 handshake!");
  1273. if (chan->conn->link_proto < 3)
  1274. ERR("We're not using link protocol >= 3");
  1275. if (chan->conn->handshake_state->received_certs_cell)
  1276. ERR("We already got one");
  1277. if (chan->conn->handshake_state->authenticated) {
  1278. /* Should be unreachable, but let's make sure. */
  1279. ERR("We're already authenticated!");
  1280. }
  1281. if (cell->payload_len < 1)
  1282. ERR("It had no body");
  1283. if (cell->circ_id)
  1284. ERR("It had a nonzero circuit ID");
  1285. n_certs = cell->payload[0];
  1286. ptr = cell->payload + 1;
  1287. for (i = 0; i < n_certs; ++i) {
  1288. uint8_t cert_type;
  1289. uint16_t cert_len;
  1290. if (ptr + 3 > cell->payload + cell->payload_len) {
  1291. goto truncated;
  1292. }
  1293. cert_type = *ptr;
  1294. cert_len = ntohs(get_uint16(ptr+1));
  1295. if (ptr + 3 + cert_len > cell->payload + cell->payload_len) {
  1296. goto truncated;
  1297. }
  1298. if (cert_type == OR_CERT_TYPE_TLS_LINK ||
  1299. cert_type == OR_CERT_TYPE_ID_1024 ||
  1300. cert_type == OR_CERT_TYPE_AUTH_1024) {
  1301. tor_cert_t *cert = tor_cert_decode(ptr + 3, cert_len);
  1302. if (!cert) {
  1303. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1304. "Received undecodable certificate in CERTS cell from %s:%d",
  1305. safe_str(chan->conn->_base.address),
  1306. chan->conn->_base.port);
  1307. } else {
  1308. if (cert_type == OR_CERT_TYPE_TLS_LINK) {
  1309. if (link_cert) {
  1310. tor_cert_free(cert);
  1311. ERR("Too many TLS_LINK certificates");
  1312. }
  1313. link_cert = cert;
  1314. } else if (cert_type == OR_CERT_TYPE_ID_1024) {
  1315. if (id_cert) {
  1316. tor_cert_free(cert);
  1317. ERR("Too many ID_1024 certificates");
  1318. }
  1319. id_cert = cert;
  1320. } else if (cert_type == OR_CERT_TYPE_AUTH_1024) {
  1321. if (auth_cert) {
  1322. tor_cert_free(cert);
  1323. ERR("Too many AUTH_1024 certificates");
  1324. }
  1325. auth_cert = cert;
  1326. } else {
  1327. tor_cert_free(cert);
  1328. }
  1329. }
  1330. }
  1331. ptr += 3 + cert_len;
  1332. continue;
  1333. truncated:
  1334. ERR("It ends in the middle of a certificate");
  1335. }
  1336. if (chan->conn->handshake_state->started_here) {
  1337. int severity;
  1338. if (! (id_cert && link_cert))
  1339. ERR("The certs we wanted were missing");
  1340. /* Okay. We should be able to check the certificates now. */
  1341. if (! tor_tls_cert_matches_key(chan->conn->tls, link_cert)) {
  1342. ERR("The link certificate didn't match the TLS public key");
  1343. }
  1344. /* Note that this warns more loudly about time and validity if we were
  1345. * _trying_ to connect to an authority, not necessarily if we _did_ connect
  1346. * to one. */
  1347. if (router_digest_is_trusted_dir(
  1348. TLS_CHAN_TO_BASE(chan)->identity_digest))
  1349. severity = LOG_WARN;
  1350. else
  1351. severity = LOG_PROTOCOL_WARN;
  1352. if (! tor_tls_cert_is_valid(severity, link_cert, id_cert, 0))
  1353. ERR("The link certificate was not valid");
  1354. if (! tor_tls_cert_is_valid(severity, id_cert, id_cert, 1))
  1355. ERR("The ID certificate was not valid");
  1356. chan->conn->handshake_state->authenticated = 1;
  1357. {
  1358. const digests_t *id_digests = tor_cert_get_id_digests(id_cert);
  1359. crypto_pk_t *identity_rcvd;
  1360. if (!id_digests)
  1361. ERR("Couldn't compute digests for key in ID cert");
  1362. identity_rcvd = tor_tls_cert_get_key(id_cert);
  1363. if (!identity_rcvd)
  1364. ERR("Internal error: Couldn't get RSA key from ID cert.");
  1365. memcpy(chan->conn->handshake_state->authenticated_peer_id,
  1366. id_digests->d[DIGEST_SHA1], DIGEST_LEN);
  1367. channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd);
  1368. crypto_pk_free(identity_rcvd);
  1369. }
  1370. if (connection_or_client_learned_peer_id(chan->conn,
  1371. chan->conn->handshake_state->authenticated_peer_id) < 0)
  1372. ERR("Problem setting or checking peer id");
  1373. log_info(LD_OR,
  1374. "Got some good certificates from %s:%d: Authenticated it.",
  1375. safe_str(chan->conn->_base.address), chan->conn->_base.port);
  1376. chan->conn->handshake_state->id_cert = id_cert;
  1377. id_cert = NULL;
  1378. if (!public_server_mode(get_options())) {
  1379. /* If we initiated the connection and we are not a public server, we
  1380. * aren't planning to authenticate at all. At this point we know who we
  1381. * are talking to, so we can just send a netinfo now. */
  1382. send_netinfo = 1;
  1383. }
  1384. } else {
  1385. if (! (id_cert && auth_cert))
  1386. ERR("The certs we wanted were missing");
  1387. /* Remember these certificates so we can check an AUTHENTICATE cell */
  1388. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, auth_cert, id_cert, 1))
  1389. ERR("The authentication certificate was not valid");
  1390. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, id_cert, id_cert, 1))
  1391. ERR("The ID certificate was not valid");
  1392. log_info(LD_OR,
  1393. "Got some good certificates from %s:%d: "
  1394. "Waiting for AUTHENTICATE.",
  1395. safe_str(chan->conn->_base.address),
  1396. chan->conn->_base.port);
  1397. /* XXXX check more stuff? */
  1398. chan->conn->handshake_state->id_cert = id_cert;
  1399. chan->conn->handshake_state->auth_cert = auth_cert;
  1400. id_cert = auth_cert = NULL;
  1401. }
  1402. chan->conn->handshake_state->received_certs_cell = 1;
  1403. if (send_netinfo) {
  1404. if (connection_or_send_netinfo(chan->conn) < 0) {
  1405. log_warn(LD_OR, "Couldn't send netinfo cell");
  1406. connection_or_close_for_error(chan->conn, 0);
  1407. goto err;
  1408. }
  1409. }
  1410. err:
  1411. tor_cert_free(id_cert);
  1412. tor_cert_free(link_cert);
  1413. tor_cert_free(auth_cert);
  1414. #undef ERR
  1415. }
  1416. /**
  1417. * Process an AUTH_CHALLENGE cell from a channel_tls_t
  1418. *
  1419. * This function is called to handle an incoming AUTH_CHALLENGE cell on a
  1420. * channel_tls_t; if we weren't supposed to get one (for example, because we're
  1421. * not the originator of the channel), or it's ill-formed, or we aren't doing
  1422. * a v3 handshake, mark the channel. If the cell is well-formed but we don't
  1423. * want to authenticate, just drop it. If the cell is well-formed *and* we
  1424. * want to authenticate, send an AUTHENTICATE cell and then a NETINFO cell.
  1425. */
  1426. static void
  1427. channel_tls_process_auth_challenge_cell(var_cell_t *cell, channel_tls_t *chan)
  1428. {
  1429. int n_types, i, use_type = -1;
  1430. uint8_t *cp;
  1431. tor_assert(cell);
  1432. tor_assert(chan);
  1433. tor_assert(chan->conn);
  1434. #define ERR(s) \
  1435. do { \
  1436. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  1437. "Received a bad AUTH_CHALLENGE cell from %s:%d: %s", \
  1438. safe_str(chan->conn->_base.address), \
  1439. chan->conn->_base.port, (s)); \
  1440. connection_or_close_for_error(chan->conn, 0); \
  1441. return; \
  1442. } while (0)
  1443. if (chan->conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  1444. ERR("We're not currently doing a v3 handshake");
  1445. if (chan->conn->link_proto < 3)
  1446. ERR("We're not using link protocol >= 3");
  1447. if (!(chan->conn->handshake_state->started_here))
  1448. ERR("We didn't originate this connection");
  1449. if (chan->conn->handshake_state->received_auth_challenge)
  1450. ERR("We already received one");
  1451. if (!(chan->conn->handshake_state->received_certs_cell))
  1452. ERR("We haven't gotten a CERTS cell yet");
  1453. if (cell->payload_len < OR_AUTH_CHALLENGE_LEN + 2)
  1454. ERR("It was too short");
  1455. if (cell->circ_id)
  1456. ERR("It had a nonzero circuit ID");
  1457. n_types = ntohs(get_uint16(cell->payload + OR_AUTH_CHALLENGE_LEN));
  1458. if (cell->payload_len < OR_AUTH_CHALLENGE_LEN + 2 + 2*n_types)
  1459. ERR("It looks truncated");
  1460. /* Now see if there is an authentication type we can use */
  1461. cp = cell->payload+OR_AUTH_CHALLENGE_LEN + 2;
  1462. for (i = 0; i < n_types; ++i, cp += 2) {
  1463. uint16_t authtype = ntohs(get_uint16(cp));
  1464. if (authtype == AUTHTYPE_RSA_SHA256_TLSSECRET)
  1465. use_type = authtype;
  1466. }
  1467. chan->conn->handshake_state->received_auth_challenge = 1;
  1468. if (! public_server_mode(get_options())) {
  1469. /* If we're not a public server then we don't want to authenticate on a
  1470. connection we originated, and we already sent a NETINFO cell when we
  1471. got the CERTS cell. We have nothing more to do. */
  1472. return;
  1473. }
  1474. if (use_type >= 0) {
  1475. log_info(LD_OR,
  1476. "Got an AUTH_CHALLENGE cell from %s:%d: Sending "
  1477. "authentication",
  1478. safe_str(chan->conn->_base.address),
  1479. chan->conn->_base.port);
  1480. if (connection_or_send_authenticate_cell(chan->conn, use_type) < 0) {
  1481. log_warn(LD_OR,
  1482. "Couldn't send authenticate cell");
  1483. connection_or_close_for_error(chan->conn, 0);
  1484. return;
  1485. }
  1486. } else {
  1487. log_info(LD_OR,
  1488. "Got an AUTH_CHALLENGE cell from %s:%d, but we don't "
  1489. "know any of its authentication types. Not authenticating.",
  1490. safe_str(chan->conn->_base.address),
  1491. chan->conn->_base.port);
  1492. }
  1493. if (connection_or_send_netinfo(chan->conn) < 0) {
  1494. log_warn(LD_OR, "Couldn't send netinfo cell");
  1495. connection_or_close_for_error(chan->conn, 0);
  1496. return;
  1497. }
  1498. #undef ERR
  1499. }
  1500. /**
  1501. * Process an AUTHENTICATE cell from a channel_tls_t
  1502. *
  1503. * If it's ill-formed or we weren't supposed to get one or we're not doing a
  1504. * v3 handshake, then mark the connection. If it does not authenticate the
  1505. * other side of the connection successfully (because it isn't signed right,
  1506. * we didn't get a CERTS cell, etc) mark the connection. Otherwise, accept
  1507. * the identity of the router on the other side of the connection.
  1508. */
  1509. static void
  1510. channel_tls_process_authenticate_cell(var_cell_t *cell, channel_tls_t *chan)
  1511. {
  1512. uint8_t expected[V3_AUTH_FIXED_PART_LEN];
  1513. const uint8_t *auth;
  1514. int authlen;
  1515. tor_assert(cell);
  1516. tor_assert(chan);
  1517. tor_assert(chan->conn);
  1518. #define ERR(s) \
  1519. do { \
  1520. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  1521. "Received a bad AUTHENTICATE cell from %s:%d: %s", \
  1522. safe_str(chan->conn->_base.address), \
  1523. chan->conn->_base.port, (s)); \
  1524. connection_or_close_for_error(chan->conn, 0); \
  1525. return; \
  1526. } while (0)
  1527. if (chan->conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  1528. ERR("We're not doing a v3 handshake");
  1529. if (chan->conn->link_proto < 3)
  1530. ERR("We're not using link protocol >= 3");
  1531. if (chan->conn->handshake_state->started_here)
  1532. ERR("We originated this connection");
  1533. if (chan->conn->handshake_state->received_authenticate)
  1534. ERR("We already got one!");
  1535. if (chan->conn->handshake_state->authenticated) {
  1536. /* Should be impossible given other checks */
  1537. ERR("The peer is already authenticated");
  1538. }
  1539. if (!(chan->conn->handshake_state->received_certs_cell))
  1540. ERR("We never got a certs cell");
  1541. if (chan->conn->handshake_state->auth_cert == NULL)
  1542. ERR("We never got an authentication certificate");
  1543. if (chan->conn->handshake_state->id_cert == NULL)
  1544. ERR("We never got an identity certificate");
  1545. if (cell->payload_len < 4)
  1546. ERR("Cell was way too short");
  1547. auth = cell->payload;
  1548. {
  1549. uint16_t type = ntohs(get_uint16(auth));
  1550. uint16_t len = ntohs(get_uint16(auth+2));
  1551. if (4 + len > cell->payload_len)
  1552. ERR("Authenticator was truncated");
  1553. if (type != AUTHTYPE_RSA_SHA256_TLSSECRET)
  1554. ERR("Authenticator type was not recognized");
  1555. auth += 4;
  1556. authlen = len;
  1557. }
  1558. if (authlen < V3_AUTH_BODY_LEN + 1)
  1559. ERR("Authenticator was too short");
  1560. if (connection_or_compute_authenticate_cell_body(
  1561. chan->conn, expected, sizeof(expected), NULL, 1) < 0)
  1562. ERR("Couldn't compute expected AUTHENTICATE cell body");
  1563. if (tor_memneq(expected, auth, sizeof(expected)))
  1564. ERR("Some field in the AUTHENTICATE cell body was not as expected");
  1565. {
  1566. crypto_pk_t *pk = tor_tls_cert_get_key(
  1567. chan->conn->handshake_state->auth_cert);
  1568. char d[DIGEST256_LEN];
  1569. char *signed_data;
  1570. size_t keysize;
  1571. int signed_len;
  1572. if (!pk)
  1573. ERR("Internal error: couldn't get RSA key from AUTH cert.");
  1574. crypto_digest256(d, (char*)auth, V3_AUTH_BODY_LEN, DIGEST_SHA256);
  1575. keysize = crypto_pk_keysize(pk);
  1576. signed_data = tor_malloc(keysize);
  1577. signed_len = crypto_pk_public_checksig(pk, signed_data, keysize,
  1578. (char*)auth + V3_AUTH_BODY_LEN,
  1579. authlen - V3_AUTH_BODY_LEN);
  1580. crypto_pk_free(pk);
  1581. if (signed_len < 0) {
  1582. tor_free(signed_data);
  1583. ERR("Signature wasn't valid");
  1584. }
  1585. if (signed_len < DIGEST256_LEN) {
  1586. tor_free(signed_data);
  1587. ERR("Not enough data was signed");
  1588. }
  1589. /* Note that we deliberately allow *more* than DIGEST256_LEN bytes here,
  1590. * in case they're later used to hold a SHA3 digest or something. */
  1591. if (tor_memneq(signed_data, d, DIGEST256_LEN)) {
  1592. tor_free(signed_data);
  1593. ERR("Signature did not match data to be signed.");
  1594. }
  1595. tor_free(signed_data);
  1596. }
  1597. /* Okay, we are authenticated. */
  1598. chan->conn->handshake_state->received_authenticate = 1;
  1599. chan->conn->handshake_state->authenticated = 1;
  1600. chan->conn->handshake_state->digest_received_data = 0;
  1601. {
  1602. crypto_pk_t *identity_rcvd =
  1603. tor_tls_cert_get_key(chan->conn->handshake_state->id_cert);
  1604. const digests_t *id_digests =
  1605. tor_cert_get_id_digests(chan->conn->handshake_state->id_cert);
  1606. /* This must exist; we checked key type when reading the cert. */
  1607. tor_assert(id_digests);
  1608. memcpy(chan->conn->handshake_state->authenticated_peer_id,
  1609. id_digests->d[DIGEST_SHA1], DIGEST_LEN);
  1610. channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd);
  1611. crypto_pk_free(identity_rcvd);
  1612. connection_or_init_conn_from_address(chan->conn,
  1613. &(chan->conn->_base.addr),
  1614. chan->conn->_base.port,
  1615. (const char*)(chan->conn->handshake_state->
  1616. authenticated_peer_id),
  1617. 0);
  1618. log_info(LD_OR,
  1619. "Got an AUTHENTICATE cell from %s:%d: Looks good.",
  1620. safe_str(chan->conn->_base.address),
  1621. chan->conn->_base.port);
  1622. }
  1623. #undef ERR
  1624. }