blob: c93d70bad7dc2a68256ca8b620c46569e635b96b [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 Millered12a262003-05-15 13:37:43 +100015RCSID("$OpenBSD: ssh-keygen.c,v 1.106 2003/05/15 03:10:52 djm 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
Damien Millered12a262003-05-15 13:37:43 +100034#ifdef DNS
35#include "dns.h"
36#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000037
Damien Millereba71ba2000-04-29 23:57:08 +100038/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039int bits = 1024;
40
Damien Miller5428f641999-11-25 11:54:57 +110041/*
42 * Flag indicating that we just want to change the passphrase. This can be
43 * set on the command line.
44 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045int change_passphrase = 0;
46
Damien Miller5428f641999-11-25 11:54:57 +110047/*
48 * Flag indicating that we just want to change the comment. This can be set
49 * on the command line.
50 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051int change_comment = 0;
52
53int quiet = 0;
54
Damien Miller10f6f6b1999-11-17 17:29:08 +110055/* Flag indicating that we just want to see the key fingerprint */
56int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000057int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110058
Damien Miller431f66b1999-11-21 18:31:57 +110059/* The identity file name, given on the command line or entered by the user. */
60char identity_file[1024];
61int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63/* This is set to the passphrase if given on the command line. */
64char *identity_passphrase = NULL;
65
66/* This is set to the new passphrase if given on the command line. */
67char *identity_new_passphrase = NULL;
68
69/* This is set to the new comment if given on the command line. */
70char *identity_comment = NULL;
71
Damien Millereba71ba2000-04-29 23:57:08 +100072/* Dump public key file in format used by real and the original SSH 2 */
73int convert_to_ssh2 = 0;
74int convert_from_ssh2 = 0;
75int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +100076int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110077
Damien Millera41c8b12002-01-22 23:05:08 +110078char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100079
Damien Miller431f66b1999-11-21 18:31:57 +110080/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110081#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110082extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000083#else
84char *__progname;
85#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Millereba71ba2000-04-29 23:57:08 +100087char hostname[MAXHOSTNAMELEN];
88
Ben Lindstrombba81212001-06-25 05:01:22 +000089static void
Damien Miller431f66b1999-11-21 18:31:57 +110090ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091{
Damien Miller95def091999-11-25 00:26:21 +110092 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110093 char *name = NULL;
94
Damien Miller993dd552002-02-19 15:22:47 +110095 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +000096 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller993dd552002-02-19 15:22:47 +110097 else
98 switch (key_type_from_name(key_type_name)) {
99 case KEY_RSA1:
100 name = _PATH_SSH_CLIENT_IDENTITY;
101 break;
102 case KEY_DSA:
103 name = _PATH_SSH_CLIENT_ID_DSA;
104 break;
105 case KEY_RSA:
106 name = _PATH_SSH_CLIENT_ID_RSA;
107 break;
108 default:
109 fprintf(stderr, "bad key type");
110 exit(1);
111 break;
112 }
113
Damien Millere39cacc2000-11-29 12:18:44 +1100114 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000115 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100116 if (fgets(buf, sizeof(buf), stdin) == NULL)
117 exit(1);
118 if (strchr(buf, '\n'))
119 *strchr(buf, '\n') = 0;
120 if (strcmp(buf, "") != 0)
121 strlcpy(identity_file, buf, sizeof(identity_file));
122 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100123}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124
Ben Lindstrombba81212001-06-25 05:01:22 +0000125static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000126load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000127{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000128 char *pass;
129 Key *prv;
130
Ben Lindstroma3700052001-04-05 23:26:32 +0000131 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000132 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000133 if (identity_passphrase)
134 pass = xstrdup(identity_passphrase);
135 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000136 pass = read_passphrase("Enter passphrase: ",
137 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000138 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000139 memset(pass, 0, strlen(pass));
140 xfree(pass);
141 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000142 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000143}
144
Damien Miller874d77b2000-10-14 16:23:11 +1100145#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000146#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100147#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000148#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000149
Ben Lindstrombba81212001-06-25 05:01:22 +0000150static void
Damien Millereba71ba2000-04-29 23:57:08 +1000151do_convert_to_ssh2(struct passwd *pw)
152{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000153 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000154 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000155 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000156 struct stat st;
157
158 if (!have_identity)
159 ask_filename(pw, "Enter file in which the key is");
160 if (stat(identity_file, &st) < 0) {
161 perror(identity_file);
162 exit(1);
163 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000164 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000165 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000166 fprintf(stderr, "load failed\n");
167 exit(1);
168 }
Damien Millereba71ba2000-04-29 23:57:08 +1000169 }
Damien Millerdb274722003-05-14 13:45:22 +1000170 if (k->type == KEY_RSA1) {
171 fprintf(stderr, "version 1 keys are not supported\n");
172 exit(1);
173 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000174 if (key_to_blob(k, &blob, &len) <= 0) {
175 fprintf(stderr, "key_to_blob failed\n");
176 exit(1);
177 }
Damien Miller874d77b2000-10-14 16:23:11 +1100178 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000179 fprintf(stdout,
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000180 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000181 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000182 pw->pw_name, hostname);
183 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100184 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000185 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000186 xfree(blob);
187 exit(0);
188}
189
Ben Lindstrombba81212001-06-25 05:01:22 +0000190static void
Damien Miller874d77b2000-10-14 16:23:11 +1100191buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
192{
193 int bits = buffer_get_int(b);
194 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000195
Damien Miller874d77b2000-10-14 16:23:11 +1100196 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000197 fatal("buffer_get_bignum_bits: input buffer too small: "
198 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100199 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100200 buffer_consume(b, bytes);
201}
202
Ben Lindstrombba81212001-06-25 05:01:22 +0000203static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000204do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100205{
206 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100207 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100208 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000209 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000210 int magic, rlen, ktype, i1, i2, i3, i4;
211 u_int slen;
212 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100213
214 buffer_init(&b);
215 buffer_append(&b, blob, blen);
216
217 magic = buffer_get_int(&b);
218 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
219 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
220 buffer_free(&b);
221 return NULL;
222 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000223 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100224 type = buffer_get_string(&b, NULL);
225 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000226 i2 = buffer_get_int(&b);
227 i3 = buffer_get_int(&b);
228 i4 = buffer_get_int(&b);
229 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100230 if (strcmp(cipher, "none") != 0) {
231 error("unsupported cipher %s", cipher);
232 xfree(cipher);
233 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000234 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100235 return NULL;
236 }
237 xfree(cipher);
238
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000239 if (strstr(type, "dsa")) {
240 ktype = KEY_DSA;
241 } else if (strstr(type, "rsa")) {
242 ktype = KEY_RSA;
243 } else {
244 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100245 return NULL;
246 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000247 key = key_new_private(ktype);
248 xfree(type);
249
250 switch (key->type) {
251 case KEY_DSA:
252 buffer_get_bignum_bits(&b, key->dsa->p);
253 buffer_get_bignum_bits(&b, key->dsa->g);
254 buffer_get_bignum_bits(&b, key->dsa->q);
255 buffer_get_bignum_bits(&b, key->dsa->pub_key);
256 buffer_get_bignum_bits(&b, key->dsa->priv_key);
257 break;
258 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000259 e = buffer_get_char(&b);
260 debug("e %lx", e);
261 if (e < 30) {
262 e <<= 8;
263 e += buffer_get_char(&b);
264 debug("e %lx", e);
265 e <<= 8;
266 e += buffer_get_char(&b);
267 debug("e %lx", e);
268 }
269 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000270 buffer_free(&b);
271 key_free(key);
272 return NULL;
273 }
274 buffer_get_bignum_bits(&b, key->rsa->d);
275 buffer_get_bignum_bits(&b, key->rsa->n);
276 buffer_get_bignum_bits(&b, key->rsa->iqmp);
277 buffer_get_bignum_bits(&b, key->rsa->q);
278 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000279 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000280 break;
281 }
Damien Miller874d77b2000-10-14 16:23:11 +1100282 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000283 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000284 error("do_convert_private_ssh2_from_blob: "
285 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100286 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000287
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000288 /* try the key */
289 key_sign(key, &sig, &slen, data, sizeof(data));
290 key_verify(key, sig, slen, data, sizeof(data));
291 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100292 return key;
293}
294
Ben Lindstrombba81212001-06-25 05:01:22 +0000295static void
Damien Millereba71ba2000-04-29 23:57:08 +1000296do_convert_from_ssh2(struct passwd *pw)
297{
298 Key *k;
299 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000300 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000301 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000302 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000303 char encoded[8096];
304 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100305 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000306 FILE *fp;
307
308 if (!have_identity)
309 ask_filename(pw, "Enter file in which the key is");
310 if (stat(identity_file, &st) < 0) {
311 perror(identity_file);
312 exit(1);
313 }
314 fp = fopen(identity_file, "r");
315 if (fp == NULL) {
316 perror(identity_file);
317 exit(1);
318 }
319 encoded[0] = '\0';
320 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000321 if (!(p = strchr(line, '\n'))) {
322 fprintf(stderr, "input line too long.\n");
323 exit(1);
324 }
325 if (p > line && p[-1] == '\\')
326 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000327 if (strncmp(line, "----", 4) == 0 ||
328 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100329 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
330 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000331 if (strstr(line, " END ") != NULL) {
332 break;
333 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000334 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000335 continue;
336 }
Damien Miller30c3d422000-05-09 11:02:59 +1000337 if (escaped) {
338 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000339 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000340 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000341 }
342 *p = '\0';
343 strlcat(encoded, line, sizeof(encoded));
344 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000345 len = strlen(encoded);
346 if (((len % 4) == 3) &&
347 (encoded[len-1] == '=') &&
348 (encoded[len-2] == '=') &&
349 (encoded[len-3] == '='))
350 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100351 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000352 if (blen < 0) {
353 fprintf(stderr, "uudecode failed.\n");
354 exit(1);
355 }
Damien Miller874d77b2000-10-14 16:23:11 +1100356 k = private ?
357 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100358 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100359 if (k == NULL) {
360 fprintf(stderr, "decode blob failed.\n");
361 exit(1);
362 }
363 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000364 (k->type == KEY_DSA ?
365 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
366 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100367 key_write(k, stdout);
368 if (!ok) {
369 fprintf(stderr, "key write failed");
370 exit(1);
371 }
Damien Millereba71ba2000-04-29 23:57:08 +1000372 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100373 if (!private)
374 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000375 fclose(fp);
376 exit(0);
377}
378
Ben Lindstrombba81212001-06-25 05:01:22 +0000379static void
Damien Millereba71ba2000-04-29 23:57:08 +1000380do_print_public(struct passwd *pw)
381{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000382 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000383 struct stat st;
384
385 if (!have_identity)
386 ask_filename(pw, "Enter file in which the key is");
387 if (stat(identity_file, &st) < 0) {
388 perror(identity_file);
389 exit(1);
390 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000391 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000392 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000393 fprintf(stderr, "load failed\n");
394 exit(1);
395 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000396 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000397 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000398 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000399 fprintf(stdout, "\n");
400 exit(0);
401}
402
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000403#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000404static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000405do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000406{
Ben Lindstromcd392282001-07-04 03:44:03 +0000407 Key *prv = NULL;
408 struct stat st;
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000409 int ret;
Ben Lindstromcd392282001-07-04 03:44:03 +0000410
411 if (!have_identity)
412 ask_filename(pw, "Enter file in which the key is");
413 if (stat(identity_file, &st) < 0) {
414 perror(identity_file);
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000415 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000416 }
417 prv = load_identity(identity_file);
418 if (prv == NULL) {
419 error("load failed");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000420 exit(1);
Ben Lindstromcd392282001-07-04 03:44:03 +0000421 }
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000422 ret = sc_put_key(prv, sc_reader_id);
423 key_free(prv);
424 if (ret < 0)
425 exit(1);
Damien Miller996acd22003-04-09 20:59:48 +1000426 logit("loading key done");
Ben Lindstrom70e3ad82002-03-22 03:33:43 +0000427 exit(0);
Ben Lindstromcd392282001-07-04 03:44:03 +0000428}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000429
430static void
431do_download(struct passwd *pw, const char *sc_reader_id)
432{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000433 Key **keys = NULL;
434 int i;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000435
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000436 keys = sc_get_keys(sc_reader_id, NULL);
437 if (keys == NULL)
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000438 fatal("cannot read public key from smartcard");
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000439 for (i = 0; keys[i]; i++) {
440 key_write(keys[i], stdout);
441 key_free(keys[i]);
442 fprintf(stdout, "\n");
443 }
444 xfree(keys);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000445 exit(0);
446}
Ben Lindstromffce1472001-08-06 21:57:31 +0000447#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000448
Ben Lindstrombba81212001-06-25 05:01:22 +0000449static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100450do_fingerprint(struct passwd *pw)
451{
Damien Miller98c7ad62000-03-09 21:27:49 +1100452 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000453 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000454 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000455 int i, skip = 0, num = 1, invalid = 1;
456 enum fp_rep rep;
457 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100458 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100459
Ben Lindstromd0fca422001-03-26 13:44:06 +0000460 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
461 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000462
Damien Miller95def091999-11-25 00:26:21 +1100463 if (!have_identity)
464 ask_filename(pw, "Enter file in which the key is");
465 if (stat(identity_file, &st) < 0) {
466 perror(identity_file);
467 exit(1);
468 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000469 public = key_load_public(identity_file, &comment);
470 if (public != NULL) {
471 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000472 printf("%u %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 key_free(public);
474 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000475 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100476 exit(0);
477 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000478 if (comment)
479 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100480
481 f = fopen(identity_file, "r");
482 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100483 while (fgets(line, sizeof(line), f)) {
484 i = strlen(line) - 1;
485 if (line[i] != '\n') {
486 error("line %d too long: %.40s...", num, line);
487 skip = 1;
488 continue;
Damien Miller95def091999-11-25 00:26:21 +1100489 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100490 num++;
491 if (skip) {
492 skip = 0;
493 continue;
494 }
495 line[i] = '\0';
496
497 /* Skip leading whitespace, empty and comment lines. */
498 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
499 ;
500 if (!*cp || *cp == '\n' || *cp == '#')
501 continue ;
502 i = strtol(cp, &ep, 10);
503 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
504 int quoted = 0;
505 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000506 for (; *cp && (quoted || (*cp != ' ' &&
507 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100508 if (*cp == '\\' && cp[1] == '"')
509 cp++; /* Skip both */
510 else if (*cp == '"')
511 quoted = !quoted;
512 }
513 if (!*cp)
514 continue;
515 *cp++ = '\0';
516 }
517 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000518 public = key_new(KEY_RSA1);
519 if (key_read(public, &cp) != 1) {
520 cp = ep;
521 key_free(public);
522 public = key_new(KEY_UNSPEC);
523 if (key_read(public, &cp) != 1) {
524 key_free(public);
525 continue;
526 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100527 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000528 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000529 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000530 printf("%u %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000531 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000532 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000533 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000534 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100535 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100536 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100537 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100538 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100539 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100540 exit(1);
541 }
Damien Miller95def091999-11-25 00:26:21 +1100542 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100543}
544
Damien Miller95def091999-11-25 00:26:21 +1100545/*
546 * Perform changing a passphrase. The argument is the passwd structure
547 * for the current user.
548 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000549static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100550do_change_passphrase(struct passwd *pw)
551{
Damien Miller95def091999-11-25 00:26:21 +1100552 char *comment;
553 char *old_passphrase, *passphrase1, *passphrase2;
554 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000555 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100556
Damien Miller95def091999-11-25 00:26:21 +1100557 if (!have_identity)
558 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100559 if (stat(identity_file, &st) < 0) {
560 perror(identity_file);
561 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562 }
Damien Miller95def091999-11-25 00:26:21 +1100563 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000564 private = key_load_private(identity_file, "", &comment);
565 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100566 if (identity_passphrase)
567 old_passphrase = xstrdup(identity_passphrase);
568 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000569 old_passphrase =
570 read_passphrase("Enter old passphrase: ",
571 RP_ALLOW_STDIN);
572 private = key_load_private(identity_file, old_passphrase,
573 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000574 memset(old_passphrase, 0, strlen(old_passphrase));
575 xfree(old_passphrase);
576 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100577 printf("Bad passphrase.\n");
578 exit(1);
579 }
Damien Miller95def091999-11-25 00:26:21 +1100580 }
581 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
Damien Miller95def091999-11-25 00:26:21 +1100583 /* Ask the new passphrase (twice). */
584 if (identity_new_passphrase) {
585 passphrase1 = xstrdup(identity_new_passphrase);
586 passphrase2 = NULL;
587 } else {
588 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000589 read_passphrase("Enter new passphrase (empty for no "
590 "passphrase): ", RP_ALLOW_STDIN);
591 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100592 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100593
594 /* Verify that they are the same. */
595 if (strcmp(passphrase1, passphrase2) != 0) {
596 memset(passphrase1, 0, strlen(passphrase1));
597 memset(passphrase2, 0, strlen(passphrase2));
598 xfree(passphrase1);
599 xfree(passphrase2);
600 printf("Pass phrases do not match. Try again.\n");
601 exit(1);
602 }
603 /* Destroy the other copy. */
604 memset(passphrase2, 0, strlen(passphrase2));
605 xfree(passphrase2);
606 }
607
608 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000609 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000610 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100611 memset(passphrase1, 0, strlen(passphrase1));
612 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000613 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100614 xfree(comment);
615 exit(1);
616 }
617 /* Destroy the passphrase and the copy of the key in memory. */
618 memset(passphrase1, 0, strlen(passphrase1));
619 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000620 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100621 xfree(comment);
622
623 printf("Your identification has been saved with the new passphrase.\n");
624 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625}
626
Damien Miller37876e92003-05-15 10:19:46 +1000627#ifdef DNS
628/*
629 * Print the SSHFP RR.
630 */
631static void
632do_print_resource_record(struct passwd *pw, char *hostname)
633{
634 Key *public;
635 char *comment = NULL;
636 struct stat st;
637
638 if (!have_identity)
639 ask_filename(pw, "Enter file in which the key is");
640 if (stat(identity_file, &st) < 0) {
641 perror(identity_file);
642 exit(1);
643 }
644 public = key_load_public(identity_file, &comment);
645 if (public != NULL) {
646 export_dns_rr(hostname, public, stdout, print_generic);
647 key_free(public);
648 xfree(comment);
649 exit(0);
650 }
651 if (comment)
652 xfree(comment);
653
654 printf("failed to read v2 public key from %s.\n", identity_file);
655 exit(1);
656}
657#endif /* DNS */
658
Damien Miller95def091999-11-25 00:26:21 +1100659/*
660 * Change the comment of a private key file.
661 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000662static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663do_change_comment(struct passwd *pw)
664{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000665 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000666 Key *private;
667 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100668 struct stat st;
669 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000670 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671
Damien Miller95def091999-11-25 00:26:21 +1100672 if (!have_identity)
673 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100674 if (stat(identity_file, &st) < 0) {
675 perror(identity_file);
676 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000678 private = key_load_private(identity_file, "", &comment);
679 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100680 if (identity_passphrase)
681 passphrase = xstrdup(identity_passphrase);
682 else if (identity_new_passphrase)
683 passphrase = xstrdup(identity_new_passphrase);
684 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000685 passphrase = read_passphrase("Enter passphrase: ",
686 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100687 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000688 private = key_load_private(identity_file, passphrase, &comment);
689 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100690 memset(passphrase, 0, strlen(passphrase));
691 xfree(passphrase);
692 printf("Bad passphrase.\n");
693 exit(1);
694 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000695 } else {
696 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100697 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000698 if (private->type != KEY_RSA1) {
699 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
700 key_free(private);
701 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100702 }
Damien Miller95def091999-11-25 00:26:21 +1100703 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704
Damien Miller95def091999-11-25 00:26:21 +1100705 if (identity_comment) {
706 strlcpy(new_comment, identity_comment, sizeof(new_comment));
707 } else {
708 printf("Enter new comment: ");
709 fflush(stdout);
710 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
711 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000712 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100713 exit(1);
714 }
Damien Miller95def091999-11-25 00:26:21 +1100715 if (strchr(new_comment, '\n'))
716 *strchr(new_comment, '\n') = 0;
717 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718
Damien Miller95def091999-11-25 00:26:21 +1100719 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000720 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000721 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100722 memset(passphrase, 0, strlen(passphrase));
723 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000724 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100725 xfree(comment);
726 exit(1);
727 }
Damien Miller95def091999-11-25 00:26:21 +1100728 memset(passphrase, 0, strlen(passphrase));
729 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000730 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000731 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732
Damien Miller95def091999-11-25 00:26:21 +1100733 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000734 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
735 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100736 printf("Could not save your public key in %s\n", identity_file);
737 exit(1);
738 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000739 f = fdopen(fd, "w");
740 if (f == NULL) {
741 printf("fdopen %s failed", identity_file);
742 exit(1);
743 }
Damien Millereba71ba2000-04-29 23:57:08 +1000744 if (!key_write(public, f))
745 fprintf(stderr, "write key failed");
746 key_free(public);
747 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100748 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749
Damien Miller95def091999-11-25 00:26:21 +1100750 xfree(comment);
751
752 printf("The comment in your key file has been changed.\n");
753 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000754}
755
Ben Lindstrombba81212001-06-25 05:01:22 +0000756static void
Damien Miller431f66b1999-11-21 18:31:57 +1100757usage(void)
758{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000759 fprintf(stderr, "Usage: %s [options]\n", __progname);
760 fprintf(stderr, "Options:\n");
761 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
762 fprintf(stderr, " -c Change comment in private and public key files.\n");
763 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
764 fprintf(stderr, " -f filename Filename of the key file.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000765 fprintf(stderr, " -g Use generic DNS resource record format.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000766 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
767 fprintf(stderr, " -l Show fingerprint of key file.\n");
768 fprintf(stderr, " -p Change passphrase of private key file.\n");
769 fprintf(stderr, " -q Quiet.\n");
770 fprintf(stderr, " -y Read private key file and print public key.\n");
771 fprintf(stderr, " -t type Specify type of key to create.\n");
772 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
773 fprintf(stderr, " -C comment Provide new comment.\n");
774 fprintf(stderr, " -N phrase Provide new passphrase.\n");
775 fprintf(stderr, " -P phrase Provide old passphrase.\n");
Damien Miller37876e92003-05-15 10:19:46 +1000776#ifdef DNS
777 fprintf(stderr, " -r hostname Print DNS resource record.\n");
778#endif /* DNS */
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000779#ifdef SMARTCARD
780 fprintf(stderr, " -D reader Download public key from smartcard.\n");
781 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
782#endif /* SMARTCARD */
783
Damien Miller95def091999-11-25 00:26:21 +1100784 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100785}
786
Damien Miller95def091999-11-25 00:26:21 +1100787/*
788 * Main program for key management.
789 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790int
791main(int ac, char **av)
792{
Damien Millera41c8b12002-01-22 23:05:08 +1100793 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000794 char *reader_id = NULL;
Damien Miller37876e92003-05-15 10:19:46 +1000795 char *resource_record_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000796 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100797 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100798 struct stat st;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000799 int opt, type, fd, download = 0;
Damien Miller95def091999-11-25 00:26:21 +1100800 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100801
Damien Miller95def091999-11-25 00:26:21 +1100802 extern int optind;
803 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000805 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000806
Damien Millerd3a18572000-06-07 19:55:44 +1000807 SSLeay_add_all_algorithms();
Kevin Steves3a881912002-07-20 19:05:40 +0000808 init_rng();
809 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +1000810
Damien Miller5428f641999-11-25 11:54:57 +1100811 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100812 pw = getpwuid(getuid());
813 if (!pw) {
814 printf("You don't exist, go away!\n");
815 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816 }
Damien Millereba71ba2000-04-29 23:57:08 +1000817 if (gethostname(hostname, sizeof(hostname)) < 0) {
818 perror("gethostname");
819 exit(1);
820 }
Damien Miller5428f641999-11-25 11:54:57 +1100821
Damien Miller37876e92003-05-15 10:19:46 +1000822 while ((opt = getopt(ac, av, "degiqpclBRxXyb:f:t:U:D:P:N:C:r:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100823 switch (opt) {
824 case 'b':
825 bits = atoi(optarg);
826 if (bits < 512 || bits > 32768) {
827 printf("Bits has bad value.\n");
828 exit(1);
829 }
830 break;
Damien Miller95def091999-11-25 00:26:21 +1100831 case 'l':
832 print_fingerprint = 1;
833 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000834 case 'B':
835 print_bubblebabble = 1;
836 break;
Damien Miller95def091999-11-25 00:26:21 +1100837 case 'p':
838 change_passphrase = 1;
839 break;
Damien Miller95def091999-11-25 00:26:21 +1100840 case 'c':
841 change_comment = 1;
842 break;
Damien Miller95def091999-11-25 00:26:21 +1100843 case 'f':
844 strlcpy(identity_file, optarg, sizeof(identity_file));
845 have_identity = 1;
846 break;
Damien Miller37876e92003-05-15 10:19:46 +1000847 case 'g':
848 print_generic = 1;
849 break;
Damien Miller95def091999-11-25 00:26:21 +1100850 case 'P':
851 identity_passphrase = optarg;
852 break;
Damien Miller95def091999-11-25 00:26:21 +1100853 case 'N':
854 identity_new_passphrase = optarg;
855 break;
Damien Miller95def091999-11-25 00:26:21 +1100856 case 'C':
857 identity_comment = optarg;
858 break;
Damien Miller95def091999-11-25 00:26:21 +1100859 case 'q':
860 quiet = 1;
861 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000862 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100863 /* unused */
864 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000865 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000866 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000867 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000868 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000869 convert_to_ssh2 = 1;
870 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000871 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000872 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000873 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000874 convert_from_ssh2 = 1;
875 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000876 case 'y':
877 print_public = 1;
878 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000879 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100880 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000881 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100882 case 't':
883 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100884 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000885 case 'D':
886 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000887 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000888 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000889 break;
Damien Miller37876e92003-05-15 10:19:46 +1000890 case 'r':
891 resource_record_hostname = optarg;
892 break;
Damien Miller95def091999-11-25 00:26:21 +1100893 case '?':
894 default:
895 usage();
896 }
897 }
898 if (optind < ac) {
899 printf("Too many arguments.\n");
900 usage();
901 }
902 if (change_passphrase && change_comment) {
903 printf("Can only have one of -p and -c.\n");
904 usage();
905 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000906 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100907 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100908 if (change_passphrase)
909 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100910 if (change_comment)
911 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +0000912 if (convert_to_ssh2)
913 do_convert_to_ssh2(pw);
914 if (convert_from_ssh2)
915 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000916 if (print_public)
917 do_print_public(pw);
Damien Miller37876e92003-05-15 10:19:46 +1000918 if (resource_record_hostname != NULL) {
919#ifdef DNS
920 do_print_resource_record(pw, resource_record_hostname);
921#else /* DNS */
922 fatal("no DNS support.");
923#endif /* DNS */
924 }
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000925 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000926#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000927 if (download)
928 do_download(pw, reader_id);
929 else
930 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000931#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000932 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000933#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000934 }
Damien Miller95def091999-11-25 00:26:21 +1100935
936 arc4random_stir();
937
Damien Miller154dda72002-01-22 23:08:16 +1100938 if (key_type_name == NULL) {
939 printf("You must specify a key type (-t).\n");
940 usage();
941 }
Damien Millere39cacc2000-11-29 12:18:44 +1100942 type = key_type_from_name(key_type_name);
943 if (type == KEY_UNSPEC) {
944 fprintf(stderr, "unknown key type %s\n", key_type_name);
945 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000946 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100947 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100948 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100949 private = key_generate(type, bits);
950 if (private == NULL) {
951 fprintf(stderr, "key_generate failed");
952 exit(1);
953 }
954 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100955
956 if (!have_identity)
957 ask_filename(pw, "Enter file in which to save the key");
958
959 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000960 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100961 if (strstr(identity_file, dotsshdir) != NULL &&
962 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000963 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100964 error("Could not create directory '%s'.", dotsshdir);
965 else if (!quiet)
966 printf("Created directory '%s'.\n", dotsshdir);
967 }
968 /* If the file already exists, ask the user to confirm. */
969 if (stat(identity_file, &st) >= 0) {
970 char yesno[3];
971 printf("%s already exists.\n", identity_file);
972 printf("Overwrite (y/n)? ");
973 fflush(stdout);
974 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
975 exit(1);
976 if (yesno[0] != 'y' && yesno[0] != 'Y')
977 exit(1);
978 }
979 /* Ask for a passphrase (twice). */
980 if (identity_passphrase)
981 passphrase1 = xstrdup(identity_passphrase);
982 else if (identity_new_passphrase)
983 passphrase1 = xstrdup(identity_new_passphrase);
984 else {
985passphrase_again:
986 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000987 read_passphrase("Enter passphrase (empty for no "
988 "passphrase): ", RP_ALLOW_STDIN);
989 passphrase2 = read_passphrase("Enter same passphrase again: ",
990 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100991 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000992 /*
993 * The passphrases do not match. Clear them and
994 * retry.
995 */
Damien Miller95def091999-11-25 00:26:21 +1100996 memset(passphrase1, 0, strlen(passphrase1));
997 memset(passphrase2, 0, strlen(passphrase2));
998 xfree(passphrase1);
999 xfree(passphrase2);
1000 printf("Passphrases do not match. Try again.\n");
1001 goto passphrase_again;
1002 }
1003 /* Clear the other copy of the passphrase. */
1004 memset(passphrase2, 0, strlen(passphrase2));
1005 xfree(passphrase2);
1006 }
1007
Damien Miller95def091999-11-25 00:26:21 +11001008 if (identity_comment) {
1009 strlcpy(comment, identity_comment, sizeof(comment));
1010 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001011 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001012 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1013 }
1014
1015 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001016 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001017 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001018 memset(passphrase1, 0, strlen(passphrase1));
1019 xfree(passphrase1);
1020 exit(1);
1021 }
1022 /* Clear the passphrase. */
1023 memset(passphrase1, 0, strlen(passphrase1));
1024 xfree(passphrase1);
1025
1026 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001027 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001028 arc4random_stir();
1029
1030 if (!quiet)
1031 printf("Your identification has been saved in %s.\n", identity_file);
1032
Damien Miller95def091999-11-25 00:26:21 +11001033 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001034 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1035 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001036 printf("Could not save your public key in %s\n", identity_file);
1037 exit(1);
1038 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001039 f = fdopen(fd, "w");
1040 if (f == NULL) {
1041 printf("fdopen %s failed", identity_file);
1042 exit(1);
1043 }
Damien Millereba71ba2000-04-29 23:57:08 +10001044 if (!key_write(public, f))
1045 fprintf(stderr, "write key failed");
1046 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001047 fclose(f);
1048
1049 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001050 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001051 printf("Your public key has been saved in %s.\n",
1052 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001053 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001054 printf("%s %s\n", fp, comment);
1055 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001056 }
Damien Millereba71ba2000-04-29 23:57:08 +10001057
1058 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001059 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060}