blob: 60261c210a7c8050525d54081030ec82a23772a1 [file] [log] [blame]
Damien Miller0a80ca12010-02-27 07:55:05 +11001/* $OpenBSD: ssh-keygen.c,v 1.179 2010/02/26 20:29:54 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 Miller10f6f6b1999-11-17 17:29:08 +110085/* Flag indicating that we just want to see the key fingerprint */
86int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000087int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110088
Damien Miller431f66b1999-11-21 18:31:57 +110089/* The identity file name, given on the command line or entered by the user. */
90char identity_file[1024];
91int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* This is set to the passphrase if given on the command line. */
94char *identity_passphrase = NULL;
95
96/* This is set to the new passphrase if given on the command line. */
97char *identity_new_passphrase = NULL;
98
99/* This is set to the new comment if given on the command line. */
100char *identity_comment = NULL;
101
Damien Miller0a80ca12010-02-27 07:55:05 +1100102/* Path to CA key when certifying keys. */
103char *ca_key_path = NULL;
104
105/* Key type when certifying */
106u_int cert_key_type = SSH2_CERT_TYPE_USER;
107
108/* "key ID" of signed key */
109char *cert_key_id = NULL;
110
111/* Comma-separated list of principal names for certifying keys */
112char *cert_principals = NULL;
113
114/* Validity period for certificates */
115u_int64_t cert_valid_from = 0;
116u_int64_t cert_valid_to = ~0ULL;
117
118/* Certificate constraints */
119#define CONSTRAINT_X_FWD (1)
120#define CONSTRAINT_AGENT_FWD (1<<1)
121#define CONSTRAINT_PORT_FWD (1<<2)
122#define CONSTRAINT_PTY (1<<3)
123#define CONSTRAINT_USER_RC (1<<4)
124#define CONSTRAINT_DEFAULT (CONSTRAINT_X_FWD|CONSTRAINT_AGENT_FWD| \
125 CONSTRAINT_PORT_FWD|CONSTRAINT_PTY| \
126 CONSTRAINT_USER_RC)
127u_int32_t constraint_flags = CONSTRAINT_DEFAULT;
128char *constraint_command = NULL;
129char *constraint_src_addr = NULL;
130
Damien Millereba71ba2000-04-29 23:57:08 +1000131/* Dump public key file in format used by real and the original SSH 2 */
132int convert_to_ssh2 = 0;
133int convert_from_ssh2 = 0;
134int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000135int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100136
Damien Millera41c8b12002-01-22 23:05:08 +1100137char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000138
Damien Miller431f66b1999-11-21 18:31:57 +1100139/* argv0 */
140extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Millereba71ba2000-04-29 23:57:08 +1000142char hostname[MAXHOSTNAMELEN];
143
Darren Tucker770fc012004-05-13 16:24:32 +1000144/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000145int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000146int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
147
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static void
Damien Miller431f66b1999-11-21 18:31:57 +1100149ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150{
Damien Miller95def091999-11-25 00:26:21 +1100151 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100152 char *name = NULL;
153
Damien Miller993dd552002-02-19 15:22:47 +1100154 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000155 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100156 else {
Damien Miller993dd552002-02-19 15:22:47 +1100157 switch (key_type_from_name(key_type_name)) {
158 case KEY_RSA1:
159 name = _PATH_SSH_CLIENT_IDENTITY;
160 break;
161 case KEY_DSA:
162 name = _PATH_SSH_CLIENT_ID_DSA;
163 break;
164 case KEY_RSA:
165 name = _PATH_SSH_CLIENT_ID_RSA;
166 break;
167 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100168 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100169 exit(1);
170 break;
171 }
Damien Miller90967402006-03-26 14:07:26 +1100172 }
Damien Millere39cacc2000-11-29 12:18:44 +1100173 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000174 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100175 if (fgets(buf, sizeof(buf), stdin) == NULL)
176 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000177 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100178 if (strcmp(buf, "") != 0)
179 strlcpy(identity_file, buf, sizeof(identity_file));
180 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100181}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000184load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000185{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000186 char *pass;
187 Key *prv;
188
Ben Lindstroma3700052001-04-05 23:26:32 +0000189 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000190 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000191 if (identity_passphrase)
192 pass = xstrdup(identity_passphrase);
193 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000194 pass = read_passphrase("Enter passphrase: ",
195 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000196 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000197 memset(pass, 0, strlen(pass));
198 xfree(pass);
199 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000200 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000201}
202
Damien Miller874d77b2000-10-14 16:23:11 +1100203#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000204#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100205#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000206#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000207
Ben Lindstrombba81212001-06-25 05:01:22 +0000208static void
Damien Millereba71ba2000-04-29 23:57:08 +1000209do_convert_to_ssh2(struct passwd *pw)
210{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000211 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000212 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000213 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100214 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000215 struct stat st;
216
217 if (!have_identity)
218 ask_filename(pw, "Enter file in which the key is");
219 if (stat(identity_file, &st) < 0) {
220 perror(identity_file);
221 exit(1);
222 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000223 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000224 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000225 fprintf(stderr, "load failed\n");
226 exit(1);
227 }
Damien Millereba71ba2000-04-29 23:57:08 +1000228 }
Damien Millerdb274722003-05-14 13:45:22 +1000229 if (k->type == KEY_RSA1) {
230 fprintf(stderr, "version 1 keys are not supported\n");
231 exit(1);
232 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000233 if (key_to_blob(k, &blob, &len) <= 0) {
234 fprintf(stderr, "key_to_blob failed\n");
235 exit(1);
236 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100237 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
238 snprintf(comment, sizeof(comment),
239 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000240 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000241 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100242
243 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
244 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000245 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100246 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000247 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000248 xfree(blob);
249 exit(0);
250}
251
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static void
Damien Miller874d77b2000-10-14 16:23:11 +1100253buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
254{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000255 u_int bignum_bits = buffer_get_int(b);
256 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000257
Damien Miller874d77b2000-10-14 16:23:11 +1100258 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000259 fatal("buffer_get_bignum_bits: input buffer too small: "
260 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100261 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
262 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100263 buffer_consume(b, bytes);
264}
265
Ben Lindstrombba81212001-06-25 05:01:22 +0000266static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000267do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100268{
269 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100270 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100271 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000272 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000273 int magic, rlen, ktype, i1, i2, i3, i4;
274 u_int slen;
275 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100276
277 buffer_init(&b);
278 buffer_append(&b, blob, blen);
279
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100280 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100281 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
282 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
283 buffer_free(&b);
284 return NULL;
285 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000286 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100287 type = buffer_get_string(&b, NULL);
288 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000289 i2 = buffer_get_int(&b);
290 i3 = buffer_get_int(&b);
291 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100292 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100293 if (strcmp(cipher, "none") != 0) {
294 error("unsupported cipher %s", cipher);
295 xfree(cipher);
296 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000297 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100298 return NULL;
299 }
300 xfree(cipher);
301
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000302 if (strstr(type, "dsa")) {
303 ktype = KEY_DSA;
304 } else if (strstr(type, "rsa")) {
305 ktype = KEY_RSA;
306 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100307 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000308 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100309 return NULL;
310 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000311 key = key_new_private(ktype);
312 xfree(type);
313
314 switch (key->type) {
315 case KEY_DSA:
316 buffer_get_bignum_bits(&b, key->dsa->p);
317 buffer_get_bignum_bits(&b, key->dsa->g);
318 buffer_get_bignum_bits(&b, key->dsa->q);
319 buffer_get_bignum_bits(&b, key->dsa->pub_key);
320 buffer_get_bignum_bits(&b, key->dsa->priv_key);
321 break;
322 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100323 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000324 debug("e %lx", e);
325 if (e < 30) {
326 e <<= 8;
327 e += buffer_get_char(&b);
328 debug("e %lx", e);
329 e <<= 8;
330 e += buffer_get_char(&b);
331 debug("e %lx", e);
332 }
333 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000334 buffer_free(&b);
335 key_free(key);
336 return NULL;
337 }
338 buffer_get_bignum_bits(&b, key->rsa->d);
339 buffer_get_bignum_bits(&b, key->rsa->n);
340 buffer_get_bignum_bits(&b, key->rsa->iqmp);
341 buffer_get_bignum_bits(&b, key->rsa->q);
342 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000343 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000344 break;
345 }
Damien Miller874d77b2000-10-14 16:23:11 +1100346 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000347 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000348 error("do_convert_private_ssh2_from_blob: "
349 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100350 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000351
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000352 /* try the key */
353 key_sign(key, &sig, &slen, data, sizeof(data));
354 key_verify(key, sig, slen, data, sizeof(data));
355 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100356 return key;
357}
358
Damien Miller8056a9d2006-03-15 12:05:40 +1100359static int
360get_line(FILE *fp, char *line, size_t len)
361{
362 int c;
363 size_t pos = 0;
364
365 line[0] = '\0';
366 while ((c = fgetc(fp)) != EOF) {
367 if (pos >= len - 1) {
368 fprintf(stderr, "input line too long.\n");
369 exit(1);
370 }
Damien Miller90967402006-03-26 14:07:26 +1100371 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100372 case '\r':
373 c = fgetc(fp);
374 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
375 fprintf(stderr, "unget: %s\n", strerror(errno));
376 exit(1);
377 }
378 return pos;
379 case '\n':
380 return pos;
381 }
382 line[pos++] = c;
383 line[pos] = '\0';
384 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100385 /* We reached EOF */
386 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100387}
388
Ben Lindstrombba81212001-06-25 05:01:22 +0000389static void
Damien Millereba71ba2000-04-29 23:57:08 +1000390do_convert_from_ssh2(struct passwd *pw)
391{
392 Key *k;
393 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000394 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100395 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000396 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000397 char encoded[8096];
398 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100399 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000400 FILE *fp;
401
402 if (!have_identity)
403 ask_filename(pw, "Enter file in which the key is");
404 if (stat(identity_file, &st) < 0) {
405 perror(identity_file);
406 exit(1);
407 }
408 fp = fopen(identity_file, "r");
409 if (fp == NULL) {
410 perror(identity_file);
411 exit(1);
412 }
413 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100414 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
415 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000416 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000417 if (strncmp(line, "----", 4) == 0 ||
418 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100419 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
420 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000421 if (strstr(line, " END ") != NULL) {
422 break;
423 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000424 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000425 continue;
426 }
Damien Miller30c3d422000-05-09 11:02:59 +1000427 if (escaped) {
428 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000429 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000430 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000431 }
Damien Millereba71ba2000-04-29 23:57:08 +1000432 strlcat(encoded, line, sizeof(encoded));
433 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000434 len = strlen(encoded);
435 if (((len % 4) == 3) &&
436 (encoded[len-1] == '=') &&
437 (encoded[len-2] == '=') &&
438 (encoded[len-3] == '='))
439 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100440 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000441 if (blen < 0) {
442 fprintf(stderr, "uudecode failed.\n");
443 exit(1);
444 }
Damien Miller874d77b2000-10-14 16:23:11 +1100445 k = private ?
446 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100447 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100448 if (k == NULL) {
449 fprintf(stderr, "decode blob failed.\n");
450 exit(1);
451 }
452 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000453 (k->type == KEY_DSA ?
454 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
455 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100456 key_write(k, stdout);
457 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100458 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100459 exit(1);
460 }
Damien Millereba71ba2000-04-29 23:57:08 +1000461 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100462 if (!private)
463 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000464 fclose(fp);
465 exit(0);
466}
467
Ben Lindstrombba81212001-06-25 05:01:22 +0000468static void
Damien Millereba71ba2000-04-29 23:57:08 +1000469do_print_public(struct passwd *pw)
470{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000471 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000472 struct stat st;
473
474 if (!have_identity)
475 ask_filename(pw, "Enter file in which the key is");
476 if (stat(identity_file, &st) < 0) {
477 perror(identity_file);
478 exit(1);
479 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000480 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000481 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000482 fprintf(stderr, "load failed\n");
483 exit(1);
484 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000485 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000486 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000487 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000488 fprintf(stdout, "\n");
489 exit(0);
490}
491
Ben Lindstromcd392282001-07-04 03:44:03 +0000492static void
Damien Miller86cbb442010-02-12 09:22:57 +1100493do_download(struct passwd *pw, char *pkcs11provider)
Ben Lindstromcd392282001-07-04 03:44:03 +0000494{
Damien Miller7ea845e2010-02-12 09:21:02 +1100495#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000496 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100497 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000498
Damien Miller7ea845e2010-02-12 09:21:02 +1100499 pkcs11_init(0);
500 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
501 if (nkeys <= 0)
502 fatal("cannot read public key from pkcs11");
503 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000504 key_write(keys[i], stdout);
505 key_free(keys[i]);
506 fprintf(stdout, "\n");
507 }
508 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100509 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000510 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100511#else
512 fatal("no pkcs11 support");
513#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000514}
Ben Lindstromcd392282001-07-04 03:44:03 +0000515
Ben Lindstrombba81212001-06-25 05:01:22 +0000516static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100517do_fingerprint(struct passwd *pw)
518{
Damien Miller98c7ad62000-03-09 21:27:49 +1100519 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000520 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000521 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100522 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000523 enum fp_rep rep;
524 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100525 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100526
Ben Lindstromd0fca422001-03-26 13:44:06 +0000527 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
528 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000529
Damien Miller95def091999-11-25 00:26:21 +1100530 if (!have_identity)
531 ask_filename(pw, "Enter file in which the key is");
532 if (stat(identity_file, &st) < 0) {
533 perror(identity_file);
534 exit(1);
535 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000536 public = key_load_public(identity_file, &comment);
537 if (public != NULL) {
538 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100539 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000540 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
541 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000542 if (log_level >= SYSLOG_LEVEL_VERBOSE)
543 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100544 key_free(public);
545 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000546 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000547 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100548 exit(0);
549 }
Damien Miller40b59852006-06-13 13:00:25 +1000550 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000551 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000552 comment = NULL;
553 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100554
555 f = fopen(identity_file, "r");
556 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100557 while (fgets(line, sizeof(line), f)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000558 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100559 error("line %d too long: %.40s...",
560 num + 1, line);
Damien Miller98c7ad62000-03-09 21:27:49 +1100561 skip = 1;
562 continue;
Damien Miller95def091999-11-25 00:26:21 +1100563 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100564 num++;
565 if (skip) {
566 skip = 0;
567 continue;
568 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000569 *cp = '\0';
Damien Miller98c7ad62000-03-09 21:27:49 +1100570
571 /* Skip leading whitespace, empty and comment lines. */
572 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
573 ;
574 if (!*cp || *cp == '\n' || *cp == '#')
Damien Miller80163902007-01-05 16:30:16 +1100575 continue;
Damien Miller98c7ad62000-03-09 21:27:49 +1100576 i = strtol(cp, &ep, 10);
577 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
578 int quoted = 0;
579 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000580 for (; *cp && (quoted || (*cp != ' ' &&
581 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100582 if (*cp == '\\' && cp[1] == '"')
583 cp++; /* Skip both */
584 else if (*cp == '"')
585 quoted = !quoted;
586 }
587 if (!*cp)
588 continue;
589 *cp++ = '\0';
590 }
591 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000592 public = key_new(KEY_RSA1);
593 if (key_read(public, &cp) != 1) {
594 cp = ep;
595 key_free(public);
596 public = key_new(KEY_UNSPEC);
597 if (key_read(public, &cp) != 1) {
598 key_free(public);
599 continue;
600 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100601 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000602 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000603 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100604 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000605 printf("%u %s %s (%s)\n", key_size(public), fp,
606 comment ? comment : "no comment", key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000607 if (log_level >= SYSLOG_LEVEL_VERBOSE)
608 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000609 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000610 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000611 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000612 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100613 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100614 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100615 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100616 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100617 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100618 exit(1);
619 }
Damien Miller95def091999-11-25 00:26:21 +1100620 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100621}
622
Damien Miller4b42d7f2005-03-01 21:48:35 +1100623static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100624printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100625{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000626 if (print_fingerprint) {
627 enum fp_rep rep;
628 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000629 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000630
631 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
632 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
633 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100634 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000635 printf("%u %s %s (%s)\n", key_size(public), fp, name,
636 key_type(public));
637 if (log_level >= SYSLOG_LEVEL_VERBOSE)
638 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000639 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000640 xfree(fp);
641 } else {
642 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
643 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100644 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000645 if (!key_write(public, f))
646 fatal("key_write failed");
647 fprintf(f, "\n");
648 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100649}
650
651static void
652do_known_hosts(struct passwd *pw, const char *name)
653{
654 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100655 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100656 char *cp, *cp2, *kp, *kp2;
657 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100658 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100659 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100660
661 if (!have_identity) {
662 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
663 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
664 sizeof(identity_file))
665 fatal("Specified known hosts path too long");
666 xfree(cp);
667 have_identity = 1;
668 }
669 if ((in = fopen(identity_file, "r")) == NULL)
670 fatal("fopen: %s", strerror(errno));
671
672 /*
673 * Find hosts goes to stdout, hash and deletions happen in-place
674 * A corner case is ssh-keygen -HF foo, which should go to stdout
675 */
676 if (!find_host && (hash_hosts || delete_host)) {
677 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
678 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
679 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
680 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
681 fatal("known_hosts path too long");
682 umask(077);
683 if ((c = mkstemp(tmp)) == -1)
684 fatal("mkstemp: %s", strerror(errno));
685 if ((out = fdopen(c, "w")) == NULL) {
686 c = errno;
687 unlink(tmp);
688 fatal("fdopen: %s", strerror(c));
689 }
690 inplace = 1;
691 }
692
693 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000694 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100695 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100696 skip = 1;
697 invalid = 1;
698 continue;
699 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000700 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100701 if (skip) {
702 skip = 0;
703 continue;
704 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000705 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100706
707 /* Skip leading whitespace, empty and comment lines. */
708 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
709 ;
710 if (!*cp || *cp == '\n' || *cp == '#') {
711 if (inplace)
712 fprintf(out, "%s\n", cp);
713 continue;
714 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100715 /* Check whether this is a CA key */
716 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
717 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
718 cp[sizeof(CA_MARKER) - 1] == '\t')) {
719 ca = 1;
720 cp += sizeof(CA_MARKER);
721 } else
722 ca = 0;
723
Damien Miller4b42d7f2005-03-01 21:48:35 +1100724 /* Find the end of the host name portion. */
725 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
726 ;
Damien Miller0a80ca12010-02-27 07:55:05 +1100727
Damien Miller4b42d7f2005-03-01 21:48:35 +1100728 if (*kp == '\0' || *(kp + 1) == '\0') {
729 error("line %d missing key: %.40s...",
730 num, line);
731 invalid = 1;
732 continue;
733 }
734 *kp++ = '\0';
735 kp2 = kp;
736
Damien Miller0a80ca12010-02-27 07:55:05 +1100737 pub = key_new(KEY_RSA1);
738 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100739 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +1100740 key_free(pub);
741 pub = key_new(KEY_UNSPEC);
742 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100743 error("line %d invalid key: %.40s...",
744 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100745 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100746 invalid = 1;
747 continue;
748 }
749 }
750
751 if (*cp == HASH_DELIM) {
752 if (find_host || delete_host) {
753 cp2 = host_hash(name, cp, strlen(cp));
754 if (cp2 == NULL) {
755 error("line %d: invalid hashed "
756 "name: %.64s...", num, line);
757 invalid = 1;
758 continue;
759 }
760 c = (strcmp(cp2, cp) == 0);
761 if (find_host && c) {
762 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100763 "line %d type %s%s\n", name,
764 num, key_type(pub),
765 ca ? " (CA key)" : "");
766 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100767 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100768 if (delete_host && !c && !ca)
769 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100770 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +1100771 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100772 } else {
773 if (find_host || delete_host) {
774 c = (match_hostname(name, cp,
775 strlen(cp)) == 1);
776 if (find_host && c) {
777 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100778 "line %d type %s%s\n", name,
779 num, key_type(pub),
780 ca ? " (CA key)" : "");
781 printhost(out, name, pub,
782 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100783 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100784 if (delete_host && !c && !ca)
785 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100786 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100787 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100788 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100789 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100790 if (ca) {
791 fprintf(stderr, "Warning: "
792 "ignoring CA key for host: "
793 "%.64s\n", cp2);
794 printhost(out, cp2, pub, ca, 0);
795 } else if (strcspn(cp2, "*?!") !=
796 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +1100797 fprintf(stderr, "Warning: "
798 "ignoring host name with "
799 "metacharacters: %.64s\n",
800 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +1100801 printhost(out, cp2, pub, ca, 0);
802 } else
803 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +1100804 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100805 has_unhashed = 1;
806 }
807 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100808 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100809 }
810 fclose(in);
811
812 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100813 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +1100814 identity_file);
815 if (inplace) {
816 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +1100817 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100818 fclose(out);
819 unlink(tmp);
820 }
821 exit(1);
822 }
823
824 if (inplace) {
825 fclose(out);
826
827 /* Backup existing file */
828 if (unlink(old) == -1 && errno != ENOENT)
829 fatal("unlink %.100s: %s", old, strerror(errno));
830 if (link(identity_file, old) == -1)
831 fatal("link %.100s to %.100s: %s", identity_file, old,
832 strerror(errno));
833 /* Move new one into place */
834 if (rename(tmp, identity_file) == -1) {
835 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
836 strerror(errno));
837 unlink(tmp);
838 unlink(old);
839 exit(1);
840 }
841
842 fprintf(stderr, "%s updated.\n", identity_file);
843 fprintf(stderr, "Original contents retained as %s\n", old);
844 if (has_unhashed) {
845 fprintf(stderr, "WARNING: %s contains unhashed "
846 "entries\n", old);
847 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000848 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100849 }
850 }
851
852 exit(0);
853}
854
Damien Miller95def091999-11-25 00:26:21 +1100855/*
856 * Perform changing a passphrase. The argument is the passwd structure
857 * for the current user.
858 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000859static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100860do_change_passphrase(struct passwd *pw)
861{
Damien Miller95def091999-11-25 00:26:21 +1100862 char *comment;
863 char *old_passphrase, *passphrase1, *passphrase2;
864 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000865 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100866
Damien Miller95def091999-11-25 00:26:21 +1100867 if (!have_identity)
868 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100869 if (stat(identity_file, &st) < 0) {
870 perror(identity_file);
871 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872 }
Damien Miller95def091999-11-25 00:26:21 +1100873 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000874 private = key_load_private(identity_file, "", &comment);
875 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100876 if (identity_passphrase)
877 old_passphrase = xstrdup(identity_passphrase);
878 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000879 old_passphrase =
880 read_passphrase("Enter old passphrase: ",
881 RP_ALLOW_STDIN);
882 private = key_load_private(identity_file, old_passphrase,
883 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000884 memset(old_passphrase, 0, strlen(old_passphrase));
885 xfree(old_passphrase);
886 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100887 printf("Bad passphrase.\n");
888 exit(1);
889 }
Damien Miller95def091999-11-25 00:26:21 +1100890 }
891 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller95def091999-11-25 00:26:21 +1100893 /* Ask the new passphrase (twice). */
894 if (identity_new_passphrase) {
895 passphrase1 = xstrdup(identity_new_passphrase);
896 passphrase2 = NULL;
897 } else {
898 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000899 read_passphrase("Enter new passphrase (empty for no "
900 "passphrase): ", RP_ALLOW_STDIN);
901 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100902 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100903
904 /* Verify that they are the same. */
905 if (strcmp(passphrase1, passphrase2) != 0) {
906 memset(passphrase1, 0, strlen(passphrase1));
907 memset(passphrase2, 0, strlen(passphrase2));
908 xfree(passphrase1);
909 xfree(passphrase2);
910 printf("Pass phrases do not match. Try again.\n");
911 exit(1);
912 }
913 /* Destroy the other copy. */
914 memset(passphrase2, 0, strlen(passphrase2));
915 xfree(passphrase2);
916 }
917
918 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000919 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000920 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100921 memset(passphrase1, 0, strlen(passphrase1));
922 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000923 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100924 xfree(comment);
925 exit(1);
926 }
927 /* Destroy the passphrase and the copy of the key in memory. */
928 memset(passphrase1, 0, strlen(passphrase1));
929 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000930 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100931 xfree(comment);
932
933 printf("Your identification has been saved with the new passphrase.\n");
934 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935}
936
Damien Miller37876e92003-05-15 10:19:46 +1000937/*
938 * Print the SSHFP RR.
939 */
Damien Millercb314822006-03-26 13:48:01 +1100940static int
941do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000942{
943 Key *public;
944 char *comment = NULL;
945 struct stat st;
946
Damien Millercb314822006-03-26 13:48:01 +1100947 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +1000948 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +1100949 if (stat(fname, &st) < 0) {
950 if (errno == ENOENT)
951 return 0;
952 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +1000953 exit(1);
954 }
Damien Millercb314822006-03-26 13:48:01 +1100955 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +1000956 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000957 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000958 key_free(public);
959 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +1100960 return 1;
Damien Miller37876e92003-05-15 10:19:46 +1000961 }
962 if (comment)
963 xfree(comment);
964
Damien Millercb314822006-03-26 13:48:01 +1100965 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +1000966 exit(1);
967}
Damien Miller37876e92003-05-15 10:19:46 +1000968
Damien Miller95def091999-11-25 00:26:21 +1100969/*
970 * Change the comment of a private key file.
971 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000972static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973do_change_comment(struct passwd *pw)
974{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000975 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000976 Key *private;
977 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100978 struct stat st;
979 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000980 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Damien Miller95def091999-11-25 00:26:21 +1100982 if (!have_identity)
983 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100984 if (stat(identity_file, &st) < 0) {
985 perror(identity_file);
986 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000988 private = key_load_private(identity_file, "", &comment);
989 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100990 if (identity_passphrase)
991 passphrase = xstrdup(identity_passphrase);
992 else if (identity_new_passphrase)
993 passphrase = xstrdup(identity_new_passphrase);
994 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000995 passphrase = read_passphrase("Enter passphrase: ",
996 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100997 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000998 private = key_load_private(identity_file, passphrase, &comment);
999 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001000 memset(passphrase, 0, strlen(passphrase));
1001 xfree(passphrase);
1002 printf("Bad passphrase.\n");
1003 exit(1);
1004 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001005 } else {
1006 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001007 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001008 if (private->type != KEY_RSA1) {
1009 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1010 key_free(private);
1011 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001012 }
Damien Miller95def091999-11-25 00:26:21 +11001013 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014
Damien Miller95def091999-11-25 00:26:21 +11001015 if (identity_comment) {
1016 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1017 } else {
1018 printf("Enter new comment: ");
1019 fflush(stdout);
1020 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1021 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001022 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001023 exit(1);
1024 }
Damien Miller14b017d2007-09-17 16:09:15 +10001025 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001026 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027
Damien Miller95def091999-11-25 00:26:21 +11001028 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001029 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001030 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001031 memset(passphrase, 0, strlen(passphrase));
1032 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001033 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001034 xfree(comment);
1035 exit(1);
1036 }
Damien Miller95def091999-11-25 00:26:21 +11001037 memset(passphrase, 0, strlen(passphrase));
1038 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001039 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001040 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Damien Miller95def091999-11-25 00:26:21 +11001042 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001043 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1044 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001045 printf("Could not save your public key in %s\n", identity_file);
1046 exit(1);
1047 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001048 f = fdopen(fd, "w");
1049 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001050 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001051 exit(1);
1052 }
Damien Millereba71ba2000-04-29 23:57:08 +10001053 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001054 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001055 key_free(public);
1056 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001057 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058
Damien Miller95def091999-11-25 00:26:21 +11001059 xfree(comment);
1060
1061 printf("The comment in your key file has been changed.\n");
1062 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063}
1064
Damien Miller0a80ca12010-02-27 07:55:05 +11001065static const char *
1066fmt_validity(void)
1067{
1068 char from[32], to[32];
1069 static char ret[64];
1070 time_t tt;
1071 struct tm *tm;
1072
1073 *from = *to = '\0';
1074 if (cert_valid_from == 0 &&
1075 cert_valid_to == 0xffffffffffffffffULL)
1076 return "forever";
1077
1078 if (cert_valid_from != 0) {
1079 /* XXX revisit INT_MAX in 2038 :) */
1080 tt = cert_valid_from > INT_MAX ? INT_MAX : cert_valid_from;
1081 tm = localtime(&tt);
1082 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1083 }
1084 if (cert_valid_to != 0xffffffffffffffffULL) {
1085 /* XXX revisit INT_MAX in 2038 :) */
1086 tt = cert_valid_to > INT_MAX ? INT_MAX : cert_valid_to;
1087 tm = localtime(&tt);
1088 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1089 }
1090
1091 if (cert_valid_from == 0) {
1092 snprintf(ret, sizeof(ret), "before %s", to);
1093 return ret;
1094 }
1095 if (cert_valid_to == 0xffffffffffffffffULL) {
1096 snprintf(ret, sizeof(ret), "after %s", from);
1097 return ret;
1098 }
1099
1100 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1101 return ret;
1102}
1103
1104static void
1105add_flag_constraint(Buffer *c, const char *name)
1106{
1107 debug3("%s: %s", __func__, name);
1108 buffer_put_cstring(c, name);
1109 buffer_put_string(c, NULL, 0);
1110}
1111
1112static void
1113add_string_constraint(Buffer *c, const char *name, const char *value)
1114{
1115 Buffer b;
1116
1117 debug3("%s: %s=%s", __func__, name, value);
1118 buffer_init(&b);
1119 buffer_put_cstring(&b, value);
1120
1121 buffer_put_cstring(c, name);
1122 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1123
1124 buffer_free(&b);
1125}
1126
1127static void
1128prepare_constraint_buf(Buffer *c)
1129{
1130
1131 buffer_clear(c);
1132 if ((constraint_flags & CONSTRAINT_X_FWD) != 0)
1133 add_flag_constraint(c, "permit-X11-forwarding");
1134 if ((constraint_flags & CONSTRAINT_AGENT_FWD) != 0)
1135 add_flag_constraint(c, "permit-agent-forwarding");
1136 if ((constraint_flags & CONSTRAINT_PORT_FWD) != 0)
1137 add_flag_constraint(c, "permit-port-forwarding");
1138 if ((constraint_flags & CONSTRAINT_PTY) != 0)
1139 add_flag_constraint(c, "permit-pty");
1140 if ((constraint_flags & CONSTRAINT_USER_RC) != 0)
1141 add_flag_constraint(c, "permit-user-rc");
1142 if (constraint_command != NULL)
1143 add_string_constraint(c, "forced-command", constraint_command);
1144 if (constraint_src_addr != NULL)
1145 add_string_constraint(c, "source-address", constraint_src_addr);
1146}
1147
1148static void
1149do_ca_sign(struct passwd *pw, int argc, char **argv)
1150{
1151 int i, fd;
1152 u_int n;
1153 Key *ca, *public;
1154 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1155 FILE *f;
1156
1157 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1158 if ((ca = load_identity(tmp)) == NULL)
1159 fatal("Couldn't load CA key \"%s\"", tmp);
1160 xfree(tmp);
1161
1162 for (i = 0; i < argc; i++) {
1163 /* Split list of principals */
1164 n = 0;
1165 if (cert_principals != NULL) {
1166 otmp = tmp = xstrdup(cert_principals);
1167 plist = NULL;
1168 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1169 plist = xrealloc(plist, n + 1, sizeof(*plist));
1170 if (*(plist[n] = xstrdup(cp)) == '\0')
1171 fatal("Empty principal name");
1172 }
1173 xfree(otmp);
1174 }
1175
1176 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1177 if ((public = key_load_public(tmp, &comment)) == NULL)
1178 fatal("%s: unable to open \"%s\"", __func__, tmp);
1179 if (public->type != KEY_RSA && public->type != KEY_DSA)
1180 fatal("%s: key \"%s\" type %s cannot be certified",
1181 __func__, tmp, key_type(public));
1182
1183 /* Prepare certificate to sign */
1184 if (key_to_certified(public) != 0)
1185 fatal("Could not upgrade key %s to certificate", tmp);
1186 public->cert->type = cert_key_type;
1187 public->cert->key_id = xstrdup(cert_key_id);
1188 public->cert->nprincipals = n;
1189 public->cert->principals = plist;
1190 public->cert->valid_after = cert_valid_from;
1191 public->cert->valid_before = cert_valid_to;
1192 prepare_constraint_buf(&public->cert->constraints);
1193 public->cert->signature_key = key_from_private(ca);
1194
1195 if (key_certify(public, ca) != 0)
1196 fatal("Couldn't not certify key %s", tmp);
1197
1198 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1199 *cp = '\0';
1200 xasprintf(&out, "%s-cert.pub", tmp);
1201 xfree(tmp);
1202
1203 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1204 fatal("Could not open \"%s\" for writing: %s", out,
1205 strerror(errno));
1206 if ((f = fdopen(fd, "w")) == NULL)
1207 fatal("%s: fdopen: %s", __func__, strerror(errno));
1208 if (!key_write(public, f))
1209 fatal("Could not write certified key to %s", out);
1210 fprintf(f, " %s\n", comment);
1211 fclose(f);
1212
1213 if (!quiet)
1214 logit("Signed %s key %s: id \"%s\"%s%s valid %s",
1215 cert_key_type == SSH2_CERT_TYPE_USER?"user":"host",
1216 out, cert_key_id,
1217 cert_principals != NULL ? " for " : "",
1218 cert_principals != NULL ? cert_principals : "",
1219 fmt_validity());
1220
1221 key_free(public);
1222 xfree(out);
1223 }
1224 exit(0);
1225}
1226
1227static u_int64_t
1228parse_relative_time(const char *s, time_t now)
1229{
1230 int64_t mul, secs;
1231
1232 mul = *s == '-' ? -1 : 1;
1233
1234 if ((secs = convtime(s + 1)) == -1)
1235 fatal("Invalid relative certificate time %s", s);
1236 if (mul == -1 && secs > now)
1237 fatal("Certificate time %s cannot be represented", s);
1238 return now + (u_int64_t)(secs * mul);
1239}
1240
1241static u_int64_t
1242parse_absolute_time(const char *s)
1243{
1244 struct tm tm;
1245 time_t tt;
1246
1247 if (strlen(s) != 8 && strlen(s) != 14)
1248 fatal("Invalid certificate time format %s", s);
1249
1250 bzero(&tm, sizeof(tm));
1251 if (strptime(s,
1252 strlen(s) == 8 ? "%Y%m%d" : "%Y%m%d%H%M%S", &tm) == NULL)
1253 fatal("Invalid certificate time %s", s);
1254 if ((tt = mktime(&tm)) < 0)
1255 fatal("Certificate time %s cannot be represented", s);
1256 return (u_int64_t)tt;
1257}
1258
1259static void
1260parse_cert_times(char *timespec)
1261{
1262 char *from, *to;
1263 time_t now = time(NULL);
1264 int64_t secs;
1265
1266 /* +timespec relative to now */
1267 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1268 if ((secs = convtime(timespec + 1)) == -1)
1269 fatal("Invalid relative certificate life %s", timespec);
1270 cert_valid_to = now + secs;
1271 /*
1272 * Backdate certificate one minute to avoid problems on hosts
1273 * with poorly-synchronised clocks.
1274 */
1275 cert_valid_from = ((now - 59)/ 60) * 60;
1276 return;
1277 }
1278
1279 /*
1280 * from:to, where
1281 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1282 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1283 */
1284 from = xstrdup(timespec);
1285 to = strchr(from, ':');
1286 if (to == NULL || from == to || *(to + 1) == '\0')
1287 fatal("Invalid certificate life specification %s", optarg);
1288 *to++ = '\0';
1289
1290 if (*from == '-' || *from == '+')
1291 cert_valid_from = parse_relative_time(from, now);
1292 else
1293 cert_valid_from = parse_absolute_time(from);
1294
1295 if (*to == '-' || *to == '+')
1296 cert_valid_to = parse_relative_time(to, cert_valid_from);
1297 else
1298 cert_valid_to = parse_absolute_time(to);
1299
1300 if (cert_valid_to <= cert_valid_from)
1301 fatal("Empty certificate validity interval");
1302 xfree(from);
1303}
1304
1305static void
1306add_cert_constraint(char *opt)
1307{
1308 char *val;
1309
1310 if (strcmp(opt, "clear") == 0)
1311 constraint_flags = 0;
1312 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
1313 constraint_flags &= ~CONSTRAINT_X_FWD;
1314 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
1315 constraint_flags |= CONSTRAINT_X_FWD;
1316 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
1317 constraint_flags &= ~CONSTRAINT_AGENT_FWD;
1318 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
1319 constraint_flags |= CONSTRAINT_AGENT_FWD;
1320 else if (strcasecmp(opt, "no-port-forwarding") == 0)
1321 constraint_flags &= ~CONSTRAINT_PORT_FWD;
1322 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
1323 constraint_flags |= CONSTRAINT_PORT_FWD;
1324 else if (strcasecmp(opt, "no-pty") == 0)
1325 constraint_flags &= ~CONSTRAINT_PTY;
1326 else if (strcasecmp(opt, "permit-pty") == 0)
1327 constraint_flags |= CONSTRAINT_PTY;
1328 else if (strcasecmp(opt, "no-user-rc") == 0)
1329 constraint_flags &= ~CONSTRAINT_USER_RC;
1330 else if (strcasecmp(opt, "permit-user-rc") == 0)
1331 constraint_flags |= CONSTRAINT_USER_RC;
1332 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1333 val = opt + 14;
1334 if (*val == '\0')
1335 fatal("Empty force-command constraint");
1336 if (constraint_command != NULL)
1337 fatal("force-command already specified");
1338 constraint_command = xstrdup(val);
1339 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1340 val = opt + 15;
1341 if (*val == '\0')
1342 fatal("Empty source-address constraint");
1343 if (constraint_src_addr != NULL)
1344 fatal("source-address already specified");
1345 if (addr_match_cidr_list(NULL, val) != 0)
1346 fatal("Invalid source-address list");
1347 constraint_src_addr = xstrdup(val);
1348 } else
1349 fatal("Unsupported certificate constraint \"%s\"", opt);
1350}
1351
Ben Lindstrombba81212001-06-25 05:01:22 +00001352static void
Damien Miller431f66b1999-11-21 18:31:57 +11001353usage(void)
1354{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001355 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001356 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001357 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001358 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001359 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001360 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001361 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001362#ifdef ENABLE_PKCS11
1363 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1364#endif
Darren Tucker26dc3e62007-02-19 22:09:06 +11001365 fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001366 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1367 fprintf(stderr, " -f filename Filename of the key file.\n");
1368 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1369 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1370 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001371 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1372 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Darren Tucker26dc3e62007-02-19 22:09:06 +11001373 fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001374 fprintf(stderr, " -l Show fingerprint of key file.\n");
1375 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001376 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001377 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001378 fprintf(stderr, " -O cnstr Specify a certificate constraint.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001379 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1380 fprintf(stderr, " -p Change passphrase of private key file.\n");
1381 fprintf(stderr, " -q Quiet.\n");
1382 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1383 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001384 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001385 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1386 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1387 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001388 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001389 fprintf(stderr, " -v Verbose.\n");
1390 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1391 fprintf(stderr, " -y Read private key file and print public key.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001392
Damien Miller95def091999-11-25 00:26:21 +11001393 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001394}
1395
Damien Miller95def091999-11-25 00:26:21 +11001396/*
1397 * Main program for key management.
1398 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001399int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001400main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401{
Damien Millera41c8b12002-01-22 23:05:08 +11001402 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller7ea845e2010-02-12 09:21:02 +11001403 char out_file[MAXPATHLEN], *pkcs11provider = NULL;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001404 char *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001405 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001406 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001407 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001408 int opt, type, fd;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001409 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001410 int do_gen_candidates = 0, do_screen_candidates = 0;
1411 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001412 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001413 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001414
Damien Miller95def091999-11-25 00:26:21 +11001415 extern int optind;
1416 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417
Darren Tuckerce321d82005-10-03 18:11:24 +10001418 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1419 sanitise_stdfd();
1420
Darren Tucker9ac56e92007-01-14 10:19:59 +11001421 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001422
Damien Millerd3a18572000-06-07 19:55:44 +10001423 SSLeay_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001424 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001425
Kevin Steves3a881912002-07-20 19:05:40 +00001426 init_rng();
1427 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001428
Damien Miller5428f641999-11-25 11:54:57 +11001429 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001430 pw = getpwuid(getuid());
1431 if (!pw) {
1432 printf("You don't exist, go away!\n");
1433 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434 }
Damien Millereba71ba2000-04-29 23:57:08 +10001435 if (gethostname(hostname, sizeof(hostname)) < 0) {
1436 perror("gethostname");
1437 exit(1);
1438 }
Damien Miller5428f641999-11-25 11:54:57 +11001439
Damien Miller0a80ca12010-02-27 07:55:05 +11001440 while ((opt = getopt(argc, argv, "degiqpclBHhvxXyF:b:f:t:D:I:P:N:n:"
1441 "O:C:r:g:R:T:G:M:S:s:a:V:W:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001442 switch (opt) {
1443 case 'b':
Damien Miller5f340062006-03-26 14:27:57 +11001444 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001445 if (errstr)
1446 fatal("Bits has bad value %s (%s)",
1447 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001448 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001449 case 'F':
1450 find_host = 1;
1451 rr_hostname = optarg;
1452 break;
1453 case 'H':
1454 hash_hosts = 1;
1455 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001456 case 'I':
1457 cert_key_id = optarg;
1458 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001459 case 'R':
1460 delete_host = 1;
1461 rr_hostname = optarg;
1462 break;
Damien Miller95def091999-11-25 00:26:21 +11001463 case 'l':
1464 print_fingerprint = 1;
1465 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001466 case 'B':
1467 print_bubblebabble = 1;
1468 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001469 case 'n':
1470 cert_principals = optarg;
1471 break;
Damien Miller95def091999-11-25 00:26:21 +11001472 case 'p':
1473 change_passphrase = 1;
1474 break;
Damien Miller95def091999-11-25 00:26:21 +11001475 case 'c':
1476 change_comment = 1;
1477 break;
Damien Miller95def091999-11-25 00:26:21 +11001478 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001479 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1480 sizeof(identity_file))
1481 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001482 have_identity = 1;
1483 break;
Damien Miller37876e92003-05-15 10:19:46 +10001484 case 'g':
1485 print_generic = 1;
1486 break;
Damien Miller95def091999-11-25 00:26:21 +11001487 case 'P':
1488 identity_passphrase = optarg;
1489 break;
Damien Miller95def091999-11-25 00:26:21 +11001490 case 'N':
1491 identity_new_passphrase = optarg;
1492 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001493 case 'O':
1494 add_cert_constraint(optarg);
1495 break;
Damien Miller95def091999-11-25 00:26:21 +11001496 case 'C':
1497 identity_comment = optarg;
1498 break;
Damien Miller95def091999-11-25 00:26:21 +11001499 case 'q':
1500 quiet = 1;
1501 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001502 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001503 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001504 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +10001505 convert_to_ssh2 = 1;
1506 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001507 case 'h':
1508 cert_key_type = SSH2_CERT_TYPE_HOST;
1509 constraint_flags = 0;
1510 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001511 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001512 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001513 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +10001514 convert_from_ssh2 = 1;
1515 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001516 case 'y':
1517 print_public = 1;
1518 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001519 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001520 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001521 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001522 case 's':
1523 ca_key_path = optarg;
1524 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001525 case 't':
1526 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001527 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001528 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001529 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001530 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001531 case 'v':
1532 if (log_level == SYSLOG_LEVEL_INFO)
1533 log_level = SYSLOG_LEVEL_DEBUG1;
1534 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001535 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001536 log_level < SYSLOG_LEVEL_DEBUG3)
1537 log_level++;
1538 }
1539 break;
Damien Miller37876e92003-05-15 10:19:46 +10001540 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001541 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001542 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001543 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001544 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1545 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001546 if (errstr)
1547 fatal("Desired generator has bad value: %s (%s)",
1548 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001549 break;
1550 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001551 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001552 if (errstr)
1553 fatal("Invalid number of trials: %s (%s)",
1554 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001555 break;
1556 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001557 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001558 if (errstr) {
1559 fatal("Memory limit is %s: %s", errstr, optarg);
1560 }
Darren Tucker019cefe2003-08-02 22:40:07 +10001561 break;
1562 case 'G':
1563 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001564 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1565 sizeof(out_file))
1566 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001567 break;
1568 case 'T':
1569 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001570 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1571 sizeof(out_file))
1572 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001573 break;
1574 case 'S':
1575 /* XXX - also compare length against bits */
1576 if (BN_hex2bn(&start, optarg) == 0)
1577 fatal("Invalid start point.");
1578 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001579 case 'V':
1580 parse_cert_times(optarg);
1581 break;
Damien Miller95def091999-11-25 00:26:21 +11001582 case '?':
1583 default:
1584 usage();
1585 }
1586 }
Darren Tucker06930c72003-12-31 11:34:51 +11001587
1588 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11001589 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11001590
Damien Miller0a80ca12010-02-27 07:55:05 +11001591 argv += optind;
1592 argc -= optind;
1593
1594 if (ca_key_path != NULL) {
1595 if (argc < 1) {
1596 printf("Too few arguments.\n");
1597 usage();
1598 }
1599 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11001600 printf("Too many arguments.\n");
1601 usage();
1602 }
1603 if (change_passphrase && change_comment) {
1604 printf("Can only have one of -p and -c.\n");
1605 usage();
1606 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10001607 if (print_fingerprint && (delete_host || hash_hosts)) {
1608 printf("Cannot use -l with -D or -R.\n");
1609 usage();
1610 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001611 if (ca_key_path != NULL) {
1612 if (cert_key_id == NULL)
1613 fatal("Must specify key id (-I) when certifying");
1614 do_ca_sign(pw, argc, argv);
1615 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001616 if (delete_host || hash_hosts || find_host)
1617 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001618 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11001619 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11001620 if (change_passphrase)
1621 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11001622 if (change_comment)
1623 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +00001624 if (convert_to_ssh2)
1625 do_convert_to_ssh2(pw);
1626 if (convert_from_ssh2)
1627 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10001628 if (print_public)
1629 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001630 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11001631 unsigned int n = 0;
1632
1633 if (have_identity) {
1634 n = do_print_resource_record(pw,
1635 identity_file, rr_hostname);
1636 if (n == 0) {
1637 perror(identity_file);
1638 exit(1);
1639 }
1640 exit(0);
1641 } else {
1642
1643 n += do_print_resource_record(pw,
1644 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1645 n += do_print_resource_record(pw,
1646 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1647
1648 if (n == 0)
1649 fatal("no keys found.");
1650 exit(0);
1651 }
Damien Miller37876e92003-05-15 10:19:46 +10001652 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001653 if (pkcs11provider != NULL)
1654 do_download(pw, pkcs11provider);
Damien Miller95def091999-11-25 00:26:21 +11001655
Darren Tucker019cefe2003-08-02 22:40:07 +10001656 if (do_gen_candidates) {
1657 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11001658
Darren Tucker019cefe2003-08-02 22:40:07 +10001659 if (out == NULL) {
1660 error("Couldn't open modulus candidate file \"%s\": %s",
1661 out_file, strerror(errno));
1662 return (1);
1663 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001664 if (bits == 0)
1665 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10001666 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001667 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10001668
1669 return (0);
1670 }
1671
1672 if (do_screen_candidates) {
1673 FILE *in;
1674 FILE *out = fopen(out_file, "w");
1675
1676 if (have_identity && strcmp(identity_file, "-") != 0) {
1677 if ((in = fopen(identity_file, "r")) == NULL) {
1678 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11001679 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10001680 strerror(errno));
1681 }
1682 } else
1683 in = stdin;
1684
1685 if (out == NULL) {
1686 fatal("Couldn't open moduli file \"%s\": %s",
1687 out_file, strerror(errno));
1688 }
1689 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001690 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001691 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001692 }
1693
Damien Miller95def091999-11-25 00:26:21 +11001694 arc4random_stir();
1695
Damien Millerf14be5c2005-11-05 15:15:49 +11001696 if (key_type_name == NULL)
1697 key_type_name = "rsa";
1698
Damien Millere39cacc2000-11-29 12:18:44 +11001699 type = key_type_from_name(key_type_name);
1700 if (type == KEY_UNSPEC) {
1701 fprintf(stderr, "unknown key type %s\n", key_type_name);
1702 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001703 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001704 if (bits == 0)
1705 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
Tim Rice660c3402005-11-28 17:45:32 -08001706 if (type == KEY_DSA && bits != 1024)
1707 fatal("DSA keys must be 1024 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11001708 if (!quiet)
1709 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001710 private = key_generate(type, bits);
1711 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001712 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001713 exit(1);
1714 }
1715 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001716
1717 if (!have_identity)
1718 ask_filename(pw, "Enter file in which to save the key");
1719
Damien Miller788f2122005-11-05 15:14:59 +11001720 /* Create ~/.ssh directory if it doesn't already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001721 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001722 if (strstr(identity_file, dotsshdir) != NULL &&
1723 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001724 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001725 error("Could not create directory '%s'.", dotsshdir);
1726 else if (!quiet)
1727 printf("Created directory '%s'.\n", dotsshdir);
1728 }
1729 /* If the file already exists, ask the user to confirm. */
1730 if (stat(identity_file, &st) >= 0) {
1731 char yesno[3];
1732 printf("%s already exists.\n", identity_file);
1733 printf("Overwrite (y/n)? ");
1734 fflush(stdout);
1735 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1736 exit(1);
1737 if (yesno[0] != 'y' && yesno[0] != 'Y')
1738 exit(1);
1739 }
1740 /* Ask for a passphrase (twice). */
1741 if (identity_passphrase)
1742 passphrase1 = xstrdup(identity_passphrase);
1743 else if (identity_new_passphrase)
1744 passphrase1 = xstrdup(identity_new_passphrase);
1745 else {
1746passphrase_again:
1747 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001748 read_passphrase("Enter passphrase (empty for no "
1749 "passphrase): ", RP_ALLOW_STDIN);
1750 passphrase2 = read_passphrase("Enter same passphrase again: ",
1751 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001752 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001753 /*
1754 * The passphrases do not match. Clear them and
1755 * retry.
1756 */
Damien Miller95def091999-11-25 00:26:21 +11001757 memset(passphrase1, 0, strlen(passphrase1));
1758 memset(passphrase2, 0, strlen(passphrase2));
1759 xfree(passphrase1);
1760 xfree(passphrase2);
1761 printf("Passphrases do not match. Try again.\n");
1762 goto passphrase_again;
1763 }
1764 /* Clear the other copy of the passphrase. */
1765 memset(passphrase2, 0, strlen(passphrase2));
1766 xfree(passphrase2);
1767 }
1768
Damien Miller95def091999-11-25 00:26:21 +11001769 if (identity_comment) {
1770 strlcpy(comment, identity_comment, sizeof(comment));
1771 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11001772 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001773 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1774 }
1775
1776 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001777 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001778 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001779 memset(passphrase1, 0, strlen(passphrase1));
1780 xfree(passphrase1);
1781 exit(1);
1782 }
1783 /* Clear the passphrase. */
1784 memset(passphrase1, 0, strlen(passphrase1));
1785 xfree(passphrase1);
1786
1787 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001788 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001789 arc4random_stir();
1790
1791 if (!quiet)
1792 printf("Your identification has been saved in %s.\n", identity_file);
1793
Damien Miller95def091999-11-25 00:26:21 +11001794 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001795 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1796 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001797 printf("Could not save your public key in %s\n", identity_file);
1798 exit(1);
1799 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001800 f = fdopen(fd, "w");
1801 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001802 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001803 exit(1);
1804 }
Damien Millereba71ba2000-04-29 23:57:08 +10001805 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001806 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001807 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001808 fclose(f);
1809
1810 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001811 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001812 char *ra = key_fingerprint(public, SSH_FP_MD5,
1813 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10001814 printf("Your public key has been saved in %s.\n",
1815 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001816 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001817 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001818 printf("The key's randomart image is:\n");
1819 printf("%s\n", ra);
1820 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00001821 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001822 }
Damien Millereba71ba2000-04-29 23:57:08 +10001823
1824 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001825 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001826}