authcert.c 41 KB

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