blob: f3ea4f1fdee6dd9fd83a90f775a9261d90dd8245 [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 Miller37876e92003-05-15 10:19:46 +100015RCSID("$OpenBSD: ssh-keygen.c,v 1.105 2003/05/14 18:16:20 jakob 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 Miller37876e92003-05-15 10:19:46 +100073int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110074
Damien Millera41c8b12002-01-22 23:05:08 +110075char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100076
Damien Miller431f66b1999-11-21 18:31:57 +110077/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110078#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110079extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000080#else
81char *__progname;
82#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Damien Millereba71ba2000-04-29 23:57:08 +100084char hostname[MAXHOSTNAMELEN];
85
Ben Lindstrombba81212001-06-25 05:01:22 +000086static void
Damien Miller431f66b1999-11-21 18:31:57 +110087ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088{
Damien Miller95def091999-11-25 00:26:21 +110089 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110090 char *name = NULL;
91
Damien Miller993dd552002-02-19 15:22:47 +110092 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +000093 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller993dd552002-02-19 15:22:47 +110094 else
95 switch (key_type_from_name(key_type_name)) {
96 case KEY_RSA1:
97 name = _PATH_SSH_CLIENT_IDENTITY;
98 break;
99 case KEY_DSA:
100 name = _PATH_SSH_CLIENT_ID_DSA;
101 break;
102 case KEY_RSA:
103 name = _PATH_SSH_CLIENT_ID_RSA;
104 break;
105 default:
106 fprintf(stderr, "bad key type");
107 exit(1);
108 break;
109 }
110
Damien Millere39cacc2000-11-29 12:18:44 +1100111 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000112 fprintf(stderr, "%s (%s): ", prompt, identity_file);
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 }
Damien Millerdb274722003-05-14 13:45:22 +1000167 if (k->type == KEY_RSA1) {
168 fprintf(stderr, "version 1 keys are not supported\n");
169 exit(1);
170 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000171 if (key_to_blob(k, &blob, &len) <= 0) {
172 fprintf(stderr, "key_to_blob failed\n");
173 exit(1);
174 }
Damien Miller874d77b2000-10-14 16:23:11 +1100175 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000176 fprintf(stdout,
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000177 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000178 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000179 pw->pw_name, hostname);
180 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100181 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000182 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000183 xfree(blob);
184 exit(0);
185}
186
Ben Lindstrombba81212001-06-25 05:01:22 +0000187static void
Damien Miller874d77b2000-10-14 16:23:11 +1100188buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
189{
190 int bits = buffer_get_int(b);
191 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000192
Damien Miller874d77b2000-10-14 16:23:11 +1100193 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000194 fatal("buffer_get_bignum_bits: input buffer too small: "
195 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100196 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100197 buffer_consume(b, bytes);
198}
199
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000201do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100202{
203 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100204 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100205 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000206 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000207 int magic, rlen, ktype, i1, i2, i3, i4;
208 u_int slen;
209 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100210
211 buffer_init(&b);
212 buffer_append(&b, blob, blen);
213
214 magic = buffer_get_int(&b);
215 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
216 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
217 buffer_free(&b);
218 return NULL;
219 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000220 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100221 type = buffer_get_string(&b, NULL);
222 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000223 i2 = buffer_get_int(&b);
224 i3 = buffer_get_int(&b);
225 i4 = buffer_get_int(&b);
226 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100227 if (strcmp(cipher, "none") != 0) {
228 error("unsupported cipher %s", cipher);
229 xfree(cipher);
230 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000231 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100232 return NULL;
233 }
234 xfree(cipher);
235
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000236 if (strstr(type, "dsa")) {
237 ktype = KEY_DSA;
238 } else if (strstr(type, "rsa")) {
239 ktype = KEY_RSA;
240 } else {
241 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100242 return NULL;
243 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000244 key = key_new_private(ktype);
245 xfree(type);
246
247 switch (key->type) {
248 case KEY_DSA:
249 buffer_get_bignum_bits(&b, key->dsa->p);
250 buffer_get_bignum_bits(&b, key->dsa->g);
251 buffer_get_bignum_bits(&b, key->dsa->q);
252 buffer_get_bignum_bits(&b, key->dsa->pub_key);
253 buffer_get_bignum_bits(&b, key->dsa->priv_key);
254 break;
255 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000256 e = buffer_get_char(&b);
257 debug("e %lx", e);
258 if (e < 30) {
259 e <<= 8;
260 e += buffer_get_char(&b);
261 debug("e %lx", e);
262 e <<= 8;
263 e += buffer_get_char(&b);
264 debug("e %lx", e);
265 }
266 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000267 buffer_free(&b);
268 key_free(key);
269 return NULL;
270 }
271 buffer_get_bignum_bits(&b, key->rsa->d);
272 buffer_get_bignum_bits(&b, key->rsa->n);
273 buffer_get_bignum_bits(&b, key->rsa->iqmp);
274 buffer_get_bignum_bits(&b, key->rsa->q);
275 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000276 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000277 break;
278 }
Damien Miller874d77b2000-10-14 16:23:11 +1100279 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000280 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000281 error("do_convert_private_ssh2_from_blob: "
282 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100283 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000284
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000285 /* try the key */
286 key_sign(key, &sig, &slen, data, sizeof(data));
287 key_verify(key, sig, slen, data, sizeof(data));
288 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100289 return key;
290}
291
Ben Lindstrombba81212001-06-25 05:01:22 +0000292static void
Damien Millereba71ba2000-04-29 23:57:08 +1000293do_convert_from_ssh2(struct passwd *pw)
294{
295 Key *k;
296 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000297 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000298 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000299 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000300 char encoded[8096];
301 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100302 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000303 FILE *fp;
304
305 if (!have_identity)
306 ask_filename(pw, "Enter file in which the key is");
307 if (stat(identity_file, &st) < 0) {
308 perror(identity_file);
309 exit(1);
310 }
311 fp = fopen(identity_file, "r");
312 if (fp == NULL) {
313 perror(identity_file);
314 exit(1);
315 }
316 encoded[0] = '\0';
317 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000318 if (!(p = strchr(line, '\n'))) {
319 fprintf(stderr, "input line too long.\n");
320 exit(1);
321 }
322 if (p > line && p[-1] == '\\')
323 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000324 if (strncmp(line, "----", 4) == 0 ||
325 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100326 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
327 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000328 if (strstr(line, " END ") != NULL) {
329 break;
330 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000331 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000332 continue;
333 }
Damien Miller30c3d422000-05-09 11:02:59 +1000334 if (escaped) {
335 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000336 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000337 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000338 }
339 *p = '\0';
340 strlcat(encoded, line, sizeof(encoded));
341 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000342 len = strlen(encoded);
343 if (((len % 4) == 3) &&
344 (encoded[len-1] == '=') &&
345 (encoded[len-2] == '=') &&
346 (encoded[len-3] == '='))
347 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100348 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000349 if (blen < 0) {
350 fprintf(stderr, "uudecode failed.\n");
351 exit(1);
352 }
Damien Miller874d77b2000-10-14 16:23:11 +1100353 k = private ?
354 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100355 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100356 if (k == NULL) {
357 fprintf(stderr, "decode blob failed.\n");
358 exit(1);
359 }
360 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000361 (k->type == KEY_DSA ?
362 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
363 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100364 key_write(k, stdout);
365 if (!ok) {
366 fprintf(stderr, "key write failed");
367 exit(1);
368 }
Damien Millereba71ba2000-04-29 23:57:08 +1000369 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100370 if (!private)
371 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000372 fclose(fp);
373 exit(0);
374}
375
Ben Lindstrombba81212001-06-25 05:01:22 +0000376static void
Damien Millereba71ba2000-04-29 23:57:08 +1000377do_print_public(struct passwd *pw)
378{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000379 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000380 struct stat st;
381
382 if (!have_identity)
383 ask_filename(pw, "Enter file in which the key is");
384 if (stat(identity_file, &st) < 0) {
385 perror(identity_file);
386 exit(1);
387 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000388 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000389 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000390 fprintf(stderr, "load failed\n");
391 exit(1);
392 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000393 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000394 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000395 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000396 fprintf(stdout, "\n");
397 exit(0);
398}
399
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000400#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000401static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000402do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000403{
Ben Lindstromcd392282001-07-04 03:44:03 +0000404 Key *prv = NULL;
405 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000406 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000407
408 if (!have_identity)
409 ask_filename(pw, "Enter file in which the key is");
410 if (stat(identity_file, &st) < 0) {
411 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000412 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000413 }
414 prv = load_identity(identity_file);
415 if (prv == NULL) {
416 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000417 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000418 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000419 ret = sc_put_key(prv, sc_reader_id);
420 key_free(prv);
421 if (ret < 0)
422 exit(1);
Damien Miller996acd22003-04-09 20:59:48 +1000423 logit("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000424 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000425}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000426
427static void
428do_download(struct passwd *pw, const char *sc_reader_id)
429{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000430 Key **keys = NULL;
431 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000432
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000433 keys = sc_get_keys(sc_reader_id, NULL);
434 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000435 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000436 for (i = 0; keys[i]; i++) {
437 key_write(keys[i], stdout);
438 key_free(keys[i]);
439 fprintf(stdout, "\n");
440 }
441 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000442 exit(0);
443}
Ben Lindstromffce1472001-08-06 21:57:31 +0000444#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000445
Ben Lindstrombba81212001-06-25 05:01:22 +0000446static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100447do_fingerprint(struct passwd *pw)
448{
Damien Miller98c7ad62000-03-09 21:27:49 +1100449 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000450 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000451 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000452 int i, skip = 0, num = 1, invalid = 1;
453 enum fp_rep rep;
454 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100455 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100456
Ben Lindstromd0fca422001-03-26 13:44:06 +0000457 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
458 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000459
Damien Miller95def091999-11-25 00:26:21 +1100460 if (!have_identity)
461 ask_filename(pw, "Enter file in which the key is");
462 if (stat(identity_file, &st) < 0) {
463 perror(identity_file);
464 exit(1);
465 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000466 public = key_load_public(identity_file, &comment);
467 if (public != NULL) {
468 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000469 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100470 key_free(public);
471 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000472 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100473 exit(0);
474 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000475 if (comment)
476 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100477
478 f = fopen(identity_file, "r");
479 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100480 while (fgets(line, sizeof(line), f)) {
481 i = strlen(line) - 1;
482 if (line[i] != '\n') {
483 error("line %d too long: %.40s...", num, line);
484 skip = 1;
485 continue;
Damien Miller95def091999-11-25 00:26:21 +1100486 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100487 num++;
488 if (skip) {
489 skip = 0;
490 continue;
491 }
492 line[i] = '\0';
493
494 /* Skip leading whitespace, empty and comment lines. */
495 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
496 ;
497 if (!*cp || *cp == '\n' || *cp == '#')
498 continue ;
499 i = strtol(cp, &ep, 10);
500 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
501 int quoted = 0;
502 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000503 for (; *cp && (quoted || (*cp != ' ' &&
504 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100505 if (*cp == '\\' && cp[1] == '"')
506 cp++; /* Skip both */
507 else if (*cp == '"')
508 quoted = !quoted;
509 }
510 if (!*cp)
511 continue;
512 *cp++ = '\0';
513 }
514 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000515 public = key_new(KEY_RSA1);
516 if (key_read(public, &cp) != 1) {
517 cp = ep;
518 key_free(public);
519 public = key_new(KEY_UNSPEC);
520 if (key_read(public, &cp) != 1) {
521 key_free(public);
522 continue;
523 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100524 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000525 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000526 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000527 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000528 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000529 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000530 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000531 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100532 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100533 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100534 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100535 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100536 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100537 exit(1);
538 }
Damien Miller95def091999-11-25 00:26:21 +1100539 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100540}
541
Damien Miller95def091999-11-25 00:26:21 +1100542/*
543 * Perform changing a passphrase. The argument is the passwd structure
544 * for the current user.
545 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000546static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100547do_change_passphrase(struct passwd *pw)
548{
Damien Miller95def091999-11-25 00:26:21 +1100549 char *comment;
550 char *old_passphrase, *passphrase1, *passphrase2;
551 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000552 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100553
Damien Miller95def091999-11-25 00:26:21 +1100554 if (!have_identity)
555 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100556 if (stat(identity_file, &st) < 0) {
557 perror(identity_file);
558 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 }
Damien Miller95def091999-11-25 00:26:21 +1100560 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000561 private = key_load_private(identity_file, "", &comment);
562 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100563 if (identity_passphrase)
564 old_passphrase = xstrdup(identity_passphrase);
565 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000566 old_passphrase =
567 read_passphrase("Enter old passphrase: ",
568 RP_ALLOW_STDIN);
569 private = key_load_private(identity_file, old_passphrase,
570 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000571 memset(old_passphrase, 0, strlen(old_passphrase));
572 xfree(old_passphrase);
573 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100574 printf("Bad passphrase.\n");
575 exit(1);
576 }
Damien Miller95def091999-11-25 00:26:21 +1100577 }
578 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579
Damien Miller95def091999-11-25 00:26:21 +1100580 /* Ask the new passphrase (twice). */
581 if (identity_new_passphrase) {
582 passphrase1 = xstrdup(identity_new_passphrase);
583 passphrase2 = NULL;
584 } else {
585 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000586 read_passphrase("Enter new passphrase (empty for no "
587 "passphrase): ", RP_ALLOW_STDIN);
588 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100589 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100590
591 /* Verify that they are the same. */
592 if (strcmp(passphrase1, passphrase2) != 0) {
593 memset(passphrase1, 0, strlen(passphrase1));
594 memset(passphrase2, 0, strlen(passphrase2));
595 xfree(passphrase1);
596 xfree(passphrase2);
597 printf("Pass phrases do not match. Try again.\n");
598 exit(1);
599 }
600 /* Destroy the other copy. */
601 memset(passphrase2, 0, strlen(passphrase2));
602 xfree(passphrase2);
603 }
604
605 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000606 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000607 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100608 memset(passphrase1, 0, strlen(passphrase1));
609 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000610 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100611 xfree(comment);
612 exit(1);
613 }
614 /* Destroy the passphrase and the copy of the key in memory. */
615 memset(passphrase1, 0, strlen(passphrase1));
616 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000617 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100618 xfree(comment);
619
620 printf("Your identification has been saved with the new passphrase.\n");
621 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622}
623
Damien Miller37876e92003-05-15 10:19:46 +1000624#ifdef DNS
625/*
626 * Print the SSHFP RR.
627 */
628static void
629do_print_resource_record(struct passwd *pw, char *hostname)
630{
631 Key *public;
632 char *comment = NULL;
633 struct stat st;
634
635 if (!have_identity)
636 ask_filename(pw, "Enter file in which the key is");
637 if (stat(identity_file, &st) < 0) {
638 perror(identity_file);
639 exit(1);
640 }
641 public = key_load_public(identity_file, &comment);
642 if (public != NULL) {
643 export_dns_rr(hostname, public, stdout, print_generic);
644 key_free(public);
645 xfree(comment);
646 exit(0);
647 }
648 if (comment)
649 xfree(comment);
650
651 printf("failed to read v2 public key from %s.\n", identity_file);
652 exit(1);
653}
654#endif /* DNS */
655
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#ifdef DNS
774 fprintf(stderr, " -r hostname Print DNS resource record.\n");
775#endif /* DNS */
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000776#ifdef SMARTCARD
777 fprintf(stderr, " -D reader Download public key from smartcard.\n");
778 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
779#endif /* SMARTCARD */
780
Damien Miller95def091999-11-25 00:26:21 +1100781 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100782}
783
Damien Miller95def091999-11-25 00:26:21 +1100784/*
785 * Main program for key management.
786 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787int
788main(int ac, char **av)
789{
Damien Millera41c8b12002-01-22 23:05:08 +1100790 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000791 char *reader_id = NULL;
Damien Miller37876e92003-05-15 10:19:46 +1000792 char *resource_record_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000793 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100794 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100795 struct stat st;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000796 int opt, type, fd, download = 0;
Damien Miller95def091999-11-25 00:26:21 +1100797 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100798
Damien Miller95def091999-11-25 00:26:21 +1100799 extern int optind;
800 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000802 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000803
Damien Millerd3a18572000-06-07 19:55:44 +1000804 SSLeay_add_all_algorithms();
Kevin Steves3a881912002-07-20 19:05:40 +0000805 init_rng();
806 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +1000807
Damien Miller5428f641999-11-25 11:54:57 +1100808 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100809 pw = getpwuid(getuid());
810 if (!pw) {
811 printf("You don't exist, go away!\n");
812 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000813 }
Damien Millereba71ba2000-04-29 23:57:08 +1000814 if (gethostname(hostname, sizeof(hostname)) < 0) {
815 perror("gethostname");
816 exit(1);
817 }
Damien Miller5428f641999-11-25 11:54:57 +1100818
Damien Miller37876e92003-05-15 10:19:46 +1000819 while ((opt = getopt(ac, av, "degiqpclBRxXyb:f:t:U:D:P:N:C:r:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100820 switch (opt) {
821 case 'b':
822 bits = atoi(optarg);
823 if (bits < 512 || bits > 32768) {
824 printf("Bits has bad value.\n");
825 exit(1);
826 }
827 break;
Damien Miller95def091999-11-25 00:26:21 +1100828 case 'l':
829 print_fingerprint = 1;
830 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000831 case 'B':
832 print_bubblebabble = 1;
833 break;
Damien Miller95def091999-11-25 00:26:21 +1100834 case 'p':
835 change_passphrase = 1;
836 break;
Damien Miller95def091999-11-25 00:26:21 +1100837 case 'c':
838 change_comment = 1;
839 break;
Damien Miller95def091999-11-25 00:26:21 +1100840 case 'f':
841 strlcpy(identity_file, optarg, sizeof(identity_file));
842 have_identity = 1;
843 break;
Damien Miller37876e92003-05-15 10:19:46 +1000844 case 'g':
845 print_generic = 1;
846 break;
Damien Miller95def091999-11-25 00:26:21 +1100847 case 'P':
848 identity_passphrase = optarg;
849 break;
Damien Miller95def091999-11-25 00:26:21 +1100850 case 'N':
851 identity_new_passphrase = optarg;
852 break;
Damien Miller95def091999-11-25 00:26:21 +1100853 case 'C':
854 identity_comment = optarg;
855 break;
Damien Miller95def091999-11-25 00:26:21 +1100856 case 'q':
857 quiet = 1;
858 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000859 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100860 /* unused */
861 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000862 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000863 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000864 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000865 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000866 convert_to_ssh2 = 1;
867 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000868 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000869 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000870 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000871 convert_from_ssh2 = 1;
872 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000873 case 'y':
874 print_public = 1;
875 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000876 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100877 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000878 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100879 case 't':
880 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100881 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000882 case 'D':
883 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000884 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000885 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000886 break;
Damien Miller37876e92003-05-15 10:19:46 +1000887 case 'r':
888 resource_record_hostname = optarg;
889 break;
Damien Miller95def091999-11-25 00:26:21 +1100890 case '?':
891 default:
892 usage();
893 }
894 }
895 if (optind < ac) {
896 printf("Too many arguments.\n");
897 usage();
898 }
899 if (change_passphrase && change_comment) {
900 printf("Can only have one of -p and -c.\n");
901 usage();
902 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000903 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100904 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100905 if (change_passphrase)
906 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100907 if (change_comment)
908 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +0000909 if (convert_to_ssh2)
910 do_convert_to_ssh2(pw);
911 if (convert_from_ssh2)
912 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000913 if (print_public)
914 do_print_public(pw);
Damien Miller37876e92003-05-15 10:19:46 +1000915 if (resource_record_hostname != NULL) {
916#ifdef DNS
917 do_print_resource_record(pw, resource_record_hostname);
918#else /* DNS */
919 fatal("no DNS support.");
920#endif /* DNS */
921 }
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000922 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000923#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000924 if (download)
925 do_download(pw, reader_id);
926 else
927 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000928#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000929 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000930#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000931 }
Damien Miller95def091999-11-25 00:26:21 +1100932
933 arc4random_stir();
934
Damien Miller154dda72002-01-22 23:08:16 +1100935 if (key_type_name == NULL) {
936 printf("You must specify a key type (-t).\n");
937 usage();
938 }
Damien Millere39cacc2000-11-29 12:18:44 +1100939 type = key_type_from_name(key_type_name);
940 if (type == KEY_UNSPEC) {
941 fprintf(stderr, "unknown key type %s\n", key_type_name);
942 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000943 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100944 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100945 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100946 private = key_generate(type, bits);
947 if (private == NULL) {
948 fprintf(stderr, "key_generate failed");
949 exit(1);
950 }
951 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100952
953 if (!have_identity)
954 ask_filename(pw, "Enter file in which to save the key");
955
956 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000957 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100958 if (strstr(identity_file, dotsshdir) != NULL &&
959 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000960 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100961 error("Could not create directory '%s'.", dotsshdir);
962 else if (!quiet)
963 printf("Created directory '%s'.\n", dotsshdir);
964 }
965 /* If the file already exists, ask the user to confirm. */
966 if (stat(identity_file, &st) >= 0) {
967 char yesno[3];
968 printf("%s already exists.\n", identity_file);
969 printf("Overwrite (y/n)? ");
970 fflush(stdout);
971 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
972 exit(1);
973 if (yesno[0] != 'y' && yesno[0] != 'Y')
974 exit(1);
975 }
976 /* Ask for a passphrase (twice). */
977 if (identity_passphrase)
978 passphrase1 = xstrdup(identity_passphrase);
979 else if (identity_new_passphrase)
980 passphrase1 = xstrdup(identity_new_passphrase);
981 else {
982passphrase_again:
983 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000984 read_passphrase("Enter passphrase (empty for no "
985 "passphrase): ", RP_ALLOW_STDIN);
986 passphrase2 = read_passphrase("Enter same passphrase again: ",
987 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100988 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000989 /*
990 * The passphrases do not match. Clear them and
991 * retry.
992 */
Damien Miller95def091999-11-25 00:26:21 +1100993 memset(passphrase1, 0, strlen(passphrase1));
994 memset(passphrase2, 0, strlen(passphrase2));
995 xfree(passphrase1);
996 xfree(passphrase2);
997 printf("Passphrases do not match. Try again.\n");
998 goto passphrase_again;
999 }
1000 /* Clear the other copy of the passphrase. */
1001 memset(passphrase2, 0, strlen(passphrase2));
1002 xfree(passphrase2);
1003 }
1004
Damien Miller95def091999-11-25 00:26:21 +11001005 if (identity_comment) {
1006 strlcpy(comment, identity_comment, sizeof(comment));
1007 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001008 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001009 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1010 }
1011
1012 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001013 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001014 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001015 memset(passphrase1, 0, strlen(passphrase1));
1016 xfree(passphrase1);
1017 exit(1);
1018 }
1019 /* Clear the passphrase. */
1020 memset(passphrase1, 0, strlen(passphrase1));
1021 xfree(passphrase1);
1022
1023 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001024 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001025 arc4random_stir();
1026
1027 if (!quiet)
1028 printf("Your identification has been saved in %s.\n", identity_file);
1029
Damien Miller95def091999-11-25 00:26:21 +11001030 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001031 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1032 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001033 printf("Could not save your public key in %s\n", identity_file);
1034 exit(1);
1035 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001036 f = fdopen(fd, "w");
1037 if (f == NULL) {
1038 printf("fdopen %s failed", identity_file);
1039 exit(1);
1040 }
Damien Millereba71ba2000-04-29 23:57:08 +10001041 if (!key_write(public, f))
1042 fprintf(stderr, "write key failed");
1043 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001044 fclose(f);
1045
1046 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001047 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001048 printf("Your public key has been saved in %s.\n",
1049 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001050 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001051 printf("%s %s\n", fp, comment);
1052 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001053 }
Damien Millereba71ba2000-04-29 23:57:08 +10001054
1055 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001056 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057}