blob: 9e3a12a54f2cf49bd1fddce325f59c9e21712d2d [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 Millera41c8b12002-01-22 23:05:08 +110015RCSID("$OpenBSD: ssh-keygen.c,v 1.87 2001/12/21 08:52:22 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
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
93 switch (key_type_from_name(key_type_name)) {
94 case KEY_RSA1:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000095 name = _PATH_SSH_CLIENT_IDENTITY;
Damien Millere39cacc2000-11-29 12:18:44 +110096 break;
97 case KEY_DSA:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000098 name = _PATH_SSH_CLIENT_ID_DSA;
Damien Millere39cacc2000-11-29 12:18:44 +110099 break;
100 case KEY_RSA:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000101 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Millere39cacc2000-11-29 12:18:44 +1100102 break;
103 default:
104 fprintf(stderr, "bad key type");
105 exit(1);
106 break;
107 }
108 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000109 fprintf(stderr, "%s (%s): ", prompt, identity_file);
110 fflush(stderr);
Damien Miller95def091999-11-25 00:26:21 +1100111 if (fgets(buf, sizeof(buf), stdin) == NULL)
112 exit(1);
113 if (strchr(buf, '\n'))
114 *strchr(buf, '\n') = 0;
115 if (strcmp(buf, "") != 0)
116 strlcpy(identity_file, buf, sizeof(identity_file));
117 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100118}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Ben Lindstrombba81212001-06-25 05:01:22 +0000120static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000121load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000122{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000123 char *pass;
124 Key *prv;
125
Ben Lindstroma3700052001-04-05 23:26:32 +0000126 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000127 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000128 if (identity_passphrase)
129 pass = xstrdup(identity_passphrase);
130 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000131 pass = read_passphrase("Enter passphrase: ",
132 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000133 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000134 memset(pass, 0, strlen(pass));
135 xfree(pass);
136 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000137 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000138}
139
Damien Miller874d77b2000-10-14 16:23:11 +1100140#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
141#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
142#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000143#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000144
Ben Lindstrombba81212001-06-25 05:01:22 +0000145static void
Damien Millereba71ba2000-04-29 23:57:08 +1000146do_convert_to_ssh2(struct passwd *pw)
147{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000148 Key *k;
Damien Millereba71ba2000-04-29 23:57:08 +1000149 int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000150 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000151 struct stat st;
152
153 if (!have_identity)
154 ask_filename(pw, "Enter file in which the key is");
155 if (stat(identity_file, &st) < 0) {
156 perror(identity_file);
157 exit(1);
158 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000159 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000160 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000161 fprintf(stderr, "load failed\n");
162 exit(1);
163 }
Damien Millereba71ba2000-04-29 23:57:08 +1000164 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000165 if (key_to_blob(k, &blob, &len) <= 0) {
166 fprintf(stderr, "key_to_blob failed\n");
167 exit(1);
168 }
Damien Miller874d77b2000-10-14 16:23:11 +1100169 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000170 fprintf(stdout,
Damien Miller874d77b2000-10-14 16:23:11 +1100171 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000172 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000173 pw->pw_name, hostname);
174 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100175 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000176 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000177 xfree(blob);
178 exit(0);
179}
180
Ben Lindstrombba81212001-06-25 05:01:22 +0000181static void
Damien Miller874d77b2000-10-14 16:23:11 +1100182buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
183{
184 int bits = buffer_get_int(b);
185 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000186
Damien Miller874d77b2000-10-14 16:23:11 +1100187 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000188 fatal("buffer_get_bignum_bits: input buffer too small: "
189 "need %d have %d", bytes, buffer_len(b));
Ben Lindstrom46c16222000-12-22 01:43:59 +0000190 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100191 buffer_consume(b, bytes);
192}
193
Ben Lindstrombba81212001-06-25 05:01:22 +0000194static Key *
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000195do_convert_private_ssh2_from_blob(u_char *blob, int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100196{
197 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100198 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100199 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000200 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000201 int magic, rlen, ktype, i1, i2, i3, i4;
202 u_int slen;
203 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100204
205 buffer_init(&b);
206 buffer_append(&b, blob, blen);
207
208 magic = buffer_get_int(&b);
209 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
210 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
211 buffer_free(&b);
212 return NULL;
213 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000214 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100215 type = buffer_get_string(&b, NULL);
216 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000217 i2 = buffer_get_int(&b);
218 i3 = buffer_get_int(&b);
219 i4 = buffer_get_int(&b);
220 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100221 if (strcmp(cipher, "none") != 0) {
222 error("unsupported cipher %s", cipher);
223 xfree(cipher);
224 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000225 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100226 return NULL;
227 }
228 xfree(cipher);
229
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000230 if (strstr(type, "dsa")) {
231 ktype = KEY_DSA;
232 } else if (strstr(type, "rsa")) {
233 ktype = KEY_RSA;
234 } else {
235 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100236 return NULL;
237 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000238 key = key_new_private(ktype);
239 xfree(type);
240
241 switch (key->type) {
242 case KEY_DSA:
243 buffer_get_bignum_bits(&b, key->dsa->p);
244 buffer_get_bignum_bits(&b, key->dsa->g);
245 buffer_get_bignum_bits(&b, key->dsa->q);
246 buffer_get_bignum_bits(&b, key->dsa->pub_key);
247 buffer_get_bignum_bits(&b, key->dsa->priv_key);
248 break;
249 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000250 e = buffer_get_char(&b);
251 debug("e %lx", e);
252 if (e < 30) {
253 e <<= 8;
254 e += buffer_get_char(&b);
255 debug("e %lx", e);
256 e <<= 8;
257 e += buffer_get_char(&b);
258 debug("e %lx", e);
259 }
260 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000261 buffer_free(&b);
262 key_free(key);
263 return NULL;
264 }
265 buffer_get_bignum_bits(&b, key->rsa->d);
266 buffer_get_bignum_bits(&b, key->rsa->n);
267 buffer_get_bignum_bits(&b, key->rsa->iqmp);
268 buffer_get_bignum_bits(&b, key->rsa->q);
269 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000270 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000271 break;
272 }
Damien Miller874d77b2000-10-14 16:23:11 +1100273 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000274 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000275 error("do_convert_private_ssh2_from_blob: "
276 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100277 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000278
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000279 /* try the key */
280 key_sign(key, &sig, &slen, data, sizeof(data));
281 key_verify(key, sig, slen, data, sizeof(data));
282 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100283 return key;
284}
285
Ben Lindstrombba81212001-06-25 05:01:22 +0000286static void
Damien Millereba71ba2000-04-29 23:57:08 +1000287do_convert_from_ssh2(struct passwd *pw)
288{
289 Key *k;
290 int blen;
291 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000292 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000293 char encoded[8096];
294 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100295 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000296 FILE *fp;
297
298 if (!have_identity)
299 ask_filename(pw, "Enter file in which the key is");
300 if (stat(identity_file, &st) < 0) {
301 perror(identity_file);
302 exit(1);
303 }
304 fp = fopen(identity_file, "r");
305 if (fp == NULL) {
306 perror(identity_file);
307 exit(1);
308 }
309 encoded[0] = '\0';
310 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000311 if (!(p = strchr(line, '\n'))) {
312 fprintf(stderr, "input line too long.\n");
313 exit(1);
314 }
315 if (p > line && p[-1] == '\\')
316 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000317 if (strncmp(line, "----", 4) == 0 ||
318 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100319 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
320 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000321 if (strstr(line, " END ") != NULL) {
322 break;
323 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000324 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000325 continue;
326 }
Damien Miller30c3d422000-05-09 11:02:59 +1000327 if (escaped) {
328 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000329 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000330 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000331 }
332 *p = '\0';
333 strlcat(encoded, line, sizeof(encoded));
334 }
Ben Lindstrom46c16222000-12-22 01:43:59 +0000335 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000336 if (blen < 0) {
337 fprintf(stderr, "uudecode failed.\n");
338 exit(1);
339 }
Damien Miller874d77b2000-10-14 16:23:11 +1100340 k = private ?
341 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100342 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100343 if (k == NULL) {
344 fprintf(stderr, "decode blob failed.\n");
345 exit(1);
346 }
347 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000348 (k->type == KEY_DSA ?
349 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
350 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100351 key_write(k, stdout);
352 if (!ok) {
353 fprintf(stderr, "key write failed");
354 exit(1);
355 }
Damien Millereba71ba2000-04-29 23:57:08 +1000356 key_free(k);
357 fprintf(stdout, "\n");
358 fclose(fp);
359 exit(0);
360}
361
Ben Lindstrombba81212001-06-25 05:01:22 +0000362static void
Damien Millereba71ba2000-04-29 23:57:08 +1000363do_print_public(struct passwd *pw)
364{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000365 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000366 struct stat st;
367
368 if (!have_identity)
369 ask_filename(pw, "Enter file in which the key is");
370 if (stat(identity_file, &st) < 0) {
371 perror(identity_file);
372 exit(1);
373 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000374 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000375 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000376 fprintf(stderr, "load failed\n");
377 exit(1);
378 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000379 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000380 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000381 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000382 fprintf(stdout, "\n");
383 exit(0);
384}
385
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000386#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000387#define NUM_RSA_KEY_ELEMENTS 5+1
388#define COPY_RSA_KEY(x, i) \
389 do { \
390 len = BN_num_bytes(prv->rsa->x); \
391 elements[i] = xmalloc(len); \
Ben Lindstrom7feba352001-07-04 05:06:59 +0000392 debug("#bytes %d", len); \
Ben Lindstromcd392282001-07-04 03:44:03 +0000393 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
394 goto done; \
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000395 } while (0)
Ben Lindstromcd392282001-07-04 03:44:03 +0000396
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000397static int
398get_AUT0(char *aut0)
399{
400 EVP_MD *evp_md = EVP_sha1();
401 EVP_MD_CTX md;
402 char *pass;
403
404 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
405 if (pass == NULL)
406 return -1;
407 EVP_DigestInit(&md, evp_md);
408 EVP_DigestUpdate(&md, pass, strlen(pass));
409 EVP_DigestFinal(&md, aut0, NULL);
410 memset(pass, 0, strlen(pass));
411 xfree(pass);
412 return 0;
413}
414
Ben Lindstromcd392282001-07-04 03:44:03 +0000415static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000416do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000417{
Ben Lindstromcd392282001-07-04 03:44:03 +0000418 Key *prv = NULL;
419 struct stat st;
420 u_char *elements[NUM_RSA_KEY_ELEMENTS];
421 u_char key_fid[2];
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000422 u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
423 u_char AUT0[EVP_MAX_MD_SIZE];
Ben Lindstromcd392282001-07-04 03:44:03 +0000424 int len, status = 1, i, fd = -1, ret;
Ben Lindstrom00477642001-07-04 05:24:27 +0000425 int sw = 0, cla = 0x00;
Ben Lindstromcd392282001-07-04 03:44:03 +0000426
Ben Lindstromd6e049c2001-07-04 05:08:39 +0000427 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
428 elements[i] = NULL;
Ben Lindstromcd392282001-07-04 03:44:03 +0000429 if (!have_identity)
430 ask_filename(pw, "Enter file in which the key is");
431 if (stat(identity_file, &st) < 0) {
432 perror(identity_file);
433 goto done;
434 }
435 prv = load_identity(identity_file);
436 if (prv == NULL) {
437 error("load failed");
438 goto done;
439 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000440 COPY_RSA_KEY(q, 0);
441 COPY_RSA_KEY(p, 1);
442 COPY_RSA_KEY(iqmp, 2);
443 COPY_RSA_KEY(dmq1, 3);
444 COPY_RSA_KEY(dmp1, 4);
445 COPY_RSA_KEY(n, 5);
446 len = BN_num_bytes(prv->rsa->n);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000447 fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000448 if (fd < 0) {
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000449 error("sectok_open failed: %s", sectok_get_sw(sw));
450 goto done;
451 }
452 if (! sectok_cardpresent(fd)) {
453 error("smartcard in reader %s not present",
454 sc_reader_id);
Ben Lindstromcd392282001-07-04 03:44:03 +0000455 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000456 }
Ben Lindstrom60df8e42001-08-06 21:10:52 +0000457 ret = sectok_reset(fd, 0, NULL, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000458 if (ret <= 0) {
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000459 error("sectok_reset failed: %s", sectok_get_sw(sw));
Ben Lindstromcd392282001-07-04 03:44:03 +0000460 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000461 }
Ben Lindstrom680b2762001-07-04 05:00:11 +0000462 if ((cla = cyberflex_inq_class(fd)) < 0) {
463 error("cyberflex_inq_class failed");
464 goto done;
465 }
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000466 memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
467 if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
468 if (get_AUT0(AUT0) < 0 ||
469 cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
470 error("cyberflex_verify_AUT0 failed");
471 goto done;
472 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000473 }
474 key_fid[0] = 0x00;
475 key_fid[1] = 0x12;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000476 if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
Ben Lindstrom00477642001-07-04 05:24:27 +0000477 &sw) < 0) {
478 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000479 goto done;
480 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000481 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000482 goto done;
483 log("cyberflex_load_rsa_priv done");
484 key_fid[0] = 0x73;
485 key_fid[1] = 0x68;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000486 if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
Ben Lindstrom00477642001-07-04 05:24:27 +0000487 &sw) < 0) {
488 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000489 goto done;
490 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000491 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000492 goto done;
493 log("cyberflex_load_rsa_pub done");
494 status = 0;
495 log("loading key done");
496done:
Ben Lindstrom1af4d3b2001-10-03 17:18:37 +0000497
498 memset(elements[0], '\0', BN_num_bytes(prv->rsa->q));
499 memset(elements[1], '\0', BN_num_bytes(prv->rsa->p));
500 memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp));
501 memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1));
502 memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1));
503 memset(elements[5], '\0', BN_num_bytes(prv->rsa->n));
504
Ben Lindstromcd392282001-07-04 03:44:03 +0000505 if (prv)
506 key_free(prv);
507 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
Ben Lindstrom00477642001-07-04 05:24:27 +0000508 if (elements[i])
509 xfree(elements[i]);
Ben Lindstromcd392282001-07-04 03:44:03 +0000510 if (fd != -1)
Ben Lindstrom00477642001-07-04 05:24:27 +0000511 sectok_close(fd);
Ben Lindstromcd392282001-07-04 03:44:03 +0000512 exit(status);
Ben Lindstromcd392282001-07-04 03:44:03 +0000513}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000514
515static void
516do_download(struct passwd *pw, const char *sc_reader_id)
517{
518 Key *pub = NULL;
519
520 pub = sc_get_key(sc_reader_id);
521 if (pub == NULL)
522 fatal("cannot read public key from smartcard");
523 key_write(pub, stdout);
524 key_free(pub);
525 fprintf(stdout, "\n");
526 exit(0);
527}
Ben Lindstromffce1472001-08-06 21:57:31 +0000528#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000529
Ben Lindstrombba81212001-06-25 05:01:22 +0000530static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100531do_fingerprint(struct passwd *pw)
532{
Damien Miller98c7ad62000-03-09 21:27:49 +1100533 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000534 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000535 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000536 int i, skip = 0, num = 1, invalid = 1;
537 enum fp_rep rep;
538 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100539 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100540
Ben Lindstromd0fca422001-03-26 13:44:06 +0000541 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
542 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000543
Damien Miller95def091999-11-25 00:26:21 +1100544 if (!have_identity)
545 ask_filename(pw, "Enter file in which the key is");
546 if (stat(identity_file, &st) < 0) {
547 perror(identity_file);
548 exit(1);
549 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000550 public = key_load_public(identity_file, &comment);
551 if (public != NULL) {
552 fp = key_fingerprint(public, fptype, rep);
553 printf("%d %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100554 key_free(public);
555 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000556 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100557 exit(0);
558 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000559 if (comment)
560 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100561
562 f = fopen(identity_file, "r");
563 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100564 while (fgets(line, sizeof(line), f)) {
565 i = strlen(line) - 1;
566 if (line[i] != '\n') {
567 error("line %d too long: %.40s...", num, line);
568 skip = 1;
569 continue;
Damien Miller95def091999-11-25 00:26:21 +1100570 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100571 num++;
572 if (skip) {
573 skip = 0;
574 continue;
575 }
576 line[i] = '\0';
577
578 /* Skip leading whitespace, empty and comment lines. */
579 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
580 ;
581 if (!*cp || *cp == '\n' || *cp == '#')
582 continue ;
583 i = strtol(cp, &ep, 10);
584 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
585 int quoted = 0;
586 comment = cp;
587 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
588 if (*cp == '\\' && cp[1] == '"')
589 cp++; /* Skip both */
590 else if (*cp == '"')
591 quoted = !quoted;
592 }
593 if (!*cp)
594 continue;
595 *cp++ = '\0';
596 }
597 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000598 public = key_new(KEY_RSA1);
599 if (key_read(public, &cp) != 1) {
600 cp = ep;
601 key_free(public);
602 public = key_new(KEY_UNSPEC);
603 if (key_read(public, &cp) != 1) {
604 key_free(public);
605 continue;
606 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100607 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000608 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000609 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000610 printf("%d %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000611 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000612 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000613 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000614 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100615 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100616 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100617 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100618 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100619 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100620 exit(1);
621 }
Damien Miller95def091999-11-25 00:26:21 +1100622 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100623}
624
Damien Miller95def091999-11-25 00:26:21 +1100625/*
626 * Perform changing a passphrase. The argument is the passwd structure
627 * for the current user.
628 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000629static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100630do_change_passphrase(struct passwd *pw)
631{
Damien Miller95def091999-11-25 00:26:21 +1100632 char *comment;
633 char *old_passphrase, *passphrase1, *passphrase2;
634 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000635 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100636
Damien Miller95def091999-11-25 00:26:21 +1100637 if (!have_identity)
638 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100639 if (stat(identity_file, &st) < 0) {
640 perror(identity_file);
641 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642 }
Damien Miller95def091999-11-25 00:26:21 +1100643 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000644 private = key_load_private(identity_file, "", &comment);
645 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100646 if (identity_passphrase)
647 old_passphrase = xstrdup(identity_passphrase);
648 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000649 old_passphrase =
650 read_passphrase("Enter old passphrase: ",
651 RP_ALLOW_STDIN);
652 private = key_load_private(identity_file, old_passphrase,
653 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000654 memset(old_passphrase, 0, strlen(old_passphrase));
655 xfree(old_passphrase);
656 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100657 printf("Bad passphrase.\n");
658 exit(1);
659 }
Damien Miller95def091999-11-25 00:26:21 +1100660 }
661 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller95def091999-11-25 00:26:21 +1100663 /* Ask the new passphrase (twice). */
664 if (identity_new_passphrase) {
665 passphrase1 = xstrdup(identity_new_passphrase);
666 passphrase2 = NULL;
667 } else {
668 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000669 read_passphrase("Enter new passphrase (empty for no "
670 "passphrase): ", RP_ALLOW_STDIN);
671 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100672 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100673
674 /* Verify that they are the same. */
675 if (strcmp(passphrase1, passphrase2) != 0) {
676 memset(passphrase1, 0, strlen(passphrase1));
677 memset(passphrase2, 0, strlen(passphrase2));
678 xfree(passphrase1);
679 xfree(passphrase2);
680 printf("Pass phrases do not match. Try again.\n");
681 exit(1);
682 }
683 /* Destroy the other copy. */
684 memset(passphrase2, 0, strlen(passphrase2));
685 xfree(passphrase2);
686 }
687
688 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000689 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000690 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100691 memset(passphrase1, 0, strlen(passphrase1));
692 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000693 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100694 xfree(comment);
695 exit(1);
696 }
697 /* Destroy the passphrase and the copy of the key in memory. */
698 memset(passphrase1, 0, strlen(passphrase1));
699 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000700 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100701 xfree(comment);
702
703 printf("Your identification has been saved with the new passphrase.\n");
704 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705}
706
Damien Miller95def091999-11-25 00:26:21 +1100707/*
708 * Change the comment of a private key file.
709 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000710static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711do_change_comment(struct passwd *pw)
712{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000713 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000714 Key *private;
715 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100716 struct stat st;
717 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000718 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
Damien Miller95def091999-11-25 00:26:21 +1100720 if (!have_identity)
721 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100722 if (stat(identity_file, &st) < 0) {
723 perror(identity_file);
724 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000726 private = key_load_private(identity_file, "", &comment);
727 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100728 if (identity_passphrase)
729 passphrase = xstrdup(identity_passphrase);
730 else if (identity_new_passphrase)
731 passphrase = xstrdup(identity_new_passphrase);
732 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000733 passphrase = read_passphrase("Enter passphrase: ",
734 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100735 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000736 private = key_load_private(identity_file, passphrase, &comment);
737 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100738 memset(passphrase, 0, strlen(passphrase));
739 xfree(passphrase);
740 printf("Bad passphrase.\n");
741 exit(1);
742 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000743 } else {
744 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100745 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000746 if (private->type != KEY_RSA1) {
747 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
748 key_free(private);
749 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100750 }
Damien Miller95def091999-11-25 00:26:21 +1100751 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752
Damien Miller95def091999-11-25 00:26:21 +1100753 if (identity_comment) {
754 strlcpy(new_comment, identity_comment, sizeof(new_comment));
755 } else {
756 printf("Enter new comment: ");
757 fflush(stdout);
758 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
759 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000760 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100761 exit(1);
762 }
Damien Miller95def091999-11-25 00:26:21 +1100763 if (strchr(new_comment, '\n'))
764 *strchr(new_comment, '\n') = 0;
765 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766
Damien Miller95def091999-11-25 00:26:21 +1100767 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000768 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000769 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100770 memset(passphrase, 0, strlen(passphrase));
771 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000772 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100773 xfree(comment);
774 exit(1);
775 }
Damien Miller95def091999-11-25 00:26:21 +1100776 memset(passphrase, 0, strlen(passphrase));
777 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000778 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000779 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780
Damien Miller95def091999-11-25 00:26:21 +1100781 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000782 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
783 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100784 printf("Could not save your public key in %s\n", identity_file);
785 exit(1);
786 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000787 f = fdopen(fd, "w");
788 if (f == NULL) {
789 printf("fdopen %s failed", identity_file);
790 exit(1);
791 }
Damien Millereba71ba2000-04-29 23:57:08 +1000792 if (!key_write(public, f))
793 fprintf(stderr, "write key failed");
794 key_free(public);
795 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100796 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797
Damien Miller95def091999-11-25 00:26:21 +1100798 xfree(comment);
799
800 printf("The comment in your key file has been changed.\n");
801 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000802}
803
Ben Lindstrombba81212001-06-25 05:01:22 +0000804static void
Damien Miller431f66b1999-11-21 18:31:57 +1100805usage(void)
806{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000807 fprintf(stderr, "Usage: %s [options]\n", __progname);
808 fprintf(stderr, "Options:\n");
809 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
810 fprintf(stderr, " -c Change comment in private and public key files.\n");
811 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
812 fprintf(stderr, " -f filename Filename of the key file.\n");
813 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
814 fprintf(stderr, " -l Show fingerprint of key file.\n");
815 fprintf(stderr, " -p Change passphrase of private key file.\n");
816 fprintf(stderr, " -q Quiet.\n");
817 fprintf(stderr, " -y Read private key file and print public key.\n");
818 fprintf(stderr, " -t type Specify type of key to create.\n");
819 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
820 fprintf(stderr, " -C comment Provide new comment.\n");
821 fprintf(stderr, " -N phrase Provide new passphrase.\n");
822 fprintf(stderr, " -P phrase Provide old passphrase.\n");
823#ifdef SMARTCARD
824 fprintf(stderr, " -D reader Download public key from smartcard.\n");
825 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
826#endif /* SMARTCARD */
827
Damien Miller95def091999-11-25 00:26:21 +1100828 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100829}
830
Damien Miller95def091999-11-25 00:26:21 +1100831/*
832 * Main program for key management.
833 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000834int
835main(int ac, char **av)
836{
Damien Millera41c8b12002-01-22 23:05:08 +1100837 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000838 char *reader_id = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000839 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100840 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100841 struct stat st;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000842 int opt, type, fd, download = 0;
Damien Miller95def091999-11-25 00:26:21 +1100843 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100844
Damien Miller95def091999-11-25 00:26:21 +1100845 extern int optind;
846 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000848 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000849 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100850 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000851
Damien Millerd3a18572000-06-07 19:55:44 +1000852 SSLeay_add_all_algorithms();
Damien Millereba71ba2000-04-29 23:57:08 +1000853
Damien Miller5428f641999-11-25 11:54:57 +1100854 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100855 pw = getpwuid(getuid());
856 if (!pw) {
857 printf("You don't exist, go away!\n");
858 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859 }
Damien Millereba71ba2000-04-29 23:57:08 +1000860 if (gethostname(hostname, sizeof(hostname)) < 0) {
861 perror("gethostname");
862 exit(1);
863 }
Damien Miller5428f641999-11-25 11:54:57 +1100864
Ben Lindstromf19578c2001-08-06 21:46:54 +0000865 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100866 switch (opt) {
867 case 'b':
868 bits = atoi(optarg);
869 if (bits < 512 || bits > 32768) {
870 printf("Bits has bad value.\n");
871 exit(1);
872 }
873 break;
Damien Miller95def091999-11-25 00:26:21 +1100874 case 'l':
875 print_fingerprint = 1;
876 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000877 case 'B':
878 print_bubblebabble = 1;
879 break;
Damien Miller95def091999-11-25 00:26:21 +1100880 case 'p':
881 change_passphrase = 1;
882 break;
Damien Miller95def091999-11-25 00:26:21 +1100883 case 'c':
884 change_comment = 1;
885 break;
Damien Miller95def091999-11-25 00:26:21 +1100886 case 'f':
887 strlcpy(identity_file, optarg, sizeof(identity_file));
888 have_identity = 1;
889 break;
Damien Miller95def091999-11-25 00:26:21 +1100890 case 'P':
891 identity_passphrase = optarg;
892 break;
Damien Miller95def091999-11-25 00:26:21 +1100893 case 'N':
894 identity_new_passphrase = optarg;
895 break;
Damien Miller95def091999-11-25 00:26:21 +1100896 case 'C':
897 identity_comment = optarg;
898 break;
Damien Miller95def091999-11-25 00:26:21 +1100899 case 'q':
900 quiet = 1;
901 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000902 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100903 /* unused */
904 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000905 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000906 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000907 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000908 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000909 convert_to_ssh2 = 1;
910 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000911 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000912 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000913 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000914 convert_from_ssh2 = 1;
915 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000916 case 'y':
917 print_public = 1;
918 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000919 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100920 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000921 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100922 case 't':
923 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100924 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000925 case 'D':
926 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000927 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000928 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000929 break;
Damien Miller95def091999-11-25 00:26:21 +1100930 case '?':
931 default:
932 usage();
933 }
934 }
935 if (optind < ac) {
936 printf("Too many arguments.\n");
937 usage();
938 }
Damien Millera41c8b12002-01-22 23:05:08 +1100939 if (key_type_name == NULL) {
940 printf("You must specify a key type (-t).\n");
941 usage();
942 }
Damien Miller95def091999-11-25 00:26:21 +1100943 if (change_passphrase && change_comment) {
944 printf("Can only have one of -p and -c.\n");
945 usage();
946 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000947 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100948 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100949 if (change_passphrase)
950 do_change_passphrase(pw);
Damien Miller95def091999-11-25 00:26:21 +1100951 if (change_comment)
952 do_change_comment(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000953 if (convert_to_ssh2)
954 do_convert_to_ssh2(pw);
955 if (convert_from_ssh2)
956 do_convert_from_ssh2(pw);
957 if (print_public)
958 do_print_public(pw);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000959 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000960#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000961 if (download)
962 do_download(pw, reader_id);
963 else
964 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000965#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000966 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000967#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000968 }
Damien Miller95def091999-11-25 00:26:21 +1100969
970 arc4random_stir();
971
Damien Millere39cacc2000-11-29 12:18:44 +1100972 type = key_type_from_name(key_type_name);
973 if (type == KEY_UNSPEC) {
974 fprintf(stderr, "unknown key type %s\n", key_type_name);
975 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000976 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100977 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100978 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100979 private = key_generate(type, bits);
980 if (private == NULL) {
981 fprintf(stderr, "key_generate failed");
982 exit(1);
983 }
984 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100985
986 if (!have_identity)
987 ask_filename(pw, "Enter file in which to save the key");
988
989 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000990 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100991 if (strstr(identity_file, dotsshdir) != NULL &&
992 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000993 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100994 error("Could not create directory '%s'.", dotsshdir);
995 else if (!quiet)
996 printf("Created directory '%s'.\n", dotsshdir);
997 }
998 /* If the file already exists, ask the user to confirm. */
999 if (stat(identity_file, &st) >= 0) {
1000 char yesno[3];
1001 printf("%s already exists.\n", identity_file);
1002 printf("Overwrite (y/n)? ");
1003 fflush(stdout);
1004 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1005 exit(1);
1006 if (yesno[0] != 'y' && yesno[0] != 'Y')
1007 exit(1);
1008 }
1009 /* Ask for a passphrase (twice). */
1010 if (identity_passphrase)
1011 passphrase1 = xstrdup(identity_passphrase);
1012 else if (identity_new_passphrase)
1013 passphrase1 = xstrdup(identity_new_passphrase);
1014 else {
1015passphrase_again:
1016 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001017 read_passphrase("Enter passphrase (empty for no "
1018 "passphrase): ", RP_ALLOW_STDIN);
1019 passphrase2 = read_passphrase("Enter same passphrase again: ",
1020 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001021 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001022 /*
1023 * The passphrases do not match. Clear them and
1024 * retry.
1025 */
Damien Miller95def091999-11-25 00:26:21 +11001026 memset(passphrase1, 0, strlen(passphrase1));
1027 memset(passphrase2, 0, strlen(passphrase2));
1028 xfree(passphrase1);
1029 xfree(passphrase2);
1030 printf("Passphrases do not match. Try again.\n");
1031 goto passphrase_again;
1032 }
1033 /* Clear the other copy of the passphrase. */
1034 memset(passphrase2, 0, strlen(passphrase2));
1035 xfree(passphrase2);
1036 }
1037
Damien Miller95def091999-11-25 00:26:21 +11001038 if (identity_comment) {
1039 strlcpy(comment, identity_comment, sizeof(comment));
1040 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001041 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001042 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1043 }
1044
1045 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001046 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001047 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001048 memset(passphrase1, 0, strlen(passphrase1));
1049 xfree(passphrase1);
1050 exit(1);
1051 }
1052 /* Clear the passphrase. */
1053 memset(passphrase1, 0, strlen(passphrase1));
1054 xfree(passphrase1);
1055
1056 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001057 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001058 arc4random_stir();
1059
1060 if (!quiet)
1061 printf("Your identification has been saved in %s.\n", identity_file);
1062
Damien Miller95def091999-11-25 00:26:21 +11001063 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001064 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1065 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001066 printf("Could not save your public key in %s\n", identity_file);
1067 exit(1);
1068 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001069 f = fdopen(fd, "w");
1070 if (f == NULL) {
1071 printf("fdopen %s failed", identity_file);
1072 exit(1);
1073 }
Damien Millereba71ba2000-04-29 23:57:08 +10001074 if (!key_write(public, f))
1075 fprintf(stderr, "write key failed");
1076 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001077 fclose(f);
1078
1079 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001080 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001081 printf("Your public key has been saved in %s.\n",
1082 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001083 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001084 printf("%s %s\n", fp, comment);
1085 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001086 }
Damien Millereba71ba2000-04-29 23:57:08 +10001087
1088 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001089 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090}