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.102 2002/11/26 00:45:03 wcobb Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
27 #include "pathnames.h"
35 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
39 * Flag indicating that we just want to change the passphrase. This can be
40 * set on the command line.
42 int change_passphrase = 0;
45 * Flag indicating that we just want to change the comment. This can be set
46 * on the command line.
48 int change_comment = 0;
52 /* Flag indicating that we just want to see the key fingerprint */
53 int print_fingerprint = 0;
54 int print_bubblebabble = 0;
56 /* The identity file name, given on the command line or entered by the user. */
57 char identity_file[1024];
58 int have_identity = 0;
60 /* This is set to the passphrase if given on the command line. */
61 char *identity_passphrase = NULL;
63 /* This is set to the new passphrase if given on the command line. */
64 char *identity_new_passphrase = NULL;
66 /* This is set to the new comment if given on the command line. */
67 char *identity_comment = NULL;
69 /* Dump public key file in format used by real and the original SSH 2 */
70 int convert_to_ssh2 = 0;
71 int convert_from_ssh2 = 0;
74 char *key_type_name = NULL;
77 #ifdef HAVE___PROGNAME
78 extern char *__progname;
83 char hostname[MAXHOSTNAMELEN];
86 ask_filename(struct passwd *pw, const char *prompt)
91 if (key_type_name == NULL)
92 name = _PATH_SSH_CLIENT_ID_RSA;
94 switch (key_type_from_name(key_type_name)) {
96 name = _PATH_SSH_CLIENT_IDENTITY;
99 name = _PATH_SSH_CLIENT_ID_DSA;
102 name = _PATH_SSH_CLIENT_ID_RSA;
105 fprintf(stderr, "bad key type");
110 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
111 fprintf(stderr, "%s (%s): ", prompt, identity_file);
112 if (fgets(buf, sizeof(buf), stdin) == NULL)
114 if (strchr(buf, '\n'))
115 *strchr(buf, '\n') = 0;
116 if (strcmp(buf, "") != 0)
117 strlcpy(identity_file, buf, sizeof(identity_file));
122 load_identity(char *filename)
127 prv = key_load_private(filename, "", NULL);
129 if (identity_passphrase)
130 pass = xstrdup(identity_passphrase);
132 pass = read_passphrase("Enter passphrase: ",
134 prv = key_load_private(filename, pass, NULL);
135 memset(pass, 0, strlen(pass));
141 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
142 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
143 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
144 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
147 do_convert_to_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 if ((k = key_load_public(identity_file, NULL)) == NULL) {
161 if ((k = load_identity(identity_file)) == NULL) {
162 fprintf(stderr, "load failed\n");
166 if (key_to_blob(k, &blob, &len) <= 0) {
167 fprintf(stderr, "key_to_blob failed\n");
170 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
172 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
173 key_size(k), key_type(k),
174 pw->pw_name, hostname);
175 dump_base64(stdout, blob, len);
176 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
183 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
185 int bits = buffer_get_int(b);
186 int bytes = (bits + 7) / 8;
188 if (buffer_len(b) < bytes)
189 fatal("buffer_get_bignum_bits: input buffer too small: "
190 "need %d have %d", bytes, buffer_len(b));
191 BN_bin2bn(buffer_ptr(b), bytes, value);
192 buffer_consume(b, bytes);
196 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
201 u_char *sig, data[] = "abcde12345";
202 int magic, rlen, ktype, i1, i2, i3, i4;
207 buffer_append(&b, blob, blen);
209 magic = buffer_get_int(&b);
210 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
211 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
215 i1 = buffer_get_int(&b);
216 type = buffer_get_string(&b, NULL);
217 cipher = buffer_get_string(&b, NULL);
218 i2 = buffer_get_int(&b);
219 i3 = buffer_get_int(&b);
220 i4 = buffer_get_int(&b);
221 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
222 if (strcmp(cipher, "none") != 0) {
223 error("unsupported cipher %s", cipher);
231 if (strstr(type, "dsa")) {
233 } else if (strstr(type, "rsa")) {
239 key = key_new_private(ktype);
244 buffer_get_bignum_bits(&b, key->dsa->p);
245 buffer_get_bignum_bits(&b, key->dsa->g);
246 buffer_get_bignum_bits(&b, key->dsa->q);
247 buffer_get_bignum_bits(&b, key->dsa->pub_key);
248 buffer_get_bignum_bits(&b, key->dsa->priv_key);
251 e = buffer_get_char(&b);
255 e += buffer_get_char(&b);
258 e += buffer_get_char(&b);
261 if (!BN_set_word(key->rsa->e, e)) {
266 buffer_get_bignum_bits(&b, key->rsa->d);
267 buffer_get_bignum_bits(&b, key->rsa->n);
268 buffer_get_bignum_bits(&b, key->rsa->iqmp);
269 buffer_get_bignum_bits(&b, key->rsa->q);
270 buffer_get_bignum_bits(&b, key->rsa->p);
271 rsa_generate_additional_parameters(key->rsa);
274 rlen = buffer_len(&b);
276 error("do_convert_private_ssh2_from_blob: "
277 "remaining bytes in key blob %d", rlen);
281 key_sign(key, &sig, &slen, data, sizeof(data));
282 key_verify(key, sig, slen, data, sizeof(data));
288 do_convert_from_ssh2(struct passwd *pw)
297 int escaped = 0, private = 0, ok;
301 ask_filename(pw, "Enter file in which the key is");
302 if (stat(identity_file, &st) < 0) {
303 perror(identity_file);
306 fp = fopen(identity_file, "r");
308 perror(identity_file);
312 while (fgets(line, sizeof(line), fp)) {
313 if (!(p = strchr(line, '\n'))) {
314 fprintf(stderr, "input line too long.\n");
317 if (p > line && p[-1] == '\\')
319 if (strncmp(line, "----", 4) == 0 ||
320 strstr(line, ": ") != NULL) {
321 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
323 if (strstr(line, " END ") != NULL) {
326 /* fprintf(stderr, "ignore: %s", line); */
331 /* fprintf(stderr, "escaped: %s", line); */
335 strlcat(encoded, line, sizeof(encoded));
337 len = strlen(encoded);
338 if (((len % 4) == 3) &&
339 (encoded[len-1] == '=') &&
340 (encoded[len-2] == '=') &&
341 (encoded[len-3] == '='))
342 encoded[len-3] = '\0';
343 blen = uudecode(encoded, blob, sizeof(blob));
345 fprintf(stderr, "uudecode failed.\n");
349 do_convert_private_ssh2_from_blob(blob, blen) :
350 key_from_blob(blob, blen);
352 fprintf(stderr, "decode blob failed.\n");
356 (k->type == KEY_DSA ?
357 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
358 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
359 key_write(k, stdout);
361 fprintf(stderr, "key write failed");
366 fprintf(stdout, "\n");
372 do_print_public(struct passwd *pw)
378 ask_filename(pw, "Enter file in which the key is");
379 if (stat(identity_file, &st) < 0) {
380 perror(identity_file);
383 prv = load_identity(identity_file);
385 fprintf(stderr, "load failed\n");
388 if (!key_write(prv, stdout))
389 fprintf(stderr, "key_write failed");
391 fprintf(stdout, "\n");
397 do_upload(struct passwd *pw, const char *sc_reader_id)
404 ask_filename(pw, "Enter file in which the key is");
405 if (stat(identity_file, &st) < 0) {
406 perror(identity_file);
409 prv = load_identity(identity_file);
411 error("load failed");
414 ret = sc_put_key(prv, sc_reader_id);
418 log("loading key done");
423 do_download(struct passwd *pw, const char *sc_reader_id)
428 keys = sc_get_keys(sc_reader_id, NULL);
430 fatal("cannot read public key from smartcard");
431 for (i = 0; keys[i]; i++) {
432 key_write(keys[i], stdout);
434 fprintf(stdout, "\n");
439 #endif /* SMARTCARD */
442 do_fingerprint(struct passwd *pw)
446 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
447 int i, skip = 0, num = 1, invalid = 1;
452 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
453 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
456 ask_filename(pw, "Enter file in which the key is");
457 if (stat(identity_file, &st) < 0) {
458 perror(identity_file);
461 public = key_load_public(identity_file, &comment);
462 if (public != NULL) {
463 fp = key_fingerprint(public, fptype, rep);
464 printf("%u %s %s\n", key_size(public), fp, comment);
473 f = fopen(identity_file, "r");
475 while (fgets(line, sizeof(line), f)) {
476 i = strlen(line) - 1;
477 if (line[i] != '\n') {
478 error("line %d too long: %.40s...", num, line);
489 /* Skip leading whitespace, empty and comment lines. */
490 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
492 if (!*cp || *cp == '\n' || *cp == '#')
494 i = strtol(cp, &ep, 10);
495 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
498 for (; *cp && (quoted || (*cp != ' ' &&
499 *cp != '\t')); cp++) {
500 if (*cp == '\\' && cp[1] == '"')
501 cp++; /* Skip both */
510 public = key_new(KEY_RSA1);
511 if (key_read(public, &cp) != 1) {
514 public = key_new(KEY_UNSPEC);
515 if (key_read(public, &cp) != 1) {
520 comment = *cp ? cp : comment;
521 fp = key_fingerprint(public, fptype, rep);
522 printf("%u %s %s\n", key_size(public), fp,
523 comment ? comment : "no comment");
531 printf("%s is not a public key file.\n", identity_file);
538 * Perform changing a passphrase. The argument is the passwd structure
539 * for the current user.
542 do_change_passphrase(struct passwd *pw)
545 char *old_passphrase, *passphrase1, *passphrase2;
550 ask_filename(pw, "Enter file in which the key is");
551 if (stat(identity_file, &st) < 0) {
552 perror(identity_file);
555 /* Try to load the file with empty passphrase. */
556 private = key_load_private(identity_file, "", &comment);
557 if (private == NULL) {
558 if (identity_passphrase)
559 old_passphrase = xstrdup(identity_passphrase);
562 read_passphrase("Enter old passphrase: ",
564 private = key_load_private(identity_file, old_passphrase,
566 memset(old_passphrase, 0, strlen(old_passphrase));
567 xfree(old_passphrase);
568 if (private == NULL) {
569 printf("Bad passphrase.\n");
573 printf("Key has comment '%s'\n", comment);
575 /* Ask the new passphrase (twice). */
576 if (identity_new_passphrase) {
577 passphrase1 = xstrdup(identity_new_passphrase);
581 read_passphrase("Enter new passphrase (empty for no "
582 "passphrase): ", RP_ALLOW_STDIN);
583 passphrase2 = read_passphrase("Enter same passphrase again: ",
586 /* Verify that they are the same. */
587 if (strcmp(passphrase1, passphrase2) != 0) {
588 memset(passphrase1, 0, strlen(passphrase1));
589 memset(passphrase2, 0, strlen(passphrase2));
592 printf("Pass phrases do not match. Try again.\n");
595 /* Destroy the other copy. */
596 memset(passphrase2, 0, strlen(passphrase2));
600 /* Save the file using the new passphrase. */
601 if (!key_save_private(private, identity_file, passphrase1, comment)) {
602 printf("Saving the key failed: %s.\n", identity_file);
603 memset(passphrase1, 0, strlen(passphrase1));
609 /* Destroy the passphrase and the copy of the key in memory. */
610 memset(passphrase1, 0, strlen(passphrase1));
612 key_free(private); /* Destroys contents */
615 printf("Your identification has been saved with the new passphrase.\n");
620 * Change the comment of a private key file.
623 do_change_comment(struct passwd *pw)
625 char new_comment[1024], *comment, *passphrase;
633 ask_filename(pw, "Enter file in which the key is");
634 if (stat(identity_file, &st) < 0) {
635 perror(identity_file);
638 private = key_load_private(identity_file, "", &comment);
639 if (private == NULL) {
640 if (identity_passphrase)
641 passphrase = xstrdup(identity_passphrase);
642 else if (identity_new_passphrase)
643 passphrase = xstrdup(identity_new_passphrase);
645 passphrase = read_passphrase("Enter passphrase: ",
647 /* Try to load using the passphrase. */
648 private = key_load_private(identity_file, passphrase, &comment);
649 if (private == NULL) {
650 memset(passphrase, 0, strlen(passphrase));
652 printf("Bad passphrase.\n");
656 passphrase = xstrdup("");
658 if (private->type != KEY_RSA1) {
659 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
663 printf("Key now has comment '%s'\n", comment);
665 if (identity_comment) {
666 strlcpy(new_comment, identity_comment, sizeof(new_comment));
668 printf("Enter new comment: ");
670 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
671 memset(passphrase, 0, strlen(passphrase));
675 if (strchr(new_comment, '\n'))
676 *strchr(new_comment, '\n') = 0;
679 /* Save the file using the new passphrase. */
680 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
681 printf("Saving the key failed: %s.\n", identity_file);
682 memset(passphrase, 0, strlen(passphrase));
688 memset(passphrase, 0, strlen(passphrase));
690 public = key_from_private(private);
693 strlcat(identity_file, ".pub", sizeof(identity_file));
694 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
696 printf("Could not save your public key in %s\n", identity_file);
701 printf("fdopen %s failed", identity_file);
704 if (!key_write(public, f))
705 fprintf(stderr, "write key failed");
707 fprintf(f, " %s\n", new_comment);
712 printf("The comment in your key file has been changed.\n");
719 fprintf(stderr, "Usage: %s [options]\n", __progname);
720 fprintf(stderr, "Options:\n");
721 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
722 fprintf(stderr, " -c Change comment in private and public key files.\n");
723 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
724 fprintf(stderr, " -f filename Filename of the key file.\n");
725 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
726 fprintf(stderr, " -l Show fingerprint of key file.\n");
727 fprintf(stderr, " -p Change passphrase of private key file.\n");
728 fprintf(stderr, " -q Quiet.\n");
729 fprintf(stderr, " -y Read private key file and print public key.\n");
730 fprintf(stderr, " -t type Specify type of key to create.\n");
731 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
732 fprintf(stderr, " -C comment Provide new comment.\n");
733 fprintf(stderr, " -N phrase Provide new passphrase.\n");
734 fprintf(stderr, " -P phrase Provide old passphrase.\n");
736 fprintf(stderr, " -D reader Download public key from smartcard.\n");
737 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
738 #endif /* SMARTCARD */
744 * Main program for key management.
747 main(int ac, char **av)
749 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
750 char *reader_id = NULL;
751 Key *private, *public;
754 int opt, type, fd, download = 0;
760 __progname = get_progname(av[0]);
762 SSLeay_add_all_algorithms();
766 /* we need this for the home * directory. */
767 pw = getpwuid(getuid());
769 printf("You don't exist, go away!\n");
772 if (gethostname(hostname, sizeof(hostname)) < 0) {
773 perror("gethostname");
777 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
781 if (bits < 512 || bits > 32768) {
782 printf("Bits has bad value.\n");
787 print_fingerprint = 1;
790 print_bubblebabble = 1;
793 change_passphrase = 1;
799 strlcpy(identity_file, optarg, sizeof(identity_file));
803 identity_passphrase = optarg;
806 identity_new_passphrase = optarg;
809 identity_comment = optarg;
826 convert_from_ssh2 = 1;
832 key_type_name = "dsa";
835 key_type_name = optarg;
848 printf("Too many arguments.\n");
851 if (change_passphrase && change_comment) {
852 printf("Can only have one of -p and -c.\n");
855 if (print_fingerprint || print_bubblebabble)
857 if (change_passphrase)
858 do_change_passphrase(pw);
860 do_change_comment(pw);
862 do_convert_to_ssh2(pw);
863 if (convert_from_ssh2)
864 do_convert_from_ssh2(pw);
867 if (reader_id != NULL) {
870 do_download(pw, reader_id);
872 do_upload(pw, reader_id);
873 #else /* SMARTCARD */
874 fatal("no support for smartcards.");
875 #endif /* SMARTCARD */
880 if (key_type_name == NULL) {
881 printf("You must specify a key type (-t).\n");
884 type = key_type_from_name(key_type_name);
885 if (type == KEY_UNSPEC) {
886 fprintf(stderr, "unknown key type %s\n", key_type_name);
890 printf("Generating public/private %s key pair.\n", key_type_name);
891 private = key_generate(type, bits);
892 if (private == NULL) {
893 fprintf(stderr, "key_generate failed");
896 public = key_from_private(private);
899 ask_filename(pw, "Enter file in which to save the key");
901 /* Create ~/.ssh directory if it doesn\'t already exist. */
902 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
903 if (strstr(identity_file, dotsshdir) != NULL &&
904 stat(dotsshdir, &st) < 0) {
905 if (mkdir(dotsshdir, 0700) < 0)
906 error("Could not create directory '%s'.", dotsshdir);
908 printf("Created directory '%s'.\n", dotsshdir);
910 /* If the file already exists, ask the user to confirm. */
911 if (stat(identity_file, &st) >= 0) {
913 printf("%s already exists.\n", identity_file);
914 printf("Overwrite (y/n)? ");
916 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
918 if (yesno[0] != 'y' && yesno[0] != 'Y')
921 /* Ask for a passphrase (twice). */
922 if (identity_passphrase)
923 passphrase1 = xstrdup(identity_passphrase);
924 else if (identity_new_passphrase)
925 passphrase1 = xstrdup(identity_new_passphrase);
929 read_passphrase("Enter passphrase (empty for no "
930 "passphrase): ", RP_ALLOW_STDIN);
931 passphrase2 = read_passphrase("Enter same passphrase again: ",
933 if (strcmp(passphrase1, passphrase2) != 0) {
935 * The passphrases do not match. Clear them and
938 memset(passphrase1, 0, strlen(passphrase1));
939 memset(passphrase2, 0, strlen(passphrase2));
942 printf("Passphrases do not match. Try again.\n");
943 goto passphrase_again;
945 /* Clear the other copy of the passphrase. */
946 memset(passphrase2, 0, strlen(passphrase2));
950 if (identity_comment) {
951 strlcpy(comment, identity_comment, sizeof(comment));
953 /* Create default commend field for the passphrase. */
954 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
957 /* Save the key with the given passphrase and comment. */
958 if (!key_save_private(private, identity_file, passphrase1, comment)) {
959 printf("Saving the key failed: %s.\n", identity_file);
960 memset(passphrase1, 0, strlen(passphrase1));
964 /* Clear the passphrase. */
965 memset(passphrase1, 0, strlen(passphrase1));
968 /* Clear the private key and the random number generator. */
973 printf("Your identification has been saved in %s.\n", identity_file);
975 strlcat(identity_file, ".pub", sizeof(identity_file));
976 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
978 printf("Could not save your public key in %s\n", identity_file);
983 printf("fdopen %s failed", identity_file);
986 if (!key_write(public, f))
987 fprintf(stderr, "write key failed");
988 fprintf(f, " %s\n", comment);
992 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
993 printf("Your public key has been saved in %s.\n",
995 printf("The key fingerprint is:\n");
996 printf("%s %s\n", fp, comment);