rendcommon.c 14 KB

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