blob: 14d2357a71bd515be7c92519a749d79b2ff63c2a [file] [log] [blame]
djm@openbsd.org89a8d452020-01-25 00:03:36 +00001/* $OpenBSD: ssh-keygen.c,v 1.392 2020/01/25 00:03:36 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
Damien Miller72ef7c12015-01-15 02:21:31 +110021#ifdef WITH_OPENSSL
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Miller72ef7c12015-01-15 02:21:31 +110025#endif
Damien Millereba71ba2000-04-29 23:57:08 +100026
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);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000330 explicit_bzero(pass, strlen(pass));
331 free(pass);
332 if (r != 0)
333 fatal("Load key \"%s\": %s", filename, ssh_err(r));
Ben Lindstromd0fca422001-03-26 13:44:06 +0000334 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000335}
336
Damien Miller874d77b2000-10-14 16:23:11 +1100337#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000338#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100339#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000340#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000341
Damien Miller1f0311c2014-05-15 14:24:09 +1000342#ifdef WITH_OPENSSL
Ben Lindstrombba81212001-06-25 05:01:22 +0000343static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000344do_convert_to_ssh2(struct passwd *pw, struct sshkey *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000345{
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000346 struct sshbuf *b;
347 char comment[61], *b64;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000348 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000349
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000350 if ((b = sshbuf_new()) == NULL)
351 fatal("%s: sshbuf_new failed", __func__);
352 if ((r = sshkey_putb(k, b)) != 0)
djm@openbsd.org3038a192015-04-17 13:19:22 +0000353 fatal("key_to_blob failed: %s", ssh_err(r));
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000354 if ((b64 = sshbuf_dtob64_string(b, 1)) == NULL)
355 fatal("%s: sshbuf_dtob64_string failed", __func__);
356
Darren Tuckerd04758d2010-01-12 19:41:57 +1100357 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
358 snprintf(comment, sizeof(comment),
359 "%u-bit %s, converted by %s@%s from OpenSSH",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000360 sshkey_size(k), sshkey_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000361 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100362
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000363 sshkey_free(k);
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000364 sshbuf_free(b);
365
366 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
367 fprintf(stdout, "Comment: \"%s\"\n%s", comment, b64);
368 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
369 free(b64);
Damien Millereba71ba2000-04-29 23:57:08 +1000370 exit(0);
371}
372
Ben Lindstrombba81212001-06-25 05:01:22 +0000373static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000374do_convert_to_pkcs8(struct sshkey *k)
Damien Miller44b25042010-07-02 13:35:01 +1000375{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000376 switch (sshkey_type_plain(k->type)) {
Damien Miller44b25042010-07-02 13:35:01 +1000377 case KEY_RSA:
378 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
379 fatal("PEM_write_RSA_PUBKEY failed");
380 break;
381 case KEY_DSA:
382 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
383 fatal("PEM_write_DSA_PUBKEY failed");
384 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000385#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000386 case KEY_ECDSA:
387 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
388 fatal("PEM_write_EC_PUBKEY failed");
389 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000390#endif
Damien Miller44b25042010-07-02 13:35:01 +1000391 default:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000392 fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000393 }
394 exit(0);
395}
396
397static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000398do_convert_to_pem(struct sshkey *k)
Damien Miller44b25042010-07-02 13:35:01 +1000399{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000400 switch (sshkey_type_plain(k->type)) {
Damien Miller44b25042010-07-02 13:35:01 +1000401 case KEY_RSA:
402 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
403 fatal("PEM_write_RSAPublicKey failed");
404 break;
djm@openbsd.org8dfb6a22020-01-24 00:00:31 +0000405 case KEY_DSA:
406 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
407 fatal("PEM_write_DSA_PUBKEY failed");
408 break;
409 case KEY_ECDSA:
410 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
411 fatal("PEM_write_EC_PUBKEY failed");
412 break;
Damien Miller44b25042010-07-02 13:35:01 +1000413 default:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000414 fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000415 }
416 exit(0);
417}
418
419static void
420do_convert_to(struct passwd *pw)
421{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000422 struct sshkey *k;
Damien Miller44b25042010-07-02 13:35:01 +1000423 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000424 int r;
Damien Miller44b25042010-07-02 13:35:01 +1000425
426 if (!have_identity)
427 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000428 if (stat(identity_file, &st) == -1)
Damien Miller44b25042010-07-02 13:35:01 +1000429 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000430 if ((r = sshkey_load_public(identity_file, &k, NULL)) != 0)
djm@openbsd.org07133222019-09-01 23:47:32 +0000431 k = load_identity(identity_file, NULL);
Damien Miller44b25042010-07-02 13:35:01 +1000432 switch (convert_format) {
433 case FMT_RFC4716:
434 do_convert_to_ssh2(pw, k);
435 break;
436 case FMT_PKCS8:
437 do_convert_to_pkcs8(k);
438 break;
439 case FMT_PEM:
440 do_convert_to_pem(k);
441 break;
442 default:
443 fatal("%s: unknown key format %d", __func__, convert_format);
444 }
445 exit(0);
446}
447
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000448/*
449 * This is almost exactly the bignum1 encoding, but with 32 bit for length
450 * instead of 16.
451 */
Damien Miller44b25042010-07-02 13:35:01 +1000452static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000453buffer_get_bignum_bits(struct sshbuf *b, BIGNUM *value)
Damien Miller874d77b2000-10-14 16:23:11 +1100454{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000455 u_int bytes, bignum_bits;
456 int r;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000457
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000458 if ((r = sshbuf_get_u32(b, &bignum_bits)) != 0)
459 fatal("%s: buffer error: %s", __func__, ssh_err(r));
460 bytes = (bignum_bits + 7) / 8;
461 if (sshbuf_len(b) < bytes)
462 fatal("%s: input buffer too small: need %d have %zu",
463 __func__, bytes, sshbuf_len(b));
464 if (BN_bin2bn(sshbuf_ptr(b), bytes, value) == NULL)
465 fatal("%s: BN_bin2bn failed", __func__);
466 if ((r = sshbuf_consume(b, bytes)) != 0)
467 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100468}
469
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000470static struct sshkey *
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000471do_convert_private_ssh2(struct sshbuf *b)
Damien Miller874d77b2000-10-14 16:23:11 +1100472{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000473 struct sshkey *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100474 char *type, *cipher;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000475 u_char e1, e2, e3, *sig = NULL, data[] = "abcde12345";
476 int r, rlen, ktype;
477 u_int magic, i1, i2, i3, i4;
478 size_t slen;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000479 u_long e;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000480 BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
481 BIGNUM *dsa_pub_key = NULL, *dsa_priv_key = NULL;
482 BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
483 BIGNUM *rsa_p = NULL, *rsa_q = NULL, *rsa_iqmp = NULL;
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000484
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000485 if ((r = sshbuf_get_u32(b, &magic)) != 0)
486 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100487
Damien Miller874d77b2000-10-14 16:23:11 +1100488 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000489 error("bad magic 0x%x != 0x%x", magic,
490 SSH_COM_PRIVATE_KEY_MAGIC);
Damien Miller874d77b2000-10-14 16:23:11 +1100491 return NULL;
492 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000493 if ((r = sshbuf_get_u32(b, &i1)) != 0 ||
494 (r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
495 (r = sshbuf_get_cstring(b, &cipher, NULL)) != 0 ||
496 (r = sshbuf_get_u32(b, &i2)) != 0 ||
497 (r = sshbuf_get_u32(b, &i3)) != 0 ||
498 (r = sshbuf_get_u32(b, &i4)) != 0)
499 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller80163902007-01-05 16:30:16 +1100500 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100501 if (strcmp(cipher, "none") != 0) {
502 error("unsupported cipher %s", cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000503 free(cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000504 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100505 return NULL;
506 }
Darren Tuckera627d422013-06-02 07:31:17 +1000507 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100508
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000509 if (strstr(type, "dsa")) {
510 ktype = KEY_DSA;
511 } else if (strstr(type, "rsa")) {
512 ktype = KEY_RSA;
513 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000514 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100515 return NULL;
516 }
djm@openbsd.org6da046f2018-09-14 04:17:44 +0000517 if ((key = sshkey_new(ktype)) == NULL)
518 fatal("sshkey_new failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000519 free(type);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000520
521 switch (key->type) {
522 case KEY_DSA:
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000523 if ((dsa_p = BN_new()) == NULL ||
524 (dsa_q = BN_new()) == NULL ||
525 (dsa_g = BN_new()) == NULL ||
526 (dsa_pub_key = BN_new()) == NULL ||
527 (dsa_priv_key = BN_new()) == NULL)
528 fatal("%s: BN_new", __func__);
529 buffer_get_bignum_bits(b, dsa_p);
530 buffer_get_bignum_bits(b, dsa_g);
531 buffer_get_bignum_bits(b, dsa_q);
532 buffer_get_bignum_bits(b, dsa_pub_key);
533 buffer_get_bignum_bits(b, dsa_priv_key);
534 if (!DSA_set0_pqg(key->dsa, dsa_p, dsa_q, dsa_g))
535 fatal("%s: DSA_set0_pqg failed", __func__);
536 dsa_p = dsa_q = dsa_g = NULL; /* transferred */
537 if (!DSA_set0_key(key->dsa, dsa_pub_key, dsa_priv_key))
538 fatal("%s: DSA_set0_key failed", __func__);
539 dsa_pub_key = dsa_priv_key = NULL; /* transferred */
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000540 break;
541 case KEY_RSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000542 if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
543 (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
544 (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
545 fatal("%s: buffer error: %s", __func__, ssh_err(r));
546 e = e1;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000547 debug("e %lx", e);
548 if (e < 30) {
549 e <<= 8;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000550 e += e2;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000551 debug("e %lx", e);
552 e <<= 8;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000553 e += e3;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000554 debug("e %lx", e);
555 }
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000556 if ((rsa_e = BN_new()) == NULL)
557 fatal("%s: BN_new", __func__);
558 if (!BN_set_word(rsa_e, e)) {
559 BN_clear_free(rsa_e);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000560 sshkey_free(key);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000561 return NULL;
562 }
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000563 if ((rsa_n = BN_new()) == NULL ||
564 (rsa_d = BN_new()) == NULL ||
565 (rsa_p = BN_new()) == NULL ||
566 (rsa_q = BN_new()) == NULL ||
567 (rsa_iqmp = BN_new()) == NULL)
568 fatal("%s: BN_new", __func__);
569 buffer_get_bignum_bits(b, rsa_d);
570 buffer_get_bignum_bits(b, rsa_n);
571 buffer_get_bignum_bits(b, rsa_iqmp);
572 buffer_get_bignum_bits(b, rsa_q);
573 buffer_get_bignum_bits(b, rsa_p);
574 if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, rsa_d))
575 fatal("%s: RSA_set0_key failed", __func__);
576 rsa_n = rsa_e = rsa_d = NULL; /* transferred */
577 if (!RSA_set0_factors(key->rsa, rsa_p, rsa_q))
578 fatal("%s: RSA_set0_factors failed", __func__);
579 rsa_p = rsa_q = NULL; /* transferred */
580 if ((r = ssh_rsa_complete_crt_parameters(key, rsa_iqmp)) != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000581 fatal("generate RSA parameters failed: %s", ssh_err(r));
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000582 BN_clear_free(rsa_iqmp);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000583 break;
584 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000585 rlen = sshbuf_len(b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000586 if (rlen != 0)
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000587 error("%s: remaining bytes in key blob %d", __func__, rlen);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000588
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000589 /* try the key */
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000590 if (sshkey_sign(key, &sig, &slen, data, sizeof(data),
591 NULL, NULL, 0) != 0 ||
592 sshkey_verify(key, sig, slen, data, sizeof(data),
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000593 NULL, 0, NULL) != 0) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000594 sshkey_free(key);
595 free(sig);
596 return NULL;
597 }
Darren Tuckera627d422013-06-02 07:31:17 +1000598 free(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100599 return key;
600}
601
Damien Miller8056a9d2006-03-15 12:05:40 +1100602static int
603get_line(FILE *fp, char *line, size_t len)
604{
605 int c;
606 size_t pos = 0;
607
608 line[0] = '\0';
609 while ((c = fgetc(fp)) != EOF) {
djm@openbsd.org3038a192015-04-17 13:19:22 +0000610 if (pos >= len - 1)
611 fatal("input line too long.");
Damien Miller90967402006-03-26 14:07:26 +1100612 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100613 case '\r':
614 c = fgetc(fp);
djm@openbsd.org3038a192015-04-17 13:19:22 +0000615 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF)
616 fatal("unget: %s", strerror(errno));
Damien Miller8056a9d2006-03-15 12:05:40 +1100617 return pos;
618 case '\n':
619 return pos;
620 }
621 line[pos++] = c;
622 line[pos] = '\0';
623 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100624 /* We reached EOF */
625 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100626}
627
Ben Lindstrombba81212001-06-25 05:01:22 +0000628static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000629do_convert_from_ssh2(struct passwd *pw, struct sshkey **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000630{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000631 int r, blen, escaped = 0;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000632 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100633 char line[1024];
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000634 struct sshbuf *buf;
Damien Millereba71ba2000-04-29 23:57:08 +1000635 char encoded[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000636 FILE *fp;
637
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000638 if ((buf = sshbuf_new()) == NULL)
639 fatal("sshbuf_new failed");
Damien Millerba3420a2010-06-26 09:39:07 +1000640 if ((fp = fopen(identity_file, "r")) == NULL)
641 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000642 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100643 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
Damien Miller746d1a62013-07-18 16:13:02 +1000644 if (blen > 0 && line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000645 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000646 if (strncmp(line, "----", 4) == 0 ||
647 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100648 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000649 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000650 if (strstr(line, " END ") != NULL) {
651 break;
652 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000653 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000654 continue;
655 }
Damien Miller30c3d422000-05-09 11:02:59 +1000656 if (escaped) {
657 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000658 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000659 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000660 }
Damien Millereba71ba2000-04-29 23:57:08 +1000661 strlcat(encoded, line, sizeof(encoded));
662 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000663 len = strlen(encoded);
664 if (((len % 4) == 3) &&
665 (encoded[len-1] == '=') &&
666 (encoded[len-2] == '=') &&
667 (encoded[len-3] == '='))
668 encoded[len-3] = '\0';
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000669 if ((r = sshbuf_b64tod(buf, encoded)) != 0)
670 fatal("%s: base64 decoding failed: %s", __func__, ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000671 if (*private)
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000672 *k = do_convert_private_ssh2(buf);
673 else if ((r = sshkey_fromb(buf, k)) != 0)
djm@openbsd.org3038a192015-04-17 13:19:22 +0000674 fatal("decode blob failed: %s", ssh_err(r));
djm@openbsd.org6500c3b2019-10-16 06:03:30 +0000675 sshbuf_free(buf);
Damien Miller44b25042010-07-02 13:35:01 +1000676 fclose(fp);
677}
678
679static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000680do_convert_from_pkcs8(struct sshkey **k, int *private)
Damien Miller44b25042010-07-02 13:35:01 +1000681{
682 EVP_PKEY *pubkey;
683 FILE *fp;
684
685 if ((fp = fopen(identity_file, "r")) == NULL)
686 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
687 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
688 fatal("%s: %s is not a recognised public key format", __func__,
689 identity_file);
690 }
691 fclose(fp);
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000692 switch (EVP_PKEY_base_id(pubkey)) {
Damien Miller44b25042010-07-02 13:35:01 +1000693 case EVP_PKEY_RSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000694 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
695 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000696 (*k)->type = KEY_RSA;
697 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
698 break;
699 case EVP_PKEY_DSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000700 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
701 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000702 (*k)->type = KEY_DSA;
703 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
704 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000705#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000706 case EVP_PKEY_EC:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000707 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
708 fatal("sshkey_new failed");
Damien Millereb8b60e2010-08-31 22:41:14 +1000709 (*k)->type = KEY_ECDSA;
710 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000711 (*k)->ecdsa_nid = sshkey_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000712 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000713#endif
Damien Miller44b25042010-07-02 13:35:01 +1000714 default:
715 fatal("%s: unsupported pubkey type %d", __func__,
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000716 EVP_PKEY_base_id(pubkey));
Damien Miller44b25042010-07-02 13:35:01 +1000717 }
718 EVP_PKEY_free(pubkey);
719 return;
720}
721
722static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000723do_convert_from_pem(struct sshkey **k, int *private)
Damien Miller44b25042010-07-02 13:35:01 +1000724{
725 FILE *fp;
726 RSA *rsa;
Damien Miller44b25042010-07-02 13:35:01 +1000727
728 if ((fp = fopen(identity_file, "r")) == NULL)
729 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
730 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000731 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
732 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000733 (*k)->type = KEY_RSA;
734 (*k)->rsa = rsa;
735 fclose(fp);
736 return;
737 }
Damien Miller44b25042010-07-02 13:35:01 +1000738 fatal("%s: unrecognised raw private key format", __func__);
739}
740
741static void
742do_convert_from(struct passwd *pw)
743{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000744 struct sshkey *k = NULL;
745 int r, private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000746 struct stat st;
747
748 if (!have_identity)
749 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000750 if (stat(identity_file, &st) == -1)
Damien Miller44b25042010-07-02 13:35:01 +1000751 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
752
753 switch (convert_format) {
754 case FMT_RFC4716:
755 do_convert_from_ssh2(pw, &k, &private);
756 break;
757 case FMT_PKCS8:
758 do_convert_from_pkcs8(&k, &private);
759 break;
760 case FMT_PEM:
761 do_convert_from_pem(&k, &private);
762 break;
763 default:
764 fatal("%s: unknown key format %d", __func__, convert_format);
765 }
766
djm@openbsd.org7a2c3682015-01-30 00:59:19 +0000767 if (!private) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000768 if ((r = sshkey_write(k, stdout)) == 0)
769 ok = 1;
Damien Miller44b25042010-07-02 13:35:01 +1000770 if (ok)
771 fprintf(stdout, "\n");
djm@openbsd.org7a2c3682015-01-30 00:59:19 +0000772 } else {
Damien Miller44b25042010-07-02 13:35:01 +1000773 switch (k->type) {
774 case KEY_DSA:
775 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
776 NULL, 0, NULL, NULL);
777 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000778#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000779 case KEY_ECDSA:
780 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
781 NULL, 0, NULL, NULL);
782 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000783#endif
Damien Miller44b25042010-07-02 13:35:01 +1000784 case KEY_RSA:
785 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
786 NULL, 0, NULL, NULL);
787 break;
788 default:
789 fatal("%s: unsupported key type %s", __func__,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000790 sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000791 }
792 }
793
djm@openbsd.org3038a192015-04-17 13:19:22 +0000794 if (!ok)
795 fatal("key write failed");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000796 sshkey_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000797 exit(0);
798}
Damien Miller1f0311c2014-05-15 14:24:09 +1000799#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000800
Ben Lindstrombba81212001-06-25 05:01:22 +0000801static void
Damien Millereba71ba2000-04-29 23:57:08 +1000802do_print_public(struct passwd *pw)
803{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000804 struct sshkey *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000805 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000806 int r;
djm@openbsd.org07133222019-09-01 23:47:32 +0000807 char *comment = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000808
809 if (!have_identity)
810 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000811 if (stat(identity_file, &st) == -1)
djm@openbsd.org3038a192015-04-17 13:19:22 +0000812 fatal("%s: %s", identity_file, strerror(errno));
djm@openbsd.org07133222019-09-01 23:47:32 +0000813 prv = load_identity(identity_file, &comment);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000814 if ((r = sshkey_write(prv, stdout)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +0000815 error("sshkey_write failed: %s", ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000816 sshkey_free(prv);
djm@openbsd.org07133222019-09-01 23:47:32 +0000817 if (comment != NULL && *comment != '\0')
818 fprintf(stdout, " %s", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000819 fprintf(stdout, "\n");
djm@openbsd.org07133222019-09-01 23:47:32 +0000820 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000821 exit(0);
822}
823
Ben Lindstromcd392282001-07-04 03:44:03 +0000824static void
Damien Miller757f34e2010-08-05 13:05:31 +1000825do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000826{
Damien Miller7ea845e2010-02-12 09:21:02 +1100827#ifdef ENABLE_PKCS11
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000828 struct sshkey **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100829 int i, nkeys;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000830 enum sshkey_fp_rep rep;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000831 int fptype;
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000832 char *fp, *ra, **comments = NULL;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000833
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000834 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
835 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
Damien Miller1422c082013-01-09 16:44:54 +1100836
benno@openbsd.orga8c807f2019-02-10 16:35:41 +0000837 pkcs11_init(1);
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000838 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys, &comments);
Damien Miller7ea845e2010-02-12 09:21:02 +1100839 if (nkeys <= 0)
840 fatal("cannot read public key from pkcs11");
841 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100842 if (print_fingerprint) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000843 fp = sshkey_fingerprint(keys[i], fptype, rep);
844 ra = sshkey_fingerprint(keys[i], fingerprint_hash,
Damien Millerec77c952013-01-09 15:58:00 +1100845 SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000846 if (fp == NULL || ra == NULL)
847 fatal("%s: sshkey_fingerprint fail", __func__);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000848 printf("%u %s %s (PKCS11 key)\n", sshkey_size(keys[i]),
849 fp, sshkey_type(keys[i]));
djm@openbsd.org851f8032019-01-23 04:16:22 +0000850 if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
Damien Millerec77c952013-01-09 15:58:00 +1100851 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000852 free(ra);
853 free(fp);
Damien Millerec77c952013-01-09 15:58:00 +1100854 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000855 (void) sshkey_write(keys[i], stdout); /* XXX check */
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000856 fprintf(stdout, "%s%s\n",
857 *(comments[i]) == '\0' ? "" : " ", comments[i]);
Damien Millerec77c952013-01-09 15:58:00 +1100858 }
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000859 free(comments[i]);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000860 sshkey_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000861 }
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000862 free(comments);
Darren Tuckera627d422013-06-02 07:31:17 +1000863 free(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100864 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000865 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100866#else
867 fatal("no pkcs11 support");
868#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000869}
Ben Lindstromcd392282001-07-04 03:44:03 +0000870
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000871static struct sshkey *
872try_read_key(char **cpp)
873{
874 struct sshkey *ret;
875 int r;
876
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000877 if ((ret = sshkey_new(KEY_UNSPEC)) == NULL)
878 fatal("sshkey_new failed");
879 if ((r = sshkey_read(ret, cpp)) == 0)
880 return ret;
881 /* Not a key */
882 sshkey_free(ret);
883 return NULL;
884}
885
886static void
887fingerprint_one_key(const struct sshkey *public, const char *comment)
888{
889 char *fp = NULL, *ra = NULL;
890 enum sshkey_fp_rep rep;
891 int fptype;
892
893 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
894 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
895 fp = sshkey_fingerprint(public, fptype, rep);
896 ra = sshkey_fingerprint(public, fingerprint_hash, SSH_FP_RANDOMART);
897 if (fp == NULL || ra == NULL)
898 fatal("%s: sshkey_fingerprint failed", __func__);
djm@openbsd.orga287c5a2017-02-10 03:36:40 +0000899 mprintf("%u %s %s (%s)\n", sshkey_size(public), fp,
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000900 comment ? comment : "no comment", sshkey_type(public));
djm@openbsd.org851f8032019-01-23 04:16:22 +0000901 if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000902 printf("%s\n", ra);
903 free(ra);
904 free(fp);
905}
906
907static void
908fingerprint_private(const char *path)
909{
910 struct stat st;
911 char *comment = NULL;
912 struct sshkey *public = NULL;
913 int r;
914
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000915 if (stat(identity_file, &st) == -1)
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000916 fatal("%s: %s", path, strerror(errno));
djm@openbsd.orgbcb7bc72015-11-18 08:37:28 +0000917 if ((r = sshkey_load_public(path, &public, &comment)) != 0) {
918 debug("load public \"%s\": %s", path, ssh_err(r));
919 if ((r = sshkey_load_private(path, NULL,
920 &public, &comment)) != 0) {
921 debug("load private \"%s\": %s", path, ssh_err(r));
922 fatal("%s is not a key file.", path);
923 }
924 }
925
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000926 fingerprint_one_key(public, comment);
927 sshkey_free(public);
928 free(comment);
929}
930
Ben Lindstrombba81212001-06-25 05:01:22 +0000931static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100932do_fingerprint(struct passwd *pw)
933{
Damien Miller98c7ad62000-03-09 21:27:49 +1100934 FILE *f;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000935 struct sshkey *public = NULL;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000936 char *comment = NULL, *cp, *ep, *line = NULL;
937 size_t linesize = 0;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000938 int i, invalid = 1;
939 const char *path;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000940 u_long lnum = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100941
Damien Miller95def091999-11-25 00:26:21 +1100942 if (!have_identity)
943 ask_filename(pw, "Enter file in which the key is");
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000944 path = identity_file;
Damien Miller98c7ad62000-03-09 21:27:49 +1100945
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000946 if (strcmp(identity_file, "-") == 0) {
947 f = stdin;
948 path = "(stdin)";
949 } else if ((f = fopen(path, "r")) == NULL)
950 fatal("%s: %s: %s", __progname, path, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100951
markus@openbsd.org7f906352018-06-06 18:29:18 +0000952 while (getline(&line, &linesize, f) != -1) {
953 lnum++;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000954 cp = line;
955 cp[strcspn(cp, "\n")] = '\0';
956 /* Trim leading space and comments */
957 cp = line + strspn(line, " \t");
958 if (*cp == '#' || *cp == '\0')
959 continue;
960
961 /*
962 * Input may be plain keys, private keys, authorized_keys
963 * or known_hosts.
964 */
965
966 /*
967 * Try private keys first. Assume a key is private if
968 * "SSH PRIVATE KEY" appears on the first line and we're
969 * not reading from stdin (XXX support private keys on stdin).
970 */
971 if (lnum == 1 && strcmp(identity_file, "-") != 0 &&
djm@openbsd.orgbcb7bc72015-11-18 08:37:28 +0000972 strstr(cp, "PRIVATE KEY") != NULL) {
markus@openbsd.org7f906352018-06-06 18:29:18 +0000973 free(line);
djm@openbsd.orgc56a2552015-11-16 22:53:07 +0000974 fclose(f);
975 fingerprint_private(path);
976 exit(0);
977 }
978
979 /*
980 * If it's not a private key, then this must be prepared to
981 * accept a public key prefixed with a hostname or options.
982 * Try a bare key first, otherwise skip the leading stuff.
983 */
984 if ((public = try_read_key(&cp)) == NULL) {
985 i = strtol(cp, &ep, 10);
986 if (i == 0 || ep == NULL ||
987 (*ep != ' ' && *ep != '\t')) {
988 int quoted = 0;
989
990 comment = cp;
991 for (; *cp && (quoted || (*cp != ' ' &&
992 *cp != '\t')); cp++) {
993 if (*cp == '\\' && cp[1] == '"')
994 cp++; /* Skip both */
995 else if (*cp == '"')
996 quoted = !quoted;
997 }
998 if (!*cp)
999 continue;
1000 *cp++ = '\0';
1001 }
1002 }
1003 /* Retry after parsing leading hostname/key options */
1004 if (public == NULL && (public = try_read_key(&cp)) == NULL) {
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001005 debug("%s:%lu: not a public key", path, lnum);
Damien Millerba3420a2010-06-26 09:39:07 +10001006 continue;
Damien Miller95def091999-11-25 00:26:21 +11001007 }
Damien Millerba3420a2010-06-26 09:39:07 +10001008
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001009 /* Find trailing comment, if any */
1010 for (; *cp == ' ' || *cp == '\t'; cp++)
Damien Millerba3420a2010-06-26 09:39:07 +10001011 ;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001012 if (*cp != '\0' && *cp != '#')
Damien Millerba3420a2010-06-26 09:39:07 +10001013 comment = cp;
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001014
1015 fingerprint_one_key(public, comment);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001016 sshkey_free(public);
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001017 invalid = 0; /* One good key in the file is sufficient */
Damien Miller95def091999-11-25 00:26:21 +11001018 }
Damien Millerba3420a2010-06-26 09:39:07 +10001019 fclose(f);
markus@openbsd.org7f906352018-06-06 18:29:18 +00001020 free(line);
Damien Millerba3420a2010-06-26 09:39:07 +10001021
djm@openbsd.org3038a192015-04-17 13:19:22 +00001022 if (invalid)
djm@openbsd.orgc56a2552015-11-16 22:53:07 +00001023 fatal("%s is not a public key file.", path);
Damien Miller95def091999-11-25 00:26:21 +11001024 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +11001025}
1026
Damien Miller4b42d7f2005-03-01 21:48:35 +11001027static void
Damien Miller58f1baf2011-05-05 14:06:15 +10001028do_gen_all_hostkeys(struct passwd *pw)
1029{
1030 struct {
1031 char *key_type;
1032 char *key_type_display;
1033 char *path;
1034 } key_types[] = {
djm@openbsd.org5c27e3b2015-03-23 06:06:38 +00001035#ifdef WITH_OPENSSL
Damien Miller58f1baf2011-05-05 14:06:15 +10001036 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
1037 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +11001038#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +10001039 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
djm@openbsd.org5c27e3b2015-03-23 06:06:38 +00001040#endif /* OPENSSL_HAS_ECC */
1041#endif /* WITH_OPENSSL */
Damien Miller5be9d9e2013-12-07 11:24:01 +11001042 { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001043#ifdef WITH_XMSS
1044 { "xmss", "XMSS",_PATH_HOST_XMSS_KEY_FILE },
1045#endif /* WITH_XMSS */
Damien Miller58f1baf2011-05-05 14:06:15 +10001046 { NULL, NULL, NULL }
1047 };
1048
dtucker@openbsd.org7afc45c2019-08-08 08:02:57 +00001049 u_int32_t bits = 0;
Damien Miller58f1baf2011-05-05 14:06:15 +10001050 int first = 0;
1051 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001052 struct sshkey *private, *public;
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001053 char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001054 int i, type, fd, r;
Damien Miller58f1baf2011-05-05 14:06:15 +10001055 FILE *f;
1056
1057 for (i = 0; key_types[i].key_type; i++) {
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001058 public = private = NULL;
1059 prv_tmp = pub_tmp = prv_file = pub_file = NULL;
1060
1061 xasprintf(&prv_file, "%s%s",
1062 identity_file, key_types[i].path);
1063
1064 /* Check whether private key exists and is not zero-length */
1065 if (stat(prv_file, &st) == 0) {
1066 if (st.st_size != 0)
1067 goto next;
1068 } else if (errno != ENOENT) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001069 error("Could not stat %s: %s", key_types[i].path,
Damien Miller58f1baf2011-05-05 14:06:15 +10001070 strerror(errno));
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001071 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001072 }
1073
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001074 /*
1075 * Private key doesn't exist or is invalid; proceed with
1076 * key generation.
1077 */
1078 xasprintf(&prv_tmp, "%s%s.XXXXXXXXXX",
1079 identity_file, key_types[i].path);
1080 xasprintf(&pub_tmp, "%s%s.pub.XXXXXXXXXX",
1081 identity_file, key_types[i].path);
1082 xasprintf(&pub_file, "%s%s.pub",
1083 identity_file, key_types[i].path);
1084
Damien Miller58f1baf2011-05-05 14:06:15 +10001085 if (first == 0) {
1086 first = 1;
1087 printf("%s: generating new host keys: ", __progname);
1088 }
1089 printf("%s ", key_types[i].key_type_display);
1090 fflush(stdout);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001091 type = sshkey_type_from_name(key_types[i].key_type);
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001092 if ((fd = mkstemp(prv_tmp)) == -1) {
1093 error("Could not save your public key in %s: %s",
1094 prv_tmp, strerror(errno));
1095 goto failnext;
1096 }
1097 close(fd); /* just using mkstemp() to generate/reserve a name */
Damien Miller58f1baf2011-05-05 14:06:15 +10001098 bits = 0;
djm@openbsd.org7efb4552015-01-18 13:22:28 +00001099 type_bits_valid(type, NULL, &bits);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001100 if ((r = sshkey_generate(type, bits, &private)) != 0) {
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001101 error("sshkey_generate failed: %s", ssh_err(r));
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001102 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001103 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001104 if ((r = sshkey_from_private(private, &public)) != 0)
1105 fatal("sshkey_from_private failed: %s", ssh_err(r));
Damien Miller58f1baf2011-05-05 14:06:15 +10001106 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
1107 hostname);
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001108 if ((r = sshkey_save_private(private, prv_tmp, "",
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00001109 comment, private_key_format, openssh_format_cipher,
1110 rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001111 error("Saving key \"%s\" failed: %s",
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001112 prv_tmp, ssh_err(r));
1113 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001114 }
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001115 if ((fd = mkstemp(pub_tmp)) == -1) {
1116 error("Could not save your public key in %s: %s",
1117 pub_tmp, strerror(errno));
1118 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001119 }
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001120 (void)fchmod(fd, 0644);
Damien Miller58f1baf2011-05-05 14:06:15 +10001121 f = fdopen(fd, "w");
1122 if (f == NULL) {
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001123 error("fdopen %s failed: %s", pub_tmp, strerror(errno));
doug@openbsd.org7df88182014-08-21 01:08:52 +00001124 close(fd);
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001125 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001126 }
djm@openbsd.orgbb8b4422015-01-16 15:55:07 +00001127 if ((r = sshkey_write(public, f)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001128 error("write key failed: %s", ssh_err(r));
doug@openbsd.org7df88182014-08-21 01:08:52 +00001129 fclose(f);
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001130 goto failnext;
Damien Miller58f1baf2011-05-05 14:06:15 +10001131 }
1132 fprintf(f, " %s\n", comment);
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001133 if (ferror(f) != 0) {
1134 error("write key failed: %s", strerror(errno));
1135 fclose(f);
1136 goto failnext;
1137 }
1138 if (fclose(f) != 0) {
1139 error("key close failed: %s", strerror(errno));
1140 goto failnext;
1141 }
Damien Miller58f1baf2011-05-05 14:06:15 +10001142
djm@openbsd.org853edbe2017-07-07 03:53:12 +00001143 /* Rename temporary files to their permanent locations. */
1144 if (rename(pub_tmp, pub_file) != 0) {
1145 error("Unable to move %s into position: %s",
1146 pub_file, strerror(errno));
1147 goto failnext;
1148 }
1149 if (rename(prv_tmp, prv_file) != 0) {
1150 error("Unable to move %s into position: %s",
1151 key_types[i].path, strerror(errno));
1152 failnext:
1153 first = 0;
1154 goto next;
1155 }
1156 next:
1157 sshkey_free(private);
1158 sshkey_free(public);
1159 free(prv_tmp);
1160 free(pub_tmp);
1161 free(prv_file);
1162 free(pub_file);
Damien Miller58f1baf2011-05-05 14:06:15 +10001163 }
1164 if (first != 0)
1165 printf("\n");
1166}
1167
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001168struct known_hosts_ctx {
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001169 const char *host; /* Hostname searched for in find/delete case */
1170 FILE *out; /* Output file, stdout for find_hosts case */
1171 int has_unhashed; /* When hashing, original had unhashed hosts */
1172 int found_key; /* For find/delete, host was found */
1173 int invalid; /* File contained invalid items; don't delete */
djm@openbsd.org851f8032019-01-23 04:16:22 +00001174 int hash_hosts; /* Hash hostnames as we go */
1175 int find_host; /* Search for specific hostname */
1176 int delete_host; /* Delete host from known_hosts */
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001177};
1178
1179static int
1180known_hosts_hash(struct hostkey_foreach_line *l, void *_ctx)
Damien Miller4b42d7f2005-03-01 21:48:35 +11001181{
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001182 struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
1183 char *hashed, *cp, *hosts, *ohosts;
1184 int has_wild = l->hosts && strcspn(l->hosts, "*?!") != strlen(l->hosts);
dtucker@openbsd.org18501152017-03-06 02:03:20 +00001185 int was_hashed = l->hosts && l->hosts[0] == HASH_DELIM;
Darren Tucker0f7e9102008-06-08 12:54:29 +10001186
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001187 switch (l->status) {
1188 case HKF_STATUS_OK:
1189 case HKF_STATUS_MATCHED:
1190 /*
1191 * Don't hash hosts already already hashed, with wildcard
1192 * characters or a CA/revocation marker.
1193 */
djm@openbsd.org12d37672017-03-03 06:13:11 +00001194 if (was_hashed || has_wild || l->marker != MRK_NONE) {
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001195 fprintf(ctx->out, "%s\n", l->line);
djm@openbsd.org851f8032019-01-23 04:16:22 +00001196 if (has_wild && !ctx->find_host) {
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001197 logit("%s:%lu: ignoring host name "
djm@openbsd.org3038a192015-04-17 13:19:22 +00001198 "with wildcard: %.64s", l->path,
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001199 l->linenum, l->hosts);
1200 }
1201 return 0;
1202 }
1203 /*
1204 * Split any comma-separated hostnames from the host list,
1205 * hash and store separately.
1206 */
1207 ohosts = hosts = xstrdup(l->hosts);
1208 while ((cp = strsep(&hosts, ",")) != NULL && *cp != '\0') {
djm@openbsd.orgdb259722017-03-10 04:26:06 +00001209 lowercase(cp);
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001210 if ((hashed = host_hash(cp, NULL, 0)) == NULL)
1211 fatal("hash_host failed");
1212 fprintf(ctx->out, "%s %s\n", hashed, l->rawkey);
1213 ctx->has_unhashed = 1;
1214 }
1215 free(ohosts);
1216 return 0;
1217 case HKF_STATUS_INVALID:
1218 /* Retain invalid lines, but mark file as invalid. */
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001219 ctx->invalid = 1;
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001220 logit("%s:%lu: invalid line", l->path, l->linenum);
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001221 /* FALLTHROUGH */
1222 default:
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001223 fprintf(ctx->out, "%s\n", l->line);
1224 return 0;
Darren Tucker0f7e9102008-06-08 12:54:29 +10001225 }
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001226 /* NOTREACHED */
1227 return -1;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001228}
1229
1230static int
1231known_hosts_find_delete(struct hostkey_foreach_line *l, void *_ctx)
1232{
1233 struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001234 enum sshkey_fp_rep rep;
1235 int fptype;
djm@openbsd.orgbca05822019-07-19 03:38:01 +00001236 char *fp = NULL, *ra = NULL;
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001237
1238 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
1239 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001240
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001241 if (l->status == HKF_STATUS_MATCHED) {
djm@openbsd.org851f8032019-01-23 04:16:22 +00001242 if (ctx->delete_host) {
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001243 if (l->marker != MRK_NONE) {
1244 /* Don't remove CA and revocation lines */
1245 fprintf(ctx->out, "%s\n", l->line);
1246 } else {
1247 /*
1248 * Hostname matches and has no CA/revoke
1249 * marker, delete it by *not* writing the
1250 * line to ctx->out.
1251 */
1252 ctx->found_key = 1;
1253 if (!quiet)
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001254 printf("# Host %s found: line %lu\n",
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001255 ctx->host, l->linenum);
1256 }
1257 return 0;
djm@openbsd.org851f8032019-01-23 04:16:22 +00001258 } else if (ctx->find_host) {
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001259 ctx->found_key = 1;
1260 if (!quiet) {
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001261 printf("# Host %s found: line %lu %s\n",
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001262 ctx->host,
1263 l->linenum, l->marker == MRK_CA ? "CA" :
1264 (l->marker == MRK_REVOKE ? "REVOKED" : ""));
1265 }
djm@openbsd.org851f8032019-01-23 04:16:22 +00001266 if (ctx->hash_hosts)
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001267 known_hosts_hash(l, ctx);
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001268 else if (print_fingerprint) {
1269 fp = sshkey_fingerprint(l->key, fptype, rep);
djm@openbsd.orgbca05822019-07-19 03:38:01 +00001270 ra = sshkey_fingerprint(l->key,
1271 fingerprint_hash, SSH_FP_RANDOMART);
1272 if (fp == NULL || ra == NULL)
1273 fatal("%s: sshkey_fingerprint failed",
1274 __func__);
claudio@openbsd.org4a32c0c2020-01-22 04:51:51 +00001275 mprintf("%s %s %s%s%s\n", ctx->host,
1276 sshkey_type(l->key), fp,
1277 l->comment[0] ? " " : "",
1278 l->comment);
djm@openbsd.orgbca05822019-07-19 03:38:01 +00001279 if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
1280 printf("%s\n", ra);
1281 free(ra);
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001282 free(fp);
1283 } else
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001284 fprintf(ctx->out, "%s\n", l->line);
1285 return 0;
1286 }
djm@openbsd.org851f8032019-01-23 04:16:22 +00001287 } else if (ctx->delete_host) {
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001288 /* Retain non-matching hosts when deleting */
1289 if (l->status == HKF_STATUS_INVALID) {
1290 ctx->invalid = 1;
dtucker@openbsd.orgd0723702017-03-06 00:44:51 +00001291 logit("%s:%lu: invalid line", l->path, l->linenum);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001292 }
1293 fprintf(ctx->out, "%s\n", l->line);
1294 }
1295 return 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001296}
1297
1298static void
djm@openbsd.org851f8032019-01-23 04:16:22 +00001299do_known_hosts(struct passwd *pw, const char *name, int find_host,
1300 int delete_host, int hash_hosts)
Damien Miller4b42d7f2005-03-01 21:48:35 +11001301{
deraadt@openbsd.orgd2099de2015-01-19 00:32:54 +00001302 char *cp, tmp[PATH_MAX], old[PATH_MAX];
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001303 int r, fd, oerrno, inplace = 0;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001304 struct known_hosts_ctx ctx;
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001305 u_int foreach_options;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001306
1307 if (!have_identity) {
1308 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1309 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1310 sizeof(identity_file))
1311 fatal("Specified known hosts path too long");
Darren Tuckera627d422013-06-02 07:31:17 +10001312 free(cp);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001313 have_identity = 1;
1314 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001315
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001316 memset(&ctx, 0, sizeof(ctx));
1317 ctx.out = stdout;
1318 ctx.host = name;
djm@openbsd.org851f8032019-01-23 04:16:22 +00001319 ctx.hash_hosts = hash_hosts;
1320 ctx.find_host = find_host;
1321 ctx.delete_host = delete_host;
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001322
Damien Miller4b42d7f2005-03-01 21:48:35 +11001323 /*
1324 * Find hosts goes to stdout, hash and deletions happen in-place
1325 * A corner case is ssh-keygen -HF foo, which should go to stdout
1326 */
1327 if (!find_host && (hash_hosts || delete_host)) {
1328 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1329 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1330 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1331 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1332 fatal("known_hosts path too long");
1333 umask(077);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001334 if ((fd = mkstemp(tmp)) == -1)
Damien Miller4b42d7f2005-03-01 21:48:35 +11001335 fatal("mkstemp: %s", strerror(errno));
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001336 if ((ctx.out = fdopen(fd, "w")) == NULL) {
1337 oerrno = errno;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001338 unlink(tmp);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001339 fatal("fdopen: %s", strerror(oerrno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001340 }
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001341 inplace = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001342 }
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001343 /* XXX support identity_file == "-" for stdin */
djm@openbsd.org4739e8d2015-05-21 12:01:19 +00001344 foreach_options = find_host ? HKF_WANT_MATCH : 0;
1345 foreach_options |= print_fingerprint ? HKF_WANT_PARSE_KEY : 0;
djm@openbsd.org01b048c2018-06-01 04:21:29 +00001346 if ((r = hostkeys_foreach(identity_file, (find_host || !hash_hosts) ?
djm@openbsd.org3e088aa2018-06-01 03:51:34 +00001347 known_hosts_find_delete : known_hosts_hash, &ctx, name, NULL,
1348 foreach_options)) != 0) {
deraadt@openbsd.org6da413c2015-11-28 06:50:52 +00001349 if (inplace)
1350 unlink(tmp);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001351 fatal("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
deraadt@openbsd.org6da413c2015-11-28 06:50:52 +00001352 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001353
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001354 if (inplace)
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001355 fclose(ctx.out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001356
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001357 if (ctx.invalid) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001358 error("%s is not a valid known_hosts file.", identity_file);
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001359 if (inplace) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001360 error("Not replacing existing known_hosts "
1361 "file because of errors");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001362 unlink(tmp);
1363 }
1364 exit(1);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001365 } else if (delete_host && !ctx.found_key) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001366 logit("Host %s not found in %s", name, identity_file);
djm@openbsd.orgc8376432015-08-19 23:17:51 +00001367 if (inplace)
1368 unlink(tmp);
djm@openbsd.org2b3c3c72015-01-18 21:51:19 +00001369 } else if (inplace) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001370 /* Backup existing file */
1371 if (unlink(old) == -1 && errno != ENOENT)
1372 fatal("unlink %.100s: %s", old, strerror(errno));
1373 if (link(identity_file, old) == -1)
1374 fatal("link %.100s to %.100s: %s", identity_file, old,
1375 strerror(errno));
1376 /* Move new one into place */
1377 if (rename(tmp, identity_file) == -1) {
1378 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1379 strerror(errno));
1380 unlink(tmp);
1381 unlink(old);
1382 exit(1);
1383 }
1384
djm@openbsd.org3038a192015-04-17 13:19:22 +00001385 printf("%s updated.\n", identity_file);
1386 printf("Original contents retained as %s\n", old);
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001387 if (ctx.has_unhashed) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001388 logit("WARNING: %s contains unhashed entries", old);
1389 logit("Delete this file to ensure privacy "
1390 "of hostnames");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001391 }
1392 }
1393
djm@openbsd.orgcecb30b2015-01-18 21:49:42 +00001394 exit (find_host && !ctx.found_key);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001395}
1396
Damien Miller95def091999-11-25 00:26:21 +11001397/*
1398 * Perform changing a passphrase. The argument is the passwd structure
1399 * for the current user.
1400 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001401static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001402do_change_passphrase(struct passwd *pw)
1403{
Damien Miller95def091999-11-25 00:26:21 +11001404 char *comment;
1405 char *old_passphrase, *passphrase1, *passphrase2;
1406 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001407 struct sshkey *private;
1408 int r;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001409
Damien Miller95def091999-11-25 00:26:21 +11001410 if (!have_identity)
1411 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001412 if (stat(identity_file, &st) == -1)
djm@openbsd.org3038a192015-04-17 13:19:22 +00001413 fatal("%s: %s", identity_file, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001414 /* Try to load the file with empty passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001415 r = sshkey_load_private(identity_file, "", &private, &comment);
1416 if (r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
Damien Miller95def091999-11-25 00:26:21 +11001417 if (identity_passphrase)
1418 old_passphrase = xstrdup(identity_passphrase);
1419 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001420 old_passphrase =
1421 read_passphrase("Enter old passphrase: ",
1422 RP_ALLOW_STDIN);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001423 r = sshkey_load_private(identity_file, old_passphrase,
1424 &private, &comment);
Damien Millera5103f42014-02-04 11:20:14 +11001425 explicit_bzero(old_passphrase, strlen(old_passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001426 free(old_passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001427 if (r != 0)
1428 goto badkey;
1429 } else if (r != 0) {
1430 badkey:
djm@openbsd.org3038a192015-04-17 13:19:22 +00001431 fatal("Failed to load key %s: %s", identity_file, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001432 }
djm@openbsd.orgf43d1722015-02-26 20:45:47 +00001433 if (comment)
djm@openbsd.orga287c5a2017-02-10 03:36:40 +00001434 mprintf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435
Damien Miller95def091999-11-25 00:26:21 +11001436 /* Ask the new passphrase (twice). */
1437 if (identity_new_passphrase) {
1438 passphrase1 = xstrdup(identity_new_passphrase);
1439 passphrase2 = NULL;
1440 } else {
1441 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001442 read_passphrase("Enter new passphrase (empty for no "
1443 "passphrase): ", RP_ALLOW_STDIN);
1444 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001445 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001446
1447 /* Verify that they are the same. */
1448 if (strcmp(passphrase1, passphrase2) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001449 explicit_bzero(passphrase1, strlen(passphrase1));
1450 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001451 free(passphrase1);
1452 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001453 printf("Pass phrases do not match. Try again.\n");
1454 exit(1);
1455 }
1456 /* Destroy the other copy. */
Damien Millera5103f42014-02-04 11:20:14 +11001457 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001458 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001459 }
1460
1461 /* Save the file using the new passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001462 if ((r = sshkey_save_private(private, identity_file, passphrase1,
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00001463 comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001464 error("Saving key \"%s\" failed: %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001465 identity_file, ssh_err(r));
Damien Millera5103f42014-02-04 11:20:14 +11001466 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001467 free(passphrase1);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001468 sshkey_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001469 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001470 exit(1);
1471 }
1472 /* Destroy the passphrase and the copy of the key in memory. */
Damien Millera5103f42014-02-04 11:20:14 +11001473 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001474 free(passphrase1);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001475 sshkey_free(private); /* Destroys contents */
Darren Tuckera627d422013-06-02 07:31:17 +10001476 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001477
1478 printf("Your identification has been saved with the new passphrase.\n");
1479 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480}
1481
Damien Miller37876e92003-05-15 10:19:46 +10001482/*
1483 * Print the SSHFP RR.
1484 */
Damien Millercb314822006-03-26 13:48:01 +11001485static int
djm@openbsd.org851f8032019-01-23 04:16:22 +00001486do_print_resource_record(struct passwd *pw, char *fname, char *hname,
1487 int print_generic)
Damien Miller37876e92003-05-15 10:19:46 +10001488{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001489 struct sshkey *public;
Damien Miller37876e92003-05-15 10:19:46 +10001490 char *comment = NULL;
1491 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001492 int r;
Damien Miller37876e92003-05-15 10:19:46 +10001493
Damien Millercb314822006-03-26 13:48:01 +11001494 if (fname == NULL)
Damien Miller5bb88332013-07-18 16:13:37 +10001495 fatal("%s: no filename", __func__);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001496 if (stat(fname, &st) == -1) {
Damien Millercb314822006-03-26 13:48:01 +11001497 if (errno == ENOENT)
1498 return 0;
djm@openbsd.org3038a192015-04-17 13:19:22 +00001499 fatal("%s: %s", fname, strerror(errno));
Damien Miller37876e92003-05-15 10:19:46 +10001500 }
djm@openbsd.org3038a192015-04-17 13:19:22 +00001501 if ((r = sshkey_load_public(fname, &public, &comment)) != 0)
1502 fatal("Failed to read v2 public key from \"%s\": %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001503 fname, ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001504 export_dns_rr(hname, public, stdout, print_generic);
1505 sshkey_free(public);
1506 free(comment);
1507 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001508}
Damien Miller37876e92003-05-15 10:19:46 +10001509
Damien Miller95def091999-11-25 00:26:21 +11001510/*
1511 * Change the comment of a private key file.
1512 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001513static void
djm@openbsd.org851f8032019-01-23 04:16:22 +00001514do_change_comment(struct passwd *pw, const char *identity_comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001516 char new_comment[1024], *comment, *passphrase;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001517 struct sshkey *private;
1518 struct sshkey *public;
Damien Miller95def091999-11-25 00:26:21 +11001519 struct stat st;
1520 FILE *f;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001521 int r, fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001522
Damien Miller95def091999-11-25 00:26:21 +11001523 if (!have_identity)
1524 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001525 if (stat(identity_file, &st) == -1)
djm@openbsd.org3038a192015-04-17 13:19:22 +00001526 fatal("%s: %s", identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001527 if ((r = sshkey_load_private(identity_file, "",
1528 &private, &comment)) == 0)
1529 passphrase = xstrdup("");
djm@openbsd.org3038a192015-04-17 13:19:22 +00001530 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
1531 fatal("Cannot load private key \"%s\": %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001532 identity_file, ssh_err(r));
djm@openbsd.org3038a192015-04-17 13:19:22 +00001533 else {
Damien Miller95def091999-11-25 00:26:21 +11001534 if (identity_passphrase)
1535 passphrase = xstrdup(identity_passphrase);
1536 else if (identity_new_passphrase)
1537 passphrase = xstrdup(identity_new_passphrase);
1538 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001539 passphrase = read_passphrase("Enter passphrase: ",
1540 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001541 /* Try to load using the passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001542 if ((r = sshkey_load_private(identity_file, passphrase,
1543 &private, &comment)) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001544 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001545 free(passphrase);
djm@openbsd.org3038a192015-04-17 13:19:22 +00001546 fatal("Cannot load private key \"%s\": %s.",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001547 identity_file, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001548 }
1549 }
halex@openbsd.org4d906252015-11-20 23:04:01 +00001550
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001551 if (private->type != KEY_ED25519 && private->type != KEY_XMSS &&
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00001552 private_key_format != SSHKEY_PRIVATE_OPENSSH) {
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001553 error("Comments are only supported for keys stored in "
halex@openbsd.org4d906252015-11-20 23:04:01 +00001554 "the new format (-o).");
tobias@openbsd.org704d8c82015-03-31 11:06:49 +00001555 explicit_bzero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001556 sshkey_free(private);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001557 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001558 }
millert@openbsd.orge40269b2017-02-08 20:32:43 +00001559 if (comment)
lum@openbsd.org410b2312019-05-29 08:30:26 +00001560 printf("Old comment: %s\n", comment);
millert@openbsd.orge40269b2017-02-08 20:32:43 +00001561 else
lum@openbsd.org410b2312019-05-29 08:30:26 +00001562 printf("No existing comment\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001563
Damien Miller95def091999-11-25 00:26:21 +11001564 if (identity_comment) {
1565 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1566 } else {
lum@openbsd.org410b2312019-05-29 08:30:26 +00001567 printf("New comment: ");
Damien Miller95def091999-11-25 00:26:21 +11001568 fflush(stdout);
1569 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
Damien Millera5103f42014-02-04 11:20:14 +11001570 explicit_bzero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001571 sshkey_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001572 exit(1);
1573 }
Damien Miller14b017d2007-09-17 16:09:15 +10001574 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001575 }
lum@openbsd.org410b2312019-05-29 08:30:26 +00001576 if (comment != NULL && strcmp(comment, new_comment) == 0) {
1577 printf("No change to comment\n");
1578 free(passphrase);
1579 sshkey_free(private);
1580 free(comment);
1581 exit(0);
1582 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583
Damien Miller95def091999-11-25 00:26:21 +11001584 /* Save the file using the new passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001585 if ((r = sshkey_save_private(private, identity_file, passphrase,
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00001586 new_comment, private_key_format, openssh_format_cipher,
1587 rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00001588 error("Saving key \"%s\" failed: %s",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001589 identity_file, ssh_err(r));
Damien Millera5103f42014-02-04 11:20:14 +11001590 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001591 free(passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001592 sshkey_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001593 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001594 exit(1);
1595 }
Damien Millera5103f42014-02-04 11:20:14 +11001596 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001597 free(passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001598 if ((r = sshkey_from_private(private, &public)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001599 fatal("sshkey_from_private failed: %s", ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001600 sshkey_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001601
Damien Miller95def091999-11-25 00:26:21 +11001602 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001603 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
djm@openbsd.org3038a192015-04-17 13:19:22 +00001604 if (fd == -1)
1605 fatal("Could not save your public key in %s", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001606 f = fdopen(fd, "w");
djm@openbsd.org3038a192015-04-17 13:19:22 +00001607 if (f == NULL)
1608 fatal("fdopen %s failed: %s", identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001609 if ((r = sshkey_write(public, f)) != 0)
djm@openbsd.org3038a192015-04-17 13:19:22 +00001610 fatal("write key failed: %s", ssh_err(r));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001611 sshkey_free(public);
Damien Millereba71ba2000-04-29 23:57:08 +10001612 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001613 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001614
Darren Tuckera627d422013-06-02 07:31:17 +10001615 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001616
lum@openbsd.org410b2312019-05-29 08:30:26 +00001617 if (strlen(new_comment) > 0)
1618 printf("Comment '%s' applied\n", new_comment);
1619 else
1620 printf("Comment removed\n");
1621
Damien Miller95def091999-11-25 00:26:21 +11001622 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001623}
1624
Damien Miller0a80ca12010-02-27 07:55:05 +11001625static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001626add_flag_option(struct sshbuf *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001627{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001628 int r;
1629
Damien Miller0a80ca12010-02-27 07:55:05 +11001630 debug3("%s: %s", __func__, name);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001631 if ((r = sshbuf_put_cstring(c, name)) != 0 ||
1632 (r = sshbuf_put_string(c, NULL, 0)) != 0)
1633 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001634}
1635
1636static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001637add_string_option(struct sshbuf *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001638{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001639 struct sshbuf *b;
1640 int r;
Damien Miller0a80ca12010-02-27 07:55:05 +11001641
1642 debug3("%s: %s=%s", __func__, name, value);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001643 if ((b = sshbuf_new()) == NULL)
1644 fatal("%s: sshbuf_new failed", __func__);
1645 if ((r = sshbuf_put_cstring(b, value)) != 0 ||
1646 (r = sshbuf_put_cstring(c, name)) != 0 ||
1647 (r = sshbuf_put_stringb(c, b)) != 0)
1648 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001649
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001650 sshbuf_free(b);
Damien Miller0a80ca12010-02-27 07:55:05 +11001651}
1652
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001653#define OPTIONS_CRITICAL 1
1654#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001655static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001656prepare_options_buf(struct sshbuf *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001657{
djm@openbsd.org249516e2017-04-29 04:12:25 +00001658 size_t i;
1659
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001660 sshbuf_reset(c);
Damien Miller1da63882010-08-05 13:03:51 +10001661 if ((which & OPTIONS_CRITICAL) != 0 &&
1662 certflags_command != NULL)
1663 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001664 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001665 (certflags_flags & CERTOPT_X_FWD) != 0)
1666 add_flag_option(c, "permit-X11-forwarding");
1667 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001668 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001669 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001670 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1671 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001672 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001673 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1674 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001675 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001676 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1677 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001678 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001679 if ((which & OPTIONS_CRITICAL) != 0 &&
djm@openbsd.org2e712632019-11-25 00:54:23 +00001680 (certflags_flags & CERTOPT_NO_REQUIRE_USER_PRESENCE) != 0)
1681 add_flag_option(c, "no-touch-required");
1682 if ((which & OPTIONS_CRITICAL) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001683 certflags_src_addr != NULL)
1684 add_string_option(c, "source-address", certflags_src_addr);
djm@openbsd.org249516e2017-04-29 04:12:25 +00001685 for (i = 0; i < ncert_userext; i++) {
1686 if ((cert_userext[i].crit && (which & OPTIONS_EXTENSIONS)) ||
1687 (!cert_userext[i].crit && (which & OPTIONS_CRITICAL)))
1688 continue;
1689 if (cert_userext[i].val == NULL)
1690 add_flag_option(c, cert_userext[i].key);
1691 else {
1692 add_string_option(c, cert_userext[i].key,
1693 cert_userext[i].val);
1694 }
1695 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001696}
1697
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001698static struct sshkey *
Damien Miller757f34e2010-08-05 13:05:31 +10001699load_pkcs11_key(char *path)
1700{
1701#ifdef ENABLE_PKCS11
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001702 struct sshkey **keys = NULL, *public, *private = NULL;
1703 int r, i, nkeys;
Damien Miller757f34e2010-08-05 13:05:31 +10001704
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001705 if ((r = sshkey_load_public(path, &public, NULL)) != 0)
1706 fatal("Couldn't load CA public key \"%s\": %s",
1707 path, ssh_err(r));
Damien Miller757f34e2010-08-05 13:05:31 +10001708
djm@openbsd.org89a8d452020-01-25 00:03:36 +00001709 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase,
1710 &keys, NULL);
Damien Miller757f34e2010-08-05 13:05:31 +10001711 debug3("%s: %d keys", __func__, nkeys);
1712 if (nkeys <= 0)
1713 fatal("cannot read public key from pkcs11");
1714 for (i = 0; i < nkeys; i++) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001715 if (sshkey_equal_public(public, keys[i])) {
Damien Miller757f34e2010-08-05 13:05:31 +10001716 private = keys[i];
1717 continue;
1718 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001719 sshkey_free(keys[i]);
Damien Miller757f34e2010-08-05 13:05:31 +10001720 }
Darren Tuckera627d422013-06-02 07:31:17 +10001721 free(keys);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001722 sshkey_free(public);
Damien Miller757f34e2010-08-05 13:05:31 +10001723 return private;
1724#else
1725 fatal("no pkcs11 support");
1726#endif /* ENABLE_PKCS11 */
1727}
1728
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001729/* Signer for sshkey_certify_custom that uses the agent */
1730static int
djm@openbsd.org4f7a56d2019-06-21 04:21:04 +00001731agent_signer(struct sshkey *key, u_char **sigp, size_t *lenp,
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001732 const u_char *data, size_t datalen,
djm@openbsd.orgb923a902019-10-31 21:28:27 +00001733 const char *alg, const char *provider, u_int compat, void *ctx)
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001734{
1735 int *agent_fdp = (int *)ctx;
1736
1737 return ssh_agent_sign(*agent_fdp, key, sigp, lenp,
1738 data, datalen, alg, compat);
1739}
1740
Damien Miller0a80ca12010-02-27 07:55:05 +11001741static void
djm@openbsd.org851f8032019-01-23 04:16:22 +00001742do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
djm@openbsd.orgbe063942019-01-23 04:51:02 +00001743 unsigned long long cert_serial, int cert_serial_autoinc,
1744 int argc, char **argv)
Damien Miller0a80ca12010-02-27 07:55:05 +11001745{
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001746 int r, i, fd, found, agent_fd = -1;
Damien Miller0a80ca12010-02-27 07:55:05 +11001747 u_int n;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001748 struct sshkey *ca, *public;
djm@openbsd.org516605f2019-12-10 22:37:20 +00001749 char valid[64], *otmp, *tmp, *cp, *out, *comment;
1750 char *ca_fp = NULL, **plist = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +11001751 FILE *f;
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001752 struct ssh_identitylist *agent_ids;
1753 size_t j;
djm@openbsd.org516605f2019-12-10 22:37:20 +00001754 struct notifier_ctx *notifier = NULL;
Damien Miller4e270b02010-04-16 15:56:21 +10001755
Damien Miller1f0311c2014-05-15 14:24:09 +10001756#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001757 pkcs11_init(1);
Damien Miller1f0311c2014-05-15 14:24:09 +10001758#endif
Damien Miller757f34e2010-08-05 13:05:31 +10001759 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1760 if (pkcs11provider != NULL) {
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001761 /* If a PKCS#11 token was specified then try to use it */
Damien Miller757f34e2010-08-05 13:05:31 +10001762 if ((ca = load_pkcs11_key(tmp)) == NULL)
1763 fatal("No PKCS#11 key matching %s found", ca_key_path);
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001764 } else if (prefer_agent) {
1765 /*
1766 * Agent signature requested. Try to use agent after making
1767 * sure the public key specified is actually present in the
1768 * agent.
1769 */
1770 if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
1771 fatal("Cannot load CA public key %s: %s",
1772 tmp, ssh_err(r));
1773 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0)
1774 fatal("Cannot use public key for CA signature: %s",
1775 ssh_err(r));
1776 if ((r = ssh_fetch_identitylist(agent_fd, &agent_ids)) != 0)
1777 fatal("Retrieve agent key list: %s", ssh_err(r));
1778 found = 0;
1779 for (j = 0; j < agent_ids->nkeys; j++) {
1780 if (sshkey_equal(ca, agent_ids->keys[j])) {
1781 found = 1;
1782 break;
1783 }
1784 }
1785 if (!found)
1786 fatal("CA key %s not found in agent", tmp);
1787 ssh_free_identitylist(agent_ids);
1788 ca->flags |= SSHKEY_FLAG_EXT;
1789 } else {
1790 /* CA key is assumed to be a private key on the filesystem */
djm@openbsd.org07133222019-09-01 23:47:32 +00001791 ca = load_identity(tmp, NULL);
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001792 }
Darren Tuckera627d422013-06-02 07:31:17 +10001793 free(tmp);
Damien Miller757f34e2010-08-05 13:05:31 +10001794
djm@openbsd.org4a41d242020-01-24 00:27:04 +00001795 if (key_type_name != NULL) {
1796 if (sshkey_type_from_name(key_type_name) != ca->type) {
1797 fatal("CA key type %s doesn't match specified %s",
1798 sshkey_ssh_name(ca), key_type_name);
1799 }
1800 } else if (ca->type == KEY_RSA) {
1801 /* Default to a good signature algorithm */
1802 key_type_name = "rsa-sha2-512";
djm@openbsd.org57464e32016-05-02 09:36:42 +00001803 }
djm@openbsd.org516605f2019-12-10 22:37:20 +00001804 ca_fp = sshkey_fingerprint(ca, fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org57464e32016-05-02 09:36:42 +00001805
Damien Miller0a80ca12010-02-27 07:55:05 +11001806 for (i = 0; i < argc; i++) {
1807 /* Split list of principals */
1808 n = 0;
1809 if (cert_principals != NULL) {
1810 otmp = tmp = xstrdup(cert_principals);
1811 plist = NULL;
1812 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001813 plist = xreallocarray(plist, n + 1, sizeof(*plist));
Damien Miller0a80ca12010-02-27 07:55:05 +11001814 if (*(plist[n] = xstrdup(cp)) == '\0')
1815 fatal("Empty principal name");
1816 }
Darren Tuckera627d422013-06-02 07:31:17 +10001817 free(otmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001818 }
djm@openbsd.orgca613242018-02-10 05:48:46 +00001819 if (n > SSHKEY_CERT_MAX_PRINCIPALS)
1820 fatal("Too many certificate principals specified");
djm@openbsd.org16dd8b22019-07-16 13:18:39 +00001821
Damien Miller0a80ca12010-02-27 07:55:05 +11001822 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001823 if ((r = sshkey_load_public(tmp, &public, &comment)) != 0)
1824 fatal("%s: unable to open \"%s\": %s",
1825 __func__, tmp, ssh_err(r));
djm@openbsd.org85409cb2019-11-18 06:24:17 +00001826 if (sshkey_is_cert(public))
Damien Miller0a80ca12010-02-27 07:55:05 +11001827 fatal("%s: key \"%s\" type %s cannot be certified",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001828 __func__, tmp, sshkey_type(public));
Damien Miller0a80ca12010-02-27 07:55:05 +11001829
1830 /* Prepare certificate to sign */
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001831 if ((r = sshkey_to_certified(public)) != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001832 fatal("Could not upgrade key %s to certificate: %s",
1833 tmp, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001834 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001835 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001836 public->cert->key_id = xstrdup(cert_key_id);
1837 public->cert->nprincipals = n;
1838 public->cert->principals = plist;
1839 public->cert->valid_after = cert_valid_from;
1840 public->cert->valid_before = cert_valid_to;
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001841 prepare_options_buf(public->cert->critical, OPTIONS_CRITICAL);
1842 prepare_options_buf(public->cert->extensions,
1843 OPTIONS_EXTENSIONS);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001844 if ((r = sshkey_from_private(ca,
1845 &public->cert->signature_key)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001846 fatal("sshkey_from_private (ca key): %s", ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001847
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001848 if (agent_fd != -1 && (ca->flags & SSHKEY_FLAG_EXT) != 0) {
1849 if ((r = sshkey_certify_custom(public, ca,
djm@openbsd.org9a14c642019-10-31 21:23:19 +00001850 key_type_name, sk_provider, agent_signer,
1851 &agent_fd)) != 0)
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001852 fatal("Couldn't certify key %s via agent: %s",
1853 tmp, ssh_err(r));
1854 } else {
djm@openbsd.org516605f2019-12-10 22:37:20 +00001855 if (sshkey_is_sk(ca) &&
1856 (ca->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
1857 notifier = notify_start(0,
1858 "Confirm user presence for key %s %s",
1859 sshkey_type(ca), ca_fp);
1860 }
1861 r = sshkey_certify(public, ca, key_type_name,
1862 sk_provider);
1863 notify_complete(notifier);
1864 if (r != 0)
djm@openbsd.orga98339e2017-06-28 01:09:22 +00001865 fatal("Couldn't certify key %s: %s",
1866 tmp, ssh_err(r));
1867 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001868
1869 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1870 *cp = '\0';
1871 xasprintf(&out, "%s-cert.pub", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001872 free(tmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001873
1874 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1875 fatal("Could not open \"%s\" for writing: %s", out,
1876 strerror(errno));
1877 if ((f = fdopen(fd, "w")) == NULL)
1878 fatal("%s: fdopen: %s", __func__, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001879 if ((r = sshkey_write(public, f)) != 0)
1880 fatal("Could not write certified key to %s: %s",
1881 out, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001882 fprintf(f, " %s\n", comment);
1883 fclose(f);
1884
Damien Miller4e270b02010-04-16 15:56:21 +10001885 if (!quiet) {
djm@openbsd.org964ab3e2015-11-19 01:12:32 +00001886 sshkey_format_cert_validity(public->cert,
djm@openbsd.org499cf362015-11-19 01:08:55 +00001887 valid, sizeof(valid));
Damien Miller4e270b02010-04-16 15:56:21 +10001888 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
djm@openbsd.org964ab3e2015-11-19 01:12:32 +00001889 "valid %s", sshkey_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001890 out, public->cert->key_id,
1891 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001892 cert_principals != NULL ? " for " : "",
1893 cert_principals != NULL ? cert_principals : "",
djm@openbsd.org499cf362015-11-19 01:08:55 +00001894 valid);
Damien Miller4e270b02010-04-16 15:56:21 +10001895 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001896
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001897 sshkey_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001898 free(out);
djm@openbsd.orgbe063942019-01-23 04:51:02 +00001899 if (cert_serial_autoinc)
1900 cert_serial++;
Damien Miller0a80ca12010-02-27 07:55:05 +11001901 }
djm@openbsd.org516605f2019-12-10 22:37:20 +00001902 free(ca_fp);
Damien Miller1f0311c2014-05-15 14:24:09 +10001903#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001904 pkcs11_terminate();
Damien Miller1f0311c2014-05-15 14:24:09 +10001905#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11001906 exit(0);
1907}
1908
1909static u_int64_t
1910parse_relative_time(const char *s, time_t now)
1911{
1912 int64_t mul, secs;
1913
1914 mul = *s == '-' ? -1 : 1;
1915
1916 if ((secs = convtime(s + 1)) == -1)
1917 fatal("Invalid relative certificate time %s", s);
1918 if (mul == -1 && secs > now)
1919 fatal("Certificate time %s cannot be represented", s);
1920 return now + (u_int64_t)(secs * mul);
1921}
1922
Damien Miller0a80ca12010-02-27 07:55:05 +11001923static void
1924parse_cert_times(char *timespec)
1925{
1926 char *from, *to;
1927 time_t now = time(NULL);
1928 int64_t secs;
1929
1930 /* +timespec relative to now */
1931 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1932 if ((secs = convtime(timespec + 1)) == -1)
1933 fatal("Invalid relative certificate life %s", timespec);
1934 cert_valid_to = now + secs;
1935 /*
1936 * Backdate certificate one minute to avoid problems on hosts
1937 * with poorly-synchronised clocks.
1938 */
1939 cert_valid_from = ((now - 59)/ 60) * 60;
1940 return;
1941 }
1942
1943 /*
1944 * from:to, where
djm@openbsd.org@openbsd.orgd52131a2017-11-03 05:14:04 +00001945 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS | "always"
1946 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS | "forever"
Damien Miller0a80ca12010-02-27 07:55:05 +11001947 */
1948 from = xstrdup(timespec);
1949 to = strchr(from, ':');
1950 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001951 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001952 *to++ = '\0';
1953
1954 if (*from == '-' || *from == '+')
1955 cert_valid_from = parse_relative_time(from, now);
djm@openbsd.org@openbsd.orgd52131a2017-11-03 05:14:04 +00001956 else if (strcmp(from, "always") == 0)
1957 cert_valid_from = 0;
djm@openbsd.orgbf0fbf22018-03-12 00:52:01 +00001958 else if (parse_absolute_time(from, &cert_valid_from) != 0)
1959 fatal("Invalid from time \"%s\"", from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001960
1961 if (*to == '-' || *to == '+')
Damien Miller5b01b0d2013-10-23 16:31:31 +11001962 cert_valid_to = parse_relative_time(to, now);
djm@openbsd.org@openbsd.orgd52131a2017-11-03 05:14:04 +00001963 else if (strcmp(to, "forever") == 0)
1964 cert_valid_to = ~(u_int64_t)0;
djm@openbsd.orgbf0fbf22018-03-12 00:52:01 +00001965 else if (parse_absolute_time(to, &cert_valid_to) != 0)
1966 fatal("Invalid to time \"%s\"", to);
Damien Miller0a80ca12010-02-27 07:55:05 +11001967
1968 if (cert_valid_to <= cert_valid_from)
1969 fatal("Empty certificate validity interval");
Darren Tuckera627d422013-06-02 07:31:17 +10001970 free(from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001971}
1972
1973static void
Damien Miller4e270b02010-04-16 15:56:21 +10001974add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001975{
djm@openbsd.org249516e2017-04-29 04:12:25 +00001976 char *val, *cp;
1977 int iscrit = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001978
Damien Miller044f4a62011-05-05 14:14:08 +10001979 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001980 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001981 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001982 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001983 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001984 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001985 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001986 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001987 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001988 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001989 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001990 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001991 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001992 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001993 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001994 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001995 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001996 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001997 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001998 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001999 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002000 certflags_flags |= CERTOPT_USER_RC;
djm@openbsd.org2e712632019-11-25 00:54:23 +00002001 else if (strcasecmp(opt, "touch-required") == 0)
2002 certflags_flags &= ~CERTOPT_NO_REQUIRE_USER_PRESENCE;
2003 else if (strcasecmp(opt, "no-touch-required") == 0)
2004 certflags_flags |= CERTOPT_NO_REQUIRE_USER_PRESENCE;
Damien Miller0a80ca12010-02-27 07:55:05 +11002005 else if (strncasecmp(opt, "force-command=", 14) == 0) {
2006 val = opt + 14;
2007 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10002008 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002009 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11002010 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002011 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11002012 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
2013 val = opt + 15;
2014 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10002015 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002016 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11002017 fatal("source-address already specified");
2018 if (addr_match_cidr_list(NULL, val) != 0)
2019 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002020 certflags_src_addr = xstrdup(val);
djm@openbsd.org249516e2017-04-29 04:12:25 +00002021 } else if (strncasecmp(opt, "extension:", 10) == 0 ||
2022 (iscrit = (strncasecmp(opt, "critical:", 9) == 0))) {
2023 val = xstrdup(strchr(opt, ':') + 1);
2024 if ((cp = strchr(val, '=')) != NULL)
2025 *cp++ = '\0';
2026 cert_userext = xreallocarray(cert_userext, ncert_userext + 1,
2027 sizeof(*cert_userext));
2028 cert_userext[ncert_userext].key = val;
2029 cert_userext[ncert_userext].val = cp == NULL ?
2030 NULL : xstrdup(cp);
2031 cert_userext[ncert_userext].crit = iscrit;
2032 ncert_userext++;
Damien Miller0a80ca12010-02-27 07:55:05 +11002033 } else
Damien Miller4e270b02010-04-16 15:56:21 +10002034 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11002035}
2036
Ben Lindstrombba81212001-06-25 05:01:22 +00002037static void
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002038show_options(struct sshbuf *optbuf, int in_critical)
Damien Millerd834d352010-06-26 09:48:02 +10002039{
Damien Miller633de332014-05-15 13:48:26 +10002040 char *name, *arg;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002041 struct sshbuf *options, *option = NULL;
2042 int r;
Damien Millerd834d352010-06-26 09:48:02 +10002043
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002044 if ((options = sshbuf_fromb(optbuf)) == NULL)
2045 fatal("%s: sshbuf_fromb failed", __func__);
2046 while (sshbuf_len(options) != 0) {
2047 sshbuf_free(option);
2048 option = NULL;
2049 if ((r = sshbuf_get_cstring(options, &name, NULL)) != 0 ||
2050 (r = sshbuf_froms(options, &option)) != 0)
2051 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd834d352010-06-26 09:48:02 +10002052 printf(" %s", name);
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002053 if (!in_critical &&
Damien Millerd834d352010-06-26 09:48:02 +10002054 (strcmp(name, "permit-X11-forwarding") == 0 ||
2055 strcmp(name, "permit-agent-forwarding") == 0 ||
2056 strcmp(name, "permit-port-forwarding") == 0 ||
2057 strcmp(name, "permit-pty") == 0 ||
djm@openbsd.org2e712632019-11-25 00:54:23 +00002058 strcmp(name, "permit-user-rc") == 0 ||
2059 strcmp(name, "no-touch-required") == 0)) {
Damien Millerd834d352010-06-26 09:48:02 +10002060 printf("\n");
djm@openbsd.org2e712632019-11-25 00:54:23 +00002061 } else if (in_critical &&
Damien Millerd834d352010-06-26 09:48:02 +10002062 (strcmp(name, "force-command") == 0 ||
2063 strcmp(name, "source-address") == 0)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002064 if ((r = sshbuf_get_cstring(option, &arg, NULL)) != 0)
2065 fatal("%s: buffer error: %s",
2066 __func__, ssh_err(r));
Damien Miller633de332014-05-15 13:48:26 +10002067 printf(" %s\n", arg);
2068 free(arg);
Damien Millerd834d352010-06-26 09:48:02 +10002069 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002070 printf(" UNKNOWN OPTION (len %zu)\n",
2071 sshbuf_len(option));
2072 sshbuf_reset(option);
Damien Millerd834d352010-06-26 09:48:02 +10002073 }
Darren Tuckera627d422013-06-02 07:31:17 +10002074 free(name);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002075 if (sshbuf_len(option) != 0)
Damien Millerd834d352010-06-26 09:48:02 +10002076 fatal("Option corrupt: extra data at end");
2077 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002078 sshbuf_free(option);
2079 sshbuf_free(options);
Damien Millerd834d352010-06-26 09:48:02 +10002080}
2081
2082static void
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002083print_cert(struct sshkey *key)
Damien Millerf2b70ca2010-03-05 07:39:35 +11002084{
djm@openbsd.org499cf362015-11-19 01:08:55 +00002085 char valid[64], *key_fp, *ca_fp;
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002086 u_int i;
Damien Miller4e270b02010-04-16 15:56:21 +10002087
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002088 key_fp = sshkey_fingerprint(key, fingerprint_hash, SSH_FP_DEFAULT);
2089 ca_fp = sshkey_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002090 fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00002091 if (key_fp == NULL || ca_fp == NULL)
2092 fatal("%s: sshkey_fingerprint fail", __func__);
djm@openbsd.org499cf362015-11-19 01:08:55 +00002093 sshkey_format_cert_validity(key->cert, valid, sizeof(valid));
Damien Millerf2b70ca2010-03-05 07:39:35 +11002094
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002095 printf(" Type: %s %s certificate\n", sshkey_ssh_name(key),
2096 sshkey_cert_type(key));
2097 printf(" Public key: %s %s\n", sshkey_type(key), key_fp);
djm@openbsd.orga4fc2532018-10-19 03:12:42 +00002098 printf(" Signing CA: %s %s (using %s)\n",
2099 sshkey_type(key->cert->signature_key), ca_fp,
2100 key->cert->signature_type);
Damien Miller4e270b02010-04-16 15:56:21 +10002101 printf(" Key ID: \"%s\"\n", key->cert->key_id);
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002102 printf(" Serial: %llu\n", (unsigned long long)key->cert->serial);
djm@openbsd.org499cf362015-11-19 01:08:55 +00002103 printf(" Valid: %s\n", valid);
Damien Millerf2b70ca2010-03-05 07:39:35 +11002104 printf(" Principals: ");
2105 if (key->cert->nprincipals == 0)
2106 printf("(none)\n");
2107 else {
2108 for (i = 0; i < key->cert->nprincipals; i++)
2109 printf("\n %s",
2110 key->cert->principals[i]);
2111 printf("\n");
2112 }
Damien Miller4e270b02010-04-16 15:56:21 +10002113 printf(" Critical Options: ");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002114 if (sshbuf_len(key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11002115 printf("(none)\n");
2116 else {
2117 printf("\n");
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002118 show_options(key->cert->critical, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11002119 }
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002120 printf(" Extensions: ");
2121 if (sshbuf_len(key->cert->extensions) == 0)
2122 printf("(none)\n");
2123 else {
2124 printf("\n");
2125 show_options(key->cert->extensions, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10002126 }
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002127}
2128
2129static void
2130do_show_cert(struct passwd *pw)
2131{
2132 struct sshkey *key = NULL;
2133 struct stat st;
2134 int r, is_stdin = 0, ok = 0;
2135 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +00002136 char *cp, *line = NULL;
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002137 const char *path;
markus@openbsd.org7f906352018-06-06 18:29:18 +00002138 size_t linesize = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002139 u_long lnum = 0;
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002140
2141 if (!have_identity)
2142 ask_filename(pw, "Enter file in which the key is");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002143 if (strcmp(identity_file, "-") != 0 && stat(identity_file, &st) == -1)
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002144 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
2145
2146 path = identity_file;
2147 if (strcmp(path, "-") == 0) {
2148 f = stdin;
2149 path = "(stdin)";
2150 is_stdin = 1;
2151 } else if ((f = fopen(identity_file, "r")) == NULL)
2152 fatal("fopen %s: %s", identity_file, strerror(errno));
2153
markus@openbsd.org7f906352018-06-06 18:29:18 +00002154 while (getline(&line, &linesize, f) != -1) {
2155 lnum++;
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002156 sshkey_free(key);
2157 key = NULL;
2158 /* Trim leading space and comments */
2159 cp = line + strspn(line, " \t");
2160 if (*cp == '#' || *cp == '\0')
2161 continue;
2162 if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00002163 fatal("sshkey_new");
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002164 if ((r = sshkey_read(key, &cp)) != 0) {
2165 error("%s:%lu: invalid key: %s", path,
2166 lnum, ssh_err(r));
2167 continue;
2168 }
2169 if (!sshkey_is_cert(key)) {
2170 error("%s:%lu is not a certificate", path, lnum);
2171 continue;
2172 }
2173 ok = 1;
2174 if (!is_stdin && lnum == 1)
2175 printf("%s:\n", path);
2176 else
2177 printf("%s:%lu:\n", path, lnum);
2178 print_cert(key);
2179 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00002180 free(line);
djm@openbsd.org94bc0b72015-11-13 04:34:15 +00002181 sshkey_free(key);
2182 fclose(f);
2183 exit(ok ? 0 : 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11002184}
2185
2186static void
Damien Millerf3747bf2013-01-18 11:44:04 +11002187load_krl(const char *path, struct ssh_krl **krlp)
2188{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002189 struct sshbuf *krlbuf;
2190 int r, fd;
Damien Millerf3747bf2013-01-18 11:44:04 +11002191
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002192 if ((krlbuf = sshbuf_new()) == NULL)
2193 fatal("sshbuf_new failed");
Damien Millerf3747bf2013-01-18 11:44:04 +11002194 if ((fd = open(path, O_RDONLY)) == -1)
2195 fatal("open %s: %s", path, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002196 if ((r = sshkey_load_file(fd, krlbuf)) != 0)
2197 fatal("Unable to load KRL: %s", ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002198 close(fd);
2199 /* XXX check sigs */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002200 if ((r = ssh_krl_from_blob(krlbuf, krlp, NULL, 0)) != 0 ||
Damien Millerf3747bf2013-01-18 11:44:04 +11002201 *krlp == NULL)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002202 fatal("Invalid KRL file: %s", ssh_err(r));
2203 sshbuf_free(krlbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11002204}
2205
2206static void
djm@openbsd.org9405c622018-09-12 01:21:34 +00002207hash_to_blob(const char *cp, u_char **blobp, size_t *lenp,
2208 const char *file, u_long lnum)
2209{
2210 char *tmp;
2211 size_t tlen;
2212 struct sshbuf *b;
2213 int r;
2214
2215 if (strncmp(cp, "SHA256:", 7) != 0)
2216 fatal("%s:%lu: unsupported hash algorithm", file, lnum);
2217 cp += 7;
2218
2219 /*
2220 * OpenSSH base64 hashes omit trailing '='
2221 * characters; put them back for decode.
2222 */
2223 tlen = strlen(cp);
2224 tmp = xmalloc(tlen + 4 + 1);
2225 strlcpy(tmp, cp, tlen + 1);
2226 while ((tlen % 4) != 0) {
2227 tmp[tlen++] = '=';
2228 tmp[tlen] = '\0';
2229 }
2230 if ((b = sshbuf_new()) == NULL)
2231 fatal("%s: sshbuf_new failed", __func__);
2232 if ((r = sshbuf_b64tod(b, tmp)) != 0)
2233 fatal("%s:%lu: decode hash failed: %s", file, lnum, ssh_err(r));
2234 free(tmp);
2235 *lenp = sshbuf_len(b);
2236 *blobp = xmalloc(*lenp);
2237 memcpy(*blobp, sshbuf_ptr(b), *lenp);
2238 sshbuf_free(b);
2239}
2240
2241static void
djm@openbsd.org669aee92015-01-30 01:10:33 +00002242update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002243 const struct sshkey *ca, struct ssh_krl *krl)
Damien Millerf3747bf2013-01-18 11:44:04 +11002244{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002245 struct sshkey *key = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11002246 u_long lnum = 0;
markus@openbsd.org7f906352018-06-06 18:29:18 +00002247 char *path, *cp, *ep, *line = NULL;
djm@openbsd.org9405c622018-09-12 01:21:34 +00002248 u_char *blob = NULL;
2249 size_t blen = 0, linesize = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002250 unsigned long long serial, serial2;
djm@openbsd.org9405c622018-09-12 01:21:34 +00002251 int i, was_explicit_key, was_sha1, was_sha256, was_hash, r;
Damien Millerf3747bf2013-01-18 11:44:04 +11002252 FILE *krl_spec;
2253
2254 path = tilde_expand_filename(file, pw->pw_uid);
2255 if (strcmp(path, "-") == 0) {
2256 krl_spec = stdin;
2257 free(path);
2258 path = xstrdup("(standard input)");
2259 } else if ((krl_spec = fopen(path, "r")) == NULL)
2260 fatal("fopen %s: %s", path, strerror(errno));
2261
2262 if (!quiet)
2263 printf("Revoking from %s\n", path);
markus@openbsd.org7f906352018-06-06 18:29:18 +00002264 while (getline(&line, &linesize, krl_spec) != -1) {
2265 lnum++;
djm@openbsd.org9405c622018-09-12 01:21:34 +00002266 was_explicit_key = was_sha1 = was_sha256 = was_hash = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002267 cp = line + strspn(line, " \t");
2268 /* Trim trailing space, comments and strip \n */
2269 for (i = 0, r = -1; cp[i] != '\0'; i++) {
2270 if (cp[i] == '#' || cp[i] == '\n') {
2271 cp[i] = '\0';
2272 break;
2273 }
2274 if (cp[i] == ' ' || cp[i] == '\t') {
2275 /* Remember the start of a span of whitespace */
2276 if (r == -1)
2277 r = i;
2278 } else
2279 r = -1;
2280 }
2281 if (r != -1)
2282 cp[r] = '\0';
2283 if (*cp == '\0')
2284 continue;
2285 if (strncasecmp(cp, "serial:", 7) == 0) {
djm@openbsd.org669aee92015-01-30 01:10:33 +00002286 if (ca == NULL && !wild_ca) {
Damien Millerd234afb2013-08-21 02:42:58 +10002287 fatal("revoking certificates by serial number "
Damien Millerf3747bf2013-01-18 11:44:04 +11002288 "requires specification of a CA key");
2289 }
2290 cp += 7;
2291 cp = cp + strspn(cp, " \t");
2292 errno = 0;
2293 serial = strtoull(cp, &ep, 0);
2294 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2295 fatal("%s:%lu: invalid serial \"%s\"",
2296 path, lnum, cp);
2297 if (errno == ERANGE && serial == ULLONG_MAX)
2298 fatal("%s:%lu: serial out of range",
2299 path, lnum);
2300 serial2 = serial;
2301 if (*ep == '-') {
2302 cp = ep + 1;
2303 errno = 0;
2304 serial2 = strtoull(cp, &ep, 0);
2305 if (*cp == '\0' || *ep != '\0')
2306 fatal("%s:%lu: invalid serial \"%s\"",
2307 path, lnum, cp);
2308 if (errno == ERANGE && serial2 == ULLONG_MAX)
2309 fatal("%s:%lu: serial out of range",
2310 path, lnum);
2311 if (serial2 <= serial)
2312 fatal("%s:%lu: invalid serial range "
2313 "%llu:%llu", path, lnum,
2314 (unsigned long long)serial,
2315 (unsigned long long)serial2);
2316 }
2317 if (ssh_krl_revoke_cert_by_serial_range(krl,
2318 ca, serial, serial2) != 0) {
2319 fatal("%s: revoke serial failed",
2320 __func__);
2321 }
2322 } else if (strncasecmp(cp, "id:", 3) == 0) {
djm@openbsd.org669aee92015-01-30 01:10:33 +00002323 if (ca == NULL && !wild_ca) {
Damien Millerd5d9d7b2013-08-21 02:43:27 +10002324 fatal("revoking certificates by key ID "
Damien Millerf3747bf2013-01-18 11:44:04 +11002325 "requires specification of a CA key");
2326 }
2327 cp += 3;
2328 cp = cp + strspn(cp, " \t");
2329 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2330 fatal("%s: revoke key ID failed", __func__);
djm@openbsd.org9405c622018-09-12 01:21:34 +00002331 } else if (strncasecmp(cp, "hash:", 5) == 0) {
2332 cp += 5;
2333 cp = cp + strspn(cp, " \t");
2334 hash_to_blob(cp, &blob, &blen, file, lnum);
2335 r = ssh_krl_revoke_key_sha256(krl, blob, blen);
Damien Millerf3747bf2013-01-18 11:44:04 +11002336 } else {
2337 if (strncasecmp(cp, "key:", 4) == 0) {
2338 cp += 4;
2339 cp = cp + strspn(cp, " \t");
2340 was_explicit_key = 1;
2341 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2342 cp += 5;
2343 cp = cp + strspn(cp, " \t");
2344 was_sha1 = 1;
djm@openbsd.org9405c622018-09-12 01:21:34 +00002345 } else if (strncasecmp(cp, "sha256:", 7) == 0) {
2346 cp += 7;
2347 cp = cp + strspn(cp, " \t");
2348 was_sha256 = 1;
Damien Millerf3747bf2013-01-18 11:44:04 +11002349 /*
2350 * Just try to process the line as a key.
2351 * Parsing will fail if it isn't.
2352 */
2353 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002354 if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00002355 fatal("sshkey_new");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002356 if ((r = sshkey_read(key, &cp)) != 0)
2357 fatal("%s:%lu: invalid key: %s",
2358 path, lnum, ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002359 if (was_explicit_key)
2360 r = ssh_krl_revoke_key_explicit(krl, key);
djm@openbsd.org9405c622018-09-12 01:21:34 +00002361 else if (was_sha1) {
2362 if (sshkey_fingerprint_raw(key,
2363 SSH_DIGEST_SHA1, &blob, &blen) != 0) {
2364 fatal("%s:%lu: fingerprint failed",
2365 file, lnum);
2366 }
2367 r = ssh_krl_revoke_key_sha1(krl, blob, blen);
2368 } else if (was_sha256) {
2369 if (sshkey_fingerprint_raw(key,
2370 SSH_DIGEST_SHA256, &blob, &blen) != 0) {
2371 fatal("%s:%lu: fingerprint failed",
2372 file, lnum);
2373 }
2374 r = ssh_krl_revoke_key_sha256(krl, blob, blen);
2375 } else
Damien Millerf3747bf2013-01-18 11:44:04 +11002376 r = ssh_krl_revoke_key(krl, key);
2377 if (r != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002378 fatal("%s: revoke key failed: %s",
2379 __func__, ssh_err(r));
djm@openbsd.org9405c622018-09-12 01:21:34 +00002380 freezero(blob, blen);
2381 blob = NULL;
2382 blen = 0;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002383 sshkey_free(key);
Damien Millerf3747bf2013-01-18 11:44:04 +11002384 }
2385 }
2386 if (strcmp(path, "-") != 0)
2387 fclose(krl_spec);
markus@openbsd.org7f906352018-06-06 18:29:18 +00002388 free(line);
Damien Miller0d6771b2013-04-23 15:23:24 +10002389 free(path);
Damien Millerf3747bf2013-01-18 11:44:04 +11002390}
2391
2392static void
djm@openbsd.org851f8032019-01-23 04:16:22 +00002393do_gen_krl(struct passwd *pw, int updating, const char *ca_key_path,
2394 unsigned long long krl_version, const char *krl_comment,
2395 int argc, char **argv)
Damien Millerf3747bf2013-01-18 11:44:04 +11002396{
2397 struct ssh_krl *krl;
2398 struct stat sb;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002399 struct sshkey *ca = NULL;
djm@openbsd.org669aee92015-01-30 01:10:33 +00002400 int fd, i, r, wild_ca = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002401 char *tmp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002402 struct sshbuf *kbuf;
Damien Millerf3747bf2013-01-18 11:44:04 +11002403
2404 if (*identity_file == '\0')
2405 fatal("KRL generation requires an output file");
2406 if (stat(identity_file, &sb) == -1) {
2407 if (errno != ENOENT)
2408 fatal("Cannot access KRL \"%s\": %s",
2409 identity_file, strerror(errno));
2410 if (updating)
2411 fatal("KRL \"%s\" does not exist", identity_file);
2412 }
2413 if (ca_key_path != NULL) {
djm@openbsd.org669aee92015-01-30 01:10:33 +00002414 if (strcasecmp(ca_key_path, "none") == 0)
2415 wild_ca = 1;
2416 else {
2417 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2418 if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
2419 fatal("Cannot load CA public key %s: %s",
2420 tmp, ssh_err(r));
2421 free(tmp);
2422 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002423 }
2424
2425 if (updating)
2426 load_krl(identity_file, &krl);
2427 else if ((krl = ssh_krl_init()) == NULL)
2428 fatal("couldn't create KRL");
2429
djm@openbsd.org851f8032019-01-23 04:16:22 +00002430 if (krl_version != 0)
2431 ssh_krl_set_version(krl, krl_version);
2432 if (krl_comment != NULL)
2433 ssh_krl_set_comment(krl, krl_comment);
Damien Millerf3747bf2013-01-18 11:44:04 +11002434
2435 for (i = 0; i < argc; i++)
djm@openbsd.org669aee92015-01-30 01:10:33 +00002436 update_krl_from_file(pw, argv[i], wild_ca, ca, krl);
Damien Millerf3747bf2013-01-18 11:44:04 +11002437
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002438 if ((kbuf = sshbuf_new()) == NULL)
2439 fatal("sshbuf_new failed");
2440 if (ssh_krl_to_blob(krl, kbuf, NULL, 0) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11002441 fatal("Couldn't generate KRL");
2442 if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2443 fatal("open %s: %s", identity_file, strerror(errno));
markus@openbsd.org49f47e62018-07-09 21:59:10 +00002444 if (atomicio(vwrite, fd, sshbuf_mutable_ptr(kbuf), sshbuf_len(kbuf)) !=
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002445 sshbuf_len(kbuf))
Damien Millerf3747bf2013-01-18 11:44:04 +11002446 fatal("write %s: %s", identity_file, strerror(errno));
2447 close(fd);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002448 sshbuf_free(kbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11002449 ssh_krl_free(krl);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00002450 sshkey_free(ca);
Damien Millerf3747bf2013-01-18 11:44:04 +11002451}
2452
2453static void
2454do_check_krl(struct passwd *pw, int argc, char **argv)
2455{
2456 int i, r, ret = 0;
2457 char *comment;
2458 struct ssh_krl *krl;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002459 struct sshkey *k;
Damien Millerf3747bf2013-01-18 11:44:04 +11002460
2461 if (*identity_file == '\0')
2462 fatal("KRL checking requires an input file");
2463 load_krl(identity_file, &krl);
2464 for (i = 0; i < argc; i++) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002465 if ((r = sshkey_load_public(argv[i], &k, &comment)) != 0)
2466 fatal("Cannot load public key %s: %s",
2467 argv[i], ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002468 r = ssh_krl_check_key(krl, k);
2469 printf("%s%s%s%s: %s\n", argv[i],
2470 *comment ? " (" : "", comment, *comment ? ")" : "",
2471 r == 0 ? "ok" : "REVOKED");
2472 if (r != 0)
2473 ret = 1;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002474 sshkey_free(k);
Damien Millerf3747bf2013-01-18 11:44:04 +11002475 free(comment);
2476 }
2477 ssh_krl_free(krl);
2478 exit(ret);
2479}
2480
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002481static struct sshkey *
2482load_sign_key(const char *keypath, const struct sshkey *pubkey)
2483{
2484 size_t i, slen, plen = strlen(keypath);
2485 char *privpath = xstrdup(keypath);
2486 const char *suffixes[] = { "-cert.pub", ".pub", NULL };
2487 struct sshkey *ret = NULL, *privkey = NULL;
2488 int r;
2489
2490 /*
2491 * If passed a public key filename, then try to locate the correponding
2492 * private key. This lets us specify certificates on the command-line
2493 * and have ssh-keygen find the appropriate private key.
2494 */
2495 for (i = 0; suffixes[i]; i++) {
2496 slen = strlen(suffixes[i]);
2497 if (plen <= slen ||
2498 strcmp(privpath + plen - slen, suffixes[i]) != 0)
2499 continue;
2500 privpath[plen - slen] = '\0';
2501 debug("%s: %s looks like a public key, using private key "
2502 "path %s instead", __func__, keypath, privpath);
2503 }
2504 if ((privkey = load_identity(privpath, NULL)) == NULL) {
2505 error("Couldn't load identity %s", keypath);
2506 goto done;
2507 }
2508 if (!sshkey_equal_public(pubkey, privkey)) {
2509 error("Public key %s doesn't match private %s",
2510 keypath, privpath);
2511 goto done;
2512 }
2513 if (sshkey_is_cert(pubkey) && !sshkey_is_cert(privkey)) {
2514 /*
2515 * Graft the certificate onto the private key to make
2516 * it capable of signing.
2517 */
2518 if ((r = sshkey_to_certified(privkey)) != 0) {
2519 error("%s: sshkey_to_certified: %s", __func__,
2520 ssh_err(r));
2521 goto done;
2522 }
2523 if ((r = sshkey_cert_copy(pubkey, privkey)) != 0) {
2524 error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r));
2525 goto done;
2526 }
2527 }
2528 /* success */
2529 ret = privkey;
2530 privkey = NULL;
2531 done:
2532 sshkey_free(privkey);
2533 free(privpath);
2534 return ret;
2535}
2536
2537static int
2538sign_one(struct sshkey *signkey, const char *filename, int fd,
2539 const char *sig_namespace, sshsig_signer *signer, void *signer_ctx)
2540{
2541 struct sshbuf *sigbuf = NULL, *abuf = NULL;
2542 int r = SSH_ERR_INTERNAL_ERROR, wfd = -1, oerrno;
djm@openbsd.orge44bb612019-11-12 22:36:44 +00002543 char *wfile = NULL, *asig = NULL, *fp = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002544
2545 if (!quiet) {
2546 if (fd == STDIN_FILENO)
2547 fprintf(stderr, "Signing data on standard input\n");
2548 else
2549 fprintf(stderr, "Signing file %s\n", filename);
2550 }
djm@openbsd.orge44bb612019-11-12 22:36:44 +00002551 if (signer == NULL && sshkey_is_sk(signkey) &&
2552 (signkey->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
2553 if ((fp = sshkey_fingerprint(signkey, fingerprint_hash,
2554 SSH_FP_DEFAULT)) == NULL)
2555 fatal("%s: sshkey_fingerprint failed", __func__);
2556 fprintf(stderr, "Confirm user presence for key %s %s\n",
2557 sshkey_type(signkey), fp);
2558 free(fp);
2559 }
djm@openbsd.org9a14c642019-10-31 21:23:19 +00002560 if ((r = sshsig_sign_fd(signkey, NULL, sk_provider, fd, sig_namespace,
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002561 &sigbuf, signer, signer_ctx)) != 0) {
2562 error("Signing %s failed: %s", filename, ssh_err(r));
2563 goto out;
2564 }
2565 if ((r = sshsig_armor(sigbuf, &abuf)) != 0) {
2566 error("%s: sshsig_armor: %s", __func__, ssh_err(r));
2567 goto out;
2568 }
2569 if ((asig = sshbuf_dup_string(abuf)) == NULL) {
2570 error("%s: buffer error", __func__);
2571 r = SSH_ERR_ALLOC_FAIL;
2572 goto out;
2573 }
2574
2575 if (fd == STDIN_FILENO) {
2576 fputs(asig, stdout);
2577 fflush(stdout);
2578 } else {
2579 xasprintf(&wfile, "%s.sig", filename);
2580 if (confirm_overwrite(wfile)) {
2581 if ((wfd = open(wfile, O_WRONLY|O_CREAT|O_TRUNC,
2582 0666)) == -1) {
2583 oerrno = errno;
2584 error("Cannot open %s: %s",
2585 wfile, strerror(errno));
2586 errno = oerrno;
2587 r = SSH_ERR_SYSTEM_ERROR;
2588 goto out;
2589 }
2590 if (atomicio(vwrite, wfd, asig,
2591 strlen(asig)) != strlen(asig)) {
2592 oerrno = errno;
2593 error("Cannot write to %s: %s",
2594 wfile, strerror(errno));
2595 errno = oerrno;
2596 r = SSH_ERR_SYSTEM_ERROR;
2597 goto out;
2598 }
2599 if (!quiet) {
2600 fprintf(stderr, "Write signature to %s\n",
2601 wfile);
2602 }
2603 }
2604 }
2605 /* success */
2606 r = 0;
2607 out:
2608 free(wfile);
2609 free(asig);
2610 sshbuf_free(abuf);
2611 sshbuf_free(sigbuf);
2612 if (wfd != -1)
2613 close(wfd);
2614 return r;
2615}
2616
2617static int
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002618sig_sign(const char *keypath, const char *sig_namespace, int argc, char **argv)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002619{
2620 int i, fd = -1, r, ret = -1;
2621 int agent_fd = -1;
2622 struct sshkey *pubkey = NULL, *privkey = NULL, *signkey = NULL;
2623 sshsig_signer *signer = NULL;
2624
2625 /* Check file arguments. */
2626 for (i = 0; i < argc; i++) {
2627 if (strcmp(argv[i], "-") != 0)
2628 continue;
2629 if (i > 0 || argc > 1)
2630 fatal("Cannot sign mix of paths and standard input");
2631 }
2632
2633 if ((r = sshkey_load_public(keypath, &pubkey, NULL)) != 0) {
2634 error("Couldn't load public key %s: %s", keypath, ssh_err(r));
2635 goto done;
2636 }
2637
2638 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0)
2639 debug("Couldn't get agent socket: %s", ssh_err(r));
2640 else {
2641 if ((r = ssh_agent_has_key(agent_fd, pubkey)) == 0)
2642 signer = agent_signer;
2643 else
2644 debug("Couldn't find key in agent: %s", ssh_err(r));
2645 }
2646
2647 if (signer == NULL) {
2648 /* Not using agent - try to load private key */
2649 if ((privkey = load_sign_key(keypath, pubkey)) == NULL)
2650 goto done;
2651 signkey = privkey;
2652 } else {
2653 /* Will use key in agent */
2654 signkey = pubkey;
2655 }
2656
2657 if (argc == 0) {
2658 if ((r = sign_one(signkey, "(stdin)", STDIN_FILENO,
2659 sig_namespace, signer, &agent_fd)) != 0)
2660 goto done;
2661 } else {
2662 for (i = 0; i < argc; i++) {
2663 if (strcmp(argv[i], "-") == 0)
2664 fd = STDIN_FILENO;
2665 else if ((fd = open(argv[i], O_RDONLY)) == -1) {
2666 error("Cannot open %s for signing: %s",
2667 argv[i], strerror(errno));
2668 goto done;
2669 }
2670 if ((r = sign_one(signkey, argv[i], fd, sig_namespace,
2671 signer, &agent_fd)) != 0)
2672 goto done;
2673 if (fd != STDIN_FILENO)
2674 close(fd);
2675 fd = -1;
2676 }
2677 }
2678
2679 ret = 0;
2680done:
2681 if (fd != -1 && fd != STDIN_FILENO)
2682 close(fd);
2683 sshkey_free(pubkey);
2684 sshkey_free(privkey);
2685 return ret;
2686}
2687
2688static int
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002689sig_verify(const char *signature, const char *sig_namespace,
2690 const char *principal, const char *allowed_keys, const char *revoked_keys)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002691{
2692 int r, ret = -1, sigfd = -1;
2693 struct sshbuf *sigbuf = NULL, *abuf = NULL;
2694 struct sshkey *sign_key = NULL;
2695 char *fp = NULL;
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002696 struct sshkey_sig_details *sig_details = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002697
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002698 memset(&sig_details, 0, sizeof(sig_details));
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002699 if ((abuf = sshbuf_new()) == NULL)
2700 fatal("%s: sshbuf_new() failed", __func__);
2701
2702 if ((sigfd = open(signature, O_RDONLY)) < 0) {
2703 error("Couldn't open signature file %s", signature);
2704 goto done;
2705 }
2706
2707 if ((r = sshkey_load_file(sigfd, abuf)) != 0) {
2708 error("Couldn't read signature file: %s", ssh_err(r));
2709 goto done;
2710 }
2711 if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) {
2712 error("%s: sshsig_armor: %s", __func__, ssh_err(r));
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002713 goto done;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002714 }
2715 if ((r = sshsig_verify_fd(sigbuf, STDIN_FILENO, sig_namespace,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002716 &sign_key, &sig_details)) != 0)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002717 goto done; /* sshsig_verify() prints error */
2718
2719 if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash,
2720 SSH_FP_DEFAULT)) == NULL)
2721 fatal("%s: sshkey_fingerprint failed", __func__);
2722 debug("Valid (unverified) signature from key %s", fp);
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002723 if (sig_details != NULL) {
2724 debug2("%s: signature details: counter = %u, flags = 0x%02x",
2725 __func__, sig_details->sk_counter, sig_details->sk_flags);
2726 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002727 free(fp);
2728 fp = NULL;
2729
2730 if (revoked_keys != NULL) {
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +00002731 if ((r = sshkey_check_revoked(sign_key, revoked_keys)) != 0) {
2732 debug3("sshkey_check_revoked failed: %s", ssh_err(r));
2733 goto done;
2734 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002735 }
2736
djm@openbsd.org8aa2aa32019-09-16 03:23:02 +00002737 if (allowed_keys != NULL &&
2738 (r = sshsig_check_allowed_keys(allowed_keys, sign_key,
2739 principal, sig_namespace)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002740 debug3("sshsig_check_allowed_keys failed: %s", ssh_err(r));
2741 goto done;
2742 }
2743 /* success */
2744 ret = 0;
2745done:
2746 if (!quiet) {
2747 if (ret == 0) {
2748 if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash,
2749 SSH_FP_DEFAULT)) == NULL) {
2750 fatal("%s: sshkey_fingerprint failed",
2751 __func__);
2752 }
djm@openbsd.org8aa2aa32019-09-16 03:23:02 +00002753 if (principal == NULL) {
2754 printf("Good \"%s\" signature with %s key %s\n",
2755 sig_namespace, sshkey_type(sign_key), fp);
2756
2757 } else {
2758 printf("Good \"%s\" signature for %s with %s key %s\n",
2759 sig_namespace, principal,
2760 sshkey_type(sign_key), fp);
2761 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002762 } else {
2763 printf("Could not verify signature.\n");
2764 }
2765 }
2766 if (sigfd != -1)
2767 close(sigfd);
2768 sshbuf_free(sigbuf);
2769 sshbuf_free(abuf);
2770 sshkey_free(sign_key);
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002771 sshkey_sig_details_free(sig_details);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00002772 free(fp);
2773 return ret;
2774}
2775
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002776static int
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00002777sig_find_principals(const char *signature, const char *allowed_keys) {
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002778 int r, ret = -1, sigfd = -1;
2779 struct sshbuf *sigbuf = NULL, *abuf = NULL;
2780 struct sshkey *sign_key = NULL;
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00002781 char *principals = NULL, *cp, *tmp;
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002782
2783 if ((abuf = sshbuf_new()) == NULL)
2784 fatal("%s: sshbuf_new() failed", __func__);
2785
2786 if ((sigfd = open(signature, O_RDONLY)) < 0) {
2787 error("Couldn't open signature file %s", signature);
2788 goto done;
2789 }
2790
2791 if ((r = sshkey_load_file(sigfd, abuf)) != 0) {
2792 error("Couldn't read signature file: %s", ssh_err(r));
2793 goto done;
2794 }
2795 if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) {
2796 error("%s: sshsig_armor: %s", __func__, ssh_err(r));
2797 goto done;
2798 }
2799 if ((r = sshsig_get_pubkey(sigbuf, &sign_key)) != 0) {
2800 error("%s: sshsig_get_pubkey: %s",
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00002801 __func__, ssh_err(r));
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002802 goto done;
2803 }
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00002804 if ((r = sshsig_find_principals(allowed_keys, sign_key,
2805 &principals)) != 0) {
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002806 error("%s: sshsig_get_principal: %s",
2807 __func__, ssh_err(r));
2808 goto done;
2809 }
2810 ret = 0;
2811done:
2812 if (ret == 0 ) {
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00002813 /* Emit matching principals one per line */
2814 tmp = principals;
2815 while ((cp = strsep(&tmp, ",")) != NULL && *cp != '\0')
2816 puts(cp);
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002817 } else {
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00002818 fprintf(stderr, "No principal matched.\n");
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002819 }
2820 if (sigfd != -1)
2821 close(sigfd);
2822 sshbuf_free(sigbuf);
2823 sshbuf_free(abuf);
2824 sshkey_free(sign_key);
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00002825 free(principals);
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00002826 return ret;
2827}
2828
Damien Millerf3747bf2013-01-18 11:44:04 +11002829static void
djm@openbsd.org3e60d182019-12-30 03:30:09 +00002830do_moduli_gen(const char *out_file, char **opts, size_t nopts)
2831{
2832#ifdef WITH_OPENSSL
2833 /* Moduli generation/screening */
2834 u_int32_t memory = 0;
2835 BIGNUM *start = NULL;
2836 int moduli_bits = 0;
2837 FILE *out;
2838 size_t i;
2839 const char *errstr;
2840
2841 /* Parse options */
2842 for (i = 0; i < nopts; i++) {
2843 if (strncmp(opts[i], "memory=", 7) == 0) {
2844 memory = (u_int32_t)strtonum(opts[i]+7, 1,
2845 UINT_MAX, &errstr);
2846 if (errstr) {
2847 fatal("Memory limit is %s: %s",
2848 errstr, opts[i]+7);
2849 }
2850 } else if (strncmp(opts[i], "start=", 6) == 0) {
2851 /* XXX - also compare length against bits */
2852 if (BN_hex2bn(&start, opts[i]+6) == 0)
2853 fatal("Invalid start point.");
2854 } else if (strncmp(opts[i], "bits=", 5) == 0) {
2855 moduli_bits = (int)strtonum(opts[i]+5, 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 "generation", opts[i]);
2864 }
2865 }
2866
2867 if ((out = fopen(out_file, "w")) == NULL) {
2868 fatal("Couldn't open modulus candidate file \"%s\": %s",
2869 out_file, strerror(errno));
2870 }
2871 setvbuf(out, NULL, _IOLBF, 0);
2872
2873 if (moduli_bits == 0)
2874 moduli_bits = DEFAULT_BITS;
2875 if (gen_candidates(out, memory, moduli_bits, start) != 0)
2876 fatal("modulus candidate generation failed");
2877#else /* WITH_OPENSSL */
2878 fatal("Moduli generation is not supported");
2879#endif /* WITH_OPENSSL */
2880}
2881
2882static void
2883do_moduli_screen(const char *out_file, char **opts, size_t nopts)
2884{
2885#ifdef WITH_OPENSSL
2886 /* Moduli generation/screening */
2887 char *checkpoint = NULL;
2888 u_int32_t generator_wanted = 0;
2889 unsigned long start_lineno = 0, lines_to_process = 0;
2890 int prime_tests = 0;
2891 FILE *out, *in = stdin;
2892 size_t i;
2893 const char *errstr;
2894
2895 /* Parse options */
2896 for (i = 0; i < nopts; i++) {
2897 if (strncmp(opts[i], "lines=", 6) == 0) {
2898 lines_to_process = strtoul(opts[i]+6, NULL, 10);
2899 } else if (strncmp(opts[i], "start-line=", 11) == 0) {
2900 start_lineno = strtoul(opts[i]+11, NULL, 10);
2901 } else if (strncmp(opts[i], "checkpoint=", 11) == 0) {
2902 checkpoint = xstrdup(opts[i]+11);
2903 } else if (strncmp(opts[i], "generator=", 10) == 0) {
2904 generator_wanted = (u_int32_t)strtonum(
2905 opts[i]+10, 1, UINT_MAX, &errstr);
2906 if (errstr != NULL) {
2907 fatal("Generator invalid: %s (%s)",
2908 opts[i]+10, errstr);
2909 }
2910 } else if (strncmp(opts[i], "prime-tests=", 12) == 0) {
2911 prime_tests = (int)strtonum(opts[i]+12, 1,
2912 INT_MAX, &errstr);
2913 if (errstr) {
2914 fatal("Invalid number: %s (%s)",
2915 opts[i]+12, errstr);
2916 }
2917 } else {
2918 fatal("Option \"%s\" is unsupported for moduli "
2919 "screening", opts[i]);
2920 }
2921 }
2922
2923 if (have_identity && strcmp(identity_file, "-") != 0) {
2924 if ((in = fopen(identity_file, "r")) == NULL) {
2925 fatal("Couldn't open modulus candidate "
2926 "file \"%s\": %s", identity_file,
2927 strerror(errno));
2928 }
2929 }
2930
2931 if ((out = fopen(out_file, "a")) == NULL) {
2932 fatal("Couldn't open moduli file \"%s\": %s",
2933 out_file, strerror(errno));
2934 }
2935 setvbuf(out, NULL, _IOLBF, 0);
2936 if (prime_test(in, out, prime_tests == 0 ? 100 : prime_tests,
2937 generator_wanted, checkpoint,
2938 start_lineno, lines_to_process) != 0)
2939 fatal("modulus screening failed");
2940#else /* WITH_OPENSSL */
2941 fatal("Moduli screening is not supported");
2942#endif /* WITH_OPENSSL */
2943}
2944
djm@openbsd.org90399712020-01-02 22:40:09 +00002945static char *
2946private_key_passphrase(void)
2947{
2948 char *passphrase1, *passphrase2;
2949
2950 /* Ask for a passphrase (twice). */
2951 if (identity_passphrase)
2952 passphrase1 = xstrdup(identity_passphrase);
2953 else if (identity_new_passphrase)
2954 passphrase1 = xstrdup(identity_new_passphrase);
2955 else {
2956passphrase_again:
2957 passphrase1 =
2958 read_passphrase("Enter passphrase (empty for no "
2959 "passphrase): ", RP_ALLOW_STDIN);
2960 passphrase2 = read_passphrase("Enter same passphrase again: ",
2961 RP_ALLOW_STDIN);
2962 if (strcmp(passphrase1, passphrase2) != 0) {
2963 /*
2964 * The passphrases do not match. Clear them and
2965 * retry.
2966 */
2967 freezero(passphrase1, strlen(passphrase1));
2968 freezero(passphrase2, strlen(passphrase2));
2969 printf("Passphrases do not match. Try again.\n");
2970 goto passphrase_again;
2971 }
2972 /* Clear the other copy of the passphrase. */
2973 freezero(passphrase2, strlen(passphrase2));
2974 }
2975 return passphrase1;
2976}
2977
2978static const char *
2979skip_ssh_url_preamble(const char *s)
2980{
2981 if (strncmp(s, "ssh://", 6) == 0)
2982 return s + 6;
2983 else if (strncmp(s, "ssh:", 4) == 0)
2984 return s + 4;
2985 return s;
2986}
2987
2988static int
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00002989do_download_sk(const char *skprovider, const char *device)
djm@openbsd.org90399712020-01-02 22:40:09 +00002990{
2991 struct sshkey **keys;
2992 size_t nkeys, i;
2993 int r, ok = -1;
2994 char *fp, *pin, *pass = NULL, *path, *pubpath;
2995 const char *ext;
2996
2997 if (skprovider == NULL)
2998 fatal("Cannot download keys without provider");
2999
3000 pin = read_passphrase("Enter PIN for security key: ", RP_ALLOW_STDIN);
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003001 if ((r = sshsk_load_resident(skprovider, device, pin,
3002 &keys, &nkeys)) != 0) {
djm@openbsd.org90399712020-01-02 22:40:09 +00003003 freezero(pin, strlen(pin));
3004 error("Unable to load resident keys: %s", ssh_err(r));
3005 return -1;
3006 }
3007 if (nkeys == 0)
3008 logit("No keys to download");
3009 freezero(pin, strlen(pin));
3010
3011 for (i = 0; i < nkeys; i++) {
3012 if (keys[i]->type != KEY_ECDSA_SK &&
3013 keys[i]->type != KEY_ED25519_SK) {
3014 error("Unsupported key type %s (%d)",
3015 sshkey_type(keys[i]), keys[i]->type);
3016 continue;
3017 }
3018 if ((fp = sshkey_fingerprint(keys[i],
3019 fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
3020 fatal("%s: sshkey_fingerprint failed", __func__);
3021 debug("%s: key %zu: %s %s %s (flags 0x%02x)", __func__, i,
3022 sshkey_type(keys[i]), fp, keys[i]->sk_application,
3023 keys[i]->sk_flags);
3024 ext = skip_ssh_url_preamble(keys[i]->sk_application);
3025 xasprintf(&path, "id_%s_rk%s%s",
3026 keys[i]->type == KEY_ECDSA_SK ? "ecdsa_sk" : "ed25519_sk",
3027 *ext == '\0' ? "" : "_", ext);
3028
3029 /* If the file already exists, ask the user to confirm. */
3030 if (!confirm_overwrite(path)) {
3031 free(path);
3032 break;
3033 }
3034
3035 /* Save the key with the application string as the comment */
3036 if (pass == NULL)
3037 pass = private_key_passphrase();
3038 if ((r = sshkey_save_private(keys[i], path, pass,
3039 keys[i]->sk_application, private_key_format,
3040 openssh_format_cipher, rounds)) != 0) {
3041 error("Saving key \"%s\" failed: %s",
3042 path, ssh_err(r));
3043 free(path);
3044 break;
3045 }
3046 if (!quiet) {
3047 printf("Saved %s key%s%s to %s\n",
3048 sshkey_type(keys[i]),
3049 *ext != '\0' ? " " : "",
3050 *ext != '\0' ? keys[i]->sk_application : "",
3051 path);
3052 }
3053
3054 /* Save public key too */
3055 xasprintf(&pubpath, "%s.pub", path);
3056 free(path);
3057 if ((r = sshkey_save_public(keys[i], pubpath,
3058 keys[i]->sk_application)) != 0) {
3059 free(pubpath);
3060 error("Saving public key \"%s\" failed: %s",
3061 pubpath, ssh_err(r));
3062 break;
3063 }
3064 free(pubpath);
3065 }
3066
3067 if (i >= nkeys)
3068 ok = 0; /* success */
3069 if (pass != NULL)
3070 freezero(pass, strlen(pass));
3071 for (i = 0; i < nkeys; i++)
3072 sshkey_free(keys[i]);
3073 free(keys);
3074 return ok ? 0 : -1;
3075}
3076
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003077static void
Damien Miller431f66b1999-11-21 18:31:57 +11003078usage(void)
3079{
Damien Millerf0858de2014-04-20 13:01:30 +10003080 fprintf(stderr,
jmc@openbsd.org69189742019-10-03 17:07:50 +00003081 "usage: ssh-keygen [-q] [-b bits] [-C comment] [-f output_keyfile] [-m format]\n"
naddy@openbsd.orgf0edda82019-11-18 23:16:49 +00003082 " [-t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa]\n"
naddy@openbsd.org0d005d62020-01-14 15:07:30 +00003083 " [-N new_passphrase] [-O option] [-w provider]\n"
jmc@openbsd.org69189742019-10-03 17:07:50 +00003084 " ssh-keygen -p [-f keyfile] [-m format] [-N new_passphrase]\n"
3085 " [-P old_passphrase]\n"
3086 " ssh-keygen -i [-f input_keyfile] [-m key_format]\n"
3087 " ssh-keygen -e [-f input_keyfile] [-m key_format]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003088 " ssh-keygen -y [-f input_keyfile]\n"
jmc@openbsd.org69189742019-10-03 17:07:50 +00003089 " ssh-keygen -c [-C comment] [-f keyfile] [-P passphrase]\n"
naddy@openbsd.org6288e3a2015-02-24 15:24:05 +00003090 " ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]\n"
djm@openbsd.org873d3e72017-04-30 23:18:44 +00003091 " ssh-keygen -B [-f input_keyfile]\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11003092#ifdef ENABLE_PKCS11
Damien Millerf0858de2014-04-20 13:01:30 +10003093 fprintf(stderr,
3094 " ssh-keygen -D pkcs11\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11003095#endif
Damien Millerf0858de2014-04-20 13:01:30 +10003096 fprintf(stderr,
jmc@openbsd.org6c91d422019-09-29 16:31:57 +00003097 " ssh-keygen -F hostname [-lv] [-f known_hosts_file]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003098 " ssh-keygen -H [-f known_hosts_file]\n"
naddy@openbsd.org0d005d62020-01-14 15:07:30 +00003099 " ssh-keygen -K [-w provider]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003100 " ssh-keygen -R hostname [-f known_hosts_file]\n"
jmc@openbsd.org6c91d422019-09-29 16:31:57 +00003101 " ssh-keygen -r hostname [-g] [-f input_keyfile]\n"
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003102#ifdef WITH_OPENSSL
naddy@openbsd.org0d005d62020-01-14 15:07:30 +00003103 " ssh-keygen -M generate [-O option] output_file\n"
3104 " ssh-keygen -M screen [-f input_file] [-O option] output_file\n"
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003105#endif
jmc@openbsd.org69189742019-10-03 17:07:50 +00003106 " ssh-keygen -I certificate_identity -s ca_key [-hU] [-D pkcs11_provider]\n"
3107 " [-n principals] [-O option] [-V validity_interval]\n"
3108 " [-z serial_number] file ...\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003109 " ssh-keygen -L [-f input_keyfile]\n"
jmc@openbsd.org69189742019-10-03 17:07:50 +00003110 " ssh-keygen -A [-f prefix_path]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10003111 " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
3112 " file ...\n"
naddy@openbsd.org0f44e592019-09-03 20:51:49 +00003113 " ssh-keygen -Q -f krl_file file ...\n"
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003114 " ssh-keygen -Y find-principals -s signature_file -f allowed_signers_file\n"
jmc@openbsd.org69189742019-10-03 17:07:50 +00003115 " ssh-keygen -Y check-novalidate -n namespace -s signature_file\n"
3116 " ssh-keygen -Y sign -f key_file -n namespace file ...\n"
3117 " ssh-keygen -Y verify -f allowed_signers_file -I signer_identity\n"
3118 " -n namespace -s signature_file [-r revocation_file]\n");
Damien Miller95def091999-11-25 00:26:21 +11003119 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11003120}
3121
Damien Miller95def091999-11-25 00:26:21 +11003122/*
3123 * Main program for key management.
3124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003125int
Damien Millerdf8b7db2007-01-05 16:22:57 +11003126main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003127{
djm@openbsd.org90399712020-01-02 22:40:09 +00003128 char dotsshdir[PATH_MAX], comment[1024], *passphrase;
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003129 char *rr_hostname = NULL, *ep, *fp, *ra;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003130 struct sshkey *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11003131 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11003132 struct stat st;
djm@openbsd.org90399712020-01-02 22:40:09 +00003133 int r, opt, type;
djm@openbsd.org851f8032019-01-23 04:16:22 +00003134 int change_passphrase = 0, change_comment = 0, show_cert = 0;
3135 int find_host = 0, delete_host = 0, hash_hosts = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11003136 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
djm@openbsd.org851f8032019-01-23 04:16:22 +00003137 int prefer_agent = 0, convert_to = 0, convert_from = 0;
djm@openbsd.orgbe063942019-01-23 04:51:02 +00003138 int print_public = 0, print_generic = 0, cert_serial_autoinc = 0;
djm@openbsd.org90399712020-01-02 22:40:09 +00003139 int do_gen_candidates = 0, do_screen_candidates = 0, download_sk = 0;
djm@openbsd.org3093d122019-12-30 09:49:52 +00003140 unsigned long long cert_serial = 0;
djm@openbsd.org1e645fe2019-12-30 03:28:41 +00003141 char *identity_comment = NULL, *ca_key_path = NULL, **opts = NULL;
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003142 char *sk_application = NULL, *sk_device = NULL, *sk_user = NULL;
djm@openbsd.org1e645fe2019-12-30 03:28:41 +00003143 size_t i, nopts = 0;
dtucker@openbsd.org7afc45c2019-08-08 08:02:57 +00003144 u_int32_t bits = 0;
djm@openbsd.orgdaeaf412019-11-25 00:55:58 +00003145 uint8_t sk_flags = SSH_SK_USER_PRESENCE_REQD;
Damien Miller02e754f2005-05-26 12:19:39 +10003146 const char *errstr;
djm@openbsd.org851f8032019-01-23 04:16:22 +00003147 int log_level = SYSLOG_LEVEL_INFO;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003148 char *sign_op = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003149
Damien Miller95def091999-11-25 00:26:21 +11003150 extern int optind;
3151 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003152
Darren Tuckerce321d82005-10-03 18:11:24 +10003153 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
3154 sanitise_stdfd();
3155
Darren Tucker9ac56e92007-01-14 10:19:59 +11003156 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10003157
Kevin Steves3a881912002-07-20 19:05:40 +00003158 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10003159
Damien Miller42c5ec42018-11-23 10:40:06 +11003160 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
3161
djm@openbsd.orga287c5a2017-02-10 03:36:40 +00003162 msetlocale();
3163
Damien Miller5428f641999-11-25 11:54:57 +11003164 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11003165 pw = getpwuid(getuid());
djm@openbsd.org3038a192015-04-17 13:19:22 +00003166 if (!pw)
3167 fatal("No user exists for uid %lu", (u_long)getuid());
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00003168 if (gethostname(hostname, sizeof(hostname)) == -1)
djm@openbsd.org3038a192015-04-17 13:19:22 +00003169 fatal("gethostname: %s", strerror(errno));
Damien Miller5428f641999-11-25 11:54:57 +11003170
djm@openbsd.org23f38c22019-10-31 21:17:09 +00003171 sk_provider = getenv("SSH_SK_PROVIDER");
3172
djm@openbsd.org90399712020-01-02 22:40:09 +00003173 /* Remaining characters: dGjJSTWx */
3174 while ((opt = getopt(argc, argv, "ABHKLQUXceghiklopquvy"
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003175 "C:D:E:F:I:M:N:O:P:R:V:Y:Z:"
djm@openbsd.org3093d122019-12-30 09:49:52 +00003176 "a:b:f:g:m:n:r:s:t:w:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11003177 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10003178 case 'A':
3179 gen_all_hostkeys = 1;
3180 break;
Damien Miller95def091999-11-25 00:26:21 +11003181 case 'b':
dtucker@openbsd.org7afc45c2019-08-08 08:02:57 +00003182 bits = (u_int32_t)strtonum(optarg, 1, UINT32_MAX,
3183 &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10003184 if (errstr)
3185 fatal("Bits has bad value %s (%s)",
3186 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11003187 break;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00003188 case 'E':
3189 fingerprint_hash = ssh_digest_alg_by_name(optarg);
3190 if (fingerprint_hash == -1)
3191 fatal("Invalid hash algorithm \"%s\"", optarg);
3192 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11003193 case 'F':
3194 find_host = 1;
3195 rr_hostname = optarg;
3196 break;
3197 case 'H':
3198 hash_hosts = 1;
3199 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11003200 case 'I':
3201 cert_key_id = optarg;
3202 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11003203 case 'R':
3204 delete_host = 1;
3205 rr_hostname = optarg;
3206 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11003207 case 'L':
3208 show_cert = 1;
3209 break;
Damien Miller95def091999-11-25 00:26:21 +11003210 case 'l':
3211 print_fingerprint = 1;
3212 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00003213 case 'B':
3214 print_bubblebabble = 1;
3215 break;
Damien Miller44b25042010-07-02 13:35:01 +10003216 case 'm':
3217 if (strcasecmp(optarg, "RFC4716") == 0 ||
3218 strcasecmp(optarg, "ssh2") == 0) {
3219 convert_format = FMT_RFC4716;
3220 break;
3221 }
3222 if (strcasecmp(optarg, "PKCS8") == 0) {
3223 convert_format = FMT_PKCS8;
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00003224 private_key_format = SSHKEY_PRIVATE_PKCS8;
Damien Miller44b25042010-07-02 13:35:01 +10003225 break;
3226 }
3227 if (strcasecmp(optarg, "PEM") == 0) {
3228 convert_format = FMT_PEM;
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00003229 private_key_format = SSHKEY_PRIVATE_PEM;
Damien Miller44b25042010-07-02 13:35:01 +10003230 break;
3231 }
3232 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11003233 case 'n':
3234 cert_principals = optarg;
3235 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11003236 case 'o':
djm@openbsd.orged7bd5d2018-08-08 01:16:01 +00003237 /* no-op; new format is already the default */
Damien Millerbcd00ab2013-12-07 10:41:55 +11003238 break;
Damien Miller95def091999-11-25 00:26:21 +11003239 case 'p':
3240 change_passphrase = 1;
3241 break;
Damien Miller95def091999-11-25 00:26:21 +11003242 case 'c':
3243 change_comment = 1;
3244 break;
Damien Miller95def091999-11-25 00:26:21 +11003245 case 'f':
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003246 if (strlcpy(identity_file, optarg,
3247 sizeof(identity_file)) >= sizeof(identity_file))
Damien Millerb089fb52005-05-26 12:16:18 +10003248 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11003249 have_identity = 1;
3250 break;
Damien Miller37876e92003-05-15 10:19:46 +10003251 case 'g':
3252 print_generic = 1;
3253 break;
djm@openbsd.org90399712020-01-02 22:40:09 +00003254 case 'K':
3255 download_sk = 1;
3256 break;
Damien Miller95def091999-11-25 00:26:21 +11003257 case 'P':
3258 identity_passphrase = optarg;
3259 break;
Damien Miller95def091999-11-25 00:26:21 +11003260 case 'N':
3261 identity_new_passphrase = optarg;
3262 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11003263 case 'Q':
3264 check_krl = 1;
3265 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11003266 case 'O':
djm@openbsd.org1e645fe2019-12-30 03:28:41 +00003267 opts = xrecallocarray(opts, nopts, nopts + 1,
3268 sizeof(*opts));
3269 opts[nopts++] = xstrdup(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11003270 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11003271 case 'Z':
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00003272 openssh_format_cipher = optarg;
Damien Millerbcd00ab2013-12-07 10:41:55 +11003273 break;
Damien Miller95def091999-11-25 00:26:21 +11003274 case 'C':
3275 identity_comment = optarg;
3276 break;
Damien Miller95def091999-11-25 00:26:21 +11003277 case 'q':
3278 quiet = 1;
3279 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00003280 case 'e':
Ben Lindstrom5a707822001-04-22 17:15:46 +00003281 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10003282 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10003283 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11003284 case 'h':
3285 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10003286 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11003287 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11003288 case 'k':
3289 gen_krl = 1;
3290 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00003291 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10003292 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00003293 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10003294 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10003295 break;
Damien Millereba71ba2000-04-29 23:57:08 +10003296 case 'y':
3297 print_public = 1;
3298 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11003299 case 's':
3300 ca_key_path = optarg;
3301 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003302 case 't':
3303 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003304 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00003305 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11003306 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00003307 break;
djm@openbsd.orga98339e2017-06-28 01:09:22 +00003308 case 'U':
3309 prefer_agent = 1;
3310 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11003311 case 'u':
3312 update_krl = 1;
3313 break;
Darren Tucker06930c72003-12-31 11:34:51 +11003314 case 'v':
3315 if (log_level == SYSLOG_LEVEL_INFO)
3316 log_level = SYSLOG_LEVEL_DEBUG1;
3317 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10003318 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11003319 log_level < SYSLOG_LEVEL_DEBUG3)
3320 log_level++;
3321 }
3322 break;
Damien Miller37876e92003-05-15 10:19:46 +10003323 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11003324 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10003325 break;
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003326 case 'a':
3327 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
3328 if (errstr)
3329 fatal("Invalid number: %s (%s)",
3330 optarg, errstr);
3331 break;
3332 case 'V':
3333 parse_cert_times(optarg);
3334 break;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003335 case 'Y':
3336 sign_op = optarg;
djm@openbsd.org103c51f2019-11-18 01:59:48 +00003337 break;
djm@openbsd.org23f38c22019-10-31 21:17:09 +00003338 case 'w':
3339 sk_provider = optarg;
3340 break;
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003341 case 'z':
3342 errno = 0;
djm@openbsd.orgbe063942019-01-23 04:51:02 +00003343 if (*optarg == '+') {
3344 cert_serial_autoinc = 1;
3345 optarg++;
3346 }
djm@openbsd.org1d9a2e22015-05-28 07:37:31 +00003347 cert_serial = strtoull(optarg, &ep, 10);
3348 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
3349 (errno == ERANGE && cert_serial == ULLONG_MAX))
3350 fatal("Invalid serial number \"%s\"", optarg);
3351 break;
Darren Tuckerd8c3cfb2016-09-12 13:30:50 +10003352 case 'M':
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003353 if (strcmp(optarg, "generate") == 0)
3354 do_gen_candidates = 1;
3355 else if (strcmp(optarg, "screen") == 0)
3356 do_screen_candidates = 1;
3357 else
3358 fatal("Unsupported moduli option %s", optarg);
Darren Tuckerd8c3cfb2016-09-12 13:30:50 +10003359 break;
Damien Miller95def091999-11-25 00:26:21 +11003360 case '?':
3361 default:
3362 usage();
3363 }
3364 }
Darren Tucker06930c72003-12-31 11:34:51 +11003365
djm@openbsd.org6bff9522019-11-14 21:27:29 +00003366#ifdef ENABLE_SK_INTERNAL
3367 if (sk_provider == NULL)
3368 sk_provider = "internal";
3369#endif
3370
Darren Tucker06930c72003-12-31 11:34:51 +11003371 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11003372 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11003373
Damien Miller0a80ca12010-02-27 07:55:05 +11003374 argv += optind;
3375 argc -= optind;
3376
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003377 if (sign_op != NULL) {
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003378 if (strncmp(sign_op, "find-principals", 15) == 0) {
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003379 if (ca_key_path == NULL) {
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003380 error("Too few arguments for find-principals:"
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003381 "missing signature file");
3382 exit(1);
3383 }
3384 if (!have_identity) {
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003385 error("Too few arguments for find-principals:"
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003386 "missing allowed keys file");
3387 exit(1);
3388 }
djm@openbsd.org72a8bea2020-01-23 23:31:52 +00003389 return sig_find_principals(ca_key_path, identity_file);
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00003390 } else if (strncmp(sign_op, "sign", 4) == 0) {
3391 if (cert_principals == NULL ||
3392 *cert_principals == '\0') {
3393 error("Too few arguments for sign: "
3394 "missing namespace");
3395 exit(1);
3396 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003397 if (!have_identity) {
3398 error("Too few arguments for sign: "
3399 "missing key");
3400 exit(1);
3401 }
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003402 return sig_sign(identity_file, cert_principals,
3403 argc, argv);
djm@openbsd.org8aa2aa32019-09-16 03:23:02 +00003404 } else if (strncmp(sign_op, "check-novalidate", 16) == 0) {
3405 if (ca_key_path == NULL) {
3406 error("Too few arguments for check-novalidate: "
3407 "missing signature file");
3408 exit(1);
3409 }
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003410 return sig_verify(ca_key_path, cert_principals,
3411 NULL, NULL, NULL);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003412 } else if (strncmp(sign_op, "verify", 6) == 0) {
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00003413 if (cert_principals == NULL ||
3414 *cert_principals == '\0') {
3415 error("Too few arguments for verify: "
3416 "missing namespace");
3417 exit(1);
3418 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003419 if (ca_key_path == NULL) {
3420 error("Too few arguments for verify: "
3421 "missing signature file");
3422 exit(1);
3423 }
3424 if (!have_identity) {
3425 error("Too few arguments for sign: "
3426 "missing allowed keys file");
3427 exit(1);
3428 }
3429 if (cert_key_id == NULL) {
3430 error("Too few arguments for verify: "
3431 "missing principal ID");
3432 exit(1);
3433 }
djm@openbsd.org56cffcc2020-01-23 02:43:48 +00003434 return sig_verify(ca_key_path, cert_principals,
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003435 cert_key_id, identity_file, rr_hostname);
3436 }
djm@openbsd.orgd15c8ad2020-01-24 05:33:01 +00003437 error("Unsupported operation for -Y: \"%s\"", sign_op);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00003438 usage();
3439 /* NOTREACHED */
3440 }
3441
Damien Miller0a80ca12010-02-27 07:55:05 +11003442 if (ca_key_path != NULL) {
Damien Millerf3747bf2013-01-18 11:44:04 +11003443 if (argc < 1 && !gen_krl) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003444 error("Too few arguments.");
Damien Miller0a80ca12010-02-27 07:55:05 +11003445 usage();
3446 }
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003447 } else if (argc > 0 && !gen_krl && !check_krl &&
3448 !do_gen_candidates && !do_screen_candidates) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003449 error("Too many arguments.");
Damien Miller95def091999-11-25 00:26:21 +11003450 usage();
3451 }
3452 if (change_passphrase && change_comment) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003453 error("Can only have one of -p and -c.");
Damien Miller95def091999-11-25 00:26:21 +11003454 usage();
3455 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10003456 if (print_fingerprint && (delete_host || hash_hosts)) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003457 error("Cannot use -l with -H or -R.");
Darren Tucker0f7e9102008-06-08 12:54:29 +10003458 usage();
3459 }
Damien Millerf3747bf2013-01-18 11:44:04 +11003460 if (gen_krl) {
djm@openbsd.org851f8032019-01-23 04:16:22 +00003461 do_gen_krl(pw, update_krl, ca_key_path,
3462 cert_serial, identity_comment, argc, argv);
Damien Millerf3747bf2013-01-18 11:44:04 +11003463 return (0);
3464 }
3465 if (check_krl) {
3466 do_check_krl(pw, argc, argv);
3467 return (0);
3468 }
Damien Miller0a80ca12010-02-27 07:55:05 +11003469 if (ca_key_path != NULL) {
3470 if (cert_key_id == NULL)
3471 fatal("Must specify key id (-I) when certifying");
djm@openbsd.org1e645fe2019-12-30 03:28:41 +00003472 for (i = 0; i < nopts; i++)
3473 add_cert_option(opts[i]);
djm@openbsd.orgbe063942019-01-23 04:51:02 +00003474 do_ca_sign(pw, ca_key_path, prefer_agent,
3475 cert_serial, cert_serial_autoinc, argc, argv);
Damien Miller0a80ca12010-02-27 07:55:05 +11003476 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11003477 if (show_cert)
3478 do_show_cert(pw);
djm@openbsd.org851f8032019-01-23 04:16:22 +00003479 if (delete_host || hash_hosts || find_host) {
3480 do_known_hosts(pw, rr_hostname, find_host,
3481 delete_host, hash_hosts);
3482 }
Damien Millerec77c952013-01-09 15:58:00 +11003483 if (pkcs11provider != NULL)
3484 do_download(pw);
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003485 if (download_sk) {
3486 for (i = 0; i < nopts; i++) {
3487 if (strncasecmp(opts[i], "device=", 7) == 0) {
3488 sk_device = xstrdup(opts[i] + 7);
3489 } else {
3490 fatal("Option \"%s\" is unsupported for "
3491 "FIDO authenticator download", opts[i]);
3492 }
3493 }
3494 return do_download_sk(sk_provider, sk_device);
3495 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00003496 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11003497 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11003498 if (change_passphrase)
3499 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11003500 if (change_comment)
djm@openbsd.org851f8032019-01-23 04:16:22 +00003501 do_change_comment(pw, identity_comment);
Damien Miller1f0311c2014-05-15 14:24:09 +10003502#ifdef WITH_OPENSSL
Damien Miller44b25042010-07-02 13:35:01 +10003503 if (convert_to)
3504 do_convert_to(pw);
3505 if (convert_from)
3506 do_convert_from(pw);
djm@openbsd.orgc8bdd2d2019-09-06 07:53:40 +00003507#else /* WITH_OPENSSL */
3508 if (convert_to || convert_from)
3509 fatal("key conversion disabled at compile time");
3510#endif /* WITH_OPENSSL */
Damien Millereba71ba2000-04-29 23:57:08 +10003511 if (print_public)
3512 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11003513 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11003514 unsigned int n = 0;
3515
3516 if (have_identity) {
djm@openbsd.org851f8032019-01-23 04:16:22 +00003517 n = do_print_resource_record(pw, identity_file,
3518 rr_hostname, print_generic);
djm@openbsd.org3038a192015-04-17 13:19:22 +00003519 if (n == 0)
3520 fatal("%s: %s", identity_file, strerror(errno));
Damien Millercb314822006-03-26 13:48:01 +11003521 exit(0);
3522 } else {
3523
3524 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003525 _PATH_HOST_RSA_KEY_FILE, rr_hostname,
3526 print_generic);
Damien Millercb314822006-03-26 13:48:01 +11003527 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003528 _PATH_HOST_DSA_KEY_FILE, rr_hostname,
3529 print_generic);
Damien Miller3bde12a2012-06-20 21:51:11 +10003530 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003531 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname,
3532 print_generic);
Damien Miller16cd3922014-05-15 13:45:58 +10003533 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003534 _PATH_HOST_ED25519_KEY_FILE, rr_hostname,
3535 print_generic);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00003536 n += do_print_resource_record(pw,
djm@openbsd.org851f8032019-01-23 04:16:22 +00003537 _PATH_HOST_XMSS_KEY_FILE, rr_hostname,
3538 print_generic);
Damien Millercb314822006-03-26 13:48:01 +11003539 if (n == 0)
3540 fatal("no keys found.");
3541 exit(0);
3542 }
Damien Miller37876e92003-05-15 10:19:46 +10003543 }
Damien Miller95def091999-11-25 00:26:21 +11003544
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003545 if (do_gen_candidates || do_screen_candidates) {
3546 if (argc <= 0)
3547 fatal("No output file specified");
3548 else if (argc > 1)
3549 fatal("Too many output files specified");
3550 }
Darren Tucker019cefe2003-08-02 22:40:07 +10003551 if (do_gen_candidates) {
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003552 do_moduli_gen(argv[0], opts, nopts);
3553 return 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10003554 }
Darren Tucker019cefe2003-08-02 22:40:07 +10003555 if (do_screen_candidates) {
djm@openbsd.org3e60d182019-12-30 03:30:09 +00003556 do_moduli_screen(argv[0], opts, nopts);
3557 return 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10003558 }
3559
Damien Miller58f1baf2011-05-05 14:06:15 +10003560 if (gen_all_hostkeys) {
3561 do_gen_all_hostkeys(pw);
3562 return (0);
3563 }
3564
Damien Millerf14be5c2005-11-05 15:15:49 +11003565 if (key_type_name == NULL)
djm@openbsd.orgd1958792015-05-28 04:40:13 +00003566 key_type_name = DEFAULT_KEY_TYPE_NAME;
Damien Millerf14be5c2005-11-05 15:15:49 +11003567
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003568 type = sshkey_type_from_name(key_type_name);
djm@openbsd.org7efb4552015-01-18 13:22:28 +00003569 type_bits_valid(type, key_type_name, &bits);
Damien Miller58f1baf2011-05-05 14:06:15 +10003570
Darren Tucker3af2ac52005-11-29 13:10:24 +11003571 if (!quiet)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003572 printf("Generating public/private %s key pair.\n",
3573 key_type_name);
markus@openbsd.org2c557442019-11-12 19:33:08 +00003574 switch (type) {
3575 case KEY_ECDSA_SK:
3576 case KEY_ED25519_SK:
djm@openbsd.org3093d122019-12-30 09:49:52 +00003577 for (i = 0; i < nopts; i++) {
3578 if (strcasecmp(opts[i], "no-touch-required") == 0) {
3579 sk_flags &= ~SSH_SK_USER_PRESENCE_REQD;
3580 } else if (strcasecmp(opts[i], "resident") == 0) {
3581 sk_flags |= SSH_SK_RESIDENT_KEY;
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003582 } else if (strncasecmp(opts[i], "device=", 7) == 0) {
3583 sk_device = xstrdup(opts[i] + 7);
3584 } else if (strncasecmp(opts[i], "user=", 5) == 0) {
3585 sk_user = xstrdup(opts[i] + 5);
3586 } else if (strncasecmp(opts[i],
3587 "application=", 12) == 0) {
3588 sk_application = xstrdup(opts[i] + 12);
djm@openbsd.org3093d122019-12-30 09:49:52 +00003589 } else {
3590 fatal("Option \"%s\" is unsupported for "
3591 "FIDO authenticator enrollment", opts[i]);
3592 }
3593 }
3594 if (!quiet) {
3595 printf("You may need to touch your security key "
3596 "to authorize key generation.\n");
3597 }
djm@openbsd.org90399712020-01-02 22:40:09 +00003598 passphrase = NULL;
djm@openbsd.org43ce9642019-12-30 09:24:45 +00003599 for (i = 0 ; i < 3; i++) {
djm@openbsd.org43ce9642019-12-30 09:24:45 +00003600 fflush(stdout);
djm@openbsd.orgc312ca02020-01-06 02:00:46 +00003601 r = sshsk_enroll(type, sk_provider, sk_device,
3602 sk_application == NULL ? "ssh:" : sk_application,
3603 sk_user, sk_flags, passphrase, NULL,
3604 &private, NULL);
djm@openbsd.org43ce9642019-12-30 09:24:45 +00003605 if (r == 0)
3606 break;
3607 if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
3608 exit(1); /* error message already printed */
djm@openbsd.org90399712020-01-02 22:40:09 +00003609 if (passphrase != NULL)
3610 freezero(passphrase, strlen(passphrase));
3611 passphrase = read_passphrase("Enter PIN for security "
djm@openbsd.org43ce9642019-12-30 09:24:45 +00003612 "key: ", RP_ALLOW_STDIN);
djm@openbsd.org26cb1282019-11-25 00:57:27 +00003613 }
djm@openbsd.org90399712020-01-02 22:40:09 +00003614 if (passphrase != NULL)
3615 freezero(passphrase, strlen(passphrase));
djm@openbsd.org43ce9642019-12-30 09:24:45 +00003616 if (i > 3)
3617 fatal("Too many incorrect PINs");
djm@openbsd.org90399712020-01-02 22:40:09 +00003618 break;
markus@openbsd.org2c557442019-11-12 19:33:08 +00003619 default:
3620 if ((r = sshkey_generate(type, bits, &private)) != 0)
3621 fatal("sshkey_generate failed");
3622 break;
3623 }
djm@openbsd.org3038a192015-04-17 13:19:22 +00003624 if ((r = sshkey_from_private(private, &public)) != 0)
markus@openbsd.org7da5df12017-05-30 14:16:41 +00003625 fatal("sshkey_from_private failed: %s\n", ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11003626
3627 if (!have_identity)
3628 ask_filename(pw, "Enter file in which to save the key");
3629
Damien Miller788f2122005-11-05 15:14:59 +11003630 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10003631 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
3632 pw->pw_dir, _PATH_SSH_USER_DIR);
3633 if (strstr(identity_file, dotsshdir) != NULL) {
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00003634 if (stat(dotsshdir, &st) == -1) {
Damien Miller50af79b2010-05-10 11:52:00 +10003635 if (errno != ENOENT) {
3636 error("Could not stat %s: %s", dotsshdir,
3637 strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00003638 } else if (mkdir(dotsshdir, 0700) == -1) {
Damien Miller50af79b2010-05-10 11:52:00 +10003639 error("Could not create directory '%s': %s",
3640 dotsshdir, strerror(errno));
3641 } else if (!quiet)
3642 printf("Created directory '%s'.\n", dotsshdir);
3643 }
Damien Miller95def091999-11-25 00:26:21 +11003644 }
3645 /* If the file already exists, ask the user to confirm. */
djm@openbsd.org85443f12019-09-03 08:27:52 +00003646 if (!confirm_overwrite(identity_file))
3647 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11003648
djm@openbsd.org90399712020-01-02 22:40:09 +00003649 /* Determine the passphrase for the private key */
3650 passphrase = private_key_passphrase();
Damien Miller95def091999-11-25 00:26:21 +11003651 if (identity_comment) {
3652 strlcpy(comment, identity_comment, sizeof(comment));
3653 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11003654 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11003655 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
3656 }
3657
3658 /* Save the key with the given passphrase and comment. */
djm@openbsd.org90399712020-01-02 22:40:09 +00003659 if ((r = sshkey_save_private(private, identity_file, passphrase,
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +00003660 comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003661 error("Saving key \"%s\" failed: %s",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003662 identity_file, ssh_err(r));
djm@openbsd.org90399712020-01-02 22:40:09 +00003663 freezero(passphrase, strlen(passphrase));
Damien Miller95def091999-11-25 00:26:21 +11003664 exit(1);
3665 }
djm@openbsd.org90399712020-01-02 22:40:09 +00003666 freezero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003667 sshkey_free(private);
Damien Miller95def091999-11-25 00:26:21 +11003668
djm@openbsd.org90399712020-01-02 22:40:09 +00003669 if (!quiet) {
djm@openbsd.org69ac4e32020-01-23 07:54:04 +00003670 printf("Your identification has been saved in %s\n",
djm@openbsd.org90399712020-01-02 22:40:09 +00003671 identity_file);
3672 }
Damien Miller95def091999-11-25 00:26:21 +11003673
Damien Miller95def091999-11-25 00:26:21 +11003674 strlcat(identity_file, ".pub", sizeof(identity_file));
djm@openbsd.org90399712020-01-02 22:40:09 +00003675 if ((r = sshkey_save_public(public, identity_file, comment)) != 0) {
djm@openbsd.org3038a192015-04-17 13:19:22 +00003676 fatal("Unable to save public key to %s: %s",
3677 identity_file, strerror(errno));
djm@openbsd.org90399712020-01-02 22:40:09 +00003678 }
Damien Miller95def091999-11-25 00:26:21 +11003679
3680 if (!quiet) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00003681 fp = sshkey_fingerprint(public, fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00003682 SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00003683 ra = sshkey_fingerprint(public, fingerprint_hash,
Darren Tucker9c16ac92008-06-13 04:40:35 +10003684 SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00003685 if (fp == NULL || ra == NULL)
3686 fatal("sshkey_fingerprint failed");
djm@openbsd.org69ac4e32020-01-23 07:54:04 +00003687 printf("Your public key has been saved in %s\n",
Damien Millereba71ba2000-04-29 23:57:08 +10003688 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11003689 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00003690 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10003691 printf("The key's randomart image is:\n");
3692 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10003693 free(ra);
3694 free(fp);
Damien Miller95def091999-11-25 00:26:21 +11003695 }
Damien Millereba71ba2000-04-29 23:57:08 +10003696
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00003697 sshkey_free(public);
Damien Miller95def091999-11-25 00:26:21 +11003698 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003699}