blob: 5fadad7cc9eeed60ef05322d398b303479e474ac [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 Lindstrom6818bfb2001-08-06 21:40:04 +000015RCSID("$OpenBSD: ssh-keygen.c,v 1.74 2001/08/01 23:33:09 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
Ben Lindstromcd392282001-07-04 03:44:03 +000020#ifdef SMARTCARD
21#include <sectok.h>
22#endif
23
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100025#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000026#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100027#include "authfile.h"
28#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110029#include "buffer.h"
30#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000031#include "pathnames.h"
32#include "log.h"
33#include "readpass.h"
Damien Miller874d77b2000-10-14 16:23:11 +110034
Ben Lindstromcd392282001-07-04 03:44:03 +000035
Damien Millereba71ba2000-04-29 23:57:08 +100036/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037int bits = 1024;
38
Damien Miller5428f641999-11-25 11:54:57 +110039/*
40 * Flag indicating that we just want to change the passphrase. This can be
41 * set on the command line.
42 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043int change_passphrase = 0;
44
Damien Miller5428f641999-11-25 11:54:57 +110045/*
46 * Flag indicating that we just want to change the comment. This can be set
47 * on the command line.
48 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049int change_comment = 0;
50
51int quiet = 0;
52
Damien Miller10f6f6b1999-11-17 17:29:08 +110053/* Flag indicating that we just want to see the key fingerprint */
54int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000055int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110056
Damien Miller431f66b1999-11-21 18:31:57 +110057/* The identity file name, given on the command line or entered by the user. */
58char identity_file[1024];
59int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
61/* This is set to the passphrase if given on the command line. */
62char *identity_passphrase = NULL;
63
64/* This is set to the new passphrase if given on the command line. */
65char *identity_new_passphrase = NULL;
66
67/* This is set to the new comment if given on the command line. */
68char *identity_comment = NULL;
69
Damien Millereba71ba2000-04-29 23:57:08 +100070/* Dump public key file in format used by real and the original SSH 2 */
71int convert_to_ssh2 = 0;
72int convert_from_ssh2 = 0;
73int print_public = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110074
Damien Millere39cacc2000-11-29 12:18:44 +110075/* default to RSA for SSH-1 */
76char *key_type_name = "rsa1";
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 Lindstrom46c264f2001-04-24 16:56:58 +0000165 key_to_blob(k, &blob, &len);
Damien Miller874d77b2000-10-14 16:23:11 +1100166 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000167 fprintf(stdout,
Damien Miller874d77b2000-10-14 16:23:11 +1100168 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000169 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000170 pw->pw_name, hostname);
171 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100172 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000173 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000174 xfree(blob);
175 exit(0);
176}
177
Ben Lindstrombba81212001-06-25 05:01:22 +0000178static void
Damien Miller874d77b2000-10-14 16:23:11 +1100179buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
180{
181 int bits = buffer_get_int(b);
182 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000183
Damien Miller874d77b2000-10-14 16:23:11 +1100184 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000185 fatal("buffer_get_bignum_bits: input buffer too small: "
186 "need %d have %d", bytes, buffer_len(b));
Ben Lindstrom46c16222000-12-22 01:43:59 +0000187 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100188 buffer_consume(b, bytes);
189}
190
Ben Lindstrombba81212001-06-25 05:01:22 +0000191static Key *
Damien Miller874d77b2000-10-14 16:23:11 +1100192do_convert_private_ssh2_from_blob(char *blob, int blen)
193{
194 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100195 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100196 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000197 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000198 int magic, rlen, ktype, i1, i2, i3, i4;
199 u_int slen;
200 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100201
202 buffer_init(&b);
203 buffer_append(&b, blob, blen);
204
205 magic = buffer_get_int(&b);
206 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
207 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
208 buffer_free(&b);
209 return NULL;
210 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000211 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100212 type = buffer_get_string(&b, NULL);
213 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000214 i2 = buffer_get_int(&b);
215 i3 = buffer_get_int(&b);
216 i4 = buffer_get_int(&b);
217 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100218 if (strcmp(cipher, "none") != 0) {
219 error("unsupported cipher %s", cipher);
220 xfree(cipher);
221 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000222 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100223 return NULL;
224 }
225 xfree(cipher);
226
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000227 if (strstr(type, "dsa")) {
228 ktype = KEY_DSA;
229 } else if (strstr(type, "rsa")) {
230 ktype = KEY_RSA;
231 } else {
232 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100233 return NULL;
234 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000235 key = key_new_private(ktype);
236 xfree(type);
237
238 switch (key->type) {
239 case KEY_DSA:
240 buffer_get_bignum_bits(&b, key->dsa->p);
241 buffer_get_bignum_bits(&b, key->dsa->g);
242 buffer_get_bignum_bits(&b, key->dsa->q);
243 buffer_get_bignum_bits(&b, key->dsa->pub_key);
244 buffer_get_bignum_bits(&b, key->dsa->priv_key);
245 break;
246 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000247 e = buffer_get_char(&b);
248 debug("e %lx", e);
249 if (e < 30) {
250 e <<= 8;
251 e += buffer_get_char(&b);
252 debug("e %lx", e);
253 e <<= 8;
254 e += buffer_get_char(&b);
255 debug("e %lx", e);
256 }
257 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000258 buffer_free(&b);
259 key_free(key);
260 return NULL;
261 }
262 buffer_get_bignum_bits(&b, key->rsa->d);
263 buffer_get_bignum_bits(&b, key->rsa->n);
264 buffer_get_bignum_bits(&b, key->rsa->iqmp);
265 buffer_get_bignum_bits(&b, key->rsa->q);
266 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000267 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000268 break;
269 }
Damien Miller874d77b2000-10-14 16:23:11 +1100270 rlen = buffer_len(&b);
271 if(rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000272 error("do_convert_private_ssh2_from_blob: "
273 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100274 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000275
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000276 /* try the key */
277 key_sign(key, &sig, &slen, data, sizeof(data));
278 key_verify(key, sig, slen, data, sizeof(data));
279 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100280 return key;
281}
282
Ben Lindstrombba81212001-06-25 05:01:22 +0000283static void
Damien Millereba71ba2000-04-29 23:57:08 +1000284do_convert_from_ssh2(struct passwd *pw)
285{
286 Key *k;
287 int blen;
288 char line[1024], *p;
289 char blob[8096];
290 char encoded[8096];
291 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100292 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000293 FILE *fp;
294
295 if (!have_identity)
296 ask_filename(pw, "Enter file in which the key is");
297 if (stat(identity_file, &st) < 0) {
298 perror(identity_file);
299 exit(1);
300 }
301 fp = fopen(identity_file, "r");
302 if (fp == NULL) {
303 perror(identity_file);
304 exit(1);
305 }
306 encoded[0] = '\0';
307 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000308 if (!(p = strchr(line, '\n'))) {
309 fprintf(stderr, "input line too long.\n");
310 exit(1);
311 }
312 if (p > line && p[-1] == '\\')
313 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000314 if (strncmp(line, "----", 4) == 0 ||
315 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100316 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
317 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000318 if (strstr(line, " END ") != NULL) {
319 break;
320 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000321 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000322 continue;
323 }
Damien Miller30c3d422000-05-09 11:02:59 +1000324 if (escaped) {
325 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000326 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000327 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000328 }
329 *p = '\0';
330 strlcat(encoded, line, sizeof(encoded));
331 }
Ben Lindstrom46c16222000-12-22 01:43:59 +0000332 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000333 if (blen < 0) {
334 fprintf(stderr, "uudecode failed.\n");
335 exit(1);
336 }
Damien Miller874d77b2000-10-14 16:23:11 +1100337 k = private ?
338 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100339 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100340 if (k == NULL) {
341 fprintf(stderr, "decode blob failed.\n");
342 exit(1);
343 }
344 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000345 (k->type == KEY_DSA ?
346 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
347 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100348 key_write(k, stdout);
349 if (!ok) {
350 fprintf(stderr, "key write failed");
351 exit(1);
352 }
Damien Millereba71ba2000-04-29 23:57:08 +1000353 key_free(k);
354 fprintf(stdout, "\n");
355 fclose(fp);
356 exit(0);
357}
358
Ben Lindstrombba81212001-06-25 05:01:22 +0000359static void
Damien Millereba71ba2000-04-29 23:57:08 +1000360do_print_public(struct passwd *pw)
361{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000362 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000363 struct stat st;
364
365 if (!have_identity)
366 ask_filename(pw, "Enter file in which the key is");
367 if (stat(identity_file, &st) < 0) {
368 perror(identity_file);
369 exit(1);
370 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000371 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000372 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000373 fprintf(stderr, "load failed\n");
374 exit(1);
375 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000376 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000377 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000378 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000379 fprintf(stdout, "\n");
380 exit(0);
381}
382
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000383#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000384#define NUM_RSA_KEY_ELEMENTS 5+1
385#define COPY_RSA_KEY(x, i) \
386 do { \
387 len = BN_num_bytes(prv->rsa->x); \
388 elements[i] = xmalloc(len); \
Ben Lindstrom7feba352001-07-04 05:06:59 +0000389 debug("#bytes %d", len); \
Ben Lindstromcd392282001-07-04 03:44:03 +0000390 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
391 goto done; \
392 } while(0)
393
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000394static int
395get_AUT0(char *aut0)
396{
397 EVP_MD *evp_md = EVP_sha1();
398 EVP_MD_CTX md;
399 char *pass;
400
401 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
402 if (pass == NULL)
403 return -1;
404 EVP_DigestInit(&md, evp_md);
405 EVP_DigestUpdate(&md, pass, strlen(pass));
406 EVP_DigestFinal(&md, aut0, NULL);
407 memset(pass, 0, strlen(pass));
408 xfree(pass);
409 return 0;
410}
411
Ben Lindstromcd392282001-07-04 03:44:03 +0000412static void
413do_upload(struct passwd *pw, int reader)
414{
Ben Lindstromcd392282001-07-04 03:44:03 +0000415 Key *prv = NULL;
416 struct stat st;
417 u_char *elements[NUM_RSA_KEY_ELEMENTS];
418 u_char key_fid[2];
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000419 u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
420 u_char AUT0[EVP_MAX_MD_SIZE];
Ben Lindstromcd392282001-07-04 03:44:03 +0000421 int len, status = 1, i, fd = -1, ret;
Ben Lindstrom00477642001-07-04 05:24:27 +0000422 int sw = 0, cla = 0x00;
Ben Lindstromcd392282001-07-04 03:44:03 +0000423
Ben Lindstromd6e049c2001-07-04 05:08:39 +0000424 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
425 elements[i] = NULL;
Ben Lindstromcd392282001-07-04 03:44:03 +0000426 if (!have_identity)
427 ask_filename(pw, "Enter file in which the key is");
428 if (stat(identity_file, &st) < 0) {
429 perror(identity_file);
430 goto done;
431 }
432 prv = load_identity(identity_file);
433 if (prv == NULL) {
434 error("load failed");
435 goto done;
436 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000437 COPY_RSA_KEY(q, 0);
438 COPY_RSA_KEY(p, 1);
439 COPY_RSA_KEY(iqmp, 2);
440 COPY_RSA_KEY(dmq1, 3);
441 COPY_RSA_KEY(dmp1, 4);
442 COPY_RSA_KEY(n, 5);
443 len = BN_num_bytes(prv->rsa->n);
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000444 fd = sectok_open(reader, STONOWAIT, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000445 if (fd < 0) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000446 error("sectok_open failed: %s", sectok_get_sw(sw));
Ben Lindstromcd392282001-07-04 03:44:03 +0000447 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000448 }
Ben Lindstrom60df8e42001-08-06 21:10:52 +0000449 ret = sectok_reset(fd, 0, NULL, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000450 if (ret <= 0) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000451 error("sectok_reset failed: %s", sectok_get_sw(sw));
Ben Lindstromcd392282001-07-04 03:44:03 +0000452 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000453 }
Ben Lindstrom680b2762001-07-04 05:00:11 +0000454 if ((cla = cyberflex_inq_class(fd)) < 0) {
455 error("cyberflex_inq_class failed");
456 goto done;
457 }
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000458 memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
459 if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
460 if (get_AUT0(AUT0) < 0 ||
461 cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
462 error("cyberflex_verify_AUT0 failed");
463 goto done;
464 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000465 }
466 key_fid[0] = 0x00;
467 key_fid[1] = 0x12;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000468 if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
Ben Lindstrom00477642001-07-04 05:24:27 +0000469 &sw) < 0) {
470 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000471 goto done;
472 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000473 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000474 goto done;
475 log("cyberflex_load_rsa_priv done");
476 key_fid[0] = 0x73;
477 key_fid[1] = 0x68;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000478 if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
Ben Lindstrom00477642001-07-04 05:24:27 +0000479 &sw) < 0) {
480 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000481 goto done;
482 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000483 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000484 goto done;
485 log("cyberflex_load_rsa_pub done");
486 status = 0;
487 log("loading key done");
488done:
489 if (prv)
490 key_free(prv);
491 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
Ben Lindstrom00477642001-07-04 05:24:27 +0000492 if (elements[i])
493 xfree(elements[i]);
Ben Lindstromcd392282001-07-04 03:44:03 +0000494 if (fd != -1)
Ben Lindstrom00477642001-07-04 05:24:27 +0000495 sectok_close(fd);
Ben Lindstromcd392282001-07-04 03:44:03 +0000496 exit(status);
Ben Lindstromcd392282001-07-04 03:44:03 +0000497}
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000498#endif
Ben Lindstromcd392282001-07-04 03:44:03 +0000499
Ben Lindstrombba81212001-06-25 05:01:22 +0000500static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100501do_fingerprint(struct passwd *pw)
502{
Damien Miller98c7ad62000-03-09 21:27:49 +1100503 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000504 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000505 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000506 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
Damien Miller95def091999-11-25 00:26:21 +1100507 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100508
Ben Lindstromd0fca422001-03-26 13:44:06 +0000509 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
510 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000511
Damien Miller95def091999-11-25 00:26:21 +1100512 if (!have_identity)
513 ask_filename(pw, "Enter file in which the key is");
514 if (stat(identity_file, &st) < 0) {
515 perror(identity_file);
516 exit(1);
517 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000518 public = key_load_public(identity_file, &comment);
519 if (public != NULL) {
520 fp = key_fingerprint(public, fptype, rep);
521 printf("%d %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100522 key_free(public);
523 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000524 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100525 exit(0);
526 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000527 if (comment)
528 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100529
530 f = fopen(identity_file, "r");
531 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100532 while (fgets(line, sizeof(line), f)) {
533 i = strlen(line) - 1;
534 if (line[i] != '\n') {
535 error("line %d too long: %.40s...", num, line);
536 skip = 1;
537 continue;
Damien Miller95def091999-11-25 00:26:21 +1100538 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100539 num++;
540 if (skip) {
541 skip = 0;
542 continue;
543 }
544 line[i] = '\0';
545
546 /* Skip leading whitespace, empty and comment lines. */
547 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
548 ;
549 if (!*cp || *cp == '\n' || *cp == '#')
550 continue ;
551 i = strtol(cp, &ep, 10);
552 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
553 int quoted = 0;
554 comment = cp;
555 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
556 if (*cp == '\\' && cp[1] == '"')
557 cp++; /* Skip both */
558 else if (*cp == '"')
559 quoted = !quoted;
560 }
561 if (!*cp)
562 continue;
563 *cp++ = '\0';
564 }
565 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000566 public = key_new(KEY_RSA1);
567 if (key_read(public, &cp) != 1) {
568 cp = ep;
569 key_free(public);
570 public = key_new(KEY_UNSPEC);
571 if (key_read(public, &cp) != 1) {
572 key_free(public);
573 continue;
574 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100575 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000576 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000577 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000578 printf("%d %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000579 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000580 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000581 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000582 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100583 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100584 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100585 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100586 if (invalid) {
587 printf("%s is not a valid key file.\n", identity_file);
588 exit(1);
589 }
Damien Miller95def091999-11-25 00:26:21 +1100590 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100591}
592
Damien Miller95def091999-11-25 00:26:21 +1100593/*
594 * Perform changing a passphrase. The argument is the passwd structure
595 * for the current user.
596 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000597static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100598do_change_passphrase(struct passwd *pw)
599{
Damien Miller95def091999-11-25 00:26:21 +1100600 char *comment;
601 char *old_passphrase, *passphrase1, *passphrase2;
602 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000603 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100604
Damien Miller95def091999-11-25 00:26:21 +1100605 if (!have_identity)
606 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100607 if (stat(identity_file, &st) < 0) {
608 perror(identity_file);
609 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610 }
Damien Miller95def091999-11-25 00:26:21 +1100611 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000612 private = key_load_private(identity_file, "", &comment);
613 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100614 if (identity_passphrase)
615 old_passphrase = xstrdup(identity_passphrase);
616 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000617 old_passphrase =
618 read_passphrase("Enter old passphrase: ",
619 RP_ALLOW_STDIN);
620 private = key_load_private(identity_file, old_passphrase,
621 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000622 memset(old_passphrase, 0, strlen(old_passphrase));
623 xfree(old_passphrase);
624 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100625 printf("Bad passphrase.\n");
626 exit(1);
627 }
Damien Miller95def091999-11-25 00:26:21 +1100628 }
629 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630
Damien Miller95def091999-11-25 00:26:21 +1100631 /* Ask the new passphrase (twice). */
632 if (identity_new_passphrase) {
633 passphrase1 = xstrdup(identity_new_passphrase);
634 passphrase2 = NULL;
635 } else {
636 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000637 read_passphrase("Enter new passphrase (empty for no "
638 "passphrase): ", RP_ALLOW_STDIN);
639 passphrase2 = read_passphrase("Enter same passphrase again: ",
640 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100641
642 /* Verify that they are the same. */
643 if (strcmp(passphrase1, passphrase2) != 0) {
644 memset(passphrase1, 0, strlen(passphrase1));
645 memset(passphrase2, 0, strlen(passphrase2));
646 xfree(passphrase1);
647 xfree(passphrase2);
648 printf("Pass phrases do not match. Try again.\n");
649 exit(1);
650 }
651 /* Destroy the other copy. */
652 memset(passphrase2, 0, strlen(passphrase2));
653 xfree(passphrase2);
654 }
655
656 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000657 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000658 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100659 memset(passphrase1, 0, strlen(passphrase1));
660 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000661 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100662 xfree(comment);
663 exit(1);
664 }
665 /* Destroy the passphrase and the copy of the key in memory. */
666 memset(passphrase1, 0, strlen(passphrase1));
667 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000668 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100669 xfree(comment);
670
671 printf("Your identification has been saved with the new passphrase.\n");
672 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673}
674
Damien Miller95def091999-11-25 00:26:21 +1100675/*
676 * Change the comment of a private key file.
677 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000678static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679do_change_comment(struct passwd *pw)
680{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000681 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000682 Key *private;
683 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100684 struct stat st;
685 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000686 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687
Damien Miller95def091999-11-25 00:26:21 +1100688 if (!have_identity)
689 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100690 if (stat(identity_file, &st) < 0) {
691 perror(identity_file);
692 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000694 private = key_load_private(identity_file, "", &comment);
695 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100696 if (identity_passphrase)
697 passphrase = xstrdup(identity_passphrase);
698 else if (identity_new_passphrase)
699 passphrase = xstrdup(identity_new_passphrase);
700 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000701 passphrase = read_passphrase("Enter passphrase: ",
702 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100703 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000704 private = key_load_private(identity_file, passphrase, &comment);
705 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100706 memset(passphrase, 0, strlen(passphrase));
707 xfree(passphrase);
708 printf("Bad passphrase.\n");
709 exit(1);
710 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000711 } else {
712 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100713 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000714 if (private->type != KEY_RSA1) {
715 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
716 key_free(private);
717 exit(1);
718 }
Damien Miller95def091999-11-25 00:26:21 +1100719 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720
Damien Miller95def091999-11-25 00:26:21 +1100721 if (identity_comment) {
722 strlcpy(new_comment, identity_comment, sizeof(new_comment));
723 } else {
724 printf("Enter new comment: ");
725 fflush(stdout);
726 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
727 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000728 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100729 exit(1);
730 }
Damien Miller95def091999-11-25 00:26:21 +1100731 if (strchr(new_comment, '\n'))
732 *strchr(new_comment, '\n') = 0;
733 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734
Damien Miller95def091999-11-25 00:26:21 +1100735 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000736 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000737 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100738 memset(passphrase, 0, strlen(passphrase));
739 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000740 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100741 xfree(comment);
742 exit(1);
743 }
Damien Miller95def091999-11-25 00:26:21 +1100744 memset(passphrase, 0, strlen(passphrase));
745 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000746 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000747 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000748
Damien Miller95def091999-11-25 00:26:21 +1100749 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000750 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
751 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100752 printf("Could not save your public key in %s\n", identity_file);
753 exit(1);
754 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000755 f = fdopen(fd, "w");
756 if (f == NULL) {
757 printf("fdopen %s failed", identity_file);
758 exit(1);
759 }
Damien Millereba71ba2000-04-29 23:57:08 +1000760 if (!key_write(public, f))
761 fprintf(stderr, "write key failed");
762 key_free(public);
763 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100764 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765
Damien Miller95def091999-11-25 00:26:21 +1100766 xfree(comment);
767
768 printf("The comment in your key file has been changed.\n");
769 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770}
771
Ben Lindstrombba81212001-06-25 05:01:22 +0000772static void
Damien Miller431f66b1999-11-21 18:31:57 +1100773usage(void)
774{
Ben Lindstrom2857d9c2001-04-22 17:19:46 +0000775 printf("Usage: %s [-ceilpqyB] [-t type] [-b bits] [-f file] [-C comment] "
Ben Lindstromb7c92322001-03-05 05:10:52 +0000776 "[-N new-pass] [-P pass]\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100777 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100778}
779
Damien Miller95def091999-11-25 00:26:21 +1100780/*
781 * Main program for key management.
782 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783int
784main(int ac, char **av)
785{
Damien Miller95def091999-11-25 00:26:21 +1100786 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000787 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100788 struct passwd *pw;
Ben Lindstromcd392282001-07-04 03:44:03 +0000789 int opt, type, fd, reader = -1;
Damien Miller95def091999-11-25 00:26:21 +1100790 struct stat st;
791 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100792
Damien Miller95def091999-11-25 00:26:21 +1100793 extern int optind;
794 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000796 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000797 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100798 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000799
Damien Millerd3a18572000-06-07 19:55:44 +1000800 SSLeay_add_all_algorithms();
Damien Millereba71ba2000-04-29 23:57:08 +1000801
Damien Miller5428f641999-11-25 11:54:57 +1100802 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100803 pw = getpwuid(getuid());
804 if (!pw) {
805 printf("You don't exist, go away!\n");
806 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807 }
Damien Millereba71ba2000-04-29 23:57:08 +1000808 if (gethostname(hostname, sizeof(hostname)) < 0) {
809 perror("gethostname");
810 exit(1);
811 }
Damien Miller5428f641999-11-25 11:54:57 +1100812
Ben Lindstromcd392282001-07-04 03:44:03 +0000813 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:u:P:N:C:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100814 switch (opt) {
815 case 'b':
816 bits = atoi(optarg);
817 if (bits < 512 || bits > 32768) {
818 printf("Bits has bad value.\n");
819 exit(1);
820 }
821 break;
Damien Miller95def091999-11-25 00:26:21 +1100822 case 'l':
823 print_fingerprint = 1;
824 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000825 case 'B':
826 print_bubblebabble = 1;
827 break;
Damien Miller95def091999-11-25 00:26:21 +1100828 case 'p':
829 change_passphrase = 1;
830 break;
Damien Miller95def091999-11-25 00:26:21 +1100831 case 'c':
832 change_comment = 1;
833 break;
Damien Miller95def091999-11-25 00:26:21 +1100834 case 'f':
835 strlcpy(identity_file, optarg, sizeof(identity_file));
836 have_identity = 1;
837 break;
Damien Miller95def091999-11-25 00:26:21 +1100838 case 'P':
839 identity_passphrase = optarg;
840 break;
Damien Miller95def091999-11-25 00:26:21 +1100841 case 'N':
842 identity_new_passphrase = optarg;
843 break;
Damien Miller95def091999-11-25 00:26:21 +1100844 case 'C':
845 identity_comment = optarg;
846 break;
Damien Miller95def091999-11-25 00:26:21 +1100847 case 'q':
848 quiet = 1;
849 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000850 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100851 /* unused */
852 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000853 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000854 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000855 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000856 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000857 convert_to_ssh2 = 1;
858 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000859 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000860 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000861 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000862 convert_from_ssh2 = 1;
863 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000864 case 'y':
865 print_public = 1;
866 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000867 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100868 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000869 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100870 case 't':
871 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100872 break;
Ben Lindstromcd392282001-07-04 03:44:03 +0000873 case 'u':
874 reader = atoi(optarg); /*XXX*/
875 break;
Damien Miller95def091999-11-25 00:26:21 +1100876 case '?':
877 default:
878 usage();
879 }
880 }
881 if (optind < ac) {
882 printf("Too many arguments.\n");
883 usage();
884 }
885 if (change_passphrase && change_comment) {
886 printf("Can only have one of -p and -c.\n");
887 usage();
888 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000889 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100890 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100891 if (change_passphrase)
892 do_change_passphrase(pw);
Damien Miller95def091999-11-25 00:26:21 +1100893 if (change_comment)
894 do_change_comment(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000895 if (convert_to_ssh2)
896 do_convert_to_ssh2(pw);
897 if (convert_from_ssh2)
898 do_convert_from_ssh2(pw);
899 if (print_public)
900 do_print_public(pw);
Ben Lindstromcd392282001-07-04 03:44:03 +0000901 if (reader != -1)
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000902#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000903 do_upload(pw, reader);
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000904#else
905 fatal("no support for smartcards.");
906#endif
Damien Miller95def091999-11-25 00:26:21 +1100907
908 arc4random_stir();
909
Damien Millere39cacc2000-11-29 12:18:44 +1100910 type = key_type_from_name(key_type_name);
911 if (type == KEY_UNSPEC) {
912 fprintf(stderr, "unknown key type %s\n", key_type_name);
913 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000914 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100915 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100916 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100917 private = key_generate(type, bits);
918 if (private == NULL) {
919 fprintf(stderr, "key_generate failed");
920 exit(1);
921 }
922 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100923
924 if (!have_identity)
925 ask_filename(pw, "Enter file in which to save the key");
926
927 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000928 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100929 if (strstr(identity_file, dotsshdir) != NULL &&
930 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000931 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100932 error("Could not create directory '%s'.", dotsshdir);
933 else if (!quiet)
934 printf("Created directory '%s'.\n", dotsshdir);
935 }
936 /* If the file already exists, ask the user to confirm. */
937 if (stat(identity_file, &st) >= 0) {
938 char yesno[3];
939 printf("%s already exists.\n", identity_file);
940 printf("Overwrite (y/n)? ");
941 fflush(stdout);
942 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
943 exit(1);
944 if (yesno[0] != 'y' && yesno[0] != 'Y')
945 exit(1);
946 }
947 /* Ask for a passphrase (twice). */
948 if (identity_passphrase)
949 passphrase1 = xstrdup(identity_passphrase);
950 else if (identity_new_passphrase)
951 passphrase1 = xstrdup(identity_new_passphrase);
952 else {
953passphrase_again:
954 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000955 read_passphrase("Enter passphrase (empty for no "
956 "passphrase): ", RP_ALLOW_STDIN);
957 passphrase2 = read_passphrase("Enter same passphrase again: ",
958 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100959 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000960 /*
961 * The passphrases do not match. Clear them and
962 * retry.
963 */
Damien Miller95def091999-11-25 00:26:21 +1100964 memset(passphrase1, 0, strlen(passphrase1));
965 memset(passphrase2, 0, strlen(passphrase2));
966 xfree(passphrase1);
967 xfree(passphrase2);
968 printf("Passphrases do not match. Try again.\n");
969 goto passphrase_again;
970 }
971 /* Clear the other copy of the passphrase. */
972 memset(passphrase2, 0, strlen(passphrase2));
973 xfree(passphrase2);
974 }
975
Damien Miller95def091999-11-25 00:26:21 +1100976 if (identity_comment) {
977 strlcpy(comment, identity_comment, sizeof(comment));
978 } else {
Damien Miller4af51302000-04-16 11:18:38 +1000979 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +1100980 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
981 }
982
983 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000984 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000985 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100986 memset(passphrase1, 0, strlen(passphrase1));
987 xfree(passphrase1);
988 exit(1);
989 }
990 /* Clear the passphrase. */
991 memset(passphrase1, 0, strlen(passphrase1));
992 xfree(passphrase1);
993
994 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100995 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100996 arc4random_stir();
997
998 if (!quiet)
999 printf("Your identification has been saved in %s.\n", identity_file);
1000
Damien Miller95def091999-11-25 00:26:21 +11001001 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001002 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1003 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001004 printf("Could not save your public key in %s\n", identity_file);
1005 exit(1);
1006 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001007 f = fdopen(fd, "w");
1008 if (f == NULL) {
1009 printf("fdopen %s failed", identity_file);
1010 exit(1);
1011 }
Damien Millereba71ba2000-04-29 23:57:08 +10001012 if (!key_write(public, f))
1013 fprintf(stderr, "write key failed");
1014 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001015 fclose(f);
1016
1017 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001018 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001019 printf("Your public key has been saved in %s.\n",
1020 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001021 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001022 printf("%s %s\n", fp, comment);
1023 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001024 }
Damien Millereba71ba2000-04-29 23:57:08 +10001025
1026 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001027 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028}