blob: 2c5c75db7f0890348ff7e6ecfc0b2884862875f3 [file] [log] [blame]
djm@openbsd.org20ebb462020-05-18 04:29:35 +00001/* $OpenBSD: ssh-keygen.c,v 1.409.2.1 2020/05/18 19:02:13 benno 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
Damien Millercfc18972019-10-09 09:06:35 +110027#ifdef HAVE_STDINT_H
28# include <stdint.h>
29#endif
Darren Tucker39972492006-07-12 22:22:46 +100030#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100031#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100036#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100037#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100039#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000042#include <limits.h>
djm@openbsd.orga287c5a2017-02-10 03:36:40 +000043#include <locale.h>
Damien Miller4d3b2f32018-07-12 14:49:14 +100044#include <time.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100045
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "xmalloc.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000047#include "sshkey.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "authfile.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000049#include "sshbuf.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "pathnames.h"
51#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100052#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110053#include "match.h"
54#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100055#include "dns.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110056#include "ssh.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110057#include "ssh2.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000058#include "ssherr.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110059#include "ssh-pkcs11.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110060#include "atomicio.h"
61#include "krl.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000062#include "digest.h"
djm@openbsd.orga287c5a2017-02-10 03:36:40 +000063#include "utf8.h"
djm@openbsd.orga98339e2017-06-28 01:09:22 +000064#include "authfd.h"
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000065#include "sshsig.h"
djm@openbsd.org23f38c22019-10-31 21:17:09 +000066#include "ssh-sk.h"
67#include "sk-api.h" /* XXX for SSH_SK_USER_PRESENCE_REQD; remove */
Ben Lindstromcd392282001-07-04 03:44:03 +000068
djm@openbsd.orgd1958792015-05-28 04:40:13 +000069#ifdef WITH_OPENSSL
70# define DEFAULT_KEY_TYPE_NAME "rsa"
71#else
72# define DEFAULT_KEY_TYPE_NAME "ed25519"
73#endif
74
dtucker@openbsd.orgf47269e2019-03-25 15:49:00 +000075/*
dtucker@openbsd.org26e0cef2019-03-25 16:19:44 +000076 * Default number of bits in the RSA, DSA and ECDSA keys. These value can be
77 * overridden on the command line.
78 *
79 * These values, with the exception of DSA, provide security equivalent to at
80 * least 128 bits of security according to NIST Special Publication 800-57:
81 * Recommendation for Key Management Part 1 rev 4 section 5.6.1.
82 * For DSA it (and FIPS-186-4 section 4.2) specifies that the only size for
83 * which a 160bit hash is acceptable is 1kbit, and since ssh-dss specifies only
84 * SHA1 we limit the DSA key size 1k bits.
dtucker@openbsd.orgf47269e2019-03-25 15:49:00 +000085 */
86#define DEFAULT_BITS 3072
Damien Miller3f54a9f2005-11-05 14:52:18 +110087#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100088#define DEFAULT_BITS_ECDSA 256
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
djm@openbsd.org851f8032019-01-23 04:16:22 +000090static int quiet = 0;
Damien Millerf2b70ca2010-03-05 07:39:35 +110091
Damien Miller10f6f6b1999-11-17 17:29:08 +110092/* Flag indicating that we just want to see the key fingerprint */
djm@openbsd.org851f8032019-01-23 04:16:22 +000093static int print_fingerprint = 0;
94static int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110095
djm@openbsd.org56d1c832014-12-21 22:27:55 +000096/* Hash algorithm to use for fingerprints. */
djm@openbsd.org851f8032019-01-23 04:16:22 +000097static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org56d1c832014-12-21 22:27:55 +000098
Damien Miller431f66b1999-11-21 18:31:57 +110099/* The identity file name, given on the command line or entered by the user. */
deraadt@openbsd.org3e53ef22019-09-24 12:50:46 +0000100static char identity_file[PATH_MAX];
djm@openbsd.org851f8032019-01-23 04:16:22 +0000101static int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
103/* This is set to the passphrase if given on the command line. */
djm@openbsd.org851f8032019-01-23 04:16:22 +0000104static char *identity_passphrase = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
106/* This is set to the new passphrase if given on the command line. */
djm@openbsd.org851f8032019-01-23 04:16:22 +0000107static char *identity_new_passphrase = NULL;
Damien Miller4e270b02010-04-16 15:56:21 +1000108
Damien Miller0a80ca12010-02-27 07:55:05 +1100109/* Key type when certifying */
djm@openbsd.org851f8032019-01-23 04:16:22 +0000110static u_int cert_key_type = SSH2_CERT_TYPE_USER;
Damien Miller0a80ca12010-02-27 07:55:05 +1100111
112/* "key ID" of signed key */
djm@openbsd.org851f8032019-01-23 04:16:22 +0000113static char *cert_key_id = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100114
115/* Comma-separated list of principal names for certifying keys */
djm@openbsd.org851f8032019-01-23 04:16:22 +0000116static char *cert_principals = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100117
118/* Validity period for certificates */
djm@openbsd.org851f8032019-01-23 04:16:22 +0000119static u_int64_t cert_valid_from = 0;
120static u_int64_t cert_valid_to = ~0ULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100121
Damien Miller4e270b02010-04-16 15:56:21 +1000122/* Certificate options */
djm@openbsd.org2e712632019-11-25 00:54:23 +0000123#define CERTOPT_X_FWD (1)
124#define CERTOPT_AGENT_FWD (1<<1)
125#define CERTOPT_PORT_FWD (1<<2)
126#define CERTOPT_PTY (1<<3)
127#define CERTOPT_USER_RC (1<<4)
128#define CERTOPT_NO_REQUIRE_USER_PRESENCE (1<<5)
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000129#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
130 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
djm@openbsd.org851f8032019-01-23 04:16:22 +0000131static u_int32_t certflags_flags = CERTOPT_DEFAULT;
132static char *certflags_command = NULL;
133static char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100134
djm@openbsd.org249516e2017-04-29 04:12:25 +0000135/* Arbitrary extensions specified by user */
136struct cert_userext {
137 char *key;
138 char *val;
139 int crit;
140};
djm@openbsd.org851f8032019-01-23 04:16:22 +0000141static struct cert_userext *cert_userext;
142static size_t ncert_userext;
djm@openbsd.org249516e2017-04-29 04:12:25 +0000143
Damien Miller44b25042010-07-02 13:35:01 +1000144/* Conversion to/from various formats */
Damien Miller44b25042010-07-02 13:35:01 +1000145enum {
146 FMT_RFC4716,
147 FMT_PKCS8,
148 FMT_PEM
149} convert_format = FMT_RFC4716;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100150
djm@openbsd.org851f8032019-01-23 04:16:22 +0000151static char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000152
Damien Miller757f34e2010-08-05 13:05:31 +1000153/* Load key from this PKCS#11 provider */
djm@openbsd.org851f8032019-01-23 04:16:22 +0000154static char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000155
djm@openbsd.org23f38c22019-10-31 21:17:09 +0000156/* FIDO/U2F provider to use */
157static char *sk_provider = NULL;
158
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +0000159/* Format for writing private keys */
160static int private_key_format = SSHKEY_PRIVATE_OPENSSH;
Damien Millerbcd00ab2013-12-07 10:41:55 +1100161
162/* Cipher for new-format private keys */
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +0000163static char *openssh_format_cipher = NULL;
Damien Millerbcd00ab2013-12-07 10:41:55 +1100164
djm@openbsd.org3e60d182019-12-30 03:30:09 +0000165/* Number of KDF rounds to derive new format keys. */
djm@openbsd.org851f8032019-01-23 04:16:22 +0000166static int rounds = 0;
Damien Millerbcd00ab2013-12-07 10:41:55 +1100167
Damien Miller431f66b1999-11-21 18:31:57 +1100168/* argv0 */
169extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
djm@openbsd.org851f8032019-01-23 04:16:22 +0000171static char hostname[NI_MAXHOST];
Damien Millereba71ba2000-04-29 23:57:08 +1000172
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +0000173#ifdef WITH_OPENSSL
Darren Tucker770fc012004-05-13 16:24:32 +1000174/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000175int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Damien Millerdfceafe2012-07-06 13:44:19 +1000176int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
177 unsigned long);
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +0000178#endif
Darren Tucker770fc012004-05-13 16:24:32 +1000179
Ben Lindstrombba81212001-06-25 05:01:22 +0000180static void
djm@openbsd.org7efb4552015-01-18 13:22:28 +0000181type_bits_valid(int type, const char *name, u_int32_t *bitsp)
Damien Miller58f1baf2011-05-05 14:06:15 +1000182{
djm@openbsd.org3038a192015-04-17 13:19:22 +0000183 if (type == KEY_UNSPEC)
184 fatal("unknown key type %s", key_type_name);
Damien Miller884b63a2011-05-05 14:14:52 +1000185 if (*bitsp == 0) {
Damien Miller773dda22015-01-30 23:10:17 +1100186#ifdef WITH_OPENSSL
naddy@openbsd.orgc31e4f52019-08-05 21:45:27 +0000187 u_int nid;
188
189 switch(type) {
190 case KEY_DSA:
Damien Miller884b63a2011-05-05 14:14:52 +1000191 *bitsp = DEFAULT_BITS_DSA;
naddy@openbsd.orgc31e4f52019-08-05 21:45:27 +0000192 break;
193 case KEY_ECDSA:
djm@openbsd.org7efb4552015-01-18 13:22:28 +0000194 if (name != NULL &&
195 (nid = sshkey_ecdsa_nid_from_name(name)) > 0)
196 *bitsp = sshkey_curve_nid_to_bits(nid);
197 if (*bitsp == 0)
198 *bitsp = DEFAULT_BITS_ECDSA;
naddy@openbsd.orgc31e4f52019-08-05 21:45:27 +0000199 break;
200 case KEY_RSA:
Damien Miller884b63a2011-05-05 14:14:52 +1000201 *bitsp = DEFAULT_BITS;
naddy@openbsd.orgc31e4f52019-08-05 21:45:27 +0000202 break;
203 }
204#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000205 }
Damien Miller72ef7c12015-01-15 02:21:31 +1100206#ifdef WITH_OPENSSL
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000207 switch (type) {
208 case KEY_DSA:
209 if (*bitsp != 1024)
210 fatal("Invalid DSA key length: must be 1024 bits");
211 break;
212 case KEY_RSA:
213 if (*bitsp < SSH_RSA_MINIMUM_MODULUS_SIZE)
214 fatal("Invalid RSA key length: minimum is %d bits",
215 SSH_RSA_MINIMUM_MODULUS_SIZE);
naddy@openbsd.orgc31e4f52019-08-05 21:45:27 +0000216 else if (*bitsp > OPENSSL_RSA_MAX_MODULUS_BITS)
217 fatal("Invalid RSA key length: maximum is %d bits",
218 OPENSSL_RSA_MAX_MODULUS_BITS);
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000219 break;
220 case KEY_ECDSA:
221 if (sshkey_ecdsa_bits_to_nid(*bitsp) == -1)
222 fatal("Invalid ECDSA key length: valid lengths are "
Manoj Ampalam9b47b082018-11-08 22:41:59 -0800223#ifdef OPENSSL_HAS_NISTP521
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000224 "256, 384 or 521 bits");
Manoj Ampalam9b47b082018-11-08 22:41:59 -0800225#else
226 "256 or 384 bits");
227#endif
djm@openbsd.orgbd636f42017-05-07 23:15:59 +0000228 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000229#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000230}
231
djm@openbsd.org85443f12019-09-03 08:27:52 +0000232/*
233 * Checks whether a file exists and, if so, asks the user whether they wish
234 * to overwrite it.
235 * Returns nonzero if the file does not already exist or if the user agrees to
236 * overwrite, or zero otherwise.
237 */
238static int
239confirm_overwrite(const char *filename)
240{
241 char yesno[3];
242 struct stat st;
243
244 if (stat(filename, &st) != 0)
245 return 1;
246 printf("%s already exists.\n", filename);
247 printf("Overwrite (y/n)? ");
248 fflush(stdout);
249 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
250 return 0;
251 if (yesno[0] != 'y' && yesno[0] != 'Y')
252 return 0;
253 return 1;
254}
255
Damien Miller58f1baf2011-05-05 14:06:15 +1000256static void
Damien Miller431f66b1999-11-21 18:31:57 +1100257ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258{
Damien Miller95def091999-11-25 00:26:21 +1100259 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100260 char *name = NULL;
261
Damien Miller993dd552002-02-19 15:22:47 +1100262 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000263 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100264 else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000265 switch (sshkey_type_from_name(key_type_name)) {
Damien Miller4e270b02010-04-16 15:56:21 +1000266 case KEY_DSA_CERT:
Damien Miller993dd552002-02-19 15:22:47 +1100267 case KEY_DSA:
268 name = _PATH_SSH_CLIENT_ID_DSA;
269 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100270#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000271 case KEY_ECDSA_CERT:
272 case KEY_ECDSA:
273 name = _PATH_SSH_CLIENT_ID_ECDSA;
274 break;
djm@openbsd.org23f38c22019-10-31 21:17:09 +0000275 case KEY_ECDSA_SK_CERT:
276 case KEY_ECDSA_SK:
277 name = _PATH_SSH_CLIENT_ID_ECDSA_SK;
278 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100279#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000280 case KEY_RSA_CERT:
Damien Miller993dd552002-02-19 15:22:47 +1100281 case KEY_RSA:
282 name = _PATH_SSH_CLIENT_ID_RSA;
283 break;
Damien Miller5be9d9e2013-12-07 11:24:01 +1100284 case KEY_ED25519:
285 case KEY_ED25519_CERT:
286 name = _PATH_SSH_CLIENT_ID_ED25519;
287 break;
markus@openbsd.org2c557442019-11-12 19:33:08 +0000288 case KEY_ED25519_SK:
289 case KEY_ED25519_SK_CERT:
290 name = _PATH_SSH_CLIENT_ID_ED25519_SK;
291 break;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000292 case KEY_XMSS:
293 case KEY_XMSS_CERT:
294 name = _PATH_SSH_CLIENT_ID_XMSS;
295 break;
Damien Miller993dd552002-02-19 15:22:47 +1100296 default:
djm@openbsd.org3038a192015-04-17 13:19:22 +0000297 fatal("bad key type");
Damien Miller993dd552002-02-19 15:22:47 +1100298 }
Damien Miller90967402006-03-26 14:07:26 +1100299 }
djm@openbsd.org3038a192015-04-17 13:19:22 +0000300 snprintf(identity_file, sizeof(identity_file),
301 "%s/%s", pw->pw_dir, name);
302 printf("%s (%s): ", prompt, identity_file);
303 fflush(stdout);
Damien Miller95def091999-11-25 00:26:21 +1100304 if (fgets(buf, sizeof(buf), stdin) == NULL)
305 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000306 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100307 if (strcmp(buf, "") != 0)
308 strlcpy(identity_file, buf, sizeof(identity_file));
309 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100310}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000312static struct sshkey *
djm@openbsd.org9a396e32019-09-02 23:46:46 +0000313load_identity(const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000314{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000315 char *pass;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000316 struct sshkey *prv;
317 int r;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000318
djm@openbsd.org07133222019-09-01 23:47:32 +0000319 if (commentp != NULL)
320 *commentp = NULL;
321 if ((r = sshkey_load_private(filename, "", &prv, commentp)) == 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000322 return prv;
323 if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
324 fatal("Load key \"%s\": %s", filename, ssh_err(r));
325 if (identity_passphrase)
326 pass = xstrdup(identity_passphrase);
327 else
328 pass = read_passphrase("Enter passphrase: ", RP_ALLOW_STDIN);
djm@openbsd.org07133222019-09-01 23:47:32 +0000329 r = sshkey_load_private(filename, pass, &prv, commentp);
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +0000330 freezero(pass, strlen(pass));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000331 if (r != 0)
332 fatal("Load key \"%s\": %s", filename, ssh_err(r));
Ben Lindstromd0fca422001-03-26 13:44:06 +0000333 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000334}
335
Damien Miller874d77b2000-10-14 16:23:11 +1100336#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000337#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100338#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000339#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000340
Damien Miller1f0311c2014-05-15 14:24:09 +1000341#ifdef WITH_OPENSSL
Ben Lindstrombba81212001-06-25 05:01:22 +0000342static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000343do_convert_to_ssh2(struct passwd *pw, struct sshkey *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000344{
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000345 struct sshbuf *b;
346 char comment[61], *b64;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000347 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000348
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000349 if ((b = sshbuf_new()) == NULL)
350 fatal("%s: sshbuf_new failed", __func__);
351 if ((r = sshkey_putb(k, b)) != 0)
djm@openbsd.org3038a192015-04-17 13:19:22 +0000352 fatal("key_to_blob failed: %s", ssh_err(r));
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000353 if ((b64 = sshbuf_dtob64_string(b, 1)) == NULL)
354 fatal("%s: sshbuf_dtob64_string failed", __func__);
355
Darren Tuckerd04758d2010-01-12 19:41:57 +1100356 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
357 snprintf(comment, sizeof(comment),
358 "%u-bit %s, converted by %s@%s from OpenSSH",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000359 sshkey_size(k), sshkey_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000360 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100361
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000362 sshkey_free(k);
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000363 sshbuf_free(b);
364
365 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
366 fprintf(stdout, "Comment: \"%s\"\n%s", comment, b64);
367 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
368 free(b64);
Damien Millereba71ba2000-04-29 23:57:08 +1000369 exit(0);
370}
371
Ben Lindstrombba81212001-06-25 05:01:22 +0000372static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000373do_convert_to_pkcs8(struct sshkey *k)
Damien Miller44b25042010-07-02 13:35:01 +1000374{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000375 switch (sshkey_type_plain(k->type)) {
Damien Miller44b25042010-07-02 13:35:01 +1000376 case KEY_RSA:
377 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
378 fatal("PEM_write_RSA_PUBKEY failed");
379 break;
380 case KEY_DSA:
381 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
382 fatal("PEM_write_DSA_PUBKEY failed");
383 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000384#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000385 case KEY_ECDSA:
386 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
387 fatal("PEM_write_EC_PUBKEY failed");
388 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000389#endif
Damien Miller44b25042010-07-02 13:35:01 +1000390 default:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000391 fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000392 }
393 exit(0);
394}
395
396static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000397do_convert_to_pem(struct sshkey *k)
Damien Miller44b25042010-07-02 13:35:01 +1000398{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000399 switch (sshkey_type_plain(k->type)) {
Damien Miller44b25042010-07-02 13:35:01 +1000400 case KEY_RSA:
401 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
402 fatal("PEM_write_RSAPublicKey failed");
403 break;
djm@openbsd.org8dfb6a22020-01-24 00:00:31 +0000404 case KEY_DSA:
405 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
406 fatal("PEM_write_DSA_PUBKEY failed");
407 break;
Darren Tuckere16dfa92020-01-25 13:05:42 +1100408#ifdef OPENSSL_HAS_ECC
djm@openbsd.org8dfb6a22020-01-24 00:00:31 +0000409 case KEY_ECDSA:
410 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
411 fatal("PEM_write_EC_PUBKEY failed");
412 break;
Darren Tuckere16dfa92020-01-25 13:05:42 +1100413#endif
Damien Miller44b25042010-07-02 13:35:01 +1000414 default:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000415 fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000416 }
417 exit(0);
418}
419
420static void
421do_convert_to(struct passwd *pw)
422{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000423 struct sshkey *k;
Damien Miller44b25042010-07-02 13:35:01 +1000424 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000425 int r;
Damien Miller44b25042010-07-02 13:35:01 +1000426
427 if (!have_identity)
428 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000429 if (stat(identity_file, &st) == -1)
Damien Miller44b25042010-07-02 13:35:01 +1000430 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000431 if ((r = sshkey_load_public(identity_file, &k, NULL)) != 0)
djm@openbsd.org07133222019-09-01 23:47:32 +0000432 k = load_identity(identity_file, NULL);
Damien Miller44b25042010-07-02 13:35:01 +1000433 switch (convert_format) {
434 case FMT_RFC4716:
435 do_convert_to_ssh2(pw, k);
436 break;
437 case FMT_PKCS8:
438 do_convert_to_pkcs8(k);
439 break;
440 case FMT_PEM:
441 do_convert_to_pem(k);
442 break;
443 default:
444 fatal("%s: unknown key format %d", __func__, convert_format);
445 }
446 exit(0);
447}
448
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000449/*
450 * This is almost exactly the bignum1 encoding, but with 32 bit for length
451 * instead of 16.
452 */
Damien Miller44b25042010-07-02 13:35:01 +1000453static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000454buffer_get_bignum_bits(struct sshbuf *b, BIGNUM *value)
Damien Miller874d77b2000-10-14 16:23:11 +1100455{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000456 u_int bytes, bignum_bits;
457 int r;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000458
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000459 if ((r = sshbuf_get_u32(b, &bignum_bits)) != 0)
460 fatal("%s: buffer error: %s", __func__, ssh_err(r));
461 bytes = (bignum_bits + 7) / 8;
462 if (sshbuf_len(b) < bytes)
463 fatal("%s: input buffer too small: need %d have %zu",
464 __func__, bytes, sshbuf_len(b));
465 if (BN_bin2bn(sshbuf_ptr(b), bytes, value) == NULL)
466 fatal("%s: BN_bin2bn failed", __func__);
467 if ((r = sshbuf_consume(b, bytes)) != 0)
468 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100469}
470
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000471static struct sshkey *
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000472do_convert_private_ssh2(struct sshbuf *b)
Damien Miller874d77b2000-10-14 16:23:11 +1100473{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000474 struct sshkey *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100475 char *type, *cipher;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000476 u_char e1, e2, e3, *sig = NULL, data[] = "abcde12345";
477 int r, rlen, ktype;
478 u_int magic, i1, i2, i3, i4;
479 size_t slen;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000480 u_long e;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000481 BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
482 BIGNUM *dsa_pub_key = NULL, *dsa_priv_key = NULL;
483 BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
484 BIGNUM *rsa_p = NULL, *rsa_q = NULL, *rsa_iqmp = NULL;
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000485
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000486 if ((r = sshbuf_get_u32(b, &magic)) != 0)
487 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100488
Damien Miller874d77b2000-10-14 16:23:11 +1100489 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000490 error("bad magic 0x%x != 0x%x", magic,
491 SSH_COM_PRIVATE_KEY_MAGIC);
Damien Miller874d77b2000-10-14 16:23:11 +1100492 return NULL;
493 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000494 if ((r = sshbuf_get_u32(b, &i1)) != 0 ||
495 (r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
496 (r = sshbuf_get_cstring(b, &cipher, NULL)) != 0 ||
497 (r = sshbuf_get_u32(b, &i2)) != 0 ||
498 (r = sshbuf_get_u32(b, &i3)) != 0 ||
499 (r = sshbuf_get_u32(b, &i4)) != 0)
500 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller80163902007-01-05 16:30:16 +1100501 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100502 if (strcmp(cipher, "none") != 0) {
503 error("unsupported cipher %s", cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000504 free(cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000505 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100506 return NULL;
507 }
Darren Tuckera627d422013-06-02 07:31:17 +1000508 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100509
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000510 if (strstr(type, "dsa")) {
511 ktype = KEY_DSA;
512 } else if (strstr(type, "rsa")) {
513 ktype = KEY_RSA;
514 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000515 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100516 return NULL;
517 }
djm@openbsd.org6da046f2018-09-14 04:17:44 +0000518 if ((key = sshkey_new(ktype)) == NULL)
519 fatal("sshkey_new failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000520 free(type);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000521
522 switch (key->type) {
523 case KEY_DSA:
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000524 if ((dsa_p = BN_new()) == NULL ||
525 (dsa_q = BN_new()) == NULL ||
526 (dsa_g = BN_new()) == NULL ||
527 (dsa_pub_key = BN_new()) == NULL ||
528 (dsa_priv_key = BN_new()) == NULL)
529 fatal("%s: BN_new", __func__);
530 buffer_get_bignum_bits(b, dsa_p);
531 buffer_get_bignum_bits(b, dsa_g);
532 buffer_get_bignum_bits(b, dsa_q);
533 buffer_get_bignum_bits(b, dsa_pub_key);
534 buffer_get_bignum_bits(b, dsa_priv_key);
535 if (!DSA_set0_pqg(key->dsa, dsa_p, dsa_q, dsa_g))
536 fatal("%s: DSA_set0_pqg failed", __func__);
537 dsa_p = dsa_q = dsa_g = NULL; /* transferred */
538 if (!DSA_set0_key(key->dsa, dsa_pub_key, dsa_priv_key))
539 fatal("%s: DSA_set0_key failed", __func__);
540 dsa_pub_key = dsa_priv_key = NULL; /* transferred */
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000541 break;
542 case KEY_RSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000543 if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
544 (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
545 (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
546 fatal("%s: buffer error: %s", __func__, ssh_err(r));
547 e = e1;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000548 debug("e %lx", e);
549 if (e < 30) {
550 e <<= 8;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000551 e += e2;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000552 debug("e %lx", e);
553 e <<= 8;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000554 e += e3;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000555 debug("e %lx", e);
556 }
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000557 if ((rsa_e = BN_new()) == NULL)
558 fatal("%s: BN_new", __func__);
559 if (!BN_set_word(rsa_e, e)) {
560 BN_clear_free(rsa_e);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000561 sshkey_free(key);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000562 return NULL;
563 }
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000564 if ((rsa_n = BN_new()) == NULL ||
565 (rsa_d = BN_new()) == NULL ||
566 (rsa_p = BN_new()) == NULL ||
567 (rsa_q = BN_new()) == NULL ||
568 (rsa_iqmp = BN_new()) == NULL)
569 fatal("%s: BN_new", __func__);
570 buffer_get_bignum_bits(b, rsa_d);
571 buffer_get_bignum_bits(b, rsa_n);
572 buffer_get_bignum_bits(b, rsa_iqmp);
573 buffer_get_bignum_bits(b, rsa_q);
574 buffer_get_bignum_bits(b, rsa_p);
575 if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, rsa_d))
576 fatal("%s: RSA_set0_key failed", __func__);
577 rsa_n = rsa_e = rsa_d = NULL; /* transferred */
578 if (!RSA_set0_factors(key->rsa, rsa_p, rsa_q))
579 fatal("%s: RSA_set0_factors failed", __func__);
580 rsa_p = rsa_q = NULL; /* transferred */
581 if ((r = ssh_rsa_complete_crt_parameters(key, rsa_iqmp)) != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000582 fatal("generate RSA parameters failed: %s", ssh_err(r));
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000583 BN_clear_free(rsa_iqmp);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000584 break;
585 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000586 rlen = sshbuf_len(b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000587 if (rlen != 0)
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000588 error("%s: remaining bytes in key blob %d", __func__, rlen);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000589
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000590 /* try the key */
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000591 if (sshkey_sign(key, &sig, &slen, data, sizeof(data),
592 NULL, NULL, 0) != 0 ||
593 sshkey_verify(key, sig, slen, data, sizeof(data),
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000594 NULL, 0, NULL) != 0) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000595 sshkey_free(key);
596 free(sig);
597 return NULL;
598 }
Darren Tuckera627d422013-06-02 07:31:17 +1000599 free(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100600 return key;
601}
602
Damien Miller8056a9d2006-03-15 12:05:40 +1100603static int
604get_line(FILE *fp, char *line, size_t len)
605{
606 int c;
607 size_t pos = 0;
608
609 line[0] = '\0';
610 while ((c = fgetc(fp)) != EOF) {
djm@openbsd.org3038a192015-04-17 13:19:22 +0000611 if (pos >= len - 1)
612 fatal("input line too long.");
Damien Miller90967402006-03-26 14:07:26 +1100613 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100614 case '\r':
615 c = fgetc(fp);
djm@openbsd.org3038a192015-04-17 13:19:22 +0000616 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF)
617 fatal("unget: %s", strerror(errno));
Damien Miller8056a9d2006-03-15 12:05:40 +1100618 return pos;
619 case '\n':
620 return pos;
621 }
622 line[pos++] = c;
623 line[pos] = '\0';
624 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100625 /* We reached EOF */
626 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100627}
628
Ben Lindstrombba81212001-06-25 05:01:22 +0000629static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000630do_convert_from_ssh2(struct passwd *pw, struct sshkey **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000631{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000632 int r, blen, escaped = 0;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000633 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100634 char line[1024];
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000635 struct sshbuf *buf;
Damien Millereba71ba2000-04-29 23:57:08 +1000636 char encoded[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000637 FILE *fp;
638
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000639 if ((buf = sshbuf_new()) == NULL)
640 fatal("sshbuf_new failed");
Damien Millerba3420a2010-06-26 09:39:07 +1000641 if ((fp = fopen(identity_file, "r")) == NULL)
642 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000643 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100644 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
Damien Miller746d1a62013-07-18 16:13:02 +1000645 if (blen > 0 && line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000646 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000647 if (strncmp(line, "----", 4) == 0 ||
648 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100649 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000650 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000651 if (strstr(line, " END ") != NULL) {
652 break;
653 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000654 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000655 continue;
656 }
Damien Miller30c3d422000-05-09 11:02:59 +1000657 if (escaped) {
658 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000659 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000660 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000661 }
Damien Millereba71ba2000-04-29 23:57:08 +1000662 strlcat(encoded, line, sizeof(encoded));
663 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000664 len = strlen(encoded);
665 if (((len % 4) == 3) &&
666 (encoded[len-1] == '=') &&
667 (encoded[len-2] == '=') &&
668 (encoded[len-3] == '='))
669 encoded[len-3] = '\0';
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000670 if ((r = sshbuf_b64tod(buf, encoded)) != 0)
671 fatal("%s: base64 decoding failed: %s", __func__, ssh_err(r));
djm@openbsd.org99ce9ce2020-05-01 04:23:11 +0000672 if (*private) {
673 if ((*k = do_convert_private_ssh2(buf)) == NULL)
674 fatal("%s: private key conversion failed", __func__);
675 } else if ((r = sshkey_fromb(buf, k)) != 0)
djm@openbsd.org3038a192015-04-17 13:19:22 +0000676 fatal("decode blob failed: %s", ssh_err(r));
djm@openbsd.org6500c3b2019-10-16 06:03:30 +0000677 sshbuf_free(buf);
Damien Miller44b25042010-07-02 13:35:01 +1000678 fclose(fp);
679}
680
681static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000682do_convert_from_pkcs8(struct sshkey **k, int *private)
Damien Miller44b25042010-07-02 13:35:01 +1000683{
684 EVP_PKEY *pubkey;
685 FILE *fp;
686
687 if ((fp = fopen(identity_file, "r")) == NULL)
688 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
689 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
690 fatal("%s: %s is not a recognised public key format", __func__,
691 identity_file);
692 }
693 fclose(fp);
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000694 switch (EVP_PKEY_base_id(pubkey)) {
Damien Miller44b25042010-07-02 13:35:01 +1000695 case EVP_PKEY_RSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000696 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
697 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000698 (*k)->type = KEY_RSA;
699 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
700 break;
701 case EVP_PKEY_DSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000702 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
703 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000704 (*k)->type = KEY_DSA;
705 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
706 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000707#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000708 case EVP_PKEY_EC:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000709 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
710 fatal("sshkey_new failed");
Damien Millereb8b60e2010-08-31 22:41:14 +1000711 (*k)->type = KEY_ECDSA;
712 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000713 (*k)->ecdsa_nid = sshkey_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000714 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000715#endif
Damien Miller44b25042010-07-02 13:35:01 +1000716 default:
717 fatal("%s: unsupported pubkey type %d", __func__,
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000718 EVP_PKEY_base_id(pubkey));
Damien Miller44b25042010-07-02 13:35:01 +1000719 }
720 EVP_PKEY_free(pubkey);
721 return;
722}
723
724static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000725do_convert_from_pem(struct sshkey **k, int *private)
Damien Miller44b25042010-07-02 13:35:01 +1000726{
727 FILE *fp;
728 RSA *rsa;
Damien Miller44b25042010-07-02 13:35:01 +1000729
730 if ((fp = fopen(identity_file, "r")) == NULL)
731 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
732 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000733 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
734 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000735 (*k)->type = KEY_RSA;
736 (*k)->rsa = rsa;
737 fclose(fp);
738 return;
739 }
Damien Miller44b25042010-07-02 13:35:01 +1000740 fatal("%s: unrecognised raw private key format", __func__);
741}
742
743static void
744do_convert_from(struct passwd *pw)
745{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000746 struct sshkey *k = NULL;
747 int r, private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000748 struct stat st;
749
750 if (!have_identity)
751 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000752 if (stat(identity_file, &st) == -1)
Damien Miller44b25042010-07-02 13:35:01 +1000753 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
754
755 switch (convert_format) {
756 case FMT_RFC4716:
757 do_convert_from_ssh2(pw, &k, &private);
758 break;
759 case FMT_PKCS8:
760 do_convert_from_pkcs8(&k, &private);
761 break;
762 case FMT_PEM:
763 do_convert_from_pem(&k, &private);
764 break;
765 default:
766 fatal("%s: unknown key format %d", __func__, convert_format);
767 }
768
djm@openbsd.org7a2c3682015-01-30 00:59:19 +0000769 if (!private) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000770 if ((r = sshkey_write(k, stdout)) == 0)
771 ok = 1;
Damien Miller44b25042010-07-02 13:35:01 +1000772 if (ok)
773 fprintf(stdout, "\n");
djm@openbsd.org7a2c3682015-01-30 00:59:19 +0000774 } else {
Damien Miller44b25042010-07-02 13:35:01 +1000775 switch (k->type) {
776 case KEY_DSA:
777 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
778 NULL, 0, NULL, NULL);
779 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000780#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000781 case KEY_ECDSA:
782 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
783 NULL, 0, NULL, NULL);
784 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000785#endif
Damien Miller44b25042010-07-02 13:35:01 +1000786 case KEY_RSA:
787 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
788 NULL, 0, NULL, NULL);
789 break;
790 default:
791 fatal("%s: unsupported key type %s", __func__,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000792 sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000793 }
794 }
795
djm@openbsd.org3038a192015-04-17 13:19:22 +0000796 if (!ok)
797 fatal("key write failed");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000798 sshkey_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000799 exit(0);
800}
Damien Miller1f0311c2014-05-15 14:24:09 +1000801#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000802
Ben Lindstrombba81212001-06-25 05:01:22 +0000803static void
Damien Millereba71ba2000-04-29 23:57:08 +1000804do_print_public(struct passwd *pw)
805{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000806 struct sshkey *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000807 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000808 int r;
djm@openbsd.org07133222019-09-01 23:47:32 +0000809 char *comment = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000810
811 if (!have_identity)
812 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000813 if (stat(identity_file, &st) == -1)
djm@openbsd.org3038a192015-04-17 13:19:22 +0000814 fatal("%s: %s", identity_file, strerror(errno));
djm@openbsd.org07133222019-09-01 23:47:32 +0000815 prv = load_identity(identity_file, &comment);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000816 if ((r = sshkey_write(prv, stdout)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +0000817 error("sshkey_write failed: %s", ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000818 sshkey_free(prv);
djm@openbsd.org07133222019-09-01 23:47:32 +0000819 if (comment != NULL && *comment != '\0')
820 fprintf(stdout, " %s", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000821 fprintf(stdout, "\n");
djm@openbsd.org07133222019-09-01 23:47:32 +0000822 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000823 exit(0);
824}
825
Ben Lindstromcd392282001-07-04 03:44:03 +0000826static void
Damien Miller757f34e2010-08-05 13:05:31 +1000827do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000828{
Damien Miller7ea845e2010-02-12 09:21:02 +1100829#ifdef ENABLE_PKCS11
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000830 struct sshkey **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100831 int i, nkeys;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000832 enum sshkey_fp_rep rep;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000833 int fptype;
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000834 char *fp, *ra, **comments = NULL;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000835
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000836 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
837 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
Damien Miller1422c082013-01-09 16:44:54 +1100838
benno@openbsd.orga8c807f2019-02-10 16:35:41 +0000839 pkcs11_init(1);
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000840 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys, &comments);
Damien Miller7ea845e2010-02-12 09:21:02 +1100841 if (nkeys <= 0)
842 fatal("cannot read public key from pkcs11");
843 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100844 if (print_fingerprint) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000845 fp = sshkey_fingerprint(keys[i], fptype, rep);
846 ra = sshkey_fingerprint(keys[i], fingerprint_hash,
Damien Millerec77c952013-01-09 15:58:00 +1100847 SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000848 if (fp == NULL || ra == NULL)
849 fatal("%s: sshkey_fingerprint fail", __func__);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000850 printf("%u %s %s (PKCS11 key)\n", sshkey_size(keys[i]),
851 fp, sshkey_type(keys[i]));
djm@openbsd.org851f8032019-01-23 04:16:22 +0000852 if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
Damien Millerec77c952013-01-09 15:58:00 +1100853 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000854 free(ra);
855 free(fp);
Damien Millerec77c952013-01-09 15:58:00 +1100856 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000857 (void) sshkey_write(keys[i], stdout); /* XXX check */
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000858 fprintf(stdout, "%s%s\n",
859 *(comments[i]) == '\0' ? "" : " ", comments[i]);
Damien Millerec77c952013-01-09 15:58:00 +1100860 }
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000861 free(comments[i]);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000862 sshkey_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000863 }
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000864 free(comments);
Darren Tuckera627d422013-06-02 07:31:17 +1000865 free(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100866 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000867 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100868#else
869 fatal("no pkcs11 support");
870#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000871}
Ben Lindstromcd392282001-07-04 03:44:03 +0000872
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000873static struct sshkey *
874try_read_key(char **cpp)
875{
876 struct sshkey *ret;
877 int r;
878
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000879 if ((ret = sshkey_new(KEY_UNSPEC)) == NULL)
880 fatal("sshkey_new failed");
881 if ((r = sshkey_read(ret, cpp)) == 0)
882 return ret;
883 /* Not a key */
884 sshkey_free(ret);
885 return NULL;
886}
887
888static void
889fingerprint_one_key(const struct sshkey *public, const char *comment)
890{
891 char *fp = NULL, *ra = NULL;
892 enum sshkey_fp_rep rep;
893 int fptype;
894
895 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
896 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
897 fp = sshkey_fingerprint(public, fptype, rep);
898 ra = sshkey_fingerprint(public, fingerprint_hash, SSH_FP_RANDOMART);
899 if (fp == NULL || ra == NULL)
900 fatal("%s: sshkey_fingerprint failed", __func__);
djm@openbsd.orga287c5a2017-02-10 03:36:40 +0000901 mprintf("%u %s %s (%s)\n", sshkey_size(public), fp,
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000902 comment ? comment : "no comment", sshkey_type(public));
djm@openbsd.org851f8032019-01-23 04:16:22 +0000903 if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000904 printf("%s\n", ra);
905 free(ra);
906 free(fp);
907}
908
909static void
910fingerprint_private(const char *path)
911{
912 struct stat st;
913 char *comment = NULL;
djm@openbsd.orga98d5ba2020-04-20 04:43:57 +0000914 struct sshkey *privkey = NULL, *pubkey = NULL;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000915 int r;
916
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000917 if (stat(identity_file, &st) == -1)
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000918 fatal("%s: %s", path, strerror(errno));
djm@openbsd.orga98d5ba2020-04-20 04:43:57 +0000919 if ((r = sshkey_load_public(path, &pubkey, &comment)) != 0)
920 debug("load public \"%s\": %s", path, ssh_err(r));
921 if (pubkey == NULL || comment == NULL || *comment == '\0') {
922 free(comment);
923 if ((r = sshkey_load_private(path, NULL,
924 &privkey, &comment)) != 0)
925 debug("load private \"%s\": %s", path, ssh_err(r));
djm@openbsd.orgbcb7bc72015-11-18 08:37:28 +0000926 }
djm@openbsd.orga98d5ba2020-04-20 04:43:57 +0000927 if (pubkey == NULL && privkey == NULL)
928 fatal("%s is not a key file.", path);
djm@openbsd.orgbcb7bc72015-11-18 08:37:28 +0000929
djm@openbsd.orga98d5ba2020-04-20 04:43:57 +0000930 fingerprint_one_key(pubkey == NULL ? privkey : pubkey, comment);
931 sshkey_free(pubkey);
932 sshkey_free(privkey);
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000933 free(comment);
934}
935
Ben Lindstrombba81212001-06-25 05:01:22 +0000936static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100937do_fingerprint(struct passwd *pw)
938{
Damien Miller98c7ad62000-03-09 21:27:49 +1100939 FILE *f;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000940 struct sshkey *public = NULL;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000941 char *comment = NULL, *cp, *ep, *line = NULL;
942 size_t linesize = 0;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000943 int i, invalid = 1;
944 const char *path;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000945 u_long lnum = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100946
Damien Miller95def091999-11-25 00:26:21 +1100947 if (!have_identity)
948 ask_filename(pw, "Enter file in which the key is");
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000949 path = identity_file;
Damien Miller98c7ad62000-03-09 21:27:49 +1100950
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000951 if (strcmp(identity_file, "-") == 0) {
952 f = stdin;
953 path = "(stdin)";
954 } else if ((f = fopen(path, "r")) == NULL)
955 fatal("%s: %s: %s", __progname, path, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100956
markus@openbsd.org7f906352018-06-06 18:29:18 +0000957 while (getline(&line, &linesize, f) != -1) {
958 lnum++;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000959 cp = line;
960 cp[strcspn(cp, "\n")] = '\0';
961 /* Trim leading space and comments */
962 cp = line + strspn(line, " \t");
963 if (*cp == '#' || *cp == '\0')
964 continue;
965
966 /*
967 * Input may be plain keys, private keys, authorized_keys
968 * or known_hosts.
969 */
970
971 /*
972 * Try private keys first. Assume a key is private if
973 * "SSH PRIVATE KEY" appears on the first line and we're
974 * not reading from stdin (XXX support private keys on stdin).
975 */
976 if (lnum == 1 && strcmp(identity_file, "-") != 0 &&
djm@openbsd.orgbcb7bc72015-11-18 08:37:28 +0000977 strstr(cp, "PRIVATE KEY") != NULL) {
markus@openbsd.org7f906352018-06-06 18:29:18 +0000978 free(line);
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000979 fclose(f);
980 fingerprint_private(path);
981 exit(0);
982 }
983
984 /*
985 * If it's not a private key, then this must be prepared to
986 * accept a public key prefixed with a hostname or options.
987 * Try a bare key first, otherwise skip the leading stuff.
988 */
989 if ((public = try_read_key(&cp)) == NULL) {
990 i = strtol(cp, &ep, 10);
991 if (i == 0 || ep == NULL ||
992 (*ep != ' ' && *ep != '\t')) {
993 int quoted = 0;
994
995 comment = cp;
996 for (; *cp && (quoted || (*cp != ' ' &&
997 *cp != '\t')); cp++) {
998 if (*cp == '\\' && cp[1] == '"')
999 cp++; /* Skip both */
1000 else if (*cp == '"')
1001 quoted = !quoted;
1002 }
1003 if (!*cp)
1004 continue;
1005 *cp++ = '\0';
1006 }
1007 }
1008 /* Retry after parsing leading hostname/key options */
1009 if (public == NULL && (public = try_read_key(&cp)) == NULL) {
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001010 debug("%s:%lu: not a public key", path, lnum);
Damien Millerba3420a2010-06-26 09:39:07 +10001011 continue;
Damien Miller95def091999-11-25 00:26:21 +11001012 }
Damien Millerba3420a2010-06-26 09:39:07 +10001013
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001014 /* Find trailing comment, if any */
1015 for (; *cp == ' ' || *cp == '\t'; cp++)
Damien Millerba3420a2010-06-26 09:39:07 +10001016 ;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001017 if (*cp != '\0' && *cp != '#')
Damien Millerba3420a2010-06-26 09:39:07 +10001018 comment = cp;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001019
1020 fingerprint_one_key(public, comment);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001021 sshkey_free(public);
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001022 invalid = 0; /* One good key in the file is sufficient */
Damien Miller95def091999-11-25 00:26:21 +11001023 }
Damien Millerba3420a2010-06-26 09:39:07 +10001024 fclose(f);
markus@openbsd.org7f906352018-06-06 18:29:18 +00001025 free(line);
Damien Millerba3420a2010-06-26 09:39:07 +10001026
djm@openbsd.org3038a192015-04-17 13:19:22 +00001027 if (invalid)
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001028 fatal("%s is not a public key file.", path);
Damien Miller95def091999-11-25 00:26:21 +11001029 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +11001030}
1031
Damien Miller4b42d7f2005-03-01 21:48:35 +11001032static void
Damien Miller58f1baf2011-05-05 14:06:15 +10001033do_gen_all_hostkeys(struct passwd *pw)
1034{
1035 struct {
1036 char *key_type;
1037 char *key_type_display;
1038 char *path;
1039 } key_types[] = {
djm@openbsd.org5c27e3b2015-03-23 06:06:38 +00001040#ifdef WITH_OPENSSL
Damien Miller58f1baf2011-05-05 14:06:15 +10001041 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
1042 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +11001043#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +10001044 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
djm@openbsd.org5c27e3b2015-03-23 06:06:38 +00001045#endif /* OPENSSL_HAS_ECC */
1046#endif /* WITH_OPENSSL */
Damien Miller5be9d9e2013-12-07 11:24:01 +11001047 { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001048#ifdef WITH_XMSS
1049 { "xmss", "XMSS",_PATH_HOST_XMSS_KEY_FILE },
1050#endif /* WITH_XMSS */
Damien Miller58f1baf2011-05-05 14:06:15 +10001051 { NULL, NULL, NULL }
1052 };
1053
dtucker@openbsd.org7afc45c2019-08-08 08:02:57 +00001054 u_int32_t bits = 0;
Damien Miller58f1baf2011-05-05 14:06:15 +10001055 int first = 0;
1056 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001057 struct sshkey *private, *public;
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001058 char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001059 int i, type, fd, r;
Damien Miller58f1baf2011-05-05 14:06:15 +10001060
1061 for (i = 0; key_types[i].key_type; i++) {
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001062 public = private = NULL;
1063 prv_tmp = pub_tmp = prv_file = pub_file = NULL;
1064
1065 xasprintf(&prv_file, "%s%s",
1066 identity_file, key_types[i].path);
1067
1068 /* Check whether private key exists and is not zero-length */
1069 if (stat(prv_file, &st) == 0) {
1070 if (st.st_size != 0)
1071 goto next;
1072 } else if (errno != ENOENT) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001073 error("Could not stat %s: %s", key_types[i].path,
Damien Miller58f1baf2011-05-05 14:06:15 +10001074 strerror(errno));
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001075 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001076 }
1077
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001078 /*
1079 * Private key doesn't exist or is invalid; proceed with
1080 * key generation.
1081 */
1082 xasprintf(&prv_tmp, "%s%s.XXXXXXXXXX",
1083 identity_file, key_types[i].path);
1084 xasprintf(&pub_tmp, "%s%s.pub.XXXXXXXXXX",
1085 identity_file, key_types[i].path);
1086 xasprintf(&pub_file, "%s%s.pub",
1087 identity_file, key_types[i].path);
1088
Damien Miller58f1baf2011-05-05 14:06:15 +10001089 if (first == 0) {
1090 first = 1;
1091 printf("%s: generating new host keys: ", __progname);
1092 }
1093 printf("%s ", key_types[i].key_type_display);
1094 fflush(stdout);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001095 type = sshkey_type_from_name(key_types[i].key_type);
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001096 if ((fd = mkstemp(prv_tmp)) == -1) {
djm@openbsd.orgd25d6302020-05-02 07:19:43 +00001097 error("Could not save your private key in %s: %s",
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001098 prv_tmp, strerror(errno));
1099 goto failnext;
1100 }
djm@openbsd.orgd25d6302020-05-02 07:19:43 +00001101 (void)close(fd); /* just using mkstemp() to reserve a name */
Damien Miller58f1baf2011-05-05 14:06:15 +10001102 bits = 0;
djm@openbsd.org7efb4552015-01-18 13:22:28 +00001103 type_bits_valid(type, NULL, &bits);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001104 if ((r = sshkey_generate(type, bits, &private)) != 0) {
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001105 error("sshkey_generate failed: %s", ssh_err(r));
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001106 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001107 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001108 if ((r = sshkey_from_private(private, &public)) != 0)
1109 fatal("sshkey_from_private failed: %s", ssh_err(r));
Damien Miller58f1baf2011-05-05 14:06:15 +10001110 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
1111 hostname);
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001112 if ((r = sshkey_save_private(private, prv_tmp, "",
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00001113 comment, private_key_format, openssh_format_cipher,
1114 rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001115 error("Saving key \"%s\" failed: %s",
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001116 prv_tmp, ssh_err(r));
1117 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001118 }
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001119 if ((fd = mkstemp(pub_tmp)) == -1) {
1120 error("Could not save your public key in %s: %s",
1121 pub_tmp, strerror(errno));
1122 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001123 }
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001124 (void)fchmod(fd, 0644);
djm@openbsd.orgd25d6302020-05-02 07:19:43 +00001125 (void)close(fd);
1126 if ((r = sshkey_save_public(public, pub_tmp, comment)) != 0) {
1127 fatal("Unable to save public key to %s: %s",
1128 identity_file, ssh_err(r));
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001129 goto failnext;
1130 }
Damien Miller58f1baf2011-05-05 14:06:15 +10001131
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001132 /* Rename temporary files to their permanent locations. */
1133 if (rename(pub_tmp, pub_file) != 0) {
1134 error("Unable to move %s into position: %s",
1135 pub_file, strerror(errno));
1136 goto failnext;
1137 }
1138 if (rename(prv_tmp, prv_file) != 0) {
1139 error("Unable to move %s into position: %s",
1140 key_types[i].path, strerror(errno));
1141 failnext:
1142 first = 0;
1143 goto next;
1144 }
1145 next:
1146 sshkey_free(private);
1147 sshkey_free(public);
1148 free(prv_tmp);
1149 free(pub_tmp);
1150 free(prv_file);
1151 free(pub_file);
Damien Miller58f1baf2011-05-05 14:06:15 +10001152 }
1153 if (first != 0)
1154 printf("\n");
1155}
1156
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001157struct known_hosts_ctx {
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001158 const char *host; /* Hostname searched for in find/delete case */
1159 FILE *out; /* Output file, stdout for find_hosts case */
1160 int has_unhashed; /* When hashing, original had unhashed hosts */
1161 int found_key; /* For find/delete, host was found */
1162 int invalid; /* File contained invalid items; don't delete */
djm@openbsd.org851f8032019-01-23 04:16:22 +00001163 int hash_hosts; /* Hash hostnames as we go */
1164 int find_host; /* Search for specific hostname */
1165 int delete_host; /* Delete host from known_hosts */
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001166};
1167
1168static int
1169known_hosts_hash(struct hostkey_foreach_line *l, void *_ctx)
Damien Miller4b42d7f2005-03-01 21:48:35 +11001170{
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001171 struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
1172 char *hashed, *cp, *hosts, *ohosts;
1173 int has_wild = l->hosts && strcspn(l->hosts, "*?!") != strlen(l->hosts);
dtucker@openbsd.org18501152017-03-06 02:03:20 +00001174 int was_hashed = l->hosts && l->hosts[0] == HASH_DELIM;
Darren Tucker0f7e9102008-06-08 12:54:29 +10001175
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001176 switch (l->status) {
1177 case HKF_STATUS_OK:
1178 case HKF_STATUS_MATCHED:
1179 /*
1180 * Don't hash hosts already already hashed, with wildcard
1181 * characters or a CA/revocation marker.
1182 */
djm@openbsd.org12d37672017-03-03 06:13:11 +00001183 if (was_hashed || has_wild || l->marker != MRK_NONE) {
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001184 fprintf(ctx->out, "%s\n", l->line);
djm@openbsd.org851f8032019-01-23 04:16:22 +00001185 if (has_wild && !ctx->find_host) {
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001186 logit("%s:%lu: ignoring host name "
djm@openbsd.org3038a192015-04-17 13:19:22 +00001187 "with wildcard: %.64s", l->path,
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001188 l->linenum, l->hosts);
1189 }
1190 return 0;
1191 }
1192 /*
1193 * Split any comma-separated hostnames from the host list,
1194 * hash and store separately.
1195 */
1196 ohosts = hosts = xstrdup(l->hosts);
1197 while ((cp = strsep(&hosts, ",")) != NULL && *cp != '\0') {
djm@openbsd.orgdb259722017-03-10 04:26:06 +00001198 lowercase(cp);
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001199 if ((hashed = host_hash(cp, NULL, 0)) == NULL)
1200 fatal("hash_host failed");
1201 fprintf(ctx->out, "%s %s\n", hashed, l->rawkey);
1202 ctx->has_unhashed = 1;
1203 }
1204 free(ohosts);
1205 return 0;
1206 case HKF_STATUS_INVALID:
1207 /* Retain invalid lines, but mark file as invalid. */
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001208 ctx->invalid = 1;
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001209 logit("%s:%lu: invalid line", l->path, l->linenum);
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001210 /* FALLTHROUGH */
1211 default:
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001212 fprintf(ctx->out, "%s\n", l->line);
1213 return 0;
Darren Tucker0f7e9102008-06-08 12:54:29 +10001214 }
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001215 /* NOTREACHED */
1216 return -1;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001217}
1218
1219static int
1220known_hosts_find_delete(struct hostkey_foreach_line *l, void *_ctx)
1221{
1222 struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001223 enum sshkey_fp_rep rep;
1224 int fptype;
djm@openbsd.orgbca05822019-07-19 03:38:01 +00001225 char *fp = NULL, *ra = NULL;
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001226
1227 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
1228 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001229
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001230 if (l->status == HKF_STATUS_MATCHED) {
djm@openbsd.org851f8032019-01-23 04:16:22 +00001231 if (ctx->delete_host) {
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001232 if (l->marker != MRK_NONE) {
1233 /* Don't remove CA and revocation lines */
1234 fprintf(ctx->out, "%s\n", l->line);
1235 } else {
1236 /*
1237 * Hostname matches and has no CA/revoke
1238 * marker, delete it by *not* writing the
1239 * line to ctx->out.
1240 */
1241 ctx->found_key = 1;
1242 if (!quiet)
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001243 printf("# Host %s found: line %lu\n",
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001244 ctx->host, l->linenum);
1245 }
1246 return 0;
djm@openbsd.org851f8032019-01-23 04:16:22 +00001247 } else if (ctx->find_host) {
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001248 ctx->found_key = 1;
1249 if (!quiet) {
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001250 printf("# Host %s found: line %lu %s\n",
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001251 ctx->host,
1252 l->linenum, l->marker == MRK_CA ? "CA" :
1253 (l->marker == MRK_REVOKE ? "REVOKED" : ""));
1254 }
djm@openbsd.org851f8032019-01-23 04:16:22 +00001255 if (ctx->hash_hosts)
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001256 known_hosts_hash(l, ctx);
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001257 else if (print_fingerprint) {
1258 fp = sshkey_fingerprint(l->key, fptype, rep);
djm@openbsd.orgbca05822019-07-19 03:38:01 +00001259 ra = sshkey_fingerprint(l->key,
1260 fingerprint_hash, SSH_FP_RANDOMART);
1261 if (fp == NULL || ra == NULL)
1262 fatal("%s: sshkey_fingerprint failed",
1263 __func__);
claudio@openbsd.org4a32c0c2020-01-22 04:51:51 +00001264 mprintf("%s %s %s%s%s\n", ctx->host,
1265 sshkey_type(l->key), fp,
1266 l->comment[0] ? " " : "",
1267 l->comment);
djm@openbsd.orgbca05822019-07-19 03:38:01 +00001268 if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
1269 printf("%s\n", ra);
1270 free(ra);
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001271 free(fp);
1272 } else
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001273 fprintf(ctx->out, "%s\n", l->line);
1274 return 0;
1275 }
djm@openbsd.org851f8032019-01-23 04:16:22 +00001276 } else if (ctx->delete_host) {
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001277 /* Retain non-matching hosts when deleting */
1278 if (l->status == HKF_STATUS_INVALID) {
1279 ctx->invalid = 1;
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001280 logit("%s:%lu: invalid line", l->path, l->linenum);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001281 }
1282 fprintf(ctx->out, "%s\n", l->line);
1283 }
1284 return 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001285}
1286
1287static void
djm@openbsd.org851f8032019-01-23 04:16:22 +00001288do_known_hosts(struct passwd *pw, const char *name, int find_host,
1289 int delete_host, int hash_hosts)
Damien Miller4b42d7f2005-03-01 21:48:35 +11001290{
deraadt@openbsd.orgd2099de2015-01-19 00:32:54 +00001291 char *cp, tmp[PATH_MAX], old[PATH_MAX];
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001292 int r, fd, oerrno, inplace = 0;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001293 struct known_hosts_ctx ctx;
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001294 u_int foreach_options;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001295
1296 if (!have_identity) {
1297 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1298 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1299 sizeof(identity_file))
1300 fatal("Specified known hosts path too long");
Darren Tuckera627d422013-06-02 07:31:17 +10001301 free(cp);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001302 have_identity = 1;
1303 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001304
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001305 memset(&ctx, 0, sizeof(ctx));
1306 ctx.out = stdout;
1307 ctx.host = name;
djm@openbsd.org851f8032019-01-23 04:16:22 +00001308 ctx.hash_hosts = hash_hosts;
1309 ctx.find_host = find_host;
1310 ctx.delete_host = delete_host;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001311
Damien Miller4b42d7f2005-03-01 21:48:35 +11001312 /*
1313 * Find hosts goes to stdout, hash and deletions happen in-place
1314 * A corner case is ssh-keygen -HF foo, which should go to stdout
1315 */
1316 if (!find_host && (hash_hosts || delete_host)) {
1317 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1318 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1319 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1320 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1321 fatal("known_hosts path too long");
1322 umask(077);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001323 if ((fd = mkstemp(tmp)) == -1)
Damien Miller4b42d7f2005-03-01 21:48:35 +11001324 fatal("mkstemp: %s", strerror(errno));
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001325 if ((ctx.out = fdopen(fd, "w")) == NULL) {
1326 oerrno = errno;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001327 unlink(tmp);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001328 fatal("fdopen: %s", strerror(oerrno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001329 }
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001330 inplace = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001331 }
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001332 /* XXX support identity_file == "-" for stdin */
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001333 foreach_options = find_host ? HKF_WANT_MATCH : 0;
1334 foreach_options |= print_fingerprint ? HKF_WANT_PARSE_KEY : 0;
djm@openbsd.org01b048c2018-06-01 04:21:29 +00001335 if ((r = hostkeys_foreach(identity_file, (find_host || !hash_hosts) ?
djm@openbsd.org3e088aa2018-06-01 03:51:34 +00001336 known_hosts_find_delete : known_hosts_hash, &ctx, name, NULL,
1337 foreach_options)) != 0) {
deraadt@openbsd.org6da413c2015-11-28 06:50:52 +00001338 if (inplace)
1339 unlink(tmp);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001340 fatal("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
deraadt@openbsd.org6da413c2015-11-28 06:50:52 +00001341 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001342
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001343 if (inplace)
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001344 fclose(ctx.out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001345
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001346 if (ctx.invalid) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001347 error("%s is not a valid known_hosts file.", identity_file);
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001348 if (inplace) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001349 error("Not replacing existing known_hosts "
1350 "file because of errors");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001351 unlink(tmp);
1352 }
1353 exit(1);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001354 } else if (delete_host && !ctx.found_key) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001355 logit("Host %s not found in %s", name, identity_file);
djm@openbsd.orgc8376432015-08-19 23:17:51 +00001356 if (inplace)
1357 unlink(tmp);
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001358 } else if (inplace) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001359 /* Backup existing file */
1360 if (unlink(old) == -1 && errno != ENOENT)
1361 fatal("unlink %.100s: %s", old, strerror(errno));
1362 if (link(identity_file, old) == -1)
1363 fatal("link %.100s to %.100s: %s", identity_file, old,
1364 strerror(errno));
1365 /* Move new one into place */
1366 if (rename(tmp, identity_file) == -1) {
1367 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1368 strerror(errno));
1369 unlink(tmp);
1370 unlink(old);
1371 exit(1);
1372 }
1373
djm@openbsd.org3038a192015-04-17 13:19:22 +00001374 printf("%s updated.\n", identity_file);
1375 printf("Original contents retained as %s\n", old);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001376 if (ctx.has_unhashed) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001377 logit("WARNING: %s contains unhashed entries", old);
1378 logit("Delete this file to ensure privacy "
1379 "of hostnames");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001380 }
1381 }
1382
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001383 exit (find_host && !ctx.found_key);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001384}
1385
Damien Miller95def091999-11-25 00:26:21 +11001386/*
1387 * Perform changing a passphrase. The argument is the passwd structure
1388 * for the current user.
1389 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001390static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001391do_change_passphrase(struct passwd *pw)
1392{
Damien Miller95def091999-11-25 00:26:21 +11001393 char *comment;
1394 char *old_passphrase, *passphrase1, *passphrase2;
1395 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001396 struct sshkey *private;
1397 int r;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001398
Damien Miller95def091999-11-25 00:26:21 +11001399 if (!have_identity)
1400 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001401 if (stat(identity_file, &st) == -1)
djm@openbsd.org3038a192015-04-17 13:19:22 +00001402 fatal("%s: %s", identity_file, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001403 /* Try to load the file with empty passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001404 r = sshkey_load_private(identity_file, "", &private, &comment);
1405 if (r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
Damien Miller95def091999-11-25 00:26:21 +11001406 if (identity_passphrase)
1407 old_passphrase = xstrdup(identity_passphrase);
1408 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001409 old_passphrase =
1410 read_passphrase("Enter old passphrase: ",
1411 RP_ALLOW_STDIN);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001412 r = sshkey_load_private(identity_file, old_passphrase,
1413 &private, &comment);
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +00001414 freezero(old_passphrase, strlen(old_passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001415 if (r != 0)
1416 goto badkey;
1417 } else if (r != 0) {
1418 badkey:
djm@openbsd.org3038a192015-04-17 13:19:22 +00001419 fatal("Failed to load key %s: %s", identity_file, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001420 }
djm@openbsd.orgf43d1722015-02-26 20:45:47 +00001421 if (comment)
djm@openbsd.orga287c5a2017-02-10 03:36:40 +00001422 mprintf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423
Damien Miller95def091999-11-25 00:26:21 +11001424 /* Ask the new passphrase (twice). */
1425 if (identity_new_passphrase) {
1426 passphrase1 = xstrdup(identity_new_passphrase);
1427 passphrase2 = NULL;
1428 } else {
1429 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001430 read_passphrase("Enter new passphrase (empty for no "
1431 "passphrase): ", RP_ALLOW_STDIN);
1432 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001433 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001434
1435 /* Verify that they are the same. */
1436 if (strcmp(passphrase1, passphrase2) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001437 explicit_bzero(passphrase1, strlen(passphrase1));
1438 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001439 free(passphrase1);
1440 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001441 printf("Pass phrases do not match. Try again.\n");
1442 exit(1);
1443 }
1444 /* Destroy the other copy. */
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +00001445 freezero(passphrase2, strlen(passphrase2));
Damien Miller95def091999-11-25 00:26:21 +11001446 }
1447
1448 /* Save the file using the new passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001449 if ((r = sshkey_save_private(private, identity_file, passphrase1,
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00001450 comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001451 error("Saving key \"%s\" failed: %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001452 identity_file, ssh_err(r));
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +00001453 freezero(passphrase1, strlen(passphrase1));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001454 sshkey_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001455 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001456 exit(1);
1457 }
1458 /* Destroy the passphrase and the copy of the key in memory. */
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +00001459 freezero(passphrase1, strlen(passphrase1));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001460 sshkey_free(private); /* Destroys contents */
Darren Tuckera627d422013-06-02 07:31:17 +10001461 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001462
1463 printf("Your identification has been saved with the new passphrase.\n");
1464 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001465}
1466
Damien Miller37876e92003-05-15 10:19:46 +10001467/*
1468 * Print the SSHFP RR.
1469 */
Damien Millercb314822006-03-26 13:48:01 +11001470static int
djm@openbsd.org851f8032019-01-23 04:16:22 +00001471do_print_resource_record(struct passwd *pw, char *fname, char *hname,
1472 int print_generic)
Damien Miller37876e92003-05-15 10:19:46 +10001473{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001474 struct sshkey *public;
Damien Miller37876e92003-05-15 10:19:46 +10001475 char *comment = NULL;
1476 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001477 int r;
Damien Miller37876e92003-05-15 10:19:46 +10001478
Damien Millercb314822006-03-26 13:48:01 +11001479 if (fname == NULL)
Damien Miller5bb88332013-07-18 16:13:37 +10001480 fatal("%s: no filename", __func__);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001481 if (stat(fname, &st) == -1) {
Damien Millercb314822006-03-26 13:48:01 +11001482 if (errno == ENOENT)
1483 return 0;
djm@openbsd.org3038a192015-04-17 13:19:22 +00001484 fatal("%s: %s", fname, strerror(errno));
Damien Miller37876e92003-05-15 10:19:46 +10001485 }
djm@openbsd.org3038a192015-04-17 13:19:22 +00001486 if ((r = sshkey_load_public(fname, &public, &comment)) != 0)
1487 fatal("Failed to read v2 public key from \"%s\": %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001488 fname, ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001489 export_dns_rr(hname, public, stdout, print_generic);
1490 sshkey_free(public);
1491 free(comment);
1492 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001493}
Damien Miller37876e92003-05-15 10:19:46 +10001494
Damien Miller95def091999-11-25 00:26:21 +11001495/*
1496 * Change the comment of a private key file.
1497 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001498static void
djm@openbsd.org851f8032019-01-23 04:16:22 +00001499do_change_comment(struct passwd *pw, const char *identity_comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001501 char new_comment[1024], *comment, *passphrase;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001502 struct sshkey *private;
1503 struct sshkey *public;
Damien Miller95def091999-11-25 00:26:21 +11001504 struct stat st;
djm@openbsd.orgd25d6302020-05-02 07:19:43 +00001505 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506
Damien Miller95def091999-11-25 00:26:21 +11001507 if (!have_identity)
1508 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001509 if (stat(identity_file, &st) == -1)
djm@openbsd.org3038a192015-04-17 13:19:22 +00001510 fatal("%s: %s", identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001511 if ((r = sshkey_load_private(identity_file, "",
1512 &private, &comment)) == 0)
1513 passphrase = xstrdup("");
djm@openbsd.org3038a192015-04-17 13:19:22 +00001514 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
1515 fatal("Cannot load private key \"%s\": %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001516 identity_file, ssh_err(r));
djm@openbsd.org3038a192015-04-17 13:19:22 +00001517 else {
Damien Miller95def091999-11-25 00:26:21 +11001518 if (identity_passphrase)
1519 passphrase = xstrdup(identity_passphrase);
1520 else if (identity_new_passphrase)
1521 passphrase = xstrdup(identity_new_passphrase);
1522 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001523 passphrase = read_passphrase("Enter passphrase: ",
1524 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001525 /* Try to load using the passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001526 if ((r = sshkey_load_private(identity_file, passphrase,
1527 &private, &comment)) != 0) {
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +00001528 freezero(passphrase, strlen(passphrase));
djm@openbsd.org3038a192015-04-17 13:19:22 +00001529 fatal("Cannot load private key \"%s\": %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001530 identity_file, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001531 }
1532 }
halex@openbsd.org4d906252015-11-20 23:04:01 +00001533
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001534 if (private->type != KEY_ED25519 && private->type != KEY_XMSS &&
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00001535 private_key_format != SSHKEY_PRIVATE_OPENSSH) {
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001536 error("Comments are only supported for keys stored in "
halex@openbsd.org4d906252015-11-20 23:04:01 +00001537 "the new format (-o).");
tobias@openbsd.org704d8c82015-03-31 11:06:49 +00001538 explicit_bzero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001539 sshkey_free(private);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001540 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001541 }
millert@openbsd.orge40269b2017-02-08 20:32:43 +00001542 if (comment)
lum@openbsd.org410b2312019-05-29 08:30:26 +00001543 printf("Old comment: %s\n", comment);
millert@openbsd.orge40269b2017-02-08 20:32:43 +00001544 else
lum@openbsd.org410b2312019-05-29 08:30:26 +00001545 printf("No existing comment\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001546
Damien Miller95def091999-11-25 00:26:21 +11001547 if (identity_comment) {
1548 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1549 } else {
lum@openbsd.org410b2312019-05-29 08:30:26 +00001550 printf("New comment: ");
Damien Miller95def091999-11-25 00:26:21 +11001551 fflush(stdout);
1552 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
Damien Millera5103f42014-02-04 11:20:14 +11001553 explicit_bzero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001554 sshkey_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001555 exit(1);
1556 }
Damien Miller14b017d2007-09-17 16:09:15 +10001557 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001558 }
lum@openbsd.org410b2312019-05-29 08:30:26 +00001559 if (comment != NULL && strcmp(comment, new_comment) == 0) {
1560 printf("No change to comment\n");
1561 free(passphrase);
1562 sshkey_free(private);
1563 free(comment);
1564 exit(0);
1565 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001566
Damien Miller95def091999-11-25 00:26:21 +11001567 /* Save the file using the new passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001568 if ((r = sshkey_save_private(private, identity_file, passphrase,
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00001569 new_comment, private_key_format, openssh_format_cipher,
1570 rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001571 error("Saving key \"%s\" failed: %s",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001572 identity_file, ssh_err(r));
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +00001573 freezero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001574 sshkey_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001575 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001576 exit(1);
1577 }
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +00001578 freezero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001579 if ((r = sshkey_from_private(private, &public)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001580 fatal("sshkey_from_private failed: %s", ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001581 sshkey_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001582
Damien Miller95def091999-11-25 00:26:21 +11001583 strlcat(identity_file, ".pub", sizeof(identity_file));
djm@openbsd.orgd25d6302020-05-02 07:19:43 +00001584 if ((r = sshkey_save_public(public, identity_file, new_comment)) != 0) {
1585 fatal("Unable to save public key to %s: %s",
1586 identity_file, ssh_err(r));
1587 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001588 sshkey_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001589 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001590
lum@openbsd.org410b2312019-05-29 08:30:26 +00001591 if (strlen(new_comment) > 0)
1592 printf("Comment '%s' applied\n", new_comment);
1593 else
1594 printf("Comment removed\n");
1595
Damien Miller95def091999-11-25 00:26:21 +11001596 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001597}
1598
Damien Miller0a80ca12010-02-27 07:55:05 +11001599static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001600add_flag_option(struct sshbuf *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001601{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001602 int r;
1603
Damien Miller0a80ca12010-02-27 07:55:05 +11001604 debug3("%s: %s", __func__, name);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001605 if ((r = sshbuf_put_cstring(c, name)) != 0 ||
1606 (r = sshbuf_put_string(c, NULL, 0)) != 0)
1607 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001608}
1609
1610static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001611add_string_option(struct sshbuf *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001612{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001613 struct sshbuf *b;
1614 int r;
Damien Miller0a80ca12010-02-27 07:55:05 +11001615
1616 debug3("%s: %s=%s", __func__, name, value);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001617 if ((b = sshbuf_new()) == NULL)
1618 fatal("%s: sshbuf_new failed", __func__);
1619 if ((r = sshbuf_put_cstring(b, value)) != 0 ||
1620 (r = sshbuf_put_cstring(c, name)) != 0 ||
1621 (r = sshbuf_put_stringb(c, b)) != 0)
1622 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001623
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001624 sshbuf_free(b);
Damien Miller0a80ca12010-02-27 07:55:05 +11001625}
1626
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001627#define OPTIONS_CRITICAL 1
1628#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001629static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001630prepare_options_buf(struct sshbuf *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001631{
djm@openbsd.org249516e2017-04-29 04:12:25 +00001632 size_t i;
1633
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001634 sshbuf_reset(c);
Damien Miller1da63882010-08-05 13:03:51 +10001635 if ((which & OPTIONS_CRITICAL) != 0 &&
1636 certflags_command != NULL)
1637 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001638 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001639 (certflags_flags & CERTOPT_X_FWD) != 0)
1640 add_flag_option(c, "permit-X11-forwarding");
1641 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001642 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001643 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001644 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1645 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001646 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001647 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1648 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001649 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001650 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1651 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001652 add_flag_option(c, "permit-user-rc");
djm@openbsd.org9b47bd72020-02-28 01:07:28 +00001653 if ((which & OPTIONS_EXTENSIONS) != 0 &&
djm@openbsd.org2e712632019-11-25 00:54:23 +00001654 (certflags_flags & CERTOPT_NO_REQUIRE_USER_PRESENCE) != 0)
1655 add_flag_option(c, "no-touch-required");
1656 if ((which & OPTIONS_CRITICAL) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001657 certflags_src_addr != NULL)
1658 add_string_option(c, "source-address", certflags_src_addr);
djm@openbsd.org249516e2017-04-29 04:12:25 +00001659 for (i = 0; i < ncert_userext; i++) {
1660 if ((cert_userext[i].crit && (which & OPTIONS_EXTENSIONS)) ||
1661 (!cert_userext[i].crit && (which & OPTIONS_CRITICAL)))
1662 continue;
1663 if (cert_userext[i].val == NULL)
1664 add_flag_option(c, cert_userext[i].key);
1665 else {
1666 add_string_option(c, cert_userext[i].key,
1667 cert_userext[i].val);
1668 }
1669 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001670}
1671
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001672static struct sshkey *
Damien Miller757f34e2010-08-05 13:05:31 +10001673load_pkcs11_key(char *path)
1674{
1675#ifdef ENABLE_PKCS11
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001676 struct sshkey **keys = NULL, *public, *private = NULL;
1677 int r, i, nkeys;
Damien Miller757f34e2010-08-05 13:05:31 +10001678
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001679 if ((r = sshkey_load_public(path, &public, NULL)) != 0)
1680 fatal("Couldn't load CA public key \"%s\": %s",
1681 path, ssh_err(r));
Damien Miller757f34e2010-08-05 13:05:31 +10001682
djm@openbsd.org89a8d452020-01-25 00:03:36 +00001683 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase,
1684 &keys, NULL);
Damien Miller757f34e2010-08-05 13:05:31 +10001685 debug3("%s: %d keys", __func__, nkeys);
1686 if (nkeys <= 0)
1687 fatal("cannot read public key from pkcs11");
1688 for (i = 0; i < nkeys; i++) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001689 if (sshkey_equal_public(public, keys[i])) {
Damien Miller757f34e2010-08-05 13:05:31 +10001690 private = keys[i];
1691 continue;
1692 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001693 sshkey_free(keys[i]);
Damien Miller757f34e2010-08-05 13:05:31 +10001694 }
Darren Tuckera627d422013-06-02 07:31:17 +10001695 free(keys);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001696 sshkey_free(public);
Damien Miller757f34e2010-08-05 13:05:31 +10001697 return private;
1698#else
1699 fatal("no pkcs11 support");
1700#endif /* ENABLE_PKCS11 */
1701}
1702
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001703/* Signer for sshkey_certify_custom that uses the agent */
1704static int
djm@openbsd.org4f7a56d2019-06-21 04:21:04 +00001705agent_signer(struct sshkey *key, u_char **sigp, size_t *lenp,
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001706 const u_char *data, size_t datalen,
djm@openbsd.orgb923a902019-10-31 21:28:27 +00001707 const char *alg, const char *provider, u_int compat, void *ctx)
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001708{
1709 int *agent_fdp = (int *)ctx;
1710
1711 return ssh_agent_sign(*agent_fdp, key, sigp, lenp,
1712 data, datalen, alg, compat);
1713}
1714
Damien Miller0a80ca12010-02-27 07:55:05 +11001715static void
djm@openbsd.org851f8032019-01-23 04:16:22 +00001716do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
djm@openbsd.orgbe063942019-01-23 04:51:02 +00001717 unsigned long long cert_serial, int cert_serial_autoinc,
1718 int argc, char **argv)
Damien Miller0a80ca12010-02-27 07:55:05 +11001719{
djm@openbsd.orgd25d6302020-05-02 07:19:43 +00001720 int r, i, found, agent_fd = -1;
Damien Miller0a80ca12010-02-27 07:55:05 +11001721 u_int n;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001722 struct sshkey *ca, *public;
djm@openbsd.org516605f2019-12-10 22:37:20 +00001723 char valid[64], *otmp, *tmp, *cp, *out, *comment;
1724 char *ca_fp = NULL, **plist = NULL;
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001725 struct ssh_identitylist *agent_ids;
1726 size_t j;
djm@openbsd.org516605f2019-12-10 22:37:20 +00001727 struct notifier_ctx *notifier = NULL;
Damien Miller4e270b02010-04-16 15:56:21 +10001728
Damien Miller1f0311c2014-05-15 14:24:09 +10001729#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001730 pkcs11_init(1);
Damien Miller1f0311c2014-05-15 14:24:09 +10001731#endif
Damien Miller757f34e2010-08-05 13:05:31 +10001732 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1733 if (pkcs11provider != NULL) {
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001734 /* If a PKCS#11 token was specified then try to use it */
Damien Miller757f34e2010-08-05 13:05:31 +10001735 if ((ca = load_pkcs11_key(tmp)) == NULL)
1736 fatal("No PKCS#11 key matching %s found", ca_key_path);
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001737 } else if (prefer_agent) {
1738 /*
1739 * Agent signature requested. Try to use agent after making
1740 * sure the public key specified is actually present in the
1741 * agent.
1742 */
1743 if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
1744 fatal("Cannot load CA public key %s: %s",
1745 tmp, ssh_err(r));
1746 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0)
1747 fatal("Cannot use public key for CA signature: %s",
1748 ssh_err(r));
1749 if ((r = ssh_fetch_identitylist(agent_fd, &agent_ids)) != 0)
1750 fatal("Retrieve agent key list: %s", ssh_err(r));
1751 found = 0;
1752 for (j = 0; j < agent_ids->nkeys; j++) {
1753 if (sshkey_equal(ca, agent_ids->keys[j])) {
1754 found = 1;
1755 break;
1756 }
1757 }
1758 if (!found)
1759 fatal("CA key %s not found in agent", tmp);
1760 ssh_free_identitylist(agent_ids);
1761 ca->flags |= SSHKEY_FLAG_EXT;
1762 } else {
1763 /* CA key is assumed to be a private key on the filesystem */
djm@openbsd.org07133222019-09-01 23:47:32 +00001764 ca = load_identity(tmp, NULL);
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001765 }
Darren Tuckera627d422013-06-02 07:31:17 +10001766 free(tmp);
Damien Miller757f34e2010-08-05 13:05:31 +10001767
djm@openbsd.org4a41d242020-01-24 00:27:04 +00001768 if (key_type_name != NULL) {
1769 if (sshkey_type_from_name(key_type_name) != ca->type) {
1770 fatal("CA key type %s doesn't match specified %s",
1771 sshkey_ssh_name(ca), key_type_name);
1772 }
1773 } else if (ca->type == KEY_RSA) {
1774 /* Default to a good signature algorithm */
1775 key_type_name = "rsa-sha2-512";
djm@openbsd.org57464e32016-05-02 09:36:42 +00001776 }
djm@openbsd.org516605f2019-12-10 22:37:20 +00001777 ca_fp = sshkey_fingerprint(ca, fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org57464e32016-05-02 09:36:42 +00001778
Damien Miller0a80ca12010-02-27 07:55:05 +11001779 for (i = 0; i < argc; i++) {
1780 /* Split list of principals */
1781 n = 0;
1782 if (cert_principals != NULL) {
1783 otmp = tmp = xstrdup(cert_principals);
1784 plist = NULL;
1785 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001786 plist = xreallocarray(plist, n + 1, sizeof(*plist));
Damien Miller0a80ca12010-02-27 07:55:05 +11001787 if (*(plist[n] = xstrdup(cp)) == '\0')
1788 fatal("Empty principal name");
1789 }
Darren Tuckera627d422013-06-02 07:31:17 +10001790 free(otmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001791 }
djm@openbsd.orgca613242018-02-10 05:48:46 +00001792 if (n > SSHKEY_CERT_MAX_PRINCIPALS)
1793 fatal("Too many certificate principals specified");
djm@openbsd.org16dd8b22019-07-16 13:18:39 +00001794
Damien Miller0a80ca12010-02-27 07:55:05 +11001795 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001796 if ((r = sshkey_load_public(tmp, &public, &comment)) != 0)
1797 fatal("%s: unable to open \"%s\": %s",
1798 __func__, tmp, ssh_err(r));
djm@openbsd.org85409cb2019-11-18 06:24:17 +00001799 if (sshkey_is_cert(public))
Damien Miller0a80ca12010-02-27 07:55:05 +11001800 fatal("%s: key \"%s\" type %s cannot be certified",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001801 __func__, tmp, sshkey_type(public));
Damien Miller0a80ca12010-02-27 07:55:05 +11001802
1803 /* Prepare certificate to sign */
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001804 if ((r = sshkey_to_certified(public)) != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001805 fatal("Could not upgrade key %s to certificate: %s",
1806 tmp, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001807 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001808 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001809 public->cert->key_id = xstrdup(cert_key_id);
1810 public->cert->nprincipals = n;
1811 public->cert->principals = plist;
1812 public->cert->valid_after = cert_valid_from;
1813 public->cert->valid_before = cert_valid_to;
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001814 prepare_options_buf(public->cert->critical, OPTIONS_CRITICAL);
1815 prepare_options_buf(public->cert->extensions,
1816 OPTIONS_EXTENSIONS);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001817 if ((r = sshkey_from_private(ca,
1818 &public->cert->signature_key)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001819 fatal("sshkey_from_private (ca key): %s", ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001820
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001821 if (agent_fd != -1 && (ca->flags & SSHKEY_FLAG_EXT) != 0) {
1822 if ((r = sshkey_certify_custom(public, ca,
djm@openbsd.org9a14c642019-10-31 21:23:19 +00001823 key_type_name, sk_provider, agent_signer,
1824 &agent_fd)) != 0)
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001825 fatal("Couldn't certify key %s via agent: %s",
1826 tmp, ssh_err(r));
1827 } else {
djm@openbsd.org516605f2019-12-10 22:37:20 +00001828 if (sshkey_is_sk(ca) &&
1829 (ca->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
1830 notifier = notify_start(0,
1831 "Confirm user presence for key %s %s",
1832 sshkey_type(ca), ca_fp);
1833 }
1834 r = sshkey_certify(public, ca, key_type_name,
1835 sk_provider);
1836 notify_complete(notifier);
1837 if (r != 0)
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001838 fatal("Couldn't certify key %s: %s",
1839 tmp, ssh_err(r));
1840 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001841
1842 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1843 *cp = '\0';
1844 xasprintf(&out, "%s-cert.pub", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001845 free(tmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001846
djm@openbsd.orgd25d6302020-05-02 07:19:43 +00001847 if ((r = sshkey_save_public(public, out, comment)) != 0) {
1848 fatal("Unable to save public key to %s: %s",
1849 identity_file, ssh_err(r));
1850 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001851
Damien Miller4e270b02010-04-16 15:56:21 +10001852 if (!quiet) {
djm@openbsd.org964ab3e2015-11-19 01:12:32 +00001853 sshkey_format_cert_validity(public->cert,
djm@openbsd.org499cf362015-11-19 01:08:55 +00001854 valid, sizeof(valid));
Damien Miller4e270b02010-04-16 15:56:21 +10001855 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
djm@openbsd.org964ab3e2015-11-19 01:12:32 +00001856 "valid %s", sshkey_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001857 out, public->cert->key_id,
1858 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001859 cert_principals != NULL ? " for " : "",
1860 cert_principals != NULL ? cert_principals : "",
djm@openbsd.org499cf362015-11-19 01:08:55 +00001861 valid);
Damien Miller4e270b02010-04-16 15:56:21 +10001862 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001863
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001864 sshkey_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001865 free(out);
djm@openbsd.orgbe063942019-01-23 04:51:02 +00001866 if (cert_serial_autoinc)
1867 cert_serial++;
Damien Miller0a80ca12010-02-27 07:55:05 +11001868 }
djm@openbsd.org516605f2019-12-10 22:37:20 +00001869 free(ca_fp);
Damien Miller1f0311c2014-05-15 14:24:09 +10001870#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001871 pkcs11_terminate();
Damien Miller1f0311c2014-05-15 14:24:09 +10001872#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11001873 exit(0);
1874}
1875
1876static u_int64_t
1877parse_relative_time(const char *s, time_t now)
1878{
1879 int64_t mul, secs;
1880
1881 mul = *s == '-' ? -1 : 1;
1882
1883 if ((secs = convtime(s + 1)) == -1)
1884 fatal("Invalid relative certificate time %s", s);
1885 if (mul == -1 && secs > now)
1886 fatal("Certificate time %s cannot be represented", s);
1887 return now + (u_int64_t)(secs * mul);
1888}
1889
Damien Miller0a80ca12010-02-27 07:55:05 +11001890static void
1891parse_cert_times(char *timespec)
1892{
1893 char *from, *to;
1894 time_t now = time(NULL);
1895 int64_t secs;
1896
1897 /* +timespec relative to now */
1898 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1899 if ((secs = convtime(timespec + 1)) == -1)
1900 fatal("Invalid relative certificate life %s", timespec);
1901 cert_valid_to = now + secs;
1902 /*
1903 * Backdate certificate one minute to avoid problems on hosts
1904 * with poorly-synchronised clocks.
1905 */
1906 cert_valid_from = ((now - 59)/ 60) * 60;
1907 return;
1908 }
1909
1910 /*
1911 * from:to, where
djm@openbsd.org@openbsd.orgd52131a2017-11-03 05:14:04 +00001912 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS | "always"
1913 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS | "forever"
Damien Miller0a80ca12010-02-27 07:55:05 +11001914 */
1915 from = xstrdup(timespec);
1916 to = strchr(from, ':');
1917 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001918 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001919 *to++ = '\0';
1920
1921 if (*from == '-' || *from == '+')
1922 cert_valid_from = parse_relative_time(from, now);
djm@openbsd.org@openbsd.orgd52131a2017-11-03 05:14:04 +00001923 else if (strcmp(from, "always") == 0)
1924 cert_valid_from = 0;
djm@openbsd.orgbf0fbf22018-03-12 00:52:01 +00001925 else if (parse_absolute_time(from, &cert_valid_from) != 0)
1926 fatal("Invalid from time \"%s\"", from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001927
1928 if (*to == '-' || *to == '+')
Damien Miller5b01b0d2013-10-23 16:31:31 +11001929 cert_valid_to = parse_relative_time(to, now);
djm@openbsd.org@openbsd.orgd52131a2017-11-03 05:14:04 +00001930 else if (strcmp(to, "forever") == 0)
1931 cert_valid_to = ~(u_int64_t)0;
djm@openbsd.orgbf0fbf22018-03-12 00:52:01 +00001932 else if (parse_absolute_time(to, &cert_valid_to) != 0)
1933 fatal("Invalid to time \"%s\"", to);
Damien Miller0a80ca12010-02-27 07:55:05 +11001934
1935 if (cert_valid_to <= cert_valid_from)
1936 fatal("Empty certificate validity interval");
Darren Tuckera627d422013-06-02 07:31:17 +10001937 free(from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001938}
1939
1940static void
Damien Miller4e270b02010-04-16 15:56:21 +10001941add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001942{
djm@openbsd.org249516e2017-04-29 04:12:25 +00001943 char *val, *cp;
1944 int iscrit = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001945
Damien Miller044f4a62011-05-05 14:14:08 +10001946 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001947 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001948 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001949 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001950 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001951 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001952 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001953 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001954 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001955 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001956 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001957 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001958 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001959 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001960 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001961 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001962 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001963 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001964 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001965 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001966 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001967 certflags_flags |= CERTOPT_USER_RC;
djm@openbsd.org2e712632019-11-25 00:54:23 +00001968 else if (strcasecmp(opt, "touch-required") == 0)
1969 certflags_flags &= ~CERTOPT_NO_REQUIRE_USER_PRESENCE;
1970 else if (strcasecmp(opt, "no-touch-required") == 0)
1971 certflags_flags |= CERTOPT_NO_REQUIRE_USER_PRESENCE;
Damien Miller0a80ca12010-02-27 07:55:05 +11001972 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1973 val = opt + 14;
1974 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001975 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001976 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001977 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001978 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001979 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1980 val = opt + 15;
1981 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001982 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001983 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001984 fatal("source-address already specified");
1985 if (addr_match_cidr_list(NULL, val) != 0)
1986 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001987 certflags_src_addr = xstrdup(val);
djm@openbsd.org249516e2017-04-29 04:12:25 +00001988 } else if (strncasecmp(opt, "extension:", 10) == 0 ||
1989 (iscrit = (strncasecmp(opt, "critical:", 9) == 0))) {
1990 val = xstrdup(strchr(opt, ':') + 1);
1991 if ((cp = strchr(val, '=')) != NULL)
1992 *cp++ = '\0';
1993 cert_userext = xreallocarray(cert_userext, ncert_userext + 1,
1994 sizeof(*cert_userext));
1995 cert_userext[ncert_userext].key = val;
1996 cert_userext[ncert_userext].val = cp == NULL ?
1997 NULL : xstrdup(cp);
1998 cert_userext[ncert_userext].crit = iscrit;
1999 ncert_userext++;
Damien Miller0a80ca12010-02-27 07:55:05 +11002000 } else
Damien Miller4e270b02010-04-16 15:56:21 +10002001 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11002002}
2003
Ben Lindstrombba81212001-06-25 05:01:22 +00002004static void
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002005show_options(struct sshbuf *optbuf, int in_critical)
Damien Millerd834d352010-06-26 09:48:02 +10002006{
Damien Miller633de332014-05-15 13:48:26 +10002007 char *name, *arg;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002008 struct sshbuf *options, *option = NULL;
2009 int r;
Damien Millerd834d352010-06-26 09:48:02 +10002010
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002011 if ((options = sshbuf_fromb(optbuf)) == NULL)
2012 fatal("%s: sshbuf_fromb failed", __func__);
2013 while (sshbuf_len(options) != 0) {
2014 sshbuf_free(option);
2015 option = NULL;
2016 if ((r = sshbuf_get_cstring(options, &name, NULL)) != 0 ||
2017 (r = sshbuf_froms(options, &option)) != 0)
2018 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd834d352010-06-26 09:48:02 +10002019 printf(" %s", name);
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002020 if (!in_critical &&
Damien Millerd834d352010-06-26 09:48:02 +10002021 (strcmp(name, "permit-X11-forwarding") == 0 ||
2022 strcmp(name, "permit-agent-forwarding") == 0 ||
2023 strcmp(name, "permit-port-forwarding") == 0 ||
2024 strcmp(name, "permit-pty") == 0 ||
djm@openbsd.org2e712632019-11-25 00:54:23 +00002025 strcmp(name, "permit-user-rc") == 0 ||
2026 strcmp(name, "no-touch-required") == 0)) {
Damien Millerd834d352010-06-26 09:48:02 +10002027 printf("\n");
djm@openbsd.org2e712632019-11-25 00:54:23 +00002028 } else if (in_critical &&
Damien Millerd834d352010-06-26 09:48:02 +10002029 (strcmp(name, "force-command") == 0 ||
2030 strcmp(name, "source-address") == 0)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002031 if ((r = sshbuf_get_cstring(option, &arg, NULL)) != 0)
2032 fatal("%s: buffer error: %s",
2033 __func__, ssh_err(r));
Damien Miller633de332014-05-15 13:48:26 +10002034 printf(" %s\n", arg);
2035 free(arg);
Damien Millerd834d352010-06-26 09:48:02 +10002036 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002037 printf(" UNKNOWN OPTION (len %zu)\n",
2038 sshbuf_len(option));
2039 sshbuf_reset(option);
Damien Millerd834d352010-06-26 09:48:02 +10002040 }
Darren Tuckera627d422013-06-02 07:31:17 +10002041 free(name);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002042 if (sshbuf_len(option) != 0)
Damien Millerd834d352010-06-26 09:48:02 +10002043 fatal("Option corrupt: extra data at end");
2044 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002045 sshbuf_free(option);
2046 sshbuf_free(options);
Damien Millerd834d352010-06-26 09:48:02 +10002047}
2048
2049static void
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002050print_cert(struct sshkey *key)
Damien Millerf2b70ca2010-03-05 07:39:35 +11002051{
djm@openbsd.org499cf362015-11-19 01:08:55 +00002052 char valid[64], *key_fp, *ca_fp;
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002053 u_int i;
Damien Miller4e270b02010-04-16 15:56:21 +10002054
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002055 key_fp = sshkey_fingerprint(key, fingerprint_hash, SSH_FP_DEFAULT);
2056 ca_fp = sshkey_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002057 fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00002058 if (key_fp == NULL || ca_fp == NULL)
2059 fatal("%s: sshkey_fingerprint fail", __func__);
djm@openbsd.org499cf362015-11-19 01:08:55 +00002060 sshkey_format_cert_validity(key->cert, valid, sizeof(valid));
Damien Millerf2b70ca2010-03-05 07:39:35 +11002061
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002062 printf(" Type: %s %s certificate\n", sshkey_ssh_name(key),
2063 sshkey_cert_type(key));
2064 printf(" Public key: %s %s\n", sshkey_type(key), key_fp);
djm@openbsd.orga4fc2532018-10-19 03:12:42 +00002065 printf(" Signing CA: %s %s (using %s)\n",
2066 sshkey_type(key->cert->signature_key), ca_fp,
2067 key->cert->signature_type);
Damien Miller4e270b02010-04-16 15:56:21 +10002068 printf(" Key ID: \"%s\"\n", key->cert->key_id);
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002069 printf(" Serial: %llu\n", (unsigned long long)key->cert->serial);
djm@openbsd.org499cf362015-11-19 01:08:55 +00002070 printf(" Valid: %s\n", valid);
Damien Millerf2b70ca2010-03-05 07:39:35 +11002071 printf(" Principals: ");
2072 if (key->cert->nprincipals == 0)
2073 printf("(none)\n");
2074 else {
2075 for (i = 0; i < key->cert->nprincipals; i++)
2076 printf("\n %s",
2077 key->cert->principals[i]);
2078 printf("\n");
2079 }
Damien Miller4e270b02010-04-16 15:56:21 +10002080 printf(" Critical Options: ");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002081 if (sshbuf_len(key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11002082 printf("(none)\n");
2083 else {
2084 printf("\n");
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002085 show_options(key->cert->critical, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11002086 }
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002087 printf(" Extensions: ");
2088 if (sshbuf_len(key->cert->extensions) == 0)
2089 printf("(none)\n");
2090 else {
2091 printf("\n");
2092 show_options(key->cert->extensions, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10002093 }
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002094}
2095
2096static void
2097do_show_cert(struct passwd *pw)
2098{
2099 struct sshkey *key = NULL;
2100 struct stat st;
2101 int r, is_stdin = 0, ok = 0;
2102 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00002103 char *cp, *line = NULL;
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002104 const char *path;
markus@openbsd.org7f906352018-06-06 18:29:18 +00002105 size_t linesize = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002106 u_long lnum = 0;
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002107
2108 if (!have_identity)
2109 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002110 if (strcmp(identity_file, "-") != 0 && stat(identity_file, &st) == -1)
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002111 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
2112
2113 path = identity_file;
2114 if (strcmp(path, "-") == 0) {
2115 f = stdin;
2116 path = "(stdin)";
2117 is_stdin = 1;
2118 } else if ((f = fopen(identity_file, "r")) == NULL)
2119 fatal("fopen %s: %s", identity_file, strerror(errno));
2120
markus@openbsd.org7f906352018-06-06 18:29:18 +00002121 while (getline(&line, &linesize, f) != -1) {
2122 lnum++;
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002123 sshkey_free(key);
2124 key = NULL;
2125 /* Trim leading space and comments */
2126 cp = line + strspn(line, " \t");
2127 if (*cp == '#' || *cp == '\0')
2128 continue;
2129 if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00002130 fatal("sshkey_new");
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002131 if ((r = sshkey_read(key, &cp)) != 0) {
2132 error("%s:%lu: invalid key: %s", path,
2133 lnum, ssh_err(r));
2134 continue;
2135 }
2136 if (!sshkey_is_cert(key)) {
2137 error("%s:%lu is not a certificate", path, lnum);
2138 continue;
2139 }
2140 ok = 1;
2141 if (!is_stdin && lnum == 1)
2142 printf("%s:\n", path);
2143 else
2144 printf("%s:%lu:\n", path, lnum);
2145 print_cert(key);
2146 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00002147 free(line);
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002148 sshkey_free(key);
2149 fclose(f);
2150 exit(ok ? 0 : 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11002151}
2152
2153static void
Damien Millerf3747bf2013-01-18 11:44:04 +11002154load_krl(const char *path, struct ssh_krl **krlp)
2155{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002156 struct sshbuf *krlbuf;
djm@openbsd.org99aa8032020-01-25 23:02:13 +00002157 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +11002158
djm@openbsd.org99aa8032020-01-25 23:02:13 +00002159 if ((r = sshbuf_load_file(path, &krlbuf)) != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002160 fatal("Unable to load KRL: %s", ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002161 /* XXX check sigs */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002162 if ((r = ssh_krl_from_blob(krlbuf, krlp, NULL, 0)) != 0 ||
Damien Millerf3747bf2013-01-18 11:44:04 +11002163 *krlp == NULL)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002164 fatal("Invalid KRL file: %s", ssh_err(r));
2165 sshbuf_free(krlbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11002166}
2167
2168static void
djm@openbsd.org9405c622018-09-12 01:21:34 +00002169hash_to_blob(const char *cp, u_char **blobp, size_t *lenp,
2170 const char *file, u_long lnum)
2171{
2172 char *tmp;
2173 size_t tlen;
2174 struct sshbuf *b;
2175 int r;
2176
2177 if (strncmp(cp, "SHA256:", 7) != 0)
2178 fatal("%s:%lu: unsupported hash algorithm", file, lnum);
2179 cp += 7;
2180
2181 /*
2182 * OpenSSH base64 hashes omit trailing '='
2183 * characters; put them back for decode.
2184 */
2185 tlen = strlen(cp);
2186 tmp = xmalloc(tlen + 4 + 1);
2187 strlcpy(tmp, cp, tlen + 1);
2188 while ((tlen % 4) != 0) {
2189 tmp[tlen++] = '=';
2190 tmp[tlen] = '\0';
2191 }
2192 if ((b = sshbuf_new()) == NULL)
2193 fatal("%s: sshbuf_new failed", __func__);
2194 if ((r = sshbuf_b64tod(b, tmp)) != 0)
2195 fatal("%s:%lu: decode hash failed: %s", file, lnum, ssh_err(r));
2196 free(tmp);
2197 *lenp = sshbuf_len(b);
2198 *blobp = xmalloc(*lenp);
2199 memcpy(*blobp, sshbuf_ptr(b), *lenp);
2200 sshbuf_free(b);
2201}
2202
2203static void
djm@openbsd.org669aee92015-01-30 01:10:33 +00002204update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002205 const struct sshkey *ca, struct ssh_krl *krl)
Damien Millerf3747bf2013-01-18 11:44:04 +11002206{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002207 struct sshkey *key = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11002208 u_long lnum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00002209 char *path, *cp, *ep, *line = NULL;
djm@openbsd.org9405c622018-09-12 01:21:34 +00002210 u_char *blob = NULL;
2211 size_t blen = 0, linesize = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002212 unsigned long long serial, serial2;
djm@openbsd.org9405c622018-09-12 01:21:34 +00002213 int i, was_explicit_key, was_sha1, was_sha256, was_hash, r;
Damien Millerf3747bf2013-01-18 11:44:04 +11002214 FILE *krl_spec;
2215
2216 path = tilde_expand_filename(file, pw->pw_uid);
2217 if (strcmp(path, "-") == 0) {
2218 krl_spec = stdin;
2219 free(path);
2220 path = xstrdup("(standard input)");
2221 } else if ((krl_spec = fopen(path, "r")) == NULL)
2222 fatal("fopen %s: %s", path, strerror(errno));
2223
2224 if (!quiet)
2225 printf("Revoking from %s\n", path);
markus@openbsd.org7f906352018-06-06 18:29:18 +00002226 while (getline(&line, &linesize, krl_spec) != -1) {
2227 lnum++;
djm@openbsd.org9405c622018-09-12 01:21:34 +00002228 was_explicit_key = was_sha1 = was_sha256 = was_hash = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002229 cp = line + strspn(line, " \t");
2230 /* Trim trailing space, comments and strip \n */
2231 for (i = 0, r = -1; cp[i] != '\0'; i++) {
2232 if (cp[i] == '#' || cp[i] == '\n') {
2233 cp[i] = '\0';
2234 break;
2235 }
2236 if (cp[i] == ' ' || cp[i] == '\t') {
2237 /* Remember the start of a span of whitespace */
2238 if (r == -1)
2239 r = i;
2240 } else
2241 r = -1;
2242 }
2243 if (r != -1)
2244 cp[r] = '\0';
2245 if (*cp == '\0')
2246 continue;
2247 if (strncasecmp(cp, "serial:", 7) == 0) {
djm@openbsd.org669aee92015-01-30 01:10:33 +00002248 if (ca == NULL && !wild_ca) {
Damien Millerd234afb2013-08-21 02:42:58 +10002249 fatal("revoking certificates by serial number "
Damien Millerf3747bf2013-01-18 11:44:04 +11002250 "requires specification of a CA key");
2251 }
2252 cp += 7;
2253 cp = cp + strspn(cp, " \t");
2254 errno = 0;
2255 serial = strtoull(cp, &ep, 0);
2256 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2257 fatal("%s:%lu: invalid serial \"%s\"",
2258 path, lnum, cp);
2259 if (errno == ERANGE && serial == ULLONG_MAX)
2260 fatal("%s:%lu: serial out of range",
2261 path, lnum);
2262 serial2 = serial;
2263 if (*ep == '-') {
2264 cp = ep + 1;
2265 errno = 0;
2266 serial2 = strtoull(cp, &ep, 0);
2267 if (*cp == '\0' || *ep != '\0')
2268 fatal("%s:%lu: invalid serial \"%s\"",
2269 path, lnum, cp);
2270 if (errno == ERANGE && serial2 == ULLONG_MAX)
2271 fatal("%s:%lu: serial out of range",
2272 path, lnum);
2273 if (serial2 <= serial)
2274 fatal("%s:%lu: invalid serial range "
2275 "%llu:%llu", path, lnum,
2276 (unsigned long long)serial,
2277 (unsigned long long)serial2);
2278 }
2279 if (ssh_krl_revoke_cert_by_serial_range(krl,
2280 ca, serial, serial2) != 0) {
2281 fatal("%s: revoke serial failed",
2282 __func__);
2283 }
2284 } else if (strncasecmp(cp, "id:", 3) == 0) {
djm@openbsd.org669aee92015-01-30 01:10:33 +00002285 if (ca == NULL && !wild_ca) {
Damien Millerd5d9d7b2013-08-21 02:43:27 +10002286 fatal("revoking certificates by key ID "
Damien Millerf3747bf2013-01-18 11:44:04 +11002287 "requires specification of a CA key");
2288 }
2289 cp += 3;
2290 cp = cp + strspn(cp, " \t");
2291 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2292 fatal("%s: revoke key ID failed", __func__);
djm@openbsd.org9405c622018-09-12 01:21:34 +00002293 } else if (strncasecmp(cp, "hash:", 5) == 0) {
2294 cp += 5;
2295 cp = cp + strspn(cp, " \t");
2296 hash_to_blob(cp, &blob, &blen, file, lnum);
2297 r = ssh_krl_revoke_key_sha256(krl, blob, blen);
markus@openbsd.orgff2acca2020-03-06 18:15:04 +00002298 if (r != 0)
2299 fatal("%s: revoke key failed: %s",
2300 __func__, ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002301 } else {
2302 if (strncasecmp(cp, "key:", 4) == 0) {
2303 cp += 4;
2304 cp = cp + strspn(cp, " \t");
2305 was_explicit_key = 1;
2306 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2307 cp += 5;
2308 cp = cp + strspn(cp, " \t");
2309 was_sha1 = 1;
djm@openbsd.org9405c622018-09-12 01:21:34 +00002310 } else if (strncasecmp(cp, "sha256:", 7) == 0) {
2311 cp += 7;
2312 cp = cp + strspn(cp, " \t");
2313 was_sha256 = 1;
Damien Millerf3747bf2013-01-18 11:44:04 +11002314 /*
2315 * Just try to process the line as a key.
2316 * Parsing will fail if it isn't.
2317 */
2318 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002319 if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00002320 fatal("sshkey_new");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002321 if ((r = sshkey_read(key, &cp)) != 0)
2322 fatal("%s:%lu: invalid key: %s",
2323 path, lnum, ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002324 if (was_explicit_key)
2325 r = ssh_krl_revoke_key_explicit(krl, key);
djm@openbsd.org9405c622018-09-12 01:21:34 +00002326 else if (was_sha1) {
2327 if (sshkey_fingerprint_raw(key,
2328 SSH_DIGEST_SHA1, &blob, &blen) != 0) {
2329 fatal("%s:%lu: fingerprint failed",
2330 file, lnum);
2331 }
2332 r = ssh_krl_revoke_key_sha1(krl, blob, blen);
2333 } else if (was_sha256) {
2334 if (sshkey_fingerprint_raw(key,
2335 SSH_DIGEST_SHA256, &blob, &blen) != 0) {
2336 fatal("%s:%lu: fingerprint failed",
2337 file, lnum);
2338 }
2339 r = ssh_krl_revoke_key_sha256(krl, blob, blen);
2340 } else
Damien Millerf3747bf2013-01-18 11:44:04 +11002341 r = ssh_krl_revoke_key(krl, key);
2342 if (r != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002343 fatal("%s: revoke key failed: %s",
2344 __func__, ssh_err(r));
djm@openbsd.org9405c622018-09-12 01:21:34 +00002345 freezero(blob, blen);
2346 blob = NULL;
2347 blen = 0;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002348 sshkey_free(key);
Damien Millerf3747bf2013-01-18 11:44:04 +11002349 }
2350 }
2351 if (strcmp(path, "-") != 0)
2352 fclose(krl_spec);
markus@openbsd.org7f906352018-06-06 18:29:18 +00002353 free(line);
Damien Miller0d6771b2013-04-23 15:23:24 +10002354 free(path);
Damien Millerf3747bf2013-01-18 11:44:04 +11002355}
2356
2357static void
djm@openbsd.org851f8032019-01-23 04:16:22 +00002358do_gen_krl(struct passwd *pw, int updating, const char *ca_key_path,
2359 unsigned long long krl_version, const char *krl_comment,
2360 int argc, char **argv)
Damien Millerf3747bf2013-01-18 11:44:04 +11002361{
2362 struct ssh_krl *krl;
2363 struct stat sb;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002364 struct sshkey *ca = NULL;
djm@openbsd.org99aa8032020-01-25 23:02:13 +00002365 int i, r, wild_ca = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002366 char *tmp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002367 struct sshbuf *kbuf;
Damien Millerf3747bf2013-01-18 11:44:04 +11002368
2369 if (*identity_file == '\0')
2370 fatal("KRL generation requires an output file");
2371 if (stat(identity_file, &sb) == -1) {
2372 if (errno != ENOENT)
2373 fatal("Cannot access KRL \"%s\": %s",
2374 identity_file, strerror(errno));
2375 if (updating)
2376 fatal("KRL \"%s\" does not exist", identity_file);
2377 }
2378 if (ca_key_path != NULL) {
djm@openbsd.org669aee92015-01-30 01:10:33 +00002379 if (strcasecmp(ca_key_path, "none") == 0)
2380 wild_ca = 1;
2381 else {
2382 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2383 if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
2384 fatal("Cannot load CA public key %s: %s",
2385 tmp, ssh_err(r));
2386 free(tmp);
2387 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002388 }
2389
2390 if (updating)
2391 load_krl(identity_file, &krl);
2392 else if ((krl = ssh_krl_init()) == NULL)
2393 fatal("couldn't create KRL");
2394
djm@openbsd.org851f8032019-01-23 04:16:22 +00002395 if (krl_version != 0)
2396 ssh_krl_set_version(krl, krl_version);
2397 if (krl_comment != NULL)
2398 ssh_krl_set_comment(krl, krl_comment);
Damien Millerf3747bf2013-01-18 11:44:04 +11002399
2400 for (i = 0; i < argc; i++)
djm@openbsd.org669aee92015-01-30 01:10:33 +00002401 update_krl_from_file(pw, argv[i], wild_ca, ca, krl);
Damien Millerf3747bf2013-01-18 11:44:04 +11002402
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002403 if ((kbuf = sshbuf_new()) == NULL)
2404 fatal("sshbuf_new failed");
2405 if (ssh_krl_to_blob(krl, kbuf, NULL, 0) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11002406 fatal("Couldn't generate KRL");
djm@openbsd.org99aa8032020-01-25 23:02:13 +00002407 if ((r = sshbuf_write_file(identity_file, kbuf)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11002408 fatal("write %s: %s", identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002409 sshbuf_free(kbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11002410 ssh_krl_free(krl);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00002411 sshkey_free(ca);
Damien Millerf3747bf2013-01-18 11:44:04 +11002412}
2413
2414static void
djm@openbsd.org6ec74572020-04-03 02:26:56 +00002415do_check_krl(struct passwd *pw, int print_krl, int argc, char **argv)
Damien Millerf3747bf2013-01-18 11:44:04 +11002416{
2417 int i, r, ret = 0;
2418 char *comment;
2419 struct ssh_krl *krl;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002420 struct sshkey *k;
Damien Millerf3747bf2013-01-18 11:44:04 +11002421
2422 if (*identity_file == '\0')
2423 fatal("KRL checking requires an input file");
2424 load_krl(identity_file, &krl);
djm@openbsd.org6ec74572020-04-03 02:26:56 +00002425 if (print_krl)
2426 krl_dump(krl, stdout);
Damien Millerf3747bf2013-01-18 11:44:04 +11002427 for (i = 0; i < argc; i++) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002428 if ((r = sshkey_load_public(argv[i], &k, &comment)) != 0)
2429 fatal("Cannot load public key %s: %s",
2430 argv[i], ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002431 r = ssh_krl_check_key(krl, k);
2432 printf("%s%s%s%s: %s\n", argv[i],
2433 *comment ? " (" : "", comment, *comment ? ")" : "",
2434 r == 0 ? "ok" : "REVOKED");
2435 if (r != 0)
2436 ret = 1;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002437 sshkey_free(k);
Damien Millerf3747bf2013-01-18 11:44:04 +11002438 free(comment);
2439 }
2440 ssh_krl_free(krl);
2441 exit(ret);
2442}
2443
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002444static struct sshkey *
2445load_sign_key(const char *keypath, const struct sshkey *pubkey)
2446{
2447 size_t i, slen, plen = strlen(keypath);
2448 char *privpath = xstrdup(keypath);
2449 const char *suffixes[] = { "-cert.pub", ".pub", NULL };
2450 struct sshkey *ret = NULL, *privkey = NULL;
2451 int r;
2452
2453 /*
djm@openbsd.orgd081f012020-03-13 03:17:07 +00002454 * If passed a public key filename, then try to locate the corresponding
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002455 * private key. This lets us specify certificates on the command-line
2456 * and have ssh-keygen find the appropriate private key.
2457 */
2458 for (i = 0; suffixes[i]; i++) {
2459 slen = strlen(suffixes[i]);
2460 if (plen <= slen ||
2461 strcmp(privpath + plen - slen, suffixes[i]) != 0)
2462 continue;
2463 privpath[plen - slen] = '\0';
2464 debug("%s: %s looks like a public key, using private key "
2465 "path %s instead", __func__, keypath, privpath);
2466 }
2467 if ((privkey = load_identity(privpath, NULL)) == NULL) {
2468 error("Couldn't load identity %s", keypath);
2469 goto done;
2470 }
2471 if (!sshkey_equal_public(pubkey, privkey)) {
2472 error("Public key %s doesn't match private %s",
2473 keypath, privpath);
2474 goto done;
2475 }
2476 if (sshkey_is_cert(pubkey) && !sshkey_is_cert(privkey)) {
2477 /*
2478 * Graft the certificate onto the private key to make
2479 * it capable of signing.
2480 */
2481 if ((r = sshkey_to_certified(privkey)) != 0) {
2482 error("%s: sshkey_to_certified: %s", __func__,
2483 ssh_err(r));
2484 goto done;
2485 }
2486 if ((r = sshkey_cert_copy(pubkey, privkey)) != 0) {
2487 error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r));
2488 goto done;
2489 }
2490 }
2491 /* success */
2492 ret = privkey;
2493 privkey = NULL;
2494 done:
2495 sshkey_free(privkey);
2496 free(privpath);
2497 return ret;
2498}
2499
2500static int
2501sign_one(struct sshkey *signkey, const char *filename, int fd,
2502 const char *sig_namespace, sshsig_signer *signer, void *signer_ctx)
2503{
2504 struct sshbuf *sigbuf = NULL, *abuf = NULL;
2505 int r = SSH_ERR_INTERNAL_ERROR, wfd = -1, oerrno;
djm@openbsd.orge44bb612019-11-12 22:36:44 +00002506 char *wfile = NULL, *asig = NULL, *fp = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002507
2508 if (!quiet) {
2509 if (fd == STDIN_FILENO)
2510 fprintf(stderr, "Signing data on standard input\n");
2511 else
2512 fprintf(stderr, "Signing file %s\n", filename);
2513 }
djm@openbsd.orge44bb612019-11-12 22:36:44 +00002514 if (signer == NULL && sshkey_is_sk(signkey) &&
2515 (signkey->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
2516 if ((fp = sshkey_fingerprint(signkey, fingerprint_hash,
2517 SSH_FP_DEFAULT)) == NULL)
2518 fatal("%s: sshkey_fingerprint failed", __func__);
2519 fprintf(stderr, "Confirm user presence for key %s %s\n",
2520 sshkey_type(signkey), fp);
2521 free(fp);
2522 }
djm@openbsd.org9a14c642019-10-31 21:23:19 +00002523 if ((r = sshsig_sign_fd(signkey, NULL, sk_provider, fd, sig_namespace,
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002524 &sigbuf, signer, signer_ctx)) != 0) {
2525 error("Signing %s failed: %s", filename, ssh_err(r));
2526 goto out;
2527 }
2528 if ((r = sshsig_armor(sigbuf, &abuf)) != 0) {
2529 error("%s: sshsig_armor: %s", __func__, ssh_err(r));
2530 goto out;
2531 }
2532 if ((asig = sshbuf_dup_string(abuf)) == NULL) {
2533 error("%s: buffer error", __func__);
2534 r = SSH_ERR_ALLOC_FAIL;
2535 goto out;
2536 }
2537
2538 if (fd == STDIN_FILENO) {
2539 fputs(asig, stdout);
2540 fflush(stdout);
2541 } else {
2542 xasprintf(&wfile, "%s.sig", filename);
2543 if (confirm_overwrite(wfile)) {
2544 if ((wfd = open(wfile, O_WRONLY|O_CREAT|O_TRUNC,
2545 0666)) == -1) {
2546 oerrno = errno;
2547 error("Cannot open %s: %s",
2548 wfile, strerror(errno));
2549 errno = oerrno;
2550 r = SSH_ERR_SYSTEM_ERROR;
2551 goto out;
2552 }
2553 if (atomicio(vwrite, wfd, asig,
2554 strlen(asig)) != strlen(asig)) {
2555 oerrno = errno;
2556 error("Cannot write to %s: %s",
2557 wfile, strerror(errno));
2558 errno = oerrno;
2559 r = SSH_ERR_SYSTEM_ERROR;
2560 goto out;
2561 }
2562 if (!quiet) {
2563 fprintf(stderr, "Write signature to %s\n",
2564 wfile);
2565 }
2566 }
2567 }
2568 /* success */
2569 r = 0;
2570 out:
2571 free(wfile);
2572 free(asig);
2573 sshbuf_free(abuf);
2574 sshbuf_free(sigbuf);
2575 if (wfd != -1)
2576 close(wfd);
2577 return r;
2578}
2579
2580static int
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002581sig_sign(const char *keypath, const char *sig_namespace, int argc, char **argv)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002582{
2583 int i, fd = -1, r, ret = -1;
2584 int agent_fd = -1;
2585 struct sshkey *pubkey = NULL, *privkey = NULL, *signkey = NULL;
2586 sshsig_signer *signer = NULL;
2587
2588 /* Check file arguments. */
2589 for (i = 0; i < argc; i++) {
2590 if (strcmp(argv[i], "-") != 0)
2591 continue;
2592 if (i > 0 || argc > 1)
2593 fatal("Cannot sign mix of paths and standard input");
2594 }
2595
2596 if ((r = sshkey_load_public(keypath, &pubkey, NULL)) != 0) {
2597 error("Couldn't load public key %s: %s", keypath, ssh_err(r));
2598 goto done;
2599 }
2600
2601 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0)
2602 debug("Couldn't get agent socket: %s", ssh_err(r));
2603 else {
2604 if ((r = ssh_agent_has_key(agent_fd, pubkey)) == 0)
2605 signer = agent_signer;
2606 else
2607 debug("Couldn't find key in agent: %s", ssh_err(r));
2608 }
2609
2610 if (signer == NULL) {
2611 /* Not using agent - try to load private key */
2612 if ((privkey = load_sign_key(keypath, pubkey)) == NULL)
2613 goto done;
2614 signkey = privkey;
2615 } else {
2616 /* Will use key in agent */
2617 signkey = pubkey;
2618 }
2619
2620 if (argc == 0) {
2621 if ((r = sign_one(signkey, "(stdin)", STDIN_FILENO,
2622 sig_namespace, signer, &agent_fd)) != 0)
2623 goto done;
2624 } else {
2625 for (i = 0; i < argc; i++) {
2626 if (strcmp(argv[i], "-") == 0)
2627 fd = STDIN_FILENO;
2628 else if ((fd = open(argv[i], O_RDONLY)) == -1) {
2629 error("Cannot open %s for signing: %s",
2630 argv[i], strerror(errno));
2631 goto done;
2632 }
2633 if ((r = sign_one(signkey, argv[i], fd, sig_namespace,
2634 signer, &agent_fd)) != 0)
2635 goto done;
2636 if (fd != STDIN_FILENO)
2637 close(fd);
2638 fd = -1;
2639 }
2640 }
2641
2642 ret = 0;
2643done:
2644 if (fd != -1 && fd != STDIN_FILENO)
2645 close(fd);
2646 sshkey_free(pubkey);
2647 sshkey_free(privkey);
2648 return ret;
2649}
2650
2651static int
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002652sig_verify(const char *signature, const char *sig_namespace,
2653 const char *principal, const char *allowed_keys, const char *revoked_keys)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002654{
djm@openbsd.org99aa8032020-01-25 23:02:13 +00002655 int r, ret = -1;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002656 struct sshbuf *sigbuf = NULL, *abuf = NULL;
2657 struct sshkey *sign_key = NULL;
2658 char *fp = NULL;
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002659 struct sshkey_sig_details *sig_details = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002660
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002661 memset(&sig_details, 0, sizeof(sig_details));
djm@openbsd.org99aa8032020-01-25 23:02:13 +00002662 if ((r = sshbuf_load_file(signature, &abuf)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002663 error("Couldn't read signature file: %s", ssh_err(r));
2664 goto done;
2665 }
djm@openbsd.org99aa8032020-01-25 23:02:13 +00002666
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002667 if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) {
2668 error("%s: sshsig_armor: %s", __func__, ssh_err(r));
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002669 goto done;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002670 }
2671 if ((r = sshsig_verify_fd(sigbuf, STDIN_FILENO, sig_namespace,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002672 &sign_key, &sig_details)) != 0)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002673 goto done; /* sshsig_verify() prints error */
2674
2675 if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash,
2676 SSH_FP_DEFAULT)) == NULL)
2677 fatal("%s: sshkey_fingerprint failed", __func__);
2678 debug("Valid (unverified) signature from key %s", fp);
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002679 if (sig_details != NULL) {
2680 debug2("%s: signature details: counter = %u, flags = 0x%02x",
2681 __func__, sig_details->sk_counter, sig_details->sk_flags);
2682 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002683 free(fp);
2684 fp = NULL;
2685
2686 if (revoked_keys != NULL) {
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +00002687 if ((r = sshkey_check_revoked(sign_key, revoked_keys)) != 0) {
2688 debug3("sshkey_check_revoked failed: %s", ssh_err(r));
2689 goto done;
2690 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002691 }
2692
djm@openbsd.org8aa2aa32019-09-16 03:23:02 +00002693 if (allowed_keys != NULL &&
2694 (r = sshsig_check_allowed_keys(allowed_keys, sign_key,
2695 principal, sig_namespace)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002696 debug3("sshsig_check_allowed_keys failed: %s", ssh_err(r));
2697 goto done;
2698 }
2699 /* success */
2700 ret = 0;
2701done:
2702 if (!quiet) {
2703 if (ret == 0) {
2704 if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash,
2705 SSH_FP_DEFAULT)) == NULL) {
2706 fatal("%s: sshkey_fingerprint failed",
2707 __func__);
2708 }
djm@openbsd.org8aa2aa32019-09-16 03:23:02 +00002709 if (principal == NULL) {
2710 printf("Good \"%s\" signature with %s key %s\n",
2711 sig_namespace, sshkey_type(sign_key), fp);
2712
2713 } else {
2714 printf("Good \"%s\" signature for %s with %s key %s\n",
2715 sig_namespace, principal,
2716 sshkey_type(sign_key), fp);
2717 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002718 } else {
2719 printf("Could not verify signature.\n");
2720 }
2721 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002722 sshbuf_free(sigbuf);
2723 sshbuf_free(abuf);
2724 sshkey_free(sign_key);
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002725 sshkey_sig_details_free(sig_details);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002726 free(fp);
2727 return ret;
2728}
2729
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002730static int
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00002731sig_find_principals(const char *signature, const char *allowed_keys) {
djm@openbsd.org99aa8032020-01-25 23:02:13 +00002732 int r, ret = -1;
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002733 struct sshbuf *sigbuf = NULL, *abuf = NULL;
2734 struct sshkey *sign_key = NULL;
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00002735 char *principals = NULL, *cp, *tmp;
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002736
djm@openbsd.org99aa8032020-01-25 23:02:13 +00002737 if ((r = sshbuf_load_file(signature, &abuf)) != 0) {
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002738 error("Couldn't read signature file: %s", ssh_err(r));
2739 goto done;
2740 }
2741 if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) {
2742 error("%s: sshsig_armor: %s", __func__, ssh_err(r));
2743 goto done;
2744 }
2745 if ((r = sshsig_get_pubkey(sigbuf, &sign_key)) != 0) {
2746 error("%s: sshsig_get_pubkey: %s",
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00002747 __func__, ssh_err(r));
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002748 goto done;
2749 }
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00002750 if ((r = sshsig_find_principals(allowed_keys, sign_key,
2751 &principals)) != 0) {
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002752 error("%s: sshsig_get_principal: %s",
2753 __func__, ssh_err(r));
2754 goto done;
2755 }
2756 ret = 0;
2757done:
2758 if (ret == 0 ) {
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00002759 /* Emit matching principals one per line */
2760 tmp = principals;
2761 while ((cp = strsep(&tmp, ",")) != NULL && *cp != '\0')
2762 puts(cp);
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002763 } else {
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00002764 fprintf(stderr, "No principal matched.\n");
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002765 }
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002766 sshbuf_free(sigbuf);
2767 sshbuf_free(abuf);
2768 sshkey_free(sign_key);
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00002769 free(principals);
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002770 return ret;
2771}
2772
Damien Millerf3747bf2013-01-18 11:44:04 +11002773static void
djm@openbsd.org3e60d182019-12-30 03:30:09 +00002774do_moduli_gen(const char *out_file, char **opts, size_t nopts)
2775{
2776#ifdef WITH_OPENSSL
2777 /* Moduli generation/screening */
2778 u_int32_t memory = 0;
2779 BIGNUM *start = NULL;
2780 int moduli_bits = 0;
2781 FILE *out;
2782 size_t i;
2783 const char *errstr;
2784
2785 /* Parse options */
2786 for (i = 0; i < nopts; i++) {
2787 if (strncmp(opts[i], "memory=", 7) == 0) {
2788 memory = (u_int32_t)strtonum(opts[i]+7, 1,
2789 UINT_MAX, &errstr);
2790 if (errstr) {
2791 fatal("Memory limit is %s: %s",
2792 errstr, opts[i]+7);
2793 }
2794 } else if (strncmp(opts[i], "start=", 6) == 0) {
2795 /* XXX - also compare length against bits */
2796 if (BN_hex2bn(&start, opts[i]+6) == 0)
2797 fatal("Invalid start point.");
2798 } else if (strncmp(opts[i], "bits=", 5) == 0) {
2799 moduli_bits = (int)strtonum(opts[i]+5, 1,
2800 INT_MAX, &errstr);
2801 if (errstr) {
2802 fatal("Invalid number: %s (%s)",
2803 opts[i]+12, errstr);
2804 }
2805 } else {
2806 fatal("Option \"%s\" is unsupported for moduli "
2807 "generation", opts[i]);
2808 }
2809 }
2810
2811 if ((out = fopen(out_file, "w")) == NULL) {
2812 fatal("Couldn't open modulus candidate file \"%s\": %s",
2813 out_file, strerror(errno));
2814 }
2815 setvbuf(out, NULL, _IOLBF, 0);
2816
2817 if (moduli_bits == 0)
2818 moduli_bits = DEFAULT_BITS;
2819 if (gen_candidates(out, memory, moduli_bits, start) != 0)
2820 fatal("modulus candidate generation failed");
2821#else /* WITH_OPENSSL */
2822 fatal("Moduli generation is not supported");
2823#endif /* WITH_OPENSSL */
2824}
2825
2826static void
2827do_moduli_screen(const char *out_file, char **opts, size_t nopts)
2828{
2829#ifdef WITH_OPENSSL
2830 /* Moduli generation/screening */
2831 char *checkpoint = NULL;
2832 u_int32_t generator_wanted = 0;
2833 unsigned long start_lineno = 0, lines_to_process = 0;
2834 int prime_tests = 0;
2835 FILE *out, *in = stdin;
2836 size_t i;
2837 const char *errstr;
2838
2839 /* Parse options */
2840 for (i = 0; i < nopts; i++) {
2841 if (strncmp(opts[i], "lines=", 6) == 0) {
2842 lines_to_process = strtoul(opts[i]+6, NULL, 10);
2843 } else if (strncmp(opts[i], "start-line=", 11) == 0) {
2844 start_lineno = strtoul(opts[i]+11, NULL, 10);
2845 } else if (strncmp(opts[i], "checkpoint=", 11) == 0) {
2846 checkpoint = xstrdup(opts[i]+11);
2847 } else if (strncmp(opts[i], "generator=", 10) == 0) {
2848 generator_wanted = (u_int32_t)strtonum(
2849 opts[i]+10, 1, UINT_MAX, &errstr);
2850 if (errstr != NULL) {
2851 fatal("Generator invalid: %s (%s)",
2852 opts[i]+10, errstr);
2853 }
2854 } else if (strncmp(opts[i], "prime-tests=", 12) == 0) {
2855 prime_tests = (int)strtonum(opts[i]+12, 1,
2856 INT_MAX, &errstr);
2857 if (errstr) {
2858 fatal("Invalid number: %s (%s)",
2859 opts[i]+12, errstr);
2860 }
2861 } else {
2862 fatal("Option \"%s\" is unsupported for moduli "
2863 "screening", opts[i]);
2864 }
2865 }
2866
2867 if (have_identity && strcmp(identity_file, "-") != 0) {
2868 if ((in = fopen(identity_file, "r")) == NULL) {
2869 fatal("Couldn't open modulus candidate "
2870 "file \"%s\": %s", identity_file,
2871 strerror(errno));
2872 }
2873 }
2874
2875 if ((out = fopen(out_file, "a")) == NULL) {
2876 fatal("Couldn't open moduli file \"%s\": %s",
2877 out_file, strerror(errno));
2878 }
2879 setvbuf(out, NULL, _IOLBF, 0);
2880 if (prime_test(in, out, prime_tests == 0 ? 100 : prime_tests,
2881 generator_wanted, checkpoint,
2882 start_lineno, lines_to_process) != 0)
2883 fatal("modulus screening failed");
2884#else /* WITH_OPENSSL */
2885 fatal("Moduli screening is not supported");
2886#endif /* WITH_OPENSSL */
2887}
2888
djm@openbsd.org90399712020-01-02 22:40:09 +00002889static char *
2890private_key_passphrase(void)
2891{
2892 char *passphrase1, *passphrase2;
2893
2894 /* Ask for a passphrase (twice). */
2895 if (identity_passphrase)
2896 passphrase1 = xstrdup(identity_passphrase);
2897 else if (identity_new_passphrase)
2898 passphrase1 = xstrdup(identity_new_passphrase);
2899 else {
2900passphrase_again:
2901 passphrase1 =
2902 read_passphrase("Enter passphrase (empty for no "
2903 "passphrase): ", RP_ALLOW_STDIN);
2904 passphrase2 = read_passphrase("Enter same passphrase again: ",
2905 RP_ALLOW_STDIN);
2906 if (strcmp(passphrase1, passphrase2) != 0) {
2907 /*
2908 * The passphrases do not match. Clear them and
2909 * retry.
2910 */
2911 freezero(passphrase1, strlen(passphrase1));
2912 freezero(passphrase2, strlen(passphrase2));
2913 printf("Passphrases do not match. Try again.\n");
2914 goto passphrase_again;
2915 }
2916 /* Clear the other copy of the passphrase. */
2917 freezero(passphrase2, strlen(passphrase2));
2918 }
2919 return passphrase1;
2920}
2921
2922static const char *
2923skip_ssh_url_preamble(const char *s)
2924{
2925 if (strncmp(s, "ssh://", 6) == 0)
2926 return s + 6;
2927 else if (strncmp(s, "ssh:", 4) == 0)
2928 return s + 4;
2929 return s;
2930}
2931
2932static int
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00002933do_download_sk(const char *skprovider, const char *device)
djm@openbsd.org90399712020-01-02 22:40:09 +00002934{
2935 struct sshkey **keys;
2936 size_t nkeys, i;
2937 int r, ok = -1;
djm@openbsd.orgc084a2d2020-03-13 03:12:17 +00002938 char *fp, *pin = NULL, *pass = NULL, *path, *pubpath;
djm@openbsd.org90399712020-01-02 22:40:09 +00002939 const char *ext;
2940
2941 if (skprovider == NULL)
2942 fatal("Cannot download keys without provider");
2943
djm@openbsd.orgc084a2d2020-03-13 03:12:17 +00002944 for (i = 0; i < 2; i++) {
2945 if (i == 1) {
2946 pin = read_passphrase("Enter PIN for authenticator: ",
2947 RP_ALLOW_STDIN);
2948 }
2949 if ((r = sshsk_load_resident(skprovider, device, pin,
2950 &keys, &nkeys)) != 0) {
2951 if (i == 0 && r == SSH_ERR_KEY_WRONG_PASSPHRASE)
2952 continue;
djm@openbsd.org20ebb462020-05-18 04:29:35 +00002953 if (pin != NULL)
2954 freezero(pin, strlen(pin));
djm@openbsd.orgc084a2d2020-03-13 03:12:17 +00002955 error("Unable to load resident keys: %s", ssh_err(r));
2956 return -1;
2957 }
djm@openbsd.org90399712020-01-02 22:40:09 +00002958 }
2959 if (nkeys == 0)
2960 logit("No keys to download");
djm@openbsd.org20ebb462020-05-18 04:29:35 +00002961 if (pin != NULL)
2962 freezero(pin, strlen(pin));
djm@openbsd.org90399712020-01-02 22:40:09 +00002963
2964 for (i = 0; i < nkeys; i++) {
2965 if (keys[i]->type != KEY_ECDSA_SK &&
2966 keys[i]->type != KEY_ED25519_SK) {
2967 error("Unsupported key type %s (%d)",
2968 sshkey_type(keys[i]), keys[i]->type);
2969 continue;
2970 }
2971 if ((fp = sshkey_fingerprint(keys[i],
2972 fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2973 fatal("%s: sshkey_fingerprint failed", __func__);
2974 debug("%s: key %zu: %s %s %s (flags 0x%02x)", __func__, i,
2975 sshkey_type(keys[i]), fp, keys[i]->sk_application,
2976 keys[i]->sk_flags);
2977 ext = skip_ssh_url_preamble(keys[i]->sk_application);
2978 xasprintf(&path, "id_%s_rk%s%s",
2979 keys[i]->type == KEY_ECDSA_SK ? "ecdsa_sk" : "ed25519_sk",
2980 *ext == '\0' ? "" : "_", ext);
2981
2982 /* If the file already exists, ask the user to confirm. */
2983 if (!confirm_overwrite(path)) {
2984 free(path);
2985 break;
2986 }
2987
2988 /* Save the key with the application string as the comment */
2989 if (pass == NULL)
2990 pass = private_key_passphrase();
2991 if ((r = sshkey_save_private(keys[i], path, pass,
2992 keys[i]->sk_application, private_key_format,
2993 openssh_format_cipher, rounds)) != 0) {
2994 error("Saving key \"%s\" failed: %s",
2995 path, ssh_err(r));
2996 free(path);
2997 break;
2998 }
2999 if (!quiet) {
3000 printf("Saved %s key%s%s to %s\n",
3001 sshkey_type(keys[i]),
3002 *ext != '\0' ? " " : "",
3003 *ext != '\0' ? keys[i]->sk_application : "",
3004 path);
3005 }
3006
3007 /* Save public key too */
3008 xasprintf(&pubpath, "%s.pub", path);
3009 free(path);
3010 if ((r = sshkey_save_public(keys[i], pubpath,
3011 keys[i]->sk_application)) != 0) {
djm@openbsd.org90399712020-01-02 22:40:09 +00003012 error("Saving public key \"%s\" failed: %s",
3013 pubpath, ssh_err(r));
markus@openbsd.orge32ef972020-03-06 18:29:14 +00003014 free(pubpath);
djm@openbsd.org90399712020-01-02 22:40:09 +00003015 break;
3016 }
3017 free(pubpath);
3018 }
3019
3020 if (i >= nkeys)
3021 ok = 0; /* success */
3022 if (pass != NULL)
3023 freezero(pass, strlen(pass));
3024 for (i = 0; i < nkeys; i++)
3025 sshkey_free(keys[i]);
3026 free(keys);
3027 return ok ? 0 : -1;
3028}
3029
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003030static void
Damien Miller431f66b1999-11-21 18:31:57 +11003031usage(void)
3032{
Damien Millerf0858de2014-04-20 13:01:30 +10003033 fprintf(stderr,
jmc@openbsd.org69189742019-10-03 17:07:50 +00003034 "usage: ssh-keygen [-q] [-b bits] [-C comment] [-f output_keyfile] [-m format]\n"
naddy@openbsd.orgf0edda82019-11-18 23:16:49 +00003035 " [-t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa]\n"
naddy@openbsd.org0d005d62020-01-14 15:07:30 +00003036 " [-N new_passphrase] [-O option] [-w provider]\n"
jmc@openbsd.org69189742019-10-03 17:07:50 +00003037 " ssh-keygen -p [-f keyfile] [-m format] [-N new_passphrase]\n"
3038 " [-P old_passphrase]\n"
3039 " ssh-keygen -i [-f input_keyfile] [-m key_format]\n"
3040 " ssh-keygen -e [-f input_keyfile] [-m key_format]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003041 " ssh-keygen -y [-f input_keyfile]\n"
jmc@openbsd.org69189742019-10-03 17:07:50 +00003042 " ssh-keygen -c [-C comment] [-f keyfile] [-P passphrase]\n"
naddy@openbsd.org6288e3a2015-02-24 15:24:05 +00003043 " ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]\n"
djm@openbsd.org873d3e72017-04-30 23:18:44 +00003044 " ssh-keygen -B [-f input_keyfile]\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11003045#ifdef ENABLE_PKCS11
Damien Millerf0858de2014-04-20 13:01:30 +10003046 fprintf(stderr,
3047 " ssh-keygen -D pkcs11\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11003048#endif
Damien Millerf0858de2014-04-20 13:01:30 +10003049 fprintf(stderr,
jmc@openbsd.org6c91d422019-09-29 16:31:57 +00003050 " ssh-keygen -F hostname [-lv] [-f known_hosts_file]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003051 " ssh-keygen -H [-f known_hosts_file]\n"
naddy@openbsd.org0d005d62020-01-14 15:07:30 +00003052 " ssh-keygen -K [-w provider]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003053 " ssh-keygen -R hostname [-f known_hosts_file]\n"
jmc@openbsd.org6c91d422019-09-29 16:31:57 +00003054 " ssh-keygen -r hostname [-g] [-f input_keyfile]\n"
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003055#ifdef WITH_OPENSSL
naddy@openbsd.org0d005d62020-01-14 15:07:30 +00003056 " ssh-keygen -M generate [-O option] output_file\n"
3057 " ssh-keygen -M screen [-f input_file] [-O option] output_file\n"
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003058#endif
jmc@openbsd.org69189742019-10-03 17:07:50 +00003059 " ssh-keygen -I certificate_identity -s ca_key [-hU] [-D pkcs11_provider]\n"
3060 " [-n principals] [-O option] [-V validity_interval]\n"
3061 " [-z serial_number] file ...\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003062 " ssh-keygen -L [-f input_keyfile]\n"
jmc@openbsd.org69189742019-10-03 17:07:50 +00003063 " ssh-keygen -A [-f prefix_path]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003064 " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
3065 " file ...\n"
djm@openbsd.org6ec74572020-04-03 02:26:56 +00003066 " ssh-keygen -Q [-l] -f krl_file [file ...]\n"
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003067 " ssh-keygen -Y find-principals -s signature_file -f allowed_signers_file\n"
jmc@openbsd.org69189742019-10-03 17:07:50 +00003068 " ssh-keygen -Y check-novalidate -n namespace -s signature_file\n"
3069 " ssh-keygen -Y sign -f key_file -n namespace file ...\n"
3070 " ssh-keygen -Y verify -f allowed_signers_file -I signer_identity\n"
3071 " -n namespace -s signature_file [-r revocation_file]\n");
Damien Miller95def091999-11-25 00:26:21 +11003072 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11003073}
3074
Damien Miller95def091999-11-25 00:26:21 +11003075/*
3076 * Main program for key management.
3077 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003078int
Damien Millerdf8b7db2007-01-05 16:22:57 +11003079main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003080{
djm@openbsd.org90399712020-01-02 22:40:09 +00003081 char dotsshdir[PATH_MAX], comment[1024], *passphrase;
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003082 char *rr_hostname = NULL, *ep, *fp, *ra;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003083 struct sshkey *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11003084 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11003085 struct stat st;
djm@openbsd.org90399712020-01-02 22:40:09 +00003086 int r, opt, type;
djm@openbsd.org851f8032019-01-23 04:16:22 +00003087 int change_passphrase = 0, change_comment = 0, show_cert = 0;
3088 int find_host = 0, delete_host = 0, hash_hosts = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11003089 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
djm@openbsd.org851f8032019-01-23 04:16:22 +00003090 int prefer_agent = 0, convert_to = 0, convert_from = 0;
djm@openbsd.orgbe063942019-01-23 04:51:02 +00003091 int print_public = 0, print_generic = 0, cert_serial_autoinc = 0;
djm@openbsd.org90399712020-01-02 22:40:09 +00003092 int do_gen_candidates = 0, do_screen_candidates = 0, download_sk = 0;
djm@openbsd.org3093d122019-12-30 09:49:52 +00003093 unsigned long long cert_serial = 0;
djm@openbsd.org1e645fe2019-12-30 03:28:41 +00003094 char *identity_comment = NULL, *ca_key_path = NULL, **opts = NULL;
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003095 char *sk_application = NULL, *sk_device = NULL, *sk_user = NULL;
djm@openbsd.org24c0f752020-01-28 08:01:34 +00003096 char *sk_attestaion_path = NULL;
3097 struct sshbuf *challenge = NULL, *attest = NULL;
djm@openbsd.org1e645fe2019-12-30 03:28:41 +00003098 size_t i, nopts = 0;
dtucker@openbsd.org7afc45c2019-08-08 08:02:57 +00003099 u_int32_t bits = 0;
djm@openbsd.orgdaeaf412019-11-25 00:55:58 +00003100 uint8_t sk_flags = SSH_SK_USER_PRESENCE_REQD;
Damien Miller02e754f2005-05-26 12:19:39 +10003101 const char *errstr;
djm@openbsd.org851f8032019-01-23 04:16:22 +00003102 int log_level = SYSLOG_LEVEL_INFO;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003103 char *sign_op = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003104
Damien Miller95def091999-11-25 00:26:21 +11003105 extern int optind;
3106 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003107
Darren Tuckerce321d82005-10-03 18:11:24 +10003108 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
3109 sanitise_stdfd();
3110
Darren Tucker9ac56e92007-01-14 10:19:59 +11003111 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10003112
Kevin Steves3a881912002-07-20 19:05:40 +00003113 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10003114
Damien Miller42c5ec42018-11-23 10:40:06 +11003115 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
3116
djm@openbsd.orga287c5a2017-02-10 03:36:40 +00003117 msetlocale();
3118
Damien Miller5428f641999-11-25 11:54:57 +11003119 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11003120 pw = getpwuid(getuid());
djm@openbsd.org3038a192015-04-17 13:19:22 +00003121 if (!pw)
3122 fatal("No user exists for uid %lu", (u_long)getuid());
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00003123 if (gethostname(hostname, sizeof(hostname)) == -1)
djm@openbsd.org3038a192015-04-17 13:19:22 +00003124 fatal("gethostname: %s", strerror(errno));
Damien Miller5428f641999-11-25 11:54:57 +11003125
djm@openbsd.org23f38c22019-10-31 21:17:09 +00003126 sk_provider = getenv("SSH_SK_PROVIDER");
3127
djm@openbsd.org90399712020-01-02 22:40:09 +00003128 /* Remaining characters: dGjJSTWx */
3129 while ((opt = getopt(argc, argv, "ABHKLQUXceghiklopquvy"
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003130 "C:D:E:F:I:M:N:O:P:R:V:Y:Z:"
djm@openbsd.org3093d122019-12-30 09:49:52 +00003131 "a:b:f:g:m:n:r:s:t:w:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11003132 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10003133 case 'A':
3134 gen_all_hostkeys = 1;
3135 break;
Damien Miller95def091999-11-25 00:26:21 +11003136 case 'b':
dtucker@openbsd.org7afc45c2019-08-08 08:02:57 +00003137 bits = (u_int32_t)strtonum(optarg, 1, UINT32_MAX,
3138 &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10003139 if (errstr)
3140 fatal("Bits has bad value %s (%s)",
3141 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11003142 break;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00003143 case 'E':
3144 fingerprint_hash = ssh_digest_alg_by_name(optarg);
3145 if (fingerprint_hash == -1)
3146 fatal("Invalid hash algorithm \"%s\"", optarg);
3147 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11003148 case 'F':
3149 find_host = 1;
3150 rr_hostname = optarg;
3151 break;
3152 case 'H':
3153 hash_hosts = 1;
3154 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11003155 case 'I':
3156 cert_key_id = optarg;
3157 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11003158 case 'R':
3159 delete_host = 1;
3160 rr_hostname = optarg;
3161 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11003162 case 'L':
3163 show_cert = 1;
3164 break;
Damien Miller95def091999-11-25 00:26:21 +11003165 case 'l':
3166 print_fingerprint = 1;
3167 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00003168 case 'B':
3169 print_bubblebabble = 1;
3170 break;
Damien Miller44b25042010-07-02 13:35:01 +10003171 case 'm':
3172 if (strcasecmp(optarg, "RFC4716") == 0 ||
3173 strcasecmp(optarg, "ssh2") == 0) {
3174 convert_format = FMT_RFC4716;
3175 break;
3176 }
3177 if (strcasecmp(optarg, "PKCS8") == 0) {
3178 convert_format = FMT_PKCS8;
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00003179 private_key_format = SSHKEY_PRIVATE_PKCS8;
Damien Miller44b25042010-07-02 13:35:01 +10003180 break;
3181 }
3182 if (strcasecmp(optarg, "PEM") == 0) {
3183 convert_format = FMT_PEM;
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00003184 private_key_format = SSHKEY_PRIVATE_PEM;
Damien Miller44b25042010-07-02 13:35:01 +10003185 break;
3186 }
3187 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11003188 case 'n':
3189 cert_principals = optarg;
3190 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11003191 case 'o':
djm@openbsd.orged7bd5d2018-08-08 01:16:01 +00003192 /* no-op; new format is already the default */
Damien Millerbcd00ab2013-12-07 10:41:55 +11003193 break;
Damien Miller95def091999-11-25 00:26:21 +11003194 case 'p':
3195 change_passphrase = 1;
3196 break;
Damien Miller95def091999-11-25 00:26:21 +11003197 case 'c':
3198 change_comment = 1;
3199 break;
Damien Miller95def091999-11-25 00:26:21 +11003200 case 'f':
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003201 if (strlcpy(identity_file, optarg,
3202 sizeof(identity_file)) >= sizeof(identity_file))
Damien Millerb089fb52005-05-26 12:16:18 +10003203 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11003204 have_identity = 1;
3205 break;
Damien Miller37876e92003-05-15 10:19:46 +10003206 case 'g':
3207 print_generic = 1;
3208 break;
djm@openbsd.org90399712020-01-02 22:40:09 +00003209 case 'K':
3210 download_sk = 1;
3211 break;
Damien Miller95def091999-11-25 00:26:21 +11003212 case 'P':
3213 identity_passphrase = optarg;
3214 break;
Damien Miller95def091999-11-25 00:26:21 +11003215 case 'N':
3216 identity_new_passphrase = optarg;
3217 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11003218 case 'Q':
3219 check_krl = 1;
3220 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11003221 case 'O':
djm@openbsd.org1e645fe2019-12-30 03:28:41 +00003222 opts = xrecallocarray(opts, nopts, nopts + 1,
3223 sizeof(*opts));
3224 opts[nopts++] = xstrdup(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11003225 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11003226 case 'Z':
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00003227 openssh_format_cipher = optarg;
Damien Millerbcd00ab2013-12-07 10:41:55 +11003228 break;
Damien Miller95def091999-11-25 00:26:21 +11003229 case 'C':
3230 identity_comment = optarg;
3231 break;
Damien Miller95def091999-11-25 00:26:21 +11003232 case 'q':
3233 quiet = 1;
3234 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00003235 case 'e':
Ben Lindstrom5a707822001-04-22 17:15:46 +00003236 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10003237 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10003238 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11003239 case 'h':
3240 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10003241 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11003242 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11003243 case 'k':
3244 gen_krl = 1;
3245 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00003246 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10003247 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00003248 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10003249 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10003250 break;
Damien Millereba71ba2000-04-29 23:57:08 +10003251 case 'y':
3252 print_public = 1;
3253 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11003254 case 's':
3255 ca_key_path = optarg;
3256 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003257 case 't':
3258 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003259 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00003260 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11003261 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00003262 break;
djm@openbsd.orga98339e2017-06-28 01:09:22 +00003263 case 'U':
3264 prefer_agent = 1;
3265 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11003266 case 'u':
3267 update_krl = 1;
3268 break;
Darren Tucker06930c72003-12-31 11:34:51 +11003269 case 'v':
3270 if (log_level == SYSLOG_LEVEL_INFO)
3271 log_level = SYSLOG_LEVEL_DEBUG1;
3272 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10003273 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11003274 log_level < SYSLOG_LEVEL_DEBUG3)
3275 log_level++;
3276 }
3277 break;
Damien Miller37876e92003-05-15 10:19:46 +10003278 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11003279 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10003280 break;
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003281 case 'a':
3282 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
3283 if (errstr)
3284 fatal("Invalid number: %s (%s)",
3285 optarg, errstr);
3286 break;
3287 case 'V':
3288 parse_cert_times(optarg);
3289 break;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003290 case 'Y':
3291 sign_op = optarg;
djm@openbsd.org103c51f2019-11-18 01:59:48 +00003292 break;
djm@openbsd.org23f38c22019-10-31 21:17:09 +00003293 case 'w':
3294 sk_provider = optarg;
3295 break;
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003296 case 'z':
3297 errno = 0;
djm@openbsd.orgbe063942019-01-23 04:51:02 +00003298 if (*optarg == '+') {
3299 cert_serial_autoinc = 1;
3300 optarg++;
3301 }
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003302 cert_serial = strtoull(optarg, &ep, 10);
3303 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
3304 (errno == ERANGE && cert_serial == ULLONG_MAX))
3305 fatal("Invalid serial number \"%s\"", optarg);
3306 break;
Darren Tuckerd8c3cfb2016-09-12 13:30:50 +10003307 case 'M':
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003308 if (strcmp(optarg, "generate") == 0)
3309 do_gen_candidates = 1;
3310 else if (strcmp(optarg, "screen") == 0)
3311 do_screen_candidates = 1;
3312 else
3313 fatal("Unsupported moduli option %s", optarg);
Darren Tuckerd8c3cfb2016-09-12 13:30:50 +10003314 break;
Damien Miller95def091999-11-25 00:26:21 +11003315 case '?':
3316 default:
3317 usage();
3318 }
3319 }
Darren Tucker06930c72003-12-31 11:34:51 +11003320
djm@openbsd.org6bff9522019-11-14 21:27:29 +00003321#ifdef ENABLE_SK_INTERNAL
3322 if (sk_provider == NULL)
3323 sk_provider = "internal";
3324#endif
3325
Darren Tucker06930c72003-12-31 11:34:51 +11003326 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11003327 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11003328
Damien Miller0a80ca12010-02-27 07:55:05 +11003329 argv += optind;
3330 argc -= optind;
3331
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003332 if (sign_op != NULL) {
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003333 if (strncmp(sign_op, "find-principals", 15) == 0) {
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003334 if (ca_key_path == NULL) {
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003335 error("Too few arguments for find-principals:"
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003336 "missing signature file");
3337 exit(1);
3338 }
3339 if (!have_identity) {
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003340 error("Too few arguments for find-principals:"
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003341 "missing allowed keys file");
3342 exit(1);
3343 }
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003344 return sig_find_principals(ca_key_path, identity_file);
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00003345 } else if (strncmp(sign_op, "sign", 4) == 0) {
3346 if (cert_principals == NULL ||
3347 *cert_principals == '\0') {
3348 error("Too few arguments for sign: "
3349 "missing namespace");
3350 exit(1);
3351 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003352 if (!have_identity) {
3353 error("Too few arguments for sign: "
3354 "missing key");
3355 exit(1);
3356 }
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003357 return sig_sign(identity_file, cert_principals,
3358 argc, argv);
djm@openbsd.org8aa2aa32019-09-16 03:23:02 +00003359 } else if (strncmp(sign_op, "check-novalidate", 16) == 0) {
3360 if (ca_key_path == NULL) {
3361 error("Too few arguments for check-novalidate: "
3362 "missing signature file");
3363 exit(1);
3364 }
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003365 return sig_verify(ca_key_path, cert_principals,
3366 NULL, NULL, NULL);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003367 } else if (strncmp(sign_op, "verify", 6) == 0) {
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00003368 if (cert_principals == NULL ||
3369 *cert_principals == '\0') {
3370 error("Too few arguments for verify: "
3371 "missing namespace");
3372 exit(1);
3373 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003374 if (ca_key_path == NULL) {
3375 error("Too few arguments for verify: "
3376 "missing signature file");
3377 exit(1);
3378 }
3379 if (!have_identity) {
3380 error("Too few arguments for sign: "
3381 "missing allowed keys file");
3382 exit(1);
3383 }
3384 if (cert_key_id == NULL) {
3385 error("Too few arguments for verify: "
3386 "missing principal ID");
3387 exit(1);
3388 }
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003389 return sig_verify(ca_key_path, cert_principals,
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003390 cert_key_id, identity_file, rr_hostname);
3391 }
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00003392 error("Unsupported operation for -Y: \"%s\"", sign_op);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003393 usage();
3394 /* NOTREACHED */
3395 }
3396
Damien Miller0a80ca12010-02-27 07:55:05 +11003397 if (ca_key_path != NULL) {
Damien Millerf3747bf2013-01-18 11:44:04 +11003398 if (argc < 1 && !gen_krl) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003399 error("Too few arguments.");
Damien Miller0a80ca12010-02-27 07:55:05 +11003400 usage();
3401 }
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003402 } else if (argc > 0 && !gen_krl && !check_krl &&
3403 !do_gen_candidates && !do_screen_candidates) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003404 error("Too many arguments.");
Damien Miller95def091999-11-25 00:26:21 +11003405 usage();
3406 }
3407 if (change_passphrase && change_comment) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003408 error("Can only have one of -p and -c.");
Damien Miller95def091999-11-25 00:26:21 +11003409 usage();
3410 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10003411 if (print_fingerprint && (delete_host || hash_hosts)) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003412 error("Cannot use -l with -H or -R.");
Darren Tucker0f7e9102008-06-08 12:54:29 +10003413 usage();
3414 }
Damien Millerf3747bf2013-01-18 11:44:04 +11003415 if (gen_krl) {
djm@openbsd.org851f8032019-01-23 04:16:22 +00003416 do_gen_krl(pw, update_krl, ca_key_path,
3417 cert_serial, identity_comment, argc, argv);
Damien Millerf3747bf2013-01-18 11:44:04 +11003418 return (0);
3419 }
3420 if (check_krl) {
djm@openbsd.org6ec74572020-04-03 02:26:56 +00003421 do_check_krl(pw, print_fingerprint, argc, argv);
Damien Millerf3747bf2013-01-18 11:44:04 +11003422 return (0);
3423 }
Damien Miller0a80ca12010-02-27 07:55:05 +11003424 if (ca_key_path != NULL) {
3425 if (cert_key_id == NULL)
3426 fatal("Must specify key id (-I) when certifying");
djm@openbsd.org1e645fe2019-12-30 03:28:41 +00003427 for (i = 0; i < nopts; i++)
3428 add_cert_option(opts[i]);
djm@openbsd.orgbe063942019-01-23 04:51:02 +00003429 do_ca_sign(pw, ca_key_path, prefer_agent,
3430 cert_serial, cert_serial_autoinc, argc, argv);
Damien Miller0a80ca12010-02-27 07:55:05 +11003431 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11003432 if (show_cert)
3433 do_show_cert(pw);
djm@openbsd.org851f8032019-01-23 04:16:22 +00003434 if (delete_host || hash_hosts || find_host) {
3435 do_known_hosts(pw, rr_hostname, find_host,
3436 delete_host, hash_hosts);
3437 }
Damien Millerec77c952013-01-09 15:58:00 +11003438 if (pkcs11provider != NULL)
3439 do_download(pw);
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003440 if (download_sk) {
3441 for (i = 0; i < nopts; i++) {
3442 if (strncasecmp(opts[i], "device=", 7) == 0) {
3443 sk_device = xstrdup(opts[i] + 7);
3444 } else {
3445 fatal("Option \"%s\" is unsupported for "
3446 "FIDO authenticator download", opts[i]);
3447 }
3448 }
3449 return do_download_sk(sk_provider, sk_device);
3450 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00003451 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11003452 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11003453 if (change_passphrase)
3454 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11003455 if (change_comment)
djm@openbsd.org851f8032019-01-23 04:16:22 +00003456 do_change_comment(pw, identity_comment);
Damien Miller1f0311c2014-05-15 14:24:09 +10003457#ifdef WITH_OPENSSL
Damien Miller44b25042010-07-02 13:35:01 +10003458 if (convert_to)
3459 do_convert_to(pw);
3460 if (convert_from)
3461 do_convert_from(pw);
djm@openbsd.orgc8bdd2d2019-09-06 07:53:40 +00003462#else /* WITH_OPENSSL */
3463 if (convert_to || convert_from)
3464 fatal("key conversion disabled at compile time");
3465#endif /* WITH_OPENSSL */
Damien Millereba71ba2000-04-29 23:57:08 +10003466 if (print_public)
3467 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11003468 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11003469 unsigned int n = 0;
3470
3471 if (have_identity) {
djm@openbsd.org851f8032019-01-23 04:16:22 +00003472 n = do_print_resource_record(pw, identity_file,
3473 rr_hostname, print_generic);
djm@openbsd.org3038a192015-04-17 13:19:22 +00003474 if (n == 0)
3475 fatal("%s: %s", identity_file, strerror(errno));
Damien Millercb314822006-03-26 13:48:01 +11003476 exit(0);
3477 } else {
3478
3479 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003480 _PATH_HOST_RSA_KEY_FILE, rr_hostname,
3481 print_generic);
Damien Millercb314822006-03-26 13:48:01 +11003482 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003483 _PATH_HOST_DSA_KEY_FILE, rr_hostname,
3484 print_generic);
Damien Miller3bde12a2012-06-20 21:51:11 +10003485 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003486 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname,
3487 print_generic);
Damien Miller16cd3922014-05-15 13:45:58 +10003488 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003489 _PATH_HOST_ED25519_KEY_FILE, rr_hostname,
3490 print_generic);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00003491 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003492 _PATH_HOST_XMSS_KEY_FILE, rr_hostname,
3493 print_generic);
Damien Millercb314822006-03-26 13:48:01 +11003494 if (n == 0)
3495 fatal("no keys found.");
3496 exit(0);
3497 }
Damien Miller37876e92003-05-15 10:19:46 +10003498 }
Damien Miller95def091999-11-25 00:26:21 +11003499
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003500 if (do_gen_candidates || do_screen_candidates) {
3501 if (argc <= 0)
3502 fatal("No output file specified");
3503 else if (argc > 1)
3504 fatal("Too many output files specified");
3505 }
Darren Tucker019cefe2003-08-02 22:40:07 +10003506 if (do_gen_candidates) {
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003507 do_moduli_gen(argv[0], opts, nopts);
3508 return 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10003509 }
Darren Tucker019cefe2003-08-02 22:40:07 +10003510 if (do_screen_candidates) {
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003511 do_moduli_screen(argv[0], opts, nopts);
3512 return 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10003513 }
3514
Damien Miller58f1baf2011-05-05 14:06:15 +10003515 if (gen_all_hostkeys) {
3516 do_gen_all_hostkeys(pw);
3517 return (0);
3518 }
3519
Damien Millerf14be5c2005-11-05 15:15:49 +11003520 if (key_type_name == NULL)
djm@openbsd.orgd1958792015-05-28 04:40:13 +00003521 key_type_name = DEFAULT_KEY_TYPE_NAME;
Damien Millerf14be5c2005-11-05 15:15:49 +11003522
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003523 type = sshkey_type_from_name(key_type_name);
djm@openbsd.org7efb4552015-01-18 13:22:28 +00003524 type_bits_valid(type, key_type_name, &bits);
Damien Miller58f1baf2011-05-05 14:06:15 +10003525
Darren Tucker3af2ac52005-11-29 13:10:24 +11003526 if (!quiet)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003527 printf("Generating public/private %s key pair.\n",
3528 key_type_name);
markus@openbsd.org2c557442019-11-12 19:33:08 +00003529 switch (type) {
3530 case KEY_ECDSA_SK:
3531 case KEY_ED25519_SK:
djm@openbsd.org3093d122019-12-30 09:49:52 +00003532 for (i = 0; i < nopts; i++) {
3533 if (strcasecmp(opts[i], "no-touch-required") == 0) {
3534 sk_flags &= ~SSH_SK_USER_PRESENCE_REQD;
3535 } else if (strcasecmp(opts[i], "resident") == 0) {
3536 sk_flags |= SSH_SK_RESIDENT_KEY;
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003537 } else if (strncasecmp(opts[i], "device=", 7) == 0) {
3538 sk_device = xstrdup(opts[i] + 7);
3539 } else if (strncasecmp(opts[i], "user=", 5) == 0) {
3540 sk_user = xstrdup(opts[i] + 5);
djm@openbsd.org24c0f752020-01-28 08:01:34 +00003541 } else if (strncasecmp(opts[i], "challenge=", 10) == 0) {
3542 if ((r = sshbuf_load_file(opts[i] + 10,
3543 &challenge)) != 0) {
3544 fatal("Unable to load FIDO enrollment "
3545 "challenge \"%s\": %s",
3546 opts[i] + 10, ssh_err(r));
3547 }
3548 } else if (strncasecmp(opts[i],
3549 "write-attestation=", 18) == 0) {
3550 sk_attestaion_path = opts[i] + 18;
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003551 } else if (strncasecmp(opts[i],
3552 "application=", 12) == 0) {
3553 sk_application = xstrdup(opts[i] + 12);
djm@openbsd.orgd596b1d2020-02-04 09:58:04 +00003554 if (strncmp(sk_application, "ssh:", 4) != 0) {
3555 fatal("FIDO application string must "
3556 "begin with \"ssh:\"");
3557 }
djm@openbsd.org3093d122019-12-30 09:49:52 +00003558 } else {
3559 fatal("Option \"%s\" is unsupported for "
3560 "FIDO authenticator enrollment", opts[i]);
3561 }
3562 }
3563 if (!quiet) {
naddy@openbsd.orga47f6a62020-02-06 22:30:54 +00003564 printf("You may need to touch your authenticator "
djm@openbsd.org3093d122019-12-30 09:49:52 +00003565 "to authorize key generation.\n");
3566 }
djm@openbsd.org90399712020-01-02 22:40:09 +00003567 passphrase = NULL;
djm@openbsd.org24c0f752020-01-28 08:01:34 +00003568 if ((attest = sshbuf_new()) == NULL)
3569 fatal("sshbuf_new failed");
djm@openbsd.orgfd68dc22020-02-07 03:27:54 +00003570 for (i = 0 ; ; i++) {
djm@openbsd.org43ce9642019-12-30 09:24:45 +00003571 fflush(stdout);
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003572 r = sshsk_enroll(type, sk_provider, sk_device,
3573 sk_application == NULL ? "ssh:" : sk_application,
djm@openbsd.org24c0f752020-01-28 08:01:34 +00003574 sk_user, sk_flags, passphrase, challenge,
3575 &private, attest);
djm@openbsd.org43ce9642019-12-30 09:24:45 +00003576 if (r == 0)
3577 break;
3578 if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
djm@openbsd.org59d01f12020-01-25 23:13:09 +00003579 fatal("Key enrollment failed: %s", ssh_err(r));
djm@openbsd.orgfd68dc22020-02-07 03:27:54 +00003580 else if (i > 0)
3581 error("PIN incorrect");
3582 if (passphrase != NULL) {
djm@openbsd.org90399712020-01-02 22:40:09 +00003583 freezero(passphrase, strlen(passphrase));
djm@openbsd.orgfd68dc22020-02-07 03:27:54 +00003584 passphrase = NULL;
3585 }
3586 if (i >= 3)
3587 fatal("Too many incorrect PINs");
naddy@openbsd.orga47f6a62020-02-06 22:30:54 +00003588 passphrase = read_passphrase("Enter PIN for "
3589 "authenticator: ", RP_ALLOW_STDIN);
djm@openbsd.org26cb1282019-11-25 00:57:27 +00003590 }
djm@openbsd.orgfd68dc22020-02-07 03:27:54 +00003591 if (passphrase != NULL) {
djm@openbsd.org90399712020-01-02 22:40:09 +00003592 freezero(passphrase, strlen(passphrase));
djm@openbsd.orgfd68dc22020-02-07 03:27:54 +00003593 passphrase = NULL;
3594 }
djm@openbsd.org90399712020-01-02 22:40:09 +00003595 break;
markus@openbsd.org2c557442019-11-12 19:33:08 +00003596 default:
3597 if ((r = sshkey_generate(type, bits, &private)) != 0)
3598 fatal("sshkey_generate failed");
3599 break;
3600 }
djm@openbsd.org3038a192015-04-17 13:19:22 +00003601 if ((r = sshkey_from_private(private, &public)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00003602 fatal("sshkey_from_private failed: %s\n", ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11003603
3604 if (!have_identity)
3605 ask_filename(pw, "Enter file in which to save the key");
3606
Damien Miller788f2122005-11-05 15:14:59 +11003607 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10003608 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
3609 pw->pw_dir, _PATH_SSH_USER_DIR);
3610 if (strstr(identity_file, dotsshdir) != NULL) {
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00003611 if (stat(dotsshdir, &st) == -1) {
Damien Miller50af79b2010-05-10 11:52:00 +10003612 if (errno != ENOENT) {
3613 error("Could not stat %s: %s", dotsshdir,
3614 strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00003615 } else if (mkdir(dotsshdir, 0700) == -1) {
Damien Miller50af79b2010-05-10 11:52:00 +10003616 error("Could not create directory '%s': %s",
3617 dotsshdir, strerror(errno));
3618 } else if (!quiet)
3619 printf("Created directory '%s'.\n", dotsshdir);
3620 }
Damien Miller95def091999-11-25 00:26:21 +11003621 }
3622 /* If the file already exists, ask the user to confirm. */
djm@openbsd.org85443f12019-09-03 08:27:52 +00003623 if (!confirm_overwrite(identity_file))
3624 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11003625
djm@openbsd.org90399712020-01-02 22:40:09 +00003626 /* Determine the passphrase for the private key */
3627 passphrase = private_key_passphrase();
Damien Miller95def091999-11-25 00:26:21 +11003628 if (identity_comment) {
3629 strlcpy(comment, identity_comment, sizeof(comment));
3630 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11003631 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11003632 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
3633 }
3634
3635 /* Save the key with the given passphrase and comment. */
djm@openbsd.org90399712020-01-02 22:40:09 +00003636 if ((r = sshkey_save_private(private, identity_file, passphrase,
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00003637 comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003638 error("Saving key \"%s\" failed: %s",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003639 identity_file, ssh_err(r));
djm@openbsd.org90399712020-01-02 22:40:09 +00003640 freezero(passphrase, strlen(passphrase));
Damien Miller95def091999-11-25 00:26:21 +11003641 exit(1);
3642 }
djm@openbsd.org90399712020-01-02 22:40:09 +00003643 freezero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003644 sshkey_free(private);
Damien Miller95def091999-11-25 00:26:21 +11003645
djm@openbsd.org90399712020-01-02 22:40:09 +00003646 if (!quiet) {
djm@openbsd.org69ac4e32020-01-23 07:54:04 +00003647 printf("Your identification has been saved in %s\n",
djm@openbsd.org90399712020-01-02 22:40:09 +00003648 identity_file);
3649 }
Damien Miller95def091999-11-25 00:26:21 +11003650
Damien Miller95def091999-11-25 00:26:21 +11003651 strlcat(identity_file, ".pub", sizeof(identity_file));
djm@openbsd.org90399712020-01-02 22:40:09 +00003652 if ((r = sshkey_save_public(public, identity_file, comment)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003653 fatal("Unable to save public key to %s: %s",
djm@openbsd.orgd25d6302020-05-02 07:19:43 +00003654 identity_file, ssh_err(r));
djm@openbsd.org90399712020-01-02 22:40:09 +00003655 }
Damien Miller95def091999-11-25 00:26:21 +11003656
3657 if (!quiet) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00003658 fp = sshkey_fingerprint(public, fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00003659 SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00003660 ra = sshkey_fingerprint(public, fingerprint_hash,
Darren Tucker9c16ac92008-06-13 04:40:35 +10003661 SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00003662 if (fp == NULL || ra == NULL)
3663 fatal("sshkey_fingerprint failed");
djm@openbsd.org69ac4e32020-01-23 07:54:04 +00003664 printf("Your public key has been saved in %s\n",
Damien Millereba71ba2000-04-29 23:57:08 +10003665 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11003666 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00003667 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10003668 printf("The key's randomart image is:\n");
3669 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10003670 free(ra);
3671 free(fp);
Damien Miller95def091999-11-25 00:26:21 +11003672 }
Damien Millereba71ba2000-04-29 23:57:08 +10003673
djm@openbsd.org24c0f752020-01-28 08:01:34 +00003674 if (sk_attestaion_path != NULL) {
3675 if (attest == NULL || sshbuf_len(attest) == 0) {
3676 fatal("Enrollment did not return attestation "
3677 "certificate");
3678 }
3679 if ((r = sshbuf_write_file(sk_attestaion_path, attest)) != 0) {
3680 fatal("Unable to write attestation certificate "
3681 "\"%s\": %s", sk_attestaion_path, ssh_err(r));
3682 }
3683 if (!quiet) {
3684 printf("Your FIDO attestation certificate has been "
3685 "saved in %s\n", sk_attestaion_path);
3686 }
3687 }
3688 sshbuf_free(attest);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003689 sshkey_free(public);
djm@openbsd.org24c0f752020-01-28 08:01:34 +00003690
Damien Miller95def091999-11-25 00:26:21 +11003691 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003692}