blob: 9b3b7f4c7a87df610725d151876c18a144ee7d0d [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 Lindstrom1c37c6a2001-12-06 18:00:18 +000015RCSID("$OpenBSD: ssh-keygen.c,v 1.85 2001/12/05 10:06:12 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Damien Millereba71ba2000-04-29 23:57:08 +100017#include <openssl/evp.h>
18#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100019
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100021#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000022#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100023#include "authfile.h"
24#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "buffer.h"
26#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "pathnames.h"
28#include "log.h"
29#include "readpass.h"
Damien Miller874d77b2000-10-14 16:23:11 +110030
Ben Lindstrom8282d6a2001-08-06 21:44:05 +000031#ifdef SMARTCARD
32#include <sectok.h>
33#include <openssl/engine.h>
34#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000035#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000036
Damien Millereba71ba2000-04-29 23:57:08 +100037/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038int bits = 1024;
39
Damien Miller5428f641999-11-25 11:54:57 +110040/*
41 * Flag indicating that we just want to change the passphrase. This can be
42 * set on the command line.
43 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044int change_passphrase = 0;
45
Damien Miller5428f641999-11-25 11:54:57 +110046/*
47 * Flag indicating that we just want to change the comment. This can be set
48 * on the command line.
49 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050int change_comment = 0;
51
52int quiet = 0;
53
Damien Miller10f6f6b1999-11-17 17:29:08 +110054/* Flag indicating that we just want to see the key fingerprint */
55int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000056int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110057
Damien Miller431f66b1999-11-21 18:31:57 +110058/* The identity file name, given on the command line or entered by the user. */
59char identity_file[1024];
60int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62/* This is set to the passphrase if given on the command line. */
63char *identity_passphrase = NULL;
64
65/* This is set to the new passphrase if given on the command line. */
66char *identity_new_passphrase = NULL;
67
68/* This is set to the new comment if given on the command line. */
69char *identity_comment = NULL;
70
Damien Millereba71ba2000-04-29 23:57:08 +100071/* Dump public key file in format used by real and the original SSH 2 */
72int convert_to_ssh2 = 0;
73int convert_from_ssh2 = 0;
74int print_public = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110075
Damien 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 Lindstrom99a30f12001-09-18 05:49:14 +0000166 if (key_to_blob(k, &blob, &len) <= 0) {
167 fprintf(stderr, "key_to_blob failed\n");
168 exit(1);
169 }
Damien Miller874d77b2000-10-14 16:23:11 +1100170 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000171 fprintf(stdout,
Damien Miller874d77b2000-10-14 16:23:11 +1100172 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000173 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000174 pw->pw_name, hostname);
175 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100176 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000177 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000178 xfree(blob);
179 exit(0);
180}
181
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static void
Damien Miller874d77b2000-10-14 16:23:11 +1100183buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
184{
185 int bits = buffer_get_int(b);
186 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000187
Damien Miller874d77b2000-10-14 16:23:11 +1100188 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000189 fatal("buffer_get_bignum_bits: input buffer too small: "
190 "need %d have %d", bytes, buffer_len(b));
Ben Lindstrom46c16222000-12-22 01:43:59 +0000191 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100192 buffer_consume(b, bytes);
193}
194
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static Key *
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000196do_convert_private_ssh2_from_blob(u_char *blob, int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100197{
198 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100199 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100200 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000201 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000202 int magic, rlen, ktype, i1, i2, i3, i4;
203 u_int slen;
204 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100205
206 buffer_init(&b);
207 buffer_append(&b, blob, blen);
208
209 magic = buffer_get_int(&b);
210 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
211 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
212 buffer_free(&b);
213 return NULL;
214 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000215 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100216 type = buffer_get_string(&b, NULL);
217 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000218 i2 = buffer_get_int(&b);
219 i3 = buffer_get_int(&b);
220 i4 = buffer_get_int(&b);
221 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100222 if (strcmp(cipher, "none") != 0) {
223 error("unsupported cipher %s", cipher);
224 xfree(cipher);
225 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000226 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100227 return NULL;
228 }
229 xfree(cipher);
230
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000231 if (strstr(type, "dsa")) {
232 ktype = KEY_DSA;
233 } else if (strstr(type, "rsa")) {
234 ktype = KEY_RSA;
235 } else {
236 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100237 return NULL;
238 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000239 key = key_new_private(ktype);
240 xfree(type);
241
242 switch (key->type) {
243 case KEY_DSA:
244 buffer_get_bignum_bits(&b, key->dsa->p);
245 buffer_get_bignum_bits(&b, key->dsa->g);
246 buffer_get_bignum_bits(&b, key->dsa->q);
247 buffer_get_bignum_bits(&b, key->dsa->pub_key);
248 buffer_get_bignum_bits(&b, key->dsa->priv_key);
249 break;
250 case KEY_RSA:
Ben Lindstrom34f91882001-06-25 04:47:54 +0000251 e = buffer_get_char(&b);
252 debug("e %lx", e);
253 if (e < 30) {
254 e <<= 8;
255 e += buffer_get_char(&b);
256 debug("e %lx", e);
257 e <<= 8;
258 e += buffer_get_char(&b);
259 debug("e %lx", e);
260 }
261 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000262 buffer_free(&b);
263 key_free(key);
264 return NULL;
265 }
266 buffer_get_bignum_bits(&b, key->rsa->d);
267 buffer_get_bignum_bits(&b, key->rsa->n);
268 buffer_get_bignum_bits(&b, key->rsa->iqmp);
269 buffer_get_bignum_bits(&b, key->rsa->q);
270 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000271 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000272 break;
273 }
Damien Miller874d77b2000-10-14 16:23:11 +1100274 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000275 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000276 error("do_convert_private_ssh2_from_blob: "
277 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100278 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000279
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000280 /* try the key */
281 key_sign(key, &sig, &slen, data, sizeof(data));
282 key_verify(key, sig, slen, data, sizeof(data));
283 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100284 return key;
285}
286
Ben Lindstrombba81212001-06-25 05:01:22 +0000287static void
Damien Millereba71ba2000-04-29 23:57:08 +1000288do_convert_from_ssh2(struct passwd *pw)
289{
290 Key *k;
291 int blen;
292 char line[1024], *p;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000293 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000294 char encoded[8096];
295 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100296 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000297 FILE *fp;
298
299 if (!have_identity)
300 ask_filename(pw, "Enter file in which the key is");
301 if (stat(identity_file, &st) < 0) {
302 perror(identity_file);
303 exit(1);
304 }
305 fp = fopen(identity_file, "r");
306 if (fp == NULL) {
307 perror(identity_file);
308 exit(1);
309 }
310 encoded[0] = '\0';
311 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000312 if (!(p = strchr(line, '\n'))) {
313 fprintf(stderr, "input line too long.\n");
314 exit(1);
315 }
316 if (p > line && p[-1] == '\\')
317 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000318 if (strncmp(line, "----", 4) == 0 ||
319 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100320 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
321 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000322 if (strstr(line, " END ") != NULL) {
323 break;
324 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000325 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000326 continue;
327 }
Damien Miller30c3d422000-05-09 11:02:59 +1000328 if (escaped) {
329 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000330 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000331 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000332 }
333 *p = '\0';
334 strlcat(encoded, line, sizeof(encoded));
335 }
Ben Lindstrom46c16222000-12-22 01:43:59 +0000336 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000337 if (blen < 0) {
338 fprintf(stderr, "uudecode failed.\n");
339 exit(1);
340 }
Damien Miller874d77b2000-10-14 16:23:11 +1100341 k = private ?
342 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100343 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100344 if (k == NULL) {
345 fprintf(stderr, "decode blob failed.\n");
346 exit(1);
347 }
348 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000349 (k->type == KEY_DSA ?
350 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
351 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100352 key_write(k, stdout);
353 if (!ok) {
354 fprintf(stderr, "key write failed");
355 exit(1);
356 }
Damien Millereba71ba2000-04-29 23:57:08 +1000357 key_free(k);
358 fprintf(stdout, "\n");
359 fclose(fp);
360 exit(0);
361}
362
Ben Lindstrombba81212001-06-25 05:01:22 +0000363static void
Damien Millereba71ba2000-04-29 23:57:08 +1000364do_print_public(struct passwd *pw)
365{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000366 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000367 struct stat st;
368
369 if (!have_identity)
370 ask_filename(pw, "Enter file in which the key is");
371 if (stat(identity_file, &st) < 0) {
372 perror(identity_file);
373 exit(1);
374 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000375 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000376 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000377 fprintf(stderr, "load failed\n");
378 exit(1);
379 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000380 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000381 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000382 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000383 fprintf(stdout, "\n");
384 exit(0);
385}
386
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000387#ifdef SMARTCARD
Ben Lindstromcd392282001-07-04 03:44:03 +0000388#define NUM_RSA_KEY_ELEMENTS 5+1
389#define COPY_RSA_KEY(x, i) \
390 do { \
391 len = BN_num_bytes(prv->rsa->x); \
392 elements[i] = xmalloc(len); \
Ben Lindstrom7feba352001-07-04 05:06:59 +0000393 debug("#bytes %d", len); \
Ben Lindstromcd392282001-07-04 03:44:03 +0000394 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
395 goto done; \
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000396 } while (0)
Ben Lindstromcd392282001-07-04 03:44:03 +0000397
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000398static int
399get_AUT0(char *aut0)
400{
401 EVP_MD *evp_md = EVP_sha1();
402 EVP_MD_CTX md;
403 char *pass;
404
405 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
406 if (pass == NULL)
407 return -1;
408 EVP_DigestInit(&md, evp_md);
409 EVP_DigestUpdate(&md, pass, strlen(pass));
410 EVP_DigestFinal(&md, aut0, NULL);
411 memset(pass, 0, strlen(pass));
412 xfree(pass);
413 return 0;
414}
415
Ben Lindstromcd392282001-07-04 03:44:03 +0000416static void
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000417do_upload(struct passwd *pw, const char *sc_reader_id)
Ben Lindstromcd392282001-07-04 03:44:03 +0000418{
Ben Lindstromcd392282001-07-04 03:44:03 +0000419 Key *prv = NULL;
420 struct stat st;
421 u_char *elements[NUM_RSA_KEY_ELEMENTS];
422 u_char key_fid[2];
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000423 u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
424 u_char AUT0[EVP_MAX_MD_SIZE];
Ben Lindstromcd392282001-07-04 03:44:03 +0000425 int len, status = 1, i, fd = -1, ret;
Ben Lindstrom00477642001-07-04 05:24:27 +0000426 int sw = 0, cla = 0x00;
Ben Lindstromcd392282001-07-04 03:44:03 +0000427
Ben Lindstromd6e049c2001-07-04 05:08:39 +0000428 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
429 elements[i] = NULL;
Ben Lindstromcd392282001-07-04 03:44:03 +0000430 if (!have_identity)
431 ask_filename(pw, "Enter file in which the key is");
432 if (stat(identity_file, &st) < 0) {
433 perror(identity_file);
434 goto done;
435 }
436 prv = load_identity(identity_file);
437 if (prv == NULL) {
438 error("load failed");
439 goto done;
440 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000441 COPY_RSA_KEY(q, 0);
442 COPY_RSA_KEY(p, 1);
443 COPY_RSA_KEY(iqmp, 2);
444 COPY_RSA_KEY(dmq1, 3);
445 COPY_RSA_KEY(dmp1, 4);
446 COPY_RSA_KEY(n, 5);
447 len = BN_num_bytes(prv->rsa->n);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000448 fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000449 if (fd < 0) {
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000450 error("sectok_open failed: %s", sectok_get_sw(sw));
451 goto done;
452 }
453 if (! sectok_cardpresent(fd)) {
454 error("smartcard in reader %s not present",
455 sc_reader_id);
Ben Lindstromcd392282001-07-04 03:44:03 +0000456 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000457 }
Ben Lindstrom60df8e42001-08-06 21:10:52 +0000458 ret = sectok_reset(fd, 0, NULL, &sw);
Ben Lindstrom00477642001-07-04 05:24:27 +0000459 if (ret <= 0) {
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000460 error("sectok_reset failed: %s", sectok_get_sw(sw));
Ben Lindstromcd392282001-07-04 03:44:03 +0000461 goto done;
Ben Lindstrom00477642001-07-04 05:24:27 +0000462 }
Ben Lindstrom680b2762001-07-04 05:00:11 +0000463 if ((cla = cyberflex_inq_class(fd)) < 0) {
464 error("cyberflex_inq_class failed");
465 goto done;
466 }
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000467 memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
468 if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
469 if (get_AUT0(AUT0) < 0 ||
470 cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
471 error("cyberflex_verify_AUT0 failed");
472 goto done;
473 }
Ben Lindstromcd392282001-07-04 03:44:03 +0000474 }
475 key_fid[0] = 0x00;
476 key_fid[1] = 0x12;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000477 if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
Ben Lindstrom00477642001-07-04 05:24:27 +0000478 &sw) < 0) {
479 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000480 goto done;
481 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000482 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000483 goto done;
484 log("cyberflex_load_rsa_priv done");
485 key_fid[0] = 0x73;
486 key_fid[1] = 0x68;
Ben Lindstrom7feba352001-07-04 05:06:59 +0000487 if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
Ben Lindstrom00477642001-07-04 05:24:27 +0000488 &sw) < 0) {
489 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
Ben Lindstrom7feba352001-07-04 05:06:59 +0000490 goto done;
491 }
Ben Lindstrom00477642001-07-04 05:24:27 +0000492 if (!sectok_swOK(sw))
Ben Lindstromcd392282001-07-04 03:44:03 +0000493 goto done;
494 log("cyberflex_load_rsa_pub done");
495 status = 0;
496 log("loading key done");
497done:
Ben Lindstrom1af4d3b2001-10-03 17:18:37 +0000498
499 memset(elements[0], '\0', BN_num_bytes(prv->rsa->q));
500 memset(elements[1], '\0', BN_num_bytes(prv->rsa->p));
501 memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp));
502 memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1));
503 memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1));
504 memset(elements[5], '\0', BN_num_bytes(prv->rsa->n));
505
Ben Lindstromcd392282001-07-04 03:44:03 +0000506 if (prv)
507 key_free(prv);
508 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
Ben Lindstrom00477642001-07-04 05:24:27 +0000509 if (elements[i])
510 xfree(elements[i]);
Ben Lindstromcd392282001-07-04 03:44:03 +0000511 if (fd != -1)
Ben Lindstrom00477642001-07-04 05:24:27 +0000512 sectok_close(fd);
Ben Lindstromcd392282001-07-04 03:44:03 +0000513 exit(status);
Ben Lindstromcd392282001-07-04 03:44:03 +0000514}
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000515
516static void
517do_download(struct passwd *pw, const char *sc_reader_id)
518{
519 Key *pub = NULL;
520
521 pub = sc_get_key(sc_reader_id);
522 if (pub == NULL)
523 fatal("cannot read public key from smartcard");
524 key_write(pub, stdout);
525 key_free(pub);
526 fprintf(stdout, "\n");
527 exit(0);
528}
Ben Lindstromffce1472001-08-06 21:57:31 +0000529#endif /* SMARTCARD */
Ben Lindstromcd392282001-07-04 03:44:03 +0000530
Ben Lindstrombba81212001-06-25 05:01:22 +0000531static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100532do_fingerprint(struct passwd *pw)
533{
Damien Miller98c7ad62000-03-09 21:27:49 +1100534 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000535 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000536 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000537 int i, skip = 0, num = 1, invalid = 1;
538 enum fp_rep rep;
539 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100540 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100541
Ben Lindstromd0fca422001-03-26 13:44:06 +0000542 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
543 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000544
Damien Miller95def091999-11-25 00:26:21 +1100545 if (!have_identity)
546 ask_filename(pw, "Enter file in which the key is");
547 if (stat(identity_file, &st) < 0) {
548 perror(identity_file);
549 exit(1);
550 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000551 public = key_load_public(identity_file, &comment);
552 if (public != NULL) {
553 fp = key_fingerprint(public, fptype, rep);
554 printf("%d %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100555 key_free(public);
556 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000557 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100558 exit(0);
559 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000560 if (comment)
561 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100562
563 f = fopen(identity_file, "r");
564 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100565 while (fgets(line, sizeof(line), f)) {
566 i = strlen(line) - 1;
567 if (line[i] != '\n') {
568 error("line %d too long: %.40s...", num, line);
569 skip = 1;
570 continue;
Damien Miller95def091999-11-25 00:26:21 +1100571 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100572 num++;
573 if (skip) {
574 skip = 0;
575 continue;
576 }
577 line[i] = '\0';
578
579 /* Skip leading whitespace, empty and comment lines. */
580 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
581 ;
582 if (!*cp || *cp == '\n' || *cp == '#')
583 continue ;
584 i = strtol(cp, &ep, 10);
585 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
586 int quoted = 0;
587 comment = cp;
588 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
589 if (*cp == '\\' && cp[1] == '"')
590 cp++; /* Skip both */
591 else if (*cp == '"')
592 quoted = !quoted;
593 }
594 if (!*cp)
595 continue;
596 *cp++ = '\0';
597 }
598 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000599 public = key_new(KEY_RSA1);
600 if (key_read(public, &cp) != 1) {
601 cp = ep;
602 key_free(public);
603 public = key_new(KEY_UNSPEC);
604 if (key_read(public, &cp) != 1) {
605 key_free(public);
606 continue;
607 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100608 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000609 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000610 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000611 printf("%d %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000612 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000613 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000614 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000615 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100616 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100617 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100618 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100619 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100620 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100621 exit(1);
622 }
Damien Miller95def091999-11-25 00:26:21 +1100623 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100624}
625
Damien Miller95def091999-11-25 00:26:21 +1100626/*
627 * Perform changing a passphrase. The argument is the passwd structure
628 * for the current user.
629 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000630static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100631do_change_passphrase(struct passwd *pw)
632{
Damien Miller95def091999-11-25 00:26:21 +1100633 char *comment;
634 char *old_passphrase, *passphrase1, *passphrase2;
635 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000636 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100637
Damien Miller95def091999-11-25 00:26:21 +1100638 if (!have_identity)
639 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100640 if (stat(identity_file, &st) < 0) {
641 perror(identity_file);
642 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643 }
Damien Miller95def091999-11-25 00:26:21 +1100644 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000645 private = key_load_private(identity_file, "", &comment);
646 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100647 if (identity_passphrase)
648 old_passphrase = xstrdup(identity_passphrase);
649 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000650 old_passphrase =
651 read_passphrase("Enter old passphrase: ",
652 RP_ALLOW_STDIN);
653 private = key_load_private(identity_file, old_passphrase,
654 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000655 memset(old_passphrase, 0, strlen(old_passphrase));
656 xfree(old_passphrase);
657 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100658 printf("Bad passphrase.\n");
659 exit(1);
660 }
Damien Miller95def091999-11-25 00:26:21 +1100661 }
662 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663
Damien Miller95def091999-11-25 00:26:21 +1100664 /* Ask the new passphrase (twice). */
665 if (identity_new_passphrase) {
666 passphrase1 = xstrdup(identity_new_passphrase);
667 passphrase2 = NULL;
668 } else {
669 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000670 read_passphrase("Enter new passphrase (empty for no "
671 "passphrase): ", RP_ALLOW_STDIN);
672 passphrase2 = read_passphrase("Enter same passphrase again: ",
673 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100674
675 /* Verify that they are the same. */
676 if (strcmp(passphrase1, passphrase2) != 0) {
677 memset(passphrase1, 0, strlen(passphrase1));
678 memset(passphrase2, 0, strlen(passphrase2));
679 xfree(passphrase1);
680 xfree(passphrase2);
681 printf("Pass phrases do not match. Try again.\n");
682 exit(1);
683 }
684 /* Destroy the other copy. */
685 memset(passphrase2, 0, strlen(passphrase2));
686 xfree(passphrase2);
687 }
688
689 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000690 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000691 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100692 memset(passphrase1, 0, strlen(passphrase1));
693 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000694 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100695 xfree(comment);
696 exit(1);
697 }
698 /* Destroy the passphrase and the copy of the key in memory. */
699 memset(passphrase1, 0, strlen(passphrase1));
700 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000701 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100702 xfree(comment);
703
704 printf("Your identification has been saved with the new passphrase.\n");
705 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706}
707
Damien Miller95def091999-11-25 00:26:21 +1100708/*
709 * Change the comment of a private key file.
710 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000711static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712do_change_comment(struct passwd *pw)
713{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000714 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000715 Key *private;
716 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100717 struct stat st;
718 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000719 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720
Damien Miller95def091999-11-25 00:26:21 +1100721 if (!have_identity)
722 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100723 if (stat(identity_file, &st) < 0) {
724 perror(identity_file);
725 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000727 private = key_load_private(identity_file, "", &comment);
728 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100729 if (identity_passphrase)
730 passphrase = xstrdup(identity_passphrase);
731 else if (identity_new_passphrase)
732 passphrase = xstrdup(identity_new_passphrase);
733 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000734 passphrase = read_passphrase("Enter passphrase: ",
735 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100736 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000737 private = key_load_private(identity_file, passphrase, &comment);
738 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100739 memset(passphrase, 0, strlen(passphrase));
740 xfree(passphrase);
741 printf("Bad passphrase.\n");
742 exit(1);
743 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000744 } else {
745 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100746 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000747 if (private->type != KEY_RSA1) {
748 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
749 key_free(private);
750 exit(1);
751 }
Damien Miller95def091999-11-25 00:26:21 +1100752 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753
Damien Miller95def091999-11-25 00:26:21 +1100754 if (identity_comment) {
755 strlcpy(new_comment, identity_comment, sizeof(new_comment));
756 } else {
757 printf("Enter new comment: ");
758 fflush(stdout);
759 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
760 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000761 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100762 exit(1);
763 }
Damien Miller95def091999-11-25 00:26:21 +1100764 if (strchr(new_comment, '\n'))
765 *strchr(new_comment, '\n') = 0;
766 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767
Damien Miller95def091999-11-25 00:26:21 +1100768 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000769 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000770 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100771 memset(passphrase, 0, strlen(passphrase));
772 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000773 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100774 xfree(comment);
775 exit(1);
776 }
Damien Miller95def091999-11-25 00:26:21 +1100777 memset(passphrase, 0, strlen(passphrase));
778 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000779 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000780 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781
Damien Miller95def091999-11-25 00:26:21 +1100782 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000783 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
784 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100785 printf("Could not save your public key in %s\n", identity_file);
786 exit(1);
787 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000788 f = fdopen(fd, "w");
789 if (f == NULL) {
790 printf("fdopen %s failed", identity_file);
791 exit(1);
792 }
Damien Millereba71ba2000-04-29 23:57:08 +1000793 if (!key_write(public, f))
794 fprintf(stderr, "write key failed");
795 key_free(public);
796 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100797 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798
Damien Miller95def091999-11-25 00:26:21 +1100799 xfree(comment);
800
801 printf("The comment in your key file has been changed.\n");
802 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803}
804
Ben Lindstrombba81212001-06-25 05:01:22 +0000805static void
Damien Miller431f66b1999-11-21 18:31:57 +1100806usage(void)
807{
Ben Lindstrom97be31e2001-08-06 21:49:06 +0000808 fprintf(stderr, "Usage: %s [options]\n", __progname);
809 fprintf(stderr, "Options:\n");
810 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
811 fprintf(stderr, " -c Change comment in private and public key files.\n");
812 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
813 fprintf(stderr, " -f filename Filename of the key file.\n");
814 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
815 fprintf(stderr, " -l Show fingerprint of key file.\n");
816 fprintf(stderr, " -p Change passphrase of private key file.\n");
817 fprintf(stderr, " -q Quiet.\n");
818 fprintf(stderr, " -y Read private key file and print public key.\n");
819 fprintf(stderr, " -t type Specify type of key to create.\n");
820 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
821 fprintf(stderr, " -C comment Provide new comment.\n");
822 fprintf(stderr, " -N phrase Provide new passphrase.\n");
823 fprintf(stderr, " -P phrase Provide old passphrase.\n");
824#ifdef SMARTCARD
825 fprintf(stderr, " -D reader Download public key from smartcard.\n");
826 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
827#endif /* SMARTCARD */
828
Damien Miller95def091999-11-25 00:26:21 +1100829 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100830}
831
Damien Miller95def091999-11-25 00:26:21 +1100832/*
833 * Main program for key management.
834 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835int
836main(int ac, char **av)
837{
Damien Miller95def091999-11-25 00:26:21 +1100838 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000839 char *reader_id = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000840 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100841 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +1100842 struct stat st;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000843 int opt, type, fd, download = 0;
Damien Miller95def091999-11-25 00:26:21 +1100844 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100845
Damien Miller95def091999-11-25 00:26:21 +1100846 extern int optind;
847 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000849 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000850 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100851 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000852
Damien Millerd3a18572000-06-07 19:55:44 +1000853 SSLeay_add_all_algorithms();
Damien Millereba71ba2000-04-29 23:57:08 +1000854
Damien Miller5428f641999-11-25 11:54:57 +1100855 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100856 pw = getpwuid(getuid());
857 if (!pw) {
858 printf("You don't exist, go away!\n");
859 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860 }
Damien Millereba71ba2000-04-29 23:57:08 +1000861 if (gethostname(hostname, sizeof(hostname)) < 0) {
862 perror("gethostname");
863 exit(1);
864 }
Damien Miller5428f641999-11-25 11:54:57 +1100865
Ben Lindstromf19578c2001-08-06 21:46:54 +0000866 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100867 switch (opt) {
868 case 'b':
869 bits = atoi(optarg);
870 if (bits < 512 || bits > 32768) {
871 printf("Bits has bad value.\n");
872 exit(1);
873 }
874 break;
Damien Miller95def091999-11-25 00:26:21 +1100875 case 'l':
876 print_fingerprint = 1;
877 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000878 case 'B':
879 print_bubblebabble = 1;
880 break;
Damien Miller95def091999-11-25 00:26:21 +1100881 case 'p':
882 change_passphrase = 1;
883 break;
Damien Miller95def091999-11-25 00:26:21 +1100884 case 'c':
885 change_comment = 1;
886 break;
Damien Miller95def091999-11-25 00:26:21 +1100887 case 'f':
888 strlcpy(identity_file, optarg, sizeof(identity_file));
889 have_identity = 1;
890 break;
Damien Miller95def091999-11-25 00:26:21 +1100891 case 'P':
892 identity_passphrase = optarg;
893 break;
Damien Miller95def091999-11-25 00:26:21 +1100894 case 'N':
895 identity_new_passphrase = optarg;
896 break;
Damien Miller95def091999-11-25 00:26:21 +1100897 case 'C':
898 identity_comment = optarg;
899 break;
Damien Miller95def091999-11-25 00:26:21 +1100900 case 'q':
901 quiet = 1;
902 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000903 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100904 /* unused */
905 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000906 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000907 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +1000908 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000909 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +1000910 convert_to_ssh2 = 1;
911 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +0000912 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +1000913 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +0000914 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +1000915 convert_from_ssh2 = 1;
916 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000917 case 'y':
918 print_public = 1;
919 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000920 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100921 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000922 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100923 case 't':
924 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100925 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000926 case 'D':
927 download = 1;
Ben Lindstromf19578c2001-08-06 21:46:54 +0000928 case 'U':
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000929 reader_id = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +0000930 break;
Damien Miller95def091999-11-25 00:26:21 +1100931 case '?':
932 default:
933 usage();
934 }
935 }
936 if (optind < ac) {
937 printf("Too many arguments.\n");
938 usage();
939 }
940 if (change_passphrase && change_comment) {
941 printf("Can only have one of -p and -c.\n");
942 usage();
943 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000944 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100945 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100946 if (change_passphrase)
947 do_change_passphrase(pw);
Damien Miller95def091999-11-25 00:26:21 +1100948 if (change_comment)
949 do_change_comment(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000950 if (convert_to_ssh2)
951 do_convert_to_ssh2(pw);
952 if (convert_from_ssh2)
953 do_convert_from_ssh2(pw);
954 if (print_public)
955 do_print_public(pw);
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000956 if (reader_id != NULL) {
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000957#ifdef SMARTCARD
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000958 if (download)
959 do_download(pw, reader_id);
960 else
961 do_upload(pw, reader_id);
Ben Lindstromffce1472001-08-06 21:57:31 +0000962#else /* SMARTCARD */
Ben Lindstrom6818bfb2001-08-06 21:40:04 +0000963 fatal("no support for smartcards.");
Ben Lindstromffce1472001-08-06 21:57:31 +0000964#endif /* SMARTCARD */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000965 }
Damien Miller95def091999-11-25 00:26:21 +1100966
967 arc4random_stir();
968
Damien Millere39cacc2000-11-29 12:18:44 +1100969 type = key_type_from_name(key_type_name);
970 if (type == KEY_UNSPEC) {
971 fprintf(stderr, "unknown key type %s\n", key_type_name);
972 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000973 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100974 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100975 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100976 private = key_generate(type, bits);
977 if (private == NULL) {
978 fprintf(stderr, "key_generate failed");
979 exit(1);
980 }
981 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100982
983 if (!have_identity)
984 ask_filename(pw, "Enter file in which to save the key");
985
986 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000987 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100988 if (strstr(identity_file, dotsshdir) != NULL &&
989 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000990 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100991 error("Could not create directory '%s'.", dotsshdir);
992 else if (!quiet)
993 printf("Created directory '%s'.\n", dotsshdir);
994 }
995 /* If the file already exists, ask the user to confirm. */
996 if (stat(identity_file, &st) >= 0) {
997 char yesno[3];
998 printf("%s already exists.\n", identity_file);
999 printf("Overwrite (y/n)? ");
1000 fflush(stdout);
1001 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1002 exit(1);
1003 if (yesno[0] != 'y' && yesno[0] != 'Y')
1004 exit(1);
1005 }
1006 /* Ask for a passphrase (twice). */
1007 if (identity_passphrase)
1008 passphrase1 = xstrdup(identity_passphrase);
1009 else if (identity_new_passphrase)
1010 passphrase1 = xstrdup(identity_new_passphrase);
1011 else {
1012passphrase_again:
1013 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001014 read_passphrase("Enter passphrase (empty for no "
1015 "passphrase): ", RP_ALLOW_STDIN);
1016 passphrase2 = read_passphrase("Enter same passphrase again: ",
1017 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001018 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001019 /*
1020 * The passphrases do not match. Clear them and
1021 * retry.
1022 */
Damien Miller95def091999-11-25 00:26:21 +11001023 memset(passphrase1, 0, strlen(passphrase1));
1024 memset(passphrase2, 0, strlen(passphrase2));
1025 xfree(passphrase1);
1026 xfree(passphrase2);
1027 printf("Passphrases do not match. Try again.\n");
1028 goto passphrase_again;
1029 }
1030 /* Clear the other copy of the passphrase. */
1031 memset(passphrase2, 0, strlen(passphrase2));
1032 xfree(passphrase2);
1033 }
1034
Damien Miller95def091999-11-25 00:26:21 +11001035 if (identity_comment) {
1036 strlcpy(comment, identity_comment, sizeof(comment));
1037 } else {
Damien Miller4af51302000-04-16 11:18:38 +10001038 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001039 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1040 }
1041
1042 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001043 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001044 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001045 memset(passphrase1, 0, strlen(passphrase1));
1046 xfree(passphrase1);
1047 exit(1);
1048 }
1049 /* Clear the passphrase. */
1050 memset(passphrase1, 0, strlen(passphrase1));
1051 xfree(passphrase1);
1052
1053 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001054 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001055 arc4random_stir();
1056
1057 if (!quiet)
1058 printf("Your identification has been saved in %s.\n", identity_file);
1059
Damien Miller95def091999-11-25 00:26:21 +11001060 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001061 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1062 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001063 printf("Could not save your public key in %s\n", identity_file);
1064 exit(1);
1065 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001066 f = fdopen(fd, "w");
1067 if (f == NULL) {
1068 printf("fdopen %s failed", identity_file);
1069 exit(1);
1070 }
Damien Millereba71ba2000-04-29 23:57:08 +10001071 if (!key_write(public, f))
1072 fprintf(stderr, "write key failed");
1073 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001074 fclose(f);
1075
1076 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001077 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +10001078 printf("Your public key has been saved in %s.\n",
1079 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001080 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001081 printf("%s %s\n", fp, comment);
1082 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001083 }
Damien Millereba71ba2000-04-29 23:57:08 +10001084
1085 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001086 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087}