rendcommon.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char rendcommon_c_id[] =
  6. "$Id$";
  7. /**
  8. * \file rendcommon.c
  9. * \brief Rendezvous implementation: shared code between
  10. * introducers, services, clients, and rendezvous points.
  11. **/
  12. #include "or.h"
  13. /** Return 0 if one and two are the same service ids, else -1 or 1 */
  14. int
  15. rend_cmp_service_ids(const char *one, const char *two)
  16. {
  17. return strcasecmp(one,two);
  18. }
  19. /** Free the storage held by the service descriptor <b>desc</b>.
  20. */
  21. void
  22. rend_service_descriptor_free(rend_service_descriptor_t *desc)
  23. {
  24. if (desc->pk)
  25. crypto_free_pk_env(desc->pk);
  26. if (desc->intro_nodes) {
  27. SMARTLIST_FOREACH(desc->intro_nodes, rend_intro_point_t *, intro,
  28. rend_intro_point_free(intro););
  29. smartlist_free(desc->intro_nodes);
  30. }
  31. tor_free(desc);
  32. }
  33. /** Length of the descriptor cookie that is used for versioned hidden
  34. * service descriptors. */
  35. #define REND_DESC_COOKIE_LEN 16
  36. /** Length of the replica number that is used to determine the secret ID
  37. * part of versioned hidden service descriptors. */
  38. #define REND_REPLICA_LEN 1
  39. /** Compute the descriptor ID for <b>service_id</b> of length
  40. * <b>REND_SERVICE_ID_LEN</b> and <b>secret_id_part</b> of length
  41. * <b>DIGEST_LEN</b>, and write it to <b>descriptor_id_out</b> of length
  42. * <b>DIGEST_LEN</b>. */
  43. void
  44. rend_get_descriptor_id_bytes(char *descriptor_id_out,
  45. const char *service_id,
  46. const char *secret_id_part)
  47. {
  48. crypto_digest_env_t *digest = crypto_new_digest_env();
  49. crypto_digest_add_bytes(digest, service_id, REND_SERVICE_ID_LEN);
  50. crypto_digest_add_bytes(digest, secret_id_part, DIGEST_LEN);
  51. crypto_digest_get_digest(digest, descriptor_id_out, DIGEST_LEN);
  52. crypto_free_digest_env(digest);
  53. }
  54. /** Compute the secret ID part for time_period,
  55. * a <b>descriptor_cookie</b> of length
  56. * <b>REND_DESC_COOKIE_LEN</b> which may also be <b>NULL</b> if no
  57. * descriptor_cookie shall be used, and <b>replica</b>, and write it to
  58. * <b>secret_id_part</b> of length DIGEST_LEN. */
  59. static void
  60. get_secret_id_part_bytes(char *secret_id_part, uint32_t time_period,
  61. const char *descriptor_cookie, uint8_t replica)
  62. {
  63. crypto_digest_env_t *digest = crypto_new_digest_env();
  64. time_period = htonl(time_period);
  65. crypto_digest_add_bytes(digest, (char*)&time_period, sizeof(uint32_t));
  66. if (descriptor_cookie) {
  67. crypto_digest_add_bytes(digest, descriptor_cookie,
  68. REND_DESC_COOKIE_LEN);
  69. }
  70. crypto_digest_add_bytes(digest, (const char *)&replica, REND_REPLICA_LEN);
  71. crypto_digest_get_digest(digest, secret_id_part, DIGEST_LEN);
  72. crypto_free_digest_env(digest);
  73. }
  74. /** Return the time period for time <b>now</b> plus a potentially
  75. * intended <b>deviation</b> of one or more periods, based on the first byte
  76. * of <b>service_id</b>. */
  77. static uint32_t
  78. get_time_period(time_t now, uint8_t deviation, const char *service_id)
  79. {
  80. /* The time period is the number of REND_TIME_PERIOD_V2_DESC_VALIDITY
  81. * intervals that have passed since the epoch, offset slightly so that
  82. * each service's time periods start and end at a fraction of that
  83. * period based on their first byte. */
  84. return (uint32_t)
  85. (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
  86. / REND_TIME_PERIOD_V2_DESC_VALIDITY + deviation;
  87. }
  88. /** Compute the time in seconds that a descriptor that is generated
  89. * <b>now</b> for <b>service_id</b> will be valid. */
  90. static uint32_t
  91. get_seconds_valid(time_t now, const char *service_id)
  92. {
  93. uint32_t result = REND_TIME_PERIOD_V2_DESC_VALIDITY -
  94. ((uint32_t)
  95. (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
  96. % REND_TIME_PERIOD_V2_DESC_VALIDITY);
  97. return result;
  98. }
  99. /** Compute the binary <b>desc_id_out</b> (DIGEST_LEN bytes long) for a given
  100. * base32-encoded <b>service_id</b> and optional unencoded
  101. * <b>descriptor_cookie</b> of length REND_DESC_COOKIE_LEN,
  102. * at time <b>now</b> for replica number
  103. * <b>replica</b>. <b>desc_id</b> needs to have <b>DIGEST_LEN</b> bytes
  104. * free. Return 0 for success, -1 otherwise. */
  105. int
  106. rend_compute_v2_desc_id(char *desc_id_out, const char *service_id,
  107. const char *descriptor_cookie, time_t now,
  108. uint8_t replica)
  109. {
  110. char service_id_binary[REND_SERVICE_ID_LEN];
  111. char secret_id_part[DIGEST_LEN];
  112. uint32_t time_period;
  113. if (!service_id ||
  114. strlen(service_id) != REND_SERVICE_ID_LEN_BASE32) {
  115. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  116. "Illegal service ID: %s", safe_str(service_id));
  117. return -1;
  118. }
  119. if (replica >= REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS) {
  120. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  121. "Replica number out of range: %d", replica);
  122. return -1;
  123. }
  124. /* Convert service ID to binary. */
  125. if (base32_decode(service_id_binary, REND_SERVICE_ID_LEN,
  126. service_id, REND_SERVICE_ID_LEN_BASE32) < 0) {
  127. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  128. "Illegal characters in service ID: %s",
  129. safe_str(service_id));
  130. return -1;
  131. }
  132. /* Calculate current time-period. */
  133. time_period = get_time_period(now, 0, service_id_binary);
  134. /* Calculate secret-id-part = h(time-period + replica). */
  135. get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
  136. replica);
  137. /* Calculate descriptor ID. */
  138. rend_get_descriptor_id_bytes(desc_id_out, service_id_binary, secret_id_part);
  139. return 0;
  140. }
  141. /* Encode the introduction points in <b>desc</b>, optionally encrypt them with
  142. * an optional <b>descriptor_cookie</b> of length REND_DESC_COOKIE_LEN,
  143. * encode it in base64, and write it to a newly allocated string, and write a
  144. * pointer to it to *<b>ipos_base64</b>. Return 0 for success, -1
  145. * otherwise. */
  146. static int
  147. rend_encode_v2_intro_points(char **ipos_base64,
  148. rend_service_descriptor_t *desc,
  149. const char *descriptor_cookie)
  150. {
  151. size_t unenc_len;
  152. char *unenc = NULL;
  153. size_t unenc_written = 0;
  154. int i;
  155. int r = -1;
  156. /* Assemble unencrypted list of introduction points. */
  157. *ipos_base64 = NULL;
  158. unenc_len = smartlist_len(desc->intro_nodes) * 1000; /* too long, but ok. */
  159. unenc = tor_malloc_zero(unenc_len);
  160. for (i = 0; i < smartlist_len(desc->intro_nodes); i++) {
  161. char id_base32[REND_INTRO_POINT_ID_LEN_BASE32 + 1];
  162. char *onion_key = NULL;
  163. size_t onion_key_len;
  164. crypto_pk_env_t *intro_key;
  165. char *service_key = NULL;
  166. char *address = NULL;
  167. size_t service_key_len;
  168. int res;
  169. rend_intro_point_t *intro = smartlist_get(desc->intro_nodes, i);
  170. /* Obtain extend info with introduction point details. */
  171. extend_info_t *info = intro->extend_info;
  172. /* Encode introduction point ID. */
  173. base32_encode(id_base32, sizeof(id_base32),
  174. info->identity_digest, DIGEST_LEN);
  175. /* Encode onion key. */
  176. if (crypto_pk_write_public_key_to_string(info->onion_key, &onion_key,
  177. &onion_key_len) < 0) {
  178. log_warn(LD_REND, "Could not write onion key.");
  179. goto done;
  180. }
  181. /* Encode intro key. */
  182. intro_key = intro->intro_key;
  183. if (!intro_key ||
  184. crypto_pk_write_public_key_to_string(intro_key, &service_key,
  185. &service_key_len) < 0) {
  186. log_warn(LD_REND, "Could not write intro key.");
  187. tor_free(onion_key);
  188. goto done;
  189. }
  190. /* Assemble everything for this introduction point. */
  191. address = tor_dup_addr(info->addr);
  192. res = tor_snprintf(unenc + unenc_written, unenc_len - unenc_written,
  193. "introduction-point %s\n"
  194. "ip-address %s\n"
  195. "onion-port %d\n"
  196. "onion-key\n%s"
  197. "service-key\n%s",
  198. id_base32,
  199. address,
  200. info->port,
  201. onion_key,
  202. service_key);
  203. tor_free(address);
  204. tor_free(onion_key);
  205. tor_free(service_key);
  206. if (res < 0) {
  207. log_warn(LD_REND, "Not enough space for writing introduction point "
  208. "string.");
  209. goto done;
  210. }
  211. /* Update total number of written bytes for unencrypted intro points. */
  212. unenc_written += res;
  213. }
  214. /* Finalize unencrypted introduction points. */
  215. if (unenc_len < unenc_written + 2) {
  216. log_warn(LD_REND, "Not enough space for finalizing introduction point "
  217. "string.");
  218. goto done;
  219. }
  220. unenc[unenc_written++] = '\n';
  221. unenc[unenc_written++] = 0;
  222. /* If a descriptor cookie is passed, encrypt introduction points. */
  223. if (descriptor_cookie) {
  224. char *enc = tor_malloc_zero(unenc_written + CIPHER_IV_LEN);
  225. crypto_cipher_env_t *cipher =
  226. crypto_create_init_cipher(descriptor_cookie, 1);
  227. int enclen = crypto_cipher_encrypt_with_iv(cipher, enc,
  228. unenc_written + CIPHER_IV_LEN,
  229. unenc, unenc_written);
  230. crypto_free_cipher_env(cipher);
  231. if (enclen < 0) {
  232. log_warn(LD_REND, "Could not encrypt introduction point string.");
  233. tor_free(enc);
  234. goto done;
  235. }
  236. /* Replace original string with the encrypted one. */
  237. tor_free(unenc);
  238. unenc = enc;
  239. unenc_written = enclen;
  240. }
  241. /* Base64-encode introduction points. */
  242. *ipos_base64 = tor_malloc_zero(unenc_written * 2);
  243. if (base64_encode(*ipos_base64, unenc_written * 2, unenc, unenc_written)<0) {
  244. log_warn(LD_REND, "Could not encode introduction point string to "
  245. "base64.");
  246. goto done;
  247. }
  248. r = 0;
  249. done:
  250. if (r<0)
  251. tor_free(*ipos_base64);
  252. tor_free(unenc);
  253. return r;
  254. }
  255. /** Attempt to parse the given <b>desc_str</b> and return true if this
  256. * succeeds, false otherwise. */
  257. static int
  258. rend_desc_v2_is_parsable(rend_encoded_v2_service_descriptor_t *desc)
  259. {
  260. rend_service_descriptor_t *test_parsed = NULL;
  261. char test_desc_id[DIGEST_LEN];
  262. char *test_intro_content = NULL;
  263. size_t test_intro_size;
  264. size_t test_encoded_size;
  265. const char *test_next;
  266. int res = rend_parse_v2_service_descriptor(&test_parsed, test_desc_id,
  267. &test_intro_content,
  268. &test_intro_size,
  269. &test_encoded_size,
  270. &test_next, desc->desc_str);
  271. if (test_parsed)
  272. rend_service_descriptor_free(test_parsed);
  273. tor_free(test_intro_content);
  274. return (res >= 0);
  275. }
  276. /** Free the storage held by an encoded v2 service descriptor. */
  277. void
  278. rend_encoded_v2_service_descriptor_free(
  279. rend_encoded_v2_service_descriptor_t *desc)
  280. {
  281. tor_free(desc->desc_str);
  282. tor_free(desc);
  283. }
  284. /** Free the storage held by an introduction point info. */
  285. void
  286. rend_intro_point_free(rend_intro_point_t *intro)
  287. {
  288. if (intro->extend_info)
  289. extend_info_free(intro->extend_info);
  290. if (intro->intro_key)
  291. crypto_free_pk_env(intro->intro_key);
  292. tor_free(intro);
  293. }
  294. /** Encode a set of rend_encoded_v2_service_descriptor_t's for <b>desc</b>
  295. * at time <b>now</b> using <b>descriptor_cookie</b> (may be <b>NULL</b> if
  296. * introduction points shall not be encrypted) and <b>period</b> (e.g. 0
  297. * for the current period, 1 for the next period, etc.) and add them to
  298. * the existing list <b>descs_out</b>; return the number of seconds that
  299. * the descriptors will be found by clients, or -1 if the encoding was not
  300. * successful. */
  301. int
  302. rend_encode_v2_descriptors(smartlist_t *descs_out,
  303. rend_service_descriptor_t *desc, time_t now,
  304. const char *descriptor_cookie, uint8_t period)
  305. {
  306. char service_id[DIGEST_LEN];
  307. uint32_t time_period;
  308. char *ipos_base64 = NULL;
  309. int k;
  310. uint32_t seconds_valid;
  311. if (!desc) {
  312. log_warn(LD_REND, "Could not encode v2 descriptor: No desc given.");
  313. return -1;
  314. }
  315. /* Obtain service_id from public key. */
  316. crypto_pk_get_digest(desc->pk, service_id);
  317. /* Calculate current time-period. */
  318. time_period = get_time_period(now, period, service_id);
  319. /* Determine how many seconds the descriptor will be valid. */
  320. seconds_valid = period * REND_TIME_PERIOD_V2_DESC_VALIDITY +
  321. get_seconds_valid(now, service_id);
  322. /* Assemble, possibly encrypt, and encode introduction points. */
  323. if (smartlist_len(desc->intro_nodes) > 0 &&
  324. rend_encode_v2_intro_points(&ipos_base64, desc, descriptor_cookie) < 0) {
  325. log_warn(LD_REND, "Encoding of introduction points did not succeed.");
  326. return -1;
  327. }
  328. /* Encode REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS descriptors. */
  329. for (k = 0; k < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; k++) {
  330. char secret_id_part[DIGEST_LEN];
  331. char secret_id_part_base32[REND_SECRET_ID_PART_LEN_BASE32 + 1];
  332. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  333. char *permanent_key = NULL;
  334. size_t permanent_key_len;
  335. char published[ISO_TIME_LEN+1];
  336. int i;
  337. char protocol_versions_string[16]; /* max len: "0,1,2,3,4,5,6,7\0" */
  338. size_t protocol_versions_written;
  339. size_t desc_len;
  340. char *desc_str = NULL;
  341. int result = 0;
  342. size_t written = 0;
  343. char desc_digest[DIGEST_LEN];
  344. rend_encoded_v2_service_descriptor_t *enc =
  345. tor_malloc_zero(sizeof(rend_encoded_v2_service_descriptor_t));
  346. /* Calculate secret-id-part = h(time-period + cookie + replica). */
  347. get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
  348. k);
  349. base32_encode(secret_id_part_base32, sizeof(secret_id_part_base32),
  350. secret_id_part, DIGEST_LEN);
  351. /* Calculate descriptor ID. */
  352. rend_get_descriptor_id_bytes(enc->desc_id, service_id, secret_id_part);
  353. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  354. enc->desc_id, DIGEST_LEN);
  355. /* PEM-encode the public key */
  356. if (crypto_pk_write_public_key_to_string(desc->pk, &permanent_key,
  357. &permanent_key_len) < 0) {
  358. log_warn(LD_BUG, "Could not write public key to string.");
  359. rend_encoded_v2_service_descriptor_free(enc);
  360. goto err;
  361. }
  362. /* Encode timestamp. */
  363. format_iso_time(published, desc->timestamp);
  364. /* Write protocol-versions bitmask to comma-separated value string. */
  365. protocol_versions_written = 0;
  366. for (i = 0; i < 8; i++) {
  367. if (desc->protocols & 1 << i) {
  368. tor_snprintf(protocol_versions_string + protocol_versions_written,
  369. 16 - protocol_versions_written, "%d,", i);
  370. protocol_versions_written += 2;
  371. }
  372. }
  373. if (protocol_versions_written)
  374. protocol_versions_string[protocol_versions_written - 1] = '\0';
  375. else
  376. protocol_versions_string[0]= '\0';
  377. /* Assemble complete descriptor. */
  378. desc_len = 2000 + smartlist_len(desc->intro_nodes) * 1000; /* far too long,
  379. but okay.*/
  380. enc->desc_str = desc_str = tor_malloc_zero(desc_len);
  381. result = tor_snprintf(desc_str, desc_len,
  382. "rendezvous-service-descriptor %s\n"
  383. "version 2\n"
  384. "permanent-key\n%s"
  385. "secret-id-part %s\n"
  386. "publication-time %s\n"
  387. "protocol-versions %s\n",
  388. desc_id_base32,
  389. permanent_key,
  390. secret_id_part_base32,
  391. published,
  392. protocol_versions_string);
  393. tor_free(permanent_key);
  394. if (result < 0) {
  395. log_warn(LD_BUG, "Descriptor ran out of room.");
  396. rend_encoded_v2_service_descriptor_free(enc);
  397. goto err;
  398. }
  399. written = result;
  400. /* Add introduction points. */
  401. if (ipos_base64) {
  402. result = tor_snprintf(desc_str + written, desc_len - written,
  403. "introduction-points\n"
  404. "-----BEGIN MESSAGE-----\n%s"
  405. "-----END MESSAGE-----\n",
  406. ipos_base64);
  407. if (result < 0) {
  408. log_warn(LD_BUG, "could not write introduction points.");
  409. rend_encoded_v2_service_descriptor_free(enc);
  410. goto err;
  411. }
  412. written += result;
  413. }
  414. /* Add signature. */
  415. strlcpy(desc_str + written, "signature\n", desc_len - written);
  416. written += strlen(desc_str + written);
  417. if (crypto_digest(desc_digest, desc_str, written) < 0) {
  418. log_warn(LD_BUG, "could not create digest.");
  419. rend_encoded_v2_service_descriptor_free(enc);
  420. goto err;
  421. }
  422. if (router_append_dirobj_signature(desc_str + written,
  423. desc_len - written,
  424. desc_digest, desc->pk) < 0) {
  425. log_warn(LD_BUG, "Couldn't sign desc.");
  426. rend_encoded_v2_service_descriptor_free(enc);
  427. goto err;
  428. }
  429. written += strlen(desc_str+written);
  430. if (written+2 > desc_len) {
  431. log_warn(LD_BUG, "Could not finish desc.");
  432. rend_encoded_v2_service_descriptor_free(enc);
  433. goto err;
  434. }
  435. desc_str[written++] = '\n';
  436. desc_str[written++] = 0;
  437. /* Check if we can parse our own descriptor. */
  438. if (!rend_desc_v2_is_parsable(enc)) {
  439. log_warn(LD_BUG, "Could not parse my own descriptor: %s", desc_str);
  440. rend_encoded_v2_service_descriptor_free(enc);
  441. goto err;
  442. }
  443. smartlist_add(descs_out, enc);
  444. }
  445. log_info(LD_REND, "Successfully encoded a v2 descriptor and "
  446. "confirmed that it is parsable.");
  447. goto done;
  448. err:
  449. SMARTLIST_FOREACH(descs_out, rend_encoded_v2_service_descriptor_t *, d,
  450. rend_encoded_v2_service_descriptor_free(d););
  451. smartlist_clear(descs_out);
  452. seconds_valid = -1;
  453. done:
  454. tor_free(ipos_base64);
  455. return seconds_valid;
  456. }
  457. /** Encode a service descriptor for <b>desc</b>, and sign it with
  458. * <b>key</b>. Store the descriptor in *<b>str_out</b>, and set
  459. * *<b>len_out</b> to its length.
  460. */
  461. int
  462. rend_encode_service_descriptor(rend_service_descriptor_t *desc,
  463. crypto_pk_env_t *key,
  464. char **str_out, size_t *len_out)
  465. {
  466. char *cp;
  467. char *end;
  468. int i, r;
  469. size_t asn1len;
  470. size_t buflen =
  471. PK_BYTES*2*(smartlist_len(desc->intro_nodes)+2);/*Too long, but ok*/
  472. cp = *str_out = tor_malloc(buflen);
  473. end = cp + PK_BYTES*2*(smartlist_len(desc->intro_nodes)+1);
  474. r = crypto_pk_asn1_encode(desc->pk, cp+2, end-(cp+2));
  475. if (r < 0) {
  476. tor_free(*str_out);
  477. return -1;
  478. }
  479. asn1len = r;
  480. set_uint16(cp, htons((uint16_t)asn1len));
  481. cp += 2+asn1len;
  482. set_uint32(cp, htonl((uint32_t)desc->timestamp));
  483. cp += 4;
  484. set_uint16(cp, htons((uint16_t)smartlist_len(desc->intro_nodes)));
  485. cp += 2;
  486. for (i=0; i < smartlist_len(desc->intro_nodes); ++i) {
  487. rend_intro_point_t *intro = smartlist_get(desc->intro_nodes, i);
  488. char ipoint[HEX_DIGEST_LEN+2];
  489. ipoint[0] = '$';
  490. base16_encode(ipoint+1, HEX_DIGEST_LEN+1,
  491. intro->extend_info->identity_digest,
  492. DIGEST_LEN);
  493. tor_assert(buflen + *str_out >= cp); /* XXX021 This assert is a kludge. */
  494. strlcpy(cp, ipoint, buflen-(cp-*str_out));
  495. cp += strlen(ipoint)+1;
  496. }
  497. note_crypto_pk_op(REND_SERVER);
  498. r = crypto_pk_private_sign_digest(key, cp, *str_out, cp-*str_out);
  499. if (r<0) {
  500. tor_free(*str_out);
  501. return -1;
  502. }
  503. cp += r;
  504. *len_out = (size_t)(cp-*str_out);
  505. return 0;
  506. }
  507. /** Parse a service descriptor at <b>str</b> (<b>len</b> bytes). On
  508. * success, return a newly alloced service_descriptor_t. On failure,
  509. * return NULL.
  510. */
  511. rend_service_descriptor_t *
  512. rend_parse_service_descriptor(const char *str, size_t len)
  513. {
  514. rend_service_descriptor_t *result = NULL;
  515. int i, n_intro_points;
  516. size_t keylen, asn1len;
  517. const char *end, *cp, *eos;
  518. rend_intro_point_t *intro;
  519. result = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  520. cp = str;
  521. end = str+len;
  522. if (end-cp<2) goto truncated;
  523. result->version = 0;
  524. if (end-cp < 2) goto truncated;
  525. asn1len = ntohs(get_uint16(cp));
  526. cp += 2;
  527. if ((size_t)(end-cp) < asn1len) goto truncated;
  528. result->pk = crypto_pk_asn1_decode(cp, asn1len);
  529. if (!result->pk) goto truncated;
  530. cp += asn1len;
  531. if (end-cp < 4) goto truncated;
  532. result->timestamp = (time_t) ntohl(get_uint32(cp));
  533. cp += 4;
  534. result->protocols = 1<<2; /* always use intro format 2 */
  535. if (end-cp < 2) goto truncated;
  536. n_intro_points = ntohs(get_uint16(cp));
  537. cp += 2;
  538. result->intro_nodes = smartlist_create();
  539. for (i=0;i<n_intro_points;++i) {
  540. if (end-cp < 2) goto truncated;
  541. eos = (const char *)memchr(cp,'\0',end-cp);
  542. if (!eos) goto truncated;
  543. /* Write nickname to extend info, but postpone the lookup whether
  544. * we know that router. It's not part of the parsing process. */
  545. intro = tor_malloc_zero(sizeof(rend_intro_point_t));
  546. intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
  547. strlcpy(intro->extend_info->nickname, cp,
  548. sizeof(intro->extend_info->nickname));
  549. smartlist_add(result->intro_nodes, intro);
  550. cp = eos+1;
  551. }
  552. keylen = crypto_pk_keysize(result->pk);
  553. tor_assert(end-cp >= 0);
  554. if ((size_t)(end-cp) < keylen) goto truncated;
  555. if ((size_t)(end-cp) > keylen) {
  556. log_warn(LD_PROTOCOL,
  557. "Signature is %d bytes too long on service descriptor.",
  558. (int)((size_t)(end-cp) - keylen));
  559. goto error;
  560. }
  561. note_crypto_pk_op(REND_CLIENT);
  562. if (crypto_pk_public_checksig_digest(result->pk,
  563. (char*)str,cp-str, /* data */
  564. (char*)cp,end-cp /* signature*/
  565. )<0) {
  566. log_warn(LD_PROTOCOL, "Bad signature on service descriptor.");
  567. goto error;
  568. }
  569. return result;
  570. truncated:
  571. log_warn(LD_PROTOCOL, "Truncated service descriptor.");
  572. error:
  573. rend_service_descriptor_free(result);
  574. return NULL;
  575. }
  576. /** Sets <b>out</b> to the first 10 bytes of the digest of <b>pk</b>,
  577. * base32 encoded. NUL-terminates out. (We use this string to
  578. * identify services in directory requests and .onion URLs.)
  579. */
  580. int
  581. rend_get_service_id(crypto_pk_env_t *pk, char *out)
  582. {
  583. char buf[DIGEST_LEN];
  584. tor_assert(pk);
  585. if (crypto_pk_get_digest(pk, buf) < 0)
  586. return -1;
  587. base32_encode(out, REND_SERVICE_ID_LEN_BASE32+1, buf, REND_SERVICE_ID_LEN);
  588. return 0;
  589. }
  590. /* ==== Rendezvous service descriptor cache. */
  591. /** How old do we let hidden service descriptors get before discarding
  592. * them as too old? */
  593. #define REND_CACHE_MAX_AGE (2*24*60*60)
  594. /** How wrong do we assume our clock may be when checking whether hidden
  595. * services are too old or too new? */
  596. #define REND_CACHE_MAX_SKEW (24*60*60)
  597. /** Map from service id (as generated by rend_get_service_id) to
  598. * rend_cache_entry_t. */
  599. static strmap_t *rend_cache = NULL;
  600. /** Map from descriptor id to rend_cache_entry_t; only for hidden service
  601. * directories. */
  602. static digestmap_t *rend_cache_v2_dir = NULL;
  603. /** Initializes the service descriptor cache.
  604. */
  605. void
  606. rend_cache_init(void)
  607. {
  608. rend_cache = strmap_new();
  609. rend_cache_v2_dir = digestmap_new();
  610. }
  611. /** Helper: free storage held by a single service descriptor cache entry. */
  612. static void
  613. _rend_cache_entry_free(void *p)
  614. {
  615. rend_cache_entry_t *e = p;
  616. rend_service_descriptor_free(e->parsed);
  617. tor_free(e->desc);
  618. tor_free(e);
  619. }
  620. /** Free all storage held by the service descriptor cache. */
  621. void
  622. rend_cache_free_all(void)
  623. {
  624. strmap_free(rend_cache, _rend_cache_entry_free);
  625. digestmap_free(rend_cache_v2_dir, _rend_cache_entry_free);
  626. rend_cache = NULL;
  627. rend_cache_v2_dir = NULL;
  628. }
  629. /** Removes all old entries from the service descriptor cache.
  630. */
  631. void
  632. rend_cache_clean(void)
  633. {
  634. strmap_iter_t *iter;
  635. const char *key;
  636. void *val;
  637. rend_cache_entry_t *ent;
  638. time_t cutoff;
  639. cutoff = time(NULL) - REND_CACHE_MAX_AGE - REND_CACHE_MAX_SKEW;
  640. for (iter = strmap_iter_init(rend_cache); !strmap_iter_done(iter); ) {
  641. strmap_iter_get(iter, &key, &val);
  642. ent = (rend_cache_entry_t*)val;
  643. if (ent->parsed->timestamp < cutoff) {
  644. iter = strmap_iter_next_rmv(rend_cache, iter);
  645. _rend_cache_entry_free(ent);
  646. } else {
  647. iter = strmap_iter_next(rend_cache, iter);
  648. }
  649. }
  650. }
  651. /** Remove all old v2 descriptors and those for which this hidden service
  652. * directory is not responsible for any more. */
  653. void
  654. rend_cache_clean_v2_descs_as_dir(void)
  655. {
  656. digestmap_iter_t *iter;
  657. time_t cutoff = time(NULL) - REND_CACHE_MAX_AGE - REND_CACHE_MAX_SKEW;
  658. for (iter = digestmap_iter_init(rend_cache_v2_dir);
  659. !digestmap_iter_done(iter); ) {
  660. const char *key;
  661. void *val;
  662. rend_cache_entry_t *ent;
  663. digestmap_iter_get(iter, &key, &val);
  664. ent = val;
  665. if (ent->parsed->timestamp < cutoff ||
  666. !hid_serv_responsible_for_desc_id(key)) {
  667. char key_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  668. base32_encode(key_base32, sizeof(key_base32), key, DIGEST_LEN);
  669. log_info(LD_REND, "Removing descriptor with ID '%s' from cache",
  670. safe_str(key_base32));
  671. iter = digestmap_iter_next_rmv(rend_cache_v2_dir, iter);
  672. _rend_cache_entry_free(ent);
  673. } else {
  674. iter = digestmap_iter_next(rend_cache_v2_dir, iter);
  675. }
  676. }
  677. }
  678. /** Determines whether <b>a</b> is in the interval of <b>b</b> (excluded) and
  679. * <b>c</b> (included) in a circular digest ring; returns 1 if this is the
  680. * case, and 0 otherwise.
  681. */
  682. int
  683. rend_id_is_in_interval(const char *a, const char *b, const char *c)
  684. {
  685. int a_b, b_c, c_a;
  686. tor_assert(a);
  687. tor_assert(b);
  688. tor_assert(c);
  689. /* There are five cases in which a is outside the interval ]b,c]: */
  690. a_b = memcmp(a,b,DIGEST_LEN);
  691. if (a_b == 0)
  692. return 0; /* 1. a == b (b is excluded) */
  693. b_c = memcmp(b,c,DIGEST_LEN);
  694. if (b_c == 0)
  695. return 0; /* 2. b == c (interval is empty) */
  696. else if (a_b <= 0 && b_c < 0)
  697. return 0; /* 3. a b c */
  698. c_a = memcmp(c,a,DIGEST_LEN);
  699. if (c_a < 0 && a_b <= 0)
  700. return 0; /* 4. c a b */
  701. else if (b_c < 0 && c_a < 0)
  702. return 0; /* 5. b c a */
  703. /* In the other cases (a c b; b a c; c b a), a is inside the interval. */
  704. return 1;
  705. }
  706. /** Return true iff <b>query</b> is a syntactically valid service ID (as
  707. * generated by rend_get_service_id). */
  708. int
  709. rend_valid_service_id(const char *query)
  710. {
  711. if (strlen(query) != REND_SERVICE_ID_LEN_BASE32)
  712. return 0;
  713. if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN_BASE32)
  714. return 0;
  715. return 1;
  716. }
  717. /** If we have a cached rend_cache_entry_t for the service ID <b>query</b>
  718. * with <b>version</b>, set *<b>e</b> to that entry and return 1.
  719. * Else return 0. If <b>version</b> is nonnegative, only return an entry
  720. * in that descriptor format version. Otherwise (if <b>version</b> is
  721. * negative), return the most recent format we have.
  722. */
  723. int
  724. rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e)
  725. {
  726. char key[REND_SERVICE_ID_LEN_BASE32+2]; /* <version><query>\0 */
  727. tor_assert(rend_cache);
  728. if (!rend_valid_service_id(query))
  729. return -1;
  730. *e = NULL;
  731. if (version != 0) {
  732. tor_snprintf(key, sizeof(key), "2%s", query);
  733. *e = strmap_get_lc(rend_cache, key);
  734. }
  735. if (!*e && version != 2) {
  736. tor_snprintf(key, sizeof(key), "0%s", query);
  737. *e = strmap_get_lc(rend_cache, key);
  738. }
  739. if (!*e)
  740. return 0;
  741. return 1;
  742. }
  743. /** <b>query</b> is a base-32'ed service id. If it's malformed, return -1.
  744. * Else look it up.
  745. * - If it is found, point *desc to it, and write its length into
  746. * *desc_len, and return 1.
  747. * - If it is not found, return 0.
  748. * Note: calls to rend_cache_clean or rend_cache_store may invalidate
  749. * *desc.
  750. */
  751. int
  752. rend_cache_lookup_desc(const char *query, int version, const char **desc,
  753. size_t *desc_len)
  754. {
  755. rend_cache_entry_t *e;
  756. int r;
  757. r = rend_cache_lookup_entry(query,version,&e);
  758. if (r <= 0) return r;
  759. *desc = e->desc;
  760. *desc_len = e->len;
  761. return 1;
  762. }
  763. /** Lookup the v2 service descriptor with base32-encoded <b>desc_id</b> and
  764. * copy the pointer to it to *<b>desc</b>. Return 1 on success, 0 on
  765. * well-formed-but-not-found, and -1 on failure.
  766. */
  767. int
  768. rend_cache_lookup_v2_desc_as_dir(const char *desc_id, const char **desc)
  769. {
  770. rend_cache_entry_t *e;
  771. char desc_id_digest[DIGEST_LEN];
  772. tor_assert(rend_cache_v2_dir);
  773. if (base32_decode(desc_id_digest, DIGEST_LEN,
  774. desc_id, REND_DESC_ID_V2_LEN_BASE32) < 0) {
  775. log_warn(LD_REND, "Descriptor ID contains illegal characters: %s",
  776. safe_str(desc_id));
  777. return -1;
  778. }
  779. /* Determine if we are responsible. */
  780. if (hid_serv_responsible_for_desc_id(desc_id_digest) < 0) {
  781. log_info(LD_REND, "Could not answer fetch request for v2 descriptor; "
  782. "either we are no hidden service directory, or we are "
  783. "not responsible for the requested ID.");
  784. return -1;
  785. }
  786. /* Lookup descriptor and return. */
  787. e = digestmap_get(rend_cache_v2_dir, desc_id_digest);
  788. if (e) {
  789. *desc = e->desc;
  790. return 1;
  791. }
  792. return 0;
  793. }
  794. /** Parse *desc, calculate its service id, and store it in the cache.
  795. * If we have a newer v0 descriptor with the same ID, ignore this one.
  796. * If we have an older descriptor with the same ID, replace it.
  797. * If we are acting as client due to the published flag and have any v2
  798. * descriptor with the same ID, reject this one in order to not get
  799. * confused with having both versions for the same service.
  800. * Return -1 if it's malformed or otherwise rejected; return 0 if
  801. * it's the same or older than one we've already got; return 1 if
  802. * it's novel. The published flag tells us if we store the descriptor
  803. * in our role as directory (1) or if we cache it as client (0).
  804. */
  805. int
  806. rend_cache_store(const char *desc, size_t desc_len, int published)
  807. {
  808. rend_cache_entry_t *e;
  809. rend_service_descriptor_t *parsed;
  810. char query[REND_SERVICE_ID_LEN_BASE32+1];
  811. char key[REND_SERVICE_ID_LEN_BASE32+2]; /* 0<query>\0 */
  812. time_t now;
  813. or_options_t *options = get_options();
  814. tor_assert(rend_cache);
  815. parsed = rend_parse_service_descriptor(desc,desc_len);
  816. if (!parsed) {
  817. log_warn(LD_PROTOCOL,"Couldn't parse service descriptor.");
  818. return -1;
  819. }
  820. if (rend_get_service_id(parsed->pk, query)<0) {
  821. log_warn(LD_BUG,"Couldn't compute service ID.");
  822. rend_service_descriptor_free(parsed);
  823. return -1;
  824. }
  825. now = time(NULL);
  826. if (parsed->timestamp < now-REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
  827. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  828. "Service descriptor %s is too old.", safe_str(query));
  829. rend_service_descriptor_free(parsed);
  830. return -1;
  831. }
  832. if (parsed->timestamp > now+REND_CACHE_MAX_SKEW) {
  833. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  834. "Service descriptor %s is too far in the future.", safe_str(query));
  835. rend_service_descriptor_free(parsed);
  836. return -1;
  837. }
  838. /* Do we have a v2 descriptor and fetched this descriptor as a client? */
  839. tor_snprintf(key, sizeof(key), "2%s", query);
  840. if (!published && strmap_get_lc(rend_cache, key)) {
  841. log_info(LD_REND, "We already have a v2 descriptor for service %s.",
  842. safe_str(query));
  843. return -1;
  844. }
  845. /* report novel publication to statistics */
  846. if (published && options->HSAuthorityRecordStats) {
  847. hs_usage_note_publish_total(query, now);
  848. }
  849. tor_snprintf(key, sizeof(key), "0%s", query);
  850. e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, key);
  851. if (e && e->parsed->timestamp > parsed->timestamp) {
  852. log_info(LD_REND,"We already have a newer service descriptor %s with the "
  853. "same ID and version.", safe_str(query));
  854. rend_service_descriptor_free(parsed);
  855. return 0;
  856. }
  857. if (e && e->len == desc_len && !memcmp(desc,e->desc,desc_len)) {
  858. log_info(LD_REND,"We already have this service descriptor %s.",
  859. safe_str(query));
  860. e->received = time(NULL);
  861. rend_service_descriptor_free(parsed);
  862. return 0;
  863. }
  864. if (!e) {
  865. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  866. strmap_set_lc(rend_cache, key, e);
  867. /* report novel publication to statistics */
  868. if (published && options->HSAuthorityRecordStats) {
  869. hs_usage_note_publish_novel(query, now);
  870. }
  871. } else {
  872. rend_service_descriptor_free(e->parsed);
  873. tor_free(e->desc);
  874. }
  875. e->received = time(NULL);
  876. e->parsed = parsed;
  877. e->len = desc_len;
  878. e->desc = tor_malloc(desc_len);
  879. memcpy(e->desc, desc, desc_len);
  880. log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
  881. safe_str(query), (int)desc_len);
  882. return 1;
  883. }
  884. /** Parse the v2 service descriptor(s) in <b>desc</b> and store it/them to the
  885. * local rend cache. Don't attempt to decrypt the included list of introduction
  886. * points (as we don't have a descriptor cookie for it).
  887. *
  888. * If we have a newer descriptor with the same ID, ignore this one.
  889. * If we have an older descriptor with the same ID, replace it.
  890. * Return -2 if we are not acting as hidden service directory;
  891. * return -1 if the descriptor(s) were not parsable; return 0 if all
  892. * descriptors are the same or older than those we've already got;
  893. * return a positive number for the number of novel stored descriptors.
  894. */
  895. int
  896. rend_cache_store_v2_desc_as_dir(const char *desc)
  897. {
  898. rend_service_descriptor_t *parsed;
  899. char desc_id[DIGEST_LEN];
  900. char *intro_content;
  901. size_t intro_size;
  902. size_t encoded_size;
  903. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  904. int number_parsed = 0, number_stored = 0;
  905. const char *current_desc = desc;
  906. const char *next_desc;
  907. rend_cache_entry_t *e;
  908. time_t now = time(NULL);
  909. tor_assert(rend_cache_v2_dir);
  910. tor_assert(desc);
  911. if (!hid_serv_acting_as_directory()) {
  912. /* Cannot store descs, because we are (currently) not acting as
  913. * hidden service directory. */
  914. log_info(LD_REND, "Cannot store descs: Not acting as hs dir");
  915. return -2;
  916. }
  917. while (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
  918. &intro_size, &encoded_size,
  919. &next_desc, current_desc) >= 0) {
  920. number_parsed++;
  921. /* We don't care about the introduction points. */
  922. tor_free(intro_content);
  923. /* For pretty log statements. */
  924. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  925. desc_id, DIGEST_LEN);
  926. /* Is desc ID in the range that we are (directly or indirectly) responsible
  927. * for? */
  928. if (!hid_serv_responsible_for_desc_id(desc_id)) {
  929. log_info(LD_REND, "Service descriptor with desc ID %s is not in "
  930. "interval that we are responsible for.",
  931. safe_str(desc_id_base32));
  932. goto skip;
  933. }
  934. /* Is descriptor too old? */
  935. if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
  936. log_info(LD_REND, "Service descriptor with desc ID %s is too old.",
  937. safe_str(desc_id_base32));
  938. goto skip;
  939. }
  940. /* Is descriptor too far in the future? */
  941. if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
  942. log_info(LD_REND, "Service descriptor with desc ID %s is too far in the "
  943. "future.",
  944. safe_str(desc_id_base32));
  945. goto skip;
  946. }
  947. /* Do we already have a newer descriptor? */
  948. e = digestmap_get(rend_cache_v2_dir, desc_id);
  949. if (e && e->parsed->timestamp > parsed->timestamp) {
  950. log_info(LD_REND, "We already have a newer service descriptor with the "
  951. "same desc ID %s and version.",
  952. safe_str(desc_id_base32));
  953. goto skip;
  954. }
  955. /* Do we already have this descriptor? */
  956. if (e && !strcmp(desc, e->desc)) {
  957. log_info(LD_REND, "We already have this service descriptor with desc "
  958. "ID %s.", safe_str(desc_id_base32));
  959. e->received = time(NULL);
  960. goto skip;
  961. }
  962. /* Store received descriptor. */
  963. if (!e) {
  964. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  965. digestmap_set(rend_cache_v2_dir, desc_id, e);
  966. } else {
  967. rend_service_descriptor_free(e->parsed);
  968. tor_free(e->desc);
  969. }
  970. e->received = time(NULL);
  971. e->parsed = parsed;
  972. e->desc = tor_strndup(current_desc, encoded_size);
  973. e->len = encoded_size;
  974. log_info(LD_REND, "Successfully stored service descriptor with desc ID "
  975. "'%s' and len %d.",
  976. safe_str(desc_id_base32), (int)encoded_size);
  977. number_stored++;
  978. goto advance;
  979. skip:
  980. rend_service_descriptor_free(parsed);
  981. advance:
  982. /* advance to next descriptor, if available. */
  983. current_desc = next_desc;
  984. /* check if there is a next descriptor. */
  985. if (!current_desc ||
  986. strcmpstart(current_desc, "rendezvous-service-descriptor "))
  987. break;
  988. }
  989. if (!number_parsed) {
  990. log_info(LD_REND, "Could not parse any descriptor.");
  991. return -1;
  992. }
  993. log_info(LD_REND, "Parsed %d and added %d descriptor%s.",
  994. number_parsed, number_stored, number_stored != 1 ? "s" : "");
  995. return number_stored;
  996. }
  997. /** Parse the v2 service descriptor in <b>desc</b>, decrypt the included list
  998. * of introduction points with <b>descriptor_cookie</b> (which may also be
  999. * <b>NULL</b> if decryption is not necessary), and store the descriptor to
  1000. * the local cache under its version and service id.
  1001. *
  1002. * If we have a newer v2 descriptor with the same ID, ignore this one.
  1003. * If we have an older descriptor with the same ID, replace it.
  1004. * If we have any v0 descriptor with the same ID, reject this one in order
  1005. * to not get confused with having both versions for the same service.
  1006. * Return -2 if it's malformed or otherwise rejected; return -1 if we
  1007. * already have a v0 descriptor here; return 0 if it's the same or older
  1008. * than one we've already got; return 1 if it's novel.
  1009. */
  1010. int
  1011. rend_cache_store_v2_desc_as_client(const char *desc,
  1012. const char *descriptor_cookie)
  1013. {
  1014. /*XXXX this seems to have a bit of duplicate code with
  1015. * rend_cache_store_v2_desc_as_dir(). Fix that. */
  1016. /* Though having similar elements, both functions were separated on
  1017. * purpose:
  1018. * - dirs don't care about encoded/encrypted introduction points, clients
  1019. * do.
  1020. * - dirs store descriptors in a separate cache by descriptor ID, whereas
  1021. * clients store them by service ID; both caches are different data
  1022. * structures and have different access methods.
  1023. * - dirs store a descriptor only if they are responsible for its ID,
  1024. * clients do so in every way (because they have requested it before).
  1025. * - dirs can process multiple concatenated descriptors which is required
  1026. * for replication, whereas clients only accept a single descriptor.
  1027. * Thus, combining both methods would result in a lot of if statements
  1028. * which probably would not improve, but worsen code readability. -KL */
  1029. rend_service_descriptor_t *parsed = NULL;
  1030. char desc_id[DIGEST_LEN];
  1031. char *intro_content = NULL;
  1032. size_t intro_size;
  1033. size_t encoded_size;
  1034. const char *next_desc;
  1035. time_t now = time(NULL);
  1036. char key[REND_SERVICE_ID_LEN_BASE32+2];
  1037. char service_id[REND_SERVICE_ID_LEN_BASE32+1];
  1038. rend_cache_entry_t *e;
  1039. tor_assert(rend_cache);
  1040. tor_assert(desc);
  1041. /* Parse the descriptor. */
  1042. if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
  1043. &intro_size, &encoded_size,
  1044. &next_desc, desc) < 0) {
  1045. if (parsed) rend_service_descriptor_free(parsed);
  1046. tor_free(intro_content);
  1047. log_warn(LD_REND, "Could not parse descriptor.");
  1048. return -2;
  1049. }
  1050. /* Compute service ID from public key. */
  1051. if (rend_get_service_id(parsed->pk, service_id)<0) {
  1052. log_warn(LD_REND, "Couldn't compute service ID.");
  1053. rend_service_descriptor_free(parsed);
  1054. tor_free(intro_content);
  1055. return -2;
  1056. }
  1057. /* Decode/decrypt introduction points. */
  1058. if (intro_content) {
  1059. if (rend_decrypt_introduction_points(parsed, descriptor_cookie,
  1060. intro_content, intro_size) < 0) {
  1061. log_warn(LD_PROTOCOL,"Couldn't decode/decrypt introduction points.");
  1062. rend_service_descriptor_free(parsed);
  1063. tor_free(intro_content);
  1064. return -2;
  1065. }
  1066. } else {
  1067. parsed->intro_nodes = smartlist_create();
  1068. }
  1069. /* We don't need the encoded/encrypted introduction points any longer. */
  1070. tor_free(intro_content);
  1071. /* Is descriptor too old? */
  1072. if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
  1073. log_warn(LD_REND, "Service descriptor with service ID %s is too old.",
  1074. safe_str(service_id));
  1075. rend_service_descriptor_free(parsed);
  1076. return -2;
  1077. }
  1078. /* Is descriptor too far in the future? */
  1079. if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
  1080. log_warn(LD_REND, "Service descriptor with service ID %s is too far in "
  1081. "the future.", safe_str(service_id));
  1082. rend_service_descriptor_free(parsed);
  1083. return -2;
  1084. }
  1085. /* Do we have a v0 descriptor? */
  1086. tor_snprintf(key, sizeof(key), "0%s", service_id);
  1087. if (strmap_get_lc(rend_cache, key)) {
  1088. log_info(LD_REND, "We already have a v0 descriptor for service ID %s.",
  1089. safe_str(service_id));
  1090. return -1;
  1091. }
  1092. /* Do we already have a newer descriptor? */
  1093. tor_snprintf(key, sizeof(key), "2%s", service_id);
  1094. e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, key);
  1095. if (e && e->parsed->timestamp > parsed->timestamp) {
  1096. log_info(LD_REND, "We already have a newer service descriptor for "
  1097. "service ID %s with the same desc ID and version.",
  1098. safe_str(service_id));
  1099. rend_service_descriptor_free(parsed);
  1100. return 0;
  1101. }
  1102. /* Do we already have this descriptor? */
  1103. if (e && !strcmp(desc, e->desc)) {
  1104. log_info(LD_REND,"We already have this service descriptor %s.",
  1105. safe_str(service_id));
  1106. e->received = time(NULL);
  1107. rend_service_descriptor_free(parsed);
  1108. return 0;
  1109. }
  1110. if (!e) {
  1111. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  1112. strmap_set_lc(rend_cache, key, e);
  1113. } else {
  1114. rend_service_descriptor_free(e->parsed);
  1115. tor_free(e->desc);
  1116. }
  1117. e->received = time(NULL);
  1118. e->parsed = parsed;
  1119. e->desc = tor_malloc_zero(encoded_size + 1);
  1120. strlcpy(e->desc, desc, encoded_size + 1);
  1121. e->len = encoded_size;
  1122. log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
  1123. safe_str(service_id), (int)encoded_size);
  1124. return 1;
  1125. }
  1126. /** Called when we get a rendezvous-related relay cell on circuit
  1127. * <b>circ</b>. Dispatch on rendezvous relay command. */
  1128. void
  1129. rend_process_relay_cell(circuit_t *circ, int command, size_t length,
  1130. const char *payload)
  1131. {
  1132. or_circuit_t *or_circ = NULL;
  1133. origin_circuit_t *origin_circ = NULL;
  1134. int r = -2;
  1135. if (CIRCUIT_IS_ORIGIN(circ))
  1136. origin_circ = TO_ORIGIN_CIRCUIT(circ);
  1137. else
  1138. or_circ = TO_OR_CIRCUIT(circ);
  1139. switch (command) {
  1140. case RELAY_COMMAND_ESTABLISH_INTRO:
  1141. if (or_circ)
  1142. r = rend_mid_establish_intro(or_circ,payload,length);
  1143. break;
  1144. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  1145. if (or_circ)
  1146. r = rend_mid_establish_rendezvous(or_circ,payload,length);
  1147. break;
  1148. case RELAY_COMMAND_INTRODUCE1:
  1149. if (or_circ)
  1150. r = rend_mid_introduce(or_circ,payload,length);
  1151. break;
  1152. case RELAY_COMMAND_INTRODUCE2:
  1153. if (origin_circ)
  1154. r = rend_service_introduce(origin_circ,payload,length);
  1155. break;
  1156. case RELAY_COMMAND_INTRODUCE_ACK:
  1157. if (origin_circ)
  1158. r = rend_client_introduction_acked(origin_circ,payload,length);
  1159. break;
  1160. case RELAY_COMMAND_RENDEZVOUS1:
  1161. if (or_circ)
  1162. r = rend_mid_rendezvous(or_circ,payload,length);
  1163. break;
  1164. case RELAY_COMMAND_RENDEZVOUS2:
  1165. if (origin_circ)
  1166. r = rend_client_receive_rendezvous(origin_circ,payload,length);
  1167. break;
  1168. case RELAY_COMMAND_INTRO_ESTABLISHED:
  1169. if (origin_circ)
  1170. r = rend_service_intro_established(origin_circ,payload,length);
  1171. break;
  1172. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  1173. if (origin_circ)
  1174. r = rend_client_rendezvous_acked(origin_circ,payload,length);
  1175. break;
  1176. default:
  1177. tor_fragile_assert();
  1178. }
  1179. if (r == -2)
  1180. log_info(LD_PROTOCOL, "Dropping cell (type %d) for wrong circuit type.",
  1181. command);
  1182. }
  1183. /** Return the number of entries in our rendezvous descriptor cache. */
  1184. int
  1185. rend_cache_size(void)
  1186. {
  1187. return strmap_size(rend_cache);
  1188. }