rendcommon.c 41 KB

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