channeltls.c 60 KB

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