rendcommon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* Copyright 2004-2006 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char rendcommon_c_id[] =
  5. "$Id$";
  6. /**
  7. * \file rendcommon.c
  8. * \brief Rendezvous implementation: shared code between
  9. * introducers, services, clients, and rendezvous points.
  10. **/
  11. #include "or.h"
  12. /** Return 0 if one and two are the same service ids, else -1 or 1 */
  13. int
  14. rend_cmp_service_ids(const char *one, const char *two)
  15. {
  16. return strcasecmp(one,two);
  17. }
  18. /** Free the storage held by the service descriptor <b>desc</b>.
  19. */
  20. void
  21. rend_service_descriptor_free(rend_service_descriptor_t *desc)
  22. {
  23. int i;
  24. if (desc->pk)
  25. crypto_free_pk_env(desc->pk);
  26. if (desc->intro_points) {
  27. for (i=0; i < desc->n_intro_points; ++i) {
  28. tor_free(desc->intro_points[i]);
  29. }
  30. tor_free(desc->intro_points);
  31. }
  32. if (desc->intro_point_extend_info) {
  33. for (i=0; i < desc->n_intro_points; ++i) {
  34. if (desc->intro_point_extend_info[i])
  35. extend_info_free(desc->intro_point_extend_info[i]);
  36. }
  37. tor_free(desc->intro_point_extend_info);
  38. }
  39. tor_free(desc);
  40. }
  41. /** Encode a service descriptor for <b>desc</b>, and sign it with
  42. * <b>key</b>. Store the descriptor in *<b>str_out</b>, and set
  43. * *<b>len_out</b> to its length.
  44. */
  45. int
  46. rend_encode_service_descriptor(rend_service_descriptor_t *desc,
  47. int version,
  48. crypto_pk_env_t *key,
  49. char **str_out, size_t *len_out)
  50. {
  51. char *cp;
  52. char *end;
  53. int i;
  54. size_t asn1len;
  55. size_t buflen = PK_BYTES*2*(desc->n_intro_points+2);/*Too long, but ok*/
  56. cp = *str_out = tor_malloc(buflen);
  57. end = cp + PK_BYTES*2*(desc->n_intro_points+1);
  58. if (version) {
  59. *(uint8_t*)cp = (uint8_t)0xff;
  60. *(uint8_t*)(cp+1) = (uint8_t)version;
  61. cp += 2;
  62. }
  63. asn1len = crypto_pk_asn1_encode(desc->pk, cp+2, end-(cp+2));
  64. set_uint16(cp, htons((uint16_t)asn1len));
  65. cp += 2+asn1len;
  66. set_uint32(cp, htonl((uint32_t)desc->timestamp));
  67. cp += 4;
  68. if (version == 1) {
  69. set_uint16(cp, htons(desc->protocols));
  70. cp += 2;
  71. }
  72. set_uint16(cp, htons((uint16_t)desc->n_intro_points));
  73. cp += 2;
  74. if (version == 0) {
  75. for (i=0; i < desc->n_intro_points; ++i) {
  76. char *ipoint = (char*)desc->intro_points[i];
  77. strlcpy(cp, ipoint, buflen-(cp-*str_out));
  78. cp += strlen(ipoint)+1;
  79. }
  80. } else {
  81. if (desc->n_intro_points)
  82. tor_assert(desc->intro_point_extend_info);
  83. for (i=0; i < desc->n_intro_points; ++i) {
  84. extend_info_t *info = desc->intro_point_extend_info[i];
  85. int klen;
  86. set_uint32(cp, htonl(info->addr));
  87. set_uint16(cp+4, htons(info->port));
  88. memcpy(cp+6, info->identity_digest, DIGEST_LEN);
  89. klen = crypto_pk_asn1_encode(info->onion_key, cp+6+DIGEST_LEN+2,
  90. (end-(cp+6+DIGEST_LEN+2)));
  91. set_uint16(cp+6+DIGEST_LEN, htons((uint16_t)klen));
  92. cp += 6+DIGEST_LEN+2+klen;
  93. }
  94. }
  95. i = crypto_pk_private_sign_digest(key, cp, *str_out, cp-*str_out);
  96. if (i<0) {
  97. tor_free(*str_out);
  98. return -1;
  99. }
  100. cp += i;
  101. *len_out = (size_t)(cp-*str_out);
  102. return 0;
  103. }
  104. /** Parse a service descriptor at <b>str</b> (<b>len</b> bytes). On
  105. * success, return a newly alloced service_descriptor_t. On failure,
  106. * return NULL.
  107. */
  108. rend_service_descriptor_t *
  109. rend_parse_service_descriptor(const char *str, size_t len)
  110. {
  111. rend_service_descriptor_t *result = NULL;
  112. int i;
  113. size_t keylen, asn1len;
  114. const char *end, *cp, *eos;
  115. int version = 0;
  116. result = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  117. cp = str;
  118. end = str+len;
  119. if (end-cp<2) goto truncated;
  120. if (*(uint8_t*)cp == 0xff) {
  121. result->version = version = *(uint8_t*)(cp+1);
  122. cp += 2;
  123. } else {
  124. result->version = version = 0;
  125. }
  126. if (end-cp < 2) goto truncated;
  127. asn1len = ntohs(get_uint16(cp));
  128. cp += 2;
  129. if ((size_t)(end-cp) < asn1len) goto truncated;
  130. result->pk = crypto_pk_asn1_decode(cp, asn1len);
  131. if (!result->pk) goto truncated;
  132. cp += asn1len;
  133. if (end-cp < 4) goto truncated;
  134. result->timestamp = (time_t) ntohl(get_uint32(cp));
  135. cp += 4;
  136. if (version == 1) {
  137. if (end-cp < 2) goto truncated;
  138. result->protocols = ntohs(get_uint16(cp));
  139. cp += 2;
  140. } else {
  141. result->protocols = 1;
  142. }
  143. if (end-cp < 2) goto truncated;
  144. result->n_intro_points = ntohs(get_uint16(cp));
  145. cp += 2;
  146. if (version == 0 && result->n_intro_points != 0) {
  147. result->intro_points =
  148. tor_malloc_zero(sizeof(char*)*result->n_intro_points);
  149. for (i=0;i<result->n_intro_points;++i) {
  150. if (end-cp < 2) goto truncated;
  151. eos = (const char *)memchr(cp,'\0',end-cp);
  152. if (!eos) goto truncated;
  153. result->intro_points[i] = tor_strdup(cp);
  154. cp = eos+1;
  155. }
  156. } else if (version != 0 && result->n_intro_points != 0) {
  157. result->intro_point_extend_info =
  158. tor_malloc_zero(sizeof(extend_info_t*)*result->n_intro_points);
  159. result->intro_points =
  160. tor_malloc_zero(sizeof(char*)*result->n_intro_points);
  161. for (i=0;i<result->n_intro_points;++i) {
  162. extend_info_t *info = result->intro_point_extend_info[i] =
  163. tor_malloc_zero(sizeof(extend_info_t));
  164. int klen;
  165. if (end-cp < 8+DIGEST_LEN) goto truncated;
  166. info->addr = ntohl(get_uint32(cp));
  167. info->port = ntohs(get_uint16(cp+4));
  168. memcpy(info->identity_digest, cp+6, DIGEST_LEN);
  169. info->nickname[0] = '$';
  170. base16_encode(info->nickname+1, sizeof(info->nickname)-1,
  171. info->identity_digest, DIGEST_LEN);
  172. result->intro_points[i] = tor_strdup(info->nickname);
  173. klen = ntohs(get_uint16(cp+6+DIGEST_LEN));
  174. cp += 8+DIGEST_LEN;
  175. if (end-cp < klen) goto truncated;
  176. if (!(info->onion_key = crypto_pk_asn1_decode(cp,klen))) {
  177. log_warn(LD_PROTOCOL,
  178. "Internal error decoding onion key for intro point.");
  179. goto error;
  180. }
  181. cp += klen;
  182. }
  183. }
  184. keylen = crypto_pk_keysize(result->pk);
  185. tor_assert(end-cp >= 0);
  186. if ((size_t)(end-cp) < keylen) goto truncated;
  187. if ((size_t)(end-cp) > keylen) {
  188. log_warn(LD_PROTOCOL,
  189. "Signature is %d bytes too long on service descriptor.",
  190. (int)((size_t)(end-cp) - keylen));
  191. goto error;
  192. }
  193. if (crypto_pk_public_checksig_digest(result->pk,
  194. (char*)str,cp-str, /* data */
  195. (char*)cp,end-cp /* signature*/
  196. )<0) {
  197. log_warn(LD_PROTOCOL, "Bad signature on service descriptor.");
  198. goto error;
  199. }
  200. return result;
  201. truncated:
  202. log_warn(LD_PROTOCOL, "Truncated service descriptor.");
  203. error:
  204. rend_service_descriptor_free(result);
  205. return NULL;
  206. }
  207. /** Sets <b>out</b> to the first 10 bytes of the digest of <b>pk</b>,
  208. * base32 encoded. NUL-terminates out. (We use this string to
  209. * identify services in directory requests and .onion URLs.)
  210. */
  211. int
  212. rend_get_service_id(crypto_pk_env_t *pk, char *out)
  213. {
  214. char buf[DIGEST_LEN];
  215. tor_assert(pk);
  216. if (crypto_pk_get_digest(pk, buf) < 0)
  217. return -1;
  218. base32_encode(out, REND_SERVICE_ID_LEN+1, buf, 10);
  219. return 0;
  220. }
  221. /* ==== Rendezvous service descriptor cache. */
  222. /** How old do we let hidden service descriptors get discarding them as too
  223. * old? */
  224. #define REND_CACHE_MAX_AGE (2*24*60*60)
  225. /** How wrong do we assume our clock may be when checking whether hidden
  226. * services are too old or too new? */
  227. #define REND_CACHE_MAX_SKEW (24*60*60)
  228. /** Map from service id (as generated by rend_get_service_id) to
  229. * rend_cache_entry_t. */
  230. static strmap_t *rend_cache = NULL;
  231. /** Initializes the service descriptor cache.
  232. */
  233. void
  234. rend_cache_init(void)
  235. {
  236. rend_cache = strmap_new();
  237. }
  238. /** Helper: free storage held by a single service descriptor cache entry. */
  239. static void
  240. _rend_cache_entry_free(void *p)
  241. {
  242. rend_cache_entry_t *e = p;
  243. rend_service_descriptor_free(e->parsed);
  244. tor_free(e->desc);
  245. tor_free(e);
  246. }
  247. /** Free all storage held by the service descriptor cache. */
  248. void
  249. rend_cache_free_all(void)
  250. {
  251. strmap_free(rend_cache, _rend_cache_entry_free);
  252. rend_cache = NULL;
  253. }
  254. /** Removes all old entries from the service descriptor cache.
  255. */
  256. void
  257. rend_cache_clean(void)
  258. {
  259. strmap_iter_t *iter;
  260. const char *key;
  261. void *val;
  262. rend_cache_entry_t *ent;
  263. time_t cutoff;
  264. cutoff = time(NULL) - REND_CACHE_MAX_AGE - REND_CACHE_MAX_SKEW;
  265. for (iter = strmap_iter_init(rend_cache); !strmap_iter_done(iter); ) {
  266. strmap_iter_get(iter, &key, &val);
  267. ent = (rend_cache_entry_t*)val;
  268. if (ent->parsed->timestamp < cutoff) {
  269. iter = strmap_iter_next_rmv(rend_cache, iter);
  270. _rend_cache_entry_free(ent);
  271. } else {
  272. iter = strmap_iter_next(rend_cache, iter);
  273. }
  274. }
  275. }
  276. /** Return true iff <b>query</b> is a syntactically valid service ID (as
  277. * generated by rend_get_service_id). */
  278. int
  279. rend_valid_service_id(const char *query)
  280. {
  281. if (strlen(query) != REND_SERVICE_ID_LEN)
  282. return 0;
  283. if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN)
  284. return 0;
  285. return 1;
  286. }
  287. /** If we have a cached rend_cache_entry_t for the service ID <b>query</b>,
  288. * set *<b>e</b> to that entry and return 1. Else return 0. If
  289. * <b>version</b> is nonnegative, only return an entry in that descriptor
  290. * format version. Otherwise (if <b>version</b> is negative), return the most
  291. * recent format we have.
  292. */
  293. int
  294. rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e)
  295. {
  296. char key[REND_SERVICE_ID_LEN+2]; /* 1<query>\0 or 0<query>\0 */
  297. tor_assert(rend_cache);
  298. if (!rend_valid_service_id(query))
  299. return -1;
  300. *e = NULL;
  301. if (version != 0) {
  302. tor_snprintf(key, sizeof(key), "1%s", query);
  303. *e = strmap_get_lc(rend_cache, key);
  304. }
  305. if (!*e && version != 1) {
  306. tor_snprintf(key, sizeof(key), "0%s", query);
  307. *e = strmap_get_lc(rend_cache, key);
  308. }
  309. if (!*e)
  310. return 0;
  311. return 1;
  312. }
  313. /** <b>query</b> is a base-32'ed service id. If it's malformed, return -1.
  314. * Else look it up.
  315. * - If it is found, point *desc to it, and write its length into
  316. * *desc_len, and return 1.
  317. * - If it is not found, return 0.
  318. * Note: calls to rend_cache_clean or rend_cache_store may invalidate
  319. * *desc.
  320. */
  321. int
  322. rend_cache_lookup_desc(const char *query, int version, const char **desc,
  323. size_t *desc_len)
  324. {
  325. rend_cache_entry_t *e;
  326. int r;
  327. r = rend_cache_lookup_entry(query,version,&e);
  328. if (r <= 0) return r;
  329. *desc = e->desc;
  330. *desc_len = e->len;
  331. return 1;
  332. }
  333. /** Parse *desc, calculate its service id, and store it in the cache.
  334. * If we have a newer descriptor with the same ID, ignore this one.
  335. * If we have an older descriptor with the same ID, replace it.
  336. * Return -1 if it's malformed or otherwise rejected; return 0 if
  337. * it's the same or older than one we've already got; return 1 if
  338. * it's novel.
  339. */
  340. int
  341. rend_cache_store(const char *desc, size_t desc_len)
  342. {
  343. rend_cache_entry_t *e;
  344. rend_service_descriptor_t *parsed;
  345. char query[REND_SERVICE_ID_LEN+1];
  346. char key[REND_SERVICE_ID_LEN+2]; /* 1<query>\0 or 0<query>\0 */
  347. time_t now;
  348. tor_assert(rend_cache);
  349. parsed = rend_parse_service_descriptor(desc,desc_len);
  350. if (!parsed) {
  351. log_warn(LD_PROTOCOL,"Couldn't parse service descriptor.");
  352. return -1;
  353. }
  354. if (rend_get_service_id(parsed->pk, query)<0) {
  355. log_warn(LD_BUG,"Couldn't compute service ID.");
  356. rend_service_descriptor_free(parsed);
  357. return -1;
  358. }
  359. tor_snprintf(key, sizeof(key), "%c%s", parsed->version?'1':'0', query);
  360. now = time(NULL);
  361. if (parsed->timestamp < now-REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
  362. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  363. "Service descriptor %s is too old.", safe_str(query));
  364. rend_service_descriptor_free(parsed);
  365. return -1;
  366. }
  367. if (parsed->timestamp > now+REND_CACHE_MAX_SKEW) {
  368. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  369. "Service descriptor %s is too far in the future.", safe_str(query));
  370. rend_service_descriptor_free(parsed);
  371. return -1;
  372. }
  373. e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, key);
  374. if (e && e->parsed->timestamp > parsed->timestamp) {
  375. log_info(LD_REND,"We already have a newer service descriptor %s with the "
  376. "same ID and version.", safe_str(query));
  377. rend_service_descriptor_free(parsed);
  378. return 0;
  379. }
  380. if (e && e->len == desc_len && !memcmp(desc,e->desc,desc_len)) {
  381. log_info(LD_REND,"We already have this service descriptor %s.",
  382. safe_str(query));
  383. e->received = time(NULL);
  384. rend_service_descriptor_free(parsed);
  385. return 0;
  386. }
  387. if (!e) {
  388. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  389. strmap_set_lc(rend_cache, key, e);
  390. } else {
  391. rend_service_descriptor_free(e->parsed);
  392. tor_free(e->desc);
  393. }
  394. e->received = time(NULL);
  395. e->parsed = parsed;
  396. e->len = desc_len;
  397. e->desc = tor_malloc(desc_len);
  398. memcpy(e->desc, desc, desc_len);
  399. log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
  400. safe_str(query), (int)desc_len);
  401. return 1;
  402. }
  403. /** Called when we get a rendezvous-related relay cell on circuit
  404. * <b>circ</b>. Dispatch on rendezvous relay command. */
  405. void
  406. rend_process_relay_cell(circuit_t *circ, int command, size_t length,
  407. const char *payload)
  408. {
  409. or_circuit_t *or_circ = NULL;
  410. origin_circuit_t *origin_circ = NULL;
  411. int r;
  412. if (CIRCUIT_IS_ORIGIN(circ))
  413. origin_circ = TO_ORIGIN_CIRCUIT(circ);
  414. else
  415. or_circ = TO_OR_CIRCUIT(circ);
  416. switch (command) {
  417. case RELAY_COMMAND_ESTABLISH_INTRO:
  418. r = rend_mid_establish_intro(or_circ,payload,length);
  419. break;
  420. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  421. r = rend_mid_establish_rendezvous(or_circ,payload,length);
  422. break;
  423. case RELAY_COMMAND_INTRODUCE1:
  424. r = rend_mid_introduce(or_circ,payload,length);
  425. break;
  426. case RELAY_COMMAND_INTRODUCE2:
  427. r = rend_service_introduce(origin_circ,payload,length);
  428. break;
  429. case RELAY_COMMAND_INTRODUCE_ACK:
  430. r = rend_client_introduction_acked(origin_circ,payload,length);
  431. break;
  432. case RELAY_COMMAND_RENDEZVOUS1:
  433. r = rend_mid_rendezvous(or_circ,payload,length);
  434. break;
  435. case RELAY_COMMAND_RENDEZVOUS2:
  436. r = rend_client_receive_rendezvous(origin_circ,payload,length);
  437. break;
  438. case RELAY_COMMAND_INTRO_ESTABLISHED:
  439. r = rend_service_intro_established(origin_circ,payload,length);
  440. break;
  441. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  442. r = rend_client_rendezvous_acked(origin_circ,payload,length);
  443. break;
  444. default:
  445. tor_assert(0);
  446. }
  447. }