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.117 2004/07/11 17:48:47 deraadt Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
27 #include "pathnames.h"
36 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
40 * Flag indicating that we just want to change the passphrase. This can be
41 * set on the command line.
43 int change_passphrase = 0;
46 * Flag indicating that we just want to change the comment. This can be set
47 * on the command line.
49 int change_comment = 0;
53 /* Flag indicating that we just want to see the key fingerprint */
54 int print_fingerprint = 0;
55 int print_bubblebabble = 0;
57 /* The identity file name, given on the command line or entered by the user. */
58 char identity_file[1024];
59 int have_identity = 0;
61 /* This is set to the passphrase if given on the command line. */
62 char *identity_passphrase = NULL;
64 /* This is set to the new passphrase if given on the command line. */
65 char *identity_new_passphrase = NULL;
67 /* This is set to the new comment if given on the command line. */
68 char *identity_comment = NULL;
70 /* Dump public key file in format used by real and the original SSH 2 */
71 int convert_to_ssh2 = 0;
72 int convert_from_ssh2 = 0;
74 int print_generic = 0;
76 char *key_type_name = NULL;
79 extern char *__progname;
81 char hostname[MAXHOSTNAMELEN];
84 int gen_candidates(FILE *, int, int, BIGNUM *);
85 int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
88 ask_filename(struct passwd *pw, const char *prompt)
93 if (key_type_name == NULL)
94 name = _PATH_SSH_CLIENT_ID_RSA;
96 switch (key_type_from_name(key_type_name)) {
98 name = _PATH_SSH_CLIENT_IDENTITY;
101 name = _PATH_SSH_CLIENT_ID_DSA;
104 name = _PATH_SSH_CLIENT_ID_RSA;
107 fprintf(stderr, "bad key type");
112 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
113 fprintf(stderr, "%s (%s): ", prompt, identity_file);
114 if (fgets(buf, sizeof(buf), stdin) == NULL)
116 if (strchr(buf, '\n'))
117 *strchr(buf, '\n') = 0;
118 if (strcmp(buf, "") != 0)
119 strlcpy(identity_file, buf, sizeof(identity_file));
124 load_identity(char *filename)
129 prv = key_load_private(filename, "", NULL);
131 if (identity_passphrase)
132 pass = xstrdup(identity_passphrase);
134 pass = read_passphrase("Enter passphrase: ",
136 prv = key_load_private(filename, pass, NULL);
137 memset(pass, 0, strlen(pass));
143 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
144 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
145 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
146 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
149 do_convert_to_ssh2(struct passwd *pw)
157 ask_filename(pw, "Enter file in which the key is");
158 if (stat(identity_file, &st) < 0) {
159 perror(identity_file);
162 if ((k = key_load_public(identity_file, NULL)) == NULL) {
163 if ((k = load_identity(identity_file)) == NULL) {
164 fprintf(stderr, "load failed\n");
168 if (k->type == KEY_RSA1) {
169 fprintf(stderr, "version 1 keys are not supported\n");
172 if (key_to_blob(k, &blob, &len) <= 0) {
173 fprintf(stderr, "key_to_blob failed\n");
176 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
178 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
179 key_size(k), key_type(k),
180 pw->pw_name, hostname);
181 dump_base64(stdout, blob, len);
182 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
189 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
191 u_int bignum_bits = buffer_get_int(b);
192 u_int bytes = (bignum_bits + 7) / 8;
194 if (buffer_len(b) < bytes)
195 fatal("buffer_get_bignum_bits: input buffer too small: "
196 "need %d have %d", bytes, buffer_len(b));
197 BN_bin2bn(buffer_ptr(b), bytes, value);
198 buffer_consume(b, bytes);
202 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
207 u_char *sig, data[] = "abcde12345";
208 int magic, rlen, ktype, i1, i2, i3, i4;
213 buffer_append(&b, blob, blen);
215 magic = buffer_get_int(&b);
216 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
217 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
221 i1 = buffer_get_int(&b);
222 type = buffer_get_string(&b, NULL);
223 cipher = buffer_get_string(&b, NULL);
224 i2 = buffer_get_int(&b);
225 i3 = buffer_get_int(&b);
226 i4 = buffer_get_int(&b);
227 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
228 if (strcmp(cipher, "none") != 0) {
229 error("unsupported cipher %s", cipher);
237 if (strstr(type, "dsa")) {
239 } else if (strstr(type, "rsa")) {
245 key = key_new_private(ktype);
250 buffer_get_bignum_bits(&b, key->dsa->p);
251 buffer_get_bignum_bits(&b, key->dsa->g);
252 buffer_get_bignum_bits(&b, key->dsa->q);
253 buffer_get_bignum_bits(&b, key->dsa->pub_key);
254 buffer_get_bignum_bits(&b, key->dsa->priv_key);
257 e = buffer_get_char(&b);
261 e += buffer_get_char(&b);
264 e += buffer_get_char(&b);
267 if (!BN_set_word(key->rsa->e, e)) {
272 buffer_get_bignum_bits(&b, key->rsa->d);
273 buffer_get_bignum_bits(&b, key->rsa->n);
274 buffer_get_bignum_bits(&b, key->rsa->iqmp);
275 buffer_get_bignum_bits(&b, key->rsa->q);
276 buffer_get_bignum_bits(&b, key->rsa->p);
277 rsa_generate_additional_parameters(key->rsa);
280 rlen = buffer_len(&b);
282 error("do_convert_private_ssh2_from_blob: "
283 "remaining bytes in key blob %d", rlen);
287 key_sign(key, &sig, &slen, data, sizeof(data));
288 key_verify(key, sig, slen, data, sizeof(data));
294 do_convert_from_ssh2(struct passwd *pw)
303 int escaped = 0, private = 0, ok;
307 ask_filename(pw, "Enter file in which the key is");
308 if (stat(identity_file, &st) < 0) {
309 perror(identity_file);
312 fp = fopen(identity_file, "r");
314 perror(identity_file);
318 while (fgets(line, sizeof(line), fp)) {
319 if (!(p = strchr(line, '\n'))) {
320 fprintf(stderr, "input line too long.\n");
323 if (p > line && p[-1] == '\\')
325 if (strncmp(line, "----", 4) == 0 ||
326 strstr(line, ": ") != NULL) {
327 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
329 if (strstr(line, " END ") != NULL) {
332 /* fprintf(stderr, "ignore: %s", line); */
337 /* fprintf(stderr, "escaped: %s", line); */
341 strlcat(encoded, line, sizeof(encoded));
343 len = strlen(encoded);
344 if (((len % 4) == 3) &&
345 (encoded[len-1] == '=') &&
346 (encoded[len-2] == '=') &&
347 (encoded[len-3] == '='))
348 encoded[len-3] = '\0';
349 blen = uudecode(encoded, blob, sizeof(blob));
351 fprintf(stderr, "uudecode failed.\n");
355 do_convert_private_ssh2_from_blob(blob, blen) :
356 key_from_blob(blob, blen);
358 fprintf(stderr, "decode blob failed.\n");
362 (k->type == KEY_DSA ?
363 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
364 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
365 key_write(k, stdout);
367 fprintf(stderr, "key write failed");
372 fprintf(stdout, "\n");
378 do_print_public(struct passwd *pw)
384 ask_filename(pw, "Enter file in which the key is");
385 if (stat(identity_file, &st) < 0) {
386 perror(identity_file);
389 prv = load_identity(identity_file);
391 fprintf(stderr, "load failed\n");
394 if (!key_write(prv, stdout))
395 fprintf(stderr, "key_write failed");
397 fprintf(stdout, "\n");
403 do_upload(struct passwd *pw, const char *sc_reader_id)
410 ask_filename(pw, "Enter file in which the key is");
411 if (stat(identity_file, &st) < 0) {
412 perror(identity_file);
415 prv = load_identity(identity_file);
417 error("load failed");
420 ret = sc_put_key(prv, sc_reader_id);
424 logit("loading key done");
429 do_download(struct passwd *pw, const char *sc_reader_id)
434 keys = sc_get_keys(sc_reader_id, NULL);
436 fatal("cannot read public key from smartcard");
437 for (i = 0; keys[i]; i++) {
438 key_write(keys[i], stdout);
440 fprintf(stdout, "\n");
445 #endif /* SMARTCARD */
448 do_fingerprint(struct passwd *pw)
452 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
453 int i, skip = 0, num = 1, invalid = 1;
458 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
459 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
462 ask_filename(pw, "Enter file in which the key is");
463 if (stat(identity_file, &st) < 0) {
464 perror(identity_file);
467 public = key_load_public(identity_file, &comment);
468 if (public != NULL) {
469 fp = key_fingerprint(public, fptype, rep);
470 printf("%u %s %s\n", key_size(public), fp, comment);
479 f = fopen(identity_file, "r");
481 while (fgets(line, sizeof(line), f)) {
482 i = strlen(line) - 1;
483 if (line[i] != '\n') {
484 error("line %d too long: %.40s...", num, line);
495 /* Skip leading whitespace, empty and comment lines. */
496 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
498 if (!*cp || *cp == '\n' || *cp == '#')
500 i = strtol(cp, &ep, 10);
501 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
504 for (; *cp && (quoted || (*cp != ' ' &&
505 *cp != '\t')); cp++) {
506 if (*cp == '\\' && cp[1] == '"')
507 cp++; /* Skip both */
516 public = key_new(KEY_RSA1);
517 if (key_read(public, &cp) != 1) {
520 public = key_new(KEY_UNSPEC);
521 if (key_read(public, &cp) != 1) {
526 comment = *cp ? cp : comment;
527 fp = key_fingerprint(public, fptype, rep);
528 printf("%u %s %s\n", key_size(public), fp,
529 comment ? comment : "no comment");
537 printf("%s is not a public key file.\n", identity_file);
544 * Perform changing a passphrase. The argument is the passwd structure
545 * for the current user.
548 do_change_passphrase(struct passwd *pw)
551 char *old_passphrase, *passphrase1, *passphrase2;
556 ask_filename(pw, "Enter file in which the key is");
557 if (stat(identity_file, &st) < 0) {
558 perror(identity_file);
561 /* Try to load the file with empty passphrase. */
562 private = key_load_private(identity_file, "", &comment);
563 if (private == NULL) {
564 if (identity_passphrase)
565 old_passphrase = xstrdup(identity_passphrase);
568 read_passphrase("Enter old passphrase: ",
570 private = key_load_private(identity_file, old_passphrase,
572 memset(old_passphrase, 0, strlen(old_passphrase));
573 xfree(old_passphrase);
574 if (private == NULL) {
575 printf("Bad passphrase.\n");
579 printf("Key has comment '%s'\n", comment);
581 /* Ask the new passphrase (twice). */
582 if (identity_new_passphrase) {
583 passphrase1 = xstrdup(identity_new_passphrase);
587 read_passphrase("Enter new passphrase (empty for no "
588 "passphrase): ", RP_ALLOW_STDIN);
589 passphrase2 = read_passphrase("Enter same passphrase again: ",
592 /* Verify that they are the same. */
593 if (strcmp(passphrase1, passphrase2) != 0) {
594 memset(passphrase1, 0, strlen(passphrase1));
595 memset(passphrase2, 0, strlen(passphrase2));
598 printf("Pass phrases do not match. Try again.\n");
601 /* Destroy the other copy. */
602 memset(passphrase2, 0, strlen(passphrase2));
606 /* Save the file using the new passphrase. */
607 if (!key_save_private(private, identity_file, passphrase1, comment)) {
608 printf("Saving the key failed: %s.\n", identity_file);
609 memset(passphrase1, 0, strlen(passphrase1));
615 /* Destroy the passphrase and the copy of the key in memory. */
616 memset(passphrase1, 0, strlen(passphrase1));
618 key_free(private); /* Destroys contents */
621 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 *hname)
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(hname, public, stdout, print_generic);
651 printf("failed to read v2 public key from %s.\n", identity_file);
656 * Change the comment of a private key file.
659 do_change_comment(struct passwd *pw)
661 char new_comment[1024], *comment, *passphrase;
669 ask_filename(pw, "Enter file in which the key is");
670 if (stat(identity_file, &st) < 0) {
671 perror(identity_file);
674 private = key_load_private(identity_file, "", &comment);
675 if (private == NULL) {
676 if (identity_passphrase)
677 passphrase = xstrdup(identity_passphrase);
678 else if (identity_new_passphrase)
679 passphrase = xstrdup(identity_new_passphrase);
681 passphrase = read_passphrase("Enter passphrase: ",
683 /* Try to load using the passphrase. */
684 private = key_load_private(identity_file, passphrase, &comment);
685 if (private == NULL) {
686 memset(passphrase, 0, strlen(passphrase));
688 printf("Bad passphrase.\n");
692 passphrase = xstrdup("");
694 if (private->type != KEY_RSA1) {
695 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
699 printf("Key now has comment '%s'\n", comment);
701 if (identity_comment) {
702 strlcpy(new_comment, identity_comment, sizeof(new_comment));
704 printf("Enter new comment: ");
706 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
707 memset(passphrase, 0, strlen(passphrase));
711 if (strchr(new_comment, '\n'))
712 *strchr(new_comment, '\n') = 0;
715 /* Save the file using the new passphrase. */
716 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
717 printf("Saving the key failed: %s.\n", identity_file);
718 memset(passphrase, 0, strlen(passphrase));
724 memset(passphrase, 0, strlen(passphrase));
726 public = key_from_private(private);
729 strlcat(identity_file, ".pub", sizeof(identity_file));
730 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
732 printf("Could not save your public key in %s\n", identity_file);
737 printf("fdopen %s failed", identity_file);
740 if (!key_write(public, f))
741 fprintf(stderr, "write key failed");
743 fprintf(f, " %s\n", new_comment);
748 printf("The comment in your key file has been changed.\n");
755 fprintf(stderr, "Usage: %s [options]\n", __progname);
756 fprintf(stderr, "Options:\n");
757 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
758 fprintf(stderr, " -c Change comment in private and public key files.\n");
759 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
760 fprintf(stderr, " -f filename Filename of the key file.\n");
761 fprintf(stderr, " -g Use generic DNS resource record format.\n");
762 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
763 fprintf(stderr, " -l Show fingerprint of key file.\n");
764 fprintf(stderr, " -p Change passphrase of private key file.\n");
765 fprintf(stderr, " -q Quiet.\n");
766 fprintf(stderr, " -y Read private key file and print public key.\n");
767 fprintf(stderr, " -t type Specify type of key to create.\n");
768 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
769 fprintf(stderr, " -C comment Provide new comment.\n");
770 fprintf(stderr, " -N phrase Provide new passphrase.\n");
771 fprintf(stderr, " -P phrase Provide old passphrase.\n");
772 fprintf(stderr, " -r hostname Print DNS resource record.\n");
774 fprintf(stderr, " -D reader Download public key from smartcard.\n");
775 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
776 #endif /* SMARTCARD */
778 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli\n");
779 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli\n");
785 * Main program for key management.
788 main(int ac, char **av)
790 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
791 char out_file[MAXPATHLEN], *reader_id = NULL;
792 char *resource_record_hostname = NULL;
793 Key *private, *public;
796 int opt, type, fd, download = 0, memory = 0;
797 int generator_wanted = 0, trials = 100;
798 int do_gen_candidates = 0, do_screen_candidates = 0;
799 int log_level = SYSLOG_LEVEL_INFO;
800 BIGNUM *start = NULL;
806 __progname = ssh_get_progname(av[0]);
808 SSLeay_add_all_algorithms();
809 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
814 /* we need this for the home * directory. */
815 pw = getpwuid(getuid());
817 printf("You don't exist, go away!\n");
820 if (gethostname(hostname, sizeof(hostname)) < 0) {
821 perror("gethostname");
825 while ((opt = getopt(ac, av,
826 "degiqpclBRvxXyb:f:t:U:D:P:N:C:r:g:T:G:M:S:a:W:")) != -1) {
830 if (bits < 512 || bits > 32768) {
831 printf("Bits has bad value.\n");
836 print_fingerprint = 1;
839 print_bubblebabble = 1;
842 change_passphrase = 1;
848 strlcpy(identity_file, optarg, sizeof(identity_file));
855 identity_passphrase = optarg;
858 identity_new_passphrase = optarg;
861 identity_comment = optarg;
878 convert_from_ssh2 = 1;
884 key_type_name = "dsa";
887 key_type_name = optarg;
895 if (log_level == SYSLOG_LEVEL_INFO)
896 log_level = SYSLOG_LEVEL_DEBUG1;
898 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
899 log_level < SYSLOG_LEVEL_DEBUG3)
904 resource_record_hostname = optarg;
907 generator_wanted = atoi(optarg);
908 if (generator_wanted < 1)
909 fatal("Desired generator has bad value.");
912 trials = atoi(optarg);
915 memory = atoi(optarg);
918 do_gen_candidates = 1;
919 strlcpy(out_file, optarg, sizeof(out_file));
922 do_screen_candidates = 1;
923 strlcpy(out_file, optarg, sizeof(out_file));
926 /* XXX - also compare length against bits */
927 if (BN_hex2bn(&start, optarg) == 0)
928 fatal("Invalid start point.");
937 log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
940 printf("Too many arguments.\n");
943 if (change_passphrase && change_comment) {
944 printf("Can only have one of -p and -c.\n");
947 if (print_fingerprint || print_bubblebabble)
949 if (change_passphrase)
950 do_change_passphrase(pw);
952 do_change_comment(pw);
954 do_convert_to_ssh2(pw);
955 if (convert_from_ssh2)
956 do_convert_from_ssh2(pw);
959 if (resource_record_hostname != NULL) {
960 do_print_resource_record(pw, resource_record_hostname);
962 if (reader_id != NULL) {
965 do_download(pw, reader_id);
967 do_upload(pw, reader_id);
968 #else /* SMARTCARD */
969 fatal("no support for smartcards.");
970 #endif /* SMARTCARD */
973 if (do_gen_candidates) {
974 FILE *out = fopen(out_file, "w");
977 error("Couldn't open modulus candidate file \"%s\": %s",
978 out_file, strerror(errno));
981 if (gen_candidates(out, memory, bits, start) != 0)
982 fatal("modulus candidate generation failed\n");
987 if (do_screen_candidates) {
989 FILE *out = fopen(out_file, "w");
991 if (have_identity && strcmp(identity_file, "-") != 0) {
992 if ((in = fopen(identity_file, "r")) == NULL) {
993 fatal("Couldn't open modulus candidate "
994 "file \"%s\": %s", identity_file,
1001 fatal("Couldn't open moduli file \"%s\": %s",
1002 out_file, strerror(errno));
1004 if (prime_test(in, out, trials, generator_wanted) != 0)
1005 fatal("modulus screening failed\n");
1011 if (key_type_name == NULL) {
1012 printf("You must specify a key type (-t).\n");
1015 type = key_type_from_name(key_type_name);
1016 if (type == KEY_UNSPEC) {
1017 fprintf(stderr, "unknown key type %s\n", key_type_name);
1021 printf("Generating public/private %s key pair.\n", key_type_name);
1022 private = key_generate(type, bits);
1023 if (private == NULL) {
1024 fprintf(stderr, "key_generate failed");
1027 public = key_from_private(private);
1030 ask_filename(pw, "Enter file in which to save the key");
1032 /* Create ~/.ssh directory if it doesn\'t already exist. */
1033 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
1034 if (strstr(identity_file, dotsshdir) != NULL &&
1035 stat(dotsshdir, &st) < 0) {
1036 if (mkdir(dotsshdir, 0700) < 0)
1037 error("Could not create directory '%s'.", dotsshdir);
1039 printf("Created directory '%s'.\n", dotsshdir);
1041 /* If the file already exists, ask the user to confirm. */
1042 if (stat(identity_file, &st) >= 0) {
1044 printf("%s already exists.\n", identity_file);
1045 printf("Overwrite (y/n)? ");
1047 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1049 if (yesno[0] != 'y' && yesno[0] != 'Y')
1052 /* Ask for a passphrase (twice). */
1053 if (identity_passphrase)
1054 passphrase1 = xstrdup(identity_passphrase);
1055 else if (identity_new_passphrase)
1056 passphrase1 = xstrdup(identity_new_passphrase);
1060 read_passphrase("Enter passphrase (empty for no "
1061 "passphrase): ", RP_ALLOW_STDIN);
1062 passphrase2 = read_passphrase("Enter same passphrase again: ",
1064 if (strcmp(passphrase1, passphrase2) != 0) {
1066 * The passphrases do not match. Clear them and
1069 memset(passphrase1, 0, strlen(passphrase1));
1070 memset(passphrase2, 0, strlen(passphrase2));
1073 printf("Passphrases do not match. Try again.\n");
1074 goto passphrase_again;
1076 /* Clear the other copy of the passphrase. */
1077 memset(passphrase2, 0, strlen(passphrase2));
1081 if (identity_comment) {
1082 strlcpy(comment, identity_comment, sizeof(comment));
1084 /* Create default commend field for the passphrase. */
1085 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1088 /* Save the key with the given passphrase and comment. */
1089 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1090 printf("Saving the key failed: %s.\n", identity_file);
1091 memset(passphrase1, 0, strlen(passphrase1));
1095 /* Clear the passphrase. */
1096 memset(passphrase1, 0, strlen(passphrase1));
1099 /* Clear the private key and the random number generator. */
1104 printf("Your identification has been saved in %s.\n", identity_file);
1106 strlcat(identity_file, ".pub", sizeof(identity_file));
1107 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1109 printf("Could not save your public key in %s\n", identity_file);
1112 f = fdopen(fd, "w");
1114 printf("fdopen %s failed", identity_file);
1117 if (!key_write(public, f))
1118 fprintf(stderr, "write key failed");
1119 fprintf(f, " %s\n", comment);
1123 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1124 printf("Your public key has been saved in %s.\n",
1126 printf("The key fingerprint is:\n");
1127 printf("%s %s\n", fp, comment);