blob: 5b7bc400a327cc695139c87be759d9bac3a1723b [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 Tuckerc0815c92003-09-22 21:05:50 +100015RCSID("$OpenBSD: ssh-keygen.c,v 1.109 2003/09/18 13:02:21 miod 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#ifdef DNS
36#include "dns.h"
37#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000038
Damien Millereba71ba2000-04-29 23:57:08 +100039/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040int bits = 1024;
41
Damien Miller5428f641999-11-25 11:54:57 +110042/*
43 * Flag indicating that we just want to change the passphrase. This can be
44 * set on the command line.
45 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046int change_passphrase = 0;
47
Damien Miller5428f641999-11-25 11:54:57 +110048/*
49 * Flag indicating that we just want to change the comment. This can be set
50 * on the command line.
51 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052int change_comment = 0;
53
54int quiet = 0;
55
Damien Miller10f6f6b1999-11-17 17:29:08 +110056/* Flag indicating that we just want to see the key fingerprint */
57int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000058int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110059
Damien Miller431f66b1999-11-21 18:31:57 +110060/* The identity file name, given on the command line or entered by the user. */
61char identity_file[1024];
62int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
64/* This is set to the passphrase if given on the command line. */
65char *identity_passphrase = NULL;
66
67/* This is set to the new passphrase if given on the command line. */
68char *identity_new_passphrase = NULL;
69
70/* This is set to the new comment if given on the command line. */
71char *identity_comment = NULL;
72
Damien Millereba71ba2000-04-29 23:57:08 +100073/* Dump public key file in format used by real and the original SSH 2 */
74int convert_to_ssh2 = 0;
75int convert_from_ssh2 = 0;
76int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +100077int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110078
Damien Millera41c8b12002-01-22 23:05:08 +110079char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100080
Damien Miller431f66b1999-11-21 18:31:57 +110081/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110082#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110083extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000084#else
85char *__progname;
86#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Millereba71ba2000-04-29 23:57:08 +100088char hostname[MAXHOSTNAMELEN];
89
Ben Lindstrombba81212001-06-25 05:01:22 +000090static void
Damien Miller431f66b1999-11-21 18:31:57 +110091ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092{
Damien Miller95def091999-11-25 00:26:21 +110093 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110094 char *name = NULL;
95
Damien Miller993dd552002-02-19 15:22:47 +110096 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +000097 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller993dd552002-02-19 15:22:47 +110098 else
99 switch (key_type_from_name(key_type_name)) {
100 case KEY_RSA1:
101 name = _PATH_SSH_CLIENT_IDENTITY;
102 break;
103 case KEY_DSA:
104 name = _PATH_SSH_CLIENT_ID_DSA;
105 break;
106 case KEY_RSA:
107 name = _PATH_SSH_CLIENT_ID_RSA;
108 break;
109 default:
110 fprintf(stderr, "bad key type");
111 exit(1);
112 break;
113 }
114
Damien Millere39cacc2000-11-29 12:18:44 +1100115 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000116 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100117 if (fgets(buf, sizeof(buf), stdin) == NULL)
118 exit(1);
119 if (strchr(buf, '\n'))
120 *strchr(buf, '\n') = 0;
121 if (strcmp(buf, "") != 0)
122 strlcpy(identity_file, buf, sizeof(identity_file));
123 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100124}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Ben Lindstrombba81212001-06-25 05:01:22 +0000126static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000127load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000128{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000129 char *pass;
130 Key *prv;
131
Ben Lindstroma3700052001-04-05 23:26:32 +0000132 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000133 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000134 if (identity_passphrase)
135 pass = xstrdup(identity_passphrase);
136 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000137 pass = read_passphrase("Enter passphrase: ",
138 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000139 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000140 memset(pass, 0, strlen(pass));
141 xfree(pass);
142 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000143 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000144}
145
Damien Miller874d77b2000-10-14 16:23:11 +1100146#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000147#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100148#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000149#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000150
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static void
Damien Millereba71ba2000-04-29 23:57:08 +1000152do_convert_to_ssh2(struct passwd *pw)
153{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000154 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000155 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000156 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000157 struct stat st;
158
159 if (!have_identity)
160 ask_filename(pw, "Enter file in which the key is");
161 if (stat(identity_file, &st) < 0) {
162 perror(identity_file);
163 exit(1);
164 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000165 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000166 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000167 fprintf(stderr, "load failed\n");
168 exit(1);
169 }
Damien Millereba71ba2000-04-29 23:57:08 +1000170 }
Damien Millerdb274722003-05-14 13:45:22 +1000171 if (k->type == KEY_RSA1) {
172 fprintf(stderr, "version 1 keys are not supported\n");
173 exit(1);
174 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000175 if (key_to_blob(k, &blob, &len) <= 0) {
176 fprintf(stderr, "key_to_blob failed\n");
177 exit(1);
178 }
Damien Miller874d77b2000-10-14 16:23:11 +1100179 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000180 fprintf(stdout,
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000181 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000182 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000183 pw->pw_name, hostname);
184 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100185 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000186 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000187 xfree(blob);
188 exit(0);
189}
190
Ben Lindstrombba81212001-06-25 05:01:22 +0000191static void
Damien Miller874d77b2000-10-14 16:23:11 +1100192buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
193{
Darren Tuckerc0815c92003-09-22 21:05:50 +1000194 u_int bits = buffer_get_int(b);
195 u_int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000196
Damien Miller874d77b2000-10-14 16:23:11 +1100197 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000198 fatal("buffer_get_bignum_bits: input buffer too small: "
199 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100200 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100201 buffer_consume(b, bytes);
202}
203
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000205do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100206{
207 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100208 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100209 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000210 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000211 int magic, rlen, ktype, i1, i2, i3, i4;
212 u_int slen;
213 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100214
215 buffer_init(&b);
216 buffer_append(&b, blob, blen);
217
218 magic = buffer_get_int(&b);
219 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
220 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
221 buffer_free(&b);
222 return NULL;
223 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000224 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100225 type = buffer_get_string(&b, NULL);
226 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000227 i2 = buffer_get_int(&b);
228 i3 = buffer_get_int(&b);
229 i4 = buffer_get_int(&b);
230 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100231 if (strcmp(cipher, "none") != 0) {
232 error("unsupported cipher %s", cipher);
233 xfree(cipher);
234 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000235 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100236 return NULL;
237 }
238 xfree(cipher);
239
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000240 if (strstr(type, "dsa")) {
241 ktype = KEY_DSA;
242 } else if (strstr(type, "rsa")) {
243 ktype = KEY_RSA;
244 } else {
245 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100246 return NULL;
247 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000248 key = key_new_private(ktype);
249 xfree(type);
250
251 switch (key->type) {
252 case KEY_DSA:
253 buffer_get_bignum_bits(&b, key->dsa->p);
254 buffer_get_bignum_bits(&b, key->dsa->g);
255 buffer_get_bignum_bits(&b, key->dsa->q);
256 buffer_get_bignum_bits(&b, key->dsa->pub_key);
257 buffer_get_bignum_bits(&b, key->dsa->priv_key);
258 break;
259 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000260 e = buffer_get_char(&b);
261 debug("e %lx", e);
262 if (e < 30) {
263 e <<= 8;
264 e += buffer_get_char(&b);
265 debug("e %lx", e);
266 e <<= 8;
267 e += buffer_get_char(&b);
268 debug("e %lx", e);
269 }
270 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000271 buffer_free(&b);
272 key_free(key);
273 return NULL;
274 }
275 buffer_get_bignum_bits(&b, key->rsa->d);
276 buffer_get_bignum_bits(&b, key->rsa->n);
277 buffer_get_bignum_bits(&b, key->rsa->iqmp);
278 buffer_get_bignum_bits(&b, key->rsa->q);
279 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000280 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000281 break;
282 }
Damien Miller874d77b2000-10-14 16:23:11 +1100283 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000284 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000285 error("do_convert_private_ssh2_from_blob: "
286 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100287 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000288
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000289 /* try the key */
290 key_sign(key, &sig, &slen, data, sizeof(data));
291 key_verify(key, sig, slen, data, sizeof(data));
292 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100293 return key;
294}
295
Ben Lindstrombba81212001-06-25 05:01:22 +0000296static void
Damien Millereba71ba2000-04-29 23:57:08 +1000297do_convert_from_ssh2(struct passwd *pw)
298{
299 Key *k;
300 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000301 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000302 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000303 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000304 char encoded[8096];
305 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100306 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000307 FILE *fp;
308
309 if (!have_identity)
310 ask_filename(pw, "Enter file in which the key is");
311 if (stat(identity_file, &st) < 0) {
312 perror(identity_file);
313 exit(1);
314 }
315 fp = fopen(identity_file, "r");
316 if (fp == NULL) {
317 perror(identity_file);
318 exit(1);
319 }
320 encoded[0] = '\0';
321 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000322 if (!(p = strchr(line, '\n'))) {
323 fprintf(stderr, "input line too long.\n");
324 exit(1);
325 }
326 if (p > line && p[-1] == '\\')
327 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000328 if (strncmp(line, "----", 4) == 0 ||
329 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100330 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
331 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000332 if (strstr(line, " END ") != NULL) {
333 break;
334 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000335 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000336 continue;
337 }
Damien Miller30c3d422000-05-09 11:02:59 +1000338 if (escaped) {
339 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000340 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000341 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000342 }
343 *p = '\0';
344 strlcat(encoded, line, sizeof(encoded));
345 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000346 len = strlen(encoded);
347 if (((len % 4) == 3) &&
348 (encoded[len-1] == '=') &&
349 (encoded[len-2] == '=') &&
350 (encoded[len-3] == '='))
351 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100352 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000353 if (blen < 0) {
354 fprintf(stderr, "uudecode failed.\n");
355 exit(1);
356 }
Damien Miller874d77b2000-10-14 16:23:11 +1100357 k = private ?
358 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100359 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100360 if (k == NULL) {
361 fprintf(stderr, "decode blob failed.\n");
362 exit(1);
363 }
364 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000365 (k->type == KEY_DSA ?
366 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
367 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100368 key_write(k, stdout);
369 if (!ok) {
370 fprintf(stderr, "key write failed");
371 exit(1);
372 }
Damien Millereba71ba2000-04-29 23:57:08 +1000373 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100374 if (!private)
375 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000376 fclose(fp);
377 exit(0);
378}
379
Ben Lindstrombba81212001-06-25 05:01:22 +0000380static void
Damien Millereba71ba2000-04-29 23:57:08 +1000381do_print_public(struct passwd *pw)
382{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000383 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000384 struct stat st;
385
386 if (!have_identity)
387 ask_filename(pw, "Enter file in which the key is");
388 if (stat(identity_file, &st) < 0) {
389 perror(identity_file);
390 exit(1);
391 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000392 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000393 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000394 fprintf(stderr, "load failed\n");
395 exit(1);
396 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000397 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000398 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000399 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000400 fprintf(stdout, "\n");
401 exit(0);
402}
403
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000404#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000405static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000406do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000407{
Ben Lindstromcd392282001-07-04 03:44:03 +0000408 Key *prv = NULL;
409 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000410 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000411
412 if (!have_identity)
413 ask_filename(pw, "Enter file in which the key is");
414 if (stat(identity_file, &st) < 0) {
415 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000416 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000417 }
418 prv = load_identity(identity_file);
419 if (prv == NULL) {
420 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000421 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000422 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000423 ret = sc_put_key(prv, sc_reader_id);
424 key_free(prv);
425 if (ret < 0)
426 exit(1);
Damien Miller996acd22003-04-09 20:59:48 +1000427 logit("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000428 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000429}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000430
431static void
432do_download(struct passwd *pw, const char *sc_reader_id)
433{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000434 Key **keys = NULL;
435 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000436
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000437 keys = sc_get_keys(sc_reader_id, NULL);
438 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000439 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000440 for (i = 0; keys[i]; i++) {
441 key_write(keys[i], stdout);
442 key_free(keys[i]);
443 fprintf(stdout, "\n");
444 }
445 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000446 exit(0);
447}
Ben Lindstromffce1472001-08-06 21:57:31 +0000448#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000449
Ben Lindstrombba81212001-06-25 05:01:22 +0000450static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100451do_fingerprint(struct passwd *pw)
452{
Damien Miller98c7ad62000-03-09 21:27:49 +1100453 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000454 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000455 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000456 int i, skip = 0, num = 1, invalid = 1;
457 enum fp_rep rep;
458 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100459 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100460
Ben Lindstromd0fca422001-03-26 13:44:06 +0000461 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
462 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000463
Damien Miller95def091999-11-25 00:26:21 +1100464 if (!have_identity)
465 ask_filename(pw, "Enter file in which the key is");
466 if (stat(identity_file, &st) < 0) {
467 perror(identity_file);
468 exit(1);
469 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000470 public = key_load_public(identity_file, &comment);
471 if (public != NULL) {
472 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000473 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474 key_free(public);
475 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000476 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100477 exit(0);
478 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000479 if (comment)
480 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100481
482 f = fopen(identity_file, "r");
483 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100484 while (fgets(line, sizeof(line), f)) {
485 i = strlen(line) - 1;
486 if (line[i] != '\n') {
487 error("line %d too long: %.40s...", num, line);
488 skip = 1;
489 continue;
Damien Miller95def091999-11-25 00:26:21 +1100490 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100491 num++;
492 if (skip) {
493 skip = 0;
494 continue;
495 }
496 line[i] = '\0';
497
498 /* Skip leading whitespace, empty and comment lines. */
499 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
500 ;
501 if (!*cp || *cp == '\n' || *cp == '#')
502 continue ;
503 i = strtol(cp, &ep, 10);
504 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
505 int quoted = 0;
506 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000507 for (; *cp && (quoted || (*cp != ' ' &&
508 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100509 if (*cp == '\\' && cp[1] == '"')
510 cp++; /* Skip both */
511 else if (*cp == '"')
512 quoted = !quoted;
513 }
514 if (!*cp)
515 continue;
516 *cp++ = '\0';
517 }
518 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000519 public = key_new(KEY_RSA1);
520 if (key_read(public, &cp) != 1) {
521 cp = ep;
522 key_free(public);
523 public = key_new(KEY_UNSPEC);
524 if (key_read(public, &cp) != 1) {
525 key_free(public);
526 continue;
527 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100528 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000529 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000530 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000531 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000532 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000533 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000534 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000535 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100536 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100537 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100538 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100539 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100540 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100541 exit(1);
542 }
Damien Miller95def091999-11-25 00:26:21 +1100543 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100544}
545
Damien Miller95def091999-11-25 00:26:21 +1100546/*
547 * Perform changing a passphrase. The argument is the passwd structure
548 * for the current user.
549 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000550static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100551do_change_passphrase(struct passwd *pw)
552{
Damien Miller95def091999-11-25 00:26:21 +1100553 char *comment;
554 char *old_passphrase, *passphrase1, *passphrase2;
555 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000556 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100557
Damien Miller95def091999-11-25 00:26:21 +1100558 if (!have_identity)
559 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100560 if (stat(identity_file, &st) < 0) {
561 perror(identity_file);
562 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563 }
Damien Miller95def091999-11-25 00:26:21 +1100564 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000565 private = key_load_private(identity_file, "", &comment);
566 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100567 if (identity_passphrase)
568 old_passphrase = xstrdup(identity_passphrase);
569 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000570 old_passphrase =
571 read_passphrase("Enter old passphrase: ",
572 RP_ALLOW_STDIN);
573 private = key_load_private(identity_file, old_passphrase,
574 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000575 memset(old_passphrase, 0, strlen(old_passphrase));
576 xfree(old_passphrase);
577 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100578 printf("Bad passphrase.\n");
579 exit(1);
580 }
Damien Miller95def091999-11-25 00:26:21 +1100581 }
582 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583
Damien Miller95def091999-11-25 00:26:21 +1100584 /* Ask the new passphrase (twice). */
585 if (identity_new_passphrase) {
586 passphrase1 = xstrdup(identity_new_passphrase);
587 passphrase2 = NULL;
588 } else {
589 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000590 read_passphrase("Enter new passphrase (empty for no "
591 "passphrase): ", RP_ALLOW_STDIN);
592 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100593 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100594
595 /* Verify that they are the same. */
596 if (strcmp(passphrase1, passphrase2) != 0) {
597 memset(passphrase1, 0, strlen(passphrase1));
598 memset(passphrase2, 0, strlen(passphrase2));
599 xfree(passphrase1);
600 xfree(passphrase2);
601 printf("Pass phrases do not match. Try again.\n");
602 exit(1);
603 }
604 /* Destroy the other copy. */
605 memset(passphrase2, 0, strlen(passphrase2));
606 xfree(passphrase2);
607 }
608
609 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000610 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000611 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100612 memset(passphrase1, 0, strlen(passphrase1));
613 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000614 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100615 xfree(comment);
616 exit(1);
617 }
618 /* Destroy the passphrase and the copy of the key in memory. */
619 memset(passphrase1, 0, strlen(passphrase1));
620 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000621 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100622 xfree(comment);
623
624 printf("Your identification has been saved with the new passphrase.\n");
625 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626}
627
Damien Miller37876e92003-05-15 10:19:46 +1000628#ifdef DNS
629/*
630 * Print the SSHFP RR.
631 */
632static void
633do_print_resource_record(struct passwd *pw, char *hostname)
634{
635 Key *public;
636 char *comment = NULL;
637 struct stat st;
638
639 if (!have_identity)
640 ask_filename(pw, "Enter file in which the key is");
641 if (stat(identity_file, &st) < 0) {
642 perror(identity_file);
643 exit(1);
644 }
645 public = key_load_public(identity_file, &comment);
646 if (public != NULL) {
647 export_dns_rr(hostname, public, stdout, print_generic);
648 key_free(public);
649 xfree(comment);
650 exit(0);
651 }
652 if (comment)
653 xfree(comment);
654
655 printf("failed to read v2 public key from %s.\n", identity_file);
656 exit(1);
657}
658#endif /* DNS */
659
Damien Miller95def091999-11-25 00:26:21 +1100660/*
661 * Change the comment of a private key file.
662 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000663static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664do_change_comment(struct passwd *pw)
665{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000666 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000667 Key *private;
668 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100669 struct stat st;
670 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000671 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672
Damien Miller95def091999-11-25 00:26:21 +1100673 if (!have_identity)
674 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100675 if (stat(identity_file, &st) < 0) {
676 perror(identity_file);
677 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000679 private = key_load_private(identity_file, "", &comment);
680 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100681 if (identity_passphrase)
682 passphrase = xstrdup(identity_passphrase);
683 else if (identity_new_passphrase)
684 passphrase = xstrdup(identity_new_passphrase);
685 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000686 passphrase = read_passphrase("Enter passphrase: ",
687 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100688 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000689 private = key_load_private(identity_file, passphrase, &comment);
690 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100691 memset(passphrase, 0, strlen(passphrase));
692 xfree(passphrase);
693 printf("Bad passphrase.\n");
694 exit(1);
695 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000696 } else {
697 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100698 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000699 if (private->type != KEY_RSA1) {
700 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
701 key_free(private);
702 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100703 }
Damien Miller95def091999-11-25 00:26:21 +1100704 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705
Damien Miller95def091999-11-25 00:26:21 +1100706 if (identity_comment) {
707 strlcpy(new_comment, identity_comment, sizeof(new_comment));
708 } else {
709 printf("Enter new comment: ");
710 fflush(stdout);
711 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
712 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000713 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100714 exit(1);
715 }
Damien Miller95def091999-11-25 00:26:21 +1100716 if (strchr(new_comment, '\n'))
717 *strchr(new_comment, '\n') = 0;
718 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
Damien Miller95def091999-11-25 00:26:21 +1100720 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000721 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000722 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100723 memset(passphrase, 0, strlen(passphrase));
724 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000725 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100726 xfree(comment);
727 exit(1);
728 }
Damien Miller95def091999-11-25 00:26:21 +1100729 memset(passphrase, 0, strlen(passphrase));
730 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000731 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000732 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733
Damien Miller95def091999-11-25 00:26:21 +1100734 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000735 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
736 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100737 printf("Could not save your public key in %s\n", identity_file);
738 exit(1);
739 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000740 f = fdopen(fd, "w");
741 if (f == NULL) {
742 printf("fdopen %s failed", identity_file);
743 exit(1);
744 }
Damien Millereba71ba2000-04-29 23:57:08 +1000745 if (!key_write(public, f))
746 fprintf(stderr, "write key failed");
747 key_free(public);
748 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100749 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750
Damien Miller95def091999-11-25 00:26:21 +1100751 xfree(comment);
752
753 printf("The comment in your key file has been changed.\n");
754 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755}
756
Ben Lindstrombba81212001-06-25 05:01:22 +0000757static void
Damien Miller431f66b1999-11-21 18:31:57 +1100758usage(void)
759{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000760 fprintf(stderr, "Usage: %s [options]\n", __progname);
761 fprintf(stderr, "Options:\n");
762 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
763 fprintf(stderr, " -c Change comment in private and public key files.\n");
764 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
765 fprintf(stderr, " -f filename Filename of the key file.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000766 fprintf(stderr, " -g Use generic DNS resource record format.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000767 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
768 fprintf(stderr, " -l Show fingerprint of key file.\n");
769 fprintf(stderr, " -p Change passphrase of private key file.\n");
770 fprintf(stderr, " -q Quiet.\n");
771 fprintf(stderr, " -y Read private key file and print public key.\n");
772 fprintf(stderr, " -t type Specify type of key to create.\n");
773 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
774 fprintf(stderr, " -C comment Provide new comment.\n");
775 fprintf(stderr, " -N phrase Provide new passphrase.\n");
776 fprintf(stderr, " -P phrase Provide old passphrase.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000777#ifdef DNS
778 fprintf(stderr, " -r hostname Print DNS resource record.\n");
779#endif /* DNS */
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000780#ifdef SMARTCARD
781 fprintf(stderr, " -D reader Download public key from smartcard.\n");
782 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
783#endif /* SMARTCARD */
784
Darren Tucker019cefe2003-08-02 22:40:07 +1000785 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli\n");
786 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli\n");
787
Damien Miller95def091999-11-25 00:26:21 +1100788 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100789}
790
Damien Miller95def091999-11-25 00:26:21 +1100791/*
792 * Main program for key management.
793 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794int
795main(int ac, char **av)
796{
Damien Millera41c8b12002-01-22 23:05:08 +1100797 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Tim Rice2e0e38e2003-09-08 16:11:33 -0700798 char out_file[MAXPATHLEN], *reader_id = NULL;
Damien Miller37876e92003-05-15 10:19:46 +1000799 char *resource_record_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000800 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100801 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100802 struct stat st;
Darren Tucker019cefe2003-08-02 22:40:07 +1000803 int opt, type, fd, download = 0, memory = 0;
804 int generator_wanted = 0, trials = 100;
805 int do_gen_candidates = 0, do_screen_candidates = 0;
806 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100807 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100808
Damien Miller95def091999-11-25 00:26:21 +1100809 extern int optind;
810 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811
Damien Miller59d3d5b2003-08-22 09:34:41 +1000812 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000813
Damien Millerd3a18572000-06-07 19:55:44 +1000814 SSLeay_add_all_algorithms();
Darren Tucker019cefe2003-08-02 22:40:07 +1000815 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
816
Kevin Steves3a881912002-07-20 19:05:40 +0000817 init_rng();
818 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +1000819
Damien Miller5428f641999-11-25 11:54:57 +1100820 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100821 pw = getpwuid(getuid());
822 if (!pw) {
823 printf("You don't exist, go away!\n");
824 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825 }
Damien Millereba71ba2000-04-29 23:57:08 +1000826 if (gethostname(hostname, sizeof(hostname)) < 0) {
827 perror("gethostname");
828 exit(1);
829 }
Damien Miller5428f641999-11-25 11:54:57 +1100830
Darren Tucker019cefe2003-08-02 22:40:07 +1000831 while ((opt = getopt(ac, av,
832 "degiqpclBRxXyb:f:t:U:D:P:N:C:r:g:T:G:M:S:a:W:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100833 switch (opt) {
834 case 'b':
835 bits = atoi(optarg);
836 if (bits < 512 || bits > 32768) {
837 printf("Bits has bad value.\n");
838 exit(1);
839 }
840 break;
Damien Miller95def091999-11-25 00:26:21 +1100841 case 'l':
842 print_fingerprint = 1;
843 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000844 case 'B':
845 print_bubblebabble = 1;
846 break;
Damien Miller95def091999-11-25 00:26:21 +1100847 case 'p':
848 change_passphrase = 1;
849 break;
Damien Miller95def091999-11-25 00:26:21 +1100850 case 'c':
851 change_comment = 1;
852 break;
Damien Miller95def091999-11-25 00:26:21 +1100853 case 'f':
854 strlcpy(identity_file, optarg, sizeof(identity_file));
855 have_identity = 1;
856 break;
Damien Miller37876e92003-05-15 10:19:46 +1000857 case 'g':
858 print_generic = 1;
859 break;
Damien Miller95def091999-11-25 00:26:21 +1100860 case 'P':
861 identity_passphrase = optarg;
862 break;
Damien Miller95def091999-11-25 00:26:21 +1100863 case 'N':
864 identity_new_passphrase = optarg;
865 break;
Damien Miller95def091999-11-25 00:26:21 +1100866 case 'C':
867 identity_comment = optarg;
868 break;
Damien Miller95def091999-11-25 00:26:21 +1100869 case 'q':
870 quiet = 1;
871 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000872 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100873 /* unused */
874 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000875 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000876 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000877 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000878 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000879 convert_to_ssh2 = 1;
880 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000881 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000882 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000883 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000884 convert_from_ssh2 = 1;
885 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000886 case 'y':
887 print_public = 1;
888 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000889 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100890 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000891 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100892 case 't':
893 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100894 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000895 case 'D':
896 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000897 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000898 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000899 break;
Damien Miller37876e92003-05-15 10:19:46 +1000900 case 'r':
901 resource_record_hostname = optarg;
902 break;
Darren Tucker019cefe2003-08-02 22:40:07 +1000903 case 'W':
904 generator_wanted = atoi(optarg);
905 if (generator_wanted < 1)
906 fatal("Desired generator has bad value.");
907 break;
908 case 'a':
909 trials = atoi(optarg);
910 if (trials < TRIAL_MINIMUM) {
911 fatal("Minimum primality trials is %d",
912 TRIAL_MINIMUM);
913 }
914 break;
915 case 'M':
916 memory = atoi(optarg);
917 if (memory != 0 &&
918 (memory < LARGE_MINIMUM || memory > LARGE_MAXIMUM)) {
919 fatal("Invalid memory amount (min %ld, max %ld)",
920 LARGE_MINIMUM, LARGE_MAXIMUM);
921 }
922 break;
923 case 'G':
924 do_gen_candidates = 1;
925 strlcpy(out_file, optarg, sizeof(out_file));
926 break;
927 case 'T':
928 do_screen_candidates = 1;
929 strlcpy(out_file, optarg, sizeof(out_file));
930 break;
931 case 'S':
932 /* XXX - also compare length against bits */
933 if (BN_hex2bn(&start, optarg) == 0)
934 fatal("Invalid start point.");
935 break;
Damien Miller95def091999-11-25 00:26:21 +1100936 case '?':
937 default:
938 usage();
939 }
940 }
941 if (optind < ac) {
942 printf("Too many arguments.\n");
943 usage();
944 }
945 if (change_passphrase && change_comment) {
946 printf("Can only have one of -p and -c.\n");
947 usage();
948 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000949 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100950 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100951 if (change_passphrase)
952 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100953 if (change_comment)
954 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +0000955 if (convert_to_ssh2)
956 do_convert_to_ssh2(pw);
957 if (convert_from_ssh2)
958 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000959 if (print_public)
960 do_print_public(pw);
Damien Miller37876e92003-05-15 10:19:46 +1000961 if (resource_record_hostname != NULL) {
962#ifdef DNS
963 do_print_resource_record(pw, resource_record_hostname);
964#else /* DNS */
965 fatal("no DNS support.");
966#endif /* DNS */
967 }
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000968 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000969#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000970 if (download)
971 do_download(pw, reader_id);
972 else
973 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000974#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000975 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000976#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000977 }
Damien Miller95def091999-11-25 00:26:21 +1100978
Darren Tucker019cefe2003-08-02 22:40:07 +1000979 if (do_gen_candidates) {
980 FILE *out = fopen(out_file, "w");
981
982 if (out == NULL) {
983 error("Couldn't open modulus candidate file \"%s\": %s",
984 out_file, strerror(errno));
985 return (1);
986 }
987 if (gen_candidates(out, memory, bits, start) != 0)
988 fatal("modulus candidate generation failed\n");
989
990 return (0);
991 }
992
993 if (do_screen_candidates) {
994 FILE *in;
995 FILE *out = fopen(out_file, "w");
996
997 if (have_identity && strcmp(identity_file, "-") != 0) {
998 if ((in = fopen(identity_file, "r")) == NULL) {
999 fatal("Couldn't open modulus candidate "
1000 "file \"%s\": %s", identity_file,
1001 strerror(errno));
1002 }
1003 } else
1004 in = stdin;
1005
1006 if (out == NULL) {
1007 fatal("Couldn't open moduli file \"%s\": %s",
1008 out_file, strerror(errno));
1009 }
1010 if (prime_test(in, out, trials, generator_wanted) != 0)
1011 fatal("modulus screening failed\n");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001012 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001013 }
1014
Damien Miller95def091999-11-25 00:26:21 +11001015 arc4random_stir();
1016
Damien Miller154dda72002-01-22 23:08:16 +11001017 if (key_type_name == NULL) {
1018 printf("You must specify a key type (-t).\n");
1019 usage();
1020 }
Damien Millere39cacc2000-11-29 12:18:44 +11001021 type = key_type_from_name(key_type_name);
1022 if (type == KEY_UNSPEC) {
1023 fprintf(stderr, "unknown key type %s\n", key_type_name);
1024 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001025 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001026 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +11001027 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001028 private = key_generate(type, bits);
1029 if (private == NULL) {
1030 fprintf(stderr, "key_generate failed");
1031 exit(1);
1032 }
1033 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001034
1035 if (!have_identity)
1036 ask_filename(pw, "Enter file in which to save the key");
1037
1038 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001039 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001040 if (strstr(identity_file, dotsshdir) != NULL &&
1041 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001042 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001043 error("Could not create directory '%s'.", dotsshdir);
1044 else if (!quiet)
1045 printf("Created directory '%s'.\n", dotsshdir);
1046 }
1047 /* If the file already exists, ask the user to confirm. */
1048 if (stat(identity_file, &st) >= 0) {
1049 char yesno[3];
1050 printf("%s already exists.\n", identity_file);
1051 printf("Overwrite (y/n)? ");
1052 fflush(stdout);
1053 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1054 exit(1);
1055 if (yesno[0] != 'y' && yesno[0] != 'Y')
1056 exit(1);
1057 }
1058 /* Ask for a passphrase (twice). */
1059 if (identity_passphrase)
1060 passphrase1 = xstrdup(identity_passphrase);
1061 else if (identity_new_passphrase)
1062 passphrase1 = xstrdup(identity_new_passphrase);
1063 else {
1064passphrase_again:
1065 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001066 read_passphrase("Enter passphrase (empty for no "
1067 "passphrase): ", RP_ALLOW_STDIN);
1068 passphrase2 = read_passphrase("Enter same passphrase again: ",
1069 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001070 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001071 /*
1072 * The passphrases do not match. Clear them and
1073 * retry.
1074 */
Damien Miller95def091999-11-25 00:26:21 +11001075 memset(passphrase1, 0, strlen(passphrase1));
1076 memset(passphrase2, 0, strlen(passphrase2));
1077 xfree(passphrase1);
1078 xfree(passphrase2);
1079 printf("Passphrases do not match. Try again.\n");
1080 goto passphrase_again;
1081 }
1082 /* Clear the other copy of the passphrase. */
1083 memset(passphrase2, 0, strlen(passphrase2));
1084 xfree(passphrase2);
1085 }
1086
Damien Miller95def091999-11-25 00:26:21 +11001087 if (identity_comment) {
1088 strlcpy(comment, identity_comment, sizeof(comment));
1089 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001090 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001091 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1092 }
1093
1094 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001095 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001096 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001097 memset(passphrase1, 0, strlen(passphrase1));
1098 xfree(passphrase1);
1099 exit(1);
1100 }
1101 /* Clear the passphrase. */
1102 memset(passphrase1, 0, strlen(passphrase1));
1103 xfree(passphrase1);
1104
1105 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001106 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001107 arc4random_stir();
1108
1109 if (!quiet)
1110 printf("Your identification has been saved in %s.\n", identity_file);
1111
Damien Miller95def091999-11-25 00:26:21 +11001112 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001113 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1114 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001115 printf("Could not save your public key in %s\n", identity_file);
1116 exit(1);
1117 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001118 f = fdopen(fd, "w");
1119 if (f == NULL) {
1120 printf("fdopen %s failed", identity_file);
1121 exit(1);
1122 }
Damien Millereba71ba2000-04-29 23:57:08 +10001123 if (!key_write(public, f))
1124 fprintf(stderr, "write key failed");
1125 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001126 fclose(f);
1127
1128 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001129 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001130 printf("Your public key has been saved in %s.\n",
1131 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001132 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001133 printf("%s %s\n", fp, comment);
1134 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001135 }
Damien Millereba71ba2000-04-29 23:57:08 +10001136
1137 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001138 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139}