hs_service.c 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134
  1. /* Copyright (c) 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_service.c
  5. * \brief Implement next generation hidden service functionality
  6. **/
  7. #define HS_SERVICE_PRIVATE
  8. #include "core/or/or.h"
  9. #include "app/config/config.h"
  10. #include "app/config/statefile.h"
  11. #include "core/mainloop/connection.h"
  12. #include "core/mainloop/mainloop.h"
  13. #include "core/or/circuitbuild.h"
  14. #include "core/or/circuitlist.h"
  15. #include "core/or/circuituse.h"
  16. #include "core/or/relay.h"
  17. #include "feature/client/circpathbias.h"
  18. #include "feature/dirclient/dirclient.h"
  19. #include "feature/dircommon/directory.h"
  20. #include "feature/hs_common/shared_random_client.h"
  21. #include "feature/keymgt/loadkey.h"
  22. #include "feature/nodelist/describe.h"
  23. #include "feature/nodelist/networkstatus.h"
  24. #include "feature/nodelist/nickname.h"
  25. #include "feature/nodelist/node_select.h"
  26. #include "feature/nodelist/nodelist.h"
  27. #include "feature/rend/rendservice.h"
  28. #include "lib/crypt_ops/crypto_ope.h"
  29. #include "lib/crypt_ops/crypto_rand.h"
  30. #include "lib/crypt_ops/crypto_rand.h"
  31. #include "lib/crypt_ops/crypto_util.h"
  32. #include "feature/hs/hs_circuit.h"
  33. #include "feature/hs/hs_common.h"
  34. #include "feature/hs/hs_config.h"
  35. #include "feature/hs/hs_control.h"
  36. #include "feature/hs/hs_descriptor.h"
  37. #include "feature/hs/hs_ident.h"
  38. #include "feature/hs/hs_intropoint.h"
  39. #include "feature/hs/hs_service.h"
  40. #include "feature/hs/hs_stats.h"
  41. #include "feature/dircommon/dir_connection_st.h"
  42. #include "core/or/edge_connection_st.h"
  43. #include "core/or/extend_info_st.h"
  44. #include "feature/nodelist/networkstatus_st.h"
  45. #include "feature/nodelist/node_st.h"
  46. #include "core/or/origin_circuit_st.h"
  47. #include "app/config/or_state_st.h"
  48. #include "feature/nodelist/routerstatus_st.h"
  49. #include "lib/encoding/confline.h"
  50. #include "lib/crypt_ops/crypto_format.h"
  51. /* Trunnel */
  52. #include "trunnel/ed25519_cert.h"
  53. #include "trunnel/hs/cell_common.h"
  54. #include "trunnel/hs/cell_establish_intro.h"
  55. #ifdef HAVE_SYS_STAT_H
  56. #include <sys/stat.h>
  57. #endif
  58. #ifdef HAVE_UNISTD_H
  59. #include <unistd.h>
  60. #endif
  61. /* Helper macro. Iterate over every service in the global map. The var is the
  62. * name of the service pointer. */
  63. #define FOR_EACH_SERVICE_BEGIN(var) \
  64. STMT_BEGIN \
  65. hs_service_t **var##_iter, *var; \
  66. HT_FOREACH(var##_iter, hs_service_ht, hs_service_map) { \
  67. var = *var##_iter;
  68. #define FOR_EACH_SERVICE_END } STMT_END ;
  69. /* Helper macro. Iterate over both current and previous descriptor of a
  70. * service. The var is the name of the descriptor pointer. This macro skips
  71. * any descriptor object of the service that is NULL. */
  72. #define FOR_EACH_DESCRIPTOR_BEGIN(service, var) \
  73. STMT_BEGIN \
  74. hs_service_descriptor_t *var; \
  75. for (int var ## _loop_idx = 0; var ## _loop_idx < 2; \
  76. ++var ## _loop_idx) { \
  77. (var ## _loop_idx == 0) ? (var = service->desc_current) : \
  78. (var = service->desc_next); \
  79. if (var == NULL) continue;
  80. #define FOR_EACH_DESCRIPTOR_END } STMT_END ;
  81. /* Onion service directory file names. */
  82. static const char fname_keyfile_prefix[] = "hs_ed25519";
  83. static const char dname_client_pubkeys[] = "authorized_clients";
  84. static const char fname_hostname[] = "hostname";
  85. static const char address_tld[] = "onion";
  86. /* Staging list of service object. When configuring service, we add them to
  87. * this list considered a staging area and they will get added to our global
  88. * map once the keys have been loaded. These two steps are separated because
  89. * loading keys requires that we are an actual running tor process. */
  90. static smartlist_t *hs_service_staging_list;
  91. /** True if the list of available router descriptors might have changed which
  92. * might result in an altered hash ring. Check if the hash ring changed and
  93. * reupload if needed */
  94. static int consider_republishing_hs_descriptors = 0;
  95. /* Static declaration. */
  96. static int load_client_keys(hs_service_t *service);
  97. static void set_descriptor_revision_counter(hs_service_descriptor_t *hs_desc,
  98. time_t now, bool is_current);
  99. static int build_service_desc_superencrypted(const hs_service_t *service,
  100. hs_service_descriptor_t *desc);
  101. static void move_descriptors(hs_service_t *src, hs_service_t *dst);
  102. static int service_encode_descriptor(const hs_service_t *service,
  103. const hs_service_descriptor_t *desc,
  104. const ed25519_keypair_t *signing_kp,
  105. char **encoded_out);
  106. /* Helper: Function to compare two objects in the service map. Return 1 if the
  107. * two service have the same master public identity key. */
  108. static inline int
  109. hs_service_ht_eq(const hs_service_t *first, const hs_service_t *second)
  110. {
  111. tor_assert(first);
  112. tor_assert(second);
  113. /* Simple key compare. */
  114. return ed25519_pubkey_eq(&first->keys.identity_pk,
  115. &second->keys.identity_pk);
  116. }
  117. /* Helper: Function for the service hash table code below. The key used is the
  118. * master public identity key which is ultimately the onion address. */
  119. static inline unsigned int
  120. hs_service_ht_hash(const hs_service_t *service)
  121. {
  122. tor_assert(service);
  123. return (unsigned int) siphash24g(service->keys.identity_pk.pubkey,
  124. sizeof(service->keys.identity_pk.pubkey));
  125. }
  126. /* This is _the_ global hash map of hidden services which indexed the service
  127. * contained in it by master public identity key which is roughly the onion
  128. * address of the service. */
  129. static struct hs_service_ht *hs_service_map;
  130. /* Register the service hash table. */
  131. HT_PROTOTYPE(hs_service_ht, /* Name of hashtable. */
  132. hs_service_t, /* Object contained in the map. */
  133. hs_service_node, /* The name of the HT_ENTRY member. */
  134. hs_service_ht_hash, /* Hashing function. */
  135. hs_service_ht_eq) /* Compare function for objects. */
  136. HT_GENERATE2(hs_service_ht, hs_service_t, hs_service_node,
  137. hs_service_ht_hash, hs_service_ht_eq,
  138. 0.6, tor_reallocarray, tor_free_)
  139. /* Query the given service map with a public key and return a service object
  140. * if found else NULL. It is also possible to set a directory path in the
  141. * search query. If pk is NULL, then it will be set to zero indicating the
  142. * hash table to compare the directory path instead. */
  143. STATIC hs_service_t *
  144. find_service(hs_service_ht *map, const ed25519_public_key_t *pk)
  145. {
  146. hs_service_t dummy_service;
  147. tor_assert(map);
  148. tor_assert(pk);
  149. memset(&dummy_service, 0, sizeof(dummy_service));
  150. ed25519_pubkey_copy(&dummy_service.keys.identity_pk, pk);
  151. return HT_FIND(hs_service_ht, map, &dummy_service);
  152. }
  153. /* Register the given service in the given map. If the service already exists
  154. * in the map, -1 is returned. On success, 0 is returned and the service
  155. * ownership has been transferred to the global map. */
  156. STATIC int
  157. register_service(hs_service_ht *map, hs_service_t *service)
  158. {
  159. tor_assert(map);
  160. tor_assert(service);
  161. tor_assert(!ed25519_public_key_is_zero(&service->keys.identity_pk));
  162. if (find_service(map, &service->keys.identity_pk)) {
  163. /* Existing service with the same key. Do not register it. */
  164. return -1;
  165. }
  166. /* Taking ownership of the object at this point. */
  167. HT_INSERT(hs_service_ht, map, service);
  168. /* If we just modified the global map, we notify. */
  169. if (map == hs_service_map) {
  170. hs_service_map_has_changed();
  171. }
  172. return 0;
  173. }
  174. /* Remove a given service from the given map. If service is NULL or the
  175. * service key is unset, return gracefully. */
  176. STATIC void
  177. remove_service(hs_service_ht *map, hs_service_t *service)
  178. {
  179. hs_service_t *elm;
  180. tor_assert(map);
  181. /* Ignore if no service or key is zero. */
  182. if (BUG(service == NULL) ||
  183. BUG(ed25519_public_key_is_zero(&service->keys.identity_pk))) {
  184. return;
  185. }
  186. elm = HT_REMOVE(hs_service_ht, map, service);
  187. if (elm) {
  188. tor_assert(elm == service);
  189. } else {
  190. log_warn(LD_BUG, "Could not find service in the global map "
  191. "while removing service %s",
  192. escaped(service->config.directory_path));
  193. }
  194. /* If we just modified the global map, we notify. */
  195. if (map == hs_service_map) {
  196. hs_service_map_has_changed();
  197. }
  198. }
  199. /* Set the default values for a service configuration object <b>c</b>. */
  200. static void
  201. set_service_default_config(hs_service_config_t *c,
  202. const or_options_t *options)
  203. {
  204. (void) options;
  205. tor_assert(c);
  206. c->ports = smartlist_new();
  207. c->directory_path = NULL;
  208. c->max_streams_per_rdv_circuit = 0;
  209. c->max_streams_close_circuit = 0;
  210. c->num_intro_points = NUM_INTRO_POINTS_DEFAULT;
  211. c->allow_unknown_ports = 0;
  212. c->is_single_onion = 0;
  213. c->dir_group_readable = 0;
  214. c->is_ephemeral = 0;
  215. }
  216. /* From a service configuration object config, clear everything from it
  217. * meaning free allocated pointers and reset the values. */
  218. STATIC void
  219. service_clear_config(hs_service_config_t *config)
  220. {
  221. if (config == NULL) {
  222. return;
  223. }
  224. tor_free(config->directory_path);
  225. if (config->ports) {
  226. SMARTLIST_FOREACH(config->ports, rend_service_port_config_t *, p,
  227. rend_service_port_config_free(p););
  228. smartlist_free(config->ports);
  229. }
  230. if (config->clients) {
  231. SMARTLIST_FOREACH(config->clients, hs_service_authorized_client_t *, p,
  232. service_authorized_client_free(p));
  233. smartlist_free(config->clients);
  234. }
  235. memset(config, 0, sizeof(*config));
  236. }
  237. /* Helper function to return a human readable description of the given intro
  238. * point object.
  239. *
  240. * This function is not thread-safe. Each call to this invalidates the
  241. * previous values returned by it. */
  242. static const char *
  243. describe_intro_point(const hs_service_intro_point_t *ip)
  244. {
  245. /* Hex identity digest of the IP prefixed by the $ sign and ends with NUL
  246. * byte hence the plus two. */
  247. static char buf[HEX_DIGEST_LEN + 2];
  248. const char *legacy_id = NULL;
  249. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  250. const hs_desc_link_specifier_t *, lspec) {
  251. if (lspec->type == LS_LEGACY_ID) {
  252. legacy_id = (const char *) lspec->u.legacy_id;
  253. break;
  254. }
  255. } SMARTLIST_FOREACH_END(lspec);
  256. /* For now, we only print the identity digest but we could improve this with
  257. * much more information such as the ed25519 identity has well. */
  258. buf[0] = '$';
  259. if (legacy_id) {
  260. base16_encode(buf + 1, HEX_DIGEST_LEN + 1, legacy_id, DIGEST_LEN);
  261. }
  262. return buf;
  263. }
  264. /* Return the lower bound of maximum INTRODUCE2 cells per circuit before we
  265. * rotate intro point (defined by a consensus parameter or the default
  266. * value). */
  267. static int32_t
  268. get_intro_point_min_introduce2(void)
  269. {
  270. /* The [0, 2147483647] range is quite large to accommodate anything we decide
  271. * in the future. */
  272. return networkstatus_get_param(NULL, "hs_intro_min_introduce2",
  273. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS,
  274. 0, INT32_MAX);
  275. }
  276. /* Return the upper bound of maximum INTRODUCE2 cells per circuit before we
  277. * rotate intro point (defined by a consensus parameter or the default
  278. * value). */
  279. static int32_t
  280. get_intro_point_max_introduce2(void)
  281. {
  282. /* The [0, 2147483647] range is quite large to accommodate anything we decide
  283. * in the future. */
  284. return networkstatus_get_param(NULL, "hs_intro_max_introduce2",
  285. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS,
  286. 0, INT32_MAX);
  287. }
  288. /* Return the minimum lifetime in seconds of an introduction point defined by a
  289. * consensus parameter or the default value. */
  290. static int32_t
  291. get_intro_point_min_lifetime(void)
  292. {
  293. #define MIN_INTRO_POINT_LIFETIME_TESTING 10
  294. if (get_options()->TestingTorNetwork) {
  295. return MIN_INTRO_POINT_LIFETIME_TESTING;
  296. }
  297. /* The [0, 2147483647] range is quite large to accommodate anything we decide
  298. * in the future. */
  299. return networkstatus_get_param(NULL, "hs_intro_min_lifetime",
  300. INTRO_POINT_LIFETIME_MIN_SECONDS,
  301. 0, INT32_MAX);
  302. }
  303. /* Return the maximum lifetime in seconds of an introduction point defined by a
  304. * consensus parameter or the default value. */
  305. static int32_t
  306. get_intro_point_max_lifetime(void)
  307. {
  308. #define MAX_INTRO_POINT_LIFETIME_TESTING 30
  309. if (get_options()->TestingTorNetwork) {
  310. return MAX_INTRO_POINT_LIFETIME_TESTING;
  311. }
  312. /* The [0, 2147483647] range is quite large to accommodate anything we decide
  313. * in the future. */
  314. return networkstatus_get_param(NULL, "hs_intro_max_lifetime",
  315. INTRO_POINT_LIFETIME_MAX_SECONDS,
  316. 0, INT32_MAX);
  317. }
  318. /* Return the number of extra introduction point defined by a consensus
  319. * parameter or the default value. */
  320. static int32_t
  321. get_intro_point_num_extra(void)
  322. {
  323. /* The [0, 128] range bounds the number of extra introduction point allowed.
  324. * Above 128 intro points, it's getting a bit crazy. */
  325. return networkstatus_get_param(NULL, "hs_intro_num_extra",
  326. NUM_INTRO_POINTS_EXTRA, 0, 128);
  327. }
  328. /* Helper: Function that needs to return 1 for the HT for each loop which
  329. * frees every service in an hash map. */
  330. static int
  331. ht_free_service_(struct hs_service_t *service, void *data)
  332. {
  333. (void) data;
  334. hs_service_free(service);
  335. /* This function MUST return 1 so the given object is then removed from the
  336. * service map leading to this free of the object being safe. */
  337. return 1;
  338. }
  339. /* Free every service that can be found in the global map. Once done, clear
  340. * and free the global map. */
  341. static void
  342. service_free_all(void)
  343. {
  344. if (hs_service_map) {
  345. /* The free helper function returns 1 so this is safe. */
  346. hs_service_ht_HT_FOREACH_FN(hs_service_map, ht_free_service_, NULL);
  347. HT_CLEAR(hs_service_ht, hs_service_map);
  348. tor_free(hs_service_map);
  349. hs_service_map = NULL;
  350. }
  351. if (hs_service_staging_list) {
  352. /* Cleanup staging list. */
  353. SMARTLIST_FOREACH(hs_service_staging_list, hs_service_t *, s,
  354. hs_service_free(s));
  355. smartlist_free(hs_service_staging_list);
  356. hs_service_staging_list = NULL;
  357. }
  358. }
  359. /* Free a given service intro point object. */
  360. STATIC void
  361. service_intro_point_free_(hs_service_intro_point_t *ip)
  362. {
  363. if (!ip) {
  364. return;
  365. }
  366. memwipe(&ip->auth_key_kp, 0, sizeof(ip->auth_key_kp));
  367. memwipe(&ip->enc_key_kp, 0, sizeof(ip->enc_key_kp));
  368. crypto_pk_free(ip->legacy_key);
  369. replaycache_free(ip->replay_cache);
  370. hs_intropoint_clear(&ip->base);
  371. tor_free(ip);
  372. }
  373. /* Helper: free an hs_service_intro_point_t object. This function is used by
  374. * digest256map_free() which requires a void * pointer. */
  375. static void
  376. service_intro_point_free_void(void *obj)
  377. {
  378. service_intro_point_free_(obj);
  379. }
  380. /* Return a newly allocated service intro point and fully initialized from the
  381. * given extend_info_t ei if non NULL.
  382. * If is_legacy is true, we also generate the legacy key.
  383. * If supports_ed25519_link_handshake_any is true, we add the relay's ed25519
  384. * key to the link specifiers.
  385. *
  386. * If ei is NULL, returns a hs_service_intro_point_t with an empty link
  387. * specifier list and no onion key. (This is used for testing.)
  388. * On any other error, NULL is returned.
  389. *
  390. * ei must be an extend_info_t containing an IPv4 address. (We will add supoort
  391. * for IPv6 in a later release.) When calling extend_info_from_node(), pass
  392. * 0 in for_direct_connection to make sure ei always has an IPv4 address. */
  393. STATIC hs_service_intro_point_t *
  394. service_intro_point_new(const extend_info_t *ei, unsigned int is_legacy,
  395. unsigned int supports_ed25519_link_handshake_any)
  396. {
  397. hs_desc_link_specifier_t *ls;
  398. hs_service_intro_point_t *ip;
  399. ip = tor_malloc_zero(sizeof(*ip));
  400. /* We'll create the key material. No need for extra strong, those are short
  401. * term keys. */
  402. ed25519_keypair_generate(&ip->auth_key_kp, 0);
  403. { /* Set introduce2 max cells limit */
  404. int32_t min_introduce2_cells = get_intro_point_min_introduce2();
  405. int32_t max_introduce2_cells = get_intro_point_max_introduce2();
  406. if (BUG(max_introduce2_cells < min_introduce2_cells)) {
  407. goto err;
  408. }
  409. ip->introduce2_max = crypto_rand_int_range(min_introduce2_cells,
  410. max_introduce2_cells);
  411. }
  412. { /* Set intro point lifetime */
  413. int32_t intro_point_min_lifetime = get_intro_point_min_lifetime();
  414. int32_t intro_point_max_lifetime = get_intro_point_max_lifetime();
  415. if (BUG(intro_point_max_lifetime < intro_point_min_lifetime)) {
  416. goto err;
  417. }
  418. ip->time_to_expire = approx_time() +
  419. crypto_rand_int_range(intro_point_min_lifetime,intro_point_max_lifetime);
  420. }
  421. ip->replay_cache = replaycache_new(0, 0);
  422. /* Initialize the base object. We don't need the certificate object. */
  423. ip->base.link_specifiers = smartlist_new();
  424. /* Generate the encryption key for this intro point. */
  425. curve25519_keypair_generate(&ip->enc_key_kp, 0);
  426. /* Figure out if this chosen node supports v3 or is legacy only. */
  427. if (is_legacy) {
  428. ip->base.is_only_legacy = 1;
  429. /* Legacy mode that is doesn't support v3+ with ed25519 auth key. */
  430. ip->legacy_key = crypto_pk_new();
  431. if (crypto_pk_generate_key(ip->legacy_key) < 0) {
  432. goto err;
  433. }
  434. if (crypto_pk_get_digest(ip->legacy_key,
  435. (char *) ip->legacy_key_digest) < 0) {
  436. goto err;
  437. }
  438. }
  439. if (ei == NULL) {
  440. goto done;
  441. }
  442. /* We'll try to add all link specifiers. Legacy is mandatory.
  443. * IPv4 or IPv6 is required, and we always send IPv4. */
  444. ls = hs_desc_link_specifier_new(ei, LS_IPV4);
  445. /* It is impossible to have an extend info object without a v4. */
  446. if (BUG(!ls)) {
  447. goto err;
  448. }
  449. smartlist_add(ip->base.link_specifiers, ls);
  450. ls = hs_desc_link_specifier_new(ei, LS_LEGACY_ID);
  451. /* It is impossible to have an extend info object without an identity
  452. * digest. */
  453. if (BUG(!ls)) {
  454. goto err;
  455. }
  456. smartlist_add(ip->base.link_specifiers, ls);
  457. /* ed25519 identity key is optional for intro points. If the node supports
  458. * ed25519 link authentication, we include it. */
  459. if (supports_ed25519_link_handshake_any) {
  460. ls = hs_desc_link_specifier_new(ei, LS_ED25519_ID);
  461. if (ls) {
  462. smartlist_add(ip->base.link_specifiers, ls);
  463. }
  464. }
  465. /* IPv6 is not supported in this release. */
  466. /* Finally, copy onion key from the extend_info_t object. */
  467. memcpy(&ip->onion_key, &ei->curve25519_onion_key, sizeof(ip->onion_key));
  468. done:
  469. return ip;
  470. err:
  471. service_intro_point_free(ip);
  472. return NULL;
  473. }
  474. /* Add the given intro point object to the given intro point map. The intro
  475. * point MUST have its RSA encryption key set if this is a legacy type or the
  476. * authentication key set otherwise. */
  477. STATIC void
  478. service_intro_point_add(digest256map_t *map, hs_service_intro_point_t *ip)
  479. {
  480. hs_service_intro_point_t *old_ip_entry;
  481. tor_assert(map);
  482. tor_assert(ip);
  483. old_ip_entry = digest256map_set(map, ip->auth_key_kp.pubkey.pubkey, ip);
  484. /* Make sure we didn't just try to double-add an intro point */
  485. tor_assert_nonfatal(!old_ip_entry);
  486. }
  487. /* For a given service, remove the intro point from that service's descriptors
  488. * (check both current and next descriptor) */
  489. STATIC void
  490. service_intro_point_remove(const hs_service_t *service,
  491. const hs_service_intro_point_t *ip)
  492. {
  493. tor_assert(service);
  494. tor_assert(ip);
  495. /* Trying all descriptors. */
  496. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  497. /* We'll try to remove the descriptor on both descriptors which is not
  498. * very expensive to do instead of doing loopup + remove. */
  499. digest256map_remove(desc->intro_points.map,
  500. ip->auth_key_kp.pubkey.pubkey);
  501. } FOR_EACH_DESCRIPTOR_END;
  502. }
  503. /* For a given service and authentication key, return the intro point or NULL
  504. * if not found. This will check both descriptors in the service. */
  505. STATIC hs_service_intro_point_t *
  506. service_intro_point_find(const hs_service_t *service,
  507. const ed25519_public_key_t *auth_key)
  508. {
  509. hs_service_intro_point_t *ip = NULL;
  510. tor_assert(service);
  511. tor_assert(auth_key);
  512. /* Trying all descriptors to find the right intro point.
  513. *
  514. * Even if we use the same node as intro point in both descriptors, the node
  515. * will have a different intro auth key for each descriptor since we generate
  516. * a new one everytime we pick an intro point.
  517. *
  518. * After #22893 gets implemented, intro points will be moved to be
  519. * per-service instead of per-descriptor so this function will need to
  520. * change.
  521. */
  522. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  523. if ((ip = digest256map_get(desc->intro_points.map,
  524. auth_key->pubkey)) != NULL) {
  525. break;
  526. }
  527. } FOR_EACH_DESCRIPTOR_END;
  528. return ip;
  529. }
  530. /* For a given service and intro point, return the descriptor for which the
  531. * intro point is assigned to. NULL is returned if not found. */
  532. STATIC hs_service_descriptor_t *
  533. service_desc_find_by_intro(const hs_service_t *service,
  534. const hs_service_intro_point_t *ip)
  535. {
  536. hs_service_descriptor_t *descp = NULL;
  537. tor_assert(service);
  538. tor_assert(ip);
  539. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  540. if (digest256map_get(desc->intro_points.map,
  541. ip->auth_key_kp.pubkey.pubkey)) {
  542. descp = desc;
  543. break;
  544. }
  545. } FOR_EACH_DESCRIPTOR_END;
  546. return descp;
  547. }
  548. /* From a circuit identifier, get all the possible objects associated with the
  549. * ident. If not NULL, service, ip or desc are set if the object can be found.
  550. * They are untouched if they can't be found.
  551. *
  552. * This is an helper function because we do those lookups often so it's more
  553. * convenient to simply call this functions to get all the things at once. */
  554. STATIC void
  555. get_objects_from_ident(const hs_ident_circuit_t *ident,
  556. hs_service_t **service, hs_service_intro_point_t **ip,
  557. hs_service_descriptor_t **desc)
  558. {
  559. hs_service_t *s;
  560. tor_assert(ident);
  561. /* Get service object from the circuit identifier. */
  562. s = find_service(hs_service_map, &ident->identity_pk);
  563. if (s && service) {
  564. *service = s;
  565. }
  566. /* From the service object, get the intro point object of that circuit. The
  567. * following will query both descriptors intro points list. */
  568. if (s && ip) {
  569. *ip = service_intro_point_find(s, &ident->intro_auth_pk);
  570. }
  571. /* Get the descriptor for this introduction point and service. */
  572. if (s && ip && *ip && desc) {
  573. *desc = service_desc_find_by_intro(s, *ip);
  574. }
  575. }
  576. /* From a given intro point, return the first link specifier of type
  577. * encountered in the link specifier list. Return NULL if it can't be found.
  578. *
  579. * The caller does NOT have ownership of the object, the intro point does. */
  580. static hs_desc_link_specifier_t *
  581. get_link_spec_by_type(const hs_service_intro_point_t *ip, uint8_t type)
  582. {
  583. hs_desc_link_specifier_t *lnk_spec = NULL;
  584. tor_assert(ip);
  585. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  586. hs_desc_link_specifier_t *, ls) {
  587. if (ls->type == type) {
  588. lnk_spec = ls;
  589. goto end;
  590. }
  591. } SMARTLIST_FOREACH_END(ls);
  592. end:
  593. return lnk_spec;
  594. }
  595. /* Given a service intro point, return the node_t associated to it. This can
  596. * return NULL if the given intro point has no legacy ID or if the node can't
  597. * be found in the consensus. */
  598. STATIC const node_t *
  599. get_node_from_intro_point(const hs_service_intro_point_t *ip)
  600. {
  601. const hs_desc_link_specifier_t *ls;
  602. tor_assert(ip);
  603. ls = get_link_spec_by_type(ip, LS_LEGACY_ID);
  604. if (BUG(!ls)) {
  605. return NULL;
  606. }
  607. /* XXX In the future, we want to only use the ed25519 ID (#22173). */
  608. return node_get_by_id((const char *) ls->u.legacy_id);
  609. }
  610. /* Given a service intro point, return the extend_info_t for it. This can
  611. * return NULL if the node can't be found for the intro point or the extend
  612. * info can't be created for the found node. If direct_conn is set, the extend
  613. * info is validated on if we can connect directly. */
  614. static extend_info_t *
  615. get_extend_info_from_intro_point(const hs_service_intro_point_t *ip,
  616. unsigned int direct_conn)
  617. {
  618. extend_info_t *info = NULL;
  619. const node_t *node;
  620. tor_assert(ip);
  621. node = get_node_from_intro_point(ip);
  622. if (node == NULL) {
  623. /* This can happen if the relay serving as intro point has been removed
  624. * from the consensus. In that case, the intro point will be removed from
  625. * the descriptor during the scheduled events. */
  626. goto end;
  627. }
  628. /* In the case of a direct connection (single onion service), it is possible
  629. * our firewall policy won't allow it so this can return a NULL value. */
  630. info = extend_info_from_node(node, direct_conn);
  631. end:
  632. return info;
  633. }
  634. /* Return the number of introduction points that are established for the
  635. * given descriptor. */
  636. static unsigned int
  637. count_desc_circuit_established(const hs_service_descriptor_t *desc)
  638. {
  639. unsigned int count = 0;
  640. tor_assert(desc);
  641. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  642. const hs_service_intro_point_t *, ip) {
  643. count += ip->circuit_established;
  644. } DIGEST256MAP_FOREACH_END;
  645. return count;
  646. }
  647. /* For a given service and descriptor of that service, close all active
  648. * directory connections. */
  649. static void
  650. close_directory_connections(const hs_service_t *service,
  651. const hs_service_descriptor_t *desc)
  652. {
  653. unsigned int count = 0;
  654. smartlist_t *dir_conns;
  655. tor_assert(service);
  656. tor_assert(desc);
  657. /* Close pending HS desc upload connections for the blinded key of 'desc'. */
  658. dir_conns = connection_list_by_type_purpose(CONN_TYPE_DIR,
  659. DIR_PURPOSE_UPLOAD_HSDESC);
  660. SMARTLIST_FOREACH_BEGIN(dir_conns, connection_t *, conn) {
  661. dir_connection_t *dir_conn = TO_DIR_CONN(conn);
  662. if (ed25519_pubkey_eq(&dir_conn->hs_ident->identity_pk,
  663. &service->keys.identity_pk) &&
  664. ed25519_pubkey_eq(&dir_conn->hs_ident->blinded_pk,
  665. &desc->blinded_kp.pubkey)) {
  666. connection_mark_for_close(conn);
  667. count++;
  668. continue;
  669. }
  670. } SMARTLIST_FOREACH_END(conn);
  671. log_info(LD_REND, "Closed %u active service directory connections for "
  672. "descriptor %s of service %s",
  673. count, safe_str_client(ed25519_fmt(&desc->blinded_kp.pubkey)),
  674. safe_str_client(service->onion_address));
  675. /* We don't have ownership of the objects in this list. */
  676. smartlist_free(dir_conns);
  677. }
  678. /* Close all rendezvous circuits for the given service. */
  679. static void
  680. close_service_rp_circuits(hs_service_t *service)
  681. {
  682. origin_circuit_t *ocirc = NULL;
  683. tor_assert(service);
  684. /* The reason we go over all circuit instead of using the circuitmap API is
  685. * because most hidden service circuits are rendezvous circuits so there is
  686. * no real improvement at getting all rendezvous circuits from the
  687. * circuitmap and then going over them all to find the right ones.
  688. * Furthermore, another option would have been to keep a list of RP cookies
  689. * for a service but it creates an engineering complexity since we don't
  690. * have a "RP circuit closed" event to clean it up properly so we avoid a
  691. * memory DoS possibility. */
  692. while ((ocirc = circuit_get_next_service_rp_circ(ocirc))) {
  693. /* Only close circuits that are v3 and for this service. */
  694. if (ocirc->hs_ident != NULL &&
  695. ed25519_pubkey_eq(&ocirc->hs_ident->identity_pk,
  696. &service->keys.identity_pk)) {
  697. /* Reason is FINISHED because service has been removed and thus the
  698. * circuit is considered old/uneeded. When freed, it is removed from the
  699. * hs circuitmap. */
  700. circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
  701. }
  702. }
  703. }
  704. /* Close the circuit(s) for the given map of introduction points. */
  705. static void
  706. close_intro_circuits(hs_service_intropoints_t *intro_points)
  707. {
  708. tor_assert(intro_points);
  709. DIGEST256MAP_FOREACH(intro_points->map, key,
  710. const hs_service_intro_point_t *, ip) {
  711. origin_circuit_t *ocirc = hs_circ_service_get_intro_circ(ip);
  712. if (ocirc) {
  713. /* Reason is FINISHED because service has been removed and thus the
  714. * circuit is considered old/uneeded. When freed, the circuit is removed
  715. * from the HS circuitmap. */
  716. circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
  717. }
  718. } DIGEST256MAP_FOREACH_END;
  719. }
  720. /* Close all introduction circuits for the given service. */
  721. static void
  722. close_service_intro_circuits(hs_service_t *service)
  723. {
  724. tor_assert(service);
  725. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  726. close_intro_circuits(&desc->intro_points);
  727. } FOR_EACH_DESCRIPTOR_END;
  728. }
  729. /* Close any circuits related to the given service. */
  730. static void
  731. close_service_circuits(hs_service_t *service)
  732. {
  733. tor_assert(service);
  734. /* Only support for version >= 3. */
  735. if (BUG(service->config.version < HS_VERSION_THREE)) {
  736. return;
  737. }
  738. /* Close intro points. */
  739. close_service_intro_circuits(service);
  740. /* Close rendezvous points. */
  741. close_service_rp_circuits(service);
  742. }
  743. /* Move every ephemeral services from the src service map to the dst service
  744. * map. It is possible that a service can't be register to the dst map which
  745. * won't stop the process of moving them all but will trigger a log warn. */
  746. static void
  747. move_ephemeral_services(hs_service_ht *src, hs_service_ht *dst)
  748. {
  749. hs_service_t **iter, **next;
  750. tor_assert(src);
  751. tor_assert(dst);
  752. /* Iterate over the map to find ephemeral service and move them to the other
  753. * map. We loop using this method to have a safe removal process. */
  754. for (iter = HT_START(hs_service_ht, src); iter != NULL; iter = next) {
  755. hs_service_t *s = *iter;
  756. if (!s->config.is_ephemeral) {
  757. /* Yeah, we are in a very manual loop :). */
  758. next = HT_NEXT(hs_service_ht, src, iter);
  759. continue;
  760. }
  761. /* Remove service from map and then register to it to the other map.
  762. * Reminder that "*iter" and "s" are the same thing. */
  763. next = HT_NEXT_RMV(hs_service_ht, src, iter);
  764. if (register_service(dst, s) < 0) {
  765. log_warn(LD_BUG, "Ephemeral service key is already being used. "
  766. "Skipping.");
  767. }
  768. }
  769. }
  770. /* Return a const string of the directory path escaped. If this is an
  771. * ephemeral service, it returns "[EPHEMERAL]". This can only be called from
  772. * the main thread because escaped() uses a static variable. */
  773. static const char *
  774. service_escaped_dir(const hs_service_t *s)
  775. {
  776. return (s->config.is_ephemeral) ? "[EPHEMERAL]" :
  777. escaped(s->config.directory_path);
  778. }
  779. /** Move the hidden service state from <b>src</b> to <b>dst</b>. We do this
  780. * when we receive a SIGHUP: <b>dst</b> is the post-HUP service */
  781. static void
  782. move_hs_state(hs_service_t *src_service, hs_service_t *dst_service)
  783. {
  784. tor_assert(src_service);
  785. tor_assert(dst_service);
  786. hs_service_state_t *src = &src_service->state;
  787. hs_service_state_t *dst = &dst_service->state;
  788. /* Let's do a shallow copy */
  789. dst->intro_circ_retry_started_time = src->intro_circ_retry_started_time;
  790. dst->num_intro_circ_launched = src->num_intro_circ_launched;
  791. /* Freeing a NULL replaycache triggers an info LD_BUG. */
  792. if (dst->replay_cache_rend_cookie != NULL) {
  793. replaycache_free(dst->replay_cache_rend_cookie);
  794. }
  795. dst->replay_cache_rend_cookie = src->replay_cache_rend_cookie;
  796. dst->next_rotation_time = src->next_rotation_time;
  797. src->replay_cache_rend_cookie = NULL; /* steal pointer reference */
  798. }
  799. /* Register services that are in the staging list. Once this function returns,
  800. * the global service map will be set with the right content and all non
  801. * surviving services will be cleaned up. */
  802. static void
  803. register_all_services(void)
  804. {
  805. struct hs_service_ht *new_service_map;
  806. tor_assert(hs_service_staging_list);
  807. /* Allocate a new map that will replace the current one. */
  808. new_service_map = tor_malloc_zero(sizeof(*new_service_map));
  809. HT_INIT(hs_service_ht, new_service_map);
  810. /* First step is to transfer all ephemeral services from the current global
  811. * map to the new one we are constructing. We do not prune ephemeral
  812. * services as the only way to kill them is by deleting it from the control
  813. * port or stopping the tor daemon. */
  814. move_ephemeral_services(hs_service_map, new_service_map);
  815. SMARTLIST_FOREACH_BEGIN(hs_service_staging_list, hs_service_t *, snew) {
  816. hs_service_t *s;
  817. /* Check if that service is already in our global map and if so, we'll
  818. * transfer the intro points to it. */
  819. s = find_service(hs_service_map, &snew->keys.identity_pk);
  820. if (s) {
  821. /* Pass ownership of the descriptors from s (the current service) to
  822. * snew (the newly configured one). */
  823. move_descriptors(s, snew);
  824. move_hs_state(s, snew);
  825. /* Remove the service from the global map because after this, we need to
  826. * go over the remaining service in that map that aren't surviving the
  827. * reload to close their circuits. */
  828. remove_service(hs_service_map, s);
  829. hs_service_free(s);
  830. }
  831. /* Great, this service is now ready to be added to our new map. */
  832. if (BUG(register_service(new_service_map, snew) < 0)) {
  833. /* This should never happen because prior to registration, we validate
  834. * every service against the entire set. Not being able to register a
  835. * service means we failed to validate correctly. In that case, don't
  836. * break tor and ignore the service but tell user. */
  837. log_warn(LD_BUG, "Unable to register service with directory %s",
  838. service_escaped_dir(snew));
  839. SMARTLIST_DEL_CURRENT(hs_service_staging_list, snew);
  840. hs_service_free(snew);
  841. }
  842. } SMARTLIST_FOREACH_END(snew);
  843. /* Close any circuits associated with the non surviving services. Every
  844. * service in the current global map are roaming. */
  845. FOR_EACH_SERVICE_BEGIN(service) {
  846. close_service_circuits(service);
  847. } FOR_EACH_SERVICE_END;
  848. /* Time to make the switch. We'll clear the staging list because its content
  849. * has now changed ownership to the map. */
  850. smartlist_clear(hs_service_staging_list);
  851. service_free_all();
  852. hs_service_map = new_service_map;
  853. /* We've just register services into the new map and now we've replaced the
  854. * global map with it so we have to notify that the change happened. When
  855. * registering a service, the notify is only triggered if the destination
  856. * map is the global map for which in here it was not. */
  857. hs_service_map_has_changed();
  858. }
  859. /* Write the onion address of a given service to the given filename fname_ in
  860. * the service directory. Return 0 on success else -1 on error. */
  861. STATIC int
  862. write_address_to_file(const hs_service_t *service, const char *fname_)
  863. {
  864. int ret = -1;
  865. char *fname = NULL;
  866. char *addr_buf = NULL;
  867. tor_assert(service);
  868. tor_assert(fname_);
  869. /* Construct the full address with the onion tld and write the hostname file
  870. * to disk. */
  871. tor_asprintf(&addr_buf, "%s.%s\n", service->onion_address, address_tld);
  872. /* Notice here that we use the given "fname_". */
  873. fname = hs_path_from_filename(service->config.directory_path, fname_);
  874. if (write_str_to_file(fname, addr_buf, 0) < 0) {
  875. log_warn(LD_REND, "Could not write onion address to hostname file %s",
  876. escaped(fname));
  877. goto end;
  878. }
  879. #ifndef _WIN32
  880. if (service->config.dir_group_readable) {
  881. /* Mode to 0640. */
  882. if (chmod(fname, S_IRUSR | S_IWUSR | S_IRGRP) < 0) {
  883. log_warn(LD_FS, "Unable to make onion service hostname file %s "
  884. "group-readable.", escaped(fname));
  885. }
  886. }
  887. #endif /* !defined(_WIN32) */
  888. /* Success. */
  889. ret = 0;
  890. end:
  891. tor_free(fname);
  892. tor_free(addr_buf);
  893. return ret;
  894. }
  895. /* Load and/or generate private keys for the given service. On success, the
  896. * hostname file will be written to disk along with the master private key iff
  897. * the service is not configured for offline keys. Return 0 on success else -1
  898. * on failure. */
  899. static int
  900. load_service_keys(hs_service_t *service)
  901. {
  902. int ret = -1;
  903. char *fname = NULL;
  904. ed25519_keypair_t *kp;
  905. const hs_service_config_t *config;
  906. tor_assert(service);
  907. config = &service->config;
  908. /* Create and fix permission on service directory. We are about to write
  909. * files to that directory so make sure it exists and has the right
  910. * permissions. We do this here because at this stage we know that Tor is
  911. * actually running and the service we have has been validated. */
  912. if (hs_check_service_private_dir(get_options()->User,
  913. config->directory_path,
  914. config->dir_group_readable, 1) < 0) {
  915. goto end;
  916. }
  917. /* Try to load the keys from file or generate it if not found. */
  918. fname = hs_path_from_filename(config->directory_path, fname_keyfile_prefix);
  919. /* Don't ask for key creation, we want to know if we were able to load it or
  920. * we had to generate it. Better logging! */
  921. kp = ed_key_init_from_file(fname, INIT_ED_KEY_SPLIT, LOG_INFO, NULL, 0, 0,
  922. 0, NULL, NULL);
  923. if (!kp) {
  924. log_info(LD_REND, "Unable to load keys from %s. Generating it...", fname);
  925. /* We'll now try to generate the keys and for it we want the strongest
  926. * randomness for it. The keypair will be written in different files. */
  927. uint32_t key_flags = INIT_ED_KEY_CREATE | INIT_ED_KEY_EXTRA_STRONG |
  928. INIT_ED_KEY_SPLIT;
  929. kp = ed_key_init_from_file(fname, key_flags, LOG_WARN, NULL, 0, 0, 0,
  930. NULL, NULL);
  931. if (!kp) {
  932. log_warn(LD_REND, "Unable to generate keys and save in %s.", fname);
  933. goto end;
  934. }
  935. }
  936. /* Copy loaded or generated keys to service object. */
  937. ed25519_pubkey_copy(&service->keys.identity_pk, &kp->pubkey);
  938. memcpy(&service->keys.identity_sk, &kp->seckey,
  939. sizeof(service->keys.identity_sk));
  940. /* This does a proper memory wipe. */
  941. ed25519_keypair_free(kp);
  942. /* Build onion address from the newly loaded keys. */
  943. tor_assert(service->config.version <= UINT8_MAX);
  944. hs_build_address(&service->keys.identity_pk,
  945. (uint8_t) service->config.version,
  946. service->onion_address);
  947. /* Write onion address to hostname file. */
  948. if (write_address_to_file(service, fname_hostname) < 0) {
  949. goto end;
  950. }
  951. /* Load all client authorization keys in the service. */
  952. if (load_client_keys(service) < 0) {
  953. goto end;
  954. }
  955. /* Succes. */
  956. ret = 0;
  957. end:
  958. tor_free(fname);
  959. return ret;
  960. }
  961. /* Check if the client file name is valid or not. Return 1 if valid,
  962. * otherwise return 0. */
  963. STATIC int
  964. client_filename_is_valid(const char *filename)
  965. {
  966. int ret = 1;
  967. const char *valid_extension = ".auth";
  968. tor_assert(filename);
  969. /* The file extension must match and the total filename length can't be the
  970. * length of the extension else we do not have a filename. */
  971. if (!strcmpend(filename, valid_extension) &&
  972. strlen(filename) != strlen(valid_extension)) {
  973. ret = 1;
  974. } else {
  975. ret = 0;
  976. }
  977. return ret;
  978. }
  979. /* Parse an authorized client from a string. The format of a client string
  980. * looks like (see rend-spec-v3.txt):
  981. *
  982. * <auth-type>:<key-type>:<base32-encoded-public-key>
  983. *
  984. * The <auth-type> can only be "descriptor".
  985. * The <key-type> can only be "x25519".
  986. *
  987. * Return the key on success, return NULL, otherwise. */
  988. STATIC hs_service_authorized_client_t *
  989. parse_authorized_client(const char *client_key_str)
  990. {
  991. char *auth_type = NULL;
  992. char *key_type = NULL;
  993. char *pubkey_b32 = NULL;
  994. hs_service_authorized_client_t *client = NULL;
  995. smartlist_t *fields = smartlist_new();
  996. tor_assert(client_key_str);
  997. smartlist_split_string(fields, client_key_str, ":",
  998. SPLIT_SKIP_SPACE, 0);
  999. /* Wrong number of fields. */
  1000. if (smartlist_len(fields) != 3) {
  1001. log_warn(LD_REND, "Unknown format of client authorization file.");
  1002. goto err;
  1003. }
  1004. auth_type = smartlist_get(fields, 0);
  1005. key_type = smartlist_get(fields, 1);
  1006. pubkey_b32 = smartlist_get(fields, 2);
  1007. /* Currently, the only supported auth type is "descriptor". */
  1008. if (strcmp(auth_type, "descriptor")) {
  1009. log_warn(LD_REND, "Client authorization auth type '%s' not supported.",
  1010. auth_type);
  1011. goto err;
  1012. }
  1013. /* Currently, the only supported key type is "x25519". */
  1014. if (strcmp(key_type, "x25519")) {
  1015. log_warn(LD_REND, "Client authorization key type '%s' not supported.",
  1016. key_type);
  1017. goto err;
  1018. }
  1019. /* We expect a specific length of the base32 encoded key so make sure we
  1020. * have that so we don't successfully decode a value with a different length
  1021. * and end up in trouble when copying the decoded key into a fixed length
  1022. * buffer. */
  1023. if (strlen(pubkey_b32) != BASE32_NOPAD_LEN(CURVE25519_PUBKEY_LEN)) {
  1024. log_warn(LD_REND, "Client authorization encoded base32 public key "
  1025. "length is invalid: %s", pubkey_b32);
  1026. goto err;
  1027. }
  1028. client = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
  1029. if (base32_decode((char *) client->client_pk.public_key,
  1030. sizeof(client->client_pk.public_key),
  1031. pubkey_b32, strlen(pubkey_b32)) < 0) {
  1032. log_warn(LD_REND, "Client authorization public key cannot be decoded: %s",
  1033. pubkey_b32);
  1034. goto err;
  1035. }
  1036. /* Success. */
  1037. goto done;
  1038. err:
  1039. service_authorized_client_free(client);
  1040. done:
  1041. /* It is also a good idea to wipe the public key. */
  1042. if (pubkey_b32) {
  1043. memwipe(pubkey_b32, 0, strlen(pubkey_b32));
  1044. }
  1045. tor_assert(fields);
  1046. SMARTLIST_FOREACH(fields, char *, s, tor_free(s));
  1047. smartlist_free(fields);
  1048. return client;
  1049. }
  1050. /* Load all the client public keys for the given service. Return 0 on
  1051. * success else -1 on failure. */
  1052. static int
  1053. load_client_keys(hs_service_t *service)
  1054. {
  1055. int ret = -1;
  1056. char *client_key_str = NULL;
  1057. char *client_key_file_path = NULL;
  1058. char *client_keys_dir_path = NULL;
  1059. hs_service_config_t *config;
  1060. smartlist_t *file_list = NULL;
  1061. tor_assert(service);
  1062. config = &service->config;
  1063. /* Before calling this function, we already call load_service_keys to make
  1064. * sure that the directory exists with the right permission. So, if we
  1065. * cannot create a client pubkey key directory, we consider it as a bug. */
  1066. client_keys_dir_path = hs_path_from_filename(config->directory_path,
  1067. dname_client_pubkeys);
  1068. if (BUG(hs_check_service_private_dir(get_options()->User,
  1069. client_keys_dir_path,
  1070. config->dir_group_readable, 1) < 0)) {
  1071. goto end;
  1072. }
  1073. /* If the list of clients already exists, we must clear it first. */
  1074. if (config->clients) {
  1075. SMARTLIST_FOREACH(config->clients, hs_service_authorized_client_t *, p,
  1076. service_authorized_client_free(p));
  1077. smartlist_free(config->clients);
  1078. }
  1079. config->clients = smartlist_new();
  1080. file_list = tor_listdir(client_keys_dir_path);
  1081. if (file_list == NULL) {
  1082. log_warn(LD_REND, "Client authorization directory %s can't be listed.",
  1083. client_keys_dir_path);
  1084. goto end;
  1085. }
  1086. SMARTLIST_FOREACH_BEGIN(file_list, const char *, filename) {
  1087. hs_service_authorized_client_t *client = NULL;
  1088. log_info(LD_REND, "Loading a client authorization key file %s...",
  1089. filename);
  1090. if (!client_filename_is_valid(filename)) {
  1091. log_warn(LD_REND, "Client authorization unrecognized filename %s. "
  1092. "File must end in .auth. Ignoring.", filename);
  1093. continue;
  1094. }
  1095. /* Create a full path for a file. */
  1096. client_key_file_path = hs_path_from_filename(client_keys_dir_path,
  1097. filename);
  1098. client_key_str = read_file_to_str(client_key_file_path, 0, NULL);
  1099. /* Free immediately after using it. */
  1100. tor_free(client_key_file_path);
  1101. /* If we cannot read the file, continue with the next file. */
  1102. if (!client_key_str) {
  1103. log_warn(LD_REND, "Client authorization file %s can't be read. "
  1104. "Corrupted or verify permission? Ignoring.",
  1105. client_key_file_path);
  1106. continue;
  1107. }
  1108. client = parse_authorized_client(client_key_str);
  1109. /* Wipe and free immediately after using it. */
  1110. memwipe(client_key_str, 0, strlen(client_key_str));
  1111. tor_free(client_key_str);
  1112. if (client) {
  1113. smartlist_add(config->clients, client);
  1114. log_info(LD_REND, "Loaded a client authorization key file %s.",
  1115. filename);
  1116. }
  1117. } SMARTLIST_FOREACH_END(filename);
  1118. /* If the number of clients is greater than zero, set the flag to be true. */
  1119. if (smartlist_len(config->clients) > 0) {
  1120. config->is_client_auth_enabled = 1;
  1121. }
  1122. /* Success. */
  1123. ret = 0;
  1124. end:
  1125. if (client_key_str) {
  1126. memwipe(client_key_str, 0, strlen(client_key_str));
  1127. }
  1128. if (file_list) {
  1129. SMARTLIST_FOREACH(file_list, char *, s, tor_free(s));
  1130. smartlist_free(file_list);
  1131. }
  1132. tor_free(client_key_str);
  1133. tor_free(client_key_file_path);
  1134. tor_free(client_keys_dir_path);
  1135. return ret;
  1136. }
  1137. STATIC void
  1138. service_authorized_client_free_(hs_service_authorized_client_t *client)
  1139. {
  1140. if (!client) {
  1141. return;
  1142. }
  1143. memwipe(&client->client_pk, 0, sizeof(client->client_pk));
  1144. tor_free(client);
  1145. }
  1146. /* Free a given service descriptor object and all key material is wiped. */
  1147. STATIC void
  1148. service_descriptor_free_(hs_service_descriptor_t *desc)
  1149. {
  1150. if (!desc) {
  1151. return;
  1152. }
  1153. hs_descriptor_free(desc->desc);
  1154. memwipe(&desc->signing_kp, 0, sizeof(desc->signing_kp));
  1155. memwipe(&desc->blinded_kp, 0, sizeof(desc->blinded_kp));
  1156. /* Cleanup all intro points. */
  1157. digest256map_free(desc->intro_points.map, service_intro_point_free_void);
  1158. digestmap_free(desc->intro_points.failed_id, tor_free_);
  1159. if (desc->previous_hsdirs) {
  1160. SMARTLIST_FOREACH(desc->previous_hsdirs, char *, s, tor_free(s));
  1161. smartlist_free(desc->previous_hsdirs);
  1162. }
  1163. crypto_ope_free(desc->ope_cipher);
  1164. tor_free(desc);
  1165. }
  1166. /* Return a newly allocated service descriptor object. */
  1167. STATIC hs_service_descriptor_t *
  1168. service_descriptor_new(void)
  1169. {
  1170. hs_service_descriptor_t *sdesc = tor_malloc_zero(sizeof(*sdesc));
  1171. sdesc->desc = tor_malloc_zero(sizeof(hs_descriptor_t));
  1172. /* Initialize the intro points map. */
  1173. sdesc->intro_points.map = digest256map_new();
  1174. sdesc->intro_points.failed_id = digestmap_new();
  1175. sdesc->previous_hsdirs = smartlist_new();
  1176. return sdesc;
  1177. }
  1178. /* Allocate and return a deep copy of client. */
  1179. static hs_service_authorized_client_t *
  1180. service_authorized_client_dup(const hs_service_authorized_client_t *client)
  1181. {
  1182. hs_service_authorized_client_t *client_dup = NULL;
  1183. tor_assert(client);
  1184. client_dup = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
  1185. /* Currently, the public key is the only component of
  1186. * hs_service_authorized_client_t. */
  1187. memcpy(client_dup->client_pk.public_key,
  1188. client->client_pk.public_key,
  1189. CURVE25519_PUBKEY_LEN);
  1190. return client_dup;
  1191. }
  1192. /* If two authorized clients are equal, return 0. If the first one should come
  1193. * before the second, return less than zero. If the first should come after
  1194. * the second, return greater than zero. */
  1195. static int
  1196. service_authorized_client_cmp(const hs_service_authorized_client_t *client1,
  1197. const hs_service_authorized_client_t *client2)
  1198. {
  1199. tor_assert(client1);
  1200. tor_assert(client2);
  1201. /* Currently, the public key is the only component of
  1202. * hs_service_authorized_client_t. */
  1203. return tor_memcmp(client1->client_pk.public_key,
  1204. client2->client_pk.public_key,
  1205. CURVE25519_PUBKEY_LEN);
  1206. }
  1207. /* Helper for sorting authorized clients. */
  1208. static int
  1209. compare_service_authorzized_client_(const void **_a, const void **_b)
  1210. {
  1211. const hs_service_authorized_client_t *a = *_a, *b = *_b;
  1212. return service_authorized_client_cmp(a, b);
  1213. }
  1214. /* If the list of hs_service_authorized_client_t's is different between
  1215. * src and dst, return 1. Otherwise, return 0. */
  1216. STATIC int
  1217. service_authorized_client_config_equal(const hs_service_config_t *config1,
  1218. const hs_service_config_t *config2)
  1219. {
  1220. int ret = 0;
  1221. int i;
  1222. smartlist_t *sl1 = smartlist_new();
  1223. smartlist_t *sl2 = smartlist_new();
  1224. tor_assert(config1);
  1225. tor_assert(config2);
  1226. tor_assert(config1->clients);
  1227. tor_assert(config2->clients);
  1228. /* If the number of clients is different, it is obvious that the list
  1229. * changes. */
  1230. if (smartlist_len(config1->clients) != smartlist_len(config2->clients)) {
  1231. goto done;
  1232. }
  1233. /* We do not want to mutate config1 and config2, so we will duplicate both
  1234. * entire client lists here. */
  1235. SMARTLIST_FOREACH(config1->clients,
  1236. hs_service_authorized_client_t *, client,
  1237. smartlist_add(sl1, service_authorized_client_dup(client)));
  1238. SMARTLIST_FOREACH(config2->clients,
  1239. hs_service_authorized_client_t *, client,
  1240. smartlist_add(sl2, service_authorized_client_dup(client)));
  1241. smartlist_sort(sl1, compare_service_authorzized_client_);
  1242. smartlist_sort(sl2, compare_service_authorzized_client_);
  1243. for (i = 0; i < smartlist_len(sl1); i++) {
  1244. /* If the clients at index i in both lists differ, the whole configs
  1245. * differ. */
  1246. if (service_authorized_client_cmp(smartlist_get(sl1, i),
  1247. smartlist_get(sl2, i))) {
  1248. goto done;
  1249. }
  1250. }
  1251. /* Success. */
  1252. ret = 1;
  1253. done:
  1254. if (sl1) {
  1255. SMARTLIST_FOREACH(sl1, hs_service_authorized_client_t *, p,
  1256. service_authorized_client_free(p));
  1257. smartlist_free(sl1);
  1258. }
  1259. if (sl2) {
  1260. SMARTLIST_FOREACH(sl2, hs_service_authorized_client_t *, p,
  1261. service_authorized_client_free(p));
  1262. smartlist_free(sl2);
  1263. }
  1264. return ret;
  1265. }
  1266. /* Move descriptor(s) from the src service to the dst service and modify their
  1267. * content if necessary. We do this during SIGHUP when we re-create our
  1268. * hidden services. */
  1269. static void
  1270. move_descriptors(hs_service_t *src, hs_service_t *dst)
  1271. {
  1272. tor_assert(src);
  1273. tor_assert(dst);
  1274. if (src->desc_current) {
  1275. /* Nothing should be there, but clean it up just in case */
  1276. if (BUG(dst->desc_current)) {
  1277. service_descriptor_free(dst->desc_current);
  1278. }
  1279. dst->desc_current = src->desc_current;
  1280. src->desc_current = NULL;
  1281. }
  1282. if (src->desc_next) {
  1283. /* Nothing should be there, but clean it up just in case */
  1284. if (BUG(dst->desc_next)) {
  1285. service_descriptor_free(dst->desc_next);
  1286. }
  1287. dst->desc_next = src->desc_next;
  1288. src->desc_next = NULL;
  1289. }
  1290. /* If the client authorization changes, we must rebuild the superencrypted
  1291. * section and republish the descriptors. */
  1292. int client_auth_changed =
  1293. !service_authorized_client_config_equal(&src->config, &dst->config);
  1294. if (client_auth_changed && dst->desc_current) {
  1295. /* We have to clear the superencrypted content first. */
  1296. hs_desc_superencrypted_data_free_contents(
  1297. &dst->desc_current->desc->superencrypted_data);
  1298. if (build_service_desc_superencrypted(dst, dst->desc_current) < 0) {
  1299. goto err;
  1300. }
  1301. service_desc_schedule_upload(dst->desc_current, time(NULL), 1);
  1302. }
  1303. if (client_auth_changed && dst->desc_next) {
  1304. /* We have to clear the superencrypted content first. */
  1305. hs_desc_superencrypted_data_free_contents(
  1306. &dst->desc_next->desc->superencrypted_data);
  1307. if (build_service_desc_superencrypted(dst, dst->desc_next) < 0) {
  1308. goto err;
  1309. }
  1310. service_desc_schedule_upload(dst->desc_next, time(NULL), 1);
  1311. }
  1312. return;
  1313. err:
  1314. /* If there is an error, free all descriptors to make it clean and generate
  1315. * them later. */
  1316. service_descriptor_free(dst->desc_current);
  1317. service_descriptor_free(dst->desc_next);
  1318. }
  1319. /* From the given service, remove all expired failing intro points for each
  1320. * descriptor. */
  1321. static void
  1322. remove_expired_failing_intro(hs_service_t *service, time_t now)
  1323. {
  1324. tor_assert(service);
  1325. /* For both descriptors, cleanup the failing intro points list. */
  1326. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  1327. DIGESTMAP_FOREACH_MODIFY(desc->intro_points.failed_id, key, time_t *, t) {
  1328. time_t failure_time = *t;
  1329. if ((failure_time + INTRO_CIRC_RETRY_PERIOD) <= now) {
  1330. MAP_DEL_CURRENT(key);
  1331. tor_free(t);
  1332. }
  1333. } DIGESTMAP_FOREACH_END;
  1334. } FOR_EACH_DESCRIPTOR_END;
  1335. }
  1336. /* For the given descriptor desc, put all node_t object found from its failing
  1337. * intro point list and put them in the given node_list. */
  1338. static void
  1339. setup_intro_point_exclude_list(const hs_service_descriptor_t *desc,
  1340. smartlist_t *node_list)
  1341. {
  1342. tor_assert(desc);
  1343. tor_assert(node_list);
  1344. DIGESTMAP_FOREACH(desc->intro_points.failed_id, key, time_t *, t) {
  1345. (void) t; /* Make gcc happy. */
  1346. const node_t *node = node_get_by_id(key);
  1347. if (node) {
  1348. smartlist_add(node_list, (void *) node);
  1349. }
  1350. } DIGESTMAP_FOREACH_END;
  1351. }
  1352. /* For the given failing intro point ip, we add its time of failure to the
  1353. * failed map and index it by identity digest (legacy ID) in the descriptor
  1354. * desc failed id map. */
  1355. static void
  1356. remember_failing_intro_point(const hs_service_intro_point_t *ip,
  1357. hs_service_descriptor_t *desc, time_t now)
  1358. {
  1359. time_t *time_of_failure, *prev_ptr;
  1360. const hs_desc_link_specifier_t *legacy_ls;
  1361. tor_assert(ip);
  1362. tor_assert(desc);
  1363. time_of_failure = tor_malloc_zero(sizeof(time_t));
  1364. *time_of_failure = now;
  1365. legacy_ls = get_link_spec_by_type(ip, LS_LEGACY_ID);
  1366. tor_assert(legacy_ls);
  1367. prev_ptr = digestmap_set(desc->intro_points.failed_id,
  1368. (const char *) legacy_ls->u.legacy_id,
  1369. time_of_failure);
  1370. tor_free(prev_ptr);
  1371. }
  1372. /* Copy the descriptor link specifier object from src to dst. */
  1373. static void
  1374. link_specifier_copy(hs_desc_link_specifier_t *dst,
  1375. const hs_desc_link_specifier_t *src)
  1376. {
  1377. tor_assert(dst);
  1378. tor_assert(src);
  1379. memcpy(dst, src, sizeof(hs_desc_link_specifier_t));
  1380. }
  1381. /* Using a given descriptor signing keypair signing_kp, a service intro point
  1382. * object ip and the time now, setup the content of an already allocated
  1383. * descriptor intro desc_ip.
  1384. *
  1385. * Return 0 on success else a negative value. */
  1386. static int
  1387. setup_desc_intro_point(const ed25519_keypair_t *signing_kp,
  1388. const hs_service_intro_point_t *ip,
  1389. time_t now, hs_desc_intro_point_t *desc_ip)
  1390. {
  1391. int ret = -1;
  1392. time_t nearest_hour = now - (now % 3600);
  1393. tor_assert(signing_kp);
  1394. tor_assert(ip);
  1395. tor_assert(desc_ip);
  1396. /* Copy the onion key. */
  1397. memcpy(&desc_ip->onion_key, &ip->onion_key, sizeof(desc_ip->onion_key));
  1398. /* Key and certificate material. */
  1399. desc_ip->auth_key_cert = tor_cert_create(signing_kp,
  1400. CERT_TYPE_AUTH_HS_IP_KEY,
  1401. &ip->auth_key_kp.pubkey,
  1402. nearest_hour,
  1403. HS_DESC_CERT_LIFETIME,
  1404. CERT_FLAG_INCLUDE_SIGNING_KEY);
  1405. if (desc_ip->auth_key_cert == NULL) {
  1406. log_warn(LD_REND, "Unable to create intro point auth-key certificate");
  1407. goto done;
  1408. }
  1409. /* Copy link specifier(s). */
  1410. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  1411. const hs_desc_link_specifier_t *, ls) {
  1412. hs_desc_link_specifier_t *copy = tor_malloc_zero(sizeof(*copy));
  1413. link_specifier_copy(copy, ls);
  1414. smartlist_add(desc_ip->link_specifiers, copy);
  1415. } SMARTLIST_FOREACH_END(ls);
  1416. /* For a legacy intro point, we'll use an RSA/ed cross certificate. */
  1417. if (ip->base.is_only_legacy) {
  1418. desc_ip->legacy.key = crypto_pk_dup_key(ip->legacy_key);
  1419. /* Create cross certification cert. */
  1420. ssize_t cert_len = tor_make_rsa_ed25519_crosscert(
  1421. &signing_kp->pubkey,
  1422. desc_ip->legacy.key,
  1423. nearest_hour + HS_DESC_CERT_LIFETIME,
  1424. &desc_ip->legacy.cert.encoded);
  1425. if (cert_len < 0) {
  1426. log_warn(LD_REND, "Unable to create enc key legacy cross cert.");
  1427. goto done;
  1428. }
  1429. desc_ip->legacy.cert.len = cert_len;
  1430. }
  1431. /* Encryption key and its cross certificate. */
  1432. {
  1433. ed25519_public_key_t ed25519_pubkey;
  1434. /* Use the public curve25519 key. */
  1435. memcpy(&desc_ip->enc_key, &ip->enc_key_kp.pubkey,
  1436. sizeof(desc_ip->enc_key));
  1437. /* The following can't fail. */
  1438. ed25519_public_key_from_curve25519_public_key(&ed25519_pubkey,
  1439. &ip->enc_key_kp.pubkey,
  1440. 0);
  1441. desc_ip->enc_key_cert = tor_cert_create(signing_kp,
  1442. CERT_TYPE_CROSS_HS_IP_KEYS,
  1443. &ed25519_pubkey, nearest_hour,
  1444. HS_DESC_CERT_LIFETIME,
  1445. CERT_FLAG_INCLUDE_SIGNING_KEY);
  1446. if (desc_ip->enc_key_cert == NULL) {
  1447. log_warn(LD_REND, "Unable to create enc key curve25519 cross cert.");
  1448. goto done;
  1449. }
  1450. }
  1451. /* Success. */
  1452. ret = 0;
  1453. done:
  1454. return ret;
  1455. }
  1456. /* Using the given descriptor from the given service, build the descriptor
  1457. * intro point list so we can then encode the descriptor for publication. This
  1458. * function does not pick intro points, they have to be in the descriptor
  1459. * current map. Cryptographic material (keys) must be initialized in the
  1460. * descriptor for this function to make sense. */
  1461. static void
  1462. build_desc_intro_points(const hs_service_t *service,
  1463. hs_service_descriptor_t *desc, time_t now)
  1464. {
  1465. hs_desc_encrypted_data_t *encrypted;
  1466. tor_assert(service);
  1467. tor_assert(desc);
  1468. /* Ease our life. */
  1469. encrypted = &desc->desc->encrypted_data;
  1470. /* Cleanup intro points, we are about to set them from scratch. */
  1471. hs_descriptor_clear_intro_points(desc->desc);
  1472. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  1473. const hs_service_intro_point_t *, ip) {
  1474. hs_desc_intro_point_t *desc_ip = hs_desc_intro_point_new();
  1475. if (setup_desc_intro_point(&desc->signing_kp, ip, now, desc_ip) < 0) {
  1476. hs_desc_intro_point_free(desc_ip);
  1477. continue;
  1478. }
  1479. /* We have a valid descriptor intro point. Add it to the list. */
  1480. smartlist_add(encrypted->intro_points, desc_ip);
  1481. } DIGEST256MAP_FOREACH_END;
  1482. }
  1483. /* Populate the descriptor encrypted section from the given service object.
  1484. * This will generate a valid list of introduction points that can be used
  1485. * after for circuit creation. Return 0 on success else -1 on error. */
  1486. static int
  1487. build_service_desc_encrypted(const hs_service_t *service,
  1488. hs_service_descriptor_t *desc)
  1489. {
  1490. hs_desc_encrypted_data_t *encrypted;
  1491. tor_assert(service);
  1492. tor_assert(desc);
  1493. encrypted = &desc->desc->encrypted_data;
  1494. encrypted->create2_ntor = 1;
  1495. encrypted->single_onion_service = service->config.is_single_onion;
  1496. /* Setup introduction points from what we have in the service. */
  1497. if (encrypted->intro_points == NULL) {
  1498. encrypted->intro_points = smartlist_new();
  1499. }
  1500. /* We do NOT build introduction point yet, we only do that once the circuit
  1501. * have been opened. Until we have the right number of introduction points,
  1502. * we do not encode anything in the descriptor. */
  1503. /* XXX: Support client authorization (#20700). */
  1504. encrypted->intro_auth_types = NULL;
  1505. return 0;
  1506. }
  1507. /* Populate the descriptor superencrypted section from the given service
  1508. * object. This will generate a valid list of hs_desc_authorized_client_t
  1509. * of clients that are authorized to use the service. Return 0 on success
  1510. * else -1 on error. */
  1511. static int
  1512. build_service_desc_superencrypted(const hs_service_t *service,
  1513. hs_service_descriptor_t *desc)
  1514. {
  1515. const hs_service_config_t *config;
  1516. int i;
  1517. hs_desc_superencrypted_data_t *superencrypted;
  1518. tor_assert(service);
  1519. tor_assert(desc);
  1520. superencrypted = &desc->desc->superencrypted_data;
  1521. config = &service->config;
  1522. /* The ephemeral key pair is already generated, so this should not give
  1523. * an error. */
  1524. if (BUG(!curve25519_public_key_is_ok(&desc->auth_ephemeral_kp.pubkey))) {
  1525. return -1;
  1526. }
  1527. memcpy(&superencrypted->auth_ephemeral_pubkey,
  1528. &desc->auth_ephemeral_kp.pubkey,
  1529. sizeof(curve25519_public_key_t));
  1530. /* Test that subcred is not zero because we might use it below */
  1531. if (BUG(tor_mem_is_zero((char*)desc->desc->subcredential, DIGEST256_LEN))) {
  1532. return -1;
  1533. }
  1534. /* Create a smartlist to store clients */
  1535. superencrypted->clients = smartlist_new();
  1536. /* We do not need to build the desc authorized client if the client
  1537. * authorization is disabled */
  1538. if (config->is_client_auth_enabled) {
  1539. SMARTLIST_FOREACH_BEGIN(config->clients,
  1540. hs_service_authorized_client_t *, client) {
  1541. hs_desc_authorized_client_t *desc_client;
  1542. desc_client = tor_malloc_zero(sizeof(hs_desc_authorized_client_t));
  1543. /* Prepare the client for descriptor and then add to the list in the
  1544. * superencrypted part of the descriptor */
  1545. hs_desc_build_authorized_client(desc->desc->subcredential,
  1546. &client->client_pk,
  1547. &desc->auth_ephemeral_kp.seckey,
  1548. desc->descriptor_cookie, desc_client);
  1549. smartlist_add(superencrypted->clients, desc_client);
  1550. } SMARTLIST_FOREACH_END(client);
  1551. }
  1552. /* We cannot let the number of auth-clients to be zero, so we need to
  1553. * make it be 16. If it is already a multiple of 16, we do not need to
  1554. * do anything. Otherwise, add the additional ones to make it a
  1555. * multiple of 16. */
  1556. int num_clients = smartlist_len(superencrypted->clients);
  1557. int num_clients_to_add;
  1558. if (num_clients == 0) {
  1559. num_clients_to_add = HS_DESC_AUTH_CLIENT_MULTIPLE;
  1560. } else if (num_clients % HS_DESC_AUTH_CLIENT_MULTIPLE == 0) {
  1561. num_clients_to_add = 0;
  1562. } else {
  1563. num_clients_to_add =
  1564. HS_DESC_AUTH_CLIENT_MULTIPLE
  1565. - (num_clients % HS_DESC_AUTH_CLIENT_MULTIPLE);
  1566. }
  1567. for (i = 0; i < num_clients_to_add; i++) {
  1568. hs_desc_authorized_client_t *desc_client =
  1569. hs_desc_build_fake_authorized_client();
  1570. smartlist_add(superencrypted->clients, desc_client);
  1571. }
  1572. /* Shuffle the list to prevent the client know the position in the
  1573. * config. */
  1574. smartlist_shuffle(superencrypted->clients);
  1575. return 0;
  1576. }
  1577. /* Populate the descriptor plaintext section from the given service object.
  1578. * The caller must make sure that the keys in the descriptors are valid that
  1579. * is are non-zero. Return 0 on success else -1 on error. */
  1580. static int
  1581. build_service_desc_plaintext(const hs_service_t *service,
  1582. hs_service_descriptor_t *desc, time_t now)
  1583. {
  1584. int ret = -1;
  1585. hs_desc_plaintext_data_t *plaintext;
  1586. tor_assert(service);
  1587. tor_assert(desc);
  1588. /* XXX: Use a "assert_desc_ok()" ? */
  1589. tor_assert(!tor_mem_is_zero((char *) &desc->blinded_kp,
  1590. sizeof(desc->blinded_kp)));
  1591. tor_assert(!tor_mem_is_zero((char *) &desc->signing_kp,
  1592. sizeof(desc->signing_kp)));
  1593. /* Set the subcredential. */
  1594. hs_get_subcredential(&service->keys.identity_pk, &desc->blinded_kp.pubkey,
  1595. desc->desc->subcredential);
  1596. plaintext = &desc->desc->plaintext_data;
  1597. plaintext->version = service->config.version;
  1598. plaintext->lifetime_sec = HS_DESC_DEFAULT_LIFETIME;
  1599. plaintext->signing_key_cert =
  1600. tor_cert_create(&desc->blinded_kp, CERT_TYPE_SIGNING_HS_DESC,
  1601. &desc->signing_kp.pubkey, now, HS_DESC_CERT_LIFETIME,
  1602. CERT_FLAG_INCLUDE_SIGNING_KEY);
  1603. if (plaintext->signing_key_cert == NULL) {
  1604. log_warn(LD_REND, "Unable to create descriptor signing certificate for "
  1605. "service %s",
  1606. safe_str_client(service->onion_address));
  1607. goto end;
  1608. }
  1609. /* Copy public key material to go in the descriptor. */
  1610. ed25519_pubkey_copy(&plaintext->signing_pubkey, &desc->signing_kp.pubkey);
  1611. ed25519_pubkey_copy(&plaintext->blinded_pubkey, &desc->blinded_kp.pubkey);
  1612. /* Success. */
  1613. ret = 0;
  1614. end:
  1615. return ret;
  1616. }
  1617. /** Compute the descriptor's OPE cipher for encrypting revision counters. */
  1618. static crypto_ope_t *
  1619. generate_ope_cipher_for_desc(const hs_service_descriptor_t *hs_desc)
  1620. {
  1621. /* Compute OPE key as H("rev-counter-generation" | blinded privkey) */
  1622. uint8_t key[DIGEST256_LEN];
  1623. crypto_digest_t *digest = crypto_digest256_new(DIGEST_SHA3_256);
  1624. const char ope_key_prefix[] = "rev-counter-generation";
  1625. const ed25519_secret_key_t *eph_privkey = &hs_desc->blinded_kp.seckey;
  1626. crypto_digest_add_bytes(digest, ope_key_prefix, sizeof(ope_key_prefix));
  1627. crypto_digest_add_bytes(digest, (char*)eph_privkey->seckey,
  1628. sizeof(eph_privkey->seckey));
  1629. crypto_digest_get_digest(digest, (char *)key, sizeof(key));
  1630. crypto_digest_free(digest);
  1631. return crypto_ope_new(key);
  1632. }
  1633. /* For the given service and descriptor object, create the key material which
  1634. * is the blinded keypair, the descriptor signing keypair, the ephemeral
  1635. * keypair, and the descriptor cookie. Return 0 on success else -1 on error
  1636. * where the generated keys MUST be ignored. */
  1637. static int
  1638. build_service_desc_keys(const hs_service_t *service,
  1639. hs_service_descriptor_t *desc)
  1640. {
  1641. int ret = -1;
  1642. ed25519_keypair_t kp;
  1643. tor_assert(desc);
  1644. tor_assert(!tor_mem_is_zero((char *) &service->keys.identity_pk,
  1645. ED25519_PUBKEY_LEN));
  1646. /* XXX: Support offline key feature (#18098). */
  1647. /* Copy the identity keys to the keypair so we can use it to create the
  1648. * blinded key. */
  1649. memcpy(&kp.pubkey, &service->keys.identity_pk, sizeof(kp.pubkey));
  1650. memcpy(&kp.seckey, &service->keys.identity_sk, sizeof(kp.seckey));
  1651. /* Build blinded keypair for this time period. */
  1652. hs_build_blinded_keypair(&kp, NULL, 0, desc->time_period_num,
  1653. &desc->blinded_kp);
  1654. /* Let's not keep too much traces of our keys in memory. */
  1655. memwipe(&kp, 0, sizeof(kp));
  1656. /* Compute the OPE cipher struct (it's tied to the current blinded key) */
  1657. log_info(LD_GENERAL,
  1658. "Getting OPE for TP#%u", (unsigned) desc->time_period_num);
  1659. tor_assert_nonfatal(!desc->ope_cipher);
  1660. desc->ope_cipher = generate_ope_cipher_for_desc(desc);
  1661. /* No need for extra strong, this is a temporary key only for this
  1662. * descriptor. Nothing long term. */
  1663. if (ed25519_keypair_generate(&desc->signing_kp, 0) < 0) {
  1664. log_warn(LD_REND, "Can't generate descriptor signing keypair for "
  1665. "service %s",
  1666. safe_str_client(service->onion_address));
  1667. goto end;
  1668. }
  1669. /* No need for extra strong, this is a temporary key only for this
  1670. * descriptor. Nothing long term. */
  1671. if (curve25519_keypair_generate(&desc->auth_ephemeral_kp, 0) < 0) {
  1672. log_warn(LD_REND, "Can't generate auth ephemeral keypair for "
  1673. "service %s",
  1674. safe_str_client(service->onion_address));
  1675. goto end;
  1676. }
  1677. /* Random a descriptor cookie to be used as a part of a key to encrypt the
  1678. * descriptor, if the client auth is enabled. */
  1679. if (service->config.is_client_auth_enabled) {
  1680. crypto_strongest_rand(desc->descriptor_cookie,
  1681. sizeof(desc->descriptor_cookie));
  1682. }
  1683. /* Success. */
  1684. ret = 0;
  1685. end:
  1686. return ret;
  1687. }
  1688. /* Given a service and the current time, build a descriptor for the service.
  1689. * This function does not pick introduction point, this needs to be done by
  1690. * the update function. On success, desc_out will point to the newly allocated
  1691. * descriptor object.
  1692. *
  1693. * This can error if we are unable to create keys or certificate. */
  1694. static void
  1695. build_service_descriptor(hs_service_t *service, time_t now,
  1696. uint64_t time_period_num,
  1697. hs_service_descriptor_t **desc_out)
  1698. {
  1699. char *encoded_desc;
  1700. hs_service_descriptor_t *desc;
  1701. tor_assert(service);
  1702. tor_assert(desc_out);
  1703. desc = service_descriptor_new();
  1704. /* Set current time period */
  1705. desc->time_period_num = time_period_num;
  1706. /* Create the needed keys so we can setup the descriptor content. */
  1707. if (build_service_desc_keys(service, desc) < 0) {
  1708. goto err;
  1709. }
  1710. /* Setup plaintext descriptor content. */
  1711. if (build_service_desc_plaintext(service, desc, now) < 0) {
  1712. goto err;
  1713. }
  1714. /* Setup superencrypted descriptor content. */
  1715. if (build_service_desc_superencrypted(service, desc) < 0) {
  1716. goto err;
  1717. }
  1718. /* Setup encrypted descriptor content. */
  1719. if (build_service_desc_encrypted(service, desc) < 0) {
  1720. goto err;
  1721. }
  1722. /* Let's make sure that we've created a descriptor that can actually be
  1723. * encoded properly. This function also checks if the encoded output is
  1724. * decodable after. */
  1725. if (BUG(service_encode_descriptor(service, desc, &desc->signing_kp,
  1726. &encoded_desc) < 0)) {
  1727. goto err;
  1728. }
  1729. tor_free(encoded_desc);
  1730. /* Assign newly built descriptor to the next slot. */
  1731. *desc_out = desc;
  1732. /* Fire a CREATED control port event. */
  1733. hs_control_desc_event_created(service->onion_address,
  1734. &desc->blinded_kp.pubkey);
  1735. return;
  1736. err:
  1737. service_descriptor_free(desc);
  1738. }
  1739. /* Build both descriptors for the given service that has just booted up.
  1740. * Because it's a special case, it deserves its special function ;). */
  1741. static void
  1742. build_descriptors_for_new_service(hs_service_t *service, time_t now)
  1743. {
  1744. uint64_t current_desc_tp, next_desc_tp;
  1745. tor_assert(service);
  1746. /* These are the conditions for a new service. */
  1747. tor_assert(!service->desc_current);
  1748. tor_assert(!service->desc_next);
  1749. /*
  1750. * +------------------------------------------------------------------+
  1751. * | |
  1752. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1753. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1754. * | |
  1755. * | $==========|-----------$===========|-----------$===========| |
  1756. * | ^ ^ |
  1757. * | A B |
  1758. * +------------------------------------------------------------------+
  1759. *
  1760. * Case A: The service boots up before a new time period, the current time
  1761. * period is thus TP#1 and the next is TP#2 which for both we have access to
  1762. * their SRVs.
  1763. *
  1764. * Case B: The service boots up inside TP#2, we can't use the TP#3 for the
  1765. * next descriptor because we don't have the SRV#3 so the current should be
  1766. * TP#1 and next TP#2.
  1767. */
  1768. if (hs_in_period_between_tp_and_srv(NULL, now)) {
  1769. /* Case B from the above, inside of the new time period. */
  1770. current_desc_tp = hs_get_previous_time_period_num(0); /* TP#1 */
  1771. next_desc_tp = hs_get_time_period_num(0); /* TP#2 */
  1772. } else {
  1773. /* Case A from the above, outside of the new time period. */
  1774. current_desc_tp = hs_get_time_period_num(0); /* TP#1 */
  1775. next_desc_tp = hs_get_next_time_period_num(0); /* TP#2 */
  1776. }
  1777. /* Build descriptors. */
  1778. build_service_descriptor(service, now, current_desc_tp,
  1779. &service->desc_current);
  1780. build_service_descriptor(service, now, next_desc_tp,
  1781. &service->desc_next);
  1782. log_info(LD_REND, "Hidden service %s has just started. Both descriptors "
  1783. "built. Now scheduled for upload.",
  1784. safe_str_client(service->onion_address));
  1785. }
  1786. /* Build descriptors for each service if needed. There are conditions to build
  1787. * a descriptor which are details in the function. */
  1788. STATIC void
  1789. build_all_descriptors(time_t now)
  1790. {
  1791. FOR_EACH_SERVICE_BEGIN(service) {
  1792. /* A service booting up will have both descriptors to NULL. No other cases
  1793. * makes both descriptor non existent. */
  1794. if (service->desc_current == NULL && service->desc_next == NULL) {
  1795. build_descriptors_for_new_service(service, now);
  1796. continue;
  1797. }
  1798. /* Reaching this point means we are pass bootup so at runtime. We should
  1799. * *never* have an empty current descriptor. If the next descriptor is
  1800. * empty, we'll try to build it for the next time period. This only
  1801. * happens when we rotate meaning that we are guaranteed to have a new SRV
  1802. * at that point for the next time period. */
  1803. if (BUG(service->desc_current == NULL)) {
  1804. continue;
  1805. }
  1806. if (service->desc_next == NULL) {
  1807. build_service_descriptor(service, now, hs_get_next_time_period_num(0),
  1808. &service->desc_next);
  1809. log_info(LD_REND, "Hidden service %s next descriptor successfully "
  1810. "built. Now scheduled for upload.",
  1811. safe_str_client(service->onion_address));
  1812. }
  1813. } FOR_EACH_DESCRIPTOR_END;
  1814. }
  1815. /* Randomly pick a node to become an introduction point but not present in the
  1816. * given exclude_nodes list. The chosen node is put in the exclude list
  1817. * regardless of success or not because in case of failure, the node is simply
  1818. * unsusable from that point on.
  1819. *
  1820. * If direct_conn is set, try to pick a node that our local firewall/policy
  1821. * allows us to connect to directly. If we can't find any, return NULL.
  1822. * This function supports selecting dual-stack nodes for direct single onion
  1823. * service IPv6 connections. But it does not send IPv6 addresses in link
  1824. * specifiers. (Current clients don't use IPv6 addresses to extend, and
  1825. * direct client connections to intro points are not supported.)
  1826. *
  1827. * Return a newly allocated service intro point ready to be used for encoding.
  1828. * Return NULL on error. */
  1829. static hs_service_intro_point_t *
  1830. pick_intro_point(unsigned int direct_conn, smartlist_t *exclude_nodes)
  1831. {
  1832. const node_t *node;
  1833. extend_info_t *info = NULL;
  1834. hs_service_intro_point_t *ip = NULL;
  1835. /* Normal 3-hop introduction point flags. */
  1836. router_crn_flags_t flags = CRN_NEED_UPTIME | CRN_NEED_DESC;
  1837. /* Single onion flags. */
  1838. router_crn_flags_t direct_flags = flags | CRN_PREF_ADDR | CRN_DIRECT_CONN;
  1839. node = router_choose_random_node(exclude_nodes, get_options()->ExcludeNodes,
  1840. direct_conn ? direct_flags : flags);
  1841. /* Unable to find a node. When looking for a node for a direct connection,
  1842. * we could try a 3-hop path instead. We'll add support for this in a later
  1843. * release. */
  1844. if (!node) {
  1845. goto err;
  1846. }
  1847. /* We have a suitable node, add it to the exclude list. We do this *before*
  1848. * we can validate the extend information because even in case of failure,
  1849. * we don't want to use that node anymore. */
  1850. smartlist_add(exclude_nodes, (void *) node);
  1851. /* We do this to ease our life but also this call makes appropriate checks
  1852. * of the node object such as validating ntor support for instance.
  1853. *
  1854. * We must provide an extend_info for clients to connect over a 3-hop path,
  1855. * so we don't pass direct_conn here. */
  1856. info = extend_info_from_node(node, 0);
  1857. if (BUG(info == NULL)) {
  1858. goto err;
  1859. }
  1860. /* Let's do a basic sanity check here so that we don't end up advertising the
  1861. * ed25519 identity key of relays that don't actually support the link
  1862. * protocol */
  1863. if (!node_supports_ed25519_link_authentication(node, 0)) {
  1864. tor_assert_nonfatal(ed25519_public_key_is_zero(&info->ed_identity));
  1865. } else {
  1866. /* Make sure we *do* have an ed key if we support the link authentication.
  1867. * Sending an empty key would result in a failure to extend. */
  1868. tor_assert_nonfatal(!ed25519_public_key_is_zero(&info->ed_identity));
  1869. }
  1870. /* Create our objects and populate them with the node information.
  1871. * We don't care if the intro's link auth is compatible with us, because
  1872. * we are sending the ed25519 key to a remote client via the descriptor. */
  1873. ip = service_intro_point_new(info, !node_supports_ed25519_hs_intro(node),
  1874. node_supports_ed25519_link_authentication(node,
  1875. 0));
  1876. if (ip == NULL) {
  1877. goto err;
  1878. }
  1879. log_info(LD_REND, "Picked intro point: %s", extend_info_describe(info));
  1880. extend_info_free(info);
  1881. return ip;
  1882. err:
  1883. service_intro_point_free(ip);
  1884. extend_info_free(info);
  1885. return NULL;
  1886. }
  1887. /* For a given descriptor from the given service, pick any needed intro points
  1888. * and update the current map with those newly picked intro points. Return the
  1889. * number node that might have been added to the descriptor current map. */
  1890. static unsigned int
  1891. pick_needed_intro_points(hs_service_t *service,
  1892. hs_service_descriptor_t *desc)
  1893. {
  1894. int i = 0, num_needed_ip;
  1895. smartlist_t *exclude_nodes = smartlist_new();
  1896. tor_assert(service);
  1897. tor_assert(desc);
  1898. /* Compute how many intro points we actually need to open. */
  1899. num_needed_ip = service->config.num_intro_points -
  1900. digest256map_size(desc->intro_points.map);
  1901. if (BUG(num_needed_ip < 0)) {
  1902. /* Let's not make tor freak out here and just skip this. */
  1903. goto done;
  1904. }
  1905. /* We want to end up with config.num_intro_points intro points, but if we
  1906. * have no intro points at all (chances are they all cycled or we are
  1907. * starting up), we launch get_intro_point_num_extra() extra circuits and
  1908. * use the first config.num_intro_points that complete. See proposal #155,
  1909. * section 4 for the rationale of this which is purely for performance.
  1910. *
  1911. * The ones after the first config.num_intro_points will be converted to
  1912. * 'General' internal circuits and then we'll drop them from the list of
  1913. * intro points. */
  1914. if (digest256map_size(desc->intro_points.map) == 0) {
  1915. num_needed_ip += get_intro_point_num_extra();
  1916. }
  1917. /* Build an exclude list of nodes of our intro point(s). The expiring intro
  1918. * points are OK to pick again because this is afterall a concept of round
  1919. * robin so they are considered valid nodes to pick again. */
  1920. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  1921. hs_service_intro_point_t *, ip) {
  1922. const node_t *intro_node = get_node_from_intro_point(ip);
  1923. if (intro_node) {
  1924. smartlist_add(exclude_nodes, (void*)intro_node);
  1925. }
  1926. } DIGEST256MAP_FOREACH_END;
  1927. /* Also, add the failing intro points that our descriptor encounteered in
  1928. * the exclude node list. */
  1929. setup_intro_point_exclude_list(desc, exclude_nodes);
  1930. for (i = 0; i < num_needed_ip; i++) {
  1931. hs_service_intro_point_t *ip;
  1932. /* This function will add the picked intro point node to the exclude nodes
  1933. * list so we don't pick the same one at the next iteration. */
  1934. ip = pick_intro_point(service->config.is_single_onion, exclude_nodes);
  1935. if (ip == NULL) {
  1936. /* If we end up unable to pick an introduction point it is because we
  1937. * can't find suitable node and calling this again is highly unlikely to
  1938. * give us a valid node all of the sudden. */
  1939. log_info(LD_REND, "Unable to find a suitable node to be an "
  1940. "introduction point for service %s.",
  1941. safe_str_client(service->onion_address));
  1942. goto done;
  1943. }
  1944. /* Valid intro point object, add it to the descriptor current map. */
  1945. service_intro_point_add(desc->intro_points.map, ip);
  1946. }
  1947. /* We've successfully picked all our needed intro points thus none are
  1948. * missing which will tell our upload process to expect the number of
  1949. * circuits to be the number of configured intro points circuits and not the
  1950. * number of intro points object that we have. */
  1951. desc->missing_intro_points = 0;
  1952. /* Success. */
  1953. done:
  1954. /* We don't have ownership of the node_t object in this list. */
  1955. smartlist_free(exclude_nodes);
  1956. return i;
  1957. }
  1958. /** Clear previous cached HSDirs in <b>desc</b>. */
  1959. static void
  1960. service_desc_clear_previous_hsdirs(hs_service_descriptor_t *desc)
  1961. {
  1962. if (BUG(!desc->previous_hsdirs)) {
  1963. return;
  1964. }
  1965. SMARTLIST_FOREACH(desc->previous_hsdirs, char*, s, tor_free(s));
  1966. smartlist_clear(desc->previous_hsdirs);
  1967. }
  1968. /** Note that we attempted to upload <b>desc</b> to <b>hsdir</b>. */
  1969. static void
  1970. service_desc_note_upload(hs_service_descriptor_t *desc, const node_t *hsdir)
  1971. {
  1972. char b64_digest[BASE64_DIGEST_LEN+1] = {0};
  1973. digest_to_base64(b64_digest, hsdir->identity);
  1974. if (BUG(!desc->previous_hsdirs)) {
  1975. return;
  1976. }
  1977. if (!smartlist_contains_string(desc->previous_hsdirs, b64_digest)) {
  1978. smartlist_add_strdup(desc->previous_hsdirs, b64_digest);
  1979. }
  1980. }
  1981. /** Schedule an upload of <b>desc</b>. If <b>descriptor_changed</b> is set, it
  1982. * means that this descriptor is dirty. */
  1983. STATIC void
  1984. service_desc_schedule_upload(hs_service_descriptor_t *desc,
  1985. time_t now,
  1986. int descriptor_changed)
  1987. {
  1988. desc->next_upload_time = now;
  1989. /* If the descriptor changed, clean up the old HSDirs list. We want to
  1990. * re-upload no matter what. */
  1991. if (descriptor_changed) {
  1992. service_desc_clear_previous_hsdirs(desc);
  1993. }
  1994. }
  1995. /* Update the given descriptor from the given service. The possible update
  1996. * actions includes:
  1997. * - Picking missing intro points if needed.
  1998. */
  1999. static void
  2000. update_service_descriptor(hs_service_t *service,
  2001. hs_service_descriptor_t *desc, time_t now)
  2002. {
  2003. unsigned int num_intro_points;
  2004. tor_assert(service);
  2005. tor_assert(desc);
  2006. tor_assert(desc->desc);
  2007. num_intro_points = digest256map_size(desc->intro_points.map);
  2008. /* Pick any missing introduction point(s). */
  2009. if (num_intro_points < service->config.num_intro_points) {
  2010. unsigned int num_new_intro_points = pick_needed_intro_points(service,
  2011. desc);
  2012. if (num_new_intro_points != 0) {
  2013. log_info(LD_REND, "Service %s just picked %u intro points and wanted "
  2014. "%u for %s descriptor. It currently has %d intro "
  2015. "points. Launching ESTABLISH_INTRO circuit shortly.",
  2016. safe_str_client(service->onion_address),
  2017. num_new_intro_points,
  2018. service->config.num_intro_points - num_intro_points,
  2019. (desc == service->desc_current) ? "current" : "next",
  2020. num_intro_points);
  2021. /* We'll build those introduction point into the descriptor once we have
  2022. * confirmation that the circuits are opened and ready. However,
  2023. * indicate that this descriptor should be uploaded from now on. */
  2024. service_desc_schedule_upload(desc, now, 1);
  2025. }
  2026. /* Were we able to pick all the intro points we needed? If not, we'll
  2027. * flag the descriptor that it's missing intro points because it
  2028. * couldn't pick enough which will trigger a descriptor upload. */
  2029. if ((num_new_intro_points + num_intro_points) <
  2030. service->config.num_intro_points) {
  2031. desc->missing_intro_points = 1;
  2032. }
  2033. }
  2034. }
  2035. /* Update descriptors for each service if needed. */
  2036. STATIC void
  2037. update_all_descriptors(time_t now)
  2038. {
  2039. FOR_EACH_SERVICE_BEGIN(service) {
  2040. /* We'll try to update each descriptor that is if certain conditions apply
  2041. * in order for the descriptor to be updated. */
  2042. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  2043. update_service_descriptor(service, desc, now);
  2044. } FOR_EACH_DESCRIPTOR_END;
  2045. } FOR_EACH_SERVICE_END;
  2046. }
  2047. /* Return true iff the given intro point has expired that is it has been used
  2048. * for too long or we've reached our max seen INTRODUCE2 cell. */
  2049. STATIC int
  2050. intro_point_should_expire(const hs_service_intro_point_t *ip,
  2051. time_t now)
  2052. {
  2053. tor_assert(ip);
  2054. if (ip->introduce2_count >= ip->introduce2_max) {
  2055. goto expired;
  2056. }
  2057. if (ip->time_to_expire <= now) {
  2058. goto expired;
  2059. }
  2060. /* Not expiring. */
  2061. return 0;
  2062. expired:
  2063. return 1;
  2064. }
  2065. /* Go over the given set of intro points for each service and remove any
  2066. * invalid ones. The conditions for removal are:
  2067. *
  2068. * - The node doesn't exists anymore (not in consensus)
  2069. * OR
  2070. * - The intro point maximum circuit retry count has been reached and no
  2071. * circuit can be found associated with it.
  2072. * OR
  2073. * - The intro point has expired and we should pick a new one.
  2074. *
  2075. * If an intro point is removed, the circuit (if any) is immediately close.
  2076. * If a circuit can't be found, the intro point is kept if it hasn't reached
  2077. * its maximum circuit retry value and thus should be retried. */
  2078. static void
  2079. cleanup_intro_points(hs_service_t *service, time_t now)
  2080. {
  2081. /* List of intro points to close. We can't mark the intro circuits for close
  2082. * in the modify loop because doing so calls
  2083. * hs_service_intro_circ_has_closed() which does a digest256map_get() on the
  2084. * intro points map (that we are iterating over). This can't be done in a
  2085. * single iteration after a MAP_DEL_CURRENT, the object will still be
  2086. * returned leading to a use-after-free. So, we close the circuits and free
  2087. * the intro points after the loop if any. */
  2088. smartlist_t *ips_to_free = smartlist_new();
  2089. tor_assert(service);
  2090. /* For both descriptors, cleanup the intro points. */
  2091. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  2092. /* Go over the current intro points we have, make sure they are still
  2093. * valid and remove any of them that aren't. */
  2094. DIGEST256MAP_FOREACH_MODIFY(desc->intro_points.map, key,
  2095. hs_service_intro_point_t *, ip) {
  2096. const node_t *node = get_node_from_intro_point(ip);
  2097. int has_expired = intro_point_should_expire(ip, now);
  2098. /* We cleanup an intro point if it has expired or if we do not know the
  2099. * node_t anymore (removed from our latest consensus) or if we've
  2100. * reached the maximum number of retry with a non existing circuit. */
  2101. if (has_expired || node == NULL ||
  2102. ip->circuit_retries > MAX_INTRO_POINT_CIRCUIT_RETRIES) {
  2103. log_info(LD_REND, "Intro point %s%s (retried: %u times). "
  2104. "Removing it.",
  2105. describe_intro_point(ip),
  2106. has_expired ? " has expired" :
  2107. (node == NULL) ? " fell off the consensus" : "",
  2108. ip->circuit_retries);
  2109. /* We've retried too many times, remember it as a failed intro point
  2110. * so we don't pick it up again for INTRO_CIRC_RETRY_PERIOD sec. */
  2111. if (ip->circuit_retries > MAX_INTRO_POINT_CIRCUIT_RETRIES) {
  2112. remember_failing_intro_point(ip, desc, approx_time());
  2113. }
  2114. /* Remove intro point from descriptor map and add it to the list of
  2115. * ips to free for which we'll also try to close the intro circuit. */
  2116. MAP_DEL_CURRENT(key);
  2117. smartlist_add(ips_to_free, ip);
  2118. }
  2119. } DIGEST256MAP_FOREACH_END;
  2120. } FOR_EACH_DESCRIPTOR_END;
  2121. /* Go over the intro points to free and close their circuit if any. */
  2122. SMARTLIST_FOREACH_BEGIN(ips_to_free, hs_service_intro_point_t *, ip) {
  2123. /* See if we need to close the intro point circuit as well */
  2124. /* XXX: Legacy code does NOT close circuits like this: it keeps the circuit
  2125. * open until a new descriptor is uploaded and then closed all expiring
  2126. * intro point circuit. Here, we close immediately and because we just
  2127. * discarded the intro point, a new one will be selected, a new descriptor
  2128. * created and uploaded. There is no difference to an attacker between the
  2129. * timing of a new consensus and intro point rotation (possibly?). */
  2130. origin_circuit_t *ocirc = hs_circ_service_get_intro_circ(ip);
  2131. if (ocirc && !TO_CIRCUIT(ocirc)->marked_for_close) {
  2132. circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
  2133. }
  2134. /* Cleanup the intro point */
  2135. service_intro_point_free(ip);
  2136. } SMARTLIST_FOREACH_END(ip);
  2137. smartlist_free(ips_to_free);
  2138. }
  2139. /* Set the next rotation time of the descriptors for the given service for the
  2140. * time now. */
  2141. static void
  2142. set_rotation_time(hs_service_t *service)
  2143. {
  2144. tor_assert(service);
  2145. service->state.next_rotation_time =
  2146. sr_state_get_start_time_of_current_protocol_run() +
  2147. sr_state_get_protocol_run_duration();
  2148. {
  2149. char fmt_time[ISO_TIME_LEN + 1];
  2150. format_local_iso_time(fmt_time, service->state.next_rotation_time);
  2151. log_info(LD_REND, "Next descriptor rotation time set to %s for %s",
  2152. fmt_time, safe_str_client(service->onion_address));
  2153. }
  2154. }
  2155. /* Return true iff the service should rotate its descriptor. The time now is
  2156. * only used to fetch the live consensus and if none can be found, this
  2157. * returns false. */
  2158. static unsigned int
  2159. should_rotate_descriptors(hs_service_t *service, time_t now)
  2160. {
  2161. const networkstatus_t *ns;
  2162. tor_assert(service);
  2163. ns = networkstatus_get_live_consensus(now);
  2164. if (ns == NULL) {
  2165. goto no_rotation;
  2166. }
  2167. if (ns->valid_after >= service->state.next_rotation_time) {
  2168. /* In theory, we should never get here with no descriptors. We can never
  2169. * have a NULL current descriptor except when tor starts up. The next
  2170. * descriptor can be NULL after a rotation but we build a new one right
  2171. * after.
  2172. *
  2173. * So, when tor starts, the next rotation time is set to the start of the
  2174. * next SRV period using the consensus valid after time so it should
  2175. * always be set to a future time value. This means that we should never
  2176. * reach this point at bootup that is this check safeguards tor in never
  2177. * allowing a rotation if the valid after time is smaller than the next
  2178. * rotation time.
  2179. *
  2180. * This is all good in theory but we've had a NULL descriptor issue here
  2181. * so this is why we BUG() on both with extra logging to try to understand
  2182. * how this can possibly happens. We'll simply ignore and tor should
  2183. * recover from this by skipping rotation and building the missing
  2184. * descriptors just after this. */
  2185. if (BUG(service->desc_current == NULL || service->desc_next == NULL)) {
  2186. log_warn(LD_BUG, "Service descriptor is NULL (%p/%p). Next rotation "
  2187. "time is %ld (now: %ld). Valid after time from "
  2188. "consensus is %ld",
  2189. service->desc_current, service->desc_next,
  2190. (long)service->state.next_rotation_time,
  2191. (long)now,
  2192. (long)ns->valid_after);
  2193. goto no_rotation;
  2194. }
  2195. goto rotation;
  2196. }
  2197. no_rotation:
  2198. return 0;
  2199. rotation:
  2200. return 1;
  2201. }
  2202. /* Rotate the service descriptors of the given service. The current descriptor
  2203. * will be freed, the next one put in as the current and finally the next
  2204. * descriptor pointer is NULLified. */
  2205. static void
  2206. rotate_service_descriptors(hs_service_t *service)
  2207. {
  2208. if (service->desc_current) {
  2209. /* Close all IP circuits for the descriptor. */
  2210. close_intro_circuits(&service->desc_current->intro_points);
  2211. /* We don't need this one anymore, we won't serve any clients coming with
  2212. * this service descriptor. */
  2213. service_descriptor_free(service->desc_current);
  2214. }
  2215. /* The next one become the current one and emptying the next will trigger
  2216. * a descriptor creation for it. */
  2217. service->desc_current = service->desc_next;
  2218. service->desc_next = NULL;
  2219. /* We've just rotated, set the next time for the rotation. */
  2220. set_rotation_time(service);
  2221. }
  2222. /* Rotate descriptors for each service if needed. A non existing current
  2223. * descriptor will trigger a descriptor build for the next time period. */
  2224. STATIC void
  2225. rotate_all_descriptors(time_t now)
  2226. {
  2227. /* XXX We rotate all our service descriptors at once. In the future it might
  2228. * be wise, to rotate service descriptors independently to hide that all
  2229. * those descriptors are on the same tor instance */
  2230. FOR_EACH_SERVICE_BEGIN(service) {
  2231. /* Note for a service booting up: Both descriptors are NULL in that case
  2232. * so this function might return true if we are in the timeframe for a
  2233. * rotation leading to basically swapping two NULL pointers which is
  2234. * harmless. However, the side effect is that triggering a rotation will
  2235. * update the service state and avoid doing anymore rotations after the
  2236. * two descriptors have been built. */
  2237. if (!should_rotate_descriptors(service, now)) {
  2238. continue;
  2239. }
  2240. log_info(LD_REND, "Time to rotate our descriptors (%p / %p) for %s",
  2241. service->desc_current, service->desc_next,
  2242. safe_str_client(service->onion_address));
  2243. rotate_service_descriptors(service);
  2244. } FOR_EACH_SERVICE_END;
  2245. }
  2246. /* Scheduled event run from the main loop. Make sure all our services are up
  2247. * to date and ready for the other scheduled events. This includes looking at
  2248. * the introduction points status and descriptor rotation time. */
  2249. STATIC void
  2250. run_housekeeping_event(time_t now)
  2251. {
  2252. /* Note that nothing here opens circuit(s) nor uploads descriptor(s). We are
  2253. * simply moving things around or removing unneeded elements. */
  2254. FOR_EACH_SERVICE_BEGIN(service) {
  2255. /* If the service is starting off, set the rotation time. We can't do that
  2256. * at configure time because the get_options() needs to be set for setting
  2257. * that time that uses the voting interval. */
  2258. if (service->state.next_rotation_time == 0) {
  2259. /* Set the next rotation time of the descriptors. If it's Oct 25th
  2260. * 23:47:00, the next rotation time is when the next SRV is computed
  2261. * which is at Oct 26th 00:00:00 that is in 13 minutes. */
  2262. set_rotation_time(service);
  2263. }
  2264. /* Cleanup invalid intro points from the service descriptor. */
  2265. cleanup_intro_points(service, now);
  2266. /* Remove expired failing intro point from the descriptor failed list. We
  2267. * reset them at each INTRO_CIRC_RETRY_PERIOD. */
  2268. remove_expired_failing_intro(service, now);
  2269. /* At this point, the service is now ready to go through the scheduled
  2270. * events guaranteeing a valid state. Intro points might be missing from
  2271. * the descriptors after the cleanup but the update/build process will
  2272. * make sure we pick those missing ones. */
  2273. } FOR_EACH_SERVICE_END;
  2274. }
  2275. /* Scheduled event run from the main loop. Make sure all descriptors are up to
  2276. * date. Once this returns, each service descriptor needs to be considered for
  2277. * new introduction circuits and then for upload. */
  2278. static void
  2279. run_build_descriptor_event(time_t now)
  2280. {
  2281. /* For v2 services, this step happens in the upload event. */
  2282. /* Run v3+ events. */
  2283. /* We start by rotating the descriptors only if needed. */
  2284. rotate_all_descriptors(now);
  2285. /* Then, we'll try to build new descriptors that we might need. The
  2286. * condition is that the next descriptor is non existing because it has
  2287. * been rotated or we just started up. */
  2288. build_all_descriptors(now);
  2289. /* Finally, we'll check if we should update the descriptors. Missing
  2290. * introduction points will be picked in this function which is useful for
  2291. * newly built descriptors. */
  2292. update_all_descriptors(now);
  2293. }
  2294. /* For the given service, launch any intro point circuits that could be
  2295. * needed. This considers every descriptor of the service. */
  2296. static void
  2297. launch_intro_point_circuits(hs_service_t *service)
  2298. {
  2299. tor_assert(service);
  2300. /* For both descriptors, try to launch any missing introduction point
  2301. * circuits using the current map. */
  2302. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  2303. /* Keep a ref on if we need a direct connection. We use this often. */
  2304. unsigned int direct_conn = service->config.is_single_onion;
  2305. DIGEST256MAP_FOREACH_MODIFY(desc->intro_points.map, key,
  2306. hs_service_intro_point_t *, ip) {
  2307. extend_info_t *ei;
  2308. /* Skip the intro point that already has an existing circuit
  2309. * (established or not). */
  2310. if (hs_circ_service_get_intro_circ(ip)) {
  2311. continue;
  2312. }
  2313. ei = get_extend_info_from_intro_point(ip, direct_conn);
  2314. if (ei == NULL) {
  2315. /* This is possible if we can get a node_t but not the extend info out
  2316. * of it. In this case, we remove the intro point and a new one will
  2317. * be picked at the next main loop callback. */
  2318. MAP_DEL_CURRENT(key);
  2319. service_intro_point_free(ip);
  2320. continue;
  2321. }
  2322. /* Launch a circuit to the intro point. */
  2323. ip->circuit_retries++;
  2324. if (hs_circ_launch_intro_point(service, ip, ei) < 0) {
  2325. log_info(LD_REND, "Unable to launch intro circuit to node %s "
  2326. "for service %s.",
  2327. safe_str_client(extend_info_describe(ei)),
  2328. safe_str_client(service->onion_address));
  2329. /* Intro point will be retried if possible after this. */
  2330. }
  2331. extend_info_free(ei);
  2332. } DIGEST256MAP_FOREACH_END;
  2333. } FOR_EACH_DESCRIPTOR_END;
  2334. }
  2335. /* Don't try to build more than this many circuits before giving up for a
  2336. * while. Dynamically calculated based on the configured number of intro
  2337. * points for the given service and how many descriptor exists. The default
  2338. * use case of 3 introduction points and two descriptors will allow 28
  2339. * circuits for a retry period (((3 + 2) + (3 * 3)) * 2). */
  2340. static unsigned int
  2341. get_max_intro_circ_per_period(const hs_service_t *service)
  2342. {
  2343. unsigned int count = 0;
  2344. unsigned int multiplier = 0;
  2345. unsigned int num_wanted_ip;
  2346. tor_assert(service);
  2347. tor_assert(service->config.num_intro_points <=
  2348. HS_CONFIG_V3_MAX_INTRO_POINTS);
  2349. /* For a testing network, allow to do it for the maximum amount so circuit
  2350. * creation and rotation and so on can actually be tested without limit. */
  2351. #define MAX_INTRO_POINT_CIRCUIT_RETRIES_TESTING -1
  2352. if (get_options()->TestingTorNetwork) {
  2353. return MAX_INTRO_POINT_CIRCUIT_RETRIES_TESTING;
  2354. }
  2355. num_wanted_ip = service->config.num_intro_points;
  2356. /* The calculation is as follow. We have a number of intro points that we
  2357. * want configured as a torrc option (num_intro_points). We then add an
  2358. * extra value so we can launch multiple circuits at once and pick the
  2359. * quickest ones. For instance, we want 3 intros, we add 2 extra so we'll
  2360. * pick 5 intros and launch 5 circuits. */
  2361. count += (num_wanted_ip + get_intro_point_num_extra());
  2362. /* Then we add the number of retries that is possible to do for each intro
  2363. * point. If we want 3 intros, we'll allow 3 times the number of possible
  2364. * retry. */
  2365. count += (num_wanted_ip * MAX_INTRO_POINT_CIRCUIT_RETRIES);
  2366. /* Then, we multiply by a factor of 2 if we have both descriptor or 0 if we
  2367. * have none. */
  2368. multiplier += (service->desc_current) ? 1 : 0;
  2369. multiplier += (service->desc_next) ? 1 : 0;
  2370. return (count * multiplier);
  2371. }
  2372. /* For the given service, return 1 if the service is allowed to launch more
  2373. * introduction circuits else 0 if the maximum has been reached for the retry
  2374. * period of INTRO_CIRC_RETRY_PERIOD. */
  2375. STATIC int
  2376. can_service_launch_intro_circuit(hs_service_t *service, time_t now)
  2377. {
  2378. tor_assert(service);
  2379. /* Consider the intro circuit retry period of the service. */
  2380. if (now > (service->state.intro_circ_retry_started_time +
  2381. INTRO_CIRC_RETRY_PERIOD)) {
  2382. service->state.intro_circ_retry_started_time = now;
  2383. service->state.num_intro_circ_launched = 0;
  2384. goto allow;
  2385. }
  2386. /* Check if we can still launch more circuits in this period. */
  2387. if (service->state.num_intro_circ_launched <=
  2388. get_max_intro_circ_per_period(service)) {
  2389. goto allow;
  2390. }
  2391. /* Rate limit log that we've reached our circuit creation limit. */
  2392. {
  2393. char *msg;
  2394. time_t elapsed_time = now - service->state.intro_circ_retry_started_time;
  2395. static ratelim_t rlimit = RATELIM_INIT(INTRO_CIRC_RETRY_PERIOD);
  2396. if ((msg = rate_limit_log(&rlimit, now))) {
  2397. log_info(LD_REND, "Hidden service %s exceeded its circuit launch limit "
  2398. "of %u per %d seconds. It launched %u circuits in "
  2399. "the last %ld seconds. Will retry in %ld seconds.",
  2400. safe_str_client(service->onion_address),
  2401. get_max_intro_circ_per_period(service),
  2402. INTRO_CIRC_RETRY_PERIOD,
  2403. service->state.num_intro_circ_launched,
  2404. (long int) elapsed_time,
  2405. (long int) (INTRO_CIRC_RETRY_PERIOD - elapsed_time));
  2406. tor_free(msg);
  2407. }
  2408. }
  2409. /* Not allow. */
  2410. return 0;
  2411. allow:
  2412. return 1;
  2413. }
  2414. /* Scheduled event run from the main loop. Make sure we have all the circuits
  2415. * we need for each service. */
  2416. static void
  2417. run_build_circuit_event(time_t now)
  2418. {
  2419. /* Make sure we can actually have enough information or able to build
  2420. * internal circuits as required by services. */
  2421. if (router_have_consensus_path() == CONSENSUS_PATH_UNKNOWN ||
  2422. !have_completed_a_circuit()) {
  2423. return;
  2424. }
  2425. /* Run v2 check. */
  2426. if (rend_num_services() > 0) {
  2427. rend_consider_services_intro_points(now);
  2428. }
  2429. /* Run v3+ check. */
  2430. FOR_EACH_SERVICE_BEGIN(service) {
  2431. /* For introduction circuit, we need to make sure we don't stress too much
  2432. * circuit creation so make sure this service is respecting that limit. */
  2433. if (can_service_launch_intro_circuit(service, now)) {
  2434. /* Launch intro point circuits if needed. */
  2435. launch_intro_point_circuits(service);
  2436. /* Once the circuits have opened, we'll make sure to update the
  2437. * descriptor intro point list and cleanup any extraneous. */
  2438. }
  2439. } FOR_EACH_SERVICE_END;
  2440. }
  2441. /* Encode and sign the service descriptor desc and upload it to the given
  2442. * hidden service directory. This does nothing if PublishHidServDescriptors
  2443. * is false. */
  2444. static void
  2445. upload_descriptor_to_hsdir(const hs_service_t *service,
  2446. hs_service_descriptor_t *desc, const node_t *hsdir)
  2447. {
  2448. char *encoded_desc = NULL;
  2449. tor_assert(service);
  2450. tor_assert(desc);
  2451. tor_assert(hsdir);
  2452. /* Let's avoid doing that if tor is configured to not publish. */
  2453. if (!get_options()->PublishHidServDescriptors) {
  2454. log_info(LD_REND, "Service %s not publishing descriptor. "
  2455. "PublishHidServDescriptors is set to 1.",
  2456. safe_str_client(service->onion_address));
  2457. goto end;
  2458. }
  2459. /* First of all, we'll encode the descriptor. This should NEVER fail but
  2460. * just in case, let's make sure we have an actual usable descriptor. */
  2461. if (BUG(service_encode_descriptor(service, desc, &desc->signing_kp,
  2462. &encoded_desc) < 0)) {
  2463. goto end;
  2464. }
  2465. /* Time to upload the descriptor to the directory. */
  2466. hs_service_upload_desc_to_dir(encoded_desc, service->config.version,
  2467. &service->keys.identity_pk,
  2468. &desc->blinded_kp.pubkey, hsdir->rs);
  2469. /* Add this node to previous_hsdirs list */
  2470. service_desc_note_upload(desc, hsdir);
  2471. /* Logging so we know where it was sent. */
  2472. {
  2473. int is_next_desc = (service->desc_next == desc);
  2474. const uint8_t *idx = (is_next_desc) ? hsdir->hsdir_index.store_second:
  2475. hsdir->hsdir_index.store_first;
  2476. char *blinded_pubkey_log_str =
  2477. tor_strdup(hex_str((char*)&desc->blinded_kp.pubkey.pubkey, 32));
  2478. log_info(LD_REND, "Service %s %s descriptor of revision %" PRIu64
  2479. " initiated upload request to %s with index %s (%s)",
  2480. safe_str_client(service->onion_address),
  2481. (is_next_desc) ? "next" : "current",
  2482. desc->desc->plaintext_data.revision_counter,
  2483. safe_str_client(node_describe(hsdir)),
  2484. safe_str_client(hex_str((const char *) idx, 32)),
  2485. safe_str_client(blinded_pubkey_log_str));
  2486. tor_free(blinded_pubkey_log_str);
  2487. /* Fire a UPLOAD control port event. */
  2488. hs_control_desc_event_upload(service->onion_address, hsdir->identity,
  2489. &desc->blinded_kp.pubkey, idx);
  2490. }
  2491. end:
  2492. tor_free(encoded_desc);
  2493. return;
  2494. }
  2495. /** Set the revision counter in <b>hs_desc</b>. We do this by encrypting a
  2496. * timestamp using an OPE scheme and using the ciphertext as our revision
  2497. * counter.
  2498. *
  2499. * If <b>is_current</b> is true, then this is the current HS descriptor,
  2500. * otherwise it's the next one. */
  2501. static void
  2502. set_descriptor_revision_counter(hs_service_descriptor_t *hs_desc, time_t now,
  2503. bool is_current)
  2504. {
  2505. uint64_t rev_counter = 0;
  2506. /* Get current time */
  2507. time_t srv_start = 0;
  2508. /* As our revision counter plaintext value, we use the seconds since the
  2509. * start of the SR protocol run that is relevant to this descriptor. This is
  2510. * guaranteed to be a positive value since we need the SRV to start making a
  2511. * descriptor (so that we know where to upload it).
  2512. *
  2513. * Depending on whether we are building the current or the next descriptor,
  2514. * services use a different SRV value. See [SERVICEUPLOAD] in
  2515. * rend-spec-v3.txt:
  2516. *
  2517. * In particular, for the current descriptor (aka first descriptor), Tor
  2518. * always uses the previous SRV for uploading the descriptor, and hence we
  2519. * should use the start time of the previous protocol run here.
  2520. *
  2521. * Whereas for the next descriptor (aka second descriptor), Tor always uses
  2522. * the current SRV for uploading the descriptor. and hence we use the start
  2523. * time of the current protocol run.
  2524. */
  2525. if (is_current) {
  2526. srv_start = sr_state_get_start_time_of_previous_protocol_run();
  2527. } else {
  2528. srv_start = sr_state_get_start_time_of_current_protocol_run();
  2529. }
  2530. log_info(LD_REND, "Setting rev counter for TP #%u: "
  2531. "SRV started at %d, now %d (%s)",
  2532. (unsigned) hs_desc->time_period_num, (int)srv_start,
  2533. (int)now, is_current ? "current" : "next");
  2534. tor_assert_nonfatal(now >= srv_start);
  2535. /* Compute seconds elapsed since the start of the time period. That's the
  2536. * number of seconds of how long this blinded key has been active. */
  2537. time_t seconds_since_start_of_srv = now - srv_start;
  2538. /* Increment by one so that we are definitely sure this is strictly
  2539. * positive and not zero. */
  2540. seconds_since_start_of_srv++;
  2541. /* Check for too big inputs. */
  2542. if (BUG(seconds_since_start_of_srv > OPE_INPUT_MAX)) {
  2543. seconds_since_start_of_srv = OPE_INPUT_MAX;
  2544. }
  2545. /* Now we compute the final revision counter value by encrypting the
  2546. plaintext using our OPE cipher: */
  2547. tor_assert(hs_desc->ope_cipher);
  2548. rev_counter = crypto_ope_encrypt(hs_desc->ope_cipher,
  2549. (int) seconds_since_start_of_srv);
  2550. /* The OPE module returns CRYPTO_OPE_ERROR in case of errors. */
  2551. tor_assert_nonfatal(rev_counter < CRYPTO_OPE_ERROR);
  2552. log_info(LD_REND, "Encrypted revision counter %d to %ld",
  2553. (int) seconds_since_start_of_srv, (long int) rev_counter);
  2554. hs_desc->desc->plaintext_data.revision_counter = rev_counter;
  2555. }
  2556. /* Encode and sign the service descriptor desc and upload it to the
  2557. * responsible hidden service directories. If for_next_period is true, the set
  2558. * of directories are selected using the next hsdir_index. This does nothing
  2559. * if PublishHidServDescriptors is false. */
  2560. STATIC void
  2561. upload_descriptor_to_all(const hs_service_t *service,
  2562. hs_service_descriptor_t *desc)
  2563. {
  2564. smartlist_t *responsible_dirs = NULL;
  2565. tor_assert(service);
  2566. tor_assert(desc);
  2567. /* We'll first cancel any directory request that are ongoing for this
  2568. * descriptor. It is possible that we can trigger multiple uploads in a
  2569. * short time frame which can lead to a race where the second upload arrives
  2570. * before the first one leading to a 400 malformed descriptor response from
  2571. * the directory. Closing all pending requests avoids that. */
  2572. close_directory_connections(service, desc);
  2573. /* Get our list of responsible HSDir. */
  2574. responsible_dirs = smartlist_new();
  2575. /* The parameter 0 means that we aren't a client so tell the function to use
  2576. * the spread store consensus paremeter. */
  2577. hs_get_responsible_hsdirs(&desc->blinded_kp.pubkey, desc->time_period_num,
  2578. service->desc_next == desc, 0, responsible_dirs);
  2579. /** Clear list of previous hsdirs since we are about to upload to a new
  2580. * list. Let's keep it up to date. */
  2581. service_desc_clear_previous_hsdirs(desc);
  2582. /* For each responsible HSDir we have, initiate an upload command. */
  2583. SMARTLIST_FOREACH_BEGIN(responsible_dirs, const routerstatus_t *,
  2584. hsdir_rs) {
  2585. const node_t *hsdir_node = node_get_by_id(hsdir_rs->identity_digest);
  2586. /* Getting responsible hsdir implies that the node_t object exists for the
  2587. * routerstatus_t found in the consensus else we have a problem. */
  2588. tor_assert(hsdir_node);
  2589. /* Upload this descriptor to the chosen directory. */
  2590. upload_descriptor_to_hsdir(service, desc, hsdir_node);
  2591. } SMARTLIST_FOREACH_END(hsdir_rs);
  2592. /* Set the next upload time for this descriptor. Even if we are configured
  2593. * to not upload, we still want to follow the right cycle of life for this
  2594. * descriptor. */
  2595. desc->next_upload_time =
  2596. (time(NULL) + crypto_rand_int_range(HS_SERVICE_NEXT_UPLOAD_TIME_MIN,
  2597. HS_SERVICE_NEXT_UPLOAD_TIME_MAX));
  2598. {
  2599. char fmt_next_time[ISO_TIME_LEN+1];
  2600. format_local_iso_time(fmt_next_time, desc->next_upload_time);
  2601. log_debug(LD_REND, "Service %s set to upload a descriptor at %s",
  2602. safe_str_client(service->onion_address), fmt_next_time);
  2603. }
  2604. smartlist_free(responsible_dirs);
  2605. return;
  2606. }
  2607. /** The set of HSDirs have changed: check if the change affects our descriptor
  2608. * HSDir placement, and if it does, reupload the desc. */
  2609. STATIC int
  2610. service_desc_hsdirs_changed(const hs_service_t *service,
  2611. const hs_service_descriptor_t *desc)
  2612. {
  2613. int should_reupload = 0;
  2614. smartlist_t *responsible_dirs = smartlist_new();
  2615. /* No desc upload has happened yet: it will happen eventually */
  2616. if (!desc->previous_hsdirs || !smartlist_len(desc->previous_hsdirs)) {
  2617. goto done;
  2618. }
  2619. /* Get list of responsible hsdirs */
  2620. hs_get_responsible_hsdirs(&desc->blinded_kp.pubkey, desc->time_period_num,
  2621. service->desc_next == desc, 0, responsible_dirs);
  2622. /* Check if any new hsdirs have been added to the responsible hsdirs set:
  2623. * Iterate over the list of new hsdirs, and reupload if any of them is not
  2624. * present in the list of previous hsdirs.
  2625. */
  2626. SMARTLIST_FOREACH_BEGIN(responsible_dirs, const routerstatus_t *, hsdir_rs) {
  2627. char b64_digest[BASE64_DIGEST_LEN+1] = {0};
  2628. digest_to_base64(b64_digest, hsdir_rs->identity_digest);
  2629. if (!smartlist_contains_string(desc->previous_hsdirs, b64_digest)) {
  2630. should_reupload = 1;
  2631. break;
  2632. }
  2633. } SMARTLIST_FOREACH_END(hsdir_rs);
  2634. done:
  2635. smartlist_free(responsible_dirs);
  2636. return should_reupload;
  2637. }
  2638. /* Return 1 if the given descriptor from the given service can be uploaded
  2639. * else return 0 if it can not. */
  2640. static int
  2641. should_service_upload_descriptor(const hs_service_t *service,
  2642. const hs_service_descriptor_t *desc, time_t now)
  2643. {
  2644. unsigned int num_intro_points;
  2645. tor_assert(service);
  2646. tor_assert(desc);
  2647. /* If this descriptors has missing intro points that is that it couldn't get
  2648. * them all when it was time to pick them, it means that we should upload
  2649. * instead of waiting an arbitrary amount of time breaking the service.
  2650. * Else, if we have no missing intro points, we use the value taken from the
  2651. * service configuration. */
  2652. if (desc->missing_intro_points) {
  2653. num_intro_points = digest256map_size(desc->intro_points.map);
  2654. } else {
  2655. num_intro_points = service->config.num_intro_points;
  2656. }
  2657. /* This means we tried to pick intro points but couldn't get any so do not
  2658. * upload descriptor in this case. We need at least one for the service to
  2659. * be reachable. */
  2660. if (desc->missing_intro_points && num_intro_points == 0) {
  2661. goto cannot;
  2662. }
  2663. /* Check if all our introduction circuit have been established for all the
  2664. * intro points we have selected. */
  2665. if (count_desc_circuit_established(desc) != num_intro_points) {
  2666. goto cannot;
  2667. }
  2668. /* Is it the right time to upload? */
  2669. if (desc->next_upload_time > now) {
  2670. goto cannot;
  2671. }
  2672. /* Don't upload desc if we don't have a live consensus */
  2673. if (!networkstatus_get_live_consensus(now)) {
  2674. goto cannot;
  2675. }
  2676. /* Do we know enough router descriptors to have adequate vision of the HSDir
  2677. hash ring? */
  2678. if (!router_have_minimum_dir_info()) {
  2679. goto cannot;
  2680. }
  2681. /* Can upload! */
  2682. return 1;
  2683. cannot:
  2684. return 0;
  2685. }
  2686. /* Scheduled event run from the main loop. Try to upload the descriptor for
  2687. * each service. */
  2688. STATIC void
  2689. run_upload_descriptor_event(time_t now)
  2690. {
  2691. /* v2 services use the same function for descriptor creation and upload so
  2692. * we do everything here because the intro circuits were checked before. */
  2693. if (rend_num_services() > 0) {
  2694. rend_consider_services_upload(now);
  2695. rend_consider_descriptor_republication();
  2696. }
  2697. /* Run v3+ check. */
  2698. FOR_EACH_SERVICE_BEGIN(service) {
  2699. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  2700. /* If we were asked to re-examine the hash ring, and it changed, then
  2701. schedule an upload */
  2702. if (consider_republishing_hs_descriptors &&
  2703. service_desc_hsdirs_changed(service, desc)) {
  2704. service_desc_schedule_upload(desc, now, 0);
  2705. }
  2706. /* Can this descriptor be uploaded? */
  2707. if (!should_service_upload_descriptor(service, desc, now)) {
  2708. continue;
  2709. }
  2710. log_info(LD_REND, "Initiating upload for hidden service %s descriptor "
  2711. "for service %s with %u/%u introduction points%s.",
  2712. (desc == service->desc_current) ? "current" : "next",
  2713. safe_str_client(service->onion_address),
  2714. digest256map_size(desc->intro_points.map),
  2715. service->config.num_intro_points,
  2716. (desc->missing_intro_points) ? " (couldn't pick more)" : "");
  2717. /* At this point, we have to upload the descriptor so start by building
  2718. * the intro points descriptor section which we are now sure to be
  2719. * accurate because all circuits have been established. */
  2720. build_desc_intro_points(service, desc, now);
  2721. /* Set the desc revision counter right before uploading */
  2722. set_descriptor_revision_counter(desc, approx_time(),
  2723. service->desc_current == desc);
  2724. upload_descriptor_to_all(service, desc);
  2725. } FOR_EACH_DESCRIPTOR_END;
  2726. } FOR_EACH_SERVICE_END;
  2727. /* We are done considering whether to republish rend descriptors */
  2728. consider_republishing_hs_descriptors = 0;
  2729. }
  2730. /* Called when the introduction point circuit is done building and ready to be
  2731. * used. */
  2732. static void
  2733. service_intro_circ_has_opened(origin_circuit_t *circ)
  2734. {
  2735. hs_service_t *service = NULL;
  2736. hs_service_intro_point_t *ip = NULL;
  2737. hs_service_descriptor_t *desc = NULL;
  2738. tor_assert(circ);
  2739. /* Let's do some basic sanity checking of the circ state */
  2740. if (BUG(!circ->cpath)) {
  2741. return;
  2742. }
  2743. if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)) {
  2744. return;
  2745. }
  2746. if (BUG(!circ->hs_ident)) {
  2747. return;
  2748. }
  2749. /* Get the corresponding service and intro point. */
  2750. get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
  2751. if (service == NULL) {
  2752. log_warn(LD_REND, "Unknown service identity key %s on the introduction "
  2753. "circuit %u. Can't find onion service.",
  2754. safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
  2755. TO_CIRCUIT(circ)->n_circ_id);
  2756. goto err;
  2757. }
  2758. if (ip == NULL) {
  2759. log_warn(LD_REND, "Unknown introduction point auth key on circuit %u "
  2760. "for service %s",
  2761. TO_CIRCUIT(circ)->n_circ_id,
  2762. safe_str_client(service->onion_address));
  2763. goto err;
  2764. }
  2765. /* We can't have an IP object without a descriptor. */
  2766. tor_assert(desc);
  2767. if (hs_circ_service_intro_has_opened(service, ip, desc, circ)) {
  2768. /* Getting here means that the circuit has been re-purposed because we
  2769. * have enough intro circuit opened. Remove the IP from the service. */
  2770. service_intro_point_remove(service, ip);
  2771. service_intro_point_free(ip);
  2772. }
  2773. goto done;
  2774. err:
  2775. /* Close circuit, we can't use it. */
  2776. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOSUCHSERVICE);
  2777. done:
  2778. return;
  2779. }
  2780. /* Called when a rendezvous circuit is done building and ready to be used. */
  2781. static void
  2782. service_rendezvous_circ_has_opened(origin_circuit_t *circ)
  2783. {
  2784. hs_service_t *service = NULL;
  2785. tor_assert(circ);
  2786. tor_assert(circ->cpath);
  2787. /* Getting here means this is a v3 rendezvous circuit. */
  2788. tor_assert(circ->hs_ident);
  2789. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  2790. /* Declare the circuit dirty to avoid reuse, and for path-bias. We set the
  2791. * timestamp regardless of its content because that circuit could have been
  2792. * cannibalized so in any cases, we are about to use that circuit more. */
  2793. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  2794. pathbias_count_use_attempt(circ);
  2795. /* Get the corresponding service and intro point. */
  2796. get_objects_from_ident(circ->hs_ident, &service, NULL, NULL);
  2797. if (service == NULL) {
  2798. log_warn(LD_REND, "Unknown service identity key %s on the rendezvous "
  2799. "circuit %u with cookie %s. Can't find onion service.",
  2800. safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
  2801. TO_CIRCUIT(circ)->n_circ_id,
  2802. hex_str((const char *) circ->hs_ident->rendezvous_cookie,
  2803. REND_COOKIE_LEN));
  2804. goto err;
  2805. }
  2806. /* If the cell can't be sent, the circuit will be closed within this
  2807. * function. */
  2808. hs_circ_service_rp_has_opened(service, circ);
  2809. goto done;
  2810. err:
  2811. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOSUCHSERVICE);
  2812. done:
  2813. return;
  2814. }
  2815. /* We've been expecting an INTRO_ESTABLISHED cell on this circuit and it just
  2816. * arrived. Handle the INTRO_ESTABLISHED cell arriving on the given
  2817. * introduction circuit. Return 0 on success else a negative value. */
  2818. static int
  2819. service_handle_intro_established(origin_circuit_t *circ,
  2820. const uint8_t *payload,
  2821. size_t payload_len)
  2822. {
  2823. hs_service_t *service = NULL;
  2824. hs_service_intro_point_t *ip = NULL;
  2825. tor_assert(circ);
  2826. tor_assert(payload);
  2827. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  2828. /* We need the service and intro point for this cell. */
  2829. get_objects_from_ident(circ->hs_ident, &service, &ip, NULL);
  2830. /* Get service object from the circuit identifier. */
  2831. if (service == NULL) {
  2832. log_warn(LD_REND, "Unknown service identity key %s on the introduction "
  2833. "circuit %u. Can't find onion service.",
  2834. safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
  2835. TO_CIRCUIT(circ)->n_circ_id);
  2836. goto err;
  2837. }
  2838. if (ip == NULL) {
  2839. /* We don't recognize the key. */
  2840. log_warn(LD_REND, "Introduction circuit established without an intro "
  2841. "point object on circuit %u for service %s",
  2842. TO_CIRCUIT(circ)->n_circ_id,
  2843. safe_str_client(service->onion_address));
  2844. goto err;
  2845. }
  2846. /* Try to parse the payload into a cell making sure we do actually have a
  2847. * valid cell. On success, the ip object and circuit purpose is updated to
  2848. * reflect the fact that the introduction circuit is established. */
  2849. if (hs_circ_handle_intro_established(service, ip, circ, payload,
  2850. payload_len) < 0) {
  2851. goto err;
  2852. }
  2853. /* Flag that we have an established circuit for this intro point. This value
  2854. * is what indicates the upload scheduled event if we are ready to build the
  2855. * intro point into the descriptor and upload. */
  2856. ip->circuit_established = 1;
  2857. log_info(LD_REND, "Successfully received an INTRO_ESTABLISHED cell "
  2858. "on circuit %u for service %s",
  2859. TO_CIRCUIT(circ)->n_circ_id,
  2860. safe_str_client(service->onion_address));
  2861. return 0;
  2862. err:
  2863. return -1;
  2864. }
  2865. /* We just received an INTRODUCE2 cell on the established introduction circuit
  2866. * circ. Handle the cell and return 0 on success else a negative value. */
  2867. static int
  2868. service_handle_introduce2(origin_circuit_t *circ, const uint8_t *payload,
  2869. size_t payload_len)
  2870. {
  2871. hs_service_t *service = NULL;
  2872. hs_service_intro_point_t *ip = NULL;
  2873. hs_service_descriptor_t *desc = NULL;
  2874. tor_assert(circ);
  2875. tor_assert(payload);
  2876. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_INTRO);
  2877. /* We'll need every object associated with this circuit. */
  2878. get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
  2879. /* Get service object from the circuit identifier. */
  2880. if (service == NULL) {
  2881. log_warn(LD_BUG, "Unknown service identity key %s when handling "
  2882. "an INTRODUCE2 cell on circuit %u",
  2883. safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
  2884. TO_CIRCUIT(circ)->n_circ_id);
  2885. goto err;
  2886. }
  2887. if (ip == NULL) {
  2888. /* We don't recognize the key. */
  2889. log_warn(LD_BUG, "Unknown introduction auth key when handling "
  2890. "an INTRODUCE2 cell on circuit %u for service %s",
  2891. TO_CIRCUIT(circ)->n_circ_id,
  2892. safe_str_client(service->onion_address));
  2893. goto err;
  2894. }
  2895. /* If we have an IP object, we MUST have a descriptor object. */
  2896. tor_assert(desc);
  2897. /* The following will parse, decode and launch the rendezvous point circuit.
  2898. * Both current and legacy cells are handled. */
  2899. if (hs_circ_handle_introduce2(service, circ, ip, desc->desc->subcredential,
  2900. payload, payload_len) < 0) {
  2901. goto err;
  2902. }
  2903. return 0;
  2904. err:
  2905. return -1;
  2906. }
  2907. /* Add to list every filename used by service. This is used by the sandbox
  2908. * subsystem. */
  2909. static void
  2910. service_add_fnames_to_list(const hs_service_t *service, smartlist_t *list)
  2911. {
  2912. const char *s_dir;
  2913. char fname[128] = {0};
  2914. tor_assert(service);
  2915. tor_assert(list);
  2916. /* Ease our life. */
  2917. s_dir = service->config.directory_path;
  2918. /* The hostname file. */
  2919. smartlist_add(list, hs_path_from_filename(s_dir, fname_hostname));
  2920. /* The key files splitted in two. */
  2921. tor_snprintf(fname, sizeof(fname), "%s_secret_key", fname_keyfile_prefix);
  2922. smartlist_add(list, hs_path_from_filename(s_dir, fname));
  2923. tor_snprintf(fname, sizeof(fname), "%s_public_key", fname_keyfile_prefix);
  2924. smartlist_add(list, hs_path_from_filename(s_dir, fname));
  2925. }
  2926. /* Return true iff the given service identity key is present on disk. */
  2927. static int
  2928. service_key_on_disk(const char *directory_path)
  2929. {
  2930. int ret = 0;
  2931. char *fname;
  2932. ed25519_keypair_t *kp = NULL;
  2933. tor_assert(directory_path);
  2934. /* Build the v3 key path name and then try to load it. */
  2935. fname = hs_path_from_filename(directory_path, fname_keyfile_prefix);
  2936. kp = ed_key_init_from_file(fname, INIT_ED_KEY_SPLIT,
  2937. LOG_DEBUG, NULL, 0, 0, 0, NULL, NULL);
  2938. if (kp) {
  2939. ret = 1;
  2940. }
  2941. ed25519_keypair_free(kp);
  2942. tor_free(fname);
  2943. return ret;
  2944. }
  2945. /* This is a proxy function before actually calling hs_desc_encode_descriptor
  2946. * because we need some preprocessing here */
  2947. static int
  2948. service_encode_descriptor(const hs_service_t *service,
  2949. const hs_service_descriptor_t *desc,
  2950. const ed25519_keypair_t *signing_kp,
  2951. char **encoded_out)
  2952. {
  2953. int ret;
  2954. const uint8_t *descriptor_cookie = NULL;
  2955. tor_assert(service);
  2956. tor_assert(desc);
  2957. tor_assert(encoded_out);
  2958. /* If the client authorization is enabled, send the descriptor cookie to
  2959. * hs_desc_encode_descriptor. Otherwise, send NULL */
  2960. if (service->config.is_client_auth_enabled) {
  2961. descriptor_cookie = desc->descriptor_cookie;
  2962. }
  2963. ret = hs_desc_encode_descriptor(desc->desc, signing_kp,
  2964. descriptor_cookie, encoded_out);
  2965. return ret;
  2966. }
  2967. /* ========== */
  2968. /* Public API */
  2969. /* ========== */
  2970. /* This is called everytime the service map (v2 or v3) changes that is if an
  2971. * element is added or removed. */
  2972. void
  2973. hs_service_map_has_changed(void)
  2974. {
  2975. /* If we now have services where previously we had not, we need to enable
  2976. * the HS service main loop event. If we changed to having no services, we
  2977. * need to disable the event. */
  2978. rescan_periodic_events(get_options());
  2979. }
  2980. /* Upload an encoded descriptor in encoded_desc of the given version. This
  2981. * descriptor is for the service identity_pk and blinded_pk used to setup the
  2982. * directory connection identifier. It is uploaded to the directory hsdir_rs
  2983. * routerstatus_t object.
  2984. *
  2985. * NOTE: This function does NOT check for PublishHidServDescriptors because it
  2986. * is only used by the control port command HSPOST outside of this subsystem.
  2987. * Inside this code, upload_descriptor_to_hsdir() should be used. */
  2988. void
  2989. hs_service_upload_desc_to_dir(const char *encoded_desc,
  2990. const uint8_t version,
  2991. const ed25519_public_key_t *identity_pk,
  2992. const ed25519_public_key_t *blinded_pk,
  2993. const routerstatus_t *hsdir_rs)
  2994. {
  2995. char version_str[4] = {0};
  2996. directory_request_t *dir_req;
  2997. hs_ident_dir_conn_t ident;
  2998. tor_assert(encoded_desc);
  2999. tor_assert(identity_pk);
  3000. tor_assert(blinded_pk);
  3001. tor_assert(hsdir_rs);
  3002. /* Setup the connection identifier. */
  3003. memset(&ident, 0, sizeof(ident));
  3004. hs_ident_dir_conn_init(identity_pk, blinded_pk, &ident);
  3005. /* This is our resource when uploading which is used to construct the URL
  3006. * with the version number: "/tor/hs/<version>/publish". */
  3007. tor_snprintf(version_str, sizeof(version_str), "%u", version);
  3008. /* Build the directory request for this HSDir. */
  3009. dir_req = directory_request_new(DIR_PURPOSE_UPLOAD_HSDESC);
  3010. directory_request_set_routerstatus(dir_req, hsdir_rs);
  3011. directory_request_set_indirection(dir_req, DIRIND_ANONYMOUS);
  3012. directory_request_set_resource(dir_req, version_str);
  3013. directory_request_set_payload(dir_req, encoded_desc,
  3014. strlen(encoded_desc));
  3015. /* The ident object is copied over the directory connection object once
  3016. * the directory request is initiated. */
  3017. directory_request_upload_set_hs_ident(dir_req, &ident);
  3018. /* Initiate the directory request to the hsdir.*/
  3019. directory_initiate_request(dir_req);
  3020. directory_request_free(dir_req);
  3021. }
  3022. /* Add the ephemeral service using the secret key sk and ports. Both max
  3023. * streams parameter will be set in the newly created service.
  3024. *
  3025. * Ownership of sk and ports is passed to this routine. Regardless of
  3026. * success/failure, callers should not touch these values after calling this
  3027. * routine, and may assume that correct cleanup has been done on failure.
  3028. *
  3029. * Return an appropriate hs_service_add_ephemeral_status_t. */
  3030. hs_service_add_ephemeral_status_t
  3031. hs_service_add_ephemeral(ed25519_secret_key_t *sk, smartlist_t *ports,
  3032. int max_streams_per_rdv_circuit,
  3033. int max_streams_close_circuit, char **address_out)
  3034. {
  3035. hs_service_add_ephemeral_status_t ret;
  3036. hs_service_t *service = NULL;
  3037. tor_assert(sk);
  3038. tor_assert(ports);
  3039. tor_assert(address_out);
  3040. service = hs_service_new(get_options());
  3041. /* Setup the service configuration with specifics. A default service is
  3042. * HS_VERSION_TWO so explicitly set it. */
  3043. service->config.version = HS_VERSION_THREE;
  3044. service->config.max_streams_per_rdv_circuit = max_streams_per_rdv_circuit;
  3045. service->config.max_streams_close_circuit = !!max_streams_close_circuit;
  3046. service->config.is_ephemeral = 1;
  3047. smartlist_free(service->config.ports);
  3048. service->config.ports = ports;
  3049. /* Handle the keys. */
  3050. memcpy(&service->keys.identity_sk, sk, sizeof(service->keys.identity_sk));
  3051. if (ed25519_public_key_generate(&service->keys.identity_pk,
  3052. &service->keys.identity_sk) < 0) {
  3053. log_warn(LD_CONFIG, "Unable to generate ed25519 public key"
  3054. "for v3 service.");
  3055. ret = RSAE_BADPRIVKEY;
  3056. goto err;
  3057. }
  3058. /* Make sure we have at least one port. */
  3059. if (smartlist_len(service->config.ports) == 0) {
  3060. log_warn(LD_CONFIG, "At least one VIRTPORT/TARGET must be specified "
  3061. "for v3 service.");
  3062. ret = RSAE_BADVIRTPORT;
  3063. goto err;
  3064. }
  3065. /* Build the onion address for logging purposes but also the control port
  3066. * uses it for the HS_DESC event. */
  3067. hs_build_address(&service->keys.identity_pk,
  3068. (uint8_t) service->config.version,
  3069. service->onion_address);
  3070. /* The only way the registration can fail is if the service public key
  3071. * already exists. */
  3072. if (BUG(register_service(hs_service_map, service) < 0)) {
  3073. log_warn(LD_CONFIG, "Onion Service private key collides with an "
  3074. "existing v3 service.");
  3075. ret = RSAE_ADDREXISTS;
  3076. goto err;
  3077. }
  3078. log_info(LD_CONFIG, "Added ephemeral v3 onion service: %s",
  3079. safe_str_client(service->onion_address));
  3080. *address_out = tor_strdup(service->onion_address);
  3081. ret = RSAE_OKAY;
  3082. goto end;
  3083. err:
  3084. hs_service_free(service);
  3085. end:
  3086. memwipe(sk, 0, sizeof(ed25519_secret_key_t));
  3087. tor_free(sk);
  3088. return ret;
  3089. }
  3090. /* For the given onion address, delete the ephemeral service. Return 0 on
  3091. * success else -1 on error. */
  3092. int
  3093. hs_service_del_ephemeral(const char *address)
  3094. {
  3095. uint8_t version;
  3096. ed25519_public_key_t pk;
  3097. hs_service_t *service = NULL;
  3098. tor_assert(address);
  3099. if (hs_parse_address(address, &pk, NULL, &version) < 0) {
  3100. log_warn(LD_CONFIG, "Requested malformed v3 onion address for removal.");
  3101. goto err;
  3102. }
  3103. if (version != HS_VERSION_THREE) {
  3104. log_warn(LD_CONFIG, "Requested version of onion address for removal "
  3105. "is not supported.");
  3106. goto err;
  3107. }
  3108. service = find_service(hs_service_map, &pk);
  3109. if (service == NULL) {
  3110. log_warn(LD_CONFIG, "Requested non-existent v3 hidden service for "
  3111. "removal.");
  3112. goto err;
  3113. }
  3114. if (!service->config.is_ephemeral) {
  3115. log_warn(LD_CONFIG, "Requested non-ephemeral v3 hidden service for "
  3116. "removal.");
  3117. goto err;
  3118. }
  3119. /* Close circuits, remove from map and finally free. */
  3120. close_service_circuits(service);
  3121. remove_service(hs_service_map, service);
  3122. hs_service_free(service);
  3123. log_info(LD_CONFIG, "Removed ephemeral v3 hidden service: %s",
  3124. safe_str_client(address));
  3125. return 0;
  3126. err:
  3127. return -1;
  3128. }
  3129. /* Using the ed25519 public key pk, find a service for that key and return the
  3130. * current encoded descriptor as a newly allocated string or NULL if not
  3131. * found. This is used by the control port subsystem. */
  3132. char *
  3133. hs_service_lookup_current_desc(const ed25519_public_key_t *pk)
  3134. {
  3135. const hs_service_t *service;
  3136. tor_assert(pk);
  3137. service = find_service(hs_service_map, pk);
  3138. if (service && service->desc_current) {
  3139. char *encoded_desc = NULL;
  3140. /* No matter what is the result (which should never be a failure), return
  3141. * the encoded variable, if success it will contain the right thing else
  3142. * it will be NULL. */
  3143. service_encode_descriptor(service,
  3144. service->desc_current,
  3145. &service->desc_current->signing_kp,
  3146. &encoded_desc);
  3147. return encoded_desc;
  3148. }
  3149. return NULL;
  3150. }
  3151. /* Return the number of service we have configured and usable. */
  3152. unsigned int
  3153. hs_service_get_num_services(void)
  3154. {
  3155. if (hs_service_map == NULL) {
  3156. return 0;
  3157. }
  3158. return HT_SIZE(hs_service_map);
  3159. }
  3160. /* Called once an introduction circuit is closed. If the circuit doesn't have
  3161. * a v3 identifier, it is ignored. */
  3162. void
  3163. hs_service_intro_circ_has_closed(origin_circuit_t *circ)
  3164. {
  3165. hs_service_t *service = NULL;
  3166. hs_service_intro_point_t *ip = NULL;
  3167. hs_service_descriptor_t *desc = NULL;
  3168. tor_assert(circ);
  3169. if (circ->hs_ident == NULL) {
  3170. /* This is not a v3 circuit, ignore. */
  3171. goto end;
  3172. }
  3173. get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
  3174. if (service == NULL) {
  3175. /* This is possible if the circuits are closed and the service is
  3176. * immediately deleted. */
  3177. log_info(LD_REND, "Unable to find any hidden service associated "
  3178. "identity key %s on intro circuit %u.",
  3179. ed25519_fmt(&circ->hs_ident->identity_pk),
  3180. TO_CIRCUIT(circ)->n_circ_id);
  3181. goto end;
  3182. }
  3183. if (ip == NULL) {
  3184. /* The introduction point object has already been removed probably by our
  3185. * cleanup process so ignore. */
  3186. goto end;
  3187. }
  3188. /* Can't have an intro point object without a descriptor. */
  3189. tor_assert(desc);
  3190. /* Circuit disappeared so make sure the intro point is updated. By
  3191. * keeping the object in the descriptor, we'll be able to retry. */
  3192. ip->circuit_established = 0;
  3193. end:
  3194. return;
  3195. }
  3196. /* Given conn, a rendezvous edge connection acting as an exit stream, look up
  3197. * the hidden service for the circuit circ, and look up the port and address
  3198. * based on the connection port. Assign the actual connection address.
  3199. *
  3200. * Return 0 on success. Return -1 on failure and the caller should NOT close
  3201. * the circuit. Return -2 on failure and the caller MUST close the circuit for
  3202. * security reasons. */
  3203. int
  3204. hs_service_set_conn_addr_port(const origin_circuit_t *circ,
  3205. edge_connection_t *conn)
  3206. {
  3207. hs_service_t *service = NULL;
  3208. tor_assert(circ);
  3209. tor_assert(conn);
  3210. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  3211. tor_assert(circ->hs_ident);
  3212. get_objects_from_ident(circ->hs_ident, &service, NULL, NULL);
  3213. if (service == NULL) {
  3214. log_warn(LD_REND, "Unable to find any hidden service associated "
  3215. "identity key %s on rendezvous circuit %u.",
  3216. ed25519_fmt(&circ->hs_ident->identity_pk),
  3217. TO_CIRCUIT(circ)->n_circ_id);
  3218. /* We want the caller to close the circuit because it's not a valid
  3219. * service so no danger. Attempting to bruteforce the entire key space by
  3220. * opening circuits to learn which service is being hosted here is
  3221. * impractical. */
  3222. goto err_close;
  3223. }
  3224. /* Enforce the streams-per-circuit limit, and refuse to provide a mapping if
  3225. * this circuit will exceed the limit. */
  3226. if (service->config.max_streams_per_rdv_circuit > 0 &&
  3227. (circ->hs_ident->num_rdv_streams >=
  3228. service->config.max_streams_per_rdv_circuit)) {
  3229. #define MAX_STREAM_WARN_INTERVAL 600
  3230. static struct ratelim_t stream_ratelim =
  3231. RATELIM_INIT(MAX_STREAM_WARN_INTERVAL);
  3232. log_fn_ratelim(&stream_ratelim, LOG_WARN, LD_REND,
  3233. "Maximum streams per circuit limit reached on "
  3234. "rendezvous circuit %u for service %s. Circuit has "
  3235. "%" PRIu64 " out of %" PRIu64 " streams. %s.",
  3236. TO_CIRCUIT(circ)->n_circ_id,
  3237. service->onion_address,
  3238. circ->hs_ident->num_rdv_streams,
  3239. service->config.max_streams_per_rdv_circuit,
  3240. service->config.max_streams_close_circuit ?
  3241. "Closing circuit" : "Ignoring open stream request");
  3242. if (service->config.max_streams_close_circuit) {
  3243. /* Service explicitly configured to close immediately. */
  3244. goto err_close;
  3245. }
  3246. /* Exceeding the limit makes tor silently ignore the stream creation
  3247. * request and keep the circuit open. */
  3248. goto err_no_close;
  3249. }
  3250. /* Find a virtual port of that service mathcing the one in the connection if
  3251. * successful, set the address in the connection. */
  3252. if (hs_set_conn_addr_port(service->config.ports, conn) < 0) {
  3253. log_info(LD_REND, "No virtual port mapping exists for port %d for "
  3254. "hidden service %s.",
  3255. TO_CONN(conn)->port, service->onion_address);
  3256. if (service->config.allow_unknown_ports) {
  3257. /* Service explicitly allow connection to unknown ports so close right
  3258. * away because we do not care about port mapping. */
  3259. goto err_close;
  3260. }
  3261. /* If the service didn't explicitly allow it, we do NOT close the circuit
  3262. * here to raise the bar in terms of performance for port mapping. */
  3263. goto err_no_close;
  3264. }
  3265. /* Success. */
  3266. return 0;
  3267. err_close:
  3268. /* Indicate the caller that the circuit should be closed. */
  3269. return -2;
  3270. err_no_close:
  3271. /* Indicate the caller to NOT close the circuit. */
  3272. return -1;
  3273. }
  3274. /** Does the service with identity pubkey <b>pk</b> export the circuit IDs of
  3275. * its clients? */
  3276. hs_circuit_id_protocol_t
  3277. hs_service_exports_circuit_id(const ed25519_public_key_t *pk)
  3278. {
  3279. hs_service_t *service = find_service(hs_service_map, pk);
  3280. if (!service) {
  3281. return HS_CIRCUIT_ID_PROTOCOL_NONE;
  3282. }
  3283. return service->config.circuit_id_protocol;
  3284. }
  3285. /* Add to file_list every filename used by a configured hidden service, and to
  3286. * dir_list every directory path used by a configured hidden service. This is
  3287. * used by the sandbox subsystem to whitelist those. */
  3288. void
  3289. hs_service_lists_fnames_for_sandbox(smartlist_t *file_list,
  3290. smartlist_t *dir_list)
  3291. {
  3292. tor_assert(file_list);
  3293. tor_assert(dir_list);
  3294. /* Add files and dirs for legacy services. */
  3295. rend_services_add_filenames_to_lists(file_list, dir_list);
  3296. /* Add files and dirs for v3+. */
  3297. FOR_EACH_SERVICE_BEGIN(service) {
  3298. /* Skip ephemeral service, they don't touch the disk. */
  3299. if (service->config.is_ephemeral) {
  3300. continue;
  3301. }
  3302. service_add_fnames_to_list(service, file_list);
  3303. smartlist_add_strdup(dir_list, service->config.directory_path);
  3304. smartlist_add_strdup(dir_list, dname_client_pubkeys);
  3305. } FOR_EACH_DESCRIPTOR_END;
  3306. }
  3307. /* Called when our internal view of the directory has changed. We might have
  3308. * received a new batch of descriptors which might affect the shape of the
  3309. * HSDir hash ring. Signal that we should reexamine the hash ring and
  3310. * re-upload our HS descriptors if needed. */
  3311. void
  3312. hs_service_dir_info_changed(void)
  3313. {
  3314. if (hs_service_get_num_services() > 0) {
  3315. /* New directory information usually goes every consensus so rate limit
  3316. * every 30 minutes to not be too conservative. */
  3317. static struct ratelim_t dir_info_changed_ratelim = RATELIM_INIT(30 * 60);
  3318. log_fn_ratelim(&dir_info_changed_ratelim, LOG_INFO, LD_REND,
  3319. "New dirinfo arrived: consider reuploading descriptor");
  3320. consider_republishing_hs_descriptors = 1;
  3321. }
  3322. }
  3323. /* Called when we get an INTRODUCE2 cell on the circ. Respond to the cell and
  3324. * launch a circuit to the rendezvous point. */
  3325. int
  3326. hs_service_receive_introduce2(origin_circuit_t *circ, const uint8_t *payload,
  3327. size_t payload_len)
  3328. {
  3329. int ret = -1;
  3330. tor_assert(circ);
  3331. tor_assert(payload);
  3332. /* Do some initial validation and logging before we parse the cell */
  3333. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_INTRO) {
  3334. log_warn(LD_PROTOCOL, "Received an INTRODUCE2 cell on a "
  3335. "non introduction circuit of purpose %d",
  3336. TO_CIRCUIT(circ)->purpose);
  3337. goto done;
  3338. }
  3339. if (circ->hs_ident) {
  3340. ret = service_handle_introduce2(circ, payload, payload_len);
  3341. hs_stats_note_introduce2_cell(1);
  3342. } else {
  3343. ret = rend_service_receive_introduction(circ, payload, payload_len);
  3344. hs_stats_note_introduce2_cell(0);
  3345. }
  3346. done:
  3347. return ret;
  3348. }
  3349. /* Called when we get an INTRO_ESTABLISHED cell. Mark the circuit as an
  3350. * established introduction point. Return 0 on success else a negative value
  3351. * and the circuit is closed. */
  3352. int
  3353. hs_service_receive_intro_established(origin_circuit_t *circ,
  3354. const uint8_t *payload,
  3355. size_t payload_len)
  3356. {
  3357. int ret = -1;
  3358. tor_assert(circ);
  3359. tor_assert(payload);
  3360. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  3361. log_warn(LD_PROTOCOL, "Received an INTRO_ESTABLISHED cell on a "
  3362. "non introduction circuit of purpose %d",
  3363. TO_CIRCUIT(circ)->purpose);
  3364. goto err;
  3365. }
  3366. /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
  3367. * identifier hs_ident. Can't be both. */
  3368. if (circ->hs_ident) {
  3369. ret = service_handle_intro_established(circ, payload, payload_len);
  3370. } else {
  3371. ret = rend_service_intro_established(circ, payload, payload_len);
  3372. }
  3373. if (ret < 0) {
  3374. goto err;
  3375. }
  3376. return 0;
  3377. err:
  3378. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  3379. return -1;
  3380. }
  3381. /* Called when any kind of hidden service circuit is done building thus
  3382. * opened. This is the entry point from the circuit subsystem. */
  3383. void
  3384. hs_service_circuit_has_opened(origin_circuit_t *circ)
  3385. {
  3386. tor_assert(circ);
  3387. /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
  3388. * identifier hs_ident. Can't be both. */
  3389. switch (TO_CIRCUIT(circ)->purpose) {
  3390. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  3391. if (circ->hs_ident) {
  3392. service_intro_circ_has_opened(circ);
  3393. } else {
  3394. rend_service_intro_has_opened(circ);
  3395. }
  3396. break;
  3397. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  3398. if (circ->hs_ident) {
  3399. service_rendezvous_circ_has_opened(circ);
  3400. } else {
  3401. rend_service_rendezvous_has_opened(circ);
  3402. }
  3403. break;
  3404. default:
  3405. tor_assert(0);
  3406. }
  3407. }
  3408. /* Return the service version by looking at the key in the service directory.
  3409. * If the key is not found or unrecognized, -1 is returned. Else, the service
  3410. * version is returned. */
  3411. int
  3412. hs_service_get_version_from_key(const hs_service_t *service)
  3413. {
  3414. int version = -1; /* Unknown version. */
  3415. const char *directory_path;
  3416. tor_assert(service);
  3417. /* We'll try to load the key for version 3. If not found, we'll try version
  3418. * 2 and if not found, we'll send back an unknown version (-1). */
  3419. directory_path = service->config.directory_path;
  3420. /* Version 3 check. */
  3421. if (service_key_on_disk(directory_path)) {
  3422. version = HS_VERSION_THREE;
  3423. goto end;
  3424. }
  3425. /* Version 2 check. */
  3426. if (rend_service_key_on_disk(directory_path)) {
  3427. version = HS_VERSION_TWO;
  3428. goto end;
  3429. }
  3430. end:
  3431. return version;
  3432. }
  3433. /* Load and/or generate keys for all onion services including the client
  3434. * authorization if any. Return 0 on success, -1 on failure. */
  3435. int
  3436. hs_service_load_all_keys(void)
  3437. {
  3438. /* Load v2 service keys if we have v2. */
  3439. if (rend_num_services() != 0) {
  3440. if (rend_service_load_all_keys(NULL) < 0) {
  3441. goto err;
  3442. }
  3443. }
  3444. /* Load or/and generate them for v3+. */
  3445. SMARTLIST_FOREACH_BEGIN(hs_service_staging_list, hs_service_t *, service) {
  3446. /* Ignore ephemeral service, they already have their keys set. */
  3447. if (service->config.is_ephemeral) {
  3448. continue;
  3449. }
  3450. log_info(LD_REND, "Loading v3 onion service keys from %s",
  3451. service_escaped_dir(service));
  3452. if (load_service_keys(service) < 0) {
  3453. goto err;
  3454. }
  3455. } SMARTLIST_FOREACH_END(service);
  3456. /* Final step, the staging list contains service in a quiescent state that
  3457. * is ready to be used. Register them to the global map. Once this is over,
  3458. * the staging list will be cleaned up. */
  3459. register_all_services();
  3460. /* All keys have been loaded successfully. */
  3461. return 0;
  3462. err:
  3463. return -1;
  3464. }
  3465. /* Put all service object in the given service list. After this, the caller
  3466. * looses ownership of every elements in the list and responsible to free the
  3467. * list pointer. */
  3468. void
  3469. hs_service_stage_services(const smartlist_t *service_list)
  3470. {
  3471. tor_assert(service_list);
  3472. /* This list is freed at registration time but this function can be called
  3473. * multiple time. */
  3474. if (hs_service_staging_list == NULL) {
  3475. hs_service_staging_list = smartlist_new();
  3476. }
  3477. /* Add all service object to our staging list. Caller is responsible for
  3478. * freeing the service_list. */
  3479. smartlist_add_all(hs_service_staging_list, service_list);
  3480. }
  3481. /* Allocate and initilize a service object. The service configuration will
  3482. * contain the default values. Return the newly allocated object pointer. This
  3483. * function can't fail. */
  3484. hs_service_t *
  3485. hs_service_new(const or_options_t *options)
  3486. {
  3487. hs_service_t *service = tor_malloc_zero(sizeof(hs_service_t));
  3488. /* Set default configuration value. */
  3489. set_service_default_config(&service->config, options);
  3490. /* Set the default service version. */
  3491. service->config.version = HS_SERVICE_DEFAULT_VERSION;
  3492. /* Allocate the CLIENT_PK replay cache in service state. */
  3493. service->state.replay_cache_rend_cookie =
  3494. replaycache_new(REND_REPLAY_TIME_INTERVAL, REND_REPLAY_TIME_INTERVAL);
  3495. return service;
  3496. }
  3497. /* Free the given <b>service</b> object and all its content. This function
  3498. * also takes care of wiping service keys from memory. It is safe to pass a
  3499. * NULL pointer. */
  3500. void
  3501. hs_service_free_(hs_service_t *service)
  3502. {
  3503. if (service == NULL) {
  3504. return;
  3505. }
  3506. /* Free descriptors. Go over both descriptor with this loop. */
  3507. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  3508. service_descriptor_free(desc);
  3509. } FOR_EACH_DESCRIPTOR_END;
  3510. /* Free service configuration. */
  3511. service_clear_config(&service->config);
  3512. /* Free replay cache from state. */
  3513. if (service->state.replay_cache_rend_cookie) {
  3514. replaycache_free(service->state.replay_cache_rend_cookie);
  3515. }
  3516. /* Wipe service keys. */
  3517. memwipe(&service->keys.identity_sk, 0, sizeof(service->keys.identity_sk));
  3518. tor_free(service);
  3519. }
  3520. /* Periodic callback. Entry point from the main loop to the HS service
  3521. * subsystem. This is call every second. This is skipped if tor can't build a
  3522. * circuit or the network is disabled. */
  3523. void
  3524. hs_service_run_scheduled_events(time_t now)
  3525. {
  3526. /* First thing we'll do here is to make sure our services are in a
  3527. * quiescent state for the scheduled events. */
  3528. run_housekeeping_event(now);
  3529. /* Order matters here. We first make sure the descriptor object for each
  3530. * service contains the latest data. Once done, we check if we need to open
  3531. * new introduction circuit. Finally, we try to upload the descriptor for
  3532. * each service. */
  3533. /* Make sure descriptors are up to date. */
  3534. run_build_descriptor_event(now);
  3535. /* Make sure services have enough circuits. */
  3536. run_build_circuit_event(now);
  3537. /* Upload the descriptors if needed/possible. */
  3538. run_upload_descriptor_event(now);
  3539. }
  3540. /* Initialize the service HS subsystem. */
  3541. void
  3542. hs_service_init(void)
  3543. {
  3544. /* Should never be called twice. */
  3545. tor_assert(!hs_service_map);
  3546. tor_assert(!hs_service_staging_list);
  3547. /* v2 specific. */
  3548. rend_service_init();
  3549. hs_service_map = tor_malloc_zero(sizeof(struct hs_service_ht));
  3550. HT_INIT(hs_service_ht, hs_service_map);
  3551. hs_service_staging_list = smartlist_new();
  3552. }
  3553. /* Release all global storage of the hidden service subsystem. */
  3554. void
  3555. hs_service_free_all(void)
  3556. {
  3557. rend_service_free_all();
  3558. service_free_all();
  3559. }
  3560. #ifdef TOR_UNIT_TESTS
  3561. /* Return the global service map size. Only used by unit test. */
  3562. STATIC unsigned int
  3563. get_hs_service_map_size(void)
  3564. {
  3565. return HT_SIZE(hs_service_map);
  3566. }
  3567. /* Return the staging list size. Only used by unit test. */
  3568. STATIC int
  3569. get_hs_service_staging_list_size(void)
  3570. {
  3571. return smartlist_len(hs_service_staging_list);
  3572. }
  3573. STATIC hs_service_ht *
  3574. get_hs_service_map(void)
  3575. {
  3576. return hs_service_map;
  3577. }
  3578. STATIC hs_service_t *
  3579. get_first_service(void)
  3580. {
  3581. hs_service_t **obj = HT_START(hs_service_ht, hs_service_map);
  3582. if (obj == NULL) {
  3583. return NULL;
  3584. }
  3585. return *obj;
  3586. }
  3587. #endif /* defined(TOR_UNIT_TESTS) */