blob: d4d19d3a19b6a74a5971b6e15536377c70d8a570 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11006 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Darren Tucker3f9fdc72004-06-22 12:56:01 +100015RCSID("$OpenBSD: ssh-keygen.c,v 1.116 2004/06/21 17:36:31 avsm Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Damien Millereba71ba2000-04-29 23:57:08 +100017#include <openssl/evp.h>
18#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100019
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100021#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000022#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100023#include "authfile.h"
24#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "buffer.h"
26#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "pathnames.h"
28#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100029#include "misc.h"
Damien Miller874d77b2000-10-14 16:23:11 +110030
Ben Lindstrom8282d6a2001-08-06 21:44:05 +000031#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +000032#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000033#endif
Damien Millered12a262003-05-15 13:37:43 +100034#include "dns.h"
Ben Lindstromcd392282001-07-04 03:44:03 +000035
Damien Millereba71ba2000-04-29 23:57:08 +100036/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037int bits = 1024;
38
Damien Miller5428f641999-11-25 11:54:57 +110039/*
40 * Flag indicating that we just want to change the passphrase. This can be
41 * set on the command line.
42 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043int change_passphrase = 0;
44
Damien Miller5428f641999-11-25 11:54:57 +110045/*
46 * Flag indicating that we just want to change the comment. This can be set
47 * on the command line.
48 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049int change_comment = 0;
50
51int quiet = 0;
52
Damien Miller10f6f6b1999-11-17 17:29:08 +110053/* Flag indicating that we just want to see the key fingerprint */
54int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000055int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110056
Damien Miller431f66b1999-11-21 18:31:57 +110057/* The identity file name, given on the command line or entered by the user. */
58char identity_file[1024];
59int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
61/* This is set to the passphrase if given on the command line. */
62char *identity_passphrase = NULL;
63
64/* This is set to the new passphrase if given on the command line. */
65char *identity_new_passphrase = NULL;
66
67/* This is set to the new comment if given on the command line. */
68char *identity_comment = NULL;
69
Damien Millereba71ba2000-04-29 23:57:08 +100070/* Dump public key file in format used by real and the original SSH 2 */
71int convert_to_ssh2 = 0;
72int convert_from_ssh2 = 0;
73int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +100074int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110075
Damien Millera41c8b12002-01-22 23:05:08 +110076char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100077
Damien Miller431f66b1999-11-21 18:31:57 +110078/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110079#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110080extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000081#else
82char *__progname;
83#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Millereba71ba2000-04-29 23:57:08 +100085char hostname[MAXHOSTNAMELEN];
86
Darren Tucker770fc012004-05-13 16:24:32 +100087/* moduli.c */
88int gen_candidates(FILE *, int, int, BIGNUM *);
89int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
90
Ben Lindstrombba81212001-06-25 05:01:22 +000091static void
Damien Miller431f66b1999-11-21 18:31:57 +110092ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093{
Damien Miller95def091999-11-25 00:26:21 +110094 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110095 char *name = NULL;
96
Damien Miller993dd552002-02-19 15:22:47 +110097 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +000098 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller993dd552002-02-19 15:22:47 +110099 else
100 switch (key_type_from_name(key_type_name)) {
101 case KEY_RSA1:
102 name = _PATH_SSH_CLIENT_IDENTITY;
103 break;
104 case KEY_DSA:
105 name = _PATH_SSH_CLIENT_ID_DSA;
106 break;
107 case KEY_RSA:
108 name = _PATH_SSH_CLIENT_ID_RSA;
109 break;
110 default:
111 fprintf(stderr, "bad key type");
112 exit(1);
113 break;
114 }
115
Damien Millere39cacc2000-11-29 12:18:44 +1100116 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000117 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100118 if (fgets(buf, sizeof(buf), stdin) == NULL)
119 exit(1);
120 if (strchr(buf, '\n'))
121 *strchr(buf, '\n') = 0;
122 if (strcmp(buf, "") != 0)
123 strlcpy(identity_file, buf, sizeof(identity_file));
124 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100125}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Ben Lindstrombba81212001-06-25 05:01:22 +0000127static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000128load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000129{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000130 char *pass;
131 Key *prv;
132
Ben Lindstroma3700052001-04-05 23:26:32 +0000133 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000134 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000135 if (identity_passphrase)
136 pass = xstrdup(identity_passphrase);
137 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000138 pass = read_passphrase("Enter passphrase: ",
139 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000140 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000141 memset(pass, 0, strlen(pass));
142 xfree(pass);
143 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000144 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000145}
146
Damien Miller874d77b2000-10-14 16:23:11 +1100147#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000148#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100149#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000150#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000151
Ben Lindstrombba81212001-06-25 05:01:22 +0000152static void
Damien Millereba71ba2000-04-29 23:57:08 +1000153do_convert_to_ssh2(struct passwd *pw)
154{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000155 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000156 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000157 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000158 struct stat st;
159
160 if (!have_identity)
161 ask_filename(pw, "Enter file in which the key is");
162 if (stat(identity_file, &st) < 0) {
163 perror(identity_file);
164 exit(1);
165 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000166 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000167 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000168 fprintf(stderr, "load failed\n");
169 exit(1);
170 }
Damien Millereba71ba2000-04-29 23:57:08 +1000171 }
Damien Millerdb274722003-05-14 13:45:22 +1000172 if (k->type == KEY_RSA1) {
173 fprintf(stderr, "version 1 keys are not supported\n");
174 exit(1);
175 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000176 if (key_to_blob(k, &blob, &len) <= 0) {
177 fprintf(stderr, "key_to_blob failed\n");
178 exit(1);
179 }
Damien Miller874d77b2000-10-14 16:23:11 +1100180 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000181 fprintf(stdout,
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000182 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000183 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000184 pw->pw_name, hostname);
185 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100186 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000187 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000188 xfree(blob);
189 exit(0);
190}
191
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static void
Damien Miller874d77b2000-10-14 16:23:11 +1100193buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
194{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000195 u_int bignum_bits = buffer_get_int(b);
196 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000197
Damien Miller874d77b2000-10-14 16:23:11 +1100198 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000199 fatal("buffer_get_bignum_bits: input buffer too small: "
200 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100201 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100202 buffer_consume(b, bytes);
203}
204
Ben Lindstrombba81212001-06-25 05:01:22 +0000205static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000206do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100207{
208 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100209 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100210 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000211 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000212 int magic, rlen, ktype, i1, i2, i3, i4;
213 u_int slen;
214 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100215
216 buffer_init(&b);
217 buffer_append(&b, blob, blen);
218
219 magic = buffer_get_int(&b);
220 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
221 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
222 buffer_free(&b);
223 return NULL;
224 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000225 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100226 type = buffer_get_string(&b, NULL);
227 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000228 i2 = buffer_get_int(&b);
229 i3 = buffer_get_int(&b);
230 i4 = buffer_get_int(&b);
231 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100232 if (strcmp(cipher, "none") != 0) {
233 error("unsupported cipher %s", cipher);
234 xfree(cipher);
235 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000236 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100237 return NULL;
238 }
239 xfree(cipher);
240
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000241 if (strstr(type, "dsa")) {
242 ktype = KEY_DSA;
243 } else if (strstr(type, "rsa")) {
244 ktype = KEY_RSA;
245 } else {
246 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100247 return NULL;
248 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000249 key = key_new_private(ktype);
250 xfree(type);
251
252 switch (key->type) {
253 case KEY_DSA:
254 buffer_get_bignum_bits(&b, key->dsa->p);
255 buffer_get_bignum_bits(&b, key->dsa->g);
256 buffer_get_bignum_bits(&b, key->dsa->q);
257 buffer_get_bignum_bits(&b, key->dsa->pub_key);
258 buffer_get_bignum_bits(&b, key->dsa->priv_key);
259 break;
260 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000261 e = buffer_get_char(&b);
262 debug("e %lx", e);
263 if (e < 30) {
264 e <<= 8;
265 e += buffer_get_char(&b);
266 debug("e %lx", e);
267 e <<= 8;
268 e += buffer_get_char(&b);
269 debug("e %lx", e);
270 }
271 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000272 buffer_free(&b);
273 key_free(key);
274 return NULL;
275 }
276 buffer_get_bignum_bits(&b, key->rsa->d);
277 buffer_get_bignum_bits(&b, key->rsa->n);
278 buffer_get_bignum_bits(&b, key->rsa->iqmp);
279 buffer_get_bignum_bits(&b, key->rsa->q);
280 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000281 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000282 break;
283 }
Damien Miller874d77b2000-10-14 16:23:11 +1100284 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000285 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000286 error("do_convert_private_ssh2_from_blob: "
287 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100288 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000289
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000290 /* try the key */
291 key_sign(key, &sig, &slen, data, sizeof(data));
292 key_verify(key, sig, slen, data, sizeof(data));
293 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100294 return key;
295}
296
Ben Lindstrombba81212001-06-25 05:01:22 +0000297static void
Damien Millereba71ba2000-04-29 23:57:08 +1000298do_convert_from_ssh2(struct passwd *pw)
299{
300 Key *k;
301 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000302 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000303 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000304 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000305 char encoded[8096];
306 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100307 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000308 FILE *fp;
309
310 if (!have_identity)
311 ask_filename(pw, "Enter file in which the key is");
312 if (stat(identity_file, &st) < 0) {
313 perror(identity_file);
314 exit(1);
315 }
316 fp = fopen(identity_file, "r");
317 if (fp == NULL) {
318 perror(identity_file);
319 exit(1);
320 }
321 encoded[0] = '\0';
322 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000323 if (!(p = strchr(line, '\n'))) {
324 fprintf(stderr, "input line too long.\n");
325 exit(1);
326 }
327 if (p > line && p[-1] == '\\')
328 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000329 if (strncmp(line, "----", 4) == 0 ||
330 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100331 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
332 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000333 if (strstr(line, " END ") != NULL) {
334 break;
335 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000336 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000337 continue;
338 }
Damien Miller30c3d422000-05-09 11:02:59 +1000339 if (escaped) {
340 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000341 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000342 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000343 }
344 *p = '\0';
345 strlcat(encoded, line, sizeof(encoded));
346 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000347 len = strlen(encoded);
348 if (((len % 4) == 3) &&
349 (encoded[len-1] == '=') &&
350 (encoded[len-2] == '=') &&
351 (encoded[len-3] == '='))
352 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100353 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000354 if (blen < 0) {
355 fprintf(stderr, "uudecode failed.\n");
356 exit(1);
357 }
Damien Miller874d77b2000-10-14 16:23:11 +1100358 k = private ?
359 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100360 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100361 if (k == NULL) {
362 fprintf(stderr, "decode blob failed.\n");
363 exit(1);
364 }
365 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000366 (k->type == KEY_DSA ?
367 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
368 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100369 key_write(k, stdout);
370 if (!ok) {
371 fprintf(stderr, "key write failed");
372 exit(1);
373 }
Damien Millereba71ba2000-04-29 23:57:08 +1000374 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100375 if (!private)
376 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000377 fclose(fp);
378 exit(0);
379}
380
Ben Lindstrombba81212001-06-25 05:01:22 +0000381static void
Damien Millereba71ba2000-04-29 23:57:08 +1000382do_print_public(struct passwd *pw)
383{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000384 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000385 struct stat st;
386
387 if (!have_identity)
388 ask_filename(pw, "Enter file in which the key is");
389 if (stat(identity_file, &st) < 0) {
390 perror(identity_file);
391 exit(1);
392 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000393 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000394 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000395 fprintf(stderr, "load failed\n");
396 exit(1);
397 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000398 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000399 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000400 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000401 fprintf(stdout, "\n");
402 exit(0);
403}
404
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000405#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000406static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000407do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000408{
Ben Lindstromcd392282001-07-04 03:44:03 +0000409 Key *prv = NULL;
410 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000411 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000412
413 if (!have_identity)
414 ask_filename(pw, "Enter file in which the key is");
415 if (stat(identity_file, &st) < 0) {
416 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000417 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000418 }
419 prv = load_identity(identity_file);
420 if (prv == NULL) {
421 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000422 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000423 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000424 ret = sc_put_key(prv, sc_reader_id);
425 key_free(prv);
426 if (ret < 0)
427 exit(1);
Damien Miller996acd22003-04-09 20:59:48 +1000428 logit("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000429 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000430}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000431
432static void
433do_download(struct passwd *pw, const char *sc_reader_id)
434{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000435 Key **keys = NULL;
436 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000437
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000438 keys = sc_get_keys(sc_reader_id, NULL);
439 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000440 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000441 for (i = 0; keys[i]; i++) {
442 key_write(keys[i], stdout);
443 key_free(keys[i]);
444 fprintf(stdout, "\n");
445 }
446 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000447 exit(0);
448}
Ben Lindstromffce1472001-08-06 21:57:31 +0000449#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000450
Ben Lindstrombba81212001-06-25 05:01:22 +0000451static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100452do_fingerprint(struct passwd *pw)
453{
Damien Miller98c7ad62000-03-09 21:27:49 +1100454 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000455 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000456 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000457 int i, skip = 0, num = 1, invalid = 1;
458 enum fp_rep rep;
459 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100460 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100461
Ben Lindstromd0fca422001-03-26 13:44:06 +0000462 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
463 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000464
Damien Miller95def091999-11-25 00:26:21 +1100465 if (!have_identity)
466 ask_filename(pw, "Enter file in which the key is");
467 if (stat(identity_file, &st) < 0) {
468 perror(identity_file);
469 exit(1);
470 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000471 public = key_load_public(identity_file, &comment);
472 if (public != NULL) {
473 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000474 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100475 key_free(public);
476 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000477 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100478 exit(0);
479 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000480 if (comment)
481 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100482
483 f = fopen(identity_file, "r");
484 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100485 while (fgets(line, sizeof(line), f)) {
486 i = strlen(line) - 1;
487 if (line[i] != '\n') {
488 error("line %d too long: %.40s...", num, line);
489 skip = 1;
490 continue;
Damien Miller95def091999-11-25 00:26:21 +1100491 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100492 num++;
493 if (skip) {
494 skip = 0;
495 continue;
496 }
497 line[i] = '\0';
498
499 /* Skip leading whitespace, empty and comment lines. */
500 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
501 ;
502 if (!*cp || *cp == '\n' || *cp == '#')
503 continue ;
504 i = strtol(cp, &ep, 10);
505 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
506 int quoted = 0;
507 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000508 for (; *cp && (quoted || (*cp != ' ' &&
509 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100510 if (*cp == '\\' && cp[1] == '"')
511 cp++; /* Skip both */
512 else if (*cp == '"')
513 quoted = !quoted;
514 }
515 if (!*cp)
516 continue;
517 *cp++ = '\0';
518 }
519 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000520 public = key_new(KEY_RSA1);
521 if (key_read(public, &cp) != 1) {
522 cp = ep;
523 key_free(public);
524 public = key_new(KEY_UNSPEC);
525 if (key_read(public, &cp) != 1) {
526 key_free(public);
527 continue;
528 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100529 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000530 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000531 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000532 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000533 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000534 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000535 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000536 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100537 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100538 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100539 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100540 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100541 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100542 exit(1);
543 }
Damien Miller95def091999-11-25 00:26:21 +1100544 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100545}
546
Damien Miller95def091999-11-25 00:26:21 +1100547/*
548 * Perform changing a passphrase. The argument is the passwd structure
549 * for the current user.
550 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000551static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100552do_change_passphrase(struct passwd *pw)
553{
Damien Miller95def091999-11-25 00:26:21 +1100554 char *comment;
555 char *old_passphrase, *passphrase1, *passphrase2;
556 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000557 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100558
Damien Miller95def091999-11-25 00:26:21 +1100559 if (!have_identity)
560 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100561 if (stat(identity_file, &st) < 0) {
562 perror(identity_file);
563 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564 }
Damien Miller95def091999-11-25 00:26:21 +1100565 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000566 private = key_load_private(identity_file, "", &comment);
567 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100568 if (identity_passphrase)
569 old_passphrase = xstrdup(identity_passphrase);
570 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000571 old_passphrase =
572 read_passphrase("Enter old passphrase: ",
573 RP_ALLOW_STDIN);
574 private = key_load_private(identity_file, old_passphrase,
575 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000576 memset(old_passphrase, 0, strlen(old_passphrase));
577 xfree(old_passphrase);
578 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100579 printf("Bad passphrase.\n");
580 exit(1);
581 }
Damien Miller95def091999-11-25 00:26:21 +1100582 }
583 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584
Damien Miller95def091999-11-25 00:26:21 +1100585 /* Ask the new passphrase (twice). */
586 if (identity_new_passphrase) {
587 passphrase1 = xstrdup(identity_new_passphrase);
588 passphrase2 = NULL;
589 } else {
590 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000591 read_passphrase("Enter new passphrase (empty for no "
592 "passphrase): ", RP_ALLOW_STDIN);
593 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100594 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100595
596 /* Verify that they are the same. */
597 if (strcmp(passphrase1, passphrase2) != 0) {
598 memset(passphrase1, 0, strlen(passphrase1));
599 memset(passphrase2, 0, strlen(passphrase2));
600 xfree(passphrase1);
601 xfree(passphrase2);
602 printf("Pass phrases do not match. Try again.\n");
603 exit(1);
604 }
605 /* Destroy the other copy. */
606 memset(passphrase2, 0, strlen(passphrase2));
607 xfree(passphrase2);
608 }
609
610 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000611 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000612 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100613 memset(passphrase1, 0, strlen(passphrase1));
614 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000615 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100616 xfree(comment);
617 exit(1);
618 }
619 /* Destroy the passphrase and the copy of the key in memory. */
620 memset(passphrase1, 0, strlen(passphrase1));
621 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000622 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100623 xfree(comment);
624
625 printf("Your identification has been saved with the new passphrase.\n");
626 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627}
628
Damien Miller37876e92003-05-15 10:19:46 +1000629/*
630 * Print the SSHFP RR.
631 */
632static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000633do_print_resource_record(struct passwd *pw, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000634{
635 Key *public;
636 char *comment = NULL;
637 struct stat st;
638
639 if (!have_identity)
640 ask_filename(pw, "Enter file in which the key is");
641 if (stat(identity_file, &st) < 0) {
642 perror(identity_file);
643 exit(1);
644 }
645 public = key_load_public(identity_file, &comment);
646 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000647 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000648 key_free(public);
649 xfree(comment);
650 exit(0);
651 }
652 if (comment)
653 xfree(comment);
654
655 printf("failed to read v2 public key from %s.\n", identity_file);
656 exit(1);
657}
Damien Miller37876e92003-05-15 10:19:46 +1000658
Damien Miller95def091999-11-25 00:26:21 +1100659/*
660 * Change the comment of a private key file.
661 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000662static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663do_change_comment(struct passwd *pw)
664{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000665 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000666 Key *private;
667 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100668 struct stat st;
669 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000670 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671
Damien Miller95def091999-11-25 00:26:21 +1100672 if (!have_identity)
673 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100674 if (stat(identity_file, &st) < 0) {
675 perror(identity_file);
676 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000678 private = key_load_private(identity_file, "", &comment);
679 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100680 if (identity_passphrase)
681 passphrase = xstrdup(identity_passphrase);
682 else if (identity_new_passphrase)
683 passphrase = xstrdup(identity_new_passphrase);
684 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000685 passphrase = read_passphrase("Enter passphrase: ",
686 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100687 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000688 private = key_load_private(identity_file, passphrase, &comment);
689 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100690 memset(passphrase, 0, strlen(passphrase));
691 xfree(passphrase);
692 printf("Bad passphrase.\n");
693 exit(1);
694 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000695 } else {
696 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100697 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000698 if (private->type != KEY_RSA1) {
699 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
700 key_free(private);
701 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100702 }
Damien Miller95def091999-11-25 00:26:21 +1100703 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704
Damien Miller95def091999-11-25 00:26:21 +1100705 if (identity_comment) {
706 strlcpy(new_comment, identity_comment, sizeof(new_comment));
707 } else {
708 printf("Enter new comment: ");
709 fflush(stdout);
710 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
711 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000712 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100713 exit(1);
714 }
Damien Miller95def091999-11-25 00:26:21 +1100715 if (strchr(new_comment, '\n'))
716 *strchr(new_comment, '\n') = 0;
717 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718
Damien Miller95def091999-11-25 00:26:21 +1100719 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000720 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000721 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100722 memset(passphrase, 0, strlen(passphrase));
723 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000724 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100725 xfree(comment);
726 exit(1);
727 }
Damien Miller95def091999-11-25 00:26:21 +1100728 memset(passphrase, 0, strlen(passphrase));
729 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000730 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000731 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732
Damien Miller95def091999-11-25 00:26:21 +1100733 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000734 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
735 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100736 printf("Could not save your public key in %s\n", identity_file);
737 exit(1);
738 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000739 f = fdopen(fd, "w");
740 if (f == NULL) {
741 printf("fdopen %s failed", identity_file);
742 exit(1);
743 }
Damien Millereba71ba2000-04-29 23:57:08 +1000744 if (!key_write(public, f))
745 fprintf(stderr, "write key failed");
746 key_free(public);
747 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100748 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749
Damien Miller95def091999-11-25 00:26:21 +1100750 xfree(comment);
751
752 printf("The comment in your key file has been changed.\n");
753 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000754}
755
Ben Lindstrombba81212001-06-25 05:01:22 +0000756static void
Damien Miller431f66b1999-11-21 18:31:57 +1100757usage(void)
758{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000759 fprintf(stderr, "Usage: %s [options]\n", __progname);
760 fprintf(stderr, "Options:\n");
761 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
762 fprintf(stderr, " -c Change comment in private and public key files.\n");
763 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
764 fprintf(stderr, " -f filename Filename of the key file.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000765 fprintf(stderr, " -g Use generic DNS resource record format.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000766 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
767 fprintf(stderr, " -l Show fingerprint of key file.\n");
768 fprintf(stderr, " -p Change passphrase of private key file.\n");
769 fprintf(stderr, " -q Quiet.\n");
770 fprintf(stderr, " -y Read private key file and print public key.\n");
771 fprintf(stderr, " -t type Specify type of key to create.\n");
772 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
773 fprintf(stderr, " -C comment Provide new comment.\n");
774 fprintf(stderr, " -N phrase Provide new passphrase.\n");
775 fprintf(stderr, " -P phrase Provide old passphrase.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000776 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000777#ifdef SMARTCARD
778 fprintf(stderr, " -D reader Download public key from smartcard.\n");
779 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
780#endif /* SMARTCARD */
781
Darren Tucker019cefe2003-08-02 22:40:07 +1000782 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli\n");
783 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli\n");
784
Damien Miller95def091999-11-25 00:26:21 +1100785 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100786}
787
Damien Miller95def091999-11-25 00:26:21 +1100788/*
789 * Main program for key management.
790 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791int
792main(int ac, char **av)
793{
Damien Millera41c8b12002-01-22 23:05:08 +1100794 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Tim Rice2e0e38e2003-09-08 16:11:33 -0700795 char out_file[MAXPATHLEN], *reader_id = NULL;
Damien Miller37876e92003-05-15 10:19:46 +1000796 char *resource_record_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000797 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100798 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100799 struct stat st;
Darren Tucker019cefe2003-08-02 22:40:07 +1000800 int opt, type, fd, download = 0, memory = 0;
801 int generator_wanted = 0, trials = 100;
802 int do_gen_candidates = 0, do_screen_candidates = 0;
Darren Tucker06930c72003-12-31 11:34:51 +1100803 int log_level = SYSLOG_LEVEL_INFO;
Darren Tucker019cefe2003-08-02 22:40:07 +1000804 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100805 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100806
Damien Miller95def091999-11-25 00:26:21 +1100807 extern int optind;
808 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809
Damien Miller59d3d5b2003-08-22 09:34:41 +1000810 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000811
Damien Millerd3a18572000-06-07 19:55:44 +1000812 SSLeay_add_all_algorithms();
Darren Tucker019cefe2003-08-02 22:40:07 +1000813 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
814
Kevin Steves3a881912002-07-20 19:05:40 +0000815 init_rng();
816 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +1000817
Damien Miller5428f641999-11-25 11:54:57 +1100818 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100819 pw = getpwuid(getuid());
820 if (!pw) {
821 printf("You don't exist, go away!\n");
822 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823 }
Damien Millereba71ba2000-04-29 23:57:08 +1000824 if (gethostname(hostname, sizeof(hostname)) < 0) {
825 perror("gethostname");
826 exit(1);
827 }
Damien Miller5428f641999-11-25 11:54:57 +1100828
Darren Tucker019cefe2003-08-02 22:40:07 +1000829 while ((opt = getopt(ac, av,
Darren Tucker06930c72003-12-31 11:34:51 +1100830 "degiqpclBRvxXyb:f:t:U:D:P:N:C:r:g:T:G:M:S:a:W:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100831 switch (opt) {
832 case 'b':
833 bits = atoi(optarg);
834 if (bits < 512 || bits > 32768) {
835 printf("Bits has bad value.\n");
836 exit(1);
837 }
838 break;
Damien Miller95def091999-11-25 00:26:21 +1100839 case 'l':
840 print_fingerprint = 1;
841 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000842 case 'B':
843 print_bubblebabble = 1;
844 break;
Damien Miller95def091999-11-25 00:26:21 +1100845 case 'p':
846 change_passphrase = 1;
847 break;
Damien Miller95def091999-11-25 00:26:21 +1100848 case 'c':
849 change_comment = 1;
850 break;
Damien Miller95def091999-11-25 00:26:21 +1100851 case 'f':
852 strlcpy(identity_file, optarg, sizeof(identity_file));
853 have_identity = 1;
854 break;
Damien Miller37876e92003-05-15 10:19:46 +1000855 case 'g':
856 print_generic = 1;
857 break;
Damien Miller95def091999-11-25 00:26:21 +1100858 case 'P':
859 identity_passphrase = optarg;
860 break;
Damien Miller95def091999-11-25 00:26:21 +1100861 case 'N':
862 identity_new_passphrase = optarg;
863 break;
Damien Miller95def091999-11-25 00:26:21 +1100864 case 'C':
865 identity_comment = optarg;
866 break;
Damien Miller95def091999-11-25 00:26:21 +1100867 case 'q':
868 quiet = 1;
869 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000870 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100871 /* unused */
872 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000873 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000874 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000875 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000876 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000877 convert_to_ssh2 = 1;
878 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000879 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000880 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000881 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000882 convert_from_ssh2 = 1;
883 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000884 case 'y':
885 print_public = 1;
886 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000887 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100888 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000889 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100890 case 't':
891 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100892 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000893 case 'D':
894 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000895 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000896 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000897 break;
Darren Tucker06930c72003-12-31 11:34:51 +1100898 case 'v':
899 if (log_level == SYSLOG_LEVEL_INFO)
900 log_level = SYSLOG_LEVEL_DEBUG1;
901 else {
902 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
903 log_level < SYSLOG_LEVEL_DEBUG3)
904 log_level++;
905 }
906 break;
Damien Miller37876e92003-05-15 10:19:46 +1000907 case 'r':
908 resource_record_hostname = optarg;
909 break;
Darren Tucker019cefe2003-08-02 22:40:07 +1000910 case 'W':
911 generator_wanted = atoi(optarg);
912 if (generator_wanted < 1)
913 fatal("Desired generator has bad value.");
914 break;
915 case 'a':
916 trials = atoi(optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +1000917 break;
918 case 'M':
919 memory = atoi(optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +1000920 break;
921 case 'G':
922 do_gen_candidates = 1;
923 strlcpy(out_file, optarg, sizeof(out_file));
924 break;
925 case 'T':
926 do_screen_candidates = 1;
927 strlcpy(out_file, optarg, sizeof(out_file));
928 break;
929 case 'S':
930 /* XXX - also compare length against bits */
931 if (BN_hex2bn(&start, optarg) == 0)
932 fatal("Invalid start point.");
933 break;
Damien Miller95def091999-11-25 00:26:21 +1100934 case '?':
935 default:
936 usage();
937 }
938 }
Darren Tucker06930c72003-12-31 11:34:51 +1100939
940 /* reinit */
941 log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
942
Damien Miller95def091999-11-25 00:26:21 +1100943 if (optind < ac) {
944 printf("Too many arguments.\n");
945 usage();
946 }
947 if (change_passphrase && change_comment) {
948 printf("Can only have one of -p and -c.\n");
949 usage();
950 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000951 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100952 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100953 if (change_passphrase)
954 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100955 if (change_comment)
956 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +0000957 if (convert_to_ssh2)
958 do_convert_to_ssh2(pw);
959 if (convert_from_ssh2)
960 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000961 if (print_public)
962 do_print_public(pw);
Damien Miller37876e92003-05-15 10:19:46 +1000963 if (resource_record_hostname != NULL) {
Damien Miller37876e92003-05-15 10:19:46 +1000964 do_print_resource_record(pw, resource_record_hostname);
Damien Miller37876e92003-05-15 10:19:46 +1000965 }
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000966 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000967#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000968 if (download)
969 do_download(pw, reader_id);
970 else
971 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000972#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000973 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000974#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000975 }
Damien Miller95def091999-11-25 00:26:21 +1100976
Darren Tucker019cefe2003-08-02 22:40:07 +1000977 if (do_gen_candidates) {
978 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +1100979
Darren Tucker019cefe2003-08-02 22:40:07 +1000980 if (out == NULL) {
981 error("Couldn't open modulus candidate file \"%s\": %s",
982 out_file, strerror(errno));
983 return (1);
984 }
985 if (gen_candidates(out, memory, bits, start) != 0)
986 fatal("modulus candidate generation failed\n");
987
988 return (0);
989 }
990
991 if (do_screen_candidates) {
992 FILE *in;
993 FILE *out = fopen(out_file, "w");
994
995 if (have_identity && strcmp(identity_file, "-") != 0) {
996 if ((in = fopen(identity_file, "r")) == NULL) {
997 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +1100998 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +1000999 strerror(errno));
1000 }
1001 } else
1002 in = stdin;
1003
1004 if (out == NULL) {
1005 fatal("Couldn't open moduli file \"%s\": %s",
1006 out_file, strerror(errno));
1007 }
1008 if (prime_test(in, out, trials, generator_wanted) != 0)
1009 fatal("modulus screening failed\n");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001010 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001011 }
1012
Damien Miller95def091999-11-25 00:26:21 +11001013 arc4random_stir();
1014
Damien Miller154dda72002-01-22 23:08:16 +11001015 if (key_type_name == NULL) {
1016 printf("You must specify a key type (-t).\n");
1017 usage();
1018 }
Damien Millere39cacc2000-11-29 12:18:44 +11001019 type = key_type_from_name(key_type_name);
1020 if (type == KEY_UNSPEC) {
1021 fprintf(stderr, "unknown key type %s\n", key_type_name);
1022 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001023 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001024 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +11001025 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001026 private = key_generate(type, bits);
1027 if (private == NULL) {
1028 fprintf(stderr, "key_generate failed");
1029 exit(1);
1030 }
1031 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001032
1033 if (!have_identity)
1034 ask_filename(pw, "Enter file in which to save the key");
1035
1036 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001037 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001038 if (strstr(identity_file, dotsshdir) != NULL &&
1039 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001040 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001041 error("Could not create directory '%s'.", dotsshdir);
1042 else if (!quiet)
1043 printf("Created directory '%s'.\n", dotsshdir);
1044 }
1045 /* If the file already exists, ask the user to confirm. */
1046 if (stat(identity_file, &st) >= 0) {
1047 char yesno[3];
1048 printf("%s already exists.\n", identity_file);
1049 printf("Overwrite (y/n)? ");
1050 fflush(stdout);
1051 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1052 exit(1);
1053 if (yesno[0] != 'y' && yesno[0] != 'Y')
1054 exit(1);
1055 }
1056 /* Ask for a passphrase (twice). */
1057 if (identity_passphrase)
1058 passphrase1 = xstrdup(identity_passphrase);
1059 else if (identity_new_passphrase)
1060 passphrase1 = xstrdup(identity_new_passphrase);
1061 else {
1062passphrase_again:
1063 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001064 read_passphrase("Enter passphrase (empty for no "
1065 "passphrase): ", RP_ALLOW_STDIN);
1066 passphrase2 = read_passphrase("Enter same passphrase again: ",
1067 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001068 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001069 /*
1070 * The passphrases do not match. Clear them and
1071 * retry.
1072 */
Damien Miller95def091999-11-25 00:26:21 +11001073 memset(passphrase1, 0, strlen(passphrase1));
1074 memset(passphrase2, 0, strlen(passphrase2));
1075 xfree(passphrase1);
1076 xfree(passphrase2);
1077 printf("Passphrases do not match. Try again.\n");
1078 goto passphrase_again;
1079 }
1080 /* Clear the other copy of the passphrase. */
1081 memset(passphrase2, 0, strlen(passphrase2));
1082 xfree(passphrase2);
1083 }
1084
Damien Miller95def091999-11-25 00:26:21 +11001085 if (identity_comment) {
1086 strlcpy(comment, identity_comment, sizeof(comment));
1087 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001088 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001089 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1090 }
1091
1092 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001093 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001094 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001095 memset(passphrase1, 0, strlen(passphrase1));
1096 xfree(passphrase1);
1097 exit(1);
1098 }
1099 /* Clear the passphrase. */
1100 memset(passphrase1, 0, strlen(passphrase1));
1101 xfree(passphrase1);
1102
1103 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001104 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001105 arc4random_stir();
1106
1107 if (!quiet)
1108 printf("Your identification has been saved in %s.\n", identity_file);
1109
Damien Miller95def091999-11-25 00:26:21 +11001110 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001111 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1112 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001113 printf("Could not save your public key in %s\n", identity_file);
1114 exit(1);
1115 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001116 f = fdopen(fd, "w");
1117 if (f == NULL) {
1118 printf("fdopen %s failed", identity_file);
1119 exit(1);
1120 }
Damien Millereba71ba2000-04-29 23:57:08 +10001121 if (!key_write(public, f))
1122 fprintf(stderr, "write key failed");
1123 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001124 fclose(f);
1125
1126 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001127 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001128 printf("Your public key has been saved in %s.\n",
1129 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001130 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001131 printf("%s %s\n", fp, comment);
1132 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001133 }
Damien Millereba71ba2000-04-29 23:57:08 +10001134
1135 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001136 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137}