26 #define ENC_KEY_HEADER "Boxed Ed25519 key"
27 #define ENC_KEY_TAG "master"
57 tor_log(severity,
LD_FS,
"Can't read key from \"%s\"", fname);
69 tor_log(severity,
LD_FS,
"Another Tor process has locked \"%s\". "
70 "Not writing any new keys.", fname);
76 log_info(
LD_GENERAL,
"No key found in \"%s\"; generating fresh key.",
78 if (crypto_pk_generate_key(prkey)) {
86 log_info(
LD_GENERAL,
"Generated key seems valid");
92 "Couldn't write generated key to \"%s\".", fname);
112 crypto_pk_free(prkey);
118 do_getpass(
const char *prompt,
char *buf,
size_t buflen,
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;
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);
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");
177 if (options->keygen_force_passphrase == FORCE_PASSPHRASE_ON && length == 0)
189 uint8_t *secret = NULL;
190 size_t secret_len = 0;
192 uint8_t encrypted_key[256];
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;
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) {
236 log_err(
LD_OR,
"%s is corrupted.", fname);
237 saved_errno = EINVAL;
244 memwipe(encrypted_key, 0,
sizeof(encrypted_key));
245 memwipe(pwbuf, 0,
sizeof(pwbuf));
248 memwipe(secret, 0, secret_len);
263 uint8_t *encrypted_key = NULL;
264 size_t encrypted_len = 0;
266 if (do_getpass(
"Enter new passphrase:", pwbuf0,
sizeof(pwbuf0), 1,
268 log_warn(
LD_OR,
"NO/failed passphrase");
272 if (strlen(pwbuf0) == 0) {
273 if (
get_options()->keygen_force_passphrase == FORCE_PASSPHRASE_ON)
281 pwbuf0, strlen(pwbuf0), 0) < 0) {
282 log_warn(
LD_OR,
"crypto_pwbox failed!?");
288 encrypted_key, encrypted_len) < 0)
293 memwipe(encrypted_key, 0, encrypted_len);
296 memwipe(pwbuf0, 0,
sizeof(pwbuf0));
304 const char *fname_tag,
305 const char *encrypted_fname)
308 int r = write_encrypted_secret_key(key, encrypted_fname);
313 if (strcmp(fname, encrypted_fname))
321 fprintf(stderr,
"Not encrypting the secret key.\n");
385 struct tor_cert_st **cert_out,
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);
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);
410 char *got_tag = NULL;
413 if (explicit_fname) {
414 secret_fname = tor_strdup(fname);
415 encrypted_secret_fname = tor_strdup(fname);
418 tor_asprintf(&encrypted_secret_fname,
"%s_secret_key_encrypted", fname);
425 int load_secret = try_to_load &&
427 (!omit_secret ||
file_status(public_fname)==FN_NOENT);
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,
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;
453 got_tag = tor_strdup(tag);
454 loaded_secret_fname = encrypted_secret_fname;
455 }
else if (errno != ENOENT && norepair) {
457 encrypted_secret_fname, strerror(errno));
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);
475 tor_log(severity,
LD_OR,
"%s can't produce a public key",
476 loaded_secret_fname);
482 int found_public = 0;
483 if (try_to_load && (!have_secret || split)) {
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);
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);
513 memcpy(&keypair->pubkey, &pubkey_tmp,
sizeof(pubkey_tmp));
521 tor_log(severity,
LD_OR,
"Couldn't repair %s", public_fname);
525 "Found secret key but not %s. Regenerating.",
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?");
557 if (!have_secret && !found_public && !(flags & INIT_ED_KEY_CREATE)) {
560 secret_fname, public_fname);
562 tor_log(severity,
LD_OR,
"No key found in %s.", secret_fname);
569 if (!have_secret && !found_public && have_encrypted_secret_file) {
571 tor_log(severity,
LD_OR,
"Found an encrypted secret key, "
572 "but not public key file %s!", public_fname);
577 if (!have_secret && !found_public) {
579 keypair =
ed_key_new(signing_key, flags, now, lifetime,
585 created_pk = created_sk = created_cert = 1;
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
602 tor_log(severity,
LD_OR,
"Couldn't write keys or cert to file.");
609 if (! (flags & INIT_ED_KEY_NEEDCERT))
614 uint8_t certbuf[256];
616 cert_fname,
"ed25519v1-cert",
617 &got_tag, certbuf,
sizeof(certbuf));
618 if (cert_body_len >= 0 && !strcmp(got_tag, tag))
631 }
else if (signing_key &&
641 tor_log(severity,
LD_OR,
"Certificate signed by unexpected key!");
655 if (!signing_key || !(flags & INIT_ED_KEY_CREATE)) {
656 tor_log(severity,
LD_OR,
"Without signing key, can't create certificate");
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;
670 tor_log(severity,
LD_OR,
"Couldn't create certificate");
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);
724 struct tor_cert_st **cert_out)
729 const int extra_strong = !! (flags & INIT_ED_KEY_EXTRA_STRONG);
734 if (! (flags & INIT_ED_KEY_NEEDCERT))
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;
const or_options_t * get_options(void)
Header file for config.c.
int ed25519_public_key_generate(ed25519_public_key_t *pubkey_out, const ed25519_secret_key_t *seckey)
int ed25519_seckey_read_from_file(ed25519_secret_key_t *seckey_out, char **tag_out, const char *filename)
int ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong)
int ed25519_pubkey_write_to_file(const ed25519_public_key_t *pubkey, const char *filename, const char *tag)
int ed25519_seckey_write_to_file(const ed25519_secret_key_t *seckey, const char *filename, const char *tag)
int ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out, char **tag_out, const char *filename)
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
int crypto_pwbox(uint8_t **out, size_t *outlen_out, const uint8_t *input, size_t input_len, const char *secret, size_t secret_len, unsigned s2k_flags)
int crypto_unpwbox(uint8_t **out, size_t *outlen_out, const uint8_t *inp, size_t input_len, const char *secret, size_t secret_len)
Header for crypto_pwbox.c.
int crypto_pk_write_private_key_to_filename(crypto_pk_t *env, const char *fname)
int crypto_pk_read_private_key_from_filename(crypto_pk_t *env, const char *keyfile)
crypto_pk_t * crypto_pk_new(void)
int crypto_pk_is_valid_private_key(const crypto_pk_t *env)
void memwipe(void *mem, uint8_t byte, size_t sz)
Common functions for cryptographic routines.
int tor_memeq(const void *a, const void *b, size_t sz)
#define tor_memneq(a, b, sz)
ssize_t read_all_from_fd(int fd, char *buf, size_t count)
file_status_t file_status(const char *filename)
ssize_t tor_getpass(const char *prompt, char *output, size_t buflen)
ed25519_keypair_t * ed_key_init_from_file(const char *fname, uint32_t flags, int severity, const ed25519_keypair_t *signing_key, time_t now, time_t lifetime, uint8_t cert_type, struct tor_cert_st **cert_out, const or_options_t *options)
crypto_pk_t * init_key_from_file(const char *fname, int generate, int severity, bool *created_out)
ed25519_keypair_t * ed_key_new(const ed25519_keypair_t *signing_key, uint32_t flags, time_t now, time_t lifetime, uint8_t cert_type, struct tor_cert_st **cert_out)
Header file for loadkey.c.
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
int try_locking(const or_options_t *options, int err_if_locked)
Master header file for Tor-specific functionality.
int tor_asprintf(char **strp, const char *fmt,...)
int tor_snprintf(char *str, size_t size, const char *format,...)
uint8_t seckey[ED25519_SECKEY_LEN]
ed25519_public_key_t signing_key
ed25519_public_key_t signed_key
unsigned signing_key_included
int tor_cert_checksig(tor_cert_t *cert, const ed25519_public_key_t *pubkey, time_t now)
const char * tor_cert_describe_signature_status(const tor_cert_t *cert)
tor_cert_t * tor_cert_parse(const uint8_t *encoded, const size_t len)
tor_cert_t * tor_cert_create_ed25519(const ed25519_keypair_t *signing_key, uint8_t cert_type, const ed25519_public_key_t *signed_key, time_t now, time_t lifetime, uint32_t flags)
#define ED25519_SECKEY_LEN
#define ED25519_PUBKEY_LEN