channeltls.c 60 KB

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