rendcommon.c 13 KB

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