blob: 4273c11321c8830820cd51936733b4653830ae10 [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"
Ben Lindstrom58d3b722002-06-23 21:28:13 +000015RCSID("$OpenBSD: ssh-keygen.c,v 1.101 2002/06/23 09:39:55 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Damien Millereba71ba2000-04-29 23:57:08 +100017#include <openssl/evp.h>
18#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100019
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100021#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000022#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100023#include "authfile.h"
24#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "buffer.h"
26#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "pathnames.h"
28#include "log.h"
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);
112 fflush(stderr);
Damien Miller95def091999-11-25 00:26:21 +1100113 if (fgets(buf, sizeof(buf), stdin) == NULL)
114 exit(1);
115 if (strchr(buf, '\n'))
116 *strchr(buf, '\n') = 0;
117 if (strcmp(buf, "") != 0)
118 strlcpy(identity_file, buf, sizeof(identity_file));
119 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100120}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
Ben Lindstrombba81212001-06-25 05:01:22 +0000122static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000123load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000124{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000125 char *pass;
126 Key *prv;
127
Ben Lindstroma3700052001-04-05 23:26:32 +0000128 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000129 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000130 if (identity_passphrase)
131 pass = xstrdup(identity_passphrase);
132 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000133 pass = read_passphrase("Enter passphrase: ",
134 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000135 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000136 memset(pass, 0, strlen(pass));
137 xfree(pass);
138 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000139 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000140}
141
Damien Miller874d77b2000-10-14 16:23:11 +1100142#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000143#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100144#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000145#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000146
Ben Lindstrombba81212001-06-25 05:01:22 +0000147static void
Damien Millereba71ba2000-04-29 23:57:08 +1000148do_convert_to_ssh2(struct passwd *pw)
149{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000150 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000151 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000152 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000153 struct stat st;
154
155 if (!have_identity)
156 ask_filename(pw, "Enter file in which the key is");
157 if (stat(identity_file, &st) < 0) {
158 perror(identity_file);
159 exit(1);
160 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000161 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000162 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000163 fprintf(stderr, "load failed\n");
164 exit(1);
165 }
Damien Millereba71ba2000-04-29 23:57:08 +1000166 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000167 if (key_to_blob(k, &blob, &len) <= 0) {
168 fprintf(stderr, "key_to_blob failed\n");
169 exit(1);
170 }
Damien Miller874d77b2000-10-14 16:23:11 +1100171 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000172 fprintf(stdout,
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000173 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000174 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000175 pw->pw_name, hostname);
176 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100177 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000178 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000179 xfree(blob);
180 exit(0);
181}
182
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static void
Damien Miller874d77b2000-10-14 16:23:11 +1100184buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
185{
186 int bits = buffer_get_int(b);
187 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000188
Damien Miller874d77b2000-10-14 16:23:11 +1100189 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000190 fatal("buffer_get_bignum_bits: input buffer too small: "
191 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100192 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100193 buffer_consume(b, bytes);
194}
195
Ben Lindstrombba81212001-06-25 05:01:22 +0000196static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000197do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100198{
199 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100200 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100201 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000202 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000203 int magic, rlen, ktype, i1, i2, i3, i4;
204 u_int slen;
205 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100206
207 buffer_init(&b);
208 buffer_append(&b, blob, blen);
209
210 magic = buffer_get_int(&b);
211 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
212 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
213 buffer_free(&b);
214 return NULL;
215 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000216 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100217 type = buffer_get_string(&b, NULL);
218 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000219 i2 = buffer_get_int(&b);
220 i3 = buffer_get_int(&b);
221 i4 = buffer_get_int(&b);
222 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100223 if (strcmp(cipher, "none") != 0) {
224 error("unsupported cipher %s", cipher);
225 xfree(cipher);
226 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000227 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100228 return NULL;
229 }
230 xfree(cipher);
231
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000232 if (strstr(type, "dsa")) {
233 ktype = KEY_DSA;
234 } else if (strstr(type, "rsa")) {
235 ktype = KEY_RSA;
236 } else {
237 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100238 return NULL;
239 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000240 key = key_new_private(ktype);
241 xfree(type);
242
243 switch (key->type) {
244 case KEY_DSA:
245 buffer_get_bignum_bits(&b, key->dsa->p);
246 buffer_get_bignum_bits(&b, key->dsa->g);
247 buffer_get_bignum_bits(&b, key->dsa->q);
248 buffer_get_bignum_bits(&b, key->dsa->pub_key);
249 buffer_get_bignum_bits(&b, key->dsa->priv_key);
250 break;
251 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000252 e = buffer_get_char(&b);
253 debug("e %lx", e);
254 if (e < 30) {
255 e <<= 8;
256 e += buffer_get_char(&b);
257 debug("e %lx", e);
258 e <<= 8;
259 e += buffer_get_char(&b);
260 debug("e %lx", e);
261 }
262 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000263 buffer_free(&b);
264 key_free(key);
265 return NULL;
266 }
267 buffer_get_bignum_bits(&b, key->rsa->d);
268 buffer_get_bignum_bits(&b, key->rsa->n);
269 buffer_get_bignum_bits(&b, key->rsa->iqmp);
270 buffer_get_bignum_bits(&b, key->rsa->q);
271 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000272 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000273 break;
274 }
Damien Miller874d77b2000-10-14 16:23:11 +1100275 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000276 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000277 error("do_convert_private_ssh2_from_blob: "
278 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100279 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000280
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000281 /* try the key */
282 key_sign(key, &sig, &slen, data, sizeof(data));
283 key_verify(key, sig, slen, data, sizeof(data));
284 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100285 return key;
286}
287
Ben Lindstrombba81212001-06-25 05:01:22 +0000288static void
Damien Millereba71ba2000-04-29 23:57:08 +1000289do_convert_from_ssh2(struct passwd *pw)
290{
291 Key *k;
292 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000293 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000294 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000295 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000296 char encoded[8096];
297 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100298 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000299 FILE *fp;
300
301 if (!have_identity)
302 ask_filename(pw, "Enter file in which the key is");
303 if (stat(identity_file, &st) < 0) {
304 perror(identity_file);
305 exit(1);
306 }
307 fp = fopen(identity_file, "r");
308 if (fp == NULL) {
309 perror(identity_file);
310 exit(1);
311 }
312 encoded[0] = '\0';
313 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000314 if (!(p = strchr(line, '\n'))) {
315 fprintf(stderr, "input line too long.\n");
316 exit(1);
317 }
318 if (p > line && p[-1] == '\\')
319 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000320 if (strncmp(line, "----", 4) == 0 ||
321 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100322 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
323 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000324 if (strstr(line, " END ") != NULL) {
325 break;
326 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000327 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000328 continue;
329 }
Damien Miller30c3d422000-05-09 11:02:59 +1000330 if (escaped) {
331 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000332 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000333 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000334 }
335 *p = '\0';
336 strlcat(encoded, line, sizeof(encoded));
337 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000338 len = strlen(encoded);
339 if (((len % 4) == 3) &&
340 (encoded[len-1] == '=') &&
341 (encoded[len-2] == '=') &&
342 (encoded[len-3] == '='))
343 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100344 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000345 if (blen < 0) {
346 fprintf(stderr, "uudecode failed.\n");
347 exit(1);
348 }
Damien Miller874d77b2000-10-14 16:23:11 +1100349 k = private ?
350 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100351 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100352 if (k == NULL) {
353 fprintf(stderr, "decode blob failed.\n");
354 exit(1);
355 }
356 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000357 (k->type == KEY_DSA ?
358 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
359 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100360 key_write(k, stdout);
361 if (!ok) {
362 fprintf(stderr, "key write failed");
363 exit(1);
364 }
Damien Millereba71ba2000-04-29 23:57:08 +1000365 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100366 if (!private)
367 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000368 fclose(fp);
369 exit(0);
370}
371
Ben Lindstrombba81212001-06-25 05:01:22 +0000372static void
Damien Millereba71ba2000-04-29 23:57:08 +1000373do_print_public(struct passwd *pw)
374{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000375 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000376 struct stat st;
377
378 if (!have_identity)
379 ask_filename(pw, "Enter file in which the key is");
380 if (stat(identity_file, &st) < 0) {
381 perror(identity_file);
382 exit(1);
383 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000384 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000385 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000386 fprintf(stderr, "load failed\n");
387 exit(1);
388 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000389 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000390 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000391 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000392 fprintf(stdout, "\n");
393 exit(0);
394}
395
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000396#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000397static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000398do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000399{
Ben Lindstromcd392282001-07-04 03:44:03 +0000400 Key *prv = NULL;
401 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000402 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000403
404 if (!have_identity)
405 ask_filename(pw, "Enter file in which the key is");
406 if (stat(identity_file, &st) < 0) {
407 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000408 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000409 }
410 prv = load_identity(identity_file);
411 if (prv == NULL) {
412 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000413 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000414 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000415 ret = sc_put_key(prv, sc_reader_id);
416 key_free(prv);
417 if (ret < 0)
418 exit(1);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000419 log("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000420 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000421}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000422
423static void
424do_download(struct passwd *pw, const char *sc_reader_id)
425{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000426 Key **keys = NULL;
427 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000428
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000429 keys = sc_get_keys(sc_reader_id, NULL);
430 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000431 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000432 for (i = 0; keys[i]; i++) {
433 key_write(keys[i], stdout);
434 key_free(keys[i]);
435 fprintf(stdout, "\n");
436 }
437 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000438 exit(0);
439}
Ben Lindstromffce1472001-08-06 21:57:31 +0000440#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000441
Ben Lindstrombba81212001-06-25 05:01:22 +0000442static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100443do_fingerprint(struct passwd *pw)
444{
Damien Miller98c7ad62000-03-09 21:27:49 +1100445 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000446 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000447 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000448 int i, skip = 0, num = 1, invalid = 1;
449 enum fp_rep rep;
450 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100451 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100452
Ben Lindstromd0fca422001-03-26 13:44:06 +0000453 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
454 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000455
Damien Miller95def091999-11-25 00:26:21 +1100456 if (!have_identity)
457 ask_filename(pw, "Enter file in which the key is");
458 if (stat(identity_file, &st) < 0) {
459 perror(identity_file);
460 exit(1);
461 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000462 public = key_load_public(identity_file, &comment);
463 if (public != NULL) {
464 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000465 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100466 key_free(public);
467 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000468 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100469 exit(0);
470 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000471 if (comment)
472 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100473
474 f = fopen(identity_file, "r");
475 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100476 while (fgets(line, sizeof(line), f)) {
477 i = strlen(line) - 1;
478 if (line[i] != '\n') {
479 error("line %d too long: %.40s...", num, line);
480 skip = 1;
481 continue;
Damien Miller95def091999-11-25 00:26:21 +1100482 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100483 num++;
484 if (skip) {
485 skip = 0;
486 continue;
487 }
488 line[i] = '\0';
489
490 /* Skip leading whitespace, empty and comment lines. */
491 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
492 ;
493 if (!*cp || *cp == '\n' || *cp == '#')
494 continue ;
495 i = strtol(cp, &ep, 10);
496 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
497 int quoted = 0;
498 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000499 for (; *cp && (quoted || (*cp != ' ' &&
500 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100501 if (*cp == '\\' && cp[1] == '"')
502 cp++; /* Skip both */
503 else if (*cp == '"')
504 quoted = !quoted;
505 }
506 if (!*cp)
507 continue;
508 *cp++ = '\0';
509 }
510 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000511 public = key_new(KEY_RSA1);
512 if (key_read(public, &cp) != 1) {
513 cp = ep;
514 key_free(public);
515 public = key_new(KEY_UNSPEC);
516 if (key_read(public, &cp) != 1) {
517 key_free(public);
518 continue;
519 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100520 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000521 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000522 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000523 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000524 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000525 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000526 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000527 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100528 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100529 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100530 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100531 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100532 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100533 exit(1);
534 }
Damien Miller95def091999-11-25 00:26:21 +1100535 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100536}
537
Damien Miller95def091999-11-25 00:26:21 +1100538/*
539 * Perform changing a passphrase. The argument is the passwd structure
540 * for the current user.
541 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000542static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100543do_change_passphrase(struct passwd *pw)
544{
Damien Miller95def091999-11-25 00:26:21 +1100545 char *comment;
546 char *old_passphrase, *passphrase1, *passphrase2;
547 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000548 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100549
Damien Miller95def091999-11-25 00:26:21 +1100550 if (!have_identity)
551 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100552 if (stat(identity_file, &st) < 0) {
553 perror(identity_file);
554 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555 }
Damien Miller95def091999-11-25 00:26:21 +1100556 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000557 private = key_load_private(identity_file, "", &comment);
558 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100559 if (identity_passphrase)
560 old_passphrase = xstrdup(identity_passphrase);
561 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000562 old_passphrase =
563 read_passphrase("Enter old passphrase: ",
564 RP_ALLOW_STDIN);
565 private = key_load_private(identity_file, old_passphrase,
566 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000567 memset(old_passphrase, 0, strlen(old_passphrase));
568 xfree(old_passphrase);
569 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100570 printf("Bad passphrase.\n");
571 exit(1);
572 }
Damien Miller95def091999-11-25 00:26:21 +1100573 }
574 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Miller95def091999-11-25 00:26:21 +1100576 /* Ask the new passphrase (twice). */
577 if (identity_new_passphrase) {
578 passphrase1 = xstrdup(identity_new_passphrase);
579 passphrase2 = NULL;
580 } else {
581 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000582 read_passphrase("Enter new passphrase (empty for no "
583 "passphrase): ", RP_ALLOW_STDIN);
584 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100585 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100586
587 /* Verify that they are the same. */
588 if (strcmp(passphrase1, passphrase2) != 0) {
589 memset(passphrase1, 0, strlen(passphrase1));
590 memset(passphrase2, 0, strlen(passphrase2));
591 xfree(passphrase1);
592 xfree(passphrase2);
593 printf("Pass phrases do not match. Try again.\n");
594 exit(1);
595 }
596 /* Destroy the other copy. */
597 memset(passphrase2, 0, strlen(passphrase2));
598 xfree(passphrase2);
599 }
600
601 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000602 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000603 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100604 memset(passphrase1, 0, strlen(passphrase1));
605 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000606 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100607 xfree(comment);
608 exit(1);
609 }
610 /* Destroy the passphrase and the copy of the key in memory. */
611 memset(passphrase1, 0, strlen(passphrase1));
612 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000613 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100614 xfree(comment);
615
616 printf("Your identification has been saved with the new passphrase.\n");
617 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618}
619
Damien Miller95def091999-11-25 00:26:21 +1100620/*
621 * Change the comment of a private key file.
622 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000623static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624do_change_comment(struct passwd *pw)
625{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000626 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000627 Key *private;
628 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100629 struct stat st;
630 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000631 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632
Damien Miller95def091999-11-25 00:26:21 +1100633 if (!have_identity)
634 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100635 if (stat(identity_file, &st) < 0) {
636 perror(identity_file);
637 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000639 private = key_load_private(identity_file, "", &comment);
640 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100641 if (identity_passphrase)
642 passphrase = xstrdup(identity_passphrase);
643 else if (identity_new_passphrase)
644 passphrase = xstrdup(identity_new_passphrase);
645 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000646 passphrase = read_passphrase("Enter passphrase: ",
647 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100648 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000649 private = key_load_private(identity_file, passphrase, &comment);
650 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100651 memset(passphrase, 0, strlen(passphrase));
652 xfree(passphrase);
653 printf("Bad passphrase.\n");
654 exit(1);
655 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000656 } else {
657 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100658 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000659 if (private->type != KEY_RSA1) {
660 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
661 key_free(private);
662 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100663 }
Damien Miller95def091999-11-25 00:26:21 +1100664 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665
Damien Miller95def091999-11-25 00:26:21 +1100666 if (identity_comment) {
667 strlcpy(new_comment, identity_comment, sizeof(new_comment));
668 } else {
669 printf("Enter new comment: ");
670 fflush(stdout);
671 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
672 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000673 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100674 exit(1);
675 }
Damien Miller95def091999-11-25 00:26:21 +1100676 if (strchr(new_comment, '\n'))
677 *strchr(new_comment, '\n') = 0;
678 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679
Damien Miller95def091999-11-25 00:26:21 +1100680 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000681 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000682 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100683 memset(passphrase, 0, strlen(passphrase));
684 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000685 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100686 xfree(comment);
687 exit(1);
688 }
Damien Miller95def091999-11-25 00:26:21 +1100689 memset(passphrase, 0, strlen(passphrase));
690 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000691 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000692 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693
Damien Miller95def091999-11-25 00:26:21 +1100694 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000695 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
696 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100697 printf("Could not save your public key in %s\n", identity_file);
698 exit(1);
699 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000700 f = fdopen(fd, "w");
701 if (f == NULL) {
702 printf("fdopen %s failed", identity_file);
703 exit(1);
704 }
Damien Millereba71ba2000-04-29 23:57:08 +1000705 if (!key_write(public, f))
706 fprintf(stderr, "write key failed");
707 key_free(public);
708 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100709 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710
Damien Miller95def091999-11-25 00:26:21 +1100711 xfree(comment);
712
713 printf("The comment in your key file has been changed.\n");
714 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715}
716
Ben Lindstrombba81212001-06-25 05:01:22 +0000717static void
Damien Miller431f66b1999-11-21 18:31:57 +1100718usage(void)
719{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000720 fprintf(stderr, "Usage: %s [options]\n", __progname);
721 fprintf(stderr, "Options:\n");
722 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
723 fprintf(stderr, " -c Change comment in private and public key files.\n");
724 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
725 fprintf(stderr, " -f filename Filename of the key file.\n");
726 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
727 fprintf(stderr, " -l Show fingerprint of key file.\n");
728 fprintf(stderr, " -p Change passphrase of private key file.\n");
729 fprintf(stderr, " -q Quiet.\n");
730 fprintf(stderr, " -y Read private key file and print public key.\n");
731 fprintf(stderr, " -t type Specify type of key to create.\n");
732 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
733 fprintf(stderr, " -C comment Provide new comment.\n");
734 fprintf(stderr, " -N phrase Provide new passphrase.\n");
735 fprintf(stderr, " -P phrase Provide old passphrase.\n");
736#ifdef SMARTCARD
737 fprintf(stderr, " -D reader Download public key from smartcard.\n");
738 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
739#endif /* SMARTCARD */
740
Damien Miller95def091999-11-25 00:26:21 +1100741 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100742}
743
Damien Miller95def091999-11-25 00:26:21 +1100744/*
745 * Main program for key management.
746 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747int
748main(int ac, char **av)
749{
Damien Millera41c8b12002-01-22 23:05:08 +1100750 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000751 char *reader_id = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000752 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100753 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100754 struct stat st;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000755 int opt, type, fd, download = 0;
Damien Miller95def091999-11-25 00:26:21 +1100756 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100757
Damien Miller95def091999-11-25 00:26:21 +1100758 extern int optind;
759 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000760
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000761 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000762
Damien Millerd3a18572000-06-07 19:55:44 +1000763 SSLeay_add_all_algorithms();
Damien Millereba71ba2000-04-29 23:57:08 +1000764
Damien Miller5428f641999-11-25 11:54:57 +1100765 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100766 pw = getpwuid(getuid());
767 if (!pw) {
768 printf("You don't exist, go away!\n");
769 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770 }
Damien Millereba71ba2000-04-29 23:57:08 +1000771 if (gethostname(hostname, sizeof(hostname)) < 0) {
772 perror("gethostname");
773 exit(1);
774 }
Damien Miller5428f641999-11-25 11:54:57 +1100775
Ben Lindstromf19578c2001-08-06 21:46:54 +0000776 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100777 switch (opt) {
778 case 'b':
779 bits = atoi(optarg);
780 if (bits < 512 || bits > 32768) {
781 printf("Bits has bad value.\n");
782 exit(1);
783 }
784 break;
Damien Miller95def091999-11-25 00:26:21 +1100785 case 'l':
786 print_fingerprint = 1;
787 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000788 case 'B':
789 print_bubblebabble = 1;
790 break;
Damien Miller95def091999-11-25 00:26:21 +1100791 case 'p':
792 change_passphrase = 1;
793 break;
Damien Miller95def091999-11-25 00:26:21 +1100794 case 'c':
795 change_comment = 1;
796 break;
Damien Miller95def091999-11-25 00:26:21 +1100797 case 'f':
798 strlcpy(identity_file, optarg, sizeof(identity_file));
799 have_identity = 1;
800 break;
Damien Miller95def091999-11-25 00:26:21 +1100801 case 'P':
802 identity_passphrase = optarg;
803 break;
Damien Miller95def091999-11-25 00:26:21 +1100804 case 'N':
805 identity_new_passphrase = optarg;
806 break;
Damien Miller95def091999-11-25 00:26:21 +1100807 case 'C':
808 identity_comment = optarg;
809 break;
Damien Miller95def091999-11-25 00:26:21 +1100810 case 'q':
811 quiet = 1;
812 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000813 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100814 /* unused */
815 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000816 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000817 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000818 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000819 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000820 convert_to_ssh2 = 1;
821 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000822 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000823 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000824 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000825 convert_from_ssh2 = 1;
826 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000827 case 'y':
828 print_public = 1;
829 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000830 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100831 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000832 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100833 case 't':
834 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100835 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000836 case 'D':
837 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000838 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000839 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000840 break;
Damien Miller95def091999-11-25 00:26:21 +1100841 case '?':
842 default:
843 usage();
844 }
845 }
846 if (optind < ac) {
847 printf("Too many arguments.\n");
848 usage();
849 }
850 if (change_passphrase && change_comment) {
851 printf("Can only have one of -p and -c.\n");
852 usage();
853 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000854 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100855 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100856 if (change_passphrase)
857 do_change_passphrase(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000858 if (convert_to_ssh2)
859 do_convert_to_ssh2(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100860 if (change_comment)
861 do_change_comment(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000862 if (print_public)
863 do_print_public(pw);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000864 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000865#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000866 if (download)
867 do_download(pw, reader_id);
868 else
869 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000870#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000871 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000872#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000873 }
Damien Miller95def091999-11-25 00:26:21 +1100874
Damien Miller8eb71412002-01-30 09:37:06 +1100875 init_rng();
876 seed_rng();
Damien Miller95def091999-11-25 00:26:21 +1100877 arc4random_stir();
878
Damien Miller4a10d2e2002-03-11 22:53:29 +1100879 if (convert_from_ssh2)
880 do_convert_from_ssh2(pw);
881
Damien Miller154dda72002-01-22 23:08:16 +1100882 if (key_type_name == NULL) {
883 printf("You must specify a key type (-t).\n");
884 usage();
885 }
Damien Millere39cacc2000-11-29 12:18:44 +1100886 type = key_type_from_name(key_type_name);
887 if (type == KEY_UNSPEC) {
888 fprintf(stderr, "unknown key type %s\n", key_type_name);
889 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000890 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100891 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100892 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100893 private = key_generate(type, bits);
894 if (private == NULL) {
895 fprintf(stderr, "key_generate failed");
896 exit(1);
897 }
898 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100899
900 if (!have_identity)
901 ask_filename(pw, "Enter file in which to save the key");
902
903 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000904 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100905 if (strstr(identity_file, dotsshdir) != NULL &&
906 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000907 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100908 error("Could not create directory '%s'.", dotsshdir);
909 else if (!quiet)
910 printf("Created directory '%s'.\n", dotsshdir);
911 }
912 /* If the file already exists, ask the user to confirm. */
913 if (stat(identity_file, &st) >= 0) {
914 char yesno[3];
915 printf("%s already exists.\n", identity_file);
916 printf("Overwrite (y/n)? ");
917 fflush(stdout);
918 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
919 exit(1);
920 if (yesno[0] != 'y' && yesno[0] != 'Y')
921 exit(1);
922 }
923 /* Ask for a passphrase (twice). */
924 if (identity_passphrase)
925 passphrase1 = xstrdup(identity_passphrase);
926 else if (identity_new_passphrase)
927 passphrase1 = xstrdup(identity_new_passphrase);
928 else {
929passphrase_again:
930 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000931 read_passphrase("Enter passphrase (empty for no "
932 "passphrase): ", RP_ALLOW_STDIN);
933 passphrase2 = read_passphrase("Enter same passphrase again: ",
934 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100935 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000936 /*
937 * The passphrases do not match. Clear them and
938 * retry.
939 */
Damien Miller95def091999-11-25 00:26:21 +1100940 memset(passphrase1, 0, strlen(passphrase1));
941 memset(passphrase2, 0, strlen(passphrase2));
942 xfree(passphrase1);
943 xfree(passphrase2);
944 printf("Passphrases do not match. Try again.\n");
945 goto passphrase_again;
946 }
947 /* Clear the other copy of the passphrase. */
948 memset(passphrase2, 0, strlen(passphrase2));
949 xfree(passphrase2);
950 }
951
Damien Miller95def091999-11-25 00:26:21 +1100952 if (identity_comment) {
953 strlcpy(comment, identity_comment, sizeof(comment));
954 } else {
Damien Miller4af51302000-04-16 11:18:38 +1000955 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +1100956 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
957 }
958
959 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000960 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000961 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100962 memset(passphrase1, 0, strlen(passphrase1));
963 xfree(passphrase1);
964 exit(1);
965 }
966 /* Clear the passphrase. */
967 memset(passphrase1, 0, strlen(passphrase1));
968 xfree(passphrase1);
969
970 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100971 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100972 arc4random_stir();
973
974 if (!quiet)
975 printf("Your identification has been saved in %s.\n", identity_file);
976
Damien Miller95def091999-11-25 00:26:21 +1100977 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000978 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
979 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100980 printf("Could not save your public key in %s\n", identity_file);
981 exit(1);
982 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000983 f = fdopen(fd, "w");
984 if (f == NULL) {
985 printf("fdopen %s failed", identity_file);
986 exit(1);
987 }
Damien Millereba71ba2000-04-29 23:57:08 +1000988 if (!key_write(public, f))
989 fprintf(stderr, "write key failed");
990 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100991 fclose(f);
992
993 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +0000994 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +1000995 printf("Your public key has been saved in %s.\n",
996 identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100997 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +0000998 printf("%s %s\n", fp, comment);
999 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001000 }
Damien Millereba71ba2000-04-29 23:57:08 +10001001
1002 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001003 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004}