blob: 8daea7f766b8a35cf08f62081711ece5a74fa5f6 [file] [log] [blame]
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001/* $OpenBSD: ssh-keygen.c,v 1.251 2014/12/21 22:27:56 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
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 Millereba71ba2000-04-29 23:57:08 +100025
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100029#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000041#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "authfile.h"
43#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110044#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
46#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100047#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110048#include "match.h"
49#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100050#include "dns.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110051#include "ssh.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110052#include "ssh2.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110053#include "ssh-pkcs11.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110054#include "atomicio.h"
55#include "krl.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000056#include "digest.h"
Ben Lindstromcd392282001-07-04 03:44:03 +000057
Damien Miller3f54a9f2005-11-05 14:52:18 +110058/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
59#define DEFAULT_BITS 2048
60#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100061#define DEFAULT_BITS_ECDSA 256
Damien Miller3f54a9f2005-11-05 14:52:18 +110062u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Damien Miller5428f641999-11-25 11:54:57 +110064/*
65 * Flag indicating that we just want to change the passphrase. This can be
66 * set on the command line.
67 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068int change_passphrase = 0;
69
Damien Miller5428f641999-11-25 11:54:57 +110070/*
71 * Flag indicating that we just want to change the comment. This can be set
72 * on the command line.
73 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074int change_comment = 0;
75
76int quiet = 0;
77
Darren Tucker35c45532008-06-13 04:43:15 +100078int log_level = SYSLOG_LEVEL_INFO;
79
Damien Miller4b42d7f2005-03-01 21:48:35 +110080/* Flag indicating that we want to hash a known_hosts file */
81int hash_hosts = 0;
82/* Flag indicating that we want lookup a host in known_hosts file */
83int find_host = 0;
84/* Flag indicating that we want to delete a host from a known_hosts file */
85int delete_host = 0;
86
Damien Millerf2b70ca2010-03-05 07:39:35 +110087/* Flag indicating that we want to show the contents of a certificate */
88int show_cert = 0;
89
Damien Miller10f6f6b1999-11-17 17:29:08 +110090/* Flag indicating that we just want to see the key fingerprint */
91int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000092int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110093
djm@openbsd.org56d1c832014-12-21 22:27:55 +000094/* Hash algorithm to use for fingerprints. */
95int fingerprint_hash = SSH_FP_HASH_DEFAULT;
96
Damien Miller431f66b1999-11-21 18:31:57 +110097/* The identity file name, given on the command line or entered by the user. */
98char identity_file[1024];
99int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
101/* This is set to the passphrase if given on the command line. */
102char *identity_passphrase = NULL;
103
104/* This is set to the new passphrase if given on the command line. */
105char *identity_new_passphrase = NULL;
106
107/* This is set to the new comment if given on the command line. */
108char *identity_comment = NULL;
109
Damien Miller0a80ca12010-02-27 07:55:05 +1100110/* Path to CA key when certifying keys. */
111char *ca_key_path = NULL;
112
Damien Miller4e270b02010-04-16 15:56:21 +1000113/* Certificate serial number */
Damien Miller55aca022012-12-03 11:25:30 +1100114unsigned long long cert_serial = 0;
Damien Miller4e270b02010-04-16 15:56:21 +1000115
Damien Miller0a80ca12010-02-27 07:55:05 +1100116/* Key type when certifying */
117u_int cert_key_type = SSH2_CERT_TYPE_USER;
118
119/* "key ID" of signed key */
120char *cert_key_id = NULL;
121
122/* Comma-separated list of principal names for certifying keys */
123char *cert_principals = NULL;
124
125/* Validity period for certificates */
126u_int64_t cert_valid_from = 0;
127u_int64_t cert_valid_to = ~0ULL;
128
Damien Miller4e270b02010-04-16 15:56:21 +1000129/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000130#define CERTOPT_X_FWD (1)
131#define CERTOPT_AGENT_FWD (1<<1)
132#define CERTOPT_PORT_FWD (1<<2)
133#define CERTOPT_PTY (1<<3)
134#define CERTOPT_USER_RC (1<<4)
135#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
136 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
137u_int32_t certflags_flags = CERTOPT_DEFAULT;
138char *certflags_command = NULL;
139char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100140
Damien Miller44b25042010-07-02 13:35:01 +1000141/* Conversion to/from various formats */
142int convert_to = 0;
143int convert_from = 0;
144enum {
145 FMT_RFC4716,
146 FMT_PKCS8,
147 FMT_PEM
148} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000149int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000150int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100151
Damien Millera41c8b12002-01-22 23:05:08 +1100152char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000153
Damien Miller757f34e2010-08-05 13:05:31 +1000154/* Load key from this PKCS#11 provider */
155char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000156
Damien Millerbcd00ab2013-12-07 10:41:55 +1100157/* Use new OpenSSH private key format when writing SSH2 keys instead of PEM */
158int use_new_format = 0;
159
160/* Cipher for new-format private keys */
161char *new_format_cipher = NULL;
162
163/*
164 * Number of KDF rounds to derive new format keys /
165 * number of primality trials when screening moduli.
166 */
167int rounds = 0;
168
Damien Miller431f66b1999-11-21 18:31:57 +1100169/* argv0 */
170extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171
Damien Millere5c0d522014-07-03 21:24:19 +1000172char hostname[NI_MAXHOST];
Damien Millereba71ba2000-04-29 23:57:08 +1000173
Darren Tucker770fc012004-05-13 16:24:32 +1000174/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000175int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Damien Millerdfceafe2012-07-06 13:44:19 +1000176int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
177 unsigned long);
Darren Tucker770fc012004-05-13 16:24:32 +1000178
Ben Lindstrombba81212001-06-25 05:01:22 +0000179static void
Damien Miller884b63a2011-05-05 14:14:52 +1000180type_bits_valid(int type, u_int32_t *bitsp)
Damien Miller58f1baf2011-05-05 14:06:15 +1000181{
182 u_int maxbits;
183
184 if (type == KEY_UNSPEC) {
185 fprintf(stderr, "unknown key type %s\n", key_type_name);
186 exit(1);
187 }
Damien Miller884b63a2011-05-05 14:14:52 +1000188 if (*bitsp == 0) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000189 if (type == KEY_DSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000190 *bitsp = DEFAULT_BITS_DSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000191 else if (type == KEY_ECDSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000192 *bitsp = DEFAULT_BITS_ECDSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000193 else
Damien Miller884b63a2011-05-05 14:14:52 +1000194 *bitsp = DEFAULT_BITS;
Damien Miller58f1baf2011-05-05 14:06:15 +1000195 }
196 maxbits = (type == KEY_DSA) ?
197 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
Damien Miller884b63a2011-05-05 14:14:52 +1000198 if (*bitsp > maxbits) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000199 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
200 exit(1);
201 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000202#ifdef WITH_OPENSSL
Damien Miller884b63a2011-05-05 14:14:52 +1000203 if (type == KEY_DSA && *bitsp != 1024)
Damien Miller58f1baf2011-05-05 14:06:15 +1000204 fatal("DSA keys must be 1024 bits");
Damien Miller5be9d9e2013-12-07 11:24:01 +1100205 else if (type != KEY_ECDSA && type != KEY_ED25519 && *bitsp < 768)
Damien Miller58f1baf2011-05-05 14:06:15 +1000206 fatal("Key must at least be 768 bits");
Damien Miller884b63a2011-05-05 14:14:52 +1000207 else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(*bitsp) == -1)
Damien Miller58f1baf2011-05-05 14:06:15 +1000208 fatal("Invalid ECDSA key length - valid lengths are "
209 "256, 384 or 521 bits");
Damien Miller1f0311c2014-05-15 14:24:09 +1000210#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000211}
212
213static void
Damien Miller431f66b1999-11-21 18:31:57 +1100214ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215{
Damien Miller95def091999-11-25 00:26:21 +1100216 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100217 char *name = NULL;
218
Damien Miller993dd552002-02-19 15:22:47 +1100219 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000220 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100221 else {
Damien Miller993dd552002-02-19 15:22:47 +1100222 switch (key_type_from_name(key_type_name)) {
223 case KEY_RSA1:
224 name = _PATH_SSH_CLIENT_IDENTITY;
225 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000226 case KEY_DSA_CERT:
227 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100228 case KEY_DSA:
229 name = _PATH_SSH_CLIENT_ID_DSA;
230 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100231#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000232 case KEY_ECDSA_CERT:
233 case KEY_ECDSA:
234 name = _PATH_SSH_CLIENT_ID_ECDSA;
235 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100236#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000237 case KEY_RSA_CERT:
238 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100239 case KEY_RSA:
240 name = _PATH_SSH_CLIENT_ID_RSA;
241 break;
Damien Miller5be9d9e2013-12-07 11:24:01 +1100242 case KEY_ED25519:
243 case KEY_ED25519_CERT:
244 name = _PATH_SSH_CLIENT_ID_ED25519;
245 break;
Damien Miller993dd552002-02-19 15:22:47 +1100246 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100247 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100248 exit(1);
249 break;
250 }
Damien Miller90967402006-03-26 14:07:26 +1100251 }
Damien Millere39cacc2000-11-29 12:18:44 +1100252 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000253 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100254 if (fgets(buf, sizeof(buf), stdin) == NULL)
255 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000256 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100257 if (strcmp(buf, "") != 0)
258 strlcpy(identity_file, buf, sizeof(identity_file));
259 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100260}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Ben Lindstrombba81212001-06-25 05:01:22 +0000262static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000263load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000264{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000265 char *pass;
266 Key *prv;
267
Ben Lindstroma3700052001-04-05 23:26:32 +0000268 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000269 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000270 if (identity_passphrase)
271 pass = xstrdup(identity_passphrase);
272 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000273 pass = read_passphrase("Enter passphrase: ",
274 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000275 prv = key_load_private(filename, pass, NULL);
Damien Millera5103f42014-02-04 11:20:14 +1100276 explicit_bzero(pass, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +1000277 free(pass);
Damien Millereba71ba2000-04-29 23:57:08 +1000278 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000279 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000280}
281
Damien Miller874d77b2000-10-14 16:23:11 +1100282#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000283#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100284#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000285#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000286
Damien Miller1f0311c2014-05-15 14:24:09 +1000287#ifdef WITH_OPENSSL
Ben Lindstrombba81212001-06-25 05:01:22 +0000288static void
Damien Miller44b25042010-07-02 13:35:01 +1000289do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000290{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000291 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000292 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100293 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000294
Damien Millera563cce2012-04-22 11:07:28 +1000295 if (k->type == KEY_RSA1) {
296 fprintf(stderr, "version 1 keys are not supported\n");
297 exit(1);
298 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000299 if (key_to_blob(k, &blob, &len) <= 0) {
300 fprintf(stderr, "key_to_blob failed\n");
301 exit(1);
302 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100303 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
304 snprintf(comment, sizeof(comment),
305 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000306 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000307 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100308
309 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
310 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000311 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100312 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000313 key_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000314 free(blob);
Damien Millereba71ba2000-04-29 23:57:08 +1000315 exit(0);
316}
317
Ben Lindstrombba81212001-06-25 05:01:22 +0000318static void
Damien Miller44b25042010-07-02 13:35:01 +1000319do_convert_to_pkcs8(Key *k)
320{
321 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000322 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000323 case KEY_RSA:
324 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
325 fatal("PEM_write_RSA_PUBKEY failed");
326 break;
327 case KEY_DSA:
328 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
329 fatal("PEM_write_DSA_PUBKEY failed");
330 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000331#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000332 case KEY_ECDSA:
333 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
334 fatal("PEM_write_EC_PUBKEY failed");
335 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000336#endif
Damien Miller44b25042010-07-02 13:35:01 +1000337 default:
338 fatal("%s: unsupported key type %s", __func__, key_type(k));
339 }
340 exit(0);
341}
342
343static void
344do_convert_to_pem(Key *k)
345{
346 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000347 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000348 case KEY_RSA:
349 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
350 fatal("PEM_write_RSAPublicKey failed");
351 break;
352#if notyet /* OpenSSH 0.9.8 lacks this function */
353 case KEY_DSA:
354 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
355 fatal("PEM_write_DSAPublicKey failed");
356 break;
357#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000358 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000359 default:
360 fatal("%s: unsupported key type %s", __func__, key_type(k));
361 }
362 exit(0);
363}
364
365static void
366do_convert_to(struct passwd *pw)
367{
368 Key *k;
369 struct stat st;
370
371 if (!have_identity)
372 ask_filename(pw, "Enter file in which the key is");
373 if (stat(identity_file, &st) < 0)
374 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
375 if ((k = key_load_public(identity_file, NULL)) == NULL) {
376 if ((k = load_identity(identity_file)) == NULL) {
377 fprintf(stderr, "load failed\n");
378 exit(1);
379 }
380 }
Damien Miller44b25042010-07-02 13:35:01 +1000381
382 switch (convert_format) {
383 case FMT_RFC4716:
384 do_convert_to_ssh2(pw, k);
385 break;
386 case FMT_PKCS8:
387 do_convert_to_pkcs8(k);
388 break;
389 case FMT_PEM:
390 do_convert_to_pem(k);
391 break;
392 default:
393 fatal("%s: unknown key format %d", __func__, convert_format);
394 }
395 exit(0);
396}
397
398static void
Damien Miller874d77b2000-10-14 16:23:11 +1100399buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
400{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000401 u_int bignum_bits = buffer_get_int(b);
402 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000403
Damien Miller874d77b2000-10-14 16:23:11 +1100404 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000405 fatal("buffer_get_bignum_bits: input buffer too small: "
406 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100407 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
408 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100409 buffer_consume(b, bytes);
410}
411
Ben Lindstrombba81212001-06-25 05:01:22 +0000412static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000413do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100414{
415 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100416 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100417 char *type, *cipher;
Damien Miller8f9cd702014-04-20 13:00:11 +1000418 u_char *sig = NULL, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000419 int magic, rlen, ktype, i1, i2, i3, i4;
420 u_int slen;
421 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100422
423 buffer_init(&b);
424 buffer_append(&b, blob, blen);
425
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100426 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100427 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
428 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
429 buffer_free(&b);
430 return NULL;
431 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000432 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100433 type = buffer_get_string(&b, NULL);
434 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000435 i2 = buffer_get_int(&b);
436 i3 = buffer_get_int(&b);
437 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100438 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100439 if (strcmp(cipher, "none") != 0) {
440 error("unsupported cipher %s", cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000441 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100442 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000443 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100444 return NULL;
445 }
Darren Tuckera627d422013-06-02 07:31:17 +1000446 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100447
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000448 if (strstr(type, "dsa")) {
449 ktype = KEY_DSA;
450 } else if (strstr(type, "rsa")) {
451 ktype = KEY_RSA;
452 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100453 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000454 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100455 return NULL;
456 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000457 key = key_new_private(ktype);
Darren Tuckera627d422013-06-02 07:31:17 +1000458 free(type);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000459
460 switch (key->type) {
461 case KEY_DSA:
462 buffer_get_bignum_bits(&b, key->dsa->p);
463 buffer_get_bignum_bits(&b, key->dsa->g);
464 buffer_get_bignum_bits(&b, key->dsa->q);
465 buffer_get_bignum_bits(&b, key->dsa->pub_key);
466 buffer_get_bignum_bits(&b, key->dsa->priv_key);
467 break;
468 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100469 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000470 debug("e %lx", e);
471 if (e < 30) {
472 e <<= 8;
473 e += buffer_get_char(&b);
474 debug("e %lx", e);
475 e <<= 8;
476 e += buffer_get_char(&b);
477 debug("e %lx", e);
478 }
479 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000480 buffer_free(&b);
481 key_free(key);
482 return NULL;
483 }
484 buffer_get_bignum_bits(&b, key->rsa->d);
485 buffer_get_bignum_bits(&b, key->rsa->n);
486 buffer_get_bignum_bits(&b, key->rsa->iqmp);
487 buffer_get_bignum_bits(&b, key->rsa->q);
488 buffer_get_bignum_bits(&b, key->rsa->p);
Damien Miller86687062014-07-02 15:28:02 +1000489 if (rsa_generate_additional_parameters(key->rsa) != 0)
490 fatal("%s: rsa_generate_additional_parameters "
491 "error", __func__);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000492 break;
493 }
Damien Miller874d77b2000-10-14 16:23:11 +1100494 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000495 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000496 error("do_convert_private_ssh2_from_blob: "
497 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100498 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000499
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000500 /* try the key */
501 key_sign(key, &sig, &slen, data, sizeof(data));
502 key_verify(key, sig, slen, data, sizeof(data));
Darren Tuckera627d422013-06-02 07:31:17 +1000503 free(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100504 return key;
505}
506
Damien Miller8056a9d2006-03-15 12:05:40 +1100507static int
508get_line(FILE *fp, char *line, size_t len)
509{
510 int c;
511 size_t pos = 0;
512
513 line[0] = '\0';
514 while ((c = fgetc(fp)) != EOF) {
515 if (pos >= len - 1) {
516 fprintf(stderr, "input line too long.\n");
517 exit(1);
518 }
Damien Miller90967402006-03-26 14:07:26 +1100519 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100520 case '\r':
521 c = fgetc(fp);
522 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
523 fprintf(stderr, "unget: %s\n", strerror(errno));
524 exit(1);
525 }
526 return pos;
527 case '\n':
528 return pos;
529 }
530 line[pos++] = c;
531 line[pos] = '\0';
532 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100533 /* We reached EOF */
534 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100535}
536
Ben Lindstrombba81212001-06-25 05:01:22 +0000537static void
Damien Miller44b25042010-07-02 13:35:01 +1000538do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000539{
Damien Millereba71ba2000-04-29 23:57:08 +1000540 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000541 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100542 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000543 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000544 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000545 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000546 FILE *fp;
547
Damien Millerba3420a2010-06-26 09:39:07 +1000548 if ((fp = fopen(identity_file, "r")) == NULL)
549 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000550 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100551 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
Damien Miller746d1a62013-07-18 16:13:02 +1000552 if (blen > 0 && line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000553 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000554 if (strncmp(line, "----", 4) == 0 ||
555 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100556 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000557 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000558 if (strstr(line, " END ") != NULL) {
559 break;
560 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000561 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000562 continue;
563 }
Damien Miller30c3d422000-05-09 11:02:59 +1000564 if (escaped) {
565 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000566 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000567 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000568 }
Damien Millereba71ba2000-04-29 23:57:08 +1000569 strlcat(encoded, line, sizeof(encoded));
570 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000571 len = strlen(encoded);
572 if (((len % 4) == 3) &&
573 (encoded[len-1] == '=') &&
574 (encoded[len-2] == '=') &&
575 (encoded[len-3] == '='))
576 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100577 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000578 if (blen < 0) {
579 fprintf(stderr, "uudecode failed.\n");
580 exit(1);
581 }
Damien Miller44b25042010-07-02 13:35:01 +1000582 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100583 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100584 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000585 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100586 fprintf(stderr, "decode blob failed.\n");
587 exit(1);
588 }
Damien Miller44b25042010-07-02 13:35:01 +1000589 fclose(fp);
590}
591
592static void
593do_convert_from_pkcs8(Key **k, int *private)
594{
595 EVP_PKEY *pubkey;
596 FILE *fp;
597
598 if ((fp = fopen(identity_file, "r")) == NULL)
599 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
600 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
601 fatal("%s: %s is not a recognised public key format", __func__,
602 identity_file);
603 }
604 fclose(fp);
605 switch (EVP_PKEY_type(pubkey->type)) {
606 case EVP_PKEY_RSA:
607 *k = key_new(KEY_UNSPEC);
608 (*k)->type = KEY_RSA;
609 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
610 break;
611 case EVP_PKEY_DSA:
612 *k = key_new(KEY_UNSPEC);
613 (*k)->type = KEY_DSA;
614 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
615 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000616#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000617 case EVP_PKEY_EC:
618 *k = key_new(KEY_UNSPEC);
619 (*k)->type = KEY_ECDSA;
620 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
Damien Millerb472a902010-11-05 10:19:49 +1100621 (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000622 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000623#endif
Damien Miller44b25042010-07-02 13:35:01 +1000624 default:
625 fatal("%s: unsupported pubkey type %d", __func__,
626 EVP_PKEY_type(pubkey->type));
627 }
628 EVP_PKEY_free(pubkey);
629 return;
630}
631
632static void
633do_convert_from_pem(Key **k, int *private)
634{
635 FILE *fp;
636 RSA *rsa;
637#ifdef notyet
638 DSA *dsa;
639#endif
640
641 if ((fp = fopen(identity_file, "r")) == NULL)
642 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
643 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
644 *k = key_new(KEY_UNSPEC);
645 (*k)->type = KEY_RSA;
646 (*k)->rsa = rsa;
647 fclose(fp);
648 return;
649 }
650#if notyet /* OpenSSH 0.9.8 lacks this function */
651 rewind(fp);
652 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
653 *k = key_new(KEY_UNSPEC);
654 (*k)->type = KEY_DSA;
655 (*k)->dsa = dsa;
656 fclose(fp);
657 return;
658 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000659 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000660#endif
661 fatal("%s: unrecognised raw private key format", __func__);
662}
663
664static void
665do_convert_from(struct passwd *pw)
666{
667 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000668 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000669 struct stat st;
670
671 if (!have_identity)
672 ask_filename(pw, "Enter file in which the key is");
673 if (stat(identity_file, &st) < 0)
674 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
675
676 switch (convert_format) {
677 case FMT_RFC4716:
678 do_convert_from_ssh2(pw, &k, &private);
679 break;
680 case FMT_PKCS8:
681 do_convert_from_pkcs8(&k, &private);
682 break;
683 case FMT_PEM:
684 do_convert_from_pem(&k, &private);
685 break;
686 default:
687 fatal("%s: unknown key format %d", __func__, convert_format);
688 }
689
690 if (!private)
691 ok = key_write(k, stdout);
692 if (ok)
693 fprintf(stdout, "\n");
694 else {
695 switch (k->type) {
696 case KEY_DSA:
697 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
698 NULL, 0, NULL, NULL);
699 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000700#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000701 case KEY_ECDSA:
702 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
703 NULL, 0, NULL, NULL);
704 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000705#endif
Damien Miller44b25042010-07-02 13:35:01 +1000706 case KEY_RSA:
707 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
708 NULL, 0, NULL, NULL);
709 break;
710 default:
711 fatal("%s: unsupported key type %s", __func__,
712 key_type(k));
713 }
714 }
715
Damien Miller874d77b2000-10-14 16:23:11 +1100716 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100717 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100718 exit(1);
719 }
Damien Millereba71ba2000-04-29 23:57:08 +1000720 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000721 exit(0);
722}
Damien Miller1f0311c2014-05-15 14:24:09 +1000723#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000724
Ben Lindstrombba81212001-06-25 05:01:22 +0000725static void
Damien Millereba71ba2000-04-29 23:57:08 +1000726do_print_public(struct passwd *pw)
727{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000728 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000729 struct stat st;
730
731 if (!have_identity)
732 ask_filename(pw, "Enter file in which the key is");
733 if (stat(identity_file, &st) < 0) {
734 perror(identity_file);
735 exit(1);
736 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000737 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000738 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000739 fprintf(stderr, "load failed\n");
740 exit(1);
741 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000742 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000743 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000744 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000745 fprintf(stdout, "\n");
746 exit(0);
747}
748
Ben Lindstromcd392282001-07-04 03:44:03 +0000749static void
Damien Miller757f34e2010-08-05 13:05:31 +1000750do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000751{
Damien Miller7ea845e2010-02-12 09:21:02 +1100752#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000753 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100754 int i, nkeys;
Damien Millerec77c952013-01-09 15:58:00 +1100755 enum fp_rep rep;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000756 int fptype;
Damien Millerec77c952013-01-09 15:58:00 +1100757 char *fp, *ra;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000758
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000759 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
760 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
Damien Miller1422c082013-01-09 16:44:54 +1100761
Damien Miller7ea845e2010-02-12 09:21:02 +1100762 pkcs11_init(0);
763 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
764 if (nkeys <= 0)
765 fatal("cannot read public key from pkcs11");
766 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100767 if (print_fingerprint) {
768 fp = key_fingerprint(keys[i], fptype, rep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000769 ra = key_fingerprint(keys[i], fingerprint_hash,
Damien Millerec77c952013-01-09 15:58:00 +1100770 SSH_FP_RANDOMART);
771 printf("%u %s %s (PKCS11 key)\n", key_size(keys[i]),
772 fp, key_type(keys[i]));
773 if (log_level >= SYSLOG_LEVEL_VERBOSE)
774 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000775 free(ra);
776 free(fp);
Damien Millerec77c952013-01-09 15:58:00 +1100777 } else {
778 key_write(keys[i], stdout);
779 fprintf(stdout, "\n");
780 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000781 key_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000782 }
Darren Tuckera627d422013-06-02 07:31:17 +1000783 free(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100784 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000785 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100786#else
787 fatal("no pkcs11 support");
788#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000789}
Ben Lindstromcd392282001-07-04 03:44:03 +0000790
Ben Lindstrombba81212001-06-25 05:01:22 +0000791static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100792do_fingerprint(struct passwd *pw)
793{
Damien Miller98c7ad62000-03-09 21:27:49 +1100794 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000795 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000796 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100797 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000798 enum fp_rep rep;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000799 int fptype;
Damien Miller95def091999-11-25 00:26:21 +1100800 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100801
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000802 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
803 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
Damien Miller95def091999-11-25 00:26:21 +1100804 if (!have_identity)
805 ask_filename(pw, "Enter file in which the key is");
806 if (stat(identity_file, &st) < 0) {
807 perror(identity_file);
808 exit(1);
809 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000810 public = key_load_public(identity_file, &comment);
811 if (public != NULL) {
812 fp = key_fingerprint(public, fptype, rep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000813 ra = key_fingerprint(public, fingerprint_hash,
814 SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000815 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
816 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000817 if (log_level >= SYSLOG_LEVEL_VERBOSE)
818 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +1000820 free(comment);
821 free(ra);
822 free(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100823 exit(0);
824 }
Damien Miller40b59852006-06-13 13:00:25 +1000825 if (comment) {
Darren Tuckera627d422013-06-02 07:31:17 +1000826 free(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000827 comment = NULL;
828 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100829
Damien Millerba3420a2010-06-26 09:39:07 +1000830 if ((f = fopen(identity_file, "r")) == NULL)
831 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100832
Damien Millerba3420a2010-06-26 09:39:07 +1000833 while (fgets(line, sizeof(line), f)) {
834 if ((cp = strchr(line, '\n')) == NULL) {
835 error("line %d too long: %.40s...",
836 num + 1, line);
837 skip = 1;
838 continue;
Damien Miller95def091999-11-25 00:26:21 +1100839 }
Damien Millerba3420a2010-06-26 09:39:07 +1000840 num++;
841 if (skip) {
842 skip = 0;
843 continue;
844 }
845 *cp = '\0';
846
847 /* Skip leading whitespace, empty and comment lines. */
848 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
849 ;
850 if (!*cp || *cp == '\n' || *cp == '#')
851 continue;
852 i = strtol(cp, &ep, 10);
853 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
854 int quoted = 0;
855 comment = cp;
856 for (; *cp && (quoted || (*cp != ' ' &&
857 *cp != '\t')); cp++) {
858 if (*cp == '\\' && cp[1] == '"')
859 cp++; /* Skip both */
860 else if (*cp == '"')
861 quoted = !quoted;
862 }
863 if (!*cp)
864 continue;
865 *cp++ = '\0';
866 }
867 ep = cp;
868 public = key_new(KEY_RSA1);
869 if (key_read(public, &cp) != 1) {
870 cp = ep;
871 key_free(public);
872 public = key_new(KEY_UNSPEC);
873 if (key_read(public, &cp) != 1) {
874 key_free(public);
875 continue;
876 }
877 }
878 comment = *cp ? cp : comment;
879 fp = key_fingerprint(public, fptype, rep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000880 ra = key_fingerprint(public, fingerprint_hash,
881 SSH_FP_RANDOMART);
Damien Millerba3420a2010-06-26 09:39:07 +1000882 printf("%u %s %s (%s)\n", key_size(public), fp,
883 comment ? comment : "no comment", key_type(public));
884 if (log_level >= SYSLOG_LEVEL_VERBOSE)
885 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000886 free(ra);
887 free(fp);
Damien Millerba3420a2010-06-26 09:39:07 +1000888 key_free(public);
889 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100890 }
Damien Millerba3420a2010-06-26 09:39:07 +1000891 fclose(f);
892
Damien Miller98c7ad62000-03-09 21:27:49 +1100893 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100894 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100895 exit(1);
896 }
Damien Miller95def091999-11-25 00:26:21 +1100897 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100898}
899
Damien Miller4b42d7f2005-03-01 21:48:35 +1100900static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000901do_gen_all_hostkeys(struct passwd *pw)
902{
903 struct {
904 char *key_type;
905 char *key_type_display;
906 char *path;
907 } key_types[] = {
908 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
909 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
910 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100911#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000912 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100913#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100914 { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
Damien Miller58f1baf2011-05-05 14:06:15 +1000915 { NULL, NULL, NULL }
916 };
917
918 int first = 0;
919 struct stat st;
920 Key *private, *public;
921 char comment[1024];
922 int i, type, fd;
923 FILE *f;
924
925 for (i = 0; key_types[i].key_type; i++) {
926 if (stat(key_types[i].path, &st) == 0)
927 continue;
928 if (errno != ENOENT) {
929 printf("Could not stat %s: %s", key_types[i].path,
930 strerror(errno));
931 first = 0;
932 continue;
933 }
934
935 if (first == 0) {
936 first = 1;
937 printf("%s: generating new host keys: ", __progname);
938 }
939 printf("%s ", key_types[i].key_type_display);
940 fflush(stdout);
Damien Miller58f1baf2011-05-05 14:06:15 +1000941 type = key_type_from_name(key_types[i].key_type);
942 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
943 bits = 0;
944 type_bits_valid(type, &bits);
945 private = key_generate(type, bits);
946 if (private == NULL) {
947 fprintf(stderr, "key_generate failed\n");
948 first = 0;
949 continue;
950 }
951 public = key_from_private(private);
952 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
953 hostname);
Damien Millerbcd00ab2013-12-07 10:41:55 +1100954 if (!key_save_private(private, identity_file, "", comment,
955 use_new_format, new_format_cipher, rounds)) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000956 printf("Saving the key failed: %s.\n", identity_file);
957 key_free(private);
958 key_free(public);
959 first = 0;
960 continue;
961 }
962 key_free(private);
Damien Miller58f1baf2011-05-05 14:06:15 +1000963 strlcat(identity_file, ".pub", sizeof(identity_file));
964 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
965 if (fd == -1) {
966 printf("Could not save your public key in %s\n",
967 identity_file);
968 key_free(public);
969 first = 0;
970 continue;
971 }
972 f = fdopen(fd, "w");
973 if (f == NULL) {
974 printf("fdopen %s failed\n", identity_file);
doug@openbsd.org7df88182014-08-21 01:08:52 +0000975 close(fd);
Damien Miller58f1baf2011-05-05 14:06:15 +1000976 key_free(public);
977 first = 0;
978 continue;
979 }
980 if (!key_write(public, f)) {
981 fprintf(stderr, "write key failed\n");
doug@openbsd.org7df88182014-08-21 01:08:52 +0000982 fclose(f);
Damien Miller58f1baf2011-05-05 14:06:15 +1000983 key_free(public);
984 first = 0;
985 continue;
986 }
987 fprintf(f, " %s\n", comment);
988 fclose(f);
989 key_free(public);
990
991 }
992 if (first != 0)
993 printf("\n");
994}
995
996static void
Damien Miller4a1d3d52014-07-03 21:24:40 +1000997printhost(FILE *f, const char *name, Key *public, int ca, int revoked, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100998{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000999 if (print_fingerprint) {
1000 enum fp_rep rep;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001001 int fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +10001002 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +10001003
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001004 fptype = print_bubblebabble ?
1005 SSH_DIGEST_SHA1 : fingerprint_hash;
1006 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
Darren Tucker0f7e9102008-06-08 12:54:29 +10001007 fp = key_fingerprint(public, fptype, rep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001008 ra = key_fingerprint(public, fingerprint_hash,
1009 SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +10001010 printf("%u %s %s (%s)\n", key_size(public), fp, name,
1011 key_type(public));
1012 if (log_level >= SYSLOG_LEVEL_VERBOSE)
1013 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10001014 free(ra);
1015 free(fp);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001016 } else {
1017 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
1018 fatal("hash_host failed");
Damien Miller4a1d3d52014-07-03 21:24:40 +10001019 fprintf(f, "%s%s%s ", ca ? CA_MARKER " " : "",
1020 revoked ? REVOKE_MARKER " " : "" , name);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001021 if (!key_write(public, f))
1022 fatal("key_write failed");
1023 fprintf(f, "\n");
1024 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001025}
1026
1027static void
1028do_known_hosts(struct passwd *pw, const char *name)
1029{
1030 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +11001031 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001032 char *cp, *cp2, *kp, *kp2;
1033 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +11001034 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller4a1d3d52014-07-03 21:24:40 +10001035 int ca, revoked;
Damien Miller66085482013-09-14 09:45:03 +10001036 int found_key = 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001037
1038 if (!have_identity) {
1039 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1040 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1041 sizeof(identity_file))
1042 fatal("Specified known hosts path too long");
Darren Tuckera627d422013-06-02 07:31:17 +10001043 free(cp);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001044 have_identity = 1;
1045 }
1046 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +10001047 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001048
Damien Miller4a1d3d52014-07-03 21:24:40 +10001049 /* XXX this code is a mess; refactor -djm */
Damien Miller4b42d7f2005-03-01 21:48:35 +11001050 /*
1051 * Find hosts goes to stdout, hash and deletions happen in-place
1052 * A corner case is ssh-keygen -HF foo, which should go to stdout
1053 */
1054 if (!find_host && (hash_hosts || delete_host)) {
1055 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1056 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1057 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1058 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1059 fatal("known_hosts path too long");
1060 umask(077);
1061 if ((c = mkstemp(tmp)) == -1)
1062 fatal("mkstemp: %s", strerror(errno));
1063 if ((out = fdopen(c, "w")) == NULL) {
1064 c = errno;
1065 unlink(tmp);
1066 fatal("fdopen: %s", strerror(c));
1067 }
1068 inplace = 1;
1069 }
1070
1071 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001072 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001073 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001074 skip = 1;
1075 invalid = 1;
1076 continue;
1077 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001078 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001079 if (skip) {
1080 skip = 0;
1081 continue;
1082 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001083 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001084
1085 /* Skip leading whitespace, empty and comment lines. */
1086 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1087 ;
1088 if (!*cp || *cp == '\n' || *cp == '#') {
1089 if (inplace)
1090 fprintf(out, "%s\n", cp);
1091 continue;
1092 }
Damien Miller4a1d3d52014-07-03 21:24:40 +10001093 /* Check whether this is a CA key or revocation marker */
Damien Miller0a80ca12010-02-27 07:55:05 +11001094 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1095 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1096 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1097 ca = 1;
1098 cp += sizeof(CA_MARKER);
1099 } else
1100 ca = 0;
Damien Miller4a1d3d52014-07-03 21:24:40 +10001101 if (strncasecmp(cp, REVOKE_MARKER,
1102 sizeof(REVOKE_MARKER) - 1) == 0 &&
1103 (cp[sizeof(REVOKE_MARKER) - 1] == ' ' ||
1104 cp[sizeof(REVOKE_MARKER) - 1] == '\t')) {
1105 revoked = 1;
1106 cp += sizeof(REVOKE_MARKER);
1107 } else
1108 revoked = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001109
Damien Miller4b42d7f2005-03-01 21:48:35 +11001110 /* Find the end of the host name portion. */
1111 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1112 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001113
Damien Miller4b42d7f2005-03-01 21:48:35 +11001114 if (*kp == '\0' || *(kp + 1) == '\0') {
1115 error("line %d missing key: %.40s...",
1116 num, line);
1117 invalid = 1;
1118 continue;
1119 }
1120 *kp++ = '\0';
1121 kp2 = kp;
1122
Damien Miller0a80ca12010-02-27 07:55:05 +11001123 pub = key_new(KEY_RSA1);
1124 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001125 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +11001126 key_free(pub);
1127 pub = key_new(KEY_UNSPEC);
1128 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001129 error("line %d invalid key: %.40s...",
1130 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001131 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001132 invalid = 1;
1133 continue;
1134 }
1135 }
1136
1137 if (*cp == HASH_DELIM) {
1138 if (find_host || delete_host) {
1139 cp2 = host_hash(name, cp, strlen(cp));
1140 if (cp2 == NULL) {
1141 error("line %d: invalid hashed "
1142 "name: %.64s...", num, line);
1143 invalid = 1;
1144 continue;
1145 }
1146 c = (strcmp(cp2, cp) == 0);
1147 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001148 if (!quiet)
1149 printf("# Host %s found: "
1150 "line %d type %s%s\n", name,
1151 num, key_type(pub),
Damien Miller4a1d3d52014-07-03 21:24:40 +10001152 ca ? " (CA key)" :
1153 revoked? " (revoked)" : "");
1154 printhost(out, cp, pub, ca, revoked, 0);
Damien Miller66085482013-09-14 09:45:03 +10001155 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001156 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001157 if (delete_host) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001158 if (!c || ca || revoked) {
1159 printhost(out, cp, pub,
1160 ca, revoked, 0);
1161 } else {
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001162 printf("# Host %s found: "
1163 "line %d type %s\n", name,
1164 num, key_type(pub));
Damien Miller4a1d3d52014-07-03 21:24:40 +10001165 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001166 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001167 } else if (hash_hosts)
Damien Miller4a1d3d52014-07-03 21:24:40 +10001168 printhost(out, cp, pub, ca, revoked, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001169 } else {
1170 if (find_host || delete_host) {
1171 c = (match_hostname(name, cp,
1172 strlen(cp)) == 1);
1173 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001174 if (!quiet)
1175 printf("# Host %s found: "
1176 "line %d type %s%s\n", name,
1177 num, key_type(pub),
1178 ca ? " (CA key)" : "");
Damien Miller4a1d3d52014-07-03 21:24:40 +10001179 printhost(out, name, pub, ca, revoked,
1180 hash_hosts && !(ca || revoked));
Damien Miller66085482013-09-14 09:45:03 +10001181 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001182 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001183 if (delete_host) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001184 if (!c || ca || revoked) {
1185 printhost(out, cp, pub,
1186 ca, revoked, 0);
1187 } else {
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001188 printf("# Host %s found: "
1189 "line %d type %s\n", name,
1190 num, key_type(pub));
Damien Miller4a1d3d52014-07-03 21:24:40 +10001191 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001192 }
Damien Miller4a1d3d52014-07-03 21:24:40 +10001193 } else if (hash_hosts && (ca || revoked)) {
1194 /* Don't hash CA and revoked keys' hostnames */
1195 printhost(out, cp, pub, ca, revoked, 0);
1196 has_unhashed = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001197 } else if (hash_hosts) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001198 /* Hash each hostname separately */
Darren Tucker47eede72005-03-14 23:08:12 +11001199 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001200 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001201 cp2 = strsep(&cp, ",")) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001202 if (strcspn(cp2, "*?!") !=
Damien Miller0a80ca12010-02-27 07:55:05 +11001203 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001204 fprintf(stderr, "Warning: "
1205 "ignoring host name with "
1206 "metacharacters: %.64s\n",
1207 cp2);
Damien Miller4a1d3d52014-07-03 21:24:40 +10001208 printhost(out, cp2, pub, ca,
1209 revoked, 0);
1210 has_unhashed = 1;
1211 } else {
1212 printhost(out, cp2, pub, ca,
1213 revoked, 1);
1214 }
Damien Miller89eac802005-03-02 12:33:04 +11001215 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001216 }
1217 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001218 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001219 }
1220 fclose(in);
1221
1222 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001223 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001224 identity_file);
1225 if (inplace) {
1226 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001227 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001228 fclose(out);
1229 unlink(tmp);
1230 }
1231 exit(1);
1232 }
1233
1234 if (inplace) {
1235 fclose(out);
1236
1237 /* Backup existing file */
1238 if (unlink(old) == -1 && errno != ENOENT)
1239 fatal("unlink %.100s: %s", old, strerror(errno));
1240 if (link(identity_file, old) == -1)
1241 fatal("link %.100s to %.100s: %s", identity_file, old,
1242 strerror(errno));
1243 /* Move new one into place */
1244 if (rename(tmp, identity_file) == -1) {
1245 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1246 strerror(errno));
1247 unlink(tmp);
1248 unlink(old);
1249 exit(1);
1250 }
1251
1252 fprintf(stderr, "%s updated.\n", identity_file);
1253 fprintf(stderr, "Original contents retained as %s\n", old);
1254 if (has_unhashed) {
1255 fprintf(stderr, "WARNING: %s contains unhashed "
1256 "entries\n", old);
1257 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001258 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001259 }
1260 }
1261
Damien Miller66085482013-09-14 09:45:03 +10001262 exit (find_host && !found_key);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001263}
1264
Damien Miller95def091999-11-25 00:26:21 +11001265/*
1266 * Perform changing a passphrase. The argument is the passwd structure
1267 * for the current user.
1268 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001269static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001270do_change_passphrase(struct passwd *pw)
1271{
Damien Miller95def091999-11-25 00:26:21 +11001272 char *comment;
1273 char *old_passphrase, *passphrase1, *passphrase2;
1274 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001275 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001276
Damien Miller95def091999-11-25 00:26:21 +11001277 if (!have_identity)
1278 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001279 if (stat(identity_file, &st) < 0) {
1280 perror(identity_file);
1281 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001282 }
Damien Miller95def091999-11-25 00:26:21 +11001283 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001284 private = key_load_private(identity_file, "", &comment);
1285 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001286 if (identity_passphrase)
1287 old_passphrase = xstrdup(identity_passphrase);
1288 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001289 old_passphrase =
1290 read_passphrase("Enter old passphrase: ",
1291 RP_ALLOW_STDIN);
1292 private = key_load_private(identity_file, old_passphrase,
1293 &comment);
Damien Millera5103f42014-02-04 11:20:14 +11001294 explicit_bzero(old_passphrase, strlen(old_passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001295 free(old_passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001296 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001297 printf("Bad passphrase.\n");
1298 exit(1);
1299 }
Damien Miller95def091999-11-25 00:26:21 +11001300 }
1301 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302
Damien Miller95def091999-11-25 00:26:21 +11001303 /* Ask the new passphrase (twice). */
1304 if (identity_new_passphrase) {
1305 passphrase1 = xstrdup(identity_new_passphrase);
1306 passphrase2 = NULL;
1307 } else {
1308 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001309 read_passphrase("Enter new passphrase (empty for no "
1310 "passphrase): ", RP_ALLOW_STDIN);
1311 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001312 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001313
1314 /* Verify that they are the same. */
1315 if (strcmp(passphrase1, passphrase2) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001316 explicit_bzero(passphrase1, strlen(passphrase1));
1317 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001318 free(passphrase1);
1319 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001320 printf("Pass phrases do not match. Try again.\n");
1321 exit(1);
1322 }
1323 /* Destroy the other copy. */
Damien Millera5103f42014-02-04 11:20:14 +11001324 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001325 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001326 }
1327
1328 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001329 if (!key_save_private(private, identity_file, passphrase1, comment,
1330 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001331 printf("Saving the key failed: %s.\n", identity_file);
Damien Millera5103f42014-02-04 11:20:14 +11001332 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001333 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001334 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001335 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001336 exit(1);
1337 }
1338 /* Destroy the passphrase and the copy of the key in memory. */
Damien Millera5103f42014-02-04 11:20:14 +11001339 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001340 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001341 key_free(private); /* Destroys contents */
Darren Tuckera627d422013-06-02 07:31:17 +10001342 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001343
1344 printf("Your identification has been saved with the new passphrase.\n");
1345 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346}
1347
Damien Miller37876e92003-05-15 10:19:46 +10001348/*
1349 * Print the SSHFP RR.
1350 */
Damien Millercb314822006-03-26 13:48:01 +11001351static int
1352do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001353{
1354 Key *public;
1355 char *comment = NULL;
1356 struct stat st;
1357
Damien Millercb314822006-03-26 13:48:01 +11001358 if (fname == NULL)
Damien Miller5bb88332013-07-18 16:13:37 +10001359 fatal("%s: no filename", __func__);
Damien Millercb314822006-03-26 13:48:01 +11001360 if (stat(fname, &st) < 0) {
1361 if (errno == ENOENT)
1362 return 0;
1363 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001364 exit(1);
1365 }
Damien Millercb314822006-03-26 13:48:01 +11001366 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001367 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001368 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001369 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001370 free(comment);
Damien Millercb314822006-03-26 13:48:01 +11001371 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001372 }
1373 if (comment)
Darren Tuckera627d422013-06-02 07:31:17 +10001374 free(comment);
Damien Miller37876e92003-05-15 10:19:46 +10001375
Damien Millercb314822006-03-26 13:48:01 +11001376 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001377 exit(1);
1378}
Damien Miller37876e92003-05-15 10:19:46 +10001379
Damien Miller95def091999-11-25 00:26:21 +11001380/*
1381 * Change the comment of a private key file.
1382 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001383static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384do_change_comment(struct passwd *pw)
1385{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001386 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001387 Key *private;
1388 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001389 struct stat st;
1390 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001391 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392
Damien Miller95def091999-11-25 00:26:21 +11001393 if (!have_identity)
1394 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001395 if (stat(identity_file, &st) < 0) {
1396 perror(identity_file);
1397 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001398 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001399 private = key_load_private(identity_file, "", &comment);
1400 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001401 if (identity_passphrase)
1402 passphrase = xstrdup(identity_passphrase);
1403 else if (identity_new_passphrase)
1404 passphrase = xstrdup(identity_new_passphrase);
1405 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001406 passphrase = read_passphrase("Enter passphrase: ",
1407 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001408 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001409 private = key_load_private(identity_file, passphrase, &comment);
1410 if (private == NULL) {
Damien Millera5103f42014-02-04 11:20:14 +11001411 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001412 free(passphrase);
Damien Miller95def091999-11-25 00:26:21 +11001413 printf("Bad passphrase.\n");
1414 exit(1);
1415 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001416 } else {
1417 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001418 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001419 if (private->type != KEY_RSA1) {
1420 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1421 key_free(private);
1422 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001423 }
Damien Miller95def091999-11-25 00:26:21 +11001424 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425
Damien Miller95def091999-11-25 00:26:21 +11001426 if (identity_comment) {
1427 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1428 } else {
1429 printf("Enter new comment: ");
1430 fflush(stdout);
1431 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
Damien Millera5103f42014-02-04 11:20:14 +11001432 explicit_bzero(passphrase, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001433 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001434 exit(1);
1435 }
Damien Miller14b017d2007-09-17 16:09:15 +10001436 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001437 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438
Damien Miller95def091999-11-25 00:26:21 +11001439 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001440 if (!key_save_private(private, identity_file, passphrase, new_comment,
1441 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001442 printf("Saving the key failed: %s.\n", identity_file);
Damien Millera5103f42014-02-04 11:20:14 +11001443 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001444 free(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001445 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001446 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001447 exit(1);
1448 }
Damien Millera5103f42014-02-04 11:20:14 +11001449 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001450 free(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001451 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001452 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453
Damien Miller95def091999-11-25 00:26:21 +11001454 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001455 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1456 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001457 printf("Could not save your public key in %s\n", identity_file);
1458 exit(1);
1459 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001460 f = fdopen(fd, "w");
1461 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001462 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001463 exit(1);
1464 }
Damien Millereba71ba2000-04-29 23:57:08 +10001465 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001466 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001467 key_free(public);
1468 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001469 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470
Darren Tuckera627d422013-06-02 07:31:17 +10001471 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001472
1473 printf("The comment in your key file has been changed.\n");
1474 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001475}
1476
Damien Miller0a80ca12010-02-27 07:55:05 +11001477static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001478fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001479{
1480 char from[32], to[32];
1481 static char ret[64];
1482 time_t tt;
1483 struct tm *tm;
1484
1485 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001486 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001487 return "forever";
1488
Damien Millerf2b70ca2010-03-05 07:39:35 +11001489 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001490 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001491 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001492 tm = localtime(&tt);
1493 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1494 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001495 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001496 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001497 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001498 tm = localtime(&tt);
1499 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1500 }
1501
Damien Millerf2b70ca2010-03-05 07:39:35 +11001502 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001503 snprintf(ret, sizeof(ret), "before %s", to);
1504 return ret;
1505 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001506 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001507 snprintf(ret, sizeof(ret), "after %s", from);
1508 return ret;
1509 }
1510
1511 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1512 return ret;
1513}
1514
1515static void
Damien Miller4e270b02010-04-16 15:56:21 +10001516add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001517{
1518 debug3("%s: %s", __func__, name);
1519 buffer_put_cstring(c, name);
1520 buffer_put_string(c, NULL, 0);
1521}
1522
1523static void
Damien Miller4e270b02010-04-16 15:56:21 +10001524add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001525{
1526 Buffer b;
1527
1528 debug3("%s: %s=%s", __func__, name, value);
1529 buffer_init(&b);
1530 buffer_put_cstring(&b, value);
1531
1532 buffer_put_cstring(c, name);
1533 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1534
1535 buffer_free(&b);
1536}
1537
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001538#define OPTIONS_CRITICAL 1
1539#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001540static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001541prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001542{
Damien Miller0a80ca12010-02-27 07:55:05 +11001543 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001544 if ((which & OPTIONS_CRITICAL) != 0 &&
1545 certflags_command != NULL)
1546 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001547 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001548 (certflags_flags & CERTOPT_X_FWD) != 0)
1549 add_flag_option(c, "permit-X11-forwarding");
1550 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001551 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001552 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001553 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1554 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001555 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001556 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1557 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001558 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001559 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1560 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001561 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001562 if ((which & OPTIONS_CRITICAL) != 0 &&
1563 certflags_src_addr != NULL)
1564 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001565}
1566
Damien Miller757f34e2010-08-05 13:05:31 +10001567static Key *
1568load_pkcs11_key(char *path)
1569{
1570#ifdef ENABLE_PKCS11
1571 Key **keys = NULL, *public, *private = NULL;
1572 int i, nkeys;
1573
1574 if ((public = key_load_public(path, NULL)) == NULL)
1575 fatal("Couldn't load CA public key \"%s\"", path);
1576
1577 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1578 debug3("%s: %d keys", __func__, nkeys);
1579 if (nkeys <= 0)
1580 fatal("cannot read public key from pkcs11");
1581 for (i = 0; i < nkeys; i++) {
1582 if (key_equal_public(public, keys[i])) {
1583 private = keys[i];
1584 continue;
1585 }
1586 key_free(keys[i]);
1587 }
Darren Tuckera627d422013-06-02 07:31:17 +10001588 free(keys);
Damien Miller757f34e2010-08-05 13:05:31 +10001589 key_free(public);
1590 return private;
1591#else
1592 fatal("no pkcs11 support");
1593#endif /* ENABLE_PKCS11 */
1594}
1595
Damien Miller0a80ca12010-02-27 07:55:05 +11001596static void
1597do_ca_sign(struct passwd *pw, int argc, char **argv)
1598{
1599 int i, fd;
1600 u_int n;
1601 Key *ca, *public;
1602 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1603 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001604 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001605
Damien Miller4e270b02010-04-16 15:56:21 +10001606 if (key_type_name != NULL) {
1607 switch (key_type_from_name(key_type_name)) {
1608 case KEY_RSA_CERT_V00:
1609 case KEY_DSA_CERT_V00:
1610 v00 = 1;
1611 break;
1612 case KEY_UNSPEC:
1613 if (strcasecmp(key_type_name, "v00") == 0) {
1614 v00 = 1;
1615 break;
1616 } else if (strcasecmp(key_type_name, "v01") == 0)
1617 break;
1618 /* FALLTHROUGH */
1619 default:
1620 fprintf(stderr, "unknown key type %s\n", key_type_name);
1621 exit(1);
1622 }
1623 }
1624
Damien Miller1f0311c2014-05-15 14:24:09 +10001625#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001626 pkcs11_init(1);
Damien Miller1f0311c2014-05-15 14:24:09 +10001627#endif
Damien Miller757f34e2010-08-05 13:05:31 +10001628 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1629 if (pkcs11provider != NULL) {
1630 if ((ca = load_pkcs11_key(tmp)) == NULL)
1631 fatal("No PKCS#11 key matching %s found", ca_key_path);
1632 } else if ((ca = load_identity(tmp)) == NULL)
1633 fatal("Couldn't load CA key \"%s\"", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001634 free(tmp);
Damien Miller757f34e2010-08-05 13:05:31 +10001635
Damien Miller0a80ca12010-02-27 07:55:05 +11001636 for (i = 0; i < argc; i++) {
1637 /* Split list of principals */
1638 n = 0;
1639 if (cert_principals != NULL) {
1640 otmp = tmp = xstrdup(cert_principals);
1641 plist = NULL;
1642 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1643 plist = xrealloc(plist, n + 1, sizeof(*plist));
1644 if (*(plist[n] = xstrdup(cp)) == '\0')
1645 fatal("Empty principal name");
1646 }
Darren Tuckera627d422013-06-02 07:31:17 +10001647 free(otmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001648 }
1649
1650 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1651 if ((public = key_load_public(tmp, &comment)) == NULL)
1652 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001653 if (public->type != KEY_RSA && public->type != KEY_DSA &&
Damien Miller5be9d9e2013-12-07 11:24:01 +11001654 public->type != KEY_ECDSA && public->type != KEY_ED25519)
Damien Miller0a80ca12010-02-27 07:55:05 +11001655 fatal("%s: key \"%s\" type %s cannot be certified",
1656 __func__, tmp, key_type(public));
1657
1658 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001659 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001660 fatal("Could not upgrade key %s to certificate", tmp);
1661 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001662 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001663 public->cert->key_id = xstrdup(cert_key_id);
1664 public->cert->nprincipals = n;
1665 public->cert->principals = plist;
1666 public->cert->valid_after = cert_valid_from;
1667 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001668 if (v00) {
Damien Miller86687062014-07-02 15:28:02 +10001669 prepare_options_buf(public->cert->critical,
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001670 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1671 } else {
Damien Miller86687062014-07-02 15:28:02 +10001672 prepare_options_buf(public->cert->critical,
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001673 OPTIONS_CRITICAL);
Damien Miller86687062014-07-02 15:28:02 +10001674 prepare_options_buf(public->cert->extensions,
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001675 OPTIONS_EXTENSIONS);
1676 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001677 public->cert->signature_key = key_from_private(ca);
1678
1679 if (key_certify(public, ca) != 0)
1680 fatal("Couldn't not certify key %s", tmp);
1681
1682 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1683 *cp = '\0';
1684 xasprintf(&out, "%s-cert.pub", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001685 free(tmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001686
1687 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1688 fatal("Could not open \"%s\" for writing: %s", out,
1689 strerror(errno));
1690 if ((f = fdopen(fd, "w")) == NULL)
1691 fatal("%s: fdopen: %s", __func__, strerror(errno));
1692 if (!key_write(public, f))
1693 fatal("Could not write certified key to %s", out);
1694 fprintf(f, " %s\n", comment);
1695 fclose(f);
1696
Damien Miller4e270b02010-04-16 15:56:21 +10001697 if (!quiet) {
1698 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1699 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001700 out, public->cert->key_id,
1701 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001702 cert_principals != NULL ? " for " : "",
1703 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001704 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001705 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001706
1707 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001708 free(out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001709 }
Damien Miller1f0311c2014-05-15 14:24:09 +10001710#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001711 pkcs11_terminate();
Damien Miller1f0311c2014-05-15 14:24:09 +10001712#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11001713 exit(0);
1714}
1715
1716static u_int64_t
1717parse_relative_time(const char *s, time_t now)
1718{
1719 int64_t mul, secs;
1720
1721 mul = *s == '-' ? -1 : 1;
1722
1723 if ((secs = convtime(s + 1)) == -1)
1724 fatal("Invalid relative certificate time %s", s);
1725 if (mul == -1 && secs > now)
1726 fatal("Certificate time %s cannot be represented", s);
1727 return now + (u_int64_t)(secs * mul);
1728}
1729
1730static u_int64_t
1731parse_absolute_time(const char *s)
1732{
1733 struct tm tm;
1734 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001735 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001736
Damien Miller2ca342b2010-03-03 12:14:15 +11001737 /*
1738 * POSIX strptime says "The application shall ensure that there
1739 * is white-space or other non-alphanumeric characters between
1740 * any two conversion specifications" so arrange things this way.
1741 */
1742 switch (strlen(s)) {
1743 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001744 fmt = "%Y-%m-%d";
1745 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001746 break;
1747 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001748 fmt = "%Y-%m-%dT%H:%M:%S";
1749 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001750 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1751 break;
1752 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001753 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001754 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001755
Damien Miller1d2c4562014-02-04 11:18:20 +11001756 memset(&tm, 0, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001757 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001758 fatal("Invalid certificate time %s", s);
1759 if ((tt = mktime(&tm)) < 0)
1760 fatal("Certificate time %s cannot be represented", s);
1761 return (u_int64_t)tt;
1762}
1763
1764static void
1765parse_cert_times(char *timespec)
1766{
1767 char *from, *to;
1768 time_t now = time(NULL);
1769 int64_t secs;
1770
1771 /* +timespec relative to now */
1772 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1773 if ((secs = convtime(timespec + 1)) == -1)
1774 fatal("Invalid relative certificate life %s", timespec);
1775 cert_valid_to = now + secs;
1776 /*
1777 * Backdate certificate one minute to avoid problems on hosts
1778 * with poorly-synchronised clocks.
1779 */
1780 cert_valid_from = ((now - 59)/ 60) * 60;
1781 return;
1782 }
1783
1784 /*
1785 * from:to, where
1786 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1787 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1788 */
1789 from = xstrdup(timespec);
1790 to = strchr(from, ':');
1791 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001792 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001793 *to++ = '\0';
1794
1795 if (*from == '-' || *from == '+')
1796 cert_valid_from = parse_relative_time(from, now);
1797 else
1798 cert_valid_from = parse_absolute_time(from);
1799
1800 if (*to == '-' || *to == '+')
Damien Miller5b01b0d2013-10-23 16:31:31 +11001801 cert_valid_to = parse_relative_time(to, now);
Damien Miller0a80ca12010-02-27 07:55:05 +11001802 else
1803 cert_valid_to = parse_absolute_time(to);
1804
1805 if (cert_valid_to <= cert_valid_from)
1806 fatal("Empty certificate validity interval");
Darren Tuckera627d422013-06-02 07:31:17 +10001807 free(from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001808}
1809
1810static void
Damien Miller4e270b02010-04-16 15:56:21 +10001811add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001812{
1813 char *val;
1814
Damien Miller044f4a62011-05-05 14:14:08 +10001815 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001816 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001817 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001818 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001819 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001820 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001821 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001822 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001823 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001824 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001825 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001826 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001827 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001828 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001829 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001830 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001831 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001832 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001833 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001834 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001835 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001836 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001837 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1838 val = opt + 14;
1839 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001840 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001841 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001842 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001843 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001844 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1845 val = opt + 15;
1846 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001847 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001848 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001849 fatal("source-address already specified");
1850 if (addr_match_cidr_list(NULL, val) != 0)
1851 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001852 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001853 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001854 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001855}
1856
Ben Lindstrombba81212001-06-25 05:01:22 +00001857static void
Damien Millerd834d352010-06-26 09:48:02 +10001858show_options(const Buffer *optbuf, int v00, int in_critical)
1859{
Damien Miller633de332014-05-15 13:48:26 +10001860 char *name, *arg;
1861 const u_char *data;
Damien Millerd834d352010-06-26 09:48:02 +10001862 u_int dlen;
1863 Buffer options, option;
1864
1865 buffer_init(&options);
1866 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1867
1868 buffer_init(&option);
1869 while (buffer_len(&options) != 0) {
1870 name = buffer_get_string(&options, NULL);
1871 data = buffer_get_string_ptr(&options, &dlen);
1872 buffer_append(&option, data, dlen);
1873 printf(" %s", name);
1874 if ((v00 || !in_critical) &&
1875 (strcmp(name, "permit-X11-forwarding") == 0 ||
1876 strcmp(name, "permit-agent-forwarding") == 0 ||
1877 strcmp(name, "permit-port-forwarding") == 0 ||
1878 strcmp(name, "permit-pty") == 0 ||
1879 strcmp(name, "permit-user-rc") == 0))
1880 printf("\n");
1881 else if ((v00 || in_critical) &&
1882 (strcmp(name, "force-command") == 0 ||
1883 strcmp(name, "source-address") == 0)) {
Damien Miller633de332014-05-15 13:48:26 +10001884 arg = buffer_get_cstring(&option, NULL);
1885 printf(" %s\n", arg);
1886 free(arg);
Damien Millerd834d352010-06-26 09:48:02 +10001887 } else {
1888 printf(" UNKNOWN OPTION (len %u)\n",
1889 buffer_len(&option));
1890 buffer_clear(&option);
1891 }
Darren Tuckera627d422013-06-02 07:31:17 +10001892 free(name);
Damien Millerd834d352010-06-26 09:48:02 +10001893 if (buffer_len(&option) != 0)
1894 fatal("Option corrupt: extra data at end");
1895 }
1896 buffer_free(&option);
1897 buffer_free(&options);
1898}
1899
1900static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001901do_show_cert(struct passwd *pw)
1902{
1903 Key *key;
1904 struct stat st;
1905 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001906 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001907
1908 if (!have_identity)
1909 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001910 if (stat(identity_file, &st) < 0)
1911 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001912 if ((key = key_load_public(identity_file, NULL)) == NULL)
1913 fatal("%s is not a public key", identity_file);
1914 if (!key_is_cert(key))
1915 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001916 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1917
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001918 key_fp = key_fingerprint(key, fingerprint_hash, SSH_FP_DEFAULT);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001919 ca_fp = key_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001920 fingerprint_hash, SSH_FP_DEFAULT);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001921
1922 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001923 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1924 key_cert_type(key));
1925 printf(" Public key: %s %s\n", key_type(key), key_fp);
1926 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001927 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001928 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001929 if (!v00) {
1930 printf(" Serial: %llu\n",
1931 (unsigned long long)key->cert->serial);
1932 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001933 printf(" Valid: %s\n",
1934 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1935 printf(" Principals: ");
1936 if (key->cert->nprincipals == 0)
1937 printf("(none)\n");
1938 else {
1939 for (i = 0; i < key->cert->nprincipals; i++)
1940 printf("\n %s",
1941 key->cert->principals[i]);
1942 printf("\n");
1943 }
Damien Miller4e270b02010-04-16 15:56:21 +10001944 printf(" Critical Options: ");
Damien Miller86687062014-07-02 15:28:02 +10001945 if (buffer_len(key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001946 printf("(none)\n");
1947 else {
1948 printf("\n");
Damien Miller86687062014-07-02 15:28:02 +10001949 show_options(key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001950 }
Damien Miller4e270b02010-04-16 15:56:21 +10001951 if (!v00) {
1952 printf(" Extensions: ");
Damien Miller86687062014-07-02 15:28:02 +10001953 if (buffer_len(key->cert->extensions) == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001954 printf("(none)\n");
1955 else {
1956 printf("\n");
Damien Miller86687062014-07-02 15:28:02 +10001957 show_options(key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001958 }
1959 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001960 exit(0);
1961}
1962
Damien Miller1f0311c2014-05-15 14:24:09 +10001963#ifdef WITH_OPENSSL
Damien Millerf2b70ca2010-03-05 07:39:35 +11001964static void
Damien Millerf3747bf2013-01-18 11:44:04 +11001965load_krl(const char *path, struct ssh_krl **krlp)
1966{
1967 Buffer krlbuf;
1968 int fd;
1969
1970 buffer_init(&krlbuf);
1971 if ((fd = open(path, O_RDONLY)) == -1)
1972 fatal("open %s: %s", path, strerror(errno));
1973 if (!key_load_file(fd, path, &krlbuf))
1974 fatal("Unable to load KRL");
1975 close(fd);
1976 /* XXX check sigs */
1977 if (ssh_krl_from_blob(&krlbuf, krlp, NULL, 0) != 0 ||
1978 *krlp == NULL)
1979 fatal("Invalid KRL file");
1980 buffer_free(&krlbuf);
1981}
1982
1983static void
1984update_krl_from_file(struct passwd *pw, const char *file, const Key *ca,
1985 struct ssh_krl *krl)
1986{
1987 Key *key = NULL;
1988 u_long lnum = 0;
1989 char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES];
1990 unsigned long long serial, serial2;
1991 int i, was_explicit_key, was_sha1, r;
1992 FILE *krl_spec;
1993
1994 path = tilde_expand_filename(file, pw->pw_uid);
1995 if (strcmp(path, "-") == 0) {
1996 krl_spec = stdin;
1997 free(path);
1998 path = xstrdup("(standard input)");
1999 } else if ((krl_spec = fopen(path, "r")) == NULL)
2000 fatal("fopen %s: %s", path, strerror(errno));
2001
2002 if (!quiet)
2003 printf("Revoking from %s\n", path);
2004 while (read_keyfile_line(krl_spec, path, line, sizeof(line),
2005 &lnum) == 0) {
2006 was_explicit_key = was_sha1 = 0;
2007 cp = line + strspn(line, " \t");
2008 /* Trim trailing space, comments and strip \n */
2009 for (i = 0, r = -1; cp[i] != '\0'; i++) {
2010 if (cp[i] == '#' || cp[i] == '\n') {
2011 cp[i] = '\0';
2012 break;
2013 }
2014 if (cp[i] == ' ' || cp[i] == '\t') {
2015 /* Remember the start of a span of whitespace */
2016 if (r == -1)
2017 r = i;
2018 } else
2019 r = -1;
2020 }
2021 if (r != -1)
2022 cp[r] = '\0';
2023 if (*cp == '\0')
2024 continue;
2025 if (strncasecmp(cp, "serial:", 7) == 0) {
2026 if (ca == NULL) {
Damien Millerd234afb2013-08-21 02:42:58 +10002027 fatal("revoking certificates by serial number "
Damien Millerf3747bf2013-01-18 11:44:04 +11002028 "requires specification of a CA key");
2029 }
2030 cp += 7;
2031 cp = cp + strspn(cp, " \t");
2032 errno = 0;
2033 serial = strtoull(cp, &ep, 0);
2034 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2035 fatal("%s:%lu: invalid serial \"%s\"",
2036 path, lnum, cp);
2037 if (errno == ERANGE && serial == ULLONG_MAX)
2038 fatal("%s:%lu: serial out of range",
2039 path, lnum);
2040 serial2 = serial;
2041 if (*ep == '-') {
2042 cp = ep + 1;
2043 errno = 0;
2044 serial2 = strtoull(cp, &ep, 0);
2045 if (*cp == '\0' || *ep != '\0')
2046 fatal("%s:%lu: invalid serial \"%s\"",
2047 path, lnum, cp);
2048 if (errno == ERANGE && serial2 == ULLONG_MAX)
2049 fatal("%s:%lu: serial out of range",
2050 path, lnum);
2051 if (serial2 <= serial)
2052 fatal("%s:%lu: invalid serial range "
2053 "%llu:%llu", path, lnum,
2054 (unsigned long long)serial,
2055 (unsigned long long)serial2);
2056 }
2057 if (ssh_krl_revoke_cert_by_serial_range(krl,
2058 ca, serial, serial2) != 0) {
2059 fatal("%s: revoke serial failed",
2060 __func__);
2061 }
2062 } else if (strncasecmp(cp, "id:", 3) == 0) {
2063 if (ca == NULL) {
Damien Millerd5d9d7b2013-08-21 02:43:27 +10002064 fatal("revoking certificates by key ID "
Damien Millerf3747bf2013-01-18 11:44:04 +11002065 "requires specification of a CA key");
2066 }
2067 cp += 3;
2068 cp = cp + strspn(cp, " \t");
2069 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2070 fatal("%s: revoke key ID failed", __func__);
2071 } else {
2072 if (strncasecmp(cp, "key:", 4) == 0) {
2073 cp += 4;
2074 cp = cp + strspn(cp, " \t");
2075 was_explicit_key = 1;
2076 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2077 cp += 5;
2078 cp = cp + strspn(cp, " \t");
2079 was_sha1 = 1;
2080 } else {
2081 /*
2082 * Just try to process the line as a key.
2083 * Parsing will fail if it isn't.
2084 */
2085 }
2086 if ((key = key_new(KEY_UNSPEC)) == NULL)
2087 fatal("key_new");
2088 if (key_read(key, &cp) != 1)
2089 fatal("%s:%lu: invalid key", path, lnum);
2090 if (was_explicit_key)
2091 r = ssh_krl_revoke_key_explicit(krl, key);
2092 else if (was_sha1)
2093 r = ssh_krl_revoke_key_sha1(krl, key);
2094 else
2095 r = ssh_krl_revoke_key(krl, key);
2096 if (r != 0)
2097 fatal("%s: revoke key failed", __func__);
2098 key_free(key);
2099 }
2100 }
2101 if (strcmp(path, "-") != 0)
2102 fclose(krl_spec);
Damien Miller0d6771b2013-04-23 15:23:24 +10002103 free(path);
Damien Millerf3747bf2013-01-18 11:44:04 +11002104}
2105
2106static void
2107do_gen_krl(struct passwd *pw, int updating, int argc, char **argv)
2108{
2109 struct ssh_krl *krl;
2110 struct stat sb;
2111 Key *ca = NULL;
2112 int fd, i;
2113 char *tmp;
2114 Buffer kbuf;
2115
2116 if (*identity_file == '\0')
2117 fatal("KRL generation requires an output file");
2118 if (stat(identity_file, &sb) == -1) {
2119 if (errno != ENOENT)
2120 fatal("Cannot access KRL \"%s\": %s",
2121 identity_file, strerror(errno));
2122 if (updating)
2123 fatal("KRL \"%s\" does not exist", identity_file);
2124 }
2125 if (ca_key_path != NULL) {
2126 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2127 if ((ca = key_load_public(tmp, NULL)) == NULL)
2128 fatal("Cannot load CA public key %s", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10002129 free(tmp);
Damien Millerf3747bf2013-01-18 11:44:04 +11002130 }
2131
2132 if (updating)
2133 load_krl(identity_file, &krl);
2134 else if ((krl = ssh_krl_init()) == NULL)
2135 fatal("couldn't create KRL");
2136
2137 if (cert_serial != 0)
2138 ssh_krl_set_version(krl, cert_serial);
2139 if (identity_comment != NULL)
2140 ssh_krl_set_comment(krl, identity_comment);
2141
2142 for (i = 0; i < argc; i++)
2143 update_krl_from_file(pw, argv[i], ca, krl);
2144
2145 buffer_init(&kbuf);
2146 if (ssh_krl_to_blob(krl, &kbuf, NULL, 0) != 0)
2147 fatal("Couldn't generate KRL");
2148 if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2149 fatal("open %s: %s", identity_file, strerror(errno));
2150 if (atomicio(vwrite, fd, buffer_ptr(&kbuf), buffer_len(&kbuf)) !=
2151 buffer_len(&kbuf))
2152 fatal("write %s: %s", identity_file, strerror(errno));
2153 close(fd);
2154 buffer_free(&kbuf);
2155 ssh_krl_free(krl);
Damien Miller0d6771b2013-04-23 15:23:24 +10002156 if (ca != NULL)
2157 key_free(ca);
Damien Millerf3747bf2013-01-18 11:44:04 +11002158}
2159
2160static void
2161do_check_krl(struct passwd *pw, int argc, char **argv)
2162{
2163 int i, r, ret = 0;
2164 char *comment;
2165 struct ssh_krl *krl;
2166 Key *k;
2167
2168 if (*identity_file == '\0')
2169 fatal("KRL checking requires an input file");
2170 load_krl(identity_file, &krl);
2171 for (i = 0; i < argc; i++) {
2172 if ((k = key_load_public(argv[i], &comment)) == NULL)
2173 fatal("Cannot load public key %s", argv[i]);
2174 r = ssh_krl_check_key(krl, k);
2175 printf("%s%s%s%s: %s\n", argv[i],
2176 *comment ? " (" : "", comment, *comment ? ")" : "",
2177 r == 0 ? "ok" : "REVOKED");
2178 if (r != 0)
2179 ret = 1;
2180 key_free(k);
2181 free(comment);
2182 }
2183 ssh_krl_free(krl);
2184 exit(ret);
2185}
Damien Miller1f0311c2014-05-15 14:24:09 +10002186#endif
Damien Millerf3747bf2013-01-18 11:44:04 +11002187
2188static void
Damien Miller431f66b1999-11-21 18:31:57 +11002189usage(void)
2190{
Damien Millerf0858de2014-04-20 13:01:30 +10002191 fprintf(stderr,
2192 "usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1]\n"
2193 " [-N new_passphrase] [-C comment] [-f output_keyfile]\n"
2194 " ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]\n"
2195 " ssh-keygen -i [-m key_format] [-f input_keyfile]\n"
2196 " ssh-keygen -e [-m key_format] [-f input_keyfile]\n"
2197 " ssh-keygen -y [-f input_keyfile]\n"
2198 " ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]\n"
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002199 " ssh-keygen -l [-E fingerprint_hash] [-f input_keyfile]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10002200 " ssh-keygen -B [-f input_keyfile]\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002201#ifdef ENABLE_PKCS11
Damien Millerf0858de2014-04-20 13:01:30 +10002202 fprintf(stderr,
2203 " ssh-keygen -D pkcs11\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002204#endif
Damien Millerf0858de2014-04-20 13:01:30 +10002205 fprintf(stderr,
2206 " ssh-keygen -F hostname [-f known_hosts_file] [-l]\n"
2207 " ssh-keygen -H [-f known_hosts_file]\n"
2208 " ssh-keygen -R hostname [-f known_hosts_file]\n"
2209 " ssh-keygen -r hostname [-f input_keyfile] [-g]\n"
2210 " ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]\n"
2211 " ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]\n"
2212 " [-j start_line] [-K checkpt] [-W generator]\n"
2213 " ssh-keygen -s ca_key -I certificate_identity [-h] [-n principals]\n"
2214 " [-O option] [-V validity_interval] [-z serial_number] file ...\n"
2215 " ssh-keygen -L [-f input_keyfile]\n"
2216 " ssh-keygen -A\n"
2217 " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
2218 " file ...\n"
2219 " ssh-keygen -Q -f krl_file file ...\n");
Damien Miller95def091999-11-25 00:26:21 +11002220 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11002221}
2222
Damien Miller95def091999-11-25 00:26:21 +11002223/*
2224 * Main program for key management.
2225 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002226int
Damien Millerdf8b7db2007-01-05 16:22:57 +11002227main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002228{
Damien Millera41c8b12002-01-22 23:05:08 +11002229 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller390d0562011-10-18 16:05:19 +11002230 char *checkpoint = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11002231 char out_file[MAXPATHLEN], *ep, *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002232 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11002233 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11002234 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11002235 int opt, type, fd;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002236 u_int32_t memory = 0, generator_wanted = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002237 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002238 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
Damien Millerdfceafe2012-07-06 13:44:19 +10002239 unsigned long start_lineno = 0, lines_to_process = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002240 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11002241 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10002242 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002243
Damien Miller95def091999-11-25 00:26:21 +11002244 extern int optind;
2245 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002246
Darren Tuckerce321d82005-10-03 18:11:24 +10002247 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2248 sanitise_stdfd();
2249
Darren Tucker9ac56e92007-01-14 10:19:59 +11002250 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10002251
Damien Miller4314c2b2010-09-10 11:12:09 +10002252 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11002253 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10002254
Kevin Steves3a881912002-07-20 19:05:40 +00002255 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10002256
Damien Miller5428f641999-11-25 11:54:57 +11002257 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11002258 pw = getpwuid(getuid());
2259 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +10002260 printf("No user exists for uid %lu\n", (u_long)getuid());
Damien Miller95def091999-11-25 00:26:21 +11002261 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002262 }
Damien Millereba71ba2000-04-29 23:57:08 +10002263 if (gethostname(hostname, sizeof(hostname)) < 0) {
2264 perror("gethostname");
2265 exit(1);
2266 }
Damien Miller5428f641999-11-25 11:54:57 +11002267
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002268 /* Remaining characters: UYdw */
Damien Millerbcd00ab2013-12-07 10:41:55 +11002269 while ((opt = getopt(argc, argv, "ABHLQXceghiklopquvxy"
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002270 "C:D:E:F:G:I:J:K:M:N:O:P:R:S:T:V:W:Z:"
2271 "a:b:f:g:j:m:n:r:s:t:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11002272 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10002273 case 'A':
2274 gen_all_hostkeys = 1;
2275 break;
Damien Miller95def091999-11-25 00:26:21 +11002276 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10002277 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10002278 if (errstr)
2279 fatal("Bits has bad value %s (%s)",
2280 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11002281 break;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002282 case 'E':
2283 fingerprint_hash = ssh_digest_alg_by_name(optarg);
2284 if (fingerprint_hash == -1)
2285 fatal("Invalid hash algorithm \"%s\"", optarg);
2286 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002287 case 'F':
2288 find_host = 1;
2289 rr_hostname = optarg;
2290 break;
2291 case 'H':
2292 hash_hosts = 1;
2293 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002294 case 'I':
2295 cert_key_id = optarg;
2296 break;
Damien Millerdfceafe2012-07-06 13:44:19 +10002297 case 'J':
2298 lines_to_process = strtoul(optarg, NULL, 10);
2299 break;
2300 case 'j':
2301 start_lineno = strtoul(optarg, NULL, 10);
2302 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002303 case 'R':
2304 delete_host = 1;
2305 rr_hostname = optarg;
2306 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11002307 case 'L':
2308 show_cert = 1;
2309 break;
Damien Miller95def091999-11-25 00:26:21 +11002310 case 'l':
2311 print_fingerprint = 1;
2312 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002313 case 'B':
2314 print_bubblebabble = 1;
2315 break;
Damien Miller44b25042010-07-02 13:35:01 +10002316 case 'm':
2317 if (strcasecmp(optarg, "RFC4716") == 0 ||
2318 strcasecmp(optarg, "ssh2") == 0) {
2319 convert_format = FMT_RFC4716;
2320 break;
2321 }
2322 if (strcasecmp(optarg, "PKCS8") == 0) {
2323 convert_format = FMT_PKCS8;
2324 break;
2325 }
2326 if (strcasecmp(optarg, "PEM") == 0) {
2327 convert_format = FMT_PEM;
2328 break;
2329 }
2330 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002331 case 'n':
2332 cert_principals = optarg;
2333 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002334 case 'o':
2335 use_new_format = 1;
2336 break;
Damien Miller95def091999-11-25 00:26:21 +11002337 case 'p':
2338 change_passphrase = 1;
2339 break;
Damien Miller95def091999-11-25 00:26:21 +11002340 case 'c':
2341 change_comment = 1;
2342 break;
Damien Miller95def091999-11-25 00:26:21 +11002343 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002344 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2345 sizeof(identity_file))
2346 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002347 have_identity = 1;
2348 break;
Damien Miller37876e92003-05-15 10:19:46 +10002349 case 'g':
2350 print_generic = 1;
2351 break;
Damien Miller95def091999-11-25 00:26:21 +11002352 case 'P':
2353 identity_passphrase = optarg;
2354 break;
Damien Miller95def091999-11-25 00:26:21 +11002355 case 'N':
2356 identity_new_passphrase = optarg;
2357 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002358 case 'Q':
2359 check_krl = 1;
2360 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002361 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002362 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002363 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002364 case 'Z':
2365 new_format_cipher = optarg;
2366 break;
Damien Miller95def091999-11-25 00:26:21 +11002367 case 'C':
2368 identity_comment = optarg;
2369 break;
Damien Miller95def091999-11-25 00:26:21 +11002370 case 'q':
2371 quiet = 1;
2372 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002373 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002374 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002375 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002376 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002377 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002378 case 'h':
2379 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002380 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002381 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002382 case 'k':
2383 gen_krl = 1;
2384 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002385 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002386 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002387 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002388 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002389 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002390 case 'y':
2391 print_public = 1;
2392 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002393 case 's':
2394 ca_key_path = optarg;
2395 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002396 case 't':
2397 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002398 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002399 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002400 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002401 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002402 case 'u':
2403 update_krl = 1;
2404 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002405 case 'v':
2406 if (log_level == SYSLOG_LEVEL_INFO)
2407 log_level = SYSLOG_LEVEL_DEBUG1;
2408 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002409 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002410 log_level < SYSLOG_LEVEL_DEBUG3)
2411 log_level++;
2412 }
2413 break;
Damien Miller37876e92003-05-15 10:19:46 +10002414 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002415 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002416 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002417 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002418 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2419 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002420 if (errstr)
2421 fatal("Desired generator has bad value: %s (%s)",
2422 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002423 break;
2424 case 'a':
Damien Millerbcd00ab2013-12-07 10:41:55 +11002425 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002426 if (errstr)
Damien Millerbcd00ab2013-12-07 10:41:55 +11002427 fatal("Invalid number: %s (%s)",
Damien Millerb089fb52005-05-26 12:16:18 +10002428 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002429 break;
2430 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002431 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002432 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002433 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002434 break;
2435 case 'G':
2436 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002437 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2438 sizeof(out_file))
2439 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002440 break;
2441 case 'T':
2442 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002443 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2444 sizeof(out_file))
2445 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002446 break;
Damien Miller390d0562011-10-18 16:05:19 +11002447 case 'K':
2448 if (strlen(optarg) >= MAXPATHLEN)
2449 fatal("Checkpoint filename too long");
2450 checkpoint = xstrdup(optarg);
2451 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002452 case 'S':
2453 /* XXX - also compare length against bits */
2454 if (BN_hex2bn(&start, optarg) == 0)
2455 fatal("Invalid start point.");
2456 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002457 case 'V':
2458 parse_cert_times(optarg);
2459 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002460 case 'z':
Damien Miller6f3b3622012-11-14 19:04:33 +11002461 errno = 0;
2462 cert_serial = strtoull(optarg, &ep, 10);
2463 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2464 (errno == ERANGE && cert_serial == ULLONG_MAX))
2465 fatal("Invalid serial number \"%s\"", optarg);
Damien Miller4e270b02010-04-16 15:56:21 +10002466 break;
Damien Miller95def091999-11-25 00:26:21 +11002467 case '?':
2468 default:
2469 usage();
2470 }
2471 }
Darren Tucker06930c72003-12-31 11:34:51 +11002472
2473 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002474 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002475
Damien Miller0a80ca12010-02-27 07:55:05 +11002476 argv += optind;
2477 argc -= optind;
2478
2479 if (ca_key_path != NULL) {
Damien Millerf3747bf2013-01-18 11:44:04 +11002480 if (argc < 1 && !gen_krl) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002481 printf("Too few arguments.\n");
2482 usage();
2483 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002484 } else if (argc > 0 && !gen_krl && !check_krl) {
Damien Miller95def091999-11-25 00:26:21 +11002485 printf("Too many arguments.\n");
2486 usage();
2487 }
2488 if (change_passphrase && change_comment) {
2489 printf("Can only have one of -p and -c.\n");
2490 usage();
2491 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002492 if (print_fingerprint && (delete_host || hash_hosts)) {
Damien Millerec77c952013-01-09 15:58:00 +11002493 printf("Cannot use -l with -H or -R.\n");
Darren Tucker0f7e9102008-06-08 12:54:29 +10002494 usage();
2495 }
Damien Miller1f0311c2014-05-15 14:24:09 +10002496#ifdef WITH_OPENSSL
Damien Millerf3747bf2013-01-18 11:44:04 +11002497 if (gen_krl) {
2498 do_gen_krl(pw, update_krl, argc, argv);
2499 return (0);
2500 }
2501 if (check_krl) {
2502 do_check_krl(pw, argc, argv);
2503 return (0);
2504 }
Damien Miller1f0311c2014-05-15 14:24:09 +10002505#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11002506 if (ca_key_path != NULL) {
2507 if (cert_key_id == NULL)
2508 fatal("Must specify key id (-I) when certifying");
2509 do_ca_sign(pw, argc, argv);
2510 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002511 if (show_cert)
2512 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002513 if (delete_host || hash_hosts || find_host)
2514 do_known_hosts(pw, rr_hostname);
Damien Millerec77c952013-01-09 15:58:00 +11002515 if (pkcs11provider != NULL)
2516 do_download(pw);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002517 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002518 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002519 if (change_passphrase)
2520 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002521 if (change_comment)
2522 do_change_comment(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002523#ifdef WITH_OPENSSL
Damien Miller44b25042010-07-02 13:35:01 +10002524 if (convert_to)
2525 do_convert_to(pw);
2526 if (convert_from)
2527 do_convert_from(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002528#endif
Damien Millereba71ba2000-04-29 23:57:08 +10002529 if (print_public)
2530 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002531 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002532 unsigned int n = 0;
2533
2534 if (have_identity) {
2535 n = do_print_resource_record(pw,
2536 identity_file, rr_hostname);
2537 if (n == 0) {
2538 perror(identity_file);
2539 exit(1);
2540 }
2541 exit(0);
2542 } else {
2543
2544 n += do_print_resource_record(pw,
2545 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2546 n += do_print_resource_record(pw,
2547 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
Damien Miller3bde12a2012-06-20 21:51:11 +10002548 n += do_print_resource_record(pw,
2549 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
Damien Miller16cd3922014-05-15 13:45:58 +10002550 n += do_print_resource_record(pw,
2551 _PATH_HOST_ED25519_KEY_FILE, rr_hostname);
Damien Millercb314822006-03-26 13:48:01 +11002552 if (n == 0)
2553 fatal("no keys found.");
2554 exit(0);
2555 }
Damien Miller37876e92003-05-15 10:19:46 +10002556 }
Damien Miller95def091999-11-25 00:26:21 +11002557
Darren Tucker019cefe2003-08-02 22:40:07 +10002558 if (do_gen_candidates) {
2559 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002560
Darren Tucker019cefe2003-08-02 22:40:07 +10002561 if (out == NULL) {
2562 error("Couldn't open modulus candidate file \"%s\": %s",
2563 out_file, strerror(errno));
2564 return (1);
2565 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002566 if (bits == 0)
2567 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002568 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002569 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002570
2571 return (0);
2572 }
2573
2574 if (do_screen_candidates) {
2575 FILE *in;
Damien Miller78d22712013-02-12 11:03:36 +11002576 FILE *out = fopen(out_file, "a");
Darren Tucker019cefe2003-08-02 22:40:07 +10002577
2578 if (have_identity && strcmp(identity_file, "-") != 0) {
2579 if ((in = fopen(identity_file, "r")) == NULL) {
2580 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002581 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002582 strerror(errno));
2583 }
2584 } else
2585 in = stdin;
2586
2587 if (out == NULL) {
2588 fatal("Couldn't open moduli file \"%s\": %s",
2589 out_file, strerror(errno));
2590 }
Damien Millerbcd00ab2013-12-07 10:41:55 +11002591 if (prime_test(in, out, rounds == 0 ? 100 : rounds,
2592 generator_wanted, checkpoint,
Damien Millerdfceafe2012-07-06 13:44:19 +10002593 start_lineno, lines_to_process) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002594 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002595 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002596 }
2597
Damien Miller58f1baf2011-05-05 14:06:15 +10002598 if (gen_all_hostkeys) {
2599 do_gen_all_hostkeys(pw);
2600 return (0);
2601 }
2602
Damien Millerf14be5c2005-11-05 15:15:49 +11002603 if (key_type_name == NULL)
2604 key_type_name = "rsa";
2605
Damien Millere39cacc2000-11-29 12:18:44 +11002606 type = key_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002607 type_bits_valid(type, &bits);
2608
Darren Tucker3af2ac52005-11-29 13:10:24 +11002609 if (!quiet)
2610 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002611 private = key_generate(type, bits);
2612 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002613 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002614 exit(1);
2615 }
2616 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002617
2618 if (!have_identity)
2619 ask_filename(pw, "Enter file in which to save the key");
2620
Damien Miller788f2122005-11-05 15:14:59 +11002621 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002622 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2623 pw->pw_dir, _PATH_SSH_USER_DIR);
2624 if (strstr(identity_file, dotsshdir) != NULL) {
2625 if (stat(dotsshdir, &st) < 0) {
2626 if (errno != ENOENT) {
2627 error("Could not stat %s: %s", dotsshdir,
2628 strerror(errno));
2629 } else if (mkdir(dotsshdir, 0700) < 0) {
2630 error("Could not create directory '%s': %s",
2631 dotsshdir, strerror(errno));
2632 } else if (!quiet)
2633 printf("Created directory '%s'.\n", dotsshdir);
2634 }
Damien Miller95def091999-11-25 00:26:21 +11002635 }
2636 /* If the file already exists, ask the user to confirm. */
2637 if (stat(identity_file, &st) >= 0) {
2638 char yesno[3];
2639 printf("%s already exists.\n", identity_file);
2640 printf("Overwrite (y/n)? ");
2641 fflush(stdout);
2642 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2643 exit(1);
2644 if (yesno[0] != 'y' && yesno[0] != 'Y')
2645 exit(1);
2646 }
2647 /* Ask for a passphrase (twice). */
2648 if (identity_passphrase)
2649 passphrase1 = xstrdup(identity_passphrase);
2650 else if (identity_new_passphrase)
2651 passphrase1 = xstrdup(identity_new_passphrase);
2652 else {
2653passphrase_again:
2654 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002655 read_passphrase("Enter passphrase (empty for no "
2656 "passphrase): ", RP_ALLOW_STDIN);
2657 passphrase2 = read_passphrase("Enter same passphrase again: ",
2658 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002659 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002660 /*
2661 * The passphrases do not match. Clear them and
2662 * retry.
2663 */
Damien Millera5103f42014-02-04 11:20:14 +11002664 explicit_bzero(passphrase1, strlen(passphrase1));
2665 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002666 free(passphrase1);
2667 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002668 printf("Passphrases do not match. Try again.\n");
2669 goto passphrase_again;
2670 }
2671 /* Clear the other copy of the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002672 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002673 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002674 }
2675
Damien Miller95def091999-11-25 00:26:21 +11002676 if (identity_comment) {
2677 strlcpy(comment, identity_comment, sizeof(comment));
2678 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002679 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002680 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2681 }
2682
2683 /* Save the key with the given passphrase and comment. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11002684 if (!key_save_private(private, identity_file, passphrase1, comment,
2685 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002686 printf("Saving the key failed: %s.\n", identity_file);
Damien Millera5103f42014-02-04 11:20:14 +11002687 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002688 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002689 exit(1);
2690 }
2691 /* Clear the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002692 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002693 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002694
2695 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002696 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002697
2698 if (!quiet)
2699 printf("Your identification has been saved in %s.\n", identity_file);
2700
Damien Miller95def091999-11-25 00:26:21 +11002701 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002702 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2703 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002704 printf("Could not save your public key in %s\n", identity_file);
2705 exit(1);
2706 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002707 f = fdopen(fd, "w");
2708 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002709 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002710 exit(1);
2711 }
Damien Millereba71ba2000-04-29 23:57:08 +10002712 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002713 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002714 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002715 fclose(f);
2716
2717 if (!quiet) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002718 char *fp = key_fingerprint(public, fingerprint_hash,
2719 SSH_FP_DEFAULT);
2720 char *ra = key_fingerprint(public, fingerprint_hash,
Darren Tucker9c16ac92008-06-13 04:40:35 +10002721 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002722 printf("Your public key has been saved in %s.\n",
2723 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002724 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002725 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002726 printf("The key's randomart image is:\n");
2727 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10002728 free(ra);
2729 free(fp);
Damien Miller95def091999-11-25 00:26:21 +11002730 }
Damien Millereba71ba2000-04-29 23:57:08 +10002731
2732 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002733 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002734}