blob: 2de87167e8c85b4854ae44945d8903a758a3c2cf [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11006 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Ben Lindstromc58ab022002-02-26 18:15:09 +000015RCSID("$OpenBSD: ssh-keygen.c,v 1.94 2002/02/25 16:33:27 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Damien Millereba71ba2000-04-29 23:57:08 +100017#include <openssl/evp.h>
18#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100019
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100021#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000022#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100023#include "authfile.h"
24#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "buffer.h"
26#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "pathnames.h"
28#include "log.h"
29#include "readpass.h"
Damien Miller874d77b2000-10-14 16:23:11 +110030
Ben Lindstrom8282d6a2001-08-06 21:44:05 +000031#ifdef SMARTCARD
32#include <sectok.h>
33#include <openssl/engine.h>
34#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000035#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000036
Damien Millereba71ba2000-04-29 23:57:08 +100037/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038int bits = 1024;
39
Damien Miller5428f641999-11-25 11:54:57 +110040/*
41 * Flag indicating that we just want to change the passphrase. This can be
42 * set on the command line.
43 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044int change_passphrase = 0;
45
Damien Miller5428f641999-11-25 11:54:57 +110046/*
47 * Flag indicating that we just want to change the comment. This can be set
48 * on the command line.
49 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050int change_comment = 0;
51
52int quiet = 0;
53
Damien Miller10f6f6b1999-11-17 17:29:08 +110054/* Flag indicating that we just want to see the key fingerprint */
55int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000056int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110057
Damien Miller431f66b1999-11-21 18:31:57 +110058/* The identity file name, given on the command line or entered by the user. */
59char identity_file[1024];
60int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62/* This is set to the passphrase if given on the command line. */
63char *identity_passphrase = NULL;
64
65/* This is set to the new passphrase if given on the command line. */
66char *identity_new_passphrase = NULL;
67
68/* This is set to the new comment if given on the command line. */
69char *identity_comment = NULL;
70
Damien Millereba71ba2000-04-29 23:57:08 +100071/* Dump public key file in format used by real and the original SSH 2 */
72int convert_to_ssh2 = 0;
73int convert_from_ssh2 = 0;
74int print_public = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110075
Damien Millera41c8b12002-01-22 23:05:08 +110076char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100077
Damien Miller431f66b1999-11-21 18:31:57 +110078/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110079#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110080extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000081#else
82char *__progname;
83#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Millereba71ba2000-04-29 23:57:08 +100085char hostname[MAXHOSTNAMELEN];
86
Ben Lindstrombba81212001-06-25 05:01:22 +000087static void
Damien Miller431f66b1999-11-21 18:31:57 +110088ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089{
Damien Miller95def091999-11-25 00:26:21 +110090 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110091 char *name = NULL;
92
Damien Miller993dd552002-02-19 15:22:47 +110093 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +000094 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller993dd552002-02-19 15:22:47 +110095 else
96 switch (key_type_from_name(key_type_name)) {
97 case KEY_RSA1:
98 name = _PATH_SSH_CLIENT_IDENTITY;
99 break;
100 case KEY_DSA:
101 name = _PATH_SSH_CLIENT_ID_DSA;
102 break;
103 case KEY_RSA:
104 name = _PATH_SSH_CLIENT_ID_RSA;
105 break;
106 default:
107 fprintf(stderr, "bad key type");
108 exit(1);
109 break;
110 }
111
Damien Millere39cacc2000-11-29 12:18:44 +1100112 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000113 fprintf(stderr, "%s (%s): ", prompt, identity_file);
114 fflush(stderr);
Damien Miller95def091999-11-25 00:26:21 +1100115 if (fgets(buf, sizeof(buf), stdin) == NULL)
116 exit(1);
117 if (strchr(buf, '\n'))
118 *strchr(buf, '\n') = 0;
119 if (strcmp(buf, "") != 0)
120 strlcpy(identity_file, buf, sizeof(identity_file));
121 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100122}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Ben Lindstrombba81212001-06-25 05:01:22 +0000124static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000125load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000126{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000127 char *pass;
128 Key *prv;
129
Ben Lindstroma3700052001-04-05 23:26:32 +0000130 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000131 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000132 if (identity_passphrase)
133 pass = xstrdup(identity_passphrase);
134 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000135 pass = read_passphrase("Enter passphrase: ",
136 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000137 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000138 memset(pass, 0, strlen(pass));
139 xfree(pass);
140 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000141 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000142}
143
Damien Miller874d77b2000-10-14 16:23:11 +1100144#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
145#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
146#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000147#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000148
Ben Lindstrombba81212001-06-25 05:01:22 +0000149static void
Damien Millereba71ba2000-04-29 23:57:08 +1000150do_convert_to_ssh2(struct passwd *pw)
151{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000152 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000153 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000154 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000155 struct stat st;
156
157 if (!have_identity)
158 ask_filename(pw, "Enter file in which the key is");
159 if (stat(identity_file, &st) < 0) {
160 perror(identity_file);
161 exit(1);
162 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000163 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000164 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000165 fprintf(stderr, "load failed\n");
166 exit(1);
167 }
Damien Millereba71ba2000-04-29 23:57:08 +1000168 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000169 if (key_to_blob(k, &blob, &len) <= 0) {
170 fprintf(stderr, "key_to_blob failed\n");
171 exit(1);
172 }
Damien Miller874d77b2000-10-14 16:23:11 +1100173 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000174 fprintf(stdout,
Damien Miller874d77b2000-10-14 16:23:11 +1100175 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000176 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000177 pw->pw_name, hostname);
178 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100179 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000180 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000181 xfree(blob);
182 exit(0);
183}
184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static void
Damien Miller874d77b2000-10-14 16:23:11 +1100186buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
187{
188 int bits = buffer_get_int(b);
189 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000190
Damien Miller874d77b2000-10-14 16:23:11 +1100191 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000192 fatal("buffer_get_bignum_bits: input buffer too small: "
193 "need %d have %d", bytes, buffer_len(b));
Damien Miller708d21c2002-01-22 23:18:15 +1100194 BN_bin2bn(buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100195 buffer_consume(b, bytes);
196}
197
Ben Lindstrombba81212001-06-25 05:01:22 +0000198static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000199do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100200{
201 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100202 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100203 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000204 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000205 int magic, rlen, ktype, i1, i2, i3, i4;
206 u_int slen;
207 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100208
209 buffer_init(&b);
210 buffer_append(&b, blob, blen);
211
212 magic = buffer_get_int(&b);
213 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
214 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
215 buffer_free(&b);
216 return NULL;
217 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000218 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100219 type = buffer_get_string(&b, NULL);
220 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000221 i2 = buffer_get_int(&b);
222 i3 = buffer_get_int(&b);
223 i4 = buffer_get_int(&b);
224 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100225 if (strcmp(cipher, "none") != 0) {
226 error("unsupported cipher %s", cipher);
227 xfree(cipher);
228 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000229 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100230 return NULL;
231 }
232 xfree(cipher);
233
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000234 if (strstr(type, "dsa")) {
235 ktype = KEY_DSA;
236 } else if (strstr(type, "rsa")) {
237 ktype = KEY_RSA;
238 } else {
239 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100240 return NULL;
241 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000242 key = key_new_private(ktype);
243 xfree(type);
244
245 switch (key->type) {
246 case KEY_DSA:
247 buffer_get_bignum_bits(&b, key->dsa->p);
248 buffer_get_bignum_bits(&b, key->dsa->g);
249 buffer_get_bignum_bits(&b, key->dsa->q);
250 buffer_get_bignum_bits(&b, key->dsa->pub_key);
251 buffer_get_bignum_bits(&b, key->dsa->priv_key);
252 break;
253 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000254 e = buffer_get_char(&b);
255 debug("e %lx", e);
256 if (e < 30) {
257 e <<= 8;
258 e += buffer_get_char(&b);
259 debug("e %lx", e);
260 e <<= 8;
261 e += buffer_get_char(&b);
262 debug("e %lx", e);
263 }
264 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000265 buffer_free(&b);
266 key_free(key);
267 return NULL;
268 }
269 buffer_get_bignum_bits(&b, key->rsa->d);
270 buffer_get_bignum_bits(&b, key->rsa->n);
271 buffer_get_bignum_bits(&b, key->rsa->iqmp);
272 buffer_get_bignum_bits(&b, key->rsa->q);
273 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000274 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000275 break;
276 }
Damien Miller874d77b2000-10-14 16:23:11 +1100277 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000278 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000279 error("do_convert_private_ssh2_from_blob: "
280 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100281 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000282
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000283 /* try the key */
284 key_sign(key, &sig, &slen, data, sizeof(data));
285 key_verify(key, sig, slen, data, sizeof(data));
286 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100287 return key;
288}
289
Ben Lindstrombba81212001-06-25 05:01:22 +0000290static void
Damien Millereba71ba2000-04-29 23:57:08 +1000291do_convert_from_ssh2(struct passwd *pw)
292{
293 Key *k;
294 int blen;
295 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000296 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000297 char encoded[8096];
298 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100299 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000300 FILE *fp;
301
302 if (!have_identity)
303 ask_filename(pw, "Enter file in which the key is");
304 if (stat(identity_file, &st) < 0) {
305 perror(identity_file);
306 exit(1);
307 }
308 fp = fopen(identity_file, "r");
309 if (fp == NULL) {
310 perror(identity_file);
311 exit(1);
312 }
313 encoded[0] = '\0';
314 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000315 if (!(p = strchr(line, '\n'))) {
316 fprintf(stderr, "input line too long.\n");
317 exit(1);
318 }
319 if (p > line && p[-1] == '\\')
320 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000321 if (strncmp(line, "----", 4) == 0 ||
322 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100323 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
324 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000325 if (strstr(line, " END ") != NULL) {
326 break;
327 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000328 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000329 continue;
330 }
Damien Miller30c3d422000-05-09 11:02:59 +1000331 if (escaped) {
332 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000333 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000334 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000335 }
336 *p = '\0';
337 strlcat(encoded, line, sizeof(encoded));
338 }
Damien Miller4a8ed542002-01-22 23:33:31 +1100339 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000340 if (blen < 0) {
341 fprintf(stderr, "uudecode failed.\n");
342 exit(1);
343 }
Damien Miller874d77b2000-10-14 16:23:11 +1100344 k = private ?
345 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100346 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100347 if (k == NULL) {
348 fprintf(stderr, "decode blob failed.\n");
349 exit(1);
350 }
351 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000352 (k->type == KEY_DSA ?
353 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
354 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100355 key_write(k, stdout);
356 if (!ok) {
357 fprintf(stderr, "key write failed");
358 exit(1);
359 }
Damien Millereba71ba2000-04-29 23:57:08 +1000360 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100361 if (!private)
362 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000363 fclose(fp);
364 exit(0);
365}
366
Ben Lindstrombba81212001-06-25 05:01:22 +0000367static void
Damien Millereba71ba2000-04-29 23:57:08 +1000368do_print_public(struct passwd *pw)
369{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000370 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000371 struct stat st;
372
373 if (!have_identity)
374 ask_filename(pw, "Enter file in which the key is");
375 if (stat(identity_file, &st) < 0) {
376 perror(identity_file);
377 exit(1);
378 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000379 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000380 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000381 fprintf(stderr, "load failed\n");
382 exit(1);
383 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000384 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000385 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000386 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000387 fprintf(stdout, "\n");
388 exit(0);
389}
390
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000391#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000392#define NUM_RSA_KEY_ELEMENTS 5+1
393#define COPY_RSA_KEY(x, i) \
394 do { \
395 len = BN_num_bytes(prv->rsa->x); \
396 elements[i] = xmalloc(len); \
Ben Lindstrom7feba352001-07-04 05:06:59 +0000397 debug("#bytes %d", len); \
Ben Lindstromcd392282001-07-04 03:44:03 +0000398 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
399 goto done; \
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000400 } while (0)
Ben Lindstromcd392282001-07-04 03:44:03 +0000401
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000402static int
403get_AUT0(char *aut0)
404{
405 EVP_MD *evp_md = EVP_sha1();
406 EVP_MD_CTX md;
407 char *pass;
408
409 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
410 if (pass == NULL)
411 return -1;
412 EVP_DigestInit(&md, evp_md);
413 EVP_DigestUpdate(&md, pass, strlen(pass));
414 EVP_DigestFinal(&md, aut0, NULL);
415 memset(pass, 0, strlen(pass));
416 xfree(pass);
417 return 0;
418}
419
Ben Lindstromcd392282001-07-04 03:44:03 +0000420static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000421do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000422{
Ben Lindstromcd392282001-07-04 03:44:03 +0000423 Key *prv = NULL;
424 struct stat st;
425 u_char *elements[NUM_RSA_KEY_ELEMENTS];
426 u_char key_fid[2];
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000427 u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
428 u_char AUT0[EVP_MAX_MD_SIZE];
Ben Lindstromcd392282001-07-04 03:44:03 +0000429 int len, status = 1, i, fd = -1, ret;
Ben Lindstrom00477642001-07-04 05:24:27 +0000430 int sw = 0, cla = 0x00;
Ben Lindstromcd392282001-07-04 03:44:03 +0000431
Ben Lindstromd6e049c2001-07-04 05:08:39 +0000432 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
433 elements[i] = NULL;
Ben Lindstromcd392282001-07-04 03:44:03 +0000434 if (!have_identity)
435 ask_filename(pw, "Enter file in which the key is");
436 if (stat(identity_file, &st) < 0) {
437 perror(identity_file);
438 goto done;
439 }
440 prv = load_identity(identity_file);
441 if (prv == NULL) {
442 error("load failed");
443 goto done;
444 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000445 COPY_RSA_KEY(q, 0);
446 COPY_RSA_KEY(p, 1);
447 COPY_RSA_KEY(iqmp, 2);
448 COPY_RSA_KEY(dmq1, 3);
449 COPY_RSA_KEY(dmp1, 4);
450 COPY_RSA_KEY(n, 5);
451 len = BN_num_bytes(prv->rsa->n);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000452 fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000453 if (fd < 0) {
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000454 error("sectok_open failed: %s", sectok_get_sw(sw));
455 goto done;
456 }
457 if (! sectok_cardpresent(fd)) {
458 error("smartcard in reader %s not present",
459 sc_reader_id);
Ben Lindstromcd392282001-07-04 03:44:03 +0000460 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000461 }
Ben Lindstrom60df8e42001-08-06 21:10:52 +0000462 ret = sectok_reset(fd, 0, NULL, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000463 if (ret <= 0) {
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000464 error("sectok_reset failed: %s", sectok_get_sw(sw));
Ben Lindstromcd392282001-07-04 03:44:03 +0000465 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000466 }
Ben Lindstrom680b2762001-07-04 05:00:11 +0000467 if ((cla = cyberflex_inq_class(fd)) < 0) {
468 error("cyberflex_inq_class failed");
469 goto done;
470 }
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000471 memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
472 if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
473 if (get_AUT0(AUT0) < 0 ||
474 cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
475 error("cyberflex_verify_AUT0 failed");
476 goto done;
477 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000478 }
479 key_fid[0] = 0x00;
480 key_fid[1] = 0x12;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000481 if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
Ben Lindstrom00477642001-07-04 05:24:27 +0000482 &sw) < 0) {
483 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000484 goto done;
485 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000486 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000487 goto done;
488 log("cyberflex_load_rsa_priv done");
489 key_fid[0] = 0x73;
490 key_fid[1] = 0x68;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000491 if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
Ben Lindstrom00477642001-07-04 05:24:27 +0000492 &sw) < 0) {
493 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000494 goto done;
495 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000496 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000497 goto done;
498 log("cyberflex_load_rsa_pub done");
499 status = 0;
500 log("loading key done");
501done:
Ben Lindstrom1af4d3b2001-10-03 17:18:37 +0000502
503 memset(elements[0], '\0', BN_num_bytes(prv->rsa->q));
504 memset(elements[1], '\0', BN_num_bytes(prv->rsa->p));
505 memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp));
506 memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1));
507 memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1));
508 memset(elements[5], '\0', BN_num_bytes(prv->rsa->n));
509
Ben Lindstromcd392282001-07-04 03:44:03 +0000510 if (prv)
511 key_free(prv);
512 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
Ben Lindstrom00477642001-07-04 05:24:27 +0000513 if (elements[i])
514 xfree(elements[i]);
Ben Lindstromcd392282001-07-04 03:44:03 +0000515 if (fd != -1)
Ben Lindstrom00477642001-07-04 05:24:27 +0000516 sectok_close(fd);
Ben Lindstromcd392282001-07-04 03:44:03 +0000517 exit(status);
Ben Lindstromcd392282001-07-04 03:44:03 +0000518}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000519
520static void
521do_download(struct passwd *pw, const char *sc_reader_id)
522{
523 Key *pub = NULL;
524
525 pub = sc_get_key(sc_reader_id);
526 if (pub == NULL)
527 fatal("cannot read public key from smartcard");
528 key_write(pub, stdout);
529 key_free(pub);
530 fprintf(stdout, "\n");
531 exit(0);
532}
Ben Lindstromffce1472001-08-06 21:57:31 +0000533#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000534
Ben Lindstrombba81212001-06-25 05:01:22 +0000535static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100536do_fingerprint(struct passwd *pw)
537{
Damien Miller98c7ad62000-03-09 21:27:49 +1100538 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000539 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000540 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000541 int i, skip = 0, num = 1, invalid = 1;
542 enum fp_rep rep;
543 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100544 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100545
Ben Lindstromd0fca422001-03-26 13:44:06 +0000546 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
547 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000548
Damien Miller95def091999-11-25 00:26:21 +1100549 if (!have_identity)
550 ask_filename(pw, "Enter file in which the key is");
551 if (stat(identity_file, &st) < 0) {
552 perror(identity_file);
553 exit(1);
554 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000555 public = key_load_public(identity_file, &comment);
556 if (public != NULL) {
557 fp = key_fingerprint(public, fptype, rep);
558 printf("%d %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100559 key_free(public);
560 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000561 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100562 exit(0);
563 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000564 if (comment)
565 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100566
567 f = fopen(identity_file, "r");
568 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100569 while (fgets(line, sizeof(line), f)) {
570 i = strlen(line) - 1;
571 if (line[i] != '\n') {
572 error("line %d too long: %.40s...", num, line);
573 skip = 1;
574 continue;
Damien Miller95def091999-11-25 00:26:21 +1100575 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100576 num++;
577 if (skip) {
578 skip = 0;
579 continue;
580 }
581 line[i] = '\0';
582
583 /* Skip leading whitespace, empty and comment lines. */
584 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
585 ;
586 if (!*cp || *cp == '\n' || *cp == '#')
587 continue ;
588 i = strtol(cp, &ep, 10);
589 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
590 int quoted = 0;
591 comment = cp;
592 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
593 if (*cp == '\\' && cp[1] == '"')
594 cp++; /* Skip both */
595 else if (*cp == '"')
596 quoted = !quoted;
597 }
598 if (!*cp)
599 continue;
600 *cp++ = '\0';
601 }
602 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000603 public = key_new(KEY_RSA1);
604 if (key_read(public, &cp) != 1) {
605 cp = ep;
606 key_free(public);
607 public = key_new(KEY_UNSPEC);
608 if (key_read(public, &cp) != 1) {
609 key_free(public);
610 continue;
611 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100612 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000613 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000614 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000615 printf("%d %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000616 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000617 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000618 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000619 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100620 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100621 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100622 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100623 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100624 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100625 exit(1);
626 }
Damien Miller95def091999-11-25 00:26:21 +1100627 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100628}
629
Damien Miller95def091999-11-25 00:26:21 +1100630/*
631 * Perform changing a passphrase. The argument is the passwd structure
632 * for the current user.
633 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000634static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100635do_change_passphrase(struct passwd *pw)
636{
Damien Miller95def091999-11-25 00:26:21 +1100637 char *comment;
638 char *old_passphrase, *passphrase1, *passphrase2;
639 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000640 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100641
Damien Miller95def091999-11-25 00:26:21 +1100642 if (!have_identity)
643 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100644 if (stat(identity_file, &st) < 0) {
645 perror(identity_file);
646 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647 }
Damien Miller95def091999-11-25 00:26:21 +1100648 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000649 private = key_load_private(identity_file, "", &comment);
650 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100651 if (identity_passphrase)
652 old_passphrase = xstrdup(identity_passphrase);
653 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000654 old_passphrase =
655 read_passphrase("Enter old passphrase: ",
656 RP_ALLOW_STDIN);
657 private = key_load_private(identity_file, old_passphrase,
658 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000659 memset(old_passphrase, 0, strlen(old_passphrase));
660 xfree(old_passphrase);
661 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100662 printf("Bad passphrase.\n");
663 exit(1);
664 }
Damien Miller95def091999-11-25 00:26:21 +1100665 }
666 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
Damien Miller95def091999-11-25 00:26:21 +1100668 /* Ask the new passphrase (twice). */
669 if (identity_new_passphrase) {
670 passphrase1 = xstrdup(identity_new_passphrase);
671 passphrase2 = NULL;
672 } else {
673 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000674 read_passphrase("Enter new passphrase (empty for no "
675 "passphrase): ", RP_ALLOW_STDIN);
676 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100677 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100678
679 /* Verify that they are the same. */
680 if (strcmp(passphrase1, passphrase2) != 0) {
681 memset(passphrase1, 0, strlen(passphrase1));
682 memset(passphrase2, 0, strlen(passphrase2));
683 xfree(passphrase1);
684 xfree(passphrase2);
685 printf("Pass phrases do not match. Try again.\n");
686 exit(1);
687 }
688 /* Destroy the other copy. */
689 memset(passphrase2, 0, strlen(passphrase2));
690 xfree(passphrase2);
691 }
692
693 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000694 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000695 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100696 memset(passphrase1, 0, strlen(passphrase1));
697 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000698 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100699 xfree(comment);
700 exit(1);
701 }
702 /* Destroy the passphrase and the copy of the key in memory. */
703 memset(passphrase1, 0, strlen(passphrase1));
704 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000705 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100706 xfree(comment);
707
708 printf("Your identification has been saved with the new passphrase.\n");
709 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710}
711
Damien Miller95def091999-11-25 00:26:21 +1100712/*
713 * Change the comment of a private key file.
714 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000715static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716do_change_comment(struct passwd *pw)
717{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000718 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000719 Key *private;
720 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100721 struct stat st;
722 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000723 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724
Damien Miller95def091999-11-25 00:26:21 +1100725 if (!have_identity)
726 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100727 if (stat(identity_file, &st) < 0) {
728 perror(identity_file);
729 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000731 private = key_load_private(identity_file, "", &comment);
732 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100733 if (identity_passphrase)
734 passphrase = xstrdup(identity_passphrase);
735 else if (identity_new_passphrase)
736 passphrase = xstrdup(identity_new_passphrase);
737 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000738 passphrase = read_passphrase("Enter passphrase: ",
739 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100740 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000741 private = key_load_private(identity_file, passphrase, &comment);
742 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100743 memset(passphrase, 0, strlen(passphrase));
744 xfree(passphrase);
745 printf("Bad passphrase.\n");
746 exit(1);
747 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000748 } else {
749 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100750 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000751 if (private->type != KEY_RSA1) {
752 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
753 key_free(private);
754 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100755 }
Damien Miller95def091999-11-25 00:26:21 +1100756 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757
Damien Miller95def091999-11-25 00:26:21 +1100758 if (identity_comment) {
759 strlcpy(new_comment, identity_comment, sizeof(new_comment));
760 } else {
761 printf("Enter new comment: ");
762 fflush(stdout);
763 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
764 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000765 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100766 exit(1);
767 }
Damien Miller95def091999-11-25 00:26:21 +1100768 if (strchr(new_comment, '\n'))
769 *strchr(new_comment, '\n') = 0;
770 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771
Damien Miller95def091999-11-25 00:26:21 +1100772 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000773 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000774 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100775 memset(passphrase, 0, strlen(passphrase));
776 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000777 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100778 xfree(comment);
779 exit(1);
780 }
Damien Miller95def091999-11-25 00:26:21 +1100781 memset(passphrase, 0, strlen(passphrase));
782 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000783 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000784 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Damien Miller95def091999-11-25 00:26:21 +1100786 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000787 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
788 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100789 printf("Could not save your public key in %s\n", identity_file);
790 exit(1);
791 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000792 f = fdopen(fd, "w");
793 if (f == NULL) {
794 printf("fdopen %s failed", identity_file);
795 exit(1);
796 }
Damien Millereba71ba2000-04-29 23:57:08 +1000797 if (!key_write(public, f))
798 fprintf(stderr, "write key failed");
799 key_free(public);
800 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100801 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000802
Damien Miller95def091999-11-25 00:26:21 +1100803 xfree(comment);
804
805 printf("The comment in your key file has been changed.\n");
806 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807}
808
Ben Lindstrombba81212001-06-25 05:01:22 +0000809static void
Damien Miller431f66b1999-11-21 18:31:57 +1100810usage(void)
811{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000812 fprintf(stderr, "Usage: %s [options]\n", __progname);
813 fprintf(stderr, "Options:\n");
814 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
815 fprintf(stderr, " -c Change comment in private and public key files.\n");
816 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
817 fprintf(stderr, " -f filename Filename of the key file.\n");
818 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
819 fprintf(stderr, " -l Show fingerprint of key file.\n");
820 fprintf(stderr, " -p Change passphrase of private key file.\n");
821 fprintf(stderr, " -q Quiet.\n");
822 fprintf(stderr, " -y Read private key file and print public key.\n");
823 fprintf(stderr, " -t type Specify type of key to create.\n");
824 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
825 fprintf(stderr, " -C comment Provide new comment.\n");
826 fprintf(stderr, " -N phrase Provide new passphrase.\n");
827 fprintf(stderr, " -P phrase Provide old passphrase.\n");
828#ifdef SMARTCARD
829 fprintf(stderr, " -D reader Download public key from smartcard.\n");
830 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
831#endif /* SMARTCARD */
832
Damien Miller95def091999-11-25 00:26:21 +1100833 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100834}
835
Damien Miller95def091999-11-25 00:26:21 +1100836/*
837 * Main program for key management.
838 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839int
840main(int ac, char **av)
841{
Damien Millera41c8b12002-01-22 23:05:08 +1100842 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000843 char *reader_id = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000844 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100845 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100846 struct stat st;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000847 int opt, type, fd, download = 0;
Damien Miller95def091999-11-25 00:26:21 +1100848 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100849
Damien Miller95def091999-11-25 00:26:21 +1100850 extern int optind;
851 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000853 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000854
Damien Millerd3a18572000-06-07 19:55:44 +1000855 SSLeay_add_all_algorithms();
Damien Millereba71ba2000-04-29 23:57:08 +1000856
Damien Miller5428f641999-11-25 11:54:57 +1100857 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100858 pw = getpwuid(getuid());
859 if (!pw) {
860 printf("You don't exist, go away!\n");
861 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862 }
Damien Millereba71ba2000-04-29 23:57:08 +1000863 if (gethostname(hostname, sizeof(hostname)) < 0) {
864 perror("gethostname");
865 exit(1);
866 }
Damien Miller5428f641999-11-25 11:54:57 +1100867
Ben Lindstromf19578c2001-08-06 21:46:54 +0000868 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100869 switch (opt) {
870 case 'b':
871 bits = atoi(optarg);
872 if (bits < 512 || bits > 32768) {
873 printf("Bits has bad value.\n");
874 exit(1);
875 }
876 break;
Damien Miller95def091999-11-25 00:26:21 +1100877 case 'l':
878 print_fingerprint = 1;
879 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000880 case 'B':
881 print_bubblebabble = 1;
882 break;
Damien Miller95def091999-11-25 00:26:21 +1100883 case 'p':
884 change_passphrase = 1;
885 break;
Damien Miller95def091999-11-25 00:26:21 +1100886 case 'c':
887 change_comment = 1;
888 break;
Damien Miller95def091999-11-25 00:26:21 +1100889 case 'f':
890 strlcpy(identity_file, optarg, sizeof(identity_file));
891 have_identity = 1;
892 break;
Damien Miller95def091999-11-25 00:26:21 +1100893 case 'P':
894 identity_passphrase = optarg;
895 break;
Damien Miller95def091999-11-25 00:26:21 +1100896 case 'N':
897 identity_new_passphrase = optarg;
898 break;
Damien Miller95def091999-11-25 00:26:21 +1100899 case 'C':
900 identity_comment = optarg;
901 break;
Damien Miller95def091999-11-25 00:26:21 +1100902 case 'q':
903 quiet = 1;
904 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000905 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100906 /* unused */
907 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000908 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000909 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000910 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000911 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000912 convert_to_ssh2 = 1;
913 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000914 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000915 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000916 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000917 convert_from_ssh2 = 1;
918 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000919 case 'y':
920 print_public = 1;
921 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000922 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100923 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000924 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100925 case 't':
926 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100927 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000928 case 'D':
929 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000930 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000931 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000932 break;
Damien Miller95def091999-11-25 00:26:21 +1100933 case '?':
934 default:
935 usage();
936 }
937 }
938 if (optind < ac) {
939 printf("Too many arguments.\n");
940 usage();
941 }
942 if (change_passphrase && change_comment) {
943 printf("Can only have one of -p and -c.\n");
944 usage();
945 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000946 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100947 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100948 if (change_passphrase)
949 do_change_passphrase(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000950 if (convert_to_ssh2)
951 do_convert_to_ssh2(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +1100952 if (change_comment)
953 do_change_comment(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000954 if (print_public)
955 do_print_public(pw);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000956 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000957#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000958 if (download)
959 do_download(pw, reader_id);
960 else
961 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000962#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000963 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000964#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000965 }
Damien Miller95def091999-11-25 00:26:21 +1100966
Damien Miller8eb71412002-01-30 09:37:06 +1100967 init_rng();
968 seed_rng();
Damien Miller95def091999-11-25 00:26:21 +1100969 arc4random_stir();
970
Damien Miller4a10d2e2002-03-11 22:53:29 +1100971 if (convert_from_ssh2)
972 do_convert_from_ssh2(pw);
973
Damien Miller154dda72002-01-22 23:08:16 +1100974 if (key_type_name == NULL) {
975 printf("You must specify a key type (-t).\n");
976 usage();
977 }
Damien Millere39cacc2000-11-29 12:18:44 +1100978 type = key_type_from_name(key_type_name);
979 if (type == KEY_UNSPEC) {
980 fprintf(stderr, "unknown key type %s\n", key_type_name);
981 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000982 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100983 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100984 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100985 private = key_generate(type, bits);
986 if (private == NULL) {
987 fprintf(stderr, "key_generate failed");
988 exit(1);
989 }
990 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100991
992 if (!have_identity)
993 ask_filename(pw, "Enter file in which to save the key");
994
995 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000996 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100997 if (strstr(identity_file, dotsshdir) != NULL &&
998 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000999 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001000 error("Could not create directory '%s'.", dotsshdir);
1001 else if (!quiet)
1002 printf("Created directory '%s'.\n", dotsshdir);
1003 }
1004 /* If the file already exists, ask the user to confirm. */
1005 if (stat(identity_file, &st) >= 0) {
1006 char yesno[3];
1007 printf("%s already exists.\n", identity_file);
1008 printf("Overwrite (y/n)? ");
1009 fflush(stdout);
1010 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1011 exit(1);
1012 if (yesno[0] != 'y' && yesno[0] != 'Y')
1013 exit(1);
1014 }
1015 /* Ask for a passphrase (twice). */
1016 if (identity_passphrase)
1017 passphrase1 = xstrdup(identity_passphrase);
1018 else if (identity_new_passphrase)
1019 passphrase1 = xstrdup(identity_new_passphrase);
1020 else {
1021passphrase_again:
1022 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001023 read_passphrase("Enter passphrase (empty for no "
1024 "passphrase): ", RP_ALLOW_STDIN);
1025 passphrase2 = read_passphrase("Enter same passphrase again: ",
1026 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001027 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001028 /*
1029 * The passphrases do not match. Clear them and
1030 * retry.
1031 */
Damien Miller95def091999-11-25 00:26:21 +11001032 memset(passphrase1, 0, strlen(passphrase1));
1033 memset(passphrase2, 0, strlen(passphrase2));
1034 xfree(passphrase1);
1035 xfree(passphrase2);
1036 printf("Passphrases do not match. Try again.\n");
1037 goto passphrase_again;
1038 }
1039 /* Clear the other copy of the passphrase. */
1040 memset(passphrase2, 0, strlen(passphrase2));
1041 xfree(passphrase2);
1042 }
1043
Damien Miller95def091999-11-25 00:26:21 +11001044 if (identity_comment) {
1045 strlcpy(comment, identity_comment, sizeof(comment));
1046 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001047 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001048 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1049 }
1050
1051 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001052 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001053 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001054 memset(passphrase1, 0, strlen(passphrase1));
1055 xfree(passphrase1);
1056 exit(1);
1057 }
1058 /* Clear the passphrase. */
1059 memset(passphrase1, 0, strlen(passphrase1));
1060 xfree(passphrase1);
1061
1062 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001063 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001064 arc4random_stir();
1065
1066 if (!quiet)
1067 printf("Your identification has been saved in %s.\n", identity_file);
1068
Damien Miller95def091999-11-25 00:26:21 +11001069 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001070 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1071 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001072 printf("Could not save your public key in %s\n", identity_file);
1073 exit(1);
1074 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001075 f = fdopen(fd, "w");
1076 if (f == NULL) {
1077 printf("fdopen %s failed", identity_file);
1078 exit(1);
1079 }
Damien Millereba71ba2000-04-29 23:57:08 +10001080 if (!key_write(public, f))
1081 fprintf(stderr, "write key failed");
1082 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001083 fclose(f);
1084
1085 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001086 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001087 printf("Your public key has been saved in %s.\n",
1088 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001089 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001090 printf("%s %s\n", fp, comment);
1091 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001092 }
Damien Millereba71ba2000-04-29 23:57:08 +10001093
1094 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001095 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001096}