summaryrefslogtreecommitdiff
path: root/ssh-keygen.c
diff options
context:
space:
mode:
Diffstat (limited to 'ssh-keygen.c')
-rw-r--r--ssh-keygen.c67
1 files changed, 18 insertions, 49 deletions
diff --git a/ssh-keygen.c b/ssh-keygen.c
index d7974f3dc..8c23a54cf 100644
--- a/ssh-keygen.c
+++ b/ssh-keygen.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssh-keygen.c,v 1.408 2020/05/01 04:23:11 djm Exp $ */ 1/* $OpenBSD: ssh-keygen.c,v 1.409 2020/05/02 07:19:43 djm Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -1057,7 +1057,6 @@ do_gen_all_hostkeys(struct passwd *pw)
1057 struct sshkey *private, *public; 1057 struct sshkey *private, *public;
1058 char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file; 1058 char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file;
1059 int i, type, fd, r; 1059 int i, type, fd, r;
1060 FILE *f;
1061 1060
1062 for (i = 0; key_types[i].key_type; i++) { 1061 for (i = 0; key_types[i].key_type; i++) {
1063 public = private = NULL; 1062 public = private = NULL;
@@ -1095,11 +1094,11 @@ do_gen_all_hostkeys(struct passwd *pw)
1095 fflush(stdout); 1094 fflush(stdout);
1096 type = sshkey_type_from_name(key_types[i].key_type); 1095 type = sshkey_type_from_name(key_types[i].key_type);
1097 if ((fd = mkstemp(prv_tmp)) == -1) { 1096 if ((fd = mkstemp(prv_tmp)) == -1) {
1098 error("Could not save your public key in %s: %s", 1097 error("Could not save your private key in %s: %s",
1099 prv_tmp, strerror(errno)); 1098 prv_tmp, strerror(errno));
1100 goto failnext; 1099 goto failnext;
1101 } 1100 }
1102 close(fd); /* just using mkstemp() to generate/reserve a name */ 1101 (void)close(fd); /* just using mkstemp() to reserve a name */
1103 bits = 0; 1102 bits = 0;
1104 type_bits_valid(type, NULL, &bits); 1103 type_bits_valid(type, NULL, &bits);
1105 if ((r = sshkey_generate(type, bits, &private)) != 0) { 1104 if ((r = sshkey_generate(type, bits, &private)) != 0) {
@@ -1123,25 +1122,10 @@ do_gen_all_hostkeys(struct passwd *pw)
1123 goto failnext; 1122 goto failnext;
1124 } 1123 }
1125 (void)fchmod(fd, 0644); 1124 (void)fchmod(fd, 0644);
1126 f = fdopen(fd, "w"); 1125 (void)close(fd);
1127 if (f == NULL) { 1126 if ((r = sshkey_save_public(public, pub_tmp, comment)) != 0) {
1128 error("fdopen %s failed: %s", pub_tmp, strerror(errno)); 1127 fatal("Unable to save public key to %s: %s",
1129 close(fd); 1128 identity_file, ssh_err(r));
1130 goto failnext;
1131 }
1132 if ((r = sshkey_write(public, f)) != 0) {
1133 error("write key failed: %s", ssh_err(r));
1134 fclose(f);
1135 goto failnext;
1136 }
1137 fprintf(f, " %s\n", comment);
1138 if (ferror(f) != 0) {
1139 error("write key failed: %s", strerror(errno));
1140 fclose(f);
1141 goto failnext;
1142 }
1143 if (fclose(f) != 0) {
1144 error("key close failed: %s", strerror(errno));
1145 goto failnext; 1129 goto failnext;
1146 } 1130 }
1147 1131
@@ -1518,8 +1502,7 @@ do_change_comment(struct passwd *pw, const char *identity_comment)
1518 struct sshkey *private; 1502 struct sshkey *private;
1519 struct sshkey *public; 1503 struct sshkey *public;
1520 struct stat st; 1504 struct stat st;
1521 FILE *f; 1505 int r;
1522 int r, fd;
1523 1506
1524 if (!have_identity) 1507 if (!have_identity)
1525 ask_filename(pw, "Enter file in which the key is"); 1508 ask_filename(pw, "Enter file in which the key is");
@@ -1598,18 +1581,11 @@ do_change_comment(struct passwd *pw, const char *identity_comment)
1598 sshkey_free(private); 1581 sshkey_free(private);
1599 1582
1600 strlcat(identity_file, ".pub", sizeof(identity_file)); 1583 strlcat(identity_file, ".pub", sizeof(identity_file));
1601 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); 1584 if ((r = sshkey_save_public(public, identity_file, new_comment)) != 0) {
1602 if (fd == -1) 1585 fatal("Unable to save public key to %s: %s",
1603 fatal("Could not save your public key in %s", identity_file); 1586 identity_file, ssh_err(r));
1604 f = fdopen(fd, "w"); 1587 }
1605 if (f == NULL)
1606 fatal("fdopen %s failed: %s", identity_file, strerror(errno));
1607 if ((r = sshkey_write(public, f)) != 0)
1608 fatal("write key failed: %s", ssh_err(r));
1609 sshkey_free(public); 1588 sshkey_free(public);
1610 fprintf(f, " %s\n", new_comment);
1611 fclose(f);
1612
1613 free(comment); 1589 free(comment);
1614 1590
1615 if (strlen(new_comment) > 0) 1591 if (strlen(new_comment) > 0)
@@ -1741,12 +1717,11 @@ do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
1741 unsigned long long cert_serial, int cert_serial_autoinc, 1717 unsigned long long cert_serial, int cert_serial_autoinc,
1742 int argc, char **argv) 1718 int argc, char **argv)
1743{ 1719{
1744 int r, i, fd, found, agent_fd = -1; 1720 int r, i, found, agent_fd = -1;
1745 u_int n; 1721 u_int n;
1746 struct sshkey *ca, *public; 1722 struct sshkey *ca, *public;
1747 char valid[64], *otmp, *tmp, *cp, *out, *comment; 1723 char valid[64], *otmp, *tmp, *cp, *out, *comment;
1748 char *ca_fp = NULL, **plist = NULL; 1724 char *ca_fp = NULL, **plist = NULL;
1749 FILE *f;
1750 struct ssh_identitylist *agent_ids; 1725 struct ssh_identitylist *agent_ids;
1751 size_t j; 1726 size_t j;
1752 struct notifier_ctx *notifier = NULL; 1727 struct notifier_ctx *notifier = NULL;
@@ -1869,16 +1844,10 @@ do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
1869 xasprintf(&out, "%s-cert.pub", tmp); 1844 xasprintf(&out, "%s-cert.pub", tmp);
1870 free(tmp); 1845 free(tmp);
1871 1846
1872 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) 1847 if ((r = sshkey_save_public(public, out, comment)) != 0) {
1873 fatal("Could not open \"%s\" for writing: %s", out, 1848 fatal("Unable to save public key to %s: %s",
1874 strerror(errno)); 1849 identity_file, ssh_err(r));
1875 if ((f = fdopen(fd, "w")) == NULL) 1850 }
1876 fatal("%s: fdopen: %s", __func__, strerror(errno));
1877 if ((r = sshkey_write(public, f)) != 0)
1878 fatal("Could not write certified key to %s: %s",
1879 out, ssh_err(r));
1880 fprintf(f, " %s\n", comment);
1881 fclose(f);
1882 1851
1883 if (!quiet) { 1852 if (!quiet) {
1884 sshkey_format_cert_validity(public->cert, 1853 sshkey_format_cert_validity(public->cert,
@@ -3680,7 +3649,7 @@ main(int argc, char **argv)
3680 strlcat(identity_file, ".pub", sizeof(identity_file)); 3649 strlcat(identity_file, ".pub", sizeof(identity_file));
3681 if ((r = sshkey_save_public(public, identity_file, comment)) != 0) { 3650 if ((r = sshkey_save_public(public, identity_file, comment)) != 0) {
3682 fatal("Unable to save public key to %s: %s", 3651 fatal("Unable to save public key to %s: %s",
3683 identity_file, strerror(errno)); 3652 identity_file, ssh_err(r));
3684 } 3653 }
3685 3654
3686 if (!quiet) { 3655 if (!quiet) {