blob: 533eed29122b9cb31e2463758a2c8cdf9526f4fe [file] [log] [blame]
Damien Millerbcd00ab2013-12-07 10:41:55 +11001/* $OpenBSD: ssh-keygen.c,v 1.237 2013/12/06 13:34:54 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 Miller884b63a2011-05-05 14:14:52 +1000200 else if (type != KEY_ECDSA && *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;
236 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100237 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100238 exit(1);
239 break;
240 }
Damien Miller90967402006-03-26 14:07:26 +1100241 }
Damien Millere39cacc2000-11-29 12:18:44 +1100242 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000243 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100244 if (fgets(buf, sizeof(buf), stdin) == NULL)
245 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000246 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100247 if (strcmp(buf, "") != 0)
248 strlcpy(identity_file, buf, sizeof(identity_file));
249 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100250}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000253load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000254{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000255 char *pass;
256 Key *prv;
257
Ben Lindstroma3700052001-04-05 23:26:32 +0000258 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000259 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000260 if (identity_passphrase)
261 pass = xstrdup(identity_passphrase);
262 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000263 pass = read_passphrase("Enter passphrase: ",
264 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000265 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000266 memset(pass, 0, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +1000267 free(pass);
Damien Millereba71ba2000-04-29 23:57:08 +1000268 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000269 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000270}
271
Damien Miller874d77b2000-10-14 16:23:11 +1100272#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000273#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100274#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000275#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000276
Ben Lindstrombba81212001-06-25 05:01:22 +0000277static void
Damien Miller44b25042010-07-02 13:35:01 +1000278do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000279{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000280 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000281 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100282 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000283
Damien Millera563cce2012-04-22 11:07:28 +1000284 if (k->type == KEY_RSA1) {
285 fprintf(stderr, "version 1 keys are not supported\n");
286 exit(1);
287 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000288 if (key_to_blob(k, &blob, &len) <= 0) {
289 fprintf(stderr, "key_to_blob failed\n");
290 exit(1);
291 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100292 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
293 snprintf(comment, sizeof(comment),
294 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000295 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000296 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100297
298 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
299 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000300 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100301 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000302 key_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000303 free(blob);
Damien Millereba71ba2000-04-29 23:57:08 +1000304 exit(0);
305}
306
Ben Lindstrombba81212001-06-25 05:01:22 +0000307static void
Damien Miller44b25042010-07-02 13:35:01 +1000308do_convert_to_pkcs8(Key *k)
309{
310 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000311 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000312 case KEY_RSA:
313 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
314 fatal("PEM_write_RSA_PUBKEY failed");
315 break;
316 case KEY_DSA:
317 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
318 fatal("PEM_write_DSA_PUBKEY failed");
319 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000320#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000321 case KEY_ECDSA:
322 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
323 fatal("PEM_write_EC_PUBKEY failed");
324 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000325#endif
Damien Miller44b25042010-07-02 13:35:01 +1000326 default:
327 fatal("%s: unsupported key type %s", __func__, key_type(k));
328 }
329 exit(0);
330}
331
332static void
333do_convert_to_pem(Key *k)
334{
335 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000336 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000337 case KEY_RSA:
338 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
339 fatal("PEM_write_RSAPublicKey failed");
340 break;
341#if notyet /* OpenSSH 0.9.8 lacks this function */
342 case KEY_DSA:
343 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
344 fatal("PEM_write_DSAPublicKey failed");
345 break;
346#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000347 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000348 default:
349 fatal("%s: unsupported key type %s", __func__, key_type(k));
350 }
351 exit(0);
352}
353
354static void
355do_convert_to(struct passwd *pw)
356{
357 Key *k;
358 struct stat st;
359
360 if (!have_identity)
361 ask_filename(pw, "Enter file in which the key is");
362 if (stat(identity_file, &st) < 0)
363 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
364 if ((k = key_load_public(identity_file, NULL)) == NULL) {
365 if ((k = load_identity(identity_file)) == NULL) {
366 fprintf(stderr, "load failed\n");
367 exit(1);
368 }
369 }
Damien Miller44b25042010-07-02 13:35:01 +1000370
371 switch (convert_format) {
372 case FMT_RFC4716:
373 do_convert_to_ssh2(pw, k);
374 break;
375 case FMT_PKCS8:
376 do_convert_to_pkcs8(k);
377 break;
378 case FMT_PEM:
379 do_convert_to_pem(k);
380 break;
381 default:
382 fatal("%s: unknown key format %d", __func__, convert_format);
383 }
384 exit(0);
385}
386
387static void
Damien Miller874d77b2000-10-14 16:23:11 +1100388buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
389{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000390 u_int bignum_bits = buffer_get_int(b);
391 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000392
Damien Miller874d77b2000-10-14 16:23:11 +1100393 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000394 fatal("buffer_get_bignum_bits: input buffer too small: "
395 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100396 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
397 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100398 buffer_consume(b, bytes);
399}
400
Ben Lindstrombba81212001-06-25 05:01:22 +0000401static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000402do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100403{
404 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100405 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100406 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000407 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000408 int magic, rlen, ktype, i1, i2, i3, i4;
409 u_int slen;
410 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100411
412 buffer_init(&b);
413 buffer_append(&b, blob, blen);
414
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100415 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100416 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
417 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
418 buffer_free(&b);
419 return NULL;
420 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000421 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100422 type = buffer_get_string(&b, NULL);
423 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000424 i2 = buffer_get_int(&b);
425 i3 = buffer_get_int(&b);
426 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100427 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100428 if (strcmp(cipher, "none") != 0) {
429 error("unsupported cipher %s", cipher);
Darren Tuckera627d422013-06-02 07:31:17 +1000430 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100431 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000432 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100433 return NULL;
434 }
Darren Tuckera627d422013-06-02 07:31:17 +1000435 free(cipher);
Damien Miller874d77b2000-10-14 16:23:11 +1100436
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000437 if (strstr(type, "dsa")) {
438 ktype = KEY_DSA;
439 } else if (strstr(type, "rsa")) {
440 ktype = KEY_RSA;
441 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100442 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000443 free(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100444 return NULL;
445 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000446 key = key_new_private(ktype);
Darren Tuckera627d422013-06-02 07:31:17 +1000447 free(type);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000448
449 switch (key->type) {
450 case KEY_DSA:
451 buffer_get_bignum_bits(&b, key->dsa->p);
452 buffer_get_bignum_bits(&b, key->dsa->g);
453 buffer_get_bignum_bits(&b, key->dsa->q);
454 buffer_get_bignum_bits(&b, key->dsa->pub_key);
455 buffer_get_bignum_bits(&b, key->dsa->priv_key);
456 break;
457 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100458 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000459 debug("e %lx", e);
460 if (e < 30) {
461 e <<= 8;
462 e += buffer_get_char(&b);
463 debug("e %lx", e);
464 e <<= 8;
465 e += buffer_get_char(&b);
466 debug("e %lx", e);
467 }
468 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000469 buffer_free(&b);
470 key_free(key);
471 return NULL;
472 }
473 buffer_get_bignum_bits(&b, key->rsa->d);
474 buffer_get_bignum_bits(&b, key->rsa->n);
475 buffer_get_bignum_bits(&b, key->rsa->iqmp);
476 buffer_get_bignum_bits(&b, key->rsa->q);
477 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000478 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000479 break;
480 }
Damien Miller874d77b2000-10-14 16:23:11 +1100481 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000482 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000483 error("do_convert_private_ssh2_from_blob: "
484 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100485 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000486
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000487 /* try the key */
488 key_sign(key, &sig, &slen, data, sizeof(data));
489 key_verify(key, sig, slen, data, sizeof(data));
Darren Tuckera627d422013-06-02 07:31:17 +1000490 free(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100491 return key;
492}
493
Damien Miller8056a9d2006-03-15 12:05:40 +1100494static int
495get_line(FILE *fp, char *line, size_t len)
496{
497 int c;
498 size_t pos = 0;
499
500 line[0] = '\0';
501 while ((c = fgetc(fp)) != EOF) {
502 if (pos >= len - 1) {
503 fprintf(stderr, "input line too long.\n");
504 exit(1);
505 }
Damien Miller90967402006-03-26 14:07:26 +1100506 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100507 case '\r':
508 c = fgetc(fp);
509 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
510 fprintf(stderr, "unget: %s\n", strerror(errno));
511 exit(1);
512 }
513 return pos;
514 case '\n':
515 return pos;
516 }
517 line[pos++] = c;
518 line[pos] = '\0';
519 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100520 /* We reached EOF */
521 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100522}
523
Ben Lindstrombba81212001-06-25 05:01:22 +0000524static void
Damien Miller44b25042010-07-02 13:35:01 +1000525do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000526{
Damien Millereba71ba2000-04-29 23:57:08 +1000527 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000528 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100529 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000530 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000531 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000532 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000533 FILE *fp;
534
Damien Millerba3420a2010-06-26 09:39:07 +1000535 if ((fp = fopen(identity_file, "r")) == NULL)
536 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000537 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100538 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
Damien Miller746d1a62013-07-18 16:13:02 +1000539 if (blen > 0 && line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000540 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000541 if (strncmp(line, "----", 4) == 0 ||
542 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100543 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000544 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000545 if (strstr(line, " END ") != NULL) {
546 break;
547 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000548 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000549 continue;
550 }
Damien Miller30c3d422000-05-09 11:02:59 +1000551 if (escaped) {
552 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000553 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000554 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000555 }
Damien Millereba71ba2000-04-29 23:57:08 +1000556 strlcat(encoded, line, sizeof(encoded));
557 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000558 len = strlen(encoded);
559 if (((len % 4) == 3) &&
560 (encoded[len-1] == '=') &&
561 (encoded[len-2] == '=') &&
562 (encoded[len-3] == '='))
563 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100564 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000565 if (blen < 0) {
566 fprintf(stderr, "uudecode failed.\n");
567 exit(1);
568 }
Damien Miller44b25042010-07-02 13:35:01 +1000569 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100570 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100571 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000572 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100573 fprintf(stderr, "decode blob failed.\n");
574 exit(1);
575 }
Damien Miller44b25042010-07-02 13:35:01 +1000576 fclose(fp);
577}
578
579static void
580do_convert_from_pkcs8(Key **k, int *private)
581{
582 EVP_PKEY *pubkey;
583 FILE *fp;
584
585 if ((fp = fopen(identity_file, "r")) == NULL)
586 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
587 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
588 fatal("%s: %s is not a recognised public key format", __func__,
589 identity_file);
590 }
591 fclose(fp);
592 switch (EVP_PKEY_type(pubkey->type)) {
593 case EVP_PKEY_RSA:
594 *k = key_new(KEY_UNSPEC);
595 (*k)->type = KEY_RSA;
596 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
597 break;
598 case EVP_PKEY_DSA:
599 *k = key_new(KEY_UNSPEC);
600 (*k)->type = KEY_DSA;
601 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
602 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000603#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000604 case EVP_PKEY_EC:
605 *k = key_new(KEY_UNSPEC);
606 (*k)->type = KEY_ECDSA;
607 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
Damien Millerb472a902010-11-05 10:19:49 +1100608 (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000609 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000610#endif
Damien Miller44b25042010-07-02 13:35:01 +1000611 default:
612 fatal("%s: unsupported pubkey type %d", __func__,
613 EVP_PKEY_type(pubkey->type));
614 }
615 EVP_PKEY_free(pubkey);
616 return;
617}
618
619static void
620do_convert_from_pem(Key **k, int *private)
621{
622 FILE *fp;
623 RSA *rsa;
624#ifdef notyet
625 DSA *dsa;
626#endif
627
628 if ((fp = fopen(identity_file, "r")) == NULL)
629 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
630 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
631 *k = key_new(KEY_UNSPEC);
632 (*k)->type = KEY_RSA;
633 (*k)->rsa = rsa;
634 fclose(fp);
635 return;
636 }
637#if notyet /* OpenSSH 0.9.8 lacks this function */
638 rewind(fp);
639 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
640 *k = key_new(KEY_UNSPEC);
641 (*k)->type = KEY_DSA;
642 (*k)->dsa = dsa;
643 fclose(fp);
644 return;
645 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000646 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000647#endif
648 fatal("%s: unrecognised raw private key format", __func__);
649}
650
651static void
652do_convert_from(struct passwd *pw)
653{
654 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000655 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000656 struct stat st;
657
658 if (!have_identity)
659 ask_filename(pw, "Enter file in which the key is");
660 if (stat(identity_file, &st) < 0)
661 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
662
663 switch (convert_format) {
664 case FMT_RFC4716:
665 do_convert_from_ssh2(pw, &k, &private);
666 break;
667 case FMT_PKCS8:
668 do_convert_from_pkcs8(&k, &private);
669 break;
670 case FMT_PEM:
671 do_convert_from_pem(&k, &private);
672 break;
673 default:
674 fatal("%s: unknown key format %d", __func__, convert_format);
675 }
676
677 if (!private)
678 ok = key_write(k, stdout);
679 if (ok)
680 fprintf(stdout, "\n");
681 else {
682 switch (k->type) {
683 case KEY_DSA:
684 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
685 NULL, 0, NULL, NULL);
686 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000687#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000688 case KEY_ECDSA:
689 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
690 NULL, 0, NULL, NULL);
691 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000692#endif
Damien Miller44b25042010-07-02 13:35:01 +1000693 case KEY_RSA:
694 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
695 NULL, 0, NULL, NULL);
696 break;
697 default:
698 fatal("%s: unsupported key type %s", __func__,
699 key_type(k));
700 }
701 }
702
Damien Miller874d77b2000-10-14 16:23:11 +1100703 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100704 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100705 exit(1);
706 }
Damien Millereba71ba2000-04-29 23:57:08 +1000707 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000708 exit(0);
709}
710
Ben Lindstrombba81212001-06-25 05:01:22 +0000711static void
Damien Millereba71ba2000-04-29 23:57:08 +1000712do_print_public(struct passwd *pw)
713{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000714 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000715 struct stat st;
716
717 if (!have_identity)
718 ask_filename(pw, "Enter file in which the key is");
719 if (stat(identity_file, &st) < 0) {
720 perror(identity_file);
721 exit(1);
722 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000723 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000724 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000725 fprintf(stderr, "load failed\n");
726 exit(1);
727 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000728 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000729 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000730 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000731 fprintf(stdout, "\n");
732 exit(0);
733}
734
Ben Lindstromcd392282001-07-04 03:44:03 +0000735static void
Damien Miller757f34e2010-08-05 13:05:31 +1000736do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000737{
Damien Miller7ea845e2010-02-12 09:21:02 +1100738#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000739 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100740 int i, nkeys;
Damien Millerec77c952013-01-09 15:58:00 +1100741 enum fp_rep rep;
742 enum fp_type fptype;
743 char *fp, *ra;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000744
Damien Miller1422c082013-01-09 16:44:54 +1100745 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
746 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
747
Damien Miller7ea845e2010-02-12 09:21:02 +1100748 pkcs11_init(0);
749 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
750 if (nkeys <= 0)
751 fatal("cannot read public key from pkcs11");
752 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100753 if (print_fingerprint) {
754 fp = key_fingerprint(keys[i], fptype, rep);
755 ra = key_fingerprint(keys[i], SSH_FP_MD5,
756 SSH_FP_RANDOMART);
757 printf("%u %s %s (PKCS11 key)\n", key_size(keys[i]),
758 fp, key_type(keys[i]));
759 if (log_level >= SYSLOG_LEVEL_VERBOSE)
760 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000761 free(ra);
762 free(fp);
Damien Millerec77c952013-01-09 15:58:00 +1100763 } else {
764 key_write(keys[i], stdout);
765 fprintf(stdout, "\n");
766 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000767 key_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000768 }
Darren Tuckera627d422013-06-02 07:31:17 +1000769 free(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100770 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000771 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100772#else
773 fatal("no pkcs11 support");
774#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000775}
Ben Lindstromcd392282001-07-04 03:44:03 +0000776
Ben Lindstrombba81212001-06-25 05:01:22 +0000777static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100778do_fingerprint(struct passwd *pw)
779{
Damien Miller98c7ad62000-03-09 21:27:49 +1100780 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000781 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000782 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100783 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000784 enum fp_rep rep;
785 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100786 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100787
Ben Lindstromd0fca422001-03-26 13:44:06 +0000788 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
789 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000790
Damien Miller95def091999-11-25 00:26:21 +1100791 if (!have_identity)
792 ask_filename(pw, "Enter file in which the key is");
793 if (stat(identity_file, &st) < 0) {
794 perror(identity_file);
795 exit(1);
796 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000797 public = key_load_public(identity_file, &comment);
798 if (public != NULL) {
799 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100800 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000801 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
802 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000803 if (log_level >= SYSLOG_LEVEL_VERBOSE)
804 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100805 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +1000806 free(comment);
807 free(ra);
808 free(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100809 exit(0);
810 }
Damien Miller40b59852006-06-13 13:00:25 +1000811 if (comment) {
Darren Tuckera627d422013-06-02 07:31:17 +1000812 free(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000813 comment = NULL;
814 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100815
Damien Millerba3420a2010-06-26 09:39:07 +1000816 if ((f = fopen(identity_file, "r")) == NULL)
817 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100818
Damien Millerba3420a2010-06-26 09:39:07 +1000819 while (fgets(line, sizeof(line), f)) {
820 if ((cp = strchr(line, '\n')) == NULL) {
821 error("line %d too long: %.40s...",
822 num + 1, line);
823 skip = 1;
824 continue;
Damien Miller95def091999-11-25 00:26:21 +1100825 }
Damien Millerba3420a2010-06-26 09:39:07 +1000826 num++;
827 if (skip) {
828 skip = 0;
829 continue;
830 }
831 *cp = '\0';
832
833 /* Skip leading whitespace, empty and comment lines. */
834 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
835 ;
836 if (!*cp || *cp == '\n' || *cp == '#')
837 continue;
838 i = strtol(cp, &ep, 10);
839 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
840 int quoted = 0;
841 comment = cp;
842 for (; *cp && (quoted || (*cp != ' ' &&
843 *cp != '\t')); cp++) {
844 if (*cp == '\\' && cp[1] == '"')
845 cp++; /* Skip both */
846 else if (*cp == '"')
847 quoted = !quoted;
848 }
849 if (!*cp)
850 continue;
851 *cp++ = '\0';
852 }
853 ep = cp;
854 public = key_new(KEY_RSA1);
855 if (key_read(public, &cp) != 1) {
856 cp = ep;
857 key_free(public);
858 public = key_new(KEY_UNSPEC);
859 if (key_read(public, &cp) != 1) {
860 key_free(public);
861 continue;
862 }
863 }
864 comment = *cp ? cp : comment;
865 fp = key_fingerprint(public, fptype, rep);
866 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
867 printf("%u %s %s (%s)\n", key_size(public), fp,
868 comment ? comment : "no comment", key_type(public));
869 if (log_level >= SYSLOG_LEVEL_VERBOSE)
870 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000871 free(ra);
872 free(fp);
Damien Millerba3420a2010-06-26 09:39:07 +1000873 key_free(public);
874 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100875 }
Damien Millerba3420a2010-06-26 09:39:07 +1000876 fclose(f);
877
Damien Miller98c7ad62000-03-09 21:27:49 +1100878 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100879 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100880 exit(1);
881 }
Damien Miller95def091999-11-25 00:26:21 +1100882 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100883}
884
Damien Miller4b42d7f2005-03-01 21:48:35 +1100885static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000886do_gen_all_hostkeys(struct passwd *pw)
887{
888 struct {
889 char *key_type;
890 char *key_type_display;
891 char *path;
892 } key_types[] = {
893 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
894 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
895 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100896#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000897 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100898#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000899 { NULL, NULL, NULL }
900 };
901
902 int first = 0;
903 struct stat st;
904 Key *private, *public;
905 char comment[1024];
906 int i, type, fd;
907 FILE *f;
908
909 for (i = 0; key_types[i].key_type; i++) {
910 if (stat(key_types[i].path, &st) == 0)
911 continue;
912 if (errno != ENOENT) {
913 printf("Could not stat %s: %s", key_types[i].path,
914 strerror(errno));
915 first = 0;
916 continue;
917 }
918
919 if (first == 0) {
920 first = 1;
921 printf("%s: generating new host keys: ", __progname);
922 }
923 printf("%s ", key_types[i].key_type_display);
924 fflush(stdout);
Damien Miller58f1baf2011-05-05 14:06:15 +1000925 type = key_type_from_name(key_types[i].key_type);
926 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
927 bits = 0;
928 type_bits_valid(type, &bits);
929 private = key_generate(type, bits);
930 if (private == NULL) {
931 fprintf(stderr, "key_generate failed\n");
932 first = 0;
933 continue;
934 }
935 public = key_from_private(private);
936 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
937 hostname);
Damien Millerbcd00ab2013-12-07 10:41:55 +1100938 if (!key_save_private(private, identity_file, "", comment,
939 use_new_format, new_format_cipher, rounds)) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000940 printf("Saving the key failed: %s.\n", identity_file);
941 key_free(private);
942 key_free(public);
943 first = 0;
944 continue;
945 }
946 key_free(private);
Damien Miller58f1baf2011-05-05 14:06:15 +1000947 strlcat(identity_file, ".pub", sizeof(identity_file));
948 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
949 if (fd == -1) {
950 printf("Could not save your public key in %s\n",
951 identity_file);
952 key_free(public);
953 first = 0;
954 continue;
955 }
956 f = fdopen(fd, "w");
957 if (f == NULL) {
958 printf("fdopen %s failed\n", identity_file);
959 key_free(public);
960 first = 0;
961 continue;
962 }
963 if (!key_write(public, f)) {
964 fprintf(stderr, "write key failed\n");
965 key_free(public);
966 first = 0;
967 continue;
968 }
969 fprintf(f, " %s\n", comment);
970 fclose(f);
971 key_free(public);
972
973 }
974 if (first != 0)
975 printf("\n");
976}
977
978static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100979printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100980{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000981 if (print_fingerprint) {
982 enum fp_rep rep;
983 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000984 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000985
986 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
987 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
988 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100989 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000990 printf("%u %s %s (%s)\n", key_size(public), fp, name,
991 key_type(public));
992 if (log_level >= SYSLOG_LEVEL_VERBOSE)
993 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000994 free(ra);
995 free(fp);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000996 } else {
997 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
998 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100999 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +10001000 if (!key_write(public, f))
1001 fatal("key_write failed");
1002 fprintf(f, "\n");
1003 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001004}
1005
1006static void
1007do_known_hosts(struct passwd *pw, const char *name)
1008{
1009 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +11001010 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001011 char *cp, *cp2, *kp, *kp2;
1012 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +11001013 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001014 int ca;
Damien Miller66085482013-09-14 09:45:03 +10001015 int found_key = 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001016
1017 if (!have_identity) {
1018 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1019 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1020 sizeof(identity_file))
1021 fatal("Specified known hosts path too long");
Darren Tuckera627d422013-06-02 07:31:17 +10001022 free(cp);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001023 have_identity = 1;
1024 }
1025 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +10001026 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001027
1028 /*
1029 * Find hosts goes to stdout, hash and deletions happen in-place
1030 * A corner case is ssh-keygen -HF foo, which should go to stdout
1031 */
1032 if (!find_host && (hash_hosts || delete_host)) {
1033 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1034 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1035 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1036 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1037 fatal("known_hosts path too long");
1038 umask(077);
1039 if ((c = mkstemp(tmp)) == -1)
1040 fatal("mkstemp: %s", strerror(errno));
1041 if ((out = fdopen(c, "w")) == NULL) {
1042 c = errno;
1043 unlink(tmp);
1044 fatal("fdopen: %s", strerror(c));
1045 }
1046 inplace = 1;
1047 }
1048
1049 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001050 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001051 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001052 skip = 1;
1053 invalid = 1;
1054 continue;
1055 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001056 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001057 if (skip) {
1058 skip = 0;
1059 continue;
1060 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001061 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001062
1063 /* Skip leading whitespace, empty and comment lines. */
1064 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1065 ;
1066 if (!*cp || *cp == '\n' || *cp == '#') {
1067 if (inplace)
1068 fprintf(out, "%s\n", cp);
1069 continue;
1070 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001071 /* Check whether this is a CA key */
1072 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1073 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1074 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1075 ca = 1;
1076 cp += sizeof(CA_MARKER);
1077 } else
1078 ca = 0;
1079
Damien Miller4b42d7f2005-03-01 21:48:35 +11001080 /* Find the end of the host name portion. */
1081 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1082 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001083
Damien Miller4b42d7f2005-03-01 21:48:35 +11001084 if (*kp == '\0' || *(kp + 1) == '\0') {
1085 error("line %d missing key: %.40s...",
1086 num, line);
1087 invalid = 1;
1088 continue;
1089 }
1090 *kp++ = '\0';
1091 kp2 = kp;
1092
Damien Miller0a80ca12010-02-27 07:55:05 +11001093 pub = key_new(KEY_RSA1);
1094 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001095 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +11001096 key_free(pub);
1097 pub = key_new(KEY_UNSPEC);
1098 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001099 error("line %d invalid key: %.40s...",
1100 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001101 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001102 invalid = 1;
1103 continue;
1104 }
1105 }
1106
1107 if (*cp == HASH_DELIM) {
1108 if (find_host || delete_host) {
1109 cp2 = host_hash(name, cp, strlen(cp));
1110 if (cp2 == NULL) {
1111 error("line %d: invalid hashed "
1112 "name: %.64s...", num, line);
1113 invalid = 1;
1114 continue;
1115 }
1116 c = (strcmp(cp2, cp) == 0);
1117 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001118 if (!quiet)
1119 printf("# Host %s found: "
1120 "line %d type %s%s\n", name,
1121 num, key_type(pub),
1122 ca ? " (CA key)" : "");
Damien Miller0a80ca12010-02-27 07:55:05 +11001123 printhost(out, cp, pub, ca, 0);
Damien Miller66085482013-09-14 09:45:03 +10001124 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001125 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001126 if (delete_host) {
1127 if (!c && !ca)
1128 printhost(out, cp, pub, ca, 0);
1129 else
1130 printf("# Host %s found: "
1131 "line %d type %s\n", name,
1132 num, key_type(pub));
1133 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001134 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +11001135 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001136 } else {
1137 if (find_host || delete_host) {
1138 c = (match_hostname(name, cp,
1139 strlen(cp)) == 1);
1140 if (find_host && c) {
Damien Miller66085482013-09-14 09:45:03 +10001141 if (!quiet)
1142 printf("# Host %s found: "
1143 "line %d type %s%s\n", name,
1144 num, key_type(pub),
1145 ca ? " (CA key)" : "");
Damien Miller0a80ca12010-02-27 07:55:05 +11001146 printhost(out, name, pub,
1147 ca, hash_hosts && !ca);
Damien Miller66085482013-09-14 09:45:03 +10001148 found_key = 1;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001149 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001150 if (delete_host) {
1151 if (!c && !ca)
1152 printhost(out, cp, pub, ca, 0);
1153 else
1154 printf("# Host %s found: "
1155 "line %d type %s\n", name,
1156 num, key_type(pub));
1157 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001158 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +11001159 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001160 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001161 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001162 if (ca) {
1163 fprintf(stderr, "Warning: "
1164 "ignoring CA key for host: "
1165 "%.64s\n", cp2);
1166 printhost(out, cp2, pub, ca, 0);
1167 } else if (strcspn(cp2, "*?!") !=
1168 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001169 fprintf(stderr, "Warning: "
1170 "ignoring host name with "
1171 "metacharacters: %.64s\n",
1172 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +11001173 printhost(out, cp2, pub, ca, 0);
1174 } else
1175 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +11001176 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001177 has_unhashed = 1;
1178 }
1179 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001180 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001181 }
1182 fclose(in);
1183
1184 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001185 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001186 identity_file);
1187 if (inplace) {
1188 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001189 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001190 fclose(out);
1191 unlink(tmp);
1192 }
1193 exit(1);
1194 }
1195
1196 if (inplace) {
1197 fclose(out);
1198
1199 /* Backup existing file */
1200 if (unlink(old) == -1 && errno != ENOENT)
1201 fatal("unlink %.100s: %s", old, strerror(errno));
1202 if (link(identity_file, old) == -1)
1203 fatal("link %.100s to %.100s: %s", identity_file, old,
1204 strerror(errno));
1205 /* Move new one into place */
1206 if (rename(tmp, identity_file) == -1) {
1207 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1208 strerror(errno));
1209 unlink(tmp);
1210 unlink(old);
1211 exit(1);
1212 }
1213
1214 fprintf(stderr, "%s updated.\n", identity_file);
1215 fprintf(stderr, "Original contents retained as %s\n", old);
1216 if (has_unhashed) {
1217 fprintf(stderr, "WARNING: %s contains unhashed "
1218 "entries\n", old);
1219 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001220 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001221 }
1222 }
1223
Damien Miller66085482013-09-14 09:45:03 +10001224 exit (find_host && !found_key);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001225}
1226
Damien Miller95def091999-11-25 00:26:21 +11001227/*
1228 * Perform changing a passphrase. The argument is the passwd structure
1229 * for the current user.
1230 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001231static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001232do_change_passphrase(struct passwd *pw)
1233{
Damien Miller95def091999-11-25 00:26:21 +11001234 char *comment;
1235 char *old_passphrase, *passphrase1, *passphrase2;
1236 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001237 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001238
Damien Miller95def091999-11-25 00:26:21 +11001239 if (!have_identity)
1240 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001241 if (stat(identity_file, &st) < 0) {
1242 perror(identity_file);
1243 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001244 }
Damien Miller95def091999-11-25 00:26:21 +11001245 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001246 private = key_load_private(identity_file, "", &comment);
1247 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001248 if (identity_passphrase)
1249 old_passphrase = xstrdup(identity_passphrase);
1250 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001251 old_passphrase =
1252 read_passphrase("Enter old passphrase: ",
1253 RP_ALLOW_STDIN);
1254 private = key_load_private(identity_file, old_passphrase,
1255 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001256 memset(old_passphrase, 0, strlen(old_passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001257 free(old_passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001258 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001259 printf("Bad passphrase.\n");
1260 exit(1);
1261 }
Damien Miller95def091999-11-25 00:26:21 +11001262 }
1263 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264
Damien Miller95def091999-11-25 00:26:21 +11001265 /* Ask the new passphrase (twice). */
1266 if (identity_new_passphrase) {
1267 passphrase1 = xstrdup(identity_new_passphrase);
1268 passphrase2 = NULL;
1269 } else {
1270 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001271 read_passphrase("Enter new passphrase (empty for no "
1272 "passphrase): ", RP_ALLOW_STDIN);
1273 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001274 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001275
1276 /* Verify that they are the same. */
1277 if (strcmp(passphrase1, passphrase2) != 0) {
1278 memset(passphrase1, 0, strlen(passphrase1));
1279 memset(passphrase2, 0, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001280 free(passphrase1);
1281 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001282 printf("Pass phrases do not match. Try again.\n");
1283 exit(1);
1284 }
1285 /* Destroy the other copy. */
1286 memset(passphrase2, 0, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10001287 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11001288 }
1289
1290 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001291 if (!key_save_private(private, identity_file, passphrase1, comment,
1292 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001293 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001294 memset(passphrase1, 0, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001295 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001296 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001297 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001298 exit(1);
1299 }
1300 /* Destroy the passphrase and the copy of the key in memory. */
1301 memset(passphrase1, 0, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10001302 free(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001303 key_free(private); /* Destroys contents */
Darren Tuckera627d422013-06-02 07:31:17 +10001304 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001305
1306 printf("Your identification has been saved with the new passphrase.\n");
1307 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001308}
1309
Damien Miller37876e92003-05-15 10:19:46 +10001310/*
1311 * Print the SSHFP RR.
1312 */
Damien Millercb314822006-03-26 13:48:01 +11001313static int
1314do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001315{
1316 Key *public;
1317 char *comment = NULL;
1318 struct stat st;
1319
Damien Millercb314822006-03-26 13:48:01 +11001320 if (fname == NULL)
Damien Miller5bb88332013-07-18 16:13:37 +10001321 fatal("%s: no filename", __func__);
Damien Millercb314822006-03-26 13:48:01 +11001322 if (stat(fname, &st) < 0) {
1323 if (errno == ENOENT)
1324 return 0;
1325 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001326 exit(1);
1327 }
Damien Millercb314822006-03-26 13:48:01 +11001328 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001329 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001330 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001331 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001332 free(comment);
Damien Millercb314822006-03-26 13:48:01 +11001333 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001334 }
1335 if (comment)
Darren Tuckera627d422013-06-02 07:31:17 +10001336 free(comment);
Damien Miller37876e92003-05-15 10:19:46 +10001337
Damien Millercb314822006-03-26 13:48:01 +11001338 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001339 exit(1);
1340}
Damien Miller37876e92003-05-15 10:19:46 +10001341
Damien Miller95def091999-11-25 00:26:21 +11001342/*
1343 * Change the comment of a private key file.
1344 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001345static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346do_change_comment(struct passwd *pw)
1347{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001348 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001349 Key *private;
1350 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001351 struct stat st;
1352 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001353 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001354
Damien Miller95def091999-11-25 00:26:21 +11001355 if (!have_identity)
1356 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001357 if (stat(identity_file, &st) < 0) {
1358 perror(identity_file);
1359 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001360 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001361 private = key_load_private(identity_file, "", &comment);
1362 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001363 if (identity_passphrase)
1364 passphrase = xstrdup(identity_passphrase);
1365 else if (identity_new_passphrase)
1366 passphrase = xstrdup(identity_new_passphrase);
1367 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001368 passphrase = read_passphrase("Enter passphrase: ",
1369 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001370 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001371 private = key_load_private(identity_file, passphrase, &comment);
1372 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001373 memset(passphrase, 0, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001374 free(passphrase);
Damien Miller95def091999-11-25 00:26:21 +11001375 printf("Bad passphrase.\n");
1376 exit(1);
1377 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001378 } else {
1379 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001380 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001381 if (private->type != KEY_RSA1) {
1382 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1383 key_free(private);
1384 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001385 }
Damien Miller95def091999-11-25 00:26:21 +11001386 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Damien Miller95def091999-11-25 00:26:21 +11001388 if (identity_comment) {
1389 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1390 } else {
1391 printf("Enter new comment: ");
1392 fflush(stdout);
1393 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1394 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001395 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001396 exit(1);
1397 }
Damien Miller14b017d2007-09-17 16:09:15 +10001398 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001399 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400
Damien Miller95def091999-11-25 00:26:21 +11001401 /* Save the file using the new passphrase. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11001402 if (!key_save_private(private, identity_file, passphrase, new_comment,
1403 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001404 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001405 memset(passphrase, 0, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001406 free(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001407 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001408 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001409 exit(1);
1410 }
Damien Miller95def091999-11-25 00:26:21 +11001411 memset(passphrase, 0, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001412 free(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001413 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001414 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001415
Damien Miller95def091999-11-25 00:26:21 +11001416 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001417 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1418 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001419 printf("Could not save your public key in %s\n", identity_file);
1420 exit(1);
1421 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001422 f = fdopen(fd, "w");
1423 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001424 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001425 exit(1);
1426 }
Damien Millereba71ba2000-04-29 23:57:08 +10001427 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001428 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001429 key_free(public);
1430 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001431 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432
Darren Tuckera627d422013-06-02 07:31:17 +10001433 free(comment);
Damien Miller95def091999-11-25 00:26:21 +11001434
1435 printf("The comment in your key file has been changed.\n");
1436 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437}
1438
Damien Miller0a80ca12010-02-27 07:55:05 +11001439static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001440fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001441{
1442 char from[32], to[32];
1443 static char ret[64];
1444 time_t tt;
1445 struct tm *tm;
1446
1447 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001448 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001449 return "forever";
1450
Damien Millerf2b70ca2010-03-05 07:39:35 +11001451 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001452 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001453 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001454 tm = localtime(&tt);
1455 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1456 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001457 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001458 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001459 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001460 tm = localtime(&tt);
1461 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1462 }
1463
Damien Millerf2b70ca2010-03-05 07:39:35 +11001464 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001465 snprintf(ret, sizeof(ret), "before %s", to);
1466 return ret;
1467 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001468 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001469 snprintf(ret, sizeof(ret), "after %s", from);
1470 return ret;
1471 }
1472
1473 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1474 return ret;
1475}
1476
1477static void
Damien Miller4e270b02010-04-16 15:56:21 +10001478add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001479{
1480 debug3("%s: %s", __func__, name);
1481 buffer_put_cstring(c, name);
1482 buffer_put_string(c, NULL, 0);
1483}
1484
1485static void
Damien Miller4e270b02010-04-16 15:56:21 +10001486add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001487{
1488 Buffer b;
1489
1490 debug3("%s: %s=%s", __func__, name, value);
1491 buffer_init(&b);
1492 buffer_put_cstring(&b, value);
1493
1494 buffer_put_cstring(c, name);
1495 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1496
1497 buffer_free(&b);
1498}
1499
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001500#define OPTIONS_CRITICAL 1
1501#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001502static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001503prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001504{
Damien Miller0a80ca12010-02-27 07:55:05 +11001505 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001506 if ((which & OPTIONS_CRITICAL) != 0 &&
1507 certflags_command != NULL)
1508 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001509 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001510 (certflags_flags & CERTOPT_X_FWD) != 0)
1511 add_flag_option(c, "permit-X11-forwarding");
1512 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001513 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001514 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001515 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1516 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001517 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001518 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1519 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001520 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001521 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1522 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001523 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001524 if ((which & OPTIONS_CRITICAL) != 0 &&
1525 certflags_src_addr != NULL)
1526 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001527}
1528
Damien Miller757f34e2010-08-05 13:05:31 +10001529static Key *
1530load_pkcs11_key(char *path)
1531{
1532#ifdef ENABLE_PKCS11
1533 Key **keys = NULL, *public, *private = NULL;
1534 int i, nkeys;
1535
1536 if ((public = key_load_public(path, NULL)) == NULL)
1537 fatal("Couldn't load CA public key \"%s\"", path);
1538
1539 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1540 debug3("%s: %d keys", __func__, nkeys);
1541 if (nkeys <= 0)
1542 fatal("cannot read public key from pkcs11");
1543 for (i = 0; i < nkeys; i++) {
1544 if (key_equal_public(public, keys[i])) {
1545 private = keys[i];
1546 continue;
1547 }
1548 key_free(keys[i]);
1549 }
Darren Tuckera627d422013-06-02 07:31:17 +10001550 free(keys);
Damien Miller757f34e2010-08-05 13:05:31 +10001551 key_free(public);
1552 return private;
1553#else
1554 fatal("no pkcs11 support");
1555#endif /* ENABLE_PKCS11 */
1556}
1557
Damien Miller0a80ca12010-02-27 07:55:05 +11001558static void
1559do_ca_sign(struct passwd *pw, int argc, char **argv)
1560{
1561 int i, fd;
1562 u_int n;
1563 Key *ca, *public;
1564 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1565 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001566 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001567
Damien Miller4e270b02010-04-16 15:56:21 +10001568 if (key_type_name != NULL) {
1569 switch (key_type_from_name(key_type_name)) {
1570 case KEY_RSA_CERT_V00:
1571 case KEY_DSA_CERT_V00:
1572 v00 = 1;
1573 break;
1574 case KEY_UNSPEC:
1575 if (strcasecmp(key_type_name, "v00") == 0) {
1576 v00 = 1;
1577 break;
1578 } else if (strcasecmp(key_type_name, "v01") == 0)
1579 break;
1580 /* FALLTHROUGH */
1581 default:
1582 fprintf(stderr, "unknown key type %s\n", key_type_name);
1583 exit(1);
1584 }
1585 }
1586
Damien Miller757f34e2010-08-05 13:05:31 +10001587 pkcs11_init(1);
1588 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1589 if (pkcs11provider != NULL) {
1590 if ((ca = load_pkcs11_key(tmp)) == NULL)
1591 fatal("No PKCS#11 key matching %s found", ca_key_path);
1592 } else if ((ca = load_identity(tmp)) == NULL)
1593 fatal("Couldn't load CA key \"%s\"", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001594 free(tmp);
Damien Miller757f34e2010-08-05 13:05:31 +10001595
Damien Miller0a80ca12010-02-27 07:55:05 +11001596 for (i = 0; i < argc; i++) {
1597 /* Split list of principals */
1598 n = 0;
1599 if (cert_principals != NULL) {
1600 otmp = tmp = xstrdup(cert_principals);
1601 plist = NULL;
1602 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1603 plist = xrealloc(plist, n + 1, sizeof(*plist));
1604 if (*(plist[n] = xstrdup(cp)) == '\0')
1605 fatal("Empty principal name");
1606 }
Darren Tuckera627d422013-06-02 07:31:17 +10001607 free(otmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001608 }
1609
1610 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1611 if ((public = key_load_public(tmp, &comment)) == NULL)
1612 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001613 if (public->type != KEY_RSA && public->type != KEY_DSA &&
1614 public->type != KEY_ECDSA)
Damien Miller0a80ca12010-02-27 07:55:05 +11001615 fatal("%s: key \"%s\" type %s cannot be certified",
1616 __func__, tmp, key_type(public));
1617
1618 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001619 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001620 fatal("Could not upgrade key %s to certificate", tmp);
1621 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001622 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001623 public->cert->key_id = xstrdup(cert_key_id);
1624 public->cert->nprincipals = n;
1625 public->cert->principals = plist;
1626 public->cert->valid_after = cert_valid_from;
1627 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001628 if (v00) {
1629 prepare_options_buf(&public->cert->critical,
1630 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1631 } else {
1632 prepare_options_buf(&public->cert->critical,
1633 OPTIONS_CRITICAL);
1634 prepare_options_buf(&public->cert->extensions,
1635 OPTIONS_EXTENSIONS);
1636 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001637 public->cert->signature_key = key_from_private(ca);
1638
1639 if (key_certify(public, ca) != 0)
1640 fatal("Couldn't not certify key %s", tmp);
1641
1642 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1643 *cp = '\0';
1644 xasprintf(&out, "%s-cert.pub", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001645 free(tmp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001646
1647 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1648 fatal("Could not open \"%s\" for writing: %s", out,
1649 strerror(errno));
1650 if ((f = fdopen(fd, "w")) == NULL)
1651 fatal("%s: fdopen: %s", __func__, strerror(errno));
1652 if (!key_write(public, f))
1653 fatal("Could not write certified key to %s", out);
1654 fprintf(f, " %s\n", comment);
1655 fclose(f);
1656
Damien Miller4e270b02010-04-16 15:56:21 +10001657 if (!quiet) {
1658 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1659 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001660 out, public->cert->key_id,
1661 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001662 cert_principals != NULL ? " for " : "",
1663 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001664 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001665 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001666
1667 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001668 free(out);
Damien Miller0a80ca12010-02-27 07:55:05 +11001669 }
Damien Miller757f34e2010-08-05 13:05:31 +10001670 pkcs11_terminate();
Damien Miller0a80ca12010-02-27 07:55:05 +11001671 exit(0);
1672}
1673
1674static u_int64_t
1675parse_relative_time(const char *s, time_t now)
1676{
1677 int64_t mul, secs;
1678
1679 mul = *s == '-' ? -1 : 1;
1680
1681 if ((secs = convtime(s + 1)) == -1)
1682 fatal("Invalid relative certificate time %s", s);
1683 if (mul == -1 && secs > now)
1684 fatal("Certificate time %s cannot be represented", s);
1685 return now + (u_int64_t)(secs * mul);
1686}
1687
1688static u_int64_t
1689parse_absolute_time(const char *s)
1690{
1691 struct tm tm;
1692 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001693 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001694
Damien Miller2ca342b2010-03-03 12:14:15 +11001695 /*
1696 * POSIX strptime says "The application shall ensure that there
1697 * is white-space or other non-alphanumeric characters between
1698 * any two conversion specifications" so arrange things this way.
1699 */
1700 switch (strlen(s)) {
1701 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001702 fmt = "%Y-%m-%d";
1703 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001704 break;
1705 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001706 fmt = "%Y-%m-%dT%H:%M:%S";
1707 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001708 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1709 break;
1710 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001711 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001712 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001713
1714 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001715 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001716 fatal("Invalid certificate time %s", s);
1717 if ((tt = mktime(&tm)) < 0)
1718 fatal("Certificate time %s cannot be represented", s);
1719 return (u_int64_t)tt;
1720}
1721
1722static void
1723parse_cert_times(char *timespec)
1724{
1725 char *from, *to;
1726 time_t now = time(NULL);
1727 int64_t secs;
1728
1729 /* +timespec relative to now */
1730 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1731 if ((secs = convtime(timespec + 1)) == -1)
1732 fatal("Invalid relative certificate life %s", timespec);
1733 cert_valid_to = now + secs;
1734 /*
1735 * Backdate certificate one minute to avoid problems on hosts
1736 * with poorly-synchronised clocks.
1737 */
1738 cert_valid_from = ((now - 59)/ 60) * 60;
1739 return;
1740 }
1741
1742 /*
1743 * from:to, where
1744 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1745 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1746 */
1747 from = xstrdup(timespec);
1748 to = strchr(from, ':');
1749 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001750 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001751 *to++ = '\0';
1752
1753 if (*from == '-' || *from == '+')
1754 cert_valid_from = parse_relative_time(from, now);
1755 else
1756 cert_valid_from = parse_absolute_time(from);
1757
1758 if (*to == '-' || *to == '+')
Damien Miller5b01b0d2013-10-23 16:31:31 +11001759 cert_valid_to = parse_relative_time(to, now);
Damien Miller0a80ca12010-02-27 07:55:05 +11001760 else
1761 cert_valid_to = parse_absolute_time(to);
1762
1763 if (cert_valid_to <= cert_valid_from)
1764 fatal("Empty certificate validity interval");
Darren Tuckera627d422013-06-02 07:31:17 +10001765 free(from);
Damien Miller0a80ca12010-02-27 07:55:05 +11001766}
1767
1768static void
Damien Miller4e270b02010-04-16 15:56:21 +10001769add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001770{
1771 char *val;
1772
Damien Miller044f4a62011-05-05 14:14:08 +10001773 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001774 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001775 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001776 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001777 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001778 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001779 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001780 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001781 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001782 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001783 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001784 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001785 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001786 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001787 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001788 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001789 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001790 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001791 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001792 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001793 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001794 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001795 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1796 val = opt + 14;
1797 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001798 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001799 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001800 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001801 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001802 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1803 val = opt + 15;
1804 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001805 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001806 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001807 fatal("source-address already specified");
1808 if (addr_match_cidr_list(NULL, val) != 0)
1809 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001810 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001811 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001812 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001813}
1814
Ben Lindstrombba81212001-06-25 05:01:22 +00001815static void
Damien Millerd834d352010-06-26 09:48:02 +10001816show_options(const Buffer *optbuf, int v00, int in_critical)
1817{
Damien Miller746d1a62013-07-18 16:13:02 +10001818 char *name;
1819 u_char *data;
Damien Millerd834d352010-06-26 09:48:02 +10001820 u_int dlen;
1821 Buffer options, option;
1822
1823 buffer_init(&options);
1824 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1825
1826 buffer_init(&option);
1827 while (buffer_len(&options) != 0) {
1828 name = buffer_get_string(&options, NULL);
1829 data = buffer_get_string_ptr(&options, &dlen);
1830 buffer_append(&option, data, dlen);
1831 printf(" %s", name);
1832 if ((v00 || !in_critical) &&
1833 (strcmp(name, "permit-X11-forwarding") == 0 ||
1834 strcmp(name, "permit-agent-forwarding") == 0 ||
1835 strcmp(name, "permit-port-forwarding") == 0 ||
1836 strcmp(name, "permit-pty") == 0 ||
1837 strcmp(name, "permit-user-rc") == 0))
1838 printf("\n");
1839 else if ((v00 || in_critical) &&
1840 (strcmp(name, "force-command") == 0 ||
1841 strcmp(name, "source-address") == 0)) {
1842 data = buffer_get_string(&option, NULL);
1843 printf(" %s\n", data);
Darren Tuckera627d422013-06-02 07:31:17 +10001844 free(data);
Damien Millerd834d352010-06-26 09:48:02 +10001845 } else {
1846 printf(" UNKNOWN OPTION (len %u)\n",
1847 buffer_len(&option));
1848 buffer_clear(&option);
1849 }
Darren Tuckera627d422013-06-02 07:31:17 +10001850 free(name);
Damien Millerd834d352010-06-26 09:48:02 +10001851 if (buffer_len(&option) != 0)
1852 fatal("Option corrupt: extra data at end");
1853 }
1854 buffer_free(&option);
1855 buffer_free(&options);
1856}
1857
1858static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001859do_show_cert(struct passwd *pw)
1860{
1861 Key *key;
1862 struct stat st;
1863 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001864 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001865
1866 if (!have_identity)
1867 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001868 if (stat(identity_file, &st) < 0)
1869 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001870 if ((key = key_load_public(identity_file, NULL)) == NULL)
1871 fatal("%s is not a public key", identity_file);
1872 if (!key_is_cert(key))
1873 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001874 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1875
Damien Millerf2b70ca2010-03-05 07:39:35 +11001876 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1877 ca_fp = key_fingerprint(key->cert->signature_key,
1878 SSH_FP_MD5, SSH_FP_HEX);
1879
1880 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001881 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1882 key_cert_type(key));
1883 printf(" Public key: %s %s\n", key_type(key), key_fp);
1884 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001885 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001886 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001887 if (!v00) {
1888 printf(" Serial: %llu\n",
1889 (unsigned long long)key->cert->serial);
1890 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001891 printf(" Valid: %s\n",
1892 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1893 printf(" Principals: ");
1894 if (key->cert->nprincipals == 0)
1895 printf("(none)\n");
1896 else {
1897 for (i = 0; i < key->cert->nprincipals; i++)
1898 printf("\n %s",
1899 key->cert->principals[i]);
1900 printf("\n");
1901 }
Damien Miller4e270b02010-04-16 15:56:21 +10001902 printf(" Critical Options: ");
1903 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001904 printf("(none)\n");
1905 else {
1906 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001907 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001908 }
Damien Miller4e270b02010-04-16 15:56:21 +10001909 if (!v00) {
1910 printf(" Extensions: ");
1911 if (buffer_len(&key->cert->extensions) == 0)
1912 printf("(none)\n");
1913 else {
1914 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001915 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001916 }
1917 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001918 exit(0);
1919}
1920
1921static void
Damien Millerf3747bf2013-01-18 11:44:04 +11001922load_krl(const char *path, struct ssh_krl **krlp)
1923{
1924 Buffer krlbuf;
1925 int fd;
1926
1927 buffer_init(&krlbuf);
1928 if ((fd = open(path, O_RDONLY)) == -1)
1929 fatal("open %s: %s", path, strerror(errno));
1930 if (!key_load_file(fd, path, &krlbuf))
1931 fatal("Unable to load KRL");
1932 close(fd);
1933 /* XXX check sigs */
1934 if (ssh_krl_from_blob(&krlbuf, krlp, NULL, 0) != 0 ||
1935 *krlp == NULL)
1936 fatal("Invalid KRL file");
1937 buffer_free(&krlbuf);
1938}
1939
1940static void
1941update_krl_from_file(struct passwd *pw, const char *file, const Key *ca,
1942 struct ssh_krl *krl)
1943{
1944 Key *key = NULL;
1945 u_long lnum = 0;
1946 char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES];
1947 unsigned long long serial, serial2;
1948 int i, was_explicit_key, was_sha1, r;
1949 FILE *krl_spec;
1950
1951 path = tilde_expand_filename(file, pw->pw_uid);
1952 if (strcmp(path, "-") == 0) {
1953 krl_spec = stdin;
1954 free(path);
1955 path = xstrdup("(standard input)");
1956 } else if ((krl_spec = fopen(path, "r")) == NULL)
1957 fatal("fopen %s: %s", path, strerror(errno));
1958
1959 if (!quiet)
1960 printf("Revoking from %s\n", path);
1961 while (read_keyfile_line(krl_spec, path, line, sizeof(line),
1962 &lnum) == 0) {
1963 was_explicit_key = was_sha1 = 0;
1964 cp = line + strspn(line, " \t");
1965 /* Trim trailing space, comments and strip \n */
1966 for (i = 0, r = -1; cp[i] != '\0'; i++) {
1967 if (cp[i] == '#' || cp[i] == '\n') {
1968 cp[i] = '\0';
1969 break;
1970 }
1971 if (cp[i] == ' ' || cp[i] == '\t') {
1972 /* Remember the start of a span of whitespace */
1973 if (r == -1)
1974 r = i;
1975 } else
1976 r = -1;
1977 }
1978 if (r != -1)
1979 cp[r] = '\0';
1980 if (*cp == '\0')
1981 continue;
1982 if (strncasecmp(cp, "serial:", 7) == 0) {
1983 if (ca == NULL) {
Damien Millerd234afb2013-08-21 02:42:58 +10001984 fatal("revoking certificates by serial number "
Damien Millerf3747bf2013-01-18 11:44:04 +11001985 "requires specification of a CA key");
1986 }
1987 cp += 7;
1988 cp = cp + strspn(cp, " \t");
1989 errno = 0;
1990 serial = strtoull(cp, &ep, 0);
1991 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
1992 fatal("%s:%lu: invalid serial \"%s\"",
1993 path, lnum, cp);
1994 if (errno == ERANGE && serial == ULLONG_MAX)
1995 fatal("%s:%lu: serial out of range",
1996 path, lnum);
1997 serial2 = serial;
1998 if (*ep == '-') {
1999 cp = ep + 1;
2000 errno = 0;
2001 serial2 = strtoull(cp, &ep, 0);
2002 if (*cp == '\0' || *ep != '\0')
2003 fatal("%s:%lu: invalid serial \"%s\"",
2004 path, lnum, cp);
2005 if (errno == ERANGE && serial2 == ULLONG_MAX)
2006 fatal("%s:%lu: serial out of range",
2007 path, lnum);
2008 if (serial2 <= serial)
2009 fatal("%s:%lu: invalid serial range "
2010 "%llu:%llu", path, lnum,
2011 (unsigned long long)serial,
2012 (unsigned long long)serial2);
2013 }
2014 if (ssh_krl_revoke_cert_by_serial_range(krl,
2015 ca, serial, serial2) != 0) {
2016 fatal("%s: revoke serial failed",
2017 __func__);
2018 }
2019 } else if (strncasecmp(cp, "id:", 3) == 0) {
2020 if (ca == NULL) {
Damien Millerd5d9d7b2013-08-21 02:43:27 +10002021 fatal("revoking certificates by key ID "
Damien Millerf3747bf2013-01-18 11:44:04 +11002022 "requires specification of a CA key");
2023 }
2024 cp += 3;
2025 cp = cp + strspn(cp, " \t");
2026 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2027 fatal("%s: revoke key ID failed", __func__);
2028 } else {
2029 if (strncasecmp(cp, "key:", 4) == 0) {
2030 cp += 4;
2031 cp = cp + strspn(cp, " \t");
2032 was_explicit_key = 1;
2033 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2034 cp += 5;
2035 cp = cp + strspn(cp, " \t");
2036 was_sha1 = 1;
2037 } else {
2038 /*
2039 * Just try to process the line as a key.
2040 * Parsing will fail if it isn't.
2041 */
2042 }
2043 if ((key = key_new(KEY_UNSPEC)) == NULL)
2044 fatal("key_new");
2045 if (key_read(key, &cp) != 1)
2046 fatal("%s:%lu: invalid key", path, lnum);
2047 if (was_explicit_key)
2048 r = ssh_krl_revoke_key_explicit(krl, key);
2049 else if (was_sha1)
2050 r = ssh_krl_revoke_key_sha1(krl, key);
2051 else
2052 r = ssh_krl_revoke_key(krl, key);
2053 if (r != 0)
2054 fatal("%s: revoke key failed", __func__);
2055 key_free(key);
2056 }
2057 }
2058 if (strcmp(path, "-") != 0)
2059 fclose(krl_spec);
Damien Miller0d6771b2013-04-23 15:23:24 +10002060 free(path);
Damien Millerf3747bf2013-01-18 11:44:04 +11002061}
2062
2063static void
2064do_gen_krl(struct passwd *pw, int updating, int argc, char **argv)
2065{
2066 struct ssh_krl *krl;
2067 struct stat sb;
2068 Key *ca = NULL;
2069 int fd, i;
2070 char *tmp;
2071 Buffer kbuf;
2072
2073 if (*identity_file == '\0')
2074 fatal("KRL generation requires an output file");
2075 if (stat(identity_file, &sb) == -1) {
2076 if (errno != ENOENT)
2077 fatal("Cannot access KRL \"%s\": %s",
2078 identity_file, strerror(errno));
2079 if (updating)
2080 fatal("KRL \"%s\" does not exist", identity_file);
2081 }
2082 if (ca_key_path != NULL) {
2083 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2084 if ((ca = key_load_public(tmp, NULL)) == NULL)
2085 fatal("Cannot load CA public key %s", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10002086 free(tmp);
Damien Millerf3747bf2013-01-18 11:44:04 +11002087 }
2088
2089 if (updating)
2090 load_krl(identity_file, &krl);
2091 else if ((krl = ssh_krl_init()) == NULL)
2092 fatal("couldn't create KRL");
2093
2094 if (cert_serial != 0)
2095 ssh_krl_set_version(krl, cert_serial);
2096 if (identity_comment != NULL)
2097 ssh_krl_set_comment(krl, identity_comment);
2098
2099 for (i = 0; i < argc; i++)
2100 update_krl_from_file(pw, argv[i], ca, krl);
2101
2102 buffer_init(&kbuf);
2103 if (ssh_krl_to_blob(krl, &kbuf, NULL, 0) != 0)
2104 fatal("Couldn't generate KRL");
2105 if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2106 fatal("open %s: %s", identity_file, strerror(errno));
2107 if (atomicio(vwrite, fd, buffer_ptr(&kbuf), buffer_len(&kbuf)) !=
2108 buffer_len(&kbuf))
2109 fatal("write %s: %s", identity_file, strerror(errno));
2110 close(fd);
2111 buffer_free(&kbuf);
2112 ssh_krl_free(krl);
Damien Miller0d6771b2013-04-23 15:23:24 +10002113 if (ca != NULL)
2114 key_free(ca);
Damien Millerf3747bf2013-01-18 11:44:04 +11002115}
2116
2117static void
2118do_check_krl(struct passwd *pw, int argc, char **argv)
2119{
2120 int i, r, ret = 0;
2121 char *comment;
2122 struct ssh_krl *krl;
2123 Key *k;
2124
2125 if (*identity_file == '\0')
2126 fatal("KRL checking requires an input file");
2127 load_krl(identity_file, &krl);
2128 for (i = 0; i < argc; i++) {
2129 if ((k = key_load_public(argv[i], &comment)) == NULL)
2130 fatal("Cannot load public key %s", argv[i]);
2131 r = ssh_krl_check_key(krl, k);
2132 printf("%s%s%s%s: %s\n", argv[i],
2133 *comment ? " (" : "", comment, *comment ? ")" : "",
2134 r == 0 ? "ok" : "REVOKED");
2135 if (r != 0)
2136 ret = 1;
2137 key_free(k);
2138 free(comment);
2139 }
2140 ssh_krl_free(krl);
2141 exit(ret);
2142}
2143
2144static void
Damien Miller431f66b1999-11-21 18:31:57 +11002145usage(void)
2146{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10002147 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00002148 fprintf(stderr, "Options:\n");
Damien Miller58f1baf2011-05-05 14:06:15 +10002149 fprintf(stderr, " -A Generate non-existent host keys for all key types.\n");
Damien Millerbcd00ab2013-12-07 10:41:55 +11002150 fprintf(stderr, " -a number Number of KDF rounds for new key format or moduli primality tests.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00002151 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002152 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00002153 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002154 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11002155#ifdef ENABLE_PKCS11
2156 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
2157#endif
Damien Miller44b25042010-07-02 13:35:01 +10002158 fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002159 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
2160 fprintf(stderr, " -f filename Filename of the key file.\n");
2161 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
2162 fprintf(stderr, " -g Use generic DNS resource record format.\n");
2163 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11002164 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
2165 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Damien Miller44b25042010-07-02 13:35:01 +10002166 fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
Damien Miller709a1e92012-07-31 12:20:43 +10002167 fprintf(stderr, " -J number Screen this number of moduli lines.\n");
Damien Millerdfceafe2012-07-06 13:44:19 +10002168 fprintf(stderr, " -j number Start screening moduli at specified line.\n");
Damien Miller927d82b2011-10-18 16:05:38 +11002169 fprintf(stderr, " -K checkpt Write checkpoints to this file.\n");
Damien Millerf3747bf2013-01-18 11:44:04 +11002170 fprintf(stderr, " -k Generate a KRL file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11002171 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002172 fprintf(stderr, " -l Show fingerprint of key file.\n");
2173 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10002174 fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002175 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller6022f582010-07-02 13:37:01 +10002176 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller1f181422010-04-18 08:08:03 +10002177 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Millerbcd00ab2013-12-07 10:41:55 +11002178 fprintf(stderr, " -o Enforce new private key format.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002179 fprintf(stderr, " -P phrase Provide old passphrase.\n");
2180 fprintf(stderr, " -p Change passphrase of private key file.\n");
Damien Millerf3747bf2013-01-18 11:44:04 +11002181 fprintf(stderr, " -Q Test whether key(s) are revoked in KRL.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002182 fprintf(stderr, " -q Quiet.\n");
2183 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
2184 fprintf(stderr, " -r hostname Print DNS resource record.\n");
2185 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10002186 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002187 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
2188 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Millerf3747bf2013-01-18 11:44:04 +11002189 fprintf(stderr, " -u Update KRL rather than creating a new one.\n");
Damien Miller3d6d68b2013-01-20 22:33:23 +11002190 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10002191 fprintf(stderr, " -v Verbose.\n");
2192 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
2193 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10002194 fprintf(stderr, " -z serial Specify a serial number.\n");
Damien Millerbcd00ab2013-12-07 10:41:55 +11002195 fprintf(stderr, " -Z cipher Specify a cipher for new private key format.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10002196
Damien Miller95def091999-11-25 00:26:21 +11002197 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11002198}
2199
Damien Miller95def091999-11-25 00:26:21 +11002200/*
2201 * Main program for key management.
2202 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002203int
Damien Millerdf8b7db2007-01-05 16:22:57 +11002204main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002205{
Damien Millera41c8b12002-01-22 23:05:08 +11002206 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller390d0562011-10-18 16:05:19 +11002207 char *checkpoint = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11002208 char out_file[MAXPATHLEN], *ep, *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002209 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11002210 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11002211 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11002212 int opt, type, fd;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002213 u_int32_t memory = 0, generator_wanted = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002214 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11002215 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
Damien Millerdfceafe2012-07-06 13:44:19 +10002216 unsigned long start_lineno = 0, lines_to_process = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10002217 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11002218 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10002219 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002220
Damien Miller95def091999-11-25 00:26:21 +11002221 extern int optind;
2222 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002223
Darren Tuckerce321d82005-10-03 18:11:24 +10002224 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2225 sanitise_stdfd();
2226
Darren Tucker9ac56e92007-01-14 10:19:59 +11002227 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10002228
Damien Miller4314c2b2010-09-10 11:12:09 +10002229 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11002230 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10002231
Kevin Steves3a881912002-07-20 19:05:40 +00002232 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10002233
Damien Miller5428f641999-11-25 11:54:57 +11002234 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11002235 pw = getpwuid(getuid());
2236 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +10002237 printf("No user exists for uid %lu\n", (u_long)getuid());
Damien Miller95def091999-11-25 00:26:21 +11002238 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002239 }
Damien Millereba71ba2000-04-29 23:57:08 +10002240 if (gethostname(hostname, sizeof(hostname)) < 0) {
2241 perror("gethostname");
2242 exit(1);
2243 }
Damien Miller5428f641999-11-25 11:54:57 +11002244
Damien Millerbcd00ab2013-12-07 10:41:55 +11002245 /* Remaining characters: EUYdw */
2246 while ((opt = getopt(argc, argv, "ABHLQXceghiklopquvxy"
2247 "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 +11002248 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10002249 case 'A':
2250 gen_all_hostkeys = 1;
2251 break;
Damien Miller95def091999-11-25 00:26:21 +11002252 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10002253 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10002254 if (errstr)
2255 fatal("Bits has bad value %s (%s)",
2256 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11002257 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002258 case 'F':
2259 find_host = 1;
2260 rr_hostname = optarg;
2261 break;
2262 case 'H':
2263 hash_hosts = 1;
2264 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002265 case 'I':
2266 cert_key_id = optarg;
2267 break;
Damien Millerdfceafe2012-07-06 13:44:19 +10002268 case 'J':
2269 lines_to_process = strtoul(optarg, NULL, 10);
2270 break;
2271 case 'j':
2272 start_lineno = strtoul(optarg, NULL, 10);
2273 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002274 case 'R':
2275 delete_host = 1;
2276 rr_hostname = optarg;
2277 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11002278 case 'L':
2279 show_cert = 1;
2280 break;
Damien Miller95def091999-11-25 00:26:21 +11002281 case 'l':
2282 print_fingerprint = 1;
2283 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002284 case 'B':
2285 print_bubblebabble = 1;
2286 break;
Damien Miller44b25042010-07-02 13:35:01 +10002287 case 'm':
2288 if (strcasecmp(optarg, "RFC4716") == 0 ||
2289 strcasecmp(optarg, "ssh2") == 0) {
2290 convert_format = FMT_RFC4716;
2291 break;
2292 }
2293 if (strcasecmp(optarg, "PKCS8") == 0) {
2294 convert_format = FMT_PKCS8;
2295 break;
2296 }
2297 if (strcasecmp(optarg, "PEM") == 0) {
2298 convert_format = FMT_PEM;
2299 break;
2300 }
2301 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002302 case 'n':
2303 cert_principals = optarg;
2304 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002305 case 'o':
2306 use_new_format = 1;
2307 break;
Damien Miller95def091999-11-25 00:26:21 +11002308 case 'p':
2309 change_passphrase = 1;
2310 break;
Damien Miller95def091999-11-25 00:26:21 +11002311 case 'c':
2312 change_comment = 1;
2313 break;
Damien Miller95def091999-11-25 00:26:21 +11002314 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002315 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2316 sizeof(identity_file))
2317 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002318 have_identity = 1;
2319 break;
Damien Miller37876e92003-05-15 10:19:46 +10002320 case 'g':
2321 print_generic = 1;
2322 break;
Damien Miller95def091999-11-25 00:26:21 +11002323 case 'P':
2324 identity_passphrase = optarg;
2325 break;
Damien Miller95def091999-11-25 00:26:21 +11002326 case 'N':
2327 identity_new_passphrase = optarg;
2328 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002329 case 'Q':
2330 check_krl = 1;
2331 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002332 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002333 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002334 break;
Damien Millerbcd00ab2013-12-07 10:41:55 +11002335 case 'Z':
2336 new_format_cipher = optarg;
2337 break;
Damien Miller95def091999-11-25 00:26:21 +11002338 case 'C':
2339 identity_comment = optarg;
2340 break;
Damien Miller95def091999-11-25 00:26:21 +11002341 case 'q':
2342 quiet = 1;
2343 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002344 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002345 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002346 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002347 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002348 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002349 case 'h':
2350 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002351 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002352 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002353 case 'k':
2354 gen_krl = 1;
2355 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002356 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002357 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002358 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002359 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002360 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002361 case 'y':
2362 print_public = 1;
2363 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002364 case 's':
2365 ca_key_path = optarg;
2366 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002367 case 't':
2368 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002369 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002370 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002371 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002372 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11002373 case 'u':
2374 update_krl = 1;
2375 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002376 case 'v':
2377 if (log_level == SYSLOG_LEVEL_INFO)
2378 log_level = SYSLOG_LEVEL_DEBUG1;
2379 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002380 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002381 log_level < SYSLOG_LEVEL_DEBUG3)
2382 log_level++;
2383 }
2384 break;
Damien Miller37876e92003-05-15 10:19:46 +10002385 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002386 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002387 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002388 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002389 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2390 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002391 if (errstr)
2392 fatal("Desired generator has bad value: %s (%s)",
2393 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002394 break;
2395 case 'a':
Damien Millerbcd00ab2013-12-07 10:41:55 +11002396 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002397 if (errstr)
Damien Millerbcd00ab2013-12-07 10:41:55 +11002398 fatal("Invalid number: %s (%s)",
Damien Millerb089fb52005-05-26 12:16:18 +10002399 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002400 break;
2401 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002402 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002403 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002404 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002405 break;
2406 case 'G':
2407 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002408 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2409 sizeof(out_file))
2410 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002411 break;
2412 case 'T':
2413 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002414 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2415 sizeof(out_file))
2416 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002417 break;
Damien Miller390d0562011-10-18 16:05:19 +11002418 case 'K':
2419 if (strlen(optarg) >= MAXPATHLEN)
2420 fatal("Checkpoint filename too long");
2421 checkpoint = xstrdup(optarg);
2422 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002423 case 'S':
2424 /* XXX - also compare length against bits */
2425 if (BN_hex2bn(&start, optarg) == 0)
2426 fatal("Invalid start point.");
2427 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002428 case 'V':
2429 parse_cert_times(optarg);
2430 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002431 case 'z':
Damien Miller6f3b3622012-11-14 19:04:33 +11002432 errno = 0;
2433 cert_serial = strtoull(optarg, &ep, 10);
2434 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2435 (errno == ERANGE && cert_serial == ULLONG_MAX))
2436 fatal("Invalid serial number \"%s\"", optarg);
Damien Miller4e270b02010-04-16 15:56:21 +10002437 break;
Damien Miller95def091999-11-25 00:26:21 +11002438 case '?':
2439 default:
2440 usage();
2441 }
2442 }
Darren Tucker06930c72003-12-31 11:34:51 +11002443
2444 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002445 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002446
Damien Miller0a80ca12010-02-27 07:55:05 +11002447 argv += optind;
2448 argc -= optind;
2449
2450 if (ca_key_path != NULL) {
Damien Millerf3747bf2013-01-18 11:44:04 +11002451 if (argc < 1 && !gen_krl) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002452 printf("Too few arguments.\n");
2453 usage();
2454 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002455 } else if (argc > 0 && !gen_krl && !check_krl) {
Damien Miller95def091999-11-25 00:26:21 +11002456 printf("Too many arguments.\n");
2457 usage();
2458 }
2459 if (change_passphrase && change_comment) {
2460 printf("Can only have one of -p and -c.\n");
2461 usage();
2462 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002463 if (print_fingerprint && (delete_host || hash_hosts)) {
Damien Millerec77c952013-01-09 15:58:00 +11002464 printf("Cannot use -l with -H or -R.\n");
Darren Tucker0f7e9102008-06-08 12:54:29 +10002465 usage();
2466 }
Damien Millerf3747bf2013-01-18 11:44:04 +11002467 if (gen_krl) {
2468 do_gen_krl(pw, update_krl, argc, argv);
2469 return (0);
2470 }
2471 if (check_krl) {
2472 do_check_krl(pw, argc, argv);
2473 return (0);
2474 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002475 if (ca_key_path != NULL) {
2476 if (cert_key_id == NULL)
2477 fatal("Must specify key id (-I) when certifying");
2478 do_ca_sign(pw, argc, argv);
2479 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002480 if (show_cert)
2481 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002482 if (delete_host || hash_hosts || find_host)
2483 do_known_hosts(pw, rr_hostname);
Damien Millerec77c952013-01-09 15:58:00 +11002484 if (pkcs11provider != NULL)
2485 do_download(pw);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002486 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002487 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002488 if (change_passphrase)
2489 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002490 if (change_comment)
2491 do_change_comment(pw);
Damien Miller44b25042010-07-02 13:35:01 +10002492 if (convert_to)
2493 do_convert_to(pw);
2494 if (convert_from)
2495 do_convert_from(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10002496 if (print_public)
2497 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002498 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002499 unsigned int n = 0;
2500
2501 if (have_identity) {
2502 n = do_print_resource_record(pw,
2503 identity_file, rr_hostname);
2504 if (n == 0) {
2505 perror(identity_file);
2506 exit(1);
2507 }
2508 exit(0);
2509 } else {
2510
2511 n += do_print_resource_record(pw,
2512 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2513 n += do_print_resource_record(pw,
2514 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
Damien Miller3bde12a2012-06-20 21:51:11 +10002515 n += do_print_resource_record(pw,
2516 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
Damien Millercb314822006-03-26 13:48:01 +11002517
2518 if (n == 0)
2519 fatal("no keys found.");
2520 exit(0);
2521 }
Damien Miller37876e92003-05-15 10:19:46 +10002522 }
Damien Miller95def091999-11-25 00:26:21 +11002523
Darren Tucker019cefe2003-08-02 22:40:07 +10002524 if (do_gen_candidates) {
2525 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002526
Darren Tucker019cefe2003-08-02 22:40:07 +10002527 if (out == NULL) {
2528 error("Couldn't open modulus candidate file \"%s\": %s",
2529 out_file, strerror(errno));
2530 return (1);
2531 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002532 if (bits == 0)
2533 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002534 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002535 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002536
2537 return (0);
2538 }
2539
2540 if (do_screen_candidates) {
2541 FILE *in;
Damien Miller78d22712013-02-12 11:03:36 +11002542 FILE *out = fopen(out_file, "a");
Darren Tucker019cefe2003-08-02 22:40:07 +10002543
2544 if (have_identity && strcmp(identity_file, "-") != 0) {
2545 if ((in = fopen(identity_file, "r")) == NULL) {
2546 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002547 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002548 strerror(errno));
2549 }
2550 } else
2551 in = stdin;
2552
2553 if (out == NULL) {
2554 fatal("Couldn't open moduli file \"%s\": %s",
2555 out_file, strerror(errno));
2556 }
Damien Millerbcd00ab2013-12-07 10:41:55 +11002557 if (prime_test(in, out, rounds == 0 ? 100 : rounds,
2558 generator_wanted, checkpoint,
Damien Millerdfceafe2012-07-06 13:44:19 +10002559 start_lineno, lines_to_process) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002560 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002561 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002562 }
2563
Damien Miller58f1baf2011-05-05 14:06:15 +10002564 if (gen_all_hostkeys) {
2565 do_gen_all_hostkeys(pw);
2566 return (0);
2567 }
2568
Damien Millerf14be5c2005-11-05 15:15:49 +11002569 if (key_type_name == NULL)
2570 key_type_name = "rsa";
2571
Damien Millere39cacc2000-11-29 12:18:44 +11002572 type = key_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002573 type_bits_valid(type, &bits);
2574
Darren Tucker3af2ac52005-11-29 13:10:24 +11002575 if (!quiet)
2576 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002577 private = key_generate(type, bits);
2578 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002579 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002580 exit(1);
2581 }
2582 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002583
2584 if (!have_identity)
2585 ask_filename(pw, "Enter file in which to save the key");
2586
Damien Miller788f2122005-11-05 15:14:59 +11002587 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002588 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2589 pw->pw_dir, _PATH_SSH_USER_DIR);
2590 if (strstr(identity_file, dotsshdir) != NULL) {
2591 if (stat(dotsshdir, &st) < 0) {
2592 if (errno != ENOENT) {
2593 error("Could not stat %s: %s", dotsshdir,
2594 strerror(errno));
2595 } else if (mkdir(dotsshdir, 0700) < 0) {
2596 error("Could not create directory '%s': %s",
2597 dotsshdir, strerror(errno));
2598 } else if (!quiet)
2599 printf("Created directory '%s'.\n", dotsshdir);
2600 }
Damien Miller95def091999-11-25 00:26:21 +11002601 }
2602 /* If the file already exists, ask the user to confirm. */
2603 if (stat(identity_file, &st) >= 0) {
2604 char yesno[3];
2605 printf("%s already exists.\n", identity_file);
2606 printf("Overwrite (y/n)? ");
2607 fflush(stdout);
2608 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2609 exit(1);
2610 if (yesno[0] != 'y' && yesno[0] != 'Y')
2611 exit(1);
2612 }
2613 /* Ask for a passphrase (twice). */
2614 if (identity_passphrase)
2615 passphrase1 = xstrdup(identity_passphrase);
2616 else if (identity_new_passphrase)
2617 passphrase1 = xstrdup(identity_new_passphrase);
2618 else {
2619passphrase_again:
2620 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002621 read_passphrase("Enter passphrase (empty for no "
2622 "passphrase): ", RP_ALLOW_STDIN);
2623 passphrase2 = read_passphrase("Enter same passphrase again: ",
2624 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002625 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002626 /*
2627 * The passphrases do not match. Clear them and
2628 * retry.
2629 */
Damien Miller95def091999-11-25 00:26:21 +11002630 memset(passphrase1, 0, strlen(passphrase1));
2631 memset(passphrase2, 0, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002632 free(passphrase1);
2633 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002634 printf("Passphrases do not match. Try again.\n");
2635 goto passphrase_again;
2636 }
2637 /* Clear the other copy of the passphrase. */
2638 memset(passphrase2, 0, strlen(passphrase2));
Darren Tuckera627d422013-06-02 07:31:17 +10002639 free(passphrase2);
Damien Miller95def091999-11-25 00:26:21 +11002640 }
2641
Damien Miller95def091999-11-25 00:26:21 +11002642 if (identity_comment) {
2643 strlcpy(comment, identity_comment, sizeof(comment));
2644 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002645 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002646 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2647 }
2648
2649 /* Save the key with the given passphrase and comment. */
Damien Millerbcd00ab2013-12-07 10:41:55 +11002650 if (!key_save_private(private, identity_file, passphrase1, comment,
2651 use_new_format, new_format_cipher, rounds)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002652 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002653 memset(passphrase1, 0, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002654 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002655 exit(1);
2656 }
2657 /* Clear the passphrase. */
2658 memset(passphrase1, 0, strlen(passphrase1));
Darren Tuckera627d422013-06-02 07:31:17 +10002659 free(passphrase1);
Damien Miller95def091999-11-25 00:26:21 +11002660
2661 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002662 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002663
2664 if (!quiet)
2665 printf("Your identification has been saved in %s.\n", identity_file);
2666
Damien Miller95def091999-11-25 00:26:21 +11002667 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002668 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2669 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002670 printf("Could not save your public key in %s\n", identity_file);
2671 exit(1);
2672 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002673 f = fdopen(fd, "w");
2674 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002675 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002676 exit(1);
2677 }
Damien Millereba71ba2000-04-29 23:57:08 +10002678 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002679 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002680 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002681 fclose(f);
2682
2683 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002684 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002685 char *ra = key_fingerprint(public, SSH_FP_MD5,
2686 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002687 printf("Your public key has been saved in %s.\n",
2688 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002689 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002690 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002691 printf("The key's randomart image is:\n");
2692 printf("%s\n", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10002693 free(ra);
2694 free(fp);
Damien Miller95def091999-11-25 00:26:21 +11002695 }
Damien Millereba71ba2000-04-29 23:57:08 +10002696
2697 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002698 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002699}