2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Identity and host key generation and maintenance.
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".
15 RCSID("$OpenBSD: ssh-keygen.c,v 1.34 2000/11/15 20:24:43 millert Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
19 #include <openssl/rsa.h>
20 #include <openssl/dsa.h>
32 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
36 * Flag indicating that we just want to change the passphrase. This can be
37 * set on the command line.
39 int change_passphrase = 0;
42 * Flag indicating that we just want to change the comment. This can be set
43 * on the command line.
45 int change_comment = 0;
49 /* Flag indicating that we just want to see the key fingerprint */
50 int print_fingerprint = 0;
52 /* The identity file name, given on the command line or entered by the user. */
53 char identity_file[1024];
54 int have_identity = 0;
56 /* This is set to the passphrase if given on the command line. */
57 char *identity_passphrase = NULL;
59 /* This is set to the new passphrase if given on the command line. */
60 char *identity_new_passphrase = NULL;
62 /* This is set to the new comment if given on the command line. */
63 char *identity_comment = NULL;
65 /* Dump public key file in format used by real and the original SSH 2 */
66 int convert_to_ssh2 = 0;
67 int convert_from_ssh2 = 0;
71 int dsa_mode = 0; /* compat */
72 char *key_type_name = NULL;
75 #ifdef HAVE___PROGNAME
76 extern char *__progname;
81 char hostname[MAXHOSTNAMELEN];
84 ask_filename(struct passwd *pw, const char *prompt)
87 snprintf(identity_file, sizeof(identity_file), "%s/%s",
89 dsa_mode ? SSH_CLIENT_ID_DSA: SSH_CLIENT_IDENTITY);
90 printf("%s (%s): ", prompt, identity_file);
92 if (fgets(buf, sizeof(buf), stdin) == NULL)
94 if (strchr(buf, '\n'))
95 *strchr(buf, '\n') = 0;
96 if (strcmp(buf, "") != 0)
97 strlcpy(identity_file, buf, sizeof(identity_file));
102 try_load_key(char *filename, Key *k)
105 if (!load_private_key(filename, "", k, NULL)) {
106 char *pass = read_passphrase("Enter passphrase: ", 1);
107 if (!load_private_key(filename, pass, k, NULL)) {
110 memset(pass, 0, strlen(pass));
116 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
117 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
118 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
119 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
122 do_convert_to_ssh2(struct passwd *pw)
130 ask_filename(pw, "Enter file in which the key is");
131 if (stat(identity_file, &st) < 0) {
132 perror(identity_file);
135 k = key_new(KEY_UNSPEC);
136 if (!try_load_key(identity_file, k)) {
137 fprintf(stderr, "load failed\n");
140 key_to_blob(k, &blob, &len);
141 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
143 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
144 key_size(k), key_type(k),
145 pw->pw_name, hostname);
146 dump_base64(stdout, blob, len);
147 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
154 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
156 int bits = buffer_get_int(b);
157 int bytes = (bits + 7) / 8;
158 if (buffer_len(b) < bytes)
159 fatal("buffer_get_bignum_bits: input buffer too small");
160 BN_bin2bn((unsigned char *)buffer_ptr(b), bytes, value);
161 buffer_consume(b, bytes);
165 do_convert_private_ssh2_from_blob(char *blob, int blen)
170 int ignore, magic, rlen;
174 buffer_append(&b, blob, blen);
176 magic = buffer_get_int(&b);
177 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
178 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
182 ignore = buffer_get_int(&b);
183 type = buffer_get_string(&b, NULL);
184 cipher = buffer_get_string(&b, NULL);
185 ignore = buffer_get_int(&b);
186 ignore = buffer_get_int(&b);
187 ignore = buffer_get_int(&b);
190 if (strcmp(cipher, "none") != 0) {
191 error("unsupported cipher %s", cipher);
198 key = key_new(KEY_DSA);
200 dsa->priv_key = BN_new();
201 if (dsa->priv_key == NULL) {
202 error("alloc priv_key failed");
206 buffer_get_bignum_bits(&b, dsa->p);
207 buffer_get_bignum_bits(&b, dsa->g);
208 buffer_get_bignum_bits(&b, dsa->q);
209 buffer_get_bignum_bits(&b, dsa->pub_key);
210 buffer_get_bignum_bits(&b, dsa->priv_key);
211 rlen = buffer_len(&b);
213 error("do_convert_private_ssh2_from_blob: remaining bytes in key blob %d", rlen);
219 do_convert_from_ssh2(struct passwd *pw)
227 int escaped = 0, private = 0, ok;
231 ask_filename(pw, "Enter file in which the key is");
232 if (stat(identity_file, &st) < 0) {
233 perror(identity_file);
236 fp = fopen(identity_file, "r");
238 perror(identity_file);
242 while (fgets(line, sizeof(line), fp)) {
243 if (!(p = strchr(line, '\n'))) {
244 fprintf(stderr, "input line too long.\n");
247 if (p > line && p[-1] == '\\')
249 if (strncmp(line, "----", 4) == 0 ||
250 strstr(line, ": ") != NULL) {
251 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
253 fprintf(stderr, "ignore: %s", line);
258 fprintf(stderr, "escaped: %s", line);
262 strlcat(encoded, line, sizeof(encoded));
264 blen = uudecode(encoded, (unsigned char *)blob, sizeof(blob));
266 fprintf(stderr, "uudecode failed.\n");
270 do_convert_private_ssh2_from_blob(blob, blen) :
271 key_from_blob(blob, blen);
273 fprintf(stderr, "decode blob failed.\n");
277 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
278 key_write(k, stdout);
280 fprintf(stderr, "key write failed");
284 fprintf(stdout, "\n");
290 do_print_public(struct passwd *pw)
296 ask_filename(pw, "Enter file in which the key is");
297 if (stat(identity_file, &st) < 0) {
298 perror(identity_file);
301 k = key_new(KEY_UNSPEC);
302 if (!try_load_key(identity_file, k)) {
303 fprintf(stderr, "load failed\n");
306 if (!key_write(k, stdout))
307 fprintf(stderr, "key_write failed");
309 fprintf(stdout, "\n");
314 do_fingerprint(struct passwd *pw)
319 char *comment = NULL, *cp, *ep, line[16*1024];
320 int i, skip = 0, num = 1, invalid = 1, success = 0;
325 ask_filename(pw, "Enter file in which the key is");
326 if (stat(identity_file, &st) < 0) {
327 perror(identity_file);
330 public = key_new(KEY_RSA1);
331 if (load_public_key(identity_file, public, &comment)) {
335 public = key_new(KEY_UNSPEC);
336 if (try_load_public_key(identity_file, public, &comment))
339 error("try_load_public_key KEY_UNSPEC failed");
342 printf("%d %s %s\n", key_size(public), key_fingerprint(public), comment);
350 public = key_new(KEY_RSA1);
351 f = fopen(identity_file, "r");
353 while (fgets(line, sizeof(line), f)) {
354 i = strlen(line) - 1;
355 if (line[i] != '\n') {
356 error("line %d too long: %.40s...", num, line);
367 /* Skip leading whitespace, empty and comment lines. */
368 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
370 if (!*cp || *cp == '\n' || *cp == '#')
372 i = strtol(cp, &ep, 10);
373 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
376 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
377 if (*cp == '\\' && cp[1] == '"')
378 cp++; /* Skip both */
387 if (auth_rsa_read_key(&cp, &ignore, public->rsa->e, public->rsa->n)) {
389 comment = *cp ? cp : comment;
390 printf("%d %s %s\n", key_size(public),
391 key_fingerprint(public),
392 comment ? comment : "no comment");
399 printf("%s is not a valid key file.\n", identity_file);
406 * Perform changing a passphrase. The argument is the passwd structure
407 * for the current user.
410 do_change_passphrase(struct passwd *pw)
413 char *old_passphrase, *passphrase1, *passphrase2;
420 ask_filename(pw, "Enter file in which the key is");
421 if (stat(identity_file, &st) < 0) {
422 perror(identity_file);
425 public = key_new(type);
426 if (!load_public_key(identity_file, public, NULL)) {
429 /* Clear the public key since we are just about to load the whole file. */
432 /* Try to load the file with empty passphrase. */
433 private = key_new(type);
434 if (!load_private_key(identity_file, "", private, &comment)) {
435 if (identity_passphrase)
436 old_passphrase = xstrdup(identity_passphrase);
438 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
439 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
440 memset(old_passphrase, 0, strlen(old_passphrase));
441 xfree(old_passphrase);
442 printf("Bad passphrase.\n");
445 memset(old_passphrase, 0, strlen(old_passphrase));
446 xfree(old_passphrase);
448 printf("Key has comment '%s'\n", comment);
450 /* Ask the new passphrase (twice). */
451 if (identity_new_passphrase) {
452 passphrase1 = xstrdup(identity_new_passphrase);
456 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
457 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
459 /* Verify that they are the same. */
460 if (strcmp(passphrase1, passphrase2) != 0) {
461 memset(passphrase1, 0, strlen(passphrase1));
462 memset(passphrase2, 0, strlen(passphrase2));
465 printf("Pass phrases do not match. Try again.\n");
468 /* Destroy the other copy. */
469 memset(passphrase2, 0, strlen(passphrase2));
473 /* Save the file using the new passphrase. */
474 if (!save_private_key(identity_file, passphrase1, private, comment)) {
475 printf("Saving the key failed: %s: %s.\n",
476 identity_file, strerror(errno));
477 memset(passphrase1, 0, strlen(passphrase1));
483 /* Destroy the passphrase and the copy of the key in memory. */
484 memset(passphrase1, 0, strlen(passphrase1));
486 key_free(private); /* Destroys contents */
489 printf("Your identification has been saved with the new passphrase.\n");
494 * Change the comment of a private key file.
497 do_change_comment(struct passwd *pw)
499 char new_comment[1024], *comment;
507 ask_filename(pw, "Enter file in which the key is");
508 if (stat(identity_file, &st) < 0) {
509 perror(identity_file);
513 * Try to load the public key from the file the verify that it is
514 * readable and of the proper format.
516 public = key_new(KEY_RSA1);
517 if (!load_public_key(identity_file, public, NULL)) {
518 printf("%s is not a valid key file.\n", identity_file);
522 private = key_new(KEY_RSA1);
523 if (load_private_key(identity_file, "", private, &comment))
524 passphrase = xstrdup("");
526 if (identity_passphrase)
527 passphrase = xstrdup(identity_passphrase);
528 else if (identity_new_passphrase)
529 passphrase = xstrdup(identity_new_passphrase);
531 passphrase = read_passphrase("Enter passphrase: ", 1);
532 /* Try to load using the passphrase. */
533 if (!load_private_key(identity_file, passphrase, private, &comment)) {
534 memset(passphrase, 0, strlen(passphrase));
536 printf("Bad passphrase.\n");
540 printf("Key now has comment '%s'\n", comment);
542 if (identity_comment) {
543 strlcpy(new_comment, identity_comment, sizeof(new_comment));
545 printf("Enter new comment: ");
547 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
548 memset(passphrase, 0, strlen(passphrase));
552 if (strchr(new_comment, '\n'))
553 *strchr(new_comment, '\n') = 0;
556 /* Save the file using the new passphrase. */
557 if (!save_private_key(identity_file, passphrase, private, new_comment)) {
558 printf("Saving the key failed: %s: %s.\n",
559 identity_file, strerror(errno));
560 memset(passphrase, 0, strlen(passphrase));
566 memset(passphrase, 0, strlen(passphrase));
570 strlcat(identity_file, ".pub", sizeof(identity_file));
571 f = fopen(identity_file, "w");
573 printf("Could not save your public key in %s\n", identity_file);
576 if (!key_write(public, f))
577 fprintf(stderr, "write key failed");
579 fprintf(f, " %s\n", new_comment);
584 printf("The comment in your key file has been changed.\n");
591 printf("Usage: %s [-lpqxXyc] [-t type] [-b bits] [-f file] [-C comment] [-N new-pass] [-P pass]\n", __progname);
596 * Main program for key management.
599 main(int ac, char **av)
601 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
613 __progname = get_progname(av[0]);
616 SSLeay_add_all_algorithms();
618 /* we need this for the home * directory. */
619 pw = getpwuid(getuid());
621 printf("You don't exist, go away!\n");
624 if (gethostname(hostname, sizeof(hostname)) < 0) {
625 perror("gethostname");
629 while ((opt = getopt(ac, av, "dqpclRxXyb:f:t:P:N:C:")) != EOF) {
633 if (bits < 512 || bits > 32768) {
634 printf("Bits has bad value.\n");
640 print_fingerprint = 1;
644 change_passphrase = 1;
652 strlcpy(identity_file, optarg, sizeof(identity_file));
657 identity_passphrase = optarg;
661 identity_new_passphrase = optarg;
665 identity_comment = optarg;
682 convert_from_ssh2 = 1;
690 key_type_name = "dsa";
695 key_type_name = optarg;
696 dsa_mode = (strcmp(optarg, "dsa") == 0);
705 printf("Too many arguments.\n");
708 if (change_passphrase && change_comment) {
709 printf("Can only have one of -p and -c.\n");
712 if (print_fingerprint)
714 if (change_passphrase)
715 do_change_passphrase(pw);
717 do_change_comment(pw);
719 do_convert_to_ssh2(pw);
720 if (convert_from_ssh2)
721 do_convert_from_ssh2(pw);
727 if (key_type_name != NULL) {
728 type = key_type_from_name(key_type_name);
729 if (type == KEY_UNSPEC) {
730 fprintf(stderr, "unknown key type %s\n", key_type_name);
735 printf("Generating public/private key pair.\n");
736 private = key_generate(type, bits);
737 if (private == NULL) {
738 fprintf(stderr, "key_generate failed");
741 public = key_from_private(private);
744 ask_filename(pw, "Enter file in which to save the key");
746 /* Create ~/.ssh directory if it doesn\'t already exist. */
747 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR);
748 if (strstr(identity_file, dotsshdir) != NULL &&
749 stat(dotsshdir, &st) < 0) {
750 if (mkdir(dotsshdir, 0700) < 0)
751 error("Could not create directory '%s'.", dotsshdir);
753 printf("Created directory '%s'.\n", dotsshdir);
755 /* If the file already exists, ask the user to confirm. */
756 if (stat(identity_file, &st) >= 0) {
758 printf("%s already exists.\n", identity_file);
759 printf("Overwrite (y/n)? ");
761 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
763 if (yesno[0] != 'y' && yesno[0] != 'Y')
766 /* Ask for a passphrase (twice). */
767 if (identity_passphrase)
768 passphrase1 = xstrdup(identity_passphrase);
769 else if (identity_new_passphrase)
770 passphrase1 = xstrdup(identity_new_passphrase);
774 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
775 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
776 if (strcmp(passphrase1, passphrase2) != 0) {
777 /* The passphrases do not match. Clear them and retry. */
778 memset(passphrase1, 0, strlen(passphrase1));
779 memset(passphrase2, 0, strlen(passphrase2));
782 printf("Passphrases do not match. Try again.\n");
783 goto passphrase_again;
785 /* Clear the other copy of the passphrase. */
786 memset(passphrase2, 0, strlen(passphrase2));
790 if (identity_comment) {
791 strlcpy(comment, identity_comment, sizeof(comment));
793 /* Create default commend field for the passphrase. */
794 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
797 /* Save the key with the given passphrase and comment. */
798 if (!save_private_key(identity_file, passphrase1, private, comment)) {
799 printf("Saving the key failed: %s: %s.\n",
800 identity_file, strerror(errno));
801 memset(passphrase1, 0, strlen(passphrase1));
805 /* Clear the passphrase. */
806 memset(passphrase1, 0, strlen(passphrase1));
809 /* Clear the private key and the random number generator. */
814 printf("Your identification has been saved in %s.\n", identity_file);
816 strlcat(identity_file, ".pub", sizeof(identity_file));
817 f = fopen(identity_file, "w");
819 printf("Could not save your public key in %s\n", identity_file);
822 if (!key_write(public, f))
823 fprintf(stderr, "write key failed");
824 fprintf(f, " %s\n", comment);
828 printf("Your public key has been saved in %s.\n",
830 printf("The key fingerprint is:\n");
831 printf("%s %s\n", key_fingerprint(public), comment);