rendcommon.c 43 KB

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