blob: 9f6106d4710673573319c05d957528853d027539 [file] [log] [blame]
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001/* $OpenBSD: ssh-keygen.c,v 1.253 2015/01/16 06:40:12 deraadt Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
Damien Miller72ef7c12015-01-15 02:21:31 +110021#ifdef WITH_OPENSSL
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Miller72ef7c12015-01-15 02:21:31 +110025#endif
Damien Millereba71ba2000-04-29 23:57:08 +100026
Darren Tucker39972492006-07-12 22:22:46 +100027#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100028#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100029#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100030#ifdef HAVE_PATHS_H
31# include <paths.h>
32#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100033#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100034#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100035#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100036#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100038#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000039#include <limits.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100040
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041#include "xmalloc.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000042#include "sshkey.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000043#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "authfile.h"
45#include "uuencode.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000046#include "sshbuf.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "pathnames.h"
48#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100049#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110050#include "match.h"
51#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100052#include "dns.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110053#include "ssh.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110054#include "ssh2.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000055#include "ssherr.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110056#include "ssh-pkcs11.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110057#include "atomicio.h"
58#include "krl.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000059#include "digest.h"
Ben Lindstromcd392282001-07-04 03:44:03 +000060
Damien Miller3f54a9f2005-11-05 14:52:18 +110061/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
62#define DEFAULT_BITS 2048
63#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100064#define DEFAULT_BITS_ECDSA 256
Damien Miller3f54a9f2005-11-05 14:52:18 +110065u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Miller5428f641999-11-25 11:54:57 +110067/*
68 * Flag indicating that we just want to change the passphrase. This can be
69 * set on the command line.
70 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071int change_passphrase = 0;
72
Damien Miller5428f641999-11-25 11:54:57 +110073/*
74 * Flag indicating that we just want to change the comment. This can be set
75 * on the command line.
76 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077int change_comment = 0;
78
79int quiet = 0;
80
Darren Tucker35c45532008-06-13 04:43:15 +100081int log_level = SYSLOG_LEVEL_INFO;
82
Damien Miller4b42d7f2005-03-01 21:48:35 +110083/* Flag indicating that we want to hash a known_hosts file */
84int hash_hosts = 0;
85/* Flag indicating that we want lookup a host in known_hosts file */
86int find_host = 0;
87/* Flag indicating that we want to delete a host from a known_hosts file */
88int delete_host = 0;
89
Damien Millerf2b70ca2010-03-05 07:39:35 +110090/* Flag indicating that we want to show the contents of a certificate */
91int show_cert = 0;
92
Damien Miller10f6f6b1999-11-17 17:29:08 +110093/* Flag indicating that we just want to see the key fingerprint */
94int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000095int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110096
djm@openbsd.org56d1c832014-12-21 22:27:55 +000097/* Hash algorithm to use for fingerprints. */
98int fingerprint_hash = SSH_FP_HASH_DEFAULT;
99
Damien Miller431f66b1999-11-21 18:31:57 +1100100/* The identity file name, given on the command line or entered by the user. */
101char identity_file[1024];
102int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
104/* This is set to the passphrase if given on the command line. */
105char *identity_passphrase = NULL;
106
107/* This is set to the new passphrase if given on the command line. */
108char *identity_new_passphrase = NULL;
109
110/* This is set to the new comment if given on the command line. */
111char *identity_comment = NULL;
112
Damien Miller0a80ca12010-02-27 07:55:05 +1100113/* Path to CA key when certifying keys. */
114char *ca_key_path = NULL;
115
Damien Miller4e270b02010-04-16 15:56:21 +1000116/* Certificate serial number */
Damien Miller55aca022012-12-03 11:25:30 +1100117unsigned long long cert_serial = 0;
Damien Miller4e270b02010-04-16 15:56:21 +1000118
Damien Miller0a80ca12010-02-27 07:55:05 +1100119/* Key type when certifying */
120u_int cert_key_type = SSH2_CERT_TYPE_USER;
121
122/* "key ID" of signed key */
123char *cert_key_id = NULL;
124
125/* Comma-separated list of principal names for certifying keys */
126char *cert_principals = NULL;
127
128/* Validity period for certificates */
129u_int64_t cert_valid_from = 0;
130u_int64_t cert_valid_to = ~0ULL;
131
Damien Miller4e270b02010-04-16 15:56:21 +1000132/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000133#define CERTOPT_X_FWD (1)
134#define CERTOPT_AGENT_FWD (1<<1)
135#define CERTOPT_PORT_FWD (1<<2)
136#define CERTOPT_PTY (1<<3)
137#define CERTOPT_USER_RC (1<<4)
138#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
139 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
140u_int32_t certflags_flags = CERTOPT_DEFAULT;
141char *certflags_command = NULL;
142char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100143
Damien Miller44b25042010-07-02 13:35:01 +1000144/* Conversion to/from various formats */
145int convert_to = 0;
146int convert_from = 0;
147enum {
148 FMT_RFC4716,
149 FMT_PKCS8,
150 FMT_PEM
151} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000152int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000153int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100154
Damien Millera41c8b12002-01-22 23:05:08 +1100155char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000156
Damien Miller757f34e2010-08-05 13:05:31 +1000157/* Load key from this PKCS#11 provider */
158char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000159
Damien Millerbcd00ab2013-12-07 10:41:55 +1100160/* Use new OpenSSH private key format when writing SSH2 keys instead of PEM */
161int use_new_format = 0;
162
163/* Cipher for new-format private keys */
164char *new_format_cipher = NULL;
165
166/*
167 * Number of KDF rounds to derive new format keys /
168 * number of primality trials when screening moduli.
169 */
170int rounds = 0;
171
Damien Miller431f66b1999-11-21 18:31:57 +1100172/* argv0 */
173extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174
Damien Millere5c0d522014-07-03 21:24:19 +1000175char hostname[NI_MAXHOST];
Damien Millereba71ba2000-04-29 23:57:08 +1000176
Darren Tucker770fc012004-05-13 16:24:32 +1000177/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000178int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Damien Millerdfceafe2012-07-06 13:44:19 +1000179int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
180 unsigned long);
Darren Tucker770fc012004-05-13 16:24:32 +1000181
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static void
Damien Miller884b63a2011-05-05 14:14:52 +1000183type_bits_valid(int type, u_int32_t *bitsp)
Damien Miller58f1baf2011-05-05 14:06:15 +1000184{
Damien Miller72ef7c12015-01-15 02:21:31 +1100185#ifdef WITH_OPENSSL
Damien Miller58f1baf2011-05-05 14:06:15 +1000186 u_int maxbits;
Damien Miller72ef7c12015-01-15 02:21:31 +1100187#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000188
189 if (type == KEY_UNSPEC) {
190 fprintf(stderr, "unknown key type %s\n", key_type_name);
191 exit(1);
192 }
Damien Miller884b63a2011-05-05 14:14:52 +1000193 if (*bitsp == 0) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000194 if (type == KEY_DSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000195 *bitsp = DEFAULT_BITS_DSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000196 else if (type == KEY_ECDSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000197 *bitsp = DEFAULT_BITS_ECDSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000198 else
Damien Miller884b63a2011-05-05 14:14:52 +1000199 *bitsp = DEFAULT_BITS;
Damien Miller58f1baf2011-05-05 14:06:15 +1000200 }
Damien Miller72ef7c12015-01-15 02:21:31 +1100201#ifdef WITH_OPENSSL
Damien Miller58f1baf2011-05-05 14:06:15 +1000202 maxbits = (type == KEY_DSA) ?
203 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
Damien Miller884b63a2011-05-05 14:14:52 +1000204 if (*bitsp > maxbits) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000205 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
206 exit(1);
207 }
Damien Miller884b63a2011-05-05 14:14:52 +1000208 if (type == KEY_DSA && *bitsp != 1024)
Damien Miller58f1baf2011-05-05 14:06:15 +1000209 fatal("DSA keys must be 1024 bits");
Damien Miller5be9d9e2013-12-07 11:24:01 +1100210 else if (type != KEY_ECDSA && type != KEY_ED25519 && *bitsp < 768)
Damien Miller58f1baf2011-05-05 14:06:15 +1000211 fatal("Key must at least be 768 bits");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000212 else if (type == KEY_ECDSA && sshkey_ecdsa_bits_to_nid(*bitsp) == -1)
Damien Miller58f1baf2011-05-05 14:06:15 +1000213 fatal("Invalid ECDSA key length - valid lengths are "
214 "256, 384 or 521 bits");
Damien Miller1f0311c2014-05-15 14:24:09 +1000215#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000216}
217
218static void
Damien Miller431f66b1999-11-21 18:31:57 +1100219ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220{
Damien Miller95def091999-11-25 00:26:21 +1100221 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100222 char *name = NULL;
223
Damien Miller993dd552002-02-19 15:22:47 +1100224 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000225 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100226 else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000227 switch (sshkey_type_from_name(key_type_name)) {
Damien Miller993dd552002-02-19 15:22:47 +1100228 case KEY_RSA1:
229 name = _PATH_SSH_CLIENT_IDENTITY;
230 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000231 case KEY_DSA_CERT:
232 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100233 case KEY_DSA:
234 name = _PATH_SSH_CLIENT_ID_DSA;
235 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100236#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000237 case KEY_ECDSA_CERT:
238 case KEY_ECDSA:
239 name = _PATH_SSH_CLIENT_ID_ECDSA;
240 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100241#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000242 case KEY_RSA_CERT:
243 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100244 case KEY_RSA:
245 name = _PATH_SSH_CLIENT_ID_RSA;
246 break;
Damien Miller5be9d9e2013-12-07 11:24:01 +1100247 case KEY_ED25519:
248 case KEY_ED25519_CERT:
249 name = _PATH_SSH_CLIENT_ID_ED25519;
250 break;
Damien Miller993dd552002-02-19 15:22:47 +1100251 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100252 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100253 exit(1);
254 break;
255 }
Damien Miller90967402006-03-26 14:07:26 +1100256 }
Damien Millere39cacc2000-11-29 12:18:44 +1100257 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000258 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100259 if (fgets(buf, sizeof(buf), stdin) == NULL)
260 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000261 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100262 if (strcmp(buf, "") != 0)
263 strlcpy(identity_file, buf, sizeof(identity_file));
264 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100265}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000267static struct sshkey *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000268load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000269{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000270 char *pass;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000271 struct sshkey *prv;
272 int r;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000273
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000274 if ((r = sshkey_load_private(filename, "", &prv, NULL)) == 0)
275 return prv;
276 if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
277 fatal("Load key \"%s\": %s", filename, ssh_err(r));
278 if (identity_passphrase)
279 pass = xstrdup(identity_passphrase);
280 else
281 pass = read_passphrase("Enter passphrase: ", RP_ALLOW_STDIN);
282 r = sshkey_load_private(filename, pass, &prv, NULL);
283 explicit_bzero(pass, strlen(pass));
284 free(pass);
285 if (r != 0)
286 fatal("Load key \"%s\": %s", filename, ssh_err(r));
Ben Lindstromd0fca422001-03-26 13:44:06 +0000287 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000288}
289
Damien Miller874d77b2000-10-14 16:23:11 +1100290#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000291#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100292#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000293#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000294
Damien Miller1f0311c2014-05-15 14:24:09 +1000295#ifdef WITH_OPENSSL
Ben Lindstrombba81212001-06-25 05:01:22 +0000296static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000297do_convert_to_ssh2(struct passwd *pw, struct sshkey *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000298{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000299 size_t len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000300 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100301 char comment[61];
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000302 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000303
Damien Millera563cce2012-04-22 11:07:28 +1000304 if (k->type == KEY_RSA1) {
305 fprintf(stderr, "version 1 keys are not supported\n");
306 exit(1);
307 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000308 if ((r = sshkey_to_blob(k, &blob, &len)) != 0) {
309 fprintf(stderr, "key_to_blob failed: %s\n", ssh_err(r));
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000310 exit(1);
311 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100312 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
313 snprintf(comment, sizeof(comment),
314 "%u-bit %s, converted by %s@%s from OpenSSH",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000315 sshkey_size(k), sshkey_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000316 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100317
318 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
319 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000320 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100321 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000322 sshkey_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000323 free(blob);
Damien Millereba71ba2000-04-29 23:57:08 +1000324 exit(0);
325}
326
Ben Lindstrombba81212001-06-25 05:01:22 +0000327static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000328do_convert_to_pkcs8(struct sshkey *k)
Damien Miller44b25042010-07-02 13:35:01 +1000329{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000330 switch (sshkey_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000331 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000332 case KEY_RSA:
333 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
334 fatal("PEM_write_RSA_PUBKEY failed");
335 break;
336 case KEY_DSA:
337 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
338 fatal("PEM_write_DSA_PUBKEY failed");
339 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000340#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000341 case KEY_ECDSA:
342 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
343 fatal("PEM_write_EC_PUBKEY failed");
344 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000345#endif
Damien Miller44b25042010-07-02 13:35:01 +1000346 default:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000347 fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000348 }
349 exit(0);
350}
351
352static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000353do_convert_to_pem(struct sshkey *k)
Damien Miller44b25042010-07-02 13:35:01 +1000354{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000355 switch (sshkey_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000356 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000357 case KEY_RSA:
358 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
359 fatal("PEM_write_RSAPublicKey failed");
360 break;
361#if notyet /* OpenSSH 0.9.8 lacks this function */
362 case KEY_DSA:
363 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
364 fatal("PEM_write_DSAPublicKey failed");
365 break;
366#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000367 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000368 default:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000369 fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000370 }
371 exit(0);
372}
373
374static void
375do_convert_to(struct passwd *pw)
376{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000377 struct sshkey *k;
Damien Miller44b25042010-07-02 13:35:01 +1000378 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000379 int r;
Damien Miller44b25042010-07-02 13:35:01 +1000380
381 if (!have_identity)
382 ask_filename(pw, "Enter file in which the key is");
383 if (stat(identity_file, &st) < 0)
384 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000385 if ((r = sshkey_load_public(identity_file, &k, NULL)) != 0)
386 k = load_identity(identity_file);
Damien Miller44b25042010-07-02 13:35:01 +1000387 switch (convert_format) {
388 case FMT_RFC4716:
389 do_convert_to_ssh2(pw, k);
390 break;
391 case FMT_PKCS8:
392 do_convert_to_pkcs8(k);
393 break;
394 case FMT_PEM:
395 do_convert_to_pem(k);
396 break;
397 default:
398 fatal("%s: unknown key format %d", __func__, convert_format);
399 }
400 exit(0);
401}
402
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000403/*
404 * This is almost exactly the bignum1 encoding, but with 32 bit for length
405 * instead of 16.
406 */
Damien Miller44b25042010-07-02 13:35:01 +1000407static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000408buffer_get_bignum_bits(struct sshbuf *b, BIGNUM *value)
Damien Miller874d77b2000-10-14 16:23:11 +1100409{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000410 u_int bytes, bignum_bits;
411 int r;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000412
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000413 if ((r = sshbuf_get_u32(b, &bignum_bits)) != 0)
414 fatal("%s: buffer error: %s", __func__, ssh_err(r));
415 bytes = (bignum_bits + 7) / 8;
416 if (sshbuf_len(b) < bytes)
417 fatal("%s: input buffer too small: need %d have %zu",
418 __func__, bytes, sshbuf_len(b));
419 if (BN_bin2bn(sshbuf_ptr(b), bytes, value) == NULL)
420 fatal("%s: BN_bin2bn failed", __func__);
421 if ((r = sshbuf_consume(b, bytes)) != 0)
422 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100423}
424
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000425static struct sshkey *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000426do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100427{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000428 struct sshbuf *b;
429 struct sshkey *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100430 char *type, *cipher;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000431 u_char e1, e2, e3, *sig = NULL, data[] = "abcde12345";
432 int r, rlen, ktype;
433 u_int magic, i1, i2, i3, i4;
434 size_t slen;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000435 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100436
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000437 if ((b = sshbuf_from(blob, blen)) == NULL)
438 fatal("%s: sshbuf_from failed", __func__);
439 if ((r = sshbuf_get_u32(b, &magic)) != 0)
440 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100441
Damien Miller874d77b2000-10-14 16:23:11 +1100442 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000443 error("bad magic 0x%x != 0x%x", magic,
444 SSH_COM_PRIVATE_KEY_MAGIC);
445 sshbuf_free(b);
Damien Miller874d77b2000-10-14 16:23:11 +1100446 return NULL;
447 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000448 if ((r = sshbuf_get_u32(b, &i1)) != 0 ||
449 (r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
450 (r = sshbuf_get_cstring(b, &cipher, NULL)) != 0 ||
451 (r = sshbuf_get_u32(b, &i2)) != 0 ||
452 (r = sshbuf_get_u32(b, &i3)) != 0 ||
453 (r = sshbuf_get_u32(b, &i4)) != 0)
454 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller80163902007-01-05 16:30:16 +1100455 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100456 if (strcmp(cipher, "none") != 0) {
457 error("unsupported cipher %s", cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000458 free(cipher);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000459 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +1000460 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100461 return NULL;
462 }
Darren Tuckera627d422013-06-02 07:31:17 +1000463 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100464
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000465 if (strstr(type, "dsa")) {
466 ktype = KEY_DSA;
467 } else if (strstr(type, "rsa")) {
468 ktype = KEY_RSA;
469 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000470 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +1000471 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100472 return NULL;
473 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000474 if ((key = sshkey_new_private(ktype)) == NULL)
475 fatal("key_new_private failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000476 free(type);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000477
478 switch (key->type) {
479 case KEY_DSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000480 buffer_get_bignum_bits(b, key->dsa->p);
481 buffer_get_bignum_bits(b, key->dsa->g);
482 buffer_get_bignum_bits(b, key->dsa->q);
483 buffer_get_bignum_bits(b, key->dsa->pub_key);
484 buffer_get_bignum_bits(b, key->dsa->priv_key);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000485 break;
486 case KEY_RSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000487 if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
488 (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
489 (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
490 fatal("%s: buffer error: %s", __func__, ssh_err(r));
491 e = e1;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000492 debug("e %lx", e);
493 if (e < 30) {
494 e <<= 8;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000495 e += e2;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000496 debug("e %lx", e);
497 e <<= 8;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000498 e += e3;
Ben Lindstrom34f91882001-06-25 04:47:54 +0000499 debug("e %lx", e);
500 }
501 if (!BN_set_word(key->rsa->e, e)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000502 sshbuf_free(b);
503 sshkey_free(key);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000504 return NULL;
505 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000506 buffer_get_bignum_bits(b, key->rsa->d);
507 buffer_get_bignum_bits(b, key->rsa->n);
508 buffer_get_bignum_bits(b, key->rsa->iqmp);
509 buffer_get_bignum_bits(b, key->rsa->q);
510 buffer_get_bignum_bits(b, key->rsa->p);
511 if ((r = rsa_generate_additional_parameters(key->rsa)) != 0)
512 fatal("generate RSA parameters failed: %s", ssh_err(r));
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000513 break;
514 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000515 rlen = sshbuf_len(b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000516 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000517 error("do_convert_private_ssh2_from_blob: "
518 "remaining bytes in key blob %d", rlen);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000519 sshbuf_free(b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000520
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000521 /* try the key */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000522 if (sshkey_sign(key, &sig, &slen, data, sizeof(data), 0) != 0 ||
523 sshkey_verify(key, sig, slen, data, sizeof(data), 0) != 0) {
524 sshkey_free(key);
525 free(sig);
526 return NULL;
527 }
Darren Tuckera627d422013-06-02 07:31:17 +1000528 free(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100529 return key;
530}
531
Damien Miller8056a9d2006-03-15 12:05:40 +1100532static int
533get_line(FILE *fp, char *line, size_t len)
534{
535 int c;
536 size_t pos = 0;
537
538 line[0] = '\0';
539 while ((c = fgetc(fp)) != EOF) {
540 if (pos >= len - 1) {
541 fprintf(stderr, "input line too long.\n");
542 exit(1);
543 }
Damien Miller90967402006-03-26 14:07:26 +1100544 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100545 case '\r':
546 c = fgetc(fp);
547 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
548 fprintf(stderr, "unget: %s\n", strerror(errno));
549 exit(1);
550 }
551 return pos;
552 case '\n':
553 return pos;
554 }
555 line[pos++] = c;
556 line[pos] = '\0';
557 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100558 /* We reached EOF */
559 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100560}
561
Ben Lindstrombba81212001-06-25 05:01:22 +0000562static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000563do_convert_from_ssh2(struct passwd *pw, struct sshkey **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000564{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000565 int r, blen, escaped = 0;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000566 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100567 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000568 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000569 char encoded[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000570 FILE *fp;
571
Damien Millerba3420a2010-06-26 09:39:07 +1000572 if ((fp = fopen(identity_file, "r")) == NULL)
573 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000574 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100575 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
Damien Miller746d1a62013-07-18 16:13:02 +1000576 if (blen > 0 && line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000577 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000578 if (strncmp(line, "----", 4) == 0 ||
579 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100580 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000581 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000582 if (strstr(line, " END ") != NULL) {
583 break;
584 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000585 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000586 continue;
587 }
Damien Miller30c3d422000-05-09 11:02:59 +1000588 if (escaped) {
589 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000590 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000591 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000592 }
Damien Millereba71ba2000-04-29 23:57:08 +1000593 strlcat(encoded, line, sizeof(encoded));
594 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000595 len = strlen(encoded);
596 if (((len % 4) == 3) &&
597 (encoded[len-1] == '=') &&
598 (encoded[len-2] == '=') &&
599 (encoded[len-3] == '='))
600 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100601 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000602 if (blen < 0) {
603 fprintf(stderr, "uudecode failed.\n");
604 exit(1);
605 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000606 if (*private)
607 *k = do_convert_private_ssh2_from_blob(blob, blen);
608 else if ((r = sshkey_from_blob(blob, blen, k)) != 0) {
609 fprintf(stderr, "decode blob failed: %s\n", ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100610 exit(1);
611 }
Damien Miller44b25042010-07-02 13:35:01 +1000612 fclose(fp);
613}
614
615static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000616do_convert_from_pkcs8(struct sshkey **k, int *private)
Damien Miller44b25042010-07-02 13:35:01 +1000617{
618 EVP_PKEY *pubkey;
619 FILE *fp;
620
621 if ((fp = fopen(identity_file, "r")) == NULL)
622 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
623 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
624 fatal("%s: %s is not a recognised public key format", __func__,
625 identity_file);
626 }
627 fclose(fp);
628 switch (EVP_PKEY_type(pubkey->type)) {
629 case EVP_PKEY_RSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000630 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
631 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000632 (*k)->type = KEY_RSA;
633 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
634 break;
635 case EVP_PKEY_DSA:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000636 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
637 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000638 (*k)->type = KEY_DSA;
639 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
640 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000641#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000642 case EVP_PKEY_EC:
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000643 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
644 fatal("sshkey_new failed");
Damien Millereb8b60e2010-08-31 22:41:14 +1000645 (*k)->type = KEY_ECDSA;
646 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000647 (*k)->ecdsa_nid = sshkey_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000648 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000649#endif
Damien Miller44b25042010-07-02 13:35:01 +1000650 default:
651 fatal("%s: unsupported pubkey type %d", __func__,
652 EVP_PKEY_type(pubkey->type));
653 }
654 EVP_PKEY_free(pubkey);
655 return;
656}
657
658static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000659do_convert_from_pem(struct sshkey **k, int *private)
Damien Miller44b25042010-07-02 13:35:01 +1000660{
661 FILE *fp;
662 RSA *rsa;
663#ifdef notyet
664 DSA *dsa;
665#endif
666
667 if ((fp = fopen(identity_file, "r")) == NULL)
668 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
669 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000670 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
671 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000672 (*k)->type = KEY_RSA;
673 (*k)->rsa = rsa;
674 fclose(fp);
675 return;
676 }
677#if notyet /* OpenSSH 0.9.8 lacks this function */
678 rewind(fp);
679 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000680 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
681 fatal("sshkey_new failed");
Damien Miller44b25042010-07-02 13:35:01 +1000682 (*k)->type = KEY_DSA;
683 (*k)->dsa = dsa;
684 fclose(fp);
685 return;
686 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000687 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000688#endif
689 fatal("%s: unrecognised raw private key format", __func__);
690}
691
692static void
693do_convert_from(struct passwd *pw)
694{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000695 struct sshkey *k = NULL;
696 int r, private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000697 struct stat st;
698
699 if (!have_identity)
700 ask_filename(pw, "Enter file in which the key is");
701 if (stat(identity_file, &st) < 0)
702 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
703
704 switch (convert_format) {
705 case FMT_RFC4716:
706 do_convert_from_ssh2(pw, &k, &private);
707 break;
708 case FMT_PKCS8:
709 do_convert_from_pkcs8(&k, &private);
710 break;
711 case FMT_PEM:
712 do_convert_from_pem(&k, &private);
713 break;
714 default:
715 fatal("%s: unknown key format %d", __func__, convert_format);
716 }
717
718 if (!private)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000719 if ((r = sshkey_write(k, stdout)) == 0)
720 ok = 1;
Damien Miller44b25042010-07-02 13:35:01 +1000721 if (ok)
722 fprintf(stdout, "\n");
723 else {
724 switch (k->type) {
725 case KEY_DSA:
726 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
727 NULL, 0, NULL, NULL);
728 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000729#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000730 case KEY_ECDSA:
731 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
732 NULL, 0, NULL, NULL);
733 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000734#endif
Damien Miller44b25042010-07-02 13:35:01 +1000735 case KEY_RSA:
736 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
737 NULL, 0, NULL, NULL);
738 break;
739 default:
740 fatal("%s: unsupported key type %s", __func__,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000741 sshkey_type(k));
Damien Miller44b25042010-07-02 13:35:01 +1000742 }
743 }
744
Damien Miller874d77b2000-10-14 16:23:11 +1100745 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100746 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100747 exit(1);
748 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000749 sshkey_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000750 exit(0);
751}
Damien Miller1f0311c2014-05-15 14:24:09 +1000752#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000753
Ben Lindstrombba81212001-06-25 05:01:22 +0000754static void
Damien Millereba71ba2000-04-29 23:57:08 +1000755do_print_public(struct passwd *pw)
756{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000757 struct sshkey *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000758 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000759 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000760
761 if (!have_identity)
762 ask_filename(pw, "Enter file in which the key is");
763 if (stat(identity_file, &st) < 0) {
764 perror(identity_file);
765 exit(1);
766 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000767 prv = load_identity(identity_file);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000768 if ((r = sshkey_write(prv, stdout)) != 0)
769 fprintf(stderr, "key_write failed: %s", ssh_err(r));
770 sshkey_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000771 fprintf(stdout, "\n");
772 exit(0);
773}
774
Ben Lindstromcd392282001-07-04 03:44:03 +0000775static void
Damien Miller757f34e2010-08-05 13:05:31 +1000776do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000777{
Damien Miller7ea845e2010-02-12 09:21:02 +1100778#ifdef ENABLE_PKCS11
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000779 struct sshkey **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100780 int i, nkeys;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000781 enum sshkey_fp_rep rep;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000782 int fptype;
Damien Millerec77c952013-01-09 15:58:00 +1100783 char *fp, *ra;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000784
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000785 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
786 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
Damien Miller1422c082013-01-09 16:44:54 +1100787
Damien Miller7ea845e2010-02-12 09:21:02 +1100788 pkcs11_init(0);
789 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
790 if (nkeys <= 0)
791 fatal("cannot read public key from pkcs11");
792 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100793 if (print_fingerprint) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000794 fp = sshkey_fingerprint(keys[i], fptype, rep);
795 ra = sshkey_fingerprint(keys[i], fingerprint_hash,
Damien Millerec77c952013-01-09 15:58:00 +1100796 SSH_FP_RANDOMART);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000797 printf("%u %s %s (PKCS11 key)\n", sshkey_size(keys[i]),
798 fp, sshkey_type(keys[i]));
Damien Millerec77c952013-01-09 15:58:00 +1100799 if (log_level >= SYSLOG_LEVEL_VERBOSE)
800 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000801 free(ra);
802 free(fp);
Damien Millerec77c952013-01-09 15:58:00 +1100803 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000804 (void) sshkey_write(keys[i], stdout); /* XXX check */
Damien Millerec77c952013-01-09 15:58:00 +1100805 fprintf(stdout, "\n");
806 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000807 sshkey_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000808 }
Darren Tuckera627d422013-06-02 07:31:17 +1000809 free(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100810 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000811 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100812#else
813 fatal("no pkcs11 support");
814#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000815}
Ben Lindstromcd392282001-07-04 03:44:03 +0000816
Ben Lindstrombba81212001-06-25 05:01:22 +0000817static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100818do_fingerprint(struct passwd *pw)
819{
Damien Miller98c7ad62000-03-09 21:27:49 +1100820 FILE *f;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000821 struct sshkey *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000822 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000823 int r, i, skip = 0, num = 0, invalid = 1;
824 enum sshkey_fp_rep rep;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000825 int fptype;
Damien Miller95def091999-11-25 00:26:21 +1100826 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100827
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000828 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
829 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
Damien Miller95def091999-11-25 00:26:21 +1100830 if (!have_identity)
831 ask_filename(pw, "Enter file in which the key is");
832 if (stat(identity_file, &st) < 0) {
833 perror(identity_file);
834 exit(1);
835 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000836 if ((r = sshkey_load_public(identity_file, &public, &comment)) != 0)
837 error("Error loading public key \"%s\": %s",
838 identity_file, ssh_err(r));
839 else {
840 fp = sshkey_fingerprint(public, fptype, rep);
841 ra = sshkey_fingerprint(public, fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000842 SSH_FP_RANDOMART);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000843 printf("%u %s %s (%s)\n", sshkey_size(public), fp, comment,
844 sshkey_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000845 if (log_level >= SYSLOG_LEVEL_VERBOSE)
846 printf("%s\n", ra);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000847 sshkey_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +1000848 free(comment);
849 free(ra);
850 free(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100851 exit(0);
852 }
Damien Miller40b59852006-06-13 13:00:25 +1000853 if (comment) {
Darren Tuckera627d422013-06-02 07:31:17 +1000854 free(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000855 comment = NULL;
856 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100857
Damien Millerba3420a2010-06-26 09:39:07 +1000858 if ((f = fopen(identity_file, "r")) == NULL)
859 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100860
Damien Millerba3420a2010-06-26 09:39:07 +1000861 while (fgets(line, sizeof(line), f)) {
862 if ((cp = strchr(line, '\n')) == NULL) {
863 error("line %d too long: %.40s...",
864 num + 1, line);
865 skip = 1;
866 continue;
Damien Miller95def091999-11-25 00:26:21 +1100867 }
Damien Millerba3420a2010-06-26 09:39:07 +1000868 num++;
869 if (skip) {
870 skip = 0;
871 continue;
872 }
873 *cp = '\0';
874
875 /* Skip leading whitespace, empty and comment lines. */
876 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
877 ;
878 if (!*cp || *cp == '\n' || *cp == '#')
879 continue;
880 i = strtol(cp, &ep, 10);
881 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
882 int quoted = 0;
883 comment = cp;
884 for (; *cp && (quoted || (*cp != ' ' &&
885 *cp != '\t')); cp++) {
886 if (*cp == '\\' && cp[1] == '"')
887 cp++; /* Skip both */
888 else if (*cp == '"')
889 quoted = !quoted;
890 }
891 if (!*cp)
892 continue;
893 *cp++ = '\0';
894 }
895 ep = cp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000896 if ((public = sshkey_new(KEY_RSA1)) == NULL)
897 fatal("sshkey_new failed");
898 if ((r = sshkey_read(public, &cp)) != 0) {
Damien Millerba3420a2010-06-26 09:39:07 +1000899 cp = ep;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000900 sshkey_free(public);
901 if ((public = sshkey_new(KEY_UNSPEC)) == NULL)
902 fatal("sshkey_new failed");
903 if ((r = sshkey_read(public, &cp)) != 0) {
904 sshkey_free(public);
Damien Millerba3420a2010-06-26 09:39:07 +1000905 continue;
906 }
907 }
908 comment = *cp ? cp : comment;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000909 fp = sshkey_fingerprint(public, fptype, rep);
910 ra = sshkey_fingerprint(public, fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000911 SSH_FP_RANDOMART);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000912 printf("%u %s %s (%s)\n", sshkey_size(public), fp,
913 comment ? comment : "no comment", sshkey_type(public));
Damien Millerba3420a2010-06-26 09:39:07 +1000914 if (log_level >= SYSLOG_LEVEL_VERBOSE)
915 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000916 free(ra);
917 free(fp);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000918 sshkey_free(public);
Damien Millerba3420a2010-06-26 09:39:07 +1000919 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100920 }
Damien Millerba3420a2010-06-26 09:39:07 +1000921 fclose(f);
922
Damien Miller98c7ad62000-03-09 21:27:49 +1100923 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100924 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100925 exit(1);
926 }
Damien Miller95def091999-11-25 00:26:21 +1100927 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100928}
929
Damien Miller4b42d7f2005-03-01 21:48:35 +1100930static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000931do_gen_all_hostkeys(struct passwd *pw)
932{
933 struct {
934 char *key_type;
935 char *key_type_display;
936 char *path;
937 } key_types[] = {
938 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
939 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
940 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100941#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000942 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100943#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100944 { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
Damien Miller58f1baf2011-05-05 14:06:15 +1000945 { NULL, NULL, NULL }
946 };
947
948 int first = 0;
949 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000950 struct sshkey *private, *public;
Damien Miller58f1baf2011-05-05 14:06:15 +1000951 char comment[1024];
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000952 int i, type, fd, r;
Damien Miller58f1baf2011-05-05 14:06:15 +1000953 FILE *f;
954
955 for (i = 0; key_types[i].key_type; i++) {
956 if (stat(key_types[i].path, &st) == 0)
957 continue;
958 if (errno != ENOENT) {
959 printf("Could not stat %s: %s", key_types[i].path,
960 strerror(errno));
961 first = 0;
962 continue;
963 }
964
965 if (first == 0) {
966 first = 1;
967 printf("%s: generating new host keys: ", __progname);
968 }
969 printf("%s ", key_types[i].key_type_display);
970 fflush(stdout);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000971 type = sshkey_type_from_name(key_types[i].key_type);
Damien Miller58f1baf2011-05-05 14:06:15 +1000972 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
973 bits = 0;
974 type_bits_valid(type, &bits);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000975 if ((r = sshkey_generate(type, bits, &private)) != 0) {
976 fprintf(stderr, "key_generate failed: %s\n",
977 ssh_err(r));
Damien Miller58f1baf2011-05-05 14:06:15 +1000978 first = 0;
979 continue;
980 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000981 if ((r = sshkey_from_private(private, &public)) != 0)
982 fatal("sshkey_from_private failed: %s", ssh_err(r));
Damien Miller58f1baf2011-05-05 14:06:15 +1000983 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
984 hostname);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000985 if ((r = sshkey_save_private(private, identity_file, "",
986 comment, use_new_format, new_format_cipher, rounds)) != 0) {
987 printf("Saving key \"%s\" failed: %s\n", identity_file,
988 ssh_err(r));
989 sshkey_free(private);
990 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +1000991 first = 0;
992 continue;
993 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000994 sshkey_free(private);
Damien Miller58f1baf2011-05-05 14:06:15 +1000995 strlcat(identity_file, ".pub", sizeof(identity_file));
996 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
997 if (fd == -1) {
998 printf("Could not save your public key in %s\n",
999 identity_file);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001000 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +10001001 first = 0;
1002 continue;
1003 }
1004 f = fdopen(fd, "w");
1005 if (f == NULL) {
1006 printf("fdopen %s failed\n", identity_file);
doug@openbsd.org7df88182014-08-21 01:08:52 +00001007 close(fd);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001008 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +10001009 first = 0;
1010 continue;
1011 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001012 if (!sshkey_write(public, f)) {
Damien Miller58f1baf2011-05-05 14:06:15 +10001013 fprintf(stderr, "write key failed\n");
doug@openbsd.org7df88182014-08-21 01:08:52 +00001014 fclose(f);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001015 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +10001016 first = 0;
1017 continue;
1018 }
1019 fprintf(f, " %s\n", comment);
1020 fclose(f);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001021 sshkey_free(public);
Damien Miller58f1baf2011-05-05 14:06:15 +10001022
1023 }
1024 if (first != 0)
1025 printf("\n");
1026}
1027
1028static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001029printhost(FILE *f, const char *name, struct sshkey *public,
1030 int ca, int revoked, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +11001031{
Darren Tucker0f7e9102008-06-08 12:54:29 +10001032 if (print_fingerprint) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001033 enum sshkey_fp_rep rep;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001034 int fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +10001035 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +10001036
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001037 fptype = print_bubblebabble ?
1038 SSH_DIGEST_SHA1 : fingerprint_hash;
1039 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001040 fp = sshkey_fingerprint(public, fptype, rep);
1041 ra = sshkey_fingerprint(public, fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001042 SSH_FP_RANDOMART);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001043 printf("%u %s %s (%s)\n", sshkey_size(public), fp, name,
1044 sshkey_type(public));
Damien Miller81dec052008-07-14 11:28:29 +10001045 if (log_level >= SYSLOG_LEVEL_VERBOSE)
1046 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10001047 free(ra);
1048 free(fp);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001049 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001050 int r;
1051
Darren Tucker0f7e9102008-06-08 12:54:29 +10001052 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
1053 fatal("hash_host failed");
Damien Miller4a1d3d52014-07-03 21:24:40 +10001054 fprintf(f, "%s%s%s ", ca ? CA_MARKER " " : "",
1055 revoked ? REVOKE_MARKER " " : "" , name);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001056 if ((r = sshkey_write(public, f)) != 0)
1057 fatal("key_write failed: %s", ssh_err(r));
Darren Tucker0f7e9102008-06-08 12:54:29 +10001058 fprintf(f, "\n");
1059 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001060}
1061
1062static void
1063do_known_hosts(struct passwd *pw, const char *name)
1064{
1065 FILE *in, *out = stdout;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001066 struct sshkey *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001067 char *cp, *cp2, *kp, *kp2;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001068 char line[16*1024], tmp[PATH_MAX], old[PATH_MAX];
Damien Millercb2fbb22008-02-10 22:24:55 +11001069 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001070 int r, ca, revoked;
Damien Miller66085482013-09-14 09:45:03 +10001071 int found_key = 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001072
1073 if (!have_identity) {
1074 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1075 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1076 sizeof(identity_file))
1077 fatal("Specified known hosts path too long");
Darren Tuckera627d422013-06-02 07:31:17 +10001078 free(cp);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001079 have_identity = 1;
1080 }
1081 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +10001082 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001083
Damien Miller4a1d3d52014-07-03 21:24:40 +10001084 /* XXX this code is a mess; refactor -djm */
Damien Miller4b42d7f2005-03-01 21:48:35 +11001085 /*
1086 * Find hosts goes to stdout, hash and deletions happen in-place
1087 * A corner case is ssh-keygen -HF foo, which should go to stdout
1088 */
1089 if (!find_host && (hash_hosts || delete_host)) {
1090 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1091 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1092 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1093 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1094 fatal("known_hosts path too long");
1095 umask(077);
1096 if ((c = mkstemp(tmp)) == -1)
1097 fatal("mkstemp: %s", strerror(errno));
1098 if ((out = fdopen(c, "w")) == NULL) {
1099 c = errno;
1100 unlink(tmp);
1101 fatal("fdopen: %s", strerror(c));
1102 }
1103 inplace = 1;
1104 }
1105
1106 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001107 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001108 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001109 skip = 1;
1110 invalid = 1;
1111 continue;
1112 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001113 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001114 if (skip) {
1115 skip = 0;
1116 continue;
1117 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001118 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001119
1120 /* Skip leading whitespace, empty and comment lines. */
1121 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1122 ;
1123 if (!*cp || *cp == '\n' || *cp == '#') {
1124 if (inplace)
1125 fprintf(out, "%s\n", cp);
1126 continue;
1127 }
Damien Miller4a1d3d52014-07-03 21:24:40 +10001128 /* Check whether this is a CA key or revocation marker */
Damien Miller0a80ca12010-02-27 07:55:05 +11001129 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1130 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1131 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1132 ca = 1;
1133 cp += sizeof(CA_MARKER);
1134 } else
1135 ca = 0;
Damien Miller4a1d3d52014-07-03 21:24:40 +10001136 if (strncasecmp(cp, REVOKE_MARKER,
1137 sizeof(REVOKE_MARKER) - 1) == 0 &&
1138 (cp[sizeof(REVOKE_MARKER) - 1] == ' ' ||
1139 cp[sizeof(REVOKE_MARKER) - 1] == '\t')) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001140 revoked = 1;
Damien Miller4a1d3d52014-07-03 21:24:40 +10001141 cp += sizeof(REVOKE_MARKER);
1142 } else
1143 revoked = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001144
Damien Miller4b42d7f2005-03-01 21:48:35 +11001145 /* Find the end of the host name portion. */
1146 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1147 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001148
Damien Miller4b42d7f2005-03-01 21:48:35 +11001149 if (*kp == '\0' || *(kp + 1) == '\0') {
1150 error("line %d missing key: %.40s...",
1151 num, line);
1152 invalid = 1;
1153 continue;
1154 }
1155 *kp++ = '\0';
1156 kp2 = kp;
1157
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001158 if ((pub = sshkey_new(KEY_RSA1)) == NULL)
1159 fatal("sshkey_new failed");
1160 if ((r = sshkey_read(pub, &kp)) != 0) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001161 kp = kp2;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001162 sshkey_free(pub);
1163 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
1164 fatal("sshkey_new failed");
1165 if ((r = sshkey_read(pub, &kp)) != 0) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001166 error("line %d invalid key: %.40s...",
1167 num, line);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001168 sshkey_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001169 invalid = 1;
1170 continue;
1171 }
1172 }
1173
1174 if (*cp == HASH_DELIM) {
1175 if (find_host || delete_host) {
1176 cp2 = host_hash(name, cp, strlen(cp));
1177 if (cp2 == NULL) {
1178 error("line %d: invalid hashed "
1179 "name: %.64s...", num, line);
1180 invalid = 1;
1181 continue;
1182 }
1183 c = (strcmp(cp2, cp) == 0);
1184 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001185 if (!quiet)
1186 printf("# Host %s found: "
1187 "line %d type %s%s\n", name,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001188 num, sshkey_type(pub),
Damien Miller4a1d3d52014-07-03 21:24:40 +10001189 ca ? " (CA key)" :
1190 revoked? " (revoked)" : "");
1191 printhost(out, cp, pub, ca, revoked, 0);
Damien Miller66085482013-09-14 09:45:03 +10001192 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001193 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001194 if (delete_host) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001195 if (!c || ca || revoked) {
1196 printhost(out, cp, pub,
1197 ca, revoked, 0);
1198 } else {
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001199 printf("# Host %s found: "
1200 "line %d type %s\n", name,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001201 num, sshkey_type(pub));
Damien Miller4a1d3d52014-07-03 21:24:40 +10001202 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001203 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001204 } else if (hash_hosts)
Damien Miller4a1d3d52014-07-03 21:24:40 +10001205 printhost(out, cp, pub, ca, revoked, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001206 } else {
1207 if (find_host || delete_host) {
1208 c = (match_hostname(name, cp,
1209 strlen(cp)) == 1);
1210 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001211 if (!quiet)
1212 printf("# Host %s found: "
1213 "line %d type %s%s\n", name,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001214 num, sshkey_type(pub),
Damien Miller66085482013-09-14 09:45:03 +10001215 ca ? " (CA key)" : "");
Damien Miller4a1d3d52014-07-03 21:24:40 +10001216 printhost(out, name, pub, ca, revoked,
1217 hash_hosts && !(ca || revoked));
Damien Miller66085482013-09-14 09:45:03 +10001218 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001219 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001220 if (delete_host) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001221 if (!c || ca || revoked) {
1222 printhost(out, cp, pub,
1223 ca, revoked, 0);
1224 } else {
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001225 printf("# Host %s found: "
1226 "line %d type %s\n", name,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001227 num, sshkey_type(pub));
Damien Miller4a1d3d52014-07-03 21:24:40 +10001228 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001229 }
Damien Miller4a1d3d52014-07-03 21:24:40 +10001230 } else if (hash_hosts && (ca || revoked)) {
1231 /* Don't hash CA and revoked keys' hostnames */
1232 printhost(out, cp, pub, ca, revoked, 0);
1233 has_unhashed = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001234 } else if (hash_hosts) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001235 /* Hash each hostname separately */
Darren Tucker47eede72005-03-14 23:08:12 +11001236 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001237 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001238 cp2 = strsep(&cp, ",")) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001239 if (strcspn(cp2, "*?!") !=
Damien Miller0a80ca12010-02-27 07:55:05 +11001240 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001241 fprintf(stderr, "Warning: "
1242 "ignoring host name with "
1243 "metacharacters: %.64s\n",
1244 cp2);
Damien Miller4a1d3d52014-07-03 21:24:40 +10001245 printhost(out, cp2, pub, ca,
1246 revoked, 0);
1247 has_unhashed = 1;
1248 } else {
1249 printhost(out, cp2, pub, ca,
1250 revoked, 1);
1251 }
Damien Miller89eac802005-03-02 12:33:04 +11001252 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001253 }
1254 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001255 sshkey_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001256 }
1257 fclose(in);
1258
1259 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001260 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001261 identity_file);
1262 if (inplace) {
1263 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001264 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001265 fclose(out);
1266 unlink(tmp);
1267 }
1268 exit(1);
1269 }
1270
1271 if (inplace) {
1272 fclose(out);
1273
1274 /* Backup existing file */
1275 if (unlink(old) == -1 && errno != ENOENT)
1276 fatal("unlink %.100s: %s", old, strerror(errno));
1277 if (link(identity_file, old) == -1)
1278 fatal("link %.100s to %.100s: %s", identity_file, old,
1279 strerror(errno));
1280 /* Move new one into place */
1281 if (rename(tmp, identity_file) == -1) {
1282 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1283 strerror(errno));
1284 unlink(tmp);
1285 unlink(old);
1286 exit(1);
1287 }
1288
1289 fprintf(stderr, "%s updated.\n", identity_file);
1290 fprintf(stderr, "Original contents retained as %s\n", old);
1291 if (has_unhashed) {
1292 fprintf(stderr, "WARNING: %s contains unhashed "
1293 "entries\n", old);
1294 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001295 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001296 }
1297 }
1298
Damien Miller66085482013-09-14 09:45:03 +10001299 exit (find_host && !found_key);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001300}
1301
Damien Miller95def091999-11-25 00:26:21 +11001302/*
1303 * Perform changing a passphrase. The argument is the passwd structure
1304 * for the current user.
1305 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001306static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001307do_change_passphrase(struct passwd *pw)
1308{
Damien Miller95def091999-11-25 00:26:21 +11001309 char *comment;
1310 char *old_passphrase, *passphrase1, *passphrase2;
1311 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001312 struct sshkey *private;
1313 int r;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001314
Damien Miller95def091999-11-25 00:26:21 +11001315 if (!have_identity)
1316 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001317 if (stat(identity_file, &st) < 0) {
1318 perror(identity_file);
1319 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320 }
Damien Miller95def091999-11-25 00:26:21 +11001321 /* Try to load the file with empty passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001322 r = sshkey_load_private(identity_file, "", &private, &comment);
1323 if (r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
Damien Miller95def091999-11-25 00:26:21 +11001324 if (identity_passphrase)
1325 old_passphrase = xstrdup(identity_passphrase);
1326 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001327 old_passphrase =
1328 read_passphrase("Enter old passphrase: ",
1329 RP_ALLOW_STDIN);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001330 r = sshkey_load_private(identity_file, old_passphrase,
1331 &private, &comment);
Damien Millera5103f42014-02-04 11:20:14 +11001332 explicit_bzero(old_passphrase, strlen(old_passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001333 free(old_passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001334 if (r != 0)
1335 goto badkey;
1336 } else if (r != 0) {
1337 badkey:
1338 fprintf(stderr, "Failed to load key \"%s\": %s\n",
1339 identity_file, ssh_err(r));
1340 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001341 }
1342 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343
Damien Miller95def091999-11-25 00:26:21 +11001344 /* Ask the new passphrase (twice). */
1345 if (identity_new_passphrase) {
1346 passphrase1 = xstrdup(identity_new_passphrase);
1347 passphrase2 = NULL;
1348 } else {
1349 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001350 read_passphrase("Enter new passphrase (empty for no "
1351 "passphrase): ", RP_ALLOW_STDIN);
1352 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001353 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001354
1355 /* Verify that they are the same. */
1356 if (strcmp(passphrase1, passphrase2) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001357 explicit_bzero(passphrase1, strlen(passphrase1));
1358 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001359 free(passphrase1);
1360 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001361 printf("Pass phrases do not match. Try again.\n");
1362 exit(1);
1363 }
1364 /* Destroy the other copy. */
Damien Millera5103f42014-02-04 11:20:14 +11001365 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001366 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001367 }
1368
1369 /* Save the file using the new passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001370 if ((r = sshkey_save_private(private, identity_file, passphrase1,
1371 comment, use_new_format, new_format_cipher, rounds)) != 0) {
1372 printf("Saving key \"%s\" failed: %s.\n",
1373 identity_file, ssh_err(r));
Damien Millera5103f42014-02-04 11:20:14 +11001374 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001375 free(passphrase1);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001376 sshkey_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001377 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001378 exit(1);
1379 }
1380 /* Destroy the passphrase and the copy of the key in memory. */
Damien Millera5103f42014-02-04 11:20:14 +11001381 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001382 free(passphrase1);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001383 sshkey_free(private); /* Destroys contents */
Darren Tuckera627d422013-06-02 07:31:17 +10001384 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001385
1386 printf("Your identification has been saved with the new passphrase.\n");
1387 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001388}
1389
Damien Miller37876e92003-05-15 10:19:46 +10001390/*
1391 * Print the SSHFP RR.
1392 */
Damien Millercb314822006-03-26 13:48:01 +11001393static int
1394do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001395{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001396 struct sshkey *public;
Damien Miller37876e92003-05-15 10:19:46 +10001397 char *comment = NULL;
1398 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001399 int r;
Damien Miller37876e92003-05-15 10:19:46 +10001400
Damien Millercb314822006-03-26 13:48:01 +11001401 if (fname == NULL)
Damien Miller5bb88332013-07-18 16:13:37 +10001402 fatal("%s: no filename", __func__);
Damien Millercb314822006-03-26 13:48:01 +11001403 if (stat(fname, &st) < 0) {
1404 if (errno == ENOENT)
1405 return 0;
1406 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001407 exit(1);
1408 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001409 if ((r = sshkey_load_public(fname, &public, &comment)) != 0) {
1410 printf("Failed to read v2 public key from \"%s\": %s.\n",
1411 fname, ssh_err(r));
1412 exit(1);
Damien Miller37876e92003-05-15 10:19:46 +10001413 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001414 export_dns_rr(hname, public, stdout, print_generic);
1415 sshkey_free(public);
1416 free(comment);
1417 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001418}
Damien Miller37876e92003-05-15 10:19:46 +10001419
Damien Miller95def091999-11-25 00:26:21 +11001420/*
1421 * Change the comment of a private key file.
1422 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001423static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424do_change_comment(struct passwd *pw)
1425{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001426 char new_comment[1024], *comment, *passphrase;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001427 struct sshkey *private;
1428 struct sshkey *public;
Damien Miller95def091999-11-25 00:26:21 +11001429 struct stat st;
1430 FILE *f;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001431 int r, fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432
Damien Miller95def091999-11-25 00:26:21 +11001433 if (!have_identity)
1434 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001435 if (stat(identity_file, &st) < 0) {
1436 perror(identity_file);
1437 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001439 if ((r = sshkey_load_private(identity_file, "",
1440 &private, &comment)) == 0)
1441 passphrase = xstrdup("");
1442 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
1443 printf("Cannot load private key \"%s\": %s.\n",
1444 identity_file, ssh_err(r));
1445 exit(1);
1446 } else {
Damien Miller95def091999-11-25 00:26:21 +11001447 if (identity_passphrase)
1448 passphrase = xstrdup(identity_passphrase);
1449 else if (identity_new_passphrase)
1450 passphrase = xstrdup(identity_new_passphrase);
1451 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001452 passphrase = read_passphrase("Enter passphrase: ",
1453 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001454 /* Try to load using the passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001455 if ((r = sshkey_load_private(identity_file, passphrase,
1456 &private, &comment)) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001457 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001458 free(passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001459 printf("Cannot load private key \"%s\": %s.\n",
1460 identity_file, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001461 exit(1);
1462 }
1463 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001464 if (private->type != KEY_RSA1) {
1465 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001466 sshkey_free(private);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001467 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001468 }
Damien Miller95def091999-11-25 00:26:21 +11001469 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470
Damien Miller95def091999-11-25 00:26:21 +11001471 if (identity_comment) {
1472 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1473 } else {
1474 printf("Enter new comment: ");
1475 fflush(stdout);
1476 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
Damien Millera5103f42014-02-04 11:20:14 +11001477 explicit_bzero(passphrase, strlen(passphrase));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001478 sshkey_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001479 exit(1);
1480 }
Damien Miller14b017d2007-09-17 16:09:15 +10001481 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001482 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483
Damien Miller95def091999-11-25 00:26:21 +11001484 /* Save the file using the new passphrase. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001485 if ((r = sshkey_save_private(private, identity_file, passphrase,
1486 new_comment, use_new_format, new_format_cipher, rounds)) != 0) {
1487 printf("Saving key \"%s\" failed: %s\n",
1488 identity_file, ssh_err(r));
Damien Millera5103f42014-02-04 11:20:14 +11001489 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001490 free(passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001491 sshkey_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001492 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001493 exit(1);
1494 }
Damien Millera5103f42014-02-04 11:20:14 +11001495 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001496 free(passphrase);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001497 if ((r = sshkey_from_private(private, &public)) != 0)
1498 fatal("key_from_private failed: %s", ssh_err(r));
1499 sshkey_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500
Damien Miller95def091999-11-25 00:26:21 +11001501 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001502 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1503 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001504 printf("Could not save your public key in %s\n", identity_file);
1505 exit(1);
1506 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001507 f = fdopen(fd, "w");
1508 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001509 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001510 exit(1);
1511 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001512 if ((r = sshkey_write(public, f)) != 0)
1513 fprintf(stderr, "write key failed: %s\n", ssh_err(r));
1514 sshkey_free(public);
Damien Millereba71ba2000-04-29 23:57:08 +10001515 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001516 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517
Darren Tuckera627d422013-06-02 07:31:17 +10001518 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001519
1520 printf("The comment in your key file has been changed.\n");
1521 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001522}
1523
Damien Miller0a80ca12010-02-27 07:55:05 +11001524static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001525fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001526{
1527 char from[32], to[32];
1528 static char ret[64];
1529 time_t tt;
1530 struct tm *tm;
1531
1532 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001533 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001534 return "forever";
1535
Damien Millerf2b70ca2010-03-05 07:39:35 +11001536 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001537 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001538 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001539 tm = localtime(&tt);
1540 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1541 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001542 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001543 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001544 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001545 tm = localtime(&tt);
1546 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1547 }
1548
Damien Millerf2b70ca2010-03-05 07:39:35 +11001549 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001550 snprintf(ret, sizeof(ret), "before %s", to);
1551 return ret;
1552 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001553 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001554 snprintf(ret, sizeof(ret), "after %s", from);
1555 return ret;
1556 }
1557
1558 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1559 return ret;
1560}
1561
1562static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001563add_flag_option(struct sshbuf *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001564{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001565 int r;
1566
Damien Miller0a80ca12010-02-27 07:55:05 +11001567 debug3("%s: %s", __func__, name);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001568 if ((r = sshbuf_put_cstring(c, name)) != 0 ||
1569 (r = sshbuf_put_string(c, NULL, 0)) != 0)
1570 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001571}
1572
1573static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001574add_string_option(struct sshbuf *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001575{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001576 struct sshbuf *b;
1577 int r;
Damien Miller0a80ca12010-02-27 07:55:05 +11001578
1579 debug3("%s: %s=%s", __func__, name, value);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001580 if ((b = sshbuf_new()) == NULL)
1581 fatal("%s: sshbuf_new failed", __func__);
1582 if ((r = sshbuf_put_cstring(b, value)) != 0 ||
1583 (r = sshbuf_put_cstring(c, name)) != 0 ||
1584 (r = sshbuf_put_stringb(c, b)) != 0)
1585 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001586
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001587 sshbuf_free(b);
Damien Miller0a80ca12010-02-27 07:55:05 +11001588}
1589
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001590#define OPTIONS_CRITICAL 1
1591#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001592static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001593prepare_options_buf(struct sshbuf *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001594{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001595 sshbuf_reset(c);
Damien Miller1da63882010-08-05 13:03:51 +10001596 if ((which & OPTIONS_CRITICAL) != 0 &&
1597 certflags_command != NULL)
1598 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001599 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001600 (certflags_flags & CERTOPT_X_FWD) != 0)
1601 add_flag_option(c, "permit-X11-forwarding");
1602 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001603 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001604 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001605 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1606 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001607 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001608 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1609 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001610 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001611 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1612 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001613 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001614 if ((which & OPTIONS_CRITICAL) != 0 &&
1615 certflags_src_addr != NULL)
1616 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001617}
1618
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001619static struct sshkey *
Damien Miller757f34e2010-08-05 13:05:31 +10001620load_pkcs11_key(char *path)
1621{
1622#ifdef ENABLE_PKCS11
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001623 struct sshkey **keys = NULL, *public, *private = NULL;
1624 int r, i, nkeys;
Damien Miller757f34e2010-08-05 13:05:31 +10001625
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001626 if ((r = sshkey_load_public(path, &public, NULL)) != 0)
1627 fatal("Couldn't load CA public key \"%s\": %s",
1628 path, ssh_err(r));
Damien Miller757f34e2010-08-05 13:05:31 +10001629
1630 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1631 debug3("%s: %d keys", __func__, nkeys);
1632 if (nkeys <= 0)
1633 fatal("cannot read public key from pkcs11");
1634 for (i = 0; i < nkeys; i++) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001635 if (sshkey_equal_public(public, keys[i])) {
Damien Miller757f34e2010-08-05 13:05:31 +10001636 private = keys[i];
1637 continue;
1638 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001639 sshkey_free(keys[i]);
Damien Miller757f34e2010-08-05 13:05:31 +10001640 }
Darren Tuckera627d422013-06-02 07:31:17 +10001641 free(keys);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001642 sshkey_free(public);
Damien Miller757f34e2010-08-05 13:05:31 +10001643 return private;
1644#else
1645 fatal("no pkcs11 support");
1646#endif /* ENABLE_PKCS11 */
1647}
1648
Damien Miller0a80ca12010-02-27 07:55:05 +11001649static void
1650do_ca_sign(struct passwd *pw, int argc, char **argv)
1651{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001652 int r, i, fd;
Damien Miller0a80ca12010-02-27 07:55:05 +11001653 u_int n;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001654 struct sshkey *ca, *public;
Damien Miller0a80ca12010-02-27 07:55:05 +11001655 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1656 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001657 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001658
Damien Miller4e270b02010-04-16 15:56:21 +10001659 if (key_type_name != NULL) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001660 switch (sshkey_type_from_name(key_type_name)) {
Damien Miller4e270b02010-04-16 15:56:21 +10001661 case KEY_RSA_CERT_V00:
1662 case KEY_DSA_CERT_V00:
1663 v00 = 1;
1664 break;
1665 case KEY_UNSPEC:
1666 if (strcasecmp(key_type_name, "v00") == 0) {
1667 v00 = 1;
1668 break;
1669 } else if (strcasecmp(key_type_name, "v01") == 0)
1670 break;
1671 /* FALLTHROUGH */
1672 default:
1673 fprintf(stderr, "unknown key type %s\n", key_type_name);
1674 exit(1);
1675 }
1676 }
1677
Damien Miller1f0311c2014-05-15 14:24:09 +10001678#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001679 pkcs11_init(1);
Damien Miller1f0311c2014-05-15 14:24:09 +10001680#endif
Damien Miller757f34e2010-08-05 13:05:31 +10001681 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1682 if (pkcs11provider != NULL) {
1683 if ((ca = load_pkcs11_key(tmp)) == NULL)
1684 fatal("No PKCS#11 key matching %s found", ca_key_path);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001685 } else
1686 ca = load_identity(tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001687 free(tmp);
Damien Miller757f34e2010-08-05 13:05:31 +10001688
Damien Miller0a80ca12010-02-27 07:55:05 +11001689 for (i = 0; i < argc; i++) {
1690 /* Split list of principals */
1691 n = 0;
1692 if (cert_principals != NULL) {
1693 otmp = tmp = xstrdup(cert_principals);
1694 plist = NULL;
1695 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1696 plist = xrealloc(plist, n + 1, sizeof(*plist));
1697 if (*(plist[n] = xstrdup(cp)) == '\0')
1698 fatal("Empty principal name");
1699 }
Darren Tuckera627d422013-06-02 07:31:17 +10001700 free(otmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001701 }
1702
1703 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001704 if ((r = sshkey_load_public(tmp, &public, &comment)) != 0)
1705 fatal("%s: unable to open \"%s\": %s",
1706 __func__, tmp, ssh_err(r));
Damien Millereb8b60e2010-08-31 22:41:14 +10001707 if (public->type != KEY_RSA && public->type != KEY_DSA &&
Damien Miller5be9d9e2013-12-07 11:24:01 +11001708 public->type != KEY_ECDSA && public->type != KEY_ED25519)
Damien Miller0a80ca12010-02-27 07:55:05 +11001709 fatal("%s: key \"%s\" type %s cannot be certified",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001710 __func__, tmp, sshkey_type(public));
Damien Miller0a80ca12010-02-27 07:55:05 +11001711
1712 /* Prepare certificate to sign */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001713 if ((r = sshkey_to_certified(public, v00)) != 0)
1714 fatal("Could not upgrade key %s to certificate: %s",
1715 tmp, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001716 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001717 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001718 public->cert->key_id = xstrdup(cert_key_id);
1719 public->cert->nprincipals = n;
1720 public->cert->principals = plist;
1721 public->cert->valid_after = cert_valid_from;
1722 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001723 if (v00) {
Damien Miller86687062014-07-02 15:28:02 +10001724 prepare_options_buf(public->cert->critical,
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001725 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1726 } else {
Damien Miller86687062014-07-02 15:28:02 +10001727 prepare_options_buf(public->cert->critical,
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001728 OPTIONS_CRITICAL);
Damien Miller86687062014-07-02 15:28:02 +10001729 prepare_options_buf(public->cert->extensions,
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001730 OPTIONS_EXTENSIONS);
1731 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001732 if ((r = sshkey_from_private(ca,
1733 &public->cert->signature_key)) != 0)
1734 fatal("key_from_private (ca key): %s", ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001735
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001736 if (sshkey_certify(public, ca) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001737 fatal("Couldn't not certify key %s", tmp);
1738
1739 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1740 *cp = '\0';
1741 xasprintf(&out, "%s-cert.pub", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001742 free(tmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001743
1744 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1745 fatal("Could not open \"%s\" for writing: %s", out,
1746 strerror(errno));
1747 if ((f = fdopen(fd, "w")) == NULL)
1748 fatal("%s: fdopen: %s", __func__, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001749 if ((r = sshkey_write(public, f)) != 0)
1750 fatal("Could not write certified key to %s: %s",
1751 out, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001752 fprintf(f, " %s\n", comment);
1753 fclose(f);
1754
Damien Miller4e270b02010-04-16 15:56:21 +10001755 if (!quiet) {
1756 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001757 "valid %s", sshkey_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001758 out, public->cert->key_id,
1759 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001760 cert_principals != NULL ? " for " : "",
1761 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001762 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001763 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001764
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001765 sshkey_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001766 free(out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001767 }
Damien Miller1f0311c2014-05-15 14:24:09 +10001768#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001769 pkcs11_terminate();
Damien Miller1f0311c2014-05-15 14:24:09 +10001770#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11001771 exit(0);
1772}
1773
1774static u_int64_t
1775parse_relative_time(const char *s, time_t now)
1776{
1777 int64_t mul, secs;
1778
1779 mul = *s == '-' ? -1 : 1;
1780
1781 if ((secs = convtime(s + 1)) == -1)
1782 fatal("Invalid relative certificate time %s", s);
1783 if (mul == -1 && secs > now)
1784 fatal("Certificate time %s cannot be represented", s);
1785 return now + (u_int64_t)(secs * mul);
1786}
1787
1788static u_int64_t
1789parse_absolute_time(const char *s)
1790{
1791 struct tm tm;
1792 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001793 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001794
Damien Miller2ca342b2010-03-03 12:14:15 +11001795 /*
1796 * POSIX strptime says "The application shall ensure that there
1797 * is white-space or other non-alphanumeric characters between
1798 * any two conversion specifications" so arrange things this way.
1799 */
1800 switch (strlen(s)) {
1801 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001802 fmt = "%Y-%m-%d";
1803 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001804 break;
1805 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001806 fmt = "%Y-%m-%dT%H:%M:%S";
1807 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001808 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1809 break;
1810 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001811 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001812 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001813
Damien Miller1d2c4562014-02-04 11:18:20 +11001814 memset(&tm, 0, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001815 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001816 fatal("Invalid certificate time %s", s);
1817 if ((tt = mktime(&tm)) < 0)
1818 fatal("Certificate time %s cannot be represented", s);
1819 return (u_int64_t)tt;
1820}
1821
1822static void
1823parse_cert_times(char *timespec)
1824{
1825 char *from, *to;
1826 time_t now = time(NULL);
1827 int64_t secs;
1828
1829 /* +timespec relative to now */
1830 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1831 if ((secs = convtime(timespec + 1)) == -1)
1832 fatal("Invalid relative certificate life %s", timespec);
1833 cert_valid_to = now + secs;
1834 /*
1835 * Backdate certificate one minute to avoid problems on hosts
1836 * with poorly-synchronised clocks.
1837 */
1838 cert_valid_from = ((now - 59)/ 60) * 60;
1839 return;
1840 }
1841
1842 /*
1843 * from:to, where
1844 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1845 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1846 */
1847 from = xstrdup(timespec);
1848 to = strchr(from, ':');
1849 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001850 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001851 *to++ = '\0';
1852
1853 if (*from == '-' || *from == '+')
1854 cert_valid_from = parse_relative_time(from, now);
1855 else
1856 cert_valid_from = parse_absolute_time(from);
1857
1858 if (*to == '-' || *to == '+')
Damien Miller5b01b0d2013-10-23 16:31:31 +11001859 cert_valid_to = parse_relative_time(to, now);
Damien Miller0a80ca12010-02-27 07:55:05 +11001860 else
1861 cert_valid_to = parse_absolute_time(to);
1862
1863 if (cert_valid_to <= cert_valid_from)
1864 fatal("Empty certificate validity interval");
Darren Tuckera627d422013-06-02 07:31:17 +10001865 free(from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001866}
1867
1868static void
Damien Miller4e270b02010-04-16 15:56:21 +10001869add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001870{
1871 char *val;
1872
Damien Miller044f4a62011-05-05 14:14:08 +10001873 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001874 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001875 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001876 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001877 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001878 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001879 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001880 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001881 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001882 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001883 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001884 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001885 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001886 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001887 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001888 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001889 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001890 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001891 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001892 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001893 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001894 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001895 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1896 val = opt + 14;
1897 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001898 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001899 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001900 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001901 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001902 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1903 val = opt + 15;
1904 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001905 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001906 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001907 fatal("source-address already specified");
1908 if (addr_match_cidr_list(NULL, val) != 0)
1909 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001910 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001911 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001912 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001913}
1914
Ben Lindstrombba81212001-06-25 05:01:22 +00001915static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001916show_options(struct sshbuf *optbuf, int v00, int in_critical)
Damien Millerd834d352010-06-26 09:48:02 +10001917{
Damien Miller633de332014-05-15 13:48:26 +10001918 char *name, *arg;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001919 struct sshbuf *options, *option = NULL;
1920 int r;
Damien Millerd834d352010-06-26 09:48:02 +10001921
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001922 if ((options = sshbuf_fromb(optbuf)) == NULL)
1923 fatal("%s: sshbuf_fromb failed", __func__);
1924 while (sshbuf_len(options) != 0) {
1925 sshbuf_free(option);
1926 option = NULL;
1927 if ((r = sshbuf_get_cstring(options, &name, NULL)) != 0 ||
1928 (r = sshbuf_froms(options, &option)) != 0)
1929 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd834d352010-06-26 09:48:02 +10001930 printf(" %s", name);
1931 if ((v00 || !in_critical) &&
1932 (strcmp(name, "permit-X11-forwarding") == 0 ||
1933 strcmp(name, "permit-agent-forwarding") == 0 ||
1934 strcmp(name, "permit-port-forwarding") == 0 ||
1935 strcmp(name, "permit-pty") == 0 ||
1936 strcmp(name, "permit-user-rc") == 0))
1937 printf("\n");
1938 else if ((v00 || in_critical) &&
1939 (strcmp(name, "force-command") == 0 ||
1940 strcmp(name, "source-address") == 0)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001941 if ((r = sshbuf_get_cstring(option, &arg, NULL)) != 0)
1942 fatal("%s: buffer error: %s",
1943 __func__, ssh_err(r));
Damien Miller633de332014-05-15 13:48:26 +10001944 printf(" %s\n", arg);
1945 free(arg);
Damien Millerd834d352010-06-26 09:48:02 +10001946 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001947 printf(" UNKNOWN OPTION (len %zu)\n",
1948 sshbuf_len(option));
1949 sshbuf_reset(option);
Damien Millerd834d352010-06-26 09:48:02 +10001950 }
Darren Tuckera627d422013-06-02 07:31:17 +10001951 free(name);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001952 if (sshbuf_len(option) != 0)
Damien Millerd834d352010-06-26 09:48:02 +10001953 fatal("Option corrupt: extra data at end");
1954 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001955 sshbuf_free(option);
1956 sshbuf_free(options);
Damien Millerd834d352010-06-26 09:48:02 +10001957}
1958
1959static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001960do_show_cert(struct passwd *pw)
1961{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001962 struct sshkey *key;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001963 struct stat st;
1964 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001965 u_int i, v00;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001966 int r;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001967
1968 if (!have_identity)
1969 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001970 if (stat(identity_file, &st) < 0)
1971 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001972 if ((r = sshkey_load_public(identity_file, &key, NULL)) != 0)
1973 fatal("Cannot load public key \"%s\": %s",
1974 identity_file, ssh_err(r));
1975 if (!sshkey_is_cert(key))
Damien Millerf2b70ca2010-03-05 07:39:35 +11001976 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001977 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1978
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001979 key_fp = sshkey_fingerprint(key, fingerprint_hash, SSH_FP_DEFAULT);
1980 ca_fp = sshkey_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001981 fingerprint_hash, SSH_FP_DEFAULT);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001982
1983 printf("%s:\n", identity_file);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001984 printf(" Type: %s %s certificate\n", sshkey_ssh_name(key),
1985 sshkey_cert_type(key));
1986 printf(" Public key: %s %s\n", sshkey_type(key), key_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001987 printf(" Signing CA: %s %s\n",
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001988 sshkey_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001989 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001990 if (!v00) {
1991 printf(" Serial: %llu\n",
1992 (unsigned long long)key->cert->serial);
1993 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001994 printf(" Valid: %s\n",
1995 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1996 printf(" Principals: ");
1997 if (key->cert->nprincipals == 0)
1998 printf("(none)\n");
1999 else {
2000 for (i = 0; i < key->cert->nprincipals; i++)
2001 printf("\n %s",
2002 key->cert->principals[i]);
2003 printf("\n");
2004 }
Damien Miller4e270b02010-04-16 15:56:21 +10002005 printf(" Critical Options: ");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002006 if (sshbuf_len(key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11002007 printf("(none)\n");
2008 else {
2009 printf("\n");
Damien Miller86687062014-07-02 15:28:02 +10002010 show_options(key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11002011 }
Damien Miller4e270b02010-04-16 15:56:21 +10002012 if (!v00) {
2013 printf(" Extensions: ");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002014 if (sshbuf_len(key->cert->extensions) == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10002015 printf("(none)\n");
2016 else {
2017 printf("\n");
Damien Miller86687062014-07-02 15:28:02 +10002018 show_options(key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10002019 }
2020 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002021 exit(0);
2022}
2023
2024static void
Damien Millerf3747bf2013-01-18 11:44:04 +11002025load_krl(const char *path, struct ssh_krl **krlp)
2026{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002027 struct sshbuf *krlbuf;
2028 int r, fd;
Damien Millerf3747bf2013-01-18 11:44:04 +11002029
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002030 if ((krlbuf = sshbuf_new()) == NULL)
2031 fatal("sshbuf_new failed");
Damien Millerf3747bf2013-01-18 11:44:04 +11002032 if ((fd = open(path, O_RDONLY)) == -1)
2033 fatal("open %s: %s", path, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002034 if ((r = sshkey_load_file(fd, krlbuf)) != 0)
2035 fatal("Unable to load KRL: %s", ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002036 close(fd);
2037 /* XXX check sigs */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002038 if ((r = ssh_krl_from_blob(krlbuf, krlp, NULL, 0)) != 0 ||
Damien Millerf3747bf2013-01-18 11:44:04 +11002039 *krlp == NULL)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002040 fatal("Invalid KRL file: %s", ssh_err(r));
2041 sshbuf_free(krlbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11002042}
2043
2044static void
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002045update_krl_from_file(struct passwd *pw, const char *file,
2046 const struct sshkey *ca, struct ssh_krl *krl)
Damien Millerf3747bf2013-01-18 11:44:04 +11002047{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002048 struct sshkey *key = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11002049 u_long lnum = 0;
2050 char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES];
2051 unsigned long long serial, serial2;
2052 int i, was_explicit_key, was_sha1, r;
2053 FILE *krl_spec;
2054
2055 path = tilde_expand_filename(file, pw->pw_uid);
2056 if (strcmp(path, "-") == 0) {
2057 krl_spec = stdin;
2058 free(path);
2059 path = xstrdup("(standard input)");
2060 } else if ((krl_spec = fopen(path, "r")) == NULL)
2061 fatal("fopen %s: %s", path, strerror(errno));
2062
2063 if (!quiet)
2064 printf("Revoking from %s\n", path);
2065 while (read_keyfile_line(krl_spec, path, line, sizeof(line),
2066 &lnum) == 0) {
2067 was_explicit_key = was_sha1 = 0;
2068 cp = line + strspn(line, " \t");
2069 /* Trim trailing space, comments and strip \n */
2070 for (i = 0, r = -1; cp[i] != '\0'; i++) {
2071 if (cp[i] == '#' || cp[i] == '\n') {
2072 cp[i] = '\0';
2073 break;
2074 }
2075 if (cp[i] == ' ' || cp[i] == '\t') {
2076 /* Remember the start of a span of whitespace */
2077 if (r == -1)
2078 r = i;
2079 } else
2080 r = -1;
2081 }
2082 if (r != -1)
2083 cp[r] = '\0';
2084 if (*cp == '\0')
2085 continue;
2086 if (strncasecmp(cp, "serial:", 7) == 0) {
2087 if (ca == NULL) {
Damien Millerd234afb2013-08-21 02:42:58 +10002088 fatal("revoking certificates by serial number "
Damien Millerf3747bf2013-01-18 11:44:04 +11002089 "requires specification of a CA key");
2090 }
2091 cp += 7;
2092 cp = cp + strspn(cp, " \t");
2093 errno = 0;
2094 serial = strtoull(cp, &ep, 0);
2095 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2096 fatal("%s:%lu: invalid serial \"%s\"",
2097 path, lnum, cp);
2098 if (errno == ERANGE && serial == ULLONG_MAX)
2099 fatal("%s:%lu: serial out of range",
2100 path, lnum);
2101 serial2 = serial;
2102 if (*ep == '-') {
2103 cp = ep + 1;
2104 errno = 0;
2105 serial2 = strtoull(cp, &ep, 0);
2106 if (*cp == '\0' || *ep != '\0')
2107 fatal("%s:%lu: invalid serial \"%s\"",
2108 path, lnum, cp);
2109 if (errno == ERANGE && serial2 == ULLONG_MAX)
2110 fatal("%s:%lu: serial out of range",
2111 path, lnum);
2112 if (serial2 <= serial)
2113 fatal("%s:%lu: invalid serial range "
2114 "%llu:%llu", path, lnum,
2115 (unsigned long long)serial,
2116 (unsigned long long)serial2);
2117 }
2118 if (ssh_krl_revoke_cert_by_serial_range(krl,
2119 ca, serial, serial2) != 0) {
2120 fatal("%s: revoke serial failed",
2121 __func__);
2122 }
2123 } else if (strncasecmp(cp, "id:", 3) == 0) {
2124 if (ca == NULL) {
Damien Millerd5d9d7b2013-08-21 02:43:27 +10002125 fatal("revoking certificates by key ID "
Damien Millerf3747bf2013-01-18 11:44:04 +11002126 "requires specification of a CA key");
2127 }
2128 cp += 3;
2129 cp = cp + strspn(cp, " \t");
2130 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2131 fatal("%s: revoke key ID failed", __func__);
2132 } else {
2133 if (strncasecmp(cp, "key:", 4) == 0) {
2134 cp += 4;
2135 cp = cp + strspn(cp, " \t");
2136 was_explicit_key = 1;
2137 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2138 cp += 5;
2139 cp = cp + strspn(cp, " \t");
2140 was_sha1 = 1;
2141 } else {
2142 /*
2143 * Just try to process the line as a key.
2144 * Parsing will fail if it isn't.
2145 */
2146 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002147 if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
Damien Millerf3747bf2013-01-18 11:44:04 +11002148 fatal("key_new");
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002149 if ((r = sshkey_read(key, &cp)) != 0)
2150 fatal("%s:%lu: invalid key: %s",
2151 path, lnum, ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002152 if (was_explicit_key)
2153 r = ssh_krl_revoke_key_explicit(krl, key);
2154 else if (was_sha1)
2155 r = ssh_krl_revoke_key_sha1(krl, key);
2156 else
2157 r = ssh_krl_revoke_key(krl, key);
2158 if (r != 0)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002159 fatal("%s: revoke key failed: %s",
2160 __func__, ssh_err(r));
2161 sshkey_free(key);
Damien Millerf3747bf2013-01-18 11:44:04 +11002162 }
2163 }
2164 if (strcmp(path, "-") != 0)
2165 fclose(krl_spec);
Damien Miller0d6771b2013-04-23 15:23:24 +10002166 free(path);
Damien Millerf3747bf2013-01-18 11:44:04 +11002167}
2168
2169static void
2170do_gen_krl(struct passwd *pw, int updating, int argc, char **argv)
2171{
2172 struct ssh_krl *krl;
2173 struct stat sb;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002174 struct sshkey *ca = NULL;
2175 int fd, i, r;
Damien Millerf3747bf2013-01-18 11:44:04 +11002176 char *tmp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002177 struct sshbuf *kbuf;
Damien Millerf3747bf2013-01-18 11:44:04 +11002178
2179 if (*identity_file == '\0')
2180 fatal("KRL generation requires an output file");
2181 if (stat(identity_file, &sb) == -1) {
2182 if (errno != ENOENT)
2183 fatal("Cannot access KRL \"%s\": %s",
2184 identity_file, strerror(errno));
2185 if (updating)
2186 fatal("KRL \"%s\" does not exist", identity_file);
2187 }
2188 if (ca_key_path != NULL) {
2189 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002190 if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
2191 fatal("Cannot load CA public key %s: %s",
2192 tmp, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10002193 free(tmp);
Damien Millerf3747bf2013-01-18 11:44:04 +11002194 }
2195
2196 if (updating)
2197 load_krl(identity_file, &krl);
2198 else if ((krl = ssh_krl_init()) == NULL)
2199 fatal("couldn't create KRL");
2200
2201 if (cert_serial != 0)
2202 ssh_krl_set_version(krl, cert_serial);
2203 if (identity_comment != NULL)
2204 ssh_krl_set_comment(krl, identity_comment);
2205
2206 for (i = 0; i < argc; i++)
2207 update_krl_from_file(pw, argv[i], ca, krl);
2208
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002209 if ((kbuf = sshbuf_new()) == NULL)
2210 fatal("sshbuf_new failed");
2211 if (ssh_krl_to_blob(krl, kbuf, NULL, 0) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11002212 fatal("Couldn't generate KRL");
2213 if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2214 fatal("open %s: %s", identity_file, strerror(errno));
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002215 if (atomicio(vwrite, fd, (void *)sshbuf_ptr(kbuf), sshbuf_len(kbuf)) !=
2216 sshbuf_len(kbuf))
Damien Millerf3747bf2013-01-18 11:44:04 +11002217 fatal("write %s: %s", identity_file, strerror(errno));
2218 close(fd);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002219 sshbuf_free(kbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11002220 ssh_krl_free(krl);
Damien Miller0d6771b2013-04-23 15:23:24 +10002221 if (ca != NULL)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002222 sshkey_free(ca);
Damien Millerf3747bf2013-01-18 11:44:04 +11002223}
2224
2225static void
2226do_check_krl(struct passwd *pw, int argc, char **argv)
2227{
2228 int i, r, ret = 0;
2229 char *comment;
2230 struct ssh_krl *krl;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002231 struct sshkey *k;
Damien Millerf3747bf2013-01-18 11:44:04 +11002232
2233 if (*identity_file == '\0')
2234 fatal("KRL checking requires an input file");
2235 load_krl(identity_file, &krl);
2236 for (i = 0; i < argc; i++) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002237 if ((r = sshkey_load_public(argv[i], &k, &comment)) != 0)
2238 fatal("Cannot load public key %s: %s",
2239 argv[i], ssh_err(r));
Damien Millerf3747bf2013-01-18 11:44:04 +11002240 r = ssh_krl_check_key(krl, k);
2241 printf("%s%s%s%s: %s\n", argv[i],
2242 *comment ? " (" : "", comment, *comment ? ")" : "",
2243 r == 0 ? "ok" : "REVOKED");
2244 if (r != 0)
2245 ret = 1;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002246 sshkey_free(k);
Damien Millerf3747bf2013-01-18 11:44:04 +11002247 free(comment);
2248 }
2249 ssh_krl_free(krl);
2250 exit(ret);
2251}
2252
2253static void
Damien Miller431f66b1999-11-21 18:31:57 +11002254usage(void)
2255{
Damien Millerf0858de2014-04-20 13:01:30 +10002256 fprintf(stderr,
2257 "usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1]\n"
2258 " [-N new_passphrase] [-C comment] [-f output_keyfile]\n"
2259 " ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]\n"
2260 " ssh-keygen -i [-m key_format] [-f input_keyfile]\n"
2261 " ssh-keygen -e [-m key_format] [-f input_keyfile]\n"
2262 " ssh-keygen -y [-f input_keyfile]\n"
2263 " ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]\n"
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002264 " ssh-keygen -l [-E fingerprint_hash] [-f input_keyfile]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10002265 " ssh-keygen -B [-f input_keyfile]\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002266#ifdef ENABLE_PKCS11
Damien Millerf0858de2014-04-20 13:01:30 +10002267 fprintf(stderr,
2268 " ssh-keygen -D pkcs11\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002269#endif
Damien Millerf0858de2014-04-20 13:01:30 +10002270 fprintf(stderr,
2271 " ssh-keygen -F hostname [-f known_hosts_file] [-l]\n"
2272 " ssh-keygen -H [-f known_hosts_file]\n"
2273 " ssh-keygen -R hostname [-f known_hosts_file]\n"
2274 " ssh-keygen -r hostname [-f input_keyfile] [-g]\n"
2275 " ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]\n"
2276 " ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]\n"
2277 " [-j start_line] [-K checkpt] [-W generator]\n"
2278 " ssh-keygen -s ca_key -I certificate_identity [-h] [-n principals]\n"
2279 " [-O option] [-V validity_interval] [-z serial_number] file ...\n"
2280 " ssh-keygen -L [-f input_keyfile]\n"
2281 " ssh-keygen -A\n"
2282 " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
2283 " file ...\n"
2284 " ssh-keygen -Q -f krl_file file ...\n");
Damien Miller95def091999-11-25 00:26:21 +11002285 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11002286}
2287
Damien Miller95def091999-11-25 00:26:21 +11002288/*
2289 * Main program for key management.
2290 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002291int
Damien Millerdf8b7db2007-01-05 16:22:57 +11002292main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002293{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00002294 char dotsshdir[PATH_MAX], comment[1024], *passphrase1, *passphrase2;
Damien Miller390d0562011-10-18 16:05:19 +11002295 char *checkpoint = NULL;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00002296 char out_file[PATH_MAX], *rr_hostname = NULL, *ep;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002297 struct sshkey *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11002298 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11002299 struct stat st;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002300 int r, opt, type, fd;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002301 u_int32_t memory = 0, generator_wanted = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002302 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002303 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
Damien Millerdfceafe2012-07-06 13:44:19 +10002304 unsigned long start_lineno = 0, lines_to_process = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002305 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11002306 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10002307 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002308
Damien Miller95def091999-11-25 00:26:21 +11002309 extern int optind;
2310 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002311
Darren Tuckerce321d82005-10-03 18:11:24 +10002312 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2313 sanitise_stdfd();
2314
Darren Tucker9ac56e92007-01-14 10:19:59 +11002315 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10002316
Damien Miller72ef7c12015-01-15 02:21:31 +11002317#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10002318 OpenSSL_add_all_algorithms();
Damien Miller72ef7c12015-01-15 02:21:31 +11002319#endif
Damien Millerdf8b7db2007-01-05 16:22:57 +11002320 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10002321
Kevin Steves3a881912002-07-20 19:05:40 +00002322 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10002323
Damien Miller5428f641999-11-25 11:54:57 +11002324 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11002325 pw = getpwuid(getuid());
2326 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +10002327 printf("No user exists for uid %lu\n", (u_long)getuid());
Damien Miller95def091999-11-25 00:26:21 +11002328 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002329 }
Damien Millereba71ba2000-04-29 23:57:08 +10002330 if (gethostname(hostname, sizeof(hostname)) < 0) {
2331 perror("gethostname");
2332 exit(1);
2333 }
Damien Miller5428f641999-11-25 11:54:57 +11002334
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002335 /* Remaining characters: UYdw */
Damien Millerbcd00ab2013-12-07 10:41:55 +11002336 while ((opt = getopt(argc, argv, "ABHLQXceghiklopquvxy"
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002337 "C:D:E:F:G:I:J:K:M:N:O:P:R:S:T:V:W:Z:"
2338 "a:b:f:g:j:m:n:r:s:t:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11002339 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10002340 case 'A':
2341 gen_all_hostkeys = 1;
2342 break;
Damien Miller95def091999-11-25 00:26:21 +11002343 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10002344 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10002345 if (errstr)
2346 fatal("Bits has bad value %s (%s)",
2347 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11002348 break;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002349 case 'E':
2350 fingerprint_hash = ssh_digest_alg_by_name(optarg);
2351 if (fingerprint_hash == -1)
2352 fatal("Invalid hash algorithm \"%s\"", optarg);
2353 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002354 case 'F':
2355 find_host = 1;
2356 rr_hostname = optarg;
2357 break;
2358 case 'H':
2359 hash_hosts = 1;
2360 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002361 case 'I':
2362 cert_key_id = optarg;
2363 break;
Damien Millerdfceafe2012-07-06 13:44:19 +10002364 case 'J':
2365 lines_to_process = strtoul(optarg, NULL, 10);
2366 break;
2367 case 'j':
2368 start_lineno = strtoul(optarg, NULL, 10);
2369 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002370 case 'R':
2371 delete_host = 1;
2372 rr_hostname = optarg;
2373 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11002374 case 'L':
2375 show_cert = 1;
2376 break;
Damien Miller95def091999-11-25 00:26:21 +11002377 case 'l':
2378 print_fingerprint = 1;
2379 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002380 case 'B':
2381 print_bubblebabble = 1;
2382 break;
Damien Miller44b25042010-07-02 13:35:01 +10002383 case 'm':
2384 if (strcasecmp(optarg, "RFC4716") == 0 ||
2385 strcasecmp(optarg, "ssh2") == 0) {
2386 convert_format = FMT_RFC4716;
2387 break;
2388 }
2389 if (strcasecmp(optarg, "PKCS8") == 0) {
2390 convert_format = FMT_PKCS8;
2391 break;
2392 }
2393 if (strcasecmp(optarg, "PEM") == 0) {
2394 convert_format = FMT_PEM;
2395 break;
2396 }
2397 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002398 case 'n':
2399 cert_principals = optarg;
2400 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002401 case 'o':
2402 use_new_format = 1;
2403 break;
Damien Miller95def091999-11-25 00:26:21 +11002404 case 'p':
2405 change_passphrase = 1;
2406 break;
Damien Miller95def091999-11-25 00:26:21 +11002407 case 'c':
2408 change_comment = 1;
2409 break;
Damien Miller95def091999-11-25 00:26:21 +11002410 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002411 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2412 sizeof(identity_file))
2413 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002414 have_identity = 1;
2415 break;
Damien Miller37876e92003-05-15 10:19:46 +10002416 case 'g':
2417 print_generic = 1;
2418 break;
Damien Miller95def091999-11-25 00:26:21 +11002419 case 'P':
2420 identity_passphrase = optarg;
2421 break;
Damien Miller95def091999-11-25 00:26:21 +11002422 case 'N':
2423 identity_new_passphrase = optarg;
2424 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002425 case 'Q':
2426 check_krl = 1;
2427 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002428 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002429 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002430 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002431 case 'Z':
2432 new_format_cipher = optarg;
2433 break;
Damien Miller95def091999-11-25 00:26:21 +11002434 case 'C':
2435 identity_comment = optarg;
2436 break;
Damien Miller95def091999-11-25 00:26:21 +11002437 case 'q':
2438 quiet = 1;
2439 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002440 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002441 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002442 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002443 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002444 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002445 case 'h':
2446 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002447 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002448 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002449 case 'k':
2450 gen_krl = 1;
2451 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002452 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002453 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002454 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002455 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002456 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002457 case 'y':
2458 print_public = 1;
2459 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002460 case 's':
2461 ca_key_path = optarg;
2462 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002463 case 't':
2464 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002465 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002466 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002467 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002468 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002469 case 'u':
2470 update_krl = 1;
2471 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002472 case 'v':
2473 if (log_level == SYSLOG_LEVEL_INFO)
2474 log_level = SYSLOG_LEVEL_DEBUG1;
2475 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002476 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002477 log_level < SYSLOG_LEVEL_DEBUG3)
2478 log_level++;
2479 }
2480 break;
Damien Miller37876e92003-05-15 10:19:46 +10002481 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002482 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002483 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002484 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002485 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2486 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002487 if (errstr)
2488 fatal("Desired generator has bad value: %s (%s)",
2489 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002490 break;
2491 case 'a':
Damien Millerbcd00ab2013-12-07 10:41:55 +11002492 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002493 if (errstr)
Damien Millerbcd00ab2013-12-07 10:41:55 +11002494 fatal("Invalid number: %s (%s)",
Damien Millerb089fb52005-05-26 12:16:18 +10002495 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002496 break;
Damien Miller72ef7c12015-01-15 02:21:31 +11002497#ifdef WITH_OPENSSL
Darren Tucker019cefe2003-08-02 22:40:07 +10002498 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002499 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002500 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002501 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002502 break;
2503 case 'G':
2504 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002505 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2506 sizeof(out_file))
2507 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002508 break;
2509 case 'T':
2510 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002511 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2512 sizeof(out_file))
2513 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002514 break;
Damien Miller390d0562011-10-18 16:05:19 +11002515 case 'K':
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00002516 if (strlen(optarg) >= PATH_MAX)
Damien Miller390d0562011-10-18 16:05:19 +11002517 fatal("Checkpoint filename too long");
2518 checkpoint = xstrdup(optarg);
2519 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002520 case 'S':
2521 /* XXX - also compare length against bits */
2522 if (BN_hex2bn(&start, optarg) == 0)
2523 fatal("Invalid start point.");
2524 break;
Damien Miller72ef7c12015-01-15 02:21:31 +11002525#endif /* WITH_OPENSSL */
Damien Miller0a80ca12010-02-27 07:55:05 +11002526 case 'V':
2527 parse_cert_times(optarg);
2528 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002529 case 'z':
Damien Miller6f3b3622012-11-14 19:04:33 +11002530 errno = 0;
2531 cert_serial = strtoull(optarg, &ep, 10);
2532 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2533 (errno == ERANGE && cert_serial == ULLONG_MAX))
2534 fatal("Invalid serial number \"%s\"", optarg);
Damien Miller4e270b02010-04-16 15:56:21 +10002535 break;
Damien Miller95def091999-11-25 00:26:21 +11002536 case '?':
2537 default:
2538 usage();
2539 }
2540 }
Darren Tucker06930c72003-12-31 11:34:51 +11002541
2542 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002543 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002544
Damien Miller0a80ca12010-02-27 07:55:05 +11002545 argv += optind;
2546 argc -= optind;
2547
2548 if (ca_key_path != NULL) {
Damien Millerf3747bf2013-01-18 11:44:04 +11002549 if (argc < 1 && !gen_krl) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002550 printf("Too few arguments.\n");
2551 usage();
2552 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002553 } else if (argc > 0 && !gen_krl && !check_krl) {
Damien Miller95def091999-11-25 00:26:21 +11002554 printf("Too many arguments.\n");
2555 usage();
2556 }
2557 if (change_passphrase && change_comment) {
2558 printf("Can only have one of -p and -c.\n");
2559 usage();
2560 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002561 if (print_fingerprint && (delete_host || hash_hosts)) {
Damien Millerec77c952013-01-09 15:58:00 +11002562 printf("Cannot use -l with -H or -R.\n");
Darren Tucker0f7e9102008-06-08 12:54:29 +10002563 usage();
2564 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002565 if (gen_krl) {
2566 do_gen_krl(pw, update_krl, argc, argv);
2567 return (0);
2568 }
2569 if (check_krl) {
2570 do_check_krl(pw, argc, argv);
2571 return (0);
2572 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002573 if (ca_key_path != NULL) {
2574 if (cert_key_id == NULL)
2575 fatal("Must specify key id (-I) when certifying");
2576 do_ca_sign(pw, argc, argv);
2577 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002578 if (show_cert)
2579 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002580 if (delete_host || hash_hosts || find_host)
2581 do_known_hosts(pw, rr_hostname);
Damien Millerec77c952013-01-09 15:58:00 +11002582 if (pkcs11provider != NULL)
2583 do_download(pw);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002584 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002585 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002586 if (change_passphrase)
2587 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002588 if (change_comment)
2589 do_change_comment(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002590#ifdef WITH_OPENSSL
Damien Miller44b25042010-07-02 13:35:01 +10002591 if (convert_to)
2592 do_convert_to(pw);
2593 if (convert_from)
2594 do_convert_from(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002595#endif
Damien Millereba71ba2000-04-29 23:57:08 +10002596 if (print_public)
2597 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002598 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002599 unsigned int n = 0;
2600
2601 if (have_identity) {
2602 n = do_print_resource_record(pw,
2603 identity_file, rr_hostname);
2604 if (n == 0) {
2605 perror(identity_file);
2606 exit(1);
2607 }
2608 exit(0);
2609 } else {
2610
2611 n += do_print_resource_record(pw,
2612 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2613 n += do_print_resource_record(pw,
2614 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
Damien Miller3bde12a2012-06-20 21:51:11 +10002615 n += do_print_resource_record(pw,
2616 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
Damien Miller16cd3922014-05-15 13:45:58 +10002617 n += do_print_resource_record(pw,
2618 _PATH_HOST_ED25519_KEY_FILE, rr_hostname);
Damien Millercb314822006-03-26 13:48:01 +11002619 if (n == 0)
2620 fatal("no keys found.");
2621 exit(0);
2622 }
Damien Miller37876e92003-05-15 10:19:46 +10002623 }
Damien Miller95def091999-11-25 00:26:21 +11002624
Darren Tucker019cefe2003-08-02 22:40:07 +10002625 if (do_gen_candidates) {
2626 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002627
Darren Tucker019cefe2003-08-02 22:40:07 +10002628 if (out == NULL) {
2629 error("Couldn't open modulus candidate file \"%s\": %s",
2630 out_file, strerror(errno));
2631 return (1);
2632 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002633 if (bits == 0)
2634 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002635 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002636 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002637
2638 return (0);
2639 }
2640
2641 if (do_screen_candidates) {
2642 FILE *in;
Damien Miller78d22712013-02-12 11:03:36 +11002643 FILE *out = fopen(out_file, "a");
Darren Tucker019cefe2003-08-02 22:40:07 +10002644
2645 if (have_identity && strcmp(identity_file, "-") != 0) {
2646 if ((in = fopen(identity_file, "r")) == NULL) {
2647 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002648 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002649 strerror(errno));
2650 }
2651 } else
2652 in = stdin;
2653
2654 if (out == NULL) {
2655 fatal("Couldn't open moduli file \"%s\": %s",
2656 out_file, strerror(errno));
2657 }
Damien Millerbcd00ab2013-12-07 10:41:55 +11002658 if (prime_test(in, out, rounds == 0 ? 100 : rounds,
2659 generator_wanted, checkpoint,
Damien Millerdfceafe2012-07-06 13:44:19 +10002660 start_lineno, lines_to_process) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002661 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002662 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002663 }
2664
Damien Miller58f1baf2011-05-05 14:06:15 +10002665 if (gen_all_hostkeys) {
2666 do_gen_all_hostkeys(pw);
2667 return (0);
2668 }
2669
Damien Millerf14be5c2005-11-05 15:15:49 +11002670 if (key_type_name == NULL)
2671 key_type_name = "rsa";
2672
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002673 type = sshkey_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002674 type_bits_valid(type, &bits);
2675
Darren Tucker3af2ac52005-11-29 13:10:24 +11002676 if (!quiet)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002677 printf("Generating public/private %s key pair.\n",
2678 key_type_name);
2679 if ((r = sshkey_generate(type, bits, &private)) != 0) {
Damien Miller9eab9562009-02-22 08:47:02 +11002680 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002681 exit(1);
2682 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002683 if ((r = sshkey_from_private(private, &public)) != 0) {
2684 fprintf(stderr, "key_from_private failed: %s\n", ssh_err(r));
2685 exit(1);
2686 }
Damien Miller95def091999-11-25 00:26:21 +11002687
2688 if (!have_identity)
2689 ask_filename(pw, "Enter file in which to save the key");
2690
Damien Miller788f2122005-11-05 15:14:59 +11002691 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002692 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2693 pw->pw_dir, _PATH_SSH_USER_DIR);
2694 if (strstr(identity_file, dotsshdir) != NULL) {
2695 if (stat(dotsshdir, &st) < 0) {
2696 if (errno != ENOENT) {
2697 error("Could not stat %s: %s", dotsshdir,
2698 strerror(errno));
2699 } else if (mkdir(dotsshdir, 0700) < 0) {
2700 error("Could not create directory '%s': %s",
2701 dotsshdir, strerror(errno));
2702 } else if (!quiet)
2703 printf("Created directory '%s'.\n", dotsshdir);
2704 }
Damien Miller95def091999-11-25 00:26:21 +11002705 }
2706 /* If the file already exists, ask the user to confirm. */
2707 if (stat(identity_file, &st) >= 0) {
2708 char yesno[3];
2709 printf("%s already exists.\n", identity_file);
2710 printf("Overwrite (y/n)? ");
2711 fflush(stdout);
2712 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2713 exit(1);
2714 if (yesno[0] != 'y' && yesno[0] != 'Y')
2715 exit(1);
2716 }
2717 /* Ask for a passphrase (twice). */
2718 if (identity_passphrase)
2719 passphrase1 = xstrdup(identity_passphrase);
2720 else if (identity_new_passphrase)
2721 passphrase1 = xstrdup(identity_new_passphrase);
2722 else {
2723passphrase_again:
2724 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002725 read_passphrase("Enter passphrase (empty for no "
2726 "passphrase): ", RP_ALLOW_STDIN);
2727 passphrase2 = read_passphrase("Enter same passphrase again: ",
2728 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002729 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002730 /*
2731 * The passphrases do not match. Clear them and
2732 * retry.
2733 */
Damien Millera5103f42014-02-04 11:20:14 +11002734 explicit_bzero(passphrase1, strlen(passphrase1));
2735 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002736 free(passphrase1);
2737 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002738 printf("Passphrases do not match. Try again.\n");
2739 goto passphrase_again;
2740 }
2741 /* Clear the other copy of the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002742 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002743 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002744 }
2745
Damien Miller95def091999-11-25 00:26:21 +11002746 if (identity_comment) {
2747 strlcpy(comment, identity_comment, sizeof(comment));
2748 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002749 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002750 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2751 }
2752
2753 /* Save the key with the given passphrase and comment. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002754 if ((r = sshkey_save_private(private, identity_file, passphrase1,
2755 comment, use_new_format, new_format_cipher, rounds)) != 0) {
2756 printf("Saving key \"%s\" failed: %s\n",
2757 identity_file, ssh_err(r));
Damien Millera5103f42014-02-04 11:20:14 +11002758 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002759 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002760 exit(1);
2761 }
2762 /* Clear the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002763 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002764 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002765
2766 /* Clear the private key and the random number generator. */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002767 sshkey_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002768
2769 if (!quiet)
2770 printf("Your identification has been saved in %s.\n", identity_file);
2771
Damien Miller95def091999-11-25 00:26:21 +11002772 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002773 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2774 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002775 printf("Could not save your public key in %s\n", identity_file);
2776 exit(1);
2777 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002778 f = fdopen(fd, "w");
2779 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002780 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002781 exit(1);
2782 }
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002783 if ((r = sshkey_write(public, f)) != 0)
2784 fprintf(stderr, "write key failed: %s\n", ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10002785 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002786 fclose(f);
2787
2788 if (!quiet) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002789 char *fp = sshkey_fingerprint(public, fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002790 SSH_FP_DEFAULT);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002791 char *ra = sshkey_fingerprint(public, fingerprint_hash,
Darren Tucker9c16ac92008-06-13 04:40:35 +10002792 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002793 printf("Your public key has been saved in %s.\n",
2794 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002795 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002796 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002797 printf("The key's randomart image is:\n");
2798 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10002799 free(ra);
2800 free(fp);
Damien Miller95def091999-11-25 00:26:21 +11002801 }
Damien Millereba71ba2000-04-29 23:57:08 +10002802
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00002803 sshkey_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002804 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002805}