blob: 1d08c7cece96a30a48f53ebab90e916dd32376ed [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11006 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerdb274722003-05-14 13:45:22 +100015RCSID("$OpenBSD: ssh-keygen.c,v 1.104 2003/05/11 16:56:48 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"
29#include "readpass.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
Ben Lindstromcd392282001-07-04 03:44:03 +000034
Damien Millereba71ba2000-04-29 23:57:08 +100035/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036int bits = 1024;
37
Damien Miller5428f641999-11-25 11:54:57 +110038/*
39 * Flag indicating that we just want to change the passphrase. This can be
40 * set on the command line.
41 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042int change_passphrase = 0;
43
Damien Miller5428f641999-11-25 11:54:57 +110044/*
45 * Flag indicating that we just want to change the comment. This can be set
46 * on the command line.
47 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048int change_comment = 0;
49
50int quiet = 0;
51
Damien Miller10f6f6b1999-11-17 17:29:08 +110052/* Flag indicating that we just want to see the key fingerprint */
53int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000054int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110055
Damien Miller431f66b1999-11-21 18:31:57 +110056/* The identity file name, given on the command line or entered by the user. */
57char identity_file[1024];
58int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
60/* This is set to the passphrase if given on the command line. */
61char *identity_passphrase = NULL;
62
63/* This is set to the new passphrase if given on the command line. */
64char *identity_new_passphrase = NULL;
65
66/* This is set to the new comment if given on the command line. */
67char *identity_comment = NULL;
68
Damien Millereba71ba2000-04-29 23:57:08 +100069/* Dump public key file in format used by real and the original SSH 2 */
70int convert_to_ssh2 = 0;
71int convert_from_ssh2 = 0;
72int print_public = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110073
Damien Millera41c8b12002-01-22 23:05:08 +110074char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100075
Damien Miller431f66b1999-11-21 18:31:57 +110076/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110077#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110078extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000079#else
80char *__progname;
81#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Millereba71ba2000-04-29 23:57:08 +100083char hostname[MAXHOSTNAMELEN];
84
Ben Lindstrombba81212001-06-25 05:01:22 +000085static void
Damien Miller431f66b1999-11-21 18:31:57 +110086ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087{
Damien Miller95def091999-11-25 00:26:21 +110088 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110089 char *name = NULL;
90
Damien Miller993dd552002-02-19 15:22:47 +110091 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller993dd552002-02-19 15:22:47 +110093 else
94 switch (key_type_from_name(key_type_name)) {
95 case KEY_RSA1:
96 name = _PATH_SSH_CLIENT_IDENTITY;
97 break;
98 case KEY_DSA:
99 name = _PATH_SSH_CLIENT_ID_DSA;
100 break;
101 case KEY_RSA:
102 name = _PATH_SSH_CLIENT_ID_RSA;
103 break;
104 default:
105 fprintf(stderr, "bad key type");
106 exit(1);
107 break;
108 }
109
Damien Millere39cacc2000-11-29 12:18:44 +1100110 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000111 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100112 if (fgets(buf, sizeof(buf), stdin) == NULL)
113 exit(1);
114 if (strchr(buf, '\n'))
115 *strchr(buf, '\n') = 0;
116 if (strcmp(buf, "") != 0)
117 strlcpy(identity_file, buf, sizeof(identity_file));
118 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100119}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Ben Lindstrombba81212001-06-25 05:01:22 +0000121static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000122load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000123{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000124 char *pass;
125 Key *prv;
126
Ben Lindstroma3700052001-04-05 23:26:32 +0000127 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000128 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000129 if (identity_passphrase)
130 pass = xstrdup(identity_passphrase);
131 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000132 pass = read_passphrase("Enter passphrase: ",
133 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000134 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000135 memset(pass, 0, strlen(pass));
136 xfree(pass);
137 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000138 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000139}
140
Damien Miller874d77b2000-10-14 16:23:11 +1100141#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000142#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100143#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000144#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000145
Ben Lindstrombba81212001-06-25 05:01:22 +0000146static void
Damien Millereba71ba2000-04-29 23:57:08 +1000147do_convert_to_ssh2(struct passwd *pw)
148{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000149 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000150 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000151 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000152 struct stat st;
153
154 if (!have_identity)
155 ask_filename(pw, "Enter file in which the key is");
156 if (stat(identity_file, &st) < 0) {
157 perror(identity_file);
158 exit(1);
159 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000160 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000161 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000162 fprintf(stderr, "load failed\n");
163 exit(1);
164 }
Damien Millereba71ba2000-04-29 23:57:08 +1000165 }
Damien Millerdb274722003-05-14 13:45:22 +1000166 if (k->type == KEY_RSA1) {
167 fprintf(stderr, "version 1 keys are not supported\n");
168 exit(1);
169 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000170 if (key_to_blob(k, &blob, &len) <= 0) {
171 fprintf(stderr, "key_to_blob failed\n");
172 exit(1);
173 }
Damien Miller874d77b2000-10-14 16:23:11 +1100174 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000175 fprintf(stdout,
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000176 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000177 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000178 pw->pw_name, hostname);
179 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100180 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000181 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000182 xfree(blob);
183 exit(0);
184}
185
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static void
Damien Miller874d77b2000-10-14 16:23:11 +1100187buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
188{
189 int bits = buffer_get_int(b);
190 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000191
Damien Miller874d77b2000-10-14 16:23:11 +1100192 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000193 fatal("buffer_get_bignum_bits: input buffer too small: "
194 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100195 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100196 buffer_consume(b, bytes);
197}
198
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000200do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100201{
202 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100203 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100204 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000205 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000206 int magic, rlen, ktype, i1, i2, i3, i4;
207 u_int slen;
208 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100209
210 buffer_init(&b);
211 buffer_append(&b, blob, blen);
212
213 magic = buffer_get_int(&b);
214 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
215 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
216 buffer_free(&b);
217 return NULL;
218 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000219 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100220 type = buffer_get_string(&b, NULL);
221 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000222 i2 = buffer_get_int(&b);
223 i3 = buffer_get_int(&b);
224 i4 = buffer_get_int(&b);
225 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100226 if (strcmp(cipher, "none") != 0) {
227 error("unsupported cipher %s", cipher);
228 xfree(cipher);
229 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000230 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100231 return NULL;
232 }
233 xfree(cipher);
234
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000235 if (strstr(type, "dsa")) {
236 ktype = KEY_DSA;
237 } else if (strstr(type, "rsa")) {
238 ktype = KEY_RSA;
239 } else {
240 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100241 return NULL;
242 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000243 key = key_new_private(ktype);
244 xfree(type);
245
246 switch (key->type) {
247 case KEY_DSA:
248 buffer_get_bignum_bits(&b, key->dsa->p);
249 buffer_get_bignum_bits(&b, key->dsa->g);
250 buffer_get_bignum_bits(&b, key->dsa->q);
251 buffer_get_bignum_bits(&b, key->dsa->pub_key);
252 buffer_get_bignum_bits(&b, key->dsa->priv_key);
253 break;
254 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000255 e = buffer_get_char(&b);
256 debug("e %lx", e);
257 if (e < 30) {
258 e <<= 8;
259 e += buffer_get_char(&b);
260 debug("e %lx", e);
261 e <<= 8;
262 e += buffer_get_char(&b);
263 debug("e %lx", e);
264 }
265 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000266 buffer_free(&b);
267 key_free(key);
268 return NULL;
269 }
270 buffer_get_bignum_bits(&b, key->rsa->d);
271 buffer_get_bignum_bits(&b, key->rsa->n);
272 buffer_get_bignum_bits(&b, key->rsa->iqmp);
273 buffer_get_bignum_bits(&b, key->rsa->q);
274 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000275 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000276 break;
277 }
Damien Miller874d77b2000-10-14 16:23:11 +1100278 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000279 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000280 error("do_convert_private_ssh2_from_blob: "
281 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100282 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000283
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000284 /* try the key */
285 key_sign(key, &sig, &slen, data, sizeof(data));
286 key_verify(key, sig, slen, data, sizeof(data));
287 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100288 return key;
289}
290
Ben Lindstrombba81212001-06-25 05:01:22 +0000291static void
Damien Millereba71ba2000-04-29 23:57:08 +1000292do_convert_from_ssh2(struct passwd *pw)
293{
294 Key *k;
295 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000296 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000297 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000298 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000299 char encoded[8096];
300 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100301 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000302 FILE *fp;
303
304 if (!have_identity)
305 ask_filename(pw, "Enter file in which the key is");
306 if (stat(identity_file, &st) < 0) {
307 perror(identity_file);
308 exit(1);
309 }
310 fp = fopen(identity_file, "r");
311 if (fp == NULL) {
312 perror(identity_file);
313 exit(1);
314 }
315 encoded[0] = '\0';
316 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000317 if (!(p = strchr(line, '\n'))) {
318 fprintf(stderr, "input line too long.\n");
319 exit(1);
320 }
321 if (p > line && p[-1] == '\\')
322 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000323 if (strncmp(line, "----", 4) == 0 ||
324 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100325 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
326 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000327 if (strstr(line, " END ") != NULL) {
328 break;
329 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000330 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000331 continue;
332 }
Damien Miller30c3d422000-05-09 11:02:59 +1000333 if (escaped) {
334 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000335 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000336 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000337 }
338 *p = '\0';
339 strlcat(encoded, line, sizeof(encoded));
340 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000341 len = strlen(encoded);
342 if (((len % 4) == 3) &&
343 (encoded[len-1] == '=') &&
344 (encoded[len-2] == '=') &&
345 (encoded[len-3] == '='))
346 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100347 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000348 if (blen < 0) {
349 fprintf(stderr, "uudecode failed.\n");
350 exit(1);
351 }
Damien Miller874d77b2000-10-14 16:23:11 +1100352 k = private ?
353 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100354 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100355 if (k == NULL) {
356 fprintf(stderr, "decode blob failed.\n");
357 exit(1);
358 }
359 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000360 (k->type == KEY_DSA ?
361 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
362 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100363 key_write(k, stdout);
364 if (!ok) {
365 fprintf(stderr, "key write failed");
366 exit(1);
367 }
Damien Millereba71ba2000-04-29 23:57:08 +1000368 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100369 if (!private)
370 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000371 fclose(fp);
372 exit(0);
373}
374
Ben Lindstrombba81212001-06-25 05:01:22 +0000375static void
Damien Millereba71ba2000-04-29 23:57:08 +1000376do_print_public(struct passwd *pw)
377{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000378 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000379 struct stat st;
380
381 if (!have_identity)
382 ask_filename(pw, "Enter file in which the key is");
383 if (stat(identity_file, &st) < 0) {
384 perror(identity_file);
385 exit(1);
386 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000387 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000388 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000389 fprintf(stderr, "load failed\n");
390 exit(1);
391 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000392 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000393 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000394 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000395 fprintf(stdout, "\n");
396 exit(0);
397}
398
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000399#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000400static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000401do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000402{
Ben Lindstromcd392282001-07-04 03:44:03 +0000403 Key *prv = NULL;
404 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000405 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000406
407 if (!have_identity)
408 ask_filename(pw, "Enter file in which the key is");
409 if (stat(identity_file, &st) < 0) {
410 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000411 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000412 }
413 prv = load_identity(identity_file);
414 if (prv == NULL) {
415 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000416 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000417 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000418 ret = sc_put_key(prv, sc_reader_id);
419 key_free(prv);
420 if (ret < 0)
421 exit(1);
Damien Miller996acd22003-04-09 20:59:48 +1000422 logit("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000423 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000424}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000425
426static void
427do_download(struct passwd *pw, const char *sc_reader_id)
428{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000429 Key **keys = NULL;
430 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000431
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000432 keys = sc_get_keys(sc_reader_id, NULL);
433 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000434 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000435 for (i = 0; keys[i]; i++) {
436 key_write(keys[i], stdout);
437 key_free(keys[i]);
438 fprintf(stdout, "\n");
439 }
440 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000441 exit(0);
442}
Ben Lindstromffce1472001-08-06 21:57:31 +0000443#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000444
Ben Lindstrombba81212001-06-25 05:01:22 +0000445static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100446do_fingerprint(struct passwd *pw)
447{
Damien Miller98c7ad62000-03-09 21:27:49 +1100448 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000449 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000450 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000451 int i, skip = 0, num = 1, invalid = 1;
452 enum fp_rep rep;
453 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100454 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100455
Ben Lindstromd0fca422001-03-26 13:44:06 +0000456 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
457 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000458
Damien Miller95def091999-11-25 00:26:21 +1100459 if (!have_identity)
460 ask_filename(pw, "Enter file in which the key is");
461 if (stat(identity_file, &st) < 0) {
462 perror(identity_file);
463 exit(1);
464 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000465 public = key_load_public(identity_file, &comment);
466 if (public != NULL) {
467 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000468 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100469 key_free(public);
470 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000471 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100472 exit(0);
473 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000474 if (comment)
475 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100476
477 f = fopen(identity_file, "r");
478 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100479 while (fgets(line, sizeof(line), f)) {
480 i = strlen(line) - 1;
481 if (line[i] != '\n') {
482 error("line %d too long: %.40s...", num, line);
483 skip = 1;
484 continue;
Damien Miller95def091999-11-25 00:26:21 +1100485 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100486 num++;
487 if (skip) {
488 skip = 0;
489 continue;
490 }
491 line[i] = '\0';
492
493 /* Skip leading whitespace, empty and comment lines. */
494 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
495 ;
496 if (!*cp || *cp == '\n' || *cp == '#')
497 continue ;
498 i = strtol(cp, &ep, 10);
499 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
500 int quoted = 0;
501 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000502 for (; *cp && (quoted || (*cp != ' ' &&
503 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100504 if (*cp == '\\' && cp[1] == '"')
505 cp++; /* Skip both */
506 else if (*cp == '"')
507 quoted = !quoted;
508 }
509 if (!*cp)
510 continue;
511 *cp++ = '\0';
512 }
513 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000514 public = key_new(KEY_RSA1);
515 if (key_read(public, &cp) != 1) {
516 cp = ep;
517 key_free(public);
518 public = key_new(KEY_UNSPEC);
519 if (key_read(public, &cp) != 1) {
520 key_free(public);
521 continue;
522 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100523 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000524 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000525 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000526 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000527 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000528 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000529 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000530 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100531 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100532 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100533 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100534 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100535 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100536 exit(1);
537 }
Damien Miller95def091999-11-25 00:26:21 +1100538 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100539}
540
Damien Miller95def091999-11-25 00:26:21 +1100541/*
542 * Perform changing a passphrase. The argument is the passwd structure
543 * for the current user.
544 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000545static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100546do_change_passphrase(struct passwd *pw)
547{
Damien Miller95def091999-11-25 00:26:21 +1100548 char *comment;
549 char *old_passphrase, *passphrase1, *passphrase2;
550 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000551 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100552
Damien Miller95def091999-11-25 00:26:21 +1100553 if (!have_identity)
554 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100555 if (stat(identity_file, &st) < 0) {
556 perror(identity_file);
557 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558 }
Damien Miller95def091999-11-25 00:26:21 +1100559 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000560 private = key_load_private(identity_file, "", &comment);
561 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100562 if (identity_passphrase)
563 old_passphrase = xstrdup(identity_passphrase);
564 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000565 old_passphrase =
566 read_passphrase("Enter old passphrase: ",
567 RP_ALLOW_STDIN);
568 private = key_load_private(identity_file, old_passphrase,
569 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000570 memset(old_passphrase, 0, strlen(old_passphrase));
571 xfree(old_passphrase);
572 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100573 printf("Bad passphrase.\n");
574 exit(1);
575 }
Damien Miller95def091999-11-25 00:26:21 +1100576 }
577 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
Damien Miller95def091999-11-25 00:26:21 +1100579 /* Ask the new passphrase (twice). */
580 if (identity_new_passphrase) {
581 passphrase1 = xstrdup(identity_new_passphrase);
582 passphrase2 = NULL;
583 } else {
584 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000585 read_passphrase("Enter new passphrase (empty for no "
586 "passphrase): ", RP_ALLOW_STDIN);
587 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100588 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100589
590 /* Verify that they are the same. */
591 if (strcmp(passphrase1, passphrase2) != 0) {
592 memset(passphrase1, 0, strlen(passphrase1));
593 memset(passphrase2, 0, strlen(passphrase2));
594 xfree(passphrase1);
595 xfree(passphrase2);
596 printf("Pass phrases do not match. Try again.\n");
597 exit(1);
598 }
599 /* Destroy the other copy. */
600 memset(passphrase2, 0, strlen(passphrase2));
601 xfree(passphrase2);
602 }
603
604 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000605 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000606 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100607 memset(passphrase1, 0, strlen(passphrase1));
608 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000609 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100610 xfree(comment);
611 exit(1);
612 }
613 /* Destroy the passphrase and the copy of the key in memory. */
614 memset(passphrase1, 0, strlen(passphrase1));
615 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000616 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100617 xfree(comment);
618
619 printf("Your identification has been saved with the new passphrase.\n");
620 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621}
622
Damien Miller95def091999-11-25 00:26:21 +1100623/*
624 * Change the comment of a private key file.
625 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000626static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627do_change_comment(struct passwd *pw)
628{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000629 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000630 Key *private;
631 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100632 struct stat st;
633 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000634 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635
Damien Miller95def091999-11-25 00:26:21 +1100636 if (!have_identity)
637 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100638 if (stat(identity_file, &st) < 0) {
639 perror(identity_file);
640 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000642 private = key_load_private(identity_file, "", &comment);
643 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100644 if (identity_passphrase)
645 passphrase = xstrdup(identity_passphrase);
646 else if (identity_new_passphrase)
647 passphrase = xstrdup(identity_new_passphrase);
648 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000649 passphrase = read_passphrase("Enter passphrase: ",
650 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100651 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000652 private = key_load_private(identity_file, passphrase, &comment);
653 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100654 memset(passphrase, 0, strlen(passphrase));
655 xfree(passphrase);
656 printf("Bad passphrase.\n");
657 exit(1);
658 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000659 } else {
660 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100661 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000662 if (private->type != KEY_RSA1) {
663 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
664 key_free(private);
665 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100666 }
Damien Miller95def091999-11-25 00:26:21 +1100667 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668
Damien Miller95def091999-11-25 00:26:21 +1100669 if (identity_comment) {
670 strlcpy(new_comment, identity_comment, sizeof(new_comment));
671 } else {
672 printf("Enter new comment: ");
673 fflush(stdout);
674 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
675 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000676 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100677 exit(1);
678 }
Damien Miller95def091999-11-25 00:26:21 +1100679 if (strchr(new_comment, '\n'))
680 *strchr(new_comment, '\n') = 0;
681 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682
Damien Miller95def091999-11-25 00:26:21 +1100683 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000684 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000685 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100686 memset(passphrase, 0, strlen(passphrase));
687 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000688 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100689 xfree(comment);
690 exit(1);
691 }
Damien Miller95def091999-11-25 00:26:21 +1100692 memset(passphrase, 0, strlen(passphrase));
693 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000694 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000695 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696
Damien Miller95def091999-11-25 00:26:21 +1100697 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000698 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
699 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100700 printf("Could not save your public key in %s\n", identity_file);
701 exit(1);
702 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000703 f = fdopen(fd, "w");
704 if (f == NULL) {
705 printf("fdopen %s failed", identity_file);
706 exit(1);
707 }
Damien Millereba71ba2000-04-29 23:57:08 +1000708 if (!key_write(public, f))
709 fprintf(stderr, "write key failed");
710 key_free(public);
711 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100712 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713
Damien Miller95def091999-11-25 00:26:21 +1100714 xfree(comment);
715
716 printf("The comment in your key file has been changed.\n");
717 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718}
719
Ben Lindstrombba81212001-06-25 05:01:22 +0000720static void
Damien Miller431f66b1999-11-21 18:31:57 +1100721usage(void)
722{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000723 fprintf(stderr, "Usage: %s [options]\n", __progname);
724 fprintf(stderr, "Options:\n");
725 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
726 fprintf(stderr, " -c Change comment in private and public key files.\n");
727 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
728 fprintf(stderr, " -f filename Filename of the key file.\n");
729 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
730 fprintf(stderr, " -l Show fingerprint of key file.\n");
731 fprintf(stderr, " -p Change passphrase of private key file.\n");
732 fprintf(stderr, " -q Quiet.\n");
733 fprintf(stderr, " -y Read private key file and print public key.\n");
734 fprintf(stderr, " -t type Specify type of key to create.\n");
735 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
736 fprintf(stderr, " -C comment Provide new comment.\n");
737 fprintf(stderr, " -N phrase Provide new passphrase.\n");
738 fprintf(stderr, " -P phrase Provide old passphrase.\n");
739#ifdef SMARTCARD
740 fprintf(stderr, " -D reader Download public key from smartcard.\n");
741 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
742#endif /* SMARTCARD */
743
Damien Miller95def091999-11-25 00:26:21 +1100744 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100745}
746
Damien Miller95def091999-11-25 00:26:21 +1100747/*
748 * Main program for key management.
749 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750int
751main(int ac, char **av)
752{
Damien Millera41c8b12002-01-22 23:05:08 +1100753 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000754 char *reader_id = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000755 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100756 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100757 struct stat st;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000758 int opt, type, fd, download = 0;
Damien Miller95def091999-11-25 00:26:21 +1100759 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100760
Damien Miller95def091999-11-25 00:26:21 +1100761 extern int optind;
762 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000763
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000764 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000765
Damien Millerd3a18572000-06-07 19:55:44 +1000766 SSLeay_add_all_algorithms();
Kevin Steves3a881912002-07-20 19:05:40 +0000767 init_rng();
768 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +1000769
Damien Miller5428f641999-11-25 11:54:57 +1100770 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100771 pw = getpwuid(getuid());
772 if (!pw) {
773 printf("You don't exist, go away!\n");
774 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775 }
Damien Millereba71ba2000-04-29 23:57:08 +1000776 if (gethostname(hostname, sizeof(hostname)) < 0) {
777 perror("gethostname");
778 exit(1);
779 }
Damien Miller5428f641999-11-25 11:54:57 +1100780
Ben Lindstromf19578c2001-08-06 21:46:54 +0000781 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100782 switch (opt) {
783 case 'b':
784 bits = atoi(optarg);
785 if (bits < 512 || bits > 32768) {
786 printf("Bits has bad value.\n");
787 exit(1);
788 }
789 break;
Damien Miller95def091999-11-25 00:26:21 +1100790 case 'l':
791 print_fingerprint = 1;
792 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000793 case 'B':
794 print_bubblebabble = 1;
795 break;
Damien Miller95def091999-11-25 00:26:21 +1100796 case 'p':
797 change_passphrase = 1;
798 break;
Damien Miller95def091999-11-25 00:26:21 +1100799 case 'c':
800 change_comment = 1;
801 break;
Damien Miller95def091999-11-25 00:26:21 +1100802 case 'f':
803 strlcpy(identity_file, optarg, sizeof(identity_file));
804 have_identity = 1;
805 break;
Damien Miller95def091999-11-25 00:26:21 +1100806 case 'P':
807 identity_passphrase = optarg;
808 break;
Damien Miller95def091999-11-25 00:26:21 +1100809 case 'N':
810 identity_new_passphrase = optarg;
811 break;
Damien Miller95def091999-11-25 00:26:21 +1100812 case 'C':
813 identity_comment = optarg;
814 break;
Damien Miller95def091999-11-25 00:26:21 +1100815 case 'q':
816 quiet = 1;
817 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000818 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 /* unused */
820 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000821 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000822 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000823 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000824 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000825 convert_to_ssh2 = 1;
826 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000827 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000828 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000829 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000830 convert_from_ssh2 = 1;
831 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000832 case 'y':
833 print_public = 1;
834 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000835 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100836 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000837 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100838 case 't':
839 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100840 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000841 case 'D':
842 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000843 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000844 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000845 break;
Damien Miller95def091999-11-25 00:26:21 +1100846 case '?':
847 default:
848 usage();
849 }
850 }
851 if (optind < ac) {
852 printf("Too many arguments.\n");
853 usage();
854 }
855 if (change_passphrase && change_comment) {
856 printf("Can only have one of -p and -c.\n");
857 usage();
858 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000859 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100860 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100861 if (change_passphrase)
862 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100863 if (change_comment)
864 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +0000865 if (convert_to_ssh2)
866 do_convert_to_ssh2(pw);
867 if (convert_from_ssh2)
868 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000869 if (print_public)
870 do_print_public(pw);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000871 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000872#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000873 if (download)
874 do_download(pw, reader_id);
875 else
876 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000877#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000878 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000879#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000880 }
Damien Miller95def091999-11-25 00:26:21 +1100881
882 arc4random_stir();
883
Damien Miller154dda72002-01-22 23:08:16 +1100884 if (key_type_name == NULL) {
885 printf("You must specify a key type (-t).\n");
886 usage();
887 }
Damien Millere39cacc2000-11-29 12:18:44 +1100888 type = key_type_from_name(key_type_name);
889 if (type == KEY_UNSPEC) {
890 fprintf(stderr, "unknown key type %s\n", key_type_name);
891 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000892 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100893 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100894 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100895 private = key_generate(type, bits);
896 if (private == NULL) {
897 fprintf(stderr, "key_generate failed");
898 exit(1);
899 }
900 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100901
902 if (!have_identity)
903 ask_filename(pw, "Enter file in which to save the key");
904
905 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000906 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100907 if (strstr(identity_file, dotsshdir) != NULL &&
908 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000909 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100910 error("Could not create directory '%s'.", dotsshdir);
911 else if (!quiet)
912 printf("Created directory '%s'.\n", dotsshdir);
913 }
914 /* If the file already exists, ask the user to confirm. */
915 if (stat(identity_file, &st) >= 0) {
916 char yesno[3];
917 printf("%s already exists.\n", identity_file);
918 printf("Overwrite (y/n)? ");
919 fflush(stdout);
920 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
921 exit(1);
922 if (yesno[0] != 'y' && yesno[0] != 'Y')
923 exit(1);
924 }
925 /* Ask for a passphrase (twice). */
926 if (identity_passphrase)
927 passphrase1 = xstrdup(identity_passphrase);
928 else if (identity_new_passphrase)
929 passphrase1 = xstrdup(identity_new_passphrase);
930 else {
931passphrase_again:
932 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000933 read_passphrase("Enter passphrase (empty for no "
934 "passphrase): ", RP_ALLOW_STDIN);
935 passphrase2 = read_passphrase("Enter same passphrase again: ",
936 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100937 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000938 /*
939 * The passphrases do not match. Clear them and
940 * retry.
941 */
Damien Miller95def091999-11-25 00:26:21 +1100942 memset(passphrase1, 0, strlen(passphrase1));
943 memset(passphrase2, 0, strlen(passphrase2));
944 xfree(passphrase1);
945 xfree(passphrase2);
946 printf("Passphrases do not match. Try again.\n");
947 goto passphrase_again;
948 }
949 /* Clear the other copy of the passphrase. */
950 memset(passphrase2, 0, strlen(passphrase2));
951 xfree(passphrase2);
952 }
953
Damien Miller95def091999-11-25 00:26:21 +1100954 if (identity_comment) {
955 strlcpy(comment, identity_comment, sizeof(comment));
956 } else {
Damien Miller4af51302000-04-16 11:18:38 +1000957 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +1100958 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
959 }
960
961 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000962 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000963 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100964 memset(passphrase1, 0, strlen(passphrase1));
965 xfree(passphrase1);
966 exit(1);
967 }
968 /* Clear the passphrase. */
969 memset(passphrase1, 0, strlen(passphrase1));
970 xfree(passphrase1);
971
972 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100973 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100974 arc4random_stir();
975
976 if (!quiet)
977 printf("Your identification has been saved in %s.\n", identity_file);
978
Damien Miller95def091999-11-25 00:26:21 +1100979 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000980 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
981 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100982 printf("Could not save your public key in %s\n", identity_file);
983 exit(1);
984 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000985 f = fdopen(fd, "w");
986 if (f == NULL) {
987 printf("fdopen %s failed", identity_file);
988 exit(1);
989 }
Damien Millereba71ba2000-04-29 23:57:08 +1000990 if (!key_write(public, f))
991 fprintf(stderr, "write key failed");
992 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100993 fclose(f);
994
995 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +0000996 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +1000997 printf("Your public key has been saved in %s.\n",
998 identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100999 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001000 printf("%s %s\n", fp, comment);
1001 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001002 }
Damien Millereba71ba2000-04-29 23:57:08 +10001003
1004 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001005 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006}