blob: 7ed62a30c5d81aea2f6910d3d73ffc8d81edf2c4 [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 Tucker7cfeecf2005-01-20 10:56:31 +110015RCSID("$OpenBSD: ssh-keygen.c,v 1.118 2004/12/23 17:38:07 markus 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 {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100242 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000243 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100244 return NULL;
245 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000246 key = key_new_private(ktype);
247 xfree(type);
248
249 switch (key->type) {
250 case KEY_DSA:
251 buffer_get_bignum_bits(&b, key->dsa->p);
252 buffer_get_bignum_bits(&b, key->dsa->g);
253 buffer_get_bignum_bits(&b, key->dsa->q);
254 buffer_get_bignum_bits(&b, key->dsa->pub_key);
255 buffer_get_bignum_bits(&b, key->dsa->priv_key);
256 break;
257 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000258 e = buffer_get_char(&b);
259 debug("e %lx", e);
260 if (e < 30) {
261 e <<= 8;
262 e += buffer_get_char(&b);
263 debug("e %lx", e);
264 e <<= 8;
265 e += buffer_get_char(&b);
266 debug("e %lx", e);
267 }
268 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000269 buffer_free(&b);
270 key_free(key);
271 return NULL;
272 }
273 buffer_get_bignum_bits(&b, key->rsa->d);
274 buffer_get_bignum_bits(&b, key->rsa->n);
275 buffer_get_bignum_bits(&b, key->rsa->iqmp);
276 buffer_get_bignum_bits(&b, key->rsa->q);
277 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000278 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000279 break;
280 }
Damien Miller874d77b2000-10-14 16:23:11 +1100281 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000282 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000283 error("do_convert_private_ssh2_from_blob: "
284 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100285 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000286
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000287 /* try the key */
288 key_sign(key, &sig, &slen, data, sizeof(data));
289 key_verify(key, sig, slen, data, sizeof(data));
290 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100291 return key;
292}
293
Ben Lindstrombba81212001-06-25 05:01:22 +0000294static void
Damien Millereba71ba2000-04-29 23:57:08 +1000295do_convert_from_ssh2(struct passwd *pw)
296{
297 Key *k;
298 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000299 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000300 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000301 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000302 char encoded[8096];
303 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100304 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000305 FILE *fp;
306
307 if (!have_identity)
308 ask_filename(pw, "Enter file in which the key is");
309 if (stat(identity_file, &st) < 0) {
310 perror(identity_file);
311 exit(1);
312 }
313 fp = fopen(identity_file, "r");
314 if (fp == NULL) {
315 perror(identity_file);
316 exit(1);
317 }
318 encoded[0] = '\0';
319 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000320 if (!(p = strchr(line, '\n'))) {
321 fprintf(stderr, "input line too long.\n");
322 exit(1);
323 }
324 if (p > line && p[-1] == '\\')
325 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000326 if (strncmp(line, "----", 4) == 0 ||
327 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100328 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
329 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000330 if (strstr(line, " END ") != NULL) {
331 break;
332 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000333 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000334 continue;
335 }
Damien Miller30c3d422000-05-09 11:02:59 +1000336 if (escaped) {
337 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000338 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000339 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000340 }
341 *p = '\0';
342 strlcat(encoded, line, sizeof(encoded));
343 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000344 len = strlen(encoded);
345 if (((len % 4) == 3) &&
346 (encoded[len-1] == '=') &&
347 (encoded[len-2] == '=') &&
348 (encoded[len-3] == '='))
349 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100350 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000351 if (blen < 0) {
352 fprintf(stderr, "uudecode failed.\n");
353 exit(1);
354 }
Damien Miller874d77b2000-10-14 16:23:11 +1100355 k = private ?
356 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100357 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100358 if (k == NULL) {
359 fprintf(stderr, "decode blob failed.\n");
360 exit(1);
361 }
362 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000363 (k->type == KEY_DSA ?
364 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
365 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100366 key_write(k, stdout);
367 if (!ok) {
368 fprintf(stderr, "key write failed");
369 exit(1);
370 }
Damien Millereba71ba2000-04-29 23:57:08 +1000371 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100372 if (!private)
373 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000374 fclose(fp);
375 exit(0);
376}
377
Ben Lindstrombba81212001-06-25 05:01:22 +0000378static void
Damien Millereba71ba2000-04-29 23:57:08 +1000379do_print_public(struct passwd *pw)
380{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000381 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000382 struct stat st;
383
384 if (!have_identity)
385 ask_filename(pw, "Enter file in which the key is");
386 if (stat(identity_file, &st) < 0) {
387 perror(identity_file);
388 exit(1);
389 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000390 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000391 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000392 fprintf(stderr, "load failed\n");
393 exit(1);
394 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000395 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000396 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000397 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000398 fprintf(stdout, "\n");
399 exit(0);
400}
401
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000402#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000403static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000404do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000405{
Ben Lindstromcd392282001-07-04 03:44:03 +0000406 Key *prv = NULL;
407 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000408 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000409
410 if (!have_identity)
411 ask_filename(pw, "Enter file in which the key is");
412 if (stat(identity_file, &st) < 0) {
413 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000414 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000415 }
416 prv = load_identity(identity_file);
417 if (prv == NULL) {
418 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000419 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000420 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000421 ret = sc_put_key(prv, sc_reader_id);
422 key_free(prv);
423 if (ret < 0)
424 exit(1);
Damien Miller996acd22003-04-09 20:59:48 +1000425 logit("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000426 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000427}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000428
429static void
430do_download(struct passwd *pw, const char *sc_reader_id)
431{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000432 Key **keys = NULL;
433 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000434
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000435 keys = sc_get_keys(sc_reader_id, NULL);
436 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000437 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000438 for (i = 0; keys[i]; i++) {
439 key_write(keys[i], stdout);
440 key_free(keys[i]);
441 fprintf(stdout, "\n");
442 }
443 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000444 exit(0);
445}
Ben Lindstromffce1472001-08-06 21:57:31 +0000446#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000447
Ben Lindstrombba81212001-06-25 05:01:22 +0000448static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100449do_fingerprint(struct passwd *pw)
450{
Damien Miller98c7ad62000-03-09 21:27:49 +1100451 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000452 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000453 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000454 int i, skip = 0, num = 1, invalid = 1;
455 enum fp_rep rep;
456 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100457 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100458
Ben Lindstromd0fca422001-03-26 13:44:06 +0000459 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
460 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000461
Damien Miller95def091999-11-25 00:26:21 +1100462 if (!have_identity)
463 ask_filename(pw, "Enter file in which the key is");
464 if (stat(identity_file, &st) < 0) {
465 perror(identity_file);
466 exit(1);
467 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000468 public = key_load_public(identity_file, &comment);
469 if (public != NULL) {
470 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000471 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100472 key_free(public);
473 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000474 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100475 exit(0);
476 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000477 if (comment)
478 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100479
480 f = fopen(identity_file, "r");
481 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100482 while (fgets(line, sizeof(line), f)) {
483 i = strlen(line) - 1;
484 if (line[i] != '\n') {
485 error("line %d too long: %.40s...", num, line);
486 skip = 1;
487 continue;
Damien Miller95def091999-11-25 00:26:21 +1100488 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100489 num++;
490 if (skip) {
491 skip = 0;
492 continue;
493 }
494 line[i] = '\0';
495
496 /* Skip leading whitespace, empty and comment lines. */
497 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
498 ;
499 if (!*cp || *cp == '\n' || *cp == '#')
500 continue ;
501 i = strtol(cp, &ep, 10);
502 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
503 int quoted = 0;
504 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000505 for (; *cp && (quoted || (*cp != ' ' &&
506 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100507 if (*cp == '\\' && cp[1] == '"')
508 cp++; /* Skip both */
509 else if (*cp == '"')
510 quoted = !quoted;
511 }
512 if (!*cp)
513 continue;
514 *cp++ = '\0';
515 }
516 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000517 public = key_new(KEY_RSA1);
518 if (key_read(public, &cp) != 1) {
519 cp = ep;
520 key_free(public);
521 public = key_new(KEY_UNSPEC);
522 if (key_read(public, &cp) != 1) {
523 key_free(public);
524 continue;
525 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100526 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000527 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000528 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000529 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000530 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000531 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000532 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000533 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100534 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100535 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100536 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100537 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100538 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100539 exit(1);
540 }
Damien Miller95def091999-11-25 00:26:21 +1100541 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100542}
543
Damien Miller95def091999-11-25 00:26:21 +1100544/*
545 * Perform changing a passphrase. The argument is the passwd structure
546 * for the current user.
547 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000548static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100549do_change_passphrase(struct passwd *pw)
550{
Damien Miller95def091999-11-25 00:26:21 +1100551 char *comment;
552 char *old_passphrase, *passphrase1, *passphrase2;
553 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000554 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100555
Damien Miller95def091999-11-25 00:26:21 +1100556 if (!have_identity)
557 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100558 if (stat(identity_file, &st) < 0) {
559 perror(identity_file);
560 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561 }
Damien Miller95def091999-11-25 00:26:21 +1100562 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000563 private = key_load_private(identity_file, "", &comment);
564 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100565 if (identity_passphrase)
566 old_passphrase = xstrdup(identity_passphrase);
567 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000568 old_passphrase =
569 read_passphrase("Enter old passphrase: ",
570 RP_ALLOW_STDIN);
571 private = key_load_private(identity_file, old_passphrase,
572 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000573 memset(old_passphrase, 0, strlen(old_passphrase));
574 xfree(old_passphrase);
575 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100576 printf("Bad passphrase.\n");
577 exit(1);
578 }
Damien Miller95def091999-11-25 00:26:21 +1100579 }
580 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581
Damien Miller95def091999-11-25 00:26:21 +1100582 /* Ask the new passphrase (twice). */
583 if (identity_new_passphrase) {
584 passphrase1 = xstrdup(identity_new_passphrase);
585 passphrase2 = NULL;
586 } else {
587 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000588 read_passphrase("Enter new passphrase (empty for no "
589 "passphrase): ", RP_ALLOW_STDIN);
590 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100591 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100592
593 /* Verify that they are the same. */
594 if (strcmp(passphrase1, passphrase2) != 0) {
595 memset(passphrase1, 0, strlen(passphrase1));
596 memset(passphrase2, 0, strlen(passphrase2));
597 xfree(passphrase1);
598 xfree(passphrase2);
599 printf("Pass phrases do not match. Try again.\n");
600 exit(1);
601 }
602 /* Destroy the other copy. */
603 memset(passphrase2, 0, strlen(passphrase2));
604 xfree(passphrase2);
605 }
606
607 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000608 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000609 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100610 memset(passphrase1, 0, strlen(passphrase1));
611 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000612 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100613 xfree(comment);
614 exit(1);
615 }
616 /* Destroy the passphrase and the copy of the key in memory. */
617 memset(passphrase1, 0, strlen(passphrase1));
618 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000619 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100620 xfree(comment);
621
622 printf("Your identification has been saved with the new passphrase.\n");
623 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624}
625
Damien Miller37876e92003-05-15 10:19:46 +1000626/*
627 * Print the SSHFP RR.
628 */
629static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000630do_print_resource_record(struct passwd *pw, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000631{
632 Key *public;
633 char *comment = NULL;
634 struct stat st;
635
636 if (!have_identity)
637 ask_filename(pw, "Enter file in which the key is");
638 if (stat(identity_file, &st) < 0) {
639 perror(identity_file);
640 exit(1);
641 }
642 public = key_load_public(identity_file, &comment);
643 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000644 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000645 key_free(public);
646 xfree(comment);
647 exit(0);
648 }
649 if (comment)
650 xfree(comment);
651
652 printf("failed to read v2 public key from %s.\n", identity_file);
653 exit(1);
654}
Damien Miller37876e92003-05-15 10:19:46 +1000655
Damien Miller95def091999-11-25 00:26:21 +1100656/*
657 * Change the comment of a private key file.
658 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000659static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660do_change_comment(struct passwd *pw)
661{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000662 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000663 Key *private;
664 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100665 struct stat st;
666 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000667 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668
Damien Miller95def091999-11-25 00:26:21 +1100669 if (!have_identity)
670 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100671 if (stat(identity_file, &st) < 0) {
672 perror(identity_file);
673 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000675 private = key_load_private(identity_file, "", &comment);
676 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100677 if (identity_passphrase)
678 passphrase = xstrdup(identity_passphrase);
679 else if (identity_new_passphrase)
680 passphrase = xstrdup(identity_new_passphrase);
681 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000682 passphrase = read_passphrase("Enter passphrase: ",
683 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100684 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000685 private = key_load_private(identity_file, passphrase, &comment);
686 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100687 memset(passphrase, 0, strlen(passphrase));
688 xfree(passphrase);
689 printf("Bad passphrase.\n");
690 exit(1);
691 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000692 } else {
693 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100694 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000695 if (private->type != KEY_RSA1) {
696 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
697 key_free(private);
698 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100699 }
Damien Miller95def091999-11-25 00:26:21 +1100700 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701
Damien Miller95def091999-11-25 00:26:21 +1100702 if (identity_comment) {
703 strlcpy(new_comment, identity_comment, sizeof(new_comment));
704 } else {
705 printf("Enter new comment: ");
706 fflush(stdout);
707 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
708 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000709 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100710 exit(1);
711 }
Damien Miller95def091999-11-25 00:26:21 +1100712 if (strchr(new_comment, '\n'))
713 *strchr(new_comment, '\n') = 0;
714 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715
Damien Miller95def091999-11-25 00:26:21 +1100716 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000717 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000718 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100719 memset(passphrase, 0, strlen(passphrase));
720 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000721 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100722 xfree(comment);
723 exit(1);
724 }
Damien Miller95def091999-11-25 00:26:21 +1100725 memset(passphrase, 0, strlen(passphrase));
726 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000727 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000728 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
Damien Miller95def091999-11-25 00:26:21 +1100730 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000731 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
732 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100733 printf("Could not save your public key in %s\n", identity_file);
734 exit(1);
735 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000736 f = fdopen(fd, "w");
737 if (f == NULL) {
738 printf("fdopen %s failed", identity_file);
739 exit(1);
740 }
Damien Millereba71ba2000-04-29 23:57:08 +1000741 if (!key_write(public, f))
742 fprintf(stderr, "write key failed");
743 key_free(public);
744 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100745 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746
Damien Miller95def091999-11-25 00:26:21 +1100747 xfree(comment);
748
749 printf("The comment in your key file has been changed.\n");
750 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751}
752
Ben Lindstrombba81212001-06-25 05:01:22 +0000753static void
Damien Miller431f66b1999-11-21 18:31:57 +1100754usage(void)
755{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000756 fprintf(stderr, "Usage: %s [options]\n", __progname);
757 fprintf(stderr, "Options:\n");
758 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
759 fprintf(stderr, " -c Change comment in private and public key files.\n");
760 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
761 fprintf(stderr, " -f filename Filename of the key file.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000762 fprintf(stderr, " -g Use generic DNS resource record format.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000763 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
764 fprintf(stderr, " -l Show fingerprint of key file.\n");
765 fprintf(stderr, " -p Change passphrase of private key file.\n");
766 fprintf(stderr, " -q Quiet.\n");
767 fprintf(stderr, " -y Read private key file and print public key.\n");
768 fprintf(stderr, " -t type Specify type of key to create.\n");
769 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
770 fprintf(stderr, " -C comment Provide new comment.\n");
771 fprintf(stderr, " -N phrase Provide new passphrase.\n");
772 fprintf(stderr, " -P phrase Provide old passphrase.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000773 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000774#ifdef SMARTCARD
775 fprintf(stderr, " -D reader Download public key from smartcard.\n");
776 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
777#endif /* SMARTCARD */
778
Darren Tucker019cefe2003-08-02 22:40:07 +1000779 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli\n");
780 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli\n");
781
Damien Miller95def091999-11-25 00:26:21 +1100782 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100783}
784
Damien Miller95def091999-11-25 00:26:21 +1100785/*
786 * Main program for key management.
787 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788int
789main(int ac, char **av)
790{
Damien Millera41c8b12002-01-22 23:05:08 +1100791 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Tim Rice2e0e38e2003-09-08 16:11:33 -0700792 char out_file[MAXPATHLEN], *reader_id = NULL;
Damien Miller37876e92003-05-15 10:19:46 +1000793 char *resource_record_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000794 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100795 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100796 struct stat st;
Darren Tucker019cefe2003-08-02 22:40:07 +1000797 int opt, type, fd, download = 0, memory = 0;
798 int generator_wanted = 0, trials = 100;
799 int do_gen_candidates = 0, do_screen_candidates = 0;
Darren Tucker06930c72003-12-31 11:34:51 +1100800 int log_level = SYSLOG_LEVEL_INFO;
Darren Tucker019cefe2003-08-02 22:40:07 +1000801 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100802 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100803
Damien Miller95def091999-11-25 00:26:21 +1100804 extern int optind;
805 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806
Damien Miller59d3d5b2003-08-22 09:34:41 +1000807 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000808
Damien Millerd3a18572000-06-07 19:55:44 +1000809 SSLeay_add_all_algorithms();
Darren Tucker019cefe2003-08-02 22:40:07 +1000810 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
811
Kevin Steves3a881912002-07-20 19:05:40 +0000812 init_rng();
813 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +1000814
Damien Miller5428f641999-11-25 11:54:57 +1100815 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100816 pw = getpwuid(getuid());
817 if (!pw) {
818 printf("You don't exist, go away!\n");
819 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820 }
Damien Millereba71ba2000-04-29 23:57:08 +1000821 if (gethostname(hostname, sizeof(hostname)) < 0) {
822 perror("gethostname");
823 exit(1);
824 }
Damien Miller5428f641999-11-25 11:54:57 +1100825
Darren Tucker019cefe2003-08-02 22:40:07 +1000826 while ((opt = getopt(ac, av,
Darren Tucker06930c72003-12-31 11:34:51 +1100827 "degiqpclBRvxXyb:f:t:U:D:P:N:C:r:g:T:G:M:S:a:W:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100828 switch (opt) {
829 case 'b':
830 bits = atoi(optarg);
831 if (bits < 512 || bits > 32768) {
832 printf("Bits has bad value.\n");
833 exit(1);
834 }
835 break;
Damien Miller95def091999-11-25 00:26:21 +1100836 case 'l':
837 print_fingerprint = 1;
838 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000839 case 'B':
840 print_bubblebabble = 1;
841 break;
Damien Miller95def091999-11-25 00:26:21 +1100842 case 'p':
843 change_passphrase = 1;
844 break;
Damien Miller95def091999-11-25 00:26:21 +1100845 case 'c':
846 change_comment = 1;
847 break;
Damien Miller95def091999-11-25 00:26:21 +1100848 case 'f':
849 strlcpy(identity_file, optarg, sizeof(identity_file));
850 have_identity = 1;
851 break;
Damien Miller37876e92003-05-15 10:19:46 +1000852 case 'g':
853 print_generic = 1;
854 break;
Damien Miller95def091999-11-25 00:26:21 +1100855 case 'P':
856 identity_passphrase = optarg;
857 break;
Damien Miller95def091999-11-25 00:26:21 +1100858 case 'N':
859 identity_new_passphrase = optarg;
860 break;
Damien Miller95def091999-11-25 00:26:21 +1100861 case 'C':
862 identity_comment = optarg;
863 break;
Damien Miller95def091999-11-25 00:26:21 +1100864 case 'q':
865 quiet = 1;
866 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000867 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100868 /* unused */
869 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000870 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000871 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000872 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000873 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000874 convert_to_ssh2 = 1;
875 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000876 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000877 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000878 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000879 convert_from_ssh2 = 1;
880 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000881 case 'y':
882 print_public = 1;
883 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000884 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100885 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000886 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100887 case 't':
888 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100889 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000890 case 'D':
891 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000892 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000893 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000894 break;
Darren Tucker06930c72003-12-31 11:34:51 +1100895 case 'v':
896 if (log_level == SYSLOG_LEVEL_INFO)
897 log_level = SYSLOG_LEVEL_DEBUG1;
898 else {
Darren Tuckerfc959702004-07-17 16:12:08 +1000899 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +1100900 log_level < SYSLOG_LEVEL_DEBUG3)
901 log_level++;
902 }
903 break;
Damien Miller37876e92003-05-15 10:19:46 +1000904 case 'r':
905 resource_record_hostname = optarg;
906 break;
Darren Tucker019cefe2003-08-02 22:40:07 +1000907 case 'W':
908 generator_wanted = atoi(optarg);
909 if (generator_wanted < 1)
910 fatal("Desired generator has bad value.");
911 break;
912 case 'a':
913 trials = atoi(optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +1000914 break;
915 case 'M':
916 memory = atoi(optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +1000917 break;
918 case 'G':
919 do_gen_candidates = 1;
920 strlcpy(out_file, optarg, sizeof(out_file));
921 break;
922 case 'T':
923 do_screen_candidates = 1;
924 strlcpy(out_file, optarg, sizeof(out_file));
925 break;
926 case 'S':
927 /* XXX - also compare length against bits */
928 if (BN_hex2bn(&start, optarg) == 0)
929 fatal("Invalid start point.");
930 break;
Damien Miller95def091999-11-25 00:26:21 +1100931 case '?':
932 default:
933 usage();
934 }
935 }
Darren Tucker06930c72003-12-31 11:34:51 +1100936
937 /* reinit */
938 log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
939
Damien Miller95def091999-11-25 00:26:21 +1100940 if (optind < ac) {
941 printf("Too many arguments.\n");
942 usage();
943 }
944 if (change_passphrase && change_comment) {
945 printf("Can only have one of -p and -c.\n");
946 usage();
947 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000948 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100949 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100950 if (change_passphrase)
951 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100952 if (change_comment)
953 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +0000954 if (convert_to_ssh2)
955 do_convert_to_ssh2(pw);
956 if (convert_from_ssh2)
957 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000958 if (print_public)
959 do_print_public(pw);
Damien Miller37876e92003-05-15 10:19:46 +1000960 if (resource_record_hostname != NULL) {
Damien Miller37876e92003-05-15 10:19:46 +1000961 do_print_resource_record(pw, resource_record_hostname);
Damien Miller37876e92003-05-15 10:19:46 +1000962 }
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000963 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000964#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000965 if (download)
966 do_download(pw, reader_id);
967 else
968 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000969#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000970 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000971#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000972 }
Damien Miller95def091999-11-25 00:26:21 +1100973
Darren Tucker019cefe2003-08-02 22:40:07 +1000974 if (do_gen_candidates) {
975 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +1100976
Darren Tucker019cefe2003-08-02 22:40:07 +1000977 if (out == NULL) {
978 error("Couldn't open modulus candidate file \"%s\": %s",
979 out_file, strerror(errno));
980 return (1);
981 }
982 if (gen_candidates(out, memory, bits, start) != 0)
983 fatal("modulus candidate generation failed\n");
984
985 return (0);
986 }
987
988 if (do_screen_candidates) {
989 FILE *in;
990 FILE *out = fopen(out_file, "w");
991
992 if (have_identity && strcmp(identity_file, "-") != 0) {
993 if ((in = fopen(identity_file, "r")) == NULL) {
994 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +1100995 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +1000996 strerror(errno));
997 }
998 } else
999 in = stdin;
1000
1001 if (out == NULL) {
1002 fatal("Couldn't open moduli file \"%s\": %s",
1003 out_file, strerror(errno));
1004 }
1005 if (prime_test(in, out, trials, generator_wanted) != 0)
1006 fatal("modulus screening failed\n");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001007 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001008 }
1009
Damien Miller95def091999-11-25 00:26:21 +11001010 arc4random_stir();
1011
Damien Miller154dda72002-01-22 23:08:16 +11001012 if (key_type_name == NULL) {
1013 printf("You must specify a key type (-t).\n");
1014 usage();
1015 }
Damien Millere39cacc2000-11-29 12:18:44 +11001016 type = key_type_from_name(key_type_name);
1017 if (type == KEY_UNSPEC) {
1018 fprintf(stderr, "unknown key type %s\n", key_type_name);
1019 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001020 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001021 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +11001022 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001023 private = key_generate(type, bits);
1024 if (private == NULL) {
1025 fprintf(stderr, "key_generate failed");
1026 exit(1);
1027 }
1028 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001029
1030 if (!have_identity)
1031 ask_filename(pw, "Enter file in which to save the key");
1032
1033 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001034 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001035 if (strstr(identity_file, dotsshdir) != NULL &&
1036 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001037 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001038 error("Could not create directory '%s'.", dotsshdir);
1039 else if (!quiet)
1040 printf("Created directory '%s'.\n", dotsshdir);
1041 }
1042 /* If the file already exists, ask the user to confirm. */
1043 if (stat(identity_file, &st) >= 0) {
1044 char yesno[3];
1045 printf("%s already exists.\n", identity_file);
1046 printf("Overwrite (y/n)? ");
1047 fflush(stdout);
1048 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1049 exit(1);
1050 if (yesno[0] != 'y' && yesno[0] != 'Y')
1051 exit(1);
1052 }
1053 /* Ask for a passphrase (twice). */
1054 if (identity_passphrase)
1055 passphrase1 = xstrdup(identity_passphrase);
1056 else if (identity_new_passphrase)
1057 passphrase1 = xstrdup(identity_new_passphrase);
1058 else {
1059passphrase_again:
1060 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001061 read_passphrase("Enter passphrase (empty for no "
1062 "passphrase): ", RP_ALLOW_STDIN);
1063 passphrase2 = read_passphrase("Enter same passphrase again: ",
1064 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001065 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001066 /*
1067 * The passphrases do not match. Clear them and
1068 * retry.
1069 */
Damien Miller95def091999-11-25 00:26:21 +11001070 memset(passphrase1, 0, strlen(passphrase1));
1071 memset(passphrase2, 0, strlen(passphrase2));
1072 xfree(passphrase1);
1073 xfree(passphrase2);
1074 printf("Passphrases do not match. Try again.\n");
1075 goto passphrase_again;
1076 }
1077 /* Clear the other copy of the passphrase. */
1078 memset(passphrase2, 0, strlen(passphrase2));
1079 xfree(passphrase2);
1080 }
1081
Damien Miller95def091999-11-25 00:26:21 +11001082 if (identity_comment) {
1083 strlcpy(comment, identity_comment, sizeof(comment));
1084 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001085 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001086 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1087 }
1088
1089 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001090 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001091 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001092 memset(passphrase1, 0, strlen(passphrase1));
1093 xfree(passphrase1);
1094 exit(1);
1095 }
1096 /* Clear the passphrase. */
1097 memset(passphrase1, 0, strlen(passphrase1));
1098 xfree(passphrase1);
1099
1100 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001101 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001102 arc4random_stir();
1103
1104 if (!quiet)
1105 printf("Your identification has been saved in %s.\n", identity_file);
1106
Damien Miller95def091999-11-25 00:26:21 +11001107 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001108 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1109 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001110 printf("Could not save your public key in %s\n", identity_file);
1111 exit(1);
1112 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001113 f = fdopen(fd, "w");
1114 if (f == NULL) {
1115 printf("fdopen %s failed", identity_file);
1116 exit(1);
1117 }
Damien Millereba71ba2000-04-29 23:57:08 +10001118 if (!key_write(public, f))
1119 fprintf(stderr, "write key failed");
1120 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001121 fclose(f);
1122
1123 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001124 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001125 printf("Your public key has been saved in %s.\n",
1126 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001127 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001128 printf("%s %s\n", fp, comment);
1129 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001130 }
Damien Millereba71ba2000-04-29 23:57:08 +10001131
1132 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001133 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001134}