rendcommon.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file rendcommon.c
  6. * \brief Rendezvous implementation: shared code between
  7. * introducers, services, clients, and rendezvous points.
  8. **/
  9. #include "or.h"
  10. #include "circuitbuild.h"
  11. #include "config.h"
  12. #include "control.h"
  13. #include "rendclient.h"
  14. #include "rendcommon.h"
  15. #include "rendmid.h"
  16. #include "rendservice.h"
  17. #include "rephist.h"
  18. #include "router.h"
  19. #include "routerlist.h"
  20. #include "routerparse.h"
  21. #include "networkstatus.h"
  22. /** Return 0 if one and two are the same service ids, else -1 or 1 */
  23. int
  24. rend_cmp_service_ids(const char *one, const char *two)
  25. {
  26. return strcasecmp(one,two);
  27. }
  28. /** Free the storage held by the service descriptor <b>desc</b>.
  29. */
  30. void
  31. rend_service_descriptor_free(rend_service_descriptor_t *desc)
  32. {
  33. if (!desc)
  34. return;
  35. if (desc->pk)
  36. crypto_pk_free(desc->pk);
  37. if (desc->intro_nodes) {
  38. SMARTLIST_FOREACH(desc->intro_nodes, rend_intro_point_t *, intro,
  39. rend_intro_point_free(intro););
  40. smartlist_free(desc->intro_nodes);
  41. }
  42. if (desc->successful_uploads) {
  43. SMARTLIST_FOREACH(desc->successful_uploads, char *, c, tor_free(c););
  44. smartlist_free(desc->successful_uploads);
  45. }
  46. tor_free(desc);
  47. }
  48. /** Length of the descriptor cookie that is used for versioned hidden
  49. * service descriptors. */
  50. #define REND_DESC_COOKIE_LEN 16
  51. /** Length of the replica number that is used to determine the secret ID
  52. * part of versioned hidden service descriptors. */
  53. #define REND_REPLICA_LEN 1
  54. /** Compute the descriptor ID for <b>service_id</b> of length
  55. * <b>REND_SERVICE_ID_LEN</b> and <b>secret_id_part</b> of length
  56. * <b>DIGEST_LEN</b>, and write it to <b>descriptor_id_out</b> of length
  57. * <b>DIGEST_LEN</b>. */
  58. void
  59. rend_get_descriptor_id_bytes(char *descriptor_id_out,
  60. const char *service_id,
  61. const char *secret_id_part)
  62. {
  63. crypto_digest_t *digest = crypto_digest_new();
  64. crypto_digest_add_bytes(digest, service_id, REND_SERVICE_ID_LEN);
  65. crypto_digest_add_bytes(digest, secret_id_part, DIGEST_LEN);
  66. crypto_digest_get_digest(digest, descriptor_id_out, DIGEST_LEN);
  67. crypto_digest_free(digest);
  68. }
  69. /** Compute the secret ID part for time_period,
  70. * a <b>descriptor_cookie</b> of length
  71. * <b>REND_DESC_COOKIE_LEN</b> which may also be <b>NULL</b> if no
  72. * descriptor_cookie shall be used, and <b>replica</b>, and write it to
  73. * <b>secret_id_part</b> of length DIGEST_LEN. */
  74. static void
  75. get_secret_id_part_bytes(char *secret_id_part, uint32_t time_period,
  76. const char *descriptor_cookie, uint8_t replica)
  77. {
  78. crypto_digest_t *digest = crypto_digest_new();
  79. time_period = htonl(time_period);
  80. crypto_digest_add_bytes(digest, (char*)&time_period, sizeof(uint32_t));
  81. if (descriptor_cookie) {
  82. crypto_digest_add_bytes(digest, descriptor_cookie,
  83. REND_DESC_COOKIE_LEN);
  84. }
  85. crypto_digest_add_bytes(digest, (const char *)&replica, REND_REPLICA_LEN);
  86. crypto_digest_get_digest(digest, secret_id_part, DIGEST_LEN);
  87. crypto_digest_free(digest);
  88. }
  89. /** Return the time period for time <b>now</b> plus a potentially
  90. * intended <b>deviation</b> of one or more periods, based on the first byte
  91. * of <b>service_id</b>. */
  92. static uint32_t
  93. get_time_period(time_t now, uint8_t deviation, const char *service_id)
  94. {
  95. /* The time period is the number of REND_TIME_PERIOD_V2_DESC_VALIDITY
  96. * intervals that have passed since the epoch, offset slightly so that
  97. * each service's time periods start and end at a fraction of that
  98. * period based on their first byte. */
  99. return (uint32_t)
  100. (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
  101. / REND_TIME_PERIOD_V2_DESC_VALIDITY + deviation;
  102. }
  103. /** Compute the time in seconds that a descriptor that is generated
  104. * <b>now</b> for <b>service_id</b> will be valid. */
  105. static uint32_t
  106. get_seconds_valid(time_t now, const char *service_id)
  107. {
  108. uint32_t result = REND_TIME_PERIOD_V2_DESC_VALIDITY -
  109. ((uint32_t)
  110. (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
  111. % REND_TIME_PERIOD_V2_DESC_VALIDITY);
  112. return result;
  113. }
  114. /** Compute the binary <b>desc_id_out</b> (DIGEST_LEN bytes long) for a given
  115. * base32-encoded <b>service_id</b> and optional unencoded
  116. * <b>descriptor_cookie</b> of length REND_DESC_COOKIE_LEN,
  117. * at time <b>now</b> for replica number
  118. * <b>replica</b>. <b>desc_id</b> needs to have <b>DIGEST_LEN</b> bytes
  119. * free. Return 0 for success, -1 otherwise. */
  120. int
  121. rend_compute_v2_desc_id(char *desc_id_out, const char *service_id,
  122. const char *descriptor_cookie, time_t now,
  123. uint8_t replica)
  124. {
  125. char service_id_binary[REND_SERVICE_ID_LEN];
  126. char secret_id_part[DIGEST_LEN];
  127. uint32_t time_period;
  128. if (!service_id ||
  129. strlen(service_id) != REND_SERVICE_ID_LEN_BASE32) {
  130. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  131. "Illegal service ID: %s",
  132. safe_str(service_id));
  133. return -1;
  134. }
  135. if (replica >= REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS) {
  136. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  137. "Replica number out of range: %d", replica);
  138. return -1;
  139. }
  140. /* Convert service ID to binary. */
  141. if (base32_decode(service_id_binary, REND_SERVICE_ID_LEN,
  142. service_id, REND_SERVICE_ID_LEN_BASE32) < 0) {
  143. log_warn(LD_REND, "Could not compute v2 descriptor ID: "
  144. "Illegal characters in service ID: %s",
  145. safe_str_client(service_id));
  146. return -1;
  147. }
  148. /* Calculate current time-period. */
  149. time_period = get_time_period(now, 0, service_id_binary);
  150. /* Calculate secret-id-part = h(time-period | desc-cookie | replica). */
  151. get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
  152. replica);
  153. /* Calculate descriptor ID: H(permanent-id | secret-id-part) */
  154. rend_get_descriptor_id_bytes(desc_id_out, service_id_binary, secret_id_part);
  155. return 0;
  156. }
  157. /** Encode the introduction points in <b>desc</b> and write the result to a
  158. * newly allocated string pointed to by <b>encoded</b>. Return 0 for
  159. * success, -1 otherwise. */
  160. static int
  161. rend_encode_v2_intro_points(char **encoded, rend_service_descriptor_t *desc)
  162. {
  163. size_t unenc_len;
  164. char *unenc = NULL;
  165. size_t unenc_written = 0;
  166. int i;
  167. int r = -1;
  168. /* Assemble unencrypted list of introduction points. */
  169. unenc_len = smartlist_len(desc->intro_nodes) * 1000; /* too long, but ok. */
  170. unenc = tor_malloc_zero(unenc_len);
  171. for (i = 0; i < smartlist_len(desc->intro_nodes); i++) {
  172. char id_base32[REND_INTRO_POINT_ID_LEN_BASE32 + 1];
  173. char *onion_key = NULL;
  174. size_t onion_key_len;
  175. crypto_pk_t *intro_key;
  176. char *service_key = NULL;
  177. char *address = NULL;
  178. size_t service_key_len;
  179. int res;
  180. rend_intro_point_t *intro = smartlist_get(desc->intro_nodes, i);
  181. /* Obtain extend info with introduction point details. */
  182. extend_info_t *info = intro->extend_info;
  183. /* Encode introduction point ID. */
  184. base32_encode(id_base32, sizeof(id_base32),
  185. info->identity_digest, DIGEST_LEN);
  186. /* Encode onion key. */
  187. if (crypto_pk_write_public_key_to_string(info->onion_key, &onion_key,
  188. &onion_key_len) < 0) {
  189. log_warn(LD_REND, "Could not write onion key.");
  190. goto done;
  191. }
  192. /* Encode intro key. */
  193. intro_key = intro->intro_key;
  194. if (!intro_key ||
  195. crypto_pk_write_public_key_to_string(intro_key, &service_key,
  196. &service_key_len) < 0) {
  197. log_warn(LD_REND, "Could not write intro key.");
  198. tor_free(onion_key);
  199. goto done;
  200. }
  201. /* Assemble everything for this introduction point. */
  202. address = tor_addr_to_str_dup(&info->addr);
  203. res = tor_snprintf(unenc + unenc_written, unenc_len - unenc_written,
  204. "introduction-point %s\n"
  205. "ip-address %s\n"
  206. "onion-port %d\n"
  207. "onion-key\n%s"
  208. "service-key\n%s",
  209. id_base32,
  210. address,
  211. info->port,
  212. onion_key,
  213. service_key);
  214. tor_free(address);
  215. tor_free(onion_key);
  216. tor_free(service_key);
  217. if (res < 0) {
  218. log_warn(LD_REND, "Not enough space for writing introduction point "
  219. "string.");
  220. goto done;
  221. }
  222. /* Update total number of written bytes for unencrypted intro points. */
  223. unenc_written += res;
  224. }
  225. /* Finalize unencrypted introduction points. */
  226. if (unenc_len < unenc_written + 2) {
  227. log_warn(LD_REND, "Not enough space for finalizing introduction point "
  228. "string.");
  229. goto done;
  230. }
  231. unenc[unenc_written++] = '\n';
  232. unenc[unenc_written++] = 0;
  233. *encoded = unenc;
  234. r = 0;
  235. done:
  236. if (r<0)
  237. tor_free(unenc);
  238. return r;
  239. }
  240. /** Encrypt the encoded introduction points in <b>encoded</b> using
  241. * authorization type 'basic' with <b>client_cookies</b> and write the
  242. * result to a newly allocated string pointed to by <b>encrypted_out</b> of
  243. * length <b>encrypted_len_out</b>. Return 0 for success, -1 otherwise. */
  244. static int
  245. rend_encrypt_v2_intro_points_basic(char **encrypted_out,
  246. size_t *encrypted_len_out,
  247. const char *encoded,
  248. smartlist_t *client_cookies)
  249. {
  250. int r = -1, i, pos, enclen, client_blocks;
  251. size_t len, client_entries_len;
  252. char *enc = NULL, iv[CIPHER_IV_LEN], *client_part = NULL,
  253. session_key[CIPHER_KEY_LEN];
  254. smartlist_t *encrypted_session_keys = NULL;
  255. crypto_digest_t *digest;
  256. crypto_cipher_t *cipher;
  257. tor_assert(encoded);
  258. tor_assert(client_cookies && smartlist_len(client_cookies) > 0);
  259. /* Generate session key. */
  260. crypto_rand(session_key, CIPHER_KEY_LEN);
  261. /* Determine length of encrypted introduction points including session
  262. * keys. */
  263. client_blocks = 1 + ((smartlist_len(client_cookies) - 1) /
  264. REND_BASIC_AUTH_CLIENT_MULTIPLE);
  265. client_entries_len = client_blocks * REND_BASIC_AUTH_CLIENT_MULTIPLE *
  266. REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
  267. len = 2 + client_entries_len + CIPHER_IV_LEN + strlen(encoded);
  268. if (client_blocks >= 256) {
  269. log_warn(LD_REND, "Too many clients in introduction point string.");
  270. goto done;
  271. }
  272. enc = tor_malloc_zero(len);
  273. enc[0] = 0x01; /* type of authorization. */
  274. enc[1] = (uint8_t)client_blocks;
  275. /* Encrypt with random session key. */
  276. enclen = crypto_cipher_encrypt_with_iv(session_key,
  277. enc + 2 + client_entries_len,
  278. CIPHER_IV_LEN + strlen(encoded), encoded, strlen(encoded));
  279. if (enclen < 0) {
  280. log_warn(LD_REND, "Could not encrypt introduction point string.");
  281. goto done;
  282. }
  283. memcpy(iv, enc + 2 + client_entries_len, CIPHER_IV_LEN);
  284. /* Encrypt session key for cookies, determine client IDs, and put both
  285. * in a smartlist. */
  286. encrypted_session_keys = smartlist_new();
  287. SMARTLIST_FOREACH_BEGIN(client_cookies, const char *, cookie) {
  288. client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  289. /* Encrypt session key. */
  290. cipher = crypto_cipher_new(cookie);
  291. if (crypto_cipher_encrypt(cipher, client_part +
  292. REND_BASIC_AUTH_CLIENT_ID_LEN,
  293. session_key, CIPHER_KEY_LEN) < 0) {
  294. log_warn(LD_REND, "Could not encrypt session key for client.");
  295. crypto_cipher_free(cipher);
  296. tor_free(client_part);
  297. goto done;
  298. }
  299. crypto_cipher_free(cipher);
  300. /* Determine client ID. */
  301. digest = crypto_digest_new();
  302. crypto_digest_add_bytes(digest, cookie, REND_DESC_COOKIE_LEN);
  303. crypto_digest_add_bytes(digest, iv, CIPHER_IV_LEN);
  304. crypto_digest_get_digest(digest, client_part,
  305. REND_BASIC_AUTH_CLIENT_ID_LEN);
  306. crypto_digest_free(digest);
  307. /* Put both together. */
  308. smartlist_add(encrypted_session_keys, client_part);
  309. } SMARTLIST_FOREACH_END(cookie);
  310. /* Add some fake client IDs and encrypted session keys. */
  311. for (i = (smartlist_len(client_cookies) - 1) %
  312. REND_BASIC_AUTH_CLIENT_MULTIPLE;
  313. i < REND_BASIC_AUTH_CLIENT_MULTIPLE - 1; i++) {
  314. client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  315. crypto_rand(client_part, REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  316. smartlist_add(encrypted_session_keys, client_part);
  317. }
  318. /* Sort smartlist and put elements in result in order. */
  319. smartlist_sort_digests(encrypted_session_keys);
  320. pos = 2;
  321. SMARTLIST_FOREACH(encrypted_session_keys, const char *, entry, {
  322. memcpy(enc + pos, entry, REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
  323. pos += REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
  324. });
  325. *encrypted_out = enc;
  326. *encrypted_len_out = len;
  327. enc = NULL; /* prevent free. */
  328. r = 0;
  329. done:
  330. tor_free(enc);
  331. if (encrypted_session_keys) {
  332. SMARTLIST_FOREACH(encrypted_session_keys, char *, d, tor_free(d););
  333. smartlist_free(encrypted_session_keys);
  334. }
  335. return r;
  336. }
  337. /** Encrypt the encoded introduction points in <b>encoded</b> using
  338. * authorization type 'stealth' with <b>descriptor_cookie</b> of length
  339. * REND_DESC_COOKIE_LEN and write the result to a newly allocated string
  340. * pointed to by <b>encrypted_out</b> of length <b>encrypted_len_out</b>.
  341. * Return 0 for success, -1 otherwise. */
  342. static int
  343. rend_encrypt_v2_intro_points_stealth(char **encrypted_out,
  344. size_t *encrypted_len_out,
  345. const char *encoded,
  346. const char *descriptor_cookie)
  347. {
  348. int r = -1, enclen;
  349. char *enc;
  350. tor_assert(encoded);
  351. tor_assert(descriptor_cookie);
  352. enc = tor_malloc_zero(1 + CIPHER_IV_LEN + strlen(encoded));
  353. enc[0] = 0x02; /* Auth type */
  354. enclen = crypto_cipher_encrypt_with_iv(descriptor_cookie,
  355. enc + 1,
  356. CIPHER_IV_LEN+strlen(encoded),
  357. encoded, strlen(encoded));
  358. if (enclen < 0) {
  359. log_warn(LD_REND, "Could not encrypt introduction point string.");
  360. goto done;
  361. }
  362. *encrypted_out = enc;
  363. *encrypted_len_out = enclen;
  364. enc = NULL; /* prevent free */
  365. r = 0;
  366. done:
  367. tor_free(enc);
  368. return r;
  369. }
  370. /** Attempt to parse the given <b>desc_str</b> and return true if this
  371. * succeeds, false otherwise. */
  372. static int
  373. rend_desc_v2_is_parsable(rend_encoded_v2_service_descriptor_t *desc)
  374. {
  375. rend_service_descriptor_t *test_parsed = NULL;
  376. char test_desc_id[DIGEST_LEN];
  377. char *test_intro_content = NULL;
  378. size_t test_intro_size;
  379. size_t test_encoded_size;
  380. const char *test_next;
  381. int res = rend_parse_v2_service_descriptor(&test_parsed, test_desc_id,
  382. &test_intro_content,
  383. &test_intro_size,
  384. &test_encoded_size,
  385. &test_next, desc->desc_str, 1);
  386. rend_service_descriptor_free(test_parsed);
  387. tor_free(test_intro_content);
  388. return (res >= 0);
  389. }
  390. /** Free the storage held by an encoded v2 service descriptor. */
  391. void
  392. rend_encoded_v2_service_descriptor_free(
  393. rend_encoded_v2_service_descriptor_t *desc)
  394. {
  395. if (!desc)
  396. return;
  397. tor_free(desc->desc_str);
  398. tor_free(desc);
  399. }
  400. /** Free the storage held by an introduction point info. */
  401. void
  402. rend_intro_point_free(rend_intro_point_t *intro)
  403. {
  404. if (!intro)
  405. return;
  406. extend_info_free(intro->extend_info);
  407. crypto_pk_free(intro->intro_key);
  408. if (intro->accepted_intro_rsa_parts != NULL) {
  409. replaycache_free(intro->accepted_intro_rsa_parts);
  410. }
  411. tor_free(intro);
  412. }
  413. /** Encode a set of rend_encoded_v2_service_descriptor_t's for <b>desc</b>
  414. * at time <b>now</b> using <b>service_key</b>, depending on
  415. * <b>auth_type</b> a <b>descriptor_cookie</b> and a list of
  416. * <b>client_cookies</b> (which are both <b>NULL</b> if no client
  417. * authorization is performed), and <b>period</b> (e.g. 0 for the current
  418. * period, 1 for the next period, etc.) and add them to the existing list
  419. * <b>descs_out</b>; return the number of seconds that the descriptors will
  420. * be found by clients, or -1 if the encoding was not successful. */
  421. int
  422. rend_encode_v2_descriptors(smartlist_t *descs_out,
  423. rend_service_descriptor_t *desc, time_t now,
  424. uint8_t period, rend_auth_type_t auth_type,
  425. crypto_pk_t *client_key,
  426. smartlist_t *client_cookies)
  427. {
  428. char service_id[DIGEST_LEN];
  429. char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
  430. uint32_t time_period;
  431. char *ipos_base64 = NULL, *ipos = NULL, *ipos_encrypted = NULL,
  432. *descriptor_cookie = NULL;
  433. size_t ipos_len = 0, ipos_encrypted_len = 0;
  434. int k;
  435. uint32_t seconds_valid;
  436. crypto_pk_t *service_key;
  437. if (!desc) {
  438. log_warn(LD_BUG, "Could not encode v2 descriptor: No desc given.");
  439. return -1;
  440. }
  441. service_key = (auth_type == REND_STEALTH_AUTH) ? client_key : desc->pk;
  442. tor_assert(service_key);
  443. if (auth_type == REND_STEALTH_AUTH) {
  444. descriptor_cookie = smartlist_get(client_cookies, 0);
  445. tor_assert(descriptor_cookie);
  446. }
  447. /* Obtain service_id from public key. */
  448. crypto_pk_get_digest(service_key, service_id);
  449. /* Calculate current time-period. */
  450. time_period = get_time_period(now, period, service_id);
  451. /* Determine how many seconds the descriptor will be valid. */
  452. seconds_valid = period * REND_TIME_PERIOD_V2_DESC_VALIDITY +
  453. get_seconds_valid(now, service_id);
  454. /* Assemble, possibly encrypt, and encode introduction points. */
  455. if (smartlist_len(desc->intro_nodes) > 0) {
  456. if (rend_encode_v2_intro_points(&ipos, desc) < 0) {
  457. log_warn(LD_REND, "Encoding of introduction points did not succeed.");
  458. return -1;
  459. }
  460. switch (auth_type) {
  461. case REND_NO_AUTH:
  462. ipos_len = strlen(ipos);
  463. break;
  464. case REND_BASIC_AUTH:
  465. if (rend_encrypt_v2_intro_points_basic(&ipos_encrypted,
  466. &ipos_encrypted_len, ipos,
  467. client_cookies) < 0) {
  468. log_warn(LD_REND, "Encrypting of introduction points did not "
  469. "succeed.");
  470. tor_free(ipos);
  471. return -1;
  472. }
  473. tor_free(ipos);
  474. ipos = ipos_encrypted;
  475. ipos_len = ipos_encrypted_len;
  476. break;
  477. case REND_STEALTH_AUTH:
  478. if (rend_encrypt_v2_intro_points_stealth(&ipos_encrypted,
  479. &ipos_encrypted_len, ipos,
  480. descriptor_cookie) < 0) {
  481. log_warn(LD_REND, "Encrypting of introduction points did not "
  482. "succeed.");
  483. tor_free(ipos);
  484. return -1;
  485. }
  486. tor_free(ipos);
  487. ipos = ipos_encrypted;
  488. ipos_len = ipos_encrypted_len;
  489. break;
  490. default:
  491. log_warn(LD_REND|LD_BUG, "Unrecognized authorization type %d",
  492. (int)auth_type);
  493. tor_free(ipos);
  494. return -1;
  495. }
  496. /* Base64-encode introduction points. */
  497. ipos_base64 = tor_calloc(ipos_len, 2);
  498. if (base64_encode(ipos_base64, ipos_len * 2, ipos, ipos_len,
  499. BASE64_ENCODE_MULTILINE)<0) {
  500. log_warn(LD_REND, "Could not encode introduction point string to "
  501. "base64. length=%d", (int)ipos_len);
  502. tor_free(ipos_base64);
  503. tor_free(ipos);
  504. return -1;
  505. }
  506. tor_free(ipos);
  507. }
  508. /* Encode REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS descriptors. */
  509. for (k = 0; k < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; k++) {
  510. char secret_id_part[DIGEST_LEN];
  511. char secret_id_part_base32[REND_SECRET_ID_PART_LEN_BASE32 + 1];
  512. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  513. char *permanent_key = NULL;
  514. size_t permanent_key_len;
  515. char published[ISO_TIME_LEN+1];
  516. int i;
  517. char protocol_versions_string[16]; /* max len: "0,1,2,3,4,5,6,7\0" */
  518. size_t protocol_versions_written;
  519. size_t desc_len;
  520. char *desc_str = NULL;
  521. int result = 0;
  522. size_t written = 0;
  523. char desc_digest[DIGEST_LEN];
  524. rend_encoded_v2_service_descriptor_t *enc =
  525. tor_malloc_zero(sizeof(rend_encoded_v2_service_descriptor_t));
  526. /* Calculate secret-id-part = h(time-period | cookie | replica). */
  527. get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
  528. k);
  529. base32_encode(secret_id_part_base32, sizeof(secret_id_part_base32),
  530. secret_id_part, DIGEST_LEN);
  531. /* Calculate descriptor ID. */
  532. rend_get_descriptor_id_bytes(enc->desc_id, service_id, secret_id_part);
  533. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  534. enc->desc_id, DIGEST_LEN);
  535. /* PEM-encode the public key */
  536. if (crypto_pk_write_public_key_to_string(service_key, &permanent_key,
  537. &permanent_key_len) < 0) {
  538. log_warn(LD_BUG, "Could not write public key to string.");
  539. rend_encoded_v2_service_descriptor_free(enc);
  540. goto err;
  541. }
  542. /* Encode timestamp. */
  543. format_iso_time(published, desc->timestamp);
  544. /* Write protocol-versions bitmask to comma-separated value string. */
  545. protocol_versions_written = 0;
  546. for (i = 0; i < 8; i++) {
  547. if (desc->protocols & 1 << i) {
  548. tor_snprintf(protocol_versions_string + protocol_versions_written,
  549. 16 - protocol_versions_written, "%d,", i);
  550. protocol_versions_written += 2;
  551. }
  552. }
  553. if (protocol_versions_written)
  554. protocol_versions_string[protocol_versions_written - 1] = '\0';
  555. else
  556. protocol_versions_string[0]= '\0';
  557. /* Assemble complete descriptor. */
  558. desc_len = 2000 + smartlist_len(desc->intro_nodes) * 1000; /* far too long,
  559. but okay.*/
  560. enc->desc_str = desc_str = tor_malloc_zero(desc_len);
  561. result = tor_snprintf(desc_str, desc_len,
  562. "rendezvous-service-descriptor %s\n"
  563. "version 2\n"
  564. "permanent-key\n%s"
  565. "secret-id-part %s\n"
  566. "publication-time %s\n"
  567. "protocol-versions %s\n",
  568. desc_id_base32,
  569. permanent_key,
  570. secret_id_part_base32,
  571. published,
  572. protocol_versions_string);
  573. tor_free(permanent_key);
  574. if (result < 0) {
  575. log_warn(LD_BUG, "Descriptor ran out of room.");
  576. rend_encoded_v2_service_descriptor_free(enc);
  577. goto err;
  578. }
  579. written = result;
  580. /* Add introduction points. */
  581. if (ipos_base64) {
  582. result = tor_snprintf(desc_str + written, desc_len - written,
  583. "introduction-points\n"
  584. "-----BEGIN MESSAGE-----\n%s"
  585. "-----END MESSAGE-----\n",
  586. ipos_base64);
  587. if (result < 0) {
  588. log_warn(LD_BUG, "could not write introduction points.");
  589. rend_encoded_v2_service_descriptor_free(enc);
  590. goto err;
  591. }
  592. written += result;
  593. }
  594. /* Add signature. */
  595. strlcpy(desc_str + written, "signature\n", desc_len - written);
  596. written += strlen(desc_str + written);
  597. if (crypto_digest(desc_digest, desc_str, written) < 0) {
  598. log_warn(LD_BUG, "could not create digest.");
  599. rend_encoded_v2_service_descriptor_free(enc);
  600. goto err;
  601. }
  602. if (router_append_dirobj_signature(desc_str + written,
  603. desc_len - written,
  604. desc_digest, DIGEST_LEN,
  605. service_key) < 0) {
  606. log_warn(LD_BUG, "Couldn't sign desc.");
  607. rend_encoded_v2_service_descriptor_free(enc);
  608. goto err;
  609. }
  610. written += strlen(desc_str+written);
  611. if (written+2 > desc_len) {
  612. log_warn(LD_BUG, "Could not finish desc.");
  613. rend_encoded_v2_service_descriptor_free(enc);
  614. goto err;
  615. }
  616. desc_str[written++] = 0;
  617. /* Check if we can parse our own descriptor. */
  618. if (!rend_desc_v2_is_parsable(enc)) {
  619. log_warn(LD_BUG, "Could not parse my own descriptor: %s", desc_str);
  620. rend_encoded_v2_service_descriptor_free(enc);
  621. goto err;
  622. }
  623. smartlist_add(descs_out, enc);
  624. /* Add the uploaded descriptor to the local service's descriptor cache */
  625. rend_cache_store_v2_desc_as_service(enc->desc_str);
  626. base32_encode(service_id_base32, sizeof(service_id_base32),
  627. service_id, REND_SERVICE_ID_LEN);
  628. control_event_hs_descriptor_created(service_id_base32, desc_id_base32, k);
  629. }
  630. log_info(LD_REND, "Successfully encoded a v2 descriptor and "
  631. "confirmed that it is parsable.");
  632. goto done;
  633. err:
  634. SMARTLIST_FOREACH(descs_out, rend_encoded_v2_service_descriptor_t *, d,
  635. rend_encoded_v2_service_descriptor_free(d););
  636. smartlist_clear(descs_out);
  637. seconds_valid = -1;
  638. done:
  639. tor_free(ipos_base64);
  640. return seconds_valid;
  641. }
  642. /** Sets <b>out</b> to the first 10 bytes of the digest of <b>pk</b>,
  643. * base32 encoded. NUL-terminates out. (We use this string to
  644. * identify services in directory requests and .onion URLs.)
  645. */
  646. int
  647. rend_get_service_id(crypto_pk_t *pk, char *out)
  648. {
  649. char buf[DIGEST_LEN];
  650. tor_assert(pk);
  651. if (crypto_pk_get_digest(pk, buf) < 0)
  652. return -1;
  653. base32_encode(out, REND_SERVICE_ID_LEN_BASE32+1, buf, REND_SERVICE_ID_LEN);
  654. return 0;
  655. }
  656. /** Return true iff <b>query</b> is a syntactically valid service ID (as
  657. * generated by rend_get_service_id). */
  658. int
  659. rend_valid_service_id(const char *query)
  660. {
  661. if (strlen(query) != REND_SERVICE_ID_LEN_BASE32)
  662. return 0;
  663. if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN_BASE32)
  664. return 0;
  665. return 1;
  666. }
  667. /** Return true iff <b>query</b> is a syntactically valid descriptor ID.
  668. * (as generated by rend_get_descriptor_id_bytes). */
  669. int
  670. rend_valid_descriptor_id(const char *query)
  671. {
  672. if (strlen(query) != REND_DESC_ID_V2_LEN_BASE32) {
  673. goto invalid;
  674. }
  675. if (strspn(query, BASE32_CHARS) != REND_DESC_ID_V2_LEN_BASE32) {
  676. goto invalid;
  677. }
  678. return 1;
  679. invalid:
  680. return 0;
  681. }
  682. /** Return true iff <b>client_name</b> is a syntactically valid name
  683. * for rendezvous client authentication. */
  684. int
  685. rend_valid_client_name(const char *client_name)
  686. {
  687. size_t len = strlen(client_name);
  688. if (len < 1 || len > REND_CLIENTNAME_MAX_LEN) {
  689. return 0;
  690. }
  691. if (strspn(client_name, REND_LEGAL_CLIENTNAME_CHARACTERS) != len) {
  692. return 0;
  693. }
  694. return 1;
  695. }
  696. /** Called when we get a rendezvous-related relay cell on circuit
  697. * <b>circ</b>. Dispatch on rendezvous relay command. */
  698. void
  699. rend_process_relay_cell(circuit_t *circ, const crypt_path_t *layer_hint,
  700. int command, size_t length,
  701. const uint8_t *payload)
  702. {
  703. or_circuit_t *or_circ = NULL;
  704. origin_circuit_t *origin_circ = NULL;
  705. int r = -2;
  706. if (CIRCUIT_IS_ORIGIN(circ)) {
  707. origin_circ = TO_ORIGIN_CIRCUIT(circ);
  708. if (!layer_hint || layer_hint != origin_circ->cpath->prev) {
  709. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  710. "Relay cell (rend purpose %d) from wrong hop on origin circ",
  711. command);
  712. origin_circ = NULL;
  713. }
  714. } else {
  715. or_circ = TO_OR_CIRCUIT(circ);
  716. }
  717. switch (command) {
  718. case RELAY_COMMAND_ESTABLISH_INTRO:
  719. if (or_circ)
  720. r = rend_mid_establish_intro(or_circ,payload,length);
  721. break;
  722. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  723. if (or_circ)
  724. r = rend_mid_establish_rendezvous(or_circ,payload,length);
  725. break;
  726. case RELAY_COMMAND_INTRODUCE1:
  727. if (or_circ)
  728. r = rend_mid_introduce(or_circ,payload,length);
  729. break;
  730. case RELAY_COMMAND_INTRODUCE2:
  731. if (origin_circ)
  732. r = rend_service_receive_introduction(origin_circ,payload,length);
  733. break;
  734. case RELAY_COMMAND_INTRODUCE_ACK:
  735. if (origin_circ)
  736. r = rend_client_introduction_acked(origin_circ,payload,length);
  737. break;
  738. case RELAY_COMMAND_RENDEZVOUS1:
  739. if (or_circ)
  740. r = rend_mid_rendezvous(or_circ,payload,length);
  741. break;
  742. case RELAY_COMMAND_RENDEZVOUS2:
  743. if (origin_circ)
  744. r = rend_client_receive_rendezvous(origin_circ,payload,length);
  745. break;
  746. case RELAY_COMMAND_INTRO_ESTABLISHED:
  747. if (origin_circ)
  748. r = rend_service_intro_established(origin_circ,payload,length);
  749. break;
  750. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  751. if (origin_circ)
  752. r = rend_client_rendezvous_acked(origin_circ,payload,length);
  753. break;
  754. default:
  755. tor_fragile_assert();
  756. }
  757. if (r == -2)
  758. log_info(LD_PROTOCOL, "Dropping cell (type %d) for wrong circuit type.",
  759. command);
  760. }
  761. /** Allocate and return a new rend_data_t with the same
  762. * contents as <b>query</b>. */
  763. rend_data_t *
  764. rend_data_dup(const rend_data_t *data)
  765. {
  766. rend_data_t *data_dup;
  767. tor_assert(data);
  768. data_dup = tor_memdup(data, sizeof(rend_data_t));
  769. data_dup->hsdirs_fp = smartlist_new();
  770. SMARTLIST_FOREACH(data->hsdirs_fp, char *, fp,
  771. smartlist_add(data_dup->hsdirs_fp,
  772. tor_memdup(fp, DIGEST_LEN)));
  773. return data_dup;
  774. }
  775. /** Compute descriptor ID for each replicas and save them. A valid onion
  776. * address must be present in the <b>rend_data</b>.
  777. *
  778. * Return 0 on success else -1. */
  779. static int
  780. compute_desc_id(rend_data_t *rend_data)
  781. {
  782. int ret = 0;
  783. unsigned replica;
  784. time_t now = time(NULL);
  785. tor_assert(rend_data);
  786. /* Compute descriptor ID for each replicas. */
  787. for (replica = 0; replica < ARRAY_LENGTH(rend_data->descriptor_id);
  788. replica++) {
  789. ret = rend_compute_v2_desc_id(rend_data->descriptor_id[replica],
  790. rend_data->onion_address,
  791. rend_data->descriptor_cookie,
  792. now, replica);
  793. if (ret < 0) {
  794. goto end;
  795. }
  796. }
  797. end:
  798. return ret;
  799. }
  800. /** Allocate and initialize a rend_data_t object for a service using the
  801. * given arguments. Only the <b>onion_address</b> is not optional.
  802. *
  803. * Return a valid rend_data_t pointer. */
  804. rend_data_t *
  805. rend_data_service_create(const char *onion_address, const char *pk_digest,
  806. const uint8_t *cookie, rend_auth_type_t auth_type)
  807. {
  808. rend_data_t *rend_data = tor_malloc_zero(sizeof(*rend_data));
  809. /* We need at least one else the call is wrong. */
  810. tor_assert(onion_address != NULL);
  811. if (pk_digest) {
  812. memcpy(rend_data->rend_pk_digest, pk_digest,
  813. sizeof(rend_data->rend_pk_digest));
  814. }
  815. if (cookie) {
  816. memcpy(rend_data->rend_cookie, cookie,
  817. sizeof(rend_data->rend_cookie));
  818. }
  819. strlcpy(rend_data->onion_address, onion_address,
  820. sizeof(rend_data->onion_address));
  821. rend_data->auth_type = auth_type;
  822. /* Won't be used but still need to initialize it for rend_data dup and
  823. * free. */
  824. rend_data->hsdirs_fp = smartlist_new();
  825. return rend_data;
  826. }
  827. /** Allocate and initialize a rend_data_t object for a client request using
  828. * the given arguments. Either an onion address or a descriptor ID is
  829. * needed. Both can be given but only the onion address will be used to make
  830. * the descriptor fetch.
  831. *
  832. * Return a valid rend_data_t pointer or NULL on error meaning the
  833. * descriptor IDs couldn't be computed from the given data. */
  834. rend_data_t *
  835. rend_data_client_create(const char *onion_address, const char *desc_id,
  836. const char *cookie, rend_auth_type_t auth_type)
  837. {
  838. rend_data_t *rend_data = tor_malloc_zero(sizeof(*rend_data));
  839. /* We need at least one else the call is wrong. */
  840. tor_assert(onion_address != NULL || desc_id != NULL);
  841. if (cookie) {
  842. memcpy(rend_data->descriptor_cookie, cookie,
  843. sizeof(rend_data->descriptor_cookie));
  844. }
  845. if (desc_id) {
  846. memcpy(rend_data->desc_id_fetch, desc_id,
  847. sizeof(rend_data->desc_id_fetch));
  848. }
  849. if (onion_address) {
  850. strlcpy(rend_data->onion_address, onion_address,
  851. sizeof(rend_data->onion_address));
  852. if (compute_desc_id(rend_data) < 0) {
  853. goto error;
  854. }
  855. }
  856. rend_data->auth_type = auth_type;
  857. rend_data->hsdirs_fp = smartlist_new();
  858. return rend_data;
  859. error:
  860. rend_data_free(rend_data);
  861. return NULL;
  862. }
  863. /** Determine the routers that are responsible for <b>id</b> (binary) and
  864. * add pointers to those routers' routerstatus_t to <b>responsible_dirs</b>.
  865. * Return -1 if we're returning an empty smartlist, else return 0.
  866. */
  867. int
  868. hid_serv_get_responsible_directories(smartlist_t *responsible_dirs,
  869. const char *id)
  870. {
  871. int start, found, n_added = 0, i;
  872. networkstatus_t *c = networkstatus_get_latest_consensus();
  873. if (!c || !smartlist_len(c->routerstatus_list)) {
  874. log_warn(LD_REND, "We don't have a consensus, so we can't perform v2 "
  875. "rendezvous operations.");
  876. return -1;
  877. }
  878. tor_assert(id);
  879. start = networkstatus_vote_find_entry_idx(c, id, &found);
  880. if (start == smartlist_len(c->routerstatus_list)) start = 0;
  881. i = start;
  882. do {
  883. routerstatus_t *r = smartlist_get(c->routerstatus_list, i);
  884. if (r->is_hs_dir) {
  885. smartlist_add(responsible_dirs, r);
  886. if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
  887. return 0;
  888. }
  889. if (++i == smartlist_len(c->routerstatus_list))
  890. i = 0;
  891. } while (i != start);
  892. /* Even though we don't have the desired number of hidden service
  893. * directories, be happy if we got any. */
  894. return smartlist_len(responsible_dirs) ? 0 : -1;
  895. }
  896. /* Length of the 'extended' auth cookie used to encode auth type before
  897. * base64 encoding. */
  898. #define REND_DESC_COOKIE_LEN_EXT (REND_DESC_COOKIE_LEN + 1)
  899. /* Length of the zero-padded auth cookie when base64 encoded. These two
  900. * padding bytes always (A=) are stripped off of the returned cookie. */
  901. #define REND_DESC_COOKIE_LEN_EXT_BASE64 (REND_DESC_COOKIE_LEN_BASE64 + 2)
  902. /** Encode a client authorization descriptor cookie.
  903. * The result of this function is suitable for use in the HidServAuth
  904. * option. The trailing padding characters are removed, and the
  905. * auth type is encoded into the cookie.
  906. *
  907. * Returns a new base64-encoded cookie. This function cannot fail.
  908. * The caller is responsible for freeing the returned value.
  909. */
  910. char *
  911. rend_auth_encode_cookie(const uint8_t *cookie_in, rend_auth_type_t auth_type)
  912. {
  913. uint8_t extended_cookie[REND_DESC_COOKIE_LEN_EXT];
  914. char *cookie_out = tor_malloc_zero(REND_DESC_COOKIE_LEN_EXT_BASE64 + 1);
  915. int re;
  916. tor_assert(cookie_in);
  917. memcpy(extended_cookie, cookie_in, REND_DESC_COOKIE_LEN);
  918. extended_cookie[REND_DESC_COOKIE_LEN] = ((int)auth_type - 1) << 4;
  919. re = base64_encode(cookie_out, REND_DESC_COOKIE_LEN_EXT_BASE64 + 1,
  920. (const char *) extended_cookie, REND_DESC_COOKIE_LEN_EXT,
  921. 0);
  922. tor_assert(re == REND_DESC_COOKIE_LEN_EXT_BASE64);
  923. /* Remove the trailing 'A='. Auth type is encoded in the high bits
  924. * of the last byte, so the last base64 character will always be zero
  925. * (A). This is subtly different behavior from base64_encode_nopad. */
  926. cookie_out[REND_DESC_COOKIE_LEN_BASE64] = '\0';
  927. memwipe(extended_cookie, 0, sizeof(extended_cookie));
  928. return cookie_out;
  929. }
  930. /** Decode a base64-encoded client authorization descriptor cookie.
  931. * The descriptor_cookie can be truncated to REND_DESC_COOKIE_LEN_BASE64
  932. * characters (as given to clients), or may include the two padding
  933. * characters (as stored by the service).
  934. *
  935. * The result is stored in REND_DESC_COOKIE_LEN bytes of cookie_out.
  936. * The rend_auth_type_t decoded from the cookie is stored in the
  937. * optional auth_type_out parameter.
  938. *
  939. * Return 0 on success, or -1 on error. The caller is responsible for
  940. * freeing the returned err_msg.
  941. */
  942. int
  943. rend_auth_decode_cookie(const char *cookie_in, uint8_t *cookie_out,
  944. rend_auth_type_t *auth_type_out, char **err_msg_out)
  945. {
  946. uint8_t descriptor_cookie_decoded[REND_DESC_COOKIE_LEN_EXT + 1] = { 0 };
  947. char descriptor_cookie_base64ext[REND_DESC_COOKIE_LEN_EXT_BASE64 + 1];
  948. const char *descriptor_cookie = cookie_in;
  949. char *err_msg = NULL;
  950. int auth_type_val = 0;
  951. int res = -1;
  952. int decoded_len;
  953. size_t len = strlen(descriptor_cookie);
  954. if (len == REND_DESC_COOKIE_LEN_BASE64) {
  955. /* Add a trailing zero byte to make base64-decoding happy. */
  956. tor_snprintf(descriptor_cookie_base64ext,
  957. sizeof(descriptor_cookie_base64ext),
  958. "%sA=", descriptor_cookie);
  959. descriptor_cookie = descriptor_cookie_base64ext;
  960. } else if (len != REND_DESC_COOKIE_LEN_EXT_BASE64) {
  961. tor_asprintf(&err_msg, "Authorization cookie has wrong length: %s",
  962. escaped(cookie_in));
  963. goto err;
  964. }
  965. decoded_len = base64_decode((char *) descriptor_cookie_decoded,
  966. sizeof(descriptor_cookie_decoded),
  967. descriptor_cookie,
  968. REND_DESC_COOKIE_LEN_EXT_BASE64);
  969. if (decoded_len != REND_DESC_COOKIE_LEN &&
  970. decoded_len != REND_DESC_COOKIE_LEN_EXT) {
  971. tor_asprintf(&err_msg, "Authorization cookie has invalid characters: %s",
  972. escaped(cookie_in));
  973. goto err;
  974. }
  975. if (auth_type_out) {
  976. auth_type_val = (descriptor_cookie_decoded[REND_DESC_COOKIE_LEN] >> 4) + 1;
  977. if (auth_type_val < 1 || auth_type_val > 2) {
  978. tor_asprintf(&err_msg, "Authorization cookie type is unknown: %s",
  979. escaped(cookie_in));
  980. goto err;
  981. }
  982. *auth_type_out = auth_type_val == 1 ? REND_BASIC_AUTH : REND_STEALTH_AUTH;
  983. }
  984. memcpy(cookie_out, descriptor_cookie_decoded, REND_DESC_COOKIE_LEN);
  985. res = 0;
  986. err:
  987. if (err_msg_out) {
  988. *err_msg_out = err_msg;
  989. } else {
  990. tor_free(err_msg);
  991. }
  992. memwipe(descriptor_cookie_decoded, 0, sizeof(descriptor_cookie_decoded));
  993. memwipe(descriptor_cookie_base64ext, 0, sizeof(descriptor_cookie_base64ext));
  994. return res;
  995. }