blob: 4e42d0cfc26bf29116bd738ed49d8a6fe812e24e [file] [log] [blame]
Damien Miller57cf6382006-07-10 21:13:46 +10001/* $OpenBSD: ssh-keygen.c,v 1.147 2006/07/09 15:15:11 stevesk 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 Millerd4a8b7e1999-10-27 13:42:43 +100020
Damien Millereba71ba2000-04-29 23:57:08 +100021#include <openssl/evp.h>
22#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100023
Damien Miller57cf6382006-07-10 21:13:46 +100024#include <fcntl.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100025#ifdef HAVE_PATHS_H
26# include <paths.h>
27#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100028#include <pwd.h>
29
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000032#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100033#include "authfile.h"
34#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110035#include "buffer.h"
36#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "pathnames.h"
38#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100039#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110040#include "match.h"
41#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100042#include "dns.h"
Damien Miller874d77b2000-10-14 16:23:11 +110043
Ben Lindstrom8282d6a2001-08-06 21:44:05 +000044#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +000045#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000046#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000047
Damien Miller3f54a9f2005-11-05 14:52:18 +110048/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
49#define DEFAULT_BITS 2048
50#define DEFAULT_BITS_DSA 1024
51u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller5428f641999-11-25 11:54:57 +110053/*
54 * Flag indicating that we just want to change the passphrase. This can be
55 * set on the command line.
56 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057int change_passphrase = 0;
58
Damien Miller5428f641999-11-25 11:54:57 +110059/*
60 * Flag indicating that we just want to change the comment. This can be set
61 * on the command line.
62 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063int change_comment = 0;
64
65int quiet = 0;
66
Damien Miller4b42d7f2005-03-01 21:48:35 +110067/* Flag indicating that we want to hash a known_hosts file */
68int hash_hosts = 0;
69/* Flag indicating that we want lookup a host in known_hosts file */
70int find_host = 0;
71/* Flag indicating that we want to delete a host from a known_hosts file */
72int delete_host = 0;
73
Damien Miller10f6f6b1999-11-17 17:29:08 +110074/* Flag indicating that we just want to see the key fingerprint */
75int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000076int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110077
Damien Miller431f66b1999-11-21 18:31:57 +110078/* The identity file name, given on the command line or entered by the user. */
79char identity_file[1024];
80int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
82/* This is set to the passphrase if given on the command line. */
83char *identity_passphrase = NULL;
84
85/* This is set to the new passphrase if given on the command line. */
86char *identity_new_passphrase = NULL;
87
88/* This is set to the new comment if given on the command line. */
89char *identity_comment = NULL;
90
Damien Millereba71ba2000-04-29 23:57:08 +100091/* Dump public key file in format used by real and the original SSH 2 */
92int convert_to_ssh2 = 0;
93int convert_from_ssh2 = 0;
94int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +100095int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110096
Damien Millera41c8b12002-01-22 23:05:08 +110097char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100098
Damien Miller431f66b1999-11-21 18:31:57 +110099/* argv0 */
100extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Millereba71ba2000-04-29 23:57:08 +1000102char hostname[MAXHOSTNAMELEN];
103
Darren Tucker770fc012004-05-13 16:24:32 +1000104/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000105int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000106int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
107
Ben Lindstrombba81212001-06-25 05:01:22 +0000108static void
Damien Miller431f66b1999-11-21 18:31:57 +1100109ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110{
Damien Miller95def091999-11-25 00:26:21 +1100111 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100112 char *name = NULL;
113
Damien Miller993dd552002-02-19 15:22:47 +1100114 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000115 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100116 else {
Damien Miller993dd552002-02-19 15:22:47 +1100117 switch (key_type_from_name(key_type_name)) {
118 case KEY_RSA1:
119 name = _PATH_SSH_CLIENT_IDENTITY;
120 break;
121 case KEY_DSA:
122 name = _PATH_SSH_CLIENT_ID_DSA;
123 break;
124 case KEY_RSA:
125 name = _PATH_SSH_CLIENT_ID_RSA;
126 break;
127 default:
128 fprintf(stderr, "bad key type");
129 exit(1);
130 break;
131 }
Damien Miller90967402006-03-26 14:07:26 +1100132 }
Damien Millere39cacc2000-11-29 12:18:44 +1100133 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000134 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100135 if (fgets(buf, sizeof(buf), stdin) == NULL)
136 exit(1);
137 if (strchr(buf, '\n'))
138 *strchr(buf, '\n') = 0;
139 if (strcmp(buf, "") != 0)
140 strlcpy(identity_file, buf, sizeof(identity_file));
141 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100142}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Ben Lindstrombba81212001-06-25 05:01:22 +0000144static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000145load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000146{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000147 char *pass;
148 Key *prv;
149
Ben Lindstroma3700052001-04-05 23:26:32 +0000150 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000151 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000152 if (identity_passphrase)
153 pass = xstrdup(identity_passphrase);
154 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000155 pass = read_passphrase("Enter passphrase: ",
156 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000157 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000158 memset(pass, 0, strlen(pass));
159 xfree(pass);
160 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000161 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000162}
163
Damien Miller874d77b2000-10-14 16:23:11 +1100164#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000165#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100166#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000167#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000168
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static void
Damien Millereba71ba2000-04-29 23:57:08 +1000170do_convert_to_ssh2(struct passwd *pw)
171{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000172 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000173 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000174 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000175 struct stat st;
176
177 if (!have_identity)
178 ask_filename(pw, "Enter file in which the key is");
179 if (stat(identity_file, &st) < 0) {
180 perror(identity_file);
181 exit(1);
182 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000183 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000184 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000185 fprintf(stderr, "load failed\n");
186 exit(1);
187 }
Damien Millereba71ba2000-04-29 23:57:08 +1000188 }
Damien Millerdb274722003-05-14 13:45:22 +1000189 if (k->type == KEY_RSA1) {
190 fprintf(stderr, "version 1 keys are not supported\n");
191 exit(1);
192 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000193 if (key_to_blob(k, &blob, &len) <= 0) {
194 fprintf(stderr, "key_to_blob failed\n");
195 exit(1);
196 }
Damien Miller874d77b2000-10-14 16:23:11 +1100197 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000198 fprintf(stdout,
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000199 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000200 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000201 pw->pw_name, hostname);
202 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100203 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000204 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000205 xfree(blob);
206 exit(0);
207}
208
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static void
Damien Miller874d77b2000-10-14 16:23:11 +1100210buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
211{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000212 u_int bignum_bits = buffer_get_int(b);
213 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000214
Damien Miller874d77b2000-10-14 16:23:11 +1100215 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000216 fatal("buffer_get_bignum_bits: input buffer too small: "
217 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100218 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100219 buffer_consume(b, bytes);
220}
221
Ben Lindstrombba81212001-06-25 05:01:22 +0000222static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000223do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100224{
225 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100226 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100227 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000228 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000229 int magic, rlen, ktype, i1, i2, i3, i4;
230 u_int slen;
231 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100232
233 buffer_init(&b);
234 buffer_append(&b, blob, blen);
235
236 magic = buffer_get_int(&b);
237 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
238 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
239 buffer_free(&b);
240 return NULL;
241 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000242 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100243 type = buffer_get_string(&b, NULL);
244 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000245 i2 = buffer_get_int(&b);
246 i3 = buffer_get_int(&b);
247 i4 = buffer_get_int(&b);
248 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100249 if (strcmp(cipher, "none") != 0) {
250 error("unsupported cipher %s", cipher);
251 xfree(cipher);
252 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000253 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100254 return NULL;
255 }
256 xfree(cipher);
257
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000258 if (strstr(type, "dsa")) {
259 ktype = KEY_DSA;
260 } else if (strstr(type, "rsa")) {
261 ktype = KEY_RSA;
262 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100263 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000264 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100265 return NULL;
266 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000267 key = key_new_private(ktype);
268 xfree(type);
269
270 switch (key->type) {
271 case KEY_DSA:
272 buffer_get_bignum_bits(&b, key->dsa->p);
273 buffer_get_bignum_bits(&b, key->dsa->g);
274 buffer_get_bignum_bits(&b, key->dsa->q);
275 buffer_get_bignum_bits(&b, key->dsa->pub_key);
276 buffer_get_bignum_bits(&b, key->dsa->priv_key);
277 break;
278 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000279 e = buffer_get_char(&b);
280 debug("e %lx", e);
281 if (e < 30) {
282 e <<= 8;
283 e += buffer_get_char(&b);
284 debug("e %lx", e);
285 e <<= 8;
286 e += buffer_get_char(&b);
287 debug("e %lx", e);
288 }
289 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000290 buffer_free(&b);
291 key_free(key);
292 return NULL;
293 }
294 buffer_get_bignum_bits(&b, key->rsa->d);
295 buffer_get_bignum_bits(&b, key->rsa->n);
296 buffer_get_bignum_bits(&b, key->rsa->iqmp);
297 buffer_get_bignum_bits(&b, key->rsa->q);
298 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000299 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000300 break;
301 }
Damien Miller874d77b2000-10-14 16:23:11 +1100302 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000303 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000304 error("do_convert_private_ssh2_from_blob: "
305 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100306 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000307
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000308 /* try the key */
309 key_sign(key, &sig, &slen, data, sizeof(data));
310 key_verify(key, sig, slen, data, sizeof(data));
311 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100312 return key;
313}
314
Damien Miller8056a9d2006-03-15 12:05:40 +1100315static int
316get_line(FILE *fp, char *line, size_t len)
317{
318 int c;
319 size_t pos = 0;
320
321 line[0] = '\0';
322 while ((c = fgetc(fp)) != EOF) {
323 if (pos >= len - 1) {
324 fprintf(stderr, "input line too long.\n");
325 exit(1);
326 }
Damien Miller90967402006-03-26 14:07:26 +1100327 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100328 case '\r':
329 c = fgetc(fp);
330 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
331 fprintf(stderr, "unget: %s\n", strerror(errno));
332 exit(1);
333 }
334 return pos;
335 case '\n':
336 return pos;
337 }
338 line[pos++] = c;
339 line[pos] = '\0';
340 }
Damien Millere23209f2006-03-31 23:13:35 +1100341 if (c == EOF)
342 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100343 return pos;
344}
345
Ben Lindstrombba81212001-06-25 05:01:22 +0000346static void
Damien Millereba71ba2000-04-29 23:57:08 +1000347do_convert_from_ssh2(struct passwd *pw)
348{
349 Key *k;
350 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000351 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100352 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000353 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000354 char encoded[8096];
355 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100356 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000357 FILE *fp;
358
359 if (!have_identity)
360 ask_filename(pw, "Enter file in which the key is");
361 if (stat(identity_file, &st) < 0) {
362 perror(identity_file);
363 exit(1);
364 }
365 fp = fopen(identity_file, "r");
366 if (fp == NULL) {
367 perror(identity_file);
368 exit(1);
369 }
370 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100371 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
372 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000373 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000374 if (strncmp(line, "----", 4) == 0 ||
375 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100376 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
377 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000378 if (strstr(line, " END ") != NULL) {
379 break;
380 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000381 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000382 continue;
383 }
Damien Miller30c3d422000-05-09 11:02:59 +1000384 if (escaped) {
385 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000386 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000387 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000388 }
Damien Millereba71ba2000-04-29 23:57:08 +1000389 strlcat(encoded, line, sizeof(encoded));
390 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000391 len = strlen(encoded);
392 if (((len % 4) == 3) &&
393 (encoded[len-1] == '=') &&
394 (encoded[len-2] == '=') &&
395 (encoded[len-3] == '='))
396 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100397 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000398 if (blen < 0) {
399 fprintf(stderr, "uudecode failed.\n");
400 exit(1);
401 }
Damien Miller874d77b2000-10-14 16:23:11 +1100402 k = private ?
403 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100404 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100405 if (k == NULL) {
406 fprintf(stderr, "decode blob failed.\n");
407 exit(1);
408 }
409 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000410 (k->type == KEY_DSA ?
411 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
412 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100413 key_write(k, stdout);
414 if (!ok) {
415 fprintf(stderr, "key write failed");
416 exit(1);
417 }
Damien Millereba71ba2000-04-29 23:57:08 +1000418 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100419 if (!private)
420 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000421 fclose(fp);
422 exit(0);
423}
424
Ben Lindstrombba81212001-06-25 05:01:22 +0000425static void
Damien Millereba71ba2000-04-29 23:57:08 +1000426do_print_public(struct passwd *pw)
427{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000428 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000429 struct stat st;
430
431 if (!have_identity)
432 ask_filename(pw, "Enter file in which the key is");
433 if (stat(identity_file, &st) < 0) {
434 perror(identity_file);
435 exit(1);
436 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000437 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000438 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000439 fprintf(stderr, "load failed\n");
440 exit(1);
441 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000442 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000443 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000444 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000445 fprintf(stdout, "\n");
446 exit(0);
447}
448
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000449#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000450static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000451do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000452{
Ben Lindstromcd392282001-07-04 03:44:03 +0000453 Key *prv = NULL;
454 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000455 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000456
457 if (!have_identity)
458 ask_filename(pw, "Enter file in which the key is");
459 if (stat(identity_file, &st) < 0) {
460 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000461 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000462 }
463 prv = load_identity(identity_file);
464 if (prv == NULL) {
465 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000466 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000467 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000468 ret = sc_put_key(prv, sc_reader_id);
469 key_free(prv);
470 if (ret < 0)
471 exit(1);
Damien Miller996acd22003-04-09 20:59:48 +1000472 logit("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000473 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000474}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000475
476static void
477do_download(struct passwd *pw, const char *sc_reader_id)
478{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000479 Key **keys = NULL;
480 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000481
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000482 keys = sc_get_keys(sc_reader_id, NULL);
483 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000484 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000485 for (i = 0; keys[i]; i++) {
486 key_write(keys[i], stdout);
487 key_free(keys[i]);
488 fprintf(stdout, "\n");
489 }
490 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000491 exit(0);
492}
Ben Lindstromffce1472001-08-06 21:57:31 +0000493#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000494
Ben Lindstrombba81212001-06-25 05:01:22 +0000495static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100496do_fingerprint(struct passwd *pw)
497{
Damien Miller98c7ad62000-03-09 21:27:49 +1100498 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000499 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000500 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000501 int i, skip = 0, num = 1, invalid = 1;
502 enum fp_rep rep;
503 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100504 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100505
Ben Lindstromd0fca422001-03-26 13:44:06 +0000506 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
507 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000508
Damien Miller95def091999-11-25 00:26:21 +1100509 if (!have_identity)
510 ask_filename(pw, "Enter file in which the key is");
511 if (stat(identity_file, &st) < 0) {
512 perror(identity_file);
513 exit(1);
514 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000515 public = key_load_public(identity_file, &comment);
516 if (public != NULL) {
517 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000518 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100519 key_free(public);
520 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000521 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100522 exit(0);
523 }
Damien Miller40b59852006-06-13 13:00:25 +1000524 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000525 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000526 comment = NULL;
527 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100528
529 f = fopen(identity_file, "r");
530 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100531 while (fgets(line, sizeof(line), f)) {
532 i = strlen(line) - 1;
533 if (line[i] != '\n') {
534 error("line %d too long: %.40s...", num, line);
535 skip = 1;
536 continue;
Damien Miller95def091999-11-25 00:26:21 +1100537 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100538 num++;
539 if (skip) {
540 skip = 0;
541 continue;
542 }
543 line[i] = '\0';
544
545 /* Skip leading whitespace, empty and comment lines. */
546 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
547 ;
548 if (!*cp || *cp == '\n' || *cp == '#')
549 continue ;
550 i = strtol(cp, &ep, 10);
551 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
552 int quoted = 0;
553 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000554 for (; *cp && (quoted || (*cp != ' ' &&
555 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100556 if (*cp == '\\' && cp[1] == '"')
557 cp++; /* Skip both */
558 else if (*cp == '"')
559 quoted = !quoted;
560 }
561 if (!*cp)
562 continue;
563 *cp++ = '\0';
564 }
565 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000566 public = key_new(KEY_RSA1);
567 if (key_read(public, &cp) != 1) {
568 cp = ep;
569 key_free(public);
570 public = key_new(KEY_UNSPEC);
571 if (key_read(public, &cp) != 1) {
572 key_free(public);
573 continue;
574 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100575 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000576 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000577 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000578 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000579 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000580 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000581 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000582 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100583 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100584 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100585 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100586 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100587 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100588 exit(1);
589 }
Damien Miller95def091999-11-25 00:26:21 +1100590 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100591}
592
Damien Miller4b42d7f2005-03-01 21:48:35 +1100593static void
594print_host(FILE *f, char *name, Key *public, int hash)
595{
596 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
597 fatal("hash_host failed");
598 fprintf(f, "%s ", name);
599 if (!key_write(public, f))
600 fatal("key_write failed");
601 fprintf(f, "\n");
602}
603
604static void
605do_known_hosts(struct passwd *pw, const char *name)
606{
607 FILE *in, *out = stdout;
608 Key *public;
609 char *cp, *cp2, *kp, *kp2;
610 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
611 int c, i, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
612
613 if (!have_identity) {
614 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
615 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
616 sizeof(identity_file))
617 fatal("Specified known hosts path too long");
618 xfree(cp);
619 have_identity = 1;
620 }
621 if ((in = fopen(identity_file, "r")) == NULL)
622 fatal("fopen: %s", strerror(errno));
623
624 /*
625 * Find hosts goes to stdout, hash and deletions happen in-place
626 * A corner case is ssh-keygen -HF foo, which should go to stdout
627 */
628 if (!find_host && (hash_hosts || delete_host)) {
629 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
630 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
631 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
632 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
633 fatal("known_hosts path too long");
634 umask(077);
635 if ((c = mkstemp(tmp)) == -1)
636 fatal("mkstemp: %s", strerror(errno));
637 if ((out = fdopen(c, "w")) == NULL) {
638 c = errno;
639 unlink(tmp);
640 fatal("fdopen: %s", strerror(c));
641 }
642 inplace = 1;
643 }
644
645 while (fgets(line, sizeof(line), in)) {
646 num++;
647 i = strlen(line) - 1;
648 if (line[i] != '\n') {
649 error("line %d too long: %.40s...", num, line);
650 skip = 1;
651 invalid = 1;
652 continue;
653 }
654 if (skip) {
655 skip = 0;
656 continue;
657 }
658 line[i] = '\0';
659
660 /* Skip leading whitespace, empty and comment lines. */
661 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
662 ;
663 if (!*cp || *cp == '\n' || *cp == '#') {
664 if (inplace)
665 fprintf(out, "%s\n", cp);
666 continue;
667 }
668 /* Find the end of the host name portion. */
669 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
670 ;
671 if (*kp == '\0' || *(kp + 1) == '\0') {
672 error("line %d missing key: %.40s...",
673 num, line);
674 invalid = 1;
675 continue;
676 }
677 *kp++ = '\0';
678 kp2 = kp;
679
680 public = key_new(KEY_RSA1);
681 if (key_read(public, &kp) != 1) {
682 kp = kp2;
683 key_free(public);
684 public = key_new(KEY_UNSPEC);
685 if (key_read(public, &kp) != 1) {
686 error("line %d invalid key: %.40s...",
687 num, line);
688 key_free(public);
689 invalid = 1;
690 continue;
691 }
692 }
693
694 if (*cp == HASH_DELIM) {
695 if (find_host || delete_host) {
696 cp2 = host_hash(name, cp, strlen(cp));
697 if (cp2 == NULL) {
698 error("line %d: invalid hashed "
699 "name: %.64s...", num, line);
700 invalid = 1;
701 continue;
702 }
703 c = (strcmp(cp2, cp) == 0);
704 if (find_host && c) {
705 printf("# Host %s found: "
706 "line %d type %s\n", name,
707 num, key_type(public));
708 print_host(out, cp, public, 0);
709 }
710 if (delete_host && !c)
711 print_host(out, cp, public, 0);
712 } else if (hash_hosts)
713 print_host(out, cp, public, 0);
714 } else {
715 if (find_host || delete_host) {
716 c = (match_hostname(name, cp,
717 strlen(cp)) == 1);
718 if (find_host && c) {
719 printf("# Host %s found: "
720 "line %d type %s\n", name,
721 num, key_type(public));
722 print_host(out, cp, public, hash_hosts);
723 }
724 if (delete_host && !c)
725 print_host(out, cp, public, 0);
726 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100727 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100728 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100729 cp2 = strsep(&cp, ",")) {
730 if (strcspn(cp2, "*?!") != strlen(cp2))
731 fprintf(stderr, "Warning: "
732 "ignoring host name with "
733 "metacharacters: %.64s\n",
734 cp2);
735 else
736 print_host(out, cp2, public, 1);
737 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100738 has_unhashed = 1;
739 }
740 }
741 key_free(public);
742 }
743 fclose(in);
744
745 if (invalid) {
746 fprintf(stderr, "%s is not a valid known_host file.\n",
747 identity_file);
748 if (inplace) {
749 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +1100750 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100751 fclose(out);
752 unlink(tmp);
753 }
754 exit(1);
755 }
756
757 if (inplace) {
758 fclose(out);
759
760 /* Backup existing file */
761 if (unlink(old) == -1 && errno != ENOENT)
762 fatal("unlink %.100s: %s", old, strerror(errno));
763 if (link(identity_file, old) == -1)
764 fatal("link %.100s to %.100s: %s", identity_file, old,
765 strerror(errno));
766 /* Move new one into place */
767 if (rename(tmp, identity_file) == -1) {
768 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
769 strerror(errno));
770 unlink(tmp);
771 unlink(old);
772 exit(1);
773 }
774
775 fprintf(stderr, "%s updated.\n", identity_file);
776 fprintf(stderr, "Original contents retained as %s\n", old);
777 if (has_unhashed) {
778 fprintf(stderr, "WARNING: %s contains unhashed "
779 "entries\n", old);
780 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000781 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100782 }
783 }
784
785 exit(0);
786}
787
Damien Miller95def091999-11-25 00:26:21 +1100788/*
789 * Perform changing a passphrase. The argument is the passwd structure
790 * for the current user.
791 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000792static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100793do_change_passphrase(struct passwd *pw)
794{
Damien Miller95def091999-11-25 00:26:21 +1100795 char *comment;
796 char *old_passphrase, *passphrase1, *passphrase2;
797 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000798 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100799
Damien Miller95def091999-11-25 00:26:21 +1100800 if (!have_identity)
801 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100802 if (stat(identity_file, &st) < 0) {
803 perror(identity_file);
804 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805 }
Damien Miller95def091999-11-25 00:26:21 +1100806 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000807 private = key_load_private(identity_file, "", &comment);
808 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100809 if (identity_passphrase)
810 old_passphrase = xstrdup(identity_passphrase);
811 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000812 old_passphrase =
813 read_passphrase("Enter old passphrase: ",
814 RP_ALLOW_STDIN);
815 private = key_load_private(identity_file, old_passphrase,
816 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000817 memset(old_passphrase, 0, strlen(old_passphrase));
818 xfree(old_passphrase);
819 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100820 printf("Bad passphrase.\n");
821 exit(1);
822 }
Damien Miller95def091999-11-25 00:26:21 +1100823 }
824 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825
Damien Miller95def091999-11-25 00:26:21 +1100826 /* Ask the new passphrase (twice). */
827 if (identity_new_passphrase) {
828 passphrase1 = xstrdup(identity_new_passphrase);
829 passphrase2 = NULL;
830 } else {
831 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000832 read_passphrase("Enter new passphrase (empty for no "
833 "passphrase): ", RP_ALLOW_STDIN);
834 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100835 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100836
837 /* Verify that they are the same. */
838 if (strcmp(passphrase1, passphrase2) != 0) {
839 memset(passphrase1, 0, strlen(passphrase1));
840 memset(passphrase2, 0, strlen(passphrase2));
841 xfree(passphrase1);
842 xfree(passphrase2);
843 printf("Pass phrases do not match. Try again.\n");
844 exit(1);
845 }
846 /* Destroy the other copy. */
847 memset(passphrase2, 0, strlen(passphrase2));
848 xfree(passphrase2);
849 }
850
851 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000852 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000853 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100854 memset(passphrase1, 0, strlen(passphrase1));
855 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000856 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100857 xfree(comment);
858 exit(1);
859 }
860 /* Destroy the passphrase and the copy of the key in memory. */
861 memset(passphrase1, 0, strlen(passphrase1));
862 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000863 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100864 xfree(comment);
865
866 printf("Your identification has been saved with the new passphrase.\n");
867 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868}
869
Damien Miller37876e92003-05-15 10:19:46 +1000870/*
871 * Print the SSHFP RR.
872 */
Damien Millercb314822006-03-26 13:48:01 +1100873static int
874do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000875{
876 Key *public;
877 char *comment = NULL;
878 struct stat st;
879
Damien Millercb314822006-03-26 13:48:01 +1100880 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +1000881 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +1100882 if (stat(fname, &st) < 0) {
883 if (errno == ENOENT)
884 return 0;
885 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +1000886 exit(1);
887 }
Damien Millercb314822006-03-26 13:48:01 +1100888 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +1000889 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000890 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000891 key_free(public);
892 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +1100893 return 1;
Damien Miller37876e92003-05-15 10:19:46 +1000894 }
895 if (comment)
896 xfree(comment);
897
Damien Millercb314822006-03-26 13:48:01 +1100898 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +1000899 exit(1);
900}
Damien Miller37876e92003-05-15 10:19:46 +1000901
Damien Miller95def091999-11-25 00:26:21 +1100902/*
903 * Change the comment of a private key file.
904 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000905static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906do_change_comment(struct passwd *pw)
907{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000908 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000909 Key *private;
910 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100911 struct stat st;
912 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000913 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller95def091999-11-25 00:26:21 +1100915 if (!have_identity)
916 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100917 if (stat(identity_file, &st) < 0) {
918 perror(identity_file);
919 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000921 private = key_load_private(identity_file, "", &comment);
922 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100923 if (identity_passphrase)
924 passphrase = xstrdup(identity_passphrase);
925 else if (identity_new_passphrase)
926 passphrase = xstrdup(identity_new_passphrase);
927 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000928 passphrase = read_passphrase("Enter passphrase: ",
929 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100930 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000931 private = key_load_private(identity_file, passphrase, &comment);
932 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100933 memset(passphrase, 0, strlen(passphrase));
934 xfree(passphrase);
935 printf("Bad passphrase.\n");
936 exit(1);
937 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000938 } else {
939 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100940 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000941 if (private->type != KEY_RSA1) {
942 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
943 key_free(private);
944 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100945 }
Damien Miller95def091999-11-25 00:26:21 +1100946 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947
Damien Miller95def091999-11-25 00:26:21 +1100948 if (identity_comment) {
949 strlcpy(new_comment, identity_comment, sizeof(new_comment));
950 } else {
951 printf("Enter new comment: ");
952 fflush(stdout);
953 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
954 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000955 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100956 exit(1);
957 }
Damien Miller95def091999-11-25 00:26:21 +1100958 if (strchr(new_comment, '\n'))
959 *strchr(new_comment, '\n') = 0;
960 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961
Damien Miller95def091999-11-25 00:26:21 +1100962 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000963 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000964 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100965 memset(passphrase, 0, strlen(passphrase));
966 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000967 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100968 xfree(comment);
969 exit(1);
970 }
Damien Miller95def091999-11-25 00:26:21 +1100971 memset(passphrase, 0, strlen(passphrase));
972 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000973 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000974 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
Damien Miller95def091999-11-25 00:26:21 +1100976 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000977 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
978 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100979 printf("Could not save your public key in %s\n", identity_file);
980 exit(1);
981 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000982 f = fdopen(fd, "w");
983 if (f == NULL) {
984 printf("fdopen %s failed", identity_file);
985 exit(1);
986 }
Damien Millereba71ba2000-04-29 23:57:08 +1000987 if (!key_write(public, f))
988 fprintf(stderr, "write key failed");
989 key_free(public);
990 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100991 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992
Damien Miller95def091999-11-25 00:26:21 +1100993 xfree(comment);
994
995 printf("The comment in your key file has been changed.\n");
996 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997}
998
Ben Lindstrombba81212001-06-25 05:01:22 +0000999static void
Damien Miller431f66b1999-11-21 18:31:57 +11001000usage(void)
1001{
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001002 fprintf(stderr, "Usage: %s [options]\n", __progname);
1003 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001004 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001005 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001006 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001007 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001008 fprintf(stderr, " -c Change comment in private and public key files.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001009#ifdef SMARTCARD
1010 fprintf(stderr, " -D reader Download public key from smartcard.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001011#endif /* SMARTCARD */
1012 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
1013 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1014 fprintf(stderr, " -f filename Filename of the key file.\n");
1015 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1016 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1017 fprintf(stderr, " -H Hash names in known_hosts file.\n");
1018 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
1019 fprintf(stderr, " -l Show fingerprint of key file.\n");
1020 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
1021 fprintf(stderr, " -N phrase Provide new passphrase.\n");
1022 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1023 fprintf(stderr, " -p Change passphrase of private key file.\n");
1024 fprintf(stderr, " -q Quiet.\n");
1025 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1026 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1027 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1028 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1029 fprintf(stderr, " -t type Specify type of key to create.\n");
1030#ifdef SMARTCARD
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001031 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
1032#endif /* SMARTCARD */
Damien Miller9278ffa2005-05-26 11:59:06 +10001033 fprintf(stderr, " -v Verbose.\n");
1034 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1035 fprintf(stderr, " -y Read private key file and print public key.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001036
Damien Miller95def091999-11-25 00:26:21 +11001037 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001038}
1039
Damien Miller95def091999-11-25 00:26:21 +11001040/*
1041 * Main program for key management.
1042 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043int
1044main(int ac, char **av)
1045{
Damien Millera41c8b12002-01-22 23:05:08 +11001046 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Tim Rice2e0e38e2003-09-08 16:11:33 -07001047 char out_file[MAXPATHLEN], *reader_id = NULL;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001048 char *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001049 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001050 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001051 struct stat st;
Damien Millerb089fb52005-05-26 12:16:18 +10001052 int opt, type, fd, download = 0;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001053 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001054 int do_gen_candidates = 0, do_screen_candidates = 0;
Darren Tucker06930c72003-12-31 11:34:51 +11001055 int log_level = SYSLOG_LEVEL_INFO;
Darren Tucker019cefe2003-08-02 22:40:07 +10001056 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001057 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001058 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001059
Damien Miller95def091999-11-25 00:26:21 +11001060 extern int optind;
1061 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001062
Darren Tuckerce321d82005-10-03 18:11:24 +10001063 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1064 sanitise_stdfd();
1065
Damien Miller59d3d5b2003-08-22 09:34:41 +10001066 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001067
Damien Millerd3a18572000-06-07 19:55:44 +10001068 SSLeay_add_all_algorithms();
Darren Tucker019cefe2003-08-02 22:40:07 +10001069 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
1070
Kevin Steves3a881912002-07-20 19:05:40 +00001071 init_rng();
1072 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001073
Damien Miller5428f641999-11-25 11:54:57 +11001074 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001075 pw = getpwuid(getuid());
1076 if (!pw) {
1077 printf("You don't exist, go away!\n");
1078 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079 }
Damien Millereba71ba2000-04-29 23:57:08 +10001080 if (gethostname(hostname, sizeof(hostname)) < 0) {
1081 perror("gethostname");
1082 exit(1);
1083 }
Damien Miller5428f641999-11-25 11:54:57 +11001084
Darren Tucker019cefe2003-08-02 22:40:07 +10001085 while ((opt = getopt(ac, av,
Damien Miller4b42d7f2005-03-01 21:48:35 +11001086 "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001087 switch (opt) {
1088 case 'b':
Damien Miller5f340062006-03-26 14:27:57 +11001089 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001090 if (errstr)
1091 fatal("Bits has bad value %s (%s)",
1092 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001093 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001094 case 'F':
1095 find_host = 1;
1096 rr_hostname = optarg;
1097 break;
1098 case 'H':
1099 hash_hosts = 1;
1100 break;
1101 case 'R':
1102 delete_host = 1;
1103 rr_hostname = optarg;
1104 break;
Damien Miller95def091999-11-25 00:26:21 +11001105 case 'l':
1106 print_fingerprint = 1;
1107 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001108 case 'B':
1109 print_bubblebabble = 1;
1110 break;
Damien Miller95def091999-11-25 00:26:21 +11001111 case 'p':
1112 change_passphrase = 1;
1113 break;
Damien Miller95def091999-11-25 00:26:21 +11001114 case 'c':
1115 change_comment = 1;
1116 break;
Damien Miller95def091999-11-25 00:26:21 +11001117 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001118 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1119 sizeof(identity_file))
1120 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001121 have_identity = 1;
1122 break;
Damien Miller37876e92003-05-15 10:19:46 +10001123 case 'g':
1124 print_generic = 1;
1125 break;
Damien Miller95def091999-11-25 00:26:21 +11001126 case 'P':
1127 identity_passphrase = optarg;
1128 break;
Damien Miller95def091999-11-25 00:26:21 +11001129 case 'N':
1130 identity_new_passphrase = optarg;
1131 break;
Damien Miller95def091999-11-25 00:26:21 +11001132 case 'C':
1133 identity_comment = optarg;
1134 break;
Damien Miller95def091999-11-25 00:26:21 +11001135 case 'q':
1136 quiet = 1;
1137 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001138 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001139 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001140 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +10001141 convert_to_ssh2 = 1;
1142 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001143 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001144 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001145 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +10001146 convert_from_ssh2 = 1;
1147 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001148 case 'y':
1149 print_public = 1;
1150 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001151 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001152 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001153 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001154 case 't':
1155 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001156 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001157 case 'D':
1158 download = 1;
Damien Miller90967402006-03-26 14:07:26 +11001159 /*FALLTHROUGH*/
Ben Lindstromf19578c2001-08-06 21:46:54 +00001160 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001161 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001162 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001163 case 'v':
1164 if (log_level == SYSLOG_LEVEL_INFO)
1165 log_level = SYSLOG_LEVEL_DEBUG1;
1166 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001167 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001168 log_level < SYSLOG_LEVEL_DEBUG3)
1169 log_level++;
1170 }
1171 break;
Damien Miller37876e92003-05-15 10:19:46 +10001172 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001173 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001174 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001175 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001176 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1177 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001178 if (errstr)
1179 fatal("Desired generator has bad value: %s (%s)",
1180 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001181 break;
1182 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001183 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001184 if (errstr)
1185 fatal("Invalid number of trials: %s (%s)",
1186 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001187 break;
1188 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001189 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001190 if (errstr) {
1191 fatal("Memory limit is %s: %s", errstr, optarg);
1192 }
Darren Tucker019cefe2003-08-02 22:40:07 +10001193 break;
1194 case 'G':
1195 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001196 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1197 sizeof(out_file))
1198 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001199 break;
1200 case 'T':
1201 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001202 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1203 sizeof(out_file))
1204 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001205 break;
1206 case 'S':
1207 /* XXX - also compare length against bits */
1208 if (BN_hex2bn(&start, optarg) == 0)
1209 fatal("Invalid start point.");
1210 break;
Damien Miller95def091999-11-25 00:26:21 +11001211 case '?':
1212 default:
1213 usage();
1214 }
1215 }
Darren Tucker06930c72003-12-31 11:34:51 +11001216
1217 /* reinit */
1218 log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
1219
Damien Miller95def091999-11-25 00:26:21 +11001220 if (optind < ac) {
1221 printf("Too many arguments.\n");
1222 usage();
1223 }
1224 if (change_passphrase && change_comment) {
1225 printf("Can only have one of -p and -c.\n");
1226 usage();
1227 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001228 if (delete_host || hash_hosts || find_host)
1229 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001230 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11001231 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11001232 if (change_passphrase)
1233 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11001234 if (change_comment)
1235 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +00001236 if (convert_to_ssh2)
1237 do_convert_to_ssh2(pw);
1238 if (convert_from_ssh2)
1239 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10001240 if (print_public)
1241 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001242 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11001243 unsigned int n = 0;
1244
1245 if (have_identity) {
1246 n = do_print_resource_record(pw,
1247 identity_file, rr_hostname);
1248 if (n == 0) {
1249 perror(identity_file);
1250 exit(1);
1251 }
1252 exit(0);
1253 } else {
1254
1255 n += do_print_resource_record(pw,
1256 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1257 n += do_print_resource_record(pw,
1258 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1259
1260 if (n == 0)
1261 fatal("no keys found.");
1262 exit(0);
1263 }
Damien Miller37876e92003-05-15 10:19:46 +10001264 }
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001265 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +00001266#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001267 if (download)
1268 do_download(pw, reader_id);
1269 else
1270 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +00001271#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +00001272 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +00001273#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001274 }
Damien Miller95def091999-11-25 00:26:21 +11001275
Darren Tucker019cefe2003-08-02 22:40:07 +10001276 if (do_gen_candidates) {
1277 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11001278
Darren Tucker019cefe2003-08-02 22:40:07 +10001279 if (out == NULL) {
1280 error("Couldn't open modulus candidate file \"%s\": %s",
1281 out_file, strerror(errno));
1282 return (1);
1283 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001284 if (bits == 0)
1285 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10001286 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001287 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10001288
1289 return (0);
1290 }
1291
1292 if (do_screen_candidates) {
1293 FILE *in;
1294 FILE *out = fopen(out_file, "w");
1295
1296 if (have_identity && strcmp(identity_file, "-") != 0) {
1297 if ((in = fopen(identity_file, "r")) == NULL) {
1298 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11001299 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10001300 strerror(errno));
1301 }
1302 } else
1303 in = stdin;
1304
1305 if (out == NULL) {
1306 fatal("Couldn't open moduli file \"%s\": %s",
1307 out_file, strerror(errno));
1308 }
1309 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001310 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001311 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001312 }
1313
Damien Miller95def091999-11-25 00:26:21 +11001314 arc4random_stir();
1315
Damien Millerf14be5c2005-11-05 15:15:49 +11001316 if (key_type_name == NULL)
1317 key_type_name = "rsa";
1318
Damien Millere39cacc2000-11-29 12:18:44 +11001319 type = key_type_from_name(key_type_name);
1320 if (type == KEY_UNSPEC) {
1321 fprintf(stderr, "unknown key type %s\n", key_type_name);
1322 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001323 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001324 if (bits == 0)
1325 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
Tim Rice660c3402005-11-28 17:45:32 -08001326 if (type == KEY_DSA && bits != 1024)
1327 fatal("DSA keys must be 1024 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11001328 if (!quiet)
1329 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001330 private = key_generate(type, bits);
1331 if (private == NULL) {
1332 fprintf(stderr, "key_generate failed");
1333 exit(1);
1334 }
1335 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001336
1337 if (!have_identity)
1338 ask_filename(pw, "Enter file in which to save the key");
1339
Damien Miller788f2122005-11-05 15:14:59 +11001340 /* Create ~/.ssh directory if it doesn't already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001341 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001342 if (strstr(identity_file, dotsshdir) != NULL &&
1343 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001344 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001345 error("Could not create directory '%s'.", dotsshdir);
1346 else if (!quiet)
1347 printf("Created directory '%s'.\n", dotsshdir);
1348 }
1349 /* If the file already exists, ask the user to confirm. */
1350 if (stat(identity_file, &st) >= 0) {
1351 char yesno[3];
1352 printf("%s already exists.\n", identity_file);
1353 printf("Overwrite (y/n)? ");
1354 fflush(stdout);
1355 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1356 exit(1);
1357 if (yesno[0] != 'y' && yesno[0] != 'Y')
1358 exit(1);
1359 }
1360 /* Ask for a passphrase (twice). */
1361 if (identity_passphrase)
1362 passphrase1 = xstrdup(identity_passphrase);
1363 else if (identity_new_passphrase)
1364 passphrase1 = xstrdup(identity_new_passphrase);
1365 else {
1366passphrase_again:
1367 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001368 read_passphrase("Enter passphrase (empty for no "
1369 "passphrase): ", RP_ALLOW_STDIN);
1370 passphrase2 = read_passphrase("Enter same passphrase again: ",
1371 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001372 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001373 /*
1374 * The passphrases do not match. Clear them and
1375 * retry.
1376 */
Damien Miller95def091999-11-25 00:26:21 +11001377 memset(passphrase1, 0, strlen(passphrase1));
1378 memset(passphrase2, 0, strlen(passphrase2));
1379 xfree(passphrase1);
1380 xfree(passphrase2);
1381 printf("Passphrases do not match. Try again.\n");
1382 goto passphrase_again;
1383 }
1384 /* Clear the other copy of the passphrase. */
1385 memset(passphrase2, 0, strlen(passphrase2));
1386 xfree(passphrase2);
1387 }
1388
Damien Miller95def091999-11-25 00:26:21 +11001389 if (identity_comment) {
1390 strlcpy(comment, identity_comment, sizeof(comment));
1391 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001392 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001393 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1394 }
1395
1396 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001397 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001398 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001399 memset(passphrase1, 0, strlen(passphrase1));
1400 xfree(passphrase1);
1401 exit(1);
1402 }
1403 /* Clear the passphrase. */
1404 memset(passphrase1, 0, strlen(passphrase1));
1405 xfree(passphrase1);
1406
1407 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001408 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001409 arc4random_stir();
1410
1411 if (!quiet)
1412 printf("Your identification has been saved in %s.\n", identity_file);
1413
Damien Miller95def091999-11-25 00:26:21 +11001414 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001415 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1416 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001417 printf("Could not save your public key in %s\n", identity_file);
1418 exit(1);
1419 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001420 f = fdopen(fd, "w");
1421 if (f == NULL) {
1422 printf("fdopen %s failed", identity_file);
1423 exit(1);
1424 }
Damien Millereba71ba2000-04-29 23:57:08 +10001425 if (!key_write(public, f))
1426 fprintf(stderr, "write key failed");
1427 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001428 fclose(f);
1429
1430 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001431 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001432 printf("Your public key has been saved in %s.\n",
1433 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001434 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001435 printf("%s %s\n", fp, comment);
1436 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001437 }
Damien Millereba71ba2000-04-29 23:57:08 +10001438
1439 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001440 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441}