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.30 2000/08/19 21:34:43 markus Exp $");
12 #include <openssl/evp.h>
13 #include <openssl/pem.h>
14 #include <openssl/rsa.h>
15 #include <openssl/dsa.h>
25 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
29 * Flag indicating that we just want to change the passphrase. This can be
30 * set on the command line.
32 int change_passphrase = 0;
35 * Flag indicating that we just want to change the comment. This can be set
36 * on the command line.
38 int change_comment = 0;
42 /* Flag indicating that we just want to see the key fingerprint */
43 int print_fingerprint = 0;
45 /* The identity file name, given on the command line or entered by the user. */
46 char identity_file[1024];
47 int have_identity = 0;
49 /* This is set to the passphrase if given on the command line. */
50 char *identity_passphrase = NULL;
52 /* This is set to the new passphrase if given on the command line. */
53 char *identity_new_passphrase = NULL;
55 /* This is set to the new comment if given on the command line. */
56 char *identity_comment = NULL;
58 /* Dump public key file in format used by real and the original SSH 2 */
59 int convert_to_ssh2 = 0;
60 int convert_from_ssh2 = 0;
65 #ifdef HAVE___PROGNAME
66 extern char *__progname;
67 #else /* HAVE___PROGNAME */
68 static const char *__progname = "ssh-keygen";
69 #endif /* HAVE___PROGNAME */
71 char hostname[MAXHOSTNAMELEN];
74 ask_filename(struct passwd *pw, const char *prompt)
77 snprintf(identity_file, sizeof(identity_file), "%s/%s",
79 dsa_mode ? SSH_CLIENT_ID_DSA: 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, "%s\n", SSH_COM_MAGIC_BEGIN);
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, "%s\n", SSH_COM_MAGIC_END);
142 do_convert_from_ssh2(struct passwd *pw)
154 ask_filename(pw, "Enter file in which the key is");
155 if (stat(identity_file, &st) < 0) {
156 perror(identity_file);
159 fp = fopen(identity_file, "r");
161 perror(identity_file);
165 while (fgets(line, sizeof(line), fp)) {
166 if (!(p = strchr(line, '\n'))) {
167 fprintf(stderr, "input line too long.\n");
170 if (p > line && p[-1] == '\\')
172 if (strncmp(line, "----", 4) == 0 ||
173 strstr(line, ": ") != NULL) {
174 fprintf(stderr, "ignore: %s", line);
179 fprintf(stderr, "escaped: %s", line);
183 strlcat(encoded, line, sizeof(encoded));
185 blen = uudecode(encoded, (unsigned char *)blob, sizeof(blob));
187 fprintf(stderr, "uudecode failed.\n");
190 k = dsa_key_from_blob(blob, blen);
191 if (!key_write(k, stdout))
192 fprintf(stderr, "key_write failed");
194 fprintf(stdout, "\n");
200 do_print_public(struct passwd *pw)
208 ask_filename(pw, "Enter file in which the key is");
209 if (stat(identity_file, &st) < 0) {
210 perror(identity_file);
213 k = key_new(KEY_DSA);
214 if (!try_load_key(identity_file, k)) {
215 fprintf(stderr, "load failed\n");
218 dsa_make_key_blob(k, &blob, &len);
219 if (!key_write(k, stdout))
220 fprintf(stderr, "key_write failed");
223 fprintf(stdout, "\n");
228 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);
253 f = fopen(identity_file, "r");
255 while (fgets(line, sizeof(line), f)) {
256 i = strlen(line) - 1;
257 if (line[i] != '\n') {
258 error("line %d too long: %.40s...", num, line);
269 /* Skip leading whitespace, empty and comment lines. */
270 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
272 if (!*cp || *cp == '\n' || *cp == '#')
274 i = strtol(cp, &ep, 10);
275 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
278 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
279 if (*cp == '\\' && cp[1] == '"')
280 cp++; /* Skip both */
289 if (auth_rsa_read_key(&cp, &ignore, public->rsa->e, public->rsa->n)) {
291 comment = *cp ? cp : comment;
292 printf("%d %s %s\n", key_size(public),
293 key_fingerprint(public),
294 comment ? comment : "no comment");
301 printf("%s is not a valid key file.\n", identity_file);
308 * Perform changing a passphrase. The argument is the passwd structure
309 * for the current user.
312 do_change_passphrase(struct passwd *pw)
315 char *old_passphrase, *passphrase1, *passphrase2;
319 int type = dsa_mode ? KEY_DSA : KEY_RSA;
322 ask_filename(pw, "Enter file in which the key is");
323 if (stat(identity_file, &st) < 0) {
324 perror(identity_file);
328 if (type == KEY_RSA) {
329 /* XXX this works currently only for RSA */
330 public = key_new(type);
331 if (!load_public_key(identity_file, public, NULL)) {
332 printf("%s is not a valid key file.\n", identity_file);
335 /* Clear the public key since we are just about to load the whole file. */
339 /* Try to load the file with empty passphrase. */
340 private = key_new(type);
341 if (!load_private_key(identity_file, "", private, &comment)) {
342 if (identity_passphrase)
343 old_passphrase = xstrdup(identity_passphrase);
345 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
346 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
347 memset(old_passphrase, 0, strlen(old_passphrase));
348 xfree(old_passphrase);
349 printf("Bad passphrase.\n");
352 memset(old_passphrase, 0, strlen(old_passphrase));
353 xfree(old_passphrase);
355 printf("Key has comment '%s'\n", comment);
357 /* Ask the new passphrase (twice). */
358 if (identity_new_passphrase) {
359 passphrase1 = xstrdup(identity_new_passphrase);
363 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
364 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
366 /* Verify that they are the same. */
367 if (strcmp(passphrase1, passphrase2) != 0) {
368 memset(passphrase1, 0, strlen(passphrase1));
369 memset(passphrase2, 0, strlen(passphrase2));
372 printf("Pass phrases do not match. Try again.\n");
375 /* Destroy the other copy. */
376 memset(passphrase2, 0, strlen(passphrase2));
380 /* Save the file using the new passphrase. */
381 if (!save_private_key(identity_file, passphrase1, private, comment)) {
382 printf("Saving the key failed: %s: %s.\n",
383 identity_file, strerror(errno));
384 memset(passphrase1, 0, strlen(passphrase1));
390 /* Destroy the passphrase and the copy of the key in memory. */
391 memset(passphrase1, 0, strlen(passphrase1));
393 key_free(private); /* Destroys contents */
396 printf("Your identification has been saved with the new passphrase.\n");
401 * Change the comment of a private key file.
404 do_change_comment(struct passwd *pw)
406 char new_comment[1024], *comment;
414 ask_filename(pw, "Enter file in which the key is");
415 if (stat(identity_file, &st) < 0) {
416 perror(identity_file);
420 * Try to load the public key from the file the verify that it is
421 * readable and of the proper format.
423 public = key_new(KEY_RSA);
424 if (!load_public_key(identity_file, public, NULL)) {
425 printf("%s is not a valid key file.\n", identity_file);
429 private = key_new(KEY_RSA);
430 if (load_private_key(identity_file, "", private, &comment))
431 passphrase = xstrdup("");
433 if (identity_passphrase)
434 passphrase = xstrdup(identity_passphrase);
435 else if (identity_new_passphrase)
436 passphrase = xstrdup(identity_new_passphrase);
438 passphrase = read_passphrase("Enter passphrase: ", 1);
439 /* Try to load using the passphrase. */
440 if (!load_private_key(identity_file, passphrase, private, &comment)) {
441 memset(passphrase, 0, strlen(passphrase));
443 printf("Bad passphrase.\n");
447 printf("Key now has comment '%s'\n", comment);
449 if (identity_comment) {
450 strlcpy(new_comment, identity_comment, sizeof(new_comment));
452 printf("Enter new comment: ");
454 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
455 memset(passphrase, 0, strlen(passphrase));
459 if (strchr(new_comment, '\n'))
460 *strchr(new_comment, '\n') = 0;
463 /* Save the file using the new passphrase. */
464 if (!save_private_key(identity_file, passphrase, private, new_comment)) {
465 printf("Saving the key failed: %s: %s.\n",
466 identity_file, strerror(errno));
467 memset(passphrase, 0, strlen(passphrase));
473 memset(passphrase, 0, strlen(passphrase));
477 strlcat(identity_file, ".pub", sizeof(identity_file));
478 f = fopen(identity_file, "w");
480 printf("Could not save your public key in %s\n", identity_file);
483 if (!key_write(public, f))
484 fprintf(stderr, "write key failed");
486 fprintf(f, " %s\n", new_comment);
491 printf("The comment in your key file has been changed.\n");
498 printf("Usage: %s [-lpqxXydc] [-b bits] [-f file] [-C comment] [-N new-pass] [-P pass]\n", __progname);
503 * Main program for key management.
506 main(int ac, char **av)
508 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
520 SSLeay_add_all_algorithms();
522 /* we need this for the home * directory. */
523 pw = getpwuid(getuid());
525 printf("You don't exist, go away!\n");
528 if (gethostname(hostname, sizeof(hostname)) < 0) {
529 perror("gethostname");
533 while ((opt = getopt(ac, av, "dqpclRxXyb:f:P:N:C:")) != EOF) {
537 if (bits < 512 || bits > 32768) {
538 printf("Bits has bad value.\n");
544 print_fingerprint = 1;
548 change_passphrase = 1;
556 strlcpy(identity_file, optarg, sizeof(identity_file));
561 identity_passphrase = optarg;
565 identity_new_passphrase = optarg;
569 identity_comment = optarg;
577 if (rsa_alive() == 0)
588 convert_from_ssh2 = 1;
605 printf("Too many arguments.\n");
608 if (change_passphrase && change_comment) {
609 printf("Can only have one of -p and -c.\n");
612 /* check if RSA support is needed and exists */
613 if (dsa_mode == 0 && rsa_alive() == 0) {
615 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
619 if (print_fingerprint)
621 if (change_passphrase)
622 do_change_passphrase(pw);
624 do_change_comment(pw);
626 do_convert_to_ssh2(pw);
627 if (convert_from_ssh2)
628 do_convert_from_ssh2(pw);
636 printf("Generating DSA parameter and key.\n");
637 public = private = dsa_generate_key(bits);
638 if (private == NULL) {
639 fprintf(stderr, "dsa_generate_keys failed");
645 /* Generate the rsa key pair. */
646 public = key_new(KEY_RSA);
647 private = key_new(KEY_RSA);
648 rsa_generate_key(private->rsa, public->rsa, bits);
652 ask_filename(pw, "Enter file in which to save the key");
654 /* Create ~/.ssh directory if it doesn\'t already exist. */
655 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR);
656 if (strstr(identity_file, dotsshdir) != NULL &&
657 stat(dotsshdir, &st) < 0) {
658 if (mkdir(dotsshdir, 0700) < 0)
659 error("Could not create directory '%s'.", dotsshdir);
661 printf("Created directory '%s'.\n", dotsshdir);
663 /* If the file already exists, ask the user to confirm. */
664 if (stat(identity_file, &st) >= 0) {
666 printf("%s already exists.\n", identity_file);
667 printf("Overwrite (y/n)? ");
669 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
671 if (yesno[0] != 'y' && yesno[0] != 'Y')
674 /* Ask for a passphrase (twice). */
675 if (identity_passphrase)
676 passphrase1 = xstrdup(identity_passphrase);
677 else if (identity_new_passphrase)
678 passphrase1 = xstrdup(identity_new_passphrase);
682 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
683 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
684 if (strcmp(passphrase1, passphrase2) != 0) {
685 /* The passphrases do not match. Clear them and retry. */
686 memset(passphrase1, 0, strlen(passphrase1));
687 memset(passphrase2, 0, strlen(passphrase2));
690 printf("Passphrases do not match. Try again.\n");
691 goto passphrase_again;
693 /* Clear the other copy of the passphrase. */
694 memset(passphrase2, 0, strlen(passphrase2));
698 if (identity_comment) {
699 strlcpy(comment, identity_comment, sizeof(comment));
701 /* Create default commend field for the passphrase. */
702 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
705 /* Save the key with the given passphrase and comment. */
706 if (!save_private_key(identity_file, passphrase1, private, comment)) {
707 printf("Saving the key failed: %s: %s.\n",
708 identity_file, strerror(errno));
709 memset(passphrase1, 0, strlen(passphrase1));
713 /* Clear the passphrase. */
714 memset(passphrase1, 0, strlen(passphrase1));
717 /* Clear the private key and the random number generator. */
718 if (private != public) {
724 printf("Your identification has been saved in %s.\n", identity_file);
726 strlcat(identity_file, ".pub", sizeof(identity_file));
727 f = fopen(identity_file, "w");
729 printf("Could not save your public key in %s\n", identity_file);
732 if (!key_write(public, f))
733 fprintf(stderr, "write key failed");
734 fprintf(f, " %s\n", comment);
738 printf("Your public key has been saved in %s.\n",
740 printf("The key fingerprint is:\n");
741 printf("%s %s\n", key_fingerprint(public), comment);