rendcommon.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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 "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. if (crypto_rand(session_key, CIPHER_KEY_LEN) < 0) {
  259. log_warn(LD_REND, "Unable to generate random session key to encrypt "
  260. "introduction point string.");
  261. goto done;
  262. }
  263. /* Determine length of encrypted introduction points including session
  264. * keys. */
  265. client_blocks = 1 + ((smartlist_len(client_cookies) - 1) /
  266. REND_BASIC_AUTH_CLIENT_MULTIPLE);
  267. client_entries_len = client_blocks * REND_BASIC_AUTH_CLIENT_MULTIPLE *
  268. REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
  269. len = 2 + client_entries_len + CIPHER_IV_LEN + strlen(encoded);
  270. if (client_blocks >= 256) {
  271. log_warn(LD_REND, "Too many clients in introduction point string.");
  272. goto done;
  273. }
  274. enc = tor_malloc_zero(len);
  275. enc[0] = 0x01; /* type of authorization. */
  276. enc[1] = (uint8_t)client_blocks;
  277. /* Encrypt with random session key. */
  278. enclen = crypto_cipher_encrypt_with_iv(session_key,
  279. enc + 2 + client_entries_len,
  280. CIPHER_IV_LEN + strlen(encoded), encoded, strlen(encoded));
  281. if (enclen < 0) {
  282. log_warn(LD_REND, "Could not encrypt introduction point string.");
  283. goto done;
  284. }
  285. memcpy(iv, enc + 2 + client_entries_len, CIPHER_IV_LEN);
  286. /* Encrypt session key for cookies, determine client IDs, and put both
  287. * in a smartlist. */
  288. encrypted_session_keys = smartlist_new();
  289. SMARTLIST_FOREACH_BEGIN(client_cookies, const char *, cookie) {
  290. client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  291. /* Encrypt session key. */
  292. cipher = crypto_cipher_new(cookie);
  293. if (crypto_cipher_encrypt(cipher, client_part +
  294. REND_BASIC_AUTH_CLIENT_ID_LEN,
  295. session_key, CIPHER_KEY_LEN) < 0) {
  296. log_warn(LD_REND, "Could not encrypt session key for client.");
  297. crypto_cipher_free(cipher);
  298. tor_free(client_part);
  299. goto done;
  300. }
  301. crypto_cipher_free(cipher);
  302. /* Determine client ID. */
  303. digest = crypto_digest_new();
  304. crypto_digest_add_bytes(digest, cookie, REND_DESC_COOKIE_LEN);
  305. crypto_digest_add_bytes(digest, iv, CIPHER_IV_LEN);
  306. crypto_digest_get_digest(digest, client_part,
  307. REND_BASIC_AUTH_CLIENT_ID_LEN);
  308. crypto_digest_free(digest);
  309. /* Put both together. */
  310. smartlist_add(encrypted_session_keys, client_part);
  311. } SMARTLIST_FOREACH_END(cookie);
  312. /* Add some fake client IDs and encrypted session keys. */
  313. for (i = (smartlist_len(client_cookies) - 1) %
  314. REND_BASIC_AUTH_CLIENT_MULTIPLE;
  315. i < REND_BASIC_AUTH_CLIENT_MULTIPLE - 1; i++) {
  316. client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  317. if (crypto_rand(client_part, REND_BASIC_AUTH_CLIENT_ENTRY_LEN) < 0) {
  318. log_warn(LD_REND, "Unable to generate fake client entry.");
  319. tor_free(client_part);
  320. goto done;
  321. }
  322. smartlist_add(encrypted_session_keys, client_part);
  323. }
  324. /* Sort smartlist and put elements in result in order. */
  325. smartlist_sort_digests(encrypted_session_keys);
  326. pos = 2;
  327. SMARTLIST_FOREACH(encrypted_session_keys, const char *, entry, {
  328. memcpy(enc + pos, entry, REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  329. pos += REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
  330. });
  331. *encrypted_out = enc;
  332. *encrypted_len_out = len;
  333. enc = NULL; /* prevent free. */
  334. r = 0;
  335. done:
  336. tor_free(enc);
  337. if (encrypted_session_keys) {
  338. SMARTLIST_FOREACH(encrypted_session_keys, char *, d, tor_free(d););
  339. smartlist_free(encrypted_session_keys);
  340. }
  341. return r;
  342. }
  343. /** Encrypt the encoded introduction points in <b>encoded</b> using
  344. * authorization type 'stealth' with <b>descriptor_cookie</b> of length
  345. * REND_DESC_COOKIE_LEN and write the result to a newly allocated string
  346. * pointed to by <b>encrypted_out</b> of length <b>encrypted_len_out</b>.
  347. * Return 0 for success, -1 otherwise. */
  348. static int
  349. rend_encrypt_v2_intro_points_stealth(char **encrypted_out,
  350. size_t *encrypted_len_out,
  351. const char *encoded,
  352. const char *descriptor_cookie)
  353. {
  354. int r = -1, enclen;
  355. char *enc;
  356. tor_assert(encoded);
  357. tor_assert(descriptor_cookie);
  358. enc = tor_malloc_zero(1 + CIPHER_IV_LEN + strlen(encoded));
  359. enc[0] = 0x02; /* Auth type */
  360. enclen = crypto_cipher_encrypt_with_iv(descriptor_cookie,
  361. enc + 1,
  362. CIPHER_IV_LEN+strlen(encoded),
  363. encoded, strlen(encoded));
  364. if (enclen < 0) {
  365. log_warn(LD_REND, "Could not encrypt introduction point string.");
  366. goto done;
  367. }
  368. *encrypted_out = enc;
  369. *encrypted_len_out = enclen;
  370. enc = NULL; /* prevent free */
  371. r = 0;
  372. done:
  373. tor_free(enc);
  374. return r;
  375. }
  376. /** Attempt to parse the given <b>desc_str</b> and return true if this
  377. * succeeds, false otherwise. */
  378. static int
  379. rend_desc_v2_is_parsable(rend_encoded_v2_service_descriptor_t *desc)
  380. {
  381. rend_service_descriptor_t *test_parsed = NULL;
  382. char test_desc_id[DIGEST_LEN];
  383. char *test_intro_content = NULL;
  384. size_t test_intro_size;
  385. size_t test_encoded_size;
  386. const char *test_next;
  387. int res = rend_parse_v2_service_descriptor(&test_parsed, test_desc_id,
  388. &test_intro_content,
  389. &test_intro_size,
  390. &test_encoded_size,
  391. &test_next, desc->desc_str, 1);
  392. rend_service_descriptor_free(test_parsed);
  393. tor_free(test_intro_content);
  394. return (res >= 0);
  395. }
  396. /** Free the storage held by an encoded v2 service descriptor. */
  397. void
  398. rend_encoded_v2_service_descriptor_free(
  399. rend_encoded_v2_service_descriptor_t *desc)
  400. {
  401. if (!desc)
  402. return;
  403. tor_free(desc->desc_str);
  404. tor_free(desc);
  405. }
  406. /** Free the storage held by an introduction point info. */
  407. void
  408. rend_intro_point_free(rend_intro_point_t *intro)
  409. {
  410. if (!intro)
  411. return;
  412. extend_info_free(intro->extend_info);
  413. crypto_pk_free(intro->intro_key);
  414. if (intro->accepted_intro_rsa_parts != NULL) {
  415. replaycache_free(intro->accepted_intro_rsa_parts);
  416. }
  417. tor_free(intro);
  418. }
  419. /** Encode a set of rend_encoded_v2_service_descriptor_t's for <b>desc</b>
  420. * at time <b>now</b> using <b>service_key</b>, depending on
  421. * <b>auth_type</b> a <b>descriptor_cookie</b> and a list of
  422. * <b>client_cookies</b> (which are both <b>NULL</b> if no client
  423. * authorization is performed), and <b>period</b> (e.g. 0 for the current
  424. * period, 1 for the next period, etc.) and add them to the existing list
  425. * <b>descs_out</b>; return the number of seconds that the descriptors will
  426. * be found by clients, or -1 if the encoding was not successful. */
  427. int
  428. rend_encode_v2_descriptors(smartlist_t *descs_out,
  429. rend_service_descriptor_t *desc, time_t now,
  430. uint8_t period, rend_auth_type_t auth_type,
  431. crypto_pk_t *client_key,
  432. smartlist_t *client_cookies)
  433. {
  434. char service_id[DIGEST_LEN];
  435. char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
  436. uint32_t time_period;
  437. char *ipos_base64 = NULL, *ipos = NULL, *ipos_encrypted = NULL,
  438. *descriptor_cookie = NULL;
  439. size_t ipos_len = 0, ipos_encrypted_len = 0;
  440. int k;
  441. uint32_t seconds_valid;
  442. crypto_pk_t *service_key;
  443. if (!desc) {
  444. log_warn(LD_BUG, "Could not encode v2 descriptor: No desc given.");
  445. return -1;
  446. }
  447. service_key = (auth_type == REND_STEALTH_AUTH) ? client_key : desc->pk;
  448. tor_assert(service_key);
  449. if (auth_type == REND_STEALTH_AUTH) {
  450. descriptor_cookie = smartlist_get(client_cookies, 0);
  451. tor_assert(descriptor_cookie);
  452. }
  453. /* Obtain service_id from public key. */
  454. crypto_pk_get_digest(service_key, service_id);
  455. /* Calculate current time-period. */
  456. time_period = get_time_period(now, period, service_id);
  457. /* Determine how many seconds the descriptor will be valid. */
  458. seconds_valid = period * REND_TIME_PERIOD_V2_DESC_VALIDITY +
  459. get_seconds_valid(now, service_id);
  460. /* Assemble, possibly encrypt, and encode introduction points. */
  461. if (smartlist_len(desc->intro_nodes) > 0) {
  462. if (rend_encode_v2_intro_points(&ipos, desc) < 0) {
  463. log_warn(LD_REND, "Encoding of introduction points did not succeed.");
  464. return -1;
  465. }
  466. switch (auth_type) {
  467. case REND_NO_AUTH:
  468. ipos_len = strlen(ipos);
  469. break;
  470. case REND_BASIC_AUTH:
  471. if (rend_encrypt_v2_intro_points_basic(&ipos_encrypted,
  472. &ipos_encrypted_len, ipos,
  473. client_cookies) < 0) {
  474. log_warn(LD_REND, "Encrypting of introduction points did not "
  475. "succeed.");
  476. tor_free(ipos);
  477. return -1;
  478. }
  479. tor_free(ipos);
  480. ipos = ipos_encrypted;
  481. ipos_len = ipos_encrypted_len;
  482. break;
  483. case REND_STEALTH_AUTH:
  484. if (rend_encrypt_v2_intro_points_stealth(&ipos_encrypted,
  485. &ipos_encrypted_len, ipos,
  486. descriptor_cookie) < 0) {
  487. log_warn(LD_REND, "Encrypting of introduction points did not "
  488. "succeed.");
  489. tor_free(ipos);
  490. return -1;
  491. }
  492. tor_free(ipos);
  493. ipos = ipos_encrypted;
  494. ipos_len = ipos_encrypted_len;
  495. break;
  496. default:
  497. log_warn(LD_REND|LD_BUG, "Unrecognized authorization type %d",
  498. (int)auth_type);
  499. tor_free(ipos);
  500. return -1;
  501. }
  502. /* Base64-encode introduction points. */
  503. ipos_base64 = tor_calloc(ipos_len, 2);
  504. if (base64_encode(ipos_base64, ipos_len * 2, ipos, ipos_len,
  505. BASE64_ENCODE_MULTILINE)<0) {
  506. log_warn(LD_REND, "Could not encode introduction point string to "
  507. "base64. length=%d", (int)ipos_len);
  508. tor_free(ipos_base64);
  509. tor_free(ipos);
  510. return -1;
  511. }
  512. tor_free(ipos);
  513. }
  514. /* Encode REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS descriptors. */
  515. for (k = 0; k < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; k++) {
  516. char secret_id_part[DIGEST_LEN];
  517. char secret_id_part_base32[REND_SECRET_ID_PART_LEN_BASE32 + 1];
  518. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  519. char *permanent_key = NULL;
  520. size_t permanent_key_len;
  521. char published[ISO_TIME_LEN+1];
  522. int i;
  523. char protocol_versions_string[16]; /* max len: "0,1,2,3,4,5,6,7\0" */
  524. size_t protocol_versions_written;
  525. size_t desc_len;
  526. char *desc_str = NULL;
  527. int result = 0;
  528. size_t written = 0;
  529. char desc_digest[DIGEST_LEN];
  530. rend_encoded_v2_service_descriptor_t *enc =
  531. tor_malloc_zero(sizeof(rend_encoded_v2_service_descriptor_t));
  532. /* Calculate secret-id-part = h(time-period | cookie | replica). */
  533. get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
  534. k);
  535. base32_encode(secret_id_part_base32, sizeof(secret_id_part_base32),
  536. secret_id_part, DIGEST_LEN);
  537. /* Calculate descriptor ID. */
  538. rend_get_descriptor_id_bytes(enc->desc_id, service_id, secret_id_part);
  539. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  540. enc->desc_id, DIGEST_LEN);
  541. /* PEM-encode the public key */
  542. if (crypto_pk_write_public_key_to_string(service_key, &permanent_key,
  543. &permanent_key_len) < 0) {
  544. log_warn(LD_BUG, "Could not write public key to string.");
  545. rend_encoded_v2_service_descriptor_free(enc);
  546. goto err;
  547. }
  548. /* Encode timestamp. */
  549. format_iso_time(published, desc->timestamp);
  550. /* Write protocol-versions bitmask to comma-separated value string. */
  551. protocol_versions_written = 0;
  552. for (i = 0; i < 8; i++) {
  553. if (desc->protocols & 1 << i) {
  554. tor_snprintf(protocol_versions_string + protocol_versions_written,
  555. 16 - protocol_versions_written, "%d,", i);
  556. protocol_versions_written += 2;
  557. }
  558. }
  559. if (protocol_versions_written)
  560. protocol_versions_string[protocol_versions_written - 1] = '\0';
  561. else
  562. protocol_versions_string[0]= '\0';
  563. /* Assemble complete descriptor. */
  564. desc_len = 2000 + smartlist_len(desc->intro_nodes) * 1000; /* far too long,
  565. but okay.*/
  566. enc->desc_str = desc_str = tor_malloc_zero(desc_len);
  567. result = tor_snprintf(desc_str, desc_len,
  568. "rendezvous-service-descriptor %s\n"
  569. "version 2\n"
  570. "permanent-key\n%s"
  571. "secret-id-part %s\n"
  572. "publication-time %s\n"
  573. "protocol-versions %s\n",
  574. desc_id_base32,
  575. permanent_key,
  576. secret_id_part_base32,
  577. published,
  578. protocol_versions_string);
  579. tor_free(permanent_key);
  580. if (result < 0) {
  581. log_warn(LD_BUG, "Descriptor ran out of room.");
  582. rend_encoded_v2_service_descriptor_free(enc);
  583. goto err;
  584. }
  585. written = result;
  586. /* Add introduction points. */
  587. if (ipos_base64) {
  588. result = tor_snprintf(desc_str + written, desc_len - written,
  589. "introduction-points\n"
  590. "-----BEGIN MESSAGE-----\n%s"
  591. "-----END MESSAGE-----\n",
  592. ipos_base64);
  593. if (result < 0) {
  594. log_warn(LD_BUG, "could not write introduction points.");
  595. rend_encoded_v2_service_descriptor_free(enc);
  596. goto err;
  597. }
  598. written += result;
  599. }
  600. /* Add signature. */
  601. strlcpy(desc_str + written, "signature\n", desc_len - written);
  602. written += strlen(desc_str + written);
  603. if (crypto_digest(desc_digest, desc_str, written) < 0) {
  604. log_warn(LD_BUG, "could not create digest.");
  605. rend_encoded_v2_service_descriptor_free(enc);
  606. goto err;
  607. }
  608. if (router_append_dirobj_signature(desc_str + written,
  609. desc_len - written,
  610. desc_digest, DIGEST_LEN,
  611. service_key) < 0) {
  612. log_warn(LD_BUG, "Couldn't sign desc.");
  613. rend_encoded_v2_service_descriptor_free(enc);
  614. goto err;
  615. }
  616. written += strlen(desc_str+written);
  617. if (written+2 > desc_len) {
  618. log_warn(LD_BUG, "Could not finish desc.");
  619. rend_encoded_v2_service_descriptor_free(enc);
  620. goto err;
  621. }
  622. desc_str[written++] = 0;
  623. /* Check if we can parse our own descriptor. */
  624. if (!rend_desc_v2_is_parsable(enc)) {
  625. log_warn(LD_BUG, "Could not parse my own descriptor: %s", desc_str);
  626. rend_encoded_v2_service_descriptor_free(enc);
  627. goto err;
  628. }
  629. smartlist_add(descs_out, enc);
  630. /* Add the uploaded descriptor to the local service's descriptor cache */
  631. rend_cache_store_v2_desc_as_service(enc->desc_str);
  632. base32_encode(service_id_base32, sizeof(service_id_base32),
  633. service_id, REND_SERVICE_ID_LEN);
  634. control_event_hs_descriptor_created(service_id_base32, desc_id_base32, k);
  635. }
  636. log_info(LD_REND, "Successfully encoded a v2 descriptor and "
  637. "confirmed that it is parsable.");
  638. goto done;
  639. err:
  640. SMARTLIST_FOREACH(descs_out, rend_encoded_v2_service_descriptor_t *, d,
  641. rend_encoded_v2_service_descriptor_free(d););
  642. smartlist_clear(descs_out);
  643. seconds_valid = -1;
  644. done:
  645. tor_free(ipos_base64);
  646. return seconds_valid;
  647. }
  648. /** Sets <b>out</b> to the first 10 bytes of the digest of <b>pk</b>,
  649. * base32 encoded. NUL-terminates out. (We use this string to
  650. * identify services in directory requests and .onion URLs.)
  651. */
  652. int
  653. rend_get_service_id(crypto_pk_t *pk, char *out)
  654. {
  655. char buf[DIGEST_LEN];
  656. tor_assert(pk);
  657. if (crypto_pk_get_digest(pk, buf) < 0)
  658. return -1;
  659. base32_encode(out, REND_SERVICE_ID_LEN_BASE32+1, buf, REND_SERVICE_ID_LEN);
  660. return 0;
  661. }
  662. /** Determines whether <b>a</b> is in the interval of <b>b</b> (excluded) and
  663. * <b>c</b> (included) in a circular digest ring; returns 1 if this is the
  664. * case, and 0 otherwise.
  665. */
  666. int
  667. rend_id_is_in_interval(const char *a, const char *b, const char *c)
  668. {
  669. int a_b, b_c, c_a;
  670. tor_assert(a);
  671. tor_assert(b);
  672. tor_assert(c);
  673. /* There are five cases in which a is outside the interval ]b,c]: */
  674. a_b = tor_memcmp(a,b,DIGEST_LEN);
  675. if (a_b == 0)
  676. return 0; /* 1. a == b (b is excluded) */
  677. b_c = tor_memcmp(b,c,DIGEST_LEN);
  678. if (b_c == 0)
  679. return 0; /* 2. b == c (interval is empty) */
  680. else if (a_b <= 0 && b_c < 0)
  681. return 0; /* 3. a b c */
  682. c_a = tor_memcmp(c,a,DIGEST_LEN);
  683. if (c_a < 0 && a_b <= 0)
  684. return 0; /* 4. c a b */
  685. else if (b_c < 0 && c_a < 0)
  686. return 0; /* 5. b c a */
  687. /* In the other cases (a c b; b a c; c b a), a is inside the interval. */
  688. return 1;
  689. }
  690. /** Return true iff <b>query</b> is a syntactically valid service ID (as
  691. * generated by rend_get_service_id). */
  692. int
  693. rend_valid_service_id(const char *query)
  694. {
  695. if (strlen(query) != REND_SERVICE_ID_LEN_BASE32)
  696. return 0;
  697. if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN_BASE32)
  698. return 0;
  699. return 1;
  700. }
  701. /** Return true iff <b>query</b> is a syntactically valid descriptor ID.
  702. * (as generated by rend_get_descriptor_id_bytes). */
  703. int
  704. rend_valid_descriptor_id(const char *query)
  705. {
  706. if (strlen(query) != REND_DESC_ID_V2_LEN_BASE32) {
  707. goto invalid;
  708. }
  709. if (strspn(query, BASE32_CHARS) != REND_DESC_ID_V2_LEN_BASE32) {
  710. goto invalid;
  711. }
  712. return 1;
  713. invalid:
  714. return 0;
  715. }
  716. /** Called when we get a rendezvous-related relay cell on circuit
  717. * <b>circ</b>. Dispatch on rendezvous relay command. */
  718. void
  719. rend_process_relay_cell(circuit_t *circ, const crypt_path_t *layer_hint,
  720. int command, size_t length,
  721. const uint8_t *payload)
  722. {
  723. or_circuit_t *or_circ = NULL;
  724. origin_circuit_t *origin_circ = NULL;
  725. int r = -2;
  726. if (CIRCUIT_IS_ORIGIN(circ)) {
  727. origin_circ = TO_ORIGIN_CIRCUIT(circ);
  728. if (!layer_hint || layer_hint != origin_circ->cpath->prev) {
  729. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  730. "Relay cell (rend purpose %d) from wrong hop on origin circ",
  731. command);
  732. origin_circ = NULL;
  733. }
  734. } else {
  735. or_circ = TO_OR_CIRCUIT(circ);
  736. }
  737. switch (command) {
  738. case RELAY_COMMAND_ESTABLISH_INTRO:
  739. if (or_circ)
  740. r = rend_mid_establish_intro(or_circ,payload,length);
  741. break;
  742. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  743. if (or_circ)
  744. r = rend_mid_establish_rendezvous(or_circ,payload,length);
  745. break;
  746. case RELAY_COMMAND_INTRODUCE1:
  747. if (or_circ)
  748. r = rend_mid_introduce(or_circ,payload,length);
  749. break;
  750. case RELAY_COMMAND_INTRODUCE2:
  751. if (origin_circ)
  752. r = rend_service_receive_introduction(origin_circ,payload,length);
  753. break;
  754. case RELAY_COMMAND_INTRODUCE_ACK:
  755. if (origin_circ)
  756. r = rend_client_introduction_acked(origin_circ,payload,length);
  757. break;
  758. case RELAY_COMMAND_RENDEZVOUS1:
  759. if (or_circ)
  760. r = rend_mid_rendezvous(or_circ,payload,length);
  761. break;
  762. case RELAY_COMMAND_RENDEZVOUS2:
  763. if (origin_circ)
  764. r = rend_client_receive_rendezvous(origin_circ,payload,length);
  765. break;
  766. case RELAY_COMMAND_INTRO_ESTABLISHED:
  767. if (origin_circ)
  768. r = rend_service_intro_established(origin_circ,payload,length);
  769. break;
  770. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  771. if (origin_circ)
  772. r = rend_client_rendezvous_acked(origin_circ,payload,length);
  773. break;
  774. default:
  775. tor_fragile_assert();
  776. }
  777. if (r == -2)
  778. log_info(LD_PROTOCOL, "Dropping cell (type %d) for wrong circuit type.",
  779. command);
  780. }
  781. /** Allocate and return a new rend_data_t with the same
  782. * contents as <b>query</b>. */
  783. rend_data_t *
  784. rend_data_dup(const rend_data_t *data)
  785. {
  786. rend_data_t *data_dup;
  787. tor_assert(data);
  788. data_dup = tor_memdup(data, sizeof(rend_data_t));
  789. data_dup->hsdirs_fp = smartlist_new();
  790. SMARTLIST_FOREACH(data->hsdirs_fp, char *, fp,
  791. smartlist_add(data_dup->hsdirs_fp,
  792. tor_memdup(fp, DIGEST_LEN)));
  793. return data_dup;
  794. }
  795. /** Compute descriptor ID for each replicas and save them. A valid onion
  796. * address must be present in the <b>rend_data</b>.
  797. *
  798. * Return 0 on success else -1. */
  799. static int
  800. compute_desc_id(rend_data_t *rend_data)
  801. {
  802. int ret = 0;
  803. unsigned replica;
  804. time_t now = time(NULL);
  805. tor_assert(rend_data);
  806. /* Compute descriptor ID for each replicas. */
  807. for (replica = 0; replica < ARRAY_LENGTH(rend_data->descriptor_id);
  808. replica++) {
  809. ret = rend_compute_v2_desc_id(rend_data->descriptor_id[replica],
  810. rend_data->onion_address,
  811. rend_data->descriptor_cookie,
  812. now, replica);
  813. if (ret < 0) {
  814. goto end;
  815. }
  816. }
  817. end:
  818. return ret;
  819. }
  820. /** Allocate and initialize a rend_data_t object for a service using the
  821. * given arguments. Only the <b>onion_address</b> is not optional.
  822. *
  823. * Return a valid rend_data_t pointer. */
  824. rend_data_t *
  825. rend_data_service_create(const char *onion_address, const char *pk_digest,
  826. const uint8_t *cookie, rend_auth_type_t auth_type)
  827. {
  828. rend_data_t *rend_data = tor_malloc_zero(sizeof(*rend_data));
  829. /* We need at least one else the call is wrong. */
  830. tor_assert(onion_address != NULL);
  831. if (pk_digest) {
  832. memcpy(rend_data->rend_pk_digest, pk_digest,
  833. sizeof(rend_data->rend_pk_digest));
  834. }
  835. if (cookie) {
  836. memcpy(rend_data->rend_cookie, cookie,
  837. sizeof(rend_data->rend_cookie));
  838. }
  839. strlcpy(rend_data->onion_address, onion_address,
  840. sizeof(rend_data->onion_address));
  841. rend_data->auth_type = auth_type;
  842. /* Won't be used but still need to initialize it for rend_data dup and
  843. * free. */
  844. rend_data->hsdirs_fp = smartlist_new();
  845. return rend_data;
  846. }
  847. /** Allocate and initialize a rend_data_t object for a client request using
  848. * the given arguments. Either an onion address or a descriptor ID is
  849. * needed. Both can be given but only the onion address will be used to make
  850. * the descriptor fetch.
  851. *
  852. * Return a valid rend_data_t pointer or NULL on error meaning the
  853. * descriptor IDs couldn't be computed from the given data. */
  854. rend_data_t *
  855. rend_data_client_create(const char *onion_address, const char *desc_id,
  856. const char *cookie, rend_auth_type_t auth_type)
  857. {
  858. rend_data_t *rend_data = tor_malloc_zero(sizeof(*rend_data));
  859. /* We need at least one else the call is wrong. */
  860. tor_assert(onion_address != NULL || desc_id != NULL);
  861. if (cookie) {
  862. memcpy(rend_data->descriptor_cookie, cookie,
  863. sizeof(rend_data->descriptor_cookie));
  864. }
  865. if (desc_id) {
  866. memcpy(rend_data->desc_id_fetch, desc_id,
  867. sizeof(rend_data->desc_id_fetch));
  868. }
  869. if (onion_address) {
  870. strlcpy(rend_data->onion_address, onion_address,
  871. sizeof(rend_data->onion_address));
  872. if (compute_desc_id(rend_data) < 0) {
  873. goto error;
  874. }
  875. }
  876. rend_data->auth_type = auth_type;
  877. rend_data->hsdirs_fp = smartlist_new();
  878. return rend_data;
  879. error:
  880. rend_data_free(rend_data);
  881. return NULL;
  882. }