blob: eae83a4612f2a695f607584fe41bf4d287ea3073 [file] [log] [blame]
Damien Miller5be9d9e2013-12-07 11:24:01 +11001/* $OpenBSD: ssh-keygen.c,v 1.238 2013/12/06 13:39:49 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100025
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100029#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000041#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "authfile.h"
43#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110044#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
46#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100047#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110048#include "match.h"
49#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100050#include "dns.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110051#include "ssh.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110052#include "ssh2.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110053#include "ssh-pkcs11.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110054#include "atomicio.h"
55#include "krl.h"
Ben Lindstromcd392282001-07-04 03:44:03 +000056
Damien Miller3f54a9f2005-11-05 14:52:18 +110057/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
58#define DEFAULT_BITS 2048
59#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100060#define DEFAULT_BITS_ECDSA 256
Damien Miller3f54a9f2005-11-05 14:52:18 +110061u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller5428f641999-11-25 11:54:57 +110063/*
64 * Flag indicating that we just want to change the passphrase. This can be
65 * set on the command line.
66 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067int change_passphrase = 0;
68
Damien Miller5428f641999-11-25 11:54:57 +110069/*
70 * Flag indicating that we just want to change the comment. This can be set
71 * on the command line.
72 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073int change_comment = 0;
74
75int quiet = 0;
76
Darren Tucker35c45532008-06-13 04:43:15 +100077int log_level = SYSLOG_LEVEL_INFO;
78
Damien Miller4b42d7f2005-03-01 21:48:35 +110079/* Flag indicating that we want to hash a known_hosts file */
80int hash_hosts = 0;
81/* Flag indicating that we want lookup a host in known_hosts file */
82int find_host = 0;
83/* Flag indicating that we want to delete a host from a known_hosts file */
84int delete_host = 0;
85
Damien Millerf2b70ca2010-03-05 07:39:35 +110086/* Flag indicating that we want to show the contents of a certificate */
87int show_cert = 0;
88
Damien Miller10f6f6b1999-11-17 17:29:08 +110089/* Flag indicating that we just want to see the key fingerprint */
90int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000091int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110092
Damien Miller431f66b1999-11-21 18:31:57 +110093/* The identity file name, given on the command line or entered by the user. */
94char identity_file[1024];
95int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
97/* This is set to the passphrase if given on the command line. */
98char *identity_passphrase = NULL;
99
100/* This is set to the new passphrase if given on the command line. */
101char *identity_new_passphrase = NULL;
102
103/* This is set to the new comment if given on the command line. */
104char *identity_comment = NULL;
105
Damien Miller0a80ca12010-02-27 07:55:05 +1100106/* Path to CA key when certifying keys. */
107char *ca_key_path = NULL;
108
Damien Miller4e270b02010-04-16 15:56:21 +1000109/* Certificate serial number */
Damien Miller55aca022012-12-03 11:25:30 +1100110unsigned long long cert_serial = 0;
Damien Miller4e270b02010-04-16 15:56:21 +1000111
Damien Miller0a80ca12010-02-27 07:55:05 +1100112/* Key type when certifying */
113u_int cert_key_type = SSH2_CERT_TYPE_USER;
114
115/* "key ID" of signed key */
116char *cert_key_id = NULL;
117
118/* Comma-separated list of principal names for certifying keys */
119char *cert_principals = NULL;
120
121/* Validity period for certificates */
122u_int64_t cert_valid_from = 0;
123u_int64_t cert_valid_to = ~0ULL;
124
Damien Miller4e270b02010-04-16 15:56:21 +1000125/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000126#define CERTOPT_X_FWD (1)
127#define CERTOPT_AGENT_FWD (1<<1)
128#define CERTOPT_PORT_FWD (1<<2)
129#define CERTOPT_PTY (1<<3)
130#define CERTOPT_USER_RC (1<<4)
131#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
132 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
133u_int32_t certflags_flags = CERTOPT_DEFAULT;
134char *certflags_command = NULL;
135char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100136
Damien Miller44b25042010-07-02 13:35:01 +1000137/* Conversion to/from various formats */
138int convert_to = 0;
139int convert_from = 0;
140enum {
141 FMT_RFC4716,
142 FMT_PKCS8,
143 FMT_PEM
144} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000145int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000146int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100147
Damien Millera41c8b12002-01-22 23:05:08 +1100148char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000149
Damien Miller757f34e2010-08-05 13:05:31 +1000150/* Load key from this PKCS#11 provider */
151char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000152
Damien Millerbcd00ab2013-12-07 10:41:55 +1100153/* Use new OpenSSH private key format when writing SSH2 keys instead of PEM */
154int use_new_format = 0;
155
156/* Cipher for new-format private keys */
157char *new_format_cipher = NULL;
158
159/*
160 * Number of KDF rounds to derive new format keys /
161 * number of primality trials when screening moduli.
162 */
163int rounds = 0;
164
Damien Miller431f66b1999-11-21 18:31:57 +1100165/* argv0 */
166extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
Damien Millereba71ba2000-04-29 23:57:08 +1000168char hostname[MAXHOSTNAMELEN];
169
Darren Tucker770fc012004-05-13 16:24:32 +1000170/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000171int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Damien Millerdfceafe2012-07-06 13:44:19 +1000172int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
173 unsigned long);
Darren Tucker770fc012004-05-13 16:24:32 +1000174
Ben Lindstrombba81212001-06-25 05:01:22 +0000175static void
Damien Miller884b63a2011-05-05 14:14:52 +1000176type_bits_valid(int type, u_int32_t *bitsp)
Damien Miller58f1baf2011-05-05 14:06:15 +1000177{
178 u_int maxbits;
179
180 if (type == KEY_UNSPEC) {
181 fprintf(stderr, "unknown key type %s\n", key_type_name);
182 exit(1);
183 }
Damien Miller884b63a2011-05-05 14:14:52 +1000184 if (*bitsp == 0) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000185 if (type == KEY_DSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000186 *bitsp = DEFAULT_BITS_DSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000187 else if (type == KEY_ECDSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000188 *bitsp = DEFAULT_BITS_ECDSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000189 else
Damien Miller884b63a2011-05-05 14:14:52 +1000190 *bitsp = DEFAULT_BITS;
Damien Miller58f1baf2011-05-05 14:06:15 +1000191 }
192 maxbits = (type == KEY_DSA) ?
193 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
Damien Miller884b63a2011-05-05 14:14:52 +1000194 if (*bitsp > maxbits) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000195 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
196 exit(1);
197 }
Damien Miller884b63a2011-05-05 14:14:52 +1000198 if (type == KEY_DSA && *bitsp != 1024)
Damien Miller58f1baf2011-05-05 14:06:15 +1000199 fatal("DSA keys must be 1024 bits");
Damien Miller5be9d9e2013-12-07 11:24:01 +1100200 else if (type != KEY_ECDSA && type != KEY_ED25519 && *bitsp < 768)
Damien Miller58f1baf2011-05-05 14:06:15 +1000201 fatal("Key must at least be 768 bits");
Damien Miller884b63a2011-05-05 14:14:52 +1000202 else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(*bitsp) == -1)
Damien Miller58f1baf2011-05-05 14:06:15 +1000203 fatal("Invalid ECDSA key length - valid lengths are "
204 "256, 384 or 521 bits");
205}
206
207static void
Damien Miller431f66b1999-11-21 18:31:57 +1100208ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209{
Damien Miller95def091999-11-25 00:26:21 +1100210 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100211 char *name = NULL;
212
Damien Miller993dd552002-02-19 15:22:47 +1100213 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000214 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100215 else {
Damien Miller993dd552002-02-19 15:22:47 +1100216 switch (key_type_from_name(key_type_name)) {
217 case KEY_RSA1:
218 name = _PATH_SSH_CLIENT_IDENTITY;
219 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000220 case KEY_DSA_CERT:
221 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100222 case KEY_DSA:
223 name = _PATH_SSH_CLIENT_ID_DSA;
224 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100225#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000226 case KEY_ECDSA_CERT:
227 case KEY_ECDSA:
228 name = _PATH_SSH_CLIENT_ID_ECDSA;
229 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100230#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000231 case KEY_RSA_CERT:
232 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100233 case KEY_RSA:
234 name = _PATH_SSH_CLIENT_ID_RSA;
235 break;
Damien Miller5be9d9e2013-12-07 11:24:01 +1100236 case KEY_ED25519:
237 case KEY_ED25519_CERT:
238 name = _PATH_SSH_CLIENT_ID_ED25519;
239 break;
Damien Miller993dd552002-02-19 15:22:47 +1100240 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100241 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100242 exit(1);
243 break;
244 }
Damien Miller90967402006-03-26 14:07:26 +1100245 }
Damien Millere39cacc2000-11-29 12:18:44 +1100246 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000247 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100248 if (fgets(buf, sizeof(buf), stdin) == NULL)
249 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000250 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100251 if (strcmp(buf, "") != 0)
252 strlcpy(identity_file, buf, sizeof(identity_file));
253 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100254}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Ben Lindstrombba81212001-06-25 05:01:22 +0000256static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000257load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000258{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000259 char *pass;
260 Key *prv;
261
Ben Lindstroma3700052001-04-05 23:26:32 +0000262 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000263 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000264 if (identity_passphrase)
265 pass = xstrdup(identity_passphrase);
266 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000267 pass = read_passphrase("Enter passphrase: ",
268 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000269 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000270 memset(pass, 0, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +1000271 free(pass);
Damien Millereba71ba2000-04-29 23:57:08 +1000272 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000273 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000274}
275
Damien Miller874d77b2000-10-14 16:23:11 +1100276#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000277#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100278#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000279#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000280
Ben Lindstrombba81212001-06-25 05:01:22 +0000281static void
Damien Miller44b25042010-07-02 13:35:01 +1000282do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000283{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000284 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000285 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100286 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000287
Damien Millera563cce2012-04-22 11:07:28 +1000288 if (k->type == KEY_RSA1) {
289 fprintf(stderr, "version 1 keys are not supported\n");
290 exit(1);
291 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000292 if (key_to_blob(k, &blob, &len) <= 0) {
293 fprintf(stderr, "key_to_blob failed\n");
294 exit(1);
295 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100296 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
297 snprintf(comment, sizeof(comment),
298 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000299 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000300 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100301
302 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
303 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000304 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100305 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000306 key_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000307 free(blob);
Damien Millereba71ba2000-04-29 23:57:08 +1000308 exit(0);
309}
310
Ben Lindstrombba81212001-06-25 05:01:22 +0000311static void
Damien Miller44b25042010-07-02 13:35:01 +1000312do_convert_to_pkcs8(Key *k)
313{
314 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000315 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000316 case KEY_RSA:
317 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
318 fatal("PEM_write_RSA_PUBKEY failed");
319 break;
320 case KEY_DSA:
321 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
322 fatal("PEM_write_DSA_PUBKEY failed");
323 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000324#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000325 case KEY_ECDSA:
326 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
327 fatal("PEM_write_EC_PUBKEY failed");
328 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000329#endif
Damien Miller44b25042010-07-02 13:35:01 +1000330 default:
331 fatal("%s: unsupported key type %s", __func__, key_type(k));
332 }
333 exit(0);
334}
335
336static void
337do_convert_to_pem(Key *k)
338{
339 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000340 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000341 case KEY_RSA:
342 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
343 fatal("PEM_write_RSAPublicKey failed");
344 break;
345#if notyet /* OpenSSH 0.9.8 lacks this function */
346 case KEY_DSA:
347 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
348 fatal("PEM_write_DSAPublicKey failed");
349 break;
350#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000351 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000352 default:
353 fatal("%s: unsupported key type %s", __func__, key_type(k));
354 }
355 exit(0);
356}
357
358static void
359do_convert_to(struct passwd *pw)
360{
361 Key *k;
362 struct stat st;
363
364 if (!have_identity)
365 ask_filename(pw, "Enter file in which the key is");
366 if (stat(identity_file, &st) < 0)
367 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
368 if ((k = key_load_public(identity_file, NULL)) == NULL) {
369 if ((k = load_identity(identity_file)) == NULL) {
370 fprintf(stderr, "load failed\n");
371 exit(1);
372 }
373 }
Damien Miller44b25042010-07-02 13:35:01 +1000374
375 switch (convert_format) {
376 case FMT_RFC4716:
377 do_convert_to_ssh2(pw, k);
378 break;
379 case FMT_PKCS8:
380 do_convert_to_pkcs8(k);
381 break;
382 case FMT_PEM:
383 do_convert_to_pem(k);
384 break;
385 default:
386 fatal("%s: unknown key format %d", __func__, convert_format);
387 }
388 exit(0);
389}
390
391static void
Damien Miller874d77b2000-10-14 16:23:11 +1100392buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
393{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000394 u_int bignum_bits = buffer_get_int(b);
395 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000396
Damien Miller874d77b2000-10-14 16:23:11 +1100397 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000398 fatal("buffer_get_bignum_bits: input buffer too small: "
399 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100400 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
401 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100402 buffer_consume(b, bytes);
403}
404
Ben Lindstrombba81212001-06-25 05:01:22 +0000405static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000406do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100407{
408 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100409 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100410 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000411 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000412 int magic, rlen, ktype, i1, i2, i3, i4;
413 u_int slen;
414 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100415
416 buffer_init(&b);
417 buffer_append(&b, blob, blen);
418
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100419 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100420 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
421 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
422 buffer_free(&b);
423 return NULL;
424 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000425 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100426 type = buffer_get_string(&b, NULL);
427 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000428 i2 = buffer_get_int(&b);
429 i3 = buffer_get_int(&b);
430 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100431 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100432 if (strcmp(cipher, "none") != 0) {
433 error("unsupported cipher %s", cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000434 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100435 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000436 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100437 return NULL;
438 }
Darren Tuckera627d422013-06-02 07:31:17 +1000439 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100440
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000441 if (strstr(type, "dsa")) {
442 ktype = KEY_DSA;
443 } else if (strstr(type, "rsa")) {
444 ktype = KEY_RSA;
445 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100446 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000447 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100448 return NULL;
449 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000450 key = key_new_private(ktype);
Darren Tuckera627d422013-06-02 07:31:17 +1000451 free(type);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000452
453 switch (key->type) {
454 case KEY_DSA:
455 buffer_get_bignum_bits(&b, key->dsa->p);
456 buffer_get_bignum_bits(&b, key->dsa->g);
457 buffer_get_bignum_bits(&b, key->dsa->q);
458 buffer_get_bignum_bits(&b, key->dsa->pub_key);
459 buffer_get_bignum_bits(&b, key->dsa->priv_key);
460 break;
461 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100462 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000463 debug("e %lx", e);
464 if (e < 30) {
465 e <<= 8;
466 e += buffer_get_char(&b);
467 debug("e %lx", e);
468 e <<= 8;
469 e += buffer_get_char(&b);
470 debug("e %lx", e);
471 }
472 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000473 buffer_free(&b);
474 key_free(key);
475 return NULL;
476 }
477 buffer_get_bignum_bits(&b, key->rsa->d);
478 buffer_get_bignum_bits(&b, key->rsa->n);
479 buffer_get_bignum_bits(&b, key->rsa->iqmp);
480 buffer_get_bignum_bits(&b, key->rsa->q);
481 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000482 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000483 break;
484 }
Damien Miller874d77b2000-10-14 16:23:11 +1100485 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000486 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000487 error("do_convert_private_ssh2_from_blob: "
488 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100489 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000490
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000491 /* try the key */
492 key_sign(key, &sig, &slen, data, sizeof(data));
493 key_verify(key, sig, slen, data, sizeof(data));
Darren Tuckera627d422013-06-02 07:31:17 +1000494 free(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100495 return key;
496}
497
Damien Miller8056a9d2006-03-15 12:05:40 +1100498static int
499get_line(FILE *fp, char *line, size_t len)
500{
501 int c;
502 size_t pos = 0;
503
504 line[0] = '\0';
505 while ((c = fgetc(fp)) != EOF) {
506 if (pos >= len - 1) {
507 fprintf(stderr, "input line too long.\n");
508 exit(1);
509 }
Damien Miller90967402006-03-26 14:07:26 +1100510 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100511 case '\r':
512 c = fgetc(fp);
513 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
514 fprintf(stderr, "unget: %s\n", strerror(errno));
515 exit(1);
516 }
517 return pos;
518 case '\n':
519 return pos;
520 }
521 line[pos++] = c;
522 line[pos] = '\0';
523 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100524 /* We reached EOF */
525 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100526}
527
Ben Lindstrombba81212001-06-25 05:01:22 +0000528static void
Damien Miller44b25042010-07-02 13:35:01 +1000529do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000530{
Damien Millereba71ba2000-04-29 23:57:08 +1000531 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000532 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100533 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000534 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000535 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000536 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000537 FILE *fp;
538
Damien Millerba3420a2010-06-26 09:39:07 +1000539 if ((fp = fopen(identity_file, "r")) == NULL)
540 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000541 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100542 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
Damien Miller746d1a62013-07-18 16:13:02 +1000543 if (blen > 0 && line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000544 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000545 if (strncmp(line, "----", 4) == 0 ||
546 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100547 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000548 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000549 if (strstr(line, " END ") != NULL) {
550 break;
551 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000552 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000553 continue;
554 }
Damien Miller30c3d422000-05-09 11:02:59 +1000555 if (escaped) {
556 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000557 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000558 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000559 }
Damien Millereba71ba2000-04-29 23:57:08 +1000560 strlcat(encoded, line, sizeof(encoded));
561 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000562 len = strlen(encoded);
563 if (((len % 4) == 3) &&
564 (encoded[len-1] == '=') &&
565 (encoded[len-2] == '=') &&
566 (encoded[len-3] == '='))
567 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100568 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000569 if (blen < 0) {
570 fprintf(stderr, "uudecode failed.\n");
571 exit(1);
572 }
Damien Miller44b25042010-07-02 13:35:01 +1000573 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100574 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100575 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000576 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100577 fprintf(stderr, "decode blob failed.\n");
578 exit(1);
579 }
Damien Miller44b25042010-07-02 13:35:01 +1000580 fclose(fp);
581}
582
583static void
584do_convert_from_pkcs8(Key **k, int *private)
585{
586 EVP_PKEY *pubkey;
587 FILE *fp;
588
589 if ((fp = fopen(identity_file, "r")) == NULL)
590 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
591 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
592 fatal("%s: %s is not a recognised public key format", __func__,
593 identity_file);
594 }
595 fclose(fp);
596 switch (EVP_PKEY_type(pubkey->type)) {
597 case EVP_PKEY_RSA:
598 *k = key_new(KEY_UNSPEC);
599 (*k)->type = KEY_RSA;
600 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
601 break;
602 case EVP_PKEY_DSA:
603 *k = key_new(KEY_UNSPEC);
604 (*k)->type = KEY_DSA;
605 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
606 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000607#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000608 case EVP_PKEY_EC:
609 *k = key_new(KEY_UNSPEC);
610 (*k)->type = KEY_ECDSA;
611 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
Damien Millerb472a902010-11-05 10:19:49 +1100612 (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000613 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000614#endif
Damien Miller44b25042010-07-02 13:35:01 +1000615 default:
616 fatal("%s: unsupported pubkey type %d", __func__,
617 EVP_PKEY_type(pubkey->type));
618 }
619 EVP_PKEY_free(pubkey);
620 return;
621}
622
623static void
624do_convert_from_pem(Key **k, int *private)
625{
626 FILE *fp;
627 RSA *rsa;
628#ifdef notyet
629 DSA *dsa;
630#endif
631
632 if ((fp = fopen(identity_file, "r")) == NULL)
633 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
634 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
635 *k = key_new(KEY_UNSPEC);
636 (*k)->type = KEY_RSA;
637 (*k)->rsa = rsa;
638 fclose(fp);
639 return;
640 }
641#if notyet /* OpenSSH 0.9.8 lacks this function */
642 rewind(fp);
643 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
644 *k = key_new(KEY_UNSPEC);
645 (*k)->type = KEY_DSA;
646 (*k)->dsa = dsa;
647 fclose(fp);
648 return;
649 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000650 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000651#endif
652 fatal("%s: unrecognised raw private key format", __func__);
653}
654
655static void
656do_convert_from(struct passwd *pw)
657{
658 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000659 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000660 struct stat st;
661
662 if (!have_identity)
663 ask_filename(pw, "Enter file in which the key is");
664 if (stat(identity_file, &st) < 0)
665 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
666
667 switch (convert_format) {
668 case FMT_RFC4716:
669 do_convert_from_ssh2(pw, &k, &private);
670 break;
671 case FMT_PKCS8:
672 do_convert_from_pkcs8(&k, &private);
673 break;
674 case FMT_PEM:
675 do_convert_from_pem(&k, &private);
676 break;
677 default:
678 fatal("%s: unknown key format %d", __func__, convert_format);
679 }
680
681 if (!private)
682 ok = key_write(k, stdout);
683 if (ok)
684 fprintf(stdout, "\n");
685 else {
686 switch (k->type) {
687 case KEY_DSA:
688 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
689 NULL, 0, NULL, NULL);
690 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000691#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000692 case KEY_ECDSA:
693 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
694 NULL, 0, NULL, NULL);
695 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000696#endif
Damien Miller44b25042010-07-02 13:35:01 +1000697 case KEY_RSA:
698 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
699 NULL, 0, NULL, NULL);
700 break;
701 default:
702 fatal("%s: unsupported key type %s", __func__,
703 key_type(k));
704 }
705 }
706
Damien Miller874d77b2000-10-14 16:23:11 +1100707 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100708 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100709 exit(1);
710 }
Damien Millereba71ba2000-04-29 23:57:08 +1000711 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000712 exit(0);
713}
714
Ben Lindstrombba81212001-06-25 05:01:22 +0000715static void
Damien Millereba71ba2000-04-29 23:57:08 +1000716do_print_public(struct passwd *pw)
717{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000718 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000719 struct stat st;
720
721 if (!have_identity)
722 ask_filename(pw, "Enter file in which the key is");
723 if (stat(identity_file, &st) < 0) {
724 perror(identity_file);
725 exit(1);
726 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000727 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000728 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000729 fprintf(stderr, "load failed\n");
730 exit(1);
731 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000732 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000733 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000734 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000735 fprintf(stdout, "\n");
736 exit(0);
737}
738
Ben Lindstromcd392282001-07-04 03:44:03 +0000739static void
Damien Miller757f34e2010-08-05 13:05:31 +1000740do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000741{
Damien Miller7ea845e2010-02-12 09:21:02 +1100742#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000743 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100744 int i, nkeys;
Damien Millerec77c952013-01-09 15:58:00 +1100745 enum fp_rep rep;
746 enum fp_type fptype;
747 char *fp, *ra;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000748
Damien Miller1422c082013-01-09 16:44:54 +1100749 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
750 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
751
Damien Miller7ea845e2010-02-12 09:21:02 +1100752 pkcs11_init(0);
753 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
754 if (nkeys <= 0)
755 fatal("cannot read public key from pkcs11");
756 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100757 if (print_fingerprint) {
758 fp = key_fingerprint(keys[i], fptype, rep);
759 ra = key_fingerprint(keys[i], SSH_FP_MD5,
760 SSH_FP_RANDOMART);
761 printf("%u %s %s (PKCS11 key)\n", key_size(keys[i]),
762 fp, key_type(keys[i]));
763 if (log_level >= SYSLOG_LEVEL_VERBOSE)
764 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000765 free(ra);
766 free(fp);
Damien Millerec77c952013-01-09 15:58:00 +1100767 } else {
768 key_write(keys[i], stdout);
769 fprintf(stdout, "\n");
770 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000771 key_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000772 }
Darren Tuckera627d422013-06-02 07:31:17 +1000773 free(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100774 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000775 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100776#else
777 fatal("no pkcs11 support");
778#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000779}
Ben Lindstromcd392282001-07-04 03:44:03 +0000780
Ben Lindstrombba81212001-06-25 05:01:22 +0000781static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100782do_fingerprint(struct passwd *pw)
783{
Damien Miller98c7ad62000-03-09 21:27:49 +1100784 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000785 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000786 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100787 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000788 enum fp_rep rep;
789 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100790 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100791
Ben Lindstromd0fca422001-03-26 13:44:06 +0000792 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
793 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000794
Damien Miller95def091999-11-25 00:26:21 +1100795 if (!have_identity)
796 ask_filename(pw, "Enter file in which the key is");
797 if (stat(identity_file, &st) < 0) {
798 perror(identity_file);
799 exit(1);
800 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000801 public = key_load_public(identity_file, &comment);
802 if (public != NULL) {
803 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100804 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000805 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
806 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000807 if (log_level >= SYSLOG_LEVEL_VERBOSE)
808 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100809 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +1000810 free(comment);
811 free(ra);
812 free(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100813 exit(0);
814 }
Damien Miller40b59852006-06-13 13:00:25 +1000815 if (comment) {
Darren Tuckera627d422013-06-02 07:31:17 +1000816 free(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000817 comment = NULL;
818 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100819
Damien Millerba3420a2010-06-26 09:39:07 +1000820 if ((f = fopen(identity_file, "r")) == NULL)
821 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100822
Damien Millerba3420a2010-06-26 09:39:07 +1000823 while (fgets(line, sizeof(line), f)) {
824 if ((cp = strchr(line, '\n')) == NULL) {
825 error("line %d too long: %.40s...",
826 num + 1, line);
827 skip = 1;
828 continue;
Damien Miller95def091999-11-25 00:26:21 +1100829 }
Damien Millerba3420a2010-06-26 09:39:07 +1000830 num++;
831 if (skip) {
832 skip = 0;
833 continue;
834 }
835 *cp = '\0';
836
837 /* Skip leading whitespace, empty and comment lines. */
838 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
839 ;
840 if (!*cp || *cp == '\n' || *cp == '#')
841 continue;
842 i = strtol(cp, &ep, 10);
843 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
844 int quoted = 0;
845 comment = cp;
846 for (; *cp && (quoted || (*cp != ' ' &&
847 *cp != '\t')); cp++) {
848 if (*cp == '\\' && cp[1] == '"')
849 cp++; /* Skip both */
850 else if (*cp == '"')
851 quoted = !quoted;
852 }
853 if (!*cp)
854 continue;
855 *cp++ = '\0';
856 }
857 ep = cp;
858 public = key_new(KEY_RSA1);
859 if (key_read(public, &cp) != 1) {
860 cp = ep;
861 key_free(public);
862 public = key_new(KEY_UNSPEC);
863 if (key_read(public, &cp) != 1) {
864 key_free(public);
865 continue;
866 }
867 }
868 comment = *cp ? cp : comment;
869 fp = key_fingerprint(public, fptype, rep);
870 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
871 printf("%u %s %s (%s)\n", key_size(public), fp,
872 comment ? comment : "no comment", key_type(public));
873 if (log_level >= SYSLOG_LEVEL_VERBOSE)
874 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000875 free(ra);
876 free(fp);
Damien Millerba3420a2010-06-26 09:39:07 +1000877 key_free(public);
878 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100879 }
Damien Millerba3420a2010-06-26 09:39:07 +1000880 fclose(f);
881
Damien Miller98c7ad62000-03-09 21:27:49 +1100882 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100883 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100884 exit(1);
885 }
Damien Miller95def091999-11-25 00:26:21 +1100886 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100887}
888
Damien Miller4b42d7f2005-03-01 21:48:35 +1100889static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000890do_gen_all_hostkeys(struct passwd *pw)
891{
892 struct {
893 char *key_type;
894 char *key_type_display;
895 char *path;
896 } key_types[] = {
897 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
898 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
899 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100900#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000901 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100902#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100903 { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
Damien Miller58f1baf2011-05-05 14:06:15 +1000904 { NULL, NULL, NULL }
905 };
906
907 int first = 0;
908 struct stat st;
909 Key *private, *public;
910 char comment[1024];
911 int i, type, fd;
912 FILE *f;
913
914 for (i = 0; key_types[i].key_type; i++) {
915 if (stat(key_types[i].path, &st) == 0)
916 continue;
917 if (errno != ENOENT) {
918 printf("Could not stat %s: %s", key_types[i].path,
919 strerror(errno));
920 first = 0;
921 continue;
922 }
923
924 if (first == 0) {
925 first = 1;
926 printf("%s: generating new host keys: ", __progname);
927 }
928 printf("%s ", key_types[i].key_type_display);
929 fflush(stdout);
Damien Miller58f1baf2011-05-05 14:06:15 +1000930 type = key_type_from_name(key_types[i].key_type);
931 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
932 bits = 0;
933 type_bits_valid(type, &bits);
934 private = key_generate(type, bits);
935 if (private == NULL) {
936 fprintf(stderr, "key_generate failed\n");
937 first = 0;
938 continue;
939 }
940 public = key_from_private(private);
941 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
942 hostname);
Damien Millerbcd00ab2013-12-07 10:41:55 +1100943 if (!key_save_private(private, identity_file, "", comment,
944 use_new_format, new_format_cipher, rounds)) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000945 printf("Saving the key failed: %s.\n", identity_file);
946 key_free(private);
947 key_free(public);
948 first = 0;
949 continue;
950 }
951 key_free(private);
Damien Miller58f1baf2011-05-05 14:06:15 +1000952 strlcat(identity_file, ".pub", sizeof(identity_file));
953 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
954 if (fd == -1) {
955 printf("Could not save your public key in %s\n",
956 identity_file);
957 key_free(public);
958 first = 0;
959 continue;
960 }
961 f = fdopen(fd, "w");
962 if (f == NULL) {
963 printf("fdopen %s failed\n", identity_file);
964 key_free(public);
965 first = 0;
966 continue;
967 }
968 if (!key_write(public, f)) {
969 fprintf(stderr, "write key failed\n");
970 key_free(public);
971 first = 0;
972 continue;
973 }
974 fprintf(f, " %s\n", comment);
975 fclose(f);
976 key_free(public);
977
978 }
979 if (first != 0)
980 printf("\n");
981}
982
983static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100984printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100985{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000986 if (print_fingerprint) {
987 enum fp_rep rep;
988 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000989 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000990
991 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
992 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
993 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100994 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000995 printf("%u %s %s (%s)\n", key_size(public), fp, name,
996 key_type(public));
997 if (log_level >= SYSLOG_LEVEL_VERBOSE)
998 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000999 free(ra);
1000 free(fp);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001001 } else {
1002 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
1003 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +11001004 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001005 if (!key_write(public, f))
1006 fatal("key_write failed");
1007 fprintf(f, "\n");
1008 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001009}
1010
1011static void
1012do_known_hosts(struct passwd *pw, const char *name)
1013{
1014 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +11001015 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001016 char *cp, *cp2, *kp, *kp2;
1017 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +11001018 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001019 int ca;
Damien Miller66085482013-09-14 09:45:03 +10001020 int found_key = 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001021
1022 if (!have_identity) {
1023 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1024 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1025 sizeof(identity_file))
1026 fatal("Specified known hosts path too long");
Darren Tuckera627d422013-06-02 07:31:17 +10001027 free(cp);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001028 have_identity = 1;
1029 }
1030 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +10001031 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001032
1033 /*
1034 * Find hosts goes to stdout, hash and deletions happen in-place
1035 * A corner case is ssh-keygen -HF foo, which should go to stdout
1036 */
1037 if (!find_host && (hash_hosts || delete_host)) {
1038 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1039 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1040 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1041 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1042 fatal("known_hosts path too long");
1043 umask(077);
1044 if ((c = mkstemp(tmp)) == -1)
1045 fatal("mkstemp: %s", strerror(errno));
1046 if ((out = fdopen(c, "w")) == NULL) {
1047 c = errno;
1048 unlink(tmp);
1049 fatal("fdopen: %s", strerror(c));
1050 }
1051 inplace = 1;
1052 }
1053
1054 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001055 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001056 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001057 skip = 1;
1058 invalid = 1;
1059 continue;
1060 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001061 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001062 if (skip) {
1063 skip = 0;
1064 continue;
1065 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001066 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001067
1068 /* Skip leading whitespace, empty and comment lines. */
1069 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1070 ;
1071 if (!*cp || *cp == '\n' || *cp == '#') {
1072 if (inplace)
1073 fprintf(out, "%s\n", cp);
1074 continue;
1075 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001076 /* Check whether this is a CA key */
1077 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1078 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1079 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1080 ca = 1;
1081 cp += sizeof(CA_MARKER);
1082 } else
1083 ca = 0;
1084
Damien Miller4b42d7f2005-03-01 21:48:35 +11001085 /* Find the end of the host name portion. */
1086 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1087 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001088
Damien Miller4b42d7f2005-03-01 21:48:35 +11001089 if (*kp == '\0' || *(kp + 1) == '\0') {
1090 error("line %d missing key: %.40s...",
1091 num, line);
1092 invalid = 1;
1093 continue;
1094 }
1095 *kp++ = '\0';
1096 kp2 = kp;
1097
Damien Miller0a80ca12010-02-27 07:55:05 +11001098 pub = key_new(KEY_RSA1);
1099 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001100 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +11001101 key_free(pub);
1102 pub = key_new(KEY_UNSPEC);
1103 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001104 error("line %d invalid key: %.40s...",
1105 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001106 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001107 invalid = 1;
1108 continue;
1109 }
1110 }
1111
1112 if (*cp == HASH_DELIM) {
1113 if (find_host || delete_host) {
1114 cp2 = host_hash(name, cp, strlen(cp));
1115 if (cp2 == NULL) {
1116 error("line %d: invalid hashed "
1117 "name: %.64s...", num, line);
1118 invalid = 1;
1119 continue;
1120 }
1121 c = (strcmp(cp2, cp) == 0);
1122 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001123 if (!quiet)
1124 printf("# Host %s found: "
1125 "line %d type %s%s\n", name,
1126 num, key_type(pub),
1127 ca ? " (CA key)" : "");
Damien Miller0a80ca12010-02-27 07:55:05 +11001128 printhost(out, cp, pub, ca, 0);
Damien Miller66085482013-09-14 09:45:03 +10001129 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001130 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001131 if (delete_host) {
1132 if (!c && !ca)
1133 printhost(out, cp, pub, ca, 0);
1134 else
1135 printf("# Host %s found: "
1136 "line %d type %s\n", name,
1137 num, key_type(pub));
1138 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001139 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +11001140 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001141 } else {
1142 if (find_host || delete_host) {
1143 c = (match_hostname(name, cp,
1144 strlen(cp)) == 1);
1145 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001146 if (!quiet)
1147 printf("# Host %s found: "
1148 "line %d type %s%s\n", name,
1149 num, key_type(pub),
1150 ca ? " (CA key)" : "");
Damien Miller0a80ca12010-02-27 07:55:05 +11001151 printhost(out, name, pub,
1152 ca, hash_hosts && !ca);
Damien Miller66085482013-09-14 09:45:03 +10001153 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001154 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001155 if (delete_host) {
1156 if (!c && !ca)
1157 printhost(out, cp, pub, ca, 0);
1158 else
1159 printf("# Host %s found: "
1160 "line %d type %s\n", name,
1161 num, key_type(pub));
1162 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001163 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +11001164 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001165 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001166 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001167 if (ca) {
1168 fprintf(stderr, "Warning: "
1169 "ignoring CA key for host: "
1170 "%.64s\n", cp2);
1171 printhost(out, cp2, pub, ca, 0);
1172 } else if (strcspn(cp2, "*?!") !=
1173 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001174 fprintf(stderr, "Warning: "
1175 "ignoring host name with "
1176 "metacharacters: %.64s\n",
1177 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +11001178 printhost(out, cp2, pub, ca, 0);
1179 } else
1180 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +11001181 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001182 has_unhashed = 1;
1183 }
1184 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001185 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001186 }
1187 fclose(in);
1188
1189 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001190 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001191 identity_file);
1192 if (inplace) {
1193 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001194 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001195 fclose(out);
1196 unlink(tmp);
1197 }
1198 exit(1);
1199 }
1200
1201 if (inplace) {
1202 fclose(out);
1203
1204 /* Backup existing file */
1205 if (unlink(old) == -1 && errno != ENOENT)
1206 fatal("unlink %.100s: %s", old, strerror(errno));
1207 if (link(identity_file, old) == -1)
1208 fatal("link %.100s to %.100s: %s", identity_file, old,
1209 strerror(errno));
1210 /* Move new one into place */
1211 if (rename(tmp, identity_file) == -1) {
1212 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1213 strerror(errno));
1214 unlink(tmp);
1215 unlink(old);
1216 exit(1);
1217 }
1218
1219 fprintf(stderr, "%s updated.\n", identity_file);
1220 fprintf(stderr, "Original contents retained as %s\n", old);
1221 if (has_unhashed) {
1222 fprintf(stderr, "WARNING: %s contains unhashed "
1223 "entries\n", old);
1224 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001225 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001226 }
1227 }
1228
Damien Miller66085482013-09-14 09:45:03 +10001229 exit (find_host && !found_key);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001230}
1231
Damien Miller95def091999-11-25 00:26:21 +11001232/*
1233 * Perform changing a passphrase. The argument is the passwd structure
1234 * for the current user.
1235 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001236static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001237do_change_passphrase(struct passwd *pw)
1238{
Damien Miller95def091999-11-25 00:26:21 +11001239 char *comment;
1240 char *old_passphrase, *passphrase1, *passphrase2;
1241 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001242 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001243
Damien Miller95def091999-11-25 00:26:21 +11001244 if (!have_identity)
1245 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001246 if (stat(identity_file, &st) < 0) {
1247 perror(identity_file);
1248 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249 }
Damien Miller95def091999-11-25 00:26:21 +11001250 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001251 private = key_load_private(identity_file, "", &comment);
1252 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001253 if (identity_passphrase)
1254 old_passphrase = xstrdup(identity_passphrase);
1255 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001256 old_passphrase =
1257 read_passphrase("Enter old passphrase: ",
1258 RP_ALLOW_STDIN);
1259 private = key_load_private(identity_file, old_passphrase,
1260 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001261 memset(old_passphrase, 0, strlen(old_passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001262 free(old_passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001263 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001264 printf("Bad passphrase.\n");
1265 exit(1);
1266 }
Damien Miller95def091999-11-25 00:26:21 +11001267 }
1268 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001269
Damien Miller95def091999-11-25 00:26:21 +11001270 /* Ask the new passphrase (twice). */
1271 if (identity_new_passphrase) {
1272 passphrase1 = xstrdup(identity_new_passphrase);
1273 passphrase2 = NULL;
1274 } else {
1275 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001276 read_passphrase("Enter new passphrase (empty for no "
1277 "passphrase): ", RP_ALLOW_STDIN);
1278 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001279 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001280
1281 /* Verify that they are the same. */
1282 if (strcmp(passphrase1, passphrase2) != 0) {
1283 memset(passphrase1, 0, strlen(passphrase1));
1284 memset(passphrase2, 0, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001285 free(passphrase1);
1286 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001287 printf("Pass phrases do not match. Try again.\n");
1288 exit(1);
1289 }
1290 /* Destroy the other copy. */
1291 memset(passphrase2, 0, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001292 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001293 }
1294
1295 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001296 if (!key_save_private(private, identity_file, passphrase1, comment,
1297 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001298 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001299 memset(passphrase1, 0, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001300 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001301 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001302 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001303 exit(1);
1304 }
1305 /* Destroy the passphrase and the copy of the key in memory. */
1306 memset(passphrase1, 0, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001307 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001308 key_free(private); /* Destroys contents */
Darren Tuckera627d422013-06-02 07:31:17 +10001309 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001310
1311 printf("Your identification has been saved with the new passphrase.\n");
1312 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001313}
1314
Damien Miller37876e92003-05-15 10:19:46 +10001315/*
1316 * Print the SSHFP RR.
1317 */
Damien Millercb314822006-03-26 13:48:01 +11001318static int
1319do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001320{
1321 Key *public;
1322 char *comment = NULL;
1323 struct stat st;
1324
Damien Millercb314822006-03-26 13:48:01 +11001325 if (fname == NULL)
Damien Miller5bb88332013-07-18 16:13:37 +10001326 fatal("%s: no filename", __func__);
Damien Millercb314822006-03-26 13:48:01 +11001327 if (stat(fname, &st) < 0) {
1328 if (errno == ENOENT)
1329 return 0;
1330 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001331 exit(1);
1332 }
Damien Millercb314822006-03-26 13:48:01 +11001333 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001334 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001335 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001336 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001337 free(comment);
Damien Millercb314822006-03-26 13:48:01 +11001338 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001339 }
1340 if (comment)
Darren Tuckera627d422013-06-02 07:31:17 +10001341 free(comment);
Damien Miller37876e92003-05-15 10:19:46 +10001342
Damien Millercb314822006-03-26 13:48:01 +11001343 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001344 exit(1);
1345}
Damien Miller37876e92003-05-15 10:19:46 +10001346
Damien Miller95def091999-11-25 00:26:21 +11001347/*
1348 * Change the comment of a private key file.
1349 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001350static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001351do_change_comment(struct passwd *pw)
1352{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001353 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001354 Key *private;
1355 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001356 struct stat st;
1357 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001358 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359
Damien Miller95def091999-11-25 00:26:21 +11001360 if (!have_identity)
1361 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001362 if (stat(identity_file, &st) < 0) {
1363 perror(identity_file);
1364 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001366 private = key_load_private(identity_file, "", &comment);
1367 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001368 if (identity_passphrase)
1369 passphrase = xstrdup(identity_passphrase);
1370 else if (identity_new_passphrase)
1371 passphrase = xstrdup(identity_new_passphrase);
1372 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001373 passphrase = read_passphrase("Enter passphrase: ",
1374 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001375 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001376 private = key_load_private(identity_file, passphrase, &comment);
1377 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001378 memset(passphrase, 0, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001379 free(passphrase);
Damien Miller95def091999-11-25 00:26:21 +11001380 printf("Bad passphrase.\n");
1381 exit(1);
1382 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001383 } else {
1384 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001385 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001386 if (private->type != KEY_RSA1) {
1387 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1388 key_free(private);
1389 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001390 }
Damien Miller95def091999-11-25 00:26:21 +11001391 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392
Damien Miller95def091999-11-25 00:26:21 +11001393 if (identity_comment) {
1394 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1395 } else {
1396 printf("Enter new comment: ");
1397 fflush(stdout);
1398 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1399 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001400 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001401 exit(1);
1402 }
Damien Miller14b017d2007-09-17 16:09:15 +10001403 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001404 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405
Damien Miller95def091999-11-25 00:26:21 +11001406 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001407 if (!key_save_private(private, identity_file, passphrase, new_comment,
1408 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001409 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001410 memset(passphrase, 0, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001411 free(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001412 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001413 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001414 exit(1);
1415 }
Damien Miller95def091999-11-25 00:26:21 +11001416 memset(passphrase, 0, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001417 free(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001418 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001419 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420
Damien Miller95def091999-11-25 00:26:21 +11001421 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001422 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1423 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001424 printf("Could not save your public key in %s\n", identity_file);
1425 exit(1);
1426 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001427 f = fdopen(fd, "w");
1428 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001429 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001430 exit(1);
1431 }
Damien Millereba71ba2000-04-29 23:57:08 +10001432 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001433 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001434 key_free(public);
1435 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001436 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437
Darren Tuckera627d422013-06-02 07:31:17 +10001438 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001439
1440 printf("The comment in your key file has been changed.\n");
1441 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001442}
1443
Damien Miller0a80ca12010-02-27 07:55:05 +11001444static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001445fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001446{
1447 char from[32], to[32];
1448 static char ret[64];
1449 time_t tt;
1450 struct tm *tm;
1451
1452 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001453 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001454 return "forever";
1455
Damien Millerf2b70ca2010-03-05 07:39:35 +11001456 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001457 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001458 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001459 tm = localtime(&tt);
1460 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1461 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001462 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001463 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001464 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001465 tm = localtime(&tt);
1466 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1467 }
1468
Damien Millerf2b70ca2010-03-05 07:39:35 +11001469 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001470 snprintf(ret, sizeof(ret), "before %s", to);
1471 return ret;
1472 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001473 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001474 snprintf(ret, sizeof(ret), "after %s", from);
1475 return ret;
1476 }
1477
1478 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1479 return ret;
1480}
1481
1482static void
Damien Miller4e270b02010-04-16 15:56:21 +10001483add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001484{
1485 debug3("%s: %s", __func__, name);
1486 buffer_put_cstring(c, name);
1487 buffer_put_string(c, NULL, 0);
1488}
1489
1490static void
Damien Miller4e270b02010-04-16 15:56:21 +10001491add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001492{
1493 Buffer b;
1494
1495 debug3("%s: %s=%s", __func__, name, value);
1496 buffer_init(&b);
1497 buffer_put_cstring(&b, value);
1498
1499 buffer_put_cstring(c, name);
1500 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1501
1502 buffer_free(&b);
1503}
1504
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001505#define OPTIONS_CRITICAL 1
1506#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001507static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001508prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001509{
Damien Miller0a80ca12010-02-27 07:55:05 +11001510 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001511 if ((which & OPTIONS_CRITICAL) != 0 &&
1512 certflags_command != NULL)
1513 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001514 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001515 (certflags_flags & CERTOPT_X_FWD) != 0)
1516 add_flag_option(c, "permit-X11-forwarding");
1517 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001518 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001519 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001520 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1521 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001522 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001523 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1524 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001525 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001526 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1527 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001528 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001529 if ((which & OPTIONS_CRITICAL) != 0 &&
1530 certflags_src_addr != NULL)
1531 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001532}
1533
Damien Miller757f34e2010-08-05 13:05:31 +10001534static Key *
1535load_pkcs11_key(char *path)
1536{
1537#ifdef ENABLE_PKCS11
1538 Key **keys = NULL, *public, *private = NULL;
1539 int i, nkeys;
1540
1541 if ((public = key_load_public(path, NULL)) == NULL)
1542 fatal("Couldn't load CA public key \"%s\"", path);
1543
1544 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1545 debug3("%s: %d keys", __func__, nkeys);
1546 if (nkeys <= 0)
1547 fatal("cannot read public key from pkcs11");
1548 for (i = 0; i < nkeys; i++) {
1549 if (key_equal_public(public, keys[i])) {
1550 private = keys[i];
1551 continue;
1552 }
1553 key_free(keys[i]);
1554 }
Darren Tuckera627d422013-06-02 07:31:17 +10001555 free(keys);
Damien Miller757f34e2010-08-05 13:05:31 +10001556 key_free(public);
1557 return private;
1558#else
1559 fatal("no pkcs11 support");
1560#endif /* ENABLE_PKCS11 */
1561}
1562
Damien Miller0a80ca12010-02-27 07:55:05 +11001563static void
1564do_ca_sign(struct passwd *pw, int argc, char **argv)
1565{
1566 int i, fd;
1567 u_int n;
1568 Key *ca, *public;
1569 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1570 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001571 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001572
Damien Miller4e270b02010-04-16 15:56:21 +10001573 if (key_type_name != NULL) {
1574 switch (key_type_from_name(key_type_name)) {
1575 case KEY_RSA_CERT_V00:
1576 case KEY_DSA_CERT_V00:
1577 v00 = 1;
1578 break;
1579 case KEY_UNSPEC:
1580 if (strcasecmp(key_type_name, "v00") == 0) {
1581 v00 = 1;
1582 break;
1583 } else if (strcasecmp(key_type_name, "v01") == 0)
1584 break;
1585 /* FALLTHROUGH */
1586 default:
1587 fprintf(stderr, "unknown key type %s\n", key_type_name);
1588 exit(1);
1589 }
1590 }
1591
Damien Miller757f34e2010-08-05 13:05:31 +10001592 pkcs11_init(1);
1593 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1594 if (pkcs11provider != NULL) {
1595 if ((ca = load_pkcs11_key(tmp)) == NULL)
1596 fatal("No PKCS#11 key matching %s found", ca_key_path);
1597 } else if ((ca = load_identity(tmp)) == NULL)
1598 fatal("Couldn't load CA key \"%s\"", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001599 free(tmp);
Damien Miller757f34e2010-08-05 13:05:31 +10001600
Damien Miller0a80ca12010-02-27 07:55:05 +11001601 for (i = 0; i < argc; i++) {
1602 /* Split list of principals */
1603 n = 0;
1604 if (cert_principals != NULL) {
1605 otmp = tmp = xstrdup(cert_principals);
1606 plist = NULL;
1607 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1608 plist = xrealloc(plist, n + 1, sizeof(*plist));
1609 if (*(plist[n] = xstrdup(cp)) == '\0')
1610 fatal("Empty principal name");
1611 }
Darren Tuckera627d422013-06-02 07:31:17 +10001612 free(otmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001613 }
1614
1615 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1616 if ((public = key_load_public(tmp, &comment)) == NULL)
1617 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001618 if (public->type != KEY_RSA && public->type != KEY_DSA &&
Damien Miller5be9d9e2013-12-07 11:24:01 +11001619 public->type != KEY_ECDSA && public->type != KEY_ED25519)
Damien Miller0a80ca12010-02-27 07:55:05 +11001620 fatal("%s: key \"%s\" type %s cannot be certified",
1621 __func__, tmp, key_type(public));
1622
1623 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001624 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001625 fatal("Could not upgrade key %s to certificate", tmp);
1626 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001627 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001628 public->cert->key_id = xstrdup(cert_key_id);
1629 public->cert->nprincipals = n;
1630 public->cert->principals = plist;
1631 public->cert->valid_after = cert_valid_from;
1632 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001633 if (v00) {
1634 prepare_options_buf(&public->cert->critical,
1635 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1636 } else {
1637 prepare_options_buf(&public->cert->critical,
1638 OPTIONS_CRITICAL);
1639 prepare_options_buf(&public->cert->extensions,
1640 OPTIONS_EXTENSIONS);
1641 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001642 public->cert->signature_key = key_from_private(ca);
1643
1644 if (key_certify(public, ca) != 0)
1645 fatal("Couldn't not certify key %s", tmp);
1646
1647 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1648 *cp = '\0';
1649 xasprintf(&out, "%s-cert.pub", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001650 free(tmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001651
1652 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1653 fatal("Could not open \"%s\" for writing: %s", out,
1654 strerror(errno));
1655 if ((f = fdopen(fd, "w")) == NULL)
1656 fatal("%s: fdopen: %s", __func__, strerror(errno));
1657 if (!key_write(public, f))
1658 fatal("Could not write certified key to %s", out);
1659 fprintf(f, " %s\n", comment);
1660 fclose(f);
1661
Damien Miller4e270b02010-04-16 15:56:21 +10001662 if (!quiet) {
1663 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1664 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001665 out, public->cert->key_id,
1666 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001667 cert_principals != NULL ? " for " : "",
1668 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001669 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001670 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001671
1672 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001673 free(out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001674 }
Damien Miller757f34e2010-08-05 13:05:31 +10001675 pkcs11_terminate();
Damien Miller0a80ca12010-02-27 07:55:05 +11001676 exit(0);
1677}
1678
1679static u_int64_t
1680parse_relative_time(const char *s, time_t now)
1681{
1682 int64_t mul, secs;
1683
1684 mul = *s == '-' ? -1 : 1;
1685
1686 if ((secs = convtime(s + 1)) == -1)
1687 fatal("Invalid relative certificate time %s", s);
1688 if (mul == -1 && secs > now)
1689 fatal("Certificate time %s cannot be represented", s);
1690 return now + (u_int64_t)(secs * mul);
1691}
1692
1693static u_int64_t
1694parse_absolute_time(const char *s)
1695{
1696 struct tm tm;
1697 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001698 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001699
Damien Miller2ca342b2010-03-03 12:14:15 +11001700 /*
1701 * POSIX strptime says "The application shall ensure that there
1702 * is white-space or other non-alphanumeric characters between
1703 * any two conversion specifications" so arrange things this way.
1704 */
1705 switch (strlen(s)) {
1706 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001707 fmt = "%Y-%m-%d";
1708 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001709 break;
1710 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001711 fmt = "%Y-%m-%dT%H:%M:%S";
1712 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001713 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1714 break;
1715 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001716 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001717 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001718
1719 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001720 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001721 fatal("Invalid certificate time %s", s);
1722 if ((tt = mktime(&tm)) < 0)
1723 fatal("Certificate time %s cannot be represented", s);
1724 return (u_int64_t)tt;
1725}
1726
1727static void
1728parse_cert_times(char *timespec)
1729{
1730 char *from, *to;
1731 time_t now = time(NULL);
1732 int64_t secs;
1733
1734 /* +timespec relative to now */
1735 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1736 if ((secs = convtime(timespec + 1)) == -1)
1737 fatal("Invalid relative certificate life %s", timespec);
1738 cert_valid_to = now + secs;
1739 /*
1740 * Backdate certificate one minute to avoid problems on hosts
1741 * with poorly-synchronised clocks.
1742 */
1743 cert_valid_from = ((now - 59)/ 60) * 60;
1744 return;
1745 }
1746
1747 /*
1748 * from:to, where
1749 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1750 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1751 */
1752 from = xstrdup(timespec);
1753 to = strchr(from, ':');
1754 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001755 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001756 *to++ = '\0';
1757
1758 if (*from == '-' || *from == '+')
1759 cert_valid_from = parse_relative_time(from, now);
1760 else
1761 cert_valid_from = parse_absolute_time(from);
1762
1763 if (*to == '-' || *to == '+')
Damien Miller5b01b0d2013-10-23 16:31:31 +11001764 cert_valid_to = parse_relative_time(to, now);
Damien Miller0a80ca12010-02-27 07:55:05 +11001765 else
1766 cert_valid_to = parse_absolute_time(to);
1767
1768 if (cert_valid_to <= cert_valid_from)
1769 fatal("Empty certificate validity interval");
Darren Tuckera627d422013-06-02 07:31:17 +10001770 free(from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001771}
1772
1773static void
Damien Miller4e270b02010-04-16 15:56:21 +10001774add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001775{
1776 char *val;
1777
Damien Miller044f4a62011-05-05 14:14:08 +10001778 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001779 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001780 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001781 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001782 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001783 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001784 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001785 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001786 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001787 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001788 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001789 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001790 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001791 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001792 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001793 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001794 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001795 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001796 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001797 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001798 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001799 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001800 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1801 val = opt + 14;
1802 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001803 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001804 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001805 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001806 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001807 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1808 val = opt + 15;
1809 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001810 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001811 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001812 fatal("source-address already specified");
1813 if (addr_match_cidr_list(NULL, val) != 0)
1814 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001815 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001816 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001817 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001818}
1819
Ben Lindstrombba81212001-06-25 05:01:22 +00001820static void
Damien Millerd834d352010-06-26 09:48:02 +10001821show_options(const Buffer *optbuf, int v00, int in_critical)
1822{
Damien Miller746d1a62013-07-18 16:13:02 +10001823 char *name;
1824 u_char *data;
Damien Millerd834d352010-06-26 09:48:02 +10001825 u_int dlen;
1826 Buffer options, option;
1827
1828 buffer_init(&options);
1829 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1830
1831 buffer_init(&option);
1832 while (buffer_len(&options) != 0) {
1833 name = buffer_get_string(&options, NULL);
1834 data = buffer_get_string_ptr(&options, &dlen);
1835 buffer_append(&option, data, dlen);
1836 printf(" %s", name);
1837 if ((v00 || !in_critical) &&
1838 (strcmp(name, "permit-X11-forwarding") == 0 ||
1839 strcmp(name, "permit-agent-forwarding") == 0 ||
1840 strcmp(name, "permit-port-forwarding") == 0 ||
1841 strcmp(name, "permit-pty") == 0 ||
1842 strcmp(name, "permit-user-rc") == 0))
1843 printf("\n");
1844 else if ((v00 || in_critical) &&
1845 (strcmp(name, "force-command") == 0 ||
1846 strcmp(name, "source-address") == 0)) {
1847 data = buffer_get_string(&option, NULL);
1848 printf(" %s\n", data);
Darren Tuckera627d422013-06-02 07:31:17 +10001849 free(data);
Damien Millerd834d352010-06-26 09:48:02 +10001850 } else {
1851 printf(" UNKNOWN OPTION (len %u)\n",
1852 buffer_len(&option));
1853 buffer_clear(&option);
1854 }
Darren Tuckera627d422013-06-02 07:31:17 +10001855 free(name);
Damien Millerd834d352010-06-26 09:48:02 +10001856 if (buffer_len(&option) != 0)
1857 fatal("Option corrupt: extra data at end");
1858 }
1859 buffer_free(&option);
1860 buffer_free(&options);
1861}
1862
1863static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001864do_show_cert(struct passwd *pw)
1865{
1866 Key *key;
1867 struct stat st;
1868 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001869 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001870
1871 if (!have_identity)
1872 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001873 if (stat(identity_file, &st) < 0)
1874 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001875 if ((key = key_load_public(identity_file, NULL)) == NULL)
1876 fatal("%s is not a public key", identity_file);
1877 if (!key_is_cert(key))
1878 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001879 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1880
Damien Millerf2b70ca2010-03-05 07:39:35 +11001881 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1882 ca_fp = key_fingerprint(key->cert->signature_key,
1883 SSH_FP_MD5, SSH_FP_HEX);
1884
1885 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001886 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1887 key_cert_type(key));
1888 printf(" Public key: %s %s\n", key_type(key), key_fp);
1889 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001890 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001891 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001892 if (!v00) {
1893 printf(" Serial: %llu\n",
1894 (unsigned long long)key->cert->serial);
1895 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001896 printf(" Valid: %s\n",
1897 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1898 printf(" Principals: ");
1899 if (key->cert->nprincipals == 0)
1900 printf("(none)\n");
1901 else {
1902 for (i = 0; i < key->cert->nprincipals; i++)
1903 printf("\n %s",
1904 key->cert->principals[i]);
1905 printf("\n");
1906 }
Damien Miller4e270b02010-04-16 15:56:21 +10001907 printf(" Critical Options: ");
1908 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001909 printf("(none)\n");
1910 else {
1911 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001912 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001913 }
Damien Miller4e270b02010-04-16 15:56:21 +10001914 if (!v00) {
1915 printf(" Extensions: ");
1916 if (buffer_len(&key->cert->extensions) == 0)
1917 printf("(none)\n");
1918 else {
1919 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001920 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001921 }
1922 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001923 exit(0);
1924}
1925
1926static void
Damien Millerf3747bf2013-01-18 11:44:04 +11001927load_krl(const char *path, struct ssh_krl **krlp)
1928{
1929 Buffer krlbuf;
1930 int fd;
1931
1932 buffer_init(&krlbuf);
1933 if ((fd = open(path, O_RDONLY)) == -1)
1934 fatal("open %s: %s", path, strerror(errno));
1935 if (!key_load_file(fd, path, &krlbuf))
1936 fatal("Unable to load KRL");
1937 close(fd);
1938 /* XXX check sigs */
1939 if (ssh_krl_from_blob(&krlbuf, krlp, NULL, 0) != 0 ||
1940 *krlp == NULL)
1941 fatal("Invalid KRL file");
1942 buffer_free(&krlbuf);
1943}
1944
1945static void
1946update_krl_from_file(struct passwd *pw, const char *file, const Key *ca,
1947 struct ssh_krl *krl)
1948{
1949 Key *key = NULL;
1950 u_long lnum = 0;
1951 char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES];
1952 unsigned long long serial, serial2;
1953 int i, was_explicit_key, was_sha1, r;
1954 FILE *krl_spec;
1955
1956 path = tilde_expand_filename(file, pw->pw_uid);
1957 if (strcmp(path, "-") == 0) {
1958 krl_spec = stdin;
1959 free(path);
1960 path = xstrdup("(standard input)");
1961 } else if ((krl_spec = fopen(path, "r")) == NULL)
1962 fatal("fopen %s: %s", path, strerror(errno));
1963
1964 if (!quiet)
1965 printf("Revoking from %s\n", path);
1966 while (read_keyfile_line(krl_spec, path, line, sizeof(line),
1967 &lnum) == 0) {
1968 was_explicit_key = was_sha1 = 0;
1969 cp = line + strspn(line, " \t");
1970 /* Trim trailing space, comments and strip \n */
1971 for (i = 0, r = -1; cp[i] != '\0'; i++) {
1972 if (cp[i] == '#' || cp[i] == '\n') {
1973 cp[i] = '\0';
1974 break;
1975 }
1976 if (cp[i] == ' ' || cp[i] == '\t') {
1977 /* Remember the start of a span of whitespace */
1978 if (r == -1)
1979 r = i;
1980 } else
1981 r = -1;
1982 }
1983 if (r != -1)
1984 cp[r] = '\0';
1985 if (*cp == '\0')
1986 continue;
1987 if (strncasecmp(cp, "serial:", 7) == 0) {
1988 if (ca == NULL) {
Damien Millerd234afb2013-08-21 02:42:58 +10001989 fatal("revoking certificates by serial number "
Damien Millerf3747bf2013-01-18 11:44:04 +11001990 "requires specification of a CA key");
1991 }
1992 cp += 7;
1993 cp = cp + strspn(cp, " \t");
1994 errno = 0;
1995 serial = strtoull(cp, &ep, 0);
1996 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
1997 fatal("%s:%lu: invalid serial \"%s\"",
1998 path, lnum, cp);
1999 if (errno == ERANGE && serial == ULLONG_MAX)
2000 fatal("%s:%lu: serial out of range",
2001 path, lnum);
2002 serial2 = serial;
2003 if (*ep == '-') {
2004 cp = ep + 1;
2005 errno = 0;
2006 serial2 = strtoull(cp, &ep, 0);
2007 if (*cp == '\0' || *ep != '\0')
2008 fatal("%s:%lu: invalid serial \"%s\"",
2009 path, lnum, cp);
2010 if (errno == ERANGE && serial2 == ULLONG_MAX)
2011 fatal("%s:%lu: serial out of range",
2012 path, lnum);
2013 if (serial2 <= serial)
2014 fatal("%s:%lu: invalid serial range "
2015 "%llu:%llu", path, lnum,
2016 (unsigned long long)serial,
2017 (unsigned long long)serial2);
2018 }
2019 if (ssh_krl_revoke_cert_by_serial_range(krl,
2020 ca, serial, serial2) != 0) {
2021 fatal("%s: revoke serial failed",
2022 __func__);
2023 }
2024 } else if (strncasecmp(cp, "id:", 3) == 0) {
2025 if (ca == NULL) {
Damien Millerd5d9d7b2013-08-21 02:43:27 +10002026 fatal("revoking certificates by key ID "
Damien Millerf3747bf2013-01-18 11:44:04 +11002027 "requires specification of a CA key");
2028 }
2029 cp += 3;
2030 cp = cp + strspn(cp, " \t");
2031 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2032 fatal("%s: revoke key ID failed", __func__);
2033 } else {
2034 if (strncasecmp(cp, "key:", 4) == 0) {
2035 cp += 4;
2036 cp = cp + strspn(cp, " \t");
2037 was_explicit_key = 1;
2038 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2039 cp += 5;
2040 cp = cp + strspn(cp, " \t");
2041 was_sha1 = 1;
2042 } else {
2043 /*
2044 * Just try to process the line as a key.
2045 * Parsing will fail if it isn't.
2046 */
2047 }
2048 if ((key = key_new(KEY_UNSPEC)) == NULL)
2049 fatal("key_new");
2050 if (key_read(key, &cp) != 1)
2051 fatal("%s:%lu: invalid key", path, lnum);
2052 if (was_explicit_key)
2053 r = ssh_krl_revoke_key_explicit(krl, key);
2054 else if (was_sha1)
2055 r = ssh_krl_revoke_key_sha1(krl, key);
2056 else
2057 r = ssh_krl_revoke_key(krl, key);
2058 if (r != 0)
2059 fatal("%s: revoke key failed", __func__);
2060 key_free(key);
2061 }
2062 }
2063 if (strcmp(path, "-") != 0)
2064 fclose(krl_spec);
Damien Miller0d6771b2013-04-23 15:23:24 +10002065 free(path);
Damien Millerf3747bf2013-01-18 11:44:04 +11002066}
2067
2068static void
2069do_gen_krl(struct passwd *pw, int updating, int argc, char **argv)
2070{
2071 struct ssh_krl *krl;
2072 struct stat sb;
2073 Key *ca = NULL;
2074 int fd, i;
2075 char *tmp;
2076 Buffer kbuf;
2077
2078 if (*identity_file == '\0')
2079 fatal("KRL generation requires an output file");
2080 if (stat(identity_file, &sb) == -1) {
2081 if (errno != ENOENT)
2082 fatal("Cannot access KRL \"%s\": %s",
2083 identity_file, strerror(errno));
2084 if (updating)
2085 fatal("KRL \"%s\" does not exist", identity_file);
2086 }
2087 if (ca_key_path != NULL) {
2088 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2089 if ((ca = key_load_public(tmp, NULL)) == NULL)
2090 fatal("Cannot load CA public key %s", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10002091 free(tmp);
Damien Millerf3747bf2013-01-18 11:44:04 +11002092 }
2093
2094 if (updating)
2095 load_krl(identity_file, &krl);
2096 else if ((krl = ssh_krl_init()) == NULL)
2097 fatal("couldn't create KRL");
2098
2099 if (cert_serial != 0)
2100 ssh_krl_set_version(krl, cert_serial);
2101 if (identity_comment != NULL)
2102 ssh_krl_set_comment(krl, identity_comment);
2103
2104 for (i = 0; i < argc; i++)
2105 update_krl_from_file(pw, argv[i], ca, krl);
2106
2107 buffer_init(&kbuf);
2108 if (ssh_krl_to_blob(krl, &kbuf, NULL, 0) != 0)
2109 fatal("Couldn't generate KRL");
2110 if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2111 fatal("open %s: %s", identity_file, strerror(errno));
2112 if (atomicio(vwrite, fd, buffer_ptr(&kbuf), buffer_len(&kbuf)) !=
2113 buffer_len(&kbuf))
2114 fatal("write %s: %s", identity_file, strerror(errno));
2115 close(fd);
2116 buffer_free(&kbuf);
2117 ssh_krl_free(krl);
Damien Miller0d6771b2013-04-23 15:23:24 +10002118 if (ca != NULL)
2119 key_free(ca);
Damien Millerf3747bf2013-01-18 11:44:04 +11002120}
2121
2122static void
2123do_check_krl(struct passwd *pw, int argc, char **argv)
2124{
2125 int i, r, ret = 0;
2126 char *comment;
2127 struct ssh_krl *krl;
2128 Key *k;
2129
2130 if (*identity_file == '\0')
2131 fatal("KRL checking requires an input file");
2132 load_krl(identity_file, &krl);
2133 for (i = 0; i < argc; i++) {
2134 if ((k = key_load_public(argv[i], &comment)) == NULL)
2135 fatal("Cannot load public key %s", argv[i]);
2136 r = ssh_krl_check_key(krl, k);
2137 printf("%s%s%s%s: %s\n", argv[i],
2138 *comment ? " (" : "", comment, *comment ? ")" : "",
2139 r == 0 ? "ok" : "REVOKED");
2140 if (r != 0)
2141 ret = 1;
2142 key_free(k);
2143 free(comment);
2144 }
2145 ssh_krl_free(krl);
2146 exit(ret);
2147}
2148
2149static void
Damien Miller431f66b1999-11-21 18:31:57 +11002150usage(void)
2151{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10002152 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00002153 fprintf(stderr, "Options:\n");
Damien Miller58f1baf2011-05-05 14:06:15 +10002154 fprintf(stderr, " -A Generate non-existent host keys for all key types.\n");
Damien Millerbcd00ab2013-12-07 10:41:55 +11002155 fprintf(stderr, " -a number Number of KDF rounds for new key format or moduli primality tests.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00002156 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002157 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00002158 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002159 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002160#ifdef ENABLE_PKCS11
2161 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
2162#endif
Damien Miller44b25042010-07-02 13:35:01 +10002163 fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002164 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
2165 fprintf(stderr, " -f filename Filename of the key file.\n");
2166 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
2167 fprintf(stderr, " -g Use generic DNS resource record format.\n");
2168 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11002169 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
2170 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Damien Miller44b25042010-07-02 13:35:01 +10002171 fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
Damien Miller709a1e92012-07-31 12:20:43 +10002172 fprintf(stderr, " -J number Screen this number of moduli lines.\n");
Damien Millerdfceafe2012-07-06 13:44:19 +10002173 fprintf(stderr, " -j number Start screening moduli at specified line.\n");
Damien Miller927d82b2011-10-18 16:05:38 +11002174 fprintf(stderr, " -K checkpt Write checkpoints to this file.\n");
Damien Millerf3747bf2013-01-18 11:44:04 +11002175 fprintf(stderr, " -k Generate a KRL file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11002176 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002177 fprintf(stderr, " -l Show fingerprint of key file.\n");
2178 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10002179 fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002180 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller6022f582010-07-02 13:37:01 +10002181 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller1f181422010-04-18 08:08:03 +10002182 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Millerbcd00ab2013-12-07 10:41:55 +11002183 fprintf(stderr, " -o Enforce new private key format.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002184 fprintf(stderr, " -P phrase Provide old passphrase.\n");
2185 fprintf(stderr, " -p Change passphrase of private key file.\n");
Damien Millerf3747bf2013-01-18 11:44:04 +11002186 fprintf(stderr, " -Q Test whether key(s) are revoked in KRL.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002187 fprintf(stderr, " -q Quiet.\n");
2188 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
2189 fprintf(stderr, " -r hostname Print DNS resource record.\n");
2190 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10002191 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002192 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
2193 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Millerf3747bf2013-01-18 11:44:04 +11002194 fprintf(stderr, " -u Update KRL rather than creating a new one.\n");
Damien Miller3d6d68b2013-01-20 22:33:23 +11002195 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002196 fprintf(stderr, " -v Verbose.\n");
2197 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
2198 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10002199 fprintf(stderr, " -z serial Specify a serial number.\n");
Damien Millerbcd00ab2013-12-07 10:41:55 +11002200 fprintf(stderr, " -Z cipher Specify a cipher for new private key format.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10002201
Damien Miller95def091999-11-25 00:26:21 +11002202 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11002203}
2204
Damien Miller95def091999-11-25 00:26:21 +11002205/*
2206 * Main program for key management.
2207 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002208int
Damien Millerdf8b7db2007-01-05 16:22:57 +11002209main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002210{
Damien Millera41c8b12002-01-22 23:05:08 +11002211 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller390d0562011-10-18 16:05:19 +11002212 char *checkpoint = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11002213 char out_file[MAXPATHLEN], *ep, *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002214 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11002215 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11002216 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11002217 int opt, type, fd;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002218 u_int32_t memory = 0, generator_wanted = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002219 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002220 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
Damien Millerdfceafe2012-07-06 13:44:19 +10002221 unsigned long start_lineno = 0, lines_to_process = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002222 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11002223 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10002224 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002225
Damien Miller95def091999-11-25 00:26:21 +11002226 extern int optind;
2227 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002228
Darren Tuckerce321d82005-10-03 18:11:24 +10002229 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2230 sanitise_stdfd();
2231
Darren Tucker9ac56e92007-01-14 10:19:59 +11002232 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10002233
Damien Miller4314c2b2010-09-10 11:12:09 +10002234 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11002235 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10002236
Kevin Steves3a881912002-07-20 19:05:40 +00002237 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10002238
Damien Miller5428f641999-11-25 11:54:57 +11002239 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11002240 pw = getpwuid(getuid());
2241 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +10002242 printf("No user exists for uid %lu\n", (u_long)getuid());
Damien Miller95def091999-11-25 00:26:21 +11002243 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002244 }
Damien Millereba71ba2000-04-29 23:57:08 +10002245 if (gethostname(hostname, sizeof(hostname)) < 0) {
2246 perror("gethostname");
2247 exit(1);
2248 }
Damien Miller5428f641999-11-25 11:54:57 +11002249
Damien Millerbcd00ab2013-12-07 10:41:55 +11002250 /* Remaining characters: EUYdw */
2251 while ((opt = getopt(argc, argv, "ABHLQXceghiklopquvxy"
2252 "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 +11002253 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10002254 case 'A':
2255 gen_all_hostkeys = 1;
2256 break;
Damien Miller95def091999-11-25 00:26:21 +11002257 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10002258 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10002259 if (errstr)
2260 fatal("Bits has bad value %s (%s)",
2261 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11002262 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002263 case 'F':
2264 find_host = 1;
2265 rr_hostname = optarg;
2266 break;
2267 case 'H':
2268 hash_hosts = 1;
2269 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002270 case 'I':
2271 cert_key_id = optarg;
2272 break;
Damien Millerdfceafe2012-07-06 13:44:19 +10002273 case 'J':
2274 lines_to_process = strtoul(optarg, NULL, 10);
2275 break;
2276 case 'j':
2277 start_lineno = strtoul(optarg, NULL, 10);
2278 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002279 case 'R':
2280 delete_host = 1;
2281 rr_hostname = optarg;
2282 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11002283 case 'L':
2284 show_cert = 1;
2285 break;
Damien Miller95def091999-11-25 00:26:21 +11002286 case 'l':
2287 print_fingerprint = 1;
2288 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002289 case 'B':
2290 print_bubblebabble = 1;
2291 break;
Damien Miller44b25042010-07-02 13:35:01 +10002292 case 'm':
2293 if (strcasecmp(optarg, "RFC4716") == 0 ||
2294 strcasecmp(optarg, "ssh2") == 0) {
2295 convert_format = FMT_RFC4716;
2296 break;
2297 }
2298 if (strcasecmp(optarg, "PKCS8") == 0) {
2299 convert_format = FMT_PKCS8;
2300 break;
2301 }
2302 if (strcasecmp(optarg, "PEM") == 0) {
2303 convert_format = FMT_PEM;
2304 break;
2305 }
2306 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002307 case 'n':
2308 cert_principals = optarg;
2309 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002310 case 'o':
2311 use_new_format = 1;
2312 break;
Damien Miller95def091999-11-25 00:26:21 +11002313 case 'p':
2314 change_passphrase = 1;
2315 break;
Damien Miller95def091999-11-25 00:26:21 +11002316 case 'c':
2317 change_comment = 1;
2318 break;
Damien Miller95def091999-11-25 00:26:21 +11002319 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002320 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2321 sizeof(identity_file))
2322 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002323 have_identity = 1;
2324 break;
Damien Miller37876e92003-05-15 10:19:46 +10002325 case 'g':
2326 print_generic = 1;
2327 break;
Damien Miller95def091999-11-25 00:26:21 +11002328 case 'P':
2329 identity_passphrase = optarg;
2330 break;
Damien Miller95def091999-11-25 00:26:21 +11002331 case 'N':
2332 identity_new_passphrase = optarg;
2333 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002334 case 'Q':
2335 check_krl = 1;
2336 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002337 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002338 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002339 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002340 case 'Z':
2341 new_format_cipher = optarg;
2342 break;
Damien Miller95def091999-11-25 00:26:21 +11002343 case 'C':
2344 identity_comment = optarg;
2345 break;
Damien Miller95def091999-11-25 00:26:21 +11002346 case 'q':
2347 quiet = 1;
2348 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002349 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002350 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002351 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002352 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002353 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002354 case 'h':
2355 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002356 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002357 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002358 case 'k':
2359 gen_krl = 1;
2360 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002361 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002362 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002363 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002364 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002365 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002366 case 'y':
2367 print_public = 1;
2368 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002369 case 's':
2370 ca_key_path = optarg;
2371 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002372 case 't':
2373 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002374 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002375 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002376 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002377 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002378 case 'u':
2379 update_krl = 1;
2380 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002381 case 'v':
2382 if (log_level == SYSLOG_LEVEL_INFO)
2383 log_level = SYSLOG_LEVEL_DEBUG1;
2384 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002385 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002386 log_level < SYSLOG_LEVEL_DEBUG3)
2387 log_level++;
2388 }
2389 break;
Damien Miller37876e92003-05-15 10:19:46 +10002390 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002391 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002392 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002393 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002394 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2395 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002396 if (errstr)
2397 fatal("Desired generator has bad value: %s (%s)",
2398 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002399 break;
2400 case 'a':
Damien Millerbcd00ab2013-12-07 10:41:55 +11002401 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002402 if (errstr)
Damien Millerbcd00ab2013-12-07 10:41:55 +11002403 fatal("Invalid number: %s (%s)",
Damien Millerb089fb52005-05-26 12:16:18 +10002404 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002405 break;
2406 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002407 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002408 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002409 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002410 break;
2411 case 'G':
2412 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002413 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2414 sizeof(out_file))
2415 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002416 break;
2417 case 'T':
2418 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002419 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2420 sizeof(out_file))
2421 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002422 break;
Damien Miller390d0562011-10-18 16:05:19 +11002423 case 'K':
2424 if (strlen(optarg) >= MAXPATHLEN)
2425 fatal("Checkpoint filename too long");
2426 checkpoint = xstrdup(optarg);
2427 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002428 case 'S':
2429 /* XXX - also compare length against bits */
2430 if (BN_hex2bn(&start, optarg) == 0)
2431 fatal("Invalid start point.");
2432 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002433 case 'V':
2434 parse_cert_times(optarg);
2435 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002436 case 'z':
Damien Miller6f3b3622012-11-14 19:04:33 +11002437 errno = 0;
2438 cert_serial = strtoull(optarg, &ep, 10);
2439 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2440 (errno == ERANGE && cert_serial == ULLONG_MAX))
2441 fatal("Invalid serial number \"%s\"", optarg);
Damien Miller4e270b02010-04-16 15:56:21 +10002442 break;
Damien Miller95def091999-11-25 00:26:21 +11002443 case '?':
2444 default:
2445 usage();
2446 }
2447 }
Darren Tucker06930c72003-12-31 11:34:51 +11002448
2449 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002450 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002451
Damien Miller0a80ca12010-02-27 07:55:05 +11002452 argv += optind;
2453 argc -= optind;
2454
2455 if (ca_key_path != NULL) {
Damien Millerf3747bf2013-01-18 11:44:04 +11002456 if (argc < 1 && !gen_krl) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002457 printf("Too few arguments.\n");
2458 usage();
2459 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002460 } else if (argc > 0 && !gen_krl && !check_krl) {
Damien Miller95def091999-11-25 00:26:21 +11002461 printf("Too many arguments.\n");
2462 usage();
2463 }
2464 if (change_passphrase && change_comment) {
2465 printf("Can only have one of -p and -c.\n");
2466 usage();
2467 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002468 if (print_fingerprint && (delete_host || hash_hosts)) {
Damien Millerec77c952013-01-09 15:58:00 +11002469 printf("Cannot use -l with -H or -R.\n");
Darren Tucker0f7e9102008-06-08 12:54:29 +10002470 usage();
2471 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002472 if (gen_krl) {
2473 do_gen_krl(pw, update_krl, argc, argv);
2474 return (0);
2475 }
2476 if (check_krl) {
2477 do_check_krl(pw, argc, argv);
2478 return (0);
2479 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002480 if (ca_key_path != NULL) {
2481 if (cert_key_id == NULL)
2482 fatal("Must specify key id (-I) when certifying");
2483 do_ca_sign(pw, argc, argv);
2484 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002485 if (show_cert)
2486 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002487 if (delete_host || hash_hosts || find_host)
2488 do_known_hosts(pw, rr_hostname);
Damien Millerec77c952013-01-09 15:58:00 +11002489 if (pkcs11provider != NULL)
2490 do_download(pw);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002491 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002492 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002493 if (change_passphrase)
2494 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002495 if (change_comment)
2496 do_change_comment(pw);
Damien Miller44b25042010-07-02 13:35:01 +10002497 if (convert_to)
2498 do_convert_to(pw);
2499 if (convert_from)
2500 do_convert_from(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10002501 if (print_public)
2502 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002503 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002504 unsigned int n = 0;
2505
2506 if (have_identity) {
2507 n = do_print_resource_record(pw,
2508 identity_file, rr_hostname);
2509 if (n == 0) {
2510 perror(identity_file);
2511 exit(1);
2512 }
2513 exit(0);
2514 } else {
2515
2516 n += do_print_resource_record(pw,
2517 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2518 n += do_print_resource_record(pw,
2519 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
Damien Miller3bde12a2012-06-20 21:51:11 +10002520 n += do_print_resource_record(pw,
2521 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
Damien Millercb314822006-03-26 13:48:01 +11002522
2523 if (n == 0)
2524 fatal("no keys found.");
2525 exit(0);
2526 }
Damien Miller37876e92003-05-15 10:19:46 +10002527 }
Damien Miller95def091999-11-25 00:26:21 +11002528
Darren Tucker019cefe2003-08-02 22:40:07 +10002529 if (do_gen_candidates) {
2530 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002531
Darren Tucker019cefe2003-08-02 22:40:07 +10002532 if (out == NULL) {
2533 error("Couldn't open modulus candidate file \"%s\": %s",
2534 out_file, strerror(errno));
2535 return (1);
2536 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002537 if (bits == 0)
2538 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002539 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002540 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002541
2542 return (0);
2543 }
2544
2545 if (do_screen_candidates) {
2546 FILE *in;
Damien Miller78d22712013-02-12 11:03:36 +11002547 FILE *out = fopen(out_file, "a");
Darren Tucker019cefe2003-08-02 22:40:07 +10002548
2549 if (have_identity && strcmp(identity_file, "-") != 0) {
2550 if ((in = fopen(identity_file, "r")) == NULL) {
2551 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002552 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002553 strerror(errno));
2554 }
2555 } else
2556 in = stdin;
2557
2558 if (out == NULL) {
2559 fatal("Couldn't open moduli file \"%s\": %s",
2560 out_file, strerror(errno));
2561 }
Damien Millerbcd00ab2013-12-07 10:41:55 +11002562 if (prime_test(in, out, rounds == 0 ? 100 : rounds,
2563 generator_wanted, checkpoint,
Damien Millerdfceafe2012-07-06 13:44:19 +10002564 start_lineno, lines_to_process) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002565 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002566 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002567 }
2568
Damien Miller58f1baf2011-05-05 14:06:15 +10002569 if (gen_all_hostkeys) {
2570 do_gen_all_hostkeys(pw);
2571 return (0);
2572 }
2573
Damien Millerf14be5c2005-11-05 15:15:49 +11002574 if (key_type_name == NULL)
2575 key_type_name = "rsa";
2576
Damien Millere39cacc2000-11-29 12:18:44 +11002577 type = key_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002578 type_bits_valid(type, &bits);
2579
Darren Tucker3af2ac52005-11-29 13:10:24 +11002580 if (!quiet)
2581 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002582 private = key_generate(type, bits);
2583 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002584 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002585 exit(1);
2586 }
2587 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002588
2589 if (!have_identity)
2590 ask_filename(pw, "Enter file in which to save the key");
2591
Damien Miller788f2122005-11-05 15:14:59 +11002592 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002593 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2594 pw->pw_dir, _PATH_SSH_USER_DIR);
2595 if (strstr(identity_file, dotsshdir) != NULL) {
2596 if (stat(dotsshdir, &st) < 0) {
2597 if (errno != ENOENT) {
2598 error("Could not stat %s: %s", dotsshdir,
2599 strerror(errno));
2600 } else if (mkdir(dotsshdir, 0700) < 0) {
2601 error("Could not create directory '%s': %s",
2602 dotsshdir, strerror(errno));
2603 } else if (!quiet)
2604 printf("Created directory '%s'.\n", dotsshdir);
2605 }
Damien Miller95def091999-11-25 00:26:21 +11002606 }
2607 /* If the file already exists, ask the user to confirm. */
2608 if (stat(identity_file, &st) >= 0) {
2609 char yesno[3];
2610 printf("%s already exists.\n", identity_file);
2611 printf("Overwrite (y/n)? ");
2612 fflush(stdout);
2613 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2614 exit(1);
2615 if (yesno[0] != 'y' && yesno[0] != 'Y')
2616 exit(1);
2617 }
2618 /* Ask for a passphrase (twice). */
2619 if (identity_passphrase)
2620 passphrase1 = xstrdup(identity_passphrase);
2621 else if (identity_new_passphrase)
2622 passphrase1 = xstrdup(identity_new_passphrase);
2623 else {
2624passphrase_again:
2625 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002626 read_passphrase("Enter passphrase (empty for no "
2627 "passphrase): ", RP_ALLOW_STDIN);
2628 passphrase2 = read_passphrase("Enter same passphrase again: ",
2629 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002630 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002631 /*
2632 * The passphrases do not match. Clear them and
2633 * retry.
2634 */
Damien Miller95def091999-11-25 00:26:21 +11002635 memset(passphrase1, 0, strlen(passphrase1));
2636 memset(passphrase2, 0, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002637 free(passphrase1);
2638 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002639 printf("Passphrases do not match. Try again.\n");
2640 goto passphrase_again;
2641 }
2642 /* Clear the other copy of the passphrase. */
2643 memset(passphrase2, 0, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002644 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002645 }
2646
Damien Miller95def091999-11-25 00:26:21 +11002647 if (identity_comment) {
2648 strlcpy(comment, identity_comment, sizeof(comment));
2649 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002650 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002651 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2652 }
2653
2654 /* Save the key with the given passphrase and comment. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11002655 if (!key_save_private(private, identity_file, passphrase1, comment,
2656 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002657 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002658 memset(passphrase1, 0, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002659 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002660 exit(1);
2661 }
2662 /* Clear the passphrase. */
2663 memset(passphrase1, 0, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002664 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002665
2666 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002667 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002668
2669 if (!quiet)
2670 printf("Your identification has been saved in %s.\n", identity_file);
2671
Damien Miller95def091999-11-25 00:26:21 +11002672 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002673 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2674 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002675 printf("Could not save your public key in %s\n", identity_file);
2676 exit(1);
2677 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002678 f = fdopen(fd, "w");
2679 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002680 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002681 exit(1);
2682 }
Damien Millereba71ba2000-04-29 23:57:08 +10002683 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002684 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002685 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002686 fclose(f);
2687
2688 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002689 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002690 char *ra = key_fingerprint(public, SSH_FP_MD5,
2691 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002692 printf("Your public key has been saved in %s.\n",
2693 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002694 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002695 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002696 printf("The key's randomart image is:\n");
2697 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10002698 free(ra);
2699 free(fp);
Damien Miller95def091999-11-25 00:26:21 +11002700 }
Damien Millereba71ba2000-04-29 23:57:08 +10002701
2702 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002703 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002704}