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.48 2001/03/11 16:39:03 deraadt Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
26 #include "pathnames.h"
30 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
34 * Flag indicating that we just want to change the passphrase. This can be
35 * set on the command line.
37 int change_passphrase = 0;
40 * Flag indicating that we just want to change the comment. This can be set
41 * on the command line.
43 int change_comment = 0;
47 /* Flag indicating that we just want to see the key fingerprint */
48 int print_fingerprint = 0;
50 /* The identity file name, given on the command line or entered by the user. */
51 char identity_file[1024];
52 int have_identity = 0;
54 /* This is set to the passphrase if given on the command line. */
55 char *identity_passphrase = NULL;
57 /* This is set to the new passphrase if given on the command line. */
58 char *identity_new_passphrase = NULL;
60 /* This is set to the new comment if given on the command line. */
61 char *identity_comment = NULL;
63 /* Dump public key file in format used by real and the original SSH 2 */
64 int convert_to_ssh2 = 0;
65 int convert_from_ssh2 = 0;
67 int print_verbose = 0;
69 /* default to RSA for SSH-1 */
70 char *key_type_name = "rsa1";
73 #ifdef HAVE___PROGNAME
74 extern char *__progname;
79 char hostname[MAXHOSTNAMELEN];
82 ask_filename(struct passwd *pw, const char *prompt)
87 switch (key_type_from_name(key_type_name)) {
89 name = _PATH_SSH_CLIENT_IDENTITY;
92 name = _PATH_SSH_CLIENT_ID_DSA;
95 name = _PATH_SSH_CLIENT_ID_RSA;
98 fprintf(stderr, "bad key type");
102 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
103 fprintf(stderr, "%s (%s): ", prompt, identity_file);
105 if (fgets(buf, sizeof(buf), stdin) == NULL)
107 if (strchr(buf, '\n'))
108 *strchr(buf, '\n') = 0;
109 if (strcmp(buf, "") != 0)
110 strlcpy(identity_file, buf, sizeof(identity_file));
115 try_load_key(char *filename, Key *k)
118 if (!load_private_key(filename, "", k, NULL)) {
119 char *pass = read_passphrase("Enter passphrase: ", 1);
120 if (!load_private_key(filename, pass, k, NULL)) {
123 memset(pass, 0, strlen(pass));
129 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
130 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
131 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
132 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
135 do_convert_to_ssh2(struct passwd *pw)
143 ask_filename(pw, "Enter file in which the key is");
144 if (stat(identity_file, &st) < 0) {
145 perror(identity_file);
148 k = key_new(KEY_UNSPEC);
149 if (!try_load_key(identity_file, k)) {
150 fprintf(stderr, "load failed\n");
153 key_to_blob(k, &blob, &len);
154 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
156 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
157 key_size(k), key_type(k),
158 pw->pw_name, hostname);
159 dump_base64(stdout, blob, len);
160 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
167 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
169 int bits = buffer_get_int(b);
170 int bytes = (bits + 7) / 8;
171 if (buffer_len(b) < bytes)
172 fatal("buffer_get_bignum_bits: input buffer too small");
173 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
174 buffer_consume(b, bytes);
178 do_convert_private_ssh2_from_blob(char *blob, int blen)
183 int ignore, magic, rlen;
187 buffer_append(&b, blob, blen);
189 magic = buffer_get_int(&b);
190 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
191 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
195 ignore = buffer_get_int(&b);
196 type = buffer_get_string(&b, NULL);
197 cipher = buffer_get_string(&b, NULL);
198 ignore = buffer_get_int(&b);
199 ignore = buffer_get_int(&b);
200 ignore = buffer_get_int(&b);
203 if (strcmp(cipher, "none") != 0) {
204 error("unsupported cipher %s", cipher);
211 key = key_new(KEY_DSA);
213 dsa->priv_key = BN_new();
214 if (dsa->priv_key == NULL) {
215 error("alloc priv_key failed");
219 buffer_get_bignum_bits(&b, dsa->p);
220 buffer_get_bignum_bits(&b, dsa->g);
221 buffer_get_bignum_bits(&b, dsa->q);
222 buffer_get_bignum_bits(&b, dsa->pub_key);
223 buffer_get_bignum_bits(&b, dsa->priv_key);
224 rlen = buffer_len(&b);
226 error("do_convert_private_ssh2_from_blob: remaining bytes in key blob %d", rlen);
232 do_convert_from_ssh2(struct passwd *pw)
240 int escaped = 0, private = 0, ok;
244 ask_filename(pw, "Enter file in which the key is");
245 if (stat(identity_file, &st) < 0) {
246 perror(identity_file);
249 fp = fopen(identity_file, "r");
251 perror(identity_file);
255 while (fgets(line, sizeof(line), fp)) {
256 if (!(p = strchr(line, '\n'))) {
257 fprintf(stderr, "input line too long.\n");
260 if (p > line && p[-1] == '\\')
262 if (strncmp(line, "----", 4) == 0 ||
263 strstr(line, ": ") != NULL) {
264 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
266 fprintf(stderr, "ignore: %s", line);
271 fprintf(stderr, "escaped: %s", line);
275 strlcat(encoded, line, sizeof(encoded));
277 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
279 fprintf(stderr, "uudecode failed.\n");
283 do_convert_private_ssh2_from_blob(blob, blen) :
284 key_from_blob(blob, blen);
286 fprintf(stderr, "decode blob failed.\n");
290 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
291 key_write(k, stdout);
293 fprintf(stderr, "key write failed");
297 fprintf(stdout, "\n");
303 do_print_public(struct passwd *pw)
309 ask_filename(pw, "Enter file in which the key is");
310 if (stat(identity_file, &st) < 0) {
311 perror(identity_file);
314 k = key_new(KEY_UNSPEC);
315 if (!try_load_key(identity_file, k)) {
316 fprintf(stderr, "load failed\n");
319 if (!key_write(k, stdout))
320 fprintf(stderr, "key_write failed");
322 fprintf(stdout, "\n");
327 do_fingerprint(struct passwd *pw)
332 char *comment = NULL, *cp, *ep, line[16*1024];
333 int i, skip = 0, num = 1, invalid = 1, success = 0;
337 ask_filename(pw, "Enter file in which the key is");
338 if (stat(identity_file, &st) < 0) {
339 perror(identity_file);
342 public = key_new(KEY_RSA1);
343 if (load_public_key(identity_file, public, &comment)) {
347 public = key_new(KEY_UNSPEC);
348 if (try_load_public_key(identity_file, public, &comment))
351 debug("try_load_public_key KEY_UNSPEC failed");
354 char *digest_md5, *digest_bubblebabble;
356 digest_md5 = key_fingerprint_ex(public, SSH_FP_MD5, SSH_FP_HEX);
357 digest_bubblebabble = key_fingerprint_ex(public, SSH_FP_SHA1,
358 SSH_FP_BUBBLEBABBLE);
361 printf("comment: %s\n", comment);
362 printf("size: %d\n", key_size(public));
363 printf("md5: %s\n", digest_md5);
364 printf("bubblebabble: %s\n", digest_bubblebabble);
366 printf("%d %s %s\n", key_size(public), digest_md5, comment);
372 xfree(digest_bubblebabble);
377 f = fopen(identity_file, "r");
379 while (fgets(line, sizeof(line), f)) {
380 i = strlen(line) - 1;
381 if (line[i] != '\n') {
382 error("line %d too long: %.40s...", num, line);
393 /* Skip leading whitespace, empty and comment lines. */
394 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
396 if (!*cp || *cp == '\n' || *cp == '#')
398 i = strtol(cp, &ep, 10);
399 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
402 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
403 if (*cp == '\\' && cp[1] == '"')
404 cp++; /* Skip both */
413 public = key_new(KEY_RSA1);
414 if (key_read(public, &cp) != 1) {
417 public = key_new(KEY_UNSPEC);
418 if (key_read(public, &cp) != 1) {
423 comment = *cp ? cp : comment;
424 printf("%d %s %s\n", key_size(public),
425 key_fingerprint(public),
426 comment ? comment : "no comment");
433 printf("%s is not a valid key file.\n", identity_file);
440 * Perform changing a passphrase. The argument is the passwd structure
441 * for the current user.
444 do_change_passphrase(struct passwd *pw)
447 char *old_passphrase, *passphrase1, *passphrase2;
454 ask_filename(pw, "Enter file in which the key is");
455 if (stat(identity_file, &st) < 0) {
456 perror(identity_file);
459 public = key_new(type);
460 if (!load_public_key(identity_file, public, NULL)) {
463 /* Clear the public key since we are just about to load the whole file. */
466 /* Try to load the file with empty passphrase. */
467 private = key_new(type);
468 if (!load_private_key(identity_file, "", private, &comment)) {
469 if (identity_passphrase)
470 old_passphrase = xstrdup(identity_passphrase);
472 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
473 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
474 memset(old_passphrase, 0, strlen(old_passphrase));
475 xfree(old_passphrase);
476 printf("Bad passphrase.\n");
479 memset(old_passphrase, 0, strlen(old_passphrase));
480 xfree(old_passphrase);
482 printf("Key has comment '%s'\n", comment);
484 /* Ask the new passphrase (twice). */
485 if (identity_new_passphrase) {
486 passphrase1 = xstrdup(identity_new_passphrase);
490 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
491 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
493 /* Verify that they are the same. */
494 if (strcmp(passphrase1, passphrase2) != 0) {
495 memset(passphrase1, 0, strlen(passphrase1));
496 memset(passphrase2, 0, strlen(passphrase2));
499 printf("Pass phrases do not match. Try again.\n");
502 /* Destroy the other copy. */
503 memset(passphrase2, 0, strlen(passphrase2));
507 /* Save the file using the new passphrase. */
508 if (!save_private_key(identity_file, passphrase1, private, comment)) {
509 printf("Saving the key failed: %s: %s.\n",
510 identity_file, strerror(errno));
511 memset(passphrase1, 0, strlen(passphrase1));
517 /* Destroy the passphrase and the copy of the key in memory. */
518 memset(passphrase1, 0, strlen(passphrase1));
520 key_free(private); /* Destroys contents */
523 printf("Your identification has been saved with the new passphrase.\n");
528 * Change the comment of a private key file.
531 do_change_comment(struct passwd *pw)
533 char new_comment[1024], *comment, *passphrase;
534 Key *private, *public;
540 ask_filename(pw, "Enter file in which the key is");
541 if (stat(identity_file, &st) < 0) {
542 perror(identity_file);
546 * Try to load the public key from the file the verify that it is
547 * readable and of the proper format.
549 public = key_new(KEY_RSA1);
550 if (!load_public_key(identity_file, public, NULL)) {
551 printf("%s is not a valid key file.\n", identity_file);
552 printf("Comments are only supported in RSA1 keys\n");
556 private = key_new(KEY_RSA1);
557 if (load_private_key(identity_file, "", private, &comment))
558 passphrase = xstrdup("");
560 if (identity_passphrase)
561 passphrase = xstrdup(identity_passphrase);
562 else if (identity_new_passphrase)
563 passphrase = xstrdup(identity_new_passphrase);
565 passphrase = read_passphrase("Enter passphrase: ", 1);
566 /* Try to load using the passphrase. */
567 if (!load_private_key(identity_file, passphrase, private, &comment)) {
568 memset(passphrase, 0, strlen(passphrase));
570 printf("Bad passphrase.\n");
574 printf("Key now has comment '%s'\n", comment);
576 if (identity_comment) {
577 strlcpy(new_comment, identity_comment, sizeof(new_comment));
579 printf("Enter new comment: ");
581 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
582 memset(passphrase, 0, strlen(passphrase));
586 if (strchr(new_comment, '\n'))
587 *strchr(new_comment, '\n') = 0;
590 /* Save the file using the new passphrase. */
591 if (!save_private_key(identity_file, passphrase, private, new_comment)) {
592 printf("Saving the key failed: %s: %s.\n",
593 identity_file, strerror(errno));
594 memset(passphrase, 0, strlen(passphrase));
600 memset(passphrase, 0, strlen(passphrase));
604 strlcat(identity_file, ".pub", sizeof(identity_file));
605 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
607 printf("Could not save your public key in %s\n", identity_file);
612 printf("fdopen %s failed", identity_file);
615 if (!key_write(public, f))
616 fprintf(stderr, "write key failed");
618 fprintf(f, " %s\n", new_comment);
623 printf("The comment in your key file has been changed.\n");
630 printf("Usage: %s [-lpqxXyc] [-t type] [-b bits] [-f file] [-C comment] "
631 "[-N new-pass] [-P pass]\n", __progname);
636 * Main program for key management.
639 main(int ac, char **av)
641 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
642 Key *private, *public;
651 __progname = get_progname(av[0]);
654 SSLeay_add_all_algorithms();
656 /* we need this for the home * directory. */
657 pw = getpwuid(getuid());
659 printf("You don't exist, go away!\n");
662 if (gethostname(hostname, sizeof(hostname)) < 0) {
663 perror("gethostname");
667 while ((opt = getopt(ac, av, "dqpclRxXyvb:f:t:P:N:C:")) != -1) {
671 if (bits < 512 || bits > 32768) {
672 printf("Bits has bad value.\n");
678 print_fingerprint = 1;
682 change_passphrase = 1;
690 strlcpy(identity_file, optarg, sizeof(identity_file));
695 identity_passphrase = optarg;
699 identity_new_passphrase = optarg;
703 identity_comment = optarg;
720 convert_from_ssh2 = 1;
732 key_type_name = "dsa";
736 key_type_name = optarg;
745 printf("Too many arguments.\n");
748 if (change_passphrase && change_comment) {
749 printf("Can only have one of -p and -c.\n");
752 if (print_fingerprint)
754 if (change_passphrase)
755 do_change_passphrase(pw);
757 do_change_comment(pw);
759 do_convert_to_ssh2(pw);
760 if (convert_from_ssh2)
761 do_convert_from_ssh2(pw);
767 type = key_type_from_name(key_type_name);
768 if (type == KEY_UNSPEC) {
769 fprintf(stderr, "unknown key type %s\n", key_type_name);
773 printf("Generating public/private %s key pair.\n", key_type_name);
774 private = key_generate(type, bits);
775 if (private == NULL) {
776 fprintf(stderr, "key_generate failed");
779 public = key_from_private(private);
782 ask_filename(pw, "Enter file in which to save the key");
784 /* Create ~/.ssh directory if it doesn\'t already exist. */
785 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
786 if (strstr(identity_file, dotsshdir) != NULL &&
787 stat(dotsshdir, &st) < 0) {
788 if (mkdir(dotsshdir, 0700) < 0)
789 error("Could not create directory '%s'.", dotsshdir);
791 printf("Created directory '%s'.\n", dotsshdir);
793 /* If the file already exists, ask the user to confirm. */
794 if (stat(identity_file, &st) >= 0) {
796 printf("%s already exists.\n", identity_file);
797 printf("Overwrite (y/n)? ");
799 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
801 if (yesno[0] != 'y' && yesno[0] != 'Y')
804 /* Ask for a passphrase (twice). */
805 if (identity_passphrase)
806 passphrase1 = xstrdup(identity_passphrase);
807 else if (identity_new_passphrase)
808 passphrase1 = xstrdup(identity_new_passphrase);
812 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
813 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
814 if (strcmp(passphrase1, passphrase2) != 0) {
815 /* The passphrases do not match. Clear them and retry. */
816 memset(passphrase1, 0, strlen(passphrase1));
817 memset(passphrase2, 0, strlen(passphrase2));
820 printf("Passphrases do not match. Try again.\n");
821 goto passphrase_again;
823 /* Clear the other copy of the passphrase. */
824 memset(passphrase2, 0, strlen(passphrase2));
828 if (identity_comment) {
829 strlcpy(comment, identity_comment, sizeof(comment));
831 /* Create default commend field for the passphrase. */
832 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
835 /* Save the key with the given passphrase and comment. */
836 if (!save_private_key(identity_file, passphrase1, private, comment)) {
837 printf("Saving the key failed: %s: %s.\n",
838 identity_file, strerror(errno));
839 memset(passphrase1, 0, strlen(passphrase1));
843 /* Clear the passphrase. */
844 memset(passphrase1, 0, strlen(passphrase1));
847 /* Clear the private key and the random number generator. */
852 printf("Your identification has been saved in %s.\n", identity_file);
854 strlcat(identity_file, ".pub", sizeof(identity_file));
855 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
857 printf("Could not save your public key in %s\n", identity_file);
862 printf("fdopen %s failed", identity_file);
865 if (!key_write(public, f))
866 fprintf(stderr, "write key failed");
867 fprintf(f, " %s\n", comment);
871 printf("Your public key has been saved in %s.\n",
873 printf("The key fingerprint is:\n");
874 printf("%s %s\n", key_fingerprint(public), comment);