channeltls.c 59 KB

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