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.105 2003/05/14 18:16:20 jakob 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;
73 int print_generic = 0;
75 char *key_type_name = NULL;
78 #ifdef HAVE___PROGNAME
79 extern char *__progname;
84 char hostname[MAXHOSTNAMELEN];
87 ask_filename(struct passwd *pw, const char *prompt)
92 if (key_type_name == NULL)
93 name = _PATH_SSH_CLIENT_ID_RSA;
95 switch (key_type_from_name(key_type_name)) {
97 name = _PATH_SSH_CLIENT_IDENTITY;
100 name = _PATH_SSH_CLIENT_ID_DSA;
103 name = _PATH_SSH_CLIENT_ID_RSA;
106 fprintf(stderr, "bad key type");
111 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
112 fprintf(stderr, "%s (%s): ", prompt, identity_file);
113 if (fgets(buf, sizeof(buf), stdin) == NULL)
115 if (strchr(buf, '\n'))
116 *strchr(buf, '\n') = 0;
117 if (strcmp(buf, "") != 0)
118 strlcpy(identity_file, buf, sizeof(identity_file));
123 load_identity(char *filename)
128 prv = key_load_private(filename, "", NULL);
130 if (identity_passphrase)
131 pass = xstrdup(identity_passphrase);
133 pass = read_passphrase("Enter passphrase: ",
135 prv = key_load_private(filename, pass, NULL);
136 memset(pass, 0, strlen(pass));
142 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
143 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
144 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
145 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
148 do_convert_to_ssh2(struct passwd *pw)
156 ask_filename(pw, "Enter file in which the key is");
157 if (stat(identity_file, &st) < 0) {
158 perror(identity_file);
161 if ((k = key_load_public(identity_file, NULL)) == NULL) {
162 if ((k = load_identity(identity_file)) == NULL) {
163 fprintf(stderr, "load failed\n");
167 if (k->type == KEY_RSA1) {
168 fprintf(stderr, "version 1 keys are not supported\n");
171 if (key_to_blob(k, &blob, &len) <= 0) {
172 fprintf(stderr, "key_to_blob failed\n");
175 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
177 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
178 key_size(k), key_type(k),
179 pw->pw_name, hostname);
180 dump_base64(stdout, blob, len);
181 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
188 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
190 int bits = buffer_get_int(b);
191 int bytes = (bits + 7) / 8;
193 if (buffer_len(b) < bytes)
194 fatal("buffer_get_bignum_bits: input buffer too small: "
195 "need %d have %d", bytes, buffer_len(b));
196 BN_bin2bn(buffer_ptr(b), bytes, value);
197 buffer_consume(b, bytes);
201 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
206 u_char *sig, data[] = "abcde12345";
207 int magic, rlen, ktype, i1, i2, i3, i4;
212 buffer_append(&b, blob, blen);
214 magic = buffer_get_int(&b);
215 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
216 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
220 i1 = buffer_get_int(&b);
221 type = buffer_get_string(&b, NULL);
222 cipher = buffer_get_string(&b, NULL);
223 i2 = buffer_get_int(&b);
224 i3 = buffer_get_int(&b);
225 i4 = buffer_get_int(&b);
226 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
227 if (strcmp(cipher, "none") != 0) {
228 error("unsupported cipher %s", cipher);
236 if (strstr(type, "dsa")) {
238 } else if (strstr(type, "rsa")) {
244 key = key_new_private(ktype);
249 buffer_get_bignum_bits(&b, key->dsa->p);
250 buffer_get_bignum_bits(&b, key->dsa->g);
251 buffer_get_bignum_bits(&b, key->dsa->q);
252 buffer_get_bignum_bits(&b, key->dsa->pub_key);
253 buffer_get_bignum_bits(&b, key->dsa->priv_key);
256 e = buffer_get_char(&b);
260 e += buffer_get_char(&b);
263 e += buffer_get_char(&b);
266 if (!BN_set_word(key->rsa->e, e)) {
271 buffer_get_bignum_bits(&b, key->rsa->d);
272 buffer_get_bignum_bits(&b, key->rsa->n);
273 buffer_get_bignum_bits(&b, key->rsa->iqmp);
274 buffer_get_bignum_bits(&b, key->rsa->q);
275 buffer_get_bignum_bits(&b, key->rsa->p);
276 rsa_generate_additional_parameters(key->rsa);
279 rlen = buffer_len(&b);
281 error("do_convert_private_ssh2_from_blob: "
282 "remaining bytes in key blob %d", rlen);
286 key_sign(key, &sig, &slen, data, sizeof(data));
287 key_verify(key, sig, slen, data, sizeof(data));
293 do_convert_from_ssh2(struct passwd *pw)
302 int escaped = 0, private = 0, ok;
306 ask_filename(pw, "Enter file in which the key is");
307 if (stat(identity_file, &st) < 0) {
308 perror(identity_file);
311 fp = fopen(identity_file, "r");
313 perror(identity_file);
317 while (fgets(line, sizeof(line), fp)) {
318 if (!(p = strchr(line, '\n'))) {
319 fprintf(stderr, "input line too long.\n");
322 if (p > line && p[-1] == '\\')
324 if (strncmp(line, "----", 4) == 0 ||
325 strstr(line, ": ") != NULL) {
326 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
328 if (strstr(line, " END ") != NULL) {
331 /* fprintf(stderr, "ignore: %s", line); */
336 /* fprintf(stderr, "escaped: %s", line); */
340 strlcat(encoded, line, sizeof(encoded));
342 len = strlen(encoded);
343 if (((len % 4) == 3) &&
344 (encoded[len-1] == '=') &&
345 (encoded[len-2] == '=') &&
346 (encoded[len-3] == '='))
347 encoded[len-3] = '\0';
348 blen = uudecode(encoded, blob, sizeof(blob));
350 fprintf(stderr, "uudecode failed.\n");
354 do_convert_private_ssh2_from_blob(blob, blen) :
355 key_from_blob(blob, blen);
357 fprintf(stderr, "decode blob failed.\n");
361 (k->type == KEY_DSA ?
362 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
363 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
364 key_write(k, stdout);
366 fprintf(stderr, "key write failed");
371 fprintf(stdout, "\n");
377 do_print_public(struct passwd *pw)
383 ask_filename(pw, "Enter file in which the key is");
384 if (stat(identity_file, &st) < 0) {
385 perror(identity_file);
388 prv = load_identity(identity_file);
390 fprintf(stderr, "load failed\n");
393 if (!key_write(prv, stdout))
394 fprintf(stderr, "key_write failed");
396 fprintf(stdout, "\n");
402 do_upload(struct passwd *pw, const char *sc_reader_id)
409 ask_filename(pw, "Enter file in which the key is");
410 if (stat(identity_file, &st) < 0) {
411 perror(identity_file);
414 prv = load_identity(identity_file);
416 error("load failed");
419 ret = sc_put_key(prv, sc_reader_id);
423 logit("loading key done");
428 do_download(struct passwd *pw, const char *sc_reader_id)
433 keys = sc_get_keys(sc_reader_id, NULL);
435 fatal("cannot read public key from smartcard");
436 for (i = 0; keys[i]; i++) {
437 key_write(keys[i], stdout);
439 fprintf(stdout, "\n");
444 #endif /* SMARTCARD */
447 do_fingerprint(struct passwd *pw)
451 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
452 int i, skip = 0, num = 1, invalid = 1;
457 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
458 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
461 ask_filename(pw, "Enter file in which the key is");
462 if (stat(identity_file, &st) < 0) {
463 perror(identity_file);
466 public = key_load_public(identity_file, &comment);
467 if (public != NULL) {
468 fp = key_fingerprint(public, fptype, rep);
469 printf("%u %s %s\n", key_size(public), fp, comment);
478 f = fopen(identity_file, "r");
480 while (fgets(line, sizeof(line), f)) {
481 i = strlen(line) - 1;
482 if (line[i] != '\n') {
483 error("line %d too long: %.40s...", num, line);
494 /* Skip leading whitespace, empty and comment lines. */
495 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
497 if (!*cp || *cp == '\n' || *cp == '#')
499 i = strtol(cp, &ep, 10);
500 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
503 for (; *cp && (quoted || (*cp != ' ' &&
504 *cp != '\t')); cp++) {
505 if (*cp == '\\' && cp[1] == '"')
506 cp++; /* Skip both */
515 public = key_new(KEY_RSA1);
516 if (key_read(public, &cp) != 1) {
519 public = key_new(KEY_UNSPEC);
520 if (key_read(public, &cp) != 1) {
525 comment = *cp ? cp : comment;
526 fp = key_fingerprint(public, fptype, rep);
527 printf("%u %s %s\n", key_size(public), fp,
528 comment ? comment : "no comment");
536 printf("%s is not a public key file.\n", identity_file);
543 * Perform changing a passphrase. The argument is the passwd structure
544 * for the current user.
547 do_change_passphrase(struct passwd *pw)
550 char *old_passphrase, *passphrase1, *passphrase2;
555 ask_filename(pw, "Enter file in which the key is");
556 if (stat(identity_file, &st) < 0) {
557 perror(identity_file);
560 /* Try to load the file with empty passphrase. */
561 private = key_load_private(identity_file, "", &comment);
562 if (private == NULL) {
563 if (identity_passphrase)
564 old_passphrase = xstrdup(identity_passphrase);
567 read_passphrase("Enter old passphrase: ",
569 private = key_load_private(identity_file, old_passphrase,
571 memset(old_passphrase, 0, strlen(old_passphrase));
572 xfree(old_passphrase);
573 if (private == NULL) {
574 printf("Bad passphrase.\n");
578 printf("Key has comment '%s'\n", comment);
580 /* Ask the new passphrase (twice). */
581 if (identity_new_passphrase) {
582 passphrase1 = xstrdup(identity_new_passphrase);
586 read_passphrase("Enter new passphrase (empty for no "
587 "passphrase): ", RP_ALLOW_STDIN);
588 passphrase2 = read_passphrase("Enter same passphrase again: ",
591 /* Verify that they are the same. */
592 if (strcmp(passphrase1, passphrase2) != 0) {
593 memset(passphrase1, 0, strlen(passphrase1));
594 memset(passphrase2, 0, strlen(passphrase2));
597 printf("Pass phrases do not match. Try again.\n");
600 /* Destroy the other copy. */
601 memset(passphrase2, 0, strlen(passphrase2));
605 /* Save the file using the new passphrase. */
606 if (!key_save_private(private, identity_file, passphrase1, comment)) {
607 printf("Saving the key failed: %s.\n", identity_file);
608 memset(passphrase1, 0, strlen(passphrase1));
614 /* Destroy the passphrase and the copy of the key in memory. */
615 memset(passphrase1, 0, strlen(passphrase1));
617 key_free(private); /* Destroys contents */
620 printf("Your identification has been saved with the new passphrase.\n");
626 * Print the SSHFP RR.
629 do_print_resource_record(struct passwd *pw, char *hostname)
632 char *comment = NULL;
636 ask_filename(pw, "Enter file in which the key is");
637 if (stat(identity_file, &st) < 0) {
638 perror(identity_file);
641 public = key_load_public(identity_file, &comment);
642 if (public != NULL) {
643 export_dns_rr(hostname, public, stdout, print_generic);
651 printf("failed to read v2 public key from %s.\n", identity_file);
657 * Change the comment of a private key file.
660 do_change_comment(struct passwd *pw)
662 char new_comment[1024], *comment, *passphrase;
670 ask_filename(pw, "Enter file in which the key is");
671 if (stat(identity_file, &st) < 0) {
672 perror(identity_file);
675 private = key_load_private(identity_file, "", &comment);
676 if (private == NULL) {
677 if (identity_passphrase)
678 passphrase = xstrdup(identity_passphrase);
679 else if (identity_new_passphrase)
680 passphrase = xstrdup(identity_new_passphrase);
682 passphrase = read_passphrase("Enter passphrase: ",
684 /* Try to load using the passphrase. */
685 private = key_load_private(identity_file, passphrase, &comment);
686 if (private == NULL) {
687 memset(passphrase, 0, strlen(passphrase));
689 printf("Bad passphrase.\n");
693 passphrase = xstrdup("");
695 if (private->type != KEY_RSA1) {
696 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
700 printf("Key now has comment '%s'\n", comment);
702 if (identity_comment) {
703 strlcpy(new_comment, identity_comment, sizeof(new_comment));
705 printf("Enter new comment: ");
707 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
708 memset(passphrase, 0, strlen(passphrase));
712 if (strchr(new_comment, '\n'))
713 *strchr(new_comment, '\n') = 0;
716 /* Save the file using the new passphrase. */
717 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
718 printf("Saving the key failed: %s.\n", identity_file);
719 memset(passphrase, 0, strlen(passphrase));
725 memset(passphrase, 0, strlen(passphrase));
727 public = key_from_private(private);
730 strlcat(identity_file, ".pub", sizeof(identity_file));
731 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
733 printf("Could not save your public key in %s\n", identity_file);
738 printf("fdopen %s failed", identity_file);
741 if (!key_write(public, f))
742 fprintf(stderr, "write key failed");
744 fprintf(f, " %s\n", new_comment);
749 printf("The comment in your key file has been changed.\n");
756 fprintf(stderr, "Usage: %s [options]\n", __progname);
757 fprintf(stderr, "Options:\n");
758 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
759 fprintf(stderr, " -c Change comment in private and public key files.\n");
760 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
761 fprintf(stderr, " -f filename Filename of the key file.\n");
762 fprintf(stderr, " -g Use generic DNS resource record format.\n");
763 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
764 fprintf(stderr, " -l Show fingerprint of key file.\n");
765 fprintf(stderr, " -p Change passphrase of private key file.\n");
766 fprintf(stderr, " -q Quiet.\n");
767 fprintf(stderr, " -y Read private key file and print public key.\n");
768 fprintf(stderr, " -t type Specify type of key to create.\n");
769 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
770 fprintf(stderr, " -C comment Provide new comment.\n");
771 fprintf(stderr, " -N phrase Provide new passphrase.\n");
772 fprintf(stderr, " -P phrase Provide old passphrase.\n");
774 fprintf(stderr, " -r hostname Print DNS resource record.\n");
777 fprintf(stderr, " -D reader Download public key from smartcard.\n");
778 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
779 #endif /* SMARTCARD */
785 * Main program for key management.
788 main(int ac, char **av)
790 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
791 char *reader_id = NULL;
792 char *resource_record_hostname = NULL;
793 Key *private, *public;
796 int opt, type, fd, download = 0;
802 __progname = get_progname(av[0]);
804 SSLeay_add_all_algorithms();
808 /* we need this for the home * directory. */
809 pw = getpwuid(getuid());
811 printf("You don't exist, go away!\n");
814 if (gethostname(hostname, sizeof(hostname)) < 0) {
815 perror("gethostname");
819 while ((opt = getopt(ac, av, "degiqpclBRxXyb:f:t:U:D:P:N:C:r:")) != -1) {
823 if (bits < 512 || bits > 32768) {
824 printf("Bits has bad value.\n");
829 print_fingerprint = 1;
832 print_bubblebabble = 1;
835 change_passphrase = 1;
841 strlcpy(identity_file, optarg, sizeof(identity_file));
848 identity_passphrase = optarg;
851 identity_new_passphrase = optarg;
854 identity_comment = optarg;
871 convert_from_ssh2 = 1;
877 key_type_name = "dsa";
880 key_type_name = optarg;
888 resource_record_hostname = optarg;
896 printf("Too many arguments.\n");
899 if (change_passphrase && change_comment) {
900 printf("Can only have one of -p and -c.\n");
903 if (print_fingerprint || print_bubblebabble)
905 if (change_passphrase)
906 do_change_passphrase(pw);
908 do_change_comment(pw);
910 do_convert_to_ssh2(pw);
911 if (convert_from_ssh2)
912 do_convert_from_ssh2(pw);
915 if (resource_record_hostname != NULL) {
917 do_print_resource_record(pw, resource_record_hostname);
919 fatal("no DNS support.");
922 if (reader_id != NULL) {
925 do_download(pw, reader_id);
927 do_upload(pw, reader_id);
928 #else /* SMARTCARD */
929 fatal("no support for smartcards.");
930 #endif /* SMARTCARD */
935 if (key_type_name == NULL) {
936 printf("You must specify a key type (-t).\n");
939 type = key_type_from_name(key_type_name);
940 if (type == KEY_UNSPEC) {
941 fprintf(stderr, "unknown key type %s\n", key_type_name);
945 printf("Generating public/private %s key pair.\n", key_type_name);
946 private = key_generate(type, bits);
947 if (private == NULL) {
948 fprintf(stderr, "key_generate failed");
951 public = key_from_private(private);
954 ask_filename(pw, "Enter file in which to save the key");
956 /* Create ~/.ssh directory if it doesn\'t already exist. */
957 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
958 if (strstr(identity_file, dotsshdir) != NULL &&
959 stat(dotsshdir, &st) < 0) {
960 if (mkdir(dotsshdir, 0700) < 0)
961 error("Could not create directory '%s'.", dotsshdir);
963 printf("Created directory '%s'.\n", dotsshdir);
965 /* If the file already exists, ask the user to confirm. */
966 if (stat(identity_file, &st) >= 0) {
968 printf("%s already exists.\n", identity_file);
969 printf("Overwrite (y/n)? ");
971 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
973 if (yesno[0] != 'y' && yesno[0] != 'Y')
976 /* Ask for a passphrase (twice). */
977 if (identity_passphrase)
978 passphrase1 = xstrdup(identity_passphrase);
979 else if (identity_new_passphrase)
980 passphrase1 = xstrdup(identity_new_passphrase);
984 read_passphrase("Enter passphrase (empty for no "
985 "passphrase): ", RP_ALLOW_STDIN);
986 passphrase2 = read_passphrase("Enter same passphrase again: ",
988 if (strcmp(passphrase1, passphrase2) != 0) {
990 * The passphrases do not match. Clear them and
993 memset(passphrase1, 0, strlen(passphrase1));
994 memset(passphrase2, 0, strlen(passphrase2));
997 printf("Passphrases do not match. Try again.\n");
998 goto passphrase_again;
1000 /* Clear the other copy of the passphrase. */
1001 memset(passphrase2, 0, strlen(passphrase2));
1005 if (identity_comment) {
1006 strlcpy(comment, identity_comment, sizeof(comment));
1008 /* Create default commend field for the passphrase. */
1009 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1012 /* Save the key with the given passphrase and comment. */
1013 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1014 printf("Saving the key failed: %s.\n", identity_file);
1015 memset(passphrase1, 0, strlen(passphrase1));
1019 /* Clear the passphrase. */
1020 memset(passphrase1, 0, strlen(passphrase1));
1023 /* Clear the private key and the random number generator. */
1028 printf("Your identification has been saved in %s.\n", identity_file);
1030 strlcat(identity_file, ".pub", sizeof(identity_file));
1031 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1033 printf("Could not save your public key in %s\n", identity_file);
1036 f = fdopen(fd, "w");
1038 printf("fdopen %s failed", identity_file);
1041 if (!key_write(public, f))
1042 fprintf(stderr, "write key failed");
1043 fprintf(f, " %s\n", comment);
1047 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1048 printf("Your public key has been saved in %s.\n",
1050 printf("The key fingerprint is:\n");
1051 printf("%s %s\n", fp, comment);