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.
10 RCSID("$OpenBSD: ssh-keygen.c,v 1.29 2000/07/15 04:01:37 djm Exp $");
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",
80 dsa_mode ? SSH_CLIENT_ID_DSA: SSH_CLIENT_IDENTITY);
81 printf("%s (%s): ", prompt, identity_file);
83 if (fgets(buf, sizeof(buf), stdin) == NULL)
85 if (strchr(buf, '\n'))
86 *strchr(buf, '\n') = 0;
87 if (strcmp(buf, "") != 0)
88 strlcpy(identity_file, buf, sizeof(identity_file));
93 try_load_key(char *filename, Key *k)
96 if (!load_private_key(filename, "", k, NULL)) {
97 char *pass = read_passphrase("Enter passphrase: ", 1);
98 if (!load_private_key(filename, pass, k, NULL)) {
101 memset(pass, 0, strlen(pass));
107 #define SSH_COM_MAGIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
108 #define SSH_COM_MAGIC_END "---- END SSH2 PUBLIC KEY ----"
111 do_convert_to_ssh2(struct passwd *pw)
119 ask_filename(pw, "Enter file in which the key is");
120 if (stat(identity_file, &st) < 0) {
121 perror(identity_file);
124 k = key_new(KEY_DSA);
125 if (!try_load_key(identity_file, k)) {
126 fprintf(stderr, "load failed\n");
129 dsa_make_key_blob(k, &blob, &len);
130 fprintf(stdout, "%s\n", SSH_COM_MAGIC_BEGIN);
132 "Comment: \"%d-bit DSA, converted from openssh by %s@%s\"\n",
133 BN_num_bits(k->dsa->p),
134 pw->pw_name, hostname);
135 dump_base64(stdout, blob, len);
136 fprintf(stdout, "%s\n", SSH_COM_MAGIC_END);
143 do_convert_from_ssh2(struct passwd *pw)
155 ask_filename(pw, "Enter file in which the key is");
156 if (stat(identity_file, &st) < 0) {
157 perror(identity_file);
160 fp = fopen(identity_file, "r");
162 perror(identity_file);
166 while (fgets(line, sizeof(line), fp)) {
167 if (!(p = strchr(line, '\n'))) {
168 fprintf(stderr, "input line too long.\n");
171 if (p > line && p[-1] == '\\')
173 if (strncmp(line, "----", 4) == 0 ||
174 strstr(line, ": ") != NULL) {
175 fprintf(stderr, "ignore: %s", line);
180 fprintf(stderr, "escaped: %s", line);
184 strlcat(encoded, line, sizeof(encoded));
186 blen = uudecode(encoded, (unsigned char *)blob, sizeof(blob));
188 fprintf(stderr, "uudecode failed.\n");
191 k = dsa_key_from_blob(blob, blen);
192 if (!key_write(k, stdout))
193 fprintf(stderr, "key_write failed");
195 fprintf(stdout, "\n");
201 do_print_public(struct passwd *pw)
209 ask_filename(pw, "Enter file in which the key is");
210 if (stat(identity_file, &st) < 0) {
211 perror(identity_file);
214 k = key_new(KEY_DSA);
215 if (!try_load_key(identity_file, k)) {
216 fprintf(stderr, "load failed\n");
219 dsa_make_key_blob(k, &blob, &len);
220 if (!key_write(k, stdout))
221 fprintf(stderr, "key_write failed");
224 fprintf(stdout, "\n");
229 do_fingerprint(struct passwd *pw)
234 char *comment = NULL, *cp, *ep, line[16*1024];
235 int i, skip = 0, num = 1, invalid = 1;
240 ask_filename(pw, "Enter file in which the key is");
241 if (stat(identity_file, &st) < 0) {
242 perror(identity_file);
245 public = key_new(KEY_RSA);
246 if (load_public_key(identity_file, public, &comment)) {
247 printf("%d %s %s\n", BN_num_bits(public->rsa->n),
248 key_fingerprint(public), comment);
255 f = fopen(identity_file, "r");
259 while (fgets(line, sizeof(line), f)) {
260 i = strlen(line) - 1;
261 if (line[i] != '\n') {
262 error("line %d too long: %.40s...", num, line);
273 /* Skip leading whitespace, empty and comment lines. */
274 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
276 if (!*cp || *cp == '\n' || *cp == '#')
278 i = strtol(cp, &ep, 10);
279 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
282 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
283 if (*cp == '\\' && cp[1] == '"')
284 cp++; /* Skip both */
293 if (auth_rsa_read_key(&cp, &ignore, e, n)) {
295 comment = *cp ? cp : comment;
296 printf("%d %s %s\n", BN_num_bits(n),
298 comment ? comment : "no comment");
306 printf("%s is not a valid key file.\n", identity_file);
313 * Perform changing a passphrase. The argument is the passwd structure
314 * for the current user.
317 do_change_passphrase(struct passwd *pw)
320 char *old_passphrase, *passphrase1, *passphrase2;
324 int type = dsa_mode ? KEY_DSA : KEY_RSA;
327 ask_filename(pw, "Enter file in which the key is");
328 if (stat(identity_file, &st) < 0) {
329 perror(identity_file);
333 if (type == KEY_RSA) {
334 /* XXX this works currently only for RSA */
335 public = key_new(type);
336 if (!load_public_key(identity_file, public, NULL)) {
337 printf("%s is not a valid key file.\n", identity_file);
340 /* Clear the public key since we are just about to load the whole file. */
344 /* Try to load the file with empty passphrase. */
345 private = key_new(type);
346 if (!load_private_key(identity_file, "", private, &comment)) {
347 if (identity_passphrase)
348 old_passphrase = xstrdup(identity_passphrase);
350 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
351 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
352 memset(old_passphrase, 0, strlen(old_passphrase));
353 xfree(old_passphrase);
354 printf("Bad passphrase.\n");
357 memset(old_passphrase, 0, strlen(old_passphrase));
358 xfree(old_passphrase);
360 printf("Key has comment '%s'\n", comment);
362 /* Ask the new passphrase (twice). */
363 if (identity_new_passphrase) {
364 passphrase1 = xstrdup(identity_new_passphrase);
368 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
369 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
371 /* Verify that they are the same. */
372 if (strcmp(passphrase1, passphrase2) != 0) {
373 memset(passphrase1, 0, strlen(passphrase1));
374 memset(passphrase2, 0, strlen(passphrase2));
377 printf("Pass phrases do not match. Try again.\n");
380 /* Destroy the other copy. */
381 memset(passphrase2, 0, strlen(passphrase2));
385 /* Save the file using the new passphrase. */
386 if (!save_private_key(identity_file, passphrase1, private, comment)) {
387 printf("Saving the key failed: %s: %s.\n",
388 identity_file, strerror(errno));
389 memset(passphrase1, 0, strlen(passphrase1));
395 /* Destroy the passphrase and the copy of the key in memory. */
396 memset(passphrase1, 0, strlen(passphrase1));
398 key_free(private); /* Destroys contents */
401 printf("Your identification has been saved with the new passphrase.\n");
406 * Change the comment of a private key file.
409 do_change_comment(struct passwd *pw)
411 char new_comment[1024], *comment;
419 ask_filename(pw, "Enter file in which the key is");
420 if (stat(identity_file, &st) < 0) {
421 perror(identity_file);
425 * Try to load the public key from the file the verify that it is
426 * readable and of the proper format.
428 public = key_new(KEY_RSA);
429 if (!load_public_key(identity_file, public, NULL)) {
430 printf("%s is not a valid key file.\n", identity_file);
434 private = key_new(KEY_RSA);
435 if (load_private_key(identity_file, "", private, &comment))
436 passphrase = xstrdup("");
438 if (identity_passphrase)
439 passphrase = xstrdup(identity_passphrase);
440 else if (identity_new_passphrase)
441 passphrase = xstrdup(identity_new_passphrase);
443 passphrase = read_passphrase("Enter passphrase: ", 1);
444 /* Try to load using the passphrase. */
445 if (!load_private_key(identity_file, passphrase, private, &comment)) {
446 memset(passphrase, 0, strlen(passphrase));
448 printf("Bad passphrase.\n");
452 printf("Key now has comment '%s'\n", comment);
454 if (identity_comment) {
455 strlcpy(new_comment, identity_comment, sizeof(new_comment));
457 printf("Enter new comment: ");
459 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
460 memset(passphrase, 0, strlen(passphrase));
464 if (strchr(new_comment, '\n'))
465 *strchr(new_comment, '\n') = 0;
468 /* Save the file using the new passphrase. */
469 if (!save_private_key(identity_file, passphrase, private, new_comment)) {
470 printf("Saving the key failed: %s: %s.\n",
471 identity_file, strerror(errno));
472 memset(passphrase, 0, strlen(passphrase));
478 memset(passphrase, 0, strlen(passphrase));
482 strlcat(identity_file, ".pub", sizeof(identity_file));
483 f = fopen(identity_file, "w");
485 printf("Could not save your public key in %s\n", identity_file);
488 if (!key_write(public, f))
489 fprintf(stderr, "write key failed");
491 fprintf(f, " %s\n", new_comment);
496 printf("The comment in your key file has been changed.\n");
503 printf("Usage: %s [-lpqxXydc] [-b bits] [-f file] [-C comment] [-N new-pass] [-P pass]\n", __progname);
508 * Main program for key management.
511 main(int ac, char **av)
513 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
525 SSLeay_add_all_algorithms();
527 /* we need this for the home * directory. */
528 pw = getpwuid(getuid());
530 printf("You don't exist, go away!\n");
533 if (gethostname(hostname, sizeof(hostname)) < 0) {
534 perror("gethostname");
538 while ((opt = getopt(ac, av, "dqpclRxXyb:f:P:N:C:")) != EOF) {
542 if (bits < 512 || bits > 32768) {
543 printf("Bits has bad value.\n");
549 print_fingerprint = 1;
553 change_passphrase = 1;
561 strlcpy(identity_file, optarg, sizeof(identity_file));
566 identity_passphrase = optarg;
570 identity_new_passphrase = optarg;
574 identity_comment = optarg;
582 if (rsa_alive() == 0)
593 convert_from_ssh2 = 1;
610 printf("Too many arguments.\n");
613 if (change_passphrase && change_comment) {
614 printf("Can only have one of -p and -c.\n");
617 /* check if RSA support is needed and exists */
618 if (dsa_mode == 0 && rsa_alive() == 0) {
620 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
624 if (print_fingerprint)
626 if (change_passphrase)
627 do_change_passphrase(pw);
629 do_change_comment(pw);
631 do_convert_to_ssh2(pw);
632 if (convert_from_ssh2)
633 do_convert_from_ssh2(pw);
641 printf("Generating DSA parameter and key.\n");
642 public = private = dsa_generate_key(bits);
643 if (private == NULL) {
644 fprintf(stderr, "dsa_generate_keys failed");
650 /* Generate the rsa key pair. */
651 public = key_new(KEY_RSA);
652 private = key_new(KEY_RSA);
653 rsa_generate_key(private->rsa, public->rsa, bits);
657 ask_filename(pw, "Enter file in which to save the key");
659 /* Create ~/.ssh directory if it doesn\'t already exist. */
660 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR);
661 if (strstr(identity_file, dotsshdir) != NULL &&
662 stat(dotsshdir, &st) < 0) {
663 if (mkdir(dotsshdir, 0700) < 0)
664 error("Could not create directory '%s'.", dotsshdir);
666 printf("Created directory '%s'.\n", dotsshdir);
668 /* If the file already exists, ask the user to confirm. */
669 if (stat(identity_file, &st) >= 0) {
671 printf("%s already exists.\n", identity_file);
672 printf("Overwrite (y/n)? ");
674 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
676 if (yesno[0] != 'y' && yesno[0] != 'Y')
679 /* Ask for a passphrase (twice). */
680 if (identity_passphrase)
681 passphrase1 = xstrdup(identity_passphrase);
682 else if (identity_new_passphrase)
683 passphrase1 = xstrdup(identity_new_passphrase);
687 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
688 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
689 if (strcmp(passphrase1, passphrase2) != 0) {
690 /* The passphrases do not match. Clear them and retry. */
691 memset(passphrase1, 0, strlen(passphrase1));
692 memset(passphrase2, 0, strlen(passphrase2));
695 printf("Passphrases do not match. Try again.\n");
696 goto passphrase_again;
698 /* Clear the other copy of the passphrase. */
699 memset(passphrase2, 0, strlen(passphrase2));
703 if (identity_comment) {
704 strlcpy(comment, identity_comment, sizeof(comment));
706 /* Create default commend field for the passphrase. */
707 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
710 /* Save the key with the given passphrase and comment. */
711 if (!save_private_key(identity_file, passphrase1, private, comment)) {
712 printf("Saving the key failed: %s: %s.\n",
713 identity_file, strerror(errno));
714 memset(passphrase1, 0, strlen(passphrase1));
718 /* Clear the passphrase. */
719 memset(passphrase1, 0, strlen(passphrase1));
722 /* Clear the private key and the random number generator. */
723 if (private != public) {
729 printf("Your identification has been saved in %s.\n", identity_file);
731 strlcat(identity_file, ".pub", sizeof(identity_file));
732 f = fopen(identity_file, "w");
734 printf("Could not save your public key in %s\n", identity_file);
737 if (!key_write(public, f))
738 fprintf(stderr, "write key failed");
739 fprintf(f, " %s\n", comment);
743 printf("Your public key has been saved in %s.\n",
745 printf("The key fingerprint is:\n");
746 printf("%s %s\n", key_fingerprint(public), comment);