summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Miller <djm@cvs.openbsd.org>2020-05-02 07:19:44 +0000
committerDamien Miller <djm@cvs.openbsd.org>2020-05-02 07:19:44 +0000
commit15874a26b83d86ec7e03b2258514b8206ba377eb (patch)
tree995e3e1c9a0a62082623fe745a1531ca8bc26e6a
parentd59f6f96219aaf0db8324a26b289ca35466ecbb2 (diff)
we have a sshkey_save_public() function to save public keys; use it
and save a bunch of redundant code. Patch from loic AT venez.fr; ok markus@ djm@
-rw-r--r--usr.bin/ssh/ssh-keygen.c67
1 files changed, 18 insertions, 49 deletions
diff --git a/usr.bin/ssh/ssh-keygen.c b/usr.bin/ssh/ssh-keygen.c
index 38f877a5e18..02cdd276fc9 100644
--- a/usr.bin/ssh/ssh-keygen.c
+++ b/usr.bin/ssh/ssh-keygen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssh-keygen.c,v 1.408 2020/05/01 04:23:11 djm Exp $ */
+/* $OpenBSD: ssh-keygen.c,v 1.409 2020/05/02 07:19:43 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -1035,7 +1035,6 @@ do_gen_all_hostkeys(struct passwd *pw)
struct sshkey *private, *public;
char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file;
int i, type, fd, r;
- FILE *f;
for (i = 0; key_types[i].key_type; i++) {
public = private = NULL;
@@ -1073,11 +1072,11 @@ do_gen_all_hostkeys(struct passwd *pw)
fflush(stdout);
type = sshkey_type_from_name(key_types[i].key_type);
if ((fd = mkstemp(prv_tmp)) == -1) {
- error("Could not save your public key in %s: %s",
+ error("Could not save your private key in %s: %s",
prv_tmp, strerror(errno));
goto failnext;
}
- close(fd); /* just using mkstemp() to generate/reserve a name */
+ (void)close(fd); /* just using mkstemp() to reserve a name */
bits = 0;
type_bits_valid(type, NULL, &bits);
if ((r = sshkey_generate(type, bits, &private)) != 0) {
@@ -1101,25 +1100,10 @@ do_gen_all_hostkeys(struct passwd *pw)
goto failnext;
}
(void)fchmod(fd, 0644);
- f = fdopen(fd, "w");
- if (f == NULL) {
- error("fdopen %s failed: %s", pub_tmp, strerror(errno));
- close(fd);
- goto failnext;
- }
- if ((r = sshkey_write(public, f)) != 0) {
- error("write key failed: %s", ssh_err(r));
- fclose(f);
- goto failnext;
- }
- fprintf(f, " %s\n", comment);
- if (ferror(f) != 0) {
- error("write key failed: %s", strerror(errno));
- fclose(f);
- goto failnext;
- }
- if (fclose(f) != 0) {
- error("key close failed: %s", strerror(errno));
+ (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));
goto failnext;
}
@@ -1496,8 +1480,7 @@ do_change_comment(struct passwd *pw, const char *identity_comment)
struct sshkey *private;
struct sshkey *public;
struct stat st;
- FILE *f;
- int r, fd;
+ int r;
if (!have_identity)
ask_filename(pw, "Enter file in which the key is");
@@ -1576,18 +1559,11 @@ do_change_comment(struct passwd *pw, const char *identity_comment)
sshkey_free(private);
strlcat(identity_file, ".pub", sizeof(identity_file));
- fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (fd == -1)
- fatal("Could not save your public key in %s", identity_file);
- f = fdopen(fd, "w");
- if (f == NULL)
- fatal("fdopen %s failed: %s", identity_file, strerror(errno));
- if ((r = sshkey_write(public, f)) != 0)
- fatal("write key failed: %s", ssh_err(r));
+ 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));
+ }
sshkey_free(public);
- fprintf(f, " %s\n", new_comment);
- fclose(f);
-
free(comment);
if (strlen(new_comment) > 0)
@@ -1719,12 +1695,11 @@ do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
unsigned long long cert_serial, int cert_serial_autoinc,
int argc, char **argv)
{
- int r, i, fd, found, agent_fd = -1;
+ int r, i, found, agent_fd = -1;
u_int n;
struct sshkey *ca, *public;
char valid[64], *otmp, *tmp, *cp, *out, *comment;
char *ca_fp = NULL, **plist = NULL;
- FILE *f;
struct ssh_identitylist *agent_ids;
size_t j;
struct notifier_ctx *notifier = NULL;
@@ -1847,16 +1822,10 @@ do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
xasprintf(&out, "%s-cert.pub", tmp);
free(tmp);
- if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
- fatal("Could not open \"%s\" for writing: %s", out,
- strerror(errno));
- if ((f = fdopen(fd, "w")) == NULL)
- fatal("%s: fdopen: %s", __func__, strerror(errno));
- if ((r = sshkey_write(public, f)) != 0)
- fatal("Could not write certified key to %s: %s",
- out, ssh_err(r));
- fprintf(f, " %s\n", comment);
- fclose(f);
+ if ((r = sshkey_save_public(public, out, comment)) != 0) {
+ fatal("Unable to save public key to %s: %s",
+ identity_file, ssh_err(r));
+ }
if (!quiet) {
sshkey_format_cert_validity(public->cert,
@@ -3655,7 +3624,7 @@ main(int argc, char **argv)
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, strerror(errno));
+ identity_file, ssh_err(r));
}
if (!quiet) {