blob: 54aa5a59c22c30318fa359e6a1505c275a2c4b3c [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"
Damien Millera8e06ce2003-11-21 23:48:55 +110015RCSID("$OpenBSD: ssh-keygen.c,v 1.111 2003/11/21 11:57:03 djm 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"
29#include "readpass.h"
Darren Tucker019cefe2003-08-02 22:40:07 +100030#include "moduli.h"
Damien Miller874d77b2000-10-14 16:23:11 +110031
Ben Lindstrom8282d6a2001-08-06 21:44:05 +000032#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +000033#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000034#endif
Damien Millered12a262003-05-15 13:37:43 +100035#include "dns.h"
Ben Lindstromcd392282001-07-04 03:44:03 +000036
Damien Millereba71ba2000-04-29 23:57:08 +100037/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038int bits = 1024;
39
Damien Miller5428f641999-11-25 11:54:57 +110040/*
41 * Flag indicating that we just want to change the passphrase. This can be
42 * set on the command line.
43 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044int change_passphrase = 0;
45
Damien Miller5428f641999-11-25 11:54:57 +110046/*
47 * Flag indicating that we just want to change the comment. This can be set
48 * on the command line.
49 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050int change_comment = 0;
51
52int quiet = 0;
53
Damien Miller10f6f6b1999-11-17 17:29:08 +110054/* Flag indicating that we just want to see the key fingerprint */
55int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000056int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110057
Damien Miller431f66b1999-11-21 18:31:57 +110058/* The identity file name, given on the command line or entered by the user. */
59char identity_file[1024];
60int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62/* This is set to the passphrase if given on the command line. */
63char *identity_passphrase = NULL;
64
65/* This is set to the new passphrase if given on the command line. */
66char *identity_new_passphrase = NULL;
67
68/* This is set to the new comment if given on the command line. */
69char *identity_comment = NULL;
70
Damien Millereba71ba2000-04-29 23:57:08 +100071/* Dump public key file in format used by real and the original SSH 2 */
72int convert_to_ssh2 = 0;
73int convert_from_ssh2 = 0;
74int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +100075int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110076
Damien Millera41c8b12002-01-22 23:05:08 +110077char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100078
Damien Miller431f66b1999-11-21 18:31:57 +110079/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110080#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110081extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000082#else
83char *__progname;
84#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Millereba71ba2000-04-29 23:57:08 +100086char hostname[MAXHOSTNAMELEN];
87
Ben Lindstrombba81212001-06-25 05:01:22 +000088static void
Damien Miller431f66b1999-11-21 18:31:57 +110089ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090{
Damien Miller95def091999-11-25 00:26:21 +110091 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110092 char *name = NULL;
93
Damien Miller993dd552002-02-19 15:22:47 +110094 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +000095 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller993dd552002-02-19 15:22:47 +110096 else
97 switch (key_type_from_name(key_type_name)) {
98 case KEY_RSA1:
99 name = _PATH_SSH_CLIENT_IDENTITY;
100 break;
101 case KEY_DSA:
102 name = _PATH_SSH_CLIENT_ID_DSA;
103 break;
104 case KEY_RSA:
105 name = _PATH_SSH_CLIENT_ID_RSA;
106 break;
107 default:
108 fprintf(stderr, "bad key type");
109 exit(1);
110 break;
111 }
112
Damien Millere39cacc2000-11-29 12:18:44 +1100113 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000114 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100115 if (fgets(buf, sizeof(buf), stdin) == NULL)
116 exit(1);
117 if (strchr(buf, '\n'))
118 *strchr(buf, '\n') = 0;
119 if (strcmp(buf, "") != 0)
120 strlcpy(identity_file, buf, sizeof(identity_file));
121 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100122}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Ben Lindstrombba81212001-06-25 05:01:22 +0000124static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000125load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000126{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000127 char *pass;
128 Key *prv;
129
Ben Lindstroma3700052001-04-05 23:26:32 +0000130 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000131 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000132 if (identity_passphrase)
133 pass = xstrdup(identity_passphrase);
134 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000135 pass = read_passphrase("Enter passphrase: ",
136 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000137 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000138 memset(pass, 0, strlen(pass));
139 xfree(pass);
140 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000141 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000142}
143
Damien Miller874d77b2000-10-14 16:23:11 +1100144#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000145#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100146#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000147#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000148
Ben Lindstrombba81212001-06-25 05:01:22 +0000149static void
Damien Millereba71ba2000-04-29 23:57:08 +1000150do_convert_to_ssh2(struct passwd *pw)
151{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000152 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000153 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000154 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000155 struct stat st;
156
157 if (!have_identity)
158 ask_filename(pw, "Enter file in which the key is");
159 if (stat(identity_file, &st) < 0) {
160 perror(identity_file);
161 exit(1);
162 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000163 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000164 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000165 fprintf(stderr, "load failed\n");
166 exit(1);
167 }
Damien Millereba71ba2000-04-29 23:57:08 +1000168 }
Damien Millerdb274722003-05-14 13:45:22 +1000169 if (k->type == KEY_RSA1) {
170 fprintf(stderr, "version 1 keys are not supported\n");
171 exit(1);
172 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000173 if (key_to_blob(k, &blob, &len) <= 0) {
174 fprintf(stderr, "key_to_blob failed\n");
175 exit(1);
176 }
Damien Miller874d77b2000-10-14 16:23:11 +1100177 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000178 fprintf(stdout,
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000179 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000180 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000181 pw->pw_name, hostname);
182 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100183 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000184 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000185 xfree(blob);
186 exit(0);
187}
188
Ben Lindstrombba81212001-06-25 05:01:22 +0000189static void
Damien Miller874d77b2000-10-14 16:23:11 +1100190buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
191{
Darren Tuckerc0815c92003-09-22 21:05:50 +1000192 u_int bits = buffer_get_int(b);
193 u_int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000194
Damien Miller874d77b2000-10-14 16:23:11 +1100195 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000196 fatal("buffer_get_bignum_bits: input buffer too small: "
197 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100198 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100199 buffer_consume(b, bytes);
200}
201
Ben Lindstrombba81212001-06-25 05:01:22 +0000202static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000203do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100204{
205 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100206 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100207 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000208 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000209 int magic, rlen, ktype, i1, i2, i3, i4;
210 u_int slen;
211 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100212
213 buffer_init(&b);
214 buffer_append(&b, blob, blen);
215
216 magic = buffer_get_int(&b);
217 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
218 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
219 buffer_free(&b);
220 return NULL;
221 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000222 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100223 type = buffer_get_string(&b, NULL);
224 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000225 i2 = buffer_get_int(&b);
226 i3 = buffer_get_int(&b);
227 i4 = buffer_get_int(&b);
228 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100229 if (strcmp(cipher, "none") != 0) {
230 error("unsupported cipher %s", cipher);
231 xfree(cipher);
232 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000233 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100234 return NULL;
235 }
236 xfree(cipher);
237
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000238 if (strstr(type, "dsa")) {
239 ktype = KEY_DSA;
240 } else if (strstr(type, "rsa")) {
241 ktype = KEY_RSA;
242 } else {
243 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100244 return NULL;
245 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000246 key = key_new_private(ktype);
247 xfree(type);
248
249 switch (key->type) {
250 case KEY_DSA:
251 buffer_get_bignum_bits(&b, key->dsa->p);
252 buffer_get_bignum_bits(&b, key->dsa->g);
253 buffer_get_bignum_bits(&b, key->dsa->q);
254 buffer_get_bignum_bits(&b, key->dsa->pub_key);
255 buffer_get_bignum_bits(&b, key->dsa->priv_key);
256 break;
257 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000258 e = buffer_get_char(&b);
259 debug("e %lx", e);
260 if (e < 30) {
261 e <<= 8;
262 e += buffer_get_char(&b);
263 debug("e %lx", e);
264 e <<= 8;
265 e += buffer_get_char(&b);
266 debug("e %lx", e);
267 }
268 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000269 buffer_free(&b);
270 key_free(key);
271 return NULL;
272 }
273 buffer_get_bignum_bits(&b, key->rsa->d);
274 buffer_get_bignum_bits(&b, key->rsa->n);
275 buffer_get_bignum_bits(&b, key->rsa->iqmp);
276 buffer_get_bignum_bits(&b, key->rsa->q);
277 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000278 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000279 break;
280 }
Damien Miller874d77b2000-10-14 16:23:11 +1100281 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000282 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000283 error("do_convert_private_ssh2_from_blob: "
284 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100285 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000286
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000287 /* try the key */
288 key_sign(key, &sig, &slen, data, sizeof(data));
289 key_verify(key, sig, slen, data, sizeof(data));
290 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100291 return key;
292}
293
Ben Lindstrombba81212001-06-25 05:01:22 +0000294static void
Damien Millereba71ba2000-04-29 23:57:08 +1000295do_convert_from_ssh2(struct passwd *pw)
296{
297 Key *k;
298 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000299 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000300 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000301 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000302 char encoded[8096];
303 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100304 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000305 FILE *fp;
306
307 if (!have_identity)
308 ask_filename(pw, "Enter file in which the key is");
309 if (stat(identity_file, &st) < 0) {
310 perror(identity_file);
311 exit(1);
312 }
313 fp = fopen(identity_file, "r");
314 if (fp == NULL) {
315 perror(identity_file);
316 exit(1);
317 }
318 encoded[0] = '\0';
319 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000320 if (!(p = strchr(line, '\n'))) {
321 fprintf(stderr, "input line too long.\n");
322 exit(1);
323 }
324 if (p > line && p[-1] == '\\')
325 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000326 if (strncmp(line, "----", 4) == 0 ||
327 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100328 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
329 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000330 if (strstr(line, " END ") != NULL) {
331 break;
332 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000333 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000334 continue;
335 }
Damien Miller30c3d422000-05-09 11:02:59 +1000336 if (escaped) {
337 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000338 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000339 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000340 }
341 *p = '\0';
342 strlcat(encoded, line, sizeof(encoded));
343 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000344 len = strlen(encoded);
345 if (((len % 4) == 3) &&
346 (encoded[len-1] == '=') &&
347 (encoded[len-2] == '=') &&
348 (encoded[len-3] == '='))
349 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100350 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000351 if (blen < 0) {
352 fprintf(stderr, "uudecode failed.\n");
353 exit(1);
354 }
Damien Miller874d77b2000-10-14 16:23:11 +1100355 k = private ?
356 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100357 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100358 if (k == NULL) {
359 fprintf(stderr, "decode blob failed.\n");
360 exit(1);
361 }
362 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000363 (k->type == KEY_DSA ?
364 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
365 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100366 key_write(k, stdout);
367 if (!ok) {
368 fprintf(stderr, "key write failed");
369 exit(1);
370 }
Damien Millereba71ba2000-04-29 23:57:08 +1000371 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100372 if (!private)
373 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000374 fclose(fp);
375 exit(0);
376}
377
Ben Lindstrombba81212001-06-25 05:01:22 +0000378static void
Damien Millereba71ba2000-04-29 23:57:08 +1000379do_print_public(struct passwd *pw)
380{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000381 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000382 struct stat st;
383
384 if (!have_identity)
385 ask_filename(pw, "Enter file in which the key is");
386 if (stat(identity_file, &st) < 0) {
387 perror(identity_file);
388 exit(1);
389 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000390 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000391 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000392 fprintf(stderr, "load failed\n");
393 exit(1);
394 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000395 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000396 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000397 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000398 fprintf(stdout, "\n");
399 exit(0);
400}
401
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000402#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000403static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000404do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000405{
Ben Lindstromcd392282001-07-04 03:44:03 +0000406 Key *prv = NULL;
407 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000408 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000409
410 if (!have_identity)
411 ask_filename(pw, "Enter file in which the key is");
412 if (stat(identity_file, &st) < 0) {
413 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000414 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000415 }
416 prv = load_identity(identity_file);
417 if (prv == NULL) {
418 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000419 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000420 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000421 ret = sc_put_key(prv, sc_reader_id);
422 key_free(prv);
423 if (ret < 0)
424 exit(1);
Damien Miller996acd22003-04-09 20:59:48 +1000425 logit("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000426 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000427}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000428
429static void
430do_download(struct passwd *pw, const char *sc_reader_id)
431{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000432 Key **keys = NULL;
433 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000434
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000435 keys = sc_get_keys(sc_reader_id, NULL);
436 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000437 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000438 for (i = 0; keys[i]; i++) {
439 key_write(keys[i], stdout);
440 key_free(keys[i]);
441 fprintf(stdout, "\n");
442 }
443 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000444 exit(0);
445}
Ben Lindstromffce1472001-08-06 21:57:31 +0000446#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000447
Ben Lindstrombba81212001-06-25 05:01:22 +0000448static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100449do_fingerprint(struct passwd *pw)
450{
Damien Miller98c7ad62000-03-09 21:27:49 +1100451 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000452 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000453 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000454 int i, skip = 0, num = 1, invalid = 1;
455 enum fp_rep rep;
456 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100457 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100458
Ben Lindstromd0fca422001-03-26 13:44:06 +0000459 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
460 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000461
Damien Miller95def091999-11-25 00:26:21 +1100462 if (!have_identity)
463 ask_filename(pw, "Enter file in which the key is");
464 if (stat(identity_file, &st) < 0) {
465 perror(identity_file);
466 exit(1);
467 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000468 public = key_load_public(identity_file, &comment);
469 if (public != NULL) {
470 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000471 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100472 key_free(public);
473 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000474 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100475 exit(0);
476 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000477 if (comment)
478 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100479
480 f = fopen(identity_file, "r");
481 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100482 while (fgets(line, sizeof(line), f)) {
483 i = strlen(line) - 1;
484 if (line[i] != '\n') {
485 error("line %d too long: %.40s...", num, line);
486 skip = 1;
487 continue;
Damien Miller95def091999-11-25 00:26:21 +1100488 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100489 num++;
490 if (skip) {
491 skip = 0;
492 continue;
493 }
494 line[i] = '\0';
495
496 /* Skip leading whitespace, empty and comment lines. */
497 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
498 ;
499 if (!*cp || *cp == '\n' || *cp == '#')
500 continue ;
501 i = strtol(cp, &ep, 10);
502 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
503 int quoted = 0;
504 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000505 for (; *cp && (quoted || (*cp != ' ' &&
506 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100507 if (*cp == '\\' && cp[1] == '"')
508 cp++; /* Skip both */
509 else if (*cp == '"')
510 quoted = !quoted;
511 }
512 if (!*cp)
513 continue;
514 *cp++ = '\0';
515 }
516 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000517 public = key_new(KEY_RSA1);
518 if (key_read(public, &cp) != 1) {
519 cp = ep;
520 key_free(public);
521 public = key_new(KEY_UNSPEC);
522 if (key_read(public, &cp) != 1) {
523 key_free(public);
524 continue;
525 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100526 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000527 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000528 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000529 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000530 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000531 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000532 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000533 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100534 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100535 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100536 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100537 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100538 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100539 exit(1);
540 }
Damien Miller95def091999-11-25 00:26:21 +1100541 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100542}
543
Damien Miller95def091999-11-25 00:26:21 +1100544/*
545 * Perform changing a passphrase. The argument is the passwd structure
546 * for the current user.
547 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000548static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100549do_change_passphrase(struct passwd *pw)
550{
Damien Miller95def091999-11-25 00:26:21 +1100551 char *comment;
552 char *old_passphrase, *passphrase1, *passphrase2;
553 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000554 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100555
Damien Miller95def091999-11-25 00:26:21 +1100556 if (!have_identity)
557 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100558 if (stat(identity_file, &st) < 0) {
559 perror(identity_file);
560 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561 }
Damien Miller95def091999-11-25 00:26:21 +1100562 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000563 private = key_load_private(identity_file, "", &comment);
564 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100565 if (identity_passphrase)
566 old_passphrase = xstrdup(identity_passphrase);
567 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000568 old_passphrase =
569 read_passphrase("Enter old passphrase: ",
570 RP_ALLOW_STDIN);
571 private = key_load_private(identity_file, old_passphrase,
572 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000573 memset(old_passphrase, 0, strlen(old_passphrase));
574 xfree(old_passphrase);
575 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100576 printf("Bad passphrase.\n");
577 exit(1);
578 }
Damien Miller95def091999-11-25 00:26:21 +1100579 }
580 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581
Damien Miller95def091999-11-25 00:26:21 +1100582 /* Ask the new passphrase (twice). */
583 if (identity_new_passphrase) {
584 passphrase1 = xstrdup(identity_new_passphrase);
585 passphrase2 = NULL;
586 } else {
587 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000588 read_passphrase("Enter new passphrase (empty for no "
589 "passphrase): ", RP_ALLOW_STDIN);
590 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100591 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100592
593 /* Verify that they are the same. */
594 if (strcmp(passphrase1, passphrase2) != 0) {
595 memset(passphrase1, 0, strlen(passphrase1));
596 memset(passphrase2, 0, strlen(passphrase2));
597 xfree(passphrase1);
598 xfree(passphrase2);
599 printf("Pass phrases do not match. Try again.\n");
600 exit(1);
601 }
602 /* Destroy the other copy. */
603 memset(passphrase2, 0, strlen(passphrase2));
604 xfree(passphrase2);
605 }
606
607 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000608 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000609 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100610 memset(passphrase1, 0, strlen(passphrase1));
611 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000612 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100613 xfree(comment);
614 exit(1);
615 }
616 /* Destroy the passphrase and the copy of the key in memory. */
617 memset(passphrase1, 0, strlen(passphrase1));
618 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000619 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100620 xfree(comment);
621
622 printf("Your identification has been saved with the new passphrase.\n");
623 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624}
625
Damien Miller37876e92003-05-15 10:19:46 +1000626/*
627 * Print the SSHFP RR.
628 */
629static void
630do_print_resource_record(struct passwd *pw, char *hostname)
631{
632 Key *public;
633 char *comment = NULL;
634 struct stat st;
635
636 if (!have_identity)
637 ask_filename(pw, "Enter file in which the key is");
638 if (stat(identity_file, &st) < 0) {
639 perror(identity_file);
640 exit(1);
641 }
642 public = key_load_public(identity_file, &comment);
643 if (public != NULL) {
644 export_dns_rr(hostname, public, stdout, print_generic);
645 key_free(public);
646 xfree(comment);
647 exit(0);
648 }
649 if (comment)
650 xfree(comment);
651
652 printf("failed to read v2 public key from %s.\n", identity_file);
653 exit(1);
654}
Damien Miller37876e92003-05-15 10:19:46 +1000655
Damien Miller95def091999-11-25 00:26:21 +1100656/*
657 * Change the comment of a private key file.
658 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000659static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660do_change_comment(struct passwd *pw)
661{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000662 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000663 Key *private;
664 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100665 struct stat st;
666 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000667 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668
Damien Miller95def091999-11-25 00:26:21 +1100669 if (!have_identity)
670 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100671 if (stat(identity_file, &st) < 0) {
672 perror(identity_file);
673 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000675 private = key_load_private(identity_file, "", &comment);
676 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100677 if (identity_passphrase)
678 passphrase = xstrdup(identity_passphrase);
679 else if (identity_new_passphrase)
680 passphrase = xstrdup(identity_new_passphrase);
681 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000682 passphrase = read_passphrase("Enter passphrase: ",
683 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100684 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000685 private = key_load_private(identity_file, passphrase, &comment);
686 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100687 memset(passphrase, 0, strlen(passphrase));
688 xfree(passphrase);
689 printf("Bad passphrase.\n");
690 exit(1);
691 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000692 } else {
693 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100694 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000695 if (private->type != KEY_RSA1) {
696 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
697 key_free(private);
698 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100699 }
Damien Miller95def091999-11-25 00:26:21 +1100700 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701
Damien Miller95def091999-11-25 00:26:21 +1100702 if (identity_comment) {
703 strlcpy(new_comment, identity_comment, sizeof(new_comment));
704 } else {
705 printf("Enter new comment: ");
706 fflush(stdout);
707 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
708 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000709 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100710 exit(1);
711 }
Damien Miller95def091999-11-25 00:26:21 +1100712 if (strchr(new_comment, '\n'))
713 *strchr(new_comment, '\n') = 0;
714 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715
Damien Miller95def091999-11-25 00:26:21 +1100716 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000717 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000718 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100719 memset(passphrase, 0, strlen(passphrase));
720 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000721 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100722 xfree(comment);
723 exit(1);
724 }
Damien Miller95def091999-11-25 00:26:21 +1100725 memset(passphrase, 0, strlen(passphrase));
726 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000727 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000728 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
Damien Miller95def091999-11-25 00:26:21 +1100730 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000731 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
732 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100733 printf("Could not save your public key in %s\n", identity_file);
734 exit(1);
735 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000736 f = fdopen(fd, "w");
737 if (f == NULL) {
738 printf("fdopen %s failed", identity_file);
739 exit(1);
740 }
Damien Millereba71ba2000-04-29 23:57:08 +1000741 if (!key_write(public, f))
742 fprintf(stderr, "write key failed");
743 key_free(public);
744 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100745 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746
Damien Miller95def091999-11-25 00:26:21 +1100747 xfree(comment);
748
749 printf("The comment in your key file has been changed.\n");
750 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751}
752
Ben Lindstrombba81212001-06-25 05:01:22 +0000753static void
Damien Miller431f66b1999-11-21 18:31:57 +1100754usage(void)
755{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000756 fprintf(stderr, "Usage: %s [options]\n", __progname);
757 fprintf(stderr, "Options:\n");
758 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
759 fprintf(stderr, " -c Change comment in private and public key files.\n");
760 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
761 fprintf(stderr, " -f filename Filename of the key file.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000762 fprintf(stderr, " -g Use generic DNS resource record format.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000763 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
764 fprintf(stderr, " -l Show fingerprint of key file.\n");
765 fprintf(stderr, " -p Change passphrase of private key file.\n");
766 fprintf(stderr, " -q Quiet.\n");
767 fprintf(stderr, " -y Read private key file and print public key.\n");
768 fprintf(stderr, " -t type Specify type of key to create.\n");
769 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
770 fprintf(stderr, " -C comment Provide new comment.\n");
771 fprintf(stderr, " -N phrase Provide new passphrase.\n");
772 fprintf(stderr, " -P phrase Provide old passphrase.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000773 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000774#ifdef SMARTCARD
775 fprintf(stderr, " -D reader Download public key from smartcard.\n");
776 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
777#endif /* SMARTCARD */
778
Darren Tucker019cefe2003-08-02 22:40:07 +1000779 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli\n");
780 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli\n");
781
Damien Miller95def091999-11-25 00:26:21 +1100782 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100783}
784
Damien Miller95def091999-11-25 00:26:21 +1100785/*
786 * Main program for key management.
787 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788int
789main(int ac, char **av)
790{
Damien Millera41c8b12002-01-22 23:05:08 +1100791 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Tim Rice2e0e38e2003-09-08 16:11:33 -0700792 char out_file[MAXPATHLEN], *reader_id = NULL;
Damien Miller37876e92003-05-15 10:19:46 +1000793 char *resource_record_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000794 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100795 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100796 struct stat st;
Darren Tucker019cefe2003-08-02 22:40:07 +1000797 int opt, type, fd, download = 0, memory = 0;
798 int generator_wanted = 0, trials = 100;
799 int do_gen_candidates = 0, do_screen_candidates = 0;
800 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100801 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100802
Damien Miller95def091999-11-25 00:26:21 +1100803 extern int optind;
804 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805
Damien Miller59d3d5b2003-08-22 09:34:41 +1000806 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000807
Damien Millerd3a18572000-06-07 19:55:44 +1000808 SSLeay_add_all_algorithms();
Darren Tucker019cefe2003-08-02 22:40:07 +1000809 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
810
Kevin Steves3a881912002-07-20 19:05:40 +0000811 init_rng();
812 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +1000813
Damien Miller5428f641999-11-25 11:54:57 +1100814 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100815 pw = getpwuid(getuid());
816 if (!pw) {
817 printf("You don't exist, go away!\n");
818 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819 }
Damien Millereba71ba2000-04-29 23:57:08 +1000820 if (gethostname(hostname, sizeof(hostname)) < 0) {
821 perror("gethostname");
822 exit(1);
823 }
Damien Miller5428f641999-11-25 11:54:57 +1100824
Darren Tucker019cefe2003-08-02 22:40:07 +1000825 while ((opt = getopt(ac, av,
826 "degiqpclBRxXyb:f:t:U:D:P:N:C:r:g:T:G:M:S:a:W:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100827 switch (opt) {
828 case 'b':
829 bits = atoi(optarg);
830 if (bits < 512 || bits > 32768) {
831 printf("Bits has bad value.\n");
832 exit(1);
833 }
834 break;
Damien Miller95def091999-11-25 00:26:21 +1100835 case 'l':
836 print_fingerprint = 1;
837 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000838 case 'B':
839 print_bubblebabble = 1;
840 break;
Damien Miller95def091999-11-25 00:26:21 +1100841 case 'p':
842 change_passphrase = 1;
843 break;
Damien Miller95def091999-11-25 00:26:21 +1100844 case 'c':
845 change_comment = 1;
846 break;
Damien Miller95def091999-11-25 00:26:21 +1100847 case 'f':
848 strlcpy(identity_file, optarg, sizeof(identity_file));
849 have_identity = 1;
850 break;
Damien Miller37876e92003-05-15 10:19:46 +1000851 case 'g':
852 print_generic = 1;
853 break;
Damien Miller95def091999-11-25 00:26:21 +1100854 case 'P':
855 identity_passphrase = optarg;
856 break;
Damien Miller95def091999-11-25 00:26:21 +1100857 case 'N':
858 identity_new_passphrase = optarg;
859 break;
Damien Miller95def091999-11-25 00:26:21 +1100860 case 'C':
861 identity_comment = optarg;
862 break;
Damien Miller95def091999-11-25 00:26:21 +1100863 case 'q':
864 quiet = 1;
865 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000866 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100867 /* unused */
868 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000869 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000870 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000871 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000872 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000873 convert_to_ssh2 = 1;
874 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000875 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000876 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000877 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000878 convert_from_ssh2 = 1;
879 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000880 case 'y':
881 print_public = 1;
882 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000883 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100884 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000885 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100886 case 't':
887 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100888 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000889 case 'D':
890 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000891 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000892 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000893 break;
Damien Miller37876e92003-05-15 10:19:46 +1000894 case 'r':
895 resource_record_hostname = optarg;
896 break;
Darren Tucker019cefe2003-08-02 22:40:07 +1000897 case 'W':
898 generator_wanted = atoi(optarg);
899 if (generator_wanted < 1)
900 fatal("Desired generator has bad value.");
901 break;
902 case 'a':
903 trials = atoi(optarg);
904 if (trials < TRIAL_MINIMUM) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100905 fatal("Minimum primality trials is %d",
Darren Tucker019cefe2003-08-02 22:40:07 +1000906 TRIAL_MINIMUM);
907 }
908 break;
909 case 'M':
910 memory = atoi(optarg);
Damien Millera8e06ce2003-11-21 23:48:55 +1100911 if (memory != 0 &&
Darren Tucker019cefe2003-08-02 22:40:07 +1000912 (memory < LARGE_MINIMUM || memory > LARGE_MAXIMUM)) {
913 fatal("Invalid memory amount (min %ld, max %ld)",
914 LARGE_MINIMUM, LARGE_MAXIMUM);
915 }
916 break;
917 case 'G':
918 do_gen_candidates = 1;
919 strlcpy(out_file, optarg, sizeof(out_file));
920 break;
921 case 'T':
922 do_screen_candidates = 1;
923 strlcpy(out_file, optarg, sizeof(out_file));
924 break;
925 case 'S':
926 /* XXX - also compare length against bits */
927 if (BN_hex2bn(&start, optarg) == 0)
928 fatal("Invalid start point.");
929 break;
Damien Miller95def091999-11-25 00:26:21 +1100930 case '?':
931 default:
932 usage();
933 }
934 }
935 if (optind < ac) {
936 printf("Too many arguments.\n");
937 usage();
938 }
939 if (change_passphrase && change_comment) {
940 printf("Can only have one of -p and -c.\n");
941 usage();
942 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000943 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100944 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100945 if (change_passphrase)
946 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100947 if (change_comment)
948 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +0000949 if (convert_to_ssh2)
950 do_convert_to_ssh2(pw);
951 if (convert_from_ssh2)
952 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000953 if (print_public)
954 do_print_public(pw);
Damien Miller37876e92003-05-15 10:19:46 +1000955 if (resource_record_hostname != NULL) {
Damien Miller37876e92003-05-15 10:19:46 +1000956 do_print_resource_record(pw, resource_record_hostname);
Damien Miller37876e92003-05-15 10:19:46 +1000957 }
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000958 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000959#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000960 if (download)
961 do_download(pw, reader_id);
962 else
963 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000964#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000965 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000966#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000967 }
Damien Miller95def091999-11-25 00:26:21 +1100968
Darren Tucker019cefe2003-08-02 22:40:07 +1000969 if (do_gen_candidates) {
970 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +1100971
Darren Tucker019cefe2003-08-02 22:40:07 +1000972 if (out == NULL) {
973 error("Couldn't open modulus candidate file \"%s\": %s",
974 out_file, strerror(errno));
975 return (1);
976 }
977 if (gen_candidates(out, memory, bits, start) != 0)
978 fatal("modulus candidate generation failed\n");
979
980 return (0);
981 }
982
983 if (do_screen_candidates) {
984 FILE *in;
985 FILE *out = fopen(out_file, "w");
986
987 if (have_identity && strcmp(identity_file, "-") != 0) {
988 if ((in = fopen(identity_file, "r")) == NULL) {
989 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +1100990 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +1000991 strerror(errno));
992 }
993 } else
994 in = stdin;
995
996 if (out == NULL) {
997 fatal("Couldn't open moduli file \"%s\": %s",
998 out_file, strerror(errno));
999 }
1000 if (prime_test(in, out, trials, generator_wanted) != 0)
1001 fatal("modulus screening failed\n");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001002 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001003 }
1004
Damien Miller95def091999-11-25 00:26:21 +11001005 arc4random_stir();
1006
Damien Miller154dda72002-01-22 23:08:16 +11001007 if (key_type_name == NULL) {
1008 printf("You must specify a key type (-t).\n");
1009 usage();
1010 }
Damien Millere39cacc2000-11-29 12:18:44 +11001011 type = key_type_from_name(key_type_name);
1012 if (type == KEY_UNSPEC) {
1013 fprintf(stderr, "unknown key type %s\n", key_type_name);
1014 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001015 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001016 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +11001017 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001018 private = key_generate(type, bits);
1019 if (private == NULL) {
1020 fprintf(stderr, "key_generate failed");
1021 exit(1);
1022 }
1023 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001024
1025 if (!have_identity)
1026 ask_filename(pw, "Enter file in which to save the key");
1027
1028 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001029 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001030 if (strstr(identity_file, dotsshdir) != NULL &&
1031 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001032 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001033 error("Could not create directory '%s'.", dotsshdir);
1034 else if (!quiet)
1035 printf("Created directory '%s'.\n", dotsshdir);
1036 }
1037 /* If the file already exists, ask the user to confirm. */
1038 if (stat(identity_file, &st) >= 0) {
1039 char yesno[3];
1040 printf("%s already exists.\n", identity_file);
1041 printf("Overwrite (y/n)? ");
1042 fflush(stdout);
1043 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1044 exit(1);
1045 if (yesno[0] != 'y' && yesno[0] != 'Y')
1046 exit(1);
1047 }
1048 /* Ask for a passphrase (twice). */
1049 if (identity_passphrase)
1050 passphrase1 = xstrdup(identity_passphrase);
1051 else if (identity_new_passphrase)
1052 passphrase1 = xstrdup(identity_new_passphrase);
1053 else {
1054passphrase_again:
1055 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001056 read_passphrase("Enter passphrase (empty for no "
1057 "passphrase): ", RP_ALLOW_STDIN);
1058 passphrase2 = read_passphrase("Enter same passphrase again: ",
1059 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001060 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001061 /*
1062 * The passphrases do not match. Clear them and
1063 * retry.
1064 */
Damien Miller95def091999-11-25 00:26:21 +11001065 memset(passphrase1, 0, strlen(passphrase1));
1066 memset(passphrase2, 0, strlen(passphrase2));
1067 xfree(passphrase1);
1068 xfree(passphrase2);
1069 printf("Passphrases do not match. Try again.\n");
1070 goto passphrase_again;
1071 }
1072 /* Clear the other copy of the passphrase. */
1073 memset(passphrase2, 0, strlen(passphrase2));
1074 xfree(passphrase2);
1075 }
1076
Damien Miller95def091999-11-25 00:26:21 +11001077 if (identity_comment) {
1078 strlcpy(comment, identity_comment, sizeof(comment));
1079 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001080 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001081 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1082 }
1083
1084 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001085 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001086 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001087 memset(passphrase1, 0, strlen(passphrase1));
1088 xfree(passphrase1);
1089 exit(1);
1090 }
1091 /* Clear the passphrase. */
1092 memset(passphrase1, 0, strlen(passphrase1));
1093 xfree(passphrase1);
1094
1095 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001096 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001097 arc4random_stir();
1098
1099 if (!quiet)
1100 printf("Your identification has been saved in %s.\n", identity_file);
1101
Damien Miller95def091999-11-25 00:26:21 +11001102 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001103 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1104 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001105 printf("Could not save your public key in %s\n", identity_file);
1106 exit(1);
1107 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001108 f = fdopen(fd, "w");
1109 if (f == NULL) {
1110 printf("fdopen %s failed", identity_file);
1111 exit(1);
1112 }
Damien Millereba71ba2000-04-29 23:57:08 +10001113 if (!key_write(public, f))
1114 fprintf(stderr, "write key failed");
1115 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001116 fclose(f);
1117
1118 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001119 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001120 printf("Your public key has been saved in %s.\n",
1121 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001122 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001123 printf("%s %s\n", fp, comment);
1124 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001125 }
Damien Millereba71ba2000-04-29 23:57:08 +10001126
1127 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001128 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129}