blob: 23058ee9964c1ac4b1e849642b043b06f3ab5d78 [file] [log] [blame]
Damien Miller4a1d3d52014-07-03 21:24:40 +10001/* $OpenBSD: ssh-keygen.c,v 1.249 2014/07/03 03:47:27 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100025
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100029#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000041#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "authfile.h"
43#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110044#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
46#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100047#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110048#include "match.h"
49#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100050#include "dns.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110051#include "ssh.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110052#include "ssh2.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110053#include "ssh-pkcs11.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110054#include "atomicio.h"
55#include "krl.h"
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 Millere5c0d522014-07-03 21:24:19 +1000168char hostname[NI_MAXHOST];
Damien Millereba71ba2000-04-29 23:57:08 +1000169
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);
Damien Miller86687062014-07-02 15:28:02 +1000485 if (rsa_generate_additional_parameters(key->rsa) != 0)
486 fatal("%s: rsa_generate_additional_parameters "
487 "error", __func__);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000488 break;
489 }
Damien Miller874d77b2000-10-14 16:23:11 +1100490 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000491 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000492 error("do_convert_private_ssh2_from_blob: "
493 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100494 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000495
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000496 /* try the key */
497 key_sign(key, &sig, &slen, data, sizeof(data));
498 key_verify(key, sig, slen, data, sizeof(data));
Darren Tuckera627d422013-06-02 07:31:17 +1000499 free(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100500 return key;
501}
502
Damien Miller8056a9d2006-03-15 12:05:40 +1100503static int
504get_line(FILE *fp, char *line, size_t len)
505{
506 int c;
507 size_t pos = 0;
508
509 line[0] = '\0';
510 while ((c = fgetc(fp)) != EOF) {
511 if (pos >= len - 1) {
512 fprintf(stderr, "input line too long.\n");
513 exit(1);
514 }
Damien Miller90967402006-03-26 14:07:26 +1100515 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100516 case '\r':
517 c = fgetc(fp);
518 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
519 fprintf(stderr, "unget: %s\n", strerror(errno));
520 exit(1);
521 }
522 return pos;
523 case '\n':
524 return pos;
525 }
526 line[pos++] = c;
527 line[pos] = '\0';
528 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100529 /* We reached EOF */
530 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100531}
532
Ben Lindstrombba81212001-06-25 05:01:22 +0000533static void
Damien Miller44b25042010-07-02 13:35:01 +1000534do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000535{
Damien Millereba71ba2000-04-29 23:57:08 +1000536 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000537 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100538 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000539 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000540 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000541 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000542 FILE *fp;
543
Damien Millerba3420a2010-06-26 09:39:07 +1000544 if ((fp = fopen(identity_file, "r")) == NULL)
545 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000546 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100547 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
Damien Miller746d1a62013-07-18 16:13:02 +1000548 if (blen > 0 && line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000549 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000550 if (strncmp(line, "----", 4) == 0 ||
551 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100552 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000553 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000554 if (strstr(line, " END ") != NULL) {
555 break;
556 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000557 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000558 continue;
559 }
Damien Miller30c3d422000-05-09 11:02:59 +1000560 if (escaped) {
561 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000562 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000563 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000564 }
Damien Millereba71ba2000-04-29 23:57:08 +1000565 strlcat(encoded, line, sizeof(encoded));
566 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000567 len = strlen(encoded);
568 if (((len % 4) == 3) &&
569 (encoded[len-1] == '=') &&
570 (encoded[len-2] == '=') &&
571 (encoded[len-3] == '='))
572 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100573 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000574 if (blen < 0) {
575 fprintf(stderr, "uudecode failed.\n");
576 exit(1);
577 }
Damien Miller44b25042010-07-02 13:35:01 +1000578 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100579 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100580 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000581 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100582 fprintf(stderr, "decode blob failed.\n");
583 exit(1);
584 }
Damien Miller44b25042010-07-02 13:35:01 +1000585 fclose(fp);
586}
587
588static void
589do_convert_from_pkcs8(Key **k, int *private)
590{
591 EVP_PKEY *pubkey;
592 FILE *fp;
593
594 if ((fp = fopen(identity_file, "r")) == NULL)
595 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
596 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
597 fatal("%s: %s is not a recognised public key format", __func__,
598 identity_file);
599 }
600 fclose(fp);
601 switch (EVP_PKEY_type(pubkey->type)) {
602 case EVP_PKEY_RSA:
603 *k = key_new(KEY_UNSPEC);
604 (*k)->type = KEY_RSA;
605 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
606 break;
607 case EVP_PKEY_DSA:
608 *k = key_new(KEY_UNSPEC);
609 (*k)->type = KEY_DSA;
610 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
611 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000612#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000613 case EVP_PKEY_EC:
614 *k = key_new(KEY_UNSPEC);
615 (*k)->type = KEY_ECDSA;
616 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
Damien Millerb472a902010-11-05 10:19:49 +1100617 (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000618 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000619#endif
Damien Miller44b25042010-07-02 13:35:01 +1000620 default:
621 fatal("%s: unsupported pubkey type %d", __func__,
622 EVP_PKEY_type(pubkey->type));
623 }
624 EVP_PKEY_free(pubkey);
625 return;
626}
627
628static void
629do_convert_from_pem(Key **k, int *private)
630{
631 FILE *fp;
632 RSA *rsa;
633#ifdef notyet
634 DSA *dsa;
635#endif
636
637 if ((fp = fopen(identity_file, "r")) == NULL)
638 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
639 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
640 *k = key_new(KEY_UNSPEC);
641 (*k)->type = KEY_RSA;
642 (*k)->rsa = rsa;
643 fclose(fp);
644 return;
645 }
646#if notyet /* OpenSSH 0.9.8 lacks this function */
647 rewind(fp);
648 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
649 *k = key_new(KEY_UNSPEC);
650 (*k)->type = KEY_DSA;
651 (*k)->dsa = dsa;
652 fclose(fp);
653 return;
654 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000655 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000656#endif
657 fatal("%s: unrecognised raw private key format", __func__);
658}
659
660static void
661do_convert_from(struct passwd *pw)
662{
663 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000664 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000665 struct stat st;
666
667 if (!have_identity)
668 ask_filename(pw, "Enter file in which the key is");
669 if (stat(identity_file, &st) < 0)
670 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
671
672 switch (convert_format) {
673 case FMT_RFC4716:
674 do_convert_from_ssh2(pw, &k, &private);
675 break;
676 case FMT_PKCS8:
677 do_convert_from_pkcs8(&k, &private);
678 break;
679 case FMT_PEM:
680 do_convert_from_pem(&k, &private);
681 break;
682 default:
683 fatal("%s: unknown key format %d", __func__, convert_format);
684 }
685
686 if (!private)
687 ok = key_write(k, stdout);
688 if (ok)
689 fprintf(stdout, "\n");
690 else {
691 switch (k->type) {
692 case KEY_DSA:
693 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
694 NULL, 0, NULL, NULL);
695 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000696#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000697 case KEY_ECDSA:
698 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
699 NULL, 0, NULL, NULL);
700 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000701#endif
Damien Miller44b25042010-07-02 13:35:01 +1000702 case KEY_RSA:
703 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
704 NULL, 0, NULL, NULL);
705 break;
706 default:
707 fatal("%s: unsupported key type %s", __func__,
708 key_type(k));
709 }
710 }
711
Damien Miller874d77b2000-10-14 16:23:11 +1100712 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100713 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100714 exit(1);
715 }
Damien Millereba71ba2000-04-29 23:57:08 +1000716 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000717 exit(0);
718}
Damien Miller1f0311c2014-05-15 14:24:09 +1000719#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000720
Ben Lindstrombba81212001-06-25 05:01:22 +0000721static void
Damien Millereba71ba2000-04-29 23:57:08 +1000722do_print_public(struct passwd *pw)
723{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000724 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000725 struct stat st;
726
727 if (!have_identity)
728 ask_filename(pw, "Enter file in which the key is");
729 if (stat(identity_file, &st) < 0) {
730 perror(identity_file);
731 exit(1);
732 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000733 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000734 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000735 fprintf(stderr, "load failed\n");
736 exit(1);
737 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000738 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000739 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000740 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000741 fprintf(stdout, "\n");
742 exit(0);
743}
744
Ben Lindstromcd392282001-07-04 03:44:03 +0000745static void
Damien Miller757f34e2010-08-05 13:05:31 +1000746do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000747{
Damien Miller7ea845e2010-02-12 09:21:02 +1100748#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000749 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100750 int i, nkeys;
Damien Millerec77c952013-01-09 15:58:00 +1100751 enum fp_rep rep;
752 enum fp_type fptype;
753 char *fp, *ra;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000754
Damien Miller1422c082013-01-09 16:44:54 +1100755 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
756 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
757
Damien Miller7ea845e2010-02-12 09:21:02 +1100758 pkcs11_init(0);
759 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
760 if (nkeys <= 0)
761 fatal("cannot read public key from pkcs11");
762 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100763 if (print_fingerprint) {
764 fp = key_fingerprint(keys[i], fptype, rep);
765 ra = key_fingerprint(keys[i], SSH_FP_MD5,
766 SSH_FP_RANDOMART);
767 printf("%u %s %s (PKCS11 key)\n", key_size(keys[i]),
768 fp, key_type(keys[i]));
769 if (log_level >= SYSLOG_LEVEL_VERBOSE)
770 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000771 free(ra);
772 free(fp);
Damien Millerec77c952013-01-09 15:58:00 +1100773 } else {
774 key_write(keys[i], stdout);
775 fprintf(stdout, "\n");
776 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000777 key_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000778 }
Darren Tuckera627d422013-06-02 07:31:17 +1000779 free(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100780 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000781 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100782#else
783 fatal("no pkcs11 support");
784#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000785}
Ben Lindstromcd392282001-07-04 03:44:03 +0000786
Ben Lindstrombba81212001-06-25 05:01:22 +0000787static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100788do_fingerprint(struct passwd *pw)
789{
Damien Miller98c7ad62000-03-09 21:27:49 +1100790 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000791 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000792 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100793 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000794 enum fp_rep rep;
795 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100796 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100797
Ben Lindstromd0fca422001-03-26 13:44:06 +0000798 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
799 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000800
Damien Miller95def091999-11-25 00:26:21 +1100801 if (!have_identity)
802 ask_filename(pw, "Enter file in which the key is");
803 if (stat(identity_file, &st) < 0) {
804 perror(identity_file);
805 exit(1);
806 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000807 public = key_load_public(identity_file, &comment);
808 if (public != NULL) {
809 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100810 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000811 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
812 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000813 if (log_level >= SYSLOG_LEVEL_VERBOSE)
814 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100815 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +1000816 free(comment);
817 free(ra);
818 free(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100819 exit(0);
820 }
Damien Miller40b59852006-06-13 13:00:25 +1000821 if (comment) {
Darren Tuckera627d422013-06-02 07:31:17 +1000822 free(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000823 comment = NULL;
824 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100825
Damien Millerba3420a2010-06-26 09:39:07 +1000826 if ((f = fopen(identity_file, "r")) == NULL)
827 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100828
Damien Millerba3420a2010-06-26 09:39:07 +1000829 while (fgets(line, sizeof(line), f)) {
830 if ((cp = strchr(line, '\n')) == NULL) {
831 error("line %d too long: %.40s...",
832 num + 1, line);
833 skip = 1;
834 continue;
Damien Miller95def091999-11-25 00:26:21 +1100835 }
Damien Millerba3420a2010-06-26 09:39:07 +1000836 num++;
837 if (skip) {
838 skip = 0;
839 continue;
840 }
841 *cp = '\0';
842
843 /* Skip leading whitespace, empty and comment lines. */
844 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
845 ;
846 if (!*cp || *cp == '\n' || *cp == '#')
847 continue;
848 i = strtol(cp, &ep, 10);
849 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
850 int quoted = 0;
851 comment = cp;
852 for (; *cp && (quoted || (*cp != ' ' &&
853 *cp != '\t')); cp++) {
854 if (*cp == '\\' && cp[1] == '"')
855 cp++; /* Skip both */
856 else if (*cp == '"')
857 quoted = !quoted;
858 }
859 if (!*cp)
860 continue;
861 *cp++ = '\0';
862 }
863 ep = cp;
864 public = key_new(KEY_RSA1);
865 if (key_read(public, &cp) != 1) {
866 cp = ep;
867 key_free(public);
868 public = key_new(KEY_UNSPEC);
869 if (key_read(public, &cp) != 1) {
870 key_free(public);
871 continue;
872 }
873 }
874 comment = *cp ? cp : comment;
875 fp = key_fingerprint(public, fptype, rep);
876 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
877 printf("%u %s %s (%s)\n", key_size(public), fp,
878 comment ? comment : "no comment", key_type(public));
879 if (log_level >= SYSLOG_LEVEL_VERBOSE)
880 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000881 free(ra);
882 free(fp);
Damien Millerba3420a2010-06-26 09:39:07 +1000883 key_free(public);
884 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100885 }
Damien Millerba3420a2010-06-26 09:39:07 +1000886 fclose(f);
887
Damien Miller98c7ad62000-03-09 21:27:49 +1100888 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100889 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100890 exit(1);
891 }
Damien Miller95def091999-11-25 00:26:21 +1100892 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100893}
894
Damien Miller4b42d7f2005-03-01 21:48:35 +1100895static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000896do_gen_all_hostkeys(struct passwd *pw)
897{
898 struct {
899 char *key_type;
900 char *key_type_display;
901 char *path;
902 } key_types[] = {
903 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
904 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
905 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100906#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000907 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100908#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100909 { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
Damien Miller58f1baf2011-05-05 14:06:15 +1000910 { NULL, NULL, NULL }
911 };
912
913 int first = 0;
914 struct stat st;
915 Key *private, *public;
916 char comment[1024];
917 int i, type, fd;
918 FILE *f;
919
920 for (i = 0; key_types[i].key_type; i++) {
921 if (stat(key_types[i].path, &st) == 0)
922 continue;
923 if (errno != ENOENT) {
924 printf("Could not stat %s: %s", key_types[i].path,
925 strerror(errno));
926 first = 0;
927 continue;
928 }
929
930 if (first == 0) {
931 first = 1;
932 printf("%s: generating new host keys: ", __progname);
933 }
934 printf("%s ", key_types[i].key_type_display);
935 fflush(stdout);
Damien Miller58f1baf2011-05-05 14:06:15 +1000936 type = key_type_from_name(key_types[i].key_type);
937 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
938 bits = 0;
939 type_bits_valid(type, &bits);
940 private = key_generate(type, bits);
941 if (private == NULL) {
942 fprintf(stderr, "key_generate failed\n");
943 first = 0;
944 continue;
945 }
946 public = key_from_private(private);
947 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
948 hostname);
Damien Millerbcd00ab2013-12-07 10:41:55 +1100949 if (!key_save_private(private, identity_file, "", comment,
950 use_new_format, new_format_cipher, rounds)) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000951 printf("Saving the key failed: %s.\n", identity_file);
952 key_free(private);
953 key_free(public);
954 first = 0;
955 continue;
956 }
957 key_free(private);
Damien Miller58f1baf2011-05-05 14:06:15 +1000958 strlcat(identity_file, ".pub", sizeof(identity_file));
959 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
960 if (fd == -1) {
961 printf("Could not save your public key in %s\n",
962 identity_file);
963 key_free(public);
964 first = 0;
965 continue;
966 }
967 f = fdopen(fd, "w");
968 if (f == NULL) {
969 printf("fdopen %s failed\n", identity_file);
970 key_free(public);
971 first = 0;
972 continue;
973 }
974 if (!key_write(public, f)) {
975 fprintf(stderr, "write key failed\n");
976 key_free(public);
977 first = 0;
978 continue;
979 }
980 fprintf(f, " %s\n", comment);
981 fclose(f);
982 key_free(public);
983
984 }
985 if (first != 0)
986 printf("\n");
987}
988
989static void
Damien Miller4a1d3d52014-07-03 21:24:40 +1000990printhost(FILE *f, const char *name, Key *public, int ca, int revoked, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100991{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000992 if (print_fingerprint) {
993 enum fp_rep rep;
994 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000995 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000996
997 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
998 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
999 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +11001000 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +10001001 printf("%u %s %s (%s)\n", key_size(public), fp, name,
1002 key_type(public));
1003 if (log_level >= SYSLOG_LEVEL_VERBOSE)
1004 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10001005 free(ra);
1006 free(fp);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001007 } else {
1008 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
1009 fatal("hash_host failed");
Damien Miller4a1d3d52014-07-03 21:24:40 +10001010 fprintf(f, "%s%s%s ", ca ? CA_MARKER " " : "",
1011 revoked ? REVOKE_MARKER " " : "" , name);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001012 if (!key_write(public, f))
1013 fatal("key_write failed");
1014 fprintf(f, "\n");
1015 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001016}
1017
1018static void
1019do_known_hosts(struct passwd *pw, const char *name)
1020{
1021 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +11001022 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001023 char *cp, *cp2, *kp, *kp2;
1024 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +11001025 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller4a1d3d52014-07-03 21:24:40 +10001026 int ca, revoked;
Damien Miller66085482013-09-14 09:45:03 +10001027 int found_key = 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001028
1029 if (!have_identity) {
1030 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1031 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1032 sizeof(identity_file))
1033 fatal("Specified known hosts path too long");
Darren Tuckera627d422013-06-02 07:31:17 +10001034 free(cp);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001035 have_identity = 1;
1036 }
1037 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +10001038 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001039
Damien Miller4a1d3d52014-07-03 21:24:40 +10001040 /* XXX this code is a mess; refactor -djm */
Damien Miller4b42d7f2005-03-01 21:48:35 +11001041 /*
1042 * Find hosts goes to stdout, hash and deletions happen in-place
1043 * A corner case is ssh-keygen -HF foo, which should go to stdout
1044 */
1045 if (!find_host && (hash_hosts || delete_host)) {
1046 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1047 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1048 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1049 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1050 fatal("known_hosts path too long");
1051 umask(077);
1052 if ((c = mkstemp(tmp)) == -1)
1053 fatal("mkstemp: %s", strerror(errno));
1054 if ((out = fdopen(c, "w")) == NULL) {
1055 c = errno;
1056 unlink(tmp);
1057 fatal("fdopen: %s", strerror(c));
1058 }
1059 inplace = 1;
1060 }
1061
1062 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001063 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001064 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001065 skip = 1;
1066 invalid = 1;
1067 continue;
1068 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001069 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001070 if (skip) {
1071 skip = 0;
1072 continue;
1073 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001074 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001075
1076 /* Skip leading whitespace, empty and comment lines. */
1077 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1078 ;
1079 if (!*cp || *cp == '\n' || *cp == '#') {
1080 if (inplace)
1081 fprintf(out, "%s\n", cp);
1082 continue;
1083 }
Damien Miller4a1d3d52014-07-03 21:24:40 +10001084 /* Check whether this is a CA key or revocation marker */
Damien Miller0a80ca12010-02-27 07:55:05 +11001085 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1086 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1087 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1088 ca = 1;
1089 cp += sizeof(CA_MARKER);
1090 } else
1091 ca = 0;
Damien Miller4a1d3d52014-07-03 21:24:40 +10001092 if (strncasecmp(cp, REVOKE_MARKER,
1093 sizeof(REVOKE_MARKER) - 1) == 0 &&
1094 (cp[sizeof(REVOKE_MARKER) - 1] == ' ' ||
1095 cp[sizeof(REVOKE_MARKER) - 1] == '\t')) {
1096 revoked = 1;
1097 cp += sizeof(REVOKE_MARKER);
1098 } else
1099 revoked = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001100
Damien Miller4b42d7f2005-03-01 21:48:35 +11001101 /* Find the end of the host name portion. */
1102 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1103 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001104
Damien Miller4b42d7f2005-03-01 21:48:35 +11001105 if (*kp == '\0' || *(kp + 1) == '\0') {
1106 error("line %d missing key: %.40s...",
1107 num, line);
1108 invalid = 1;
1109 continue;
1110 }
1111 *kp++ = '\0';
1112 kp2 = kp;
1113
Damien Miller0a80ca12010-02-27 07:55:05 +11001114 pub = key_new(KEY_RSA1);
1115 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001116 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +11001117 key_free(pub);
1118 pub = key_new(KEY_UNSPEC);
1119 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001120 error("line %d invalid key: %.40s...",
1121 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001122 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001123 invalid = 1;
1124 continue;
1125 }
1126 }
1127
1128 if (*cp == HASH_DELIM) {
1129 if (find_host || delete_host) {
1130 cp2 = host_hash(name, cp, strlen(cp));
1131 if (cp2 == NULL) {
1132 error("line %d: invalid hashed "
1133 "name: %.64s...", num, line);
1134 invalid = 1;
1135 continue;
1136 }
1137 c = (strcmp(cp2, cp) == 0);
1138 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001139 if (!quiet)
1140 printf("# Host %s found: "
1141 "line %d type %s%s\n", name,
1142 num, key_type(pub),
Damien Miller4a1d3d52014-07-03 21:24:40 +10001143 ca ? " (CA key)" :
1144 revoked? " (revoked)" : "");
1145 printhost(out, cp, pub, ca, revoked, 0);
Damien Miller66085482013-09-14 09:45:03 +10001146 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001147 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001148 if (delete_host) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001149 if (!c || ca || revoked) {
1150 printhost(out, cp, pub,
1151 ca, revoked, 0);
1152 } else {
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001153 printf("# Host %s found: "
1154 "line %d type %s\n", name,
1155 num, key_type(pub));
Damien Miller4a1d3d52014-07-03 21:24:40 +10001156 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001157 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001158 } else if (hash_hosts)
Damien Miller4a1d3d52014-07-03 21:24:40 +10001159 printhost(out, cp, pub, ca, revoked, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001160 } else {
1161 if (find_host || delete_host) {
1162 c = (match_hostname(name, cp,
1163 strlen(cp)) == 1);
1164 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001165 if (!quiet)
1166 printf("# Host %s found: "
1167 "line %d type %s%s\n", name,
1168 num, key_type(pub),
1169 ca ? " (CA key)" : "");
Damien Miller4a1d3d52014-07-03 21:24:40 +10001170 printhost(out, name, pub, ca, revoked,
1171 hash_hosts && !(ca || revoked));
Damien Miller66085482013-09-14 09:45:03 +10001172 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001173 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001174 if (delete_host) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001175 if (!c || ca || revoked) {
1176 printhost(out, cp, pub,
1177 ca, revoked, 0);
1178 } else {
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001179 printf("# Host %s found: "
1180 "line %d type %s\n", name,
1181 num, key_type(pub));
Damien Miller4a1d3d52014-07-03 21:24:40 +10001182 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001183 }
Damien Miller4a1d3d52014-07-03 21:24:40 +10001184 } else if (hash_hosts && (ca || revoked)) {
1185 /* Don't hash CA and revoked keys' hostnames */
1186 printhost(out, cp, pub, ca, revoked, 0);
1187 has_unhashed = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001188 } else if (hash_hosts) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001189 /* Hash each hostname separately */
Darren Tucker47eede72005-03-14 23:08:12 +11001190 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001191 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001192 cp2 = strsep(&cp, ",")) {
Damien Miller4a1d3d52014-07-03 21:24:40 +10001193 if (strcspn(cp2, "*?!") !=
Damien Miller0a80ca12010-02-27 07:55:05 +11001194 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001195 fprintf(stderr, "Warning: "
1196 "ignoring host name with "
1197 "metacharacters: %.64s\n",
1198 cp2);
Damien Miller4a1d3d52014-07-03 21:24:40 +10001199 printhost(out, cp2, pub, ca,
1200 revoked, 0);
1201 has_unhashed = 1;
1202 } else {
1203 printhost(out, cp2, pub, ca,
1204 revoked, 1);
1205 }
Damien Miller89eac802005-03-02 12:33:04 +11001206 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001207 }
1208 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001209 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001210 }
1211 fclose(in);
1212
1213 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001214 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001215 identity_file);
1216 if (inplace) {
1217 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001218 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001219 fclose(out);
1220 unlink(tmp);
1221 }
1222 exit(1);
1223 }
1224
1225 if (inplace) {
1226 fclose(out);
1227
1228 /* Backup existing file */
1229 if (unlink(old) == -1 && errno != ENOENT)
1230 fatal("unlink %.100s: %s", old, strerror(errno));
1231 if (link(identity_file, old) == -1)
1232 fatal("link %.100s to %.100s: %s", identity_file, old,
1233 strerror(errno));
1234 /* Move new one into place */
1235 if (rename(tmp, identity_file) == -1) {
1236 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1237 strerror(errno));
1238 unlink(tmp);
1239 unlink(old);
1240 exit(1);
1241 }
1242
1243 fprintf(stderr, "%s updated.\n", identity_file);
1244 fprintf(stderr, "Original contents retained as %s\n", old);
1245 if (has_unhashed) {
1246 fprintf(stderr, "WARNING: %s contains unhashed "
1247 "entries\n", old);
1248 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001249 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001250 }
1251 }
1252
Damien Miller66085482013-09-14 09:45:03 +10001253 exit (find_host && !found_key);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001254}
1255
Damien Miller95def091999-11-25 00:26:21 +11001256/*
1257 * Perform changing a passphrase. The argument is the passwd structure
1258 * for the current user.
1259 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001260static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001261do_change_passphrase(struct passwd *pw)
1262{
Damien Miller95def091999-11-25 00:26:21 +11001263 char *comment;
1264 char *old_passphrase, *passphrase1, *passphrase2;
1265 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001266 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001267
Damien Miller95def091999-11-25 00:26:21 +11001268 if (!have_identity)
1269 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001270 if (stat(identity_file, &st) < 0) {
1271 perror(identity_file);
1272 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001273 }
Damien Miller95def091999-11-25 00:26:21 +11001274 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001275 private = key_load_private(identity_file, "", &comment);
1276 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001277 if (identity_passphrase)
1278 old_passphrase = xstrdup(identity_passphrase);
1279 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001280 old_passphrase =
1281 read_passphrase("Enter old passphrase: ",
1282 RP_ALLOW_STDIN);
1283 private = key_load_private(identity_file, old_passphrase,
1284 &comment);
Damien Millera5103f42014-02-04 11:20:14 +11001285 explicit_bzero(old_passphrase, strlen(old_passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001286 free(old_passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001287 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001288 printf("Bad passphrase.\n");
1289 exit(1);
1290 }
Damien Miller95def091999-11-25 00:26:21 +11001291 }
1292 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293
Damien Miller95def091999-11-25 00:26:21 +11001294 /* Ask the new passphrase (twice). */
1295 if (identity_new_passphrase) {
1296 passphrase1 = xstrdup(identity_new_passphrase);
1297 passphrase2 = NULL;
1298 } else {
1299 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001300 read_passphrase("Enter new passphrase (empty for no "
1301 "passphrase): ", RP_ALLOW_STDIN);
1302 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001303 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001304
1305 /* Verify that they are the same. */
1306 if (strcmp(passphrase1, passphrase2) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001307 explicit_bzero(passphrase1, strlen(passphrase1));
1308 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001309 free(passphrase1);
1310 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001311 printf("Pass phrases do not match. Try again.\n");
1312 exit(1);
1313 }
1314 /* Destroy the other copy. */
Damien Millera5103f42014-02-04 11:20:14 +11001315 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001316 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001317 }
1318
1319 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001320 if (!key_save_private(private, identity_file, passphrase1, comment,
1321 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001322 printf("Saving the key failed: %s.\n", identity_file);
Damien Millera5103f42014-02-04 11:20:14 +11001323 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001324 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001325 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001326 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001327 exit(1);
1328 }
1329 /* Destroy the passphrase and the copy of the key in memory. */
Damien Millera5103f42014-02-04 11:20:14 +11001330 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001331 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001332 key_free(private); /* Destroys contents */
Darren Tuckera627d422013-06-02 07:31:17 +10001333 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001334
1335 printf("Your identification has been saved with the new passphrase.\n");
1336 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001337}
1338
Damien Miller37876e92003-05-15 10:19:46 +10001339/*
1340 * Print the SSHFP RR.
1341 */
Damien Millercb314822006-03-26 13:48:01 +11001342static int
1343do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001344{
1345 Key *public;
1346 char *comment = NULL;
1347 struct stat st;
1348
Damien Millercb314822006-03-26 13:48:01 +11001349 if (fname == NULL)
Damien Miller5bb88332013-07-18 16:13:37 +10001350 fatal("%s: no filename", __func__);
Damien Millercb314822006-03-26 13:48:01 +11001351 if (stat(fname, &st) < 0) {
1352 if (errno == ENOENT)
1353 return 0;
1354 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001355 exit(1);
1356 }
Damien Millercb314822006-03-26 13:48:01 +11001357 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001358 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001359 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001360 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001361 free(comment);
Damien Millercb314822006-03-26 13:48:01 +11001362 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001363 }
1364 if (comment)
Darren Tuckera627d422013-06-02 07:31:17 +10001365 free(comment);
Damien Miller37876e92003-05-15 10:19:46 +10001366
Damien Millercb314822006-03-26 13:48:01 +11001367 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001368 exit(1);
1369}
Damien Miller37876e92003-05-15 10:19:46 +10001370
Damien Miller95def091999-11-25 00:26:21 +11001371/*
1372 * Change the comment of a private key file.
1373 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001374static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375do_change_comment(struct passwd *pw)
1376{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001377 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001378 Key *private;
1379 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001380 struct stat st;
1381 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001382 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001383
Damien Miller95def091999-11-25 00:26:21 +11001384 if (!have_identity)
1385 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001386 if (stat(identity_file, &st) < 0) {
1387 perror(identity_file);
1388 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001390 private = key_load_private(identity_file, "", &comment);
1391 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001392 if (identity_passphrase)
1393 passphrase = xstrdup(identity_passphrase);
1394 else if (identity_new_passphrase)
1395 passphrase = xstrdup(identity_new_passphrase);
1396 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001397 passphrase = read_passphrase("Enter passphrase: ",
1398 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001399 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001400 private = key_load_private(identity_file, passphrase, &comment);
1401 if (private == NULL) {
Damien Millera5103f42014-02-04 11:20:14 +11001402 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001403 free(passphrase);
Damien Miller95def091999-11-25 00:26:21 +11001404 printf("Bad passphrase.\n");
1405 exit(1);
1406 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001407 } else {
1408 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001409 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001410 if (private->type != KEY_RSA1) {
1411 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1412 key_free(private);
1413 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001414 }
Damien Miller95def091999-11-25 00:26:21 +11001415 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416
Damien Miller95def091999-11-25 00:26:21 +11001417 if (identity_comment) {
1418 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1419 } else {
1420 printf("Enter new comment: ");
1421 fflush(stdout);
1422 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
Damien Millera5103f42014-02-04 11:20:14 +11001423 explicit_bzero(passphrase, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001424 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001425 exit(1);
1426 }
Damien Miller14b017d2007-09-17 16:09:15 +10001427 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001428 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429
Damien Miller95def091999-11-25 00:26:21 +11001430 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001431 if (!key_save_private(private, identity_file, passphrase, new_comment,
1432 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001433 printf("Saving the key failed: %s.\n", identity_file);
Damien Millera5103f42014-02-04 11:20:14 +11001434 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001435 free(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001436 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001437 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001438 exit(1);
1439 }
Damien Millera5103f42014-02-04 11:20:14 +11001440 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001441 free(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001442 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001443 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444
Damien Miller95def091999-11-25 00:26:21 +11001445 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001446 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1447 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001448 printf("Could not save your public key in %s\n", identity_file);
1449 exit(1);
1450 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001451 f = fdopen(fd, "w");
1452 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001453 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001454 exit(1);
1455 }
Damien Millereba71ba2000-04-29 23:57:08 +10001456 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001457 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001458 key_free(public);
1459 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001460 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461
Darren Tuckera627d422013-06-02 07:31:17 +10001462 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001463
1464 printf("The comment in your key file has been changed.\n");
1465 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466}
1467
Damien Miller0a80ca12010-02-27 07:55:05 +11001468static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001469fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001470{
1471 char from[32], to[32];
1472 static char ret[64];
1473 time_t tt;
1474 struct tm *tm;
1475
1476 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001477 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001478 return "forever";
1479
Damien Millerf2b70ca2010-03-05 07:39:35 +11001480 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001481 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001482 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001483 tm = localtime(&tt);
1484 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1485 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001486 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001487 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001488 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001489 tm = localtime(&tt);
1490 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1491 }
1492
Damien Millerf2b70ca2010-03-05 07:39:35 +11001493 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001494 snprintf(ret, sizeof(ret), "before %s", to);
1495 return ret;
1496 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001497 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001498 snprintf(ret, sizeof(ret), "after %s", from);
1499 return ret;
1500 }
1501
1502 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1503 return ret;
1504}
1505
1506static void
Damien Miller4e270b02010-04-16 15:56:21 +10001507add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001508{
1509 debug3("%s: %s", __func__, name);
1510 buffer_put_cstring(c, name);
1511 buffer_put_string(c, NULL, 0);
1512}
1513
1514static void
Damien Miller4e270b02010-04-16 15:56:21 +10001515add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001516{
1517 Buffer b;
1518
1519 debug3("%s: %s=%s", __func__, name, value);
1520 buffer_init(&b);
1521 buffer_put_cstring(&b, value);
1522
1523 buffer_put_cstring(c, name);
1524 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1525
1526 buffer_free(&b);
1527}
1528
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001529#define OPTIONS_CRITICAL 1
1530#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001531static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001532prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001533{
Damien Miller0a80ca12010-02-27 07:55:05 +11001534 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001535 if ((which & OPTIONS_CRITICAL) != 0 &&
1536 certflags_command != NULL)
1537 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001538 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001539 (certflags_flags & CERTOPT_X_FWD) != 0)
1540 add_flag_option(c, "permit-X11-forwarding");
1541 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001542 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001543 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001544 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1545 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001546 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001547 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1548 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001549 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001550 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1551 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001552 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001553 if ((which & OPTIONS_CRITICAL) != 0 &&
1554 certflags_src_addr != NULL)
1555 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001556}
1557
Damien Miller757f34e2010-08-05 13:05:31 +10001558static Key *
1559load_pkcs11_key(char *path)
1560{
1561#ifdef ENABLE_PKCS11
1562 Key **keys = NULL, *public, *private = NULL;
1563 int i, nkeys;
1564
1565 if ((public = key_load_public(path, NULL)) == NULL)
1566 fatal("Couldn't load CA public key \"%s\"", path);
1567
1568 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1569 debug3("%s: %d keys", __func__, nkeys);
1570 if (nkeys <= 0)
1571 fatal("cannot read public key from pkcs11");
1572 for (i = 0; i < nkeys; i++) {
1573 if (key_equal_public(public, keys[i])) {
1574 private = keys[i];
1575 continue;
1576 }
1577 key_free(keys[i]);
1578 }
Darren Tuckera627d422013-06-02 07:31:17 +10001579 free(keys);
Damien Miller757f34e2010-08-05 13:05:31 +10001580 key_free(public);
1581 return private;
1582#else
1583 fatal("no pkcs11 support");
1584#endif /* ENABLE_PKCS11 */
1585}
1586
Damien Miller0a80ca12010-02-27 07:55:05 +11001587static void
1588do_ca_sign(struct passwd *pw, int argc, char **argv)
1589{
1590 int i, fd;
1591 u_int n;
1592 Key *ca, *public;
1593 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1594 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001595 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001596
Damien Miller4e270b02010-04-16 15:56:21 +10001597 if (key_type_name != NULL) {
1598 switch (key_type_from_name(key_type_name)) {
1599 case KEY_RSA_CERT_V00:
1600 case KEY_DSA_CERT_V00:
1601 v00 = 1;
1602 break;
1603 case KEY_UNSPEC:
1604 if (strcasecmp(key_type_name, "v00") == 0) {
1605 v00 = 1;
1606 break;
1607 } else if (strcasecmp(key_type_name, "v01") == 0)
1608 break;
1609 /* FALLTHROUGH */
1610 default:
1611 fprintf(stderr, "unknown key type %s\n", key_type_name);
1612 exit(1);
1613 }
1614 }
1615
Damien Miller1f0311c2014-05-15 14:24:09 +10001616#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001617 pkcs11_init(1);
Damien Miller1f0311c2014-05-15 14:24:09 +10001618#endif
Damien Miller757f34e2010-08-05 13:05:31 +10001619 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1620 if (pkcs11provider != NULL) {
1621 if ((ca = load_pkcs11_key(tmp)) == NULL)
1622 fatal("No PKCS#11 key matching %s found", ca_key_path);
1623 } else if ((ca = load_identity(tmp)) == NULL)
1624 fatal("Couldn't load CA key \"%s\"", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001625 free(tmp);
Damien Miller757f34e2010-08-05 13:05:31 +10001626
Damien Miller0a80ca12010-02-27 07:55:05 +11001627 for (i = 0; i < argc; i++) {
1628 /* Split list of principals */
1629 n = 0;
1630 if (cert_principals != NULL) {
1631 otmp = tmp = xstrdup(cert_principals);
1632 plist = NULL;
1633 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1634 plist = xrealloc(plist, n + 1, sizeof(*plist));
1635 if (*(plist[n] = xstrdup(cp)) == '\0')
1636 fatal("Empty principal name");
1637 }
Darren Tuckera627d422013-06-02 07:31:17 +10001638 free(otmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001639 }
1640
1641 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1642 if ((public = key_load_public(tmp, &comment)) == NULL)
1643 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001644 if (public->type != KEY_RSA && public->type != KEY_DSA &&
Damien Miller5be9d9e2013-12-07 11:24:01 +11001645 public->type != KEY_ECDSA && public->type != KEY_ED25519)
Damien Miller0a80ca12010-02-27 07:55:05 +11001646 fatal("%s: key \"%s\" type %s cannot be certified",
1647 __func__, tmp, key_type(public));
1648
1649 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001650 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001651 fatal("Could not upgrade key %s to certificate", tmp);
1652 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001653 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001654 public->cert->key_id = xstrdup(cert_key_id);
1655 public->cert->nprincipals = n;
1656 public->cert->principals = plist;
1657 public->cert->valid_after = cert_valid_from;
1658 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001659 if (v00) {
Damien Miller86687062014-07-02 15:28:02 +10001660 prepare_options_buf(public->cert->critical,
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001661 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1662 } else {
Damien Miller86687062014-07-02 15:28:02 +10001663 prepare_options_buf(public->cert->critical,
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001664 OPTIONS_CRITICAL);
Damien Miller86687062014-07-02 15:28:02 +10001665 prepare_options_buf(public->cert->extensions,
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001666 OPTIONS_EXTENSIONS);
1667 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001668 public->cert->signature_key = key_from_private(ca);
1669
1670 if (key_certify(public, ca) != 0)
1671 fatal("Couldn't not certify key %s", tmp);
1672
1673 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1674 *cp = '\0';
1675 xasprintf(&out, "%s-cert.pub", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001676 free(tmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001677
1678 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1679 fatal("Could not open \"%s\" for writing: %s", out,
1680 strerror(errno));
1681 if ((f = fdopen(fd, "w")) == NULL)
1682 fatal("%s: fdopen: %s", __func__, strerror(errno));
1683 if (!key_write(public, f))
1684 fatal("Could not write certified key to %s", out);
1685 fprintf(f, " %s\n", comment);
1686 fclose(f);
1687
Damien Miller4e270b02010-04-16 15:56:21 +10001688 if (!quiet) {
1689 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1690 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001691 out, public->cert->key_id,
1692 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001693 cert_principals != NULL ? " for " : "",
1694 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001695 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001696 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001697
1698 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001699 free(out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001700 }
Damien Miller1f0311c2014-05-15 14:24:09 +10001701#ifdef ENABLE_PKCS11
Damien Miller757f34e2010-08-05 13:05:31 +10001702 pkcs11_terminate();
Damien Miller1f0311c2014-05-15 14:24:09 +10001703#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11001704 exit(0);
1705}
1706
1707static u_int64_t
1708parse_relative_time(const char *s, time_t now)
1709{
1710 int64_t mul, secs;
1711
1712 mul = *s == '-' ? -1 : 1;
1713
1714 if ((secs = convtime(s + 1)) == -1)
1715 fatal("Invalid relative certificate time %s", s);
1716 if (mul == -1 && secs > now)
1717 fatal("Certificate time %s cannot be represented", s);
1718 return now + (u_int64_t)(secs * mul);
1719}
1720
1721static u_int64_t
1722parse_absolute_time(const char *s)
1723{
1724 struct tm tm;
1725 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001726 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001727
Damien Miller2ca342b2010-03-03 12:14:15 +11001728 /*
1729 * POSIX strptime says "The application shall ensure that there
1730 * is white-space or other non-alphanumeric characters between
1731 * any two conversion specifications" so arrange things this way.
1732 */
1733 switch (strlen(s)) {
1734 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001735 fmt = "%Y-%m-%d";
1736 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001737 break;
1738 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001739 fmt = "%Y-%m-%dT%H:%M:%S";
1740 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001741 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1742 break;
1743 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001744 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001745 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001746
Damien Miller1d2c4562014-02-04 11:18:20 +11001747 memset(&tm, 0, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001748 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001749 fatal("Invalid certificate time %s", s);
1750 if ((tt = mktime(&tm)) < 0)
1751 fatal("Certificate time %s cannot be represented", s);
1752 return (u_int64_t)tt;
1753}
1754
1755static void
1756parse_cert_times(char *timespec)
1757{
1758 char *from, *to;
1759 time_t now = time(NULL);
1760 int64_t secs;
1761
1762 /* +timespec relative to now */
1763 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1764 if ((secs = convtime(timespec + 1)) == -1)
1765 fatal("Invalid relative certificate life %s", timespec);
1766 cert_valid_to = now + secs;
1767 /*
1768 * Backdate certificate one minute to avoid problems on hosts
1769 * with poorly-synchronised clocks.
1770 */
1771 cert_valid_from = ((now - 59)/ 60) * 60;
1772 return;
1773 }
1774
1775 /*
1776 * from:to, where
1777 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1778 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1779 */
1780 from = xstrdup(timespec);
1781 to = strchr(from, ':');
1782 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001783 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001784 *to++ = '\0';
1785
1786 if (*from == '-' || *from == '+')
1787 cert_valid_from = parse_relative_time(from, now);
1788 else
1789 cert_valid_from = parse_absolute_time(from);
1790
1791 if (*to == '-' || *to == '+')
Damien Miller5b01b0d2013-10-23 16:31:31 +11001792 cert_valid_to = parse_relative_time(to, now);
Damien Miller0a80ca12010-02-27 07:55:05 +11001793 else
1794 cert_valid_to = parse_absolute_time(to);
1795
1796 if (cert_valid_to <= cert_valid_from)
1797 fatal("Empty certificate validity interval");
Darren Tuckera627d422013-06-02 07:31:17 +10001798 free(from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001799}
1800
1801static void
Damien Miller4e270b02010-04-16 15:56:21 +10001802add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001803{
1804 char *val;
1805
Damien Miller044f4a62011-05-05 14:14:08 +10001806 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001807 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001808 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001809 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001810 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001811 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001812 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001813 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001814 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001815 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001816 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001817 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001818 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001819 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001820 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001821 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001822 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001823 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001824 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001825 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001826 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001827 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001828 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1829 val = opt + 14;
1830 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001831 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001832 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001833 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001834 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001835 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1836 val = opt + 15;
1837 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001838 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001839 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001840 fatal("source-address already specified");
1841 if (addr_match_cidr_list(NULL, val) != 0)
1842 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001843 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001844 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001845 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001846}
1847
Ben Lindstrombba81212001-06-25 05:01:22 +00001848static void
Damien Millerd834d352010-06-26 09:48:02 +10001849show_options(const Buffer *optbuf, int v00, int in_critical)
1850{
Damien Miller633de332014-05-15 13:48:26 +10001851 char *name, *arg;
1852 const u_char *data;
Damien Millerd834d352010-06-26 09:48:02 +10001853 u_int dlen;
1854 Buffer options, option;
1855
1856 buffer_init(&options);
1857 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1858
1859 buffer_init(&option);
1860 while (buffer_len(&options) != 0) {
1861 name = buffer_get_string(&options, NULL);
1862 data = buffer_get_string_ptr(&options, &dlen);
1863 buffer_append(&option, data, dlen);
1864 printf(" %s", name);
1865 if ((v00 || !in_critical) &&
1866 (strcmp(name, "permit-X11-forwarding") == 0 ||
1867 strcmp(name, "permit-agent-forwarding") == 0 ||
1868 strcmp(name, "permit-port-forwarding") == 0 ||
1869 strcmp(name, "permit-pty") == 0 ||
1870 strcmp(name, "permit-user-rc") == 0))
1871 printf("\n");
1872 else if ((v00 || in_critical) &&
1873 (strcmp(name, "force-command") == 0 ||
1874 strcmp(name, "source-address") == 0)) {
Damien Miller633de332014-05-15 13:48:26 +10001875 arg = buffer_get_cstring(&option, NULL);
1876 printf(" %s\n", arg);
1877 free(arg);
Damien Millerd834d352010-06-26 09:48:02 +10001878 } else {
1879 printf(" UNKNOWN OPTION (len %u)\n",
1880 buffer_len(&option));
1881 buffer_clear(&option);
1882 }
Darren Tuckera627d422013-06-02 07:31:17 +10001883 free(name);
Damien Millerd834d352010-06-26 09:48:02 +10001884 if (buffer_len(&option) != 0)
1885 fatal("Option corrupt: extra data at end");
1886 }
1887 buffer_free(&option);
1888 buffer_free(&options);
1889}
1890
1891static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001892do_show_cert(struct passwd *pw)
1893{
1894 Key *key;
1895 struct stat st;
1896 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001897 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001898
1899 if (!have_identity)
1900 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001901 if (stat(identity_file, &st) < 0)
1902 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001903 if ((key = key_load_public(identity_file, NULL)) == NULL)
1904 fatal("%s is not a public key", identity_file);
1905 if (!key_is_cert(key))
1906 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001907 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1908
Damien Millerf2b70ca2010-03-05 07:39:35 +11001909 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1910 ca_fp = key_fingerprint(key->cert->signature_key,
1911 SSH_FP_MD5, SSH_FP_HEX);
1912
1913 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001914 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1915 key_cert_type(key));
1916 printf(" Public key: %s %s\n", key_type(key), key_fp);
1917 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001918 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001919 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001920 if (!v00) {
1921 printf(" Serial: %llu\n",
1922 (unsigned long long)key->cert->serial);
1923 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001924 printf(" Valid: %s\n",
1925 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1926 printf(" Principals: ");
1927 if (key->cert->nprincipals == 0)
1928 printf("(none)\n");
1929 else {
1930 for (i = 0; i < key->cert->nprincipals; i++)
1931 printf("\n %s",
1932 key->cert->principals[i]);
1933 printf("\n");
1934 }
Damien Miller4e270b02010-04-16 15:56:21 +10001935 printf(" Critical Options: ");
Damien Miller86687062014-07-02 15:28:02 +10001936 if (buffer_len(key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001937 printf("(none)\n");
1938 else {
1939 printf("\n");
Damien Miller86687062014-07-02 15:28:02 +10001940 show_options(key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001941 }
Damien Miller4e270b02010-04-16 15:56:21 +10001942 if (!v00) {
1943 printf(" Extensions: ");
Damien Miller86687062014-07-02 15:28:02 +10001944 if (buffer_len(key->cert->extensions) == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001945 printf("(none)\n");
1946 else {
1947 printf("\n");
Damien Miller86687062014-07-02 15:28:02 +10001948 show_options(key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001949 }
1950 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001951 exit(0);
1952}
1953
Damien Miller1f0311c2014-05-15 14:24:09 +10001954#ifdef WITH_OPENSSL
Damien Millerf2b70ca2010-03-05 07:39:35 +11001955static void
Damien Millerf3747bf2013-01-18 11:44:04 +11001956load_krl(const char *path, struct ssh_krl **krlp)
1957{
1958 Buffer krlbuf;
1959 int fd;
1960
1961 buffer_init(&krlbuf);
1962 if ((fd = open(path, O_RDONLY)) == -1)
1963 fatal("open %s: %s", path, strerror(errno));
1964 if (!key_load_file(fd, path, &krlbuf))
1965 fatal("Unable to load KRL");
1966 close(fd);
1967 /* XXX check sigs */
1968 if (ssh_krl_from_blob(&krlbuf, krlp, NULL, 0) != 0 ||
1969 *krlp == NULL)
1970 fatal("Invalid KRL file");
1971 buffer_free(&krlbuf);
1972}
1973
1974static void
1975update_krl_from_file(struct passwd *pw, const char *file, const Key *ca,
1976 struct ssh_krl *krl)
1977{
1978 Key *key = NULL;
1979 u_long lnum = 0;
1980 char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES];
1981 unsigned long long serial, serial2;
1982 int i, was_explicit_key, was_sha1, r;
1983 FILE *krl_spec;
1984
1985 path = tilde_expand_filename(file, pw->pw_uid);
1986 if (strcmp(path, "-") == 0) {
1987 krl_spec = stdin;
1988 free(path);
1989 path = xstrdup("(standard input)");
1990 } else if ((krl_spec = fopen(path, "r")) == NULL)
1991 fatal("fopen %s: %s", path, strerror(errno));
1992
1993 if (!quiet)
1994 printf("Revoking from %s\n", path);
1995 while (read_keyfile_line(krl_spec, path, line, sizeof(line),
1996 &lnum) == 0) {
1997 was_explicit_key = was_sha1 = 0;
1998 cp = line + strspn(line, " \t");
1999 /* Trim trailing space, comments and strip \n */
2000 for (i = 0, r = -1; cp[i] != '\0'; i++) {
2001 if (cp[i] == '#' || cp[i] == '\n') {
2002 cp[i] = '\0';
2003 break;
2004 }
2005 if (cp[i] == ' ' || cp[i] == '\t') {
2006 /* Remember the start of a span of whitespace */
2007 if (r == -1)
2008 r = i;
2009 } else
2010 r = -1;
2011 }
2012 if (r != -1)
2013 cp[r] = '\0';
2014 if (*cp == '\0')
2015 continue;
2016 if (strncasecmp(cp, "serial:", 7) == 0) {
2017 if (ca == NULL) {
Damien Millerd234afb2013-08-21 02:42:58 +10002018 fatal("revoking certificates by serial number "
Damien Millerf3747bf2013-01-18 11:44:04 +11002019 "requires specification of a CA key");
2020 }
2021 cp += 7;
2022 cp = cp + strspn(cp, " \t");
2023 errno = 0;
2024 serial = strtoull(cp, &ep, 0);
2025 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2026 fatal("%s:%lu: invalid serial \"%s\"",
2027 path, lnum, cp);
2028 if (errno == ERANGE && serial == ULLONG_MAX)
2029 fatal("%s:%lu: serial out of range",
2030 path, lnum);
2031 serial2 = serial;
2032 if (*ep == '-') {
2033 cp = ep + 1;
2034 errno = 0;
2035 serial2 = strtoull(cp, &ep, 0);
2036 if (*cp == '\0' || *ep != '\0')
2037 fatal("%s:%lu: invalid serial \"%s\"",
2038 path, lnum, cp);
2039 if (errno == ERANGE && serial2 == ULLONG_MAX)
2040 fatal("%s:%lu: serial out of range",
2041 path, lnum);
2042 if (serial2 <= serial)
2043 fatal("%s:%lu: invalid serial range "
2044 "%llu:%llu", path, lnum,
2045 (unsigned long long)serial,
2046 (unsigned long long)serial2);
2047 }
2048 if (ssh_krl_revoke_cert_by_serial_range(krl,
2049 ca, serial, serial2) != 0) {
2050 fatal("%s: revoke serial failed",
2051 __func__);
2052 }
2053 } else if (strncasecmp(cp, "id:", 3) == 0) {
2054 if (ca == NULL) {
Damien Millerd5d9d7b2013-08-21 02:43:27 +10002055 fatal("revoking certificates by key ID "
Damien Millerf3747bf2013-01-18 11:44:04 +11002056 "requires specification of a CA key");
2057 }
2058 cp += 3;
2059 cp = cp + strspn(cp, " \t");
2060 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2061 fatal("%s: revoke key ID failed", __func__);
2062 } else {
2063 if (strncasecmp(cp, "key:", 4) == 0) {
2064 cp += 4;
2065 cp = cp + strspn(cp, " \t");
2066 was_explicit_key = 1;
2067 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2068 cp += 5;
2069 cp = cp + strspn(cp, " \t");
2070 was_sha1 = 1;
2071 } else {
2072 /*
2073 * Just try to process the line as a key.
2074 * Parsing will fail if it isn't.
2075 */
2076 }
2077 if ((key = key_new(KEY_UNSPEC)) == NULL)
2078 fatal("key_new");
2079 if (key_read(key, &cp) != 1)
2080 fatal("%s:%lu: invalid key", path, lnum);
2081 if (was_explicit_key)
2082 r = ssh_krl_revoke_key_explicit(krl, key);
2083 else if (was_sha1)
2084 r = ssh_krl_revoke_key_sha1(krl, key);
2085 else
2086 r = ssh_krl_revoke_key(krl, key);
2087 if (r != 0)
2088 fatal("%s: revoke key failed", __func__);
2089 key_free(key);
2090 }
2091 }
2092 if (strcmp(path, "-") != 0)
2093 fclose(krl_spec);
Damien Miller0d6771b2013-04-23 15:23:24 +10002094 free(path);
Damien Millerf3747bf2013-01-18 11:44:04 +11002095}
2096
2097static void
2098do_gen_krl(struct passwd *pw, int updating, int argc, char **argv)
2099{
2100 struct ssh_krl *krl;
2101 struct stat sb;
2102 Key *ca = NULL;
2103 int fd, i;
2104 char *tmp;
2105 Buffer kbuf;
2106
2107 if (*identity_file == '\0')
2108 fatal("KRL generation requires an output file");
2109 if (stat(identity_file, &sb) == -1) {
2110 if (errno != ENOENT)
2111 fatal("Cannot access KRL \"%s\": %s",
2112 identity_file, strerror(errno));
2113 if (updating)
2114 fatal("KRL \"%s\" does not exist", identity_file);
2115 }
2116 if (ca_key_path != NULL) {
2117 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2118 if ((ca = key_load_public(tmp, NULL)) == NULL)
2119 fatal("Cannot load CA public key %s", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10002120 free(tmp);
Damien Millerf3747bf2013-01-18 11:44:04 +11002121 }
2122
2123 if (updating)
2124 load_krl(identity_file, &krl);
2125 else if ((krl = ssh_krl_init()) == NULL)
2126 fatal("couldn't create KRL");
2127
2128 if (cert_serial != 0)
2129 ssh_krl_set_version(krl, cert_serial);
2130 if (identity_comment != NULL)
2131 ssh_krl_set_comment(krl, identity_comment);
2132
2133 for (i = 0; i < argc; i++)
2134 update_krl_from_file(pw, argv[i], ca, krl);
2135
2136 buffer_init(&kbuf);
2137 if (ssh_krl_to_blob(krl, &kbuf, NULL, 0) != 0)
2138 fatal("Couldn't generate KRL");
2139 if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2140 fatal("open %s: %s", identity_file, strerror(errno));
2141 if (atomicio(vwrite, fd, buffer_ptr(&kbuf), buffer_len(&kbuf)) !=
2142 buffer_len(&kbuf))
2143 fatal("write %s: %s", identity_file, strerror(errno));
2144 close(fd);
2145 buffer_free(&kbuf);
2146 ssh_krl_free(krl);
Damien Miller0d6771b2013-04-23 15:23:24 +10002147 if (ca != NULL)
2148 key_free(ca);
Damien Millerf3747bf2013-01-18 11:44:04 +11002149}
2150
2151static void
2152do_check_krl(struct passwd *pw, int argc, char **argv)
2153{
2154 int i, r, ret = 0;
2155 char *comment;
2156 struct ssh_krl *krl;
2157 Key *k;
2158
2159 if (*identity_file == '\0')
2160 fatal("KRL checking requires an input file");
2161 load_krl(identity_file, &krl);
2162 for (i = 0; i < argc; i++) {
2163 if ((k = key_load_public(argv[i], &comment)) == NULL)
2164 fatal("Cannot load public key %s", argv[i]);
2165 r = ssh_krl_check_key(krl, k);
2166 printf("%s%s%s%s: %s\n", argv[i],
2167 *comment ? " (" : "", comment, *comment ? ")" : "",
2168 r == 0 ? "ok" : "REVOKED");
2169 if (r != 0)
2170 ret = 1;
2171 key_free(k);
2172 free(comment);
2173 }
2174 ssh_krl_free(krl);
2175 exit(ret);
2176}
Damien Miller1f0311c2014-05-15 14:24:09 +10002177#endif
Damien Millerf3747bf2013-01-18 11:44:04 +11002178
2179static void
Damien Miller431f66b1999-11-21 18:31:57 +11002180usage(void)
2181{
Damien Millerf0858de2014-04-20 13:01:30 +10002182 fprintf(stderr,
2183 "usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1]\n"
2184 " [-N new_passphrase] [-C comment] [-f output_keyfile]\n"
2185 " ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]\n"
2186 " ssh-keygen -i [-m key_format] [-f input_keyfile]\n"
2187 " ssh-keygen -e [-m key_format] [-f input_keyfile]\n"
2188 " ssh-keygen -y [-f input_keyfile]\n"
2189 " ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]\n"
2190 " ssh-keygen -l [-f input_keyfile]\n"
2191 " ssh-keygen -B [-f input_keyfile]\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002192#ifdef ENABLE_PKCS11
Damien Millerf0858de2014-04-20 13:01:30 +10002193 fprintf(stderr,
2194 " ssh-keygen -D pkcs11\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002195#endif
Damien Millerf0858de2014-04-20 13:01:30 +10002196 fprintf(stderr,
2197 " ssh-keygen -F hostname [-f known_hosts_file] [-l]\n"
2198 " ssh-keygen -H [-f known_hosts_file]\n"
2199 " ssh-keygen -R hostname [-f known_hosts_file]\n"
2200 " ssh-keygen -r hostname [-f input_keyfile] [-g]\n"
2201 " ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]\n"
2202 " ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]\n"
2203 " [-j start_line] [-K checkpt] [-W generator]\n"
2204 " ssh-keygen -s ca_key -I certificate_identity [-h] [-n principals]\n"
2205 " [-O option] [-V validity_interval] [-z serial_number] file ...\n"
2206 " ssh-keygen -L [-f input_keyfile]\n"
2207 " ssh-keygen -A\n"
2208 " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
2209 " file ...\n"
2210 " ssh-keygen -Q -f krl_file file ...\n");
Damien Miller95def091999-11-25 00:26:21 +11002211 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11002212}
2213
Damien Miller95def091999-11-25 00:26:21 +11002214/*
2215 * Main program for key management.
2216 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002217int
Damien Millerdf8b7db2007-01-05 16:22:57 +11002218main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002219{
Damien Millera41c8b12002-01-22 23:05:08 +11002220 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller390d0562011-10-18 16:05:19 +11002221 char *checkpoint = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11002222 char out_file[MAXPATHLEN], *ep, *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002223 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11002224 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11002225 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11002226 int opt, type, fd;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002227 u_int32_t memory = 0, generator_wanted = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002228 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002229 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
Damien Millerdfceafe2012-07-06 13:44:19 +10002230 unsigned long start_lineno = 0, lines_to_process = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002231 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11002232 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10002233 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002234
Damien Miller95def091999-11-25 00:26:21 +11002235 extern int optind;
2236 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002237
Darren Tuckerce321d82005-10-03 18:11:24 +10002238 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2239 sanitise_stdfd();
2240
Darren Tucker9ac56e92007-01-14 10:19:59 +11002241 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10002242
Damien Miller4314c2b2010-09-10 11:12:09 +10002243 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11002244 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10002245
Kevin Steves3a881912002-07-20 19:05:40 +00002246 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10002247
Damien Miller5428f641999-11-25 11:54:57 +11002248 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11002249 pw = getpwuid(getuid());
2250 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +10002251 printf("No user exists for uid %lu\n", (u_long)getuid());
Damien Miller95def091999-11-25 00:26:21 +11002252 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002253 }
Damien Millereba71ba2000-04-29 23:57:08 +10002254 if (gethostname(hostname, sizeof(hostname)) < 0) {
2255 perror("gethostname");
2256 exit(1);
2257 }
Damien Miller5428f641999-11-25 11:54:57 +11002258
Damien Millerbcd00ab2013-12-07 10:41:55 +11002259 /* Remaining characters: EUYdw */
2260 while ((opt = getopt(argc, argv, "ABHLQXceghiklopquvxy"
2261 "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 +11002262 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10002263 case 'A':
2264 gen_all_hostkeys = 1;
2265 break;
Damien Miller95def091999-11-25 00:26:21 +11002266 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10002267 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10002268 if (errstr)
2269 fatal("Bits has bad value %s (%s)",
2270 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11002271 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002272 case 'F':
2273 find_host = 1;
2274 rr_hostname = optarg;
2275 break;
2276 case 'H':
2277 hash_hosts = 1;
2278 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002279 case 'I':
2280 cert_key_id = optarg;
2281 break;
Damien Millerdfceafe2012-07-06 13:44:19 +10002282 case 'J':
2283 lines_to_process = strtoul(optarg, NULL, 10);
2284 break;
2285 case 'j':
2286 start_lineno = strtoul(optarg, NULL, 10);
2287 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002288 case 'R':
2289 delete_host = 1;
2290 rr_hostname = optarg;
2291 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11002292 case 'L':
2293 show_cert = 1;
2294 break;
Damien Miller95def091999-11-25 00:26:21 +11002295 case 'l':
2296 print_fingerprint = 1;
2297 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002298 case 'B':
2299 print_bubblebabble = 1;
2300 break;
Damien Miller44b25042010-07-02 13:35:01 +10002301 case 'm':
2302 if (strcasecmp(optarg, "RFC4716") == 0 ||
2303 strcasecmp(optarg, "ssh2") == 0) {
2304 convert_format = FMT_RFC4716;
2305 break;
2306 }
2307 if (strcasecmp(optarg, "PKCS8") == 0) {
2308 convert_format = FMT_PKCS8;
2309 break;
2310 }
2311 if (strcasecmp(optarg, "PEM") == 0) {
2312 convert_format = FMT_PEM;
2313 break;
2314 }
2315 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002316 case 'n':
2317 cert_principals = optarg;
2318 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002319 case 'o':
2320 use_new_format = 1;
2321 break;
Damien Miller95def091999-11-25 00:26:21 +11002322 case 'p':
2323 change_passphrase = 1;
2324 break;
Damien Miller95def091999-11-25 00:26:21 +11002325 case 'c':
2326 change_comment = 1;
2327 break;
Damien Miller95def091999-11-25 00:26:21 +11002328 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002329 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2330 sizeof(identity_file))
2331 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002332 have_identity = 1;
2333 break;
Damien Miller37876e92003-05-15 10:19:46 +10002334 case 'g':
2335 print_generic = 1;
2336 break;
Damien Miller95def091999-11-25 00:26:21 +11002337 case 'P':
2338 identity_passphrase = optarg;
2339 break;
Damien Miller95def091999-11-25 00:26:21 +11002340 case 'N':
2341 identity_new_passphrase = optarg;
2342 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002343 case 'Q':
2344 check_krl = 1;
2345 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002346 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002347 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002348 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002349 case 'Z':
2350 new_format_cipher = optarg;
2351 break;
Damien Miller95def091999-11-25 00:26:21 +11002352 case 'C':
2353 identity_comment = optarg;
2354 break;
Damien Miller95def091999-11-25 00:26:21 +11002355 case 'q':
2356 quiet = 1;
2357 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002358 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002359 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002360 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002361 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002362 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002363 case 'h':
2364 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002365 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002366 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002367 case 'k':
2368 gen_krl = 1;
2369 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002370 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002371 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002372 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002373 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002374 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002375 case 'y':
2376 print_public = 1;
2377 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002378 case 's':
2379 ca_key_path = optarg;
2380 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002381 case 't':
2382 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002383 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002384 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002385 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002386 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002387 case 'u':
2388 update_krl = 1;
2389 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002390 case 'v':
2391 if (log_level == SYSLOG_LEVEL_INFO)
2392 log_level = SYSLOG_LEVEL_DEBUG1;
2393 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002394 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002395 log_level < SYSLOG_LEVEL_DEBUG3)
2396 log_level++;
2397 }
2398 break;
Damien Miller37876e92003-05-15 10:19:46 +10002399 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002400 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002401 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002402 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002403 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2404 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002405 if (errstr)
2406 fatal("Desired generator has bad value: %s (%s)",
2407 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002408 break;
2409 case 'a':
Damien Millerbcd00ab2013-12-07 10:41:55 +11002410 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002411 if (errstr)
Damien Millerbcd00ab2013-12-07 10:41:55 +11002412 fatal("Invalid number: %s (%s)",
Damien Millerb089fb52005-05-26 12:16:18 +10002413 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002414 break;
2415 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002416 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002417 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002418 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002419 break;
2420 case 'G':
2421 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002422 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2423 sizeof(out_file))
2424 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002425 break;
2426 case 'T':
2427 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002428 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2429 sizeof(out_file))
2430 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002431 break;
Damien Miller390d0562011-10-18 16:05:19 +11002432 case 'K':
2433 if (strlen(optarg) >= MAXPATHLEN)
2434 fatal("Checkpoint filename too long");
2435 checkpoint = xstrdup(optarg);
2436 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002437 case 'S':
2438 /* XXX - also compare length against bits */
2439 if (BN_hex2bn(&start, optarg) == 0)
2440 fatal("Invalid start point.");
2441 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002442 case 'V':
2443 parse_cert_times(optarg);
2444 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002445 case 'z':
Damien Miller6f3b3622012-11-14 19:04:33 +11002446 errno = 0;
2447 cert_serial = strtoull(optarg, &ep, 10);
2448 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2449 (errno == ERANGE && cert_serial == ULLONG_MAX))
2450 fatal("Invalid serial number \"%s\"", optarg);
Damien Miller4e270b02010-04-16 15:56:21 +10002451 break;
Damien Miller95def091999-11-25 00:26:21 +11002452 case '?':
2453 default:
2454 usage();
2455 }
2456 }
Darren Tucker06930c72003-12-31 11:34:51 +11002457
2458 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002459 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002460
Damien Miller0a80ca12010-02-27 07:55:05 +11002461 argv += optind;
2462 argc -= optind;
2463
2464 if (ca_key_path != NULL) {
Damien Millerf3747bf2013-01-18 11:44:04 +11002465 if (argc < 1 && !gen_krl) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002466 printf("Too few arguments.\n");
2467 usage();
2468 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002469 } else if (argc > 0 && !gen_krl && !check_krl) {
Damien Miller95def091999-11-25 00:26:21 +11002470 printf("Too many arguments.\n");
2471 usage();
2472 }
2473 if (change_passphrase && change_comment) {
2474 printf("Can only have one of -p and -c.\n");
2475 usage();
2476 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002477 if (print_fingerprint && (delete_host || hash_hosts)) {
Damien Millerec77c952013-01-09 15:58:00 +11002478 printf("Cannot use -l with -H or -R.\n");
Darren Tucker0f7e9102008-06-08 12:54:29 +10002479 usage();
2480 }
Damien Miller1f0311c2014-05-15 14:24:09 +10002481#ifdef WITH_OPENSSL
Damien Millerf3747bf2013-01-18 11:44:04 +11002482 if (gen_krl) {
2483 do_gen_krl(pw, update_krl, argc, argv);
2484 return (0);
2485 }
2486 if (check_krl) {
2487 do_check_krl(pw, argc, argv);
2488 return (0);
2489 }
Damien Miller1f0311c2014-05-15 14:24:09 +10002490#endif
Damien Miller0a80ca12010-02-27 07:55:05 +11002491 if (ca_key_path != NULL) {
2492 if (cert_key_id == NULL)
2493 fatal("Must specify key id (-I) when certifying");
2494 do_ca_sign(pw, argc, argv);
2495 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002496 if (show_cert)
2497 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002498 if (delete_host || hash_hosts || find_host)
2499 do_known_hosts(pw, rr_hostname);
Damien Millerec77c952013-01-09 15:58:00 +11002500 if (pkcs11provider != NULL)
2501 do_download(pw);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002502 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002503 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002504 if (change_passphrase)
2505 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002506 if (change_comment)
2507 do_change_comment(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002508#ifdef WITH_OPENSSL
Damien Miller44b25042010-07-02 13:35:01 +10002509 if (convert_to)
2510 do_convert_to(pw);
2511 if (convert_from)
2512 do_convert_from(pw);
Damien Miller1f0311c2014-05-15 14:24:09 +10002513#endif
Damien Millereba71ba2000-04-29 23:57:08 +10002514 if (print_public)
2515 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002516 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002517 unsigned int n = 0;
2518
2519 if (have_identity) {
2520 n = do_print_resource_record(pw,
2521 identity_file, rr_hostname);
2522 if (n == 0) {
2523 perror(identity_file);
2524 exit(1);
2525 }
2526 exit(0);
2527 } else {
2528
2529 n += do_print_resource_record(pw,
2530 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2531 n += do_print_resource_record(pw,
2532 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
Damien Miller3bde12a2012-06-20 21:51:11 +10002533 n += do_print_resource_record(pw,
2534 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
Damien Miller16cd3922014-05-15 13:45:58 +10002535 n += do_print_resource_record(pw,
2536 _PATH_HOST_ED25519_KEY_FILE, rr_hostname);
Damien Millercb314822006-03-26 13:48:01 +11002537 if (n == 0)
2538 fatal("no keys found.");
2539 exit(0);
2540 }
Damien Miller37876e92003-05-15 10:19:46 +10002541 }
Damien Miller95def091999-11-25 00:26:21 +11002542
Darren Tucker019cefe2003-08-02 22:40:07 +10002543 if (do_gen_candidates) {
2544 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002545
Darren Tucker019cefe2003-08-02 22:40:07 +10002546 if (out == NULL) {
2547 error("Couldn't open modulus candidate file \"%s\": %s",
2548 out_file, strerror(errno));
2549 return (1);
2550 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002551 if (bits == 0)
2552 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002553 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002554 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002555
2556 return (0);
2557 }
2558
2559 if (do_screen_candidates) {
2560 FILE *in;
Damien Miller78d22712013-02-12 11:03:36 +11002561 FILE *out = fopen(out_file, "a");
Darren Tucker019cefe2003-08-02 22:40:07 +10002562
2563 if (have_identity && strcmp(identity_file, "-") != 0) {
2564 if ((in = fopen(identity_file, "r")) == NULL) {
2565 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002566 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002567 strerror(errno));
2568 }
2569 } else
2570 in = stdin;
2571
2572 if (out == NULL) {
2573 fatal("Couldn't open moduli file \"%s\": %s",
2574 out_file, strerror(errno));
2575 }
Damien Millerbcd00ab2013-12-07 10:41:55 +11002576 if (prime_test(in, out, rounds == 0 ? 100 : rounds,
2577 generator_wanted, checkpoint,
Damien Millerdfceafe2012-07-06 13:44:19 +10002578 start_lineno, lines_to_process) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002579 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002580 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002581 }
2582
Damien Miller58f1baf2011-05-05 14:06:15 +10002583 if (gen_all_hostkeys) {
2584 do_gen_all_hostkeys(pw);
2585 return (0);
2586 }
2587
Damien Millerf14be5c2005-11-05 15:15:49 +11002588 if (key_type_name == NULL)
2589 key_type_name = "rsa";
2590
Damien Millere39cacc2000-11-29 12:18:44 +11002591 type = key_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002592 type_bits_valid(type, &bits);
2593
Darren Tucker3af2ac52005-11-29 13:10:24 +11002594 if (!quiet)
2595 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002596 private = key_generate(type, bits);
2597 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002598 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002599 exit(1);
2600 }
2601 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002602
2603 if (!have_identity)
2604 ask_filename(pw, "Enter file in which to save the key");
2605
Damien Miller788f2122005-11-05 15:14:59 +11002606 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002607 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2608 pw->pw_dir, _PATH_SSH_USER_DIR);
2609 if (strstr(identity_file, dotsshdir) != NULL) {
2610 if (stat(dotsshdir, &st) < 0) {
2611 if (errno != ENOENT) {
2612 error("Could not stat %s: %s", dotsshdir,
2613 strerror(errno));
2614 } else if (mkdir(dotsshdir, 0700) < 0) {
2615 error("Could not create directory '%s': %s",
2616 dotsshdir, strerror(errno));
2617 } else if (!quiet)
2618 printf("Created directory '%s'.\n", dotsshdir);
2619 }
Damien Miller95def091999-11-25 00:26:21 +11002620 }
2621 /* If the file already exists, ask the user to confirm. */
2622 if (stat(identity_file, &st) >= 0) {
2623 char yesno[3];
2624 printf("%s already exists.\n", identity_file);
2625 printf("Overwrite (y/n)? ");
2626 fflush(stdout);
2627 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2628 exit(1);
2629 if (yesno[0] != 'y' && yesno[0] != 'Y')
2630 exit(1);
2631 }
2632 /* Ask for a passphrase (twice). */
2633 if (identity_passphrase)
2634 passphrase1 = xstrdup(identity_passphrase);
2635 else if (identity_new_passphrase)
2636 passphrase1 = xstrdup(identity_new_passphrase);
2637 else {
2638passphrase_again:
2639 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002640 read_passphrase("Enter passphrase (empty for no "
2641 "passphrase): ", RP_ALLOW_STDIN);
2642 passphrase2 = read_passphrase("Enter same passphrase again: ",
2643 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002644 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002645 /*
2646 * The passphrases do not match. Clear them and
2647 * retry.
2648 */
Damien Millera5103f42014-02-04 11:20:14 +11002649 explicit_bzero(passphrase1, strlen(passphrase1));
2650 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002651 free(passphrase1);
2652 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002653 printf("Passphrases do not match. Try again.\n");
2654 goto passphrase_again;
2655 }
2656 /* Clear the other copy of the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002657 explicit_bzero(passphrase2, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002658 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002659 }
2660
Damien Miller95def091999-11-25 00:26:21 +11002661 if (identity_comment) {
2662 strlcpy(comment, identity_comment, sizeof(comment));
2663 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002664 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002665 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2666 }
2667
2668 /* Save the key with the given passphrase and comment. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11002669 if (!key_save_private(private, identity_file, passphrase1, comment,
2670 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002671 printf("Saving the key failed: %s.\n", identity_file);
Damien Millera5103f42014-02-04 11:20:14 +11002672 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002673 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002674 exit(1);
2675 }
2676 /* Clear the passphrase. */
Damien Millera5103f42014-02-04 11:20:14 +11002677 explicit_bzero(passphrase1, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002678 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002679
2680 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002681 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002682
2683 if (!quiet)
2684 printf("Your identification has been saved in %s.\n", identity_file);
2685
Damien Miller95def091999-11-25 00:26:21 +11002686 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002687 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2688 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002689 printf("Could not save your public key in %s\n", identity_file);
2690 exit(1);
2691 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002692 f = fdopen(fd, "w");
2693 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002694 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002695 exit(1);
2696 }
Damien Millereba71ba2000-04-29 23:57:08 +10002697 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002698 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002699 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002700 fclose(f);
2701
2702 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002703 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002704 char *ra = key_fingerprint(public, SSH_FP_MD5,
2705 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002706 printf("Your public key has been saved in %s.\n",
2707 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002708 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002709 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002710 printf("The key's randomart image is:\n");
2711 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10002712 free(ra);
2713 free(fp);
Damien Miller95def091999-11-25 00:26:21 +11002714 }
Damien Millereba71ba2000-04-29 23:57:08 +10002715
2716 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002717 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002718}