blob: b6b6900516758c9a8262ebd0bdd7dd002e4b1ef7 [file] [log] [blame]
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001/* $OpenBSD: ssh-keygen.c,v 1.305 2017/06/28 01:09:22 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
Damien Miller72ef7c12015-01-15 02:21:31 +110021#ifdef WITH_OPENSSL
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Miller72ef7c12015-01-15 02:21:31 +110025#endif
Damien Millereba71ba2000-04-29 23:57:08 +100026
Darren Tucker39972492006-07-12 22:22:46 +100027#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100028#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100029#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100030#ifdef HAVE_PATHS_H
31# include <paths.h>
32#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100033#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100034#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100035#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100036#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100038#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000039#include <limits.h>
djm@openbsd.orga287c5a2017-02-10 03:36:40 +000040#include <locale.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100041
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042#include "xmalloc.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000043#include "sshkey.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000044#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100045#include "authfile.h"
46#include "uuencode.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000047#include "sshbuf.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "pathnames.h"
49#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100050#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110051#include "match.h"
52#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100053#include "dns.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110054#include "ssh.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110055#include "ssh2.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000056#include "ssherr.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110057#include "ssh-pkcs11.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110058#include "atomicio.h"
59#include "krl.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000060#include "digest.h"
djm@openbsd.orga287c5a2017-02-10 03:36:40 +000061#include "utf8.h"
djm@openbsd.orga98339e2017-06-28 01:09:22 +000062#include "authfd.h"
Ben Lindstromcd392282001-07-04 03:44:03 +000063
djm@openbsd.orgd1958792015-05-28 04:40:13 +000064#ifdef WITH_OPENSSL
65# define DEFAULT_KEY_TYPE_NAME "rsa"
66#else
67# define DEFAULT_KEY_TYPE_NAME "ed25519"
68#endif
69
Damien Miller3f54a9f2005-11-05 14:52:18 +110070/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
71#define DEFAULT_BITS 2048
72#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100073#define DEFAULT_BITS_ECDSA 256
Damien Miller3f54a9f2005-11-05 14:52:18 +110074u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller5428f641999-11-25 11:54:57 +110076/*
77 * Flag indicating that we just want to change the passphrase. This can be
78 * set on the command line.
79 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080int change_passphrase = 0;
81
Damien Miller5428f641999-11-25 11:54:57 +110082/*
83 * Flag indicating that we just want to change the comment. This can be set
84 * on the command line.
85 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086int change_comment = 0;
87
88int quiet = 0;
89
Darren Tucker35c45532008-06-13 04:43:15 +100090int log_level = SYSLOG_LEVEL_INFO;
91
Damien Miller4b42d7f2005-03-01 21:48:35 +110092/* Flag indicating that we want to hash a known_hosts file */
93int hash_hosts = 0;
94/* Flag indicating that we want lookup a host in known_hosts file */
95int find_host = 0;
96/* Flag indicating that we want to delete a host from a known_hosts file */
97int delete_host = 0;
98
Damien Millerf2b70ca2010-03-05 07:39:35 +110099/* Flag indicating that we want to show the contents of a certificate */
100int show_cert = 0;
101
Damien Miller10f6f6b1999-11-17 17:29:08 +1100102/* Flag indicating that we just want to see the key fingerprint */
103int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000104int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100105
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000106/* Hash algorithm to use for fingerprints. */
107int fingerprint_hash = SSH_FP_HASH_DEFAULT;
108
Damien Miller431f66b1999-11-21 18:31:57 +1100109/* The identity file name, given on the command line or entered by the user. */
110char identity_file[1024];
111int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
113/* This is set to the passphrase if given on the command line. */
114char *identity_passphrase = NULL;
115
116/* This is set to the new passphrase if given on the command line. */
117char *identity_new_passphrase = NULL;
118
119/* This is set to the new comment if given on the command line. */
120char *identity_comment = NULL;
121
Damien Miller0a80ca12010-02-27 07:55:05 +1100122/* Path to CA key when certifying keys. */
123char *ca_key_path = NULL;
124
djm@openbsd.orga98339e2017-06-28 01:09:22 +0000125/* Prefer to use agent keys for CA signing */
126int prefer_agent = 0;
127
Damien Miller4e270b02010-04-16 15:56:21 +1000128/* Certificate serial number */
Damien Miller55aca022012-12-03 11:25:30 +1100129unsigned long long cert_serial = 0;
Damien Miller4e270b02010-04-16 15:56:21 +1000130
Damien Miller0a80ca12010-02-27 07:55:05 +1100131/* Key type when certifying */
132u_int cert_key_type = SSH2_CERT_TYPE_USER;
133
134/* "key ID" of signed key */
135char *cert_key_id = NULL;
136
137/* Comma-separated list of principal names for certifying keys */
138char *cert_principals = NULL;
139
140/* Validity period for certificates */
141u_int64_t cert_valid_from = 0;
142u_int64_t cert_valid_to = ~0ULL;
143
Damien Miller4e270b02010-04-16 15:56:21 +1000144/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000145#define CERTOPT_X_FWD (1)
146#define CERTOPT_AGENT_FWD (1<<1)
147#define CERTOPT_PORT_FWD (1<<2)
148#define CERTOPT_PTY (1<<3)
149#define CERTOPT_USER_RC (1<<4)
150#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
151 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
152u_int32_t certflags_flags = CERTOPT_DEFAULT;
153char *certflags_command = NULL;
154char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100155
djm@openbsd.org249516e2017-04-29 04:12:25 +0000156/* Arbitrary extensions specified by user */
157struct cert_userext {
158 char *key;
159 char *val;
160 int crit;
161};
162struct cert_userext *cert_userext;
163size_t ncert_userext;
164
Damien Miller44b25042010-07-02 13:35:01 +1000165/* Conversion to/from various formats */
166int convert_to = 0;
167int convert_from = 0;
168enum {
169 FMT_RFC4716,
170 FMT_PKCS8,
171 FMT_PEM
172} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000173int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000174int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100175
Damien Millera41c8b12002-01-22 23:05:08 +1100176char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000177
Damien Miller757f34e2010-08-05 13:05:31 +1000178/* Load key from this PKCS#11 provider */
179char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000180
Damien Millerbcd00ab2013-12-07 10:41:55 +1100181/* Use new OpenSSH private key format when writing SSH2 keys instead of PEM */
182int use_new_format = 0;
183
184/* Cipher for new-format private keys */
185char *new_format_cipher = NULL;
186
187/*
188 * Number of KDF rounds to derive new format keys /
189 * number of primality trials when screening moduli.
190 */
191int rounds = 0;
192
Damien Miller431f66b1999-11-21 18:31:57 +1100193/* argv0 */
194extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Millere5c0d522014-07-03 21:24:19 +1000196char hostname[NI_MAXHOST];
Damien Millereba71ba2000-04-29 23:57:08 +1000197
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +0000198#ifdef WITH_OPENSSL
Darren Tucker770fc012004-05-13 16:24:32 +1000199/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000200int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Damien Millerdfceafe2012-07-06 13:44:19 +1000201int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
202 unsigned long);
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +0000203#endif
Darren Tucker770fc012004-05-13 16:24:32 +1000204
Ben Lindstrombba81212001-06-25 05:01:22 +0000205static void
djm@openbsd.org7efb4552015-01-18 13:22:28 +0000206type_bits_valid(int type, const char *name, u_int32_t *bitsp)
Damien Miller58f1baf2011-05-05 14:06:15 +1000207{
Damien Miller72ef7c12015-01-15 02:21:31 +1100208#ifdef WITH_OPENSSL
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +0000209 u_int maxbits, nid;
Damien Miller72ef7c12015-01-15 02:21:31 +1100210#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000211
djm@openbsd.org3038a192015-04-17 13:19:22 +0000212 if (type == KEY_UNSPEC)
213 fatal("unknown key type %s", key_type_name);
Damien Miller884b63a2011-05-05 14:14:52 +1000214 if (*bitsp == 0) {
Damien Miller773dda22015-01-30 23:10:17 +1100215#ifdef WITH_OPENSSL
Damien Miller58f1baf2011-05-05 14:06:15 +1000216 if (type == KEY_DSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000217 *bitsp = DEFAULT_BITS_DSA;
djm@openbsd.org7efb4552015-01-18 13:22:28 +0000218 else if (type == KEY_ECDSA) {
219 if (name != NULL &&
220 (nid = sshkey_ecdsa_nid_from_name(name)) > 0)
221 *bitsp = sshkey_curve_nid_to_bits(nid);
222 if (*bitsp == 0)
223 *bitsp = DEFAULT_BITS_ECDSA;
Damien Miller773dda22015-01-30 23:10:17 +1100224 } else
225#endif
Damien Miller884b63a2011-05-05 14:14:52 +1000226 *bitsp = DEFAULT_BITS;
Damien Miller58f1baf2011-05-05 14:06:15 +1000227 }
Damien Miller72ef7c12015-01-15 02:21:31 +1100228#ifdef WITH_OPENSSL
Damien Miller58f1baf2011-05-05 14:06:15 +1000229 maxbits = (type == KEY_DSA) ?
230 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
djm@openbsd.org3038a192015-04-17 13:19:22 +0000231 if (*bitsp > maxbits)
232 fatal("key bits exceeds maximum %d", maxbits);
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000233 switch (type) {
234 case KEY_DSA:
235 if (*bitsp != 1024)
236 fatal("Invalid DSA key length: must be 1024 bits");
237 break;
238 case KEY_RSA:
239 if (*bitsp < SSH_RSA_MINIMUM_MODULUS_SIZE)
240 fatal("Invalid RSA key length: minimum is %d bits",
241 SSH_RSA_MINIMUM_MODULUS_SIZE);
242 break;
243 case KEY_ECDSA:
244 if (sshkey_ecdsa_bits_to_nid(*bitsp) == -1)
245 fatal("Invalid ECDSA key length: valid lengths are "
246 "256, 384 or 521 bits");
247 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000248#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000249}
250
251static void
Damien Miller431f66b1999-11-21 18:31:57 +1100252ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253{
Damien Miller95def091999-11-25 00:26:21 +1100254 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100255 char *name = NULL;
256
Damien Miller993dd552002-02-19 15:22:47 +1100257 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000258 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100259 else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000260 switch (sshkey_type_from_name(key_type_name)) {
Damien Miller4e270b02010-04-16 15:56:21 +1000261 case KEY_DSA_CERT:
Damien Miller993dd552002-02-19 15:22:47 +1100262 case KEY_DSA:
263 name = _PATH_SSH_CLIENT_ID_DSA;
264 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100265#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000266 case KEY_ECDSA_CERT:
267 case KEY_ECDSA:
268 name = _PATH_SSH_CLIENT_ID_ECDSA;
269 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100270#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000271 case KEY_RSA_CERT:
Damien Miller993dd552002-02-19 15:22:47 +1100272 case KEY_RSA:
273 name = _PATH_SSH_CLIENT_ID_RSA;
274 break;
Damien Miller5be9d9e2013-12-07 11:24:01 +1100275 case KEY_ED25519:
276 case KEY_ED25519_CERT:
277 name = _PATH_SSH_CLIENT_ID_ED25519;
278 break;
Damien Miller993dd552002-02-19 15:22:47 +1100279 default:
djm@openbsd.org3038a192015-04-17 13:19:22 +0000280 fatal("bad key type");
Damien Miller993dd552002-02-19 15:22:47 +1100281 }
Damien Miller90967402006-03-26 14:07:26 +1100282 }
djm@openbsd.org3038a192015-04-17 13:19:22 +0000283 snprintf(identity_file, sizeof(identity_file),
284 "%s/%s", pw->pw_dir, name);
285 printf("%s (%s): ", prompt, identity_file);
286 fflush(stdout);
Damien Miller95def091999-11-25 00:26:21 +1100287 if (fgets(buf, sizeof(buf), stdin) == NULL)
288 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000289 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100290 if (strcmp(buf, "") != 0)
291 strlcpy(identity_file, buf, sizeof(identity_file));
292 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100293}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000295static struct sshkey *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000296load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000297{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000298 char *pass;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000299 struct sshkey *prv;
300 int r;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000301
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000302 if ((r = sshkey_load_private(filename, "", &prv, NULL)) == 0)
303 return prv;
304 if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
305 fatal("Load key \"%s\": %s", filename, ssh_err(r));
306 if (identity_passphrase)
307 pass = xstrdup(identity_passphrase);
308 else
309 pass = read_passphrase("Enter passphrase: ", RP_ALLOW_STDIN);
310 r = sshkey_load_private(filename, pass, &prv, NULL);
311 explicit_bzero(pass, strlen(pass));
312 free(pass);
313 if (r != 0)
314 fatal("Load key \"%s\": %s", filename, ssh_err(r));
Ben Lindstromd0fca422001-03-26 13:44:06 +0000315 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000316}
317
Damien Miller874d77b2000-10-14 16:23:11 +1100318#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000319#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100320#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000321#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000322
Damien Miller1f0311c2014-05-15 14:24:09 +1000323#ifdef WITH_OPENSSL
Ben Lindstrombba81212001-06-25 05:01:22 +0000324static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000325do_convert_to_ssh2(struct passwd *pw, struct sshkey *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000326{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000327 size_t len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000328 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100329 char comment[61];
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000330 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000331
djm@openbsd.org3038a192015-04-17 13:19:22 +0000332 if ((r = sshkey_to_blob(k, &blob, &len)) != 0)
333 fatal("key_to_blob failed: %s", ssh_err(r));
Darren Tuckerd04758d2010-01-12 19:41:57 +1100334 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
335 snprintf(comment, sizeof(comment),
336 "%u-bit %s, converted by %s@%s from OpenSSH",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000337 sshkey_size(k), sshkey_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000338 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100339
340 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
341 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000342 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100343 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000344 sshkey_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000345 free(blob);
Damien Millereba71ba2000-04-29 23:57:08 +1000346 exit(0);
347}
348
Ben Lindstrombba81212001-06-25 05:01:22 +0000349static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000350do_convert_to_pkcs8(struct sshkey *k)
Damien Miller44b25042010-07-02 13:35:01 +1000351{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000352 switch (sshkey_type_plain(k->type)) {
Damien Miller44b25042010-07-02 13:35:01 +1000353 case KEY_RSA:
354 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
355 fatal("PEM_write_RSA_PUBKEY failed");
356 break;
357 case KEY_DSA:
358 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
359 fatal("PEM_write_DSA_PUBKEY failed");
360 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000361#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000362 case KEY_ECDSA:
363 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
364 fatal("PEM_write_EC_PUBKEY failed");
365 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000366#endif
Damien Miller44b25042010-07-02 13:35:01 +1000367 default:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000368 fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000369 }
370 exit(0);
371}
372
373static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000374do_convert_to_pem(struct sshkey *k)
Damien Miller44b25042010-07-02 13:35:01 +1000375{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000376 switch (sshkey_type_plain(k->type)) {
Damien Miller44b25042010-07-02 13:35:01 +1000377 case KEY_RSA:
378 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
379 fatal("PEM_write_RSAPublicKey failed");
380 break;
381#if notyet /* OpenSSH 0.9.8 lacks this function */
382 case KEY_DSA:
383 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
384 fatal("PEM_write_DSAPublicKey failed");
385 break;
386#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000387 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000388 default:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000389 fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000390 }
391 exit(0);
392}
393
394static void
395do_convert_to(struct passwd *pw)
396{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000397 struct sshkey *k;
Damien Miller44b25042010-07-02 13:35:01 +1000398 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000399 int r;
Damien Miller44b25042010-07-02 13:35:01 +1000400
401 if (!have_identity)
402 ask_filename(pw, "Enter file in which the key is");
403 if (stat(identity_file, &st) < 0)
404 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000405 if ((r = sshkey_load_public(identity_file, &k, NULL)) != 0)
406 k = load_identity(identity_file);
Damien Miller44b25042010-07-02 13:35:01 +1000407 switch (convert_format) {
408 case FMT_RFC4716:
409 do_convert_to_ssh2(pw, k);
410 break;
411 case FMT_PKCS8:
412 do_convert_to_pkcs8(k);
413 break;
414 case FMT_PEM:
415 do_convert_to_pem(k);
416 break;
417 default:
418 fatal("%s: unknown key format %d", __func__, convert_format);
419 }
420 exit(0);
421}
422
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000423/*
424 * This is almost exactly the bignum1 encoding, but with 32 bit for length
425 * instead of 16.
426 */
Damien Miller44b25042010-07-02 13:35:01 +1000427static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000428buffer_get_bignum_bits(struct sshbuf *b, BIGNUM *value)
Damien Miller874d77b2000-10-14 16:23:11 +1100429{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000430 u_int bytes, bignum_bits;
431 int r;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000432
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000433 if ((r = sshbuf_get_u32(b, &bignum_bits)) != 0)
434 fatal("%s: buffer error: %s", __func__, ssh_err(r));
435 bytes = (bignum_bits + 7) / 8;
436 if (sshbuf_len(b) < bytes)
437 fatal("%s: input buffer too small: need %d have %zu",
438 __func__, bytes, sshbuf_len(b));
439 if (BN_bin2bn(sshbuf_ptr(b), bytes, value) == NULL)
440 fatal("%s: BN_bin2bn failed", __func__);
441 if ((r = sshbuf_consume(b, bytes)) != 0)
442 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100443}
444
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000445static struct sshkey *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000446do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100447{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000448 struct sshbuf *b;
449 struct sshkey *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100450 char *type, *cipher;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000451 u_char e1, e2, e3, *sig = NULL, data[] = "abcde12345";
452 int r, rlen, ktype;
453 u_int magic, i1, i2, i3, i4;
454 size_t slen;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000455 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100456
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000457 if ((b = sshbuf_from(blob, blen)) == NULL)
458 fatal("%s: sshbuf_from failed", __func__);
459 if ((r = sshbuf_get_u32(b, &magic)) != 0)
460 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100461
Damien Miller874d77b2000-10-14 16:23:11 +1100462 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000463 error("bad magic 0x%x != 0x%x", magic,
464 SSH_COM_PRIVATE_KEY_MAGIC);
465 sshbuf_free(b);
Damien Miller874d77b2000-10-14 16:23:11 +1100466 return NULL;
467 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000468 if ((r = sshbuf_get_u32(b, &i1)) != 0 ||
469 (r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
470 (r = sshbuf_get_cstring(b, &cipher, NULL)) != 0 ||
471 (r = sshbuf_get_u32(b, &i2)) != 0 ||
472 (r = sshbuf_get_u32(b, &i3)) != 0 ||
473 (r = sshbuf_get_u32(b, &i4)) != 0)
474 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller80163902007-01-05 16:30:16 +1100475 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100476 if (strcmp(cipher, "none") != 0) {
477 error("unsupported cipher %s", cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000478 free(cipher);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000479 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +1000480 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100481 return NULL;
482 }
Darren Tuckera627d422013-06-02 07:31:17 +1000483 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100484
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000485 if (strstr(type, "dsa")) {
486 ktype = KEY_DSA;
487 } else if (strstr(type, "rsa")) {
488 ktype = KEY_RSA;
489 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000490 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +1000491 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100492 return NULL;
493 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000494 if ((key = sshkey_new_private(ktype)) == NULL)
markus@openbsd.org7da5df12017-05-30 14:16:41 +0000495 fatal("sshkey_new_private failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000496 free(type);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000497
498 switch (key->type) {
499 case KEY_DSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000500 buffer_get_bignum_bits(b, key->dsa->p);
501 buffer_get_bignum_bits(b, key->dsa->g);
502 buffer_get_bignum_bits(b, key->dsa->q);
503 buffer_get_bignum_bits(b, key->dsa->pub_key);
504 buffer_get_bignum_bits(b, key->dsa->priv_key);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000505 break;
506 case KEY_RSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000507 if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
508 (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
509 (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
510 fatal("%s: buffer error: %s", __func__, ssh_err(r));
511 e = e1;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000512 debug("e %lx", e);
513 if (e < 30) {
514 e <<= 8;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000515 e += e2;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000516 debug("e %lx", e);
517 e <<= 8;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000518 e += e3;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000519 debug("e %lx", e);
520 }
521 if (!BN_set_word(key->rsa->e, e)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000522 sshbuf_free(b);
523 sshkey_free(key);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000524 return NULL;
525 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000526 buffer_get_bignum_bits(b, key->rsa->d);
527 buffer_get_bignum_bits(b, key->rsa->n);
528 buffer_get_bignum_bits(b, key->rsa->iqmp);
529 buffer_get_bignum_bits(b, key->rsa->q);
530 buffer_get_bignum_bits(b, key->rsa->p);
531 if ((r = rsa_generate_additional_parameters(key->rsa)) != 0)
532 fatal("generate RSA parameters failed: %s", ssh_err(r));
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000533 break;
534 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000535 rlen = sshbuf_len(b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000536 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000537 error("do_convert_private_ssh2_from_blob: "
538 "remaining bytes in key blob %d", rlen);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000539 sshbuf_free(b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000540
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000541 /* try the key */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000542 if (sshkey_sign(key, &sig, &slen, data, sizeof(data), NULL, 0) != 0 ||
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000543 sshkey_verify(key, sig, slen, data, sizeof(data), 0) != 0) {
544 sshkey_free(key);
545 free(sig);
546 return NULL;
547 }
Darren Tuckera627d422013-06-02 07:31:17 +1000548 free(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100549 return key;
550}
551
Damien Miller8056a9d2006-03-15 12:05:40 +1100552static int
553get_line(FILE *fp, char *line, size_t len)
554{
555 int c;
556 size_t pos = 0;
557
558 line[0] = '\0';
559 while ((c = fgetc(fp)) != EOF) {
djm@openbsd.org3038a192015-04-17 13:19:22 +0000560 if (pos >= len - 1)
561 fatal("input line too long.");
Damien Miller90967402006-03-26 14:07:26 +1100562 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100563 case '\r':
564 c = fgetc(fp);
djm@openbsd.org3038a192015-04-17 13:19:22 +0000565 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF)
566 fatal("unget: %s", strerror(errno));
Damien Miller8056a9d2006-03-15 12:05:40 +1100567 return pos;
568 case '\n':
569 return pos;
570 }
571 line[pos++] = c;
572 line[pos] = '\0';
573 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100574 /* We reached EOF */
575 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100576}
577
Ben Lindstrombba81212001-06-25 05:01:22 +0000578static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000579do_convert_from_ssh2(struct passwd *pw, struct sshkey **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000580{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000581 int r, blen, escaped = 0;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000582 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100583 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000584 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000585 char encoded[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000586 FILE *fp;
587
Damien Millerba3420a2010-06-26 09:39:07 +1000588 if ((fp = fopen(identity_file, "r")) == NULL)
589 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000590 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100591 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
Damien Miller746d1a62013-07-18 16:13:02 +1000592 if (blen > 0 && line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000593 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000594 if (strncmp(line, "----", 4) == 0 ||
595 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100596 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000597 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000598 if (strstr(line, " END ") != NULL) {
599 break;
600 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000601 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000602 continue;
603 }
Damien Miller30c3d422000-05-09 11:02:59 +1000604 if (escaped) {
605 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000606 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000607 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000608 }
Damien Millereba71ba2000-04-29 23:57:08 +1000609 strlcat(encoded, line, sizeof(encoded));
610 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000611 len = strlen(encoded);
612 if (((len % 4) == 3) &&
613 (encoded[len-1] == '=') &&
614 (encoded[len-2] == '=') &&
615 (encoded[len-3] == '='))
616 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100617 blen = uudecode(encoded, blob, sizeof(blob));
djm@openbsd.org3038a192015-04-17 13:19:22 +0000618 if (blen < 0)
619 fatal("uudecode failed.");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000620 if (*private)
621 *k = do_convert_private_ssh2_from_blob(blob, blen);
djm@openbsd.org3038a192015-04-17 13:19:22 +0000622 else if ((r = sshkey_from_blob(blob, blen, k)) != 0)
623 fatal("decode blob failed: %s", ssh_err(r));
Damien Miller44b25042010-07-02 13:35:01 +1000624 fclose(fp);
625}
626
627static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000628do_convert_from_pkcs8(struct sshkey **k, int *private)
Damien Miller44b25042010-07-02 13:35:01 +1000629{
630 EVP_PKEY *pubkey;
631 FILE *fp;
632
633 if ((fp = fopen(identity_file, "r")) == NULL)
634 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
635 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
636 fatal("%s: %s is not a recognised public key format", __func__,
637 identity_file);
638 }
639 fclose(fp);
640 switch (EVP_PKEY_type(pubkey->type)) {
641 case EVP_PKEY_RSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000642 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
643 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000644 (*k)->type = KEY_RSA;
645 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
646 break;
647 case EVP_PKEY_DSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000648 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
649 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000650 (*k)->type = KEY_DSA;
651 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
652 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000653#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000654 case EVP_PKEY_EC:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000655 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
656 fatal("sshkey_new failed");
Damien Millereb8b60e2010-08-31 22:41:14 +1000657 (*k)->type = KEY_ECDSA;
658 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000659 (*k)->ecdsa_nid = sshkey_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000660 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000661#endif
Damien Miller44b25042010-07-02 13:35:01 +1000662 default:
663 fatal("%s: unsupported pubkey type %d", __func__,
664 EVP_PKEY_type(pubkey->type));
665 }
666 EVP_PKEY_free(pubkey);
667 return;
668}
669
670static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000671do_convert_from_pem(struct sshkey **k, int *private)
Damien Miller44b25042010-07-02 13:35:01 +1000672{
673 FILE *fp;
674 RSA *rsa;
675#ifdef notyet
676 DSA *dsa;
677#endif
678
679 if ((fp = fopen(identity_file, "r")) == NULL)
680 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
681 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000682 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
683 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000684 (*k)->type = KEY_RSA;
685 (*k)->rsa = rsa;
686 fclose(fp);
687 return;
688 }
689#if notyet /* OpenSSH 0.9.8 lacks this function */
690 rewind(fp);
691 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000692 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
693 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000694 (*k)->type = KEY_DSA;
695 (*k)->dsa = dsa;
696 fclose(fp);
697 return;
698 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000699 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000700#endif
701 fatal("%s: unrecognised raw private key format", __func__);
702}
703
704static void
705do_convert_from(struct passwd *pw)
706{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000707 struct sshkey *k = NULL;
708 int r, private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000709 struct stat st;
710
711 if (!have_identity)
712 ask_filename(pw, "Enter file in which the key is");
713 if (stat(identity_file, &st) < 0)
714 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
715
716 switch (convert_format) {
717 case FMT_RFC4716:
718 do_convert_from_ssh2(pw, &k, &private);
719 break;
720 case FMT_PKCS8:
721 do_convert_from_pkcs8(&k, &private);
722 break;
723 case FMT_PEM:
724 do_convert_from_pem(&k, &private);
725 break;
726 default:
727 fatal("%s: unknown key format %d", __func__, convert_format);
728 }
729
djm@openbsd.org7a2c3682015-01-30 00:59:19 +0000730 if (!private) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000731 if ((r = sshkey_write(k, stdout)) == 0)
732 ok = 1;
Damien Miller44b25042010-07-02 13:35:01 +1000733 if (ok)
734 fprintf(stdout, "\n");
djm@openbsd.org7a2c3682015-01-30 00:59:19 +0000735 } else {
Damien Miller44b25042010-07-02 13:35:01 +1000736 switch (k->type) {
737 case KEY_DSA:
738 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
739 NULL, 0, NULL, NULL);
740 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000741#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000742 case KEY_ECDSA:
743 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
744 NULL, 0, NULL, NULL);
745 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000746#endif
Damien Miller44b25042010-07-02 13:35:01 +1000747 case KEY_RSA:
748 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
749 NULL, 0, NULL, NULL);
750 break;
751 default:
752 fatal("%s: unsupported key type %s", __func__,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000753 sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000754 }
755 }
756
djm@openbsd.org3038a192015-04-17 13:19:22 +0000757 if (!ok)
758 fatal("key write failed");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000759 sshkey_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000760 exit(0);
761}
Damien Miller1f0311c2014-05-15 14:24:09 +1000762#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000763
Ben Lindstrombba81212001-06-25 05:01:22 +0000764static void
Damien Millereba71ba2000-04-29 23:57:08 +1000765do_print_public(struct passwd *pw)
766{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000767 struct sshkey *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000768 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000769 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000770
771 if (!have_identity)
772 ask_filename(pw, "Enter file in which the key is");
djm@openbsd.org3038a192015-04-17 13:19:22 +0000773 if (stat(identity_file, &st) < 0)
774 fatal("%s: %s", identity_file, strerror(errno));
Ben Lindstromd78ae762001-06-05 20:35:09 +0000775 prv = load_identity(identity_file);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000776 if ((r = sshkey_write(prv, stdout)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +0000777 error("sshkey_write failed: %s", ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000778 sshkey_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000779 fprintf(stdout, "\n");
780 exit(0);
781}
782
Ben Lindstromcd392282001-07-04 03:44:03 +0000783static void
Damien Miller757f34e2010-08-05 13:05:31 +1000784do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000785{
Damien Miller7ea845e2010-02-12 09:21:02 +1100786#ifdef ENABLE_PKCS11
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000787 struct sshkey **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100788 int i, nkeys;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000789 enum sshkey_fp_rep rep;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000790 int fptype;
Damien Millerec77c952013-01-09 15:58:00 +1100791 char *fp, *ra;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000792
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000793 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
794 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
Damien Miller1422c082013-01-09 16:44:54 +1100795
Damien Miller7ea845e2010-02-12 09:21:02 +1100796 pkcs11_init(0);
797 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
798 if (nkeys <= 0)
799 fatal("cannot read public key from pkcs11");
800 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100801 if (print_fingerprint) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000802 fp = sshkey_fingerprint(keys[i], fptype, rep);
803 ra = sshkey_fingerprint(keys[i], fingerprint_hash,
Damien Millerec77c952013-01-09 15:58:00 +1100804 SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000805 if (fp == NULL || ra == NULL)
806 fatal("%s: sshkey_fingerprint fail", __func__);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000807 printf("%u %s %s (PKCS11 key)\n", sshkey_size(keys[i]),
808 fp, sshkey_type(keys[i]));
Damien Millerec77c952013-01-09 15:58:00 +1100809 if (log_level >= SYSLOG_LEVEL_VERBOSE)
810 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000811 free(ra);
812 free(fp);
Damien Millerec77c952013-01-09 15:58:00 +1100813 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000814 (void) sshkey_write(keys[i], stdout); /* XXX check */
Damien Millerec77c952013-01-09 15:58:00 +1100815 fprintf(stdout, "\n");
816 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000817 sshkey_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000818 }
Darren Tuckera627d422013-06-02 07:31:17 +1000819 free(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100820 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000821 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100822#else
823 fatal("no pkcs11 support");
824#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000825}
Ben Lindstromcd392282001-07-04 03:44:03 +0000826
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000827static struct sshkey *
828try_read_key(char **cpp)
829{
830 struct sshkey *ret;
831 int r;
832
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000833 if ((ret = sshkey_new(KEY_UNSPEC)) == NULL)
834 fatal("sshkey_new failed");
835 if ((r = sshkey_read(ret, cpp)) == 0)
836 return ret;
837 /* Not a key */
838 sshkey_free(ret);
839 return NULL;
840}
841
842static void
843fingerprint_one_key(const struct sshkey *public, const char *comment)
844{
845 char *fp = NULL, *ra = NULL;
846 enum sshkey_fp_rep rep;
847 int fptype;
848
849 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
850 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
851 fp = sshkey_fingerprint(public, fptype, rep);
852 ra = sshkey_fingerprint(public, fingerprint_hash, SSH_FP_RANDOMART);
853 if (fp == NULL || ra == NULL)
854 fatal("%s: sshkey_fingerprint failed", __func__);
djm@openbsd.orga287c5a2017-02-10 03:36:40 +0000855 mprintf("%u %s %s (%s)\n", sshkey_size(public), fp,
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000856 comment ? comment : "no comment", sshkey_type(public));
857 if (log_level >= SYSLOG_LEVEL_VERBOSE)
858 printf("%s\n", ra);
859 free(ra);
860 free(fp);
861}
862
863static void
864fingerprint_private(const char *path)
865{
866 struct stat st;
867 char *comment = NULL;
868 struct sshkey *public = NULL;
869 int r;
870
871 if (stat(identity_file, &st) < 0)
872 fatal("%s: %s", path, strerror(errno));
djm@openbsd.orgbcb7bc72015-11-18 08:37:28 +0000873 if ((r = sshkey_load_public(path, &public, &comment)) != 0) {
874 debug("load public \"%s\": %s", path, ssh_err(r));
875 if ((r = sshkey_load_private(path, NULL,
876 &public, &comment)) != 0) {
877 debug("load private \"%s\": %s", path, ssh_err(r));
878 fatal("%s is not a key file.", path);
879 }
880 }
881
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000882 fingerprint_one_key(public, comment);
883 sshkey_free(public);
884 free(comment);
885}
886
Ben Lindstrombba81212001-06-25 05:01:22 +0000887static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100888do_fingerprint(struct passwd *pw)
889{
Damien Miller98c7ad62000-03-09 21:27:49 +1100890 FILE *f;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000891 struct sshkey *public = NULL;
djm@openbsd.orgcce6a362015-12-11 03:19:09 +0000892 char *comment = NULL, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES];
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000893 int i, invalid = 1;
894 const char *path;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000895 u_long lnum = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100896
Damien Miller95def091999-11-25 00:26:21 +1100897 if (!have_identity)
898 ask_filename(pw, "Enter file in which the key is");
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000899 path = identity_file;
Damien Miller98c7ad62000-03-09 21:27:49 +1100900
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000901 if (strcmp(identity_file, "-") == 0) {
902 f = stdin;
903 path = "(stdin)";
904 } else if ((f = fopen(path, "r")) == NULL)
905 fatal("%s: %s: %s", __progname, path, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100906
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000907 while (read_keyfile_line(f, path, line, sizeof(line), &lnum) == 0) {
908 cp = line;
909 cp[strcspn(cp, "\n")] = '\0';
910 /* Trim leading space and comments */
911 cp = line + strspn(line, " \t");
912 if (*cp == '#' || *cp == '\0')
913 continue;
914
915 /*
916 * Input may be plain keys, private keys, authorized_keys
917 * or known_hosts.
918 */
919
920 /*
921 * Try private keys first. Assume a key is private if
922 * "SSH PRIVATE KEY" appears on the first line and we're
923 * not reading from stdin (XXX support private keys on stdin).
924 */
925 if (lnum == 1 && strcmp(identity_file, "-") != 0 &&
djm@openbsd.orgbcb7bc72015-11-18 08:37:28 +0000926 strstr(cp, "PRIVATE KEY") != NULL) {
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000927 fclose(f);
928 fingerprint_private(path);
929 exit(0);
930 }
931
932 /*
933 * If it's not a private key, then this must be prepared to
934 * accept a public key prefixed with a hostname or options.
935 * Try a bare key first, otherwise skip the leading stuff.
936 */
937 if ((public = try_read_key(&cp)) == NULL) {
938 i = strtol(cp, &ep, 10);
939 if (i == 0 || ep == NULL ||
940 (*ep != ' ' && *ep != '\t')) {
941 int quoted = 0;
942
943 comment = cp;
944 for (; *cp && (quoted || (*cp != ' ' &&
945 *cp != '\t')); cp++) {
946 if (*cp == '\\' && cp[1] == '"')
947 cp++; /* Skip both */
948 else if (*cp == '"')
949 quoted = !quoted;
950 }
951 if (!*cp)
952 continue;
953 *cp++ = '\0';
954 }
955 }
956 /* Retry after parsing leading hostname/key options */
957 if (public == NULL && (public = try_read_key(&cp)) == NULL) {
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000958 debug("%s:%lu: not a public key", path, lnum);
Damien Millerba3420a2010-06-26 09:39:07 +1000959 continue;
Damien Miller95def091999-11-25 00:26:21 +1100960 }
Damien Millerba3420a2010-06-26 09:39:07 +1000961
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000962 /* Find trailing comment, if any */
963 for (; *cp == ' ' || *cp == '\t'; cp++)
Damien Millerba3420a2010-06-26 09:39:07 +1000964 ;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000965 if (*cp != '\0' && *cp != '#')
Damien Millerba3420a2010-06-26 09:39:07 +1000966 comment = cp;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000967
968 fingerprint_one_key(public, comment);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000969 sshkey_free(public);
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000970 invalid = 0; /* One good key in the file is sufficient */
Damien Miller95def091999-11-25 00:26:21 +1100971 }
Damien Millerba3420a2010-06-26 09:39:07 +1000972 fclose(f);
973
djm@openbsd.org3038a192015-04-17 13:19:22 +0000974 if (invalid)
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000975 fatal("%s is not a public key file.", path);
Damien Miller95def091999-11-25 00:26:21 +1100976 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100977}
978
Damien Miller4b42d7f2005-03-01 21:48:35 +1100979static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000980do_gen_all_hostkeys(struct passwd *pw)
981{
982 struct {
983 char *key_type;
984 char *key_type_display;
985 char *path;
986 } key_types[] = {
djm@openbsd.org5c27e3b2015-03-23 06:06:38 +0000987#ifdef WITH_OPENSSL
Damien Miller58f1baf2011-05-05 14:06:15 +1000988 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
989 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100990#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000991 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
djm@openbsd.org5c27e3b2015-03-23 06:06:38 +0000992#endif /* OPENSSL_HAS_ECC */
993#endif /* WITH_OPENSSL */
Damien Miller5be9d9e2013-12-07 11:24:01 +1100994 { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
Damien Miller58f1baf2011-05-05 14:06:15 +1000995 { NULL, NULL, NULL }
996 };
997
998 int first = 0;
999 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001000 struct sshkey *private, *public;
Damien Miller58f1baf2011-05-05 14:06:15 +10001001 char comment[1024];
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001002 int i, type, fd, r;
Damien Miller58f1baf2011-05-05 14:06:15 +10001003 FILE *f;
1004
1005 for (i = 0; key_types[i].key_type; i++) {
1006 if (stat(key_types[i].path, &st) == 0)
1007 continue;
1008 if (errno != ENOENT) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001009 error("Could not stat %s: %s", key_types[i].path,
Damien Miller58f1baf2011-05-05 14:06:15 +10001010 strerror(errno));
1011 first = 0;
1012 continue;
1013 }
1014
1015 if (first == 0) {
1016 first = 1;
1017 printf("%s: generating new host keys: ", __progname);
1018 }
1019 printf("%s ", key_types[i].key_type_display);
1020 fflush(stdout);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001021 type = sshkey_type_from_name(key_types[i].key_type);
Damien Miller58f1baf2011-05-05 14:06:15 +10001022 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
1023 bits = 0;
djm@openbsd.org7efb4552015-01-18 13:22:28 +00001024 type_bits_valid(type, NULL, &bits);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001025 if ((r = sshkey_generate(type, bits, &private)) != 0) {
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001026 error("sshkey_generate failed: %s", ssh_err(r));
Damien Miller58f1baf2011-05-05 14:06:15 +10001027 first = 0;
1028 continue;
1029 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001030 if ((r = sshkey_from_private(private, &public)) != 0)
1031 fatal("sshkey_from_private failed: %s", ssh_err(r));
Damien Miller58f1baf2011-05-05 14:06:15 +10001032 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
1033 hostname);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001034 if ((r = sshkey_save_private(private, identity_file, "",
1035 comment, use_new_format, new_format_cipher, rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001036 error("Saving key \"%s\" failed: %s",
1037 identity_file, ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001038 sshkey_free(private);
1039 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +10001040 first = 0;
1041 continue;
1042 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001043 sshkey_free(private);
Damien Miller58f1baf2011-05-05 14:06:15 +10001044 strlcat(identity_file, ".pub", sizeof(identity_file));
1045 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1046 if (fd == -1) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001047 error("Could not save your public key in %s",
Damien Miller58f1baf2011-05-05 14:06:15 +10001048 identity_file);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001049 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +10001050 first = 0;
1051 continue;
1052 }
1053 f = fdopen(fd, "w");
1054 if (f == NULL) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001055 error("fdopen %s failed", identity_file);
doug@openbsd.org7df88182014-08-21 01:08:52 +00001056 close(fd);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001057 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +10001058 first = 0;
1059 continue;
1060 }
djm@openbsd.orgbb8b4422015-01-16 15:55:07 +00001061 if ((r = sshkey_write(public, f)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001062 error("write key failed: %s", ssh_err(r));
doug@openbsd.org7df88182014-08-21 01:08:52 +00001063 fclose(f);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001064 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +10001065 first = 0;
1066 continue;
1067 }
1068 fprintf(f, " %s\n", comment);
1069 fclose(f);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001070 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +10001071
1072 }
1073 if (first != 0)
1074 printf("\n");
1075}
1076
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001077struct known_hosts_ctx {
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001078 const char *host; /* Hostname searched for in find/delete case */
1079 FILE *out; /* Output file, stdout for find_hosts case */
1080 int has_unhashed; /* When hashing, original had unhashed hosts */
1081 int found_key; /* For find/delete, host was found */
1082 int invalid; /* File contained invalid items; don't delete */
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001083};
1084
1085static int
1086known_hosts_hash(struct hostkey_foreach_line *l, void *_ctx)
Damien Miller4b42d7f2005-03-01 21:48:35 +11001087{
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001088 struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
1089 char *hashed, *cp, *hosts, *ohosts;
1090 int has_wild = l->hosts && strcspn(l->hosts, "*?!") != strlen(l->hosts);
dtucker@openbsd.org18501152017-03-06 02:03:20 +00001091 int was_hashed = l->hosts && l->hosts[0] == HASH_DELIM;
Darren Tucker0f7e9102008-06-08 12:54:29 +10001092
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001093 switch (l->status) {
1094 case HKF_STATUS_OK:
1095 case HKF_STATUS_MATCHED:
1096 /*
1097 * Don't hash hosts already already hashed, with wildcard
1098 * characters or a CA/revocation marker.
1099 */
djm@openbsd.org12d37672017-03-03 06:13:11 +00001100 if (was_hashed || has_wild || l->marker != MRK_NONE) {
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001101 fprintf(ctx->out, "%s\n", l->line);
1102 if (has_wild && !find_host) {
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001103 logit("%s:%lu: ignoring host name "
djm@openbsd.org3038a192015-04-17 13:19:22 +00001104 "with wildcard: %.64s", l->path,
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001105 l->linenum, l->hosts);
1106 }
1107 return 0;
1108 }
1109 /*
1110 * Split any comma-separated hostnames from the host list,
1111 * hash and store separately.
1112 */
1113 ohosts = hosts = xstrdup(l->hosts);
1114 while ((cp = strsep(&hosts, ",")) != NULL && *cp != '\0') {
djm@openbsd.orgdb259722017-03-10 04:26:06 +00001115 lowercase(cp);
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001116 if ((hashed = host_hash(cp, NULL, 0)) == NULL)
1117 fatal("hash_host failed");
1118 fprintf(ctx->out, "%s %s\n", hashed, l->rawkey);
1119 ctx->has_unhashed = 1;
1120 }
1121 free(ohosts);
1122 return 0;
1123 case HKF_STATUS_INVALID:
1124 /* Retain invalid lines, but mark file as invalid. */
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001125 ctx->invalid = 1;
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001126 logit("%s:%lu: invalid line", l->path, l->linenum);
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001127 /* FALLTHROUGH */
1128 default:
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001129 fprintf(ctx->out, "%s\n", l->line);
1130 return 0;
Darren Tucker0f7e9102008-06-08 12:54:29 +10001131 }
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001132 /* NOTREACHED */
1133 return -1;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001134}
1135
1136static int
1137known_hosts_find_delete(struct hostkey_foreach_line *l, void *_ctx)
1138{
1139 struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001140 enum sshkey_fp_rep rep;
1141 int fptype;
1142 char *fp;
1143
1144 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
1145 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001146
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001147 if (l->status == HKF_STATUS_MATCHED) {
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001148 if (delete_host) {
1149 if (l->marker != MRK_NONE) {
1150 /* Don't remove CA and revocation lines */
1151 fprintf(ctx->out, "%s\n", l->line);
1152 } else {
1153 /*
1154 * Hostname matches and has no CA/revoke
1155 * marker, delete it by *not* writing the
1156 * line to ctx->out.
1157 */
1158 ctx->found_key = 1;
1159 if (!quiet)
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001160 printf("# Host %s found: line %lu\n",
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001161 ctx->host, l->linenum);
1162 }
1163 return 0;
1164 } else if (find_host) {
1165 ctx->found_key = 1;
1166 if (!quiet) {
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001167 printf("# Host %s found: line %lu %s\n",
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001168 ctx->host,
1169 l->linenum, l->marker == MRK_CA ? "CA" :
1170 (l->marker == MRK_REVOKE ? "REVOKED" : ""));
1171 }
1172 if (hash_hosts)
1173 known_hosts_hash(l, ctx);
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001174 else if (print_fingerprint) {
1175 fp = sshkey_fingerprint(l->key, fptype, rep);
djm@openbsd.orga287c5a2017-02-10 03:36:40 +00001176 mprintf("%s %s %s %s\n", ctx->host,
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001177 sshkey_type(l->key), fp, l->comment);
1178 free(fp);
1179 } else
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001180 fprintf(ctx->out, "%s\n", l->line);
1181 return 0;
1182 }
1183 } else if (delete_host) {
1184 /* Retain non-matching hosts when deleting */
1185 if (l->status == HKF_STATUS_INVALID) {
1186 ctx->invalid = 1;
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001187 logit("%s:%lu: invalid line", l->path, l->linenum);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001188 }
1189 fprintf(ctx->out, "%s\n", l->line);
1190 }
1191 return 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001192}
1193
1194static void
1195do_known_hosts(struct passwd *pw, const char *name)
1196{
deraadt@openbsd.orgd2099de2015-01-19 00:32:54 +00001197 char *cp, tmp[PATH_MAX], old[PATH_MAX];
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001198 int r, fd, oerrno, inplace = 0;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001199 struct known_hosts_ctx ctx;
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001200 u_int foreach_options;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001201
1202 if (!have_identity) {
1203 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1204 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1205 sizeof(identity_file))
1206 fatal("Specified known hosts path too long");
Darren Tuckera627d422013-06-02 07:31:17 +10001207 free(cp);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001208 have_identity = 1;
1209 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001210
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001211 memset(&ctx, 0, sizeof(ctx));
1212 ctx.out = stdout;
1213 ctx.host = name;
1214
Damien Miller4b42d7f2005-03-01 21:48:35 +11001215 /*
1216 * Find hosts goes to stdout, hash and deletions happen in-place
1217 * A corner case is ssh-keygen -HF foo, which should go to stdout
1218 */
1219 if (!find_host && (hash_hosts || delete_host)) {
1220 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1221 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1222 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1223 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1224 fatal("known_hosts path too long");
1225 umask(077);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001226 if ((fd = mkstemp(tmp)) == -1)
Damien Miller4b42d7f2005-03-01 21:48:35 +11001227 fatal("mkstemp: %s", strerror(errno));
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001228 if ((ctx.out = fdopen(fd, "w")) == NULL) {
1229 oerrno = errno;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001230 unlink(tmp);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001231 fatal("fdopen: %s", strerror(oerrno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001232 }
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001233 inplace = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001234 }
1235
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001236 /* XXX support identity_file == "-" for stdin */
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001237 foreach_options = find_host ? HKF_WANT_MATCH : 0;
1238 foreach_options |= print_fingerprint ? HKF_WANT_PARSE_KEY : 0;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001239 if ((r = hostkeys_foreach(identity_file,
1240 hash_hosts ? known_hosts_hash : known_hosts_find_delete, &ctx,
deraadt@openbsd.org6da413c2015-11-28 06:50:52 +00001241 name, NULL, foreach_options)) != 0) {
1242 if (inplace)
1243 unlink(tmp);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001244 fatal("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
deraadt@openbsd.org6da413c2015-11-28 06:50:52 +00001245 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001246
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001247 if (inplace)
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001248 fclose(ctx.out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001249
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001250 if (ctx.invalid) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001251 error("%s is not a valid known_hosts file.", identity_file);
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001252 if (inplace) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001253 error("Not replacing existing known_hosts "
1254 "file because of errors");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001255 unlink(tmp);
1256 }
1257 exit(1);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001258 } else if (delete_host && !ctx.found_key) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001259 logit("Host %s not found in %s", name, identity_file);
djm@openbsd.orgc8376432015-08-19 23:17:51 +00001260 if (inplace)
1261 unlink(tmp);
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001262 } else if (inplace) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001263 /* Backup existing file */
1264 if (unlink(old) == -1 && errno != ENOENT)
1265 fatal("unlink %.100s: %s", old, strerror(errno));
1266 if (link(identity_file, old) == -1)
1267 fatal("link %.100s to %.100s: %s", identity_file, old,
1268 strerror(errno));
1269 /* Move new one into place */
1270 if (rename(tmp, identity_file) == -1) {
1271 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1272 strerror(errno));
1273 unlink(tmp);
1274 unlink(old);
1275 exit(1);
1276 }
1277
djm@openbsd.org3038a192015-04-17 13:19:22 +00001278 printf("%s updated.\n", identity_file);
1279 printf("Original contents retained as %s\n", old);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001280 if (ctx.has_unhashed) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001281 logit("WARNING: %s contains unhashed entries", old);
1282 logit("Delete this file to ensure privacy "
1283 "of hostnames");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001284 }
1285 }
1286
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001287 exit (find_host && !ctx.found_key);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001288}
1289
Damien Miller95def091999-11-25 00:26:21 +11001290/*
1291 * Perform changing a passphrase. The argument is the passwd structure
1292 * for the current user.
1293 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001294static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001295do_change_passphrase(struct passwd *pw)
1296{
Damien Miller95def091999-11-25 00:26:21 +11001297 char *comment;
1298 char *old_passphrase, *passphrase1, *passphrase2;
1299 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001300 struct sshkey *private;
1301 int r;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001302
Damien Miller95def091999-11-25 00:26:21 +11001303 if (!have_identity)
1304 ask_filename(pw, "Enter file in which the key is");
djm@openbsd.org3038a192015-04-17 13:19:22 +00001305 if (stat(identity_file, &st) < 0)
1306 fatal("%s: %s", identity_file, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001307 /* Try to load the file with empty passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001308 r = sshkey_load_private(identity_file, "", &private, &comment);
1309 if (r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
Damien Miller95def091999-11-25 00:26:21 +11001310 if (identity_passphrase)
1311 old_passphrase = xstrdup(identity_passphrase);
1312 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001313 old_passphrase =
1314 read_passphrase("Enter old passphrase: ",
1315 RP_ALLOW_STDIN);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001316 r = sshkey_load_private(identity_file, old_passphrase,
1317 &private, &comment);
Damien Millera5103f42014-02-04 11:20:14 +11001318 explicit_bzero(old_passphrase, strlen(old_passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001319 free(old_passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001320 if (r != 0)
1321 goto badkey;
1322 } else if (r != 0) {
1323 badkey:
djm@openbsd.org3038a192015-04-17 13:19:22 +00001324 fatal("Failed to load key %s: %s", identity_file, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001325 }
djm@openbsd.orgf43d1722015-02-26 20:45:47 +00001326 if (comment)
djm@openbsd.orga287c5a2017-02-10 03:36:40 +00001327 mprintf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001328
Damien Miller95def091999-11-25 00:26:21 +11001329 /* Ask the new passphrase (twice). */
1330 if (identity_new_passphrase) {
1331 passphrase1 = xstrdup(identity_new_passphrase);
1332 passphrase2 = NULL;
1333 } else {
1334 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001335 read_passphrase("Enter new passphrase (empty for no "
1336 "passphrase): ", RP_ALLOW_STDIN);
1337 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001338 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001339
1340 /* Verify that they are the same. */
1341 if (strcmp(passphrase1, passphrase2) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001342 explicit_bzero(passphrase1, strlen(passphrase1));
1343 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001344 free(passphrase1);
1345 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001346 printf("Pass phrases do not match. Try again.\n");
1347 exit(1);
1348 }
1349 /* Destroy the other copy. */
Damien Millera5103f42014-02-04 11:20:14 +11001350 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001351 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001352 }
1353
1354 /* Save the file using the new passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001355 if ((r = sshkey_save_private(private, identity_file, passphrase1,
1356 comment, use_new_format, new_format_cipher, rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001357 error("Saving key \"%s\" failed: %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001358 identity_file, ssh_err(r));
Damien Millera5103f42014-02-04 11:20:14 +11001359 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001360 free(passphrase1);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001361 sshkey_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001362 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001363 exit(1);
1364 }
1365 /* Destroy the passphrase and the copy of the key in memory. */
Damien Millera5103f42014-02-04 11:20:14 +11001366 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001367 free(passphrase1);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001368 sshkey_free(private); /* Destroys contents */
Darren Tuckera627d422013-06-02 07:31:17 +10001369 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001370
1371 printf("Your identification has been saved with the new passphrase.\n");
1372 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001373}
1374
Damien Miller37876e92003-05-15 10:19:46 +10001375/*
1376 * Print the SSHFP RR.
1377 */
Damien Millercb314822006-03-26 13:48:01 +11001378static int
1379do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001380{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001381 struct sshkey *public;
Damien Miller37876e92003-05-15 10:19:46 +10001382 char *comment = NULL;
1383 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001384 int r;
Damien Miller37876e92003-05-15 10:19:46 +10001385
Damien Millercb314822006-03-26 13:48:01 +11001386 if (fname == NULL)
Damien Miller5bb88332013-07-18 16:13:37 +10001387 fatal("%s: no filename", __func__);
Damien Millercb314822006-03-26 13:48:01 +11001388 if (stat(fname, &st) < 0) {
1389 if (errno == ENOENT)
1390 return 0;
djm@openbsd.org3038a192015-04-17 13:19:22 +00001391 fatal("%s: %s", fname, strerror(errno));
Damien Miller37876e92003-05-15 10:19:46 +10001392 }
djm@openbsd.org3038a192015-04-17 13:19:22 +00001393 if ((r = sshkey_load_public(fname, &public, &comment)) != 0)
1394 fatal("Failed to read v2 public key from \"%s\": %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001395 fname, ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001396 export_dns_rr(hname, public, stdout, print_generic);
1397 sshkey_free(public);
1398 free(comment);
1399 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001400}
Damien Miller37876e92003-05-15 10:19:46 +10001401
Damien Miller95def091999-11-25 00:26:21 +11001402/*
1403 * Change the comment of a private key file.
1404 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001405static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406do_change_comment(struct passwd *pw)
1407{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001408 char new_comment[1024], *comment, *passphrase;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001409 struct sshkey *private;
1410 struct sshkey *public;
Damien Miller95def091999-11-25 00:26:21 +11001411 struct stat st;
1412 FILE *f;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001413 int r, fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414
Damien Miller95def091999-11-25 00:26:21 +11001415 if (!have_identity)
1416 ask_filename(pw, "Enter file in which the key is");
djm@openbsd.org3038a192015-04-17 13:19:22 +00001417 if (stat(identity_file, &st) < 0)
1418 fatal("%s: %s", identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001419 if ((r = sshkey_load_private(identity_file, "",
1420 &private, &comment)) == 0)
1421 passphrase = xstrdup("");
djm@openbsd.org3038a192015-04-17 13:19:22 +00001422 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
1423 fatal("Cannot load private key \"%s\": %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001424 identity_file, ssh_err(r));
djm@openbsd.org3038a192015-04-17 13:19:22 +00001425 else {
Damien Miller95def091999-11-25 00:26:21 +11001426 if (identity_passphrase)
1427 passphrase = xstrdup(identity_passphrase);
1428 else if (identity_new_passphrase)
1429 passphrase = xstrdup(identity_new_passphrase);
1430 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001431 passphrase = read_passphrase("Enter passphrase: ",
1432 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001433 /* Try to load using the passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001434 if ((r = sshkey_load_private(identity_file, passphrase,
1435 &private, &comment)) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001436 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001437 free(passphrase);
djm@openbsd.org3038a192015-04-17 13:19:22 +00001438 fatal("Cannot load private key \"%s\": %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001439 identity_file, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001440 }
1441 }
halex@openbsd.org4d906252015-11-20 23:04:01 +00001442
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001443 if (private->type != KEY_ED25519 && !use_new_format) {
1444 error("Comments are only supported for keys stored in "
halex@openbsd.org4d906252015-11-20 23:04:01 +00001445 "the new format (-o).");
tobias@openbsd.org704d8c82015-03-31 11:06:49 +00001446 explicit_bzero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001447 sshkey_free(private);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001448 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001449 }
millert@openbsd.orge40269b2017-02-08 20:32:43 +00001450 if (comment)
1451 printf("Key now has comment '%s'\n", comment);
1452 else
1453 printf("Key now has no comment\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454
Damien Miller95def091999-11-25 00:26:21 +11001455 if (identity_comment) {
1456 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1457 } else {
1458 printf("Enter new comment: ");
1459 fflush(stdout);
1460 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
Damien Millera5103f42014-02-04 11:20:14 +11001461 explicit_bzero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001462 sshkey_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001463 exit(1);
1464 }
Damien Miller14b017d2007-09-17 16:09:15 +10001465 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001466 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467
Damien Miller95def091999-11-25 00:26:21 +11001468 /* Save the file using the new passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001469 if ((r = sshkey_save_private(private, identity_file, passphrase,
1470 new_comment, use_new_format, new_format_cipher, rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001471 error("Saving key \"%s\" failed: %s",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001472 identity_file, ssh_err(r));
Damien Millera5103f42014-02-04 11:20:14 +11001473 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001474 free(passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001475 sshkey_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001476 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001477 exit(1);
1478 }
Damien Millera5103f42014-02-04 11:20:14 +11001479 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001480 free(passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001481 if ((r = sshkey_from_private(private, &public)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001482 fatal("sshkey_from_private failed: %s", ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001483 sshkey_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001484
Damien Miller95def091999-11-25 00:26:21 +11001485 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001486 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
djm@openbsd.org3038a192015-04-17 13:19:22 +00001487 if (fd == -1)
1488 fatal("Could not save your public key in %s", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001489 f = fdopen(fd, "w");
djm@openbsd.org3038a192015-04-17 13:19:22 +00001490 if (f == NULL)
1491 fatal("fdopen %s failed: %s", identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001492 if ((r = sshkey_write(public, f)) != 0)
djm@openbsd.org3038a192015-04-17 13:19:22 +00001493 fatal("write key failed: %s", ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001494 sshkey_free(public);
Damien Millereba71ba2000-04-29 23:57:08 +10001495 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001496 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497
Darren Tuckera627d422013-06-02 07:31:17 +10001498 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001499
1500 printf("The comment in your key file has been changed.\n");
1501 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001502}
1503
Damien Miller0a80ca12010-02-27 07:55:05 +11001504static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001505add_flag_option(struct sshbuf *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001506{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001507 int r;
1508
Damien Miller0a80ca12010-02-27 07:55:05 +11001509 debug3("%s: %s", __func__, name);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001510 if ((r = sshbuf_put_cstring(c, name)) != 0 ||
1511 (r = sshbuf_put_string(c, NULL, 0)) != 0)
1512 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001513}
1514
1515static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001516add_string_option(struct sshbuf *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001517{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001518 struct sshbuf *b;
1519 int r;
Damien Miller0a80ca12010-02-27 07:55:05 +11001520
1521 debug3("%s: %s=%s", __func__, name, value);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001522 if ((b = sshbuf_new()) == NULL)
1523 fatal("%s: sshbuf_new failed", __func__);
1524 if ((r = sshbuf_put_cstring(b, value)) != 0 ||
1525 (r = sshbuf_put_cstring(c, name)) != 0 ||
1526 (r = sshbuf_put_stringb(c, b)) != 0)
1527 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001528
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001529 sshbuf_free(b);
Damien Miller0a80ca12010-02-27 07:55:05 +11001530}
1531
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001532#define OPTIONS_CRITICAL 1
1533#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001534static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001535prepare_options_buf(struct sshbuf *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001536{
djm@openbsd.org249516e2017-04-29 04:12:25 +00001537 size_t i;
1538
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001539 sshbuf_reset(c);
Damien Miller1da63882010-08-05 13:03:51 +10001540 if ((which & OPTIONS_CRITICAL) != 0 &&
1541 certflags_command != NULL)
1542 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001543 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001544 (certflags_flags & CERTOPT_X_FWD) != 0)
1545 add_flag_option(c, "permit-X11-forwarding");
1546 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001547 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001548 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001549 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1550 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001551 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001552 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1553 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001554 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001555 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1556 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001557 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001558 if ((which & OPTIONS_CRITICAL) != 0 &&
1559 certflags_src_addr != NULL)
1560 add_string_option(c, "source-address", certflags_src_addr);
djm@openbsd.org249516e2017-04-29 04:12:25 +00001561 for (i = 0; i < ncert_userext; i++) {
1562 if ((cert_userext[i].crit && (which & OPTIONS_EXTENSIONS)) ||
1563 (!cert_userext[i].crit && (which & OPTIONS_CRITICAL)))
1564 continue;
1565 if (cert_userext[i].val == NULL)
1566 add_flag_option(c, cert_userext[i].key);
1567 else {
1568 add_string_option(c, cert_userext[i].key,
1569 cert_userext[i].val);
1570 }
1571 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001572}
1573
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001574static struct sshkey *
Damien Miller757f34e2010-08-05 13:05:31 +10001575load_pkcs11_key(char *path)
1576{
1577#ifdef ENABLE_PKCS11
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001578 struct sshkey **keys = NULL, *public, *private = NULL;
1579 int r, i, nkeys;
Damien Miller757f34e2010-08-05 13:05:31 +10001580
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001581 if ((r = sshkey_load_public(path, &public, NULL)) != 0)
1582 fatal("Couldn't load CA public key \"%s\": %s",
1583 path, ssh_err(r));
Damien Miller757f34e2010-08-05 13:05:31 +10001584
1585 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1586 debug3("%s: %d keys", __func__, nkeys);
1587 if (nkeys <= 0)
1588 fatal("cannot read public key from pkcs11");
1589 for (i = 0; i < nkeys; i++) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001590 if (sshkey_equal_public(public, keys[i])) {
Damien Miller757f34e2010-08-05 13:05:31 +10001591 private = keys[i];
1592 continue;
1593 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001594 sshkey_free(keys[i]);
Damien Miller757f34e2010-08-05 13:05:31 +10001595 }
Darren Tuckera627d422013-06-02 07:31:17 +10001596 free(keys);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001597 sshkey_free(public);
Damien Miller757f34e2010-08-05 13:05:31 +10001598 return private;
1599#else
1600 fatal("no pkcs11 support");
1601#endif /* ENABLE_PKCS11 */
1602}
1603
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001604/* Signer for sshkey_certify_custom that uses the agent */
1605static int
1606agent_signer(const struct sshkey *key, u_char **sigp, size_t *lenp,
1607 const u_char *data, size_t datalen,
1608 const char *alg, u_int compat, void *ctx)
1609{
1610 int *agent_fdp = (int *)ctx;
1611
1612 return ssh_agent_sign(*agent_fdp, key, sigp, lenp,
1613 data, datalen, alg, compat);
1614}
1615
Damien Miller0a80ca12010-02-27 07:55:05 +11001616static void
1617do_ca_sign(struct passwd *pw, int argc, char **argv)
1618{
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001619 int r, i, fd, found, agent_fd = -1;
Damien Miller0a80ca12010-02-27 07:55:05 +11001620 u_int n;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001621 struct sshkey *ca, *public;
djm@openbsd.org499cf362015-11-19 01:08:55 +00001622 char valid[64], *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +11001623 FILE *f;
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001624 struct ssh_identitylist *agent_ids;
1625 size_t j;
Damien Miller4e270b02010-04-16 15:56:21 +10001626
Damien Miller1f0311c2014-05-15 14:24:09 +10001627#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001628 pkcs11_init(1);
Damien Miller1f0311c2014-05-15 14:24:09 +10001629#endif
Damien Miller757f34e2010-08-05 13:05:31 +10001630 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1631 if (pkcs11provider != NULL) {
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001632 /* If a PKCS#11 token was specified then try to use it */
Damien Miller757f34e2010-08-05 13:05:31 +10001633 if ((ca = load_pkcs11_key(tmp)) == NULL)
1634 fatal("No PKCS#11 key matching %s found", ca_key_path);
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001635 } else if (prefer_agent) {
1636 /*
1637 * Agent signature requested. Try to use agent after making
1638 * sure the public key specified is actually present in the
1639 * agent.
1640 */
1641 if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
1642 fatal("Cannot load CA public key %s: %s",
1643 tmp, ssh_err(r));
1644 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0)
1645 fatal("Cannot use public key for CA signature: %s",
1646 ssh_err(r));
1647 if ((r = ssh_fetch_identitylist(agent_fd, &agent_ids)) != 0)
1648 fatal("Retrieve agent key list: %s", ssh_err(r));
1649 found = 0;
1650 for (j = 0; j < agent_ids->nkeys; j++) {
1651 if (sshkey_equal(ca, agent_ids->keys[j])) {
1652 found = 1;
1653 break;
1654 }
1655 }
1656 if (!found)
1657 fatal("CA key %s not found in agent", tmp);
1658 ssh_free_identitylist(agent_ids);
1659 ca->flags |= SSHKEY_FLAG_EXT;
1660 } else {
1661 /* CA key is assumed to be a private key on the filesystem */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001662 ca = load_identity(tmp);
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001663 }
Darren Tuckera627d422013-06-02 07:31:17 +10001664 free(tmp);
Damien Miller757f34e2010-08-05 13:05:31 +10001665
djm@openbsd.org57464e32016-05-02 09:36:42 +00001666 if (key_type_name != NULL &&
1667 sshkey_type_from_name(key_type_name) != ca->type) {
1668 fatal("CA key type %s doesn't match specified %s",
1669 sshkey_ssh_name(ca), key_type_name);
1670 }
1671
Damien Miller0a80ca12010-02-27 07:55:05 +11001672 for (i = 0; i < argc; i++) {
1673 /* Split list of principals */
1674 n = 0;
1675 if (cert_principals != NULL) {
1676 otmp = tmp = xstrdup(cert_principals);
1677 plist = NULL;
1678 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001679 plist = xreallocarray(plist, n + 1, sizeof(*plist));
Damien Miller0a80ca12010-02-27 07:55:05 +11001680 if (*(plist[n] = xstrdup(cp)) == '\0')
1681 fatal("Empty principal name");
1682 }
Darren Tuckera627d422013-06-02 07:31:17 +10001683 free(otmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001684 }
1685
1686 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001687 if ((r = sshkey_load_public(tmp, &public, &comment)) != 0)
1688 fatal("%s: unable to open \"%s\": %s",
1689 __func__, tmp, ssh_err(r));
Damien Millereb8b60e2010-08-31 22:41:14 +10001690 if (public->type != KEY_RSA && public->type != KEY_DSA &&
Damien Miller5be9d9e2013-12-07 11:24:01 +11001691 public->type != KEY_ECDSA && public->type != KEY_ED25519)
Damien Miller0a80ca12010-02-27 07:55:05 +11001692 fatal("%s: key \"%s\" type %s cannot be certified",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001693 __func__, tmp, sshkey_type(public));
Damien Miller0a80ca12010-02-27 07:55:05 +11001694
1695 /* Prepare certificate to sign */
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001696 if ((r = sshkey_to_certified(public)) != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001697 fatal("Could not upgrade key %s to certificate: %s",
1698 tmp, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001699 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001700 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001701 public->cert->key_id = xstrdup(cert_key_id);
1702 public->cert->nprincipals = n;
1703 public->cert->principals = plist;
1704 public->cert->valid_after = cert_valid_from;
1705 public->cert->valid_before = cert_valid_to;
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001706 prepare_options_buf(public->cert->critical, OPTIONS_CRITICAL);
1707 prepare_options_buf(public->cert->extensions,
1708 OPTIONS_EXTENSIONS);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001709 if ((r = sshkey_from_private(ca,
1710 &public->cert->signature_key)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001711 fatal("sshkey_from_private (ca key): %s", ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001712
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001713 if (agent_fd != -1 && (ca->flags & SSHKEY_FLAG_EXT) != 0) {
1714 if ((r = sshkey_certify_custom(public, ca,
1715 key_type_name, agent_signer, &agent_fd)) != 0)
1716 fatal("Couldn't certify key %s via agent: %s",
1717 tmp, ssh_err(r));
1718 } else {
1719 if ((sshkey_certify(public, ca, key_type_name)) != 0)
1720 fatal("Couldn't certify key %s: %s",
1721 tmp, ssh_err(r));
1722 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001723
1724 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1725 *cp = '\0';
1726 xasprintf(&out, "%s-cert.pub", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001727 free(tmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001728
1729 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1730 fatal("Could not open \"%s\" for writing: %s", out,
1731 strerror(errno));
1732 if ((f = fdopen(fd, "w")) == NULL)
1733 fatal("%s: fdopen: %s", __func__, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001734 if ((r = sshkey_write(public, f)) != 0)
1735 fatal("Could not write certified key to %s: %s",
1736 out, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001737 fprintf(f, " %s\n", comment);
1738 fclose(f);
1739
Damien Miller4e270b02010-04-16 15:56:21 +10001740 if (!quiet) {
djm@openbsd.org964ab3e2015-11-19 01:12:32 +00001741 sshkey_format_cert_validity(public->cert,
djm@openbsd.org499cf362015-11-19 01:08:55 +00001742 valid, sizeof(valid));
Damien Miller4e270b02010-04-16 15:56:21 +10001743 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
djm@openbsd.org964ab3e2015-11-19 01:12:32 +00001744 "valid %s", sshkey_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001745 out, public->cert->key_id,
1746 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001747 cert_principals != NULL ? " for " : "",
1748 cert_principals != NULL ? cert_principals : "",
djm@openbsd.org499cf362015-11-19 01:08:55 +00001749 valid);
Damien Miller4e270b02010-04-16 15:56:21 +10001750 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001751
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001752 sshkey_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001753 free(out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001754 }
Damien Miller1f0311c2014-05-15 14:24:09 +10001755#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001756 pkcs11_terminate();
Damien Miller1f0311c2014-05-15 14:24:09 +10001757#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11001758 exit(0);
1759}
1760
1761static u_int64_t
1762parse_relative_time(const char *s, time_t now)
1763{
1764 int64_t mul, secs;
1765
1766 mul = *s == '-' ? -1 : 1;
1767
1768 if ((secs = convtime(s + 1)) == -1)
1769 fatal("Invalid relative certificate time %s", s);
1770 if (mul == -1 && secs > now)
1771 fatal("Certificate time %s cannot be represented", s);
1772 return now + (u_int64_t)(secs * mul);
1773}
1774
1775static u_int64_t
1776parse_absolute_time(const char *s)
1777{
1778 struct tm tm;
1779 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001780 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001781
Damien Miller2ca342b2010-03-03 12:14:15 +11001782 /*
djm@openbsd.org964ab3e2015-11-19 01:12:32 +00001783 * POSIX strptime says "The application shall ensure that there
Damien Miller2ca342b2010-03-03 12:14:15 +11001784 * is white-space or other non-alphanumeric characters between
1785 * any two conversion specifications" so arrange things this way.
1786 */
1787 switch (strlen(s)) {
1788 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001789 fmt = "%Y-%m-%d";
1790 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001791 break;
1792 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001793 fmt = "%Y-%m-%dT%H:%M:%S";
1794 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001795 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1796 break;
1797 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001798 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001799 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001800
Damien Miller1d2c4562014-02-04 11:18:20 +11001801 memset(&tm, 0, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001802 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001803 fatal("Invalid certificate time %s", s);
1804 if ((tt = mktime(&tm)) < 0)
1805 fatal("Certificate time %s cannot be represented", s);
1806 return (u_int64_t)tt;
1807}
1808
1809static void
1810parse_cert_times(char *timespec)
1811{
1812 char *from, *to;
1813 time_t now = time(NULL);
1814 int64_t secs;
1815
1816 /* +timespec relative to now */
1817 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1818 if ((secs = convtime(timespec + 1)) == -1)
1819 fatal("Invalid relative certificate life %s", timespec);
1820 cert_valid_to = now + secs;
1821 /*
1822 * Backdate certificate one minute to avoid problems on hosts
1823 * with poorly-synchronised clocks.
1824 */
1825 cert_valid_from = ((now - 59)/ 60) * 60;
1826 return;
1827 }
1828
1829 /*
1830 * from:to, where
1831 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1832 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1833 */
1834 from = xstrdup(timespec);
1835 to = strchr(from, ':');
1836 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001837 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001838 *to++ = '\0';
1839
1840 if (*from == '-' || *from == '+')
1841 cert_valid_from = parse_relative_time(from, now);
1842 else
1843 cert_valid_from = parse_absolute_time(from);
1844
1845 if (*to == '-' || *to == '+')
Damien Miller5b01b0d2013-10-23 16:31:31 +11001846 cert_valid_to = parse_relative_time(to, now);
Damien Miller0a80ca12010-02-27 07:55:05 +11001847 else
1848 cert_valid_to = parse_absolute_time(to);
1849
1850 if (cert_valid_to <= cert_valid_from)
1851 fatal("Empty certificate validity interval");
Darren Tuckera627d422013-06-02 07:31:17 +10001852 free(from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001853}
1854
1855static void
Damien Miller4e270b02010-04-16 15:56:21 +10001856add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001857{
djm@openbsd.org249516e2017-04-29 04:12:25 +00001858 char *val, *cp;
1859 int iscrit = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001860
Damien Miller044f4a62011-05-05 14:14:08 +10001861 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001862 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001863 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001864 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001865 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001866 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001867 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001868 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001869 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001870 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001871 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001872 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001873 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001874 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001875 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001876 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001877 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001878 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001879 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001880 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001881 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001882 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001883 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1884 val = opt + 14;
1885 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001886 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001887 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001888 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001889 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001890 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1891 val = opt + 15;
1892 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001893 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001894 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001895 fatal("source-address already specified");
1896 if (addr_match_cidr_list(NULL, val) != 0)
1897 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001898 certflags_src_addr = xstrdup(val);
djm@openbsd.org249516e2017-04-29 04:12:25 +00001899 } else if (strncasecmp(opt, "extension:", 10) == 0 ||
1900 (iscrit = (strncasecmp(opt, "critical:", 9) == 0))) {
1901 val = xstrdup(strchr(opt, ':') + 1);
1902 if ((cp = strchr(val, '=')) != NULL)
1903 *cp++ = '\0';
1904 cert_userext = xreallocarray(cert_userext, ncert_userext + 1,
1905 sizeof(*cert_userext));
1906 cert_userext[ncert_userext].key = val;
1907 cert_userext[ncert_userext].val = cp == NULL ?
1908 NULL : xstrdup(cp);
1909 cert_userext[ncert_userext].crit = iscrit;
1910 ncert_userext++;
Damien Miller0a80ca12010-02-27 07:55:05 +11001911 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001912 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001913}
1914
Ben Lindstrombba81212001-06-25 05:01:22 +00001915static void
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001916show_options(struct sshbuf *optbuf, int in_critical)
Damien Millerd834d352010-06-26 09:48:02 +10001917{
Damien Miller633de332014-05-15 13:48:26 +10001918 char *name, *arg;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001919 struct sshbuf *options, *option = NULL;
1920 int r;
Damien Millerd834d352010-06-26 09:48:02 +10001921
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001922 if ((options = sshbuf_fromb(optbuf)) == NULL)
1923 fatal("%s: sshbuf_fromb failed", __func__);
1924 while (sshbuf_len(options) != 0) {
1925 sshbuf_free(option);
1926 option = NULL;
1927 if ((r = sshbuf_get_cstring(options, &name, NULL)) != 0 ||
1928 (r = sshbuf_froms(options, &option)) != 0)
1929 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd834d352010-06-26 09:48:02 +10001930 printf(" %s", name);
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001931 if (!in_critical &&
Damien Millerd834d352010-06-26 09:48:02 +10001932 (strcmp(name, "permit-X11-forwarding") == 0 ||
1933 strcmp(name, "permit-agent-forwarding") == 0 ||
1934 strcmp(name, "permit-port-forwarding") == 0 ||
1935 strcmp(name, "permit-pty") == 0 ||
1936 strcmp(name, "permit-user-rc") == 0))
1937 printf("\n");
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001938 else if (in_critical &&
Damien Millerd834d352010-06-26 09:48:02 +10001939 (strcmp(name, "force-command") == 0 ||
1940 strcmp(name, "source-address") == 0)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001941 if ((r = sshbuf_get_cstring(option, &arg, NULL)) != 0)
1942 fatal("%s: buffer error: %s",
1943 __func__, ssh_err(r));
Damien Miller633de332014-05-15 13:48:26 +10001944 printf(" %s\n", arg);
1945 free(arg);
Damien Millerd834d352010-06-26 09:48:02 +10001946 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001947 printf(" UNKNOWN OPTION (len %zu)\n",
1948 sshbuf_len(option));
1949 sshbuf_reset(option);
Damien Millerd834d352010-06-26 09:48:02 +10001950 }
Darren Tuckera627d422013-06-02 07:31:17 +10001951 free(name);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001952 if (sshbuf_len(option) != 0)
Damien Millerd834d352010-06-26 09:48:02 +10001953 fatal("Option corrupt: extra data at end");
1954 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001955 sshbuf_free(option);
1956 sshbuf_free(options);
Damien Millerd834d352010-06-26 09:48:02 +10001957}
1958
1959static void
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00001960print_cert(struct sshkey *key)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001961{
djm@openbsd.org499cf362015-11-19 01:08:55 +00001962 char valid[64], *key_fp, *ca_fp;
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001963 u_int i;
Damien Miller4e270b02010-04-16 15:56:21 +10001964
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001965 key_fp = sshkey_fingerprint(key, fingerprint_hash, SSH_FP_DEFAULT);
1966 ca_fp = sshkey_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001967 fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001968 if (key_fp == NULL || ca_fp == NULL)
1969 fatal("%s: sshkey_fingerprint fail", __func__);
djm@openbsd.org499cf362015-11-19 01:08:55 +00001970 sshkey_format_cert_validity(key->cert, valid, sizeof(valid));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001971
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001972 printf(" Type: %s %s certificate\n", sshkey_ssh_name(key),
1973 sshkey_cert_type(key));
1974 printf(" Public key: %s %s\n", sshkey_type(key), key_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001975 printf(" Signing CA: %s %s\n",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001976 sshkey_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001977 printf(" Key ID: \"%s\"\n", key->cert->key_id);
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001978 printf(" Serial: %llu\n", (unsigned long long)key->cert->serial);
djm@openbsd.org499cf362015-11-19 01:08:55 +00001979 printf(" Valid: %s\n", valid);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001980 printf(" Principals: ");
1981 if (key->cert->nprincipals == 0)
1982 printf("(none)\n");
1983 else {
1984 for (i = 0; i < key->cert->nprincipals; i++)
1985 printf("\n %s",
1986 key->cert->principals[i]);
1987 printf("\n");
1988 }
Damien Miller4e270b02010-04-16 15:56:21 +10001989 printf(" Critical Options: ");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001990 if (sshbuf_len(key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001991 printf("(none)\n");
1992 else {
1993 printf("\n");
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001994 show_options(key->cert->critical, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001995 }
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001996 printf(" Extensions: ");
1997 if (sshbuf_len(key->cert->extensions) == 0)
1998 printf("(none)\n");
1999 else {
2000 printf("\n");
2001 show_options(key->cert->extensions, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10002002 }
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002003}
2004
2005static void
2006do_show_cert(struct passwd *pw)
2007{
2008 struct sshkey *key = NULL;
2009 struct stat st;
2010 int r, is_stdin = 0, ok = 0;
2011 FILE *f;
djm@openbsd.orgcce6a362015-12-11 03:19:09 +00002012 char *cp, line[SSH_MAX_PUBKEY_BYTES];
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002013 const char *path;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002014 u_long lnum = 0;
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002015
2016 if (!have_identity)
2017 ask_filename(pw, "Enter file in which the key is");
2018 if (strcmp(identity_file, "-") != 0 && stat(identity_file, &st) < 0)
2019 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
2020
2021 path = identity_file;
2022 if (strcmp(path, "-") == 0) {
2023 f = stdin;
2024 path = "(stdin)";
2025 is_stdin = 1;
2026 } else if ((f = fopen(identity_file, "r")) == NULL)
2027 fatal("fopen %s: %s", identity_file, strerror(errno));
2028
2029 while (read_keyfile_line(f, path, line, sizeof(line), &lnum) == 0) {
2030 sshkey_free(key);
2031 key = NULL;
2032 /* Trim leading space and comments */
2033 cp = line + strspn(line, " \t");
2034 if (*cp == '#' || *cp == '\0')
2035 continue;
2036 if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00002037 fatal("sshkey_new");
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002038 if ((r = sshkey_read(key, &cp)) != 0) {
2039 error("%s:%lu: invalid key: %s", path,
2040 lnum, ssh_err(r));
2041 continue;
2042 }
2043 if (!sshkey_is_cert(key)) {
2044 error("%s:%lu is not a certificate", path, lnum);
2045 continue;
2046 }
2047 ok = 1;
2048 if (!is_stdin && lnum == 1)
2049 printf("%s:\n", path);
2050 else
2051 printf("%s:%lu:\n", path, lnum);
2052 print_cert(key);
2053 }
2054 sshkey_free(key);
2055 fclose(f);
2056 exit(ok ? 0 : 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11002057}
2058
2059static void
Damien Millerf3747bf2013-01-18 11:44:04 +11002060load_krl(const char *path, struct ssh_krl **krlp)
2061{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002062 struct sshbuf *krlbuf;
2063 int r, fd;
Damien Millerf3747bf2013-01-18 11:44:04 +11002064
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002065 if ((krlbuf = sshbuf_new()) == NULL)
2066 fatal("sshbuf_new failed");
Damien Millerf3747bf2013-01-18 11:44:04 +11002067 if ((fd = open(path, O_RDONLY)) == -1)
2068 fatal("open %s: %s", path, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002069 if ((r = sshkey_load_file(fd, krlbuf)) != 0)
2070 fatal("Unable to load KRL: %s", ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002071 close(fd);
2072 /* XXX check sigs */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002073 if ((r = ssh_krl_from_blob(krlbuf, krlp, NULL, 0)) != 0 ||
Damien Millerf3747bf2013-01-18 11:44:04 +11002074 *krlp == NULL)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002075 fatal("Invalid KRL file: %s", ssh_err(r));
2076 sshbuf_free(krlbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11002077}
2078
2079static void
djm@openbsd.org669aee92015-01-30 01:10:33 +00002080update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002081 const struct sshkey *ca, struct ssh_krl *krl)
Damien Millerf3747bf2013-01-18 11:44:04 +11002082{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002083 struct sshkey *key = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11002084 u_long lnum = 0;
2085 char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES];
2086 unsigned long long serial, serial2;
2087 int i, was_explicit_key, was_sha1, r;
2088 FILE *krl_spec;
2089
2090 path = tilde_expand_filename(file, pw->pw_uid);
2091 if (strcmp(path, "-") == 0) {
2092 krl_spec = stdin;
2093 free(path);
2094 path = xstrdup("(standard input)");
2095 } else if ((krl_spec = fopen(path, "r")) == NULL)
2096 fatal("fopen %s: %s", path, strerror(errno));
2097
2098 if (!quiet)
2099 printf("Revoking from %s\n", path);
2100 while (read_keyfile_line(krl_spec, path, line, sizeof(line),
2101 &lnum) == 0) {
2102 was_explicit_key = was_sha1 = 0;
2103 cp = line + strspn(line, " \t");
2104 /* Trim trailing space, comments and strip \n */
2105 for (i = 0, r = -1; cp[i] != '\0'; i++) {
2106 if (cp[i] == '#' || cp[i] == '\n') {
2107 cp[i] = '\0';
2108 break;
2109 }
2110 if (cp[i] == ' ' || cp[i] == '\t') {
2111 /* Remember the start of a span of whitespace */
2112 if (r == -1)
2113 r = i;
2114 } else
2115 r = -1;
2116 }
2117 if (r != -1)
2118 cp[r] = '\0';
2119 if (*cp == '\0')
2120 continue;
2121 if (strncasecmp(cp, "serial:", 7) == 0) {
djm@openbsd.org669aee92015-01-30 01:10:33 +00002122 if (ca == NULL && !wild_ca) {
Damien Millerd234afb2013-08-21 02:42:58 +10002123 fatal("revoking certificates by serial number "
Damien Millerf3747bf2013-01-18 11:44:04 +11002124 "requires specification of a CA key");
2125 }
2126 cp += 7;
2127 cp = cp + strspn(cp, " \t");
2128 errno = 0;
2129 serial = strtoull(cp, &ep, 0);
2130 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2131 fatal("%s:%lu: invalid serial \"%s\"",
2132 path, lnum, cp);
2133 if (errno == ERANGE && serial == ULLONG_MAX)
2134 fatal("%s:%lu: serial out of range",
2135 path, lnum);
2136 serial2 = serial;
2137 if (*ep == '-') {
2138 cp = ep + 1;
2139 errno = 0;
2140 serial2 = strtoull(cp, &ep, 0);
2141 if (*cp == '\0' || *ep != '\0')
2142 fatal("%s:%lu: invalid serial \"%s\"",
2143 path, lnum, cp);
2144 if (errno == ERANGE && serial2 == ULLONG_MAX)
2145 fatal("%s:%lu: serial out of range",
2146 path, lnum);
2147 if (serial2 <= serial)
2148 fatal("%s:%lu: invalid serial range "
2149 "%llu:%llu", path, lnum,
2150 (unsigned long long)serial,
2151 (unsigned long long)serial2);
2152 }
2153 if (ssh_krl_revoke_cert_by_serial_range(krl,
2154 ca, serial, serial2) != 0) {
2155 fatal("%s: revoke serial failed",
2156 __func__);
2157 }
2158 } else if (strncasecmp(cp, "id:", 3) == 0) {
djm@openbsd.org669aee92015-01-30 01:10:33 +00002159 if (ca == NULL && !wild_ca) {
Damien Millerd5d9d7b2013-08-21 02:43:27 +10002160 fatal("revoking certificates by key ID "
Damien Millerf3747bf2013-01-18 11:44:04 +11002161 "requires specification of a CA key");
2162 }
2163 cp += 3;
2164 cp = cp + strspn(cp, " \t");
2165 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2166 fatal("%s: revoke key ID failed", __func__);
2167 } else {
2168 if (strncasecmp(cp, "key:", 4) == 0) {
2169 cp += 4;
2170 cp = cp + strspn(cp, " \t");
2171 was_explicit_key = 1;
2172 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2173 cp += 5;
2174 cp = cp + strspn(cp, " \t");
2175 was_sha1 = 1;
2176 } else {
2177 /*
2178 * Just try to process the line as a key.
2179 * Parsing will fail if it isn't.
2180 */
2181 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002182 if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00002183 fatal("sshkey_new");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002184 if ((r = sshkey_read(key, &cp)) != 0)
2185 fatal("%s:%lu: invalid key: %s",
2186 path, lnum, ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002187 if (was_explicit_key)
2188 r = ssh_krl_revoke_key_explicit(krl, key);
2189 else if (was_sha1)
2190 r = ssh_krl_revoke_key_sha1(krl, key);
2191 else
2192 r = ssh_krl_revoke_key(krl, key);
2193 if (r != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002194 fatal("%s: revoke key failed: %s",
2195 __func__, ssh_err(r));
2196 sshkey_free(key);
Damien Millerf3747bf2013-01-18 11:44:04 +11002197 }
2198 }
2199 if (strcmp(path, "-") != 0)
2200 fclose(krl_spec);
Damien Miller0d6771b2013-04-23 15:23:24 +10002201 free(path);
Damien Millerf3747bf2013-01-18 11:44:04 +11002202}
2203
2204static void
2205do_gen_krl(struct passwd *pw, int updating, int argc, char **argv)
2206{
2207 struct ssh_krl *krl;
2208 struct stat sb;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002209 struct sshkey *ca = NULL;
djm@openbsd.org669aee92015-01-30 01:10:33 +00002210 int fd, i, r, wild_ca = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002211 char *tmp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002212 struct sshbuf *kbuf;
Damien Millerf3747bf2013-01-18 11:44:04 +11002213
2214 if (*identity_file == '\0')
2215 fatal("KRL generation requires an output file");
2216 if (stat(identity_file, &sb) == -1) {
2217 if (errno != ENOENT)
2218 fatal("Cannot access KRL \"%s\": %s",
2219 identity_file, strerror(errno));
2220 if (updating)
2221 fatal("KRL \"%s\" does not exist", identity_file);
2222 }
2223 if (ca_key_path != NULL) {
djm@openbsd.org669aee92015-01-30 01:10:33 +00002224 if (strcasecmp(ca_key_path, "none") == 0)
2225 wild_ca = 1;
2226 else {
2227 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2228 if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
2229 fatal("Cannot load CA public key %s: %s",
2230 tmp, ssh_err(r));
2231 free(tmp);
2232 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002233 }
2234
2235 if (updating)
2236 load_krl(identity_file, &krl);
2237 else if ((krl = ssh_krl_init()) == NULL)
2238 fatal("couldn't create KRL");
2239
2240 if (cert_serial != 0)
2241 ssh_krl_set_version(krl, cert_serial);
2242 if (identity_comment != NULL)
2243 ssh_krl_set_comment(krl, identity_comment);
2244
2245 for (i = 0; i < argc; i++)
djm@openbsd.org669aee92015-01-30 01:10:33 +00002246 update_krl_from_file(pw, argv[i], wild_ca, ca, krl);
Damien Millerf3747bf2013-01-18 11:44:04 +11002247
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002248 if ((kbuf = sshbuf_new()) == NULL)
2249 fatal("sshbuf_new failed");
2250 if (ssh_krl_to_blob(krl, kbuf, NULL, 0) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11002251 fatal("Couldn't generate KRL");
2252 if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2253 fatal("open %s: %s", identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002254 if (atomicio(vwrite, fd, (void *)sshbuf_ptr(kbuf), sshbuf_len(kbuf)) !=
2255 sshbuf_len(kbuf))
Damien Millerf3747bf2013-01-18 11:44:04 +11002256 fatal("write %s: %s", identity_file, strerror(errno));
2257 close(fd);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002258 sshbuf_free(kbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11002259 ssh_krl_free(krl);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00002260 sshkey_free(ca);
Damien Millerf3747bf2013-01-18 11:44:04 +11002261}
2262
2263static void
2264do_check_krl(struct passwd *pw, int argc, char **argv)
2265{
2266 int i, r, ret = 0;
2267 char *comment;
2268 struct ssh_krl *krl;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002269 struct sshkey *k;
Damien Millerf3747bf2013-01-18 11:44:04 +11002270
2271 if (*identity_file == '\0')
2272 fatal("KRL checking requires an input file");
2273 load_krl(identity_file, &krl);
2274 for (i = 0; i < argc; i++) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002275 if ((r = sshkey_load_public(argv[i], &k, &comment)) != 0)
2276 fatal("Cannot load public key %s: %s",
2277 argv[i], ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002278 r = ssh_krl_check_key(krl, k);
2279 printf("%s%s%s%s: %s\n", argv[i],
2280 *comment ? " (" : "", comment, *comment ? ")" : "",
2281 r == 0 ? "ok" : "REVOKED");
2282 if (r != 0)
2283 ret = 1;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002284 sshkey_free(k);
Damien Millerf3747bf2013-01-18 11:44:04 +11002285 free(comment);
2286 }
2287 ssh_krl_free(krl);
2288 exit(ret);
2289}
2290
2291static void
Damien Miller431f66b1999-11-21 18:31:57 +11002292usage(void)
2293{
Damien Millerf0858de2014-04-20 13:01:30 +10002294 fprintf(stderr,
djm@openbsd.org873d3e72017-04-30 23:18:44 +00002295 "usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10002296 " [-N new_passphrase] [-C comment] [-f output_keyfile]\n"
2297 " ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]\n"
2298 " ssh-keygen -i [-m key_format] [-f input_keyfile]\n"
2299 " ssh-keygen -e [-m key_format] [-f input_keyfile]\n"
2300 " ssh-keygen -y [-f input_keyfile]\n"
2301 " ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]\n"
naddy@openbsd.org6288e3a2015-02-24 15:24:05 +00002302 " ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]\n"
djm@openbsd.org873d3e72017-04-30 23:18:44 +00002303 " ssh-keygen -B [-f input_keyfile]\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002304#ifdef ENABLE_PKCS11
Damien Millerf0858de2014-04-20 13:01:30 +10002305 fprintf(stderr,
2306 " ssh-keygen -D pkcs11\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002307#endif
Damien Millerf0858de2014-04-20 13:01:30 +10002308 fprintf(stderr,
2309 " ssh-keygen -F hostname [-f known_hosts_file] [-l]\n"
2310 " ssh-keygen -H [-f known_hosts_file]\n"
2311 " ssh-keygen -R hostname [-f known_hosts_file]\n"
2312 " ssh-keygen -r hostname [-f input_keyfile] [-g]\n"
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00002313#ifdef WITH_OPENSSL
Damien Millerf0858de2014-04-20 13:01:30 +10002314 " ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]\n"
2315 " ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]\n"
2316 " [-j start_line] [-K checkpt] [-W generator]\n"
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00002317#endif
djm@openbsd.orga98339e2017-06-28 01:09:22 +00002318 " ssh-keygen -s ca_key -I certificate_identity [-h] [-U]\n"
2319 " [-D pkcs11_provider] [-n principals] [-O option]\n"
2320 " [-V validity_interval] [-z serial_number] file ...\n"
Damien Millerf0858de2014-04-20 13:01:30 +10002321 " ssh-keygen -L [-f input_keyfile]\n"
2322 " ssh-keygen -A\n"
2323 " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
2324 " file ...\n"
2325 " ssh-keygen -Q -f krl_file file ...\n");
Damien Miller95def091999-11-25 00:26:21 +11002326 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11002327}
2328
Damien Miller95def091999-11-25 00:26:21 +11002329/*
2330 * Main program for key management.
2331 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002332int
Damien Millerdf8b7db2007-01-05 16:22:57 +11002333main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002334{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00002335 char dotsshdir[PATH_MAX], comment[1024], *passphrase1, *passphrase2;
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00002336 char *rr_hostname = NULL, *ep, *fp, *ra;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002337 struct sshkey *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11002338 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11002339 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002340 int r, opt, type, fd;
Damien Millerf3747bf2013-01-18 11:44:04 +11002341 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
Damien Miller95def091999-11-25 00:26:21 +11002342 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10002343 const char *errstr;
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00002344#ifdef WITH_OPENSSL
2345 /* Moduli generation/screening */
2346 char out_file[PATH_MAX], *checkpoint = NULL;
2347 u_int32_t memory = 0, generator_wanted = 0;
2348 int do_gen_candidates = 0, do_screen_candidates = 0;
2349 unsigned long start_lineno = 0, lines_to_process = 0;
2350 BIGNUM *start = NULL;
2351#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002352
Damien Miller95def091999-11-25 00:26:21 +11002353 extern int optind;
2354 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002355
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00002356 ssh_malloc_init(); /* must be called before any mallocs */
Darren Tuckerce321d82005-10-03 18:11:24 +10002357 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2358 sanitise_stdfd();
2359
Darren Tucker9ac56e92007-01-14 10:19:59 +11002360 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10002361
Damien Miller72ef7c12015-01-15 02:21:31 +11002362#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10002363 OpenSSL_add_all_algorithms();
Damien Miller72ef7c12015-01-15 02:21:31 +11002364#endif
Damien Millerdf8b7db2007-01-05 16:22:57 +11002365 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10002366
Kevin Steves3a881912002-07-20 19:05:40 +00002367 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10002368
djm@openbsd.orga287c5a2017-02-10 03:36:40 +00002369 msetlocale();
2370
Damien Miller5428f641999-11-25 11:54:57 +11002371 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11002372 pw = getpwuid(getuid());
djm@openbsd.org3038a192015-04-17 13:19:22 +00002373 if (!pw)
2374 fatal("No user exists for uid %lu", (u_long)getuid());
2375 if (gethostname(hostname, sizeof(hostname)) < 0)
2376 fatal("gethostname: %s", strerror(errno));
Damien Miller5428f641999-11-25 11:54:57 +11002377
djm@openbsd.orga98339e2017-06-28 01:09:22 +00002378 /* Remaining characters: Ydw */
2379 while ((opt = getopt(argc, argv, "ABHLQUXceghiklopquvxy"
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002380 "C:D:E:F:G:I:J:K:M:N:O:P:R:S:T:V:W:Z:"
2381 "a:b:f:g:j:m:n:r:s:t:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11002382 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10002383 case 'A':
2384 gen_all_hostkeys = 1;
2385 break;
Damien Miller95def091999-11-25 00:26:21 +11002386 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10002387 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10002388 if (errstr)
2389 fatal("Bits has bad value %s (%s)",
2390 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11002391 break;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002392 case 'E':
2393 fingerprint_hash = ssh_digest_alg_by_name(optarg);
2394 if (fingerprint_hash == -1)
2395 fatal("Invalid hash algorithm \"%s\"", optarg);
2396 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002397 case 'F':
2398 find_host = 1;
2399 rr_hostname = optarg;
2400 break;
2401 case 'H':
2402 hash_hosts = 1;
2403 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002404 case 'I':
2405 cert_key_id = optarg;
2406 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002407 case 'R':
2408 delete_host = 1;
2409 rr_hostname = optarg;
2410 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11002411 case 'L':
2412 show_cert = 1;
2413 break;
Damien Miller95def091999-11-25 00:26:21 +11002414 case 'l':
2415 print_fingerprint = 1;
2416 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002417 case 'B':
2418 print_bubblebabble = 1;
2419 break;
Damien Miller44b25042010-07-02 13:35:01 +10002420 case 'm':
2421 if (strcasecmp(optarg, "RFC4716") == 0 ||
2422 strcasecmp(optarg, "ssh2") == 0) {
2423 convert_format = FMT_RFC4716;
2424 break;
2425 }
2426 if (strcasecmp(optarg, "PKCS8") == 0) {
2427 convert_format = FMT_PKCS8;
2428 break;
2429 }
2430 if (strcasecmp(optarg, "PEM") == 0) {
2431 convert_format = FMT_PEM;
2432 break;
2433 }
2434 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002435 case 'n':
2436 cert_principals = optarg;
2437 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002438 case 'o':
2439 use_new_format = 1;
2440 break;
Damien Miller95def091999-11-25 00:26:21 +11002441 case 'p':
2442 change_passphrase = 1;
2443 break;
Damien Miller95def091999-11-25 00:26:21 +11002444 case 'c':
2445 change_comment = 1;
2446 break;
Damien Miller95def091999-11-25 00:26:21 +11002447 case 'f':
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00002448 if (strlcpy(identity_file, optarg,
2449 sizeof(identity_file)) >= sizeof(identity_file))
Damien Millerb089fb52005-05-26 12:16:18 +10002450 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002451 have_identity = 1;
2452 break;
Damien Miller37876e92003-05-15 10:19:46 +10002453 case 'g':
2454 print_generic = 1;
2455 break;
Damien Miller95def091999-11-25 00:26:21 +11002456 case 'P':
2457 identity_passphrase = optarg;
2458 break;
Damien Miller95def091999-11-25 00:26:21 +11002459 case 'N':
2460 identity_new_passphrase = optarg;
2461 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002462 case 'Q':
2463 check_krl = 1;
2464 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002465 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002466 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002467 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002468 case 'Z':
2469 new_format_cipher = optarg;
2470 break;
Damien Miller95def091999-11-25 00:26:21 +11002471 case 'C':
2472 identity_comment = optarg;
2473 break;
Damien Miller95def091999-11-25 00:26:21 +11002474 case 'q':
2475 quiet = 1;
2476 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002477 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002478 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002479 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002480 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002481 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002482 case 'h':
2483 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002484 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002485 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002486 case 'k':
2487 gen_krl = 1;
2488 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002489 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002490 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002491 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002492 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002493 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002494 case 'y':
2495 print_public = 1;
2496 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002497 case 's':
2498 ca_key_path = optarg;
2499 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002500 case 't':
2501 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002502 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002503 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002504 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002505 break;
djm@openbsd.orga98339e2017-06-28 01:09:22 +00002506 case 'U':
2507 prefer_agent = 1;
2508 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002509 case 'u':
2510 update_krl = 1;
2511 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002512 case 'v':
2513 if (log_level == SYSLOG_LEVEL_INFO)
2514 log_level = SYSLOG_LEVEL_DEBUG1;
2515 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002516 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002517 log_level < SYSLOG_LEVEL_DEBUG3)
2518 log_level++;
2519 }
2520 break;
Damien Miller37876e92003-05-15 10:19:46 +10002521 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002522 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002523 break;
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00002524 case 'a':
2525 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
2526 if (errstr)
2527 fatal("Invalid number: %s (%s)",
2528 optarg, errstr);
2529 break;
2530 case 'V':
2531 parse_cert_times(optarg);
2532 break;
2533 case 'z':
2534 errno = 0;
2535 cert_serial = strtoull(optarg, &ep, 10);
2536 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2537 (errno == ERANGE && cert_serial == ULLONG_MAX))
2538 fatal("Invalid serial number \"%s\"", optarg);
2539 break;
2540#ifdef WITH_OPENSSL
2541 /* Moduli generation/screening */
Darren Tucker019cefe2003-08-02 22:40:07 +10002542 case 'G':
2543 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002544 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2545 sizeof(out_file))
2546 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002547 break;
Darren Tucker0bb29802016-09-12 11:07:00 +10002548 case 'J':
2549 lines_to_process = strtoul(optarg, NULL, 10);
dtucker@openbsd.org7b63cf62016-09-12 03:29:16 +00002550 break;
Darren Tucker0bb29802016-09-12 11:07:00 +10002551 case 'j':
2552 start_lineno = strtoul(optarg, NULL, 10);
dtucker@openbsd.org7b63cf62016-09-12 03:29:16 +00002553 break;
Damien Miller390d0562011-10-18 16:05:19 +11002554 case 'K':
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00002555 if (strlen(optarg) >= PATH_MAX)
Damien Miller390d0562011-10-18 16:05:19 +11002556 fatal("Checkpoint filename too long");
2557 checkpoint = xstrdup(optarg);
2558 break;
Darren Tuckerd8c3cfb2016-09-12 13:30:50 +10002559 case 'M':
2560 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX,
2561 &errstr);
2562 if (errstr)
2563 fatal("Memory limit is %s: %s", errstr, optarg);
2564 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002565 case 'S':
2566 /* XXX - also compare length against bits */
2567 if (BN_hex2bn(&start, optarg) == 0)
2568 fatal("Invalid start point.");
2569 break;
Darren Tuckeraf48d542016-09-12 13:52:17 +10002570 case 'T':
2571 do_screen_candidates = 1;
2572 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2573 sizeof(out_file))
2574 fatal("Output filename too long");
2575 break;
Darren Tucker43cceff2016-09-12 13:55:37 +10002576 case 'W':
2577 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2578 UINT_MAX, &errstr);
Darren Tucker70508962016-09-12 13:57:28 +10002579 if (errstr != NULL)
2580 fatal("Desired generator invalid: %s (%s)",
2581 optarg, errstr);
Darren Tucker43cceff2016-09-12 13:55:37 +10002582 break;
Damien Miller72ef7c12015-01-15 02:21:31 +11002583#endif /* WITH_OPENSSL */
Damien Miller95def091999-11-25 00:26:21 +11002584 case '?':
2585 default:
2586 usage();
2587 }
2588 }
Darren Tucker06930c72003-12-31 11:34:51 +11002589
2590 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002591 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002592
Damien Miller0a80ca12010-02-27 07:55:05 +11002593 argv += optind;
2594 argc -= optind;
2595
2596 if (ca_key_path != NULL) {
Damien Millerf3747bf2013-01-18 11:44:04 +11002597 if (argc < 1 && !gen_krl) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00002598 error("Too few arguments.");
Damien Miller0a80ca12010-02-27 07:55:05 +11002599 usage();
2600 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002601 } else if (argc > 0 && !gen_krl && !check_krl) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00002602 error("Too many arguments.");
Damien Miller95def091999-11-25 00:26:21 +11002603 usage();
2604 }
2605 if (change_passphrase && change_comment) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00002606 error("Can only have one of -p and -c.");
Damien Miller95def091999-11-25 00:26:21 +11002607 usage();
2608 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002609 if (print_fingerprint && (delete_host || hash_hosts)) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00002610 error("Cannot use -l with -H or -R.");
Darren Tucker0f7e9102008-06-08 12:54:29 +10002611 usage();
2612 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002613 if (gen_krl) {
2614 do_gen_krl(pw, update_krl, argc, argv);
2615 return (0);
2616 }
2617 if (check_krl) {
2618 do_check_krl(pw, argc, argv);
2619 return (0);
2620 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002621 if (ca_key_path != NULL) {
2622 if (cert_key_id == NULL)
2623 fatal("Must specify key id (-I) when certifying");
2624 do_ca_sign(pw, argc, argv);
2625 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002626 if (show_cert)
2627 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002628 if (delete_host || hash_hosts || find_host)
2629 do_known_hosts(pw, rr_hostname);
Damien Millerec77c952013-01-09 15:58:00 +11002630 if (pkcs11provider != NULL)
2631 do_download(pw);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002632 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002633 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002634 if (change_passphrase)
2635 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002636 if (change_comment)
2637 do_change_comment(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002638#ifdef WITH_OPENSSL
Damien Miller44b25042010-07-02 13:35:01 +10002639 if (convert_to)
2640 do_convert_to(pw);
2641 if (convert_from)
2642 do_convert_from(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002643#endif
Damien Millereba71ba2000-04-29 23:57:08 +10002644 if (print_public)
2645 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002646 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002647 unsigned int n = 0;
2648
2649 if (have_identity) {
2650 n = do_print_resource_record(pw,
2651 identity_file, rr_hostname);
djm@openbsd.org3038a192015-04-17 13:19:22 +00002652 if (n == 0)
2653 fatal("%s: %s", identity_file, strerror(errno));
Damien Millercb314822006-03-26 13:48:01 +11002654 exit(0);
2655 } else {
2656
2657 n += do_print_resource_record(pw,
2658 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2659 n += do_print_resource_record(pw,
2660 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
Damien Miller3bde12a2012-06-20 21:51:11 +10002661 n += do_print_resource_record(pw,
2662 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
Damien Miller16cd3922014-05-15 13:45:58 +10002663 n += do_print_resource_record(pw,
2664 _PATH_HOST_ED25519_KEY_FILE, rr_hostname);
Damien Millercb314822006-03-26 13:48:01 +11002665 if (n == 0)
2666 fatal("no keys found.");
2667 exit(0);
2668 }
Damien Miller37876e92003-05-15 10:19:46 +10002669 }
Damien Miller95def091999-11-25 00:26:21 +11002670
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00002671#ifdef WITH_OPENSSL
Darren Tucker019cefe2003-08-02 22:40:07 +10002672 if (do_gen_candidates) {
2673 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002674
Darren Tucker019cefe2003-08-02 22:40:07 +10002675 if (out == NULL) {
2676 error("Couldn't open modulus candidate file \"%s\": %s",
2677 out_file, strerror(errno));
2678 return (1);
2679 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002680 if (bits == 0)
2681 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002682 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002683 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002684
2685 return (0);
2686 }
2687
2688 if (do_screen_candidates) {
2689 FILE *in;
Damien Miller78d22712013-02-12 11:03:36 +11002690 FILE *out = fopen(out_file, "a");
Darren Tucker019cefe2003-08-02 22:40:07 +10002691
2692 if (have_identity && strcmp(identity_file, "-") != 0) {
2693 if ((in = fopen(identity_file, "r")) == NULL) {
2694 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002695 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002696 strerror(errno));
2697 }
2698 } else
2699 in = stdin;
2700
2701 if (out == NULL) {
2702 fatal("Couldn't open moduli file \"%s\": %s",
2703 out_file, strerror(errno));
2704 }
Damien Millerbcd00ab2013-12-07 10:41:55 +11002705 if (prime_test(in, out, rounds == 0 ? 100 : rounds,
2706 generator_wanted, checkpoint,
Damien Millerdfceafe2012-07-06 13:44:19 +10002707 start_lineno, lines_to_process) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002708 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002709 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002710 }
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00002711#endif
Darren Tucker019cefe2003-08-02 22:40:07 +10002712
Damien Miller58f1baf2011-05-05 14:06:15 +10002713 if (gen_all_hostkeys) {
2714 do_gen_all_hostkeys(pw);
2715 return (0);
2716 }
2717
Damien Millerf14be5c2005-11-05 15:15:49 +11002718 if (key_type_name == NULL)
djm@openbsd.orgd1958792015-05-28 04:40:13 +00002719 key_type_name = DEFAULT_KEY_TYPE_NAME;
Damien Millerf14be5c2005-11-05 15:15:49 +11002720
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002721 type = sshkey_type_from_name(key_type_name);
djm@openbsd.org7efb4552015-01-18 13:22:28 +00002722 type_bits_valid(type, key_type_name, &bits);
Damien Miller58f1baf2011-05-05 14:06:15 +10002723
Darren Tucker3af2ac52005-11-29 13:10:24 +11002724 if (!quiet)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002725 printf("Generating public/private %s key pair.\n",
2726 key_type_name);
djm@openbsd.org3038a192015-04-17 13:19:22 +00002727 if ((r = sshkey_generate(type, bits, &private)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00002728 fatal("sshkey_generate failed");
djm@openbsd.org3038a192015-04-17 13:19:22 +00002729 if ((r = sshkey_from_private(private, &public)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00002730 fatal("sshkey_from_private failed: %s\n", ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11002731
2732 if (!have_identity)
2733 ask_filename(pw, "Enter file in which to save the key");
2734
Damien Miller788f2122005-11-05 15:14:59 +11002735 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002736 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2737 pw->pw_dir, _PATH_SSH_USER_DIR);
2738 if (strstr(identity_file, dotsshdir) != NULL) {
2739 if (stat(dotsshdir, &st) < 0) {
2740 if (errno != ENOENT) {
2741 error("Could not stat %s: %s", dotsshdir,
2742 strerror(errno));
2743 } else if (mkdir(dotsshdir, 0700) < 0) {
2744 error("Could not create directory '%s': %s",
2745 dotsshdir, strerror(errno));
2746 } else if (!quiet)
2747 printf("Created directory '%s'.\n", dotsshdir);
2748 }
Damien Miller95def091999-11-25 00:26:21 +11002749 }
2750 /* If the file already exists, ask the user to confirm. */
2751 if (stat(identity_file, &st) >= 0) {
2752 char yesno[3];
2753 printf("%s already exists.\n", identity_file);
2754 printf("Overwrite (y/n)? ");
2755 fflush(stdout);
2756 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2757 exit(1);
2758 if (yesno[0] != 'y' && yesno[0] != 'Y')
2759 exit(1);
2760 }
2761 /* Ask for a passphrase (twice). */
2762 if (identity_passphrase)
2763 passphrase1 = xstrdup(identity_passphrase);
2764 else if (identity_new_passphrase)
2765 passphrase1 = xstrdup(identity_new_passphrase);
2766 else {
2767passphrase_again:
2768 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002769 read_passphrase("Enter passphrase (empty for no "
2770 "passphrase): ", RP_ALLOW_STDIN);
2771 passphrase2 = read_passphrase("Enter same passphrase again: ",
2772 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002773 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002774 /*
2775 * The passphrases do not match. Clear them and
2776 * retry.
2777 */
Damien Millera5103f42014-02-04 11:20:14 +11002778 explicit_bzero(passphrase1, strlen(passphrase1));
2779 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002780 free(passphrase1);
2781 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002782 printf("Passphrases do not match. Try again.\n");
2783 goto passphrase_again;
2784 }
2785 /* Clear the other copy of the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002786 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002787 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002788 }
2789
Damien Miller95def091999-11-25 00:26:21 +11002790 if (identity_comment) {
2791 strlcpy(comment, identity_comment, sizeof(comment));
2792 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002793 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002794 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2795 }
2796
2797 /* Save the key with the given passphrase and comment. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002798 if ((r = sshkey_save_private(private, identity_file, passphrase1,
2799 comment, use_new_format, new_format_cipher, rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00002800 error("Saving key \"%s\" failed: %s",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002801 identity_file, ssh_err(r));
Damien Millera5103f42014-02-04 11:20:14 +11002802 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002803 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002804 exit(1);
2805 }
2806 /* Clear the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002807 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002808 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002809
2810 /* Clear the private key and the random number generator. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002811 sshkey_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002812
2813 if (!quiet)
2814 printf("Your identification has been saved in %s.\n", identity_file);
2815
Damien Miller95def091999-11-25 00:26:21 +11002816 strlcat(identity_file, ".pub", sizeof(identity_file));
djm@openbsd.org3038a192015-04-17 13:19:22 +00002817 if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2818 fatal("Unable to save public key to %s: %s",
2819 identity_file, strerror(errno));
2820 if ((f = fdopen(fd, "w")) == NULL)
2821 fatal("fdopen %s failed: %s", identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002822 if ((r = sshkey_write(public, f)) != 0)
djm@openbsd.org3038a192015-04-17 13:19:22 +00002823 error("write key failed: %s", ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10002824 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002825 fclose(f);
2826
2827 if (!quiet) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00002828 fp = sshkey_fingerprint(public, fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002829 SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00002830 ra = sshkey_fingerprint(public, fingerprint_hash,
Darren Tucker9c16ac92008-06-13 04:40:35 +10002831 SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00002832 if (fp == NULL || ra == NULL)
2833 fatal("sshkey_fingerprint failed");
Damien Millereba71ba2000-04-29 23:57:08 +10002834 printf("Your public key has been saved in %s.\n",
2835 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002836 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002837 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002838 printf("The key's randomart image is:\n");
2839 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10002840 free(ra);
2841 free(fp);
Damien Miller95def091999-11-25 00:26:21 +11002842 }
Damien Millereba71ba2000-04-29 23:57:08 +10002843
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002844 sshkey_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002845 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002846}