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-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Read keys from disk, creating as needed
11 * This code is shared by relays and onion services, which both need
15 #include "core/or/or.h"
16 #include "app/config/config.h"
17 #include "app/main/main.h"
18 #include "feature/keymgt/loadkey.h"
19 #include "feature/nodelist/torcert.h"
21 #include "lib/crypt_ops/crypto_pwbox.h"
22 #include "lib/crypt_ops/crypto_util.h"
23 #include "lib/term/getpass.h"
24 #include "lib/crypt_ops/crypto_format.h"
26 #define ENC_KEY_HEADER "Boxed Ed25519 key"
27 #define ENC_KEY_TAG "master"
33 /** Try to read an RSA key from <b>fname</b>. If <b>fname</b> doesn't exist
34 * and <b>generate</b> is true, create a new RSA key and save it in
35 * <b>fname</b>. Return the read/created key, or NULL on error. Log all
36 * errors at level <b>severity</b>. If <b>created_out</b> is non-NULL and a
37 * new key was created, set *<b>created_out</b> to true.
40 init_key_from_file(const char *fname
, int generate
, int severity
,
43 crypto_pk_t
*prkey
= NULL
;
49 if (!(prkey
= crypto_pk_new())) {
50 tor_log(severity
, LD_GENERAL
,"Error constructing key");
54 switch (file_status(fname
)) {
57 tor_log(severity
, LD_FS
,"Can't read key from \"%s\"", fname
);
59 /* treat empty key files as if the file doesn't exist, and,
60 * if generate is set, replace the empty file in
61 * crypto_pk_write_private_key_to_filename() */
65 if (!have_lockfile()) {
66 if (try_locking(get_options(), 0)<0) {
67 /* Make sure that --list-fingerprint only creates new keys
68 * if there is no possibility for a deadlock. */
69 tor_log(severity
, LD_FS
, "Another Tor process has locked \"%s\". "
70 "Not writing any new keys.", fname
);
71 /*XXXX The 'other process' might make a key in a second or two;
72 * maybe we should wait for it. */
76 log_info(LD_GENERAL
, "No key found in \"%s\"; generating fresh key.",
78 if (crypto_pk_generate_key(prkey
)) {
79 tor_log(severity
, LD_GENERAL
,"Error generating onion key");
82 if (! crypto_pk_is_valid_private_key(prkey
)) {
83 tor_log(severity
, LD_GENERAL
,"Generated key seems invalid");
86 log_info(LD_GENERAL
, "Generated key seems valid");
90 if (crypto_pk_write_private_key_to_filename(prkey
, fname
)) {
91 tor_log(severity
, LD_FS
,
92 "Couldn't write generated key to \"%s\".", fname
);
96 tor_log(severity
, LD_GENERAL
, "No key found in \"%s\"", fname
);
101 if (crypto_pk_read_private_key_from_filename(prkey
, fname
)) {
102 tor_log(severity
, LD_GENERAL
,"Error loading private key.");
112 crypto_pk_free(prkey
);
118 do_getpass(const char *prompt
, char *buf
, size_t buflen
,
119 int twice
, const or_options_t
*options
)
121 if (options
->keygen_force_passphrase
== FORCE_PASSPHRASE_OFF
) {
127 char *prompt2
= NULL
;
132 if (options
->use_keygen_passphrase_fd
) {
134 fd
= options
->keygen_passphrase_fd
;
135 length
= read_all_from_fd(fd
, buf
, buflen
-1);
142 const char msg
[] = "One more time:";
143 size_t p2len
= strlen(prompt
) + 1;
144 if (p2len
< sizeof(msg
))
146 prompt2
= tor_malloc(p2len
);
147 memset(prompt2
, ' ', p2len
);
148 memcpy(prompt2
+ p2len
- sizeof(msg
), msg
, sizeof(msg
));
150 buf2
= tor_malloc_zero(buflen
);
154 length
= tor_getpass(prompt
, buf
, buflen
);
161 ssize_t length2
= tor_getpass(prompt2
, buf2
, buflen
);
163 if (length
!= length2
|| tor_memneq(buf
, buf2
, length
)) {
164 fprintf(stderr
, "That didn't match.\n");
173 memwipe(buf2
, 0, buflen
);
177 if (options
->keygen_force_passphrase
== FORCE_PASSPHRASE_ON
&& length
== 0)
185 read_encrypted_secret_key(ed25519_secret_key_t
*out
,
189 uint8_t *secret
= NULL
;
190 size_t secret_len
= 0;
192 uint8_t encrypted_key
[256];
196 ssize_t encrypted_len
= crypto_read_tagged_contents_from_file(fname
,
200 sizeof(encrypted_key
));
201 if (encrypted_len
< 0) {
203 log_info(LD_OR
, "%s is missing", fname
);
207 if (strcmp(tag
, ENC_KEY_TAG
)) {
208 saved_errno
= EINVAL
;
214 do_getpass("Enter passphrase for master key:", pwbuf
, sizeof(pwbuf
), 0,
217 saved_errno
= EINVAL
;
220 const int r_unbox
= crypto_unpwbox(&secret
, &secret_len
,
221 encrypted_key
, encrypted_len
,
223 if (r_unbox
== UNPWBOX_CORRUPTED
) {
224 log_err(LD_OR
, "%s is corrupted.", fname
);
225 saved_errno
= EINVAL
;
227 } else if (r_unbox
== UNPWBOX_OKAY
) {
231 /* Otherwise, passphrase is bad, so try again till user does ctrl-c or gets
235 if (secret_len
!= ED25519_SECKEY_LEN
) {
236 log_err(LD_OR
, "%s is corrupted.", fname
);
237 saved_errno
= EINVAL
;
240 memcpy(out
->seckey
, secret
, ED25519_SECKEY_LEN
);
244 memwipe(encrypted_key
, 0, sizeof(encrypted_key
));
245 memwipe(pwbuf
, 0, sizeof(pwbuf
));
248 memwipe(secret
, 0, secret_len
);
258 write_encrypted_secret_key(const ed25519_secret_key_t
*key
,
263 uint8_t *encrypted_key
= NULL
;
264 size_t encrypted_len
= 0;
266 if (do_getpass("Enter new passphrase:", pwbuf0
, sizeof(pwbuf0
), 1,
267 get_options()) < 0) {
268 log_warn(LD_OR
, "NO/failed passphrase");
272 if (strlen(pwbuf0
) == 0) {
273 if (get_options()->keygen_force_passphrase
== FORCE_PASSPHRASE_ON
)
279 if (crypto_pwbox(&encrypted_key
, &encrypted_len
,
280 key
->seckey
, sizeof(key
->seckey
),
281 pwbuf0
, strlen(pwbuf0
), 0) < 0) {
282 log_warn(LD_OR
, "crypto_pwbox failed!?");
285 if (crypto_write_tagged_contents_to_file(fname
,
288 encrypted_key
, encrypted_len
) < 0)
293 memwipe(encrypted_key
, 0, encrypted_len
);
294 tor_free(encrypted_key
);
296 memwipe(pwbuf0
, 0, sizeof(pwbuf0
));
302 write_secret_key(const ed25519_secret_key_t
*key
, int encrypted
,
304 const char *fname_tag
,
305 const char *encrypted_fname
)
308 int r
= write_encrypted_secret_key(key
, encrypted_fname
);
312 /* Try to unlink the unencrypted key, if any existed before */
313 if (strcmp(fname
, encrypted_fname
))
317 /* Unrecoverable failure! */
321 fprintf(stderr
, "Not encrypting the secret key.\n");
323 return ed25519_seckey_write_to_file(key
, fname
, fname_tag
);
327 * Read an ed25519 key and associated certificates from files beginning with
328 * <b>fname</b>, with certificate type <b>cert_type</b>. On failure, return
329 * NULL; on success return the keypair.
331 * The <b>options</b> is used to look at the change_key_passphrase value when
332 * writing to disk a secret key. It is safe to be NULL even in that case.
334 * If INIT_ED_KEY_CREATE is set in <b>flags</b>, then create the key (and
335 * certificate if requested) if it doesn't exist, and save it to disk.
337 * If INIT_ED_KEY_NEEDCERT is set in <b>flags</b>, load/create a certificate
338 * too and store it in *<b>cert_out</b>. Fail if the cert can't be
339 * found/created. To create a certificate, <b>signing_key</b> must be set to
340 * the key that should sign it; <b>now</b> to the current time, and
341 * <b>lifetime</b> to the lifetime of the key.
343 * If INIT_ED_KEY_REPLACE is set in <b>flags</b>, then create and save new key
344 * whether we can read the old one or not.
346 * If INIT_ED_KEY_EXTRA_STRONG is set in <b>flags</b>, set the extra_strong
347 * flag when creating the secret key.
349 * If INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT is set in <b>flags</b>, and
350 * we create a new certificate, create it with the signing key embedded.
352 * If INIT_ED_KEY_SPLIT is set in <b>flags</b>, and we create a new key,
353 * store the public key in a separate file from the secret key.
355 * If INIT_ED_KEY_MISSING_SECRET_OK is set in <b>flags</b>, and we find a
356 * public key file but no secret key file, return successfully anyway.
358 * If INIT_ED_KEY_OMIT_SECRET is set in <b>flags</b>, do not try to load a
359 * secret key unless no public key is found. Do not return a secret key. (but
360 * create and save one if needed).
362 * If INIT_ED_KEY_TRY_ENCRYPTED is set, we look for an encrypted secret key
363 * and consider encrypting any new secret key.
365 * If INIT_ED_KEY_NO_REPAIR is set, and there is any issue loading the keys
366 * from disk _other than their absence_ (full or partial), we do not try to
369 * If INIT_ED_KEY_SUGGEST_KEYGEN is set, have log messages about failures
370 * refer to the --keygen option.
372 * If INIT_ED_KEY_EXPLICIT_FNAME is set, use the provided file name for the
373 * secret key file, encrypted or not.
375 * If INIT_ED_KEY_OFFLINE_SECRET is set, we won't try to load the master
376 * secret key and we log a message at <b>severity</b> that we've done so.
379 ed_key_init_from_file(const char *fname
, uint32_t flags
,
381 const ed25519_keypair_t
*signing_key
,
385 struct tor_cert_st
**cert_out
,
386 const or_options_t
*options
)
388 char *secret_fname
= NULL
;
389 char *encrypted_secret_fname
= NULL
;
390 char *public_fname
= NULL
;
391 char *cert_fname
= NULL
;
392 const char *loaded_secret_fname
= NULL
;
393 int created_pk
= 0, created_sk
= 0, created_cert
= 0;
394 const int try_to_load
= ! (flags
& INIT_ED_KEY_REPLACE
);
395 const int encrypt_key
= !! (flags
& INIT_ED_KEY_TRY_ENCRYPTED
);
396 const int norepair
= !! (flags
& INIT_ED_KEY_NO_REPAIR
);
397 const int split
= !! (flags
& INIT_ED_KEY_SPLIT
);
398 const int omit_secret
= !! (flags
& INIT_ED_KEY_OMIT_SECRET
);
399 const int offline_secret
= !! (flags
& INIT_ED_KEY_OFFLINE_SECRET
);
400 const int explicit_fname
= !! (flags
& INIT_ED_KEY_EXPLICIT_FNAME
);
402 /* we don't support setting both of these flags at once. */
403 tor_assert((flags
& (INIT_ED_KEY_NO_REPAIR
|INIT_ED_KEY_NEEDCERT
)) !=
404 (INIT_ED_KEY_NO_REPAIR
|INIT_ED_KEY_NEEDCERT
));
407 tor_snprintf(tag
, sizeof(tag
), "type%d", (int)cert_type
);
409 tor_cert_t
*cert
= NULL
;
410 char *got_tag
= NULL
;
411 ed25519_keypair_t
*keypair
= tor_malloc_zero(sizeof(ed25519_keypair_t
));
413 if (explicit_fname
) {
414 secret_fname
= tor_strdup(fname
);
415 encrypted_secret_fname
= tor_strdup(fname
);
417 tor_asprintf(&secret_fname
, "%s_secret_key", fname
);
418 tor_asprintf(&encrypted_secret_fname
, "%s_secret_key_encrypted", fname
);
420 tor_asprintf(&public_fname
, "%s_public_key", fname
);
421 tor_asprintf(&cert_fname
, "%s_cert", fname
);
423 /* Try to read the secret key. */
425 int load_secret
= try_to_load
&&
427 (!omit_secret
|| file_status(public_fname
)==FN_NOENT
);
429 int rv
= ed25519_seckey_read_from_file(&keypair
->seckey
,
430 &got_tag
, secret_fname
);
433 loaded_secret_fname
= secret_fname
;
436 if (errno
!= ENOENT
&& norepair
) {
437 tor_log(severity
, LD_OR
, "Unable to read %s: %s", secret_fname
,
444 /* Should we try for an encrypted key? */
445 int have_encrypted_secret_file
= 0;
446 if (!have_secret
&& try_to_load
&& encrypt_key
) {
447 int r
= read_encrypted_secret_key(&keypair
->seckey
,
448 encrypted_secret_fname
);
451 have_encrypted_secret_file
= 1;
452 tor_free(got_tag
); /* convince coverity we aren't leaking */
453 got_tag
= tor_strdup(tag
);
454 loaded_secret_fname
= encrypted_secret_fname
;
455 } else if (errno
!= ENOENT
&& norepair
) {
456 tor_log(severity
, LD_OR
, "Unable to read %s: %s",
457 encrypted_secret_fname
, strerror(errno
));
462 /* Check if it's there anyway, so we don't replace it. */
463 if (file_status(encrypted_secret_fname
) != FN_NOENT
)
464 have_encrypted_secret_file
= 1;
469 if (strcmp(got_tag
, tag
)) {
470 tor_log(severity
, LD_OR
, "%s has wrong tag", loaded_secret_fname
);
473 /* Derive the public key */
474 if (ed25519_public_key_generate(&keypair
->pubkey
, &keypair
->seckey
)<0) {
475 tor_log(severity
, LD_OR
, "%s can't produce a public key",
476 loaded_secret_fname
);
481 /* If we do split keys here, try to read the pubkey. */
482 int found_public
= 0;
483 if (try_to_load
&& (!have_secret
|| split
)) {
484 ed25519_public_key_t pubkey_tmp
;
486 found_public
= ed25519_pubkey_read_from_file(&pubkey_tmp
,
487 &got_tag
, public_fname
) == 0;
488 if (!found_public
&& errno
!= ENOENT
&& norepair
) {
489 tor_log(severity
, LD_OR
, "Unable to read %s: %s", public_fname
,
493 if (found_public
&& strcmp(got_tag
, tag
)) {
494 tor_log(severity
, LD_OR
, "%s has wrong tag", public_fname
);
499 /* If we have a secret key and we're reloading the public key,
500 * the key must match! */
501 if (! ed25519_pubkey_eq(&keypair
->pubkey
, &pubkey_tmp
)) {
502 tor_log(severity
, LD_OR
, "%s does not match %s! If you are trying "
503 "to restore from backup, make sure you didn't mix up the "
504 "key files. If you are absolutely sure that %s is the right "
505 "key for this relay, delete %s or move it out of the way.",
506 public_fname
, loaded_secret_fname
,
507 loaded_secret_fname
, public_fname
);
511 /* We only have the public key; better use that. */
513 memcpy(&keypair
->pubkey
, &pubkey_tmp
, sizeof(pubkey_tmp
));
516 /* We have no public key file, but we do have a secret key, make the
517 * public key file! */
519 if (ed25519_pubkey_write_to_file(&keypair
->pubkey
, public_fname
, tag
)
521 tor_log(severity
, LD_OR
, "Couldn't repair %s", public_fname
);
524 tor_log(LOG_NOTICE
, LD_OR
,
525 "Found secret key but not %s. Regenerating.",
532 /* If the secret key is absent and it's not allowed to be, fail. */
533 if (!have_secret
&& found_public
&&
534 !(flags
& INIT_ED_KEY_MISSING_SECRET_OK
)) {
535 if (have_encrypted_secret_file
) {
536 tor_log(severity
, LD_OR
, "We needed to load a secret key from %s, "
537 "but it was encrypted. Try 'tor --keygen' instead, so you "
538 "can enter the passphrase.",
540 } else if (offline_secret
) {
541 tor_log(severity
, LD_OR
, "We wanted to load a secret key from %s, "
542 "but you're keeping it offline. (OfflineMasterKey is set.)",
545 tor_log(severity
, LD_OR
, "We needed to load a secret key from %s, "
546 "but couldn't find it. %s", secret_fname
,
547 (flags
& INIT_ED_KEY_SUGGEST_KEYGEN
) ?
548 "If you're keeping your master secret key offline, you will "
549 "need to run 'tor --keygen' to generate new signing keys." :
550 "Did you forget to copy it over when you copied the rest of the "
551 "signing key material?");
556 /* If it's absent, and we're not supposed to make a new keypair, fail. */
557 if (!have_secret
&& !found_public
&& !(flags
& INIT_ED_KEY_CREATE
)) {
559 tor_log(severity
, LD_OR
, "No key found in %s or %s.",
560 secret_fname
, public_fname
);
562 tor_log(severity
, LD_OR
, "No key found in %s.", secret_fname
);
567 /* If the secret key is absent, but the encrypted key would be present,
569 if (!have_secret
&& !found_public
&& have_encrypted_secret_file
) {
570 tor_assert(!encrypt_key
);
571 tor_log(severity
, LD_OR
, "Found an encrypted secret key, "
572 "but not public key file %s!", public_fname
);
576 /* if it's absent, make a new keypair... */
577 if (!have_secret
&& !found_public
) {
579 keypair
= ed_key_new(signing_key
, flags
, now
, lifetime
,
582 tor_log(severity
, LD_OR
, "Couldn't create keypair");
585 created_pk
= created_sk
= created_cert
= 1;
588 /* Write it to disk if we're supposed to do with a new passphrase, or if
589 * we just created it. */
590 if (created_sk
|| (have_secret
&& options
!= NULL
&&
591 options
->change_key_passphrase
)) {
592 if (write_secret_key(&keypair
->seckey
,
594 secret_fname
, tag
, encrypted_secret_fname
) < 0
597 ed25519_pubkey_write_to_file(&keypair
->pubkey
, public_fname
, tag
) < 0)
600 crypto_write_tagged_contents_to_file(cert_fname
, "ed25519v1-cert",
601 tag
, cert
->encoded
, cert
->encoded_len
) < 0)) {
602 tor_log(severity
, LD_OR
, "Couldn't write keys or cert to file.");
608 /* If we're not supposed to get a cert, we're done. */
609 if (! (flags
& INIT_ED_KEY_NEEDCERT
))
614 uint8_t certbuf
[256];
615 ssize_t cert_body_len
= crypto_read_tagged_contents_from_file(
616 cert_fname
, "ed25519v1-cert",
617 &got_tag
, certbuf
, sizeof(certbuf
));
618 if (cert_body_len
>= 0 && !strcmp(got_tag
, tag
))
619 cert
= tor_cert_parse(certbuf
, cert_body_len
);
621 /* If we got it, check it to the extent we can. */
625 tor_log(severity
, LD_OR
, "Cert was unparseable");
627 } else if (!tor_memeq(cert
->signed_key
.pubkey
, keypair
->pubkey
.pubkey
,
628 ED25519_PUBKEY_LEN
)) {
629 tor_log(severity
, LD_OR
, "Cert was for wrong key");
631 } else if (signing_key
&&
632 tor_cert_checksig(cert
, &signing_key
->pubkey
, now
) < 0) {
633 tor_log(severity
, LD_OR
, "Can't check certificate: %s",
634 tor_cert_describe_signature_status(cert
));
636 } else if (cert
->cert_expired
) {
637 tor_log(severity
, LD_OR
, "Certificate is expired");
639 } else if (signing_key
&& cert
->signing_key_included
&&
640 ! ed25519_pubkey_eq(&signing_key
->pubkey
, &cert
->signing_key
)) {
641 tor_log(severity
, LD_OR
, "Certificate signed by unexpected key!");
650 /* If we got a cert, we're done. */
654 /* If we didn't get a cert, and we're not supposed to make one, fail. */
655 if (!signing_key
|| !(flags
& INIT_ED_KEY_CREATE
)) {
656 tor_log(severity
, LD_OR
, "Without signing key, can't create certificate");
660 /* We have keys but not a certificate, so make one. */
661 uint32_t cert_flags
= 0;
662 if (flags
& INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT
)
663 cert_flags
|= CERT_FLAG_INCLUDE_SIGNING_KEY
;
664 cert
= tor_cert_create_ed25519(signing_key
, cert_type
,
670 tor_log(severity
, LD_OR
, "Couldn't create certificate");
674 /* Write it to disk. */
676 if (crypto_write_tagged_contents_to_file(cert_fname
, "ed25519v1-cert",
677 tag
, cert
->encoded
, cert
->encoded_len
) < 0) {
678 tor_log(severity
, LD_OR
, "Couldn't write cert to disk.");
692 memwipe(keypair
, 0, sizeof(*keypair
));
698 unlink(secret_fname
);
700 unlink(public_fname
);
705 tor_free(encrypted_secret_fname
);
706 tor_free(secret_fname
);
707 tor_free(public_fname
);
708 tor_free(cert_fname
);
715 * Create a new signing key and (optionally) certificate; do not read or write
716 * from disk. See ed_key_init_from_file() for more information.
719 ed_key_new(const ed25519_keypair_t
*signing_key
,
724 struct tor_cert_st
**cert_out
)
729 const int extra_strong
= !! (flags
& INIT_ED_KEY_EXTRA_STRONG
);
730 ed25519_keypair_t
*keypair
= tor_malloc_zero(sizeof(ed25519_keypair_t
));
731 if (ed25519_keypair_generate(keypair
, extra_strong
) < 0)
734 if (! (flags
& INIT_ED_KEY_NEEDCERT
))
737 tor_assert(signing_key
);
738 tor_assert(cert_out
);
739 uint32_t cert_flags
= 0;
740 if (flags
& INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT
)
741 cert_flags
|= CERT_FLAG_INCLUDE_SIGNING_KEY
;
742 tor_cert_t
*cert
= tor_cert_create_ed25519(signing_key
, cert_type
,