authcert.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file authcert.c
  8. * \brief Code to maintain directory authorities' certificates.
  9. *
  10. * Authority certificates are signed with authority identity keys; they
  11. * are used to authenticate shorter-term authority signing keys. We
  12. * fetch them when we find a consensus or a vote that has been signed
  13. * with a signing key we don't recognize. We cache them on disk and
  14. * load them on startup. Authority operators generate them with the
  15. * "tor-gencert" utility.
  16. */
  17. #include "core/or/or.h"
  18. #include "app/config/config.h"
  19. #include "core/mainloop/connection.h"
  20. #include "core/mainloop/mainloop.h"
  21. #include "core/or/policies.h"
  22. #include "feature/client/bridges.h"
  23. #include "feature/dircommon/directory.h"
  24. #include "feature/dirclient/dirclient.h"
  25. #include "feature/dirclient/dlstatus.h"
  26. #include "feature/dircommon/fp_pair.h"
  27. #include "feature/nodelist/authcert.h"
  28. #include "feature/nodelist/dirlist.h"
  29. #include "feature/nodelist/networkstatus.h"
  30. #include "feature/nodelist/node_select.h"
  31. #include "feature/nodelist/nodelist.h"
  32. #include "feature/nodelist/routerlist.h"
  33. #include "feature/nodelist/routerparse.h"
  34. #include "feature/relay/router.h"
  35. #include "core/or/connection_st.h"
  36. #include "feature/dirclient/dir_server_st.h"
  37. #include "feature/dircommon/dir_connection_st.h"
  38. #include "feature/nodelist/authority_cert_st.h"
  39. #include "feature/nodelist/document_signature_st.h"
  40. #include "feature/nodelist/networkstatus_st.h"
  41. #include "feature/nodelist/networkstatus_voter_info_st.h"
  42. #include "feature/nodelist/node_st.h"
  43. DECLARE_TYPED_DIGESTMAP_FNS(dsmap_, digest_ds_map_t, download_status_t)
  44. #define DSMAP_FOREACH(map, keyvar, valvar) \
  45. DIGESTMAP_FOREACH(dsmap_to_digestmap(map), keyvar, download_status_t *, \
  46. valvar)
  47. #define dsmap_free(map, fn) MAP_FREE_AND_NULL(dsmap, (map), (fn))
  48. /* Forward declaration for cert_list_t */
  49. typedef struct cert_list_t cert_list_t;
  50. static void download_status_reset_by_sk_in_cl(cert_list_t *cl,
  51. const char *digest);
  52. static int download_status_is_ready_by_sk_in_cl(cert_list_t *cl,
  53. const char *digest,
  54. time_t now);
  55. static void list_pending_fpsk_downloads(fp_pair_map_t *result);
  56. /** List of certificates for a single authority, and download status for
  57. * latest certificate.
  58. */
  59. struct cert_list_t {
  60. /*
  61. * The keys of download status map are cert->signing_key_digest for pending
  62. * downloads by (identity digest/signing key digest) pair; functions such
  63. * as authority_cert_get_by_digest() already assume these are unique.
  64. */
  65. struct digest_ds_map_t *dl_status_map;
  66. /* There is also a dlstatus for the download by identity key only */
  67. download_status_t dl_status_by_id;
  68. smartlist_t *certs;
  69. };
  70. /** Map from v3 identity key digest to cert_list_t. */
  71. static digestmap_t *trusted_dir_certs = NULL;
  72. /** True iff any key certificate in at least one member of
  73. * <b>trusted_dir_certs</b> has changed since we last flushed the
  74. * certificates to disk. */
  75. static int trusted_dir_servers_certs_changed = 0;
  76. /** Initialise schedule, want_authority, and increment_on in the download
  77. * status dlstatus, then call download_status_reset() on it.
  78. * It is safe to call this function or download_status_reset() multiple times
  79. * on a new dlstatus. But it should *not* be called after a dlstatus has been
  80. * used to count download attempts or failures. */
  81. static void
  82. download_status_cert_init(download_status_t *dlstatus)
  83. {
  84. dlstatus->schedule = DL_SCHED_CONSENSUS;
  85. dlstatus->want_authority = DL_WANT_ANY_DIRSERVER;
  86. dlstatus->increment_on = DL_SCHED_INCREMENT_FAILURE;
  87. dlstatus->last_backoff_position = 0;
  88. dlstatus->last_delay_used = 0;
  89. /* Use the new schedule to set next_attempt_at */
  90. download_status_reset(dlstatus);
  91. }
  92. /** Reset the download status of a specified element in a dsmap */
  93. static void
  94. download_status_reset_by_sk_in_cl(cert_list_t *cl, const char *digest)
  95. {
  96. download_status_t *dlstatus = NULL;
  97. tor_assert(cl);
  98. tor_assert(digest);
  99. /* Make sure we have a dsmap */
  100. if (!(cl->dl_status_map)) {
  101. cl->dl_status_map = dsmap_new();
  102. }
  103. /* Look for a download_status_t in the map with this digest */
  104. dlstatus = dsmap_get(cl->dl_status_map, digest);
  105. /* Got one? */
  106. if (!dlstatus) {
  107. /* Insert before we reset */
  108. dlstatus = tor_malloc_zero(sizeof(*dlstatus));
  109. dsmap_set(cl->dl_status_map, digest, dlstatus);
  110. download_status_cert_init(dlstatus);
  111. }
  112. tor_assert(dlstatus);
  113. /* Go ahead and reset it */
  114. download_status_reset(dlstatus);
  115. }
  116. /**
  117. * Return true if the download for this signing key digest in cl is ready
  118. * to be re-attempted.
  119. */
  120. static int
  121. download_status_is_ready_by_sk_in_cl(cert_list_t *cl,
  122. const char *digest,
  123. time_t now)
  124. {
  125. int rv = 0;
  126. download_status_t *dlstatus = NULL;
  127. tor_assert(cl);
  128. tor_assert(digest);
  129. /* Make sure we have a dsmap */
  130. if (!(cl->dl_status_map)) {
  131. cl->dl_status_map = dsmap_new();
  132. }
  133. /* Look for a download_status_t in the map with this digest */
  134. dlstatus = dsmap_get(cl->dl_status_map, digest);
  135. /* Got one? */
  136. if (dlstatus) {
  137. /* Use download_status_is_ready() */
  138. rv = download_status_is_ready(dlstatus, now);
  139. } else {
  140. /*
  141. * If we don't know anything about it, return 1, since we haven't
  142. * tried this one before. We need to create a new entry here,
  143. * too.
  144. */
  145. dlstatus = tor_malloc_zero(sizeof(*dlstatus));
  146. download_status_cert_init(dlstatus);
  147. dsmap_set(cl->dl_status_map, digest, dlstatus);
  148. rv = 1;
  149. }
  150. return rv;
  151. }
  152. /** Helper: Return the cert_list_t for an authority whose authority ID is
  153. * <b>id_digest</b>, allocating a new list if necessary. */
  154. static cert_list_t *
  155. get_cert_list(const char *id_digest)
  156. {
  157. cert_list_t *cl;
  158. if (!trusted_dir_certs)
  159. trusted_dir_certs = digestmap_new();
  160. cl = digestmap_get(trusted_dir_certs, id_digest);
  161. if (!cl) {
  162. cl = tor_malloc_zero(sizeof(cert_list_t));
  163. download_status_cert_init(&cl->dl_status_by_id);
  164. cl->certs = smartlist_new();
  165. cl->dl_status_map = dsmap_new();
  166. digestmap_set(trusted_dir_certs, id_digest, cl);
  167. }
  168. return cl;
  169. }
  170. /** Return a list of authority ID digests with potentially enumerable lists
  171. * of download_status_t objects; used by controller GETINFO queries.
  172. */
  173. MOCK_IMPL(smartlist_t *,
  174. list_authority_ids_with_downloads, (void))
  175. {
  176. smartlist_t *ids = smartlist_new();
  177. digestmap_iter_t *i;
  178. const char *digest;
  179. char *tmp;
  180. void *cl;
  181. if (trusted_dir_certs) {
  182. for (i = digestmap_iter_init(trusted_dir_certs);
  183. !(digestmap_iter_done(i));
  184. i = digestmap_iter_next(trusted_dir_certs, i)) {
  185. /*
  186. * We always have at least dl_status_by_id to query, so no need to
  187. * probe deeper than the existence of a cert_list_t.
  188. */
  189. digestmap_iter_get(i, &digest, &cl);
  190. tmp = tor_malloc(DIGEST_LEN);
  191. memcpy(tmp, digest, DIGEST_LEN);
  192. smartlist_add(ids, tmp);
  193. }
  194. }
  195. /* else definitely no downloads going since nothing even has a cert list */
  196. return ids;
  197. }
  198. /** Given an authority ID digest, return a pointer to the default download
  199. * status, or NULL if there is no such entry in trusted_dir_certs */
  200. MOCK_IMPL(download_status_t *,
  201. id_only_download_status_for_authority_id, (const char *digest))
  202. {
  203. download_status_t *dl = NULL;
  204. cert_list_t *cl;
  205. if (trusted_dir_certs) {
  206. cl = digestmap_get(trusted_dir_certs, digest);
  207. if (cl) {
  208. dl = &(cl->dl_status_by_id);
  209. }
  210. }
  211. return dl;
  212. }
  213. /** Given an authority ID digest, return a smartlist of signing key digests
  214. * for which download_status_t is potentially queryable, or NULL if no such
  215. * authority ID digest is known. */
  216. MOCK_IMPL(smartlist_t *,
  217. list_sk_digests_for_authority_id, (const char *digest))
  218. {
  219. smartlist_t *sks = NULL;
  220. cert_list_t *cl;
  221. dsmap_iter_t *i;
  222. const char *sk_digest;
  223. char *tmp;
  224. download_status_t *dl;
  225. if (trusted_dir_certs) {
  226. cl = digestmap_get(trusted_dir_certs, digest);
  227. if (cl) {
  228. sks = smartlist_new();
  229. if (cl->dl_status_map) {
  230. for (i = dsmap_iter_init(cl->dl_status_map);
  231. !(dsmap_iter_done(i));
  232. i = dsmap_iter_next(cl->dl_status_map, i)) {
  233. /* Pull the digest out and add it to the list */
  234. dsmap_iter_get(i, &sk_digest, &dl);
  235. tmp = tor_malloc(DIGEST_LEN);
  236. memcpy(tmp, sk_digest, DIGEST_LEN);
  237. smartlist_add(sks, tmp);
  238. }
  239. }
  240. }
  241. }
  242. return sks;
  243. }
  244. /** Given an authority ID digest and a signing key digest, return the
  245. * download_status_t or NULL if none exists. */
  246. MOCK_IMPL(download_status_t *,
  247. download_status_for_authority_id_and_sk,(const char *id_digest,
  248. const char *sk_digest))
  249. {
  250. download_status_t *dl = NULL;
  251. cert_list_t *cl = NULL;
  252. if (trusted_dir_certs) {
  253. cl = digestmap_get(trusted_dir_certs, id_digest);
  254. if (cl && cl->dl_status_map) {
  255. dl = dsmap_get(cl->dl_status_map, sk_digest);
  256. }
  257. }
  258. return dl;
  259. }
  260. #define cert_list_free(val) \
  261. FREE_AND_NULL(cert_list_t, cert_list_free_, (val))
  262. /** Release all space held by a cert_list_t */
  263. static void
  264. cert_list_free_(cert_list_t *cl)
  265. {
  266. if (!cl)
  267. return;
  268. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  269. authority_cert_free(cert));
  270. smartlist_free(cl->certs);
  271. dsmap_free(cl->dl_status_map, tor_free_);
  272. tor_free(cl);
  273. }
  274. /** Wrapper for cert_list_free so we can pass it to digestmap_free */
  275. static void
  276. cert_list_free_void(void *cl)
  277. {
  278. cert_list_free_(cl);
  279. }
  280. /** Reload the cached v3 key certificates from the cached-certs file in
  281. * the data directory. Return 0 on success, -1 on failure. */
  282. int
  283. trusted_dirs_reload_certs(void)
  284. {
  285. char *filename;
  286. char *contents;
  287. int r;
  288. filename = get_cachedir_fname("cached-certs");
  289. contents = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  290. tor_free(filename);
  291. if (!contents)
  292. return 0;
  293. r = trusted_dirs_load_certs_from_string(
  294. contents,
  295. TRUSTED_DIRS_CERTS_SRC_FROM_STORE, 1, NULL);
  296. tor_free(contents);
  297. return r;
  298. }
  299. /** Helper: return true iff we already have loaded the exact cert
  300. * <b>cert</b>. */
  301. static inline int
  302. already_have_cert(authority_cert_t *cert)
  303. {
  304. cert_list_t *cl = get_cert_list(cert->cache_info.identity_digest);
  305. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
  306. {
  307. if (tor_memeq(c->cache_info.signed_descriptor_digest,
  308. cert->cache_info.signed_descriptor_digest,
  309. DIGEST_LEN))
  310. return 1;
  311. });
  312. return 0;
  313. }
  314. /** Load a bunch of new key certificates from the string <b>contents</b>. If
  315. * <b>source</b> is TRUSTED_DIRS_CERTS_SRC_FROM_STORE, the certificates are
  316. * from the cache, and we don't need to flush them to disk. If we are a
  317. * dirauth loading our own cert, source is TRUSTED_DIRS_CERTS_SRC_SELF.
  318. * Otherwise, source is download type: TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
  319. * or TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST. If <b>flush</b> is true, we
  320. * need to flush any changed certificates to disk now. Return 0 on success,
  321. * -1 if any certs fail to parse.
  322. *
  323. * If source_dir is non-NULL, it's the identity digest for a directory that
  324. * we've just successfully retrieved certificates from, so try it first to
  325. * fetch any missing certificates.
  326. */
  327. int
  328. trusted_dirs_load_certs_from_string(const char *contents, int source,
  329. int flush, const char *source_dir)
  330. {
  331. dir_server_t *ds;
  332. const char *s, *eos;
  333. int failure_code = 0;
  334. int from_store = (source == TRUSTED_DIRS_CERTS_SRC_FROM_STORE);
  335. int added_trusted_cert = 0;
  336. for (s = contents; *s; s = eos) {
  337. authority_cert_t *cert = authority_cert_parse_from_string(s, &eos);
  338. cert_list_t *cl;
  339. if (!cert) {
  340. failure_code = -1;
  341. break;
  342. }
  343. ds = trusteddirserver_get_by_v3_auth_digest(
  344. cert->cache_info.identity_digest);
  345. log_debug(LD_DIR, "Parsed certificate for %s",
  346. ds ? ds->nickname : "unknown authority");
  347. if (already_have_cert(cert)) {
  348. /* we already have this one. continue. */
  349. log_info(LD_DIR, "Skipping %s certificate for %s that we "
  350. "already have.",
  351. from_store ? "cached" : "downloaded",
  352. ds ? ds->nickname : "an old or new authority");
  353. /*
  354. * A duplicate on download should be treated as a failure, so we call
  355. * authority_cert_dl_failed() to reset the download status to make sure
  356. * we can't try again. Since we've implemented the fp-sk mechanism
  357. * to download certs by signing key, this should be much rarer than it
  358. * was and is perhaps cause for concern.
  359. */
  360. if (!from_store) {
  361. if (authdir_mode(get_options())) {
  362. log_warn(LD_DIR,
  363. "Got a certificate for %s, but we already have it. "
  364. "Maybe they haven't updated it. Waiting for a while.",
  365. ds ? ds->nickname : "an old or new authority");
  366. } else {
  367. log_info(LD_DIR,
  368. "Got a certificate for %s, but we already have it. "
  369. "Maybe they haven't updated it. Waiting for a while.",
  370. ds ? ds->nickname : "an old or new authority");
  371. }
  372. /*
  373. * This is where we care about the source; authority_cert_dl_failed()
  374. * needs to know whether the download was by fp or (fp,sk) pair to
  375. * twiddle the right bit in the download map.
  376. */
  377. if (source == TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST) {
  378. authority_cert_dl_failed(cert->cache_info.identity_digest,
  379. NULL, 404);
  380. } else if (source == TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST) {
  381. authority_cert_dl_failed(cert->cache_info.identity_digest,
  382. cert->signing_key_digest, 404);
  383. }
  384. }
  385. authority_cert_free(cert);
  386. continue;
  387. }
  388. if (ds) {
  389. added_trusted_cert = 1;
  390. log_info(LD_DIR, "Adding %s certificate for directory authority %s with "
  391. "signing key %s", from_store ? "cached" : "downloaded",
  392. ds->nickname, hex_str(cert->signing_key_digest,DIGEST_LEN));
  393. } else {
  394. int adding = we_want_to_fetch_unknown_auth_certs(get_options());
  395. log_info(LD_DIR, "%s %s certificate for unrecognized directory "
  396. "authority with signing key %s",
  397. adding ? "Adding" : "Not adding",
  398. from_store ? "cached" : "downloaded",
  399. hex_str(cert->signing_key_digest,DIGEST_LEN));
  400. if (!adding) {
  401. authority_cert_free(cert);
  402. continue;
  403. }
  404. }
  405. cl = get_cert_list(cert->cache_info.identity_digest);
  406. smartlist_add(cl->certs, cert);
  407. if (ds && cert->cache_info.published_on > ds->addr_current_at) {
  408. /* Check to see whether we should update our view of the authority's
  409. * address. */
  410. if (cert->addr && cert->dir_port &&
  411. (ds->addr != cert->addr ||
  412. ds->dir_port != cert->dir_port)) {
  413. char *a = tor_dup_ip(cert->addr);
  414. log_notice(LD_DIR, "Updating address for directory authority %s "
  415. "from %s:%d to %s:%d based on certificate.",
  416. ds->nickname, ds->address, (int)ds->dir_port,
  417. a, cert->dir_port);
  418. tor_free(a);
  419. ds->addr = cert->addr;
  420. ds->dir_port = cert->dir_port;
  421. }
  422. ds->addr_current_at = cert->cache_info.published_on;
  423. }
  424. if (!from_store)
  425. trusted_dir_servers_certs_changed = 1;
  426. }
  427. if (flush)
  428. trusted_dirs_flush_certs_to_disk();
  429. /* call this even if failure_code is <0, since some certs might have
  430. * succeeded, but only pass source_dir if there were no failures,
  431. * and at least one more authority certificate was added to the store.
  432. * This avoids retrying a directory that's serving bad or entirely duplicate
  433. * certificates. */
  434. if (failure_code == 0 && added_trusted_cert) {
  435. networkstatus_note_certs_arrived(source_dir);
  436. } else {
  437. networkstatus_note_certs_arrived(NULL);
  438. }
  439. return failure_code;
  440. }
  441. /** Save all v3 key certificates to the cached-certs file. */
  442. void
  443. trusted_dirs_flush_certs_to_disk(void)
  444. {
  445. char *filename;
  446. smartlist_t *chunks;
  447. if (!trusted_dir_servers_certs_changed || !trusted_dir_certs)
  448. return;
  449. chunks = smartlist_new();
  450. DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
  451. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  452. {
  453. sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
  454. c->bytes = cert->cache_info.signed_descriptor_body;
  455. c->len = cert->cache_info.signed_descriptor_len;
  456. smartlist_add(chunks, c);
  457. });
  458. } DIGESTMAP_FOREACH_END;
  459. filename = get_cachedir_fname("cached-certs");
  460. if (write_chunks_to_file(filename, chunks, 0, 0)) {
  461. log_warn(LD_FS, "Error writing certificates to disk.");
  462. }
  463. tor_free(filename);
  464. SMARTLIST_FOREACH(chunks, sized_chunk_t *, c, tor_free(c));
  465. smartlist_free(chunks);
  466. trusted_dir_servers_certs_changed = 0;
  467. }
  468. static int
  469. compare_certs_by_pubdates(const void **_a, const void **_b)
  470. {
  471. const authority_cert_t *cert1 = *_a, *cert2=*_b;
  472. if (cert1->cache_info.published_on < cert2->cache_info.published_on)
  473. return -1;
  474. else if (cert1->cache_info.published_on > cert2->cache_info.published_on)
  475. return 1;
  476. else
  477. return 0;
  478. }
  479. /** Remove all expired v3 authority certificates that have been superseded for
  480. * more than 48 hours or, if not expired, that were published more than 7 days
  481. * before being superseded. (If the most recent cert was published more than 48
  482. * hours ago, then we aren't going to get any consensuses signed with older
  483. * keys.) */
  484. void
  485. trusted_dirs_remove_old_certs(void)
  486. {
  487. time_t now = time(NULL);
  488. #define DEAD_CERT_LIFETIME (2*24*60*60)
  489. #define SUPERSEDED_CERT_LIFETIME (2*24*60*60)
  490. if (!trusted_dir_certs)
  491. return;
  492. DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
  493. /* Sort the list from first-published to last-published */
  494. smartlist_sort(cl->certs, compare_certs_by_pubdates);
  495. SMARTLIST_FOREACH_BEGIN(cl->certs, authority_cert_t *, cert) {
  496. if (cert_sl_idx == smartlist_len(cl->certs) - 1) {
  497. /* This is the most recently published cert. Keep it. */
  498. continue;
  499. }
  500. authority_cert_t *next_cert = smartlist_get(cl->certs, cert_sl_idx+1);
  501. const time_t next_cert_published = next_cert->cache_info.published_on;
  502. if (next_cert_published > now) {
  503. /* All later certs are published in the future. Keep everything
  504. * we didn't discard. */
  505. break;
  506. }
  507. int should_remove = 0;
  508. if (cert->expires + DEAD_CERT_LIFETIME < now) {
  509. /* Certificate has been expired for at least DEAD_CERT_LIFETIME.
  510. * Remove it. */
  511. should_remove = 1;
  512. } else if (next_cert_published + SUPERSEDED_CERT_LIFETIME < now) {
  513. /* Certificate has been superseded for OLD_CERT_LIFETIME.
  514. * Remove it.
  515. */
  516. should_remove = 1;
  517. }
  518. if (should_remove) {
  519. SMARTLIST_DEL_CURRENT_KEEPORDER(cl->certs, cert);
  520. authority_cert_free(cert);
  521. trusted_dir_servers_certs_changed = 1;
  522. }
  523. } SMARTLIST_FOREACH_END(cert);
  524. } DIGESTMAP_FOREACH_END;
  525. #undef DEAD_CERT_LIFETIME
  526. #undef OLD_CERT_LIFETIME
  527. trusted_dirs_flush_certs_to_disk();
  528. }
  529. /** Return the newest v3 authority certificate whose v3 authority identity key
  530. * has digest <b>id_digest</b>. Return NULL if no such authority is known,
  531. * or it has no certificate. */
  532. authority_cert_t *
  533. authority_cert_get_newest_by_id(const char *id_digest)
  534. {
  535. cert_list_t *cl;
  536. authority_cert_t *best = NULL;
  537. if (!trusted_dir_certs ||
  538. !(cl = digestmap_get(trusted_dir_certs, id_digest)))
  539. return NULL;
  540. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  541. {
  542. if (!best || cert->cache_info.published_on > best->cache_info.published_on)
  543. best = cert;
  544. });
  545. return best;
  546. }
  547. /** Return the newest v3 authority certificate whose directory signing key has
  548. * digest <b>sk_digest</b>. Return NULL if no such certificate is known.
  549. */
  550. authority_cert_t *
  551. authority_cert_get_by_sk_digest(const char *sk_digest)
  552. {
  553. authority_cert_t *c;
  554. if (!trusted_dir_certs)
  555. return NULL;
  556. if ((c = get_my_v3_authority_cert()) &&
  557. tor_memeq(c->signing_key_digest, sk_digest, DIGEST_LEN))
  558. return c;
  559. if ((c = get_my_v3_legacy_cert()) &&
  560. tor_memeq(c->signing_key_digest, sk_digest, DIGEST_LEN))
  561. return c;
  562. DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
  563. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  564. {
  565. if (tor_memeq(cert->signing_key_digest, sk_digest, DIGEST_LEN))
  566. return cert;
  567. });
  568. } DIGESTMAP_FOREACH_END;
  569. return NULL;
  570. }
  571. /** Return the v3 authority certificate with signing key matching
  572. * <b>sk_digest</b>, for the authority with identity digest <b>id_digest</b>.
  573. * Return NULL if no such authority is known. */
  574. authority_cert_t *
  575. authority_cert_get_by_digests(const char *id_digest,
  576. const char *sk_digest)
  577. {
  578. cert_list_t *cl;
  579. if (!trusted_dir_certs ||
  580. !(cl = digestmap_get(trusted_dir_certs, id_digest)))
  581. return NULL;
  582. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  583. if (tor_memeq(cert->signing_key_digest, sk_digest, DIGEST_LEN))
  584. return cert; );
  585. return NULL;
  586. }
  587. /** Add every known authority_cert_t to <b>certs_out</b>. */
  588. void
  589. authority_cert_get_all(smartlist_t *certs_out)
  590. {
  591. tor_assert(certs_out);
  592. if (!trusted_dir_certs)
  593. return;
  594. DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
  595. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
  596. smartlist_add(certs_out, c));
  597. } DIGESTMAP_FOREACH_END;
  598. }
  599. /** Called when an attempt to download a certificate with the authority with
  600. * ID <b>id_digest</b> and, if not NULL, signed with key signing_key_digest
  601. * fails with HTTP response code <b>status</b>: remember the failure, so we
  602. * don't try again immediately. */
  603. void
  604. authority_cert_dl_failed(const char *id_digest,
  605. const char *signing_key_digest, int status)
  606. {
  607. cert_list_t *cl;
  608. download_status_t *dlstatus = NULL;
  609. char id_digest_str[2*DIGEST_LEN+1];
  610. char sk_digest_str[2*DIGEST_LEN+1];
  611. if (!trusted_dir_certs ||
  612. !(cl = digestmap_get(trusted_dir_certs, id_digest)))
  613. return;
  614. /*
  615. * Are we noting a failed download of the latest cert for the id digest,
  616. * or of a download by (id, signing key) digest pair?
  617. */
  618. if (!signing_key_digest) {
  619. /* Just by id digest */
  620. download_status_failed(&cl->dl_status_by_id, status);
  621. } else {
  622. /* Reset by (id, signing key) digest pair
  623. *
  624. * Look for a download_status_t in the map with this digest
  625. */
  626. dlstatus = dsmap_get(cl->dl_status_map, signing_key_digest);
  627. /* Got one? */
  628. if (dlstatus) {
  629. download_status_failed(dlstatus, status);
  630. } else {
  631. /*
  632. * Do this rather than hex_str(), since hex_str clobbers
  633. * old results and we call twice in the param list.
  634. */
  635. base16_encode(id_digest_str, sizeof(id_digest_str),
  636. id_digest, DIGEST_LEN);
  637. base16_encode(sk_digest_str, sizeof(sk_digest_str),
  638. signing_key_digest, DIGEST_LEN);
  639. log_warn(LD_BUG,
  640. "Got failure for cert fetch with (fp,sk) = (%s,%s), with "
  641. "status %d, but knew nothing about the download.",
  642. id_digest_str, sk_digest_str, status);
  643. }
  644. }
  645. }
  646. static const char *BAD_SIGNING_KEYS[] = {
  647. "09CD84F751FD6E955E0F8ADB497D5401470D697E", // Expires 2015-01-11 16:26:31
  648. "0E7E9C07F0969D0468AD741E172A6109DC289F3C", // Expires 2014-08-12 10:18:26
  649. "57B85409891D3FB32137F642FDEDF8B7F8CDFDCD", // Expires 2015-02-11 17:19:09
  650. "87326329007AF781F587AF5B594E540B2B6C7630", // Expires 2014-07-17 11:10:09
  651. "98CC82342DE8D298CF99D3F1A396475901E0D38E", // Expires 2014-11-10 13:18:56
  652. "9904B52336713A5ADCB13E4FB14DC919E0D45571", // Expires 2014-04-20 20:01:01
  653. "9DCD8E3F1DD1597E2AD476BBA28A1A89F3095227", // Expires 2015-01-16 03:52:30
  654. "A61682F34B9BB9694AC98491FE1ABBFE61923941", // Expires 2014-06-11 09:25:09
  655. "B59F6E99C575113650C99F1C425BA7B20A8C071D", // Expires 2014-07-31 13:22:10
  656. "D27178388FA75B96D37FA36E0B015227DDDBDA51", // Expires 2014-08-04 04:01:57
  657. NULL,
  658. };
  659. /** Return true iff <b>cert</b> authenticates some atuhority signing key
  660. * which, because of the old openssl heartbleed vulnerability, should
  661. * never be trusted. */
  662. int
  663. authority_cert_is_blacklisted(const authority_cert_t *cert)
  664. {
  665. char hex_digest[HEX_DIGEST_LEN+1];
  666. int i;
  667. base16_encode(hex_digest, sizeof(hex_digest),
  668. cert->signing_key_digest, sizeof(cert->signing_key_digest));
  669. for (i = 0; BAD_SIGNING_KEYS[i]; ++i) {
  670. if (!strcasecmp(hex_digest, BAD_SIGNING_KEYS[i])) {
  671. return 1;
  672. }
  673. }
  674. return 0;
  675. }
  676. /** Return true iff when we've been getting enough failures when trying to
  677. * download the certificate with ID digest <b>id_digest</b> that we're willing
  678. * to start bugging the user about it. */
  679. int
  680. authority_cert_dl_looks_uncertain(const char *id_digest)
  681. {
  682. #define N_AUTH_CERT_DL_FAILURES_TO_BUG_USER 2
  683. cert_list_t *cl;
  684. int n_failures;
  685. if (!trusted_dir_certs ||
  686. !(cl = digestmap_get(trusted_dir_certs, id_digest)))
  687. return 0;
  688. n_failures = download_status_get_n_failures(&cl->dl_status_by_id);
  689. return n_failures >= N_AUTH_CERT_DL_FAILURES_TO_BUG_USER;
  690. }
  691. /* Fetch the authority certificates specified in resource.
  692. * If we are a bridge client, and node is a configured bridge, fetch from node
  693. * using dir_hint as the fingerprint. Otherwise, if rs is not NULL, fetch from
  694. * rs. Otherwise, fetch from a random directory mirror. */
  695. static void
  696. authority_certs_fetch_resource_impl(const char *resource,
  697. const char *dir_hint,
  698. const node_t *node,
  699. const routerstatus_t *rs)
  700. {
  701. const or_options_t *options = get_options();
  702. int get_via_tor = purpose_needs_anonymity(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
  703. resource);
  704. /* Make sure bridge clients never connect to anything but a bridge */
  705. if (options->UseBridges) {
  706. if (node && !node_is_a_configured_bridge(node)) {
  707. /* If we're using bridges, and node is not a bridge, use a 3-hop path. */
  708. get_via_tor = 1;
  709. } else if (!node) {
  710. /* If we're using bridges, and there's no node, use a 3-hop path. */
  711. get_via_tor = 1;
  712. }
  713. }
  714. const dir_indirection_t indirection = get_via_tor ? DIRIND_ANONYMOUS
  715. : DIRIND_ONEHOP;
  716. directory_request_t *req = NULL;
  717. /* If we've just downloaded a consensus from a bridge, re-use that
  718. * bridge */
  719. if (options->UseBridges && node && node->ri && !get_via_tor) {
  720. /* clients always make OR connections to bridges */
  721. tor_addr_port_t or_ap;
  722. /* we are willing to use a non-preferred address if we need to */
  723. fascist_firewall_choose_address_node(node, FIREWALL_OR_CONNECTION, 0,
  724. &or_ap);
  725. req = directory_request_new(DIR_PURPOSE_FETCH_CERTIFICATE);
  726. directory_request_set_or_addr_port(req, &or_ap);
  727. if (dir_hint)
  728. directory_request_set_directory_id_digest(req, dir_hint);
  729. } else if (rs) {
  730. /* And if we've just downloaded a consensus from a directory, re-use that
  731. * directory */
  732. req = directory_request_new(DIR_PURPOSE_FETCH_CERTIFICATE);
  733. directory_request_set_routerstatus(req, rs);
  734. }
  735. if (req) {
  736. /* We've set up a request object -- fill in the other request fields, and
  737. * send the request. */
  738. directory_request_set_indirection(req, indirection);
  739. directory_request_set_resource(req, resource);
  740. directory_initiate_request(req);
  741. directory_request_free(req);
  742. return;
  743. }
  744. /* Otherwise, we want certs from a random fallback or directory
  745. * mirror, because they will almost always succeed. */
  746. directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
  747. resource, PDS_RETRY_IF_NO_SERVERS,
  748. DL_WANT_ANY_DIRSERVER);
  749. }
  750. /** Try to download any v3 authority certificates that we may be missing. If
  751. * <b>status</b> is provided, try to get all the ones that were used to sign
  752. * <b>status</b>. Additionally, try to have a non-expired certificate for
  753. * every V3 authority in trusted_dir_servers. Don't fetch certificates we
  754. * already have.
  755. *
  756. * If dir_hint is non-NULL, it's the identity digest for a directory that
  757. * we've just successfully retrieved a consensus or certificates from, so try
  758. * it first to fetch any missing certificates.
  759. **/
  760. void
  761. authority_certs_fetch_missing(networkstatus_t *status, time_t now,
  762. const char *dir_hint)
  763. {
  764. /*
  765. * The pending_id digestmap tracks pending certificate downloads by
  766. * identity digest; the pending_cert digestmap tracks pending downloads
  767. * by (identity digest, signing key digest) pairs.
  768. */
  769. digestmap_t *pending_id;
  770. fp_pair_map_t *pending_cert;
  771. /*
  772. * The missing_id_digests smartlist will hold a list of id digests
  773. * we want to fetch the newest cert for; the missing_cert_digests
  774. * smartlist will hold a list of fp_pair_t with an identity and
  775. * signing key digest.
  776. */
  777. smartlist_t *missing_cert_digests, *missing_id_digests;
  778. char *resource = NULL;
  779. cert_list_t *cl;
  780. const or_options_t *options = get_options();
  781. const int keep_unknown = we_want_to_fetch_unknown_auth_certs(options);
  782. fp_pair_t *fp_tmp = NULL;
  783. char id_digest_str[2*DIGEST_LEN+1];
  784. char sk_digest_str[2*DIGEST_LEN+1];
  785. if (should_delay_dir_fetches(options, NULL))
  786. return;
  787. pending_cert = fp_pair_map_new();
  788. pending_id = digestmap_new();
  789. missing_cert_digests = smartlist_new();
  790. missing_id_digests = smartlist_new();
  791. /*
  792. * First, we get the lists of already pending downloads so we don't
  793. * duplicate effort.
  794. */
  795. list_pending_downloads(pending_id, NULL,
  796. DIR_PURPOSE_FETCH_CERTIFICATE, "fp/");
  797. list_pending_fpsk_downloads(pending_cert);
  798. /*
  799. * Now, we download any trusted authority certs we don't have by
  800. * identity digest only. This gets the latest cert for that authority.
  801. */
  802. SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
  803. dir_server_t *, ds) {
  804. int found = 0;
  805. if (!(ds->type & V3_DIRINFO))
  806. continue;
  807. if (smartlist_contains_digest(missing_id_digests,
  808. ds->v3_identity_digest))
  809. continue;
  810. cl = get_cert_list(ds->v3_identity_digest);
  811. SMARTLIST_FOREACH_BEGIN(cl->certs, authority_cert_t *, cert) {
  812. if (now < cert->expires) {
  813. /* It's not expired, and we weren't looking for something to
  814. * verify a consensus with. Call it done. */
  815. download_status_reset(&(cl->dl_status_by_id));
  816. /* No sense trying to download it specifically by signing key hash */
  817. download_status_reset_by_sk_in_cl(cl, cert->signing_key_digest);
  818. found = 1;
  819. break;
  820. }
  821. } SMARTLIST_FOREACH_END(cert);
  822. if (!found &&
  823. download_status_is_ready(&(cl->dl_status_by_id), now) &&
  824. !digestmap_get(pending_id, ds->v3_identity_digest)) {
  825. log_info(LD_DIR,
  826. "No current certificate known for authority %s "
  827. "(ID digest %s); launching request.",
  828. ds->nickname, hex_str(ds->v3_identity_digest, DIGEST_LEN));
  829. smartlist_add(missing_id_digests, ds->v3_identity_digest);
  830. }
  831. } SMARTLIST_FOREACH_END(ds);
  832. /*
  833. * Next, if we have a consensus, scan through it and look for anything
  834. * signed with a key from a cert we don't have. Those get downloaded
  835. * by (fp,sk) pair, but if we don't know any certs at all for the fp
  836. * (identity digest), and it's one of the trusted dir server certs
  837. * we started off above or a pending download in pending_id, don't
  838. * try to get it yet. Most likely, the one we'll get for that will
  839. * have the right signing key too, and we'd just be downloading
  840. * redundantly.
  841. */
  842. if (status) {
  843. SMARTLIST_FOREACH_BEGIN(status->voters, networkstatus_voter_info_t *,
  844. voter) {
  845. if (!smartlist_len(voter->sigs))
  846. continue; /* This authority never signed this consensus, so don't
  847. * go looking for a cert with key digest 0000000000. */
  848. if (!keep_unknown &&
  849. !trusteddirserver_get_by_v3_auth_digest(voter->identity_digest))
  850. continue; /* We don't want unknown certs, and we don't know this
  851. * authority.*/
  852. /*
  853. * If we don't know *any* cert for this authority, and a download by ID
  854. * is pending or we added it to missing_id_digests above, skip this
  855. * one for now to avoid duplicate downloads.
  856. */
  857. cl = get_cert_list(voter->identity_digest);
  858. if (smartlist_len(cl->certs) == 0) {
  859. /* We have no certs at all for this one */
  860. /* Do we have a download of one pending? */
  861. if (digestmap_get(pending_id, voter->identity_digest))
  862. continue;
  863. /*
  864. * Are we about to launch a download of one due to the trusted
  865. * dir server check above?
  866. */
  867. if (smartlist_contains_digest(missing_id_digests,
  868. voter->identity_digest))
  869. continue;
  870. }
  871. SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
  872. authority_cert_t *cert =
  873. authority_cert_get_by_digests(voter->identity_digest,
  874. sig->signing_key_digest);
  875. if (cert) {
  876. if (now < cert->expires)
  877. download_status_reset_by_sk_in_cl(cl, sig->signing_key_digest);
  878. continue;
  879. }
  880. if (download_status_is_ready_by_sk_in_cl(
  881. cl, sig->signing_key_digest, now) &&
  882. !fp_pair_map_get_by_digests(pending_cert,
  883. voter->identity_digest,
  884. sig->signing_key_digest)) {
  885. /*
  886. * Do this rather than hex_str(), since hex_str clobbers
  887. * old results and we call twice in the param list.
  888. */
  889. base16_encode(id_digest_str, sizeof(id_digest_str),
  890. voter->identity_digest, DIGEST_LEN);
  891. base16_encode(sk_digest_str, sizeof(sk_digest_str),
  892. sig->signing_key_digest, DIGEST_LEN);
  893. if (voter->nickname) {
  894. log_info(LD_DIR,
  895. "We're missing a certificate from authority %s "
  896. "(ID digest %s) with signing key %s: "
  897. "launching request.",
  898. voter->nickname, id_digest_str, sk_digest_str);
  899. } else {
  900. log_info(LD_DIR,
  901. "We're missing a certificate from authority ID digest "
  902. "%s with signing key %s: launching request.",
  903. id_digest_str, sk_digest_str);
  904. }
  905. /* Allocate a new fp_pair_t to append */
  906. fp_tmp = tor_malloc(sizeof(*fp_tmp));
  907. memcpy(fp_tmp->first, voter->identity_digest, sizeof(fp_tmp->first));
  908. memcpy(fp_tmp->second, sig->signing_key_digest,
  909. sizeof(fp_tmp->second));
  910. smartlist_add(missing_cert_digests, fp_tmp);
  911. }
  912. } SMARTLIST_FOREACH_END(sig);
  913. } SMARTLIST_FOREACH_END(voter);
  914. }
  915. /* Bridge clients look up the node for the dir_hint */
  916. const node_t *node = NULL;
  917. /* All clients, including bridge clients, look up the routerstatus for the
  918. * dir_hint */
  919. const routerstatus_t *rs = NULL;
  920. /* If we still need certificates, try the directory that just successfully
  921. * served us a consensus or certificates.
  922. * As soon as the directory fails to provide additional certificates, we try
  923. * another, randomly selected directory. This avoids continual retries.
  924. * (We only ever have one outstanding request per certificate.)
  925. */
  926. if (dir_hint) {
  927. if (options->UseBridges) {
  928. /* Bridge clients try the nodelist. If the dir_hint is from an authority,
  929. * or something else fetched over tor, we won't find the node here, but
  930. * we will find the rs. */
  931. node = node_get_by_id(dir_hint);
  932. }
  933. /* All clients try the consensus routerstatus, then the fallback
  934. * routerstatus */
  935. rs = router_get_consensus_status_by_id(dir_hint);
  936. if (!rs) {
  937. /* This will also find authorities */
  938. const dir_server_t *ds = router_get_fallback_dirserver_by_digest(
  939. dir_hint);
  940. if (ds) {
  941. rs = &ds->fake_status;
  942. }
  943. }
  944. if (!node && !rs) {
  945. log_warn(LD_BUG, "Directory %s delivered a consensus, but %s"
  946. "no routerstatus could be found for it.",
  947. options->UseBridges ? "no node and " : "",
  948. hex_str(dir_hint, DIGEST_LEN));
  949. }
  950. }
  951. /* Do downloads by identity digest */
  952. if (smartlist_len(missing_id_digests) > 0) {
  953. int need_plus = 0;
  954. smartlist_t *fps = smartlist_new();
  955. smartlist_add_strdup(fps, "fp/");
  956. SMARTLIST_FOREACH_BEGIN(missing_id_digests, const char *, d) {
  957. char *fp = NULL;
  958. if (digestmap_get(pending_id, d))
  959. continue;
  960. base16_encode(id_digest_str, sizeof(id_digest_str),
  961. d, DIGEST_LEN);
  962. if (need_plus) {
  963. tor_asprintf(&fp, "+%s", id_digest_str);
  964. } else {
  965. /* No need for tor_asprintf() in this case; first one gets no '+' */
  966. fp = tor_strdup(id_digest_str);
  967. need_plus = 1;
  968. }
  969. smartlist_add(fps, fp);
  970. } SMARTLIST_FOREACH_END(d);
  971. if (smartlist_len(fps) > 1) {
  972. resource = smartlist_join_strings(fps, "", 0, NULL);
  973. /* node and rs are directories that just gave us a consensus or
  974. * certificates */
  975. authority_certs_fetch_resource_impl(resource, dir_hint, node, rs);
  976. tor_free(resource);
  977. }
  978. /* else we didn't add any: they were all pending */
  979. SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
  980. smartlist_free(fps);
  981. }
  982. /* Do downloads by identity digest/signing key pair */
  983. if (smartlist_len(missing_cert_digests) > 0) {
  984. int need_plus = 0;
  985. smartlist_t *fp_pairs = smartlist_new();
  986. smartlist_add_strdup(fp_pairs, "fp-sk/");
  987. SMARTLIST_FOREACH_BEGIN(missing_cert_digests, const fp_pair_t *, d) {
  988. char *fp_pair = NULL;
  989. if (fp_pair_map_get(pending_cert, d))
  990. continue;
  991. /* Construct string encodings of the digests */
  992. base16_encode(id_digest_str, sizeof(id_digest_str),
  993. d->first, DIGEST_LEN);
  994. base16_encode(sk_digest_str, sizeof(sk_digest_str),
  995. d->second, DIGEST_LEN);
  996. /* Now tor_asprintf() */
  997. if (need_plus) {
  998. tor_asprintf(&fp_pair, "+%s-%s", id_digest_str, sk_digest_str);
  999. } else {
  1000. /* First one in the list doesn't get a '+' */
  1001. tor_asprintf(&fp_pair, "%s-%s", id_digest_str, sk_digest_str);
  1002. need_plus = 1;
  1003. }
  1004. /* Add it to the list of pairs to request */
  1005. smartlist_add(fp_pairs, fp_pair);
  1006. } SMARTLIST_FOREACH_END(d);
  1007. if (smartlist_len(fp_pairs) > 1) {
  1008. resource = smartlist_join_strings(fp_pairs, "", 0, NULL);
  1009. /* node and rs are directories that just gave us a consensus or
  1010. * certificates */
  1011. authority_certs_fetch_resource_impl(resource, dir_hint, node, rs);
  1012. tor_free(resource);
  1013. }
  1014. /* else they were all pending */
  1015. SMARTLIST_FOREACH(fp_pairs, char *, p, tor_free(p));
  1016. smartlist_free(fp_pairs);
  1017. }
  1018. smartlist_free(missing_id_digests);
  1019. SMARTLIST_FOREACH(missing_cert_digests, fp_pair_t *, p, tor_free(p));
  1020. smartlist_free(missing_cert_digests);
  1021. digestmap_free(pending_id, NULL);
  1022. fp_pair_map_free(pending_cert, NULL);
  1023. }
  1024. void
  1025. authcert_free_all(void)
  1026. {
  1027. if (trusted_dir_certs) {
  1028. digestmap_free(trusted_dir_certs, cert_list_free_void);
  1029. trusted_dir_certs = NULL;
  1030. }
  1031. }
  1032. /** Free storage held in <b>cert</b>. */
  1033. void
  1034. authority_cert_free_(authority_cert_t *cert)
  1035. {
  1036. if (!cert)
  1037. return;
  1038. tor_free(cert->cache_info.signed_descriptor_body);
  1039. crypto_pk_free(cert->signing_key);
  1040. crypto_pk_free(cert->identity_key);
  1041. tor_free(cert);
  1042. }
  1043. /** For every certificate we are currently downloading by (identity digest,
  1044. * signing key digest) pair, set result[fp_pair] to (void *1).
  1045. */
  1046. static void
  1047. list_pending_fpsk_downloads(fp_pair_map_t *result)
  1048. {
  1049. const char *pfx = "fp-sk/";
  1050. smartlist_t *tmp;
  1051. smartlist_t *conns;
  1052. const char *resource;
  1053. tor_assert(result);
  1054. tmp = smartlist_new();
  1055. conns = get_connection_array();
  1056. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  1057. if (conn->type == CONN_TYPE_DIR &&
  1058. conn->purpose == DIR_PURPOSE_FETCH_CERTIFICATE &&
  1059. !conn->marked_for_close) {
  1060. resource = TO_DIR_CONN(conn)->requested_resource;
  1061. if (!strcmpstart(resource, pfx))
  1062. dir_split_resource_into_fingerprint_pairs(resource + strlen(pfx),
  1063. tmp);
  1064. }
  1065. } SMARTLIST_FOREACH_END(conn);
  1066. SMARTLIST_FOREACH_BEGIN(tmp, fp_pair_t *, fp) {
  1067. fp_pair_map_set(result, fp, (void*)1);
  1068. tor_free(fp);
  1069. } SMARTLIST_FOREACH_END(fp);
  1070. smartlist_free(tmp);
  1071. }