2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Created: Mon Mar 27 02:26:40 1995 ylo
6 * Identity and host key generation and maintenance.
12 #include <openssl/evp.h>
13 #include <openssl/pem.h>
14 #include <openssl/rsa.h>
15 #include <openssl/dsa.h>
19 #include "fingerprint.h"
26 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
30 * Flag indicating that we just want to change the passphrase. This can be
31 * set on the command line.
33 int change_passphrase = 0;
36 * Flag indicating that we just want to change the comment. This can be set
37 * on the command line.
39 int change_comment = 0;
43 /* Flag indicating that we just want to see the key fingerprint */
44 int print_fingerprint = 0;
46 /* The identity file name, given on the command line or entered by the user. */
47 char identity_file[1024];
48 int have_identity = 0;
50 /* This is set to the passphrase if given on the command line. */
51 char *identity_passphrase = NULL;
53 /* This is set to the new passphrase if given on the command line. */
54 char *identity_new_passphrase = NULL;
56 /* This is set to the new comment if given on the command line. */
57 char *identity_comment = NULL;
59 /* Dump public key file in format used by real and the original SSH 2 */
60 int convert_to_ssh2 = 0;
61 int convert_from_ssh2 = 0;
66 #ifdef HAVE___PROGNAME
67 extern char *__progname;
68 #else /* HAVE___PROGNAME */
69 static const char *__progname = "ssh-keygen";
70 #endif /* HAVE___PROGNAME */
72 char hostname[MAXHOSTNAMELEN];
75 ask_filename(struct passwd *pw, const char *prompt)
78 snprintf(identity_file, sizeof(identity_file), "%s/%s",
79 pw->pw_dir, SSH_CLIENT_IDENTITY);
80 printf("%s (%s): ", prompt, identity_file);
82 if (fgets(buf, sizeof(buf), stdin) == NULL)
84 if (strchr(buf, '\n'))
85 *strchr(buf, '\n') = 0;
86 if (strcmp(buf, "") != 0)
87 strlcpy(identity_file, buf, sizeof(identity_file));
92 try_load_key(char *filename, Key *k)
95 if (!load_private_key(filename, "", k, NULL)) {
96 char *pass = read_passphrase("Enter passphrase: ", 1);
97 if (!load_private_key(filename, pass, k, NULL)) {
100 memset(pass, 0, strlen(pass));
106 #define SSH_COM_MAGIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
107 #define SSH_COM_MAGIC_END "---- END SSH2 PUBLIC KEY ----"
110 do_convert_to_ssh2(struct passwd *pw)
118 ask_filename(pw, "Enter file in which the key is");
119 if (stat(identity_file, &st) < 0) {
120 perror(identity_file);
123 k = key_new(KEY_DSA);
124 if (!try_load_key(identity_file, k)) {
125 fprintf(stderr, "load failed\n");
128 dsa_make_key_blob(k, &blob, &len);
129 fprintf(stdout, SSH_COM_MAGIC_BEGIN "\n");
131 "Comment: \"%d-bit DSA, converted from openssh by %s@%s\"\n",
132 BN_num_bits(k->dsa->p),
133 pw->pw_name, hostname);
134 dump_base64(stdout, blob, len);
135 fprintf(stdout, SSH_COM_MAGIC_END "\n");
142 do_convert_from_ssh2(struct passwd *pw)
153 ask_filename(pw, "Enter file in which the key is");
154 if (stat(identity_file, &st) < 0) {
155 perror(identity_file);
158 fp = fopen(identity_file, "r");
160 perror(identity_file);
164 while (fgets(line, sizeof(line), fp)) {
165 if (strncmp(line, "----", 4) == 0 ||
166 strstr(line, ": ") != NULL) {
167 fprintf(stderr, "ignore: %s", line);
170 if (!(p = strchr(line, '\n'))) {
171 fprintf(stderr, "input line too long.\n");
175 strlcat(encoded, line, sizeof(encoded));
177 blen = uudecode(encoded, (unsigned char *)blob, sizeof(blob));
179 fprintf(stderr, "uudecode failed.\n");
182 k = dsa_key_from_blob(blob, blen);
183 if (!key_write(k, stdout))
184 fprintf(stderr, "key_write failed");
186 fprintf(stdout, "\n");
192 do_print_public(struct passwd *pw)
200 ask_filename(pw, "Enter file in which the key is");
201 if (stat(identity_file, &st) < 0) {
202 perror(identity_file);
205 k = key_new(KEY_DSA);
206 if (!try_load_key(identity_file, k)) {
207 fprintf(stderr, "load failed\n");
210 dsa_make_key_blob(k, &blob, &len);
211 if (!key_write(k, stdout))
212 fprintf(stderr, "key_write failed");
215 fprintf(stdout, "\n");
220 do_fingerprint(struct passwd *pw)
225 char *comment = NULL, *cp, *ep, line[16*1024];
226 int i, skip = 0, num = 1, invalid = 1;
231 ask_filename(pw, "Enter file in which the key is");
232 if (stat(identity_file, &st) < 0) {
233 perror(identity_file);
236 public = key_new(KEY_RSA);
237 if (load_public_key(identity_file, public, &comment)) {
238 printf("%d %s %s\n", BN_num_bits(public->rsa->n),
239 key_fingerprint(public), comment);
246 f = fopen(identity_file, "r");
250 while (fgets(line, sizeof(line), f)) {
251 i = strlen(line) - 1;
252 if (line[i] != '\n') {
253 error("line %d too long: %.40s...", num, line);
264 /* Skip leading whitespace, empty and comment lines. */
265 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
267 if (!*cp || *cp == '\n' || *cp == '#')
269 i = strtol(cp, &ep, 10);
270 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
273 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
274 if (*cp == '\\' && cp[1] == '"')
275 cp++; /* Skip both */
284 if (auth_rsa_read_key(&cp, &ignore, e, n)) {
286 comment = *cp ? cp : comment;
287 printf("%d %s %s\n", BN_num_bits(n),
289 comment ? comment : "no comment");
297 printf("%s is not a valid key file.\n", identity_file);
304 * Perform changing a passphrase. The argument is the passwd structure
305 * for the current user.
308 do_change_passphrase(struct passwd *pw)
311 char *old_passphrase, *passphrase1, *passphrase2;
315 int type = dsa_mode ? KEY_DSA : KEY_RSA;
318 ask_filename(pw, "Enter file in which the key is");
319 if (stat(identity_file, &st) < 0) {
320 perror(identity_file);
324 if (type == KEY_RSA) {
325 /* XXX this works currently only for RSA */
326 public = key_new(type);
327 if (!load_public_key(identity_file, public, NULL)) {
328 printf("%s is not a valid key file.\n", identity_file);
331 /* Clear the public key since we are just about to load the whole file. */
335 /* Try to load the file with empty passphrase. */
336 private = key_new(type);
337 if (!load_private_key(identity_file, "", private, &comment)) {
338 if (identity_passphrase)
339 old_passphrase = xstrdup(identity_passphrase);
341 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
342 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
343 memset(old_passphrase, 0, strlen(old_passphrase));
344 xfree(old_passphrase);
345 printf("Bad passphrase.\n");
348 memset(old_passphrase, 0, strlen(old_passphrase));
349 xfree(old_passphrase);
351 printf("Key has comment '%s'\n", comment);
353 /* Ask the new passphrase (twice). */
354 if (identity_new_passphrase) {
355 passphrase1 = xstrdup(identity_new_passphrase);
359 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
360 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
362 /* Verify that they are the same. */
363 if (strcmp(passphrase1, passphrase2) != 0) {
364 memset(passphrase1, 0, strlen(passphrase1));
365 memset(passphrase2, 0, strlen(passphrase2));
368 printf("Pass phrases do not match. Try again.\n");
371 /* Destroy the other copy. */
372 memset(passphrase2, 0, strlen(passphrase2));
376 /* Save the file using the new passphrase. */
377 if (!save_private_key(identity_file, passphrase1, private, comment)) {
378 printf("Saving the key failed: %s: %s.\n",
379 identity_file, strerror(errno));
380 memset(passphrase1, 0, strlen(passphrase1));
386 /* Destroy the passphrase and the copy of the key in memory. */
387 memset(passphrase1, 0, strlen(passphrase1));
389 key_free(private); /* Destroys contents */
392 printf("Your identification has been saved with the new passphrase.\n");
397 * Change the comment of a private key file.
400 do_change_comment(struct passwd *pw)
402 char new_comment[1024], *comment;
410 ask_filename(pw, "Enter file in which the key is");
411 if (stat(identity_file, &st) < 0) {
412 perror(identity_file);
416 * Try to load the public key from the file the verify that it is
417 * readable and of the proper format.
419 public = key_new(KEY_RSA);
420 if (!load_public_key(identity_file, public, NULL)) {
421 printf("%s is not a valid key file.\n", identity_file);
425 private = key_new(KEY_RSA);
426 if (load_private_key(identity_file, "", private, &comment))
427 passphrase = xstrdup("");
429 if (identity_passphrase)
430 passphrase = xstrdup(identity_passphrase);
431 else if (identity_new_passphrase)
432 passphrase = xstrdup(identity_new_passphrase);
434 passphrase = read_passphrase("Enter passphrase: ", 1);
435 /* Try to load using the passphrase. */
436 if (!load_private_key(identity_file, passphrase, private, &comment)) {
437 memset(passphrase, 0, strlen(passphrase));
439 printf("Bad passphrase.\n");
443 printf("Key now has comment '%s'\n", comment);
445 if (identity_comment) {
446 strlcpy(new_comment, identity_comment, sizeof(new_comment));
448 printf("Enter new comment: ");
450 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
451 memset(passphrase, 0, strlen(passphrase));
455 if (strchr(new_comment, '\n'))
456 *strchr(new_comment, '\n') = 0;
459 /* Save the file using the new passphrase. */
460 if (!save_private_key(identity_file, passphrase, private, new_comment)) {
461 printf("Saving the key failed: %s: %s.\n",
462 identity_file, strerror(errno));
463 memset(passphrase, 0, strlen(passphrase));
469 memset(passphrase, 0, strlen(passphrase));
473 strlcat(identity_file, ".pub", sizeof(identity_file));
474 f = fopen(identity_file, "w");
476 printf("Could not save your public key in %s\n", identity_file);
479 if (!key_write(public, f))
480 fprintf(stderr, "write key failed");
482 fprintf(f, " %s\n", new_comment);
487 printf("The comment in your key file has been changed.\n");
494 printf("ssh-keygen version %s\n", SSH_VERSION);
495 printf("Usage: %s [-b bits] [-c] [-d] [-f file] [-l] [-p] [-q] [-x] [-y] [-C comment] [-N new-pass] [-P pass] [-X]\n", __progname);
500 * Main program for key management.
503 main(int ac, char **av)
505 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
515 OpenSSL_add_all_algorithms();
517 /* we need this for the home * directory. */
518 pw = getpwuid(getuid());
520 printf("You don't exist, go away!\n");
523 if (gethostname(hostname, sizeof(hostname)) < 0) {
524 perror("gethostname");
528 while ((opt = getopt(ac, av, "dqpclRxXyb:f:P:N:C:")) != EOF) {
532 if (bits < 512 || bits > 32768) {
533 printf("Bits has bad value.\n");
539 print_fingerprint = 1;
543 change_passphrase = 1;
551 strlcpy(identity_file, optarg, sizeof(identity_file));
556 identity_passphrase = optarg;
560 identity_new_passphrase = optarg;
564 identity_comment = optarg;
572 if (rsa_alive() == 0)
583 convert_from_ssh2 = 1;
600 printf("Too many arguments.\n");
603 if (change_passphrase && change_comment) {
604 printf("Can only have one of -p and -c.\n");
607 /* check if RSA support is needed and exists */
608 if (dsa_mode == 0 && rsa_alive() == 0) {
610 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
614 if (print_fingerprint)
616 if (change_passphrase)
617 do_change_passphrase(pw);
619 do_change_comment(pw);
621 do_convert_to_ssh2(pw);
622 if (convert_from_ssh2)
623 do_convert_from_ssh2(pw);
631 printf("Generating DSA parameter and key.\n");
632 public = private = dsa_generate_key(bits);
633 if (private == NULL) {
634 fprintf(stderr, "dsa_generate_keys failed");
640 /* Generate the rsa key pair. */
641 public = key_new(KEY_RSA);
642 private = key_new(KEY_RSA);
643 rsa_generate_key(private->rsa, public->rsa, bits);
647 ask_filename(pw, "Enter file in which to save the key");
649 /* Create ~/.ssh directory if it doesn\'t already exist. */
650 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR);
651 if (strstr(identity_file, dotsshdir) != NULL &&
652 stat(dotsshdir, &st) < 0) {
653 if (mkdir(dotsshdir, 0755) < 0)
654 error("Could not create directory '%s'.", dotsshdir);
656 printf("Created directory '%s'.\n", dotsshdir);
658 /* If the file already exists, ask the user to confirm. */
659 if (stat(identity_file, &st) >= 0) {
661 printf("%s already exists.\n", identity_file);
662 printf("Overwrite (y/n)? ");
664 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
666 if (yesno[0] != 'y' && yesno[0] != 'Y')
669 /* Ask for a passphrase (twice). */
670 if (identity_passphrase)
671 passphrase1 = xstrdup(identity_passphrase);
672 else if (identity_new_passphrase)
673 passphrase1 = xstrdup(identity_new_passphrase);
677 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
678 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
679 if (strcmp(passphrase1, passphrase2) != 0) {
680 /* The passphrases do not match. Clear them and retry. */
681 memset(passphrase1, 0, strlen(passphrase1));
682 memset(passphrase2, 0, strlen(passphrase2));
685 printf("Passphrases do not match. Try again.\n");
686 goto passphrase_again;
688 /* Clear the other copy of the passphrase. */
689 memset(passphrase2, 0, strlen(passphrase2));
693 if (identity_comment) {
694 strlcpy(comment, identity_comment, sizeof(comment));
696 /* Create default commend field for the passphrase. */
697 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
700 /* Save the key with the given passphrase and comment. */
701 if (!save_private_key(identity_file, passphrase1, private, comment)) {
702 printf("Saving the key failed: %s: %s.\n",
703 identity_file, strerror(errno));
704 memset(passphrase1, 0, strlen(passphrase1));
708 /* Clear the passphrase. */
709 memset(passphrase1, 0, strlen(passphrase1));
712 /* Clear the private key and the random number generator. */
713 if (private != public) {
719 printf("Your identification has been saved in %s.\n", identity_file);
721 strlcat(identity_file, ".pub", sizeof(identity_file));
722 f = fopen(identity_file, "w");
724 printf("Could not save your public key in %s\n", identity_file);
727 if (!key_write(public, f))
728 fprintf(stderr, "write key failed");
729 fprintf(f, " %s\n", comment);
733 printf("Your public key has been saved in %s.\n",
735 printf("The key fingerprint is:\n");
736 printf("%s %s\n", key_fingerprint(public), comment);