rendcommon.c 32 KB

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