/src/tor/src/feature/relay/routerkeys.c
Line | Count | Source |
1 | | /* Copyright (c) 2014-2021, The Tor Project, Inc. */ |
2 | | /* See LICENSE for licensing information */ |
3 | | |
4 | | /** |
5 | | * \file routerkeys.c |
6 | | * |
7 | | * \brief Functions and structures to handle generating and maintaining the |
8 | | * set of keypairs necessary to be an OR. |
9 | | * |
10 | | * The keys handled here now are the Ed25519 keys that Tor relays use to sign |
11 | | * descriptors, authenticate themselves on links, and identify one another |
12 | | * uniquely. Other keys are maintained in router.c and rendservice.c. |
13 | | * |
14 | | * (TODO: The keys in router.c should go here too.) |
15 | | */ |
16 | | |
17 | | #define ROUTERKEYS_PRIVATE |
18 | | |
19 | | #include "core/or/or.h" |
20 | | #include "app/config/config.h" |
21 | | #include "feature/relay/router.h" |
22 | | #include "feature/relay/routerkeys.h" |
23 | | #include "feature/relay/routermode.h" |
24 | | #include "feature/keymgt/loadkey.h" |
25 | | #include "feature/nodelist/torcert.h" |
26 | | #include "feature/nodelist/networkstatus_st.h" |
27 | | #include "feature/dirauth/dirvote.h" |
28 | | |
29 | | #include "lib/crypt_ops/crypto_util.h" |
30 | | #include "lib/crypt_ops/crypto_format.h" |
31 | | #include "lib/tls/tortls.h" |
32 | | #include "lib/tls/x509.h" |
33 | | |
34 | | #define ENC_KEY_HEADER "Boxed Ed25519 key" |
35 | | #define ENC_KEY_TAG "master" |
36 | | |
37 | | #ifdef HAVE_UNISTD_H |
38 | | #include <unistd.h> |
39 | | #endif |
40 | | |
41 | | static ed25519_keypair_t *master_identity_key = NULL; |
42 | | static ed25519_keypair_t *master_signing_key = NULL; |
43 | | static ed25519_keypair_t *current_auth_key = NULL; |
44 | | static tor_cert_t *signing_key_cert = NULL; |
45 | | static tor_cert_t *link_cert_cert = NULL; |
46 | | static tor_cert_t *auth_key_cert = NULL; |
47 | | |
48 | | static uint8_t *rsa_ed_crosscert = NULL; |
49 | | static size_t rsa_ed_crosscert_len = 0; |
50 | | static time_t rsa_ed_crosscert_expiration = 0; |
51 | | |
52 | | // list of ed25519_keypair_t |
53 | | static smartlist_t *family_id_keys = NULL; |
54 | | |
55 | | /** |
56 | | * Running as a server: load, reload, or refresh our ed25519 keys and |
57 | | * certificates, creating and saving new ones as needed. |
58 | | * |
59 | | * Return -1 on failure; 0 on success if the signing key was not replaced; |
60 | | * and 1 on success if the signing key was replaced. |
61 | | */ |
62 | | int |
63 | | load_ed_keys(const or_options_t *options, time_t now) |
64 | 0 | { |
65 | 0 | ed25519_keypair_t *id = NULL; |
66 | 0 | ed25519_keypair_t *sign = NULL; |
67 | 0 | ed25519_keypair_t *auth = NULL; |
68 | 0 | const ed25519_keypair_t *sign_signing_key_with_id = NULL; |
69 | 0 | const ed25519_keypair_t *use_signing = NULL; |
70 | 0 | const tor_cert_t *check_signing_cert = NULL; |
71 | 0 | tor_cert_t *sign_cert = NULL; |
72 | 0 | tor_cert_t *auth_cert = NULL; |
73 | 0 | int signing_key_changed = 0; |
74 | | |
75 | | // It is later than 1972, since otherwise there would be no C compilers. |
76 | | // (Try to diagnose #22466.) |
77 | 0 | tor_assert_nonfatal(now >= 2 * 365 * 86400); |
78 | |
|
79 | 0 | #define FAIL(msg) do { \ |
80 | 0 | log_warn(LD_OR, (msg)); \ |
81 | 0 | goto err; \ |
82 | 0 | } while (0) |
83 | 0 | #define SET_KEY(key, newval) do { \ |
84 | 0 | if ((key) != (newval)) \ |
85 | 0 | ed25519_keypair_free(key); \ |
86 | 0 | key = (newval); \ |
87 | 0 | } while (0) |
88 | 0 | #define SET_CERT(cert, newval) do { \ |
89 | 0 | if ((cert) != (newval)) \ |
90 | 0 | tor_cert_free(cert); \ |
91 | 0 | cert = (newval); \ |
92 | 0 | } while (0) |
93 | 0 | #define HAPPENS_SOON(when, interval) \ |
94 | 0 | ((when) < now + (interval)) |
95 | 0 | #define EXPIRES_SOON(cert, interval) \ |
96 | 0 | (!(cert) || HAPPENS_SOON((cert)->valid_until, (interval))) |
97 | | |
98 | | /* XXXX support encrypted identity keys fully */ |
99 | | |
100 | | /* First try to get the signing key to see how it is. */ |
101 | 0 | { |
102 | 0 | char *fname = |
103 | 0 | options_get_keydir_fname(options, "ed25519_signing"); |
104 | 0 | sign = ed_key_init_from_file( |
105 | 0 | fname, |
106 | 0 | INIT_ED_KEY_NEEDCERT| |
107 | 0 | INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT, |
108 | 0 | LOG_INFO, |
109 | 0 | NULL, 0, 0, CERT_TYPE_ID_SIGNING, &sign_cert, options); |
110 | 0 | tor_free(fname); |
111 | 0 | check_signing_cert = sign_cert; |
112 | 0 | use_signing = sign; |
113 | 0 | } |
114 | |
|
115 | 0 | if (use_signing) { |
116 | | /* We loaded a signing key with its certificate. */ |
117 | 0 | if (! master_signing_key) { |
118 | | /* We didn't know one before! */ |
119 | 0 | signing_key_changed = 1; |
120 | 0 | } else if (! ed25519_pubkey_eq(&use_signing->pubkey, |
121 | 0 | &master_signing_key->pubkey) || |
122 | 0 | ! tor_memeq(use_signing->seckey.seckey, |
123 | 0 | master_signing_key->seckey.seckey, |
124 | 0 | ED25519_SECKEY_LEN)) { |
125 | | /* We loaded a different signing key than the one we knew before. */ |
126 | 0 | signing_key_changed = 1; |
127 | 0 | } |
128 | 0 | } |
129 | |
|
130 | 0 | if (!use_signing && master_signing_key) { |
131 | | /* We couldn't load a signing key, but we already had one loaded */ |
132 | 0 | check_signing_cert = signing_key_cert; |
133 | 0 | use_signing = master_signing_key; |
134 | 0 | } |
135 | |
|
136 | 0 | const int offline_master = |
137 | 0 | options->OfflineMasterKey && options->command != CMD_KEYGEN; |
138 | 0 | const int need_new_signing_key = |
139 | 0 | NULL == use_signing || |
140 | 0 | EXPIRES_SOON(check_signing_cert, 0) || |
141 | 0 | (options->command == CMD_KEYGEN && ! options->change_key_passphrase); |
142 | 0 | const int want_new_signing_key = |
143 | 0 | need_new_signing_key || |
144 | 0 | EXPIRES_SOON(check_signing_cert, options->TestingSigningKeySlop); |
145 | | |
146 | | /* We can only create a master key if we haven't been told that the |
147 | | * master key will always be offline. Also, if we have a signing key, |
148 | | * then we shouldn't make a new master ID key. */ |
149 | 0 | const int can_make_master_id_key = !offline_master && |
150 | 0 | NULL == use_signing; |
151 | |
|
152 | 0 | if (need_new_signing_key) { |
153 | 0 | log_notice(LD_OR, "It looks like I need to generate and sign a new " |
154 | 0 | "medium-term signing key, because %s. To do that, I " |
155 | 0 | "need to load%s the permanent master identity key. " |
156 | 0 | "If the master identity key was not moved or encrypted " |
157 | 0 | "with a passphrase, this will be done automatically and " |
158 | 0 | "no further action is required. Otherwise, provide the " |
159 | 0 | "necessary data using 'tor --keygen' to do it manually.", |
160 | 0 | (NULL == use_signing) ? "I don't have one" : |
161 | 0 | EXPIRES_SOON(check_signing_cert, 0) ? "the one I have is expired" : |
162 | 0 | "you asked me to make one with --keygen", |
163 | 0 | can_make_master_id_key ? " (or create)" : ""); |
164 | 0 | } else if (want_new_signing_key && !offline_master) { |
165 | 0 | log_notice(LD_OR, "It looks like I should try to generate and sign a " |
166 | 0 | "new medium-term signing key, because the one I have is " |
167 | 0 | "going to expire soon. To do that, I'm going to have to " |
168 | 0 | "try to load the permanent master identity key. " |
169 | 0 | "If the master identity key was not moved or encrypted " |
170 | 0 | "with a passphrase, this will be done automatically and " |
171 | 0 | "no further action is required. Otherwise, provide the " |
172 | 0 | "necessary data using 'tor --keygen' to do it manually."); |
173 | 0 | } else if (want_new_signing_key) { |
174 | 0 | log_notice(LD_OR, "It looks like I should try to generate and sign a " |
175 | 0 | "new medium-term signing key, because the one I have is " |
176 | 0 | "going to expire soon. But OfflineMasterKey is set, so I " |
177 | 0 | "won't try to load a permanent master identity key. You " |
178 | 0 | "will need to use 'tor --keygen' to make a new signing " |
179 | 0 | "key and certificate."); |
180 | 0 | } |
181 | |
|
182 | 0 | { |
183 | 0 | uint32_t flags = |
184 | 0 | (INIT_ED_KEY_SPLIT| |
185 | 0 | INIT_ED_KEY_EXTRA_STRONG|INIT_ED_KEY_NO_REPAIR); |
186 | 0 | if (can_make_master_id_key) |
187 | 0 | flags |= INIT_ED_KEY_CREATE; |
188 | 0 | if (! need_new_signing_key) |
189 | 0 | flags |= INIT_ED_KEY_MISSING_SECRET_OK; |
190 | 0 | if (! want_new_signing_key || offline_master) |
191 | 0 | flags |= INIT_ED_KEY_OMIT_SECRET; |
192 | 0 | if (offline_master) |
193 | 0 | flags |= INIT_ED_KEY_OFFLINE_SECRET; |
194 | 0 | if (options->command == CMD_KEYGEN) |
195 | 0 | flags |= INIT_ED_KEY_TRY_ENCRYPTED; |
196 | | |
197 | | /* Check/Create the key directory */ |
198 | 0 | if (create_keys_directory(options) < 0) |
199 | 0 | goto err; |
200 | | |
201 | 0 | char *fname; |
202 | 0 | if (options->master_key_fname) { |
203 | 0 | fname = tor_strdup(options->master_key_fname); |
204 | 0 | flags |= INIT_ED_KEY_EXPLICIT_FNAME; |
205 | 0 | } else { |
206 | 0 | fname = options_get_keydir_fname(options, "ed25519_master_id"); |
207 | 0 | } |
208 | 0 | id = ed_key_init_from_file( |
209 | 0 | fname, |
210 | 0 | flags, |
211 | 0 | LOG_WARN, NULL, 0, 0, 0, NULL, options); |
212 | 0 | tor_free(fname); |
213 | 0 | if (!id) { |
214 | 0 | if (need_new_signing_key) { |
215 | 0 | if (offline_master) |
216 | 0 | FAIL("Can't load master identity key; OfflineMasterKey is set."); |
217 | 0 | else |
218 | 0 | FAIL("Missing identity key"); |
219 | 0 | } else { |
220 | 0 | log_warn(LD_OR, "Master public key was absent; inferring from " |
221 | 0 | "public key in signing certificate and saving to disk."); |
222 | 0 | tor_assert(check_signing_cert); |
223 | 0 | id = tor_malloc_zero(sizeof(*id)); |
224 | 0 | memcpy(&id->pubkey, &check_signing_cert->signing_key, |
225 | 0 | sizeof(ed25519_public_key_t)); |
226 | 0 | fname = options_get_keydir_fname(options, |
227 | 0 | "ed25519_master_id_public_key"); |
228 | 0 | if (ed25519_pubkey_write_to_file(&id->pubkey, fname, "type0") < 0) { |
229 | 0 | log_warn(LD_OR, "Error while attempting to write master public key " |
230 | 0 | "to disk"); |
231 | 0 | tor_free(fname); |
232 | 0 | goto err; |
233 | 0 | } |
234 | 0 | tor_free(fname); |
235 | 0 | } |
236 | 0 | } |
237 | 0 | if (safe_mem_is_zero((char*)id->seckey.seckey, sizeof(id->seckey))) |
238 | 0 | sign_signing_key_with_id = NULL; |
239 | 0 | else |
240 | 0 | sign_signing_key_with_id = id; |
241 | 0 | } |
242 | | |
243 | 0 | if (master_identity_key && |
244 | 0 | !ed25519_pubkey_eq(&id->pubkey, &master_identity_key->pubkey)) { |
245 | 0 | FAIL("Identity key on disk does not match key we loaded earlier!"); |
246 | 0 | } |
247 | | |
248 | 0 | if (need_new_signing_key && NULL == sign_signing_key_with_id) |
249 | 0 | FAIL("Can't load master key make a new signing key."); |
250 | | |
251 | 0 | if (sign_cert) { |
252 | 0 | if (! sign_cert->signing_key_included) |
253 | 0 | FAIL("Loaded a signing cert with no key included!"); |
254 | 0 | if (! ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey)) |
255 | 0 | FAIL("The signing cert we have was not signed with the master key " |
256 | 0 | "we loaded!"); |
257 | 0 | if (tor_cert_checksig(sign_cert, &id->pubkey, 0) < 0) { |
258 | 0 | log_warn(LD_OR, "The signing cert we loaded was not signed " |
259 | 0 | "correctly: %s!", |
260 | 0 | tor_cert_describe_signature_status(sign_cert)); |
261 | 0 | goto err; |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | 0 | if (want_new_signing_key && sign_signing_key_with_id) { |
266 | 0 | uint32_t flags = (INIT_ED_KEY_CREATE| |
267 | 0 | INIT_ED_KEY_REPLACE| |
268 | 0 | INIT_ED_KEY_EXTRA_STRONG| |
269 | 0 | INIT_ED_KEY_NEEDCERT| |
270 | 0 | INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT); |
271 | 0 | char *fname = |
272 | 0 | options_get_keydir_fname(options, "ed25519_signing"); |
273 | 0 | ed25519_keypair_free(sign); |
274 | 0 | tor_cert_free(sign_cert); |
275 | 0 | sign = ed_key_init_from_file(fname, |
276 | 0 | flags, LOG_WARN, |
277 | 0 | sign_signing_key_with_id, now, |
278 | 0 | options->SigningKeyLifetime, |
279 | 0 | CERT_TYPE_ID_SIGNING, &sign_cert, options); |
280 | 0 | tor_free(fname); |
281 | 0 | if (!sign) |
282 | 0 | FAIL("Missing signing key"); |
283 | 0 | use_signing = sign; |
284 | 0 | signing_key_changed = 1; |
285 | |
|
286 | 0 | tor_assert(sign_cert->signing_key_included); |
287 | 0 | tor_assert(ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey)); |
288 | 0 | tor_assert(ed25519_pubkey_eq(&sign_cert->signed_key, &sign->pubkey)); |
289 | 0 | } else if (want_new_signing_key) { |
290 | 0 | static ratelim_t missing_master = RATELIM_INIT(3600); |
291 | 0 | log_fn_ratelim(&missing_master, LOG_WARN, LD_OR, |
292 | 0 | "Signing key will expire soon, but I can't load the " |
293 | 0 | "master key to sign a new one!"); |
294 | 0 | } |
295 | | |
296 | 0 | tor_assert(use_signing); |
297 | | |
298 | | /* At this point we no longer need our secret identity key. So wipe |
299 | | * it, if we loaded it in the first place. */ |
300 | 0 | memwipe(id->seckey.seckey, 0, sizeof(id->seckey)); |
301 | |
|
302 | 0 | if (options->command == CMD_KEYGEN) |
303 | 0 | goto end; |
304 | | |
305 | 0 | if (server_mode(options) && |
306 | 0 | (!rsa_ed_crosscert || |
307 | 0 | HAPPENS_SOON(rsa_ed_crosscert_expiration, 30*86400))) { |
308 | 0 | uint8_t *crosscert; |
309 | 0 | time_t expiration = now+6*30*86400; /* 6 months in the future. */ |
310 | 0 | ssize_t crosscert_len = tor_make_rsa_ed25519_crosscert(&id->pubkey, |
311 | 0 | get_server_identity_key(), |
312 | 0 | expiration, |
313 | 0 | &crosscert); |
314 | 0 | tor_free(rsa_ed_crosscert); |
315 | 0 | rsa_ed_crosscert_len = crosscert_len; |
316 | 0 | rsa_ed_crosscert = crosscert; |
317 | 0 | rsa_ed_crosscert_expiration = expiration; |
318 | 0 | } |
319 | |
|
320 | 0 | if (!current_auth_key || |
321 | 0 | signing_key_changed || |
322 | 0 | EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop)) { |
323 | 0 | auth = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT, |
324 | 0 | now, |
325 | 0 | options->TestingAuthKeyLifetime, |
326 | 0 | CERT_TYPE_SIGNING_AUTH, &auth_cert); |
327 | |
|
328 | 0 | if (!auth) |
329 | 0 | FAIL("Can't create auth key"); |
330 | 0 | } |
331 | | |
332 | | /* We've generated or loaded everything. Put them in memory. */ |
333 | | |
334 | 0 | end: |
335 | 0 | if (! master_identity_key) { |
336 | 0 | SET_KEY(master_identity_key, id); |
337 | 0 | } else { |
338 | 0 | tor_free(id); |
339 | 0 | } |
340 | 0 | if (sign) { |
341 | 0 | SET_KEY(master_signing_key, sign); |
342 | 0 | SET_CERT(signing_key_cert, sign_cert); |
343 | 0 | } |
344 | 0 | if (auth) { |
345 | 0 | SET_KEY(current_auth_key, auth); |
346 | 0 | SET_CERT(auth_key_cert, auth_cert); |
347 | 0 | } |
348 | |
|
349 | 0 | return signing_key_changed; |
350 | 0 | err: |
351 | 0 | ed25519_keypair_free(id); |
352 | 0 | ed25519_keypair_free(sign); |
353 | 0 | ed25519_keypair_free(auth); |
354 | 0 | tor_cert_free(sign_cert); |
355 | 0 | tor_cert_free(auth_cert); |
356 | 0 | return -1; |
357 | 0 | } |
358 | | |
359 | | /** |
360 | | * Retrieve our currently-in-use Ed25519 link certificate and id certificate, |
361 | | * and, if they would expire soon (based on the time <b>now</b>, generate new |
362 | | * certificates (without embedding the public part of the signing key inside). |
363 | | * If <b>force</b> is true, always generate a new certificate. |
364 | | * |
365 | | * The signed_key from the current id->signing certificate will be used to |
366 | | * sign the new key within newly generated X509 certificate. |
367 | | * |
368 | | * Returns -1 upon error. Otherwise, returns 0 upon success (either when the |
369 | | * current certificate is still valid, or when a new certificate was |
370 | | * successfully generated, or no certificate was needed). |
371 | | */ |
372 | | int |
373 | | generate_ed_link_cert(const or_options_t *options, time_t now, |
374 | | int force) |
375 | 0 | { |
376 | 0 | const tor_x509_cert_t *link_ = NULL, *id = NULL; |
377 | 0 | tor_cert_t *link_cert = NULL; |
378 | |
|
379 | 0 | if (tor_tls_get_my_certs(1, &link_, &id) < 0 || link_ == NULL) { |
380 | 0 | if (!server_mode(options)) { |
381 | | /* No need to make an Ed25519->Link cert: we are a client */ |
382 | 0 | return 0; |
383 | 0 | } |
384 | 0 | log_warn(LD_OR, "Can't get my x509 link cert."); |
385 | 0 | return -1; |
386 | 0 | } |
387 | | |
388 | 0 | const common_digests_t *digests = tor_x509_cert_get_cert_digests(link_); |
389 | |
|
390 | 0 | if (force == 0 && |
391 | 0 | link_cert_cert && |
392 | 0 | ! EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop) && |
393 | 0 | fast_memeq(digests->d[DIGEST_SHA256], link_cert_cert->signed_key.pubkey, |
394 | 0 | DIGEST256_LEN)) { |
395 | 0 | return 0; |
396 | 0 | } |
397 | | |
398 | 0 | link_cert = tor_cert_create_raw(get_master_signing_keypair(), |
399 | 0 | CERT_TYPE_SIGNING_LINK, |
400 | 0 | SIGNED_KEY_TYPE_SHA256_OF_X509, |
401 | 0 | (const uint8_t*)digests->d[DIGEST_SHA256], |
402 | 0 | now, |
403 | 0 | options->TestingLinkCertLifetime, 0); |
404 | |
|
405 | 0 | if (link_cert) { |
406 | 0 | SET_CERT(link_cert_cert, link_cert); |
407 | 0 | } |
408 | 0 | return 0; |
409 | 0 | } |
410 | | |
411 | | #undef FAIL |
412 | | #undef SET_KEY |
413 | | #undef SET_CERT |
414 | | |
415 | | /** |
416 | | * Return 1 if any of the following are true: |
417 | | * |
418 | | * - if one of our Ed25519 signing, auth, or link certificates would expire |
419 | | * soon w.r.t. the time <b>now</b>, |
420 | | * - if we do not currently have a link certificate, or |
421 | | * - if our cached Ed25519 link certificate is not same as the one we're |
422 | | * currently using. |
423 | | * |
424 | | * Otherwise, returns 0. |
425 | | */ |
426 | | int |
427 | | should_make_new_ed_keys(const or_options_t *options, const time_t now) |
428 | 0 | { |
429 | 0 | if (!master_identity_key || |
430 | 0 | !master_signing_key || |
431 | 0 | !current_auth_key || |
432 | 0 | !link_cert_cert || |
433 | 0 | EXPIRES_SOON(signing_key_cert, options->TestingSigningKeySlop) || |
434 | 0 | EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop) || |
435 | 0 | EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop)) |
436 | 0 | return 1; |
437 | | |
438 | 0 | const tor_x509_cert_t *link_ = NULL, *id = NULL; |
439 | |
|
440 | 0 | if (tor_tls_get_my_certs(1, &link_, &id) < 0 || link_ == NULL) |
441 | 0 | return 1; |
442 | | |
443 | 0 | const common_digests_t *digests = tor_x509_cert_get_cert_digests(link_); |
444 | |
|
445 | 0 | if (!fast_memeq(digests->d[DIGEST_SHA256], |
446 | 0 | link_cert_cert->signed_key.pubkey, |
447 | 0 | DIGEST256_LEN)) { |
448 | 0 | return 1; |
449 | 0 | } |
450 | | |
451 | 0 | return 0; |
452 | 0 | } |
453 | | |
454 | | #undef EXPIRES_SOON |
455 | | #undef HAPPENS_SOON |
456 | | |
457 | | #ifdef TOR_UNIT_TESTS |
458 | | /* Helper for unit tests: populate the ed25519 keys without saving or |
459 | | * loading */ |
460 | | void |
461 | | init_mock_ed_keys(const crypto_pk_t *rsa_identity_key) |
462 | 0 | { |
463 | 0 | routerkeys_free_all(); |
464 | |
|
465 | 0 | #define MAKEKEY(k) \ |
466 | 0 | k = tor_malloc_zero(sizeof(*k)); \ |
467 | 0 | if (ed25519_keypair_generate(k, 0) < 0) { \ |
468 | 0 | log_warn(LD_BUG, "Couldn't make a keypair"); \ |
469 | 0 | goto err; \ |
470 | 0 | } |
471 | 0 | MAKEKEY(master_identity_key); |
472 | 0 | MAKEKEY(master_signing_key); |
473 | 0 | MAKEKEY(current_auth_key); |
474 | 0 | #define MAKECERT(cert, signing, signed_, type, flags) \ |
475 | 0 | cert = tor_cert_create_ed25519(signing, \ |
476 | 0 | type, \ |
477 | 0 | &signed_->pubkey, \ |
478 | 0 | time(NULL), 86400, \ |
479 | 0 | flags); \ |
480 | 0 | if (!cert) { \ |
481 | 0 | log_warn(LD_BUG, "Couldn't make a %s certificate!", #cert); \ |
482 | 0 | goto err; \ |
483 | 0 | } |
484 | |
|
485 | 0 | MAKECERT(signing_key_cert, |
486 | 0 | master_identity_key, master_signing_key, CERT_TYPE_ID_SIGNING, |
487 | 0 | CERT_FLAG_INCLUDE_SIGNING_KEY); |
488 | 0 | MAKECERT(auth_key_cert, |
489 | 0 | master_signing_key, current_auth_key, CERT_TYPE_SIGNING_AUTH, 0); |
490 | |
|
491 | 0 | if (generate_ed_link_cert(get_options(), time(NULL), 0) < 0) { |
492 | 0 | log_warn(LD_BUG, "Couldn't make link certificate"); |
493 | 0 | goto err; |
494 | 0 | } |
495 | | |
496 | 0 | rsa_ed_crosscert_len = tor_make_rsa_ed25519_crosscert( |
497 | 0 | &master_identity_key->pubkey, |
498 | 0 | rsa_identity_key, |
499 | 0 | time(NULL)+86400, |
500 | 0 | &rsa_ed_crosscert); |
501 | |
|
502 | 0 | return; |
503 | | |
504 | 0 | err: |
505 | 0 | routerkeys_free_all(); |
506 | 0 | tor_assert_nonfatal_unreached(); |
507 | 0 | } |
508 | | #undef MAKEKEY |
509 | | #undef MAKECERT |
510 | | #endif /* defined(TOR_UNIT_TESTS) */ |
511 | | |
512 | | /** |
513 | | * Print the ISO8601-formated <b>expiration</b> for a certificate with |
514 | | * some <b>description</b> to stdout. |
515 | | * |
516 | | * For example, for a signing certificate, this might print out: |
517 | | * signing-cert-expiry: 2017-07-25 08:30:15 UTC |
518 | | */ |
519 | | static void |
520 | | print_cert_expiration(const char *expiration, |
521 | | const char *description) |
522 | 0 | { |
523 | 0 | fprintf(stderr, "%s-cert-expiry: %s\n", description, expiration); |
524 | 0 | } |
525 | | |
526 | | /** |
527 | | * Log when a certificate, <b>cert</b>, with some <b>description</b> and |
528 | | * stored in a file named <b>fname</b>, is going to expire. Formats the expire |
529 | | * time according to <b>time_format</b>. |
530 | | */ |
531 | | static void |
532 | | log_ed_cert_expiration(const tor_cert_t *cert, |
533 | | const char *description, |
534 | | const char *fname, |
535 | 0 | key_expiration_format_t time_format) { |
536 | 0 | if (BUG(!cert)) { /* If the specified key hasn't been loaded */ |
537 | 0 | log_warn(LD_OR, "No %s key loaded; can't get certificate expiration.", |
538 | 0 | description); |
539 | 0 | } else { |
540 | 0 | char expiration[ISO_TIME_LEN+1]; |
541 | 0 | switch (time_format) { |
542 | 0 | case KEY_EXPIRATION_FORMAT_ISO8601: |
543 | 0 | format_local_iso_time(expiration, cert->valid_until); |
544 | 0 | break; |
545 | | |
546 | 0 | case KEY_EXPIRATION_FORMAT_TIMESTAMP: |
547 | 0 | tor_snprintf(expiration, sizeof(expiration), "%"PRId64, |
548 | 0 | (int64_t) cert->valid_until); |
549 | 0 | break; |
550 | | |
551 | 0 | default: |
552 | 0 | log_err(LD_BUG, "Unknown time format value: %d.", time_format); |
553 | 0 | return; |
554 | 0 | } |
555 | 0 | log_notice(LD_OR, "The %s certificate stored in %s is valid until %s.", |
556 | 0 | description, fname, expiration); |
557 | 0 | print_cert_expiration(expiration, description); |
558 | 0 | } |
559 | 0 | } |
560 | | |
561 | | /** |
562 | | * Log when our master signing key certificate expires. Used when tor is given |
563 | | * the --key-expiration command-line option. |
564 | | * |
565 | | * Returns 0 on success and 1 on failure. |
566 | | */ |
567 | | static int |
568 | | log_master_signing_key_cert_expiration(const or_options_t *options) |
569 | 0 | { |
570 | 0 | const tor_cert_t *signing_key; |
571 | 0 | char *fn = NULL; |
572 | 0 | int failed = 0; |
573 | 0 | time_t now = approx_time(); |
574 | |
|
575 | 0 | fn = options_get_keydir_fname(options, "ed25519_signing_cert"); |
576 | | |
577 | | /* Try to grab our cached copy of the key. */ |
578 | 0 | signing_key = get_master_signing_key_cert(); |
579 | |
|
580 | 0 | tor_assert(server_identity_key_is_set()); |
581 | | |
582 | | /* Load our keys from disk, if necessary. */ |
583 | 0 | if (!signing_key) { |
584 | 0 | failed = load_ed_keys(options, now) < 0; |
585 | 0 | signing_key = get_master_signing_key_cert(); |
586 | 0 | } |
587 | | |
588 | | /* If we do have a signing key, log the expiration time. */ |
589 | 0 | if (signing_key) { |
590 | 0 | key_expiration_format_t time_format = options->key_expiration_format; |
591 | 0 | log_ed_cert_expiration(signing_key, "signing", fn, time_format); |
592 | 0 | } else { |
593 | 0 | log_warn(LD_OR, "Could not load signing key certificate from %s, so " \ |
594 | 0 | "we couldn't learn anything about certificate expiration.", fn); |
595 | 0 | } |
596 | |
|
597 | 0 | tor_free(fn); |
598 | |
|
599 | 0 | return failed; |
600 | 0 | } |
601 | | |
602 | | /** |
603 | | * Log when a key certificate expires. Used when tor is given the |
604 | | * --key-expiration command-line option. |
605 | | * |
606 | | * If an command argument is given, which should specify the type of |
607 | | * key to get expiry information about (currently supported arguments |
608 | | * are "sign"), get info about that type of certificate. Otherwise, |
609 | | * print info about the supported arguments. |
610 | | * |
611 | | * Returns 0 on success and -1 on failure. |
612 | | */ |
613 | | int |
614 | | log_cert_expiration(void) |
615 | 0 | { |
616 | 0 | const or_options_t *options = get_options(); |
617 | 0 | const char *arg = options->command_arg; |
618 | |
|
619 | 0 | if (!strcmp(arg, "sign")) { |
620 | 0 | return log_master_signing_key_cert_expiration(options); |
621 | 0 | } else { |
622 | 0 | fprintf(stderr, "No valid argument to --key-expiration found!\n"); |
623 | 0 | fprintf(stderr, "Currently recognised arguments are: 'sign'\n"); |
624 | |
|
625 | 0 | return -1; |
626 | 0 | } |
627 | 0 | } |
628 | | |
629 | | const ed25519_public_key_t * |
630 | | get_master_identity_key(void) |
631 | 0 | { |
632 | 0 | if (!master_identity_key) |
633 | 0 | return NULL; |
634 | 0 | return &master_identity_key->pubkey; |
635 | 0 | } |
636 | | |
637 | | /** Return true iff <b>id</b> is our Ed25519 master identity key. */ |
638 | | int |
639 | | router_ed25519_id_is_me(const ed25519_public_key_t *id) |
640 | 0 | { |
641 | 0 | return id && master_identity_key && |
642 | 0 | ed25519_pubkey_eq(id, &master_identity_key->pubkey); |
643 | 0 | } |
644 | | |
645 | | #ifdef TOR_UNIT_TESTS |
646 | | /* only exists for the unit tests, since otherwise the identity key |
647 | | * should be used to sign nothing but the signing key. */ |
648 | | const ed25519_keypair_t * |
649 | | get_master_identity_keypair(void) |
650 | 0 | { |
651 | 0 | return master_identity_key; |
652 | 0 | } |
653 | | #endif /* defined(TOR_UNIT_TESTS) */ |
654 | | |
655 | | MOCK_IMPL(const ed25519_keypair_t *, |
656 | | get_master_signing_keypair,(void)) |
657 | 0 | { |
658 | 0 | return master_signing_key; |
659 | 0 | } |
660 | | |
661 | | MOCK_IMPL(const struct tor_cert_st *, |
662 | | get_master_signing_key_cert,(void)) |
663 | 0 | { |
664 | 0 | return signing_key_cert; |
665 | 0 | } |
666 | | |
667 | | const ed25519_keypair_t * |
668 | | get_current_auth_keypair(void) |
669 | 0 | { |
670 | 0 | return current_auth_key; |
671 | 0 | } |
672 | | |
673 | | const tor_cert_t * |
674 | | get_current_link_cert_cert(void) |
675 | 0 | { |
676 | 0 | return link_cert_cert; |
677 | 0 | } |
678 | | |
679 | | const tor_cert_t * |
680 | | get_current_auth_key_cert(void) |
681 | 0 | { |
682 | 0 | return auth_key_cert; |
683 | 0 | } |
684 | | |
685 | | /** |
686 | | * Suffix for the filenames in which we expect to find a family ID key. |
687 | | */ |
688 | 0 | #define FAMILY_KEY_SUFFIX ".secret_family_key" |
689 | | |
690 | | /** |
691 | | * Return true if `fname` is a possible filename of a family ID key. |
692 | | * |
693 | | * Family ID key filenames are FAMILY_KEY_FNAME, followed optionally |
694 | | * by "." and a positive integer. |
695 | | */ |
696 | | STATIC bool |
697 | | is_family_key_fname(const char *fname) |
698 | 0 | { |
699 | 0 | return 0 == strcmpend(fname, FAMILY_KEY_SUFFIX); |
700 | 0 | } |
701 | | |
702 | | /** Return true if `id` is configured in `options`. */ |
703 | | static bool |
704 | | family_key_id_is_expected(const or_options_t *options, |
705 | | const ed25519_public_key_t *id) |
706 | 0 | { |
707 | 0 | if (options->AllFamilyIdsExpected) |
708 | 0 | return true; |
709 | | |
710 | 0 | SMARTLIST_FOREACH(options->FamilyIds, const ed25519_public_key_t *, k, { |
711 | 0 | if (ed25519_pubkey_eq(k, id)) |
712 | 0 | return true; |
713 | 0 | }); |
714 | 0 | return false; |
715 | 0 | } |
716 | | |
717 | | /** Return true if the key for `id` has been loaded. */ |
718 | | static bool |
719 | | family_key_is_present(const ed25519_public_key_t *id) |
720 | 0 | { |
721 | 0 | if (!family_id_keys) |
722 | 0 | return false; |
723 | | |
724 | 0 | SMARTLIST_FOREACH(family_id_keys, const ed25519_keypair_t *, kp, { |
725 | 0 | if (ed25519_pubkey_eq(&kp->pubkey, id)) |
726 | 0 | return true; |
727 | 0 | }); |
728 | 0 | return false; |
729 | 0 | } |
730 | | |
731 | | /** |
732 | | * Tag to use on family key files. |
733 | | */ |
734 | 0 | #define FAMILY_KEY_FILE_TAG "fmly-id" |
735 | | |
736 | | /** Return a list of all the possible family-key files in `keydir`. |
737 | | * Return NULL on error. |
738 | | * |
739 | | * (Unlike list_family_key_files, this function does not use a cached |
740 | | * list when the seccomp2 sandbox is enabled.) */ |
741 | | static smartlist_t * |
742 | | list_family_key_files_impl(const char *keydir) |
743 | 0 | { |
744 | 0 | smartlist_t *files = tor_listdir(keydir); |
745 | 0 | smartlist_t *result = smartlist_new(); |
746 | |
|
747 | 0 | if (!files) { |
748 | 0 | log_warn(LD_OR, "Unable to list contents of directory %s", keydir); |
749 | 0 | goto err; |
750 | 0 | } |
751 | | |
752 | 0 | SMARTLIST_FOREACH_BEGIN(files, const char *, fn) { |
753 | 0 | if (!is_family_key_fname(fn)) |
754 | 0 | continue; |
755 | | |
756 | 0 | smartlist_add_asprintf(result, "%s%s%s", keydir, PATH_SEPARATOR, fn); |
757 | 0 | } SMARTLIST_FOREACH_END(fn); |
758 | |
|
759 | 0 | goto done; |
760 | 0 | err: |
761 | 0 | SMARTLIST_FOREACH(result, char *, cp, tor_free(cp)); |
762 | 0 | smartlist_free(result); // sets result to NULL. |
763 | 0 | done: |
764 | 0 | if (files) { |
765 | 0 | SMARTLIST_FOREACH(files, char *, cp, tor_free(cp)); |
766 | 0 | smartlist_free(files); |
767 | 0 | } |
768 | |
|
769 | 0 | return result; |
770 | 0 | } |
771 | | |
772 | | /** |
773 | | * A list of files returned by list_family_key_files_impl. |
774 | | * Used when the seccomp2 sandbox is enabled. |
775 | | */ |
776 | | static smartlist_t *cached_family_key_file_list = NULL; |
777 | | |
778 | | /** Return a list of all the possible family-key files in `keydir`. |
779 | | * Return NULL on error. |
780 | | */ |
781 | | smartlist_t * |
782 | | list_family_key_files(const or_options_t *options, |
783 | | const char *keydir) |
784 | 0 | { |
785 | 0 | if (options->Sandbox) { |
786 | 0 | if (!cached_family_key_file_list) |
787 | 0 | cached_family_key_file_list = list_family_key_files_impl(keydir); |
788 | 0 | if (!cached_family_key_file_list) |
789 | 0 | return NULL; |
790 | | |
791 | 0 | smartlist_t *result = smartlist_new(); |
792 | 0 | SMARTLIST_FOREACH(cached_family_key_file_list, char *, fn, |
793 | 0 | smartlist_add_strdup(result, fn)); |
794 | 0 | return result; |
795 | 0 | } |
796 | | |
797 | 0 | return list_family_key_files_impl(keydir); |
798 | 0 | } |
799 | | |
800 | | /** |
801 | | * Look for all the family keys in `keydir`, load them into |
802 | | * family_id_keys. |
803 | | */ |
804 | | STATIC int |
805 | | load_family_id_keys_impl(const or_options_t *options, |
806 | | const char *keydir) |
807 | 0 | { |
808 | 0 | if (BUG(!options) || BUG(!keydir)) |
809 | 0 | return -1; |
810 | | |
811 | 0 | smartlist_t *key_files = list_family_key_files(options, keydir); |
812 | 0 | smartlist_t *new_keys = NULL; |
813 | 0 | ed25519_keypair_t *kp_tmp = NULL; |
814 | 0 | char *tag_tmp = NULL; |
815 | 0 | int r = -1; |
816 | |
|
817 | 0 | if (key_files == NULL) { |
818 | 0 | goto end; // already warned. |
819 | 0 | } |
820 | | |
821 | 0 | new_keys = smartlist_new(); |
822 | 0 | SMARTLIST_FOREACH_BEGIN(key_files, const char *, fn) { |
823 | 0 | kp_tmp = tor_malloc_zero(sizeof(*kp_tmp)); |
824 | | // TODO: If we ever allow cert provisioning here, |
825 | | // use ed_key_init_from_file() instead. |
826 | 0 | if (ed25519_seckey_read_from_file(&kp_tmp->seckey, &tag_tmp, fn) < 0) { |
827 | 0 | log_warn(LD_OR, "%s was not an ed25519 secret key.", fn); |
828 | 0 | goto end; |
829 | 0 | } |
830 | 0 | if (0 != strcmp(tag_tmp, FAMILY_KEY_FILE_TAG)) { |
831 | 0 | log_warn(LD_OR, "%s was not a family ID key.", fn); |
832 | 0 | goto end; |
833 | 0 | } |
834 | 0 | if (ed25519_public_key_generate(&kp_tmp->pubkey, &kp_tmp->seckey) < 0) { |
835 | 0 | log_warn(LD_OR, "Unable to generate public key for %s", fn); |
836 | 0 | goto end; |
837 | 0 | } |
838 | | |
839 | 0 | if (family_key_id_is_expected(options, &kp_tmp->pubkey)) { |
840 | 0 | smartlist_add(new_keys, kp_tmp); |
841 | 0 | kp_tmp = NULL; // prevent double-free |
842 | 0 | } else { |
843 | 0 | log_warn(LD_OR, "Found secret family key in %s " |
844 | 0 | "with unexpected FamilyID %s", |
845 | 0 | fn, ed25519_fmt(&kp_tmp->pubkey)); |
846 | 0 | } |
847 | |
|
848 | 0 | ed25519_keypair_free(kp_tmp); |
849 | 0 | tor_free(tag_tmp); |
850 | 0 | } SMARTLIST_FOREACH_END(fn); |
851 | |
|
852 | 0 | set_family_id_keys(new_keys); |
853 | 0 | new_keys = NULL; // prevent double-free |
854 | 0 | r = 0; |
855 | 0 | end: |
856 | 0 | if (key_files) { |
857 | 0 | SMARTLIST_FOREACH(key_files, char *, cp, tor_free(cp)); |
858 | 0 | smartlist_free(key_files); |
859 | 0 | } |
860 | 0 | if (new_keys) { |
861 | 0 | SMARTLIST_FOREACH(new_keys, ed25519_keypair_t *, kp, |
862 | 0 | ed25519_keypair_free(kp)); |
863 | 0 | smartlist_free(new_keys); |
864 | 0 | } |
865 | 0 | tor_free(tag_tmp); |
866 | 0 | ed25519_keypair_free(kp_tmp); |
867 | 0 | return r; |
868 | 0 | } |
869 | | |
870 | | /** |
871 | | * Create a new family ID key, and store it in `fname`. |
872 | | * |
873 | | * If pk_out is provided, set it to the generated public key. |
874 | | **/ |
875 | | int |
876 | | create_family_id_key(const char *fname, ed25519_public_key_t *pk_out) |
877 | 0 | { |
878 | 0 | int r = -1; |
879 | 0 | ed25519_keypair_t *kp = tor_malloc_zero(sizeof(ed25519_keypair_t)); |
880 | 0 | if (ed25519_keypair_generate(kp, 1) < 0) { |
881 | 0 | log_warn(LD_BUG, "Can't generate ed25519 key!"); |
882 | 0 | goto done; |
883 | 0 | } |
884 | | |
885 | 0 | if (ed25519_seckey_write_to_file(&kp->seckey, |
886 | 0 | fname, FAMILY_KEY_FILE_TAG)<0) { |
887 | 0 | log_warn(LD_BUG, "Can't write key to file."); |
888 | 0 | goto done; |
889 | 0 | } |
890 | | |
891 | 0 | if (pk_out) { |
892 | 0 | ed25519_pubkey_copy(pk_out, &kp->pubkey); |
893 | 0 | } |
894 | |
|
895 | 0 | r = 0; |
896 | |
|
897 | 0 | done: |
898 | 0 | ed25519_keypair_free(kp); |
899 | 0 | return r; |
900 | 0 | } |
901 | | |
902 | | /** |
903 | | * If configured to do so, load our family keys from the key directory. |
904 | | * Otherwise, clear the family keys. |
905 | | * |
906 | | * Additionally, warn about inconsistencies between family options. |
907 | | * If `ns` is provided, provide additional warnings. |
908 | | * |
909 | | * `options` is required; `ns` may be NULL. |
910 | | */ |
911 | | int |
912 | | load_family_id_keys(const or_options_t *options, |
913 | | const networkstatus_t *ns) |
914 | 0 | { |
915 | 0 | if (options->FamilyIds) { |
916 | 0 | if (load_family_id_keys_impl(options, options->FamilyKeyDirectory) < 0) |
917 | 0 | return -1; |
918 | | |
919 | 0 | bool any_missing = false; |
920 | 0 | SMARTLIST_FOREACH_BEGIN(options->FamilyIds, |
921 | 0 | const ed25519_public_key_t *, id) { |
922 | 0 | if (!family_key_is_present(id)) { |
923 | 0 | log_err(LD_OR, "No key was found for listed FamilyID %s", |
924 | 0 | ed25519_fmt(id)); |
925 | 0 | any_missing = true; |
926 | 0 | } |
927 | 0 | } SMARTLIST_FOREACH_END(id); |
928 | 0 | if (any_missing) |
929 | 0 | return -1; |
930 | | |
931 | 0 | log_info(LD_OR, "Found %d family ID keys", |
932 | 0 | smartlist_len(get_current_family_id_keys())); |
933 | 0 | } else { |
934 | 0 | set_family_id_keys(NULL); |
935 | 0 | } |
936 | 0 | warn_about_family_id_config(options, ns); |
937 | 0 | return 0; |
938 | 0 | } |
939 | | |
940 | | #define FAMILY_INFO_URL \ |
941 | | "https://community.torproject.org/relay/setup/post-install/family-ids/" |
942 | | |
943 | | /** Generate warnings as appropriate about our family ID configuration. |
944 | | * |
945 | | * `options` is required; `ns` may be NULL. |
946 | | */ |
947 | | void |
948 | | warn_about_family_id_config(const or_options_t *options, |
949 | | const networkstatus_t *ns) |
950 | 0 | { |
951 | 0 | static int have_warned_absent_myfamily = 0; |
952 | 0 | static int have_warned_absent_familykeys = 0; |
953 | |
|
954 | 0 | if (options->FamilyIds) { |
955 | 0 | if (!have_warned_absent_myfamily && |
956 | 0 | !options->MyFamily && ns && should_publish_family_list(ns)) { |
957 | 0 | log_warn(LD_OR, |
958 | 0 | "FamilyId was configured, but MyFamily was not. " |
959 | 0 | "FamilyId is good, but the Tor network still requires " |
960 | 0 | "MyFamily while clients are migrating to use family " |
961 | 0 | "keys instead."); |
962 | 0 | have_warned_absent_myfamily = 1; |
963 | 0 | } |
964 | 0 | } else { |
965 | 0 | if (!have_warned_absent_familykeys && |
966 | 0 | options->MyFamily && |
967 | 0 | ns && ns->consensus_method >= MIN_METHOD_FOR_FAMILY_IDS) { |
968 | 0 | log_notice(LD_OR, |
969 | 0 | "MyFamily was configured, but FamilyId was not. " |
970 | 0 | "It's a good time to start migrating your relays " |
971 | 0 | "to use family keys. " |
972 | 0 | "See "FAMILY_INFO_URL " for instructions."); |
973 | 0 | have_warned_absent_familykeys = 1; |
974 | 0 | } |
975 | 0 | } |
976 | 0 | } |
977 | | |
978 | | /** |
979 | | * Return a list of our current family id keypairs, |
980 | | * as a list of `ed25519_keypair_t`. |
981 | | * |
982 | | * Never returns NULL. |
983 | | * |
984 | | * TODO PROP321: Right now this is only used in testing; |
985 | | * when we add relay support we'll need a way to actually |
986 | | * read these keys from disk. |
987 | | **/ |
988 | | const smartlist_t * |
989 | | get_current_family_id_keys(void) |
990 | 0 | { |
991 | 0 | if (family_id_keys == NULL) |
992 | 0 | family_id_keys = smartlist_new(); |
993 | 0 | return family_id_keys; |
994 | 0 | } |
995 | | |
996 | | /** |
997 | | * Replace our list of family ID keys with `family_id_keys`, |
998 | | * which must be a list of `ed25519_keypair_t`. |
999 | | * |
1000 | | * Takes ownership of its input. |
1001 | | */ |
1002 | | STATIC void |
1003 | | set_family_id_keys(smartlist_t *keys) |
1004 | 0 | { |
1005 | 0 | if (family_id_keys) { |
1006 | 0 | SMARTLIST_FOREACH(family_id_keys, ed25519_keypair_t *, kp, |
1007 | 0 | ed25519_keypair_free(kp)); |
1008 | 0 | smartlist_free(family_id_keys); |
1009 | 0 | } |
1010 | 0 | family_id_keys = keys; |
1011 | 0 | } |
1012 | | |
1013 | | void |
1014 | | get_master_rsa_crosscert(const uint8_t **cert_out, |
1015 | | size_t *size_out) |
1016 | 0 | { |
1017 | 0 | *cert_out = rsa_ed_crosscert; |
1018 | 0 | *size_out = rsa_ed_crosscert_len; |
1019 | 0 | } |
1020 | | |
1021 | | /** Construct cross-certification for the master identity key with |
1022 | | * the ntor onion key. Store the sign of the corresponding ed25519 public key |
1023 | | * in *<b>sign_out</b>. */ |
1024 | | tor_cert_t * |
1025 | | make_ntor_onion_key_crosscert(const curve25519_keypair_t *onion_key, |
1026 | | const ed25519_public_key_t *master_id_key, time_t now, time_t lifetime, |
1027 | | int *sign_out) |
1028 | 0 | { |
1029 | 0 | tor_cert_t *cert = NULL; |
1030 | 0 | ed25519_keypair_t ed_onion_key; |
1031 | |
|
1032 | 0 | if (ed25519_keypair_from_curve25519_keypair(&ed_onion_key, sign_out, |
1033 | 0 | onion_key) < 0) |
1034 | 0 | goto end; |
1035 | | |
1036 | 0 | cert = tor_cert_create_ed25519(&ed_onion_key, CERT_TYPE_ONION_ID, |
1037 | 0 | master_id_key, now, lifetime, 0); |
1038 | |
|
1039 | 0 | end: |
1040 | 0 | memwipe(&ed_onion_key, 0, sizeof(ed_onion_key)); |
1041 | 0 | return cert; |
1042 | 0 | } |
1043 | | |
1044 | | /** Construct and return an RSA signature for the TAP onion key to |
1045 | | * cross-certify the RSA and Ed25519 identity keys. Set <b>len_out</b> to its |
1046 | | * length. */ |
1047 | | uint8_t * |
1048 | | make_tap_onion_key_crosscert(const crypto_pk_t *onion_key, |
1049 | | const ed25519_public_key_t *master_id_key, |
1050 | | const crypto_pk_t *rsa_id_key, |
1051 | | int *len_out) |
1052 | 0 | { |
1053 | 0 | uint8_t signature[PK_BYTES]; |
1054 | 0 | uint8_t signed_data[DIGEST_LEN + ED25519_PUBKEY_LEN]; |
1055 | |
|
1056 | 0 | *len_out = 0; |
1057 | 0 | if (crypto_pk_get_digest(rsa_id_key, (char*)signed_data) < 0) { |
1058 | 0 | log_info(LD_OR, "crypto_pk_get_digest failed in " |
1059 | 0 | "make_tap_onion_key_crosscert!"); |
1060 | 0 | return NULL; |
1061 | 0 | } |
1062 | 0 | memcpy(signed_data + DIGEST_LEN, master_id_key->pubkey, ED25519_PUBKEY_LEN); |
1063 | |
|
1064 | 0 | int r = crypto_pk_private_sign(onion_key, |
1065 | 0 | (char*)signature, sizeof(signature), |
1066 | 0 | (const char*)signed_data, sizeof(signed_data)); |
1067 | 0 | if (r < 0) { |
1068 | | /* It's probably missing the private key */ |
1069 | 0 | log_info(LD_OR, "crypto_pk_private_sign failed in " |
1070 | 0 | "make_tap_onion_key_crosscert!"); |
1071 | 0 | return NULL; |
1072 | 0 | } |
1073 | | |
1074 | 0 | *len_out = r; |
1075 | |
|
1076 | 0 | return tor_memdup(signature, r); |
1077 | 0 | } |
1078 | | |
1079 | | void |
1080 | | routerkeys_free_all(void) |
1081 | 0 | { |
1082 | 0 | ed25519_keypair_free(master_identity_key); |
1083 | 0 | ed25519_keypair_free(master_signing_key); |
1084 | 0 | ed25519_keypair_free(current_auth_key); |
1085 | 0 | set_family_id_keys(NULL); |
1086 | |
|
1087 | 0 | tor_cert_free(signing_key_cert); |
1088 | 0 | tor_cert_free(link_cert_cert); |
1089 | 0 | tor_cert_free(auth_key_cert); |
1090 | 0 | tor_free(rsa_ed_crosscert); |
1091 | |
|
1092 | 0 | if (cached_family_key_file_list) { |
1093 | 0 | SMARTLIST_FOREACH(cached_family_key_file_list, char *, cp, tor_free(cp)); |
1094 | 0 | smartlist_free(cached_family_key_file_list); |
1095 | 0 | } |
1096 | |
|
1097 | 0 | master_identity_key = master_signing_key = NULL; |
1098 | 0 | current_auth_key = NULL; |
1099 | 0 | signing_key_cert = link_cert_cert = auth_key_cert = NULL; |
1100 | | rsa_ed_crosscert = NULL; // redundant |
1101 | 0 | rsa_ed_crosscert_len = 0; |
1102 | 0 | } |