1 /* Copyright (c) 2014, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
7 #include "routerkeys.h"
11 * Read an ed25519 key and associated certificates from files beginning with
12 * <b>fname</b>, with certificate type <b>cert_type</b>. On failure, return
13 * NULL; on success return the keypair.
15 * If INIT_ED_KEY_CREATE is set in <b>flags</b>, then create the key (and
16 * certificate if requested) if it doesn't exist, and save it to disk.
18 * If INIT_ED_KEY_NEEDCERT is set in <b>flags</b>, load/create a certificate
19 * too and store it in *<b>cert_out</b>. Fail if the cert can't be
20 * found/created. To create a certificate, <b>signing_key</b> must be set to
21 * the key that should sign it; <b>now</b> to the current time, and
22 * <b>lifetime</b> to the lifetime of the key.
24 * If INIT_ED_KEY_REPLACE is set in <b>flags</b>, then create and save new key
25 * whether we can read the old one or not.
27 * If INIT_ED_KEY_EXTRA_STRONG is set in <b>flags</b>, set the extra_strong
28 * flag when creating the secret key.
30 * If INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT is set in <b>flags</b>, and
31 * we create a new certificate, create it with the signing key embedded.
33 * If INIT_ED_KEY_SPLIT is set in <b>flags</b>, and we create a new key,
34 * store the public key in a separate file from the secret key.
36 * If INIT_ED_KEY_MISSING_SECRET_OK is set in <b>flags</b>, and we find a
37 * public key file but no secret key file, return successfully anyway.
39 * If INIT_ED_KEY_OMIT_SECRET is set in <b>flags</b>, do not even try to
40 * load or return a secret key (but create and save on if needed).
43 ed_key_init_from_file(const char *fname
, uint32_t flags
,
45 const ed25519_keypair_t
*signing_key
,
49 struct tor_cert_st
**cert_out
)
51 char *secret_fname
= NULL
;
52 char *public_fname
= NULL
;
53 char *cert_fname
= NULL
;
54 int created_pk
= 0, created_sk
= 0, created_cert
= 0;
55 const int try_to_load
= ! (flags
& INIT_ED_KEY_REPLACE
);
58 tor_snprintf(tag
, sizeof(tag
), "type%d", (int)cert_type
);
60 tor_cert_t
*cert
= NULL
;
62 ed25519_keypair_t
*keypair
= tor_malloc_zero(sizeof(ed25519_keypair_t
));
64 tor_asprintf(&secret_fname
, "%s_secret_key", fname
);
65 tor_asprintf(&public_fname
, "%s_public_key", fname
);
66 tor_asprintf(&cert_fname
, "%s_cert", fname
);
68 /* Try to read the secret key. */
69 const int have_secret
= try_to_load
&&
70 !(flags
& INIT_ED_KEY_OMIT_SECRET
) &&
71 ed25519_seckey_read_from_file(&keypair
->seckey
,
72 &got_tag
, secret_fname
) == 0;
75 if (strcmp(got_tag
, tag
)) {
76 tor_log(severity
, LD_OR
, "%s has wrong tag", secret_fname
);
79 /* Derive the public key */
80 if (ed25519_public_key_generate(&keypair
->pubkey
, &keypair
->seckey
)<0) {
81 tor_log(severity
, LD_OR
, "%s can't produce a public key", secret_fname
);
86 /* If it's absent and that's okay, try to read the pubkey. */
88 if (!have_secret
&& try_to_load
) {
90 found_public
= ed25519_pubkey_read_from_file(&keypair
->pubkey
,
91 &got_tag
, public_fname
) == 0;
92 if (found_public
&& strcmp(got_tag
, tag
)) {
93 tor_log(severity
, LD_OR
, "%s has wrong tag", public_fname
);
98 /* If the secret key is absent and it's not allowed to be, fail. */
99 if (!have_secret
&& found_public
&& !(flags
& INIT_ED_KEY_MISSING_SECRET_OK
))
102 /* If it's absent, and we're not supposed to make a new keypair, fail. */
103 if (!have_secret
&& !found_public
&& !(flags
& INIT_ED_KEY_CREATE
))
106 /* if it's absent, make a new keypair and save it. */
107 if (!have_secret
&& !found_public
) {
108 const int split
= !! (flags
& INIT_ED_KEY_SPLIT
);
110 keypair
= ed_key_new(signing_key
, flags
, now
, lifetime
,
113 tor_log(severity
, LD_OR
, "Couldn't create keypair");
117 created_pk
= created_sk
= created_cert
= 1;
118 if (ed25519_seckey_write_to_file(&keypair
->seckey
, secret_fname
, tag
) < 0
121 ed25519_pubkey_write_to_file(&keypair
->pubkey
, public_fname
, tag
) < 0)
124 crypto_write_tagged_contents_to_file(cert_fname
, "ed25519v1-cert",
125 tag
, cert
->encoded
, cert
->encoded_len
) < 0)) {
126 tor_log(severity
, LD_OR
, "Couldn't write keys or cert to file.");
132 /* If we're not supposed to get a cert, we're done. */
133 if (! (flags
& INIT_ED_KEY_NEEDCERT
))
138 uint8_t certbuf
[256];
139 ssize_t cert_body_len
= crypto_read_tagged_contents_from_file(
140 cert_fname
, "ed25519v1-cert",
141 &got_tag
, certbuf
, sizeof(certbuf
));
142 if (cert_body_len
>= 0 && !strcmp(got_tag
, tag
))
143 cert
= tor_cert_parse(certbuf
, cert_body_len
);
145 /* If we got it, check it to the extent we can. */
149 tor_log(severity
, LD_OR
, "Cert was unparseable");
151 } else if (!tor_memeq(cert
->signed_key
.pubkey
, keypair
->pubkey
.pubkey
,
152 ED25519_PUBKEY_LEN
)) {
153 tor_log(severity
, LD_OR
, "Cert was for wrong key");
155 } else if (signing_key
&&
156 tor_cert_checksig(cert
, &signing_key
->pubkey
, now
) < 0 &&
157 (signing_key
|| cert
->cert_expired
)) {
158 tor_log(severity
, LD_OR
, "Can't check certificate");
167 /* If we got a cert, we're done. */
171 /* If we didn't get a cert, and we're not supposed to make one, fail. */
172 if (!signing_key
|| !(flags
& INIT_ED_KEY_CREATE
))
175 /* We have keys but not a certificate, so make one. */
176 uint32_t cert_flags
= 0;
177 if (flags
& INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT
)
178 cert_flags
|= CERT_FLAG_INCLUDE_SIGNING_KEY
;
179 cert
= tor_cert_create(signing_key
, cert_type
,
187 /* Write it to disk. */
189 if (crypto_write_tagged_contents_to_file(cert_fname
, "ed25519v1-cert",
190 tag
, cert
->encoded
, cert
->encoded_len
) < 0) {
191 tor_log(severity
, LD_OR
, "Couldn't write cert to disk.");
205 memwipe(keypair
, 0, sizeof(*keypair
));
211 unlink(secret_fname
);
213 unlink(public_fname
);
218 tor_free(secret_fname
);
219 tor_free(public_fname
);
220 tor_free(cert_fname
);
227 * Create a new signing key and (optionally) certficiate; do not read or write
228 * from disk. See ed_key_init_from_file() for more information.
231 ed_key_new(const ed25519_keypair_t
*signing_key
,
236 struct tor_cert_st
**cert_out
)
241 const int extra_strong
= !! (flags
& INIT_ED_KEY_EXTRA_STRONG
);
242 ed25519_keypair_t
*keypair
= tor_malloc_zero(sizeof(ed25519_keypair_t
));
243 if (ed25519_keypair_generate(keypair
, extra_strong
) < 0)
246 if (! (flags
& INIT_ED_KEY_NEEDCERT
))
249 tor_assert(signing_key
);
250 tor_assert(cert_out
);
251 uint32_t cert_flags
= 0;
252 if (flags
& INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT
)
253 cert_flags
|= CERT_FLAG_INCLUDE_SIGNING_KEY
;
254 tor_cert_t
*cert
= tor_cert_create(signing_key
, cert_type
,
269 static ed25519_keypair_t
*master_identity_key
= NULL
;
270 static ed25519_keypair_t
*master_signing_key
= NULL
;
271 static ed25519_keypair_t
*current_auth_key
= NULL
;
272 static tor_cert_t
*signing_key_cert
= NULL
;
273 static tor_cert_t
*link_cert_cert
= NULL
;
274 static tor_cert_t
*auth_key_cert
= NULL
;
276 static uint8_t *rsa_ed_crosscert
= NULL
;
277 static size_t rsa_ed_crosscert_len
= 0;
280 * Running as a server: load, reload, or refresh our ed25519 keys and
281 * certificates, creating and saving new ones as needed.
284 load_ed_keys(const or_options_t
*options
, time_t now
)
286 ed25519_keypair_t
*id
= NULL
;
287 ed25519_keypair_t
*sign
= NULL
;
288 ed25519_keypair_t
*auth
= NULL
;
289 const ed25519_keypair_t
*sign_signing_key_with_id
= NULL
;
290 const ed25519_keypair_t
*use_signing
= NULL
;
291 const tor_cert_t
*check_signing_cert
= NULL
;
292 tor_cert_t
*sign_cert
= NULL
;
293 tor_cert_t
*auth_cert
= NULL
;
295 #define FAIL(msg) do { \
296 log_warn(LD_OR, (msg)); \
299 #define SET_KEY(key, newval) do { \
300 ed25519_keypair_free(key); \
303 #define SET_CERT(cert, newval) do { \
304 tor_cert_free(cert); \
307 #define EXPIRES_SOON(cert, interval) \
308 (!(cert) || (cert)->valid_until < now + (interval))
310 /* XXXX support encrypted identity keys fully */
312 /* First try to get the signing key to see how it is. */
313 if (master_signing_key
) {
314 check_signing_cert
= signing_key_cert
;
315 use_signing
= master_signing_key
;
318 options_get_datadir_fname2(options
, "keys", "ed25519_signing");
319 sign
= ed_key_init_from_file(
321 INIT_ED_KEY_NEEDCERT
|
322 INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT
,
324 NULL
, 0, 0, CERT_TYPE_ID_SIGNING
, &sign_cert
);
326 check_signing_cert
= sign_cert
;
330 const int need_new_signing_key
=
331 NULL
== use_signing
||
332 EXPIRES_SOON(check_signing_cert
, 0);
333 const int want_new_signing_key
=
334 need_new_signing_key
||
335 EXPIRES_SOON(check_signing_cert
, options
->TestingSigningKeySlop
);
339 (INIT_ED_KEY_CREATE
|INIT_ED_KEY_SPLIT
|
340 INIT_ED_KEY_EXTRA_STRONG
);
341 if (! need_new_signing_key
)
342 flags
|= INIT_ED_KEY_MISSING_SECRET_OK
;
343 if (! want_new_signing_key
)
344 flags
|= INIT_ED_KEY_OMIT_SECRET
;
347 options_get_datadir_fname2(options
, "keys", "ed25519_master_id");
348 id
= ed_key_init_from_file(
351 LOG_WARN
, NULL
, 0, 0, 0, NULL
);
354 FAIL("Missing identity key");
355 if (tor_mem_is_zero((char*)id
->seckey
.seckey
, sizeof(id
->seckey
)))
356 sign_signing_key_with_id
= NULL
;
358 sign_signing_key_with_id
= id
;
361 if (need_new_signing_key
&& NULL
== sign_signing_key_with_id
)
362 FAIL("Can't load master key make a new signing key.");
364 if (want_new_signing_key
&& sign_signing_key_with_id
) {
365 uint32_t flags
= (INIT_ED_KEY_CREATE
|
367 INIT_ED_KEY_EXTRA_STRONG
|
368 INIT_ED_KEY_NEEDCERT
|
369 INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT
);
371 options_get_datadir_fname2(options
, "keys", "ed25519_signing");
372 sign
= ed_key_init_from_file(fname
,
374 sign_signing_key_with_id
, now
,
375 options
->SigningKeyLifetime
,
376 CERT_TYPE_ID_SIGNING
, &sign_cert
);
379 FAIL("Missing signing key");
381 } else if (want_new_signing_key
) {
382 static ratelim_t missing_master
= RATELIM_INIT(3600);
383 log_fn_ratelim(&missing_master
, LOG_WARN
, LD_OR
,
384 "Signing key will expire soon, but I can't load the "
385 "master key to sign a new one!");
388 tor_assert(use_signing
);
390 /* At this point we no longer need our secret identity key. So wipe
391 * it, if we loaded it in the first place. */
392 memwipe(id
->seckey
.seckey
, 0, sizeof(id
->seckey
));
394 if (!rsa_ed_crosscert
&& server_mode(options
)) {
396 ssize_t crosscert_len
= tor_make_rsa_ed25519_crosscert(&id
->pubkey
,
397 get_server_identity_key(),
398 now
+10*365*86400,/*XXXX*/
400 rsa_ed_crosscert_len
= crosscert_len
;
401 rsa_ed_crosscert
= crosscert
;
404 if (!current_auth_key
||
405 EXPIRES_SOON(auth_key_cert
, options
->TestingAuthKeySlop
)) {
406 auth
= ed_key_new(use_signing
, INIT_ED_KEY_NEEDCERT
,
408 options
->TestingAuthKeyLifetime
,
409 CERT_TYPE_SIGNING_AUTH
, &auth_cert
);
412 FAIL("Can't create auth key");
415 /* We've generated or loaded everything. Put them in memory. */
417 if (! master_identity_key
) {
418 SET_KEY(master_identity_key
, id
);
423 SET_KEY(master_signing_key
, sign
);
424 SET_CERT(signing_key_cert
, sign_cert
);
427 SET_KEY(current_auth_key
, auth
);
428 SET_CERT(auth_key_cert
, auth_cert
);
433 ed25519_keypair_free(id
);
434 ed25519_keypair_free(sign
);
435 ed25519_keypair_free(auth
);
436 tor_cert_free(sign_cert
);
437 tor_cert_free(auth_cert
);
443 generate_ed_link_cert(const or_options_t
*options
, time_t now
)
445 const tor_x509_cert_t
*link
= NULL
, *id
= NULL
;
446 tor_cert_t
*link_cert
= NULL
;
448 if (tor_tls_get_my_certs(1, &link
, &id
) < 0 || link
== NULL
) {
449 log_warn(LD_OR
, "Can't get my x509 link cert.");
453 const digests_t
*digests
= tor_x509_cert_get_cert_digests(link
);
455 if (link_cert_cert
&&
456 ! EXPIRES_SOON(link_cert_cert
, options
->TestingLinkKeySlop
) &&
457 fast_memeq(digests
->d
[DIGEST_SHA256
], link_cert_cert
->signed_key
.pubkey
,
462 ed25519_public_key_t dummy_key
;
463 memcpy(dummy_key
.pubkey
, digests
->d
[DIGEST_SHA256
], DIGEST256_LEN
);
465 link_cert
= tor_cert_create(get_master_signing_keypair(),
466 CERT_TYPE_SIGNING_LINK
,
469 options
->TestingLinkCertLifetime
, 0);
472 SET_CERT(link_cert_cert
, link_cert
);
482 should_make_new_ed_keys(const or_options_t
*options
, const time_t now
)
484 if (!master_identity_key
||
485 !master_signing_key
||
488 EXPIRES_SOON(signing_key_cert
, options
->TestingSigningKeySlop
) ||
489 EXPIRES_SOON(auth_key_cert
, options
->TestingAuthKeySlop
) ||
490 EXPIRES_SOON(link_cert_cert
, options
->TestingLinkKeySlop
))
493 const tor_x509_cert_t
*link
= NULL
, *id
= NULL
;
495 if (tor_tls_get_my_certs(1, &link
, &id
) < 0 || link
== NULL
)
498 const digests_t
*digests
= tor_x509_cert_get_cert_digests(link
);
500 if (!fast_memeq(digests
->d
[DIGEST_SHA256
],
501 link_cert_cert
->signed_key
.pubkey
,
511 const ed25519_public_key_t
*
512 get_master_identity_key(void)
514 if (!master_identity_key
)
516 return &master_identity_key
->pubkey
;
519 const ed25519_keypair_t
*
520 get_master_signing_keypair(void)
522 return master_signing_key
;
525 const struct tor_cert_st
*
526 get_master_signing_key_cert(void)
528 return signing_key_cert
;
531 const ed25519_keypair_t
*
532 get_current_auth_keypair(void)
534 return current_auth_key
;
538 get_current_link_cert_cert(void)
540 return link_cert_cert
;
544 get_current_auth_key_cert(void)
546 return auth_key_cert
;
550 get_master_rsa_crosscert(const uint8_t **cert_out
,
553 *cert_out
= rsa_ed_crosscert
;
554 *size_out
= rsa_ed_crosscert_len
;
557 /** Construct cross-certification for the master identity key with
558 * the ntor onion key. Store the sign of the corresponding ed25519 public key
559 * in *<b>sign_out</b>. */
561 make_ntor_onion_key_crosscert(const curve25519_keypair_t
*onion_key
,
562 const ed25519_public_key_t
*master_id_key
, time_t now
, time_t lifetime
,
565 tor_cert_t
*cert
= NULL
;
566 ed25519_keypair_t ed_onion_key
;
568 if (ed25519_keypair_from_curve25519_keypair(&ed_onion_key
, sign_out
,
572 cert
= tor_cert_create(&ed_onion_key
, CERT_TYPE_ONION_ID
, master_id_key
,
576 memwipe(&ed_onion_key
, 0, sizeof(ed_onion_key
));
580 /** Construct and return an RSA signature for the TAP onion key to
581 * cross-certify the RSA and Ed25519 identity keys. Set <b>len_out</b> to its
584 make_tap_onion_key_crosscert(const crypto_pk_t
*onion_key
,
585 const ed25519_public_key_t
*master_id_key
,
586 const crypto_pk_t
*rsa_id_key
,
589 uint8_t signature
[PK_BYTES
];
590 uint8_t signed_data
[DIGEST_LEN
+ ED25519_PUBKEY_LEN
];
593 crypto_pk_get_digest(rsa_id_key
, (char*)signed_data
);
594 memcpy(signed_data
+ DIGEST_LEN
, master_id_key
->pubkey
, ED25519_PUBKEY_LEN
);
596 int r
= crypto_pk_private_sign(onion_key
,
597 (char*)signature
, sizeof(signature
),
598 (const char*)signed_data
, sizeof(signed_data
));
604 return tor_memdup(signature
, r
);
607 /** Check whether an RSA-TAP cross-certification is correct. Return 0 if it
608 * is, -1 if it isn't. */
610 check_tap_onion_key_crosscert(const uint8_t *crosscert
,
612 const crypto_pk_t
*onion_pkey
,
613 const ed25519_public_key_t
*master_id_pkey
,
614 const uint8_t *rsa_id_digest
)
616 uint8_t *cc
= tor_malloc(crypto_pk_keysize(onion_pkey
));
618 crypto_pk_public_checksig(onion_pkey
,
620 crypto_pk_keysize(onion_pkey
),
621 (const char*)crosscert
,
626 if (cc_len
< DIGEST_LEN
+ ED25519_PUBKEY_LEN
) {
627 log_warn(LD_DIR
, "Short signature on cross-certification with TAP key");
630 if (tor_memneq(cc
, rsa_id_digest
, DIGEST_LEN
) ||
631 tor_memneq(cc
+ DIGEST_LEN
, master_id_pkey
->pubkey
,
632 ED25519_PUBKEY_LEN
)) {
633 log_warn(LD_DIR
, "Incorrect cross-certification with TAP key");
645 routerkeys_free_all(void)
647 ed25519_keypair_free(master_identity_key
);
648 ed25519_keypair_free(master_signing_key
);
649 ed25519_keypair_free(current_auth_key
);
650 tor_cert_free(signing_key_cert
);
651 tor_cert_free(link_cert_cert
);
652 tor_cert_free(auth_key_cert
);
654 master_identity_key
= master_signing_key
= NULL
;
655 current_auth_key
= NULL
;
656 signing_key_cert
= link_cert_cert
= auth_key_cert
= NULL
;