hs_service.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823
  1. /* Copyright (c) 2016-2017, 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 "or.h"
  9. #include "circpathbias.h"
  10. #include "circuitbuild.h"
  11. #include "circuitlist.h"
  12. #include "circuituse.h"
  13. #include "config.h"
  14. #include "directory.h"
  15. #include "main.h"
  16. #include "networkstatus.h"
  17. #include "nodelist.h"
  18. #include "relay.h"
  19. #include "rendservice.h"
  20. #include "router.h"
  21. #include "routerkeys.h"
  22. #include "routerlist.h"
  23. #include "hs_circuit.h"
  24. #include "hs_common.h"
  25. #include "hs_config.h"
  26. #include "hs_circuit.h"
  27. #include "hs_descriptor.h"
  28. #include "hs_ident.h"
  29. #include "hs_intropoint.h"
  30. #include "hs_service.h"
  31. /* Trunnel */
  32. #include "ed25519_cert.h"
  33. #include "hs/cell_common.h"
  34. #include "hs/cell_establish_intro.h"
  35. /* Helper macro. Iterate over every service in the global map. The var is the
  36. * name of the service pointer. */
  37. #define FOR_EACH_SERVICE_BEGIN(var) \
  38. STMT_BEGIN \
  39. hs_service_t **var##_iter, *var; \
  40. HT_FOREACH(var##_iter, hs_service_ht, hs_service_map) { \
  41. var = *var##_iter;
  42. #define FOR_EACH_SERVICE_END } STMT_END ;
  43. /* Helper macro. Iterate over both current and previous descriptor of a
  44. * service. The var is the name of the descriptor pointer. This macro skips
  45. * any descriptor object of the service that is NULL. */
  46. #define FOR_EACH_DESCRIPTOR_BEGIN(service, var) \
  47. STMT_BEGIN \
  48. hs_service_descriptor_t *var; \
  49. for (int var ## _loop_idx = 0; var ## _loop_idx < 2; \
  50. ++var ## _loop_idx) { \
  51. (var ## _loop_idx == 0) ? (var = service->desc_current) : \
  52. (var = service->desc_next); \
  53. if (var == NULL) continue;
  54. #define FOR_EACH_DESCRIPTOR_END } STMT_END ;
  55. /* Onion service directory file names. */
  56. static const char *fname_keyfile_prefix = "hs_ed25519";
  57. static const char *fname_hostname = "hostname";
  58. static const char *address_tld = "onion";
  59. /* Staging list of service object. When configuring service, we add them to
  60. * this list considered a staging area and they will get added to our global
  61. * map once the keys have been loaded. These two steps are seperated because
  62. * loading keys requires that we are an actual running tor process. */
  63. static smartlist_t *hs_service_staging_list;
  64. /* Helper: Function to compare two objects in the service map. Return 1 if the
  65. * two service have the same master public identity key. */
  66. static inline int
  67. hs_service_ht_eq(const hs_service_t *first, const hs_service_t *second)
  68. {
  69. tor_assert(first);
  70. tor_assert(second);
  71. /* Simple key compare. */
  72. return ed25519_pubkey_eq(&first->keys.identity_pk,
  73. &second->keys.identity_pk);
  74. }
  75. /* Helper: Function for the service hash table code below. The key used is the
  76. * master public identity key which is ultimately the onion address. */
  77. static inline unsigned int
  78. hs_service_ht_hash(const hs_service_t *service)
  79. {
  80. tor_assert(service);
  81. return (unsigned int) siphash24g(service->keys.identity_pk.pubkey,
  82. sizeof(service->keys.identity_pk.pubkey));
  83. }
  84. /* This is _the_ global hash map of hidden services which indexed the service
  85. * contained in it by master public identity key which is roughly the onion
  86. * address of the service. */
  87. static struct hs_service_ht *hs_service_map;
  88. /* Register the service hash table. */
  89. HT_PROTOTYPE(hs_service_ht, /* Name of hashtable. */
  90. hs_service_t, /* Object contained in the map. */
  91. hs_service_node, /* The name of the HT_ENTRY member. */
  92. hs_service_ht_hash, /* Hashing function. */
  93. hs_service_ht_eq) /* Compare function for objects. */
  94. HT_GENERATE2(hs_service_ht, hs_service_t, hs_service_node,
  95. hs_service_ht_hash, hs_service_ht_eq,
  96. 0.6, tor_reallocarray, tor_free_)
  97. /* Query the given service map with a public key and return a service object
  98. * if found else NULL. It is also possible to set a directory path in the
  99. * search query. If pk is NULL, then it will be set to zero indicating the
  100. * hash table to compare the directory path instead. */
  101. STATIC hs_service_t *
  102. find_service(hs_service_ht *map, const ed25519_public_key_t *pk)
  103. {
  104. hs_service_t dummy_service;
  105. tor_assert(map);
  106. tor_assert(pk);
  107. memset(&dummy_service, 0, sizeof(dummy_service));
  108. ed25519_pubkey_copy(&dummy_service.keys.identity_pk, pk);
  109. return HT_FIND(hs_service_ht, map, &dummy_service);
  110. }
  111. /* Register the given service in the given map. If the service already exists
  112. * in the map, -1 is returned. On success, 0 is returned and the service
  113. * ownership has been transfered to the global map. */
  114. STATIC int
  115. register_service(hs_service_ht *map, hs_service_t *service)
  116. {
  117. tor_assert(map);
  118. tor_assert(service);
  119. tor_assert(!ed25519_public_key_is_zero(&service->keys.identity_pk));
  120. if (find_service(map, &service->keys.identity_pk)) {
  121. /* Existing service with the same key. Do not register it. */
  122. return -1;
  123. }
  124. /* Taking ownership of the object at this point. */
  125. HT_INSERT(hs_service_ht, map, service);
  126. return 0;
  127. }
  128. /* Remove a given service from the given map. If service is NULL or the
  129. * service key is unset, return gracefully. */
  130. STATIC void
  131. remove_service(hs_service_ht *map, hs_service_t *service)
  132. {
  133. hs_service_t *elm;
  134. tor_assert(map);
  135. /* Ignore if no service or key is zero. */
  136. if (BUG(service == NULL) ||
  137. BUG(ed25519_public_key_is_zero(&service->keys.identity_pk))) {
  138. return;
  139. }
  140. elm = HT_REMOVE(hs_service_ht, map, service);
  141. if (elm) {
  142. tor_assert(elm == service);
  143. } else {
  144. log_warn(LD_BUG, "Could not find service in the global map "
  145. "while removing service %s",
  146. escaped(service->config.directory_path));
  147. }
  148. }
  149. /* Set the default values for a service configuration object <b>c</b>. */
  150. static void
  151. set_service_default_config(hs_service_config_t *c,
  152. const or_options_t *options)
  153. {
  154. (void) options;
  155. tor_assert(c);
  156. c->ports = smartlist_new();
  157. c->directory_path = NULL;
  158. c->max_streams_per_rdv_circuit = 0;
  159. c->max_streams_close_circuit = 0;
  160. c->num_intro_points = NUM_INTRO_POINTS_DEFAULT;
  161. c->allow_unknown_ports = 0;
  162. c->is_single_onion = 0;
  163. c->dir_group_readable = 0;
  164. c->is_ephemeral = 0;
  165. }
  166. /* From a service configuration object config, clear everything from it
  167. * meaning free allocated pointers and reset the values. */
  168. static void
  169. service_clear_config(hs_service_config_t *config)
  170. {
  171. if (config == NULL) {
  172. return;
  173. }
  174. tor_free(config->directory_path);
  175. if (config->ports) {
  176. SMARTLIST_FOREACH(config->ports, rend_service_port_config_t *, p,
  177. rend_service_port_config_free(p););
  178. smartlist_free(config->ports);
  179. }
  180. memset(config, 0, sizeof(*config));
  181. }
  182. /* Return the number of minimum INTRODUCE2 cell defined by a consensus
  183. * parameter or the default value. */
  184. static int32_t
  185. get_intro_point_min_introduce2(void)
  186. {
  187. /* The [0, 2147483647] range is quite large to accomodate anything we decide
  188. * in the future. */
  189. return networkstatus_get_param(NULL, "hs_intro_min_introduce2",
  190. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS,
  191. 0, INT32_MAX);
  192. }
  193. /* Return the number of maximum INTRODUCE2 cell defined by a consensus
  194. * parameter or the default value. */
  195. static int32_t
  196. get_intro_point_max_introduce2(void)
  197. {
  198. /* The [0, 2147483647] range is quite large to accomodate anything we decide
  199. * in the future. */
  200. return networkstatus_get_param(NULL, "hs_intro_max_introduce2",
  201. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS,
  202. 0, INT32_MAX);
  203. }
  204. /* Return the minimum lifetime of an introduction point defined by a consensus
  205. * parameter or the default value. */
  206. static int32_t
  207. get_intro_point_min_lifetime(void)
  208. {
  209. #define MIN_INTRO_POINT_LIFETIME_TESTING 10
  210. if (get_options()->TestingTorNetwork) {
  211. return MIN_INTRO_POINT_LIFETIME_TESTING;
  212. }
  213. /* The [0, 2147483647] range is quite large to accomodate anything we decide
  214. * in the future. */
  215. return networkstatus_get_param(NULL, "hs_intro_min_lifetime",
  216. INTRO_POINT_LIFETIME_MIN_SECONDS,
  217. 0, INT32_MAX);
  218. }
  219. /* Return the maximum lifetime of an introduction point defined by a consensus
  220. * parameter or the default value. */
  221. static int32_t
  222. get_intro_point_max_lifetime(void)
  223. {
  224. #define MAX_INTRO_POINT_LIFETIME_TESTING 30
  225. if (get_options()->TestingTorNetwork) {
  226. return MAX_INTRO_POINT_LIFETIME_TESTING;
  227. }
  228. /* The [0, 2147483647] range is quite large to accomodate anything we decide
  229. * in the future. */
  230. return networkstatus_get_param(NULL, "hs_intro_max_lifetime",
  231. INTRO_POINT_LIFETIME_MAX_SECONDS,
  232. 0, INT32_MAX);
  233. }
  234. /* Return the number of extra introduction point defined by a consensus
  235. * parameter or the default value. */
  236. static int32_t
  237. get_intro_point_num_extra(void)
  238. {
  239. /* The [0, 128] range bounds the number of extra introduction point allowed.
  240. * Above 128 intro points, it's getting a bit crazy. */
  241. return networkstatus_get_param(NULL, "hs_intro_num_extra",
  242. NUM_INTRO_POINTS_EXTRA, 0, 128);
  243. }
  244. /* Helper: Function that needs to return 1 for the HT for each loop which
  245. * frees every service in an hash map. */
  246. static int
  247. ht_free_service_(struct hs_service_t *service, void *data)
  248. {
  249. (void) data;
  250. hs_service_free(service);
  251. /* This function MUST return 1 so the given object is then removed from the
  252. * service map leading to this free of the object being safe. */
  253. return 1;
  254. }
  255. /* Free every service that can be found in the global map. Once done, clear
  256. * and free the global map. */
  257. static void
  258. service_free_all(void)
  259. {
  260. if (hs_service_map) {
  261. /* The free helper function returns 1 so this is safe. */
  262. hs_service_ht_HT_FOREACH_FN(hs_service_map, ht_free_service_, NULL);
  263. HT_CLEAR(hs_service_ht, hs_service_map);
  264. tor_free(hs_service_map);
  265. hs_service_map = NULL;
  266. }
  267. if (hs_service_staging_list) {
  268. /* Cleanup staging list. */
  269. SMARTLIST_FOREACH(hs_service_staging_list, hs_service_t *, s,
  270. hs_service_free(s));
  271. smartlist_free(hs_service_staging_list);
  272. hs_service_staging_list = NULL;
  273. }
  274. }
  275. /* Free a given service intro point object. */
  276. STATIC void
  277. service_intro_point_free(hs_service_intro_point_t *ip)
  278. {
  279. if (!ip) {
  280. return;
  281. }
  282. memwipe(&ip->auth_key_kp, 0, sizeof(ip->auth_key_kp));
  283. memwipe(&ip->enc_key_kp, 0, sizeof(ip->enc_key_kp));
  284. crypto_pk_free(ip->legacy_key);
  285. replaycache_free(ip->replay_cache);
  286. hs_intro_free_content(&ip->base);
  287. tor_free(ip);
  288. }
  289. /* Helper: free an hs_service_intro_point_t object. This function is used by
  290. * digest256map_free() which requires a void * pointer. */
  291. static void
  292. service_intro_point_free_(void *obj)
  293. {
  294. service_intro_point_free(obj);
  295. }
  296. /* Return a newly allocated service intro point and fully initialized from the
  297. * given extend_info_t ei if non NULL. If is_legacy is true, we also generate
  298. * the legacy key. On error, NULL is returned. */
  299. STATIC hs_service_intro_point_t *
  300. service_intro_point_new(const extend_info_t *ei, unsigned int is_legacy)
  301. {
  302. hs_desc_link_specifier_t *ls;
  303. hs_service_intro_point_t *ip;
  304. ip = tor_malloc_zero(sizeof(*ip));
  305. /* We'll create the key material. No need for extra strong, those are short
  306. * term keys. */
  307. ed25519_keypair_generate(&ip->auth_key_kp, 0);
  308. ip->introduce2_max =
  309. crypto_rand_int_range(get_intro_point_min_introduce2(),
  310. get_intro_point_max_introduce2());
  311. ip->time_to_expire = time(NULL) +
  312. crypto_rand_int_range(get_intro_point_min_lifetime(),
  313. get_intro_point_max_lifetime());
  314. ip->replay_cache = replaycache_new(0, 0);
  315. /* Initialize the base object. We don't need the certificate object. */
  316. ip->base.link_specifiers = smartlist_new();
  317. /* Generate the encryption key for this intro point. */
  318. curve25519_keypair_generate(&ip->enc_key_kp, 0);
  319. /* Figure out if this chosen node supports v3 or is legacy only. */
  320. if (is_legacy) {
  321. ip->base.is_only_legacy = 1;
  322. /* Legacy mode that is doesn't support v3+ with ed25519 auth key. */
  323. ip->legacy_key = crypto_pk_new();
  324. if (crypto_pk_generate_key(ip->legacy_key) < 0) {
  325. goto err;
  326. }
  327. }
  328. if (ei == NULL) {
  329. goto done;
  330. }
  331. /* We'll try to add all link specifier. Legacy, IPv4 and ed25519 are
  332. * mandatory. */
  333. ls = hs_desc_link_specifier_new(ei, LS_IPV4);
  334. /* It is impossible to have an extend info object without a v4. */
  335. tor_assert(ls);
  336. smartlist_add(ip->base.link_specifiers, ls);
  337. ls = hs_desc_link_specifier_new(ei, LS_LEGACY_ID);
  338. /* It is impossible to have an extend info object without an identity
  339. * digest. */
  340. tor_assert(ls);
  341. smartlist_add(ip->base.link_specifiers, ls);
  342. ls = hs_desc_link_specifier_new(ei, LS_ED25519_ID);
  343. /* It is impossible to have an extend info object without an ed25519
  344. * identity key. */
  345. tor_assert(ls);
  346. smartlist_add(ip->base.link_specifiers, ls);
  347. /* IPv6 is optional. */
  348. ls = hs_desc_link_specifier_new(ei, LS_IPV6);
  349. if (ls) {
  350. smartlist_add(ip->base.link_specifiers, ls);
  351. }
  352. /* Finally, copy onion key from the extend_info_t object. */
  353. memcpy(&ip->onion_key, &ei->curve25519_onion_key, sizeof(ip->onion_key));
  354. done:
  355. return ip;
  356. err:
  357. service_intro_point_free(ip);
  358. return NULL;
  359. }
  360. /* Add the given intro point object to the given intro point map. The intro
  361. * point MUST have its RSA encryption key set if this is a legacy type or the
  362. * authentication key set otherwise. */
  363. STATIC void
  364. service_intro_point_add(digest256map_t *map, hs_service_intro_point_t *ip)
  365. {
  366. tor_assert(map);
  367. tor_assert(ip);
  368. digest256map_set(map, ip->auth_key_kp.pubkey.pubkey, ip);
  369. }
  370. /* For a given service, remove the intro point from that service which will
  371. * look in both descriptors. */
  372. STATIC void
  373. service_intro_point_remove(const hs_service_t *service,
  374. const hs_service_intro_point_t *ip)
  375. {
  376. tor_assert(service);
  377. tor_assert(ip);
  378. /* Trying all descriptors. */
  379. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  380. /* We'll try to remove the descriptor on both descriptors which is not
  381. * very expensive to do instead of doing loopup + remove. */
  382. digest256map_remove(desc->intro_points.map,
  383. ip->auth_key_kp.pubkey.pubkey);
  384. } FOR_EACH_DESCRIPTOR_END;
  385. }
  386. /* For a given service and authentication key, return the intro point or NULL
  387. * if not found. This will check both descriptors in the service. */
  388. STATIC hs_service_intro_point_t *
  389. service_intro_point_find(const hs_service_t *service,
  390. const ed25519_public_key_t *auth_key)
  391. {
  392. hs_service_intro_point_t *ip = NULL;
  393. tor_assert(service);
  394. tor_assert(auth_key);
  395. /* Trying all descriptors. */
  396. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  397. if ((ip = digest256map_get(desc->intro_points.map,
  398. auth_key->pubkey)) != NULL) {
  399. break;
  400. }
  401. } FOR_EACH_DESCRIPTOR_END;
  402. return ip;
  403. }
  404. /* For a given service and intro point, return the descriptor for which the
  405. * intro point is assigned to. NULL is returned if not found. */
  406. STATIC hs_service_descriptor_t *
  407. service_desc_find_by_intro(const hs_service_t *service,
  408. const hs_service_intro_point_t *ip)
  409. {
  410. hs_service_descriptor_t *descp = NULL;
  411. tor_assert(service);
  412. tor_assert(ip);
  413. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  414. if (digest256map_get(desc->intro_points.map,
  415. ip->auth_key_kp.pubkey.pubkey)) {
  416. descp = desc;
  417. break;
  418. }
  419. } FOR_EACH_DESCRIPTOR_END;
  420. return descp;
  421. }
  422. /* From a circuit identifier, get all the possible objects associated with the
  423. * ident. If not NULL, service, ip or desc are set if the object can be found.
  424. * They are untouched if they can't be found.
  425. *
  426. * This is an helper function because we do those lookups often so it's more
  427. * convenient to simply call this functions to get all the things at once. */
  428. STATIC void
  429. get_objects_from_ident(const hs_ident_circuit_t *ident,
  430. hs_service_t **service, hs_service_intro_point_t **ip,
  431. hs_service_descriptor_t **desc)
  432. {
  433. hs_service_t *s;
  434. tor_assert(ident);
  435. /* Get service object from the circuit identifier. */
  436. s = find_service(hs_service_map, &ident->identity_pk);
  437. if (s && service) {
  438. *service = s;
  439. }
  440. /* From the service object, get the intro point object of that circuit. The
  441. * following will query both descriptors intro points list. */
  442. if (s && ip) {
  443. *ip = service_intro_point_find(s, &ident->intro_auth_pk);
  444. }
  445. /* Get the descriptor for this introduction point and service. */
  446. if (s && ip && *ip && desc) {
  447. *desc = service_desc_find_by_intro(s, *ip);
  448. }
  449. }
  450. /* From a given intro point, return the first link specifier of type
  451. * encountered in the link specifier list. Return NULL if it can't be found.
  452. *
  453. * The caller does NOT have ownership of the object, the intro point does. */
  454. static hs_desc_link_specifier_t *
  455. get_link_spec_by_type(const hs_service_intro_point_t *ip, uint8_t type)
  456. {
  457. hs_desc_link_specifier_t *lnk_spec = NULL;
  458. tor_assert(ip);
  459. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  460. hs_desc_link_specifier_t *, ls) {
  461. if (ls->type == type) {
  462. lnk_spec = ls;
  463. goto end;
  464. }
  465. } SMARTLIST_FOREACH_END(ls);
  466. end:
  467. return lnk_spec;
  468. }
  469. /* Given a service intro point, return the node_t associated to it. This can
  470. * return NULL if the given intro point has no legacy ID or if the node can't
  471. * be found in the consensus. */
  472. STATIC const node_t *
  473. get_node_from_intro_point(const hs_service_intro_point_t *ip)
  474. {
  475. const hs_desc_link_specifier_t *ls;
  476. tor_assert(ip);
  477. ls = get_link_spec_by_type(ip, LS_LEGACY_ID);
  478. /* Legacy ID is mandatory for an intro point object to have. */
  479. tor_assert(ls);
  480. /* XXX In the future, we want to only use the ed25519 ID (#22173). */
  481. return node_get_by_id((const char *) ls->u.legacy_id);
  482. }
  483. /* Given a service intro point, return the extend_info_t for it. This can
  484. * return NULL if the node can't be found for the intro point or the extend
  485. * info can't be created for the found node. If direct_conn is set, the extend
  486. * info is validated on if we can connect directly. */
  487. static extend_info_t *
  488. get_extend_info_from_intro_point(const hs_service_intro_point_t *ip,
  489. unsigned int direct_conn)
  490. {
  491. extend_info_t *info = NULL;
  492. const node_t *node;
  493. tor_assert(ip);
  494. node = get_node_from_intro_point(ip);
  495. if (node == NULL) {
  496. /* This can happen if the relay serving as intro point has been removed
  497. * from the consensus. In that case, the intro point will be removed from
  498. * the descriptor during the scheduled events. */
  499. goto end;
  500. }
  501. /* In the case of a direct connection (single onion service), it is possible
  502. * our firewall policy won't allow it so this can return a NULL value. */
  503. info = extend_info_from_node(node, direct_conn);
  504. end:
  505. return info;
  506. }
  507. /* Return the number of introduction points that are established for the
  508. * given descriptor. */
  509. static unsigned int
  510. count_desc_circuit_established(const hs_service_descriptor_t *desc)
  511. {
  512. unsigned int count = 0;
  513. tor_assert(desc);
  514. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  515. const hs_service_intro_point_t *, ip) {
  516. count += ip->circuit_established;
  517. } DIGEST256MAP_FOREACH_END;
  518. return count;
  519. }
  520. /* Close all rendezvous circuits for the given service. */
  521. static void
  522. close_service_rp_circuits(hs_service_t *service)
  523. {
  524. origin_circuit_t *ocirc = NULL;
  525. tor_assert(service);
  526. /* The reason we go over all circuit instead of using the circuitmap API is
  527. * because most hidden service circuits are rendezvous circuits so there is
  528. * no real improvement at getting all rendezvous circuits from the
  529. * circuitmap and then going over them all to find the right ones.
  530. * Furthermore, another option would have been to keep a list of RP cookies
  531. * for a service but it creates an engineering complexity since we don't
  532. * have a "RP circuit closed" event to clean it up properly so we avoid a
  533. * memory DoS possibility. */
  534. while ((ocirc = circuit_get_next_service_rp_circ(ocirc))) {
  535. /* Only close circuits that are v3 and for this service. */
  536. if (ocirc->hs_ident != NULL &&
  537. ed25519_pubkey_eq(&ocirc->hs_ident->identity_pk,
  538. &service->keys.identity_pk)) {
  539. /* Reason is FINISHED because service has been removed and thus the
  540. * circuit is considered old/uneeded. When freed, it is removed from the
  541. * hs circuitmap. */
  542. circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
  543. }
  544. }
  545. }
  546. /* Close the circuit(s) for the given map of introduction points. */
  547. static void
  548. close_intro_circuits(hs_service_intropoints_t *intro_points)
  549. {
  550. tor_assert(intro_points);
  551. DIGEST256MAP_FOREACH(intro_points->map, key,
  552. const hs_service_intro_point_t *, ip) {
  553. origin_circuit_t *ocirc = hs_circ_service_get_intro_circ(ip);
  554. if (ocirc) {
  555. /* Reason is FINISHED because service has been removed and thus the
  556. * circuit is considered old/uneeded. When freed, the circuit is removed
  557. * from the HS circuitmap. */
  558. circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
  559. }
  560. } DIGEST256MAP_FOREACH_END;
  561. }
  562. /* Close all introduction circuits for the given service. */
  563. static void
  564. close_service_intro_circuits(hs_service_t *service)
  565. {
  566. tor_assert(service);
  567. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  568. close_intro_circuits(&desc->intro_points);
  569. } FOR_EACH_DESCRIPTOR_END;
  570. }
  571. /* Close any circuits related to the given service. */
  572. static void
  573. close_service_circuits(hs_service_t *service)
  574. {
  575. tor_assert(service);
  576. /* Only support for version >= 3. */
  577. if (BUG(service->config.version < HS_VERSION_THREE)) {
  578. return;
  579. }
  580. /* Close intro points. */
  581. close_service_intro_circuits(service);
  582. /* Close rendezvous points. */
  583. close_service_rp_circuits(service);
  584. }
  585. /* Move introduction points from the src descriptor to the dst descriptor. The
  586. * destination service intropoints are wiped out if any before moving. */
  587. static void
  588. move_descriptor_intro_points(hs_service_descriptor_t *src,
  589. hs_service_descriptor_t *dst)
  590. {
  591. tor_assert(src);
  592. tor_assert(dst);
  593. digest256map_free(dst->intro_points.map, service_intro_point_free_);
  594. dst->intro_points.map = src->intro_points.map;
  595. /* Nullify the source. */
  596. src->intro_points.map = NULL;
  597. }
  598. /* Move introduction points from the src service to the dst service. The
  599. * destination service intropoints are wiped out if any before moving. */
  600. static void
  601. move_intro_points(hs_service_t *src, hs_service_t *dst)
  602. {
  603. tor_assert(src);
  604. tor_assert(dst);
  605. if (src->desc_current && dst->desc_current) {
  606. move_descriptor_intro_points(src->desc_current, dst->desc_current);
  607. }
  608. if (src->desc_next && dst->desc_next) {
  609. move_descriptor_intro_points(src->desc_next, dst->desc_next);
  610. }
  611. }
  612. /* Move every ephemeral services from the src service map to the dst service
  613. * map. It is possible that a service can't be register to the dst map which
  614. * won't stop the process of moving them all but will trigger a log warn. */
  615. static void
  616. move_ephemeral_services(hs_service_ht *src, hs_service_ht *dst)
  617. {
  618. hs_service_t **iter, **next;
  619. tor_assert(src);
  620. tor_assert(dst);
  621. /* Iterate over the map to find ephemeral service and move them to the other
  622. * map. We loop using this method to have a safe removal process. */
  623. for (iter = HT_START(hs_service_ht, src); iter != NULL; iter = next) {
  624. hs_service_t *s = *iter;
  625. if (!s->config.is_ephemeral) {
  626. /* Yeah, we are in a very manual loop :). */
  627. next = HT_NEXT(hs_service_ht, src, iter);
  628. continue;
  629. }
  630. /* Remove service from map and then register to it to the other map.
  631. * Reminder that "*iter" and "s" are the same thing. */
  632. next = HT_NEXT_RMV(hs_service_ht, src, iter);
  633. if (register_service(dst, s) < 0) {
  634. log_warn(LD_BUG, "Ephemeral service key is already being used. "
  635. "Skipping.");
  636. }
  637. }
  638. }
  639. /* Return a const string of the directory path escaped. If this is an
  640. * ephemeral service, it returns "[EPHEMERAL]". This can only be called from
  641. * the main thread because escaped() uses a static variable. */
  642. static const char *
  643. service_escaped_dir(const hs_service_t *s)
  644. {
  645. return (s->config.is_ephemeral) ? "[EPHEMERAL]" :
  646. escaped(s->config.directory_path);
  647. }
  648. /* Register services that are in the staging list. Once this function returns,
  649. * the global service map will be set with the right content and all non
  650. * surviving services will be cleaned up. */
  651. static void
  652. register_all_services(void)
  653. {
  654. struct hs_service_ht *new_service_map;
  655. tor_assert(hs_service_staging_list);
  656. /* We'll save us some allocation and computing time. */
  657. if (smartlist_len(hs_service_staging_list) == 0) {
  658. return;
  659. }
  660. /* Allocate a new map that will replace the current one. */
  661. new_service_map = tor_malloc_zero(sizeof(*new_service_map));
  662. HT_INIT(hs_service_ht, new_service_map);
  663. /* First step is to transfer all ephemeral services from the current global
  664. * map to the new one we are constructing. We do not prune ephemeral
  665. * services as the only way to kill them is by deleting it from the control
  666. * port or stopping the tor daemon. */
  667. move_ephemeral_services(hs_service_map, new_service_map);
  668. SMARTLIST_FOREACH_BEGIN(hs_service_staging_list, hs_service_t *, snew) {
  669. hs_service_t *s;
  670. /* Check if that service is already in our global map and if so, we'll
  671. * transfer the intro points to it. */
  672. s = find_service(hs_service_map, &snew->keys.identity_pk);
  673. if (s) {
  674. /* Pass ownership of intro points from s (the current service) to snew
  675. * (the newly configured one). */
  676. move_intro_points(s, snew);
  677. /* Remove the service from the global map because after this, we need to
  678. * go over the remaining service in that map that aren't surviving the
  679. * reload to close their circuits. */
  680. remove_service(hs_service_map, s);
  681. }
  682. /* Great, this service is now ready to be added to our new map. */
  683. if (BUG(register_service(new_service_map, snew) < 0)) {
  684. /* This should never happen because prior to registration, we validate
  685. * every service against the entire set. Not being able to register a
  686. * service means we failed to validate correctly. In that case, don't
  687. * break tor and ignore the service but tell user. */
  688. log_warn(LD_BUG, "Unable to register service with directory %s",
  689. service_escaped_dir(snew));
  690. SMARTLIST_DEL_CURRENT(hs_service_staging_list, snew);
  691. hs_service_free(snew);
  692. }
  693. } SMARTLIST_FOREACH_END(snew);
  694. /* Close any circuits associated with the non surviving services. Every
  695. * service in the current global map are roaming. */
  696. FOR_EACH_SERVICE_BEGIN(service) {
  697. close_service_circuits(service);
  698. } FOR_EACH_SERVICE_END;
  699. /* Time to make the switch. We'll clear the staging list because its content
  700. * has now changed ownership to the map. */
  701. smartlist_clear(hs_service_staging_list);
  702. service_free_all();
  703. hs_service_map = new_service_map;
  704. }
  705. /* Write the onion address of a given service to the given filename fname_ in
  706. * the service directory. Return 0 on success else -1 on error. */
  707. static int
  708. write_address_to_file(const hs_service_t *service, const char *fname_)
  709. {
  710. int ret = -1;
  711. char *fname = NULL;
  712. /* Length of an address plus the sizeof the address tld (onion) which counts
  713. * the NUL terminated byte so we keep it for the "." and the newline. */
  714. char buf[HS_SERVICE_ADDR_LEN_BASE32 + sizeof(address_tld) + 1];
  715. tor_assert(service);
  716. tor_assert(fname_);
  717. /* Construct the full address with the onion tld and write the hostname file
  718. * to disk. */
  719. tor_snprintf(buf, sizeof(buf), "%s.%s\n", service->onion_address,
  720. address_tld);
  721. /* Notice here that we use the given "fname_". */
  722. fname = hs_path_from_filename(service->config.directory_path, fname_);
  723. if (write_str_to_file(fname, buf, 0) < 0) {
  724. log_warn(LD_REND, "Could not write onion address to hostname file %s",
  725. escaped(fname));
  726. goto end;
  727. }
  728. #ifndef _WIN32
  729. if (service->config.dir_group_readable) {
  730. /* Mode to 0640. */
  731. if (chmod(fname, S_IRUSR | S_IWUSR | S_IRGRP) < 0) {
  732. log_warn(LD_FS, "Unable to make onion service hostname file %s "
  733. "group-readable.", escaped(fname));
  734. }
  735. }
  736. #endif /* _WIN32 */
  737. /* Success. */
  738. ret = 0;
  739. end:
  740. tor_free(fname);
  741. return ret;
  742. }
  743. /* Load and/or generate private keys for the given service. On success, the
  744. * hostname file will be written to disk along with the master private key iff
  745. * the service is not configured for offline keys. Return 0 on success else -1
  746. * on failure. */
  747. static int
  748. load_service_keys(hs_service_t *service)
  749. {
  750. int ret = -1;
  751. char *fname = NULL;
  752. ed25519_keypair_t *kp;
  753. const hs_service_config_t *config;
  754. tor_assert(service);
  755. config = &service->config;
  756. /* Create and fix permission on service directory. We are about to write
  757. * files to that directory so make sure it exists and has the right
  758. * permissions. We do this here because at this stage we know that Tor is
  759. * actually running and the service we have has been validated. */
  760. if (BUG(hs_check_service_private_dir(get_options()->User,
  761. config->directory_path,
  762. config->dir_group_readable, 1) < 0)) {
  763. goto end;
  764. }
  765. /* Try to load the keys from file or generate it if not found. */
  766. fname = hs_path_from_filename(config->directory_path, fname_keyfile_prefix);
  767. /* Don't ask for key creation, we want to know if we were able to load it or
  768. * we had to generate it. Better logging! */
  769. kp = ed_key_init_from_file(fname, 0, LOG_INFO, NULL, 0, 0, 0, NULL);
  770. if (!kp) {
  771. log_info(LD_REND, "Unable to load keys from %s. Generating it...", fname);
  772. /* We'll now try to generate the keys and for it we want the strongest
  773. * randomness for it. The keypair will be written in different files. */
  774. uint32_t key_flags = INIT_ED_KEY_CREATE | INIT_ED_KEY_EXTRA_STRONG |
  775. INIT_ED_KEY_SPLIT;
  776. kp = ed_key_init_from_file(fname, key_flags, LOG_WARN, NULL, 0, 0, 0,
  777. NULL);
  778. if (!kp) {
  779. log_warn(LD_REND, "Unable to generate keys and save in %s.", fname);
  780. goto end;
  781. }
  782. }
  783. /* Copy loaded or generated keys to service object. */
  784. ed25519_pubkey_copy(&service->keys.identity_pk, &kp->pubkey);
  785. memcpy(&service->keys.identity_sk, &kp->seckey,
  786. sizeof(service->keys.identity_sk));
  787. /* This does a proper memory wipe. */
  788. ed25519_keypair_free(kp);
  789. /* Build onion address from the newly loaded keys. */
  790. tor_assert(service->config.version <= UINT8_MAX);
  791. hs_build_address(&service->keys.identity_pk,
  792. (uint8_t) service->config.version,
  793. service->onion_address);
  794. /* Write onion address to hostname file. */
  795. if (write_address_to_file(service, fname_hostname) < 0) {
  796. goto end;
  797. }
  798. /* Succes. */
  799. ret = 0;
  800. end:
  801. tor_free(fname);
  802. return ret;
  803. }
  804. /* Free a given service descriptor object and all key material is wiped. */
  805. static void
  806. service_descriptor_free(hs_service_descriptor_t *desc)
  807. {
  808. if (!desc) {
  809. return;
  810. }
  811. hs_descriptor_free(desc->desc);
  812. memwipe(&desc->signing_kp, 0, sizeof(desc->signing_kp));
  813. memwipe(&desc->blinded_kp, 0, sizeof(desc->blinded_kp));
  814. SMARTLIST_FOREACH(desc->hsdir_missing_info, char *, id, tor_free(id));
  815. smartlist_free(desc->hsdir_missing_info);
  816. /* Cleanup all intro points. */
  817. digest256map_free(desc->intro_points.map, service_intro_point_free_);
  818. digestmap_free(desc->intro_points.failed_id, tor_free_);
  819. tor_free(desc);
  820. }
  821. /* Return a newly allocated service descriptor object. */
  822. STATIC hs_service_descriptor_t *
  823. service_descriptor_new(void)
  824. {
  825. hs_service_descriptor_t *sdesc = tor_malloc_zero(sizeof(*sdesc));
  826. sdesc->desc = tor_malloc_zero(sizeof(hs_descriptor_t));
  827. /* Initialize the intro points map. */
  828. sdesc->intro_points.map = digest256map_new();
  829. sdesc->intro_points.failed_id = digestmap_new();
  830. sdesc->hsdir_missing_info = smartlist_new();
  831. return sdesc;
  832. }
  833. /* From the given service, remove all expired failing intro points for each
  834. * descriptor. */
  835. static void
  836. remove_expired_failing_intro(hs_service_t *service, time_t now)
  837. {
  838. tor_assert(service);
  839. /* For both descriptors, cleanup the failing intro points list. */
  840. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  841. DIGESTMAP_FOREACH_MODIFY(desc->intro_points.failed_id, key, time_t *, t) {
  842. time_t failure_time = *t;
  843. if ((failure_time + INTRO_CIRC_RETRY_PERIOD) <= now) {
  844. MAP_DEL_CURRENT(key);
  845. tor_free(t);
  846. }
  847. } DIGESTMAP_FOREACH_END;
  848. } FOR_EACH_DESCRIPTOR_END;
  849. }
  850. /* For the given descriptor desc, put all node_t object found from its failing
  851. * intro point list and put them in the given node_list. */
  852. static void
  853. setup_intro_point_exclude_list(const hs_service_descriptor_t *desc,
  854. smartlist_t *node_list)
  855. {
  856. tor_assert(desc);
  857. tor_assert(node_list);
  858. DIGESTMAP_FOREACH(desc->intro_points.failed_id, key, time_t *, t) {
  859. (void) t; /* Make gcc happy. */
  860. const node_t *node = node_get_by_id(key);
  861. if (node) {
  862. smartlist_add(node_list, (void *) node);
  863. }
  864. } DIGESTMAP_FOREACH_END;
  865. }
  866. /* For the given failing intro point ip, we add its time of failure to the
  867. * failed map and index it by identity digest (legacy ID) in the descriptor
  868. * desc failed id map. */
  869. static void
  870. remember_failing_intro_point(const hs_service_intro_point_t *ip,
  871. hs_service_descriptor_t *desc, time_t now)
  872. {
  873. time_t *time_of_failure, *prev_ptr;
  874. const hs_desc_link_specifier_t *legacy_ls;
  875. tor_assert(ip);
  876. tor_assert(desc);
  877. time_of_failure = tor_malloc_zero(sizeof(time_t));
  878. *time_of_failure = now;
  879. legacy_ls = get_link_spec_by_type(ip, LS_LEGACY_ID);
  880. tor_assert(legacy_ls);
  881. prev_ptr = digestmap_set(desc->intro_points.failed_id,
  882. (const char *) legacy_ls->u.legacy_id,
  883. time_of_failure);
  884. tor_free(prev_ptr);
  885. }
  886. /* Copy the descriptor link specifier object from src to dst. */
  887. static void
  888. link_specifier_copy(hs_desc_link_specifier_t *dst,
  889. const hs_desc_link_specifier_t *src)
  890. {
  891. tor_assert(dst);
  892. tor_assert(src);
  893. memcpy(dst, src, sizeof(hs_desc_link_specifier_t));
  894. }
  895. /* Using a given descriptor signing keypair signing_kp, a service intro point
  896. * object ip and the time now, setup the content of an already allocated
  897. * descriptor intro desc_ip.
  898. *
  899. * Return 0 on success else a negative value. */
  900. static int
  901. setup_desc_intro_point(const ed25519_keypair_t *signing_kp,
  902. const hs_service_intro_point_t *ip,
  903. time_t now, hs_desc_intro_point_t *desc_ip)
  904. {
  905. int ret = -1;
  906. time_t nearest_hour = now - (now % 3600);
  907. tor_assert(signing_kp);
  908. tor_assert(ip);
  909. tor_assert(desc_ip);
  910. /* Copy the onion key. */
  911. memcpy(&desc_ip->onion_key, &ip->onion_key, sizeof(desc_ip->onion_key));
  912. /* Key and certificate material. */
  913. desc_ip->auth_key_cert = tor_cert_create(signing_kp,
  914. CERT_TYPE_AUTH_HS_IP_KEY,
  915. &ip->auth_key_kp.pubkey,
  916. nearest_hour,
  917. HS_DESC_CERT_LIFETIME,
  918. CERT_FLAG_INCLUDE_SIGNING_KEY);
  919. if (desc_ip->auth_key_cert == NULL) {
  920. log_warn(LD_REND, "Unable to create intro point auth-key certificate");
  921. goto done;
  922. }
  923. /* Copy link specifier(s). */
  924. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  925. const hs_desc_link_specifier_t *, ls) {
  926. hs_desc_link_specifier_t *dup = tor_malloc_zero(sizeof(*dup));
  927. link_specifier_copy(dup, ls);
  928. smartlist_add(desc_ip->link_specifiers, dup);
  929. } SMARTLIST_FOREACH_END(ls);
  930. /* For a legacy intro point, we'll use an RSA/ed cross certificate. */
  931. if (ip->base.is_only_legacy) {
  932. desc_ip->legacy.key = crypto_pk_dup_key(ip->legacy_key);
  933. /* Create cross certification cert. */
  934. ssize_t cert_len = tor_make_rsa_ed25519_crosscert(
  935. &signing_kp->pubkey,
  936. desc_ip->legacy.key,
  937. nearest_hour + HS_DESC_CERT_LIFETIME,
  938. &desc_ip->legacy.cert.encoded);
  939. if (cert_len < 0) {
  940. log_warn(LD_REND, "Unable to create enc key legacy cross cert.");
  941. goto done;
  942. }
  943. desc_ip->legacy.cert.len = cert_len;
  944. }
  945. /* Encryption key and its cross certificate. */
  946. {
  947. ed25519_public_key_t ed25519_pubkey;
  948. /* Use the public curve25519 key. */
  949. memcpy(&desc_ip->enc_key, &ip->enc_key_kp.pubkey,
  950. sizeof(desc_ip->enc_key));
  951. /* The following can't fail. */
  952. ed25519_public_key_from_curve25519_public_key(&ed25519_pubkey,
  953. &ip->enc_key_kp.pubkey,
  954. 0);
  955. desc_ip->enc_key_cert = tor_cert_create(signing_kp,
  956. CERT_TYPE_CROSS_HS_IP_KEYS,
  957. &ed25519_pubkey, nearest_hour,
  958. HS_DESC_CERT_LIFETIME,
  959. CERT_FLAG_INCLUDE_SIGNING_KEY);
  960. if (desc_ip->enc_key_cert == NULL) {
  961. log_warn(LD_REND, "Unable to create enc key curve25519 cross cert.");
  962. goto done;
  963. }
  964. }
  965. /* Success. */
  966. ret = 0;
  967. done:
  968. return ret;
  969. }
  970. /* Using the given descriptor from the given service, build the descriptor
  971. * intro point list so we can then encode the descriptor for publication. This
  972. * function does not pick intro points, they have to be in the descriptor
  973. * current map. Cryptographic material (keys) must be initialized in the
  974. * descriptor for this function to make sense. */
  975. static void
  976. build_desc_intro_points(const hs_service_t *service,
  977. hs_service_descriptor_t *desc, time_t now)
  978. {
  979. hs_desc_encrypted_data_t *encrypted;
  980. tor_assert(service);
  981. tor_assert(desc);
  982. /* Ease our life. */
  983. encrypted = &desc->desc->encrypted_data;
  984. /* Cleanup intro points, we are about to set them from scratch. */
  985. hs_descriptor_free_intro_points(desc->desc);
  986. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  987. const hs_service_intro_point_t *, ip) {
  988. hs_desc_intro_point_t *desc_ip = hs_desc_intro_point_new();
  989. if (setup_desc_intro_point(&desc->signing_kp, ip, now, desc_ip) < 0) {
  990. hs_desc_intro_point_free(desc_ip);
  991. continue;
  992. }
  993. /* We have a valid descriptor intro point. Add it to the list. */
  994. smartlist_add(encrypted->intro_points, desc_ip);
  995. } DIGEST256MAP_FOREACH_END;
  996. }
  997. /* Populate the descriptor encrypted section fomr the given service object.
  998. * This will generate a valid list of introduction points that can be used
  999. * after for circuit creation. Return 0 on success else -1 on error. */
  1000. static int
  1001. build_service_desc_encrypted(const hs_service_t *service,
  1002. hs_service_descriptor_t *desc)
  1003. {
  1004. hs_desc_encrypted_data_t *encrypted;
  1005. tor_assert(service);
  1006. tor_assert(desc);
  1007. encrypted = &desc->desc->encrypted_data;
  1008. encrypted->create2_ntor = 1;
  1009. encrypted->single_onion_service = service->config.is_single_onion;
  1010. /* Setup introduction points from what we have in the service. */
  1011. if (encrypted->intro_points == NULL) {
  1012. encrypted->intro_points = smartlist_new();
  1013. }
  1014. /* We do NOT build introduction point yet, we only do that once the circuit
  1015. * have been opened. Until we have the right number of introduction points,
  1016. * we do not encode anything in the descriptor. */
  1017. /* XXX: Support client authorization (#20700). */
  1018. encrypted->intro_auth_types = NULL;
  1019. return 0;
  1020. }
  1021. /* Populare the descriptor plaintext section from the given service object.
  1022. * The caller must make sure that the keys in the descriptors are valid that
  1023. * is are non-zero. Return 0 on success else -1 on error. */
  1024. static int
  1025. build_service_desc_plaintext(const hs_service_t *service,
  1026. hs_service_descriptor_t *desc, time_t now)
  1027. {
  1028. int ret = -1;
  1029. hs_desc_plaintext_data_t *plaintext;
  1030. tor_assert(service);
  1031. tor_assert(desc);
  1032. /* XXX: Use a "assert_desc_ok()" ? */
  1033. tor_assert(!tor_mem_is_zero((char *) &desc->blinded_kp,
  1034. sizeof(desc->blinded_kp)));
  1035. tor_assert(!tor_mem_is_zero((char *) &desc->signing_kp,
  1036. sizeof(desc->signing_kp)));
  1037. /* Set the subcredential. */
  1038. hs_get_subcredential(&service->keys.identity_pk, &desc->blinded_kp.pubkey,
  1039. desc->desc->subcredential);
  1040. plaintext = &desc->desc->plaintext_data;
  1041. plaintext->version = service->config.version;
  1042. plaintext->lifetime_sec = HS_DESC_DEFAULT_LIFETIME;
  1043. plaintext->signing_key_cert =
  1044. tor_cert_create(&desc->blinded_kp, CERT_TYPE_SIGNING_HS_DESC,
  1045. &desc->signing_kp.pubkey, now, HS_DESC_CERT_LIFETIME,
  1046. CERT_FLAG_INCLUDE_SIGNING_KEY);
  1047. if (plaintext->signing_key_cert == NULL) {
  1048. log_warn(LD_REND, "Unable to create descriptor signing certificate for "
  1049. "service %s",
  1050. safe_str_client(service->onion_address));
  1051. goto end;
  1052. }
  1053. /* Copy public key material to go in the descriptor. */
  1054. ed25519_pubkey_copy(&plaintext->signing_pubkey, &desc->signing_kp.pubkey);
  1055. ed25519_pubkey_copy(&plaintext->blinded_pubkey, &desc->blinded_kp.pubkey);
  1056. /* Success. */
  1057. ret = 0;
  1058. end:
  1059. return ret;
  1060. }
  1061. /* For the given service and descriptor object, create the key material which
  1062. * is the blinded keypair and the descriptor signing keypair. Return 0 on
  1063. * success else -1 on error where the generated keys MUST be ignored. */
  1064. static int
  1065. build_service_desc_keys(const hs_service_t *service,
  1066. hs_service_descriptor_t *desc,
  1067. uint64_t time_period_num)
  1068. {
  1069. int ret = 0;
  1070. ed25519_keypair_t kp;
  1071. tor_assert(desc);
  1072. tor_assert(!tor_mem_is_zero((char *) &service->keys.identity_pk,
  1073. ED25519_PUBKEY_LEN));
  1074. /* XXX: Support offline key feature (#18098). */
  1075. /* Copy the identity keys to the keypair so we can use it to create the
  1076. * blinded key. */
  1077. memcpy(&kp.pubkey, &service->keys.identity_pk, sizeof(kp.pubkey));
  1078. memcpy(&kp.seckey, &service->keys.identity_sk, sizeof(kp.seckey));
  1079. /* Build blinded keypair for this time period. */
  1080. hs_build_blinded_keypair(&kp, NULL, 0, time_period_num, &desc->blinded_kp);
  1081. /* Let's not keep too much traces of our keys in memory. */
  1082. memwipe(&kp, 0, sizeof(kp));
  1083. /* No need for extra strong, this is a temporary key only for this
  1084. * descriptor. Nothing long term. */
  1085. if (ed25519_keypair_generate(&desc->signing_kp, 0) < 0) {
  1086. log_warn(LD_REND, "Can't generate descriptor signing keypair for "
  1087. "service %s",
  1088. safe_str_client(service->onion_address));
  1089. ret = -1;
  1090. }
  1091. return ret;
  1092. }
  1093. /* Given a service and the current time, build a descriptor for the service.
  1094. * This function does not pick introduction point, this needs to be done by
  1095. * the update function. On success, desc_out will point to the newly allocated
  1096. * descriptor object.
  1097. *
  1098. * This can error if we are unable to create keys or certificate. */
  1099. static void
  1100. build_service_descriptor(hs_service_t *service, time_t now,
  1101. uint64_t time_period_num,
  1102. hs_service_descriptor_t **desc_out)
  1103. {
  1104. char *encoded_desc;
  1105. hs_service_descriptor_t *desc;
  1106. tor_assert(service);
  1107. tor_assert(desc_out);
  1108. desc = service_descriptor_new();
  1109. desc->time_period_num = time_period_num;
  1110. /* Create the needed keys so we can setup the descriptor content. */
  1111. if (build_service_desc_keys(service, desc, time_period_num) < 0) {
  1112. goto err;
  1113. }
  1114. /* Setup plaintext descriptor content. */
  1115. if (build_service_desc_plaintext(service, desc, now) < 0) {
  1116. goto err;
  1117. }
  1118. /* Setup encrypted descriptor content. */
  1119. if (build_service_desc_encrypted(service, desc) < 0) {
  1120. goto err;
  1121. }
  1122. /* Let's make sure that we've created a descriptor that can actually be
  1123. * encoded properly. This function also checks if the encoded output is
  1124. * decodable after. */
  1125. if (BUG(hs_desc_encode_descriptor(desc->desc, &desc->signing_kp,
  1126. &encoded_desc) < 0)) {
  1127. goto err;
  1128. }
  1129. tor_free(encoded_desc);
  1130. /* Assign newly built descriptor to the next slot. */
  1131. *desc_out = desc;
  1132. return;
  1133. err:
  1134. service_descriptor_free(desc);
  1135. }
  1136. /* Build descriptors for each service if needed. There are conditions to build
  1137. * a descriptor which are details in the function. */
  1138. STATIC void
  1139. build_all_descriptors(time_t now)
  1140. {
  1141. FOR_EACH_SERVICE_BEGIN(service) {
  1142. if (service->desc_current == NULL) {
  1143. /* This means we just booted up because else this descriptor will never
  1144. * be NULL as it should always point to the descriptor that was in
  1145. * desc_next after rotation. */
  1146. build_service_descriptor(service, now, hs_get_time_period_num(now),
  1147. &service->desc_current);
  1148. log_info(LD_REND, "Hidden service %s current descriptor successfully "
  1149. "built. Now scheduled for upload.",
  1150. safe_str_client(service->onion_address));
  1151. }
  1152. /* A next descriptor to NULL indicate that we need to build a fresh one if
  1153. * we are in the overlap period for the _next_ time period since it means
  1154. * we either just booted or we just rotated our descriptors. */
  1155. if (hs_overlap_mode_is_active(NULL, now) && service->desc_next == NULL) {
  1156. build_service_descriptor(service, now, hs_get_next_time_period_num(now),
  1157. &service->desc_next);
  1158. log_info(LD_REND, "Hidden service %s next descriptor successfully "
  1159. "built. Now scheduled for upload.",
  1160. safe_str_client(service->onion_address));
  1161. }
  1162. } FOR_EACH_DESCRIPTOR_END;
  1163. }
  1164. /* Randomly pick a node to become an introduction point but not present in the
  1165. * given exclude_nodes list. The chosen node is put in the exclude list
  1166. * regardless of success or not because in case of failure, the node is simply
  1167. * unsusable from that point on. If direct_conn is set, try to pick a node
  1168. * that our local firewall/policy allows to directly connect to and if not,
  1169. * fallback to a normal 3-hop node. Return a newly allocated service intro
  1170. * point ready to be used for encoding. NULL on error. */
  1171. static hs_service_intro_point_t *
  1172. pick_intro_point(unsigned int direct_conn, smartlist_t *exclude_nodes)
  1173. {
  1174. const node_t *node;
  1175. extend_info_t *info = NULL;
  1176. hs_service_intro_point_t *ip = NULL;
  1177. /* Normal 3-hop introduction point flags. */
  1178. router_crn_flags_t flags = CRN_NEED_UPTIME | CRN_NEED_DESC;
  1179. /* Single onion flags. */
  1180. router_crn_flags_t direct_flags = flags | CRN_PREF_ADDR | CRN_DIRECT_CONN;
  1181. node = router_choose_random_node(exclude_nodes, get_options()->ExcludeNodes,
  1182. direct_conn ? direct_flags : flags);
  1183. if (node == NULL && direct_conn) {
  1184. /* Unable to find a node for direct connection, let's fall back to a
  1185. * normal 3-hop node. */
  1186. node = router_choose_random_node(exclude_nodes,
  1187. get_options()->ExcludeNodes, flags);
  1188. }
  1189. if (!node) {
  1190. goto err;
  1191. }
  1192. /* We have a suitable node, add it to the exclude list. We do this *before*
  1193. * we can validate the extend information because even in case of failure,
  1194. * we don't want to use that node anymore. */
  1195. smartlist_add(exclude_nodes, (void *) node);
  1196. /* We do this to ease our life but also this call makes appropriate checks
  1197. * of the node object such as validating ntor support for instance. */
  1198. info = extend_info_from_node(node, direct_conn);
  1199. if (BUG(info == NULL)) {
  1200. goto err;
  1201. }
  1202. /* Create our objects and populate them with the node information. */
  1203. ip = service_intro_point_new(info, !node_supports_ed25519_hs_intro(node));
  1204. if (ip == NULL) {
  1205. goto err;
  1206. }
  1207. extend_info_free(info);
  1208. return ip;
  1209. err:
  1210. service_intro_point_free(ip);
  1211. extend_info_free(info);
  1212. return NULL;
  1213. }
  1214. /* For a given descriptor from the given service, pick any needed intro points
  1215. * and update the current map with those newly picked intro points. Return the
  1216. * number node that might have been added to the descriptor current map. */
  1217. static unsigned int
  1218. pick_needed_intro_points(hs_service_t *service,
  1219. hs_service_descriptor_t *desc)
  1220. {
  1221. int i = 0, num_needed_ip;
  1222. smartlist_t *exclude_nodes = smartlist_new();
  1223. tor_assert(service);
  1224. tor_assert(desc);
  1225. /* Compute how many intro points we actually need to open. */
  1226. num_needed_ip = service->config.num_intro_points -
  1227. digest256map_size(desc->intro_points.map);
  1228. if (BUG(num_needed_ip < 0)) {
  1229. /* Let's not make tor freak out here and just skip this. */
  1230. goto done;
  1231. }
  1232. /* We want to end up with config.num_intro_points intro points, but if we
  1233. * have no intro points at all (chances are they all cycled or we are
  1234. * starting up), we launch get_intro_point_num_extra() extra circuits and
  1235. * use the first config.num_intro_points that complete. See proposal #155,
  1236. * section 4 for the rationale of this which is purely for performance.
  1237. *
  1238. * The ones after the first config.num_intro_points will be converted to
  1239. * 'General' internal circuits and then we'll drop them from the list of
  1240. * intro points. */
  1241. if (digest256map_size(desc->intro_points.map) == 0) {
  1242. num_needed_ip += get_intro_point_num_extra();
  1243. }
  1244. /* Build an exclude list of nodes of our intro point(s). The expiring intro
  1245. * points are OK to pick again because this is afterall a concept of round
  1246. * robin so they are considered valid nodes to pick again. */
  1247. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  1248. hs_service_intro_point_t *, ip) {
  1249. smartlist_add(exclude_nodes, (void *) get_node_from_intro_point(ip));
  1250. } DIGEST256MAP_FOREACH_END;
  1251. /* Also, add the failing intro points that our descriptor encounteered in
  1252. * the exclude node list. */
  1253. setup_intro_point_exclude_list(desc, exclude_nodes);
  1254. for (i = 0; i < num_needed_ip; i++) {
  1255. hs_service_intro_point_t *ip;
  1256. /* This function will add the picked intro point node to the exclude nodes
  1257. * list so we don't pick the same one at the next iteration. */
  1258. ip = pick_intro_point(service->config.is_single_onion, exclude_nodes);
  1259. if (ip == NULL) {
  1260. /* If we end up unable to pick an introduction point it is because we
  1261. * can't find suitable node and calling this again is highly unlikely to
  1262. * give us a valid node all of the sudden. */
  1263. log_info(LD_REND, "Unable to find a suitable node to be an "
  1264. "introduction point for service %s.",
  1265. safe_str_client(service->onion_address));
  1266. goto done;
  1267. }
  1268. /* Valid intro point object, add it to the descriptor current map. */
  1269. service_intro_point_add(desc->intro_points.map, ip);
  1270. }
  1271. /* We've successfully picked all our needed intro points thus none are
  1272. * missing which will tell our upload process to expect the number of
  1273. * circuits to be the number of configured intro points circuits and not the
  1274. * number of intro points object that we have. */
  1275. desc->missing_intro_points = 0;
  1276. /* Success. */
  1277. done:
  1278. /* We don't have ownership of the node_t object in this list. */
  1279. smartlist_free(exclude_nodes);
  1280. return i;
  1281. }
  1282. /* Update the given descriptor from the given service. The possible update
  1283. * actions includes:
  1284. * - Picking missing intro points if needed.
  1285. * - Incrementing the revision counter if needed.
  1286. */
  1287. static void
  1288. update_service_descriptor(hs_service_t *service,
  1289. hs_service_descriptor_t *desc, time_t now)
  1290. {
  1291. unsigned int num_intro_points;
  1292. tor_assert(service);
  1293. tor_assert(desc);
  1294. tor_assert(desc->desc);
  1295. num_intro_points = digest256map_size(desc->intro_points.map);
  1296. /* Pick any missing introduction point(s). */
  1297. if (num_intro_points < service->config.num_intro_points) {
  1298. unsigned int num_new_intro_points = pick_needed_intro_points(service,
  1299. desc);
  1300. if (num_new_intro_points != 0) {
  1301. log_info(LD_REND, "Service %s just picked %u intro points and wanted "
  1302. "%u. It currently has %d intro points. "
  1303. "Launching ESTABLISH_INTRO circuit shortly.",
  1304. safe_str_client(service->onion_address),
  1305. num_new_intro_points,
  1306. service->config.num_intro_points - num_intro_points,
  1307. num_intro_points);
  1308. /* We'll build those introduction point into the descriptor once we have
  1309. * confirmation that the circuits are opened and ready. However,
  1310. * indicate that this descriptor should be uploaded from now on. */
  1311. desc->next_upload_time = now;
  1312. }
  1313. /* Were we able to pick all the intro points we needed? If not, we'll
  1314. * flag the descriptor that it's missing intro points because it
  1315. * couldn't pick enough which will trigger a descriptor upload. */
  1316. if ((num_new_intro_points + num_intro_points) <
  1317. service->config.num_intro_points) {
  1318. desc->missing_intro_points = 1;
  1319. }
  1320. }
  1321. }
  1322. /* Update descriptors for each service if needed. */
  1323. STATIC void
  1324. update_all_descriptors(time_t now)
  1325. {
  1326. FOR_EACH_SERVICE_BEGIN(service) {
  1327. /* We'll try to update each descriptor that is if certain conditions apply
  1328. * in order for the descriptor to be updated. */
  1329. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  1330. update_service_descriptor(service, desc, now);
  1331. } FOR_EACH_DESCRIPTOR_END;
  1332. } FOR_EACH_SERVICE_END;
  1333. }
  1334. /* Return true iff the given intro point has expired that is it has been used
  1335. * for too long or we've reached our max seen INTRODUCE2 cell. */
  1336. STATIC int
  1337. intro_point_should_expire(const hs_service_intro_point_t *ip,
  1338. time_t now)
  1339. {
  1340. tor_assert(ip);
  1341. if (ip->introduce2_count >= ip->introduce2_max) {
  1342. goto expired;
  1343. }
  1344. if (ip->time_to_expire <= now) {
  1345. goto expired;
  1346. }
  1347. /* Not expiring. */
  1348. return 0;
  1349. expired:
  1350. return 1;
  1351. }
  1352. /* Go over the given set of intro points for each service and remove any
  1353. * invalid ones. The conditions for removal are:
  1354. *
  1355. * - The node doesn't exists anymore (not in consensus)
  1356. * OR
  1357. * - The intro point maximum circuit retry count has been reached and no
  1358. * circuit can be found associated with it.
  1359. * OR
  1360. * - The intro point has expired and we should pick a new one.
  1361. *
  1362. * If an intro point is removed, the circuit (if any) is immediately close.
  1363. * If a circuit can't be found, the intro point is kept if it hasn't reached
  1364. * its maximum circuit retry value and thus should be retried. */
  1365. static void
  1366. cleanup_intro_points(hs_service_t *service, time_t now)
  1367. {
  1368. tor_assert(service);
  1369. /* For both descriptors, cleanup the intro points. */
  1370. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  1371. /* Go over the current intro points we have, make sure they are still
  1372. * valid and remove any of them that aren't. */
  1373. DIGEST256MAP_FOREACH_MODIFY(desc->intro_points.map, key,
  1374. hs_service_intro_point_t *, ip) {
  1375. const node_t *node = get_node_from_intro_point(ip);
  1376. origin_circuit_t *ocirc = hs_circ_service_get_intro_circ(ip);
  1377. int has_expired = intro_point_should_expire(ip, now);
  1378. /* We cleanup an intro point if it has expired or if we do not know the
  1379. * node_t anymore (removed from our latest consensus) or if we've
  1380. * reached the maximum number of retry with a non existing circuit. */
  1381. if (has_expired || node == NULL ||
  1382. ip->circuit_retries > MAX_INTRO_POINT_CIRCUIT_RETRIES) {
  1383. /* Remove intro point from descriptor map. We'll add it to the failed
  1384. * map if we retried it too many times. */
  1385. MAP_DEL_CURRENT(key);
  1386. service_intro_point_free(ip);
  1387. /* XXX: Legacy code does NOT do that, it keeps the circuit open until
  1388. * a new descriptor is uploaded and then closed all expiring intro
  1389. * point circuit. Here, we close immediately and because we just
  1390. * discarded the intro point, a new one will be selected, a new
  1391. * descriptor created and uploaded. There is no difference to an
  1392. * attacker between the timing of a new consensus and intro point
  1393. * rotation (possibly?). */
  1394. if (ocirc) {
  1395. /* After this, no new cells will be handled on the circuit. */
  1396. circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
  1397. }
  1398. continue;
  1399. }
  1400. } DIGEST256MAP_FOREACH_END;
  1401. } FOR_EACH_DESCRIPTOR_END;
  1402. }
  1403. /** We just entered overlap period and we need to rotate our <b>service</b>
  1404. * descriptors */
  1405. static void
  1406. rotate_service_descriptors(hs_service_t *service)
  1407. {
  1408. if (service->desc_current) {
  1409. /* Close all IP circuits for the descriptor. */
  1410. close_intro_circuits(&service->desc_current->intro_points);
  1411. /* We don't need this one anymore, we won't serve any clients coming with
  1412. * this service descriptor. */
  1413. service_descriptor_free(service->desc_current);
  1414. }
  1415. /* The next one become the current one and emptying the next will trigger
  1416. * a descriptor creation for it. */
  1417. service->desc_current = service->desc_next;
  1418. service->desc_next = NULL;
  1419. }
  1420. /* Rotate descriptors for each service if needed. If we are just entering
  1421. * the overlap period, rotate them that is point the previous descriptor to
  1422. * the current and cleanup the previous one. A non existing current
  1423. * descriptor will trigger a descriptor build for the next time period. */
  1424. STATIC void
  1425. rotate_all_descriptors(time_t now)
  1426. {
  1427. FOR_EACH_SERVICE_BEGIN(service) {
  1428. /* We are _not_ in the overlap period so skip rotation. */
  1429. if (!hs_overlap_mode_is_active(NULL, now)) {
  1430. service->state.in_overlap_period = 0;
  1431. continue;
  1432. }
  1433. /* We've entered the overlap period already so skip rotation. */
  1434. if (service->state.in_overlap_period) {
  1435. continue;
  1436. }
  1437. /* It's the first time the service encounters the overlap period so flag
  1438. * it in order to make sure we don't rotate at next check. */
  1439. service->state.in_overlap_period = 1;
  1440. /* If we have a next descriptor lined up, rotate the descriptors so that it
  1441. * becomes current. */
  1442. if (service->desc_next) {
  1443. rotate_service_descriptors(service);
  1444. }
  1445. log_info(LD_REND, "We've just entered the overlap period. Service %s "
  1446. "descriptors have been rotated!",
  1447. safe_str_client(service->onion_address));
  1448. } FOR_EACH_SERVICE_END;
  1449. }
  1450. /* Scheduled event run from the main loop. Make sure all our services are up
  1451. * to date and ready for the other scheduled events. This includes looking at
  1452. * the introduction points status and descriptor rotation time. */
  1453. STATIC void
  1454. run_housekeeping_event(time_t now)
  1455. {
  1456. /* Note that nothing here opens circuit(s) nor uploads descriptor(s). We are
  1457. * simply moving things around or removing uneeded elements. */
  1458. FOR_EACH_SERVICE_BEGIN(service) {
  1459. /* Cleanup invalid intro points from the service descriptor. */
  1460. cleanup_intro_points(service, now);
  1461. /* Remove expired failing intro point from the descriptor failed list. We
  1462. * reset them at each INTRO_CIRC_RETRY_PERIOD. */
  1463. remove_expired_failing_intro(service, now);
  1464. /* At this point, the service is now ready to go through the scheduled
  1465. * events guaranteeing a valid state. Intro points might be missing from
  1466. * the descriptors after the cleanup but the update/build process will
  1467. * make sure we pick those missing ones. */
  1468. } FOR_EACH_SERVICE_END;
  1469. }
  1470. /* Scheduled event run from the main loop. Make sure all descriptors are up to
  1471. * date. Once this returns, each service descriptor needs to be considered for
  1472. * new introduction circuits and then for upload. */
  1473. static void
  1474. run_build_descriptor_event(time_t now)
  1475. {
  1476. /* For v2 services, this step happens in the upload event. */
  1477. /* Run v3+ events. */
  1478. /* We start by rotating the descriptors only if needed. */
  1479. rotate_all_descriptors(now);
  1480. /* Then, we'll try to build new descriptors that we might need. The
  1481. * condition is that the next descriptor is non existing because it has
  1482. * been rotated or we just started up. */
  1483. build_all_descriptors(now);
  1484. /* Finally, we'll check if we should update the descriptors. Missing
  1485. * introduction points will be picked in this function which is useful for
  1486. * newly built descriptors. */
  1487. update_all_descriptors(now);
  1488. }
  1489. /* For the given service, launch any intro point circuits that could be
  1490. * needed. This considers every descriptor of the service. */
  1491. static void
  1492. launch_intro_point_circuits(hs_service_t *service, time_t now)
  1493. {
  1494. tor_assert(service);
  1495. /* For both descriptors, try to launch any missing introduction point
  1496. * circuits using the current map. */
  1497. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  1498. /* Keep a ref on if we need a direct connection. We use this often. */
  1499. unsigned int direct_conn = service->config.is_single_onion;
  1500. DIGEST256MAP_FOREACH_MODIFY(desc->intro_points.map, key,
  1501. hs_service_intro_point_t *, ip) {
  1502. extend_info_t *ei;
  1503. /* Skip the intro point that already has an existing circuit
  1504. * (established or not). */
  1505. if (hs_circ_service_get_intro_circ(ip)) {
  1506. continue;
  1507. }
  1508. ei = get_extend_info_from_intro_point(ip, direct_conn);
  1509. if (ei == NULL) {
  1510. if (!direct_conn) {
  1511. /* In case of a multi-hop connection, it should never happen that we
  1512. * can't get the extend info from the node. Avoid connection and
  1513. * remove intro point from descriptor in order to recover from this
  1514. * potential bug. */
  1515. tor_assert_nonfatal(ei);
  1516. }
  1517. MAP_DEL_CURRENT(key);
  1518. service_intro_point_free(ip);
  1519. continue;
  1520. }
  1521. /* Launch a circuit to the intro point. */
  1522. ip->circuit_retries++;
  1523. if (hs_circ_launch_intro_point(service, ip, ei, now) < 0) {
  1524. log_warn(LD_REND, "Unable to launch intro circuit to node %s "
  1525. "for service %s.",
  1526. safe_str_client(extend_info_describe(ei)),
  1527. safe_str_client(service->onion_address));
  1528. /* Intro point will be retried if possible after this. */
  1529. }
  1530. extend_info_free(ei);
  1531. } DIGEST256MAP_FOREACH_END;
  1532. } FOR_EACH_DESCRIPTOR_END;
  1533. }
  1534. /* Don't try to build more than this many circuits before giving up for a
  1535. * while. Dynamically calculated based on the configured number of intro
  1536. * points for the given service and how many descriptor exists. The default
  1537. * use case of 3 introduction points and two descriptors will allow 28
  1538. * circuits for a retry period (((3 + 2) + (3 * 3)) * 2). */
  1539. static unsigned int
  1540. get_max_intro_circ_per_period(const hs_service_t *service)
  1541. {
  1542. unsigned int count = 0;
  1543. unsigned int multiplier = 0;
  1544. unsigned int num_wanted_ip;
  1545. tor_assert(service);
  1546. tor_assert(service->config.num_intro_points <=
  1547. HS_CONFIG_V3_MAX_INTRO_POINTS);
  1548. /* For a testing network, allow to do it for the maximum amount so circuit
  1549. * creation and rotation and so on can actually be tested without limit. */
  1550. #define MAX_INTRO_POINT_CIRCUIT_RETRIES_TESTING -1
  1551. if (get_options()->TestingTorNetwork) {
  1552. return MAX_INTRO_POINT_CIRCUIT_RETRIES_TESTING;
  1553. }
  1554. num_wanted_ip = service->config.num_intro_points;
  1555. /* The calculation is as follow. We have a number of intro points that we
  1556. * want configured as a torrc option (num_intro_points). We then add an
  1557. * extra value so we can launch multiple circuits at once and pick the
  1558. * quickest ones. For instance, we want 3 intros, we add 2 extra so we'll
  1559. * pick 5 intros and launch 5 circuits. */
  1560. count += (num_wanted_ip + get_intro_point_num_extra());
  1561. /* Then we add the number of retries that is possible to do for each intro
  1562. * point. If we want 3 intros, we'll allow 3 times the number of possible
  1563. * retry. */
  1564. count += (num_wanted_ip * MAX_INTRO_POINT_CIRCUIT_RETRIES);
  1565. /* Then, we multiply by a factor of 2 if we have both descriptor or 0 if we
  1566. * have none. */
  1567. multiplier += (service->desc_current) ? 1 : 0;
  1568. multiplier += (service->desc_next) ? 1 : 0;
  1569. return (count * multiplier);
  1570. }
  1571. /* For the given service, return 1 if the service is allowed to launch more
  1572. * introduction circuits else 0 if the maximum has been reached for the retry
  1573. * period of INTRO_CIRC_RETRY_PERIOD. */
  1574. STATIC int
  1575. can_service_launch_intro_circuit(hs_service_t *service, time_t now)
  1576. {
  1577. tor_assert(service);
  1578. /* Consider the intro circuit retry period of the service. */
  1579. if (now > (service->state.intro_circ_retry_started_time +
  1580. INTRO_CIRC_RETRY_PERIOD)) {
  1581. service->state.intro_circ_retry_started_time = now;
  1582. service->state.num_intro_circ_launched = 0;
  1583. goto allow;
  1584. }
  1585. /* Check if we can still launch more circuits in this period. */
  1586. if (service->state.num_intro_circ_launched <=
  1587. get_max_intro_circ_per_period(service)) {
  1588. goto allow;
  1589. }
  1590. /* Rate limit log that we've reached our circuit creation limit. */
  1591. {
  1592. char *msg;
  1593. time_t elapsed_time = now - service->state.intro_circ_retry_started_time;
  1594. static ratelim_t rlimit = RATELIM_INIT(INTRO_CIRC_RETRY_PERIOD);
  1595. if ((msg = rate_limit_log(&rlimit, now))) {
  1596. log_info(LD_REND, "Hidden service %s exceeded its circuit launch limit "
  1597. "of %u per %d seconds. It launched %u circuits in "
  1598. "the last %ld seconds. Will retry in %ld seconds.",
  1599. safe_str_client(service->onion_address),
  1600. get_max_intro_circ_per_period(service),
  1601. INTRO_CIRC_RETRY_PERIOD,
  1602. service->state.num_intro_circ_launched, elapsed_time,
  1603. INTRO_CIRC_RETRY_PERIOD - elapsed_time);
  1604. tor_free(msg);
  1605. }
  1606. }
  1607. /* Not allow. */
  1608. return 0;
  1609. allow:
  1610. return 1;
  1611. }
  1612. /* Scheduled event run from the main loop. Make sure we have all the circuits
  1613. * we need for each service. */
  1614. static void
  1615. run_build_circuit_event(time_t now)
  1616. {
  1617. /* Make sure we can actually have enough information or able to build
  1618. * internal circuits as required by services. */
  1619. if (router_have_consensus_path() == CONSENSUS_PATH_UNKNOWN ||
  1620. !have_completed_a_circuit()) {
  1621. return;
  1622. }
  1623. /* Run v2 check. */
  1624. if (rend_num_services() > 0) {
  1625. rend_consider_services_intro_points(now);
  1626. }
  1627. /* Run v3+ check. */
  1628. FOR_EACH_SERVICE_BEGIN(service) {
  1629. /* For introduction circuit, we need to make sure we don't stress too much
  1630. * circuit creation so make sure this service is respecting that limit. */
  1631. if (can_service_launch_intro_circuit(service, now)) {
  1632. /* Launch intro point circuits if needed. */
  1633. launch_intro_point_circuits(service, now);
  1634. /* Once the circuits have opened, we'll make sure to update the
  1635. * descriptor intro point list and cleanup any extraneous. */
  1636. }
  1637. } FOR_EACH_SERVICE_END;
  1638. }
  1639. /* Encode and sign the service descriptor desc and upload it to the given
  1640. * hidden service directory. This does nothing if PublishHidServDescriptors
  1641. * is false. */
  1642. static void
  1643. upload_descriptor_to_hsdir(const hs_service_t *service,
  1644. hs_service_descriptor_t *desc, const node_t *hsdir)
  1645. {
  1646. char version_str[4] = {0}, *encoded_desc = NULL;
  1647. directory_request_t *dir_req;
  1648. hs_ident_dir_conn_t ident;
  1649. tor_assert(service);
  1650. tor_assert(desc);
  1651. tor_assert(hsdir);
  1652. memset(&ident, 0, sizeof(ident));
  1653. /* Let's avoid doing that if tor is configured to not publish. */
  1654. if (!get_options()->PublishHidServDescriptors) {
  1655. log_info(LD_REND, "Service %s not publishing descriptor. "
  1656. "PublishHidServDescriptors is set to 1.",
  1657. safe_str_client(service->onion_address));
  1658. goto end;
  1659. }
  1660. /* First of all, we'll encode the descriptor. This should NEVER fail but
  1661. * just in case, let's make sure we have an actual usable descriptor. */
  1662. if (BUG(hs_desc_encode_descriptor(desc->desc, &desc->signing_kp,
  1663. &encoded_desc) < 0)) {
  1664. goto end;
  1665. }
  1666. /* Setup the connection identifier. */
  1667. ed25519_pubkey_copy(&ident.identity_pk, &service->keys.identity_pk);
  1668. /* This is our resource when uploading which is used to construct the URL
  1669. * with the version number: "/tor/hs/<version>/publish". */
  1670. tor_snprintf(version_str, sizeof(version_str), "%u",
  1671. service->config.version);
  1672. /* Build the directory request for this HSDir. */
  1673. dir_req = directory_request_new(DIR_PURPOSE_UPLOAD_HSDESC);
  1674. directory_request_set_routerstatus(dir_req, hsdir->rs);
  1675. directory_request_set_indirection(dir_req, DIRIND_ANONYMOUS);
  1676. directory_request_set_resource(dir_req, version_str);
  1677. directory_request_set_payload(dir_req, encoded_desc,
  1678. strlen(encoded_desc));
  1679. /* The ident object is copied over the directory connection object once
  1680. * the directory request is initiated. */
  1681. directory_request_set_hs_ident(dir_req, &ident);
  1682. /* Initiate the directory request to the hsdir.*/
  1683. directory_initiate_request(dir_req);
  1684. directory_request_free(dir_req);
  1685. /* Logging so we know where it was sent. */
  1686. {
  1687. int is_next_desc = (service->desc_next == desc);
  1688. const uint8_t *index = (is_next_desc) ? hsdir->hsdir_index->next :
  1689. hsdir->hsdir_index->current;
  1690. log_info(LD_REND, "Service %s %s descriptor of revision %" PRIu64
  1691. " initiated upload request to %s with index %s",
  1692. safe_str_client(service->onion_address),
  1693. (is_next_desc) ? "next" : "current",
  1694. desc->desc->plaintext_data.revision_counter,
  1695. safe_str_client(node_describe(hsdir)),
  1696. safe_str_client(hex_str((const char *) index, 32)));
  1697. }
  1698. /* XXX: Inform control port of the upload event (#20699). */
  1699. end:
  1700. tor_free(encoded_desc);
  1701. return;
  1702. }
  1703. /* Encode and sign the service descriptor desc and upload it to the
  1704. * responsible hidden service directories. If for_next_period is true, the set
  1705. * of directories are selected using the next hsdir_index. This does nothing
  1706. * if PublishHidServDescriptors is false. */
  1707. static void
  1708. upload_descriptor_to_all(const hs_service_t *service,
  1709. hs_service_descriptor_t *desc, int for_next_period)
  1710. {
  1711. smartlist_t *responsible_dirs = NULL;
  1712. tor_assert(service);
  1713. tor_assert(desc);
  1714. /* Get our list of responsible HSDir. */
  1715. responsible_dirs = smartlist_new();
  1716. /* The parameter 0 means that we aren't a client so tell the function to use
  1717. * the spread store consensus paremeter. */
  1718. hs_get_responsible_hsdirs(&desc->blinded_kp.pubkey, desc->time_period_num,
  1719. for_next_period, 0, responsible_dirs);
  1720. /* For each responsible HSDir we have, initiate an upload command. */
  1721. SMARTLIST_FOREACH_BEGIN(responsible_dirs, const routerstatus_t *,
  1722. hsdir_rs) {
  1723. const node_t *hsdir_node = node_get_by_id(hsdir_rs->identity_digest);
  1724. /* Getting responsible hsdir implies that the node_t object exists for the
  1725. * routerstatus_t found in the consensus else we have a problem. */
  1726. tor_assert(hsdir_node);
  1727. /* Do not upload to an HSDir we don't have a descriptor for. */
  1728. if (!node_has_descriptor(hsdir_node)) {
  1729. log_info(LD_REND, "Missing descriptor for HSDir %s. Not uploading "
  1730. "descriptor. We'll try later once we have it.",
  1731. safe_str_client(node_describe(hsdir_node)));
  1732. /* Once we get new directory information, this HSDir will be retried if
  1733. * we ever get the descriptor. */
  1734. smartlist_add(desc->hsdir_missing_info,
  1735. tor_memdup(hsdir_rs->identity_digest, DIGEST_LEN));
  1736. continue;
  1737. }
  1738. /* Upload this descriptor to the chosen directory. */
  1739. upload_descriptor_to_hsdir(service, desc, hsdir_node);
  1740. } SMARTLIST_FOREACH_END(hsdir_rs);
  1741. /* Set the next upload time for this descriptor. Even if we are configured
  1742. * to not upload, we still want to follow the right cycle of life for this
  1743. * descriptor. */
  1744. desc->next_upload_time =
  1745. (time(NULL) + crypto_rand_int_range(HS_SERVICE_NEXT_UPLOAD_TIME_MIN,
  1746. HS_SERVICE_NEXT_UPLOAD_TIME_MAX));
  1747. {
  1748. char fmt_next_time[ISO_TIME_LEN+1];
  1749. format_local_iso_time(fmt_next_time, desc->next_upload_time);
  1750. log_debug(LD_REND, "Service %s set to upload a descriptor at %s",
  1751. safe_str_client(service->onion_address), fmt_next_time);
  1752. }
  1753. /* Increment the revision counter so the next update (which will trigger an
  1754. * upload) will have the right value. We do this at this stage to only do it
  1755. * once because a descriptor can have many updates before being uploaded. By
  1756. * doing it at upload, we are sure to only increment by 1 and thus avoid
  1757. * leaking how many operations we made on the descriptor from the previous
  1758. * one before uploading. */
  1759. desc->desc->plaintext_data.revision_counter += 1;
  1760. smartlist_free(responsible_dirs);
  1761. return;
  1762. }
  1763. /* Return 1 if the given descriptor from the given service can be uploaded
  1764. * else return 0 if it can not. */
  1765. static int
  1766. should_service_upload_descriptor(const hs_service_t *service,
  1767. const hs_service_descriptor_t *desc, time_t now)
  1768. {
  1769. unsigned int num_intro_points;
  1770. tor_assert(service);
  1771. tor_assert(desc);
  1772. /* If this descriptors has missing intro points that is that it couldn't get
  1773. * them all when it was time to pick them, it means that we should upload
  1774. * instead of waiting an arbitrary amount of time breaking the service.
  1775. * Else, if we have no missing intro points, we use the value taken from the
  1776. * service configuration. */
  1777. (desc->missing_intro_points) ?
  1778. (num_intro_points = digest256map_size(desc->intro_points.map)) :
  1779. (num_intro_points = service->config.num_intro_points);
  1780. /* This means we tried to pick intro points but couldn't get any so do not
  1781. * upload descriptor in this case. We need at least one for the service to
  1782. * be reachable. */
  1783. if (desc->missing_intro_points && num_intro_points == 0) {
  1784. goto cannot;
  1785. }
  1786. /* Check if all our introduction circuit have been established for all the
  1787. * intro points we have selected. */
  1788. if (count_desc_circuit_established(desc) != num_intro_points) {
  1789. goto cannot;
  1790. }
  1791. /* Is it the right time to upload? */
  1792. if (desc->next_upload_time > now) {
  1793. goto cannot;
  1794. }
  1795. /* Can upload! */
  1796. return 1;
  1797. cannot:
  1798. return 0;
  1799. }
  1800. /* Scheduled event run from the main loop. Try to upload the descriptor for
  1801. * each service. */
  1802. STATIC void
  1803. run_upload_descriptor_event(time_t now)
  1804. {
  1805. /* v2 services use the same function for descriptor creation and upload so
  1806. * we do everything here because the intro circuits were checked before. */
  1807. if (rend_num_services() > 0) {
  1808. rend_consider_services_upload(now);
  1809. rend_consider_descriptor_republication();
  1810. }
  1811. /* Run v3+ check. */
  1812. FOR_EACH_SERVICE_BEGIN(service) {
  1813. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  1814. int for_next_period = 0;
  1815. /* Can this descriptor be uploaed? */
  1816. if (!should_service_upload_descriptor(service, desc, now)) {
  1817. continue;
  1818. }
  1819. log_info(LD_REND, "Initiating upload for hidden service %s descriptor "
  1820. "for service %s with %u/%u introduction points%s.",
  1821. (desc == service->desc_current) ? "current" : "next",
  1822. safe_str_client(service->onion_address),
  1823. digest256map_size(desc->intro_points.map),
  1824. service->config.num_intro_points,
  1825. (desc->missing_intro_points) ? " (couldn't pick more)" : "");
  1826. /* At this point, we have to upload the descriptor so start by building
  1827. * the intro points descriptor section which we are now sure to be
  1828. * accurate because all circuits have been established. */
  1829. build_desc_intro_points(service, desc, now);
  1830. /* If the service is in the overlap period and this descriptor is the
  1831. * next one, it has to be uploaded for the next time period meaning
  1832. * we'll use the next node_t hsdir_index to pick the HSDirs. */
  1833. if (desc == service->desc_next) {
  1834. for_next_period = 1;
  1835. }
  1836. upload_descriptor_to_all(service, desc, for_next_period);
  1837. } FOR_EACH_DESCRIPTOR_END;
  1838. } FOR_EACH_SERVICE_END;
  1839. }
  1840. /* Called when the introduction point circuit is done building and ready to be
  1841. * used. */
  1842. static void
  1843. service_intro_circ_has_opened(origin_circuit_t *circ)
  1844. {
  1845. hs_service_t *service = NULL;
  1846. hs_service_intro_point_t *ip = NULL;
  1847. hs_service_descriptor_t *desc = NULL;
  1848. tor_assert(circ);
  1849. tor_assert(circ->cpath);
  1850. /* Getting here means this is a v3 intro circuit. */
  1851. tor_assert(circ->hs_ident);
  1852. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  1853. /* Get the corresponding service and intro point. */
  1854. get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
  1855. if (service == NULL) {
  1856. log_warn(LD_REND, "Unknown service identity key %s on the introduction "
  1857. "circuit %u. Can't find onion service.",
  1858. safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
  1859. TO_CIRCUIT(circ)->n_circ_id);
  1860. goto err;
  1861. }
  1862. if (ip == NULL) {
  1863. log_warn(LD_REND, "Unknown introduction point auth key on circuit %u "
  1864. "for service %s",
  1865. TO_CIRCUIT(circ)->n_circ_id,
  1866. safe_str_client(service->onion_address));
  1867. goto err;
  1868. }
  1869. /* We can't have an IP object without a descriptor. */
  1870. tor_assert(desc);
  1871. if (hs_circ_service_intro_has_opened(service, ip, desc, circ)) {
  1872. /* Getting here means that the circuit has been re-purposed because we
  1873. * have enough intro circuit opened. Remove the IP from the service. */
  1874. service_intro_point_remove(service, ip);
  1875. service_intro_point_free(ip);
  1876. }
  1877. goto done;
  1878. err:
  1879. /* Close circuit, we can't use it. */
  1880. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOSUCHSERVICE);
  1881. done:
  1882. return;
  1883. }
  1884. /* Called when a rendezvous circuit is done building and ready to be used. */
  1885. static void
  1886. service_rendezvous_circ_has_opened(origin_circuit_t *circ)
  1887. {
  1888. hs_service_t *service = NULL;
  1889. tor_assert(circ);
  1890. tor_assert(circ->cpath);
  1891. /* Getting here means this is a v3 intro circuit. */
  1892. tor_assert(circ->hs_ident);
  1893. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  1894. /* Declare the circuit dirty to avoid reuse, and for path-bias */
  1895. if (!TO_CIRCUIT(circ)->timestamp_dirty)
  1896. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  1897. pathbias_count_use_attempt(circ);
  1898. /* Get the corresponding service and intro point. */
  1899. get_objects_from_ident(circ->hs_ident, &service, NULL, NULL);
  1900. if (service == NULL) {
  1901. log_warn(LD_REND, "Unknown service identity key %s on the rendezvous "
  1902. "circuit %u with cookie %s. Can't find onion service.",
  1903. safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
  1904. TO_CIRCUIT(circ)->n_circ_id,
  1905. hex_str((const char *) circ->hs_ident->rendezvous_cookie,
  1906. REND_COOKIE_LEN));
  1907. goto err;
  1908. }
  1909. /* If the cell can't be sent, the circuit will be closed within this
  1910. * function. */
  1911. hs_circ_service_rp_has_opened(service, circ);
  1912. goto done;
  1913. err:
  1914. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOSUCHSERVICE);
  1915. done:
  1916. return;
  1917. }
  1918. /* Handle an INTRO_ESTABLISHED cell arriving on the given introduction
  1919. * circuit. Return 0 on success else a negative value. */
  1920. static int
  1921. service_handle_intro_established(origin_circuit_t *circ,
  1922. const uint8_t *payload,
  1923. size_t payload_len)
  1924. {
  1925. hs_service_t *service = NULL;
  1926. hs_service_intro_point_t *ip = NULL;
  1927. tor_assert(circ);
  1928. tor_assert(payload);
  1929. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  1930. /* We need the service and intro point for this cell. */
  1931. get_objects_from_ident(circ->hs_ident, &service, &ip, NULL);
  1932. /* Get service object from the circuit identifier. */
  1933. if (service == NULL) {
  1934. log_warn(LD_REND, "Unknown service identity key %s on the introduction "
  1935. "circuit %u. Can't find onion service.",
  1936. safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
  1937. TO_CIRCUIT(circ)->n_circ_id);
  1938. goto err;
  1939. }
  1940. if (ip == NULL) {
  1941. /* We don't recognize the key. */
  1942. log_warn(LD_REND, "Introduction circuit established without an intro "
  1943. "point object on circuit %u for service %s",
  1944. TO_CIRCUIT(circ)->n_circ_id,
  1945. safe_str_client(service->onion_address));
  1946. goto err;
  1947. }
  1948. /* Try to parse the payload into a cell making sure we do actually have a
  1949. * valid cell. On success, the ip object is updated. */
  1950. if (hs_circ_handle_intro_established(service, ip, circ, payload,
  1951. payload_len) < 0) {
  1952. goto err;
  1953. }
  1954. /* Flag that we have an established circuit for this intro point. This value
  1955. * is what indicates the upload scheduled event if we are ready to build the
  1956. * intro point into the descriptor and upload. */
  1957. ip->circuit_established = 1;
  1958. log_info(LD_REND, "Successfully received an INTRO_ESTABLISHED cell "
  1959. "on circuit %u for service %s",
  1960. TO_CIRCUIT(circ)->n_circ_id,
  1961. safe_str_client(service->onion_address));
  1962. return 0;
  1963. err:
  1964. return -1;
  1965. }
  1966. /* Handle an INTRODUCE2 cell arriving on the given introduction circuit.
  1967. * Return 0 on success else a negative value. */
  1968. static int
  1969. service_handle_introduce2(origin_circuit_t *circ, const uint8_t *payload,
  1970. size_t payload_len)
  1971. {
  1972. hs_service_t *service = NULL;
  1973. hs_service_intro_point_t *ip = NULL;
  1974. hs_service_descriptor_t *desc = NULL;
  1975. tor_assert(circ);
  1976. tor_assert(payload);
  1977. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_INTRO);
  1978. /* We'll need every object associated with this circuit. */
  1979. get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
  1980. /* Get service object from the circuit identifier. */
  1981. if (service == NULL) {
  1982. log_warn(LD_BUG, "Unknown service identity key %s when handling "
  1983. "an INTRODUCE2 cell on circuit %u",
  1984. safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
  1985. TO_CIRCUIT(circ)->n_circ_id);
  1986. goto err;
  1987. }
  1988. if (ip == NULL) {
  1989. /* We don't recognize the key. */
  1990. log_warn(LD_BUG, "Unknown introduction auth key when handling "
  1991. "an INTRODUCE2 cell on circuit %u for service %s",
  1992. TO_CIRCUIT(circ)->n_circ_id,
  1993. safe_str_client(service->onion_address));
  1994. goto err;
  1995. }
  1996. /* If we have an IP object, we MUST have a descriptor object. */
  1997. tor_assert(desc);
  1998. /* The following will parse, decode and launch the rendezvous point circuit.
  1999. * Both current and legacy cells are handled. */
  2000. if (hs_circ_handle_introduce2(service, circ, ip, desc->desc->subcredential,
  2001. payload, payload_len) < 0) {
  2002. goto err;
  2003. }
  2004. return 0;
  2005. err:
  2006. return -1;
  2007. }
  2008. /* For a given service and a descriptor of that service, consider retrying to
  2009. * upload the descriptor to any directories from which we had missing
  2010. * information when originally tried to be uploaded. This is called when our
  2011. * directory information has changed. */
  2012. static void
  2013. consider_hsdir_retry(const hs_service_t *service,
  2014. hs_service_descriptor_t *desc)
  2015. {
  2016. smartlist_t *responsible_dirs = NULL;
  2017. smartlist_t *still_missing_dirs = NULL;
  2018. tor_assert(service);
  2019. tor_assert(desc);
  2020. responsible_dirs = smartlist_new();
  2021. still_missing_dirs = smartlist_new();
  2022. /* We first need to get responsible directories from the latest consensus so
  2023. * we can then make sure that the node that we were missing information for
  2024. * is still responsible for this descriptor. */
  2025. hs_get_responsible_hsdirs(&desc->blinded_kp.pubkey, desc->time_period_num,
  2026. service->desc_next == desc, 0, responsible_dirs);
  2027. SMARTLIST_FOREACH_BEGIN(responsible_dirs, const routerstatus_t *, rs) {
  2028. const node_t *node;
  2029. const char *id = rs->identity_digest;
  2030. if (!smartlist_contains_digest(desc->hsdir_missing_info, id)) {
  2031. continue;
  2032. }
  2033. /* We do need a node_t object and descriptor to perform an upload. If
  2034. * found, we remove the id from the missing dir list else we add it to the
  2035. * still missing dir list to keep track of id that are still missing. */
  2036. node = node_get_by_id(id);
  2037. if (node && node_has_descriptor(node)) {
  2038. upload_descriptor_to_hsdir(service, desc, node);
  2039. smartlist_remove(desc->hsdir_missing_info, id);
  2040. } else {
  2041. smartlist_add(still_missing_dirs, tor_memdup(id, DIGEST_LEN));
  2042. }
  2043. } SMARTLIST_FOREACH_END(rs);
  2044. /* Switch the still missing dir list with the current missing dir list in
  2045. * the descriptor. It is possible that the list ends up empty which is what
  2046. * we want if we have no more missing dir. */
  2047. SMARTLIST_FOREACH(desc->hsdir_missing_info, char *, id, tor_free(id));
  2048. smartlist_free(desc->hsdir_missing_info);
  2049. desc->hsdir_missing_info = still_missing_dirs;
  2050. /* No ownership of the routerstatus_t object in this list. */
  2051. smartlist_free(responsible_dirs);
  2052. }
  2053. /* Add to list every filename used by service. This is used by the sandbox
  2054. * subsystem. */
  2055. static void
  2056. service_add_fnames_to_list(const hs_service_t *service, smartlist_t *list)
  2057. {
  2058. const char *s_dir;
  2059. char fname[128] = {0};
  2060. tor_assert(service);
  2061. tor_assert(list);
  2062. /* Ease our life. */
  2063. s_dir = service->config.directory_path;
  2064. /* The hostname file. */
  2065. smartlist_add(list, hs_path_from_filename(s_dir, fname_hostname));
  2066. /* The key files splitted in two. */
  2067. tor_snprintf(fname, sizeof(fname), "%s_secret_key", fname_keyfile_prefix);
  2068. smartlist_add(list, hs_path_from_filename(s_dir, fname));
  2069. tor_snprintf(fname, sizeof(fname), "%s_public_key", fname_keyfile_prefix);
  2070. smartlist_add(list, hs_path_from_filename(s_dir, fname));
  2071. }
  2072. /* ========== */
  2073. /* Public API */
  2074. /* ========== */
  2075. /* Return the number of service we have configured and usable. */
  2076. unsigned int
  2077. hs_service_get_num_services(void)
  2078. {
  2079. if (hs_service_map == NULL) {
  2080. return 0;
  2081. }
  2082. return HT_SIZE(hs_service_map);
  2083. }
  2084. /* Called once an introduction circuit is closed. If the circuit doesn't have
  2085. * a v3 identifier, it is ignored. */
  2086. void
  2087. hs_service_intro_circ_has_closed(origin_circuit_t *circ)
  2088. {
  2089. hs_service_t *service = NULL;
  2090. hs_service_intro_point_t *ip = NULL;
  2091. hs_service_descriptor_t *desc = NULL;
  2092. tor_assert(circ);
  2093. if (circ->hs_ident == NULL) {
  2094. /* This is not a v3 circuit, ignore. */
  2095. goto end;
  2096. }
  2097. get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
  2098. if (service == NULL) {
  2099. log_warn(LD_REND, "Unable to find any hidden service associated "
  2100. "identity key %s on intro circuit %u.",
  2101. ed25519_fmt(&circ->hs_ident->identity_pk),
  2102. TO_CIRCUIT(circ)->n_circ_id);
  2103. goto end;
  2104. }
  2105. if (ip == NULL) {
  2106. /* The introduction point object has already been removed probably by our
  2107. * cleanup process so ignore. */
  2108. goto end;
  2109. }
  2110. /* Can't have an intro point object without a descriptor. */
  2111. tor_assert(desc);
  2112. /* Circuit disappeared so make sure the intro point is updated. By
  2113. * keeping the object in the descriptor, we'll be able to retry. */
  2114. ip->circuit_established = 0;
  2115. /* We've retried too many times, remember it as a failed intro point so we
  2116. * don't pick it up again. It will be retried in INTRO_CIRC_RETRY_PERIOD
  2117. * seconds. */
  2118. if (ip->circuit_retries > MAX_INTRO_POINT_CIRCUIT_RETRIES) {
  2119. remember_failing_intro_point(ip, desc, approx_time());
  2120. service_intro_point_remove(service, ip);
  2121. service_intro_point_free(ip);
  2122. }
  2123. end:
  2124. return;
  2125. }
  2126. /* Given conn, a rendezvous edge connection acting as an exit stream, look up
  2127. * the hidden service for the circuit circ, and look up the port and address
  2128. * based on the connection port. Assign the actual connection address.
  2129. *
  2130. * Return 0 on success. Return -1 on failure and the caller should NOT close
  2131. * the circuit. Return -2 on failure and the caller MUST close the circuit for
  2132. * security reasons. */
  2133. int
  2134. hs_service_set_conn_addr_port(const origin_circuit_t *circ,
  2135. edge_connection_t *conn)
  2136. {
  2137. hs_service_t *service = NULL;
  2138. tor_assert(circ);
  2139. tor_assert(conn);
  2140. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  2141. tor_assert(circ->hs_ident);
  2142. get_objects_from_ident(circ->hs_ident, &service, NULL, NULL);
  2143. if (service == NULL) {
  2144. log_warn(LD_REND, "Unable to find any hidden service associated "
  2145. "identity key %s on rendezvous circuit %u.",
  2146. ed25519_fmt(&circ->hs_ident->identity_pk),
  2147. TO_CIRCUIT(circ)->n_circ_id);
  2148. /* We want the caller to close the circuit because it's not a valid
  2149. * service so no danger. Attempting to bruteforce the entire key space by
  2150. * opening circuits to learn which service is being hosted here is
  2151. * impractical. */
  2152. goto err_close;
  2153. }
  2154. /* Enforce the streams-per-circuit limit, and refuse to provide a mapping if
  2155. * this circuit will exceed the limit. */
  2156. if (service->config.max_streams_per_rdv_circuit > 0 &&
  2157. (circ->hs_ident->num_rdv_streams >=
  2158. service->config.max_streams_per_rdv_circuit)) {
  2159. #define MAX_STREAM_WARN_INTERVAL 600
  2160. static struct ratelim_t stream_ratelim =
  2161. RATELIM_INIT(MAX_STREAM_WARN_INTERVAL);
  2162. log_fn_ratelim(&stream_ratelim, LOG_WARN, LD_REND,
  2163. "Maximum streams per circuit limit reached on "
  2164. "rendezvous circuit %u for service %s. Circuit has "
  2165. "%" PRIu64 " out of %" PRIu64 " streams. %s.",
  2166. TO_CIRCUIT(circ)->n_circ_id,
  2167. service->onion_address,
  2168. circ->hs_ident->num_rdv_streams,
  2169. service->config.max_streams_per_rdv_circuit,
  2170. service->config.max_streams_close_circuit ?
  2171. "Closing circuit" : "Ignoring open stream request");
  2172. if (service->config.max_streams_close_circuit) {
  2173. /* Service explicitly configured to close immediately. */
  2174. goto err_close;
  2175. }
  2176. /* Exceeding the limit makes tor silently ignore the stream creation
  2177. * request and keep the circuit open. */
  2178. goto err_no_close;
  2179. }
  2180. /* Find a virtual port of that service mathcing the one in the connection if
  2181. * succesful, set the address in the connection. */
  2182. if (hs_set_conn_addr_port(service->config.ports, conn) < 0) {
  2183. log_info(LD_REND, "No virtual port mapping exists for port %d for "
  2184. "hidden service %s.",
  2185. TO_CONN(conn)->port, service->onion_address);
  2186. if (service->config.allow_unknown_ports) {
  2187. /* Service explicitly allow connection to unknown ports so close right
  2188. * away because we do not care about port mapping. */
  2189. goto err_close;
  2190. }
  2191. /* If the service didn't explicitly allow it, we do NOT close the circuit
  2192. * here to raise the bar in terms of performance for port mapping. */
  2193. goto err_no_close;
  2194. }
  2195. /* Success. */
  2196. return 0;
  2197. err_close:
  2198. /* Indicate the caller that the circuit should be closed. */
  2199. return -2;
  2200. err_no_close:
  2201. /* Indicate the caller to NOT close the circuit. */
  2202. return -1;
  2203. }
  2204. /* Add to file_list every filename used by a configured hidden service, and to
  2205. * dir_list every directory path used by a configured hidden service. This is
  2206. * used by the sandbox subsystem to whitelist those. */
  2207. void
  2208. hs_service_lists_fnames_for_sandbox(smartlist_t *file_list,
  2209. smartlist_t *dir_list)
  2210. {
  2211. tor_assert(file_list);
  2212. tor_assert(dir_list);
  2213. /* Add files and dirs for legacy services. */
  2214. rend_services_add_filenames_to_lists(file_list, dir_list);
  2215. /* Add files and dirs for v3+. */
  2216. FOR_EACH_SERVICE_BEGIN(service) {
  2217. /* Skip ephemeral service, they don't touch the disk. */
  2218. if (service->config.is_ephemeral) {
  2219. continue;
  2220. }
  2221. service_add_fnames_to_list(service, file_list);
  2222. smartlist_add_strdup(dir_list, service->config.directory_path);
  2223. } FOR_EACH_DESCRIPTOR_END;
  2224. }
  2225. /* Called when our internal view of the directory has changed. We might have
  2226. * new descriptors for hidden service directories that we didn't have before
  2227. * so try them if it's the case. */
  2228. void
  2229. hs_service_dir_info_changed(void)
  2230. {
  2231. /* For each service we have, check every descriptor and consider retrying to
  2232. * upload it to directories that we might have had missing information
  2233. * previously that is missing a router descriptor. */
  2234. FOR_EACH_SERVICE_BEGIN(service) {
  2235. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  2236. /* This cleans up the descriptor missing hsdir information list if a
  2237. * successful upload is made or if any of the directory aren't
  2238. * responsible anymore for the service descriptor. */
  2239. consider_hsdir_retry(service, desc);
  2240. } FOR_EACH_DESCRIPTOR_END;
  2241. } FOR_EACH_SERVICE_END;
  2242. }
  2243. /* Called when we get an INTRODUCE2 cell on the circ. Respond to the cell and
  2244. * launch a circuit to the rendezvous point. */
  2245. int
  2246. hs_service_receive_introduce2(origin_circuit_t *circ, const uint8_t *payload,
  2247. size_t payload_len)
  2248. {
  2249. int ret = -1;
  2250. tor_assert(circ);
  2251. tor_assert(payload);
  2252. /* Do some initial validation and logging before we parse the cell */
  2253. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_INTRO) {
  2254. log_warn(LD_PROTOCOL, "Received an INTRODUCE2 cell on a "
  2255. "non introduction circuit of purpose %d",
  2256. TO_CIRCUIT(circ)->purpose);
  2257. goto done;
  2258. }
  2259. ret = (circ->hs_ident) ? service_handle_introduce2(circ, payload,
  2260. payload_len) :
  2261. rend_service_receive_introduction(circ, payload,
  2262. payload_len);
  2263. done:
  2264. return ret;
  2265. }
  2266. /* Called when we get an INTRO_ESTABLISHED cell. Mark the circuit as an
  2267. * established introduction point. Return 0 on success else a negative value
  2268. * and the circuit is closed. */
  2269. int
  2270. hs_service_receive_intro_established(origin_circuit_t *circ,
  2271. const uint8_t *payload,
  2272. size_t payload_len)
  2273. {
  2274. int ret = -1;
  2275. tor_assert(circ);
  2276. tor_assert(payload);
  2277. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  2278. log_warn(LD_PROTOCOL, "Received an INTRO_ESTABLISHED cell on a "
  2279. "non introduction circuit of purpose %d",
  2280. TO_CIRCUIT(circ)->purpose);
  2281. goto err;
  2282. }
  2283. /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
  2284. * identifier hs_ident. Can't be both. */
  2285. ret = (circ->hs_ident) ? service_handle_intro_established(circ, payload,
  2286. payload_len) :
  2287. rend_service_intro_established(circ, payload,
  2288. payload_len);
  2289. if (ret < 0) {
  2290. goto err;
  2291. }
  2292. return 0;
  2293. err:
  2294. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  2295. return -1;
  2296. }
  2297. /* Called when any kind of hidden service circuit is done building thus
  2298. * opened. This is the entry point from the circuit subsystem. */
  2299. void
  2300. hs_service_circuit_has_opened(origin_circuit_t *circ)
  2301. {
  2302. tor_assert(circ);
  2303. /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
  2304. * identifier hs_ident. Can't be both. */
  2305. switch (TO_CIRCUIT(circ)->purpose) {
  2306. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  2307. (circ->hs_ident) ? service_intro_circ_has_opened(circ) :
  2308. rend_service_intro_has_opened(circ);
  2309. break;
  2310. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  2311. (circ->hs_ident) ? service_rendezvous_circ_has_opened(circ) :
  2312. rend_service_rendezvous_has_opened(circ);
  2313. break;
  2314. default:
  2315. tor_assert(0);
  2316. }
  2317. }
  2318. /* Load and/or generate keys for all onion services including the client
  2319. * authorization if any. Return 0 on success, -1 on failure. */
  2320. int
  2321. hs_service_load_all_keys(void)
  2322. {
  2323. /* Load v2 service keys if we have v2. */
  2324. if (rend_num_services() != 0) {
  2325. if (rend_service_load_all_keys(NULL) < 0) {
  2326. goto err;
  2327. }
  2328. }
  2329. /* Load or/and generate them for v3+. */
  2330. SMARTLIST_FOREACH_BEGIN(hs_service_staging_list, hs_service_t *, service) {
  2331. /* Ignore ephemeral service, they already have their keys set. */
  2332. if (service->config.is_ephemeral) {
  2333. continue;
  2334. }
  2335. log_info(LD_REND, "Loading v3 onion service keys from %s",
  2336. service_escaped_dir(service));
  2337. if (load_service_keys(service) < 0) {
  2338. goto err;
  2339. }
  2340. /* XXX: Load/Generate client authorization keys. (#20700) */
  2341. } SMARTLIST_FOREACH_END(service);
  2342. /* Final step, the staging list contains service in a quiescent state that
  2343. * is ready to be used. Register them to the global map. Once this is over,
  2344. * the staging list will be cleaned up. */
  2345. register_all_services();
  2346. /* All keys have been loaded successfully. */
  2347. return 0;
  2348. err:
  2349. return -1;
  2350. }
  2351. /* Put all service object in the given service list. After this, the caller
  2352. * looses ownership of every elements in the list and responsible to free the
  2353. * list pointer. */
  2354. void
  2355. hs_service_stage_services(const smartlist_t *service_list)
  2356. {
  2357. tor_assert(service_list);
  2358. /* This list is freed at registration time but this function can be called
  2359. * multiple time. */
  2360. if (hs_service_staging_list == NULL) {
  2361. hs_service_staging_list = smartlist_new();
  2362. }
  2363. /* Add all service object to our staging list. Caller is responsible for
  2364. * freeing the service_list. */
  2365. smartlist_add_all(hs_service_staging_list, service_list);
  2366. }
  2367. /* Allocate and initilize a service object. The service configuration will
  2368. * contain the default values. Return the newly allocated object pointer. This
  2369. * function can't fail. */
  2370. hs_service_t *
  2371. hs_service_new(const or_options_t *options)
  2372. {
  2373. hs_service_t *service = tor_malloc_zero(sizeof(hs_service_t));
  2374. /* Set default configuration value. */
  2375. set_service_default_config(&service->config, options);
  2376. /* Set the default service version. */
  2377. service->config.version = HS_SERVICE_DEFAULT_VERSION;
  2378. /* Allocate the CLIENT_PK replay cache in service state. */
  2379. service->state.replay_cache_rend_cookie =
  2380. replaycache_new(REND_REPLAY_TIME_INTERVAL, REND_REPLAY_TIME_INTERVAL);
  2381. return service;
  2382. }
  2383. /* Free the given <b>service</b> object and all its content. This function
  2384. * also takes care of wiping service keys from memory. It is safe to pass a
  2385. * NULL pointer. */
  2386. void
  2387. hs_service_free(hs_service_t *service)
  2388. {
  2389. if (service == NULL) {
  2390. return;
  2391. }
  2392. /* Free descriptors. Go over both descriptor with this loop. */
  2393. FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
  2394. service_descriptor_free(desc);
  2395. } FOR_EACH_DESCRIPTOR_END;
  2396. /* Free service configuration. */
  2397. service_clear_config(&service->config);
  2398. /* Free replay cache from state. */
  2399. if (service->state.replay_cache_rend_cookie) {
  2400. replaycache_free(service->state.replay_cache_rend_cookie);
  2401. }
  2402. /* Wipe service keys. */
  2403. memwipe(&service->keys.identity_sk, 0, sizeof(service->keys.identity_sk));
  2404. tor_free(service);
  2405. }
  2406. /* Periodic callback. Entry point from the main loop to the HS service
  2407. * subsystem. This is call every second. This is skipped if tor can't build a
  2408. * circuit or the network is disabled. */
  2409. void
  2410. hs_service_run_scheduled_events(time_t now)
  2411. {
  2412. /* First thing we'll do here is to make sure our services are in a
  2413. * quiescent state for the scheduled events. */
  2414. run_housekeeping_event(now);
  2415. /* Order matters here. We first make sure the descriptor object for each
  2416. * service contains the latest data. Once done, we check if we need to open
  2417. * new introduction circuit. Finally, we try to upload the descriptor for
  2418. * each service. */
  2419. /* Make sure descriptors are up to date. */
  2420. run_build_descriptor_event(now);
  2421. /* Make sure services have enough circuits. */
  2422. run_build_circuit_event(now);
  2423. /* Upload the descriptors if needed/possible. */
  2424. run_upload_descriptor_event(now);
  2425. }
  2426. /* Initialize the service HS subsystem. */
  2427. void
  2428. hs_service_init(void)
  2429. {
  2430. /* Should never be called twice. */
  2431. tor_assert(!hs_service_map);
  2432. tor_assert(!hs_service_staging_list);
  2433. /* v2 specific. */
  2434. rend_service_init();
  2435. hs_service_map = tor_malloc_zero(sizeof(struct hs_service_ht));
  2436. HT_INIT(hs_service_ht, hs_service_map);
  2437. hs_service_staging_list = smartlist_new();
  2438. }
  2439. /* Release all global storage of the hidden service subsystem. */
  2440. void
  2441. hs_service_free_all(void)
  2442. {
  2443. rend_service_free_all();
  2444. service_free_all();
  2445. }
  2446. #ifdef TOR_UNIT_TESTS
  2447. /* Return the global service map size. Only used by unit test. */
  2448. STATIC unsigned int
  2449. get_hs_service_map_size(void)
  2450. {
  2451. return HT_SIZE(hs_service_map);
  2452. }
  2453. /* Return the staging list size. Only used by unit test. */
  2454. STATIC int
  2455. get_hs_service_staging_list_size(void)
  2456. {
  2457. return smartlist_len(hs_service_staging_list);
  2458. }
  2459. STATIC hs_service_ht *
  2460. get_hs_service_map(void)
  2461. {
  2462. return hs_service_map;
  2463. }
  2464. STATIC hs_service_t *
  2465. get_first_service(void)
  2466. {
  2467. hs_service_t **obj = HT_START(hs_service_ht, hs_service_map);
  2468. if (obj == NULL) {
  2469. return NULL;
  2470. }
  2471. return *obj;
  2472. }
  2473. #endif /* TOR_UNIT_TESTS */