rendcommon.c 42 KB

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