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.106 2003/05/15 03:10:52 djm Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
27 #include "pathnames.h"
38 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
42 * Flag indicating that we just want to change the passphrase. This can be
43 * set on the command line.
45 int change_passphrase = 0;
48 * Flag indicating that we just want to change the comment. This can be set
49 * on the command line.
51 int change_comment = 0;
55 /* Flag indicating that we just want to see the key fingerprint */
56 int print_fingerprint = 0;
57 int print_bubblebabble = 0;
59 /* The identity file name, given on the command line or entered by the user. */
60 char identity_file[1024];
61 int have_identity = 0;
63 /* This is set to the passphrase if given on the command line. */
64 char *identity_passphrase = NULL;
66 /* This is set to the new passphrase if given on the command line. */
67 char *identity_new_passphrase = NULL;
69 /* This is set to the new comment if given on the command line. */
70 char *identity_comment = NULL;
72 /* Dump public key file in format used by real and the original SSH 2 */
73 int convert_to_ssh2 = 0;
74 int convert_from_ssh2 = 0;
76 int print_generic = 0;
78 char *key_type_name = NULL;
81 #ifdef HAVE___PROGNAME
82 extern char *__progname;
87 char hostname[MAXHOSTNAMELEN];
90 ask_filename(struct passwd *pw, const char *prompt)
95 if (key_type_name == NULL)
96 name = _PATH_SSH_CLIENT_ID_RSA;
98 switch (key_type_from_name(key_type_name)) {
100 name = _PATH_SSH_CLIENT_IDENTITY;
103 name = _PATH_SSH_CLIENT_ID_DSA;
106 name = _PATH_SSH_CLIENT_ID_RSA;
109 fprintf(stderr, "bad key type");
114 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
115 fprintf(stderr, "%s (%s): ", prompt, identity_file);
116 if (fgets(buf, sizeof(buf), stdin) == NULL)
118 if (strchr(buf, '\n'))
119 *strchr(buf, '\n') = 0;
120 if (strcmp(buf, "") != 0)
121 strlcpy(identity_file, buf, sizeof(identity_file));
126 load_identity(char *filename)
131 prv = key_load_private(filename, "", NULL);
133 if (identity_passphrase)
134 pass = xstrdup(identity_passphrase);
136 pass = read_passphrase("Enter passphrase: ",
138 prv = key_load_private(filename, pass, NULL);
139 memset(pass, 0, strlen(pass));
145 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
146 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
147 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
148 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
151 do_convert_to_ssh2(struct passwd *pw)
159 ask_filename(pw, "Enter file in which the key is");
160 if (stat(identity_file, &st) < 0) {
161 perror(identity_file);
164 if ((k = key_load_public(identity_file, NULL)) == NULL) {
165 if ((k = load_identity(identity_file)) == NULL) {
166 fprintf(stderr, "load failed\n");
170 if (k->type == KEY_RSA1) {
171 fprintf(stderr, "version 1 keys are not supported\n");
174 if (key_to_blob(k, &blob, &len) <= 0) {
175 fprintf(stderr, "key_to_blob failed\n");
178 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
180 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
181 key_size(k), key_type(k),
182 pw->pw_name, hostname);
183 dump_base64(stdout, blob, len);
184 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
191 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
193 int bits = buffer_get_int(b);
194 int bytes = (bits + 7) / 8;
196 if (buffer_len(b) < bytes)
197 fatal("buffer_get_bignum_bits: input buffer too small: "
198 "need %d have %d", bytes, buffer_len(b));
199 BN_bin2bn(buffer_ptr(b), bytes, value);
200 buffer_consume(b, bytes);
204 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
209 u_char *sig, data[] = "abcde12345";
210 int magic, rlen, ktype, i1, i2, i3, i4;
215 buffer_append(&b, blob, blen);
217 magic = buffer_get_int(&b);
218 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
219 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
223 i1 = buffer_get_int(&b);
224 type = buffer_get_string(&b, NULL);
225 cipher = buffer_get_string(&b, NULL);
226 i2 = buffer_get_int(&b);
227 i3 = buffer_get_int(&b);
228 i4 = buffer_get_int(&b);
229 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
230 if (strcmp(cipher, "none") != 0) {
231 error("unsupported cipher %s", cipher);
239 if (strstr(type, "dsa")) {
241 } else if (strstr(type, "rsa")) {
247 key = key_new_private(ktype);
252 buffer_get_bignum_bits(&b, key->dsa->p);
253 buffer_get_bignum_bits(&b, key->dsa->g);
254 buffer_get_bignum_bits(&b, key->dsa->q);
255 buffer_get_bignum_bits(&b, key->dsa->pub_key);
256 buffer_get_bignum_bits(&b, key->dsa->priv_key);
259 e = buffer_get_char(&b);
263 e += buffer_get_char(&b);
266 e += buffer_get_char(&b);
269 if (!BN_set_word(key->rsa->e, e)) {
274 buffer_get_bignum_bits(&b, key->rsa->d);
275 buffer_get_bignum_bits(&b, key->rsa->n);
276 buffer_get_bignum_bits(&b, key->rsa->iqmp);
277 buffer_get_bignum_bits(&b, key->rsa->q);
278 buffer_get_bignum_bits(&b, key->rsa->p);
279 rsa_generate_additional_parameters(key->rsa);
282 rlen = buffer_len(&b);
284 error("do_convert_private_ssh2_from_blob: "
285 "remaining bytes in key blob %d", rlen);
289 key_sign(key, &sig, &slen, data, sizeof(data));
290 key_verify(key, sig, slen, data, sizeof(data));
296 do_convert_from_ssh2(struct passwd *pw)
305 int escaped = 0, private = 0, ok;
309 ask_filename(pw, "Enter file in which the key is");
310 if (stat(identity_file, &st) < 0) {
311 perror(identity_file);
314 fp = fopen(identity_file, "r");
316 perror(identity_file);
320 while (fgets(line, sizeof(line), fp)) {
321 if (!(p = strchr(line, '\n'))) {
322 fprintf(stderr, "input line too long.\n");
325 if (p > line && p[-1] == '\\')
327 if (strncmp(line, "----", 4) == 0 ||
328 strstr(line, ": ") != NULL) {
329 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
331 if (strstr(line, " END ") != NULL) {
334 /* fprintf(stderr, "ignore: %s", line); */
339 /* fprintf(stderr, "escaped: %s", line); */
343 strlcat(encoded, line, sizeof(encoded));
345 len = strlen(encoded);
346 if (((len % 4) == 3) &&
347 (encoded[len-1] == '=') &&
348 (encoded[len-2] == '=') &&
349 (encoded[len-3] == '='))
350 encoded[len-3] = '\0';
351 blen = uudecode(encoded, blob, sizeof(blob));
353 fprintf(stderr, "uudecode failed.\n");
357 do_convert_private_ssh2_from_blob(blob, blen) :
358 key_from_blob(blob, blen);
360 fprintf(stderr, "decode blob failed.\n");
364 (k->type == KEY_DSA ?
365 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
366 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
367 key_write(k, stdout);
369 fprintf(stderr, "key write failed");
374 fprintf(stdout, "\n");
380 do_print_public(struct passwd *pw)
386 ask_filename(pw, "Enter file in which the key is");
387 if (stat(identity_file, &st) < 0) {
388 perror(identity_file);
391 prv = load_identity(identity_file);
393 fprintf(stderr, "load failed\n");
396 if (!key_write(prv, stdout))
397 fprintf(stderr, "key_write failed");
399 fprintf(stdout, "\n");
405 do_upload(struct passwd *pw, const char *sc_reader_id)
412 ask_filename(pw, "Enter file in which the key is");
413 if (stat(identity_file, &st) < 0) {
414 perror(identity_file);
417 prv = load_identity(identity_file);
419 error("load failed");
422 ret = sc_put_key(prv, sc_reader_id);
426 logit("loading key done");
431 do_download(struct passwd *pw, const char *sc_reader_id)
436 keys = sc_get_keys(sc_reader_id, NULL);
438 fatal("cannot read public key from smartcard");
439 for (i = 0; keys[i]; i++) {
440 key_write(keys[i], stdout);
442 fprintf(stdout, "\n");
447 #endif /* SMARTCARD */
450 do_fingerprint(struct passwd *pw)
454 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
455 int i, skip = 0, num = 1, invalid = 1;
460 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
461 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
464 ask_filename(pw, "Enter file in which the key is");
465 if (stat(identity_file, &st) < 0) {
466 perror(identity_file);
469 public = key_load_public(identity_file, &comment);
470 if (public != NULL) {
471 fp = key_fingerprint(public, fptype, rep);
472 printf("%u %s %s\n", key_size(public), fp, comment);
481 f = fopen(identity_file, "r");
483 while (fgets(line, sizeof(line), f)) {
484 i = strlen(line) - 1;
485 if (line[i] != '\n') {
486 error("line %d too long: %.40s...", num, line);
497 /* Skip leading whitespace, empty and comment lines. */
498 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
500 if (!*cp || *cp == '\n' || *cp == '#')
502 i = strtol(cp, &ep, 10);
503 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
506 for (; *cp && (quoted || (*cp != ' ' &&
507 *cp != '\t')); cp++) {
508 if (*cp == '\\' && cp[1] == '"')
509 cp++; /* Skip both */
518 public = key_new(KEY_RSA1);
519 if (key_read(public, &cp) != 1) {
522 public = key_new(KEY_UNSPEC);
523 if (key_read(public, &cp) != 1) {
528 comment = *cp ? cp : comment;
529 fp = key_fingerprint(public, fptype, rep);
530 printf("%u %s %s\n", key_size(public), fp,
531 comment ? comment : "no comment");
539 printf("%s is not a public key file.\n", identity_file);
546 * Perform changing a passphrase. The argument is the passwd structure
547 * for the current user.
550 do_change_passphrase(struct passwd *pw)
553 char *old_passphrase, *passphrase1, *passphrase2;
558 ask_filename(pw, "Enter file in which the key is");
559 if (stat(identity_file, &st) < 0) {
560 perror(identity_file);
563 /* Try to load the file with empty passphrase. */
564 private = key_load_private(identity_file, "", &comment);
565 if (private == NULL) {
566 if (identity_passphrase)
567 old_passphrase = xstrdup(identity_passphrase);
570 read_passphrase("Enter old passphrase: ",
572 private = key_load_private(identity_file, old_passphrase,
574 memset(old_passphrase, 0, strlen(old_passphrase));
575 xfree(old_passphrase);
576 if (private == NULL) {
577 printf("Bad passphrase.\n");
581 printf("Key has comment '%s'\n", comment);
583 /* Ask the new passphrase (twice). */
584 if (identity_new_passphrase) {
585 passphrase1 = xstrdup(identity_new_passphrase);
589 read_passphrase("Enter new passphrase (empty for no "
590 "passphrase): ", RP_ALLOW_STDIN);
591 passphrase2 = read_passphrase("Enter same passphrase again: ",
594 /* Verify that they are the same. */
595 if (strcmp(passphrase1, passphrase2) != 0) {
596 memset(passphrase1, 0, strlen(passphrase1));
597 memset(passphrase2, 0, strlen(passphrase2));
600 printf("Pass phrases do not match. Try again.\n");
603 /* Destroy the other copy. */
604 memset(passphrase2, 0, strlen(passphrase2));
608 /* Save the file using the new passphrase. */
609 if (!key_save_private(private, identity_file, passphrase1, comment)) {
610 printf("Saving the key failed: %s.\n", identity_file);
611 memset(passphrase1, 0, strlen(passphrase1));
617 /* Destroy the passphrase and the copy of the key in memory. */
618 memset(passphrase1, 0, strlen(passphrase1));
620 key_free(private); /* Destroys contents */
623 printf("Your identification has been saved with the new passphrase.\n");
629 * Print the SSHFP RR.
632 do_print_resource_record(struct passwd *pw, char *hostname)
635 char *comment = NULL;
639 ask_filename(pw, "Enter file in which the key is");
640 if (stat(identity_file, &st) < 0) {
641 perror(identity_file);
644 public = key_load_public(identity_file, &comment);
645 if (public != NULL) {
646 export_dns_rr(hostname, public, stdout, print_generic);
654 printf("failed to read v2 public key from %s.\n", identity_file);
660 * Change the comment of a private key file.
663 do_change_comment(struct passwd *pw)
665 char new_comment[1024], *comment, *passphrase;
673 ask_filename(pw, "Enter file in which the key is");
674 if (stat(identity_file, &st) < 0) {
675 perror(identity_file);
678 private = key_load_private(identity_file, "", &comment);
679 if (private == NULL) {
680 if (identity_passphrase)
681 passphrase = xstrdup(identity_passphrase);
682 else if (identity_new_passphrase)
683 passphrase = xstrdup(identity_new_passphrase);
685 passphrase = read_passphrase("Enter passphrase: ",
687 /* Try to load using the passphrase. */
688 private = key_load_private(identity_file, passphrase, &comment);
689 if (private == NULL) {
690 memset(passphrase, 0, strlen(passphrase));
692 printf("Bad passphrase.\n");
696 passphrase = xstrdup("");
698 if (private->type != KEY_RSA1) {
699 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
703 printf("Key now has comment '%s'\n", comment);
705 if (identity_comment) {
706 strlcpy(new_comment, identity_comment, sizeof(new_comment));
708 printf("Enter new comment: ");
710 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
711 memset(passphrase, 0, strlen(passphrase));
715 if (strchr(new_comment, '\n'))
716 *strchr(new_comment, '\n') = 0;
719 /* Save the file using the new passphrase. */
720 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
721 printf("Saving the key failed: %s.\n", identity_file);
722 memset(passphrase, 0, strlen(passphrase));
728 memset(passphrase, 0, strlen(passphrase));
730 public = key_from_private(private);
733 strlcat(identity_file, ".pub", sizeof(identity_file));
734 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
736 printf("Could not save your public key in %s\n", identity_file);
741 printf("fdopen %s failed", identity_file);
744 if (!key_write(public, f))
745 fprintf(stderr, "write key failed");
747 fprintf(f, " %s\n", new_comment);
752 printf("The comment in your key file has been changed.\n");
759 fprintf(stderr, "Usage: %s [options]\n", __progname);
760 fprintf(stderr, "Options:\n");
761 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
762 fprintf(stderr, " -c Change comment in private and public key files.\n");
763 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
764 fprintf(stderr, " -f filename Filename of the key file.\n");
765 fprintf(stderr, " -g Use generic DNS resource record format.\n");
766 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
767 fprintf(stderr, " -l Show fingerprint of key file.\n");
768 fprintf(stderr, " -p Change passphrase of private key file.\n");
769 fprintf(stderr, " -q Quiet.\n");
770 fprintf(stderr, " -y Read private key file and print public key.\n");
771 fprintf(stderr, " -t type Specify type of key to create.\n");
772 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
773 fprintf(stderr, " -C comment Provide new comment.\n");
774 fprintf(stderr, " -N phrase Provide new passphrase.\n");
775 fprintf(stderr, " -P phrase Provide old passphrase.\n");
777 fprintf(stderr, " -r hostname Print DNS resource record.\n");
780 fprintf(stderr, " -D reader Download public key from smartcard.\n");
781 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
782 #endif /* SMARTCARD */
788 * Main program for key management.
791 main(int ac, char **av)
793 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
794 char *reader_id = NULL;
795 char *resource_record_hostname = NULL;
796 Key *private, *public;
799 int opt, type, fd, download = 0;
805 __progname = get_progname(av[0]);
807 SSLeay_add_all_algorithms();
811 /* we need this for the home * directory. */
812 pw = getpwuid(getuid());
814 printf("You don't exist, go away!\n");
817 if (gethostname(hostname, sizeof(hostname)) < 0) {
818 perror("gethostname");
822 while ((opt = getopt(ac, av, "degiqpclBRxXyb:f:t:U:D:P:N:C:r:")) != -1) {
826 if (bits < 512 || bits > 32768) {
827 printf("Bits has bad value.\n");
832 print_fingerprint = 1;
835 print_bubblebabble = 1;
838 change_passphrase = 1;
844 strlcpy(identity_file, optarg, sizeof(identity_file));
851 identity_passphrase = optarg;
854 identity_new_passphrase = optarg;
857 identity_comment = optarg;
874 convert_from_ssh2 = 1;
880 key_type_name = "dsa";
883 key_type_name = optarg;
891 resource_record_hostname = optarg;
899 printf("Too many arguments.\n");
902 if (change_passphrase && change_comment) {
903 printf("Can only have one of -p and -c.\n");
906 if (print_fingerprint || print_bubblebabble)
908 if (change_passphrase)
909 do_change_passphrase(pw);
911 do_change_comment(pw);
913 do_convert_to_ssh2(pw);
914 if (convert_from_ssh2)
915 do_convert_from_ssh2(pw);
918 if (resource_record_hostname != NULL) {
920 do_print_resource_record(pw, resource_record_hostname);
922 fatal("no DNS support.");
925 if (reader_id != NULL) {
928 do_download(pw, reader_id);
930 do_upload(pw, reader_id);
931 #else /* SMARTCARD */
932 fatal("no support for smartcards.");
933 #endif /* SMARTCARD */
938 if (key_type_name == NULL) {
939 printf("You must specify a key type (-t).\n");
942 type = key_type_from_name(key_type_name);
943 if (type == KEY_UNSPEC) {
944 fprintf(stderr, "unknown key type %s\n", key_type_name);
948 printf("Generating public/private %s key pair.\n", key_type_name);
949 private = key_generate(type, bits);
950 if (private == NULL) {
951 fprintf(stderr, "key_generate failed");
954 public = key_from_private(private);
957 ask_filename(pw, "Enter file in which to save the key");
959 /* Create ~/.ssh directory if it doesn\'t already exist. */
960 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
961 if (strstr(identity_file, dotsshdir) != NULL &&
962 stat(dotsshdir, &st) < 0) {
963 if (mkdir(dotsshdir, 0700) < 0)
964 error("Could not create directory '%s'.", dotsshdir);
966 printf("Created directory '%s'.\n", dotsshdir);
968 /* If the file already exists, ask the user to confirm. */
969 if (stat(identity_file, &st) >= 0) {
971 printf("%s already exists.\n", identity_file);
972 printf("Overwrite (y/n)? ");
974 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
976 if (yesno[0] != 'y' && yesno[0] != 'Y')
979 /* Ask for a passphrase (twice). */
980 if (identity_passphrase)
981 passphrase1 = xstrdup(identity_passphrase);
982 else if (identity_new_passphrase)
983 passphrase1 = xstrdup(identity_new_passphrase);
987 read_passphrase("Enter passphrase (empty for no "
988 "passphrase): ", RP_ALLOW_STDIN);
989 passphrase2 = read_passphrase("Enter same passphrase again: ",
991 if (strcmp(passphrase1, passphrase2) != 0) {
993 * The passphrases do not match. Clear them and
996 memset(passphrase1, 0, strlen(passphrase1));
997 memset(passphrase2, 0, strlen(passphrase2));
1000 printf("Passphrases do not match. Try again.\n");
1001 goto passphrase_again;
1003 /* Clear the other copy of the passphrase. */
1004 memset(passphrase2, 0, strlen(passphrase2));
1008 if (identity_comment) {
1009 strlcpy(comment, identity_comment, sizeof(comment));
1011 /* Create default commend field for the passphrase. */
1012 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1015 /* Save the key with the given passphrase and comment. */
1016 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1017 printf("Saving the key failed: %s.\n", identity_file);
1018 memset(passphrase1, 0, strlen(passphrase1));
1022 /* Clear the passphrase. */
1023 memset(passphrase1, 0, strlen(passphrase1));
1026 /* Clear the private key and the random number generator. */
1031 printf("Your identification has been saved in %s.\n", identity_file);
1033 strlcat(identity_file, ".pub", sizeof(identity_file));
1034 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1036 printf("Could not save your public key in %s\n", identity_file);
1039 f = fdopen(fd, "w");
1041 printf("fdopen %s failed", identity_file);
1044 if (!key_write(public, f))
1045 fprintf(stderr, "write key failed");
1046 fprintf(f, " %s\n", comment);
1050 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1051 printf("Your public key has been saved in %s.\n",
1053 printf("The key fingerprint is:\n");
1054 printf("%s %s\n", fp, comment);