blob: 085f1ec5596aa96b0f6fdacb988d87adcb3d3d97 [file] [log] [blame]
Damien Miller1f0311c2014-05-15 14:24:09 +10001/* $OpenBSD: ssh-keygen.c,v 1.246 2014/04/29 18:01:49 markus 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"
Ben Lindstromcd392282001-07-04 03:44:03 +000056
Damien Miller3f54a9f2005-11-05 14:52:18 +110057/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
58#define DEFAULT_BITS 2048
59#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100060#define DEFAULT_BITS_ECDSA 256
Damien Miller3f54a9f2005-11-05 14:52:18 +110061u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller5428f641999-11-25 11:54:57 +110063/*
64 * Flag indicating that we just want to change the passphrase. This can be
65 * set on the command line.
66 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067int change_passphrase = 0;
68
Damien Miller5428f641999-11-25 11:54:57 +110069/*
70 * Flag indicating that we just want to change the comment. This can be set
71 * on the command line.
72 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073int change_comment = 0;
74
75int quiet = 0;
76
Darren Tucker35c45532008-06-13 04:43:15 +100077int log_level = SYSLOG_LEVEL_INFO;
78
Damien Miller4b42d7f2005-03-01 21:48:35 +110079/* Flag indicating that we want to hash a known_hosts file */
80int hash_hosts = 0;
81/* Flag indicating that we want lookup a host in known_hosts file */
82int find_host = 0;
83/* Flag indicating that we want to delete a host from a known_hosts file */
84int delete_host = 0;
85
Damien Millerf2b70ca2010-03-05 07:39:35 +110086/* Flag indicating that we want to show the contents of a certificate */
87int show_cert = 0;
88
Damien Miller10f6f6b1999-11-17 17:29:08 +110089/* Flag indicating that we just want to see the key fingerprint */
90int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000091int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110092
Damien Miller431f66b1999-11-21 18:31:57 +110093/* The identity file name, given on the command line or entered by the user. */
94char identity_file[1024];
95int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
97/* This is set to the passphrase if given on the command line. */
98char *identity_passphrase = NULL;
99
100/* This is set to the new passphrase if given on the command line. */
101char *identity_new_passphrase = NULL;
102
103/* This is set to the new comment if given on the command line. */
104char *identity_comment = NULL;
105
Damien Miller0a80ca12010-02-27 07:55:05 +1100106/* Path to CA key when certifying keys. */
107char *ca_key_path = NULL;
108
Damien Miller4e270b02010-04-16 15:56:21 +1000109/* Certificate serial number */
Damien Miller55aca022012-12-03 11:25:30 +1100110unsigned long long cert_serial = 0;
Damien Miller4e270b02010-04-16 15:56:21 +1000111
Damien Miller0a80ca12010-02-27 07:55:05 +1100112/* Key type when certifying */
113u_int cert_key_type = SSH2_CERT_TYPE_USER;
114
115/* "key ID" of signed key */
116char *cert_key_id = NULL;
117
118/* Comma-separated list of principal names for certifying keys */
119char *cert_principals = NULL;
120
121/* Validity period for certificates */
122u_int64_t cert_valid_from = 0;
123u_int64_t cert_valid_to = ~0ULL;
124
Damien Miller4e270b02010-04-16 15:56:21 +1000125/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000126#define CERTOPT_X_FWD (1)
127#define CERTOPT_AGENT_FWD (1<<1)
128#define CERTOPT_PORT_FWD (1<<2)
129#define CERTOPT_PTY (1<<3)
130#define CERTOPT_USER_RC (1<<4)
131#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
132 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
133u_int32_t certflags_flags = CERTOPT_DEFAULT;
134char *certflags_command = NULL;
135char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100136
Damien Miller44b25042010-07-02 13:35:01 +1000137/* Conversion to/from various formats */
138int convert_to = 0;
139int convert_from = 0;
140enum {
141 FMT_RFC4716,
142 FMT_PKCS8,
143 FMT_PEM
144} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000145int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000146int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100147
Damien Millera41c8b12002-01-22 23:05:08 +1100148char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000149
Damien Miller757f34e2010-08-05 13:05:31 +1000150/* Load key from this PKCS#11 provider */
151char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000152
Damien Millerbcd00ab2013-12-07 10:41:55 +1100153/* Use new OpenSSH private key format when writing SSH2 keys instead of PEM */
154int use_new_format = 0;
155
156/* Cipher for new-format private keys */
157char *new_format_cipher = NULL;
158
159/*
160 * Number of KDF rounds to derive new format keys /
161 * number of primality trials when screening moduli.
162 */
163int rounds = 0;
164
Damien Miller431f66b1999-11-21 18:31:57 +1100165/* argv0 */
166extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
Damien Millereba71ba2000-04-29 23:57:08 +1000168char hostname[MAXHOSTNAMELEN];
169
Darren Tucker770fc012004-05-13 16:24:32 +1000170/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000171int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Damien Millerdfceafe2012-07-06 13:44:19 +1000172int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
173 unsigned long);
Darren Tucker770fc012004-05-13 16:24:32 +1000174
Ben Lindstrombba81212001-06-25 05:01:22 +0000175static void
Damien Miller884b63a2011-05-05 14:14:52 +1000176type_bits_valid(int type, u_int32_t *bitsp)
Damien Miller58f1baf2011-05-05 14:06:15 +1000177{
178 u_int maxbits;
179
180 if (type == KEY_UNSPEC) {
181 fprintf(stderr, "unknown key type %s\n", key_type_name);
182 exit(1);
183 }
Damien Miller884b63a2011-05-05 14:14:52 +1000184 if (*bitsp == 0) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000185 if (type == KEY_DSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000186 *bitsp = DEFAULT_BITS_DSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000187 else if (type == KEY_ECDSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000188 *bitsp = DEFAULT_BITS_ECDSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000189 else
Damien Miller884b63a2011-05-05 14:14:52 +1000190 *bitsp = DEFAULT_BITS;
Damien Miller58f1baf2011-05-05 14:06:15 +1000191 }
192 maxbits = (type == KEY_DSA) ?
193 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
Damien Miller884b63a2011-05-05 14:14:52 +1000194 if (*bitsp > maxbits) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000195 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
196 exit(1);
197 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000198#ifdef WITH_OPENSSL
Damien Miller884b63a2011-05-05 14:14:52 +1000199 if (type == KEY_DSA && *bitsp != 1024)
Damien Miller58f1baf2011-05-05 14:06:15 +1000200 fatal("DSA keys must be 1024 bits");
Damien Miller5be9d9e2013-12-07 11:24:01 +1100201 else if (type != KEY_ECDSA && type != KEY_ED25519 && *bitsp < 768)
Damien Miller58f1baf2011-05-05 14:06:15 +1000202 fatal("Key must at least be 768 bits");
Damien Miller884b63a2011-05-05 14:14:52 +1000203 else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(*bitsp) == -1)
Damien Miller58f1baf2011-05-05 14:06:15 +1000204 fatal("Invalid ECDSA key length - valid lengths are "
205 "256, 384 or 521 bits");
Damien Miller1f0311c2014-05-15 14:24:09 +1000206#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000207}
208
209static void
Damien Miller431f66b1999-11-21 18:31:57 +1100210ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211{
Damien Miller95def091999-11-25 00:26:21 +1100212 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100213 char *name = NULL;
214
Damien Miller993dd552002-02-19 15:22:47 +1100215 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000216 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100217 else {
Damien Miller993dd552002-02-19 15:22:47 +1100218 switch (key_type_from_name(key_type_name)) {
219 case KEY_RSA1:
220 name = _PATH_SSH_CLIENT_IDENTITY;
221 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000222 case KEY_DSA_CERT:
223 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100224 case KEY_DSA:
225 name = _PATH_SSH_CLIENT_ID_DSA;
226 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100227#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000228 case KEY_ECDSA_CERT:
229 case KEY_ECDSA:
230 name = _PATH_SSH_CLIENT_ID_ECDSA;
231 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100232#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000233 case KEY_RSA_CERT:
234 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100235 case KEY_RSA:
236 name = _PATH_SSH_CLIENT_ID_RSA;
237 break;
Damien Miller5be9d9e2013-12-07 11:24:01 +1100238 case KEY_ED25519:
239 case KEY_ED25519_CERT:
240 name = _PATH_SSH_CLIENT_ID_ED25519;
241 break;
Damien Miller993dd552002-02-19 15:22:47 +1100242 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100243 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100244 exit(1);
245 break;
246 }
Damien Miller90967402006-03-26 14:07:26 +1100247 }
Damien Millere39cacc2000-11-29 12:18:44 +1100248 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000249 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100250 if (fgets(buf, sizeof(buf), stdin) == NULL)
251 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000252 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100253 if (strcmp(buf, "") != 0)
254 strlcpy(identity_file, buf, sizeof(identity_file));
255 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100256}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Ben Lindstrombba81212001-06-25 05:01:22 +0000258static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000259load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000260{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000261 char *pass;
262 Key *prv;
263
Ben Lindstroma3700052001-04-05 23:26:32 +0000264 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000265 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000266 if (identity_passphrase)
267 pass = xstrdup(identity_passphrase);
268 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000269 pass = read_passphrase("Enter passphrase: ",
270 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000271 prv = key_load_private(filename, pass, NULL);
Damien Millera5103f42014-02-04 11:20:14 +1100272 explicit_bzero(pass, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +1000273 free(pass);
Damien Millereba71ba2000-04-29 23:57:08 +1000274 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000275 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000276}
277
Damien Miller874d77b2000-10-14 16:23:11 +1100278#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000279#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100280#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000281#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000282
Damien Miller1f0311c2014-05-15 14:24:09 +1000283#ifdef WITH_OPENSSL
Ben Lindstrombba81212001-06-25 05:01:22 +0000284static void
Damien Miller44b25042010-07-02 13:35:01 +1000285do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000286{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000287 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000288 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100289 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000290
Damien Millera563cce2012-04-22 11:07:28 +1000291 if (k->type == KEY_RSA1) {
292 fprintf(stderr, "version 1 keys are not supported\n");
293 exit(1);
294 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000295 if (key_to_blob(k, &blob, &len) <= 0) {
296 fprintf(stderr, "key_to_blob failed\n");
297 exit(1);
298 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100299 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
300 snprintf(comment, sizeof(comment),
301 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000302 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000303 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100304
305 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
306 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000307 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100308 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000309 key_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000310 free(blob);
Damien Millereba71ba2000-04-29 23:57:08 +1000311 exit(0);
312}
313
Ben Lindstrombba81212001-06-25 05:01:22 +0000314static void
Damien Miller44b25042010-07-02 13:35:01 +1000315do_convert_to_pkcs8(Key *k)
316{
317 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000318 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000319 case KEY_RSA:
320 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
321 fatal("PEM_write_RSA_PUBKEY failed");
322 break;
323 case KEY_DSA:
324 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
325 fatal("PEM_write_DSA_PUBKEY failed");
326 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000327#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000328 case KEY_ECDSA:
329 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
330 fatal("PEM_write_EC_PUBKEY failed");
331 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000332#endif
Damien Miller44b25042010-07-02 13:35:01 +1000333 default:
334 fatal("%s: unsupported key type %s", __func__, key_type(k));
335 }
336 exit(0);
337}
338
339static void
340do_convert_to_pem(Key *k)
341{
342 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000343 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000344 case KEY_RSA:
345 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
346 fatal("PEM_write_RSAPublicKey failed");
347 break;
348#if notyet /* OpenSSH 0.9.8 lacks this function */
349 case KEY_DSA:
350 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
351 fatal("PEM_write_DSAPublicKey failed");
352 break;
353#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000354 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000355 default:
356 fatal("%s: unsupported key type %s", __func__, key_type(k));
357 }
358 exit(0);
359}
360
361static void
362do_convert_to(struct passwd *pw)
363{
364 Key *k;
365 struct stat st;
366
367 if (!have_identity)
368 ask_filename(pw, "Enter file in which the key is");
369 if (stat(identity_file, &st) < 0)
370 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
371 if ((k = key_load_public(identity_file, NULL)) == NULL) {
372 if ((k = load_identity(identity_file)) == NULL) {
373 fprintf(stderr, "load failed\n");
374 exit(1);
375 }
376 }
Damien Miller44b25042010-07-02 13:35:01 +1000377
378 switch (convert_format) {
379 case FMT_RFC4716:
380 do_convert_to_ssh2(pw, k);
381 break;
382 case FMT_PKCS8:
383 do_convert_to_pkcs8(k);
384 break;
385 case FMT_PEM:
386 do_convert_to_pem(k);
387 break;
388 default:
389 fatal("%s: unknown key format %d", __func__, convert_format);
390 }
391 exit(0);
392}
393
394static void
Damien Miller874d77b2000-10-14 16:23:11 +1100395buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
396{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000397 u_int bignum_bits = buffer_get_int(b);
398 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000399
Damien Miller874d77b2000-10-14 16:23:11 +1100400 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000401 fatal("buffer_get_bignum_bits: input buffer too small: "
402 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100403 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
404 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100405 buffer_consume(b, bytes);
406}
407
Ben Lindstrombba81212001-06-25 05:01:22 +0000408static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000409do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100410{
411 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100412 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100413 char *type, *cipher;
Damien Miller8f9cd702014-04-20 13:00:11 +1000414 u_char *sig = NULL, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000415 int magic, rlen, ktype, i1, i2, i3, i4;
416 u_int slen;
417 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100418
419 buffer_init(&b);
420 buffer_append(&b, blob, blen);
421
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100422 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100423 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
424 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
425 buffer_free(&b);
426 return NULL;
427 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000428 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100429 type = buffer_get_string(&b, NULL);
430 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000431 i2 = buffer_get_int(&b);
432 i3 = buffer_get_int(&b);
433 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100434 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100435 if (strcmp(cipher, "none") != 0) {
436 error("unsupported cipher %s", cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000437 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100438 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000439 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100440 return NULL;
441 }
Darren Tuckera627d422013-06-02 07:31:17 +1000442 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100443
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000444 if (strstr(type, "dsa")) {
445 ktype = KEY_DSA;
446 } else if (strstr(type, "rsa")) {
447 ktype = KEY_RSA;
448 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100449 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000450 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100451 return NULL;
452 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000453 key = key_new_private(ktype);
Darren Tuckera627d422013-06-02 07:31:17 +1000454 free(type);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000455
456 switch (key->type) {
457 case KEY_DSA:
458 buffer_get_bignum_bits(&b, key->dsa->p);
459 buffer_get_bignum_bits(&b, key->dsa->g);
460 buffer_get_bignum_bits(&b, key->dsa->q);
461 buffer_get_bignum_bits(&b, key->dsa->pub_key);
462 buffer_get_bignum_bits(&b, key->dsa->priv_key);
463 break;
464 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100465 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000466 debug("e %lx", e);
467 if (e < 30) {
468 e <<= 8;
469 e += buffer_get_char(&b);
470 debug("e %lx", e);
471 e <<= 8;
472 e += buffer_get_char(&b);
473 debug("e %lx", e);
474 }
475 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000476 buffer_free(&b);
477 key_free(key);
478 return NULL;
479 }
480 buffer_get_bignum_bits(&b, key->rsa->d);
481 buffer_get_bignum_bits(&b, key->rsa->n);
482 buffer_get_bignum_bits(&b, key->rsa->iqmp);
483 buffer_get_bignum_bits(&b, key->rsa->q);
484 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000485 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000486 break;
487 }
Damien Miller874d77b2000-10-14 16:23:11 +1100488 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000489 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000490 error("do_convert_private_ssh2_from_blob: "
491 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100492 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000493
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000494 /* try the key */
495 key_sign(key, &sig, &slen, data, sizeof(data));
496 key_verify(key, sig, slen, data, sizeof(data));
Darren Tuckera627d422013-06-02 07:31:17 +1000497 free(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100498 return key;
499}
500
Damien Miller8056a9d2006-03-15 12:05:40 +1100501static int
502get_line(FILE *fp, char *line, size_t len)
503{
504 int c;
505 size_t pos = 0;
506
507 line[0] = '\0';
508 while ((c = fgetc(fp)) != EOF) {
509 if (pos >= len - 1) {
510 fprintf(stderr, "input line too long.\n");
511 exit(1);
512 }
Damien Miller90967402006-03-26 14:07:26 +1100513 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100514 case '\r':
515 c = fgetc(fp);
516 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
517 fprintf(stderr, "unget: %s\n", strerror(errno));
518 exit(1);
519 }
520 return pos;
521 case '\n':
522 return pos;
523 }
524 line[pos++] = c;
525 line[pos] = '\0';
526 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100527 /* We reached EOF */
528 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100529}
530
Ben Lindstrombba81212001-06-25 05:01:22 +0000531static void
Damien Miller44b25042010-07-02 13:35:01 +1000532do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000533{
Damien Millereba71ba2000-04-29 23:57:08 +1000534 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000535 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100536 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000537 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000538 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000539 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000540 FILE *fp;
541
Damien Millerba3420a2010-06-26 09:39:07 +1000542 if ((fp = fopen(identity_file, "r")) == NULL)
543 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000544 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100545 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
Damien Miller746d1a62013-07-18 16:13:02 +1000546 if (blen > 0 && line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000547 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000548 if (strncmp(line, "----", 4) == 0 ||
549 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100550 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000551 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000552 if (strstr(line, " END ") != NULL) {
553 break;
554 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000555 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000556 continue;
557 }
Damien Miller30c3d422000-05-09 11:02:59 +1000558 if (escaped) {
559 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000560 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000561 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000562 }
Damien Millereba71ba2000-04-29 23:57:08 +1000563 strlcat(encoded, line, sizeof(encoded));
564 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000565 len = strlen(encoded);
566 if (((len % 4) == 3) &&
567 (encoded[len-1] == '=') &&
568 (encoded[len-2] == '=') &&
569 (encoded[len-3] == '='))
570 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100571 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000572 if (blen < 0) {
573 fprintf(stderr, "uudecode failed.\n");
574 exit(1);
575 }
Damien Miller44b25042010-07-02 13:35:01 +1000576 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100577 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100578 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000579 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100580 fprintf(stderr, "decode blob failed.\n");
581 exit(1);
582 }
Damien Miller44b25042010-07-02 13:35:01 +1000583 fclose(fp);
584}
585
586static void
587do_convert_from_pkcs8(Key **k, int *private)
588{
589 EVP_PKEY *pubkey;
590 FILE *fp;
591
592 if ((fp = fopen(identity_file, "r")) == NULL)
593 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
594 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
595 fatal("%s: %s is not a recognised public key format", __func__,
596 identity_file);
597 }
598 fclose(fp);
599 switch (EVP_PKEY_type(pubkey->type)) {
600 case EVP_PKEY_RSA:
601 *k = key_new(KEY_UNSPEC);
602 (*k)->type = KEY_RSA;
603 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
604 break;
605 case EVP_PKEY_DSA:
606 *k = key_new(KEY_UNSPEC);
607 (*k)->type = KEY_DSA;
608 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
609 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000610#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000611 case EVP_PKEY_EC:
612 *k = key_new(KEY_UNSPEC);
613 (*k)->type = KEY_ECDSA;
614 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
Damien Millerb472a902010-11-05 10:19:49 +1100615 (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000616 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000617#endif
Damien Miller44b25042010-07-02 13:35:01 +1000618 default:
619 fatal("%s: unsupported pubkey type %d", __func__,
620 EVP_PKEY_type(pubkey->type));
621 }
622 EVP_PKEY_free(pubkey);
623 return;
624}
625
626static void
627do_convert_from_pem(Key **k, int *private)
628{
629 FILE *fp;
630 RSA *rsa;
631#ifdef notyet
632 DSA *dsa;
633#endif
634
635 if ((fp = fopen(identity_file, "r")) == NULL)
636 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
637 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
638 *k = key_new(KEY_UNSPEC);
639 (*k)->type = KEY_RSA;
640 (*k)->rsa = rsa;
641 fclose(fp);
642 return;
643 }
644#if notyet /* OpenSSH 0.9.8 lacks this function */
645 rewind(fp);
646 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
647 *k = key_new(KEY_UNSPEC);
648 (*k)->type = KEY_DSA;
649 (*k)->dsa = dsa;
650 fclose(fp);
651 return;
652 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000653 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000654#endif
655 fatal("%s: unrecognised raw private key format", __func__);
656}
657
658static void
659do_convert_from(struct passwd *pw)
660{
661 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000662 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000663 struct stat st;
664
665 if (!have_identity)
666 ask_filename(pw, "Enter file in which the key is");
667 if (stat(identity_file, &st) < 0)
668 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
669
670 switch (convert_format) {
671 case FMT_RFC4716:
672 do_convert_from_ssh2(pw, &k, &private);
673 break;
674 case FMT_PKCS8:
675 do_convert_from_pkcs8(&k, &private);
676 break;
677 case FMT_PEM:
678 do_convert_from_pem(&k, &private);
679 break;
680 default:
681 fatal("%s: unknown key format %d", __func__, convert_format);
682 }
683
684 if (!private)
685 ok = key_write(k, stdout);
686 if (ok)
687 fprintf(stdout, "\n");
688 else {
689 switch (k->type) {
690 case KEY_DSA:
691 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
692 NULL, 0, NULL, NULL);
693 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000694#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000695 case KEY_ECDSA:
696 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
697 NULL, 0, NULL, NULL);
698 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000699#endif
Damien Miller44b25042010-07-02 13:35:01 +1000700 case KEY_RSA:
701 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
702 NULL, 0, NULL, NULL);
703 break;
704 default:
705 fatal("%s: unsupported key type %s", __func__,
706 key_type(k));
707 }
708 }
709
Damien Miller874d77b2000-10-14 16:23:11 +1100710 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100711 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100712 exit(1);
713 }
Damien Millereba71ba2000-04-29 23:57:08 +1000714 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000715 exit(0);
716}
Damien Miller1f0311c2014-05-15 14:24:09 +1000717#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000718
Ben Lindstrombba81212001-06-25 05:01:22 +0000719static void
Damien Millereba71ba2000-04-29 23:57:08 +1000720do_print_public(struct passwd *pw)
721{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000722 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000723 struct stat st;
724
725 if (!have_identity)
726 ask_filename(pw, "Enter file in which the key is");
727 if (stat(identity_file, &st) < 0) {
728 perror(identity_file);
729 exit(1);
730 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000731 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000732 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000733 fprintf(stderr, "load failed\n");
734 exit(1);
735 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000736 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000737 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000738 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000739 fprintf(stdout, "\n");
740 exit(0);
741}
742
Ben Lindstromcd392282001-07-04 03:44:03 +0000743static void
Damien Miller757f34e2010-08-05 13:05:31 +1000744do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000745{
Damien Miller7ea845e2010-02-12 09:21:02 +1100746#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000747 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100748 int i, nkeys;
Damien Millerec77c952013-01-09 15:58:00 +1100749 enum fp_rep rep;
750 enum fp_type fptype;
751 char *fp, *ra;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000752
Damien Miller1422c082013-01-09 16:44:54 +1100753 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
754 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
755
Damien Miller7ea845e2010-02-12 09:21:02 +1100756 pkcs11_init(0);
757 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
758 if (nkeys <= 0)
759 fatal("cannot read public key from pkcs11");
760 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100761 if (print_fingerprint) {
762 fp = key_fingerprint(keys[i], fptype, rep);
763 ra = key_fingerprint(keys[i], SSH_FP_MD5,
764 SSH_FP_RANDOMART);
765 printf("%u %s %s (PKCS11 key)\n", key_size(keys[i]),
766 fp, key_type(keys[i]));
767 if (log_level >= SYSLOG_LEVEL_VERBOSE)
768 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000769 free(ra);
770 free(fp);
Damien Millerec77c952013-01-09 15:58:00 +1100771 } else {
772 key_write(keys[i], stdout);
773 fprintf(stdout, "\n");
774 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000775 key_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000776 }
Darren Tuckera627d422013-06-02 07:31:17 +1000777 free(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100778 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000779 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100780#else
781 fatal("no pkcs11 support");
782#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000783}
Ben Lindstromcd392282001-07-04 03:44:03 +0000784
Ben Lindstrombba81212001-06-25 05:01:22 +0000785static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100786do_fingerprint(struct passwd *pw)
787{
Damien Miller98c7ad62000-03-09 21:27:49 +1100788 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000789 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000790 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100791 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000792 enum fp_rep rep;
793 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100794 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100795
Ben Lindstromd0fca422001-03-26 13:44:06 +0000796 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
797 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000798
Damien Miller95def091999-11-25 00:26:21 +1100799 if (!have_identity)
800 ask_filename(pw, "Enter file in which the key is");
801 if (stat(identity_file, &st) < 0) {
802 perror(identity_file);
803 exit(1);
804 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000805 public = key_load_public(identity_file, &comment);
806 if (public != NULL) {
807 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100808 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000809 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
810 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000811 if (log_level >= SYSLOG_LEVEL_VERBOSE)
812 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100813 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +1000814 free(comment);
815 free(ra);
816 free(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100817 exit(0);
818 }
Damien Miller40b59852006-06-13 13:00:25 +1000819 if (comment) {
Darren Tuckera627d422013-06-02 07:31:17 +1000820 free(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000821 comment = NULL;
822 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100823
Damien Millerba3420a2010-06-26 09:39:07 +1000824 if ((f = fopen(identity_file, "r")) == NULL)
825 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100826
Damien Millerba3420a2010-06-26 09:39:07 +1000827 while (fgets(line, sizeof(line), f)) {
828 if ((cp = strchr(line, '\n')) == NULL) {
829 error("line %d too long: %.40s...",
830 num + 1, line);
831 skip = 1;
832 continue;
Damien Miller95def091999-11-25 00:26:21 +1100833 }
Damien Millerba3420a2010-06-26 09:39:07 +1000834 num++;
835 if (skip) {
836 skip = 0;
837 continue;
838 }
839 *cp = '\0';
840
841 /* Skip leading whitespace, empty and comment lines. */
842 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
843 ;
844 if (!*cp || *cp == '\n' || *cp == '#')
845 continue;
846 i = strtol(cp, &ep, 10);
847 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
848 int quoted = 0;
849 comment = cp;
850 for (; *cp && (quoted || (*cp != ' ' &&
851 *cp != '\t')); cp++) {
852 if (*cp == '\\' && cp[1] == '"')
853 cp++; /* Skip both */
854 else if (*cp == '"')
855 quoted = !quoted;
856 }
857 if (!*cp)
858 continue;
859 *cp++ = '\0';
860 }
861 ep = cp;
862 public = key_new(KEY_RSA1);
863 if (key_read(public, &cp) != 1) {
864 cp = ep;
865 key_free(public);
866 public = key_new(KEY_UNSPEC);
867 if (key_read(public, &cp) != 1) {
868 key_free(public);
869 continue;
870 }
871 }
872 comment = *cp ? cp : comment;
873 fp = key_fingerprint(public, fptype, rep);
874 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
875 printf("%u %s %s (%s)\n", key_size(public), fp,
876 comment ? comment : "no comment", key_type(public));
877 if (log_level >= SYSLOG_LEVEL_VERBOSE)
878 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000879 free(ra);
880 free(fp);
Damien Millerba3420a2010-06-26 09:39:07 +1000881 key_free(public);
882 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100883 }
Damien Millerba3420a2010-06-26 09:39:07 +1000884 fclose(f);
885
Damien Miller98c7ad62000-03-09 21:27:49 +1100886 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100887 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100888 exit(1);
889 }
Damien Miller95def091999-11-25 00:26:21 +1100890 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100891}
892
Damien Miller4b42d7f2005-03-01 21:48:35 +1100893static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000894do_gen_all_hostkeys(struct passwd *pw)
895{
896 struct {
897 char *key_type;
898 char *key_type_display;
899 char *path;
900 } key_types[] = {
901 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
902 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
903 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100904#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000905 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100906#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100907 { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
Damien Miller58f1baf2011-05-05 14:06:15 +1000908 { NULL, NULL, NULL }
909 };
910
911 int first = 0;
912 struct stat st;
913 Key *private, *public;
914 char comment[1024];
915 int i, type, fd;
916 FILE *f;
917
918 for (i = 0; key_types[i].key_type; i++) {
919 if (stat(key_types[i].path, &st) == 0)
920 continue;
921 if (errno != ENOENT) {
922 printf("Could not stat %s: %s", key_types[i].path,
923 strerror(errno));
924 first = 0;
925 continue;
926 }
927
928 if (first == 0) {
929 first = 1;
930 printf("%s: generating new host keys: ", __progname);
931 }
932 printf("%s ", key_types[i].key_type_display);
933 fflush(stdout);
Damien Miller58f1baf2011-05-05 14:06:15 +1000934 type = key_type_from_name(key_types[i].key_type);
935 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
936 bits = 0;
937 type_bits_valid(type, &bits);
938 private = key_generate(type, bits);
939 if (private == NULL) {
940 fprintf(stderr, "key_generate failed\n");
941 first = 0;
942 continue;
943 }
944 public = key_from_private(private);
945 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
946 hostname);
Damien Millerbcd00ab2013-12-07 10:41:55 +1100947 if (!key_save_private(private, identity_file, "", comment,
948 use_new_format, new_format_cipher, rounds)) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000949 printf("Saving the key failed: %s.\n", identity_file);
950 key_free(private);
951 key_free(public);
952 first = 0;
953 continue;
954 }
955 key_free(private);
Damien Miller58f1baf2011-05-05 14:06:15 +1000956 strlcat(identity_file, ".pub", sizeof(identity_file));
957 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
958 if (fd == -1) {
959 printf("Could not save your public key in %s\n",
960 identity_file);
961 key_free(public);
962 first = 0;
963 continue;
964 }
965 f = fdopen(fd, "w");
966 if (f == NULL) {
967 printf("fdopen %s failed\n", identity_file);
968 key_free(public);
969 first = 0;
970 continue;
971 }
972 if (!key_write(public, f)) {
973 fprintf(stderr, "write key failed\n");
974 key_free(public);
975 first = 0;
976 continue;
977 }
978 fprintf(f, " %s\n", comment);
979 fclose(f);
980 key_free(public);
981
982 }
983 if (first != 0)
984 printf("\n");
985}
986
987static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100988printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100989{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000990 if (print_fingerprint) {
991 enum fp_rep rep;
992 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000993 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000994
995 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
996 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
997 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100998 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000999 printf("%u %s %s (%s)\n", key_size(public), fp, name,
1000 key_type(public));
1001 if (log_level >= SYSLOG_LEVEL_VERBOSE)
1002 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10001003 free(ra);
1004 free(fp);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001005 } else {
1006 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
1007 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +11001008 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001009 if (!key_write(public, f))
1010 fatal("key_write failed");
1011 fprintf(f, "\n");
1012 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001013}
1014
1015static void
1016do_known_hosts(struct passwd *pw, const char *name)
1017{
1018 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +11001019 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001020 char *cp, *cp2, *kp, *kp2;
1021 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +11001022 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001023 int ca;
Damien Miller66085482013-09-14 09:45:03 +10001024 int found_key = 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001025
1026 if (!have_identity) {
1027 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1028 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1029 sizeof(identity_file))
1030 fatal("Specified known hosts path too long");
Darren Tuckera627d422013-06-02 07:31:17 +10001031 free(cp);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001032 have_identity = 1;
1033 }
1034 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +10001035 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001036
1037 /*
1038 * Find hosts goes to stdout, hash and deletions happen in-place
1039 * A corner case is ssh-keygen -HF foo, which should go to stdout
1040 */
1041 if (!find_host && (hash_hosts || delete_host)) {
1042 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1043 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1044 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1045 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1046 fatal("known_hosts path too long");
1047 umask(077);
1048 if ((c = mkstemp(tmp)) == -1)
1049 fatal("mkstemp: %s", strerror(errno));
1050 if ((out = fdopen(c, "w")) == NULL) {
1051 c = errno;
1052 unlink(tmp);
1053 fatal("fdopen: %s", strerror(c));
1054 }
1055 inplace = 1;
1056 }
1057
1058 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001059 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001060 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001061 skip = 1;
1062 invalid = 1;
1063 continue;
1064 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001065 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001066 if (skip) {
1067 skip = 0;
1068 continue;
1069 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001070 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001071
1072 /* Skip leading whitespace, empty and comment lines. */
1073 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1074 ;
1075 if (!*cp || *cp == '\n' || *cp == '#') {
1076 if (inplace)
1077 fprintf(out, "%s\n", cp);
1078 continue;
1079 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001080 /* Check whether this is a CA key */
1081 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1082 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1083 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1084 ca = 1;
1085 cp += sizeof(CA_MARKER);
1086 } else
1087 ca = 0;
1088
Damien Miller4b42d7f2005-03-01 21:48:35 +11001089 /* Find the end of the host name portion. */
1090 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1091 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001092
Damien Miller4b42d7f2005-03-01 21:48:35 +11001093 if (*kp == '\0' || *(kp + 1) == '\0') {
1094 error("line %d missing key: %.40s...",
1095 num, line);
1096 invalid = 1;
1097 continue;
1098 }
1099 *kp++ = '\0';
1100 kp2 = kp;
1101
Damien Miller0a80ca12010-02-27 07:55:05 +11001102 pub = key_new(KEY_RSA1);
1103 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001104 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +11001105 key_free(pub);
1106 pub = key_new(KEY_UNSPEC);
1107 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001108 error("line %d invalid key: %.40s...",
1109 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001110 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001111 invalid = 1;
1112 continue;
1113 }
1114 }
1115
1116 if (*cp == HASH_DELIM) {
1117 if (find_host || delete_host) {
1118 cp2 = host_hash(name, cp, strlen(cp));
1119 if (cp2 == NULL) {
1120 error("line %d: invalid hashed "
1121 "name: %.64s...", num, line);
1122 invalid = 1;
1123 continue;
1124 }
1125 c = (strcmp(cp2, cp) == 0);
1126 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001127 if (!quiet)
1128 printf("# Host %s found: "
1129 "line %d type %s%s\n", name,
1130 num, key_type(pub),
1131 ca ? " (CA key)" : "");
Damien Miller0a80ca12010-02-27 07:55:05 +11001132 printhost(out, cp, pub, ca, 0);
Damien Miller66085482013-09-14 09:45:03 +10001133 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001134 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001135 if (delete_host) {
1136 if (!c && !ca)
1137 printhost(out, cp, pub, ca, 0);
1138 else
1139 printf("# Host %s found: "
1140 "line %d type %s\n", name,
1141 num, key_type(pub));
1142 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001143 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +11001144 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001145 } else {
1146 if (find_host || delete_host) {
1147 c = (match_hostname(name, cp,
1148 strlen(cp)) == 1);
1149 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001150 if (!quiet)
1151 printf("# Host %s found: "
1152 "line %d type %s%s\n", name,
1153 num, key_type(pub),
1154 ca ? " (CA key)" : "");
Damien Miller0a80ca12010-02-27 07:55:05 +11001155 printhost(out, name, pub,
1156 ca, hash_hosts && !ca);
Damien Miller66085482013-09-14 09:45:03 +10001157 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001158 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001159 if (delete_host) {
1160 if (!c && !ca)
1161 printhost(out, cp, pub, ca, 0);
1162 else
1163 printf("# Host %s found: "
1164 "line %d type %s\n", name,
1165 num, key_type(pub));
1166 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001167 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +11001168 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001169 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001170 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001171 if (ca) {
1172 fprintf(stderr, "Warning: "
1173 "ignoring CA key for host: "
1174 "%.64s\n", cp2);
1175 printhost(out, cp2, pub, ca, 0);
1176 } else if (strcspn(cp2, "*?!") !=
1177 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001178 fprintf(stderr, "Warning: "
1179 "ignoring host name with "
1180 "metacharacters: %.64s\n",
1181 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +11001182 printhost(out, cp2, pub, ca, 0);
1183 } else
1184 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +11001185 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001186 has_unhashed = 1;
1187 }
1188 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001189 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001190 }
1191 fclose(in);
1192
1193 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001194 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001195 identity_file);
1196 if (inplace) {
1197 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001198 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001199 fclose(out);
1200 unlink(tmp);
1201 }
1202 exit(1);
1203 }
1204
1205 if (inplace) {
1206 fclose(out);
1207
1208 /* Backup existing file */
1209 if (unlink(old) == -1 && errno != ENOENT)
1210 fatal("unlink %.100s: %s", old, strerror(errno));
1211 if (link(identity_file, old) == -1)
1212 fatal("link %.100s to %.100s: %s", identity_file, old,
1213 strerror(errno));
1214 /* Move new one into place */
1215 if (rename(tmp, identity_file) == -1) {
1216 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1217 strerror(errno));
1218 unlink(tmp);
1219 unlink(old);
1220 exit(1);
1221 }
1222
1223 fprintf(stderr, "%s updated.\n", identity_file);
1224 fprintf(stderr, "Original contents retained as %s\n", old);
1225 if (has_unhashed) {
1226 fprintf(stderr, "WARNING: %s contains unhashed "
1227 "entries\n", old);
1228 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001229 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001230 }
1231 }
1232
Damien Miller66085482013-09-14 09:45:03 +10001233 exit (find_host && !found_key);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001234}
1235
Damien Miller95def091999-11-25 00:26:21 +11001236/*
1237 * Perform changing a passphrase. The argument is the passwd structure
1238 * for the current user.
1239 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001240static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001241do_change_passphrase(struct passwd *pw)
1242{
Damien Miller95def091999-11-25 00:26:21 +11001243 char *comment;
1244 char *old_passphrase, *passphrase1, *passphrase2;
1245 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001246 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001247
Damien Miller95def091999-11-25 00:26:21 +11001248 if (!have_identity)
1249 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001250 if (stat(identity_file, &st) < 0) {
1251 perror(identity_file);
1252 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253 }
Damien Miller95def091999-11-25 00:26:21 +11001254 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001255 private = key_load_private(identity_file, "", &comment);
1256 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001257 if (identity_passphrase)
1258 old_passphrase = xstrdup(identity_passphrase);
1259 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001260 old_passphrase =
1261 read_passphrase("Enter old passphrase: ",
1262 RP_ALLOW_STDIN);
1263 private = key_load_private(identity_file, old_passphrase,
1264 &comment);
Damien Millera5103f42014-02-04 11:20:14 +11001265 explicit_bzero(old_passphrase, strlen(old_passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001266 free(old_passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001267 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001268 printf("Bad passphrase.\n");
1269 exit(1);
1270 }
Damien Miller95def091999-11-25 00:26:21 +11001271 }
1272 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001273
Damien Miller95def091999-11-25 00:26:21 +11001274 /* Ask the new passphrase (twice). */
1275 if (identity_new_passphrase) {
1276 passphrase1 = xstrdup(identity_new_passphrase);
1277 passphrase2 = NULL;
1278 } else {
1279 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001280 read_passphrase("Enter new passphrase (empty for no "
1281 "passphrase): ", RP_ALLOW_STDIN);
1282 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001283 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001284
1285 /* Verify that they are the same. */
1286 if (strcmp(passphrase1, passphrase2) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001287 explicit_bzero(passphrase1, strlen(passphrase1));
1288 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001289 free(passphrase1);
1290 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001291 printf("Pass phrases do not match. Try again.\n");
1292 exit(1);
1293 }
1294 /* Destroy the other copy. */
Damien Millera5103f42014-02-04 11:20:14 +11001295 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001296 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001297 }
1298
1299 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001300 if (!key_save_private(private, identity_file, passphrase1, comment,
1301 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001302 printf("Saving the key failed: %s.\n", identity_file);
Damien Millera5103f42014-02-04 11:20:14 +11001303 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001304 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001305 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001306 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001307 exit(1);
1308 }
1309 /* Destroy the passphrase and the copy of the key in memory. */
Damien Millera5103f42014-02-04 11:20:14 +11001310 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001311 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001312 key_free(private); /* Destroys contents */
Darren Tuckera627d422013-06-02 07:31:17 +10001313 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001314
1315 printf("Your identification has been saved with the new passphrase.\n");
1316 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001317}
1318
Damien Miller37876e92003-05-15 10:19:46 +10001319/*
1320 * Print the SSHFP RR.
1321 */
Damien Millercb314822006-03-26 13:48:01 +11001322static int
1323do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001324{
1325 Key *public;
1326 char *comment = NULL;
1327 struct stat st;
1328
Damien Millercb314822006-03-26 13:48:01 +11001329 if (fname == NULL)
Damien Miller5bb88332013-07-18 16:13:37 +10001330 fatal("%s: no filename", __func__);
Damien Millercb314822006-03-26 13:48:01 +11001331 if (stat(fname, &st) < 0) {
1332 if (errno == ENOENT)
1333 return 0;
1334 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001335 exit(1);
1336 }
Damien Millercb314822006-03-26 13:48:01 +11001337 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001338 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001339 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001340 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001341 free(comment);
Damien Millercb314822006-03-26 13:48:01 +11001342 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001343 }
1344 if (comment)
Darren Tuckera627d422013-06-02 07:31:17 +10001345 free(comment);
Damien Miller37876e92003-05-15 10:19:46 +10001346
Damien Millercb314822006-03-26 13:48:01 +11001347 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001348 exit(1);
1349}
Damien Miller37876e92003-05-15 10:19:46 +10001350
Damien Miller95def091999-11-25 00:26:21 +11001351/*
1352 * Change the comment of a private key file.
1353 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001354static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355do_change_comment(struct passwd *pw)
1356{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001357 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001358 Key *private;
1359 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001360 struct stat st;
1361 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001362 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363
Damien Miller95def091999-11-25 00:26:21 +11001364 if (!have_identity)
1365 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001366 if (stat(identity_file, &st) < 0) {
1367 perror(identity_file);
1368 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001369 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001370 private = key_load_private(identity_file, "", &comment);
1371 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001372 if (identity_passphrase)
1373 passphrase = xstrdup(identity_passphrase);
1374 else if (identity_new_passphrase)
1375 passphrase = xstrdup(identity_new_passphrase);
1376 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001377 passphrase = read_passphrase("Enter passphrase: ",
1378 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001379 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001380 private = key_load_private(identity_file, passphrase, &comment);
1381 if (private == NULL) {
Damien Millera5103f42014-02-04 11:20:14 +11001382 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001383 free(passphrase);
Damien Miller95def091999-11-25 00:26:21 +11001384 printf("Bad passphrase.\n");
1385 exit(1);
1386 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001387 } else {
1388 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001389 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001390 if (private->type != KEY_RSA1) {
1391 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1392 key_free(private);
1393 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001394 }
Damien Miller95def091999-11-25 00:26:21 +11001395 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396
Damien Miller95def091999-11-25 00:26:21 +11001397 if (identity_comment) {
1398 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1399 } else {
1400 printf("Enter new comment: ");
1401 fflush(stdout);
1402 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
Damien Millera5103f42014-02-04 11:20:14 +11001403 explicit_bzero(passphrase, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001404 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001405 exit(1);
1406 }
Damien Miller14b017d2007-09-17 16:09:15 +10001407 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001408 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001409
Damien Miller95def091999-11-25 00:26:21 +11001410 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001411 if (!key_save_private(private, identity_file, passphrase, new_comment,
1412 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001413 printf("Saving the key failed: %s.\n", identity_file);
Damien Millera5103f42014-02-04 11:20:14 +11001414 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001415 free(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001416 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001417 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001418 exit(1);
1419 }
Damien Millera5103f42014-02-04 11:20:14 +11001420 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001421 free(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001422 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001423 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424
Damien Miller95def091999-11-25 00:26:21 +11001425 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001426 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1427 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001428 printf("Could not save your public key in %s\n", identity_file);
1429 exit(1);
1430 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001431 f = fdopen(fd, "w");
1432 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001433 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001434 exit(1);
1435 }
Damien Millereba71ba2000-04-29 23:57:08 +10001436 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001437 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001438 key_free(public);
1439 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001440 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441
Darren Tuckera627d422013-06-02 07:31:17 +10001442 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001443
1444 printf("The comment in your key file has been changed.\n");
1445 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446}
1447
Damien Miller0a80ca12010-02-27 07:55:05 +11001448static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001449fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001450{
1451 char from[32], to[32];
1452 static char ret[64];
1453 time_t tt;
1454 struct tm *tm;
1455
1456 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001457 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001458 return "forever";
1459
Damien Millerf2b70ca2010-03-05 07:39:35 +11001460 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001461 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001462 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001463 tm = localtime(&tt);
1464 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1465 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001466 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001467 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001468 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001469 tm = localtime(&tt);
1470 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1471 }
1472
Damien Millerf2b70ca2010-03-05 07:39:35 +11001473 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001474 snprintf(ret, sizeof(ret), "before %s", to);
1475 return ret;
1476 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001477 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001478 snprintf(ret, sizeof(ret), "after %s", from);
1479 return ret;
1480 }
1481
1482 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1483 return ret;
1484}
1485
1486static void
Damien Miller4e270b02010-04-16 15:56:21 +10001487add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001488{
1489 debug3("%s: %s", __func__, name);
1490 buffer_put_cstring(c, name);
1491 buffer_put_string(c, NULL, 0);
1492}
1493
1494static void
Damien Miller4e270b02010-04-16 15:56:21 +10001495add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001496{
1497 Buffer b;
1498
1499 debug3("%s: %s=%s", __func__, name, value);
1500 buffer_init(&b);
1501 buffer_put_cstring(&b, value);
1502
1503 buffer_put_cstring(c, name);
1504 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1505
1506 buffer_free(&b);
1507}
1508
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001509#define OPTIONS_CRITICAL 1
1510#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001511static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001512prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001513{
Damien Miller0a80ca12010-02-27 07:55:05 +11001514 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001515 if ((which & OPTIONS_CRITICAL) != 0 &&
1516 certflags_command != NULL)
1517 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001518 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001519 (certflags_flags & CERTOPT_X_FWD) != 0)
1520 add_flag_option(c, "permit-X11-forwarding");
1521 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001522 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001523 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001524 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1525 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001526 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001527 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1528 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001529 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001530 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1531 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001532 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001533 if ((which & OPTIONS_CRITICAL) != 0 &&
1534 certflags_src_addr != NULL)
1535 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001536}
1537
Damien Miller757f34e2010-08-05 13:05:31 +10001538static Key *
1539load_pkcs11_key(char *path)
1540{
1541#ifdef ENABLE_PKCS11
1542 Key **keys = NULL, *public, *private = NULL;
1543 int i, nkeys;
1544
1545 if ((public = key_load_public(path, NULL)) == NULL)
1546 fatal("Couldn't load CA public key \"%s\"", path);
1547
1548 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1549 debug3("%s: %d keys", __func__, nkeys);
1550 if (nkeys <= 0)
1551 fatal("cannot read public key from pkcs11");
1552 for (i = 0; i < nkeys; i++) {
1553 if (key_equal_public(public, keys[i])) {
1554 private = keys[i];
1555 continue;
1556 }
1557 key_free(keys[i]);
1558 }
Darren Tuckera627d422013-06-02 07:31:17 +10001559 free(keys);
Damien Miller757f34e2010-08-05 13:05:31 +10001560 key_free(public);
1561 return private;
1562#else
1563 fatal("no pkcs11 support");
1564#endif /* ENABLE_PKCS11 */
1565}
1566
Damien Miller0a80ca12010-02-27 07:55:05 +11001567static void
1568do_ca_sign(struct passwd *pw, int argc, char **argv)
1569{
1570 int i, fd;
1571 u_int n;
1572 Key *ca, *public;
1573 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1574 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001575 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001576
Damien Miller4e270b02010-04-16 15:56:21 +10001577 if (key_type_name != NULL) {
1578 switch (key_type_from_name(key_type_name)) {
1579 case KEY_RSA_CERT_V00:
1580 case KEY_DSA_CERT_V00:
1581 v00 = 1;
1582 break;
1583 case KEY_UNSPEC:
1584 if (strcasecmp(key_type_name, "v00") == 0) {
1585 v00 = 1;
1586 break;
1587 } else if (strcasecmp(key_type_name, "v01") == 0)
1588 break;
1589 /* FALLTHROUGH */
1590 default:
1591 fprintf(stderr, "unknown key type %s\n", key_type_name);
1592 exit(1);
1593 }
1594 }
1595
Damien Miller1f0311c2014-05-15 14:24:09 +10001596#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001597 pkcs11_init(1);
Damien Miller1f0311c2014-05-15 14:24:09 +10001598#endif
Damien Miller757f34e2010-08-05 13:05:31 +10001599 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1600 if (pkcs11provider != NULL) {
1601 if ((ca = load_pkcs11_key(tmp)) == NULL)
1602 fatal("No PKCS#11 key matching %s found", ca_key_path);
1603 } else if ((ca = load_identity(tmp)) == NULL)
1604 fatal("Couldn't load CA key \"%s\"", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001605 free(tmp);
Damien Miller757f34e2010-08-05 13:05:31 +10001606
Damien Miller0a80ca12010-02-27 07:55:05 +11001607 for (i = 0; i < argc; i++) {
1608 /* Split list of principals */
1609 n = 0;
1610 if (cert_principals != NULL) {
1611 otmp = tmp = xstrdup(cert_principals);
1612 plist = NULL;
1613 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1614 plist = xrealloc(plist, n + 1, sizeof(*plist));
1615 if (*(plist[n] = xstrdup(cp)) == '\0')
1616 fatal("Empty principal name");
1617 }
Darren Tuckera627d422013-06-02 07:31:17 +10001618 free(otmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001619 }
1620
1621 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1622 if ((public = key_load_public(tmp, &comment)) == NULL)
1623 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001624 if (public->type != KEY_RSA && public->type != KEY_DSA &&
Damien Miller5be9d9e2013-12-07 11:24:01 +11001625 public->type != KEY_ECDSA && public->type != KEY_ED25519)
Damien Miller0a80ca12010-02-27 07:55:05 +11001626 fatal("%s: key \"%s\" type %s cannot be certified",
1627 __func__, tmp, key_type(public));
1628
1629 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001630 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001631 fatal("Could not upgrade key %s to certificate", tmp);
1632 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001633 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001634 public->cert->key_id = xstrdup(cert_key_id);
1635 public->cert->nprincipals = n;
1636 public->cert->principals = plist;
1637 public->cert->valid_after = cert_valid_from;
1638 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001639 if (v00) {
1640 prepare_options_buf(&public->cert->critical,
1641 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1642 } else {
1643 prepare_options_buf(&public->cert->critical,
1644 OPTIONS_CRITICAL);
1645 prepare_options_buf(&public->cert->extensions,
1646 OPTIONS_EXTENSIONS);
1647 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001648 public->cert->signature_key = key_from_private(ca);
1649
1650 if (key_certify(public, ca) != 0)
1651 fatal("Couldn't not certify key %s", tmp);
1652
1653 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1654 *cp = '\0';
1655 xasprintf(&out, "%s-cert.pub", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001656 free(tmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001657
1658 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1659 fatal("Could not open \"%s\" for writing: %s", out,
1660 strerror(errno));
1661 if ((f = fdopen(fd, "w")) == NULL)
1662 fatal("%s: fdopen: %s", __func__, strerror(errno));
1663 if (!key_write(public, f))
1664 fatal("Could not write certified key to %s", out);
1665 fprintf(f, " %s\n", comment);
1666 fclose(f);
1667
Damien Miller4e270b02010-04-16 15:56:21 +10001668 if (!quiet) {
1669 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1670 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001671 out, public->cert->key_id,
1672 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001673 cert_principals != NULL ? " for " : "",
1674 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001675 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001676 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001677
1678 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001679 free(out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001680 }
Damien Miller1f0311c2014-05-15 14:24:09 +10001681#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001682 pkcs11_terminate();
Damien Miller1f0311c2014-05-15 14:24:09 +10001683#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11001684 exit(0);
1685}
1686
1687static u_int64_t
1688parse_relative_time(const char *s, time_t now)
1689{
1690 int64_t mul, secs;
1691
1692 mul = *s == '-' ? -1 : 1;
1693
1694 if ((secs = convtime(s + 1)) == -1)
1695 fatal("Invalid relative certificate time %s", s);
1696 if (mul == -1 && secs > now)
1697 fatal("Certificate time %s cannot be represented", s);
1698 return now + (u_int64_t)(secs * mul);
1699}
1700
1701static u_int64_t
1702parse_absolute_time(const char *s)
1703{
1704 struct tm tm;
1705 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001706 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001707
Damien Miller2ca342b2010-03-03 12:14:15 +11001708 /*
1709 * POSIX strptime says "The application shall ensure that there
1710 * is white-space or other non-alphanumeric characters between
1711 * any two conversion specifications" so arrange things this way.
1712 */
1713 switch (strlen(s)) {
1714 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001715 fmt = "%Y-%m-%d";
1716 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001717 break;
1718 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001719 fmt = "%Y-%m-%dT%H:%M:%S";
1720 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001721 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1722 break;
1723 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001724 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001725 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001726
Damien Miller1d2c4562014-02-04 11:18:20 +11001727 memset(&tm, 0, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001728 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001729 fatal("Invalid certificate time %s", s);
1730 if ((tt = mktime(&tm)) < 0)
1731 fatal("Certificate time %s cannot be represented", s);
1732 return (u_int64_t)tt;
1733}
1734
1735static void
1736parse_cert_times(char *timespec)
1737{
1738 char *from, *to;
1739 time_t now = time(NULL);
1740 int64_t secs;
1741
1742 /* +timespec relative to now */
1743 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1744 if ((secs = convtime(timespec + 1)) == -1)
1745 fatal("Invalid relative certificate life %s", timespec);
1746 cert_valid_to = now + secs;
1747 /*
1748 * Backdate certificate one minute to avoid problems on hosts
1749 * with poorly-synchronised clocks.
1750 */
1751 cert_valid_from = ((now - 59)/ 60) * 60;
1752 return;
1753 }
1754
1755 /*
1756 * from:to, where
1757 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1758 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1759 */
1760 from = xstrdup(timespec);
1761 to = strchr(from, ':');
1762 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001763 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001764 *to++ = '\0';
1765
1766 if (*from == '-' || *from == '+')
1767 cert_valid_from = parse_relative_time(from, now);
1768 else
1769 cert_valid_from = parse_absolute_time(from);
1770
1771 if (*to == '-' || *to == '+')
Damien Miller5b01b0d2013-10-23 16:31:31 +11001772 cert_valid_to = parse_relative_time(to, now);
Damien Miller0a80ca12010-02-27 07:55:05 +11001773 else
1774 cert_valid_to = parse_absolute_time(to);
1775
1776 if (cert_valid_to <= cert_valid_from)
1777 fatal("Empty certificate validity interval");
Darren Tuckera627d422013-06-02 07:31:17 +10001778 free(from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001779}
1780
1781static void
Damien Miller4e270b02010-04-16 15:56:21 +10001782add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001783{
1784 char *val;
1785
Damien Miller044f4a62011-05-05 14:14:08 +10001786 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001787 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001788 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001789 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001790 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001791 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001792 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001793 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001794 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001795 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001796 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001797 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001798 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001799 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001800 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001801 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001802 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001803 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001804 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001805 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001806 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001807 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001808 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1809 val = opt + 14;
1810 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001811 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001812 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001813 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001814 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001815 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1816 val = opt + 15;
1817 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001818 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001819 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001820 fatal("source-address already specified");
1821 if (addr_match_cidr_list(NULL, val) != 0)
1822 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001823 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001824 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001825 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001826}
1827
Ben Lindstrombba81212001-06-25 05:01:22 +00001828static void
Damien Millerd834d352010-06-26 09:48:02 +10001829show_options(const Buffer *optbuf, int v00, int in_critical)
1830{
Damien Miller633de332014-05-15 13:48:26 +10001831 char *name, *arg;
1832 const u_char *data;
Damien Millerd834d352010-06-26 09:48:02 +10001833 u_int dlen;
1834 Buffer options, option;
1835
1836 buffer_init(&options);
1837 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1838
1839 buffer_init(&option);
1840 while (buffer_len(&options) != 0) {
1841 name = buffer_get_string(&options, NULL);
1842 data = buffer_get_string_ptr(&options, &dlen);
1843 buffer_append(&option, data, dlen);
1844 printf(" %s", name);
1845 if ((v00 || !in_critical) &&
1846 (strcmp(name, "permit-X11-forwarding") == 0 ||
1847 strcmp(name, "permit-agent-forwarding") == 0 ||
1848 strcmp(name, "permit-port-forwarding") == 0 ||
1849 strcmp(name, "permit-pty") == 0 ||
1850 strcmp(name, "permit-user-rc") == 0))
1851 printf("\n");
1852 else if ((v00 || in_critical) &&
1853 (strcmp(name, "force-command") == 0 ||
1854 strcmp(name, "source-address") == 0)) {
Damien Miller633de332014-05-15 13:48:26 +10001855 arg = buffer_get_cstring(&option, NULL);
1856 printf(" %s\n", arg);
1857 free(arg);
Damien Millerd834d352010-06-26 09:48:02 +10001858 } else {
1859 printf(" UNKNOWN OPTION (len %u)\n",
1860 buffer_len(&option));
1861 buffer_clear(&option);
1862 }
Darren Tuckera627d422013-06-02 07:31:17 +10001863 free(name);
Damien Millerd834d352010-06-26 09:48:02 +10001864 if (buffer_len(&option) != 0)
1865 fatal("Option corrupt: extra data at end");
1866 }
1867 buffer_free(&option);
1868 buffer_free(&options);
1869}
1870
1871static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001872do_show_cert(struct passwd *pw)
1873{
1874 Key *key;
1875 struct stat st;
1876 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001877 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001878
1879 if (!have_identity)
1880 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001881 if (stat(identity_file, &st) < 0)
1882 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001883 if ((key = key_load_public(identity_file, NULL)) == NULL)
1884 fatal("%s is not a public key", identity_file);
1885 if (!key_is_cert(key))
1886 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001887 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1888
Damien Millerf2b70ca2010-03-05 07:39:35 +11001889 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1890 ca_fp = key_fingerprint(key->cert->signature_key,
1891 SSH_FP_MD5, SSH_FP_HEX);
1892
1893 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001894 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1895 key_cert_type(key));
1896 printf(" Public key: %s %s\n", key_type(key), key_fp);
1897 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001898 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001899 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001900 if (!v00) {
1901 printf(" Serial: %llu\n",
1902 (unsigned long long)key->cert->serial);
1903 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001904 printf(" Valid: %s\n",
1905 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1906 printf(" Principals: ");
1907 if (key->cert->nprincipals == 0)
1908 printf("(none)\n");
1909 else {
1910 for (i = 0; i < key->cert->nprincipals; i++)
1911 printf("\n %s",
1912 key->cert->principals[i]);
1913 printf("\n");
1914 }
Damien Miller4e270b02010-04-16 15:56:21 +10001915 printf(" Critical Options: ");
1916 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001917 printf("(none)\n");
1918 else {
1919 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001920 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001921 }
Damien Miller4e270b02010-04-16 15:56:21 +10001922 if (!v00) {
1923 printf(" Extensions: ");
1924 if (buffer_len(&key->cert->extensions) == 0)
1925 printf("(none)\n");
1926 else {
1927 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001928 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001929 }
1930 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001931 exit(0);
1932}
1933
Damien Miller1f0311c2014-05-15 14:24:09 +10001934#ifdef WITH_OPENSSL
Damien Millerf2b70ca2010-03-05 07:39:35 +11001935static void
Damien Millerf3747bf2013-01-18 11:44:04 +11001936load_krl(const char *path, struct ssh_krl **krlp)
1937{
1938 Buffer krlbuf;
1939 int fd;
1940
1941 buffer_init(&krlbuf);
1942 if ((fd = open(path, O_RDONLY)) == -1)
1943 fatal("open %s: %s", path, strerror(errno));
1944 if (!key_load_file(fd, path, &krlbuf))
1945 fatal("Unable to load KRL");
1946 close(fd);
1947 /* XXX check sigs */
1948 if (ssh_krl_from_blob(&krlbuf, krlp, NULL, 0) != 0 ||
1949 *krlp == NULL)
1950 fatal("Invalid KRL file");
1951 buffer_free(&krlbuf);
1952}
1953
1954static void
1955update_krl_from_file(struct passwd *pw, const char *file, const Key *ca,
1956 struct ssh_krl *krl)
1957{
1958 Key *key = NULL;
1959 u_long lnum = 0;
1960 char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES];
1961 unsigned long long serial, serial2;
1962 int i, was_explicit_key, was_sha1, r;
1963 FILE *krl_spec;
1964
1965 path = tilde_expand_filename(file, pw->pw_uid);
1966 if (strcmp(path, "-") == 0) {
1967 krl_spec = stdin;
1968 free(path);
1969 path = xstrdup("(standard input)");
1970 } else if ((krl_spec = fopen(path, "r")) == NULL)
1971 fatal("fopen %s: %s", path, strerror(errno));
1972
1973 if (!quiet)
1974 printf("Revoking from %s\n", path);
1975 while (read_keyfile_line(krl_spec, path, line, sizeof(line),
1976 &lnum) == 0) {
1977 was_explicit_key = was_sha1 = 0;
1978 cp = line + strspn(line, " \t");
1979 /* Trim trailing space, comments and strip \n */
1980 for (i = 0, r = -1; cp[i] != '\0'; i++) {
1981 if (cp[i] == '#' || cp[i] == '\n') {
1982 cp[i] = '\0';
1983 break;
1984 }
1985 if (cp[i] == ' ' || cp[i] == '\t') {
1986 /* Remember the start of a span of whitespace */
1987 if (r == -1)
1988 r = i;
1989 } else
1990 r = -1;
1991 }
1992 if (r != -1)
1993 cp[r] = '\0';
1994 if (*cp == '\0')
1995 continue;
1996 if (strncasecmp(cp, "serial:", 7) == 0) {
1997 if (ca == NULL) {
Damien Millerd234afb2013-08-21 02:42:58 +10001998 fatal("revoking certificates by serial number "
Damien Millerf3747bf2013-01-18 11:44:04 +11001999 "requires specification of a CA key");
2000 }
2001 cp += 7;
2002 cp = cp + strspn(cp, " \t");
2003 errno = 0;
2004 serial = strtoull(cp, &ep, 0);
2005 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2006 fatal("%s:%lu: invalid serial \"%s\"",
2007 path, lnum, cp);
2008 if (errno == ERANGE && serial == ULLONG_MAX)
2009 fatal("%s:%lu: serial out of range",
2010 path, lnum);
2011 serial2 = serial;
2012 if (*ep == '-') {
2013 cp = ep + 1;
2014 errno = 0;
2015 serial2 = strtoull(cp, &ep, 0);
2016 if (*cp == '\0' || *ep != '\0')
2017 fatal("%s:%lu: invalid serial \"%s\"",
2018 path, lnum, cp);
2019 if (errno == ERANGE && serial2 == ULLONG_MAX)
2020 fatal("%s:%lu: serial out of range",
2021 path, lnum);
2022 if (serial2 <= serial)
2023 fatal("%s:%lu: invalid serial range "
2024 "%llu:%llu", path, lnum,
2025 (unsigned long long)serial,
2026 (unsigned long long)serial2);
2027 }
2028 if (ssh_krl_revoke_cert_by_serial_range(krl,
2029 ca, serial, serial2) != 0) {
2030 fatal("%s: revoke serial failed",
2031 __func__);
2032 }
2033 } else if (strncasecmp(cp, "id:", 3) == 0) {
2034 if (ca == NULL) {
Damien Millerd5d9d7b2013-08-21 02:43:27 +10002035 fatal("revoking certificates by key ID "
Damien Millerf3747bf2013-01-18 11:44:04 +11002036 "requires specification of a CA key");
2037 }
2038 cp += 3;
2039 cp = cp + strspn(cp, " \t");
2040 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2041 fatal("%s: revoke key ID failed", __func__);
2042 } else {
2043 if (strncasecmp(cp, "key:", 4) == 0) {
2044 cp += 4;
2045 cp = cp + strspn(cp, " \t");
2046 was_explicit_key = 1;
2047 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2048 cp += 5;
2049 cp = cp + strspn(cp, " \t");
2050 was_sha1 = 1;
2051 } else {
2052 /*
2053 * Just try to process the line as a key.
2054 * Parsing will fail if it isn't.
2055 */
2056 }
2057 if ((key = key_new(KEY_UNSPEC)) == NULL)
2058 fatal("key_new");
2059 if (key_read(key, &cp) != 1)
2060 fatal("%s:%lu: invalid key", path, lnum);
2061 if (was_explicit_key)
2062 r = ssh_krl_revoke_key_explicit(krl, key);
2063 else if (was_sha1)
2064 r = ssh_krl_revoke_key_sha1(krl, key);
2065 else
2066 r = ssh_krl_revoke_key(krl, key);
2067 if (r != 0)
2068 fatal("%s: revoke key failed", __func__);
2069 key_free(key);
2070 }
2071 }
2072 if (strcmp(path, "-") != 0)
2073 fclose(krl_spec);
Damien Miller0d6771b2013-04-23 15:23:24 +10002074 free(path);
Damien Millerf3747bf2013-01-18 11:44:04 +11002075}
2076
2077static void
2078do_gen_krl(struct passwd *pw, int updating, int argc, char **argv)
2079{
2080 struct ssh_krl *krl;
2081 struct stat sb;
2082 Key *ca = NULL;
2083 int fd, i;
2084 char *tmp;
2085 Buffer kbuf;
2086
2087 if (*identity_file == '\0')
2088 fatal("KRL generation requires an output file");
2089 if (stat(identity_file, &sb) == -1) {
2090 if (errno != ENOENT)
2091 fatal("Cannot access KRL \"%s\": %s",
2092 identity_file, strerror(errno));
2093 if (updating)
2094 fatal("KRL \"%s\" does not exist", identity_file);
2095 }
2096 if (ca_key_path != NULL) {
2097 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2098 if ((ca = key_load_public(tmp, NULL)) == NULL)
2099 fatal("Cannot load CA public key %s", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10002100 free(tmp);
Damien Millerf3747bf2013-01-18 11:44:04 +11002101 }
2102
2103 if (updating)
2104 load_krl(identity_file, &krl);
2105 else if ((krl = ssh_krl_init()) == NULL)
2106 fatal("couldn't create KRL");
2107
2108 if (cert_serial != 0)
2109 ssh_krl_set_version(krl, cert_serial);
2110 if (identity_comment != NULL)
2111 ssh_krl_set_comment(krl, identity_comment);
2112
2113 for (i = 0; i < argc; i++)
2114 update_krl_from_file(pw, argv[i], ca, krl);
2115
2116 buffer_init(&kbuf);
2117 if (ssh_krl_to_blob(krl, &kbuf, NULL, 0) != 0)
2118 fatal("Couldn't generate KRL");
2119 if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2120 fatal("open %s: %s", identity_file, strerror(errno));
2121 if (atomicio(vwrite, fd, buffer_ptr(&kbuf), buffer_len(&kbuf)) !=
2122 buffer_len(&kbuf))
2123 fatal("write %s: %s", identity_file, strerror(errno));
2124 close(fd);
2125 buffer_free(&kbuf);
2126 ssh_krl_free(krl);
Damien Miller0d6771b2013-04-23 15:23:24 +10002127 if (ca != NULL)
2128 key_free(ca);
Damien Millerf3747bf2013-01-18 11:44:04 +11002129}
2130
2131static void
2132do_check_krl(struct passwd *pw, int argc, char **argv)
2133{
2134 int i, r, ret = 0;
2135 char *comment;
2136 struct ssh_krl *krl;
2137 Key *k;
2138
2139 if (*identity_file == '\0')
2140 fatal("KRL checking requires an input file");
2141 load_krl(identity_file, &krl);
2142 for (i = 0; i < argc; i++) {
2143 if ((k = key_load_public(argv[i], &comment)) == NULL)
2144 fatal("Cannot load public key %s", argv[i]);
2145 r = ssh_krl_check_key(krl, k);
2146 printf("%s%s%s%s: %s\n", argv[i],
2147 *comment ? " (" : "", comment, *comment ? ")" : "",
2148 r == 0 ? "ok" : "REVOKED");
2149 if (r != 0)
2150 ret = 1;
2151 key_free(k);
2152 free(comment);
2153 }
2154 ssh_krl_free(krl);
2155 exit(ret);
2156}
Damien Miller1f0311c2014-05-15 14:24:09 +10002157#endif
Damien Millerf3747bf2013-01-18 11:44:04 +11002158
2159static void
Damien Miller431f66b1999-11-21 18:31:57 +11002160usage(void)
2161{
Damien Millerf0858de2014-04-20 13:01:30 +10002162 fprintf(stderr,
2163 "usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1]\n"
2164 " [-N new_passphrase] [-C comment] [-f output_keyfile]\n"
2165 " ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]\n"
2166 " ssh-keygen -i [-m key_format] [-f input_keyfile]\n"
2167 " ssh-keygen -e [-m key_format] [-f input_keyfile]\n"
2168 " ssh-keygen -y [-f input_keyfile]\n"
2169 " ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]\n"
2170 " ssh-keygen -l [-f input_keyfile]\n"
2171 " ssh-keygen -B [-f input_keyfile]\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002172#ifdef ENABLE_PKCS11
Damien Millerf0858de2014-04-20 13:01:30 +10002173 fprintf(stderr,
2174 " ssh-keygen -D pkcs11\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002175#endif
Damien Millerf0858de2014-04-20 13:01:30 +10002176 fprintf(stderr,
2177 " ssh-keygen -F hostname [-f known_hosts_file] [-l]\n"
2178 " ssh-keygen -H [-f known_hosts_file]\n"
2179 " ssh-keygen -R hostname [-f known_hosts_file]\n"
2180 " ssh-keygen -r hostname [-f input_keyfile] [-g]\n"
2181 " ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]\n"
2182 " ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]\n"
2183 " [-j start_line] [-K checkpt] [-W generator]\n"
2184 " ssh-keygen -s ca_key -I certificate_identity [-h] [-n principals]\n"
2185 " [-O option] [-V validity_interval] [-z serial_number] file ...\n"
2186 " ssh-keygen -L [-f input_keyfile]\n"
2187 " ssh-keygen -A\n"
2188 " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
2189 " file ...\n"
2190 " ssh-keygen -Q -f krl_file file ...\n");
Damien Miller95def091999-11-25 00:26:21 +11002191 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11002192}
2193
Damien Miller95def091999-11-25 00:26:21 +11002194/*
2195 * Main program for key management.
2196 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002197int
Damien Millerdf8b7db2007-01-05 16:22:57 +11002198main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002199{
Damien Millera41c8b12002-01-22 23:05:08 +11002200 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller390d0562011-10-18 16:05:19 +11002201 char *checkpoint = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11002202 char out_file[MAXPATHLEN], *ep, *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002203 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11002204 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11002205 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11002206 int opt, type, fd;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002207 u_int32_t memory = 0, generator_wanted = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002208 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002209 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
Damien Millerdfceafe2012-07-06 13:44:19 +10002210 unsigned long start_lineno = 0, lines_to_process = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002211 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11002212 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10002213 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002214
Damien Miller95def091999-11-25 00:26:21 +11002215 extern int optind;
2216 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002217
Darren Tuckerce321d82005-10-03 18:11:24 +10002218 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2219 sanitise_stdfd();
2220
Darren Tucker9ac56e92007-01-14 10:19:59 +11002221 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10002222
Damien Miller4314c2b2010-09-10 11:12:09 +10002223 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11002224 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10002225
Kevin Steves3a881912002-07-20 19:05:40 +00002226 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10002227
Damien Miller5428f641999-11-25 11:54:57 +11002228 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11002229 pw = getpwuid(getuid());
2230 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +10002231 printf("No user exists for uid %lu\n", (u_long)getuid());
Damien Miller95def091999-11-25 00:26:21 +11002232 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002233 }
Damien Millereba71ba2000-04-29 23:57:08 +10002234 if (gethostname(hostname, sizeof(hostname)) < 0) {
2235 perror("gethostname");
2236 exit(1);
2237 }
Damien Miller5428f641999-11-25 11:54:57 +11002238
Damien Millerbcd00ab2013-12-07 10:41:55 +11002239 /* Remaining characters: EUYdw */
2240 while ((opt = getopt(argc, argv, "ABHLQXceghiklopquvxy"
2241 "C:D:F:G:I:J:K:M:N:O:P:R:S:T:V:W:Z:a:b:f:g:j:m:n:r:s:t:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11002242 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10002243 case 'A':
2244 gen_all_hostkeys = 1;
2245 break;
Damien Miller95def091999-11-25 00:26:21 +11002246 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10002247 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10002248 if (errstr)
2249 fatal("Bits has bad value %s (%s)",
2250 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11002251 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002252 case 'F':
2253 find_host = 1;
2254 rr_hostname = optarg;
2255 break;
2256 case 'H':
2257 hash_hosts = 1;
2258 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002259 case 'I':
2260 cert_key_id = optarg;
2261 break;
Damien Millerdfceafe2012-07-06 13:44:19 +10002262 case 'J':
2263 lines_to_process = strtoul(optarg, NULL, 10);
2264 break;
2265 case 'j':
2266 start_lineno = strtoul(optarg, NULL, 10);
2267 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002268 case 'R':
2269 delete_host = 1;
2270 rr_hostname = optarg;
2271 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11002272 case 'L':
2273 show_cert = 1;
2274 break;
Damien Miller95def091999-11-25 00:26:21 +11002275 case 'l':
2276 print_fingerprint = 1;
2277 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002278 case 'B':
2279 print_bubblebabble = 1;
2280 break;
Damien Miller44b25042010-07-02 13:35:01 +10002281 case 'm':
2282 if (strcasecmp(optarg, "RFC4716") == 0 ||
2283 strcasecmp(optarg, "ssh2") == 0) {
2284 convert_format = FMT_RFC4716;
2285 break;
2286 }
2287 if (strcasecmp(optarg, "PKCS8") == 0) {
2288 convert_format = FMT_PKCS8;
2289 break;
2290 }
2291 if (strcasecmp(optarg, "PEM") == 0) {
2292 convert_format = FMT_PEM;
2293 break;
2294 }
2295 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002296 case 'n':
2297 cert_principals = optarg;
2298 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002299 case 'o':
2300 use_new_format = 1;
2301 break;
Damien Miller95def091999-11-25 00:26:21 +11002302 case 'p':
2303 change_passphrase = 1;
2304 break;
Damien Miller95def091999-11-25 00:26:21 +11002305 case 'c':
2306 change_comment = 1;
2307 break;
Damien Miller95def091999-11-25 00:26:21 +11002308 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002309 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2310 sizeof(identity_file))
2311 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002312 have_identity = 1;
2313 break;
Damien Miller37876e92003-05-15 10:19:46 +10002314 case 'g':
2315 print_generic = 1;
2316 break;
Damien Miller95def091999-11-25 00:26:21 +11002317 case 'P':
2318 identity_passphrase = optarg;
2319 break;
Damien Miller95def091999-11-25 00:26:21 +11002320 case 'N':
2321 identity_new_passphrase = optarg;
2322 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002323 case 'Q':
2324 check_krl = 1;
2325 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002326 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002327 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002328 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002329 case 'Z':
2330 new_format_cipher = optarg;
2331 break;
Damien Miller95def091999-11-25 00:26:21 +11002332 case 'C':
2333 identity_comment = optarg;
2334 break;
Damien Miller95def091999-11-25 00:26:21 +11002335 case 'q':
2336 quiet = 1;
2337 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002338 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002339 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002340 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002341 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002342 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002343 case 'h':
2344 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002345 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002346 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002347 case 'k':
2348 gen_krl = 1;
2349 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002350 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002351 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002352 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002353 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002354 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002355 case 'y':
2356 print_public = 1;
2357 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002358 case 's':
2359 ca_key_path = optarg;
2360 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002361 case 't':
2362 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002363 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002364 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002365 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002366 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002367 case 'u':
2368 update_krl = 1;
2369 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002370 case 'v':
2371 if (log_level == SYSLOG_LEVEL_INFO)
2372 log_level = SYSLOG_LEVEL_DEBUG1;
2373 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002374 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002375 log_level < SYSLOG_LEVEL_DEBUG3)
2376 log_level++;
2377 }
2378 break;
Damien Miller37876e92003-05-15 10:19:46 +10002379 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002380 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002381 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002382 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002383 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2384 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002385 if (errstr)
2386 fatal("Desired generator has bad value: %s (%s)",
2387 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002388 break;
2389 case 'a':
Damien Millerbcd00ab2013-12-07 10:41:55 +11002390 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002391 if (errstr)
Damien Millerbcd00ab2013-12-07 10:41:55 +11002392 fatal("Invalid number: %s (%s)",
Damien Millerb089fb52005-05-26 12:16:18 +10002393 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002394 break;
2395 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002396 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002397 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002398 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002399 break;
2400 case 'G':
2401 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002402 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2403 sizeof(out_file))
2404 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002405 break;
2406 case 'T':
2407 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002408 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2409 sizeof(out_file))
2410 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002411 break;
Damien Miller390d0562011-10-18 16:05:19 +11002412 case 'K':
2413 if (strlen(optarg) >= MAXPATHLEN)
2414 fatal("Checkpoint filename too long");
2415 checkpoint = xstrdup(optarg);
2416 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002417 case 'S':
2418 /* XXX - also compare length against bits */
2419 if (BN_hex2bn(&start, optarg) == 0)
2420 fatal("Invalid start point.");
2421 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002422 case 'V':
2423 parse_cert_times(optarg);
2424 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002425 case 'z':
Damien Miller6f3b3622012-11-14 19:04:33 +11002426 errno = 0;
2427 cert_serial = strtoull(optarg, &ep, 10);
2428 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2429 (errno == ERANGE && cert_serial == ULLONG_MAX))
2430 fatal("Invalid serial number \"%s\"", optarg);
Damien Miller4e270b02010-04-16 15:56:21 +10002431 break;
Damien Miller95def091999-11-25 00:26:21 +11002432 case '?':
2433 default:
2434 usage();
2435 }
2436 }
Darren Tucker06930c72003-12-31 11:34:51 +11002437
2438 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002439 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002440
Damien Miller0a80ca12010-02-27 07:55:05 +11002441 argv += optind;
2442 argc -= optind;
2443
2444 if (ca_key_path != NULL) {
Damien Millerf3747bf2013-01-18 11:44:04 +11002445 if (argc < 1 && !gen_krl) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002446 printf("Too few arguments.\n");
2447 usage();
2448 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002449 } else if (argc > 0 && !gen_krl && !check_krl) {
Damien Miller95def091999-11-25 00:26:21 +11002450 printf("Too many arguments.\n");
2451 usage();
2452 }
2453 if (change_passphrase && change_comment) {
2454 printf("Can only have one of -p and -c.\n");
2455 usage();
2456 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002457 if (print_fingerprint && (delete_host || hash_hosts)) {
Damien Millerec77c952013-01-09 15:58:00 +11002458 printf("Cannot use -l with -H or -R.\n");
Darren Tucker0f7e9102008-06-08 12:54:29 +10002459 usage();
2460 }
Damien Miller1f0311c2014-05-15 14:24:09 +10002461#ifdef WITH_OPENSSL
Damien Millerf3747bf2013-01-18 11:44:04 +11002462 if (gen_krl) {
2463 do_gen_krl(pw, update_krl, argc, argv);
2464 return (0);
2465 }
2466 if (check_krl) {
2467 do_check_krl(pw, argc, argv);
2468 return (0);
2469 }
Damien Miller1f0311c2014-05-15 14:24:09 +10002470#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11002471 if (ca_key_path != NULL) {
2472 if (cert_key_id == NULL)
2473 fatal("Must specify key id (-I) when certifying");
2474 do_ca_sign(pw, argc, argv);
2475 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002476 if (show_cert)
2477 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002478 if (delete_host || hash_hosts || find_host)
2479 do_known_hosts(pw, rr_hostname);
Damien Millerec77c952013-01-09 15:58:00 +11002480 if (pkcs11provider != NULL)
2481 do_download(pw);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002482 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002483 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002484 if (change_passphrase)
2485 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002486 if (change_comment)
2487 do_change_comment(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002488#ifdef WITH_OPENSSL
Damien Miller44b25042010-07-02 13:35:01 +10002489 if (convert_to)
2490 do_convert_to(pw);
2491 if (convert_from)
2492 do_convert_from(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002493#endif
Damien Millereba71ba2000-04-29 23:57:08 +10002494 if (print_public)
2495 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002496 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002497 unsigned int n = 0;
2498
2499 if (have_identity) {
2500 n = do_print_resource_record(pw,
2501 identity_file, rr_hostname);
2502 if (n == 0) {
2503 perror(identity_file);
2504 exit(1);
2505 }
2506 exit(0);
2507 } else {
2508
2509 n += do_print_resource_record(pw,
2510 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2511 n += do_print_resource_record(pw,
2512 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
Damien Miller3bde12a2012-06-20 21:51:11 +10002513 n += do_print_resource_record(pw,
2514 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
Damien Miller16cd3922014-05-15 13:45:58 +10002515 n += do_print_resource_record(pw,
2516 _PATH_HOST_ED25519_KEY_FILE, rr_hostname);
Damien Millercb314822006-03-26 13:48:01 +11002517 if (n == 0)
2518 fatal("no keys found.");
2519 exit(0);
2520 }
Damien Miller37876e92003-05-15 10:19:46 +10002521 }
Damien Miller95def091999-11-25 00:26:21 +11002522
Darren Tucker019cefe2003-08-02 22:40:07 +10002523 if (do_gen_candidates) {
2524 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002525
Darren Tucker019cefe2003-08-02 22:40:07 +10002526 if (out == NULL) {
2527 error("Couldn't open modulus candidate file \"%s\": %s",
2528 out_file, strerror(errno));
2529 return (1);
2530 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002531 if (bits == 0)
2532 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002533 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002534 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002535
2536 return (0);
2537 }
2538
2539 if (do_screen_candidates) {
2540 FILE *in;
Damien Miller78d22712013-02-12 11:03:36 +11002541 FILE *out = fopen(out_file, "a");
Darren Tucker019cefe2003-08-02 22:40:07 +10002542
2543 if (have_identity && strcmp(identity_file, "-") != 0) {
2544 if ((in = fopen(identity_file, "r")) == NULL) {
2545 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002546 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002547 strerror(errno));
2548 }
2549 } else
2550 in = stdin;
2551
2552 if (out == NULL) {
2553 fatal("Couldn't open moduli file \"%s\": %s",
2554 out_file, strerror(errno));
2555 }
Damien Millerbcd00ab2013-12-07 10:41:55 +11002556 if (prime_test(in, out, rounds == 0 ? 100 : rounds,
2557 generator_wanted, checkpoint,
Damien Millerdfceafe2012-07-06 13:44:19 +10002558 start_lineno, lines_to_process) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002559 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002560 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002561 }
2562
Damien Miller58f1baf2011-05-05 14:06:15 +10002563 if (gen_all_hostkeys) {
2564 do_gen_all_hostkeys(pw);
2565 return (0);
2566 }
2567
Damien Millerf14be5c2005-11-05 15:15:49 +11002568 if (key_type_name == NULL)
2569 key_type_name = "rsa";
2570
Damien Millere39cacc2000-11-29 12:18:44 +11002571 type = key_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002572 type_bits_valid(type, &bits);
2573
Darren Tucker3af2ac52005-11-29 13:10:24 +11002574 if (!quiet)
2575 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002576 private = key_generate(type, bits);
2577 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002578 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002579 exit(1);
2580 }
2581 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002582
2583 if (!have_identity)
2584 ask_filename(pw, "Enter file in which to save the key");
2585
Damien Miller788f2122005-11-05 15:14:59 +11002586 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002587 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2588 pw->pw_dir, _PATH_SSH_USER_DIR);
2589 if (strstr(identity_file, dotsshdir) != NULL) {
2590 if (stat(dotsshdir, &st) < 0) {
2591 if (errno != ENOENT) {
2592 error("Could not stat %s: %s", dotsshdir,
2593 strerror(errno));
2594 } else if (mkdir(dotsshdir, 0700) < 0) {
2595 error("Could not create directory '%s': %s",
2596 dotsshdir, strerror(errno));
2597 } else if (!quiet)
2598 printf("Created directory '%s'.\n", dotsshdir);
2599 }
Damien Miller95def091999-11-25 00:26:21 +11002600 }
2601 /* If the file already exists, ask the user to confirm. */
2602 if (stat(identity_file, &st) >= 0) {
2603 char yesno[3];
2604 printf("%s already exists.\n", identity_file);
2605 printf("Overwrite (y/n)? ");
2606 fflush(stdout);
2607 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2608 exit(1);
2609 if (yesno[0] != 'y' && yesno[0] != 'Y')
2610 exit(1);
2611 }
2612 /* Ask for a passphrase (twice). */
2613 if (identity_passphrase)
2614 passphrase1 = xstrdup(identity_passphrase);
2615 else if (identity_new_passphrase)
2616 passphrase1 = xstrdup(identity_new_passphrase);
2617 else {
2618passphrase_again:
2619 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002620 read_passphrase("Enter passphrase (empty for no "
2621 "passphrase): ", RP_ALLOW_STDIN);
2622 passphrase2 = read_passphrase("Enter same passphrase again: ",
2623 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002624 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002625 /*
2626 * The passphrases do not match. Clear them and
2627 * retry.
2628 */
Damien Millera5103f42014-02-04 11:20:14 +11002629 explicit_bzero(passphrase1, strlen(passphrase1));
2630 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002631 free(passphrase1);
2632 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002633 printf("Passphrases do not match. Try again.\n");
2634 goto passphrase_again;
2635 }
2636 /* Clear the other copy of the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002637 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002638 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002639 }
2640
Damien Miller95def091999-11-25 00:26:21 +11002641 if (identity_comment) {
2642 strlcpy(comment, identity_comment, sizeof(comment));
2643 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002644 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002645 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2646 }
2647
2648 /* Save the key with the given passphrase and comment. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11002649 if (!key_save_private(private, identity_file, passphrase1, comment,
2650 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002651 printf("Saving the key failed: %s.\n", identity_file);
Damien Millera5103f42014-02-04 11:20:14 +11002652 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002653 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002654 exit(1);
2655 }
2656 /* Clear the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002657 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002658 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002659
2660 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002661 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002662
2663 if (!quiet)
2664 printf("Your identification has been saved in %s.\n", identity_file);
2665
Damien Miller95def091999-11-25 00:26:21 +11002666 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002667 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2668 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002669 printf("Could not save your public key in %s\n", identity_file);
2670 exit(1);
2671 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002672 f = fdopen(fd, "w");
2673 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002674 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002675 exit(1);
2676 }
Damien Millereba71ba2000-04-29 23:57:08 +10002677 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002678 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002679 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002680 fclose(f);
2681
2682 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002683 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002684 char *ra = key_fingerprint(public, SSH_FP_MD5,
2685 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002686 printf("Your public key has been saved in %s.\n",
2687 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002688 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002689 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002690 printf("The key's randomart image is:\n");
2691 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10002692 free(ra);
2693 free(fp);
Damien Miller95def091999-11-25 00:26:21 +11002694 }
Damien Millereba71ba2000-04-29 23:57:08 +10002695
2696 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002697 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002698}