blob: d39e7d8811e9ccc393269eb229d6fcd773b08cc8 [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 Tuckerfc959702004-07-17 16:12:08 +100015RCSID("$OpenBSD: ssh-keygen.c,v 1.117 2004/07/11 17:48:47 deraadt 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 */
79extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Millereba71ba2000-04-29 23:57:08 +100081char hostname[MAXHOSTNAMELEN];
82
Darren Tucker770fc012004-05-13 16:24:32 +100083/* moduli.c */
84int gen_candidates(FILE *, int, int, BIGNUM *);
85int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
86
Ben Lindstrombba81212001-06-25 05:01:22 +000087static void
Damien Miller431f66b1999-11-21 18:31:57 +110088ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089{
Damien Miller95def091999-11-25 00:26:21 +110090 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110091 char *name = NULL;
92
Damien Miller993dd552002-02-19 15:22:47 +110093 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +000094 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller993dd552002-02-19 15:22:47 +110095 else
96 switch (key_type_from_name(key_type_name)) {
97 case KEY_RSA1:
98 name = _PATH_SSH_CLIENT_IDENTITY;
99 break;
100 case KEY_DSA:
101 name = _PATH_SSH_CLIENT_ID_DSA;
102 break;
103 case KEY_RSA:
104 name = _PATH_SSH_CLIENT_ID_RSA;
105 break;
106 default:
107 fprintf(stderr, "bad key type");
108 exit(1);
109 break;
110 }
111
Damien Millere39cacc2000-11-29 12:18:44 +1100112 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000113 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100114 if (fgets(buf, sizeof(buf), stdin) == NULL)
115 exit(1);
116 if (strchr(buf, '\n'))
117 *strchr(buf, '\n') = 0;
118 if (strcmp(buf, "") != 0)
119 strlcpy(identity_file, buf, sizeof(identity_file));
120 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100121}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Ben Lindstrombba81212001-06-25 05:01:22 +0000123static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000124load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000125{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000126 char *pass;
127 Key *prv;
128
Ben Lindstroma3700052001-04-05 23:26:32 +0000129 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000130 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000131 if (identity_passphrase)
132 pass = xstrdup(identity_passphrase);
133 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000134 pass = read_passphrase("Enter passphrase: ",
135 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000136 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000137 memset(pass, 0, strlen(pass));
138 xfree(pass);
139 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000140 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000141}
142
Damien Miller874d77b2000-10-14 16:23:11 +1100143#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000144#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100145#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000146#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000147
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static void
Damien Millereba71ba2000-04-29 23:57:08 +1000149do_convert_to_ssh2(struct passwd *pw)
150{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000151 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000152 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000153 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000154 struct stat st;
155
156 if (!have_identity)
157 ask_filename(pw, "Enter file in which the key is");
158 if (stat(identity_file, &st) < 0) {
159 perror(identity_file);
160 exit(1);
161 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000162 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000163 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000164 fprintf(stderr, "load failed\n");
165 exit(1);
166 }
Damien Millereba71ba2000-04-29 23:57:08 +1000167 }
Damien Millerdb274722003-05-14 13:45:22 +1000168 if (k->type == KEY_RSA1) {
169 fprintf(stderr, "version 1 keys are not supported\n");
170 exit(1);
171 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000172 if (key_to_blob(k, &blob, &len) <= 0) {
173 fprintf(stderr, "key_to_blob failed\n");
174 exit(1);
175 }
Damien Miller874d77b2000-10-14 16:23:11 +1100176 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000177 fprintf(stdout,
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000178 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000179 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000180 pw->pw_name, hostname);
181 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100182 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000183 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000184 xfree(blob);
185 exit(0);
186}
187
Ben Lindstrombba81212001-06-25 05:01:22 +0000188static void
Damien Miller874d77b2000-10-14 16:23:11 +1100189buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
190{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000191 u_int bignum_bits = buffer_get_int(b);
192 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000193
Damien Miller874d77b2000-10-14 16:23:11 +1100194 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000195 fatal("buffer_get_bignum_bits: input buffer too small: "
196 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100197 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100198 buffer_consume(b, bytes);
199}
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000202do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100203{
204 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100205 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100206 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000207 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000208 int magic, rlen, ktype, i1, i2, i3, i4;
209 u_int slen;
210 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100211
212 buffer_init(&b);
213 buffer_append(&b, blob, blen);
214
215 magic = buffer_get_int(&b);
216 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
217 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
218 buffer_free(&b);
219 return NULL;
220 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000221 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100222 type = buffer_get_string(&b, NULL);
223 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000224 i2 = buffer_get_int(&b);
225 i3 = buffer_get_int(&b);
226 i4 = buffer_get_int(&b);
227 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100228 if (strcmp(cipher, "none") != 0) {
229 error("unsupported cipher %s", cipher);
230 xfree(cipher);
231 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000232 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100233 return NULL;
234 }
235 xfree(cipher);
236
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000237 if (strstr(type, "dsa")) {
238 ktype = KEY_DSA;
239 } else if (strstr(type, "rsa")) {
240 ktype = KEY_RSA;
241 } else {
242 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100243 return NULL;
244 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000245 key = key_new_private(ktype);
246 xfree(type);
247
248 switch (key->type) {
249 case KEY_DSA:
250 buffer_get_bignum_bits(&b, key->dsa->p);
251 buffer_get_bignum_bits(&b, key->dsa->g);
252 buffer_get_bignum_bits(&b, key->dsa->q);
253 buffer_get_bignum_bits(&b, key->dsa->pub_key);
254 buffer_get_bignum_bits(&b, key->dsa->priv_key);
255 break;
256 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000257 e = buffer_get_char(&b);
258 debug("e %lx", e);
259 if (e < 30) {
260 e <<= 8;
261 e += buffer_get_char(&b);
262 debug("e %lx", e);
263 e <<= 8;
264 e += buffer_get_char(&b);
265 debug("e %lx", e);
266 }
267 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000268 buffer_free(&b);
269 key_free(key);
270 return NULL;
271 }
272 buffer_get_bignum_bits(&b, key->rsa->d);
273 buffer_get_bignum_bits(&b, key->rsa->n);
274 buffer_get_bignum_bits(&b, key->rsa->iqmp);
275 buffer_get_bignum_bits(&b, key->rsa->q);
276 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000277 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000278 break;
279 }
Damien Miller874d77b2000-10-14 16:23:11 +1100280 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000281 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000282 error("do_convert_private_ssh2_from_blob: "
283 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100284 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000285
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000286 /* try the key */
287 key_sign(key, &sig, &slen, data, sizeof(data));
288 key_verify(key, sig, slen, data, sizeof(data));
289 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100290 return key;
291}
292
Ben Lindstrombba81212001-06-25 05:01:22 +0000293static void
Damien Millereba71ba2000-04-29 23:57:08 +1000294do_convert_from_ssh2(struct passwd *pw)
295{
296 Key *k;
297 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000298 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000299 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000300 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000301 char encoded[8096];
302 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100303 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000304 FILE *fp;
305
306 if (!have_identity)
307 ask_filename(pw, "Enter file in which the key is");
308 if (stat(identity_file, &st) < 0) {
309 perror(identity_file);
310 exit(1);
311 }
312 fp = fopen(identity_file, "r");
313 if (fp == NULL) {
314 perror(identity_file);
315 exit(1);
316 }
317 encoded[0] = '\0';
318 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000319 if (!(p = strchr(line, '\n'))) {
320 fprintf(stderr, "input line too long.\n");
321 exit(1);
322 }
323 if (p > line && p[-1] == '\\')
324 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000325 if (strncmp(line, "----", 4) == 0 ||
326 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100327 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
328 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000329 if (strstr(line, " END ") != NULL) {
330 break;
331 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000332 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000333 continue;
334 }
Damien Miller30c3d422000-05-09 11:02:59 +1000335 if (escaped) {
336 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000337 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000338 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000339 }
340 *p = '\0';
341 strlcat(encoded, line, sizeof(encoded));
342 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000343 len = strlen(encoded);
344 if (((len % 4) == 3) &&
345 (encoded[len-1] == '=') &&
346 (encoded[len-2] == '=') &&
347 (encoded[len-3] == '='))
348 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100349 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000350 if (blen < 0) {
351 fprintf(stderr, "uudecode failed.\n");
352 exit(1);
353 }
Damien Miller874d77b2000-10-14 16:23:11 +1100354 k = private ?
355 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100356 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100357 if (k == NULL) {
358 fprintf(stderr, "decode blob failed.\n");
359 exit(1);
360 }
361 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000362 (k->type == KEY_DSA ?
363 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
364 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100365 key_write(k, stdout);
366 if (!ok) {
367 fprintf(stderr, "key write failed");
368 exit(1);
369 }
Damien Millereba71ba2000-04-29 23:57:08 +1000370 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100371 if (!private)
372 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000373 fclose(fp);
374 exit(0);
375}
376
Ben Lindstrombba81212001-06-25 05:01:22 +0000377static void
Damien Millereba71ba2000-04-29 23:57:08 +1000378do_print_public(struct passwd *pw)
379{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000380 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000381 struct stat st;
382
383 if (!have_identity)
384 ask_filename(pw, "Enter file in which the key is");
385 if (stat(identity_file, &st) < 0) {
386 perror(identity_file);
387 exit(1);
388 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000389 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000390 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000391 fprintf(stderr, "load failed\n");
392 exit(1);
393 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000394 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000395 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000396 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000397 fprintf(stdout, "\n");
398 exit(0);
399}
400
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000401#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000402static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000403do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000404{
Ben Lindstromcd392282001-07-04 03:44:03 +0000405 Key *prv = NULL;
406 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000407 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000408
409 if (!have_identity)
410 ask_filename(pw, "Enter file in which the key is");
411 if (stat(identity_file, &st) < 0) {
412 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000413 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000414 }
415 prv = load_identity(identity_file);
416 if (prv == NULL) {
417 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000418 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000419 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000420 ret = sc_put_key(prv, sc_reader_id);
421 key_free(prv);
422 if (ret < 0)
423 exit(1);
Damien Miller996acd22003-04-09 20:59:48 +1000424 logit("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000425 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000426}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000427
428static void
429do_download(struct passwd *pw, const char *sc_reader_id)
430{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000431 Key **keys = NULL;
432 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000433
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000434 keys = sc_get_keys(sc_reader_id, NULL);
435 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000436 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000437 for (i = 0; keys[i]; i++) {
438 key_write(keys[i], stdout);
439 key_free(keys[i]);
440 fprintf(stdout, "\n");
441 }
442 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000443 exit(0);
444}
Ben Lindstromffce1472001-08-06 21:57:31 +0000445#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000446
Ben Lindstrombba81212001-06-25 05:01:22 +0000447static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100448do_fingerprint(struct passwd *pw)
449{
Damien Miller98c7ad62000-03-09 21:27:49 +1100450 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000451 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000452 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000453 int i, skip = 0, num = 1, invalid = 1;
454 enum fp_rep rep;
455 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100456 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100457
Ben Lindstromd0fca422001-03-26 13:44:06 +0000458 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
459 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000460
Damien Miller95def091999-11-25 00:26:21 +1100461 if (!have_identity)
462 ask_filename(pw, "Enter file in which the key is");
463 if (stat(identity_file, &st) < 0) {
464 perror(identity_file);
465 exit(1);
466 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000467 public = key_load_public(identity_file, &comment);
468 if (public != NULL) {
469 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000470 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471 key_free(public);
472 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000473 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100474 exit(0);
475 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000476 if (comment)
477 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100478
479 f = fopen(identity_file, "r");
480 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100481 while (fgets(line, sizeof(line), f)) {
482 i = strlen(line) - 1;
483 if (line[i] != '\n') {
484 error("line %d too long: %.40s...", num, line);
485 skip = 1;
486 continue;
Damien Miller95def091999-11-25 00:26:21 +1100487 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100488 num++;
489 if (skip) {
490 skip = 0;
491 continue;
492 }
493 line[i] = '\0';
494
495 /* Skip leading whitespace, empty and comment lines. */
496 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
497 ;
498 if (!*cp || *cp == '\n' || *cp == '#')
499 continue ;
500 i = strtol(cp, &ep, 10);
501 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
502 int quoted = 0;
503 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000504 for (; *cp && (quoted || (*cp != ' ' &&
505 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100506 if (*cp == '\\' && cp[1] == '"')
507 cp++; /* Skip both */
508 else if (*cp == '"')
509 quoted = !quoted;
510 }
511 if (!*cp)
512 continue;
513 *cp++ = '\0';
514 }
515 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000516 public = key_new(KEY_RSA1);
517 if (key_read(public, &cp) != 1) {
518 cp = ep;
519 key_free(public);
520 public = key_new(KEY_UNSPEC);
521 if (key_read(public, &cp) != 1) {
522 key_free(public);
523 continue;
524 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100525 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000526 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000527 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000528 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000529 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000530 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000531 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000532 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100533 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100534 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100535 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100536 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100537 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100538 exit(1);
539 }
Damien Miller95def091999-11-25 00:26:21 +1100540 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100541}
542
Damien Miller95def091999-11-25 00:26:21 +1100543/*
544 * Perform changing a passphrase. The argument is the passwd structure
545 * for the current user.
546 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000547static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100548do_change_passphrase(struct passwd *pw)
549{
Damien Miller95def091999-11-25 00:26:21 +1100550 char *comment;
551 char *old_passphrase, *passphrase1, *passphrase2;
552 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000553 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100554
Damien Miller95def091999-11-25 00:26:21 +1100555 if (!have_identity)
556 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100557 if (stat(identity_file, &st) < 0) {
558 perror(identity_file);
559 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560 }
Damien Miller95def091999-11-25 00:26:21 +1100561 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000562 private = key_load_private(identity_file, "", &comment);
563 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100564 if (identity_passphrase)
565 old_passphrase = xstrdup(identity_passphrase);
566 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000567 old_passphrase =
568 read_passphrase("Enter old passphrase: ",
569 RP_ALLOW_STDIN);
570 private = key_load_private(identity_file, old_passphrase,
571 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000572 memset(old_passphrase, 0, strlen(old_passphrase));
573 xfree(old_passphrase);
574 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100575 printf("Bad passphrase.\n");
576 exit(1);
577 }
Damien Miller95def091999-11-25 00:26:21 +1100578 }
579 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580
Damien Miller95def091999-11-25 00:26:21 +1100581 /* Ask the new passphrase (twice). */
582 if (identity_new_passphrase) {
583 passphrase1 = xstrdup(identity_new_passphrase);
584 passphrase2 = NULL;
585 } else {
586 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000587 read_passphrase("Enter new passphrase (empty for no "
588 "passphrase): ", RP_ALLOW_STDIN);
589 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100590 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100591
592 /* Verify that they are the same. */
593 if (strcmp(passphrase1, passphrase2) != 0) {
594 memset(passphrase1, 0, strlen(passphrase1));
595 memset(passphrase2, 0, strlen(passphrase2));
596 xfree(passphrase1);
597 xfree(passphrase2);
598 printf("Pass phrases do not match. Try again.\n");
599 exit(1);
600 }
601 /* Destroy the other copy. */
602 memset(passphrase2, 0, strlen(passphrase2));
603 xfree(passphrase2);
604 }
605
606 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000607 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000608 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100609 memset(passphrase1, 0, strlen(passphrase1));
610 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000611 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100612 xfree(comment);
613 exit(1);
614 }
615 /* Destroy the passphrase and the copy of the key in memory. */
616 memset(passphrase1, 0, strlen(passphrase1));
617 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000618 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100619 xfree(comment);
620
621 printf("Your identification has been saved with the new passphrase.\n");
622 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623}
624
Damien Miller37876e92003-05-15 10:19:46 +1000625/*
626 * Print the SSHFP RR.
627 */
628static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000629do_print_resource_record(struct passwd *pw, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000630{
631 Key *public;
632 char *comment = NULL;
633 struct stat st;
634
635 if (!have_identity)
636 ask_filename(pw, "Enter file in which the key is");
637 if (stat(identity_file, &st) < 0) {
638 perror(identity_file);
639 exit(1);
640 }
641 public = key_load_public(identity_file, &comment);
642 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000643 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000644 key_free(public);
645 xfree(comment);
646 exit(0);
647 }
648 if (comment)
649 xfree(comment);
650
651 printf("failed to read v2 public key from %s.\n", identity_file);
652 exit(1);
653}
Damien Miller37876e92003-05-15 10:19:46 +1000654
Damien Miller95def091999-11-25 00:26:21 +1100655/*
656 * Change the comment of a private key file.
657 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000658static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659do_change_comment(struct passwd *pw)
660{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000661 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000662 Key *private;
663 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100664 struct stat st;
665 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000666 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
Damien Miller95def091999-11-25 00:26:21 +1100668 if (!have_identity)
669 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100670 if (stat(identity_file, &st) < 0) {
671 perror(identity_file);
672 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000674 private = key_load_private(identity_file, "", &comment);
675 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100676 if (identity_passphrase)
677 passphrase = xstrdup(identity_passphrase);
678 else if (identity_new_passphrase)
679 passphrase = xstrdup(identity_new_passphrase);
680 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000681 passphrase = read_passphrase("Enter passphrase: ",
682 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100683 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000684 private = key_load_private(identity_file, passphrase, &comment);
685 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100686 memset(passphrase, 0, strlen(passphrase));
687 xfree(passphrase);
688 printf("Bad passphrase.\n");
689 exit(1);
690 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000691 } else {
692 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100693 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000694 if (private->type != KEY_RSA1) {
695 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
696 key_free(private);
697 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100698 }
Damien Miller95def091999-11-25 00:26:21 +1100699 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700
Damien Miller95def091999-11-25 00:26:21 +1100701 if (identity_comment) {
702 strlcpy(new_comment, identity_comment, sizeof(new_comment));
703 } else {
704 printf("Enter new comment: ");
705 fflush(stdout);
706 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
707 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000708 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100709 exit(1);
710 }
Damien Miller95def091999-11-25 00:26:21 +1100711 if (strchr(new_comment, '\n'))
712 *strchr(new_comment, '\n') = 0;
713 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714
Damien Miller95def091999-11-25 00:26:21 +1100715 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000716 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000717 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100718 memset(passphrase, 0, strlen(passphrase));
719 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000720 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100721 xfree(comment);
722 exit(1);
723 }
Damien Miller95def091999-11-25 00:26:21 +1100724 memset(passphrase, 0, strlen(passphrase));
725 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000726 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000727 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728
Damien Miller95def091999-11-25 00:26:21 +1100729 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000730 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
731 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100732 printf("Could not save your public key in %s\n", identity_file);
733 exit(1);
734 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000735 f = fdopen(fd, "w");
736 if (f == NULL) {
737 printf("fdopen %s failed", identity_file);
738 exit(1);
739 }
Damien Millereba71ba2000-04-29 23:57:08 +1000740 if (!key_write(public, f))
741 fprintf(stderr, "write key failed");
742 key_free(public);
743 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100744 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745
Damien Miller95def091999-11-25 00:26:21 +1100746 xfree(comment);
747
748 printf("The comment in your key file has been changed.\n");
749 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750}
751
Ben Lindstrombba81212001-06-25 05:01:22 +0000752static void
Damien Miller431f66b1999-11-21 18:31:57 +1100753usage(void)
754{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000755 fprintf(stderr, "Usage: %s [options]\n", __progname);
756 fprintf(stderr, "Options:\n");
757 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
758 fprintf(stderr, " -c Change comment in private and public key files.\n");
759 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
760 fprintf(stderr, " -f filename Filename of the key file.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000761 fprintf(stderr, " -g Use generic DNS resource record format.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000762 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
763 fprintf(stderr, " -l Show fingerprint of key file.\n");
764 fprintf(stderr, " -p Change passphrase of private key file.\n");
765 fprintf(stderr, " -q Quiet.\n");
766 fprintf(stderr, " -y Read private key file and print public key.\n");
767 fprintf(stderr, " -t type Specify type of key to create.\n");
768 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
769 fprintf(stderr, " -C comment Provide new comment.\n");
770 fprintf(stderr, " -N phrase Provide new passphrase.\n");
771 fprintf(stderr, " -P phrase Provide old passphrase.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000772 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000773#ifdef SMARTCARD
774 fprintf(stderr, " -D reader Download public key from smartcard.\n");
775 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
776#endif /* SMARTCARD */
777
Darren Tucker019cefe2003-08-02 22:40:07 +1000778 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli\n");
779 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli\n");
780
Damien Miller95def091999-11-25 00:26:21 +1100781 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100782}
783
Damien Miller95def091999-11-25 00:26:21 +1100784/*
785 * Main program for key management.
786 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787int
788main(int ac, char **av)
789{
Damien Millera41c8b12002-01-22 23:05:08 +1100790 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Tim Rice2e0e38e2003-09-08 16:11:33 -0700791 char out_file[MAXPATHLEN], *reader_id = NULL;
Damien Miller37876e92003-05-15 10:19:46 +1000792 char *resource_record_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000793 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100794 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100795 struct stat st;
Darren Tucker019cefe2003-08-02 22:40:07 +1000796 int opt, type, fd, download = 0, memory = 0;
797 int generator_wanted = 0, trials = 100;
798 int do_gen_candidates = 0, do_screen_candidates = 0;
Darren Tucker06930c72003-12-31 11:34:51 +1100799 int log_level = SYSLOG_LEVEL_INFO;
Darren Tucker019cefe2003-08-02 22:40:07 +1000800 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,
Darren Tucker06930c72003-12-31 11:34:51 +1100826 "degiqpclBRvxXyb: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;
Darren Tucker06930c72003-12-31 11:34:51 +1100894 case 'v':
895 if (log_level == SYSLOG_LEVEL_INFO)
896 log_level = SYSLOG_LEVEL_DEBUG1;
897 else {
Darren Tuckerfc959702004-07-17 16:12:08 +1000898 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +1100899 log_level < SYSLOG_LEVEL_DEBUG3)
900 log_level++;
901 }
902 break;
Damien Miller37876e92003-05-15 10:19:46 +1000903 case 'r':
904 resource_record_hostname = optarg;
905 break;
Darren Tucker019cefe2003-08-02 22:40:07 +1000906 case 'W':
907 generator_wanted = atoi(optarg);
908 if (generator_wanted < 1)
909 fatal("Desired generator has bad value.");
910 break;
911 case 'a':
912 trials = atoi(optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +1000913 break;
914 case 'M':
915 memory = atoi(optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +1000916 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 }
Darren Tucker06930c72003-12-31 11:34:51 +1100935
936 /* reinit */
937 log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
938
Damien Miller95def091999-11-25 00:26:21 +1100939 if (optind < ac) {
940 printf("Too many arguments.\n");
941 usage();
942 }
943 if (change_passphrase && change_comment) {
944 printf("Can only have one of -p and -c.\n");
945 usage();
946 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000947 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100948 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100949 if (change_passphrase)
950 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100951 if (change_comment)
952 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +0000953 if (convert_to_ssh2)
954 do_convert_to_ssh2(pw);
955 if (convert_from_ssh2)
956 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000957 if (print_public)
958 do_print_public(pw);
Damien Miller37876e92003-05-15 10:19:46 +1000959 if (resource_record_hostname != NULL) {
Damien Miller37876e92003-05-15 10:19:46 +1000960 do_print_resource_record(pw, resource_record_hostname);
Damien Miller37876e92003-05-15 10:19:46 +1000961 }
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000962 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000963#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000964 if (download)
965 do_download(pw, reader_id);
966 else
967 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000968#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000969 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000970#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000971 }
Damien Miller95def091999-11-25 00:26:21 +1100972
Darren Tucker019cefe2003-08-02 22:40:07 +1000973 if (do_gen_candidates) {
974 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +1100975
Darren Tucker019cefe2003-08-02 22:40:07 +1000976 if (out == NULL) {
977 error("Couldn't open modulus candidate file \"%s\": %s",
978 out_file, strerror(errno));
979 return (1);
980 }
981 if (gen_candidates(out, memory, bits, start) != 0)
982 fatal("modulus candidate generation failed\n");
983
984 return (0);
985 }
986
987 if (do_screen_candidates) {
988 FILE *in;
989 FILE *out = fopen(out_file, "w");
990
991 if (have_identity && strcmp(identity_file, "-") != 0) {
992 if ((in = fopen(identity_file, "r")) == NULL) {
993 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +1100994 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +1000995 strerror(errno));
996 }
997 } else
998 in = stdin;
999
1000 if (out == NULL) {
1001 fatal("Couldn't open moduli file \"%s\": %s",
1002 out_file, strerror(errno));
1003 }
1004 if (prime_test(in, out, trials, generator_wanted) != 0)
1005 fatal("modulus screening failed\n");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001006 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001007 }
1008
Damien Miller95def091999-11-25 00:26:21 +11001009 arc4random_stir();
1010
Damien Miller154dda72002-01-22 23:08:16 +11001011 if (key_type_name == NULL) {
1012 printf("You must specify a key type (-t).\n");
1013 usage();
1014 }
Damien Millere39cacc2000-11-29 12:18:44 +11001015 type = key_type_from_name(key_type_name);
1016 if (type == KEY_UNSPEC) {
1017 fprintf(stderr, "unknown key type %s\n", key_type_name);
1018 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001019 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001020 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +11001021 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001022 private = key_generate(type, bits);
1023 if (private == NULL) {
1024 fprintf(stderr, "key_generate failed");
1025 exit(1);
1026 }
1027 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001028
1029 if (!have_identity)
1030 ask_filename(pw, "Enter file in which to save the key");
1031
1032 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001033 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001034 if (strstr(identity_file, dotsshdir) != NULL &&
1035 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001036 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001037 error("Could not create directory '%s'.", dotsshdir);
1038 else if (!quiet)
1039 printf("Created directory '%s'.\n", dotsshdir);
1040 }
1041 /* If the file already exists, ask the user to confirm. */
1042 if (stat(identity_file, &st) >= 0) {
1043 char yesno[3];
1044 printf("%s already exists.\n", identity_file);
1045 printf("Overwrite (y/n)? ");
1046 fflush(stdout);
1047 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1048 exit(1);
1049 if (yesno[0] != 'y' && yesno[0] != 'Y')
1050 exit(1);
1051 }
1052 /* Ask for a passphrase (twice). */
1053 if (identity_passphrase)
1054 passphrase1 = xstrdup(identity_passphrase);
1055 else if (identity_new_passphrase)
1056 passphrase1 = xstrdup(identity_new_passphrase);
1057 else {
1058passphrase_again:
1059 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001060 read_passphrase("Enter passphrase (empty for no "
1061 "passphrase): ", RP_ALLOW_STDIN);
1062 passphrase2 = read_passphrase("Enter same passphrase again: ",
1063 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001064 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001065 /*
1066 * The passphrases do not match. Clear them and
1067 * retry.
1068 */
Damien Miller95def091999-11-25 00:26:21 +11001069 memset(passphrase1, 0, strlen(passphrase1));
1070 memset(passphrase2, 0, strlen(passphrase2));
1071 xfree(passphrase1);
1072 xfree(passphrase2);
1073 printf("Passphrases do not match. Try again.\n");
1074 goto passphrase_again;
1075 }
1076 /* Clear the other copy of the passphrase. */
1077 memset(passphrase2, 0, strlen(passphrase2));
1078 xfree(passphrase2);
1079 }
1080
Damien Miller95def091999-11-25 00:26:21 +11001081 if (identity_comment) {
1082 strlcpy(comment, identity_comment, sizeof(comment));
1083 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001084 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001085 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1086 }
1087
1088 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001089 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001090 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001091 memset(passphrase1, 0, strlen(passphrase1));
1092 xfree(passphrase1);
1093 exit(1);
1094 }
1095 /* Clear the passphrase. */
1096 memset(passphrase1, 0, strlen(passphrase1));
1097 xfree(passphrase1);
1098
1099 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001100 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001101 arc4random_stir();
1102
1103 if (!quiet)
1104 printf("Your identification has been saved in %s.\n", identity_file);
1105
Damien Miller95def091999-11-25 00:26:21 +11001106 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001107 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1108 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001109 printf("Could not save your public key in %s\n", identity_file);
1110 exit(1);
1111 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001112 f = fdopen(fd, "w");
1113 if (f == NULL) {
1114 printf("fdopen %s failed", identity_file);
1115 exit(1);
1116 }
Damien Millereba71ba2000-04-29 23:57:08 +10001117 if (!key_write(public, f))
1118 fprintf(stderr, "write key failed");
1119 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001120 fclose(f);
1121
1122 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001123 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001124 printf("Your public key has been saved in %s.\n",
1125 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001126 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001127 printf("%s %s\n", fp, comment);
1128 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001129 }
Damien Millereba71ba2000-04-29 23:57:08 +10001130
1131 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001132 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133}