rendcommon.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file rendcommon.c
  6. * \brief Rendezvous implementation: shared code between
  7. * introducers, services, clients, and rendezvous points.
  8. **/
  9. #include "or.h"
  10. #include "circuitbuild.h"
  11. #include "config.h"
  12. #include "control.h"
  13. #include "rendclient.h"
  14. #include "rendcommon.h"
  15. #include "rendmid.h"
  16. #include "rendservice.h"
  17. #include "rephist.h"
  18. #include "router.h"
  19. #include "routerlist.h"
  20. #include "routerparse.h"
  21. #include "networkstatus.h"
  22. /** Return 0 if one and two are the same service ids, else -1 or 1 */
  23. int
  24. rend_cmp_service_ids(const char *one, const char *two)
  25. {
  26. return strcasecmp(one,two);
  27. }
  28. /** Free the storage held by the service descriptor <b>desc</b>.
  29. */
  30. void
  31. rend_service_descriptor_free(rend_service_descriptor_t *desc)
  32. {
  33. if (!desc)
  34. return;
  35. if (desc->pk)
  36. crypto_pk_free(desc->pk);
  37. if (desc->intro_nodes) {
  38. SMARTLIST_FOREACH(desc->intro_nodes, rend_intro_point_t *, intro,
  39. rend_intro_point_free(intro););
  40. smartlist_free(desc->intro_nodes);
  41. }
  42. if (desc->successful_uploads) {
  43. SMARTLIST_FOREACH(desc->successful_uploads, char *, c, tor_free(c););
  44. smartlist_free(desc->successful_uploads);
  45. }
  46. tor_free(desc);
  47. }
  48. /** Length of the descriptor cookie that is used for versioned hidden
  49. * service descriptors. */
  50. #define REND_DESC_COOKIE_LEN 16
  51. /** Length of the replica number that is used to determine the secret ID
  52. * part of versioned hidden service descriptors. */
  53. #define REND_REPLICA_LEN 1
  54. /** Compute the descriptor ID for <b>service_id</b> of length
  55. * <b>REND_SERVICE_ID_LEN</b> and <b>secret_id_part</b> of length
  56. * <b>DIGEST_LEN</b>, and write it to <b>descriptor_id_out</b> of length
  57. * <b>DIGEST_LEN</b>. */
  58. void
  59. rend_get_descriptor_id_bytes(char *descriptor_id_out,
  60. const char *service_id,
  61. const char *secret_id_part)
  62. {
  63. crypto_digest_t *digest = crypto_digest_new();
  64. crypto_digest_add_bytes(digest, service_id, REND_SERVICE_ID_LEN);
  65. crypto_digest_add_bytes(digest, secret_id_part, DIGEST_LEN);
  66. crypto_digest_get_digest(digest, descriptor_id_out, DIGEST_LEN);
  67. crypto_digest_free(digest);
  68. }
  69. /** Compute the secret ID part for time_period,
  70. * a <b>descriptor_cookie</b> of length
  71. * <b>REND_DESC_COOKIE_LEN</b> which may also be <b>NULL</b> if no
  72. * descriptor_cookie shall be used, and <b>replica</b>, and write it to
  73. * <b>secret_id_part</b> of length DIGEST_LEN. */
  74. static void
  75. get_secret_id_part_bytes(char *secret_id_part, uint32_t time_period,
  76. const char *descriptor_cookie, uint8_t replica)
  77. {
  78. crypto_digest_t *digest = crypto_digest_new();
  79. time_period = htonl(time_period);
  80. crypto_digest_add_bytes(digest, (char*)&time_period, sizeof(uint32_t));
  81. if (descriptor_cookie) {
  82. crypto_digest_add_bytes(digest, descriptor_cookie,
  83. REND_DESC_COOKIE_LEN);
  84. }
  85. crypto_digest_add_bytes(digest, (const char *)&replica, REND_REPLICA_LEN);
  86. crypto_digest_get_digest(digest, secret_id_part, DIGEST_LEN);
  87. crypto_digest_free(digest);
  88. }
  89. /** Return the time period for time <b>now</b> plus a potentially
  90. * intended <b>deviation</b> of one or more periods, based on the first byte
  91. * of <b>service_id</b>. */
  92. static uint32_t
  93. get_time_period(time_t now, uint8_t deviation, const char *service_id)
  94. {
  95. /* The time period is the number of REND_TIME_PERIOD_V2_DESC_VALIDITY
  96. * intervals that have passed since the epoch, offset slightly so that
  97. * each service's time periods start and end at a fraction of that
  98. * period based on their first byte. */
  99. return (uint32_t)
  100. (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
  101. / REND_TIME_PERIOD_V2_DESC_VALIDITY + deviation;
  102. }
  103. /** Compute the time in seconds that a descriptor that is generated
  104. * <b>now</b> for <b>service_id</b> will be valid. */
  105. static uint32_t
  106. get_seconds_valid(time_t now, const char *service_id)
  107. {
  108. uint32_t result = REND_TIME_PERIOD_V2_DESC_VALIDITY -
  109. ((uint32_t)
  110. (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
  111. % REND_TIME_PERIOD_V2_DESC_VALIDITY);
  112. return result;
  113. }
  114. /** Compute the binary <b>desc_id_out</b> (DIGEST_LEN bytes long) for a given
  115. * base32-encoded <b>service_id</b> and optional unencoded
  116. * <b>descriptor_cookie</b> of length REND_DESC_COOKIE_LEN,
  117. * at time <b>now</b> for replica number
  118. * <b>replica</b>. <b>desc_id</b> needs to have <b>DIGEST_LEN</b> bytes
  119. * free. Return 0 for success, -1 otherwise. */
  120. int
  121. rend_compute_v2_desc_id(char *desc_id_out, const char *service_id,
  122. const char *descriptor_cookie, time_t now,
  123. uint8_t replica)
  124. {
  125. char service_id_binary[REND_SERVICE_ID_LEN];
  126. char secret_id_part[DIGEST_LEN];
  127. uint32_t time_period;
  128. if (!service_id ||
  129. strlen(service_id) != REND_SERVICE_ID_LEN_BASE32) {
  130. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  131. "Illegal service ID: %s",
  132. safe_str(service_id));
  133. return -1;
  134. }
  135. if (replica >= REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS) {
  136. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  137. "Replica number out of range: %d", replica);
  138. return -1;
  139. }
  140. /* Convert service ID to binary. */
  141. if (base32_decode(service_id_binary, REND_SERVICE_ID_LEN,
  142. service_id, REND_SERVICE_ID_LEN_BASE32) < 0) {
  143. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  144. "Illegal characters in service ID: %s",
  145. safe_str_client(service_id));
  146. return -1;
  147. }
  148. /* Calculate current time-period. */
  149. time_period = get_time_period(now, 0, service_id_binary);
  150. /* Calculate secret-id-part = h(time-period | desc-cookie | replica). */
  151. get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
  152. replica);
  153. /* Calculate descriptor ID: H(permanent-id | secret-id-part) */
  154. rend_get_descriptor_id_bytes(desc_id_out, service_id_binary, secret_id_part);
  155. return 0;
  156. }
  157. /** Encode the introduction points in <b>desc</b> and write the result to a
  158. * newly allocated string pointed to by <b>encoded</b>. Return 0 for
  159. * success, -1 otherwise. */
  160. static int
  161. rend_encode_v2_intro_points(char **encoded, rend_service_descriptor_t *desc)
  162. {
  163. size_t unenc_len;
  164. char *unenc = NULL;
  165. size_t unenc_written = 0;
  166. int i;
  167. int r = -1;
  168. /* Assemble unencrypted list of introduction points. */
  169. unenc_len = smartlist_len(desc->intro_nodes) * 1000; /* too long, but ok. */
  170. unenc = tor_malloc_zero(unenc_len);
  171. for (i = 0; i < smartlist_len(desc->intro_nodes); i++) {
  172. char id_base32[REND_INTRO_POINT_ID_LEN_BASE32 + 1];
  173. char *onion_key = NULL;
  174. size_t onion_key_len;
  175. crypto_pk_t *intro_key;
  176. char *service_key = NULL;
  177. char *address = NULL;
  178. size_t service_key_len;
  179. int res;
  180. rend_intro_point_t *intro = smartlist_get(desc->intro_nodes, i);
  181. /* Obtain extend info with introduction point details. */
  182. extend_info_t *info = intro->extend_info;
  183. /* Encode introduction point ID. */
  184. base32_encode(id_base32, sizeof(id_base32),
  185. info->identity_digest, DIGEST_LEN);
  186. /* Encode onion key. */
  187. if (crypto_pk_write_public_key_to_string(info->onion_key, &onion_key,
  188. &onion_key_len) < 0) {
  189. log_warn(LD_REND, "Could not write onion key.");
  190. goto done;
  191. }
  192. /* Encode intro key. */
  193. intro_key = intro->intro_key;
  194. if (!intro_key ||
  195. crypto_pk_write_public_key_to_string(intro_key, &service_key,
  196. &service_key_len) < 0) {
  197. log_warn(LD_REND, "Could not write intro key.");
  198. tor_free(onion_key);
  199. goto done;
  200. }
  201. /* Assemble everything for this introduction point. */
  202. address = tor_dup_addr(&info->addr);
  203. res = tor_snprintf(unenc + unenc_written, unenc_len - unenc_written,
  204. "introduction-point %s\n"
  205. "ip-address %s\n"
  206. "onion-port %d\n"
  207. "onion-key\n%s"
  208. "service-key\n%s",
  209. id_base32,
  210. address,
  211. info->port,
  212. onion_key,
  213. service_key);
  214. tor_free(address);
  215. tor_free(onion_key);
  216. tor_free(service_key);
  217. if (res < 0) {
  218. log_warn(LD_REND, "Not enough space for writing introduction point "
  219. "string.");
  220. goto done;
  221. }
  222. /* Update total number of written bytes for unencrypted intro points. */
  223. unenc_written += res;
  224. }
  225. /* Finalize unencrypted introduction points. */
  226. if (unenc_len < unenc_written + 2) {
  227. log_warn(LD_REND, "Not enough space for finalizing introduction point "
  228. "string.");
  229. goto done;
  230. }
  231. unenc[unenc_written++] = '\n';
  232. unenc[unenc_written++] = 0;
  233. *encoded = unenc;
  234. r = 0;
  235. done:
  236. if (r<0)
  237. tor_free(unenc);
  238. return r;
  239. }
  240. /** Encrypt the encoded introduction points in <b>encoded</b> using
  241. * authorization type 'basic' with <b>client_cookies</b> and write the
  242. * result to a newly allocated string pointed to by <b>encrypted_out</b> of
  243. * length <b>encrypted_len_out</b>. Return 0 for success, -1 otherwise. */
  244. static int
  245. rend_encrypt_v2_intro_points_basic(char **encrypted_out,
  246. size_t *encrypted_len_out,
  247. const char *encoded,
  248. smartlist_t *client_cookies)
  249. {
  250. int r = -1, i, pos, enclen, client_blocks;
  251. size_t len, client_entries_len;
  252. char *enc = NULL, iv[CIPHER_IV_LEN], *client_part = NULL,
  253. session_key[CIPHER_KEY_LEN];
  254. smartlist_t *encrypted_session_keys = NULL;
  255. crypto_digest_t *digest;
  256. crypto_cipher_t *cipher;
  257. tor_assert(encoded);
  258. tor_assert(client_cookies && smartlist_len(client_cookies) > 0);
  259. /* Generate session key. */
  260. crypto_rand(session_key, CIPHER_KEY_LEN);
  261. /* Determine length of encrypted introduction points including session
  262. * keys. */
  263. client_blocks = 1 + ((smartlist_len(client_cookies) - 1) /
  264. REND_BASIC_AUTH_CLIENT_MULTIPLE);
  265. client_entries_len = client_blocks * REND_BASIC_AUTH_CLIENT_MULTIPLE *
  266. REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
  267. len = 2 + client_entries_len + CIPHER_IV_LEN + strlen(encoded);
  268. if (client_blocks >= 256) {
  269. log_warn(LD_REND, "Too many clients in introduction point string.");
  270. goto done;
  271. }
  272. enc = tor_malloc_zero(len);
  273. enc[0] = 0x01; /* type of authorization. */
  274. enc[1] = (uint8_t)client_blocks;
  275. /* Encrypt with random session key. */
  276. enclen = crypto_cipher_encrypt_with_iv(session_key,
  277. enc + 2 + client_entries_len,
  278. CIPHER_IV_LEN + strlen(encoded), encoded, strlen(encoded));
  279. if (enclen < 0) {
  280. log_warn(LD_REND, "Could not encrypt introduction point string.");
  281. goto done;
  282. }
  283. memcpy(iv, enc + 2 + client_entries_len, CIPHER_IV_LEN);
  284. /* Encrypt session key for cookies, determine client IDs, and put both
  285. * in a smartlist. */
  286. encrypted_session_keys = smartlist_new();
  287. SMARTLIST_FOREACH_BEGIN(client_cookies, const char *, cookie) {
  288. client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  289. /* Encrypt session key. */
  290. cipher = crypto_cipher_new(cookie);
  291. if (crypto_cipher_encrypt(cipher, client_part +
  292. REND_BASIC_AUTH_CLIENT_ID_LEN,
  293. session_key, CIPHER_KEY_LEN) < 0) {
  294. log_warn(LD_REND, "Could not encrypt session key for client.");
  295. crypto_cipher_free(cipher);
  296. tor_free(client_part);
  297. goto done;
  298. }
  299. crypto_cipher_free(cipher);
  300. /* Determine client ID. */
  301. digest = crypto_digest_new();
  302. crypto_digest_add_bytes(digest, cookie, REND_DESC_COOKIE_LEN);
  303. crypto_digest_add_bytes(digest, iv, CIPHER_IV_LEN);
  304. crypto_digest_get_digest(digest, client_part,
  305. REND_BASIC_AUTH_CLIENT_ID_LEN);
  306. crypto_digest_free(digest);
  307. /* Put both together. */
  308. smartlist_add(encrypted_session_keys, client_part);
  309. } SMARTLIST_FOREACH_END(cookie);
  310. /* Add some fake client IDs and encrypted session keys. */
  311. for (i = (smartlist_len(client_cookies) - 1) %
  312. REND_BASIC_AUTH_CLIENT_MULTIPLE;
  313. i < REND_BASIC_AUTH_CLIENT_MULTIPLE - 1; i++) {
  314. client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  315. crypto_rand(client_part, REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  316. smartlist_add(encrypted_session_keys, client_part);
  317. }
  318. /* Sort smartlist and put elements in result in order. */
  319. smartlist_sort_digests(encrypted_session_keys);
  320. pos = 2;
  321. SMARTLIST_FOREACH(encrypted_session_keys, const char *, entry, {
  322. memcpy(enc + pos, entry, REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  323. pos += REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
  324. });
  325. *encrypted_out = enc;
  326. *encrypted_len_out = len;
  327. enc = NULL; /* prevent free. */
  328. r = 0;
  329. done:
  330. tor_free(enc);
  331. if (encrypted_session_keys) {
  332. SMARTLIST_FOREACH(encrypted_session_keys, char *, d, tor_free(d););
  333. smartlist_free(encrypted_session_keys);
  334. }
  335. return r;
  336. }
  337. /** Encrypt the encoded introduction points in <b>encoded</b> using
  338. * authorization type 'stealth' with <b>descriptor_cookie</b> of length
  339. * REND_DESC_COOKIE_LEN and write the result to a newly allocated string
  340. * pointed to by <b>encrypted_out</b> of length <b>encrypted_len_out</b>.
  341. * Return 0 for success, -1 otherwise. */
  342. static int
  343. rend_encrypt_v2_intro_points_stealth(char **encrypted_out,
  344. size_t *encrypted_len_out,
  345. const char *encoded,
  346. const char *descriptor_cookie)
  347. {
  348. int r = -1, enclen;
  349. char *enc;
  350. tor_assert(encoded);
  351. tor_assert(descriptor_cookie);
  352. enc = tor_malloc_zero(1 + CIPHER_IV_LEN + strlen(encoded));
  353. enc[0] = 0x02; /* Auth type */
  354. enclen = crypto_cipher_encrypt_with_iv(descriptor_cookie,
  355. enc + 1,
  356. CIPHER_IV_LEN+strlen(encoded),
  357. encoded, strlen(encoded));
  358. if (enclen < 0) {
  359. log_warn(LD_REND, "Could not encrypt introduction point string.");
  360. goto done;
  361. }
  362. *encrypted_out = enc;
  363. *encrypted_len_out = enclen;
  364. enc = NULL; /* prevent free */
  365. r = 0;
  366. done:
  367. tor_free(enc);
  368. return r;
  369. }
  370. /** Attempt to parse the given <b>desc_str</b> and return true if this
  371. * succeeds, false otherwise. */
  372. static int
  373. rend_desc_v2_is_parsable(rend_encoded_v2_service_descriptor_t *desc)
  374. {
  375. rend_service_descriptor_t *test_parsed = NULL;
  376. char test_desc_id[DIGEST_LEN];
  377. char *test_intro_content = NULL;
  378. size_t test_intro_size;
  379. size_t test_encoded_size;
  380. const char *test_next;
  381. int res = rend_parse_v2_service_descriptor(&test_parsed, test_desc_id,
  382. &test_intro_content,
  383. &test_intro_size,
  384. &test_encoded_size,
  385. &test_next, desc->desc_str, 1);
  386. rend_service_descriptor_free(test_parsed);
  387. tor_free(test_intro_content);
  388. return (res >= 0);
  389. }
  390. /** Free the storage held by an encoded v2 service descriptor. */
  391. void
  392. rend_encoded_v2_service_descriptor_free(
  393. rend_encoded_v2_service_descriptor_t *desc)
  394. {
  395. if (!desc)
  396. return;
  397. tor_free(desc->desc_str);
  398. tor_free(desc);
  399. }
  400. /** Free the storage held by an introduction point info. */
  401. void
  402. rend_intro_point_free(rend_intro_point_t *intro)
  403. {
  404. if (!intro)
  405. return;
  406. extend_info_free(intro->extend_info);
  407. crypto_pk_free(intro->intro_key);
  408. if (intro->accepted_intro_rsa_parts != NULL) {
  409. replaycache_free(intro->accepted_intro_rsa_parts);
  410. }
  411. tor_free(intro);
  412. }
  413. /** Encode a set of rend_encoded_v2_service_descriptor_t's for <b>desc</b>
  414. * at time <b>now</b> using <b>service_key</b>, depending on
  415. * <b>auth_type</b> a <b>descriptor_cookie</b> and a list of
  416. * <b>client_cookies</b> (which are both <b>NULL</b> if no client
  417. * authorization is performed), and <b>period</b> (e.g. 0 for the current
  418. * period, 1 for the next period, etc.) and add them to the existing list
  419. * <b>descs_out</b>; return the number of seconds that the descriptors will
  420. * be found by clients, or -1 if the encoding was not successful. */
  421. int
  422. rend_encode_v2_descriptors(smartlist_t *descs_out,
  423. rend_service_descriptor_t *desc, time_t now,
  424. uint8_t period, rend_auth_type_t auth_type,
  425. crypto_pk_t *client_key,
  426. smartlist_t *client_cookies)
  427. {
  428. char service_id[DIGEST_LEN];
  429. char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
  430. uint32_t time_period;
  431. char *ipos_base64 = NULL, *ipos = NULL, *ipos_encrypted = NULL,
  432. *descriptor_cookie = NULL;
  433. size_t ipos_len = 0, ipos_encrypted_len = 0;
  434. int k;
  435. uint32_t seconds_valid;
  436. crypto_pk_t *service_key;
  437. if (!desc) {
  438. log_warn(LD_BUG, "Could not encode v2 descriptor: No desc given.");
  439. return -1;
  440. }
  441. service_key = (auth_type == REND_STEALTH_AUTH) ? client_key : desc->pk;
  442. tor_assert(service_key);
  443. if (auth_type == REND_STEALTH_AUTH) {
  444. descriptor_cookie = smartlist_get(client_cookies, 0);
  445. tor_assert(descriptor_cookie);
  446. }
  447. /* Obtain service_id from public key. */
  448. crypto_pk_get_digest(service_key, service_id);
  449. /* Calculate current time-period. */
  450. time_period = get_time_period(now, period, service_id);
  451. /* Determine how many seconds the descriptor will be valid. */
  452. seconds_valid = period * REND_TIME_PERIOD_V2_DESC_VALIDITY +
  453. get_seconds_valid(now, service_id);
  454. /* Assemble, possibly encrypt, and encode introduction points. */
  455. if (smartlist_len(desc->intro_nodes) > 0) {
  456. if (rend_encode_v2_intro_points(&ipos, desc) < 0) {
  457. log_warn(LD_REND, "Encoding of introduction points did not succeed.");
  458. return -1;
  459. }
  460. switch (auth_type) {
  461. case REND_NO_AUTH:
  462. ipos_len = strlen(ipos);
  463. break;
  464. case REND_BASIC_AUTH:
  465. if (rend_encrypt_v2_intro_points_basic(&ipos_encrypted,
  466. &ipos_encrypted_len, ipos,
  467. client_cookies) < 0) {
  468. log_warn(LD_REND, "Encrypting of introduction points did not "
  469. "succeed.");
  470. tor_free(ipos);
  471. return -1;
  472. }
  473. tor_free(ipos);
  474. ipos = ipos_encrypted;
  475. ipos_len = ipos_encrypted_len;
  476. break;
  477. case REND_STEALTH_AUTH:
  478. if (rend_encrypt_v2_intro_points_stealth(&ipos_encrypted,
  479. &ipos_encrypted_len, ipos,
  480. descriptor_cookie) < 0) {
  481. log_warn(LD_REND, "Encrypting of introduction points did not "
  482. "succeed.");
  483. tor_free(ipos);
  484. return -1;
  485. }
  486. tor_free(ipos);
  487. ipos = ipos_encrypted;
  488. ipos_len = ipos_encrypted_len;
  489. break;
  490. default:
  491. log_warn(LD_REND|LD_BUG, "Unrecognized authorization type %d",
  492. (int)auth_type);
  493. tor_free(ipos);
  494. return -1;
  495. }
  496. /* Base64-encode introduction points. */
  497. ipos_base64 = tor_calloc(ipos_len, 2);
  498. if (base64_encode(ipos_base64, ipos_len * 2, ipos, ipos_len,
  499. BASE64_ENCODE_MULTILINE)<0) {
  500. log_warn(LD_REND, "Could not encode introduction point string to "
  501. "base64. length=%d", (int)ipos_len);
  502. tor_free(ipos_base64);
  503. tor_free(ipos);
  504. return -1;
  505. }
  506. tor_free(ipos);
  507. }
  508. /* Encode REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS descriptors. */
  509. for (k = 0; k < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; k++) {
  510. char secret_id_part[DIGEST_LEN];
  511. char secret_id_part_base32[REND_SECRET_ID_PART_LEN_BASE32 + 1];
  512. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  513. char *permanent_key = NULL;
  514. size_t permanent_key_len;
  515. char published[ISO_TIME_LEN+1];
  516. int i;
  517. char protocol_versions_string[16]; /* max len: "0,1,2,3,4,5,6,7\0" */
  518. size_t protocol_versions_written;
  519. size_t desc_len;
  520. char *desc_str = NULL;
  521. int result = 0;
  522. size_t written = 0;
  523. char desc_digest[DIGEST_LEN];
  524. rend_encoded_v2_service_descriptor_t *enc =
  525. tor_malloc_zero(sizeof(rend_encoded_v2_service_descriptor_t));
  526. /* Calculate secret-id-part = h(time-period | cookie | replica). */
  527. get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
  528. k);
  529. base32_encode(secret_id_part_base32, sizeof(secret_id_part_base32),
  530. secret_id_part, DIGEST_LEN);
  531. /* Calculate descriptor ID. */
  532. rend_get_descriptor_id_bytes(enc->desc_id, service_id, secret_id_part);
  533. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  534. enc->desc_id, DIGEST_LEN);
  535. /* PEM-encode the public key */
  536. if (crypto_pk_write_public_key_to_string(service_key, &permanent_key,
  537. &permanent_key_len) < 0) {
  538. log_warn(LD_BUG, "Could not write public key to string.");
  539. rend_encoded_v2_service_descriptor_free(enc);
  540. goto err;
  541. }
  542. /* Encode timestamp. */
  543. format_iso_time(published, desc->timestamp);
  544. /* Write protocol-versions bitmask to comma-separated value string. */
  545. protocol_versions_written = 0;
  546. for (i = 0; i < 8; i++) {
  547. if (desc->protocols & 1 << i) {
  548. tor_snprintf(protocol_versions_string + protocol_versions_written,
  549. 16 - protocol_versions_written, "%d,", i);
  550. protocol_versions_written += 2;
  551. }
  552. }
  553. if (protocol_versions_written)
  554. protocol_versions_string[protocol_versions_written - 1] = '\0';
  555. else
  556. protocol_versions_string[0]= '\0';
  557. /* Assemble complete descriptor. */
  558. desc_len = 2000 + smartlist_len(desc->intro_nodes) * 1000; /* far too long,
  559. but okay.*/
  560. enc->desc_str = desc_str = tor_malloc_zero(desc_len);
  561. result = tor_snprintf(desc_str, desc_len,
  562. "rendezvous-service-descriptor %s\n"
  563. "version 2\n"
  564. "permanent-key\n%s"
  565. "secret-id-part %s\n"
  566. "publication-time %s\n"
  567. "protocol-versions %s\n",
  568. desc_id_base32,
  569. permanent_key,
  570. secret_id_part_base32,
  571. published,
  572. protocol_versions_string);
  573. tor_free(permanent_key);
  574. if (result < 0) {
  575. log_warn(LD_BUG, "Descriptor ran out of room.");
  576. rend_encoded_v2_service_descriptor_free(enc);
  577. goto err;
  578. }
  579. written = result;
  580. /* Add introduction points. */
  581. if (ipos_base64) {
  582. result = tor_snprintf(desc_str + written, desc_len - written,
  583. "introduction-points\n"
  584. "-----BEGIN MESSAGE-----\n%s"
  585. "-----END MESSAGE-----\n",
  586. ipos_base64);
  587. if (result < 0) {
  588. log_warn(LD_BUG, "could not write introduction points.");
  589. rend_encoded_v2_service_descriptor_free(enc);
  590. goto err;
  591. }
  592. written += result;
  593. }
  594. /* Add signature. */
  595. strlcpy(desc_str + written, "signature\n", desc_len - written);
  596. written += strlen(desc_str + written);
  597. if (crypto_digest(desc_digest, desc_str, written) < 0) {
  598. log_warn(LD_BUG, "could not create digest.");
  599. rend_encoded_v2_service_descriptor_free(enc);
  600. goto err;
  601. }
  602. if (router_append_dirobj_signature(desc_str + written,
  603. desc_len - written,
  604. desc_digest, DIGEST_LEN,
  605. service_key) < 0) {
  606. log_warn(LD_BUG, "Couldn't sign desc.");
  607. rend_encoded_v2_service_descriptor_free(enc);
  608. goto err;
  609. }
  610. written += strlen(desc_str+written);
  611. if (written+2 > desc_len) {
  612. log_warn(LD_BUG, "Could not finish desc.");
  613. rend_encoded_v2_service_descriptor_free(enc);
  614. goto err;
  615. }
  616. desc_str[written++] = 0;
  617. /* Check if we can parse our own descriptor. */
  618. if (!rend_desc_v2_is_parsable(enc)) {
  619. log_warn(LD_BUG, "Could not parse my own descriptor: %s", desc_str);
  620. rend_encoded_v2_service_descriptor_free(enc);
  621. goto err;
  622. }
  623. smartlist_add(descs_out, enc);
  624. /* Add the uploaded descriptor to the local service's descriptor cache */
  625. rend_cache_store_v2_desc_as_service(enc->desc_str);
  626. base32_encode(service_id_base32, sizeof(service_id_base32),
  627. service_id, REND_SERVICE_ID_LEN);
  628. control_event_hs_descriptor_created(service_id_base32, desc_id_base32, k);
  629. }
  630. log_info(LD_REND, "Successfully encoded a v2 descriptor and "
  631. "confirmed that it is parsable.");
  632. goto done;
  633. err:
  634. SMARTLIST_FOREACH(descs_out, rend_encoded_v2_service_descriptor_t *, d,
  635. rend_encoded_v2_service_descriptor_free(d););
  636. smartlist_clear(descs_out);
  637. seconds_valid = -1;
  638. done:
  639. tor_free(ipos_base64);
  640. return seconds_valid;
  641. }
  642. /** Sets <b>out</b> to the first 10 bytes of the digest of <b>pk</b>,
  643. * base32 encoded. NUL-terminates out. (We use this string to
  644. * identify services in directory requests and .onion URLs.)
  645. */
  646. int
  647. rend_get_service_id(crypto_pk_t *pk, char *out)
  648. {
  649. char buf[DIGEST_LEN];
  650. tor_assert(pk);
  651. if (crypto_pk_get_digest(pk, buf) < 0)
  652. return -1;
  653. base32_encode(out, REND_SERVICE_ID_LEN_BASE32+1, buf, REND_SERVICE_ID_LEN);
  654. return 0;
  655. }
  656. /** Determines whether <b>a</b> is in the interval of <b>b</b> (excluded) and
  657. * <b>c</b> (included) in a circular digest ring; returns 1 if this is the
  658. * case, and 0 otherwise.
  659. */
  660. int
  661. rend_id_is_in_interval(const char *a, const char *b, const char *c)
  662. {
  663. int a_b, b_c, c_a;
  664. tor_assert(a);
  665. tor_assert(b);
  666. tor_assert(c);
  667. /* There are five cases in which a is outside the interval ]b,c]: */
  668. a_b = tor_memcmp(a,b,DIGEST_LEN);
  669. if (a_b == 0)
  670. return 0; /* 1. a == b (b is excluded) */
  671. b_c = tor_memcmp(b,c,DIGEST_LEN);
  672. if (b_c == 0)
  673. return 0; /* 2. b == c (interval is empty) */
  674. else if (a_b <= 0 && b_c < 0)
  675. return 0; /* 3. a b c */
  676. c_a = tor_memcmp(c,a,DIGEST_LEN);
  677. if (c_a < 0 && a_b <= 0)
  678. return 0; /* 4. c a b */
  679. else if (b_c < 0 && c_a < 0)
  680. return 0; /* 5. b c a */
  681. /* In the other cases (a c b; b a c; c b a), a is inside the interval. */
  682. return 1;
  683. }
  684. /** Return true iff <b>query</b> is a syntactically valid service ID (as
  685. * generated by rend_get_service_id). */
  686. int
  687. rend_valid_service_id(const char *query)
  688. {
  689. if (strlen(query) != REND_SERVICE_ID_LEN_BASE32)
  690. return 0;
  691. if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN_BASE32)
  692. return 0;
  693. return 1;
  694. }
  695. /** Return true iff <b>query</b> is a syntactically valid descriptor ID.
  696. * (as generated by rend_get_descriptor_id_bytes). */
  697. int
  698. rend_valid_descriptor_id(const char *query)
  699. {
  700. if (strlen(query) != REND_DESC_ID_V2_LEN_BASE32) {
  701. goto invalid;
  702. }
  703. if (strspn(query, BASE32_CHARS) != REND_DESC_ID_V2_LEN_BASE32) {
  704. goto invalid;
  705. }
  706. return 1;
  707. invalid:
  708. return 0;
  709. }
  710. /** Called when we get a rendezvous-related relay cell on circuit
  711. * <b>circ</b>. Dispatch on rendezvous relay command. */
  712. void
  713. rend_process_relay_cell(circuit_t *circ, const crypt_path_t *layer_hint,
  714. int command, size_t length,
  715. const uint8_t *payload)
  716. {
  717. or_circuit_t *or_circ = NULL;
  718. origin_circuit_t *origin_circ = NULL;
  719. int r = -2;
  720. if (CIRCUIT_IS_ORIGIN(circ)) {
  721. origin_circ = TO_ORIGIN_CIRCUIT(circ);
  722. if (!layer_hint || layer_hint != origin_circ->cpath->prev) {
  723. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  724. "Relay cell (rend purpose %d) from wrong hop on origin circ",
  725. command);
  726. origin_circ = NULL;
  727. }
  728. } else {
  729. or_circ = TO_OR_CIRCUIT(circ);
  730. }
  731. switch (command) {
  732. case RELAY_COMMAND_ESTABLISH_INTRO:
  733. if (or_circ)
  734. r = rend_mid_establish_intro(or_circ,payload,length);
  735. break;
  736. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  737. if (or_circ)
  738. r = rend_mid_establish_rendezvous(or_circ,payload,length);
  739. break;
  740. case RELAY_COMMAND_INTRODUCE1:
  741. if (or_circ)
  742. r = rend_mid_introduce(or_circ,payload,length);
  743. break;
  744. case RELAY_COMMAND_INTRODUCE2:
  745. if (origin_circ)
  746. r = rend_service_receive_introduction(origin_circ,payload,length);
  747. break;
  748. case RELAY_COMMAND_INTRODUCE_ACK:
  749. if (origin_circ)
  750. r = rend_client_introduction_acked(origin_circ,payload,length);
  751. break;
  752. case RELAY_COMMAND_RENDEZVOUS1:
  753. if (or_circ)
  754. r = rend_mid_rendezvous(or_circ,payload,length);
  755. break;
  756. case RELAY_COMMAND_RENDEZVOUS2:
  757. if (origin_circ)
  758. r = rend_client_receive_rendezvous(origin_circ,payload,length);
  759. break;
  760. case RELAY_COMMAND_INTRO_ESTABLISHED:
  761. if (origin_circ)
  762. r = rend_service_intro_established(origin_circ,payload,length);
  763. break;
  764. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  765. if (origin_circ)
  766. r = rend_client_rendezvous_acked(origin_circ,payload,length);
  767. break;
  768. default:
  769. tor_fragile_assert();
  770. }
  771. if (r == -2)
  772. log_info(LD_PROTOCOL, "Dropping cell (type %d) for wrong circuit type.",
  773. command);
  774. }
  775. /** Allocate and return a new rend_data_t with the same
  776. * contents as <b>query</b>. */
  777. rend_data_t *
  778. rend_data_dup(const rend_data_t *data)
  779. {
  780. rend_data_t *data_dup;
  781. tor_assert(data);
  782. data_dup = tor_memdup(data, sizeof(rend_data_t));
  783. data_dup->hsdirs_fp = smartlist_new();
  784. SMARTLIST_FOREACH(data->hsdirs_fp, char *, fp,
  785. smartlist_add(data_dup->hsdirs_fp,
  786. tor_memdup(fp, DIGEST_LEN)));
  787. return data_dup;
  788. }
  789. /** Compute descriptor ID for each replicas and save them. A valid onion
  790. * address must be present in the <b>rend_data</b>.
  791. *
  792. * Return 0 on success else -1. */
  793. static int
  794. compute_desc_id(rend_data_t *rend_data)
  795. {
  796. int ret = 0;
  797. unsigned replica;
  798. time_t now = time(NULL);
  799. tor_assert(rend_data);
  800. /* Compute descriptor ID for each replicas. */
  801. for (replica = 0; replica < ARRAY_LENGTH(rend_data->descriptor_id);
  802. replica++) {
  803. ret = rend_compute_v2_desc_id(rend_data->descriptor_id[replica],
  804. rend_data->onion_address,
  805. rend_data->descriptor_cookie,
  806. now, replica);
  807. if (ret < 0) {
  808. goto end;
  809. }
  810. }
  811. end:
  812. return ret;
  813. }
  814. /** Allocate and initialize a rend_data_t object for a service using the
  815. * given arguments. Only the <b>onion_address</b> is not optional.
  816. *
  817. * Return a valid rend_data_t pointer. */
  818. rend_data_t *
  819. rend_data_service_create(const char *onion_address, const char *pk_digest,
  820. const uint8_t *cookie, rend_auth_type_t auth_type)
  821. {
  822. rend_data_t *rend_data = tor_malloc_zero(sizeof(*rend_data));
  823. /* We need at least one else the call is wrong. */
  824. tor_assert(onion_address != NULL);
  825. if (pk_digest) {
  826. memcpy(rend_data->rend_pk_digest, pk_digest,
  827. sizeof(rend_data->rend_pk_digest));
  828. }
  829. if (cookie) {
  830. memcpy(rend_data->rend_cookie, cookie,
  831. sizeof(rend_data->rend_cookie));
  832. }
  833. strlcpy(rend_data->onion_address, onion_address,
  834. sizeof(rend_data->onion_address));
  835. rend_data->auth_type = auth_type;
  836. /* Won't be used but still need to initialize it for rend_data dup and
  837. * free. */
  838. rend_data->hsdirs_fp = smartlist_new();
  839. return rend_data;
  840. }
  841. /** Allocate and initialize a rend_data_t object for a client request using
  842. * the given arguments. Either an onion address or a descriptor ID is
  843. * needed. Both can be given but only the onion address will be used to make
  844. * the descriptor fetch.
  845. *
  846. * Return a valid rend_data_t pointer or NULL on error meaning the
  847. * descriptor IDs couldn't be computed from the given data. */
  848. rend_data_t *
  849. rend_data_client_create(const char *onion_address, const char *desc_id,
  850. const char *cookie, rend_auth_type_t auth_type)
  851. {
  852. rend_data_t *rend_data = tor_malloc_zero(sizeof(*rend_data));
  853. /* We need at least one else the call is wrong. */
  854. tor_assert(onion_address != NULL || desc_id != NULL);
  855. if (cookie) {
  856. memcpy(rend_data->descriptor_cookie, cookie,
  857. sizeof(rend_data->descriptor_cookie));
  858. }
  859. if (desc_id) {
  860. memcpy(rend_data->desc_id_fetch, desc_id,
  861. sizeof(rend_data->desc_id_fetch));
  862. }
  863. if (onion_address) {
  864. strlcpy(rend_data->onion_address, onion_address,
  865. sizeof(rend_data->onion_address));
  866. if (compute_desc_id(rend_data) < 0) {
  867. goto error;
  868. }
  869. }
  870. rend_data->auth_type = auth_type;
  871. rend_data->hsdirs_fp = smartlist_new();
  872. return rend_data;
  873. error:
  874. rend_data_free(rend_data);
  875. return NULL;
  876. }
  877. /** Determine the routers that are responsible for <b>id</b> (binary) and
  878. * add pointers to those routers' routerstatus_t to <b>responsible_dirs</b>.
  879. * Return -1 if we're returning an empty smartlist, else return 0.
  880. */
  881. int
  882. hid_serv_get_responsible_directories(smartlist_t *responsible_dirs,
  883. const char *id)
  884. {
  885. int start, found, n_added = 0, i;
  886. networkstatus_t *c = networkstatus_get_latest_consensus();
  887. if (!c || !smartlist_len(c->routerstatus_list)) {
  888. log_warn(LD_REND, "We don't have a consensus, so we can't perform v2 "
  889. "rendezvous operations.");
  890. return -1;
  891. }
  892. tor_assert(id);
  893. start = networkstatus_vote_find_entry_idx(c, id, &found);
  894. if (start == smartlist_len(c->routerstatus_list)) start = 0;
  895. i = start;
  896. do {
  897. routerstatus_t *r = smartlist_get(c->routerstatus_list, i);
  898. if (r->is_hs_dir) {
  899. smartlist_add(responsible_dirs, r);
  900. if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
  901. return 0;
  902. }
  903. if (++i == smartlist_len(c->routerstatus_list))
  904. i = 0;
  905. } while (i != start);
  906. /* Even though we don't have the desired number of hidden service
  907. * directories, be happy if we got any. */
  908. return smartlist_len(responsible_dirs) ? 0 : -1;
  909. }