rendcommon.c 32 KB

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