blob: d90b1dfdd389236cc762543481780bd037b6a4a0 [file] [log] [blame]
Damien Miller757f34e2010-08-05 13:05:31 +10001/* $OpenBSD: ssh-keygen.c,v 1.197 2010/08/04 06:07:11 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
60u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller5428f641999-11-25 11:54:57 +110062/*
63 * Flag indicating that we just want to change the passphrase. This can be
64 * set on the command line.
65 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066int change_passphrase = 0;
67
Damien Miller5428f641999-11-25 11:54:57 +110068/*
69 * Flag indicating that we just want to change the comment. This can be set
70 * on the command line.
71 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072int change_comment = 0;
73
74int quiet = 0;
75
Darren Tucker35c45532008-06-13 04:43:15 +100076int log_level = SYSLOG_LEVEL_INFO;
77
Damien Miller4b42d7f2005-03-01 21:48:35 +110078/* Flag indicating that we want to hash a known_hosts file */
79int hash_hosts = 0;
80/* Flag indicating that we want lookup a host in known_hosts file */
81int find_host = 0;
82/* Flag indicating that we want to delete a host from a known_hosts file */
83int delete_host = 0;
84
Damien Millerf2b70ca2010-03-05 07:39:35 +110085/* Flag indicating that we want to show the contents of a certificate */
86int show_cert = 0;
87
Damien Miller10f6f6b1999-11-17 17:29:08 +110088/* Flag indicating that we just want to see the key fingerprint */
89int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000090int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110091
Damien Miller431f66b1999-11-21 18:31:57 +110092/* The identity file name, given on the command line or entered by the user. */
93char identity_file[1024];
94int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
96/* This is set to the passphrase if given on the command line. */
97char *identity_passphrase = NULL;
98
99/* This is set to the new passphrase if given on the command line. */
100char *identity_new_passphrase = NULL;
101
102/* This is set to the new comment if given on the command line. */
103char *identity_comment = NULL;
104
Damien Miller0a80ca12010-02-27 07:55:05 +1100105/* Path to CA key when certifying keys. */
106char *ca_key_path = NULL;
107
Damien Miller4e270b02010-04-16 15:56:21 +1000108/* Certificate serial number */
109long long cert_serial = 0;
110
Damien Miller0a80ca12010-02-27 07:55:05 +1100111/* Key type when certifying */
112u_int cert_key_type = SSH2_CERT_TYPE_USER;
113
114/* "key ID" of signed key */
115char *cert_key_id = NULL;
116
117/* Comma-separated list of principal names for certifying keys */
118char *cert_principals = NULL;
119
120/* Validity period for certificates */
121u_int64_t cert_valid_from = 0;
122u_int64_t cert_valid_to = ~0ULL;
123
Damien Miller4e270b02010-04-16 15:56:21 +1000124/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000125#define CERTOPT_X_FWD (1)
126#define CERTOPT_AGENT_FWD (1<<1)
127#define CERTOPT_PORT_FWD (1<<2)
128#define CERTOPT_PTY (1<<3)
129#define CERTOPT_USER_RC (1<<4)
130#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
131 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
132u_int32_t certflags_flags = CERTOPT_DEFAULT;
133char *certflags_command = NULL;
134char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100135
Damien Miller44b25042010-07-02 13:35:01 +1000136/* Conversion to/from various formats */
137int convert_to = 0;
138int convert_from = 0;
139enum {
140 FMT_RFC4716,
141 FMT_PKCS8,
142 FMT_PEM
143} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000144int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000145int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100146
Damien Millera41c8b12002-01-22 23:05:08 +1100147char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000148
Damien Miller757f34e2010-08-05 13:05:31 +1000149/* Load key from this PKCS#11 provider */
150char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000151
Damien Miller431f66b1999-11-21 18:31:57 +1100152/* argv0 */
153extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Millereba71ba2000-04-29 23:57:08 +1000155char hostname[MAXHOSTNAMELEN];
156
Darren Tucker770fc012004-05-13 16:24:32 +1000157/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000158int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000159int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
160
Ben Lindstrombba81212001-06-25 05:01:22 +0000161static void
Damien Miller431f66b1999-11-21 18:31:57 +1100162ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163{
Damien Miller95def091999-11-25 00:26:21 +1100164 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100165 char *name = NULL;
166
Damien Miller993dd552002-02-19 15:22:47 +1100167 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000168 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100169 else {
Damien Miller993dd552002-02-19 15:22:47 +1100170 switch (key_type_from_name(key_type_name)) {
171 case KEY_RSA1:
172 name = _PATH_SSH_CLIENT_IDENTITY;
173 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000174 case KEY_DSA_CERT:
175 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100176 case KEY_DSA:
177 name = _PATH_SSH_CLIENT_ID_DSA;
178 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000179 case KEY_RSA_CERT:
180 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100181 case KEY_RSA:
182 name = _PATH_SSH_CLIENT_ID_RSA;
183 break;
184 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100185 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100186 exit(1);
187 break;
188 }
Damien Miller90967402006-03-26 14:07:26 +1100189 }
Damien Millere39cacc2000-11-29 12:18:44 +1100190 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000191 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100192 if (fgets(buf, sizeof(buf), stdin) == NULL)
193 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000194 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100195 if (strcmp(buf, "") != 0)
196 strlcpy(identity_file, buf, sizeof(identity_file));
197 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100198}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000201load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000202{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000203 char *pass;
204 Key *prv;
205
Ben Lindstroma3700052001-04-05 23:26:32 +0000206 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000207 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000208 if (identity_passphrase)
209 pass = xstrdup(identity_passphrase);
210 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000211 pass = read_passphrase("Enter passphrase: ",
212 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000213 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000214 memset(pass, 0, strlen(pass));
215 xfree(pass);
216 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000217 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000218}
219
Damien Miller874d77b2000-10-14 16:23:11 +1100220#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000221#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100222#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000223#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000224
Ben Lindstrombba81212001-06-25 05:01:22 +0000225static void
Damien Miller44b25042010-07-02 13:35:01 +1000226do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000227{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000228 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000229 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100230 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000231
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000232 if (key_to_blob(k, &blob, &len) <= 0) {
233 fprintf(stderr, "key_to_blob failed\n");
234 exit(1);
235 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100236 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
237 snprintf(comment, sizeof(comment),
238 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000239 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000240 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100241
242 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
243 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000244 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100245 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000246 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000247 xfree(blob);
248 exit(0);
249}
250
Ben Lindstrombba81212001-06-25 05:01:22 +0000251static void
Damien Miller44b25042010-07-02 13:35:01 +1000252do_convert_to_pkcs8(Key *k)
253{
254 switch (key_type_plain(k->type)) {
255 case KEY_RSA:
256 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
257 fatal("PEM_write_RSA_PUBKEY failed");
258 break;
259 case KEY_DSA:
260 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
261 fatal("PEM_write_DSA_PUBKEY failed");
262 break;
263 default:
264 fatal("%s: unsupported key type %s", __func__, key_type(k));
265 }
266 exit(0);
267}
268
269static void
270do_convert_to_pem(Key *k)
271{
272 switch (key_type_plain(k->type)) {
273 case KEY_RSA:
274 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
275 fatal("PEM_write_RSAPublicKey failed");
276 break;
277#if notyet /* OpenSSH 0.9.8 lacks this function */
278 case KEY_DSA:
279 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
280 fatal("PEM_write_DSAPublicKey failed");
281 break;
282#endif
283 default:
284 fatal("%s: unsupported key type %s", __func__, key_type(k));
285 }
286 exit(0);
287}
288
289static void
290do_convert_to(struct passwd *pw)
291{
292 Key *k;
293 struct stat st;
294
295 if (!have_identity)
296 ask_filename(pw, "Enter file in which the key is");
297 if (stat(identity_file, &st) < 0)
298 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
299 if ((k = key_load_public(identity_file, NULL)) == NULL) {
300 if ((k = load_identity(identity_file)) == NULL) {
301 fprintf(stderr, "load failed\n");
302 exit(1);
303 }
304 }
305 if (k->type == KEY_RSA1) {
306 fprintf(stderr, "version 1 keys are not supported\n");
307 exit(1);
308 }
309
310 switch (convert_format) {
311 case FMT_RFC4716:
312 do_convert_to_ssh2(pw, k);
313 break;
314 case FMT_PKCS8:
315 do_convert_to_pkcs8(k);
316 break;
317 case FMT_PEM:
318 do_convert_to_pem(k);
319 break;
320 default:
321 fatal("%s: unknown key format %d", __func__, convert_format);
322 }
323 exit(0);
324}
325
326static void
Damien Miller874d77b2000-10-14 16:23:11 +1100327buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
328{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000329 u_int bignum_bits = buffer_get_int(b);
330 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000331
Damien Miller874d77b2000-10-14 16:23:11 +1100332 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000333 fatal("buffer_get_bignum_bits: input buffer too small: "
334 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100335 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
336 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100337 buffer_consume(b, bytes);
338}
339
Ben Lindstrombba81212001-06-25 05:01:22 +0000340static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000341do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100342{
343 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100344 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100345 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000346 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000347 int magic, rlen, ktype, i1, i2, i3, i4;
348 u_int slen;
349 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100350
351 buffer_init(&b);
352 buffer_append(&b, blob, blen);
353
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100354 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100355 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
356 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
357 buffer_free(&b);
358 return NULL;
359 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000360 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100361 type = buffer_get_string(&b, NULL);
362 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000363 i2 = buffer_get_int(&b);
364 i3 = buffer_get_int(&b);
365 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100366 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100367 if (strcmp(cipher, "none") != 0) {
368 error("unsupported cipher %s", cipher);
369 xfree(cipher);
370 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000371 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100372 return NULL;
373 }
374 xfree(cipher);
375
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000376 if (strstr(type, "dsa")) {
377 ktype = KEY_DSA;
378 } else if (strstr(type, "rsa")) {
379 ktype = KEY_RSA;
380 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100381 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000382 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100383 return NULL;
384 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000385 key = key_new_private(ktype);
386 xfree(type);
387
388 switch (key->type) {
389 case KEY_DSA:
390 buffer_get_bignum_bits(&b, key->dsa->p);
391 buffer_get_bignum_bits(&b, key->dsa->g);
392 buffer_get_bignum_bits(&b, key->dsa->q);
393 buffer_get_bignum_bits(&b, key->dsa->pub_key);
394 buffer_get_bignum_bits(&b, key->dsa->priv_key);
395 break;
396 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100397 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000398 debug("e %lx", e);
399 if (e < 30) {
400 e <<= 8;
401 e += buffer_get_char(&b);
402 debug("e %lx", e);
403 e <<= 8;
404 e += buffer_get_char(&b);
405 debug("e %lx", e);
406 }
407 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000408 buffer_free(&b);
409 key_free(key);
410 return NULL;
411 }
412 buffer_get_bignum_bits(&b, key->rsa->d);
413 buffer_get_bignum_bits(&b, key->rsa->n);
414 buffer_get_bignum_bits(&b, key->rsa->iqmp);
415 buffer_get_bignum_bits(&b, key->rsa->q);
416 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000417 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000418 break;
419 }
Damien Miller874d77b2000-10-14 16:23:11 +1100420 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000421 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000422 error("do_convert_private_ssh2_from_blob: "
423 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100424 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000425
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000426 /* try the key */
427 key_sign(key, &sig, &slen, data, sizeof(data));
428 key_verify(key, sig, slen, data, sizeof(data));
429 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100430 return key;
431}
432
Damien Miller8056a9d2006-03-15 12:05:40 +1100433static int
434get_line(FILE *fp, char *line, size_t len)
435{
436 int c;
437 size_t pos = 0;
438
439 line[0] = '\0';
440 while ((c = fgetc(fp)) != EOF) {
441 if (pos >= len - 1) {
442 fprintf(stderr, "input line too long.\n");
443 exit(1);
444 }
Damien Miller90967402006-03-26 14:07:26 +1100445 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100446 case '\r':
447 c = fgetc(fp);
448 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
449 fprintf(stderr, "unget: %s\n", strerror(errno));
450 exit(1);
451 }
452 return pos;
453 case '\n':
454 return pos;
455 }
456 line[pos++] = c;
457 line[pos] = '\0';
458 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100459 /* We reached EOF */
460 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100461}
462
Ben Lindstrombba81212001-06-25 05:01:22 +0000463static void
Damien Miller44b25042010-07-02 13:35:01 +1000464do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000465{
Damien Millereba71ba2000-04-29 23:57:08 +1000466 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000467 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100468 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000469 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000470 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000471 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000472 FILE *fp;
473
Damien Millerba3420a2010-06-26 09:39:07 +1000474 if ((fp = fopen(identity_file, "r")) == NULL)
475 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000476 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100477 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
478 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000479 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000480 if (strncmp(line, "----", 4) == 0 ||
481 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100482 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000483 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000484 if (strstr(line, " END ") != NULL) {
485 break;
486 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000487 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000488 continue;
489 }
Damien Miller30c3d422000-05-09 11:02:59 +1000490 if (escaped) {
491 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000492 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000493 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000494 }
Damien Millereba71ba2000-04-29 23:57:08 +1000495 strlcat(encoded, line, sizeof(encoded));
496 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000497 len = strlen(encoded);
498 if (((len % 4) == 3) &&
499 (encoded[len-1] == '=') &&
500 (encoded[len-2] == '=') &&
501 (encoded[len-3] == '='))
502 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100503 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000504 if (blen < 0) {
505 fprintf(stderr, "uudecode failed.\n");
506 exit(1);
507 }
Damien Miller44b25042010-07-02 13:35:01 +1000508 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100509 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100510 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000511 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100512 fprintf(stderr, "decode blob failed.\n");
513 exit(1);
514 }
Damien Miller44b25042010-07-02 13:35:01 +1000515 fclose(fp);
516}
517
518static void
519do_convert_from_pkcs8(Key **k, int *private)
520{
521 EVP_PKEY *pubkey;
522 FILE *fp;
523
524 if ((fp = fopen(identity_file, "r")) == NULL)
525 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
526 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
527 fatal("%s: %s is not a recognised public key format", __func__,
528 identity_file);
529 }
530 fclose(fp);
531 switch (EVP_PKEY_type(pubkey->type)) {
532 case EVP_PKEY_RSA:
533 *k = key_new(KEY_UNSPEC);
534 (*k)->type = KEY_RSA;
535 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
536 break;
537 case EVP_PKEY_DSA:
538 *k = key_new(KEY_UNSPEC);
539 (*k)->type = KEY_DSA;
540 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
541 break;
542 default:
543 fatal("%s: unsupported pubkey type %d", __func__,
544 EVP_PKEY_type(pubkey->type));
545 }
546 EVP_PKEY_free(pubkey);
547 return;
548}
549
550static void
551do_convert_from_pem(Key **k, int *private)
552{
553 FILE *fp;
554 RSA *rsa;
555#ifdef notyet
556 DSA *dsa;
557#endif
558
559 if ((fp = fopen(identity_file, "r")) == NULL)
560 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
561 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
562 *k = key_new(KEY_UNSPEC);
563 (*k)->type = KEY_RSA;
564 (*k)->rsa = rsa;
565 fclose(fp);
566 return;
567 }
568#if notyet /* OpenSSH 0.9.8 lacks this function */
569 rewind(fp);
570 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
571 *k = key_new(KEY_UNSPEC);
572 (*k)->type = KEY_DSA;
573 (*k)->dsa = dsa;
574 fclose(fp);
575 return;
576 }
577#endif
578 fatal("%s: unrecognised raw private key format", __func__);
579}
580
581static void
582do_convert_from(struct passwd *pw)
583{
584 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000585 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000586 struct stat st;
587
588 if (!have_identity)
589 ask_filename(pw, "Enter file in which the key is");
590 if (stat(identity_file, &st) < 0)
591 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
592
593 switch (convert_format) {
594 case FMT_RFC4716:
595 do_convert_from_ssh2(pw, &k, &private);
596 break;
597 case FMT_PKCS8:
598 do_convert_from_pkcs8(&k, &private);
599 break;
600 case FMT_PEM:
601 do_convert_from_pem(&k, &private);
602 break;
603 default:
604 fatal("%s: unknown key format %d", __func__, convert_format);
605 }
606
607 if (!private)
608 ok = key_write(k, stdout);
609 if (ok)
610 fprintf(stdout, "\n");
611 else {
612 switch (k->type) {
613 case KEY_DSA:
614 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
615 NULL, 0, NULL, NULL);
616 break;
617 case KEY_RSA:
618 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
619 NULL, 0, NULL, NULL);
620 break;
621 default:
622 fatal("%s: unsupported key type %s", __func__,
623 key_type(k));
624 }
625 }
626
Damien Miller874d77b2000-10-14 16:23:11 +1100627 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100628 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100629 exit(1);
630 }
Damien Millereba71ba2000-04-29 23:57:08 +1000631 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000632 exit(0);
633}
634
Ben Lindstrombba81212001-06-25 05:01:22 +0000635static void
Damien Millereba71ba2000-04-29 23:57:08 +1000636do_print_public(struct passwd *pw)
637{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000638 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000639 struct stat st;
640
641 if (!have_identity)
642 ask_filename(pw, "Enter file in which the key is");
643 if (stat(identity_file, &st) < 0) {
644 perror(identity_file);
645 exit(1);
646 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000647 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000648 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000649 fprintf(stderr, "load failed\n");
650 exit(1);
651 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000652 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000653 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000654 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000655 fprintf(stdout, "\n");
656 exit(0);
657}
658
Ben Lindstromcd392282001-07-04 03:44:03 +0000659static void
Damien Miller757f34e2010-08-05 13:05:31 +1000660do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000661{
Damien Miller7ea845e2010-02-12 09:21:02 +1100662#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000663 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100664 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000665
Damien Miller7ea845e2010-02-12 09:21:02 +1100666 pkcs11_init(0);
667 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
668 if (nkeys <= 0)
669 fatal("cannot read public key from pkcs11");
670 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000671 key_write(keys[i], stdout);
672 key_free(keys[i]);
673 fprintf(stdout, "\n");
674 }
675 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100676 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000677 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100678#else
679 fatal("no pkcs11 support");
680#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000681}
Ben Lindstromcd392282001-07-04 03:44:03 +0000682
Ben Lindstrombba81212001-06-25 05:01:22 +0000683static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100684do_fingerprint(struct passwd *pw)
685{
Damien Miller98c7ad62000-03-09 21:27:49 +1100686 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000687 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000688 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100689 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000690 enum fp_rep rep;
691 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100692 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100693
Ben Lindstromd0fca422001-03-26 13:44:06 +0000694 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
695 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000696
Damien Miller95def091999-11-25 00:26:21 +1100697 if (!have_identity)
698 ask_filename(pw, "Enter file in which the key is");
699 if (stat(identity_file, &st) < 0) {
700 perror(identity_file);
701 exit(1);
702 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000703 public = key_load_public(identity_file, &comment);
704 if (public != NULL) {
705 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100706 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000707 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
708 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000709 if (log_level >= SYSLOG_LEVEL_VERBOSE)
710 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100711 key_free(public);
712 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000713 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000714 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100715 exit(0);
716 }
Damien Miller40b59852006-06-13 13:00:25 +1000717 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000718 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000719 comment = NULL;
720 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100721
Damien Millerba3420a2010-06-26 09:39:07 +1000722 if ((f = fopen(identity_file, "r")) == NULL)
723 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100724
Damien Millerba3420a2010-06-26 09:39:07 +1000725 while (fgets(line, sizeof(line), f)) {
726 if ((cp = strchr(line, '\n')) == NULL) {
727 error("line %d too long: %.40s...",
728 num + 1, line);
729 skip = 1;
730 continue;
Damien Miller95def091999-11-25 00:26:21 +1100731 }
Damien Millerba3420a2010-06-26 09:39:07 +1000732 num++;
733 if (skip) {
734 skip = 0;
735 continue;
736 }
737 *cp = '\0';
738
739 /* Skip leading whitespace, empty and comment lines. */
740 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
741 ;
742 if (!*cp || *cp == '\n' || *cp == '#')
743 continue;
744 i = strtol(cp, &ep, 10);
745 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
746 int quoted = 0;
747 comment = cp;
748 for (; *cp && (quoted || (*cp != ' ' &&
749 *cp != '\t')); cp++) {
750 if (*cp == '\\' && cp[1] == '"')
751 cp++; /* Skip both */
752 else if (*cp == '"')
753 quoted = !quoted;
754 }
755 if (!*cp)
756 continue;
757 *cp++ = '\0';
758 }
759 ep = cp;
760 public = key_new(KEY_RSA1);
761 if (key_read(public, &cp) != 1) {
762 cp = ep;
763 key_free(public);
764 public = key_new(KEY_UNSPEC);
765 if (key_read(public, &cp) != 1) {
766 key_free(public);
767 continue;
768 }
769 }
770 comment = *cp ? cp : comment;
771 fp = key_fingerprint(public, fptype, rep);
772 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
773 printf("%u %s %s (%s)\n", key_size(public), fp,
774 comment ? comment : "no comment", key_type(public));
775 if (log_level >= SYSLOG_LEVEL_VERBOSE)
776 printf("%s\n", ra);
777 xfree(ra);
778 xfree(fp);
779 key_free(public);
780 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100781 }
Damien Millerba3420a2010-06-26 09:39:07 +1000782 fclose(f);
783
Damien Miller98c7ad62000-03-09 21:27:49 +1100784 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100785 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100786 exit(1);
787 }
Damien Miller95def091999-11-25 00:26:21 +1100788 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100789}
790
Damien Miller4b42d7f2005-03-01 21:48:35 +1100791static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100792printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100793{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000794 if (print_fingerprint) {
795 enum fp_rep rep;
796 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000797 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000798
799 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
800 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
801 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100802 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000803 printf("%u %s %s (%s)\n", key_size(public), fp, name,
804 key_type(public));
805 if (log_level >= SYSLOG_LEVEL_VERBOSE)
806 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000807 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000808 xfree(fp);
809 } else {
810 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
811 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100812 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000813 if (!key_write(public, f))
814 fatal("key_write failed");
815 fprintf(f, "\n");
816 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100817}
818
819static void
820do_known_hosts(struct passwd *pw, const char *name)
821{
822 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100823 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100824 char *cp, *cp2, *kp, *kp2;
825 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100826 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100827 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100828
829 if (!have_identity) {
830 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
831 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
832 sizeof(identity_file))
833 fatal("Specified known hosts path too long");
834 xfree(cp);
835 have_identity = 1;
836 }
837 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +1000838 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +1100839
840 /*
841 * Find hosts goes to stdout, hash and deletions happen in-place
842 * A corner case is ssh-keygen -HF foo, which should go to stdout
843 */
844 if (!find_host && (hash_hosts || delete_host)) {
845 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
846 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
847 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
848 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
849 fatal("known_hosts path too long");
850 umask(077);
851 if ((c = mkstemp(tmp)) == -1)
852 fatal("mkstemp: %s", strerror(errno));
853 if ((out = fdopen(c, "w")) == NULL) {
854 c = errno;
855 unlink(tmp);
856 fatal("fdopen: %s", strerror(c));
857 }
858 inplace = 1;
859 }
860
861 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000862 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100863 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100864 skip = 1;
865 invalid = 1;
866 continue;
867 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000868 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100869 if (skip) {
870 skip = 0;
871 continue;
872 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000873 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100874
875 /* Skip leading whitespace, empty and comment lines. */
876 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
877 ;
878 if (!*cp || *cp == '\n' || *cp == '#') {
879 if (inplace)
880 fprintf(out, "%s\n", cp);
881 continue;
882 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100883 /* Check whether this is a CA key */
884 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
885 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
886 cp[sizeof(CA_MARKER) - 1] == '\t')) {
887 ca = 1;
888 cp += sizeof(CA_MARKER);
889 } else
890 ca = 0;
891
Damien Miller4b42d7f2005-03-01 21:48:35 +1100892 /* Find the end of the host name portion. */
893 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
894 ;
Damien Miller0a80ca12010-02-27 07:55:05 +1100895
Damien Miller4b42d7f2005-03-01 21:48:35 +1100896 if (*kp == '\0' || *(kp + 1) == '\0') {
897 error("line %d missing key: %.40s...",
898 num, line);
899 invalid = 1;
900 continue;
901 }
902 *kp++ = '\0';
903 kp2 = kp;
904
Damien Miller0a80ca12010-02-27 07:55:05 +1100905 pub = key_new(KEY_RSA1);
906 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100907 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +1100908 key_free(pub);
909 pub = key_new(KEY_UNSPEC);
910 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100911 error("line %d invalid key: %.40s...",
912 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100913 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100914 invalid = 1;
915 continue;
916 }
917 }
918
919 if (*cp == HASH_DELIM) {
920 if (find_host || delete_host) {
921 cp2 = host_hash(name, cp, strlen(cp));
922 if (cp2 == NULL) {
923 error("line %d: invalid hashed "
924 "name: %.64s...", num, line);
925 invalid = 1;
926 continue;
927 }
928 c = (strcmp(cp2, cp) == 0);
929 if (find_host && c) {
930 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100931 "line %d type %s%s\n", name,
932 num, key_type(pub),
933 ca ? " (CA key)" : "");
934 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100935 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100936 if (delete_host && !c && !ca)
937 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100938 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +1100939 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100940 } else {
941 if (find_host || delete_host) {
942 c = (match_hostname(name, cp,
943 strlen(cp)) == 1);
944 if (find_host && c) {
945 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100946 "line %d type %s%s\n", name,
947 num, key_type(pub),
948 ca ? " (CA key)" : "");
949 printhost(out, name, pub,
950 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100951 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100952 if (delete_host && !c && !ca)
953 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100954 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100955 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100956 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100957 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100958 if (ca) {
959 fprintf(stderr, "Warning: "
960 "ignoring CA key for host: "
961 "%.64s\n", cp2);
962 printhost(out, cp2, pub, ca, 0);
963 } else if (strcspn(cp2, "*?!") !=
964 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +1100965 fprintf(stderr, "Warning: "
966 "ignoring host name with "
967 "metacharacters: %.64s\n",
968 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +1100969 printhost(out, cp2, pub, ca, 0);
970 } else
971 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +1100972 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100973 has_unhashed = 1;
974 }
975 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100976 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100977 }
978 fclose(in);
979
980 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100981 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +1100982 identity_file);
983 if (inplace) {
984 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +1100985 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100986 fclose(out);
987 unlink(tmp);
988 }
989 exit(1);
990 }
991
992 if (inplace) {
993 fclose(out);
994
995 /* Backup existing file */
996 if (unlink(old) == -1 && errno != ENOENT)
997 fatal("unlink %.100s: %s", old, strerror(errno));
998 if (link(identity_file, old) == -1)
999 fatal("link %.100s to %.100s: %s", identity_file, old,
1000 strerror(errno));
1001 /* Move new one into place */
1002 if (rename(tmp, identity_file) == -1) {
1003 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1004 strerror(errno));
1005 unlink(tmp);
1006 unlink(old);
1007 exit(1);
1008 }
1009
1010 fprintf(stderr, "%s updated.\n", identity_file);
1011 fprintf(stderr, "Original contents retained as %s\n", old);
1012 if (has_unhashed) {
1013 fprintf(stderr, "WARNING: %s contains unhashed "
1014 "entries\n", old);
1015 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001016 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001017 }
1018 }
1019
1020 exit(0);
1021}
1022
Damien Miller95def091999-11-25 00:26:21 +11001023/*
1024 * Perform changing a passphrase. The argument is the passwd structure
1025 * for the current user.
1026 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001027static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001028do_change_passphrase(struct passwd *pw)
1029{
Damien Miller95def091999-11-25 00:26:21 +11001030 char *comment;
1031 char *old_passphrase, *passphrase1, *passphrase2;
1032 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001033 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001034
Damien Miller95def091999-11-25 00:26:21 +11001035 if (!have_identity)
1036 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001037 if (stat(identity_file, &st) < 0) {
1038 perror(identity_file);
1039 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040 }
Damien Miller95def091999-11-25 00:26:21 +11001041 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001042 private = key_load_private(identity_file, "", &comment);
1043 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001044 if (identity_passphrase)
1045 old_passphrase = xstrdup(identity_passphrase);
1046 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001047 old_passphrase =
1048 read_passphrase("Enter old passphrase: ",
1049 RP_ALLOW_STDIN);
1050 private = key_load_private(identity_file, old_passphrase,
1051 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001052 memset(old_passphrase, 0, strlen(old_passphrase));
1053 xfree(old_passphrase);
1054 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001055 printf("Bad passphrase.\n");
1056 exit(1);
1057 }
Damien Miller95def091999-11-25 00:26:21 +11001058 }
1059 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060
Damien Miller95def091999-11-25 00:26:21 +11001061 /* Ask the new passphrase (twice). */
1062 if (identity_new_passphrase) {
1063 passphrase1 = xstrdup(identity_new_passphrase);
1064 passphrase2 = NULL;
1065 } else {
1066 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001067 read_passphrase("Enter new passphrase (empty for no "
1068 "passphrase): ", RP_ALLOW_STDIN);
1069 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001070 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001071
1072 /* Verify that they are the same. */
1073 if (strcmp(passphrase1, passphrase2) != 0) {
1074 memset(passphrase1, 0, strlen(passphrase1));
1075 memset(passphrase2, 0, strlen(passphrase2));
1076 xfree(passphrase1);
1077 xfree(passphrase2);
1078 printf("Pass phrases do not match. Try again.\n");
1079 exit(1);
1080 }
1081 /* Destroy the other copy. */
1082 memset(passphrase2, 0, strlen(passphrase2));
1083 xfree(passphrase2);
1084 }
1085
1086 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001087 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001088 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001089 memset(passphrase1, 0, strlen(passphrase1));
1090 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001091 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001092 xfree(comment);
1093 exit(1);
1094 }
1095 /* Destroy the passphrase and the copy of the key in memory. */
1096 memset(passphrase1, 0, strlen(passphrase1));
1097 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001098 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +11001099 xfree(comment);
1100
1101 printf("Your identification has been saved with the new passphrase.\n");
1102 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001103}
1104
Damien Miller37876e92003-05-15 10:19:46 +10001105/*
1106 * Print the SSHFP RR.
1107 */
Damien Millercb314822006-03-26 13:48:01 +11001108static int
1109do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001110{
1111 Key *public;
1112 char *comment = NULL;
1113 struct stat st;
1114
Damien Millercb314822006-03-26 13:48:01 +11001115 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +10001116 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +11001117 if (stat(fname, &st) < 0) {
1118 if (errno == ENOENT)
1119 return 0;
1120 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001121 exit(1);
1122 }
Damien Millercb314822006-03-26 13:48:01 +11001123 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001124 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001125 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001126 key_free(public);
1127 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +11001128 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001129 }
1130 if (comment)
1131 xfree(comment);
1132
Damien Millercb314822006-03-26 13:48:01 +11001133 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001134 exit(1);
1135}
Damien Miller37876e92003-05-15 10:19:46 +10001136
Damien Miller95def091999-11-25 00:26:21 +11001137/*
1138 * Change the comment of a private key file.
1139 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001140static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001141do_change_comment(struct passwd *pw)
1142{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001143 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001144 Key *private;
1145 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001146 struct stat st;
1147 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001148 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149
Damien Miller95def091999-11-25 00:26:21 +11001150 if (!have_identity)
1151 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001152 if (stat(identity_file, &st) < 0) {
1153 perror(identity_file);
1154 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001156 private = key_load_private(identity_file, "", &comment);
1157 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001158 if (identity_passphrase)
1159 passphrase = xstrdup(identity_passphrase);
1160 else if (identity_new_passphrase)
1161 passphrase = xstrdup(identity_new_passphrase);
1162 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001163 passphrase = read_passphrase("Enter passphrase: ",
1164 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001165 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001166 private = key_load_private(identity_file, passphrase, &comment);
1167 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001168 memset(passphrase, 0, strlen(passphrase));
1169 xfree(passphrase);
1170 printf("Bad passphrase.\n");
1171 exit(1);
1172 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001173 } else {
1174 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001175 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001176 if (private->type != KEY_RSA1) {
1177 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1178 key_free(private);
1179 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001180 }
Damien Miller95def091999-11-25 00:26:21 +11001181 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182
Damien Miller95def091999-11-25 00:26:21 +11001183 if (identity_comment) {
1184 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1185 } else {
1186 printf("Enter new comment: ");
1187 fflush(stdout);
1188 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1189 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001190 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001191 exit(1);
1192 }
Damien Miller14b017d2007-09-17 16:09:15 +10001193 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001194 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001195
Damien Miller95def091999-11-25 00:26:21 +11001196 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001197 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001198 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001199 memset(passphrase, 0, strlen(passphrase));
1200 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001201 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001202 xfree(comment);
1203 exit(1);
1204 }
Damien Miller95def091999-11-25 00:26:21 +11001205 memset(passphrase, 0, strlen(passphrase));
1206 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001207 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001208 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209
Damien Miller95def091999-11-25 00:26:21 +11001210 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001211 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1212 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001213 printf("Could not save your public key in %s\n", identity_file);
1214 exit(1);
1215 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001216 f = fdopen(fd, "w");
1217 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001218 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001219 exit(1);
1220 }
Damien Millereba71ba2000-04-29 23:57:08 +10001221 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001222 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001223 key_free(public);
1224 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001225 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226
Damien Miller95def091999-11-25 00:26:21 +11001227 xfree(comment);
1228
1229 printf("The comment in your key file has been changed.\n");
1230 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001231}
1232
Damien Miller0a80ca12010-02-27 07:55:05 +11001233static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001234fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001235{
1236 char from[32], to[32];
1237 static char ret[64];
1238 time_t tt;
1239 struct tm *tm;
1240
1241 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001242 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001243 return "forever";
1244
Damien Millerf2b70ca2010-03-05 07:39:35 +11001245 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001246 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001247 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001248 tm = localtime(&tt);
1249 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1250 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001251 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001252 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001253 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001254 tm = localtime(&tt);
1255 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1256 }
1257
Damien Millerf2b70ca2010-03-05 07:39:35 +11001258 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001259 snprintf(ret, sizeof(ret), "before %s", to);
1260 return ret;
1261 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001262 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001263 snprintf(ret, sizeof(ret), "after %s", from);
1264 return ret;
1265 }
1266
1267 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1268 return ret;
1269}
1270
1271static void
Damien Miller4e270b02010-04-16 15:56:21 +10001272add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001273{
1274 debug3("%s: %s", __func__, name);
1275 buffer_put_cstring(c, name);
1276 buffer_put_string(c, NULL, 0);
1277}
1278
1279static void
Damien Miller4e270b02010-04-16 15:56:21 +10001280add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001281{
1282 Buffer b;
1283
1284 debug3("%s: %s=%s", __func__, name, value);
1285 buffer_init(&b);
1286 buffer_put_cstring(&b, value);
1287
1288 buffer_put_cstring(c, name);
1289 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1290
1291 buffer_free(&b);
1292}
1293
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001294#define OPTIONS_CRITICAL 1
1295#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001296static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001297prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001298{
Damien Miller0a80ca12010-02-27 07:55:05 +11001299 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001300 if ((which & OPTIONS_CRITICAL) != 0 &&
1301 certflags_command != NULL)
1302 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001303 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1304 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001305 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001306 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1307 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001308 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001309 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1310 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001311 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001312 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1313 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001314 add_flag_option(c, "permit-user-rc");
Damien Miller1da63882010-08-05 13:03:51 +10001315 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1316 (certflags_flags & CERTOPT_X_FWD) != 0)
1317 add_flag_option(c, "permit-X11-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001318 if ((which & OPTIONS_CRITICAL) != 0 &&
1319 certflags_src_addr != NULL)
1320 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001321}
1322
Damien Miller757f34e2010-08-05 13:05:31 +10001323static Key *
1324load_pkcs11_key(char *path)
1325{
1326#ifdef ENABLE_PKCS11
1327 Key **keys = NULL, *public, *private = NULL;
1328 int i, nkeys;
1329
1330 if ((public = key_load_public(path, NULL)) == NULL)
1331 fatal("Couldn't load CA public key \"%s\"", path);
1332
1333 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1334 debug3("%s: %d keys", __func__, nkeys);
1335 if (nkeys <= 0)
1336 fatal("cannot read public key from pkcs11");
1337 for (i = 0; i < nkeys; i++) {
1338 if (key_equal_public(public, keys[i])) {
1339 private = keys[i];
1340 continue;
1341 }
1342 key_free(keys[i]);
1343 }
1344 xfree(keys);
1345 key_free(public);
1346 return private;
1347#else
1348 fatal("no pkcs11 support");
1349#endif /* ENABLE_PKCS11 */
1350}
1351
Damien Miller0a80ca12010-02-27 07:55:05 +11001352static void
1353do_ca_sign(struct passwd *pw, int argc, char **argv)
1354{
1355 int i, fd;
1356 u_int n;
1357 Key *ca, *public;
1358 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1359 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001360 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001361
Damien Miller4e270b02010-04-16 15:56:21 +10001362 if (key_type_name != NULL) {
1363 switch (key_type_from_name(key_type_name)) {
1364 case KEY_RSA_CERT_V00:
1365 case KEY_DSA_CERT_V00:
1366 v00 = 1;
1367 break;
1368 case KEY_UNSPEC:
1369 if (strcasecmp(key_type_name, "v00") == 0) {
1370 v00 = 1;
1371 break;
1372 } else if (strcasecmp(key_type_name, "v01") == 0)
1373 break;
1374 /* FALLTHROUGH */
1375 default:
1376 fprintf(stderr, "unknown key type %s\n", key_type_name);
1377 exit(1);
1378 }
1379 }
1380
Damien Miller757f34e2010-08-05 13:05:31 +10001381 pkcs11_init(1);
1382 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1383 if (pkcs11provider != NULL) {
1384 if ((ca = load_pkcs11_key(tmp)) == NULL)
1385 fatal("No PKCS#11 key matching %s found", ca_key_path);
1386 } else if ((ca = load_identity(tmp)) == NULL)
1387 fatal("Couldn't load CA key \"%s\"", tmp);
1388 xfree(tmp);
1389
Damien Miller0a80ca12010-02-27 07:55:05 +11001390 for (i = 0; i < argc; i++) {
1391 /* Split list of principals */
1392 n = 0;
1393 if (cert_principals != NULL) {
1394 otmp = tmp = xstrdup(cert_principals);
1395 plist = NULL;
1396 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1397 plist = xrealloc(plist, n + 1, sizeof(*plist));
1398 if (*(plist[n] = xstrdup(cp)) == '\0')
1399 fatal("Empty principal name");
1400 }
1401 xfree(otmp);
1402 }
1403
1404 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1405 if ((public = key_load_public(tmp, &comment)) == NULL)
1406 fatal("%s: unable to open \"%s\"", __func__, tmp);
1407 if (public->type != KEY_RSA && public->type != KEY_DSA)
1408 fatal("%s: key \"%s\" type %s cannot be certified",
1409 __func__, tmp, key_type(public));
1410
1411 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001412 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001413 fatal("Could not upgrade key %s to certificate", tmp);
1414 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001415 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001416 public->cert->key_id = xstrdup(cert_key_id);
1417 public->cert->nprincipals = n;
1418 public->cert->principals = plist;
1419 public->cert->valid_after = cert_valid_from;
1420 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001421 if (v00) {
1422 prepare_options_buf(&public->cert->critical,
1423 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1424 } else {
1425 prepare_options_buf(&public->cert->critical,
1426 OPTIONS_CRITICAL);
1427 prepare_options_buf(&public->cert->extensions,
1428 OPTIONS_EXTENSIONS);
1429 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001430 public->cert->signature_key = key_from_private(ca);
1431
1432 if (key_certify(public, ca) != 0)
1433 fatal("Couldn't not certify key %s", tmp);
1434
1435 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1436 *cp = '\0';
1437 xasprintf(&out, "%s-cert.pub", tmp);
1438 xfree(tmp);
1439
1440 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1441 fatal("Could not open \"%s\" for writing: %s", out,
1442 strerror(errno));
1443 if ((f = fdopen(fd, "w")) == NULL)
1444 fatal("%s: fdopen: %s", __func__, strerror(errno));
1445 if (!key_write(public, f))
1446 fatal("Could not write certified key to %s", out);
1447 fprintf(f, " %s\n", comment);
1448 fclose(f);
1449
Damien Miller4e270b02010-04-16 15:56:21 +10001450 if (!quiet) {
1451 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1452 "valid %s", key_cert_type(public),
1453 out, public->cert->key_id, public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001454 cert_principals != NULL ? " for " : "",
1455 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001456 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001457 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001458
1459 key_free(public);
1460 xfree(out);
1461 }
Damien Miller757f34e2010-08-05 13:05:31 +10001462 pkcs11_terminate();
Damien Miller0a80ca12010-02-27 07:55:05 +11001463 exit(0);
1464}
1465
1466static u_int64_t
1467parse_relative_time(const char *s, time_t now)
1468{
1469 int64_t mul, secs;
1470
1471 mul = *s == '-' ? -1 : 1;
1472
1473 if ((secs = convtime(s + 1)) == -1)
1474 fatal("Invalid relative certificate time %s", s);
1475 if (mul == -1 && secs > now)
1476 fatal("Certificate time %s cannot be represented", s);
1477 return now + (u_int64_t)(secs * mul);
1478}
1479
1480static u_int64_t
1481parse_absolute_time(const char *s)
1482{
1483 struct tm tm;
1484 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001485 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001486
Damien Miller2ca342b2010-03-03 12:14:15 +11001487 /*
1488 * POSIX strptime says "The application shall ensure that there
1489 * is white-space or other non-alphanumeric characters between
1490 * any two conversion specifications" so arrange things this way.
1491 */
1492 switch (strlen(s)) {
1493 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001494 fmt = "%Y-%m-%d";
1495 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001496 break;
1497 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001498 fmt = "%Y-%m-%dT%H:%M:%S";
1499 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001500 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1501 break;
1502 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001503 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001504 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001505
1506 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001507 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001508 fatal("Invalid certificate time %s", s);
1509 if ((tt = mktime(&tm)) < 0)
1510 fatal("Certificate time %s cannot be represented", s);
1511 return (u_int64_t)tt;
1512}
1513
1514static void
1515parse_cert_times(char *timespec)
1516{
1517 char *from, *to;
1518 time_t now = time(NULL);
1519 int64_t secs;
1520
1521 /* +timespec relative to now */
1522 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1523 if ((secs = convtime(timespec + 1)) == -1)
1524 fatal("Invalid relative certificate life %s", timespec);
1525 cert_valid_to = now + secs;
1526 /*
1527 * Backdate certificate one minute to avoid problems on hosts
1528 * with poorly-synchronised clocks.
1529 */
1530 cert_valid_from = ((now - 59)/ 60) * 60;
1531 return;
1532 }
1533
1534 /*
1535 * from:to, where
1536 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1537 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1538 */
1539 from = xstrdup(timespec);
1540 to = strchr(from, ':');
1541 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001542 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001543 *to++ = '\0';
1544
1545 if (*from == '-' || *from == '+')
1546 cert_valid_from = parse_relative_time(from, now);
1547 else
1548 cert_valid_from = parse_absolute_time(from);
1549
1550 if (*to == '-' || *to == '+')
1551 cert_valid_to = parse_relative_time(to, cert_valid_from);
1552 else
1553 cert_valid_to = parse_absolute_time(to);
1554
1555 if (cert_valid_to <= cert_valid_from)
1556 fatal("Empty certificate validity interval");
1557 xfree(from);
1558}
1559
1560static void
Damien Miller4e270b02010-04-16 15:56:21 +10001561add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001562{
1563 char *val;
1564
1565 if (strcmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001566 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001567 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001568 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001569 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001570 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001571 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001572 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001573 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001574 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001575 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001576 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001577 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001578 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001579 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001580 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001581 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001582 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001583 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001584 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001585 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001586 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001587 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1588 val = opt + 14;
1589 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001590 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001591 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001592 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001593 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001594 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1595 val = opt + 15;
1596 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001597 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001598 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001599 fatal("source-address already specified");
1600 if (addr_match_cidr_list(NULL, val) != 0)
1601 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001602 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001603 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001604 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001605}
1606
Ben Lindstrombba81212001-06-25 05:01:22 +00001607static void
Damien Millerd834d352010-06-26 09:48:02 +10001608show_options(const Buffer *optbuf, int v00, int in_critical)
1609{
1610 u_char *name, *data;
1611 u_int dlen;
1612 Buffer options, option;
1613
1614 buffer_init(&options);
1615 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1616
1617 buffer_init(&option);
1618 while (buffer_len(&options) != 0) {
1619 name = buffer_get_string(&options, NULL);
1620 data = buffer_get_string_ptr(&options, &dlen);
1621 buffer_append(&option, data, dlen);
1622 printf(" %s", name);
1623 if ((v00 || !in_critical) &&
1624 (strcmp(name, "permit-X11-forwarding") == 0 ||
1625 strcmp(name, "permit-agent-forwarding") == 0 ||
1626 strcmp(name, "permit-port-forwarding") == 0 ||
1627 strcmp(name, "permit-pty") == 0 ||
1628 strcmp(name, "permit-user-rc") == 0))
1629 printf("\n");
1630 else if ((v00 || in_critical) &&
1631 (strcmp(name, "force-command") == 0 ||
1632 strcmp(name, "source-address") == 0)) {
1633 data = buffer_get_string(&option, NULL);
1634 printf(" %s\n", data);
1635 xfree(data);
1636 } else {
1637 printf(" UNKNOWN OPTION (len %u)\n",
1638 buffer_len(&option));
1639 buffer_clear(&option);
1640 }
1641 xfree(name);
1642 if (buffer_len(&option) != 0)
1643 fatal("Option corrupt: extra data at end");
1644 }
1645 buffer_free(&option);
1646 buffer_free(&options);
1647}
1648
1649static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001650do_show_cert(struct passwd *pw)
1651{
1652 Key *key;
1653 struct stat st;
1654 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001655 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001656
1657 if (!have_identity)
1658 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001659 if (stat(identity_file, &st) < 0)
1660 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001661 if ((key = key_load_public(identity_file, NULL)) == NULL)
1662 fatal("%s is not a public key", identity_file);
1663 if (!key_is_cert(key))
1664 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001665 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1666
Damien Millerf2b70ca2010-03-05 07:39:35 +11001667 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1668 ca_fp = key_fingerprint(key->cert->signature_key,
1669 SSH_FP_MD5, SSH_FP_HEX);
1670
1671 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001672 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1673 key_cert_type(key));
1674 printf(" Public key: %s %s\n", key_type(key), key_fp);
1675 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001676 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001677 printf(" Key ID: \"%s\"\n", key->cert->key_id);
1678 if (!v00)
1679 printf(" Serial: %llu\n", key->cert->serial);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001680 printf(" Valid: %s\n",
1681 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1682 printf(" Principals: ");
1683 if (key->cert->nprincipals == 0)
1684 printf("(none)\n");
1685 else {
1686 for (i = 0; i < key->cert->nprincipals; i++)
1687 printf("\n %s",
1688 key->cert->principals[i]);
1689 printf("\n");
1690 }
Damien Miller4e270b02010-04-16 15:56:21 +10001691 printf(" Critical Options: ");
1692 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001693 printf("(none)\n");
1694 else {
1695 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001696 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001697 }
Damien Miller4e270b02010-04-16 15:56:21 +10001698 if (!v00) {
1699 printf(" Extensions: ");
1700 if (buffer_len(&key->cert->extensions) == 0)
1701 printf("(none)\n");
1702 else {
1703 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001704 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001705 }
1706 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001707 exit(0);
1708}
1709
1710static void
Damien Miller431f66b1999-11-21 18:31:57 +11001711usage(void)
1712{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001713 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001714 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001715 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001716 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001717 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001718 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001719 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001720#ifdef ENABLE_PKCS11
1721 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1722#endif
Damien Miller44b25042010-07-02 13:35:01 +10001723 fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001724 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1725 fprintf(stderr, " -f filename Filename of the key file.\n");
1726 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1727 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1728 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001729 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1730 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Damien Miller44b25042010-07-02 13:35:01 +10001731 fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001732 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001733 fprintf(stderr, " -l Show fingerprint of key file.\n");
1734 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001735 fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001736 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001737 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller1f181422010-04-18 08:08:03 +10001738 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001739 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1740 fprintf(stderr, " -p Change passphrase of private key file.\n");
1741 fprintf(stderr, " -q Quiet.\n");
1742 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1743 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1744 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001745 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001746 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1747 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001748 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001749 fprintf(stderr, " -v Verbose.\n");
1750 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1751 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001752 fprintf(stderr, " -z serial Specify a serial number.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001753
Damien Miller95def091999-11-25 00:26:21 +11001754 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001755}
1756
Damien Miller95def091999-11-25 00:26:21 +11001757/*
1758 * Main program for key management.
1759 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001760int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001761main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001762{
Damien Millera41c8b12002-01-22 23:05:08 +11001763 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller757f34e2010-08-05 13:05:31 +10001764 char out_file[MAXPATHLEN], *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001765 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001766 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001767 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001768 int opt, type, fd;
Damien Millerbebbb7e2010-05-10 11:54:38 +10001769 u_int maxbits;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001770 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001771 int do_gen_candidates = 0, do_screen_candidates = 0;
1772 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001773 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001774 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001775
Damien Miller95def091999-11-25 00:26:21 +11001776 extern int optind;
1777 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001778
Darren Tuckerce321d82005-10-03 18:11:24 +10001779 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1780 sanitise_stdfd();
1781
Darren Tucker9ac56e92007-01-14 10:19:59 +11001782 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001783
Damien Millerd3a18572000-06-07 19:55:44 +10001784 SSLeay_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001785 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001786
Kevin Steves3a881912002-07-20 19:05:40 +00001787 init_rng();
1788 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001789
Damien Miller5428f641999-11-25 11:54:57 +11001790 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001791 pw = getpwuid(getuid());
1792 if (!pw) {
1793 printf("You don't exist, go away!\n");
1794 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001795 }
Damien Millereba71ba2000-04-29 23:57:08 +10001796 if (gethostname(hostname, sizeof(hostname)) < 0) {
1797 perror("gethostname");
1798 exit(1);
1799 }
Damien Miller5428f641999-11-25 11:54:57 +11001800
Damien Miller44b25042010-07-02 13:35:01 +10001801 while ((opt = getopt(argc, argv, "degiqpclBHLhvxXyF:b:f:t:D:I:P:m:N:n:"
Damien Miller4e270b02010-04-16 15:56:21 +10001802 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001803 switch (opt) {
1804 case 'b':
Damien Miller5f340062006-03-26 14:27:57 +11001805 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001806 if (errstr)
1807 fatal("Bits has bad value %s (%s)",
1808 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001809 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001810 case 'F':
1811 find_host = 1;
1812 rr_hostname = optarg;
1813 break;
1814 case 'H':
1815 hash_hosts = 1;
1816 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001817 case 'I':
1818 cert_key_id = optarg;
1819 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001820 case 'R':
1821 delete_host = 1;
1822 rr_hostname = optarg;
1823 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001824 case 'L':
1825 show_cert = 1;
1826 break;
Damien Miller95def091999-11-25 00:26:21 +11001827 case 'l':
1828 print_fingerprint = 1;
1829 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001830 case 'B':
1831 print_bubblebabble = 1;
1832 break;
Damien Miller44b25042010-07-02 13:35:01 +10001833 case 'm':
1834 if (strcasecmp(optarg, "RFC4716") == 0 ||
1835 strcasecmp(optarg, "ssh2") == 0) {
1836 convert_format = FMT_RFC4716;
1837 break;
1838 }
1839 if (strcasecmp(optarg, "PKCS8") == 0) {
1840 convert_format = FMT_PKCS8;
1841 break;
1842 }
1843 if (strcasecmp(optarg, "PEM") == 0) {
1844 convert_format = FMT_PEM;
1845 break;
1846 }
1847 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001848 case 'n':
1849 cert_principals = optarg;
1850 break;
Damien Miller95def091999-11-25 00:26:21 +11001851 case 'p':
1852 change_passphrase = 1;
1853 break;
Damien Miller95def091999-11-25 00:26:21 +11001854 case 'c':
1855 change_comment = 1;
1856 break;
Damien Miller95def091999-11-25 00:26:21 +11001857 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001858 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1859 sizeof(identity_file))
1860 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001861 have_identity = 1;
1862 break;
Damien Miller37876e92003-05-15 10:19:46 +10001863 case 'g':
1864 print_generic = 1;
1865 break;
Damien Miller95def091999-11-25 00:26:21 +11001866 case 'P':
1867 identity_passphrase = optarg;
1868 break;
Damien Miller95def091999-11-25 00:26:21 +11001869 case 'N':
1870 identity_new_passphrase = optarg;
1871 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001872 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10001873 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001874 break;
Damien Miller95def091999-11-25 00:26:21 +11001875 case 'C':
1876 identity_comment = optarg;
1877 break;
Damien Miller95def091999-11-25 00:26:21 +11001878 case 'q':
1879 quiet = 1;
1880 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001881 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001882 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001883 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10001884 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10001885 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001886 case 'h':
1887 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001888 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001889 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001890 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001891 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001892 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10001893 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10001894 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001895 case 'y':
1896 print_public = 1;
1897 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001898 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001899 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001900 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001901 case 's':
1902 ca_key_path = optarg;
1903 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001904 case 't':
1905 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001906 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001907 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001908 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001909 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001910 case 'v':
1911 if (log_level == SYSLOG_LEVEL_INFO)
1912 log_level = SYSLOG_LEVEL_DEBUG1;
1913 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001914 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001915 log_level < SYSLOG_LEVEL_DEBUG3)
1916 log_level++;
1917 }
1918 break;
Damien Miller37876e92003-05-15 10:19:46 +10001919 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001920 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001921 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001922 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001923 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1924 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001925 if (errstr)
1926 fatal("Desired generator has bad value: %s (%s)",
1927 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001928 break;
1929 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001930 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001931 if (errstr)
1932 fatal("Invalid number of trials: %s (%s)",
1933 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001934 break;
1935 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001936 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10001937 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10001938 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10001939 break;
1940 case 'G':
1941 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001942 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1943 sizeof(out_file))
1944 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001945 break;
1946 case 'T':
1947 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001948 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1949 sizeof(out_file))
1950 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001951 break;
1952 case 'S':
1953 /* XXX - also compare length against bits */
1954 if (BN_hex2bn(&start, optarg) == 0)
1955 fatal("Invalid start point.");
1956 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001957 case 'V':
1958 parse_cert_times(optarg);
1959 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001960 case 'z':
1961 cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
1962 if (errstr)
1963 fatal("Invalid serial number: %s", errstr);
1964 break;
Damien Miller95def091999-11-25 00:26:21 +11001965 case '?':
1966 default:
1967 usage();
1968 }
1969 }
Darren Tucker06930c72003-12-31 11:34:51 +11001970
1971 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11001972 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11001973
Damien Miller0a80ca12010-02-27 07:55:05 +11001974 argv += optind;
1975 argc -= optind;
1976
1977 if (ca_key_path != NULL) {
1978 if (argc < 1) {
1979 printf("Too few arguments.\n");
1980 usage();
1981 }
1982 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11001983 printf("Too many arguments.\n");
1984 usage();
1985 }
1986 if (change_passphrase && change_comment) {
1987 printf("Can only have one of -p and -c.\n");
1988 usage();
1989 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10001990 if (print_fingerprint && (delete_host || hash_hosts)) {
1991 printf("Cannot use -l with -D or -R.\n");
1992 usage();
1993 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001994 if (ca_key_path != NULL) {
1995 if (cert_key_id == NULL)
1996 fatal("Must specify key id (-I) when certifying");
1997 do_ca_sign(pw, argc, argv);
1998 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001999 if (show_cert)
2000 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002001 if (delete_host || hash_hosts || find_host)
2002 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002003 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002004 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002005 if (change_passphrase)
2006 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002007 if (change_comment)
2008 do_change_comment(pw);
Damien Miller44b25042010-07-02 13:35:01 +10002009 if (convert_to)
2010 do_convert_to(pw);
2011 if (convert_from)
2012 do_convert_from(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10002013 if (print_public)
2014 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002015 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002016 unsigned int n = 0;
2017
2018 if (have_identity) {
2019 n = do_print_resource_record(pw,
2020 identity_file, rr_hostname);
2021 if (n == 0) {
2022 perror(identity_file);
2023 exit(1);
2024 }
2025 exit(0);
2026 } else {
2027
2028 n += do_print_resource_record(pw,
2029 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2030 n += do_print_resource_record(pw,
2031 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
2032
2033 if (n == 0)
2034 fatal("no keys found.");
2035 exit(0);
2036 }
Damien Miller37876e92003-05-15 10:19:46 +10002037 }
Damien Miller7ea845e2010-02-12 09:21:02 +11002038 if (pkcs11provider != NULL)
Damien Miller757f34e2010-08-05 13:05:31 +10002039 do_download(pw);
Damien Miller95def091999-11-25 00:26:21 +11002040
Darren Tucker019cefe2003-08-02 22:40:07 +10002041 if (do_gen_candidates) {
2042 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002043
Darren Tucker019cefe2003-08-02 22:40:07 +10002044 if (out == NULL) {
2045 error("Couldn't open modulus candidate file \"%s\": %s",
2046 out_file, strerror(errno));
2047 return (1);
2048 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002049 if (bits == 0)
2050 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002051 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002052 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002053
2054 return (0);
2055 }
2056
2057 if (do_screen_candidates) {
2058 FILE *in;
2059 FILE *out = fopen(out_file, "w");
2060
2061 if (have_identity && strcmp(identity_file, "-") != 0) {
2062 if ((in = fopen(identity_file, "r")) == NULL) {
2063 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002064 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002065 strerror(errno));
2066 }
2067 } else
2068 in = stdin;
2069
2070 if (out == NULL) {
2071 fatal("Couldn't open moduli file \"%s\": %s",
2072 out_file, strerror(errno));
2073 }
2074 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002075 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002076 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002077 }
2078
Damien Miller95def091999-11-25 00:26:21 +11002079 arc4random_stir();
2080
Damien Millerf14be5c2005-11-05 15:15:49 +11002081 if (key_type_name == NULL)
2082 key_type_name = "rsa";
2083
Damien Millere39cacc2000-11-29 12:18:44 +11002084 type = key_type_from_name(key_type_name);
2085 if (type == KEY_UNSPEC) {
2086 fprintf(stderr, "unknown key type %s\n", key_type_name);
2087 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10002088 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002089 if (bits == 0)
2090 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
Damien Millerbebbb7e2010-05-10 11:54:38 +10002091 maxbits = (type == KEY_DSA) ?
2092 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
2093 if (bits > maxbits) {
2094 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
2095 exit(1);
2096 }
Tim Rice660c3402005-11-28 17:45:32 -08002097 if (type == KEY_DSA && bits != 1024)
2098 fatal("DSA keys must be 1024 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11002099 if (!quiet)
2100 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002101 private = key_generate(type, bits);
2102 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002103 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002104 exit(1);
2105 }
2106 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002107
2108 if (!have_identity)
2109 ask_filename(pw, "Enter file in which to save the key");
2110
Damien Miller788f2122005-11-05 15:14:59 +11002111 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002112 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2113 pw->pw_dir, _PATH_SSH_USER_DIR);
2114 if (strstr(identity_file, dotsshdir) != NULL) {
2115 if (stat(dotsshdir, &st) < 0) {
2116 if (errno != ENOENT) {
2117 error("Could not stat %s: %s", dotsshdir,
2118 strerror(errno));
2119 } else if (mkdir(dotsshdir, 0700) < 0) {
2120 error("Could not create directory '%s': %s",
2121 dotsshdir, strerror(errno));
2122 } else if (!quiet)
2123 printf("Created directory '%s'.\n", dotsshdir);
2124 }
Damien Miller95def091999-11-25 00:26:21 +11002125 }
2126 /* If the file already exists, ask the user to confirm. */
2127 if (stat(identity_file, &st) >= 0) {
2128 char yesno[3];
2129 printf("%s already exists.\n", identity_file);
2130 printf("Overwrite (y/n)? ");
2131 fflush(stdout);
2132 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2133 exit(1);
2134 if (yesno[0] != 'y' && yesno[0] != 'Y')
2135 exit(1);
2136 }
2137 /* Ask for a passphrase (twice). */
2138 if (identity_passphrase)
2139 passphrase1 = xstrdup(identity_passphrase);
2140 else if (identity_new_passphrase)
2141 passphrase1 = xstrdup(identity_new_passphrase);
2142 else {
2143passphrase_again:
2144 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002145 read_passphrase("Enter passphrase (empty for no "
2146 "passphrase): ", RP_ALLOW_STDIN);
2147 passphrase2 = read_passphrase("Enter same passphrase again: ",
2148 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002149 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002150 /*
2151 * The passphrases do not match. Clear them and
2152 * retry.
2153 */
Damien Miller95def091999-11-25 00:26:21 +11002154 memset(passphrase1, 0, strlen(passphrase1));
2155 memset(passphrase2, 0, strlen(passphrase2));
2156 xfree(passphrase1);
2157 xfree(passphrase2);
2158 printf("Passphrases do not match. Try again.\n");
2159 goto passphrase_again;
2160 }
2161 /* Clear the other copy of the passphrase. */
2162 memset(passphrase2, 0, strlen(passphrase2));
2163 xfree(passphrase2);
2164 }
2165
Damien Miller95def091999-11-25 00:26:21 +11002166 if (identity_comment) {
2167 strlcpy(comment, identity_comment, sizeof(comment));
2168 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002169 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002170 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2171 }
2172
2173 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00002174 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002175 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002176 memset(passphrase1, 0, strlen(passphrase1));
2177 xfree(passphrase1);
2178 exit(1);
2179 }
2180 /* Clear the passphrase. */
2181 memset(passphrase1, 0, strlen(passphrase1));
2182 xfree(passphrase1);
2183
2184 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002185 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002186 arc4random_stir();
2187
2188 if (!quiet)
2189 printf("Your identification has been saved in %s.\n", identity_file);
2190
Damien Miller95def091999-11-25 00:26:21 +11002191 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002192 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2193 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002194 printf("Could not save your public key in %s\n", identity_file);
2195 exit(1);
2196 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002197 f = fdopen(fd, "w");
2198 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002199 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002200 exit(1);
2201 }
Damien Millereba71ba2000-04-29 23:57:08 +10002202 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002203 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002204 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002205 fclose(f);
2206
2207 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002208 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002209 char *ra = key_fingerprint(public, SSH_FP_MD5,
2210 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002211 printf("Your public key has been saved in %s.\n",
2212 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002213 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002214 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002215 printf("The key's randomart image is:\n");
2216 printf("%s\n", ra);
2217 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00002218 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11002219 }
Damien Millereba71ba2000-04-29 23:57:08 +10002220
2221 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002222 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002223}