blob: beffa928c9f4cda9a3450e4e938ec31e577fce5c [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 Lindstromf19578c2001-08-06 21:46:54 +000015RCSID("$OpenBSD: ssh-keygen.c,v 1.76 2001/08/02 08:58:35 jakob Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Damien Millereba71ba2000-04-29 23:57:08 +100017#include <openssl/evp.h>
18#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100019
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100021#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000022#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100023#include "authfile.h"
24#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "buffer.h"
26#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "pathnames.h"
28#include "log.h"
29#include "readpass.h"
Damien Miller874d77b2000-10-14 16:23:11 +110030
Ben Lindstrom8282d6a2001-08-06 21:44:05 +000031#ifdef SMARTCARD
32#include <sectok.h>
33#include <openssl/engine.h>
34#include "scard.h"
35#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 Millere39cacc2000-11-29 12:18:44 +110076/* default to RSA for SSH-1 */
77char *key_type_name = "rsa1";
Damien Millereba71ba2000-04-29 23:57:08 +100078
Damien Miller431f66b1999-11-21 18:31:57 +110079/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110080#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110081extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000082#else
83char *__progname;
84#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Millereba71ba2000-04-29 23:57:08 +100086char hostname[MAXHOSTNAMELEN];
87
Ben Lindstrombba81212001-06-25 05:01:22 +000088static void
Damien Miller431f66b1999-11-21 18:31:57 +110089ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090{
Damien Miller95def091999-11-25 00:26:21 +110091 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110092 char *name = NULL;
93
94 switch (key_type_from_name(key_type_name)) {
95 case KEY_RSA1:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000096 name = _PATH_SSH_CLIENT_IDENTITY;
Damien Millere39cacc2000-11-29 12:18:44 +110097 break;
98 case KEY_DSA:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000099 name = _PATH_SSH_CLIENT_ID_DSA;
Damien Millere39cacc2000-11-29 12:18:44 +1100100 break;
101 case KEY_RSA:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000102 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Millere39cacc2000-11-29 12:18:44 +1100103 break;
104 default:
105 fprintf(stderr, "bad key type");
106 exit(1);
107 break;
108 }
109 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000110 fprintf(stderr, "%s (%s): ", prompt, identity_file);
111 fflush(stderr);
Damien Miller95def091999-11-25 00:26:21 +1100112 if (fgets(buf, sizeof(buf), stdin) == NULL)
113 exit(1);
114 if (strchr(buf, '\n'))
115 *strchr(buf, '\n') = 0;
116 if (strcmp(buf, "") != 0)
117 strlcpy(identity_file, buf, sizeof(identity_file));
118 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100119}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Ben Lindstrombba81212001-06-25 05:01:22 +0000121static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000122load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000123{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000124 char *pass;
125 Key *prv;
126
Ben Lindstroma3700052001-04-05 23:26:32 +0000127 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000128 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000129 if (identity_passphrase)
130 pass = xstrdup(identity_passphrase);
131 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000132 pass = read_passphrase("Enter passphrase: ",
133 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000134 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000135 memset(pass, 0, strlen(pass));
136 xfree(pass);
137 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000138 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000139}
140
Damien Miller874d77b2000-10-14 16:23:11 +1100141#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
142#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
143#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000144#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000145
Ben Lindstrombba81212001-06-25 05:01:22 +0000146static void
Damien Millereba71ba2000-04-29 23:57:08 +1000147do_convert_to_ssh2(struct passwd *pw)
148{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000149 Key *k;
Damien Millereba71ba2000-04-29 23:57:08 +1000150 int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000151 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000152 struct stat st;
153
154 if (!have_identity)
155 ask_filename(pw, "Enter file in which the key is");
156 if (stat(identity_file, &st) < 0) {
157 perror(identity_file);
158 exit(1);
159 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000160 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000161 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000162 fprintf(stderr, "load failed\n");
163 exit(1);
164 }
Damien Millereba71ba2000-04-29 23:57:08 +1000165 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000166 key_to_blob(k, &blob, &len);
Damien Miller874d77b2000-10-14 16:23:11 +1100167 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000168 fprintf(stdout,
Damien Miller874d77b2000-10-14 16:23:11 +1100169 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000170 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000171 pw->pw_name, hostname);
172 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100173 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000174 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000175 xfree(blob);
176 exit(0);
177}
178
Ben Lindstrombba81212001-06-25 05:01:22 +0000179static void
Damien Miller874d77b2000-10-14 16:23:11 +1100180buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
181{
182 int bits = buffer_get_int(b);
183 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000184
Damien Miller874d77b2000-10-14 16:23:11 +1100185 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000186 fatal("buffer_get_bignum_bits: input buffer too small: "
187 "need %d have %d", bytes, buffer_len(b));
Ben Lindstrom46c16222000-12-22 01:43:59 +0000188 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100189 buffer_consume(b, bytes);
190}
191
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static Key *
Damien Miller874d77b2000-10-14 16:23:11 +1100193do_convert_private_ssh2_from_blob(char *blob, int blen)
194{
195 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100196 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100197 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000198 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000199 int magic, rlen, ktype, i1, i2, i3, i4;
200 u_int slen;
201 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100202
203 buffer_init(&b);
204 buffer_append(&b, blob, blen);
205
206 magic = buffer_get_int(&b);
207 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
208 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
209 buffer_free(&b);
210 return NULL;
211 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000212 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100213 type = buffer_get_string(&b, NULL);
214 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000215 i2 = buffer_get_int(&b);
216 i3 = buffer_get_int(&b);
217 i4 = buffer_get_int(&b);
218 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100219 if (strcmp(cipher, "none") != 0) {
220 error("unsupported cipher %s", cipher);
221 xfree(cipher);
222 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000223 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100224 return NULL;
225 }
226 xfree(cipher);
227
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000228 if (strstr(type, "dsa")) {
229 ktype = KEY_DSA;
230 } else if (strstr(type, "rsa")) {
231 ktype = KEY_RSA;
232 } else {
233 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100234 return NULL;
235 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000236 key = key_new_private(ktype);
237 xfree(type);
238
239 switch (key->type) {
240 case KEY_DSA:
241 buffer_get_bignum_bits(&b, key->dsa->p);
242 buffer_get_bignum_bits(&b, key->dsa->g);
243 buffer_get_bignum_bits(&b, key->dsa->q);
244 buffer_get_bignum_bits(&b, key->dsa->pub_key);
245 buffer_get_bignum_bits(&b, key->dsa->priv_key);
246 break;
247 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000248 e = buffer_get_char(&b);
249 debug("e %lx", e);
250 if (e < 30) {
251 e <<= 8;
252 e += buffer_get_char(&b);
253 debug("e %lx", e);
254 e <<= 8;
255 e += buffer_get_char(&b);
256 debug("e %lx", e);
257 }
258 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000259 buffer_free(&b);
260 key_free(key);
261 return NULL;
262 }
263 buffer_get_bignum_bits(&b, key->rsa->d);
264 buffer_get_bignum_bits(&b, key->rsa->n);
265 buffer_get_bignum_bits(&b, key->rsa->iqmp);
266 buffer_get_bignum_bits(&b, key->rsa->q);
267 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000268 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000269 break;
270 }
Damien Miller874d77b2000-10-14 16:23:11 +1100271 rlen = buffer_len(&b);
272 if(rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000273 error("do_convert_private_ssh2_from_blob: "
274 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100275 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000276
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000277 /* try the key */
278 key_sign(key, &sig, &slen, data, sizeof(data));
279 key_verify(key, sig, slen, data, sizeof(data));
280 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100281 return key;
282}
283
Ben Lindstrombba81212001-06-25 05:01:22 +0000284static void
Damien Millereba71ba2000-04-29 23:57:08 +1000285do_convert_from_ssh2(struct passwd *pw)
286{
287 Key *k;
288 int blen;
289 char line[1024], *p;
290 char blob[8096];
291 char encoded[8096];
292 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100293 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000294 FILE *fp;
295
296 if (!have_identity)
297 ask_filename(pw, "Enter file in which the key is");
298 if (stat(identity_file, &st) < 0) {
299 perror(identity_file);
300 exit(1);
301 }
302 fp = fopen(identity_file, "r");
303 if (fp == NULL) {
304 perror(identity_file);
305 exit(1);
306 }
307 encoded[0] = '\0';
308 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000309 if (!(p = strchr(line, '\n'))) {
310 fprintf(stderr, "input line too long.\n");
311 exit(1);
312 }
313 if (p > line && p[-1] == '\\')
314 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000315 if (strncmp(line, "----", 4) == 0 ||
316 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100317 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
318 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000319 if (strstr(line, " END ") != NULL) {
320 break;
321 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000322 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000323 continue;
324 }
Damien Miller30c3d422000-05-09 11:02:59 +1000325 if (escaped) {
326 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000327 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000328 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000329 }
330 *p = '\0';
331 strlcat(encoded, line, sizeof(encoded));
332 }
Ben Lindstrom46c16222000-12-22 01:43:59 +0000333 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000334 if (blen < 0) {
335 fprintf(stderr, "uudecode failed.\n");
336 exit(1);
337 }
Damien Miller874d77b2000-10-14 16:23:11 +1100338 k = private ?
339 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100340 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100341 if (k == NULL) {
342 fprintf(stderr, "decode blob failed.\n");
343 exit(1);
344 }
345 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000346 (k->type == KEY_DSA ?
347 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
348 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100349 key_write(k, stdout);
350 if (!ok) {
351 fprintf(stderr, "key write failed");
352 exit(1);
353 }
Damien Millereba71ba2000-04-29 23:57:08 +1000354 key_free(k);
355 fprintf(stdout, "\n");
356 fclose(fp);
357 exit(0);
358}
359
Ben Lindstrombba81212001-06-25 05:01:22 +0000360static void
Damien Millereba71ba2000-04-29 23:57:08 +1000361do_print_public(struct passwd *pw)
362{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000363 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000364 struct stat st;
365
366 if (!have_identity)
367 ask_filename(pw, "Enter file in which the key is");
368 if (stat(identity_file, &st) < 0) {
369 perror(identity_file);
370 exit(1);
371 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000372 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000373 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000374 fprintf(stderr, "load failed\n");
375 exit(1);
376 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000377 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000378 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000379 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000380 fprintf(stdout, "\n");
381 exit(0);
382}
383
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000384#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000385#define NUM_RSA_KEY_ELEMENTS 5+1
386#define COPY_RSA_KEY(x, i) \
387 do { \
388 len = BN_num_bytes(prv->rsa->x); \
389 elements[i] = xmalloc(len); \
Ben Lindstrom7feba352001-07-04 05:06:59 +0000390 debug("#bytes %d", len); \
Ben Lindstromcd392282001-07-04 03:44:03 +0000391 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
392 goto done; \
393 } while(0)
394
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000395static int
396get_AUT0(char *aut0)
397{
398 EVP_MD *evp_md = EVP_sha1();
399 EVP_MD_CTX md;
400 char *pass;
401
402 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
403 if (pass == NULL)
404 return -1;
405 EVP_DigestInit(&md, evp_md);
406 EVP_DigestUpdate(&md, pass, strlen(pass));
407 EVP_DigestFinal(&md, aut0, NULL);
408 memset(pass, 0, strlen(pass));
409 xfree(pass);
410 return 0;
411}
412
Ben Lindstromcd392282001-07-04 03:44:03 +0000413static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000414do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000415{
Ben Lindstromcd392282001-07-04 03:44:03 +0000416 Key *prv = NULL;
417 struct stat st;
418 u_char *elements[NUM_RSA_KEY_ELEMENTS];
419 u_char key_fid[2];
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000420 u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
421 u_char AUT0[EVP_MAX_MD_SIZE];
Ben Lindstromcd392282001-07-04 03:44:03 +0000422 int len, status = 1, i, fd = -1, ret;
Ben Lindstrom00477642001-07-04 05:24:27 +0000423 int sw = 0, cla = 0x00;
Ben Lindstromcd392282001-07-04 03:44:03 +0000424
Ben Lindstromd6e049c2001-07-04 05:08:39 +0000425 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
426 elements[i] = NULL;
Ben Lindstromcd392282001-07-04 03:44:03 +0000427 if (!have_identity)
428 ask_filename(pw, "Enter file in which the key is");
429 if (stat(identity_file, &st) < 0) {
430 perror(identity_file);
431 goto done;
432 }
433 prv = load_identity(identity_file);
434 if (prv == NULL) {
435 error("load failed");
436 goto done;
437 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000438 COPY_RSA_KEY(q, 0);
439 COPY_RSA_KEY(p, 1);
440 COPY_RSA_KEY(iqmp, 2);
441 COPY_RSA_KEY(dmq1, 3);
442 COPY_RSA_KEY(dmp1, 4);
443 COPY_RSA_KEY(n, 5);
444 len = BN_num_bytes(prv->rsa->n);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000445 fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000446 if (fd < 0) {
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000447 error("sectok_open failed: %s", sectok_get_sw(sw));
448 goto done;
449 }
450 if (! sectok_cardpresent(fd)) {
451 error("smartcard in reader %s not present",
452 sc_reader_id);
Ben Lindstromcd392282001-07-04 03:44:03 +0000453 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000454 }
Ben Lindstrom60df8e42001-08-06 21:10:52 +0000455 ret = sectok_reset(fd, 0, NULL, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000456 if (ret <= 0) {
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000457 error("sectok_reset failed: %s", sectok_get_sw(sw));
Ben Lindstromcd392282001-07-04 03:44:03 +0000458 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000459 }
Ben Lindstrom680b2762001-07-04 05:00:11 +0000460 if ((cla = cyberflex_inq_class(fd)) < 0) {
461 error("cyberflex_inq_class failed");
462 goto done;
463 }
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000464 memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
465 if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
466 if (get_AUT0(AUT0) < 0 ||
467 cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
468 error("cyberflex_verify_AUT0 failed");
469 goto done;
470 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000471 }
472 key_fid[0] = 0x00;
473 key_fid[1] = 0x12;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000474 if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
Ben Lindstrom00477642001-07-04 05:24:27 +0000475 &sw) < 0) {
476 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000477 goto done;
478 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000479 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000480 goto done;
481 log("cyberflex_load_rsa_priv done");
482 key_fid[0] = 0x73;
483 key_fid[1] = 0x68;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000484 if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
Ben Lindstrom00477642001-07-04 05:24:27 +0000485 &sw) < 0) {
486 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000487 goto done;
488 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000489 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000490 goto done;
491 log("cyberflex_load_rsa_pub done");
492 status = 0;
493 log("loading key done");
494done:
495 if (prv)
496 key_free(prv);
497 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
Ben Lindstrom00477642001-07-04 05:24:27 +0000498 if (elements[i])
499 xfree(elements[i]);
Ben Lindstromcd392282001-07-04 03:44:03 +0000500 if (fd != -1)
Ben Lindstrom00477642001-07-04 05:24:27 +0000501 sectok_close(fd);
Ben Lindstromcd392282001-07-04 03:44:03 +0000502 exit(status);
Ben Lindstromcd392282001-07-04 03:44:03 +0000503}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000504
505static void
506do_download(struct passwd *pw, const char *sc_reader_id)
507{
508 Key *pub = NULL;
509
510 pub = sc_get_key(sc_reader_id);
511 if (pub == NULL)
512 fatal("cannot read public key from smartcard");
513 key_write(pub, stdout);
514 key_free(pub);
515 fprintf(stdout, "\n");
516 exit(0);
517}
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000518#endif
Ben Lindstromcd392282001-07-04 03:44:03 +0000519
Ben Lindstrombba81212001-06-25 05:01:22 +0000520static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100521do_fingerprint(struct passwd *pw)
522{
Damien Miller98c7ad62000-03-09 21:27:49 +1100523 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000524 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000525 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000526 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
Damien Miller95def091999-11-25 00:26:21 +1100527 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100528
Ben Lindstromd0fca422001-03-26 13:44:06 +0000529 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
530 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000531
Damien Miller95def091999-11-25 00:26:21 +1100532 if (!have_identity)
533 ask_filename(pw, "Enter file in which the key is");
534 if (stat(identity_file, &st) < 0) {
535 perror(identity_file);
536 exit(1);
537 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000538 public = key_load_public(identity_file, &comment);
539 if (public != NULL) {
540 fp = key_fingerprint(public, fptype, rep);
541 printf("%d %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100542 key_free(public);
543 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000544 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100545 exit(0);
546 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000547 if (comment)
548 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100549
550 f = fopen(identity_file, "r");
551 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100552 while (fgets(line, sizeof(line), f)) {
553 i = strlen(line) - 1;
554 if (line[i] != '\n') {
555 error("line %d too long: %.40s...", num, line);
556 skip = 1;
557 continue;
Damien Miller95def091999-11-25 00:26:21 +1100558 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100559 num++;
560 if (skip) {
561 skip = 0;
562 continue;
563 }
564 line[i] = '\0';
565
566 /* Skip leading whitespace, empty and comment lines. */
567 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
568 ;
569 if (!*cp || *cp == '\n' || *cp == '#')
570 continue ;
571 i = strtol(cp, &ep, 10);
572 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
573 int quoted = 0;
574 comment = cp;
575 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
576 if (*cp == '\\' && cp[1] == '"')
577 cp++; /* Skip both */
578 else if (*cp == '"')
579 quoted = !quoted;
580 }
581 if (!*cp)
582 continue;
583 *cp++ = '\0';
584 }
585 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000586 public = key_new(KEY_RSA1);
587 if (key_read(public, &cp) != 1) {
588 cp = ep;
589 key_free(public);
590 public = key_new(KEY_UNSPEC);
591 if (key_read(public, &cp) != 1) {
592 key_free(public);
593 continue;
594 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100595 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000596 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000597 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000598 printf("%d %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000599 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000600 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000601 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000602 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100603 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100604 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100605 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100606 if (invalid) {
607 printf("%s is not a valid key file.\n", identity_file);
608 exit(1);
609 }
Damien Miller95def091999-11-25 00:26:21 +1100610 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100611}
612
Damien Miller95def091999-11-25 00:26:21 +1100613/*
614 * Perform changing a passphrase. The argument is the passwd structure
615 * for the current user.
616 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000617static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100618do_change_passphrase(struct passwd *pw)
619{
Damien Miller95def091999-11-25 00:26:21 +1100620 char *comment;
621 char *old_passphrase, *passphrase1, *passphrase2;
622 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000623 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100624
Damien Miller95def091999-11-25 00:26:21 +1100625 if (!have_identity)
626 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100627 if (stat(identity_file, &st) < 0) {
628 perror(identity_file);
629 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630 }
Damien Miller95def091999-11-25 00:26:21 +1100631 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000632 private = key_load_private(identity_file, "", &comment);
633 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100634 if (identity_passphrase)
635 old_passphrase = xstrdup(identity_passphrase);
636 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000637 old_passphrase =
638 read_passphrase("Enter old passphrase: ",
639 RP_ALLOW_STDIN);
640 private = key_load_private(identity_file, old_passphrase,
641 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000642 memset(old_passphrase, 0, strlen(old_passphrase));
643 xfree(old_passphrase);
644 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100645 printf("Bad passphrase.\n");
646 exit(1);
647 }
Damien Miller95def091999-11-25 00:26:21 +1100648 }
649 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650
Damien Miller95def091999-11-25 00:26:21 +1100651 /* Ask the new passphrase (twice). */
652 if (identity_new_passphrase) {
653 passphrase1 = xstrdup(identity_new_passphrase);
654 passphrase2 = NULL;
655 } else {
656 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000657 read_passphrase("Enter new passphrase (empty for no "
658 "passphrase): ", RP_ALLOW_STDIN);
659 passphrase2 = read_passphrase("Enter same passphrase again: ",
660 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100661
662 /* Verify that they are the same. */
663 if (strcmp(passphrase1, passphrase2) != 0) {
664 memset(passphrase1, 0, strlen(passphrase1));
665 memset(passphrase2, 0, strlen(passphrase2));
666 xfree(passphrase1);
667 xfree(passphrase2);
668 printf("Pass phrases do not match. Try again.\n");
669 exit(1);
670 }
671 /* Destroy the other copy. */
672 memset(passphrase2, 0, strlen(passphrase2));
673 xfree(passphrase2);
674 }
675
676 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000677 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000678 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100679 memset(passphrase1, 0, strlen(passphrase1));
680 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000681 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100682 xfree(comment);
683 exit(1);
684 }
685 /* Destroy the passphrase and the copy of the key in memory. */
686 memset(passphrase1, 0, strlen(passphrase1));
687 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000688 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100689 xfree(comment);
690
691 printf("Your identification has been saved with the new passphrase.\n");
692 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693}
694
Damien Miller95def091999-11-25 00:26:21 +1100695/*
696 * Change the comment of a private key file.
697 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000698static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000699do_change_comment(struct passwd *pw)
700{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000701 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000702 Key *private;
703 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100704 struct stat st;
705 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000706 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707
Damien Miller95def091999-11-25 00:26:21 +1100708 if (!have_identity)
709 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100710 if (stat(identity_file, &st) < 0) {
711 perror(identity_file);
712 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000714 private = key_load_private(identity_file, "", &comment);
715 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100716 if (identity_passphrase)
717 passphrase = xstrdup(identity_passphrase);
718 else if (identity_new_passphrase)
719 passphrase = xstrdup(identity_new_passphrase);
720 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000721 passphrase = read_passphrase("Enter passphrase: ",
722 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100723 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000724 private = key_load_private(identity_file, passphrase, &comment);
725 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100726 memset(passphrase, 0, strlen(passphrase));
727 xfree(passphrase);
728 printf("Bad passphrase.\n");
729 exit(1);
730 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000731 } else {
732 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100733 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000734 if (private->type != KEY_RSA1) {
735 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
736 key_free(private);
737 exit(1);
738 }
Damien Miller95def091999-11-25 00:26:21 +1100739 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740
Damien Miller95def091999-11-25 00:26:21 +1100741 if (identity_comment) {
742 strlcpy(new_comment, identity_comment, sizeof(new_comment));
743 } else {
744 printf("Enter new comment: ");
745 fflush(stdout);
746 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
747 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000748 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100749 exit(1);
750 }
Damien Miller95def091999-11-25 00:26:21 +1100751 if (strchr(new_comment, '\n'))
752 *strchr(new_comment, '\n') = 0;
753 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000754
Damien Miller95def091999-11-25 00:26:21 +1100755 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000756 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000757 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100758 memset(passphrase, 0, strlen(passphrase));
759 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000760 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100761 xfree(comment);
762 exit(1);
763 }
Damien Miller95def091999-11-25 00:26:21 +1100764 memset(passphrase, 0, strlen(passphrase));
765 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000766 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000767 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000768
Damien Miller95def091999-11-25 00:26:21 +1100769 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000770 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
771 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100772 printf("Could not save your public key in %s\n", identity_file);
773 exit(1);
774 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000775 f = fdopen(fd, "w");
776 if (f == NULL) {
777 printf("fdopen %s failed", identity_file);
778 exit(1);
779 }
Damien Millereba71ba2000-04-29 23:57:08 +1000780 if (!key_write(public, f))
781 fprintf(stderr, "write key failed");
782 key_free(public);
783 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100784 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Damien Miller95def091999-11-25 00:26:21 +1100786 xfree(comment);
787
788 printf("The comment in your key file has been changed.\n");
789 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790}
791
Ben Lindstrombba81212001-06-25 05:01:22 +0000792static void
Damien Miller431f66b1999-11-21 18:31:57 +1100793usage(void)
794{
Ben Lindstrom2857d9c2001-04-22 17:19:46 +0000795 printf("Usage: %s [-ceilpqyB] [-t type] [-b bits] [-f file] [-C comment] "
Ben Lindstromb7c92322001-03-05 05:10:52 +0000796 "[-N new-pass] [-P pass]\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100797 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100798}
799
Damien Miller95def091999-11-25 00:26:21 +1100800/*
801 * Main program for key management.
802 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803int
804main(int ac, char **av)
805{
Damien Miller95def091999-11-25 00:26:21 +1100806 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000807 char *reader_id = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000808 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100809 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100810 struct stat st;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000811 int opt, type, fd, download = 0;
Damien Miller95def091999-11-25 00:26:21 +1100812 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100813
Damien Miller95def091999-11-25 00:26:21 +1100814 extern int optind;
815 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000817 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000818 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100819 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000820
Damien Millerd3a18572000-06-07 19:55:44 +1000821 SSLeay_add_all_algorithms();
Damien Millereba71ba2000-04-29 23:57:08 +1000822
Damien Miller5428f641999-11-25 11:54:57 +1100823 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100824 pw = getpwuid(getuid());
825 if (!pw) {
826 printf("You don't exist, go away!\n");
827 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828 }
Damien Millereba71ba2000-04-29 23:57:08 +1000829 if (gethostname(hostname, sizeof(hostname)) < 0) {
830 perror("gethostname");
831 exit(1);
832 }
Damien Miller5428f641999-11-25 11:54:57 +1100833
Ben Lindstromf19578c2001-08-06 21:46:54 +0000834 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100835 switch (opt) {
836 case 'b':
837 bits = atoi(optarg);
838 if (bits < 512 || bits > 32768) {
839 printf("Bits has bad value.\n");
840 exit(1);
841 }
842 break;
Damien Miller95def091999-11-25 00:26:21 +1100843 case 'l':
844 print_fingerprint = 1;
845 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000846 case 'B':
847 print_bubblebabble = 1;
848 break;
Damien Miller95def091999-11-25 00:26:21 +1100849 case 'p':
850 change_passphrase = 1;
851 break;
Damien Miller95def091999-11-25 00:26:21 +1100852 case 'c':
853 change_comment = 1;
854 break;
Damien Miller95def091999-11-25 00:26:21 +1100855 case 'f':
856 strlcpy(identity_file, optarg, sizeof(identity_file));
857 have_identity = 1;
858 break;
Damien Miller95def091999-11-25 00:26:21 +1100859 case 'P':
860 identity_passphrase = optarg;
861 break;
Damien Miller95def091999-11-25 00:26:21 +1100862 case 'N':
863 identity_new_passphrase = optarg;
864 break;
Damien Miller95def091999-11-25 00:26:21 +1100865 case 'C':
866 identity_comment = optarg;
867 break;
Damien Miller95def091999-11-25 00:26:21 +1100868 case 'q':
869 quiet = 1;
870 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000871 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100872 /* unused */
873 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000874 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000875 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000876 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000877 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000878 convert_to_ssh2 = 1;
879 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000880 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000881 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000882 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000883 convert_from_ssh2 = 1;
884 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000885 case 'y':
886 print_public = 1;
887 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000888 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100889 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000890 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100891 case 't':
892 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100893 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000894 case 'D':
895 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000896 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000897 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000898 break;
Damien Miller95def091999-11-25 00:26:21 +1100899 case '?':
900 default:
901 usage();
902 }
903 }
904 if (optind < ac) {
905 printf("Too many arguments.\n");
906 usage();
907 }
908 if (change_passphrase && change_comment) {
909 printf("Can only have one of -p and -c.\n");
910 usage();
911 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000912 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100913 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100914 if (change_passphrase)
915 do_change_passphrase(pw);
Damien Miller95def091999-11-25 00:26:21 +1100916 if (change_comment)
917 do_change_comment(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000918 if (convert_to_ssh2)
919 do_convert_to_ssh2(pw);
920 if (convert_from_ssh2)
921 do_convert_from_ssh2(pw);
922 if (print_public)
923 do_print_public(pw);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000924 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000925#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000926 if (download)
927 do_download(pw, reader_id);
928 else
929 do_upload(pw, reader_id);
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000930#else
931 fatal("no support for smartcards.");
932#endif
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000933 }
Damien Miller95def091999-11-25 00:26:21 +1100934
935 arc4random_stir();
936
Damien Millere39cacc2000-11-29 12:18:44 +1100937 type = key_type_from_name(key_type_name);
938 if (type == KEY_UNSPEC) {
939 fprintf(stderr, "unknown key type %s\n", key_type_name);
940 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000941 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100942 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100943 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100944 private = key_generate(type, bits);
945 if (private == NULL) {
946 fprintf(stderr, "key_generate failed");
947 exit(1);
948 }
949 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100950
951 if (!have_identity)
952 ask_filename(pw, "Enter file in which to save the key");
953
954 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000955 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100956 if (strstr(identity_file, dotsshdir) != NULL &&
957 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000958 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100959 error("Could not create directory '%s'.", dotsshdir);
960 else if (!quiet)
961 printf("Created directory '%s'.\n", dotsshdir);
962 }
963 /* If the file already exists, ask the user to confirm. */
964 if (stat(identity_file, &st) >= 0) {
965 char yesno[3];
966 printf("%s already exists.\n", identity_file);
967 printf("Overwrite (y/n)? ");
968 fflush(stdout);
969 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
970 exit(1);
971 if (yesno[0] != 'y' && yesno[0] != 'Y')
972 exit(1);
973 }
974 /* Ask for a passphrase (twice). */
975 if (identity_passphrase)
976 passphrase1 = xstrdup(identity_passphrase);
977 else if (identity_new_passphrase)
978 passphrase1 = xstrdup(identity_new_passphrase);
979 else {
980passphrase_again:
981 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000982 read_passphrase("Enter passphrase (empty for no "
983 "passphrase): ", RP_ALLOW_STDIN);
984 passphrase2 = read_passphrase("Enter same passphrase again: ",
985 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100986 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000987 /*
988 * The passphrases do not match. Clear them and
989 * retry.
990 */
Damien Miller95def091999-11-25 00:26:21 +1100991 memset(passphrase1, 0, strlen(passphrase1));
992 memset(passphrase2, 0, strlen(passphrase2));
993 xfree(passphrase1);
994 xfree(passphrase2);
995 printf("Passphrases do not match. Try again.\n");
996 goto passphrase_again;
997 }
998 /* Clear the other copy of the passphrase. */
999 memset(passphrase2, 0, strlen(passphrase2));
1000 xfree(passphrase2);
1001 }
1002
Damien Miller95def091999-11-25 00:26:21 +11001003 if (identity_comment) {
1004 strlcpy(comment, identity_comment, sizeof(comment));
1005 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001006 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001007 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1008 }
1009
1010 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001011 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001012 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001013 memset(passphrase1, 0, strlen(passphrase1));
1014 xfree(passphrase1);
1015 exit(1);
1016 }
1017 /* Clear the passphrase. */
1018 memset(passphrase1, 0, strlen(passphrase1));
1019 xfree(passphrase1);
1020
1021 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001022 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001023 arc4random_stir();
1024
1025 if (!quiet)
1026 printf("Your identification has been saved in %s.\n", identity_file);
1027
Damien Miller95def091999-11-25 00:26:21 +11001028 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001029 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1030 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001031 printf("Could not save your public key in %s\n", identity_file);
1032 exit(1);
1033 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001034 f = fdopen(fd, "w");
1035 if (f == NULL) {
1036 printf("fdopen %s failed", identity_file);
1037 exit(1);
1038 }
Damien Millereba71ba2000-04-29 23:57:08 +10001039 if (!key_write(public, f))
1040 fprintf(stderr, "write key failed");
1041 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001042 fclose(f);
1043
1044 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001045 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001046 printf("Your public key has been saved in %s.\n",
1047 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001048 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001049 printf("%s %s\n", fp, comment);
1050 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001051 }
Damien Millereba71ba2000-04-29 23:57:08 +10001052
1053 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001054 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055}