rendcommon.c 37 KB

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