channeltls.c 59 KB

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