blob: c95e4ab2987f6d81e9cc522dbdd8b5396154dc84 [file] [log] [blame]
Damien Miller821de0a2011-01-11 17:20:29 +11001/* $OpenBSD: ssh-keygen.c,v 1.205 2011/01/11 06:13:10 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100025
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100029#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000041#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "authfile.h"
43#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110044#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
46#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100047#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110048#include "match.h"
49#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100050#include "dns.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110051#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110052
Damien Miller7ea845e2010-02-12 09:21:02 +110053#ifdef ENABLE_PKCS11
54#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000055#endif
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 */
110long long cert_serial = 0;
111
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 Miller431f66b1999-11-21 18:31:57 +1100153/* argv0 */
154extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Millereba71ba2000-04-29 23:57:08 +1000156char hostname[MAXHOSTNAMELEN];
157
Darren Tucker770fc012004-05-13 16:24:32 +1000158/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000159int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000160int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
161
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void
Damien Miller431f66b1999-11-21 18:31:57 +1100163ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Miller95def091999-11-25 00:26:21 +1100165 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100166 char *name = NULL;
167
Damien Miller993dd552002-02-19 15:22:47 +1100168 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000169 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100170 else {
Damien Miller993dd552002-02-19 15:22:47 +1100171 switch (key_type_from_name(key_type_name)) {
172 case KEY_RSA1:
173 name = _PATH_SSH_CLIENT_IDENTITY;
174 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000175 case KEY_DSA_CERT:
176 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100177 case KEY_DSA:
178 name = _PATH_SSH_CLIENT_ID_DSA;
179 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100180#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000181 case KEY_ECDSA_CERT:
182 case KEY_ECDSA:
183 name = _PATH_SSH_CLIENT_ID_ECDSA;
184 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100185#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000186 case KEY_RSA_CERT:
187 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100188 case KEY_RSA:
189 name = _PATH_SSH_CLIENT_ID_RSA;
190 break;
191 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100192 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100193 exit(1);
194 break;
195 }
Damien Miller90967402006-03-26 14:07:26 +1100196 }
Damien Millere39cacc2000-11-29 12:18:44 +1100197 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000198 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100199 if (fgets(buf, sizeof(buf), stdin) == NULL)
200 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000201 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100202 if (strcmp(buf, "") != 0)
203 strlcpy(identity_file, buf, sizeof(identity_file));
204 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100205}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000208load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000209{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000210 char *pass;
211 Key *prv;
212
Ben Lindstroma3700052001-04-05 23:26:32 +0000213 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000214 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000215 if (identity_passphrase)
216 pass = xstrdup(identity_passphrase);
217 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000218 pass = read_passphrase("Enter passphrase: ",
219 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000220 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000221 memset(pass, 0, strlen(pass));
222 xfree(pass);
223 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000224 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000225}
226
Damien Miller874d77b2000-10-14 16:23:11 +1100227#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000228#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100229#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000230#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000231
Ben Lindstrombba81212001-06-25 05:01:22 +0000232static void
Damien Miller44b25042010-07-02 13:35:01 +1000233do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000234{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000235 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000236 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100237 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000238
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000239 if (key_to_blob(k, &blob, &len) <= 0) {
240 fprintf(stderr, "key_to_blob failed\n");
241 exit(1);
242 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100243 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
244 snprintf(comment, sizeof(comment),
245 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000246 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000247 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100248
249 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
250 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000251 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100252 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000253 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000254 xfree(blob);
255 exit(0);
256}
257
Ben Lindstrombba81212001-06-25 05:01:22 +0000258static void
Damien Miller44b25042010-07-02 13:35:01 +1000259do_convert_to_pkcs8(Key *k)
260{
261 switch (key_type_plain(k->type)) {
262 case KEY_RSA:
263 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
264 fatal("PEM_write_RSA_PUBKEY failed");
265 break;
266 case KEY_DSA:
267 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
268 fatal("PEM_write_DSA_PUBKEY failed");
269 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000270#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000271 case KEY_ECDSA:
272 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
273 fatal("PEM_write_EC_PUBKEY failed");
274 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000275#endif
Damien Miller44b25042010-07-02 13:35:01 +1000276 default:
277 fatal("%s: unsupported key type %s", __func__, key_type(k));
278 }
279 exit(0);
280}
281
282static void
283do_convert_to_pem(Key *k)
284{
285 switch (key_type_plain(k->type)) {
286 case KEY_RSA:
287 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
288 fatal("PEM_write_RSAPublicKey failed");
289 break;
290#if notyet /* OpenSSH 0.9.8 lacks this function */
291 case KEY_DSA:
292 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
293 fatal("PEM_write_DSAPublicKey failed");
294 break;
295#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000296 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000297 default:
298 fatal("%s: unsupported key type %s", __func__, key_type(k));
299 }
300 exit(0);
301}
302
303static void
304do_convert_to(struct passwd *pw)
305{
306 Key *k;
307 struct stat st;
308
309 if (!have_identity)
310 ask_filename(pw, "Enter file in which the key is");
311 if (stat(identity_file, &st) < 0)
312 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
313 if ((k = key_load_public(identity_file, NULL)) == NULL) {
314 if ((k = load_identity(identity_file)) == NULL) {
315 fprintf(stderr, "load failed\n");
316 exit(1);
317 }
318 }
319 if (k->type == KEY_RSA1) {
320 fprintf(stderr, "version 1 keys are not supported\n");
321 exit(1);
322 }
323
324 switch (convert_format) {
325 case FMT_RFC4716:
326 do_convert_to_ssh2(pw, k);
327 break;
328 case FMT_PKCS8:
329 do_convert_to_pkcs8(k);
330 break;
331 case FMT_PEM:
332 do_convert_to_pem(k);
333 break;
334 default:
335 fatal("%s: unknown key format %d", __func__, convert_format);
336 }
337 exit(0);
338}
339
340static void
Damien Miller874d77b2000-10-14 16:23:11 +1100341buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
342{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000343 u_int bignum_bits = buffer_get_int(b);
344 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000345
Damien Miller874d77b2000-10-14 16:23:11 +1100346 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000347 fatal("buffer_get_bignum_bits: input buffer too small: "
348 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100349 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
350 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100351 buffer_consume(b, bytes);
352}
353
Ben Lindstrombba81212001-06-25 05:01:22 +0000354static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000355do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100356{
357 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100358 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100359 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000360 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000361 int magic, rlen, ktype, i1, i2, i3, i4;
362 u_int slen;
363 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100364
365 buffer_init(&b);
366 buffer_append(&b, blob, blen);
367
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100368 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100369 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
370 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
371 buffer_free(&b);
372 return NULL;
373 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000374 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100375 type = buffer_get_string(&b, NULL);
376 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000377 i2 = buffer_get_int(&b);
378 i3 = buffer_get_int(&b);
379 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100380 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100381 if (strcmp(cipher, "none") != 0) {
382 error("unsupported cipher %s", cipher);
383 xfree(cipher);
384 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000385 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100386 return NULL;
387 }
388 xfree(cipher);
389
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000390 if (strstr(type, "dsa")) {
391 ktype = KEY_DSA;
392 } else if (strstr(type, "rsa")) {
393 ktype = KEY_RSA;
394 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100395 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000396 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100397 return NULL;
398 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000399 key = key_new_private(ktype);
400 xfree(type);
401
402 switch (key->type) {
403 case KEY_DSA:
404 buffer_get_bignum_bits(&b, key->dsa->p);
405 buffer_get_bignum_bits(&b, key->dsa->g);
406 buffer_get_bignum_bits(&b, key->dsa->q);
407 buffer_get_bignum_bits(&b, key->dsa->pub_key);
408 buffer_get_bignum_bits(&b, key->dsa->priv_key);
409 break;
410 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100411 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000412 debug("e %lx", e);
413 if (e < 30) {
414 e <<= 8;
415 e += buffer_get_char(&b);
416 debug("e %lx", e);
417 e <<= 8;
418 e += buffer_get_char(&b);
419 debug("e %lx", e);
420 }
421 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000422 buffer_free(&b);
423 key_free(key);
424 return NULL;
425 }
426 buffer_get_bignum_bits(&b, key->rsa->d);
427 buffer_get_bignum_bits(&b, key->rsa->n);
428 buffer_get_bignum_bits(&b, key->rsa->iqmp);
429 buffer_get_bignum_bits(&b, key->rsa->q);
430 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000431 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000432 break;
433 }
Damien Miller874d77b2000-10-14 16:23:11 +1100434 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000435 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000436 error("do_convert_private_ssh2_from_blob: "
437 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100438 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000439
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000440 /* try the key */
441 key_sign(key, &sig, &slen, data, sizeof(data));
442 key_verify(key, sig, slen, data, sizeof(data));
443 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100444 return key;
445}
446
Damien Miller8056a9d2006-03-15 12:05:40 +1100447static int
448get_line(FILE *fp, char *line, size_t len)
449{
450 int c;
451 size_t pos = 0;
452
453 line[0] = '\0';
454 while ((c = fgetc(fp)) != EOF) {
455 if (pos >= len - 1) {
456 fprintf(stderr, "input line too long.\n");
457 exit(1);
458 }
Damien Miller90967402006-03-26 14:07:26 +1100459 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100460 case '\r':
461 c = fgetc(fp);
462 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
463 fprintf(stderr, "unget: %s\n", strerror(errno));
464 exit(1);
465 }
466 return pos;
467 case '\n':
468 return pos;
469 }
470 line[pos++] = c;
471 line[pos] = '\0';
472 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100473 /* We reached EOF */
474 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100475}
476
Ben Lindstrombba81212001-06-25 05:01:22 +0000477static void
Damien Miller44b25042010-07-02 13:35:01 +1000478do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000479{
Damien Millereba71ba2000-04-29 23:57:08 +1000480 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000481 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100482 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000483 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000484 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000485 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000486 FILE *fp;
487
Damien Millerba3420a2010-06-26 09:39:07 +1000488 if ((fp = fopen(identity_file, "r")) == NULL)
489 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000490 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100491 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
492 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000493 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000494 if (strncmp(line, "----", 4) == 0 ||
495 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100496 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000497 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000498 if (strstr(line, " END ") != NULL) {
499 break;
500 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000501 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000502 continue;
503 }
Damien Miller30c3d422000-05-09 11:02:59 +1000504 if (escaped) {
505 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000506 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000507 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000508 }
Damien Millereba71ba2000-04-29 23:57:08 +1000509 strlcat(encoded, line, sizeof(encoded));
510 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000511 len = strlen(encoded);
512 if (((len % 4) == 3) &&
513 (encoded[len-1] == '=') &&
514 (encoded[len-2] == '=') &&
515 (encoded[len-3] == '='))
516 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100517 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000518 if (blen < 0) {
519 fprintf(stderr, "uudecode failed.\n");
520 exit(1);
521 }
Damien Miller44b25042010-07-02 13:35:01 +1000522 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100523 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100524 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000525 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100526 fprintf(stderr, "decode blob failed.\n");
527 exit(1);
528 }
Damien Miller44b25042010-07-02 13:35:01 +1000529 fclose(fp);
530}
531
532static void
533do_convert_from_pkcs8(Key **k, int *private)
534{
535 EVP_PKEY *pubkey;
536 FILE *fp;
537
538 if ((fp = fopen(identity_file, "r")) == NULL)
539 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
540 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
541 fatal("%s: %s is not a recognised public key format", __func__,
542 identity_file);
543 }
544 fclose(fp);
545 switch (EVP_PKEY_type(pubkey->type)) {
546 case EVP_PKEY_RSA:
547 *k = key_new(KEY_UNSPEC);
548 (*k)->type = KEY_RSA;
549 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
550 break;
551 case EVP_PKEY_DSA:
552 *k = key_new(KEY_UNSPEC);
553 (*k)->type = KEY_DSA;
554 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
555 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000556#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000557 case EVP_PKEY_EC:
558 *k = key_new(KEY_UNSPEC);
559 (*k)->type = KEY_ECDSA;
560 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
Damien Millerb472a902010-11-05 10:19:49 +1100561 (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000562 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000563#endif
Damien Miller44b25042010-07-02 13:35:01 +1000564 default:
565 fatal("%s: unsupported pubkey type %d", __func__,
566 EVP_PKEY_type(pubkey->type));
567 }
568 EVP_PKEY_free(pubkey);
569 return;
570}
571
572static void
573do_convert_from_pem(Key **k, int *private)
574{
575 FILE *fp;
576 RSA *rsa;
577#ifdef notyet
578 DSA *dsa;
579#endif
580
581 if ((fp = fopen(identity_file, "r")) == NULL)
582 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
583 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
584 *k = key_new(KEY_UNSPEC);
585 (*k)->type = KEY_RSA;
586 (*k)->rsa = rsa;
587 fclose(fp);
588 return;
589 }
590#if notyet /* OpenSSH 0.9.8 lacks this function */
591 rewind(fp);
592 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
593 *k = key_new(KEY_UNSPEC);
594 (*k)->type = KEY_DSA;
595 (*k)->dsa = dsa;
596 fclose(fp);
597 return;
598 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000599 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000600#endif
601 fatal("%s: unrecognised raw private key format", __func__);
602}
603
604static void
605do_convert_from(struct passwd *pw)
606{
607 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000608 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000609 struct stat st;
610
611 if (!have_identity)
612 ask_filename(pw, "Enter file in which the key is");
613 if (stat(identity_file, &st) < 0)
614 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
615
616 switch (convert_format) {
617 case FMT_RFC4716:
618 do_convert_from_ssh2(pw, &k, &private);
619 break;
620 case FMT_PKCS8:
621 do_convert_from_pkcs8(&k, &private);
622 break;
623 case FMT_PEM:
624 do_convert_from_pem(&k, &private);
625 break;
626 default:
627 fatal("%s: unknown key format %d", __func__, convert_format);
628 }
629
630 if (!private)
631 ok = key_write(k, stdout);
632 if (ok)
633 fprintf(stdout, "\n");
634 else {
635 switch (k->type) {
636 case KEY_DSA:
637 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
638 NULL, 0, NULL, NULL);
639 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000640#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000641 case KEY_ECDSA:
642 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
643 NULL, 0, NULL, NULL);
644 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000645#endif
Damien Miller44b25042010-07-02 13:35:01 +1000646 case KEY_RSA:
647 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
648 NULL, 0, NULL, NULL);
649 break;
650 default:
651 fatal("%s: unsupported key type %s", __func__,
652 key_type(k));
653 }
654 }
655
Damien Miller874d77b2000-10-14 16:23:11 +1100656 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100657 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100658 exit(1);
659 }
Damien Millereba71ba2000-04-29 23:57:08 +1000660 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000661 exit(0);
662}
663
Ben Lindstrombba81212001-06-25 05:01:22 +0000664static void
Damien Millereba71ba2000-04-29 23:57:08 +1000665do_print_public(struct passwd *pw)
666{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000667 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000668 struct stat st;
669
670 if (!have_identity)
671 ask_filename(pw, "Enter file in which the key is");
672 if (stat(identity_file, &st) < 0) {
673 perror(identity_file);
674 exit(1);
675 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000676 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000677 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000678 fprintf(stderr, "load failed\n");
679 exit(1);
680 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000681 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000682 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000683 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000684 fprintf(stdout, "\n");
685 exit(0);
686}
687
Ben Lindstromcd392282001-07-04 03:44:03 +0000688static void
Damien Miller757f34e2010-08-05 13:05:31 +1000689do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000690{
Damien Miller7ea845e2010-02-12 09:21:02 +1100691#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000692 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100693 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000694
Damien Miller7ea845e2010-02-12 09:21:02 +1100695 pkcs11_init(0);
696 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
697 if (nkeys <= 0)
698 fatal("cannot read public key from pkcs11");
699 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000700 key_write(keys[i], stdout);
701 key_free(keys[i]);
702 fprintf(stdout, "\n");
703 }
704 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100705 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000706 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100707#else
708 fatal("no pkcs11 support");
709#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000710}
Ben Lindstromcd392282001-07-04 03:44:03 +0000711
Ben Lindstrombba81212001-06-25 05:01:22 +0000712static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100713do_fingerprint(struct passwd *pw)
714{
Damien Miller98c7ad62000-03-09 21:27:49 +1100715 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000716 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000717 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100718 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000719 enum fp_rep rep;
720 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100721 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100722
Ben Lindstromd0fca422001-03-26 13:44:06 +0000723 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
724 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000725
Damien Miller95def091999-11-25 00:26:21 +1100726 if (!have_identity)
727 ask_filename(pw, "Enter file in which the key is");
728 if (stat(identity_file, &st) < 0) {
729 perror(identity_file);
730 exit(1);
731 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000732 public = key_load_public(identity_file, &comment);
733 if (public != NULL) {
734 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100735 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000736 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
737 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000738 if (log_level >= SYSLOG_LEVEL_VERBOSE)
739 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100740 key_free(public);
741 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000742 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000743 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100744 exit(0);
745 }
Damien Miller40b59852006-06-13 13:00:25 +1000746 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000747 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000748 comment = NULL;
749 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100750
Damien Millerba3420a2010-06-26 09:39:07 +1000751 if ((f = fopen(identity_file, "r")) == NULL)
752 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100753
Damien Millerba3420a2010-06-26 09:39:07 +1000754 while (fgets(line, sizeof(line), f)) {
755 if ((cp = strchr(line, '\n')) == NULL) {
756 error("line %d too long: %.40s...",
757 num + 1, line);
758 skip = 1;
759 continue;
Damien Miller95def091999-11-25 00:26:21 +1100760 }
Damien Millerba3420a2010-06-26 09:39:07 +1000761 num++;
762 if (skip) {
763 skip = 0;
764 continue;
765 }
766 *cp = '\0';
767
768 /* Skip leading whitespace, empty and comment lines. */
769 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
770 ;
771 if (!*cp || *cp == '\n' || *cp == '#')
772 continue;
773 i = strtol(cp, &ep, 10);
774 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
775 int quoted = 0;
776 comment = cp;
777 for (; *cp && (quoted || (*cp != ' ' &&
778 *cp != '\t')); cp++) {
779 if (*cp == '\\' && cp[1] == '"')
780 cp++; /* Skip both */
781 else if (*cp == '"')
782 quoted = !quoted;
783 }
784 if (!*cp)
785 continue;
786 *cp++ = '\0';
787 }
788 ep = cp;
789 public = key_new(KEY_RSA1);
790 if (key_read(public, &cp) != 1) {
791 cp = ep;
792 key_free(public);
793 public = key_new(KEY_UNSPEC);
794 if (key_read(public, &cp) != 1) {
795 key_free(public);
796 continue;
797 }
798 }
799 comment = *cp ? cp : comment;
800 fp = key_fingerprint(public, fptype, rep);
801 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
802 printf("%u %s %s (%s)\n", key_size(public), fp,
803 comment ? comment : "no comment", key_type(public));
804 if (log_level >= SYSLOG_LEVEL_VERBOSE)
805 printf("%s\n", ra);
806 xfree(ra);
807 xfree(fp);
808 key_free(public);
809 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100810 }
Damien Millerba3420a2010-06-26 09:39:07 +1000811 fclose(f);
812
Damien Miller98c7ad62000-03-09 21:27:49 +1100813 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100814 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100815 exit(1);
816 }
Damien Miller95def091999-11-25 00:26:21 +1100817 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100818}
819
Damien Miller4b42d7f2005-03-01 21:48:35 +1100820static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100821printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100822{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000823 if (print_fingerprint) {
824 enum fp_rep rep;
825 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000826 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000827
828 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
829 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
830 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100831 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000832 printf("%u %s %s (%s)\n", key_size(public), fp, name,
833 key_type(public));
834 if (log_level >= SYSLOG_LEVEL_VERBOSE)
835 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000836 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000837 xfree(fp);
838 } else {
839 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
840 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100841 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000842 if (!key_write(public, f))
843 fatal("key_write failed");
844 fprintf(f, "\n");
845 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100846}
847
848static void
849do_known_hosts(struct passwd *pw, const char *name)
850{
851 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100852 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100853 char *cp, *cp2, *kp, *kp2;
854 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100855 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100856 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100857
858 if (!have_identity) {
859 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
860 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
861 sizeof(identity_file))
862 fatal("Specified known hosts path too long");
863 xfree(cp);
864 have_identity = 1;
865 }
866 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +1000867 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +1100868
869 /*
870 * Find hosts goes to stdout, hash and deletions happen in-place
871 * A corner case is ssh-keygen -HF foo, which should go to stdout
872 */
873 if (!find_host && (hash_hosts || delete_host)) {
874 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
875 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
876 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
877 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
878 fatal("known_hosts path too long");
879 umask(077);
880 if ((c = mkstemp(tmp)) == -1)
881 fatal("mkstemp: %s", strerror(errno));
882 if ((out = fdopen(c, "w")) == NULL) {
883 c = errno;
884 unlink(tmp);
885 fatal("fdopen: %s", strerror(c));
886 }
887 inplace = 1;
888 }
889
890 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000891 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100892 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100893 skip = 1;
894 invalid = 1;
895 continue;
896 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000897 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100898 if (skip) {
899 skip = 0;
900 continue;
901 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000902 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100903
904 /* Skip leading whitespace, empty and comment lines. */
905 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
906 ;
907 if (!*cp || *cp == '\n' || *cp == '#') {
908 if (inplace)
909 fprintf(out, "%s\n", cp);
910 continue;
911 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100912 /* Check whether this is a CA key */
913 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
914 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
915 cp[sizeof(CA_MARKER) - 1] == '\t')) {
916 ca = 1;
917 cp += sizeof(CA_MARKER);
918 } else
919 ca = 0;
920
Damien Miller4b42d7f2005-03-01 21:48:35 +1100921 /* Find the end of the host name portion. */
922 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
923 ;
Damien Miller0a80ca12010-02-27 07:55:05 +1100924
Damien Miller4b42d7f2005-03-01 21:48:35 +1100925 if (*kp == '\0' || *(kp + 1) == '\0') {
926 error("line %d missing key: %.40s...",
927 num, line);
928 invalid = 1;
929 continue;
930 }
931 *kp++ = '\0';
932 kp2 = kp;
933
Damien Miller0a80ca12010-02-27 07:55:05 +1100934 pub = key_new(KEY_RSA1);
935 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100936 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +1100937 key_free(pub);
938 pub = key_new(KEY_UNSPEC);
939 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100940 error("line %d invalid key: %.40s...",
941 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100942 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100943 invalid = 1;
944 continue;
945 }
946 }
947
948 if (*cp == HASH_DELIM) {
949 if (find_host || delete_host) {
950 cp2 = host_hash(name, cp, strlen(cp));
951 if (cp2 == NULL) {
952 error("line %d: invalid hashed "
953 "name: %.64s...", num, line);
954 invalid = 1;
955 continue;
956 }
957 c = (strcmp(cp2, cp) == 0);
958 if (find_host && c) {
959 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100960 "line %d type %s%s\n", name,
961 num, key_type(pub),
962 ca ? " (CA key)" : "");
963 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100964 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100965 if (delete_host && !c && !ca)
966 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100967 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +1100968 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100969 } else {
970 if (find_host || delete_host) {
971 c = (match_hostname(name, cp,
972 strlen(cp)) == 1);
973 if (find_host && c) {
974 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100975 "line %d type %s%s\n", name,
976 num, key_type(pub),
977 ca ? " (CA key)" : "");
978 printhost(out, name, pub,
979 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100980 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100981 if (delete_host && !c && !ca)
982 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100983 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100984 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100985 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100986 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100987 if (ca) {
988 fprintf(stderr, "Warning: "
989 "ignoring CA key for host: "
990 "%.64s\n", cp2);
991 printhost(out, cp2, pub, ca, 0);
992 } else if (strcspn(cp2, "*?!") !=
993 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +1100994 fprintf(stderr, "Warning: "
995 "ignoring host name with "
996 "metacharacters: %.64s\n",
997 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +1100998 printhost(out, cp2, pub, ca, 0);
999 } else
1000 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +11001001 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001002 has_unhashed = 1;
1003 }
1004 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001005 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001006 }
1007 fclose(in);
1008
1009 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001010 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001011 identity_file);
1012 if (inplace) {
1013 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001014 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001015 fclose(out);
1016 unlink(tmp);
1017 }
1018 exit(1);
1019 }
1020
1021 if (inplace) {
1022 fclose(out);
1023
1024 /* Backup existing file */
1025 if (unlink(old) == -1 && errno != ENOENT)
1026 fatal("unlink %.100s: %s", old, strerror(errno));
1027 if (link(identity_file, old) == -1)
1028 fatal("link %.100s to %.100s: %s", identity_file, old,
1029 strerror(errno));
1030 /* Move new one into place */
1031 if (rename(tmp, identity_file) == -1) {
1032 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1033 strerror(errno));
1034 unlink(tmp);
1035 unlink(old);
1036 exit(1);
1037 }
1038
1039 fprintf(stderr, "%s updated.\n", identity_file);
1040 fprintf(stderr, "Original contents retained as %s\n", old);
1041 if (has_unhashed) {
1042 fprintf(stderr, "WARNING: %s contains unhashed "
1043 "entries\n", old);
1044 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001045 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001046 }
1047 }
1048
1049 exit(0);
1050}
1051
Damien Miller95def091999-11-25 00:26:21 +11001052/*
1053 * Perform changing a passphrase. The argument is the passwd structure
1054 * for the current user.
1055 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001056static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001057do_change_passphrase(struct passwd *pw)
1058{
Damien Miller95def091999-11-25 00:26:21 +11001059 char *comment;
1060 char *old_passphrase, *passphrase1, *passphrase2;
1061 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001062 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001063
Damien Miller95def091999-11-25 00:26:21 +11001064 if (!have_identity)
1065 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001066 if (stat(identity_file, &st) < 0) {
1067 perror(identity_file);
1068 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069 }
Damien Miller95def091999-11-25 00:26:21 +11001070 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001071 private = key_load_private(identity_file, "", &comment);
1072 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001073 if (identity_passphrase)
1074 old_passphrase = xstrdup(identity_passphrase);
1075 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001076 old_passphrase =
1077 read_passphrase("Enter old passphrase: ",
1078 RP_ALLOW_STDIN);
1079 private = key_load_private(identity_file, old_passphrase,
1080 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001081 memset(old_passphrase, 0, strlen(old_passphrase));
1082 xfree(old_passphrase);
1083 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001084 printf("Bad passphrase.\n");
1085 exit(1);
1086 }
Damien Miller95def091999-11-25 00:26:21 +11001087 }
1088 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001089
Damien Miller95def091999-11-25 00:26:21 +11001090 /* Ask the new passphrase (twice). */
1091 if (identity_new_passphrase) {
1092 passphrase1 = xstrdup(identity_new_passphrase);
1093 passphrase2 = NULL;
1094 } else {
1095 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001096 read_passphrase("Enter new passphrase (empty for no "
1097 "passphrase): ", RP_ALLOW_STDIN);
1098 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001099 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001100
1101 /* Verify that they are the same. */
1102 if (strcmp(passphrase1, passphrase2) != 0) {
1103 memset(passphrase1, 0, strlen(passphrase1));
1104 memset(passphrase2, 0, strlen(passphrase2));
1105 xfree(passphrase1);
1106 xfree(passphrase2);
1107 printf("Pass phrases do not match. Try again.\n");
1108 exit(1);
1109 }
1110 /* Destroy the other copy. */
1111 memset(passphrase2, 0, strlen(passphrase2));
1112 xfree(passphrase2);
1113 }
1114
1115 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001116 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001117 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001118 memset(passphrase1, 0, strlen(passphrase1));
1119 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001120 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001121 xfree(comment);
1122 exit(1);
1123 }
1124 /* Destroy the passphrase and the copy of the key in memory. */
1125 memset(passphrase1, 0, strlen(passphrase1));
1126 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001127 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +11001128 xfree(comment);
1129
1130 printf("Your identification has been saved with the new passphrase.\n");
1131 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132}
1133
Damien Miller37876e92003-05-15 10:19:46 +10001134/*
1135 * Print the SSHFP RR.
1136 */
Damien Millercb314822006-03-26 13:48:01 +11001137static int
1138do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001139{
1140 Key *public;
1141 char *comment = NULL;
1142 struct stat st;
1143
Damien Millercb314822006-03-26 13:48:01 +11001144 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +10001145 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +11001146 if (stat(fname, &st) < 0) {
1147 if (errno == ENOENT)
1148 return 0;
1149 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001150 exit(1);
1151 }
Damien Millercb314822006-03-26 13:48:01 +11001152 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001153 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001154 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001155 key_free(public);
1156 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +11001157 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001158 }
1159 if (comment)
1160 xfree(comment);
1161
Damien Millercb314822006-03-26 13:48:01 +11001162 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001163 exit(1);
1164}
Damien Miller37876e92003-05-15 10:19:46 +10001165
Damien Miller95def091999-11-25 00:26:21 +11001166/*
1167 * Change the comment of a private key file.
1168 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001169static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170do_change_comment(struct passwd *pw)
1171{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001172 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001173 Key *private;
1174 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001175 struct stat st;
1176 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001177 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178
Damien Miller95def091999-11-25 00:26:21 +11001179 if (!have_identity)
1180 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001181 if (stat(identity_file, &st) < 0) {
1182 perror(identity_file);
1183 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001185 private = key_load_private(identity_file, "", &comment);
1186 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001187 if (identity_passphrase)
1188 passphrase = xstrdup(identity_passphrase);
1189 else if (identity_new_passphrase)
1190 passphrase = xstrdup(identity_new_passphrase);
1191 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001192 passphrase = read_passphrase("Enter passphrase: ",
1193 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001194 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001195 private = key_load_private(identity_file, passphrase, &comment);
1196 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001197 memset(passphrase, 0, strlen(passphrase));
1198 xfree(passphrase);
1199 printf("Bad passphrase.\n");
1200 exit(1);
1201 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001202 } else {
1203 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001204 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001205 if (private->type != KEY_RSA1) {
1206 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1207 key_free(private);
1208 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001209 }
Damien Miller95def091999-11-25 00:26:21 +11001210 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211
Damien Miller95def091999-11-25 00:26:21 +11001212 if (identity_comment) {
1213 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1214 } else {
1215 printf("Enter new comment: ");
1216 fflush(stdout);
1217 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1218 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001219 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001220 exit(1);
1221 }
Damien Miller14b017d2007-09-17 16:09:15 +10001222 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001223 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001224
Damien Miller95def091999-11-25 00:26:21 +11001225 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001226 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001227 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001228 memset(passphrase, 0, strlen(passphrase));
1229 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001230 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001231 xfree(comment);
1232 exit(1);
1233 }
Damien Miller95def091999-11-25 00:26:21 +11001234 memset(passphrase, 0, strlen(passphrase));
1235 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001236 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001237 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001238
Damien Miller95def091999-11-25 00:26:21 +11001239 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001240 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1241 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001242 printf("Could not save your public key in %s\n", identity_file);
1243 exit(1);
1244 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001245 f = fdopen(fd, "w");
1246 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001247 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001248 exit(1);
1249 }
Damien Millereba71ba2000-04-29 23:57:08 +10001250 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001251 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001252 key_free(public);
1253 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001254 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255
Damien Miller95def091999-11-25 00:26:21 +11001256 xfree(comment);
1257
1258 printf("The comment in your key file has been changed.\n");
1259 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260}
1261
Damien Miller0a80ca12010-02-27 07:55:05 +11001262static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001263fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001264{
1265 char from[32], to[32];
1266 static char ret[64];
1267 time_t tt;
1268 struct tm *tm;
1269
1270 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001271 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001272 return "forever";
1273
Damien Millerf2b70ca2010-03-05 07:39:35 +11001274 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001275 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001276 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001277 tm = localtime(&tt);
1278 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1279 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001280 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001281 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001282 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001283 tm = localtime(&tt);
1284 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1285 }
1286
Damien Millerf2b70ca2010-03-05 07:39:35 +11001287 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001288 snprintf(ret, sizeof(ret), "before %s", to);
1289 return ret;
1290 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001291 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001292 snprintf(ret, sizeof(ret), "after %s", from);
1293 return ret;
1294 }
1295
1296 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1297 return ret;
1298}
1299
1300static void
Damien Miller4e270b02010-04-16 15:56:21 +10001301add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001302{
1303 debug3("%s: %s", __func__, name);
1304 buffer_put_cstring(c, name);
1305 buffer_put_string(c, NULL, 0);
1306}
1307
1308static void
Damien Miller4e270b02010-04-16 15:56:21 +10001309add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001310{
1311 Buffer b;
1312
1313 debug3("%s: %s=%s", __func__, name, value);
1314 buffer_init(&b);
1315 buffer_put_cstring(&b, value);
1316
1317 buffer_put_cstring(c, name);
1318 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1319
1320 buffer_free(&b);
1321}
1322
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001323#define OPTIONS_CRITICAL 1
1324#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001325static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001326prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001327{
Damien Miller0a80ca12010-02-27 07:55:05 +11001328 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001329 if ((which & OPTIONS_CRITICAL) != 0 &&
1330 certflags_command != NULL)
1331 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001332 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1333 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001334 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001335 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1336 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001337 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001338 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1339 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001340 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001341 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1342 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001343 add_flag_option(c, "permit-user-rc");
Damien Miller1da63882010-08-05 13:03:51 +10001344 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1345 (certflags_flags & CERTOPT_X_FWD) != 0)
1346 add_flag_option(c, "permit-X11-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001347 if ((which & OPTIONS_CRITICAL) != 0 &&
1348 certflags_src_addr != NULL)
1349 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001350}
1351
Damien Miller757f34e2010-08-05 13:05:31 +10001352static Key *
1353load_pkcs11_key(char *path)
1354{
1355#ifdef ENABLE_PKCS11
1356 Key **keys = NULL, *public, *private = NULL;
1357 int i, nkeys;
1358
1359 if ((public = key_load_public(path, NULL)) == NULL)
1360 fatal("Couldn't load CA public key \"%s\"", path);
1361
1362 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1363 debug3("%s: %d keys", __func__, nkeys);
1364 if (nkeys <= 0)
1365 fatal("cannot read public key from pkcs11");
1366 for (i = 0; i < nkeys; i++) {
1367 if (key_equal_public(public, keys[i])) {
1368 private = keys[i];
1369 continue;
1370 }
1371 key_free(keys[i]);
1372 }
1373 xfree(keys);
1374 key_free(public);
1375 return private;
1376#else
1377 fatal("no pkcs11 support");
1378#endif /* ENABLE_PKCS11 */
1379}
1380
Damien Miller0a80ca12010-02-27 07:55:05 +11001381static void
1382do_ca_sign(struct passwd *pw, int argc, char **argv)
1383{
1384 int i, fd;
1385 u_int n;
1386 Key *ca, *public;
1387 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1388 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001389 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001390
Damien Miller4e270b02010-04-16 15:56:21 +10001391 if (key_type_name != NULL) {
1392 switch (key_type_from_name(key_type_name)) {
1393 case KEY_RSA_CERT_V00:
1394 case KEY_DSA_CERT_V00:
1395 v00 = 1;
1396 break;
1397 case KEY_UNSPEC:
1398 if (strcasecmp(key_type_name, "v00") == 0) {
1399 v00 = 1;
1400 break;
1401 } else if (strcasecmp(key_type_name, "v01") == 0)
1402 break;
1403 /* FALLTHROUGH */
1404 default:
1405 fprintf(stderr, "unknown key type %s\n", key_type_name);
1406 exit(1);
1407 }
1408 }
1409
Damien Miller757f34e2010-08-05 13:05:31 +10001410 pkcs11_init(1);
1411 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1412 if (pkcs11provider != NULL) {
1413 if ((ca = load_pkcs11_key(tmp)) == NULL)
1414 fatal("No PKCS#11 key matching %s found", ca_key_path);
1415 } else if ((ca = load_identity(tmp)) == NULL)
1416 fatal("Couldn't load CA key \"%s\"", tmp);
1417 xfree(tmp);
1418
Damien Miller0a80ca12010-02-27 07:55:05 +11001419 for (i = 0; i < argc; i++) {
1420 /* Split list of principals */
1421 n = 0;
1422 if (cert_principals != NULL) {
1423 otmp = tmp = xstrdup(cert_principals);
1424 plist = NULL;
1425 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1426 plist = xrealloc(plist, n + 1, sizeof(*plist));
1427 if (*(plist[n] = xstrdup(cp)) == '\0')
1428 fatal("Empty principal name");
1429 }
1430 xfree(otmp);
1431 }
1432
1433 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1434 if ((public = key_load_public(tmp, &comment)) == NULL)
1435 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001436 if (public->type != KEY_RSA && public->type != KEY_DSA &&
1437 public->type != KEY_ECDSA)
Damien Miller0a80ca12010-02-27 07:55:05 +11001438 fatal("%s: key \"%s\" type %s cannot be certified",
1439 __func__, tmp, key_type(public));
1440
1441 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001442 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001443 fatal("Could not upgrade key %s to certificate", tmp);
1444 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001445 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001446 public->cert->key_id = xstrdup(cert_key_id);
1447 public->cert->nprincipals = n;
1448 public->cert->principals = plist;
1449 public->cert->valid_after = cert_valid_from;
1450 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001451 if (v00) {
1452 prepare_options_buf(&public->cert->critical,
1453 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1454 } else {
1455 prepare_options_buf(&public->cert->critical,
1456 OPTIONS_CRITICAL);
1457 prepare_options_buf(&public->cert->extensions,
1458 OPTIONS_EXTENSIONS);
1459 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001460 public->cert->signature_key = key_from_private(ca);
1461
1462 if (key_certify(public, ca) != 0)
1463 fatal("Couldn't not certify key %s", tmp);
1464
1465 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1466 *cp = '\0';
1467 xasprintf(&out, "%s-cert.pub", tmp);
1468 xfree(tmp);
1469
1470 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1471 fatal("Could not open \"%s\" for writing: %s", out,
1472 strerror(errno));
1473 if ((f = fdopen(fd, "w")) == NULL)
1474 fatal("%s: fdopen: %s", __func__, strerror(errno));
1475 if (!key_write(public, f))
1476 fatal("Could not write certified key to %s", out);
1477 fprintf(f, " %s\n", comment);
1478 fclose(f);
1479
Damien Miller4e270b02010-04-16 15:56:21 +10001480 if (!quiet) {
1481 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1482 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001483 out, public->cert->key_id,
1484 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001485 cert_principals != NULL ? " for " : "",
1486 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001487 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001488 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001489
1490 key_free(public);
1491 xfree(out);
1492 }
Damien Miller757f34e2010-08-05 13:05:31 +10001493 pkcs11_terminate();
Damien Miller0a80ca12010-02-27 07:55:05 +11001494 exit(0);
1495}
1496
1497static u_int64_t
1498parse_relative_time(const char *s, time_t now)
1499{
1500 int64_t mul, secs;
1501
1502 mul = *s == '-' ? -1 : 1;
1503
1504 if ((secs = convtime(s + 1)) == -1)
1505 fatal("Invalid relative certificate time %s", s);
1506 if (mul == -1 && secs > now)
1507 fatal("Certificate time %s cannot be represented", s);
1508 return now + (u_int64_t)(secs * mul);
1509}
1510
1511static u_int64_t
1512parse_absolute_time(const char *s)
1513{
1514 struct tm tm;
1515 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001516 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001517
Damien Miller2ca342b2010-03-03 12:14:15 +11001518 /*
1519 * POSIX strptime says "The application shall ensure that there
1520 * is white-space or other non-alphanumeric characters between
1521 * any two conversion specifications" so arrange things this way.
1522 */
1523 switch (strlen(s)) {
1524 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001525 fmt = "%Y-%m-%d";
1526 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001527 break;
1528 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001529 fmt = "%Y-%m-%dT%H:%M:%S";
1530 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001531 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1532 break;
1533 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001534 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001535 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001536
1537 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001538 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001539 fatal("Invalid certificate time %s", s);
1540 if ((tt = mktime(&tm)) < 0)
1541 fatal("Certificate time %s cannot be represented", s);
1542 return (u_int64_t)tt;
1543}
1544
1545static void
1546parse_cert_times(char *timespec)
1547{
1548 char *from, *to;
1549 time_t now = time(NULL);
1550 int64_t secs;
1551
1552 /* +timespec relative to now */
1553 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1554 if ((secs = convtime(timespec + 1)) == -1)
1555 fatal("Invalid relative certificate life %s", timespec);
1556 cert_valid_to = now + secs;
1557 /*
1558 * Backdate certificate one minute to avoid problems on hosts
1559 * with poorly-synchronised clocks.
1560 */
1561 cert_valid_from = ((now - 59)/ 60) * 60;
1562 return;
1563 }
1564
1565 /*
1566 * from:to, where
1567 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1568 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1569 */
1570 from = xstrdup(timespec);
1571 to = strchr(from, ':');
1572 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001573 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001574 *to++ = '\0';
1575
1576 if (*from == '-' || *from == '+')
1577 cert_valid_from = parse_relative_time(from, now);
1578 else
1579 cert_valid_from = parse_absolute_time(from);
1580
1581 if (*to == '-' || *to == '+')
1582 cert_valid_to = parse_relative_time(to, cert_valid_from);
1583 else
1584 cert_valid_to = parse_absolute_time(to);
1585
1586 if (cert_valid_to <= cert_valid_from)
1587 fatal("Empty certificate validity interval");
1588 xfree(from);
1589}
1590
1591static void
Damien Miller4e270b02010-04-16 15:56:21 +10001592add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001593{
1594 char *val;
1595
1596 if (strcmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001597 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001598 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001599 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001600 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001601 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001602 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001603 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001604 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001605 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001606 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001607 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001608 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001609 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001610 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001611 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001612 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001613 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001614 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001615 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001616 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001617 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001618 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1619 val = opt + 14;
1620 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001621 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001622 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001623 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001624 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001625 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1626 val = opt + 15;
1627 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001628 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001629 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001630 fatal("source-address already specified");
1631 if (addr_match_cidr_list(NULL, val) != 0)
1632 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001633 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001634 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001635 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001636}
1637
Ben Lindstrombba81212001-06-25 05:01:22 +00001638static void
Damien Millerd834d352010-06-26 09:48:02 +10001639show_options(const Buffer *optbuf, int v00, int in_critical)
1640{
1641 u_char *name, *data;
1642 u_int dlen;
1643 Buffer options, option;
1644
1645 buffer_init(&options);
1646 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1647
1648 buffer_init(&option);
1649 while (buffer_len(&options) != 0) {
1650 name = buffer_get_string(&options, NULL);
1651 data = buffer_get_string_ptr(&options, &dlen);
1652 buffer_append(&option, data, dlen);
1653 printf(" %s", name);
1654 if ((v00 || !in_critical) &&
1655 (strcmp(name, "permit-X11-forwarding") == 0 ||
1656 strcmp(name, "permit-agent-forwarding") == 0 ||
1657 strcmp(name, "permit-port-forwarding") == 0 ||
1658 strcmp(name, "permit-pty") == 0 ||
1659 strcmp(name, "permit-user-rc") == 0))
1660 printf("\n");
1661 else if ((v00 || in_critical) &&
1662 (strcmp(name, "force-command") == 0 ||
1663 strcmp(name, "source-address") == 0)) {
1664 data = buffer_get_string(&option, NULL);
1665 printf(" %s\n", data);
1666 xfree(data);
1667 } else {
1668 printf(" UNKNOWN OPTION (len %u)\n",
1669 buffer_len(&option));
1670 buffer_clear(&option);
1671 }
1672 xfree(name);
1673 if (buffer_len(&option) != 0)
1674 fatal("Option corrupt: extra data at end");
1675 }
1676 buffer_free(&option);
1677 buffer_free(&options);
1678}
1679
1680static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001681do_show_cert(struct passwd *pw)
1682{
1683 Key *key;
1684 struct stat st;
1685 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001686 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001687
1688 if (!have_identity)
1689 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001690 if (stat(identity_file, &st) < 0)
1691 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001692 if ((key = key_load_public(identity_file, NULL)) == NULL)
1693 fatal("%s is not a public key", identity_file);
1694 if (!key_is_cert(key))
1695 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001696 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1697
Damien Millerf2b70ca2010-03-05 07:39:35 +11001698 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1699 ca_fp = key_fingerprint(key->cert->signature_key,
1700 SSH_FP_MD5, SSH_FP_HEX);
1701
1702 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001703 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1704 key_cert_type(key));
1705 printf(" Public key: %s %s\n", key_type(key), key_fp);
1706 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001707 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001708 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001709 if (!v00) {
1710 printf(" Serial: %llu\n",
1711 (unsigned long long)key->cert->serial);
1712 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001713 printf(" Valid: %s\n",
1714 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1715 printf(" Principals: ");
1716 if (key->cert->nprincipals == 0)
1717 printf("(none)\n");
1718 else {
1719 for (i = 0; i < key->cert->nprincipals; i++)
1720 printf("\n %s",
1721 key->cert->principals[i]);
1722 printf("\n");
1723 }
Damien Miller4e270b02010-04-16 15:56:21 +10001724 printf(" Critical Options: ");
1725 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001726 printf("(none)\n");
1727 else {
1728 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001729 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001730 }
Damien Miller4e270b02010-04-16 15:56:21 +10001731 if (!v00) {
1732 printf(" Extensions: ");
1733 if (buffer_len(&key->cert->extensions) == 0)
1734 printf("(none)\n");
1735 else {
1736 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001737 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001738 }
1739 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001740 exit(0);
1741}
1742
1743static void
Damien Miller431f66b1999-11-21 18:31:57 +11001744usage(void)
1745{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001746 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001747 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001748 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001749 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001750 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001751 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001752 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001753#ifdef ENABLE_PKCS11
1754 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1755#endif
Damien Miller44b25042010-07-02 13:35:01 +10001756 fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001757 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1758 fprintf(stderr, " -f filename Filename of the key file.\n");
1759 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1760 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1761 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001762 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1763 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Damien Miller44b25042010-07-02 13:35:01 +10001764 fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001765 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001766 fprintf(stderr, " -l Show fingerprint of key file.\n");
1767 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001768 fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001769 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001770 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller1f181422010-04-18 08:08:03 +10001771 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001772 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1773 fprintf(stderr, " -p Change passphrase of private key file.\n");
1774 fprintf(stderr, " -q Quiet.\n");
1775 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1776 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1777 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001778 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001779 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1780 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001781 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001782 fprintf(stderr, " -v Verbose.\n");
1783 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1784 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001785 fprintf(stderr, " -z serial Specify a serial number.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001786
Damien Miller95def091999-11-25 00:26:21 +11001787 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001788}
1789
Damien Miller95def091999-11-25 00:26:21 +11001790/*
1791 * Main program for key management.
1792 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001793int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001794main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001795{
Damien Millera41c8b12002-01-22 23:05:08 +11001796 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller757f34e2010-08-05 13:05:31 +10001797 char out_file[MAXPATHLEN], *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001798 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001799 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001800 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001801 int opt, type, fd;
Damien Millerbebbb7e2010-05-10 11:54:38 +10001802 u_int maxbits;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001803 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001804 int do_gen_candidates = 0, do_screen_candidates = 0;
1805 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001806 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001807 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001808
Damien Miller95def091999-11-25 00:26:21 +11001809 extern int optind;
1810 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001811
Darren Tuckerce321d82005-10-03 18:11:24 +10001812 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1813 sanitise_stdfd();
1814
Darren Tucker9ac56e92007-01-14 10:19:59 +11001815 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001816
Damien Miller4314c2b2010-09-10 11:12:09 +10001817 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001818 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001819
Kevin Steves3a881912002-07-20 19:05:40 +00001820 init_rng();
1821 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001822
Damien Miller5428f641999-11-25 11:54:57 +11001823 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001824 pw = getpwuid(getuid());
1825 if (!pw) {
1826 printf("You don't exist, go away!\n");
1827 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001828 }
Damien Millereba71ba2000-04-29 23:57:08 +10001829 if (gethostname(hostname, sizeof(hostname)) < 0) {
1830 perror("gethostname");
1831 exit(1);
1832 }
Damien Miller5428f641999-11-25 11:54:57 +11001833
Damien Miller44b25042010-07-02 13:35:01 +10001834 while ((opt = getopt(argc, argv, "degiqpclBHLhvxXyF:b:f:t:D:I:P:m:N:n:"
Damien Miller4e270b02010-04-16 15:56:21 +10001835 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001836 switch (opt) {
1837 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10001838 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001839 if (errstr)
1840 fatal("Bits has bad value %s (%s)",
1841 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001842 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001843 case 'F':
1844 find_host = 1;
1845 rr_hostname = optarg;
1846 break;
1847 case 'H':
1848 hash_hosts = 1;
1849 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001850 case 'I':
1851 cert_key_id = optarg;
1852 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001853 case 'R':
1854 delete_host = 1;
1855 rr_hostname = optarg;
1856 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001857 case 'L':
1858 show_cert = 1;
1859 break;
Damien Miller95def091999-11-25 00:26:21 +11001860 case 'l':
1861 print_fingerprint = 1;
1862 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001863 case 'B':
1864 print_bubblebabble = 1;
1865 break;
Damien Miller44b25042010-07-02 13:35:01 +10001866 case 'm':
1867 if (strcasecmp(optarg, "RFC4716") == 0 ||
1868 strcasecmp(optarg, "ssh2") == 0) {
1869 convert_format = FMT_RFC4716;
1870 break;
1871 }
1872 if (strcasecmp(optarg, "PKCS8") == 0) {
1873 convert_format = FMT_PKCS8;
1874 break;
1875 }
1876 if (strcasecmp(optarg, "PEM") == 0) {
1877 convert_format = FMT_PEM;
1878 break;
1879 }
1880 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001881 case 'n':
1882 cert_principals = optarg;
1883 break;
Damien Miller95def091999-11-25 00:26:21 +11001884 case 'p':
1885 change_passphrase = 1;
1886 break;
Damien Miller95def091999-11-25 00:26:21 +11001887 case 'c':
1888 change_comment = 1;
1889 break;
Damien Miller95def091999-11-25 00:26:21 +11001890 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001891 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1892 sizeof(identity_file))
1893 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001894 have_identity = 1;
1895 break;
Damien Miller37876e92003-05-15 10:19:46 +10001896 case 'g':
1897 print_generic = 1;
1898 break;
Damien Miller95def091999-11-25 00:26:21 +11001899 case 'P':
1900 identity_passphrase = optarg;
1901 break;
Damien Miller95def091999-11-25 00:26:21 +11001902 case 'N':
1903 identity_new_passphrase = optarg;
1904 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001905 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10001906 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001907 break;
Damien Miller95def091999-11-25 00:26:21 +11001908 case 'C':
1909 identity_comment = optarg;
1910 break;
Damien Miller95def091999-11-25 00:26:21 +11001911 case 'q':
1912 quiet = 1;
1913 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001914 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001915 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001916 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10001917 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10001918 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001919 case 'h':
1920 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001921 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001922 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001923 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001924 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001925 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10001926 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10001927 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001928 case 'y':
1929 print_public = 1;
1930 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001931 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001932 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001933 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001934 case 's':
1935 ca_key_path = optarg;
1936 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001937 case 't':
1938 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001939 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001940 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001941 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001942 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001943 case 'v':
1944 if (log_level == SYSLOG_LEVEL_INFO)
1945 log_level = SYSLOG_LEVEL_DEBUG1;
1946 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001947 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001948 log_level < SYSLOG_LEVEL_DEBUG3)
1949 log_level++;
1950 }
1951 break;
Damien Miller37876e92003-05-15 10:19:46 +10001952 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001953 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001954 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001955 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001956 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1957 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001958 if (errstr)
1959 fatal("Desired generator has bad value: %s (%s)",
1960 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001961 break;
1962 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001963 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001964 if (errstr)
1965 fatal("Invalid number of trials: %s (%s)",
1966 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001967 break;
1968 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001969 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10001970 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10001971 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10001972 break;
1973 case 'G':
1974 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001975 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1976 sizeof(out_file))
1977 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001978 break;
1979 case 'T':
1980 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001981 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1982 sizeof(out_file))
1983 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001984 break;
1985 case 'S':
1986 /* XXX - also compare length against bits */
1987 if (BN_hex2bn(&start, optarg) == 0)
1988 fatal("Invalid start point.");
1989 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001990 case 'V':
1991 parse_cert_times(optarg);
1992 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001993 case 'z':
1994 cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
1995 if (errstr)
1996 fatal("Invalid serial number: %s", errstr);
1997 break;
Damien Miller95def091999-11-25 00:26:21 +11001998 case '?':
1999 default:
2000 usage();
2001 }
2002 }
Darren Tucker06930c72003-12-31 11:34:51 +11002003
2004 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002005 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002006
Damien Miller0a80ca12010-02-27 07:55:05 +11002007 argv += optind;
2008 argc -= optind;
2009
2010 if (ca_key_path != NULL) {
2011 if (argc < 1) {
2012 printf("Too few arguments.\n");
2013 usage();
2014 }
2015 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11002016 printf("Too many arguments.\n");
2017 usage();
2018 }
2019 if (change_passphrase && change_comment) {
2020 printf("Can only have one of -p and -c.\n");
2021 usage();
2022 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002023 if (print_fingerprint && (delete_host || hash_hosts)) {
2024 printf("Cannot use -l with -D or -R.\n");
2025 usage();
2026 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002027 if (ca_key_path != NULL) {
2028 if (cert_key_id == NULL)
2029 fatal("Must specify key id (-I) when certifying");
2030 do_ca_sign(pw, argc, argv);
2031 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002032 if (show_cert)
2033 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002034 if (delete_host || hash_hosts || find_host)
2035 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002036 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002037 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002038 if (change_passphrase)
2039 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002040 if (change_comment)
2041 do_change_comment(pw);
Damien Miller44b25042010-07-02 13:35:01 +10002042 if (convert_to)
2043 do_convert_to(pw);
2044 if (convert_from)
2045 do_convert_from(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10002046 if (print_public)
2047 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002048 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002049 unsigned int n = 0;
2050
2051 if (have_identity) {
2052 n = do_print_resource_record(pw,
2053 identity_file, rr_hostname);
2054 if (n == 0) {
2055 perror(identity_file);
2056 exit(1);
2057 }
2058 exit(0);
2059 } else {
2060
2061 n += do_print_resource_record(pw,
2062 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2063 n += do_print_resource_record(pw,
2064 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
2065
2066 if (n == 0)
2067 fatal("no keys found.");
2068 exit(0);
2069 }
Damien Miller37876e92003-05-15 10:19:46 +10002070 }
Damien Miller7ea845e2010-02-12 09:21:02 +11002071 if (pkcs11provider != NULL)
Damien Miller757f34e2010-08-05 13:05:31 +10002072 do_download(pw);
Damien Miller95def091999-11-25 00:26:21 +11002073
Darren Tucker019cefe2003-08-02 22:40:07 +10002074 if (do_gen_candidates) {
2075 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002076
Darren Tucker019cefe2003-08-02 22:40:07 +10002077 if (out == NULL) {
2078 error("Couldn't open modulus candidate file \"%s\": %s",
2079 out_file, strerror(errno));
2080 return (1);
2081 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002082 if (bits == 0)
2083 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002084 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002085 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002086
2087 return (0);
2088 }
2089
2090 if (do_screen_candidates) {
2091 FILE *in;
2092 FILE *out = fopen(out_file, "w");
2093
2094 if (have_identity && strcmp(identity_file, "-") != 0) {
2095 if ((in = fopen(identity_file, "r")) == NULL) {
2096 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002097 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002098 strerror(errno));
2099 }
2100 } else
2101 in = stdin;
2102
2103 if (out == NULL) {
2104 fatal("Couldn't open moduli file \"%s\": %s",
2105 out_file, strerror(errno));
2106 }
2107 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002108 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002109 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002110 }
2111
Damien Miller95def091999-11-25 00:26:21 +11002112 arc4random_stir();
2113
Damien Millerf14be5c2005-11-05 15:15:49 +11002114 if (key_type_name == NULL)
2115 key_type_name = "rsa";
2116
Damien Millere39cacc2000-11-29 12:18:44 +11002117 type = key_type_from_name(key_type_name);
2118 if (type == KEY_UNSPEC) {
2119 fprintf(stderr, "unknown key type %s\n", key_type_name);
2120 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10002121 }
Damien Millereb8b60e2010-08-31 22:41:14 +10002122 if (bits == 0) {
2123 if (type == KEY_DSA)
2124 bits = DEFAULT_BITS_DSA;
2125 else if (type == KEY_ECDSA)
2126 bits = DEFAULT_BITS_ECDSA;
2127 else
2128 bits = DEFAULT_BITS;
2129 }
Damien Millerbebbb7e2010-05-10 11:54:38 +10002130 maxbits = (type == KEY_DSA) ?
2131 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
2132 if (bits > maxbits) {
2133 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
2134 exit(1);
2135 }
Tim Rice660c3402005-11-28 17:45:32 -08002136 if (type == KEY_DSA && bits != 1024)
2137 fatal("DSA keys must be 1024 bits");
Damien Miller57737942010-09-10 11:16:37 +10002138 else if (type != KEY_ECDSA && bits < 768)
2139 fatal("Key must at least be 768 bits");
Damien Millereb8b60e2010-08-31 22:41:14 +10002140 else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(bits) == -1)
2141 fatal("Invalid ECDSA key length - valid lengths are "
2142 "256, 384 or 521 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11002143 if (!quiet)
2144 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002145 private = key_generate(type, bits);
2146 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002147 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002148 exit(1);
2149 }
2150 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002151
2152 if (!have_identity)
2153 ask_filename(pw, "Enter file in which to save the key");
2154
Damien Miller788f2122005-11-05 15:14:59 +11002155 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002156 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2157 pw->pw_dir, _PATH_SSH_USER_DIR);
2158 if (strstr(identity_file, dotsshdir) != NULL) {
2159 if (stat(dotsshdir, &st) < 0) {
2160 if (errno != ENOENT) {
2161 error("Could not stat %s: %s", dotsshdir,
2162 strerror(errno));
2163 } else if (mkdir(dotsshdir, 0700) < 0) {
2164 error("Could not create directory '%s': %s",
2165 dotsshdir, strerror(errno));
2166 } else if (!quiet)
2167 printf("Created directory '%s'.\n", dotsshdir);
2168 }
Damien Miller95def091999-11-25 00:26:21 +11002169 }
2170 /* If the file already exists, ask the user to confirm. */
2171 if (stat(identity_file, &st) >= 0) {
2172 char yesno[3];
2173 printf("%s already exists.\n", identity_file);
2174 printf("Overwrite (y/n)? ");
2175 fflush(stdout);
2176 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2177 exit(1);
2178 if (yesno[0] != 'y' && yesno[0] != 'Y')
2179 exit(1);
2180 }
2181 /* Ask for a passphrase (twice). */
2182 if (identity_passphrase)
2183 passphrase1 = xstrdup(identity_passphrase);
2184 else if (identity_new_passphrase)
2185 passphrase1 = xstrdup(identity_new_passphrase);
2186 else {
2187passphrase_again:
2188 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002189 read_passphrase("Enter passphrase (empty for no "
2190 "passphrase): ", RP_ALLOW_STDIN);
2191 passphrase2 = read_passphrase("Enter same passphrase again: ",
2192 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002193 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002194 /*
2195 * The passphrases do not match. Clear them and
2196 * retry.
2197 */
Damien Miller95def091999-11-25 00:26:21 +11002198 memset(passphrase1, 0, strlen(passphrase1));
2199 memset(passphrase2, 0, strlen(passphrase2));
2200 xfree(passphrase1);
2201 xfree(passphrase2);
2202 printf("Passphrases do not match. Try again.\n");
2203 goto passphrase_again;
2204 }
2205 /* Clear the other copy of the passphrase. */
2206 memset(passphrase2, 0, strlen(passphrase2));
2207 xfree(passphrase2);
2208 }
2209
Damien Miller95def091999-11-25 00:26:21 +11002210 if (identity_comment) {
2211 strlcpy(comment, identity_comment, sizeof(comment));
2212 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002213 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002214 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2215 }
2216
2217 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00002218 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002219 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002220 memset(passphrase1, 0, strlen(passphrase1));
2221 xfree(passphrase1);
2222 exit(1);
2223 }
2224 /* Clear the passphrase. */
2225 memset(passphrase1, 0, strlen(passphrase1));
2226 xfree(passphrase1);
2227
2228 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002229 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002230 arc4random_stir();
2231
2232 if (!quiet)
2233 printf("Your identification has been saved in %s.\n", identity_file);
2234
Damien Miller95def091999-11-25 00:26:21 +11002235 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002236 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2237 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002238 printf("Could not save your public key in %s\n", identity_file);
2239 exit(1);
2240 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002241 f = fdopen(fd, "w");
2242 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002243 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002244 exit(1);
2245 }
Damien Millereba71ba2000-04-29 23:57:08 +10002246 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002247 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002248 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002249 fclose(f);
2250
2251 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002252 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002253 char *ra = key_fingerprint(public, SSH_FP_MD5,
2254 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002255 printf("Your public key has been saved in %s.\n",
2256 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002257 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002258 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002259 printf("The key's randomart image is:\n");
2260 printf("%s\n", ra);
2261 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00002262 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11002263 }
Damien Millereba71ba2000-04-29 23:57:08 +10002264
2265 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002266 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002267}