=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/ssh-keygen.c,v retrieving revision 1.419 retrieving revision 1.421 diff -u -r1.419 -r1.421 --- src/usr.bin/ssh/ssh-keygen.c 2020/08/27 09:46:04 1.419 +++ src/usr.bin/ssh/ssh-keygen.c 2020/10/18 11:32:02 1.421 @@ -1,4 +1,4 @@ -/* $OpenBSD: ssh-keygen.c,v 1.419 2020/08/27 09:46:04 djm Exp $ */ +/* $OpenBSD: ssh-keygen.c,v 1.421 2020/10/18 11:32:02 djm Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1994 Tatu Ylonen , Espoo, Finland @@ -309,7 +309,7 @@ if ((r = sshkey_load_private(filename, "", &prv, commentp)) == 0) return prv; if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) - fatal("Load key \"%s\": %s", filename, ssh_err(r)); + fatal_r(r, "Load key \"%s\"", filename); if (identity_passphrase) pass = xstrdup(identity_passphrase); else @@ -317,7 +317,7 @@ r = sshkey_load_private(filename, pass, &prv, commentp); freezero(pass, strlen(pass)); if (r != 0) - fatal("Load key \"%s\": %s", filename, ssh_err(r)); + fatal_r(r, "Load key \"%s\"", filename); return prv; } @@ -335,11 +335,11 @@ int r; if ((b = sshbuf_new()) == NULL) - fatal("%s: sshbuf_new failed", __func__); + fatal_f("sshbuf_new failed"); if ((r = sshkey_putb(k, b)) != 0) - fatal("key_to_blob failed: %s", ssh_err(r)); + fatal_fr(r, "put key"); if ((b64 = sshbuf_dtob64_string(b, 1)) == NULL) - fatal("%s: sshbuf_dtob64_string failed", __func__); + fatal_f("sshbuf_dtob64_string failed"); /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */ snprintf(comment, sizeof(comment), @@ -374,7 +374,7 @@ fatal("PEM_write_EC_PUBKEY failed"); break; default: - fatal("%s: unsupported key type %s", __func__, sshkey_type(k)); + fatal_f("unsupported key type %s", sshkey_type(k)); } exit(0); } @@ -396,7 +396,7 @@ fatal("PEM_write_EC_PUBKEY failed"); break; default: - fatal("%s: unsupported key type %s", __func__, sshkey_type(k)); + fatal_f("unsupported key type %s", sshkey_type(k)); } exit(0); } @@ -425,7 +425,7 @@ do_convert_to_pem(k); break; default: - fatal("%s: unknown key format %d", __func__, convert_format); + fatal_f("unknown key format %d", convert_format); } exit(0); } @@ -441,15 +441,15 @@ int r; if ((r = sshbuf_get_u32(b, &bignum_bits)) != 0) - fatal("%s: buffer error: %s", __func__, ssh_err(r)); + fatal_fr(r, "parse"); bytes = (bignum_bits + 7) / 8; if (sshbuf_len(b) < bytes) - fatal("%s: input buffer too small: need %d have %zu", - __func__, bytes, sshbuf_len(b)); + fatal_f("input buffer too small: need %d have %zu", + bytes, sshbuf_len(b)); if (BN_bin2bn(sshbuf_ptr(b), bytes, value) == NULL) - fatal("%s: BN_bin2bn failed", __func__); + fatal_f("BN_bin2bn failed"); if ((r = sshbuf_consume(b, bytes)) != 0) - fatal("%s: buffer error: %s", __func__, ssh_err(r)); + fatal_fr(r, "consume"); } static struct sshkey * @@ -468,7 +468,7 @@ BIGNUM *rsa_p = NULL, *rsa_q = NULL, *rsa_iqmp = NULL; if ((r = sshbuf_get_u32(b, &magic)) != 0) - fatal("%s: buffer error: %s", __func__, ssh_err(r)); + fatal_fr(r, "parse magic"); if (magic != SSH_COM_PRIVATE_KEY_MAGIC) { error("bad magic 0x%x != 0x%x", magic, @@ -481,7 +481,7 @@ (r = sshbuf_get_u32(b, &i2)) != 0 || (r = sshbuf_get_u32(b, &i3)) != 0 || (r = sshbuf_get_u32(b, &i4)) != 0) - fatal("%s: buffer error: %s", __func__, ssh_err(r)); + fatal_fr(r, "parse"); debug("ignore (%d %d %d %d)", i1, i2, i3, i4); if (strcmp(cipher, "none") != 0) { error("unsupported cipher %s", cipher); @@ -510,24 +510,24 @@ (dsa_g = BN_new()) == NULL || (dsa_pub_key = BN_new()) == NULL || (dsa_priv_key = BN_new()) == NULL) - fatal("%s: BN_new", __func__); + fatal_f("BN_new"); buffer_get_bignum_bits(b, dsa_p); buffer_get_bignum_bits(b, dsa_g); buffer_get_bignum_bits(b, dsa_q); buffer_get_bignum_bits(b, dsa_pub_key); buffer_get_bignum_bits(b, dsa_priv_key); if (!DSA_set0_pqg(key->dsa, dsa_p, dsa_q, dsa_g)) - fatal("%s: DSA_set0_pqg failed", __func__); + fatal_f("DSA_set0_pqg failed"); dsa_p = dsa_q = dsa_g = NULL; /* transferred */ if (!DSA_set0_key(key->dsa, dsa_pub_key, dsa_priv_key)) - fatal("%s: DSA_set0_key failed", __func__); + fatal_f("DSA_set0_key failed"); dsa_pub_key = dsa_priv_key = NULL; /* transferred */ break; case KEY_RSA: if ((r = sshbuf_get_u8(b, &e1)) != 0 || (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) || (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0)) - fatal("%s: buffer error: %s", __func__, ssh_err(r)); + fatal_fr(r, "parse RSA"); e = e1; debug("e %lx", e); if (e < 30) { @@ -539,7 +539,7 @@ debug("e %lx", e); } if ((rsa_e = BN_new()) == NULL) - fatal("%s: BN_new", __func__); + fatal_f("BN_new"); if (!BN_set_word(rsa_e, e)) { BN_clear_free(rsa_e); sshkey_free(key); @@ -550,26 +550,26 @@ (rsa_p = BN_new()) == NULL || (rsa_q = BN_new()) == NULL || (rsa_iqmp = BN_new()) == NULL) - fatal("%s: BN_new", __func__); + fatal_f("BN_new"); buffer_get_bignum_bits(b, rsa_d); buffer_get_bignum_bits(b, rsa_n); buffer_get_bignum_bits(b, rsa_iqmp); buffer_get_bignum_bits(b, rsa_q); buffer_get_bignum_bits(b, rsa_p); if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, rsa_d)) - fatal("%s: RSA_set0_key failed", __func__); + fatal_f("RSA_set0_key failed"); rsa_n = rsa_e = rsa_d = NULL; /* transferred */ if (!RSA_set0_factors(key->rsa, rsa_p, rsa_q)) - fatal("%s: RSA_set0_factors failed", __func__); + fatal_f("RSA_set0_factors failed"); rsa_p = rsa_q = NULL; /* transferred */ if ((r = ssh_rsa_complete_crt_parameters(key, rsa_iqmp)) != 0) - fatal("generate RSA parameters failed: %s", ssh_err(r)); + fatal_fr(r, "generate RSA parameters"); BN_clear_free(rsa_iqmp); break; } rlen = sshbuf_len(b); if (rlen != 0) - error("%s: remaining bytes in key blob %d", __func__, rlen); + error_f("remaining bytes in key blob %d", rlen); /* try the key */ if (sshkey_sign(key, &sig, &slen, data, sizeof(data), @@ -652,12 +652,12 @@ (encoded[len-3] == '=')) encoded[len-3] = '\0'; if ((r = sshbuf_b64tod(buf, encoded)) != 0) - fatal("%s: base64 decoding failed: %s", __func__, ssh_err(r)); + fatal_fr(r, "base64 decode"); if (*private) { if ((*k = do_convert_private_ssh2(buf)) == NULL) - fatal("%s: private key conversion failed", __func__); + fatal_f("private key conversion failed"); } else if ((r = sshkey_fromb(buf, k)) != 0) - fatal("decode blob failed: %s", ssh_err(r)); + fatal_fr(r, "parse key"); sshbuf_free(buf); fclose(fp); } @@ -671,7 +671,7 @@ if ((fp = fopen(identity_file, "r")) == NULL) fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) { - fatal("%s: %s is not a recognised public key format", __func__, + fatal_f("%s is not a recognised public key format", identity_file); } fclose(fp); @@ -696,7 +696,7 @@ (*k)->ecdsa_nid = sshkey_ecdsa_key_to_nid((*k)->ecdsa); break; default: - fatal("%s: unsupported pubkey type %d", __func__, + fatal_f("unsupported pubkey type %d", EVP_PKEY_base_id(pubkey)); } EVP_PKEY_free(pubkey); @@ -719,7 +719,7 @@ fclose(fp); return; } - fatal("%s: unrecognised raw private key format", __func__); + fatal_f("unrecognised raw private key format"); } static void @@ -745,7 +745,7 @@ do_convert_from_pem(&k, &private); break; default: - fatal("%s: unknown key format %d", __func__, convert_format); + fatal_f("unknown key format %d", convert_format); } if (!private) { @@ -768,8 +768,7 @@ NULL, 0, NULL, NULL); break; default: - fatal("%s: unsupported key type %s", __func__, - sshkey_type(k)); + fatal_f("unsupported key type %s", sshkey_type(k)); } } @@ -794,7 +793,7 @@ fatal("%s: %s", identity_file, strerror(errno)); prv = load_identity(identity_file, &comment); if ((r = sshkey_write(prv, stdout)) != 0) - error("sshkey_write failed: %s", ssh_err(r)); + fatal_fr(r, "write key"); if (comment != NULL && *comment != '\0') fprintf(stdout, " %s", comment); fprintf(stdout, "\n"); @@ -830,7 +829,7 @@ ra = sshkey_fingerprint(keys[i], fingerprint_hash, SSH_FP_RANDOMART); if (fp == NULL || ra == NULL) - fatal("%s: sshkey_fingerprint fail", __func__); + fatal_f("sshkey_fingerprint fail"); printf("%u %s %s (PKCS11 key)\n", sshkey_size(keys[i]), fp, sshkey_type(keys[i])); if (log_level_get() >= SYSLOG_LEVEL_VERBOSE) @@ -881,7 +880,7 @@ fp = sshkey_fingerprint(public, fptype, rep); ra = sshkey_fingerprint(public, fingerprint_hash, SSH_FP_RANDOMART); if (fp == NULL || ra == NULL) - fatal("%s: sshkey_fingerprint failed", __func__); + fatal_f("sshkey_fingerprint failed"); mprintf("%u %s %s (%s)\n", sshkey_size(public), fp, comment ? comment : "no comment", sshkey_type(public)); if (log_level_get() >= SYSLOG_LEVEL_VERBOSE) @@ -901,12 +900,12 @@ if (stat(identity_file, &st) == -1) fatal("%s: %s", path, strerror(errno)); if ((r = sshkey_load_public(path, &pubkey, &comment)) != 0) - debug("load public \"%s\": %s", path, ssh_err(r)); + debug_r(r, "load public \"%s\"", path); if (pubkey == NULL || comment == NULL || *comment == '\0') { free(comment); if ((r = sshkey_load_private(path, NULL, &privkey, &comment)) != 0) - debug("load private \"%s\": %s", path, ssh_err(r)); + debug_r(r, "load private \"%s\"", path); } if (pubkey == NULL && privkey == NULL) fatal("%s is not a key file.", path); @@ -1084,18 +1083,17 @@ bits = 0; type_bits_valid(type, NULL, &bits); if ((r = sshkey_generate(type, bits, &private)) != 0) { - error("sshkey_generate failed: %s", ssh_err(r)); + error_r(r, "sshkey_generate failed"); goto failnext; } if ((r = sshkey_from_private(private, &public)) != 0) - fatal("sshkey_from_private failed: %s", ssh_err(r)); + fatal_fr(r, "sshkey_from_private"); snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); if ((r = sshkey_save_private(private, prv_tmp, "", comment, private_key_format, openssh_format_cipher, rounds)) != 0) { - error("Saving key \"%s\" failed: %s", - prv_tmp, ssh_err(r)); + error_r(r, "Saving key \"%s\" failed", prv_tmp); goto failnext; } if ((fd = mkstemp(pub_tmp)) == -1) { @@ -1106,8 +1104,8 @@ (void)fchmod(fd, 0644); (void)close(fd); if ((r = sshkey_save_public(public, pub_tmp, comment)) != 0) { - fatal("Unable to save public key to %s: %s", - identity_file, ssh_err(r)); + error_r(r, "Unable to save public key to %s", + identity_file); goto failnext; } @@ -1241,8 +1239,7 @@ ra = sshkey_fingerprint(l->key, fingerprint_hash, SSH_FP_RANDOMART); if (fp == NULL || ra == NULL) - fatal("%s: sshkey_fingerprint failed", - __func__); + fatal_f("sshkey_fingerprint failed"); mprintf("%s %s %s%s%s\n", ctx->host, sshkey_type(l->key), fp, l->comment[0] ? " " : "", @@ -1323,7 +1320,7 @@ foreach_options)) != 0) { if (inplace) unlink(tmp); - fatal("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r)); + fatal_fr(r, "hostkeys_foreach"); } if (inplace) @@ -1402,7 +1399,7 @@ goto badkey; } else if (r != 0) { badkey: - fatal("Failed to load key %s: %s", identity_file, ssh_err(r)); + fatal_r(r, "Failed to load key %s", identity_file); } if (comment) mprintf("Key has comment '%s'\n", comment); @@ -1434,8 +1431,7 @@ /* Save the file using the new passphrase. */ if ((r = sshkey_save_private(private, identity_file, passphrase1, comment, private_key_format, openssh_format_cipher, rounds)) != 0) { - error("Saving key \"%s\" failed: %s.", - identity_file, ssh_err(r)); + error_r(r, "Saving key \"%s\" failed", identity_file); freezero(passphrase1, strlen(passphrase1)); sshkey_free(private); free(comment); @@ -1463,15 +1459,14 @@ int r; if (fname == NULL) - fatal("%s: no filename", __func__); + fatal_f("no filename"); if (stat(fname, &st) == -1) { if (errno == ENOENT) return 0; fatal("%s: %s", fname, strerror(errno)); } if ((r = sshkey_load_public(fname, &public, &comment)) != 0) - fatal("Failed to read v2 public key from \"%s\": %s.", - fname, ssh_err(r)); + fatal_r(r, "Failed to read v2 public key from \"%s\"", fname); export_dns_rr(hname, public, stdout, print_generic); sshkey_free(public); free(comment); @@ -1498,8 +1493,7 @@ &private, &comment)) == 0) passphrase = xstrdup(""); else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) - fatal("Cannot load private key \"%s\": %s.", - identity_file, ssh_err(r)); + fatal_r(r, "Cannot load private key \"%s\"", identity_file); else { if (identity_passphrase) passphrase = xstrdup(identity_passphrase); @@ -1512,8 +1506,8 @@ if ((r = sshkey_load_private(identity_file, passphrase, &private, &comment)) != 0) { freezero(passphrase, strlen(passphrase)); - fatal("Cannot load private key \"%s\": %s.", - identity_file, ssh_err(r)); + fatal_r(r, "Cannot load private key \"%s\"", + identity_file); } } @@ -1554,8 +1548,7 @@ if ((r = sshkey_save_private(private, identity_file, passphrase, new_comment, private_key_format, openssh_format_cipher, rounds)) != 0) { - error("Saving key \"%s\" failed: %s", - identity_file, ssh_err(r)); + error_r(r, "Saving key \"%s\" failed", identity_file); freezero(passphrase, strlen(passphrase)); sshkey_free(private); free(comment); @@ -1563,14 +1556,12 @@ } freezero(passphrase, strlen(passphrase)); if ((r = sshkey_from_private(private, &public)) != 0) - fatal("sshkey_from_private failed: %s", ssh_err(r)); + fatal_fr(r, "sshkey_from_private"); sshkey_free(private); strlcat(identity_file, ".pub", sizeof(identity_file)); - if ((r = sshkey_save_public(public, identity_file, new_comment)) != 0) { - fatal("Unable to save public key to %s: %s", - identity_file, ssh_err(r)); - } + if ((r = sshkey_save_public(public, identity_file, new_comment)) != 0) + fatal_r(r, "Unable to save public key to %s", identity_file); sshkey_free(public); free(comment); @@ -1622,7 +1613,7 @@ const struct cert_ext *ext; if ((b = sshbuf_new()) == NULL) - fatal("%s: sshbuf_new failed", __func__); + fatal_f("sshbuf_new failed"); sshbuf_reset(c); for (i = 0; i < ncert_ext; i++) { ext = &cert_ext[i]; @@ -1631,18 +1622,18 @@ continue; if (ext->val == NULL) { /* flag option */ - debug3("%s: %s", __func__, ext->key); + debug3_f("%s", ext->key); if ((r = sshbuf_put_cstring(c, ext->key)) != 0 || (r = sshbuf_put_string(c, NULL, 0)) != 0) - fatal("%s: buffer: %s", __func__, ssh_err(r)); + fatal_fr(r, "prepare flag"); } else { /* key/value option */ - debug3("%s: %s=%s", __func__, ext->key, ext->val); + debug3_f("%s=%s", ext->key, ext->val); sshbuf_reset(b); if ((r = sshbuf_put_cstring(c, ext->key)) != 0 || (r = sshbuf_put_cstring(b, ext->val)) != 0 || (r = sshbuf_put_stringb(c, b)) != 0) - fatal("%s: buffer: %s", __func__, ssh_err(r)); + fatal_fr(r, "prepare k/v"); } } sshbuf_free(b); @@ -1682,12 +1673,11 @@ int r, i, nkeys; if ((r = sshkey_load_public(path, &public, NULL)) != 0) - fatal("Couldn't load CA public key \"%s\": %s", - path, ssh_err(r)); + fatal_r(r, "Couldn't load CA public key \"%s\"", path); nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys, NULL); - debug3("%s: %d keys", __func__, nkeys); + debug3_f("%d keys", nkeys); if (nkeys <= 0) fatal("cannot read public key from pkcs11"); for (i = 0; i < nkeys; i++) { @@ -1747,13 +1737,11 @@ * agent. */ if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0) - fatal("Cannot load CA public key %s: %s", - tmp, ssh_err(r)); + fatal_r(r, "Cannot load CA public key %s", tmp); if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) - fatal("Cannot use public key for CA signature: %s", - ssh_err(r)); + fatal_r(r, "Cannot use public key for CA signature"); if ((r = ssh_fetch_identitylist(agent_fd, &agent_ids)) != 0) - fatal("Retrieve agent key list: %s", ssh_err(r)); + fatal_r(r, "Retrieve agent key list"); found = 0; for (j = 0; j < agent_ids->nkeys; j++) { if (sshkey_equal(ca, agent_ids->keys[j])) { @@ -1772,7 +1760,7 @@ (ca->sk_flags & SSH_SK_USER_VERIFICATION_REQD)) { if ((pin = read_passphrase("Enter PIN for CA key: ", RP_ALLOW_STDIN)) == NULL) - fatal("%s: couldn't read PIN", __func__); + fatal_f("couldn't read PIN"); } } free(tmp); @@ -1807,16 +1795,14 @@ tmp = tilde_expand_filename(argv[i], pw->pw_uid); if ((r = sshkey_load_public(tmp, &public, &comment)) != 0) - fatal("%s: unable to open \"%s\": %s", - __func__, tmp, ssh_err(r)); + fatal_r(r, "load pubkey \"%s\"", tmp); if (sshkey_is_cert(public)) - fatal("%s: key \"%s\" type %s cannot be certified", - __func__, tmp, sshkey_type(public)); + fatal_f("key \"%s\" type %s cannot be certified", + tmp, sshkey_type(public)); /* Prepare certificate to sign */ if ((r = sshkey_to_certified(public)) != 0) - fatal("Could not upgrade key %s to certificate: %s", - tmp, ssh_err(r)); + fatal_r(r, "Could not upgrade key %s to certificate", tmp); public->cert->type = cert_key_type; public->cert->serial = (u_int64_t)cert_serial; public->cert->key_id = xstrdup(cert_key_id); @@ -1829,14 +1815,13 @@ OPTIONS_EXTENSIONS); if ((r = sshkey_from_private(ca, &public->cert->signature_key)) != 0) - fatal("sshkey_from_private (ca key): %s", ssh_err(r)); + fatal_r(r, "sshkey_from_private (ca key)"); if (agent_fd != -1 && (ca->flags & SSHKEY_FLAG_EXT) != 0) { if ((r = sshkey_certify_custom(public, ca, key_type_name, sk_provider, NULL, agent_signer, &agent_fd)) != 0) - fatal("Couldn't certify key %s via agent: %s", - tmp, ssh_err(r)); + fatal_r(r, "Couldn't certify %s via agent", tmp); } else { if (sshkey_is_sk(ca) && (ca->sk_flags & SSH_SK_USER_PRESENCE_REQD)) { @@ -1848,8 +1833,7 @@ sk_provider, pin); notify_complete(notifier); if (r != 0) - fatal("Couldn't certify key %s: %s", - tmp, ssh_err(r)); + fatal_r(r, "Couldn't certify key %s", tmp); } if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0) @@ -1858,8 +1842,8 @@ free(tmp); if ((r = sshkey_save_public(public, out, comment)) != 0) { - fatal("Unable to save public key to %s: %s", - identity_file, ssh_err(r)); + fatal_r(r, "Unable to save public key to %s", + identity_file); } if (!quiet) { @@ -2019,13 +2003,13 @@ int r; if ((options = sshbuf_fromb(optbuf)) == NULL) - fatal("%s: sshbuf_fromb failed", __func__); + fatal_f("sshbuf_fromb failed"); while (sshbuf_len(options) != 0) { sshbuf_free(option); option = NULL; if ((r = sshbuf_get_cstring(options, &name, NULL)) != 0 || (r = sshbuf_froms(options, &option)) != 0) - fatal("%s: buffer error: %s", __func__, ssh_err(r)); + fatal_fr(r, "parse option"); printf(" %s", name); if (!in_critical && (strcmp(name, "permit-X11-forwarding") == 0 || @@ -2039,8 +2023,7 @@ (strcmp(name, "force-command") == 0 || strcmp(name, "source-address") == 0)) { if ((r = sshbuf_get_cstring(option, &arg, NULL)) != 0) - fatal("%s: buffer error: %s", - __func__, ssh_err(r)); + fatal_fr(r, "parse critical"); printf(" %s\n", arg); free(arg); } else if (sshbuf_len(option) > 0) { @@ -2069,7 +2052,7 @@ ca_fp = sshkey_fingerprint(key->cert->signature_key, fingerprint_hash, SSH_FP_DEFAULT); if (key_fp == NULL || ca_fp == NULL) - fatal("%s: sshkey_fingerprint fail", __func__); + fatal_f("sshkey_fingerprint fail"); sshkey_format_cert_validity(key->cert, valid, sizeof(valid)); printf(" Type: %s %s certificate\n", sshkey_ssh_name(key), @@ -2142,8 +2125,7 @@ if ((key = sshkey_new(KEY_UNSPEC)) == NULL) fatal("sshkey_new"); if ((r = sshkey_read(key, &cp)) != 0) { - error("%s:%lu: invalid key: %s", path, - lnum, ssh_err(r)); + error_r(r, "%s:%lu: invalid key", path, lnum); continue; } if (!sshkey_is_cert(key)) { @@ -2170,11 +2152,11 @@ int r; if ((r = sshbuf_load_file(path, &krlbuf)) != 0) - fatal("Unable to load KRL: %s", ssh_err(r)); + fatal_r(r, "Unable to load KRL %s", path); /* XXX check sigs */ if ((r = ssh_krl_from_blob(krlbuf, krlp, NULL, 0)) != 0 || *krlp == NULL) - fatal("Invalid KRL file: %s", ssh_err(r)); + fatal_r(r, "Invalid KRL file %s", path); sshbuf_free(krlbuf); } @@ -2203,9 +2185,9 @@ tmp[tlen] = '\0'; } if ((b = sshbuf_new()) == NULL) - fatal("%s: sshbuf_new failed", __func__); + fatal_f("sshbuf_new failed"); if ((r = sshbuf_b64tod(b, tmp)) != 0) - fatal("%s:%lu: decode hash failed: %s", file, lnum, ssh_err(r)); + fatal_r(r, "%s:%lu: decode hash failed", file, lnum); free(tmp); *lenp = sshbuf_len(b); *blobp = xmalloc(*lenp); @@ -2291,8 +2273,7 @@ } if (ssh_krl_revoke_cert_by_serial_range(krl, ca, serial, serial2) != 0) { - fatal("%s: revoke serial failed", - __func__); + fatal_f("revoke serial failed"); } } else if (strncasecmp(cp, "id:", 3) == 0) { if (ca == NULL && !wild_ca) { @@ -2302,15 +2283,14 @@ cp += 3; cp = cp + strspn(cp, " \t"); if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0) - fatal("%s: revoke key ID failed", __func__); + fatal_f("revoke key ID failed"); } else if (strncasecmp(cp, "hash:", 5) == 0) { cp += 5; cp = cp + strspn(cp, " \t"); hash_to_blob(cp, &blob, &blen, file, lnum); r = ssh_krl_revoke_key_sha256(krl, blob, blen); if (r != 0) - fatal("%s: revoke key failed: %s", - __func__, ssh_err(r)); + fatal_fr(r, "revoke key failed"); } else { if (strncasecmp(cp, "key:", 4) == 0) { cp += 4; @@ -2332,8 +2312,7 @@ if ((key = sshkey_new(KEY_UNSPEC)) == NULL) fatal("sshkey_new"); if ((r = sshkey_read(key, &cp)) != 0) - fatal("%s:%lu: invalid key: %s", - path, lnum, ssh_err(r)); + fatal_r(r, "%s:%lu: invalid key", path, lnum); if (was_explicit_key) r = ssh_krl_revoke_key_explicit(krl, key); else if (was_sha1) { @@ -2353,8 +2332,7 @@ } else r = ssh_krl_revoke_key(krl, key); if (r != 0) - fatal("%s: revoke key failed: %s", - __func__, ssh_err(r)); + fatal_fr(r, "revoke key failed"); freezero(blob, blen); blob = NULL; blen = 0; @@ -2394,8 +2372,7 @@ else { tmp = tilde_expand_filename(ca_key_path, pw->pw_uid); if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0) - fatal("Cannot load CA public key %s: %s", - tmp, ssh_err(r)); + fatal_r(r, "Cannot load CA public key %s", tmp); free(tmp); } } @@ -2439,8 +2416,7 @@ krl_dump(krl, stdout); for (i = 0; i < argc; i++) { if ((r = sshkey_load_public(argv[i], &k, &comment)) != 0) - fatal("Cannot load public key %s: %s", - argv[i], ssh_err(r)); + fatal_r(r, "Cannot load public key %s", argv[i]); r = ssh_krl_check_key(krl, k); printf("%s%s%s%s: %s\n", argv[i], *comment ? " (" : "", comment, *comment ? ")" : "", @@ -2474,8 +2450,8 @@ strcmp(privpath + plen - slen, suffixes[i]) != 0) continue; privpath[plen - slen] = '\0'; - debug("%s: %s looks like a public key, using private key " - "path %s instead", __func__, keypath, privpath); + debug_f("%s looks like a public key, using private key " + "path %s instead", keypath, privpath); } if ((privkey = load_identity(privpath, NULL)) == NULL) { error("Couldn't load identity %s", keypath); @@ -2492,12 +2468,11 @@ * it capable of signing. */ if ((r = sshkey_to_certified(privkey)) != 0) { - error("%s: sshkey_to_certified: %s", __func__, - ssh_err(r)); + error_fr(r, "sshkey_to_certified"); goto done; } if ((r = sshkey_cert_copy(pubkey, privkey)) != 0) { - error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r)); + error_fr(r, "sshkey_cert_copy"); goto done; } } @@ -2531,12 +2506,12 @@ sshkey_type(signkey)); if ((pin = read_passphrase(prompt, RP_ALLOW_STDIN)) == NULL) - fatal("%s: couldn't read PIN", __func__); + fatal_f("couldn't read PIN"); } if ((signkey->sk_flags & SSH_SK_USER_PRESENCE_REQD)) { if ((fp = sshkey_fingerprint(signkey, fingerprint_hash, SSH_FP_DEFAULT)) == NULL) - fatal("%s: fingerprint failed", __func__); + fatal_f("fingerprint failed"); fprintf(stderr, "Confirm user presence for key %s %s\n", sshkey_type(signkey), fp); free(fp); @@ -2544,15 +2519,15 @@ } if ((r = sshsig_sign_fd(signkey, NULL, sk_provider, pin, fd, sig_namespace, &sigbuf, signer, signer_ctx)) != 0) { - error("Signing %s failed: %s", filename, ssh_err(r)); + error_r(r, "Signing %s failed", filename); goto out; } if ((r = sshsig_armor(sigbuf, &abuf)) != 0) { - error("%s: sshsig_armor: %s", __func__, ssh_err(r)); + error_fr(r, "sshsig_armor"); goto out; } if ((asig = sshbuf_dup_string(abuf)) == NULL) { - error("%s: buffer error", __func__); + error_f("buffer error"); r = SSH_ERR_ALLOC_FAIL; goto out; } @@ -2619,17 +2594,17 @@ } if ((r = sshkey_load_public(keypath, &pubkey, NULL)) != 0) { - error("Couldn't load public key %s: %s", keypath, ssh_err(r)); + error_r(r, "Couldn't load public key %s", keypath); goto done; } if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) - debug("Couldn't get agent socket: %s", ssh_err(r)); + debug_r(r, "Couldn't get agent socket"); else { if ((r = ssh_agent_has_key(agent_fd, pubkey)) == 0) signer = agent_signer; else - debug("Couldn't find key in agent: %s", ssh_err(r)); + debug_r(r, "Couldn't find key in agent"); } if (signer == NULL) { @@ -2685,12 +2660,12 @@ memset(&sig_details, 0, sizeof(sig_details)); if ((r = sshbuf_load_file(signature, &abuf)) != 0) { - error("Couldn't read signature file: %s", ssh_err(r)); + error_r(r, "Couldn't read signature file"); goto done; } if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) { - error("%s: sshsig_armor: %s", __func__, ssh_err(r)); + error_fr(r, "sshsig_armor"); goto done; } if ((r = sshsig_verify_fd(sigbuf, STDIN_FILENO, sig_namespace, @@ -2699,26 +2674,25 @@ if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash, SSH_FP_DEFAULT)) == NULL) - fatal("%s: sshkey_fingerprint failed", __func__); + fatal_f("sshkey_fingerprint failed"); debug("Valid (unverified) signature from key %s", fp); if (sig_details != NULL) { - debug2("%s: signature details: counter = %u, flags = 0x%02x", - __func__, sig_details->sk_counter, sig_details->sk_flags); + debug2_f("signature details: counter = %u, flags = 0x%02x", + sig_details->sk_counter, sig_details->sk_flags); } free(fp); fp = NULL; if (revoked_keys != NULL) { if ((r = sshkey_check_revoked(sign_key, revoked_keys)) != 0) { - debug3("sshkey_check_revoked failed: %s", ssh_err(r)); + debug3_fr(r, "sshkey_check_revoked"); goto done; } } - if (allowed_keys != NULL && - (r = sshsig_check_allowed_keys(allowed_keys, sign_key, - principal, sig_namespace)) != 0) { - debug3("sshsig_check_allowed_keys failed: %s", ssh_err(r)); + if (allowed_keys != NULL && (r = sshsig_check_allowed_keys(allowed_keys, + sign_key, principal, sig_namespace)) != 0) { + debug3_fr(r, "sshsig_check_allowed_keys"); goto done; } /* success */ @@ -2727,10 +2701,8 @@ if (!quiet) { if (ret == 0) { if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash, - SSH_FP_DEFAULT)) == NULL) { - fatal("%s: sshkey_fingerprint failed", - __func__); - } + SSH_FP_DEFAULT)) == NULL) + fatal_f("sshkey_fingerprint failed"); if (principal == NULL) { printf("Good \"%s\" signature with %s key %s\n", sig_namespace, sshkey_type(sign_key), fp); @@ -2760,22 +2732,20 @@ char *principals = NULL, *cp, *tmp; if ((r = sshbuf_load_file(signature, &abuf)) != 0) { - error("Couldn't read signature file: %s", ssh_err(r)); + error_r(r, "Couldn't read signature file"); goto done; } if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) { - error("%s: sshsig_armor: %s", __func__, ssh_err(r)); + error_fr(r, "sshsig_armor"); goto done; } if ((r = sshsig_get_pubkey(sigbuf, &sign_key)) != 0) { - error("%s: sshsig_get_pubkey: %s", - __func__, ssh_err(r)); + error_fr(r, "sshsig_get_pubkey"); goto done; } if ((r = sshsig_find_principals(allowed_keys, sign_key, &principals)) != 0) { - error("%s: sshsig_get_principal: %s", - __func__, ssh_err(r)); + error_fr(r, "sshsig_get_principal"); goto done; } ret = 0; @@ -2975,7 +2945,7 @@ &keys, &nkeys)) != 0) { if (pin != NULL) freezero(pin, strlen(pin)); - error("Unable to load resident keys: %s", ssh_err(r)); + error_r(r, "Unable to load resident keys"); return -1; } if (nkeys == 0) @@ -2992,8 +2962,8 @@ } if ((fp = sshkey_fingerprint(keys[i], fingerprint_hash, SSH_FP_DEFAULT)) == NULL) - fatal("%s: sshkey_fingerprint failed", __func__); - debug("%s: key %zu: %s %s %s (flags 0x%02x)", __func__, i, + fatal_f("sshkey_fingerprint failed"); + debug_f("key %zu: %s %s %s (flags 0x%02x)", i, sshkey_type(keys[i]), fp, keys[i]->sk_application, keys[i]->sk_flags); ext = skip_ssh_url_preamble(keys[i]->sk_application); @@ -3013,8 +2983,7 @@ if ((r = sshkey_save_private(keys[i], path, pass, keys[i]->sk_application, private_key_format, openssh_format_cipher, rounds)) != 0) { - error("Saving key \"%s\" failed: %s", - path, ssh_err(r)); + error_r(r, "Saving key \"%s\" failed", path); free(path); break; } @@ -3031,8 +3000,7 @@ free(path); if ((r = sshkey_save_public(keys[i], pubpath, keys[i]->sk_application)) != 0) { - error("Saving public key \"%s\" failed: %s", - pubpath, ssh_err(r)); + error_r(r, "Saving public key \"%s\" failed", pubpath); free(pubpath); break; } @@ -3050,6 +3018,26 @@ } static void +save_attestation(struct sshbuf *attest, const char *path) +{ + mode_t omask; + int r; + + if (path == NULL) + return; /* nothing to do */ + if (attest == NULL || sshbuf_len(attest) == 0) + fatal("Enrollment did not return attestation data"); + omask = umask(077); + r = sshbuf_write_file(path, attest); + umask(omask); + if (r != 0) + fatal_r(r, "Unable to write attestation data \"%s\"", path); + if (!quiet) + printf("Your FIDO attestation certificate has been saved in " + "%s\n", path); +} + +static void usage(void) { fprintf(stderr, @@ -3115,7 +3103,7 @@ unsigned long long cert_serial = 0; char *identity_comment = NULL, *ca_key_path = NULL, **opts = NULL; char *sk_application = NULL, *sk_device = NULL, *sk_user = NULL; - char *sk_attestaion_path = NULL; + char *sk_attestation_path = NULL; struct sshbuf *challenge = NULL, *attest = NULL; size_t i, nopts = 0; u_int32_t bits = 0; @@ -3562,13 +3550,13 @@ } else if (strncasecmp(opts[i], "challenge=", 10) == 0) { if ((r = sshbuf_load_file(opts[i] + 10, &challenge)) != 0) { - fatal("Unable to load FIDO enrollment " - "challenge \"%s\": %s", - opts[i] + 10, ssh_err(r)); + fatal_r(r, "Unable to load FIDO " + "enrollment challenge \"%s\"", + opts[i] + 10); } } else if (strncasecmp(opts[i], "write-attestation=", 18) == 0) { - sk_attestaion_path = opts[i] + 18; + sk_attestation_path = opts[i] + 18; } else if (strncasecmp(opts[i], "application=", 12) == 0) { sk_application = xstrdup(opts[i] + 12); @@ -3603,7 +3591,7 @@ if (r == 0) break; if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) - fatal("Key enrollment failed: %s", ssh_err(r)); + fatal_r(r, "Key enrollment failed"); else if (passphrase != NULL) { error("PIN incorrect"); freezero(passphrase, strlen(passphrase)); @@ -3630,7 +3618,7 @@ break; } if ((r = sshkey_from_private(private, &public)) != 0) - fatal("sshkey_from_private failed: %s\n", ssh_err(r)); + fatal_r(r, "sshkey_from_private"); if (!have_identity) ask_filename(pw, "Enter file in which to save the key"); @@ -3654,8 +3642,7 @@ /* Save the key with the given passphrase and comment. */ if ((r = sshkey_save_private(private, identity_file, passphrase, comment, private_key_format, openssh_format_cipher, rounds)) != 0) { - error("Saving key \"%s\" failed: %s", - identity_file, ssh_err(r)); + error_r(r, "Saving key \"%s\" failed", identity_file); freezero(passphrase, strlen(passphrase)); exit(1); } @@ -3668,10 +3655,8 @@ } strlcat(identity_file, ".pub", sizeof(identity_file)); - if ((r = sshkey_save_public(public, identity_file, comment)) != 0) { - fatal("Unable to save public key to %s: %s", - identity_file, ssh_err(r)); - } + if ((r = sshkey_save_public(public, identity_file, comment)) != 0) + fatal_r(r, "Unable to save public key to %s", identity_file); if (!quiet) { fp = sshkey_fingerprint(public, fingerprint_hash, @@ -3690,20 +3675,9 @@ free(fp); } - if (sk_attestaion_path != NULL) { - if (attest == NULL || sshbuf_len(attest) == 0) { - fatal("Enrollment did not return attestation " - "certificate"); - } - if ((r = sshbuf_write_file(sk_attestaion_path, attest)) != 0) { - fatal("Unable to write attestation certificate " - "\"%s\": %s", sk_attestaion_path, ssh_err(r)); - } - if (!quiet) { - printf("Your FIDO attestation certificate has been " - "saved in %s\n", sk_attestaion_path); - } - } + if (sk_attestation_path != NULL) + save_attestation(attest, sk_attestation_path); + sshbuf_free(attest); sshkey_free(public);