]> andersk Git - openssh.git/blame - ssh-keygen.c
- markus@cvs.openbsd.org 2002/03/25 17:34:27
[openssh.git] / ssh-keygen.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * Identity and host key generation and maintenance.
bcbf86ec 6 *
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".
5260325f 12 */
8efc0c15 13
14#include "includes.h"
0659cace 15RCSID("$OpenBSD: ssh-keygen.c,v 1.97 2002/03/25 17:34:27 markus Exp $");
8efc0c15 16
a306f2dd 17#include <openssl/evp.h>
18#include <openssl/pem.h>
a306f2dd 19
8efc0c15 20#include "xmalloc.h"
a306f2dd 21#include "key.h"
457fc0c6 22#include "rsa.h"
a306f2dd 23#include "authfile.h"
24#include "uuencode.h"
94ec8c6b 25#include "buffer.h"
26#include "bufaux.h"
42f11eb2 27#include "pathnames.h"
28#include "log.h"
29#include "readpass.h"
94ec8c6b 30
93a56445 31#ifdef SMARTCARD
93a56445 32#include "scard.h"
dd2495cb 33#endif
2b30154a 34
a306f2dd 35/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
8efc0c15 36int bits = 1024;
37
aa3378df 38/*
39 * Flag indicating that we just want to change the passphrase. This can be
40 * set on the command line.
41 */
8efc0c15 42int change_passphrase = 0;
43
aa3378df 44/*
45 * Flag indicating that we just want to change the comment. This can be set
46 * on the command line.
47 */
8efc0c15 48int change_comment = 0;
49
50int quiet = 0;
51
f095fcc7 52/* Flag indicating that we just want to see the key fingerprint */
53int print_fingerprint = 0;
aaf45d87 54int print_bubblebabble = 0;
f095fcc7 55
5ad13cd7 56/* The identity file name, given on the command line or entered by the user. */
57char identity_file[1024];
58int have_identity = 0;
8efc0c15 59
60/* This is set to the passphrase if given on the command line. */
61char *identity_passphrase = NULL;
62
63/* This is set to the new passphrase if given on the command line. */
64char *identity_new_passphrase = NULL;
65
66/* This is set to the new comment if given on the command line. */
67char *identity_comment = NULL;
68
a306f2dd 69/* Dump public key file in format used by real and the original SSH 2 */
70int convert_to_ssh2 = 0;
71int convert_from_ssh2 = 0;
72int print_public = 0;
fa08c86b 73
3dc93cd8 74char *key_type_name = NULL;
a306f2dd 75
5ad13cd7 76/* argv0 */
5260325f 77#ifdef HAVE___PROGNAME
5ad13cd7 78extern char *__progname;
260d427b 79#else
80char *__progname;
81#endif
8efc0c15 82
a306f2dd 83char hostname[MAXHOSTNAMELEN];
84
396c147e 85static void
5ad13cd7 86ask_filename(struct passwd *pw, const char *prompt)
8efc0c15 87{
5260325f 88 char buf[1024];
c523303b 89 char *name = NULL;
90
8d22d775 91 if (key_type_name == NULL)
42f11eb2 92 name = _PATH_SSH_CLIENT_ID_RSA;
8d22d775 93 else
94 switch (key_type_from_name(key_type_name)) {
95 case KEY_RSA1:
96 name = _PATH_SSH_CLIENT_IDENTITY;
97 break;
98 case KEY_DSA:
99 name = _PATH_SSH_CLIENT_ID_DSA;
100 break;
101 case KEY_RSA:
102 name = _PATH_SSH_CLIENT_ID_RSA;
103 break;
104 default:
105 fprintf(stderr, "bad key type");
106 exit(1);
107 break;
108 }
109
c523303b 110 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
8d88011e 111 fprintf(stderr, "%s (%s): ", prompt, identity_file);
112 fflush(stderr);
5260325f 113 if (fgets(buf, sizeof(buf), stdin) == NULL)
114 exit(1);
115 if (strchr(buf, '\n'))
116 *strchr(buf, '\n') = 0;
117 if (strcmp(buf, "") != 0)
118 strlcpy(identity_file, buf, sizeof(identity_file));
119 have_identity = 1;
f095fcc7 120}
8efc0c15 121
396c147e 122static Key *
abe0fb9f 123load_identity(char *filename)
a306f2dd 124{
aeaa3d9e 125 char *pass;
126 Key *prv;
127
cd332296 128 prv = key_load_private(filename, "", NULL);
aeaa3d9e 129 if (prv == NULL) {
abe0fb9f 130 if (identity_passphrase)
131 pass = xstrdup(identity_passphrase);
132 else
1843a425 133 pass = read_passphrase("Enter passphrase: ",
134 RP_ALLOW_STDIN);
aeaa3d9e 135 prv = key_load_private(filename, pass, NULL);
a306f2dd 136 memset(pass, 0, strlen(pass));
137 xfree(pass);
138 }
aeaa3d9e 139 return prv;
a306f2dd 140}
141
94ec8c6b 142#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
143#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
144#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
2b87da3b 145#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
a306f2dd 146
396c147e 147static void
a306f2dd 148do_convert_to_ssh2(struct passwd *pw)
149{
b587c165 150 Key *k;
21b30f38 151 u_int len;
1e3b8b07 152 u_char *blob;
a306f2dd 153 struct stat st;
154
155 if (!have_identity)
156 ask_filename(pw, "Enter file in which the key is");
157 if (stat(identity_file, &st) < 0) {
158 perror(identity_file);
159 exit(1);
160 }
b587c165 161 if ((k = key_load_public(identity_file, NULL)) == NULL) {
abe0fb9f 162 if ((k = load_identity(identity_file)) == NULL) {
b587c165 163 fprintf(stderr, "load failed\n");
164 exit(1);
165 }
a306f2dd 166 }
f7436b8c 167 if (key_to_blob(k, &blob, &len) <= 0) {
168 fprintf(stderr, "key_to_blob failed\n");
169 exit(1);
170 }
94ec8c6b 171 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
a306f2dd 172 fprintf(stdout,
94ec8c6b 173 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
b587c165 174 key_size(k), key_type(k),
a306f2dd 175 pw->pw_name, hostname);
176 dump_base64(stdout, blob, len);
94ec8c6b 177 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
b587c165 178 key_free(k);
a306f2dd 179 xfree(blob);
180 exit(0);
181}
182
396c147e 183static void
94ec8c6b 184buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
185{
186 int bits = buffer_get_int(b);
187 int bytes = (bits + 7) / 8;
457fc0c6 188
94ec8c6b 189 if (buffer_len(b) < bytes)
457fc0c6 190 fatal("buffer_get_bignum_bits: input buffer too small: "
191 "need %d have %d", bytes, buffer_len(b));
20905a8e 192 BN_bin2bn(buffer_ptr(b), bytes, value);
94ec8c6b 193 buffer_consume(b, bytes);
194}
195
396c147e 196static Key *
c66f9d0e 197do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
94ec8c6b 198{
199 Buffer b;
94ec8c6b 200 Key *key = NULL;
a599bd06 201 char *type, *cipher;
d3260e12 202 u_char *sig, data[] = "abcde12345";
2e0becb6 203 int magic, rlen, ktype, i1, i2, i3, i4;
a599bd06 204 u_int slen;
2e0becb6 205 u_long e;
94ec8c6b 206
207 buffer_init(&b);
208 buffer_append(&b, blob, blen);
209
210 magic = buffer_get_int(&b);
211 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
212 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
213 buffer_free(&b);
214 return NULL;
215 }
2e0becb6 216 i1 = buffer_get_int(&b);
94ec8c6b 217 type = buffer_get_string(&b, NULL);
218 cipher = buffer_get_string(&b, NULL);
2e0becb6 219 i2 = buffer_get_int(&b);
220 i3 = buffer_get_int(&b);
221 i4 = buffer_get_int(&b);
222 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
94ec8c6b 223 if (strcmp(cipher, "none") != 0) {
224 error("unsupported cipher %s", cipher);
225 xfree(cipher);
226 buffer_free(&b);
457fc0c6 227 xfree(type);
94ec8c6b 228 return NULL;
229 }
230 xfree(cipher);
231
457fc0c6 232 if (strstr(type, "dsa")) {
233 ktype = KEY_DSA;
234 } else if (strstr(type, "rsa")) {
235 ktype = KEY_RSA;
236 } else {
237 xfree(type);
94ec8c6b 238 return NULL;
239 }
457fc0c6 240 key = key_new_private(ktype);
241 xfree(type);
242
243 switch (key->type) {
244 case KEY_DSA:
245 buffer_get_bignum_bits(&b, key->dsa->p);
246 buffer_get_bignum_bits(&b, key->dsa->g);
247 buffer_get_bignum_bits(&b, key->dsa->q);
248 buffer_get_bignum_bits(&b, key->dsa->pub_key);
249 buffer_get_bignum_bits(&b, key->dsa->priv_key);
250 break;
251 case KEY_RSA:
2e0becb6 252 e = buffer_get_char(&b);
253 debug("e %lx", e);
254 if (e < 30) {
255 e <<= 8;
256 e += buffer_get_char(&b);
257 debug("e %lx", e);
258 e <<= 8;
259 e += buffer_get_char(&b);
260 debug("e %lx", e);
261 }
262 if (!BN_set_word(key->rsa->e, e)) {
457fc0c6 263 buffer_free(&b);
264 key_free(key);
265 return NULL;
266 }
267 buffer_get_bignum_bits(&b, key->rsa->d);
268 buffer_get_bignum_bits(&b, key->rsa->n);
269 buffer_get_bignum_bits(&b, key->rsa->iqmp);
270 buffer_get_bignum_bits(&b, key->rsa->q);
271 buffer_get_bignum_bits(&b, key->rsa->p);
2b63e803 272 rsa_generate_additional_parameters(key->rsa);
457fc0c6 273 break;
274 }
94ec8c6b 275 rlen = buffer_len(&b);
6aacefa7 276 if (rlen != 0)
457fc0c6 277 error("do_convert_private_ssh2_from_blob: "
278 "remaining bytes in key blob %d", rlen);
94ec8c6b 279 buffer_free(&b);
a599bd06 280
281 /* try the key */
282 key_sign(key, &sig, &slen, data, sizeof(data));
283 key_verify(key, sig, slen, data, sizeof(data));
284 xfree(sig);
94ec8c6b 285 return key;
286}
287
396c147e 288static void
a306f2dd 289do_convert_from_ssh2(struct passwd *pw)
290{
291 Key *k;
292 int blen;
293 char line[1024], *p;
cf54363d 294 u_char blob[8096];
a306f2dd 295 char encoded[8096];
296 struct stat st;
94ec8c6b 297 int escaped = 0, private = 0, ok;
a306f2dd 298 FILE *fp;
299
300 if (!have_identity)
301 ask_filename(pw, "Enter file in which the key is");
302 if (stat(identity_file, &st) < 0) {
303 perror(identity_file);
304 exit(1);
305 }
306 fp = fopen(identity_file, "r");
307 if (fp == NULL) {
308 perror(identity_file);
309 exit(1);
310 }
311 encoded[0] = '\0';
312 while (fgets(line, sizeof(line), fp)) {
d0c832f3 313 if (!(p = strchr(line, '\n'))) {
314 fprintf(stderr, "input line too long.\n");
315 exit(1);
316 }
317 if (p > line && p[-1] == '\\')
318 escaped++;
a306f2dd 319 if (strncmp(line, "----", 4) == 0 ||
320 strstr(line, ": ") != NULL) {
94ec8c6b 321 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
322 private = 1;
a599bd06 323 if (strstr(line, " END ") != NULL) {
324 break;
325 }
012bc0e1 326 /* fprintf(stderr, "ignore: %s", line); */
a306f2dd 327 continue;
328 }
d0c832f3 329 if (escaped) {
330 escaped--;
012bc0e1 331 /* fprintf(stderr, "escaped: %s", line); */
d0c832f3 332 continue;
a306f2dd 333 }
334 *p = '\0';
335 strlcat(encoded, line, sizeof(encoded));
336 }
e6207598 337 blen = uudecode(encoded, blob, sizeof(blob));
a306f2dd 338 if (blen < 0) {
339 fprintf(stderr, "uudecode failed.\n");
340 exit(1);
341 }
94ec8c6b 342 k = private ?
343 do_convert_private_ssh2_from_blob(blob, blen) :
fa08c86b 344 key_from_blob(blob, blen);
94ec8c6b 345 if (k == NULL) {
346 fprintf(stderr, "decode blob failed.\n");
347 exit(1);
348 }
349 ok = private ?
457fc0c6 350 (k->type == KEY_DSA ?
351 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
352 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
94ec8c6b 353 key_write(k, stdout);
354 if (!ok) {
355 fprintf(stderr, "key write failed");
356 exit(1);
357 }
a306f2dd 358 key_free(k);
4ccb828d 359 if (!private)
360 fprintf(stdout, "\n");
a306f2dd 361 fclose(fp);
362 exit(0);
363}
364
396c147e 365static void
a306f2dd 366do_print_public(struct passwd *pw)
367{
aeaa3d9e 368 Key *prv;
a306f2dd 369 struct stat st;
370
371 if (!have_identity)
372 ask_filename(pw, "Enter file in which the key is");
373 if (stat(identity_file, &st) < 0) {
374 perror(identity_file);
375 exit(1);
376 }
abe0fb9f 377 prv = load_identity(identity_file);
aeaa3d9e 378 if (prv == NULL) {
a306f2dd 379 fprintf(stderr, "load failed\n");
380 exit(1);
381 }
aeaa3d9e 382 if (!key_write(prv, stdout))
a306f2dd 383 fprintf(stderr, "key_write failed");
aeaa3d9e 384 key_free(prv);
a306f2dd 385 fprintf(stdout, "\n");
386 exit(0);
387}
388
4f831fd7 389#ifdef SMARTCARD
2b30154a 390static void
93a56445 391do_upload(struct passwd *pw, const char *sc_reader_id)
2b30154a 392{
2b30154a 393 Key *prv = NULL;
394 struct stat st;
b9f62352 395 int ret;
396
2b30154a 397 if (!have_identity)
398 ask_filename(pw, "Enter file in which the key is");
399 if (stat(identity_file, &st) < 0) {
400 perror(identity_file);
b9f62352 401 exit(1);
2b30154a 402 }
403 prv = load_identity(identity_file);
404 if (prv == NULL) {
405 error("load failed");
b9f62352 406 exit(1);
49ccba9c 407 }
b9f62352 408 ret = sc_put_key(prv, sc_reader_id);
409 key_free(prv);
410 if (ret < 0)
411 exit(1);
412 log("loading key done");
413 exit(0);
2b30154a 414}
93a56445 415
416static void
417do_download(struct passwd *pw, const char *sc_reader_id)
418{
0659cace 419 Key **keys = NULL;
420 int i;
93a56445 421
0659cace 422 keys = sc_get_keys(sc_reader_id, NULL);
423 if (keys == NULL)
93a56445 424 fatal("cannot read public key from smartcard");
0659cace 425 for (i = 0; keys[i]; i++) {
426 key_write(keys[i], stdout);
427 key_free(keys[i]);
428 fprintf(stdout, "\n");
429 }
430 xfree(keys);
93a56445 431 exit(0);
432}
3e984472 433#endif /* SMARTCARD */
2b30154a 434
396c147e 435static void
f095fcc7 436do_fingerprint(struct passwd *pw)
437{
c8d54615 438 FILE *f;
a306f2dd 439 Key *public;
aaf45d87 440 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
17a3011c 441 int i, skip = 0, num = 1, invalid = 1;
442 enum fp_rep rep;
443 enum fp_type fptype;
5260325f 444 struct stat st;
445
aeaa3d9e 446 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
447 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
aaf45d87 448
5260325f 449 if (!have_identity)
450 ask_filename(pw, "Enter file in which the key is");
451 if (stat(identity_file, &st) < 0) {
452 perror(identity_file);
453 exit(1);
454 }
aeaa3d9e 455 public = key_load_public(identity_file, &comment);
456 if (public != NULL) {
457 fp = key_fingerprint(public, fptype, rep);
458 printf("%d %s %s\n", key_size(public), fp, comment);
a306f2dd 459 key_free(public);
fa08c86b 460 xfree(comment);
aaf45d87 461 xfree(fp);
c8d54615 462 exit(0);
463 }
aeaa3d9e 464 if (comment)
465 xfree(comment);
c8d54615 466
467 f = fopen(identity_file, "r");
468 if (f != NULL) {
c8d54615 469 while (fgets(line, sizeof(line), f)) {
470 i = strlen(line) - 1;
471 if (line[i] != '\n') {
472 error("line %d too long: %.40s...", num, line);
473 skip = 1;
474 continue;
475 }
476 num++;
477 if (skip) {
478 skip = 0;
479 continue;
480 }
481 line[i] = '\0';
482
483 /* Skip leading whitespace, empty and comment lines. */
484 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
485 ;
486 if (!*cp || *cp == '\n' || *cp == '#')
487 continue ;
488 i = strtol(cp, &ep, 10);
489 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
490 int quoted = 0;
491 comment = cp;
492 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
493 if (*cp == '\\' && cp[1] == '"')
494 cp++; /* Skip both */
495 else if (*cp == '"')
496 quoted = !quoted;
497 }
498 if (!*cp)
499 continue;
500 *cp++ = '\0';
501 }
502 ep = cp;
efcae5b1 503 public = key_new(KEY_RSA1);
504 if (key_read(public, &cp) != 1) {
505 cp = ep;
506 key_free(public);
507 public = key_new(KEY_UNSPEC);
508 if (key_read(public, &cp) != 1) {
509 key_free(public);
510 continue;
511 }
5260325f 512 }
efcae5b1 513 comment = *cp ? cp : comment;
aeaa3d9e 514 fp = key_fingerprint(public, fptype, rep);
aaf45d87 515 printf("%d %s %s\n", key_size(public), fp,
efcae5b1 516 comment ? comment : "no comment");
aaf45d87 517 xfree(fp);
aeaa3d9e 518 key_free(public);
efcae5b1 519 invalid = 0;
5260325f 520 }
c8d54615 521 fclose(f);
522 }
523 if (invalid) {
f564d016 524 printf("%s is not a public key file.\n", identity_file);
c8d54615 525 exit(1);
5260325f 526 }
5260325f 527 exit(0);
f095fcc7 528}
529
5260325f 530/*
531 * Perform changing a passphrase. The argument is the passwd structure
532 * for the current user.
533 */
396c147e 534static void
f095fcc7 535do_change_passphrase(struct passwd *pw)
536{
5260325f 537 char *comment;
538 char *old_passphrase, *passphrase1, *passphrase2;
539 struct stat st;
a306f2dd 540 Key *private;
5260325f 541
542 if (!have_identity)
543 ask_filename(pw, "Enter file in which the key is");
5260325f 544 if (stat(identity_file, &st) < 0) {
545 perror(identity_file);
546 exit(1);
547 }
5260325f 548 /* Try to load the file with empty passphrase. */
aeaa3d9e 549 private = key_load_private(identity_file, "", &comment);
550 if (private == NULL) {
5260325f 551 if (identity_passphrase)
552 old_passphrase = xstrdup(identity_passphrase);
553 else
1843a425 554 old_passphrase =
555 read_passphrase("Enter old passphrase: ",
556 RP_ALLOW_STDIN);
557 private = key_load_private(identity_file, old_passphrase,
558 &comment);
aeaa3d9e 559 memset(old_passphrase, 0, strlen(old_passphrase));
560 xfree(old_passphrase);
561 if (private == NULL) {
5260325f 562 printf("Bad passphrase.\n");
563 exit(1);
564 }
5260325f 565 }
566 printf("Key has comment '%s'\n", comment);
567
568 /* Ask the new passphrase (twice). */
569 if (identity_new_passphrase) {
570 passphrase1 = xstrdup(identity_new_passphrase);
571 passphrase2 = NULL;
572 } else {
573 passphrase1 =
1843a425 574 read_passphrase("Enter new passphrase (empty for no "
575 "passphrase): ", RP_ALLOW_STDIN);
576 passphrase2 = read_passphrase("Enter same passphrase again: ",
184eed6a 577 RP_ALLOW_STDIN);
5260325f 578
579 /* Verify that they are the same. */
580 if (strcmp(passphrase1, passphrase2) != 0) {
581 memset(passphrase1, 0, strlen(passphrase1));
582 memset(passphrase2, 0, strlen(passphrase2));
583 xfree(passphrase1);
584 xfree(passphrase2);
585 printf("Pass phrases do not match. Try again.\n");
586 exit(1);
587 }
588 /* Destroy the other copy. */
589 memset(passphrase2, 0, strlen(passphrase2));
590 xfree(passphrase2);
8efc0c15 591 }
8efc0c15 592
5260325f 593 /* Save the file using the new passphrase. */
aeaa3d9e 594 if (!key_save_private(private, identity_file, passphrase1, comment)) {
58cfa257 595 printf("Saving the key failed: %s.\n", identity_file);
5260325f 596 memset(passphrase1, 0, strlen(passphrase1));
597 xfree(passphrase1);
a306f2dd 598 key_free(private);
5260325f 599 xfree(comment);
600 exit(1);
601 }
602 /* Destroy the passphrase and the copy of the key in memory. */
603 memset(passphrase1, 0, strlen(passphrase1));
604 xfree(passphrase1);
a306f2dd 605 key_free(private); /* Destroys contents */
5260325f 606 xfree(comment);
607
608 printf("Your identification has been saved with the new passphrase.\n");
609 exit(0);
610}
8efc0c15 611
5260325f 612/*
613 * Change the comment of a private key file.
614 */
396c147e 615static void
8efc0c15 616do_change_comment(struct passwd *pw)
617{
1c9a907f 618 char new_comment[1024], *comment, *passphrase;
aeaa3d9e 619 Key *private;
620 Key *public;
5260325f 621 struct stat st;
622 FILE *f;
1c9a907f 623 int fd;
5260325f 624
625 if (!have_identity)
626 ask_filename(pw, "Enter file in which the key is");
5260325f 627 if (stat(identity_file, &st) < 0) {
628 perror(identity_file);
629 exit(1);
630 }
aeaa3d9e 631 private = key_load_private(identity_file, "", &comment);
632 if (private == NULL) {
5260325f 633 if (identity_passphrase)
634 passphrase = xstrdup(identity_passphrase);
635 else if (identity_new_passphrase)
636 passphrase = xstrdup(identity_new_passphrase);
637 else
1843a425 638 passphrase = read_passphrase("Enter passphrase: ",
639 RP_ALLOW_STDIN);
5260325f 640 /* Try to load using the passphrase. */
aeaa3d9e 641 private = key_load_private(identity_file, passphrase, &comment);
642 if (private == NULL) {
5260325f 643 memset(passphrase, 0, strlen(passphrase));
644 xfree(passphrase);
645 printf("Bad passphrase.\n");
646 exit(1);
647 }
aeaa3d9e 648 } else {
649 passphrase = xstrdup("");
5260325f 650 }
aeaa3d9e 651 if (private->type != KEY_RSA1) {
652 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
653 key_free(private);
654 exit(1);
184eed6a 655 }
5260325f 656 printf("Key now has comment '%s'\n", comment);
657
658 if (identity_comment) {
659 strlcpy(new_comment, identity_comment, sizeof(new_comment));
660 } else {
661 printf("Enter new comment: ");
662 fflush(stdout);
663 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
664 memset(passphrase, 0, strlen(passphrase));
a306f2dd 665 key_free(private);
5260325f 666 exit(1);
667 }
5260325f 668 if (strchr(new_comment, '\n'))
669 *strchr(new_comment, '\n') = 0;
670 }
671
672 /* Save the file using the new passphrase. */
aeaa3d9e 673 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
58cfa257 674 printf("Saving the key failed: %s.\n", identity_file);
5260325f 675 memset(passphrase, 0, strlen(passphrase));
676 xfree(passphrase);
a306f2dd 677 key_free(private);
5260325f 678 xfree(comment);
679 exit(1);
8efc0c15 680 }
5260325f 681 memset(passphrase, 0, strlen(passphrase));
682 xfree(passphrase);
aeaa3d9e 683 public = key_from_private(private);
a306f2dd 684 key_free(private);
5260325f 685
5260325f 686 strlcat(identity_file, ".pub", sizeof(identity_file));
1c9a907f 687 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
688 if (fd == -1) {
5260325f 689 printf("Could not save your public key in %s\n", identity_file);
690 exit(1);
691 }
1c9a907f 692 f = fdopen(fd, "w");
693 if (f == NULL) {
694 printf("fdopen %s failed", identity_file);
695 exit(1);
696 }
a306f2dd 697 if (!key_write(public, f))
698 fprintf(stderr, "write key failed");
699 key_free(public);
700 fprintf(f, " %s\n", new_comment);
5260325f 701 fclose(f);
702
703 xfree(comment);
704
705 printf("The comment in your key file has been changed.\n");
706 exit(0);
8efc0c15 707}
708
396c147e 709static void
5ad13cd7 710usage(void)
711{
58153e34 712 fprintf(stderr, "Usage: %s [options]\n", __progname);
713 fprintf(stderr, "Options:\n");
714 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
715 fprintf(stderr, " -c Change comment in private and public key files.\n");
716 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
717 fprintf(stderr, " -f filename Filename of the key file.\n");
718 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
719 fprintf(stderr, " -l Show fingerprint of key file.\n");
720 fprintf(stderr, " -p Change passphrase of private key file.\n");
721 fprintf(stderr, " -q Quiet.\n");
722 fprintf(stderr, " -y Read private key file and print public key.\n");
723 fprintf(stderr, " -t type Specify type of key to create.\n");
724 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
725 fprintf(stderr, " -C comment Provide new comment.\n");
726 fprintf(stderr, " -N phrase Provide new passphrase.\n");
727 fprintf(stderr, " -P phrase Provide old passphrase.\n");
728#ifdef SMARTCARD
729 fprintf(stderr, " -D reader Download public key from smartcard.\n");
730 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
731#endif /* SMARTCARD */
732
5260325f 733 exit(1);
5ad13cd7 734}
735
5260325f 736/*
737 * Main program for key management.
738 */
8efc0c15 739int
740main(int ac, char **av)
741{
3dc93cd8 742 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
93a56445 743 char *reader_id = NULL;
1c9a907f 744 Key *private, *public;
5260325f 745 struct passwd *pw;
5260325f 746 struct stat st;
93a56445 747 int opt, type, fd, download = 0;
5260325f 748 FILE *f;
fa08c86b 749
5260325f 750 extern int optind;
751 extern char *optarg;
752
260d427b 753 __progname = get_progname(av[0]);
264dce47 754
fa649821 755 SSLeay_add_all_algorithms();
a306f2dd 756
aa3378df 757 /* we need this for the home * directory. */
5260325f 758 pw = getpwuid(getuid());
759 if (!pw) {
760 printf("You don't exist, go away!\n");
761 exit(1);
762 }
a306f2dd 763 if (gethostname(hostname, sizeof(hostname)) < 0) {
764 perror("gethostname");
765 exit(1);
766 }
aa3378df 767
285d2b15 768 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
5260325f 769 switch (opt) {
770 case 'b':
771 bits = atoi(optarg);
772 if (bits < 512 || bits > 32768) {
773 printf("Bits has bad value.\n");
774 exit(1);
775 }
776 break;
5260325f 777 case 'l':
778 print_fingerprint = 1;
779 break;
aaf45d87 780 case 'B':
781 print_bubblebabble = 1;
782 break;
5260325f 783 case 'p':
784 change_passphrase = 1;
785 break;
5260325f 786 case 'c':
787 change_comment = 1;
788 break;
5260325f 789 case 'f':
790 strlcpy(identity_file, optarg, sizeof(identity_file));
791 have_identity = 1;
792 break;
5260325f 793 case 'P':
794 identity_passphrase = optarg;
795 break;
5260325f 796 case 'N':
797 identity_new_passphrase = optarg;
798 break;
5260325f 799 case 'C':
800 identity_comment = optarg;
801 break;
5260325f 802 case 'q':
803 quiet = 1;
804 break;
a306f2dd 805 case 'R':
fa08c86b 806 /* unused */
807 exit(0);
a306f2dd 808 break;
5deceabb 809 case 'e':
a306f2dd 810 case 'x':
5deceabb 811 /* export key */
a306f2dd 812 convert_to_ssh2 = 1;
813 break;
5deceabb 814 case 'i':
a306f2dd 815 case 'X':
5deceabb 816 /* import key */
a306f2dd 817 convert_from_ssh2 = 1;
818 break;
a306f2dd 819 case 'y':
820 print_public = 1;
821 break;
a306f2dd 822 case 'd':
fa08c86b 823 key_type_name = "dsa";
a306f2dd 824 break;
fa08c86b 825 case 't':
826 key_type_name = optarg;
fa08c86b 827 break;
93a56445 828 case 'D':
829 download = 1;
285d2b15 830 case 'U':
93a56445 831 reader_id = optarg;
2b30154a 832 break;
5260325f 833 case '?':
834 default:
835 usage();
836 }
837 }
838 if (optind < ac) {
839 printf("Too many arguments.\n");
840 usage();
841 }
842 if (change_passphrase && change_comment) {
843 printf("Can only have one of -p and -c.\n");
844 usage();
845 }
aaf45d87 846 if (print_fingerprint || print_bubblebabble)
5260325f 847 do_fingerprint(pw);
5260325f 848 if (change_passphrase)
849 do_change_passphrase(pw);
a306f2dd 850 if (convert_to_ssh2)
851 do_convert_to_ssh2(pw);
633151a3 852 if (change_comment)
853 do_change_comment(pw);
a306f2dd 854 if (print_public)
855 do_print_public(pw);
93a56445 856 if (reader_id != NULL) {
4f831fd7 857#ifdef SMARTCARD
93a56445 858 if (download)
859 do_download(pw, reader_id);
860 else
861 do_upload(pw, reader_id);
3e984472 862#else /* SMARTCARD */
4f831fd7 863 fatal("no support for smartcards.");
3e984472 864#endif /* SMARTCARD */
93a56445 865 }
5260325f 866
8d7324af 867 init_rng();
868 seed_rng();
5260325f 869 arc4random_stir();
870
633151a3 871 if (convert_from_ssh2)
872 do_convert_from_ssh2(pw);
873
bb28e836 874 if (key_type_name == NULL) {
875 printf("You must specify a key type (-t).\n");
876 usage();
877 }
c523303b 878 type = key_type_from_name(key_type_name);
879 if (type == KEY_UNSPEC) {
880 fprintf(stderr, "unknown key type %s\n", key_type_name);
881 exit(1);
a306f2dd 882 }
fa08c86b 883 if (!quiet)
c523303b 884 printf("Generating public/private %s key pair.\n", key_type_name);
fa08c86b 885 private = key_generate(type, bits);
886 if (private == NULL) {
887 fprintf(stderr, "key_generate failed");
888 exit(1);
889 }
890 public = key_from_private(private);
5260325f 891
892 if (!have_identity)
893 ask_filename(pw, "Enter file in which to save the key");
894
895 /* Create ~/.ssh directory if it doesn\'t already exist. */
42f11eb2 896 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
5260325f 897 if (strstr(identity_file, dotsshdir) != NULL &&
898 stat(dotsshdir, &st) < 0) {
704b1659 899 if (mkdir(dotsshdir, 0700) < 0)
5260325f 900 error("Could not create directory '%s'.", dotsshdir);
901 else if (!quiet)
902 printf("Created directory '%s'.\n", dotsshdir);
903 }
904 /* If the file already exists, ask the user to confirm. */
905 if (stat(identity_file, &st) >= 0) {
906 char yesno[3];
907 printf("%s already exists.\n", identity_file);
908 printf("Overwrite (y/n)? ");
909 fflush(stdout);
910 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
911 exit(1);
912 if (yesno[0] != 'y' && yesno[0] != 'Y')
913 exit(1);
914 }
915 /* Ask for a passphrase (twice). */
916 if (identity_passphrase)
917 passphrase1 = xstrdup(identity_passphrase);
918 else if (identity_new_passphrase)
919 passphrase1 = xstrdup(identity_new_passphrase);
920 else {
921passphrase_again:
922 passphrase1 =
1843a425 923 read_passphrase("Enter passphrase (empty for no "
924 "passphrase): ", RP_ALLOW_STDIN);
925 passphrase2 = read_passphrase("Enter same passphrase again: ",
926 RP_ALLOW_STDIN);
5260325f 927 if (strcmp(passphrase1, passphrase2) != 0) {
1843a425 928 /*
929 * The passphrases do not match. Clear them and
930 * retry.
931 */
5260325f 932 memset(passphrase1, 0, strlen(passphrase1));
933 memset(passphrase2, 0, strlen(passphrase2));
934 xfree(passphrase1);
935 xfree(passphrase2);
936 printf("Passphrases do not match. Try again.\n");
937 goto passphrase_again;
938 }
939 /* Clear the other copy of the passphrase. */
940 memset(passphrase2, 0, strlen(passphrase2));
941 xfree(passphrase2);
942 }
943
5260325f 944 if (identity_comment) {
945 strlcpy(comment, identity_comment, sizeof(comment));
946 } else {
6ae2364d 947 /* Create default commend field for the passphrase. */
5260325f 948 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
949 }
950
951 /* Save the key with the given passphrase and comment. */
aeaa3d9e 952 if (!key_save_private(private, identity_file, passphrase1, comment)) {
58cfa257 953 printf("Saving the key failed: %s.\n", identity_file);
5260325f 954 memset(passphrase1, 0, strlen(passphrase1));
955 xfree(passphrase1);
956 exit(1);
957 }
958 /* Clear the passphrase. */
959 memset(passphrase1, 0, strlen(passphrase1));
960 xfree(passphrase1);
961
962 /* Clear the private key and the random number generator. */
fa08c86b 963 key_free(private);
5260325f 964 arc4random_stir();
965
966 if (!quiet)
967 printf("Your identification has been saved in %s.\n", identity_file);
968
5260325f 969 strlcat(identity_file, ".pub", sizeof(identity_file));
1c9a907f 970 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
971 if (fd == -1) {
5260325f 972 printf("Could not save your public key in %s\n", identity_file);
973 exit(1);
974 }
1c9a907f 975 f = fdopen(fd, "w");
976 if (f == NULL) {
977 printf("fdopen %s failed", identity_file);
978 exit(1);
979 }
a306f2dd 980 if (!key_write(public, f))
981 fprintf(stderr, "write key failed");
982 fprintf(f, " %s\n", comment);
5260325f 983 fclose(f);
984
985 if (!quiet) {
22138a36 986 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
a306f2dd 987 printf("Your public key has been saved in %s.\n",
988 identity_file);
5260325f 989 printf("The key fingerprint is:\n");
22138a36 990 printf("%s %s\n", fp, comment);
991 xfree(fp);
8efc0c15 992 }
a306f2dd 993
994 key_free(public);
5260325f 995 exit(0);
8efc0c15 996}
This page took 0.365538 seconds and 5 git commands to generate.