rendcommon.c 42 KB

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