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.108 2003/08/14 16:08:58 markus Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
27 #include "pathnames.h"
39 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
43 * Flag indicating that we just want to change the passphrase. This can be
44 * set on the command line.
46 int change_passphrase = 0;
49 * Flag indicating that we just want to change the comment. This can be set
50 * on the command line.
52 int change_comment = 0;
56 /* Flag indicating that we just want to see the key fingerprint */
57 int print_fingerprint = 0;
58 int print_bubblebabble = 0;
60 /* The identity file name, given on the command line or entered by the user. */
61 char identity_file[1024];
62 int have_identity = 0;
64 /* This is set to the passphrase if given on the command line. */
65 char *identity_passphrase = NULL;
67 /* This is set to the new passphrase if given on the command line. */
68 char *identity_new_passphrase = NULL;
70 /* This is set to the new comment if given on the command line. */
71 char *identity_comment = NULL;
73 /* Dump public key file in format used by real and the original SSH 2 */
74 int convert_to_ssh2 = 0;
75 int convert_from_ssh2 = 0;
77 int print_generic = 0;
79 char *key_type_name = NULL;
82 #ifdef HAVE___PROGNAME
83 extern char *__progname;
88 char hostname[MAXHOSTNAMELEN];
91 ask_filename(struct passwd *pw, const char *prompt)
96 if (key_type_name == NULL)
97 name = _PATH_SSH_CLIENT_ID_RSA;
99 switch (key_type_from_name(key_type_name)) {
101 name = _PATH_SSH_CLIENT_IDENTITY;
104 name = _PATH_SSH_CLIENT_ID_DSA;
107 name = _PATH_SSH_CLIENT_ID_RSA;
110 fprintf(stderr, "bad key type");
115 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
116 fprintf(stderr, "%s (%s): ", prompt, identity_file);
117 if (fgets(buf, sizeof(buf), stdin) == NULL)
119 if (strchr(buf, '\n'))
120 *strchr(buf, '\n') = 0;
121 if (strcmp(buf, "") != 0)
122 strlcpy(identity_file, buf, sizeof(identity_file));
127 load_identity(char *filename)
132 prv = key_load_private(filename, "", NULL);
134 if (identity_passphrase)
135 pass = xstrdup(identity_passphrase);
137 pass = read_passphrase("Enter passphrase: ",
139 prv = key_load_private(filename, pass, NULL);
140 memset(pass, 0, strlen(pass));
146 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
147 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
148 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
149 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
152 do_convert_to_ssh2(struct passwd *pw)
160 ask_filename(pw, "Enter file in which the key is");
161 if (stat(identity_file, &st) < 0) {
162 perror(identity_file);
165 if ((k = key_load_public(identity_file, NULL)) == NULL) {
166 if ((k = load_identity(identity_file)) == NULL) {
167 fprintf(stderr, "load failed\n");
171 if (k->type == KEY_RSA1) {
172 fprintf(stderr, "version 1 keys are not supported\n");
175 if (key_to_blob(k, &blob, &len) <= 0) {
176 fprintf(stderr, "key_to_blob failed\n");
179 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
181 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
182 key_size(k), key_type(k),
183 pw->pw_name, hostname);
184 dump_base64(stdout, blob, len);
185 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
192 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
194 int bits = buffer_get_int(b);
195 int bytes = (bits + 7) / 8;
197 if (buffer_len(b) < bytes)
198 fatal("buffer_get_bignum_bits: input buffer too small: "
199 "need %d have %d", bytes, buffer_len(b));
200 BN_bin2bn(buffer_ptr(b), bytes, value);
201 buffer_consume(b, bytes);
205 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
210 u_char *sig, data[] = "abcde12345";
211 int magic, rlen, ktype, i1, i2, i3, i4;
216 buffer_append(&b, blob, blen);
218 magic = buffer_get_int(&b);
219 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
220 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
224 i1 = buffer_get_int(&b);
225 type = buffer_get_string(&b, NULL);
226 cipher = buffer_get_string(&b, NULL);
227 i2 = buffer_get_int(&b);
228 i3 = buffer_get_int(&b);
229 i4 = buffer_get_int(&b);
230 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
231 if (strcmp(cipher, "none") != 0) {
232 error("unsupported cipher %s", cipher);
240 if (strstr(type, "dsa")) {
242 } else if (strstr(type, "rsa")) {
248 key = key_new_private(ktype);
253 buffer_get_bignum_bits(&b, key->dsa->p);
254 buffer_get_bignum_bits(&b, key->dsa->g);
255 buffer_get_bignum_bits(&b, key->dsa->q);
256 buffer_get_bignum_bits(&b, key->dsa->pub_key);
257 buffer_get_bignum_bits(&b, key->dsa->priv_key);
260 e = buffer_get_char(&b);
264 e += buffer_get_char(&b);
267 e += buffer_get_char(&b);
270 if (!BN_set_word(key->rsa->e, e)) {
275 buffer_get_bignum_bits(&b, key->rsa->d);
276 buffer_get_bignum_bits(&b, key->rsa->n);
277 buffer_get_bignum_bits(&b, key->rsa->iqmp);
278 buffer_get_bignum_bits(&b, key->rsa->q);
279 buffer_get_bignum_bits(&b, key->rsa->p);
280 rsa_generate_additional_parameters(key->rsa);
283 rlen = buffer_len(&b);
285 error("do_convert_private_ssh2_from_blob: "
286 "remaining bytes in key blob %d", rlen);
290 key_sign(key, &sig, &slen, data, sizeof(data));
291 key_verify(key, sig, slen, data, sizeof(data));
297 do_convert_from_ssh2(struct passwd *pw)
306 int escaped = 0, private = 0, ok;
310 ask_filename(pw, "Enter file in which the key is");
311 if (stat(identity_file, &st) < 0) {
312 perror(identity_file);
315 fp = fopen(identity_file, "r");
317 perror(identity_file);
321 while (fgets(line, sizeof(line), fp)) {
322 if (!(p = strchr(line, '\n'))) {
323 fprintf(stderr, "input line too long.\n");
326 if (p > line && p[-1] == '\\')
328 if (strncmp(line, "----", 4) == 0 ||
329 strstr(line, ": ") != NULL) {
330 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
332 if (strstr(line, " END ") != NULL) {
335 /* fprintf(stderr, "ignore: %s", line); */
340 /* fprintf(stderr, "escaped: %s", line); */
344 strlcat(encoded, line, sizeof(encoded));
346 len = strlen(encoded);
347 if (((len % 4) == 3) &&
348 (encoded[len-1] == '=') &&
349 (encoded[len-2] == '=') &&
350 (encoded[len-3] == '='))
351 encoded[len-3] = '\0';
352 blen = uudecode(encoded, blob, sizeof(blob));
354 fprintf(stderr, "uudecode failed.\n");
358 do_convert_private_ssh2_from_blob(blob, blen) :
359 key_from_blob(blob, blen);
361 fprintf(stderr, "decode blob failed.\n");
365 (k->type == KEY_DSA ?
366 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
367 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
368 key_write(k, stdout);
370 fprintf(stderr, "key write failed");
375 fprintf(stdout, "\n");
381 do_print_public(struct passwd *pw)
387 ask_filename(pw, "Enter file in which the key is");
388 if (stat(identity_file, &st) < 0) {
389 perror(identity_file);
392 prv = load_identity(identity_file);
394 fprintf(stderr, "load failed\n");
397 if (!key_write(prv, stdout))
398 fprintf(stderr, "key_write failed");
400 fprintf(stdout, "\n");
406 do_upload(struct passwd *pw, const char *sc_reader_id)
413 ask_filename(pw, "Enter file in which the key is");
414 if (stat(identity_file, &st) < 0) {
415 perror(identity_file);
418 prv = load_identity(identity_file);
420 error("load failed");
423 ret = sc_put_key(prv, sc_reader_id);
427 logit("loading key done");
432 do_download(struct passwd *pw, const char *sc_reader_id)
437 keys = sc_get_keys(sc_reader_id, NULL);
439 fatal("cannot read public key from smartcard");
440 for (i = 0; keys[i]; i++) {
441 key_write(keys[i], stdout);
443 fprintf(stdout, "\n");
448 #endif /* SMARTCARD */
451 do_fingerprint(struct passwd *pw)
455 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
456 int i, skip = 0, num = 1, invalid = 1;
461 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
462 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
465 ask_filename(pw, "Enter file in which the key is");
466 if (stat(identity_file, &st) < 0) {
467 perror(identity_file);
470 public = key_load_public(identity_file, &comment);
471 if (public != NULL) {
472 fp = key_fingerprint(public, fptype, rep);
473 printf("%u %s %s\n", key_size(public), fp, comment);
482 f = fopen(identity_file, "r");
484 while (fgets(line, sizeof(line), f)) {
485 i = strlen(line) - 1;
486 if (line[i] != '\n') {
487 error("line %d too long: %.40s...", num, line);
498 /* Skip leading whitespace, empty and comment lines. */
499 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
501 if (!*cp || *cp == '\n' || *cp == '#')
503 i = strtol(cp, &ep, 10);
504 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
507 for (; *cp && (quoted || (*cp != ' ' &&
508 *cp != '\t')); cp++) {
509 if (*cp == '\\' && cp[1] == '"')
510 cp++; /* Skip both */
519 public = key_new(KEY_RSA1);
520 if (key_read(public, &cp) != 1) {
523 public = key_new(KEY_UNSPEC);
524 if (key_read(public, &cp) != 1) {
529 comment = *cp ? cp : comment;
530 fp = key_fingerprint(public, fptype, rep);
531 printf("%u %s %s\n", key_size(public), fp,
532 comment ? comment : "no comment");
540 printf("%s is not a public key file.\n", identity_file);
547 * Perform changing a passphrase. The argument is the passwd structure
548 * for the current user.
551 do_change_passphrase(struct passwd *pw)
554 char *old_passphrase, *passphrase1, *passphrase2;
559 ask_filename(pw, "Enter file in which the key is");
560 if (stat(identity_file, &st) < 0) {
561 perror(identity_file);
564 /* Try to load the file with empty passphrase. */
565 private = key_load_private(identity_file, "", &comment);
566 if (private == NULL) {
567 if (identity_passphrase)
568 old_passphrase = xstrdup(identity_passphrase);
571 read_passphrase("Enter old passphrase: ",
573 private = key_load_private(identity_file, old_passphrase,
575 memset(old_passphrase, 0, strlen(old_passphrase));
576 xfree(old_passphrase);
577 if (private == NULL) {
578 printf("Bad passphrase.\n");
582 printf("Key has comment '%s'\n", comment);
584 /* Ask the new passphrase (twice). */
585 if (identity_new_passphrase) {
586 passphrase1 = xstrdup(identity_new_passphrase);
590 read_passphrase("Enter new passphrase (empty for no "
591 "passphrase): ", RP_ALLOW_STDIN);
592 passphrase2 = read_passphrase("Enter same passphrase again: ",
595 /* Verify that they are the same. */
596 if (strcmp(passphrase1, passphrase2) != 0) {
597 memset(passphrase1, 0, strlen(passphrase1));
598 memset(passphrase2, 0, strlen(passphrase2));
601 printf("Pass phrases do not match. Try again.\n");
604 /* Destroy the other copy. */
605 memset(passphrase2, 0, strlen(passphrase2));
609 /* Save the file using the new passphrase. */
610 if (!key_save_private(private, identity_file, passphrase1, comment)) {
611 printf("Saving the key failed: %s.\n", identity_file);
612 memset(passphrase1, 0, strlen(passphrase1));
618 /* Destroy the passphrase and the copy of the key in memory. */
619 memset(passphrase1, 0, strlen(passphrase1));
621 key_free(private); /* Destroys contents */
624 printf("Your identification has been saved with the new passphrase.\n");
630 * Print the SSHFP RR.
633 do_print_resource_record(struct passwd *pw, char *hostname)
636 char *comment = NULL;
640 ask_filename(pw, "Enter file in which the key is");
641 if (stat(identity_file, &st) < 0) {
642 perror(identity_file);
645 public = key_load_public(identity_file, &comment);
646 if (public != NULL) {
647 export_dns_rr(hostname, public, stdout, print_generic);
655 printf("failed to read v2 public key from %s.\n", identity_file);
661 * Change the comment of a private key file.
664 do_change_comment(struct passwd *pw)
666 char new_comment[1024], *comment, *passphrase;
674 ask_filename(pw, "Enter file in which the key is");
675 if (stat(identity_file, &st) < 0) {
676 perror(identity_file);
679 private = key_load_private(identity_file, "", &comment);
680 if (private == NULL) {
681 if (identity_passphrase)
682 passphrase = xstrdup(identity_passphrase);
683 else if (identity_new_passphrase)
684 passphrase = xstrdup(identity_new_passphrase);
686 passphrase = read_passphrase("Enter passphrase: ",
688 /* Try to load using the passphrase. */
689 private = key_load_private(identity_file, passphrase, &comment);
690 if (private == NULL) {
691 memset(passphrase, 0, strlen(passphrase));
693 printf("Bad passphrase.\n");
697 passphrase = xstrdup("");
699 if (private->type != KEY_RSA1) {
700 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
704 printf("Key now has comment '%s'\n", comment);
706 if (identity_comment) {
707 strlcpy(new_comment, identity_comment, sizeof(new_comment));
709 printf("Enter new comment: ");
711 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
712 memset(passphrase, 0, strlen(passphrase));
716 if (strchr(new_comment, '\n'))
717 *strchr(new_comment, '\n') = 0;
720 /* Save the file using the new passphrase. */
721 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
722 printf("Saving the key failed: %s.\n", identity_file);
723 memset(passphrase, 0, strlen(passphrase));
729 memset(passphrase, 0, strlen(passphrase));
731 public = key_from_private(private);
734 strlcat(identity_file, ".pub", sizeof(identity_file));
735 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
737 printf("Could not save your public key in %s\n", identity_file);
742 printf("fdopen %s failed", identity_file);
745 if (!key_write(public, f))
746 fprintf(stderr, "write key failed");
748 fprintf(f, " %s\n", new_comment);
753 printf("The comment in your key file has been changed.\n");
760 fprintf(stderr, "Usage: %s [options]\n", __progname);
761 fprintf(stderr, "Options:\n");
762 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
763 fprintf(stderr, " -c Change comment in private and public key files.\n");
764 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
765 fprintf(stderr, " -f filename Filename of the key file.\n");
766 fprintf(stderr, " -g Use generic DNS resource record format.\n");
767 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
768 fprintf(stderr, " -l Show fingerprint of key file.\n");
769 fprintf(stderr, " -p Change passphrase of private key file.\n");
770 fprintf(stderr, " -q Quiet.\n");
771 fprintf(stderr, " -y Read private key file and print public key.\n");
772 fprintf(stderr, " -t type Specify type of key to create.\n");
773 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
774 fprintf(stderr, " -C comment Provide new comment.\n");
775 fprintf(stderr, " -N phrase Provide new passphrase.\n");
776 fprintf(stderr, " -P phrase Provide old passphrase.\n");
778 fprintf(stderr, " -r hostname Print DNS resource record.\n");
781 fprintf(stderr, " -D reader Download public key from smartcard.\n");
782 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
783 #endif /* SMARTCARD */
785 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli\n");
786 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli\n");
792 * Main program for key management.
795 main(int ac, char **av)
797 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
798 char out_file[MAXPATHLEN], *reader_id = NULL;
799 char *resource_record_hostname = NULL;
800 Key *private, *public;
803 int opt, type, fd, download = 0, memory = 0;
804 int generator_wanted = 0, trials = 100;
805 int do_gen_candidates = 0, do_screen_candidates = 0;
806 BIGNUM *start = NULL;
812 __progname = ssh_get_progname(av[0]);
814 SSLeay_add_all_algorithms();
815 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
820 /* we need this for the home * directory. */
821 pw = getpwuid(getuid());
823 printf("You don't exist, go away!\n");
826 if (gethostname(hostname, sizeof(hostname)) < 0) {
827 perror("gethostname");
831 while ((opt = getopt(ac, av,
832 "degiqpclBRxXyb:f:t:U:D:P:N:C:r:g:T:G:M:S:a:W:")) != -1) {
836 if (bits < 512 || bits > 32768) {
837 printf("Bits has bad value.\n");
842 print_fingerprint = 1;
845 print_bubblebabble = 1;
848 change_passphrase = 1;
854 strlcpy(identity_file, optarg, sizeof(identity_file));
861 identity_passphrase = optarg;
864 identity_new_passphrase = optarg;
867 identity_comment = optarg;
884 convert_from_ssh2 = 1;
890 key_type_name = "dsa";
893 key_type_name = optarg;
901 resource_record_hostname = optarg;
904 generator_wanted = atoi(optarg);
905 if (generator_wanted < 1)
906 fatal("Desired generator has bad value.");
909 trials = atoi(optarg);
910 if (trials < TRIAL_MINIMUM) {
911 fatal("Minimum primality trials is %d",
916 memory = atoi(optarg);
918 (memory < LARGE_MINIMUM || memory > LARGE_MAXIMUM)) {
919 fatal("Invalid memory amount (min %ld, max %ld)",
920 LARGE_MINIMUM, LARGE_MAXIMUM);
924 do_gen_candidates = 1;
925 strlcpy(out_file, optarg, sizeof(out_file));
928 do_screen_candidates = 1;
929 strlcpy(out_file, optarg, sizeof(out_file));
932 /* XXX - also compare length against bits */
933 if (BN_hex2bn(&start, optarg) == 0)
934 fatal("Invalid start point.");
942 printf("Too many arguments.\n");
945 if (change_passphrase && change_comment) {
946 printf("Can only have one of -p and -c.\n");
949 if (print_fingerprint || print_bubblebabble)
951 if (change_passphrase)
952 do_change_passphrase(pw);
954 do_change_comment(pw);
956 do_convert_to_ssh2(pw);
957 if (convert_from_ssh2)
958 do_convert_from_ssh2(pw);
961 if (resource_record_hostname != NULL) {
963 do_print_resource_record(pw, resource_record_hostname);
965 fatal("no DNS support.");
968 if (reader_id != NULL) {
971 do_download(pw, reader_id);
973 do_upload(pw, reader_id);
974 #else /* SMARTCARD */
975 fatal("no support for smartcards.");
976 #endif /* SMARTCARD */
979 if (do_gen_candidates) {
980 FILE *out = fopen(out_file, "w");
983 error("Couldn't open modulus candidate file \"%s\": %s",
984 out_file, strerror(errno));
987 if (gen_candidates(out, memory, bits, start) != 0)
988 fatal("modulus candidate generation failed\n");
993 if (do_screen_candidates) {
995 FILE *out = fopen(out_file, "w");
997 if (have_identity && strcmp(identity_file, "-") != 0) {
998 if ((in = fopen(identity_file, "r")) == NULL) {
999 fatal("Couldn't open modulus candidate "
1000 "file \"%s\": %s", identity_file,
1007 fatal("Couldn't open moduli file \"%s\": %s",
1008 out_file, strerror(errno));
1010 if (prime_test(in, out, trials, generator_wanted) != 0)
1011 fatal("modulus screening failed\n");
1017 if (key_type_name == NULL) {
1018 printf("You must specify a key type (-t).\n");
1021 type = key_type_from_name(key_type_name);
1022 if (type == KEY_UNSPEC) {
1023 fprintf(stderr, "unknown key type %s\n", key_type_name);
1027 printf("Generating public/private %s key pair.\n", key_type_name);
1028 private = key_generate(type, bits);
1029 if (private == NULL) {
1030 fprintf(stderr, "key_generate failed");
1033 public = key_from_private(private);
1036 ask_filename(pw, "Enter file in which to save the key");
1038 /* Create ~/.ssh directory if it doesn\'t already exist. */
1039 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
1040 if (strstr(identity_file, dotsshdir) != NULL &&
1041 stat(dotsshdir, &st) < 0) {
1042 if (mkdir(dotsshdir, 0700) < 0)
1043 error("Could not create directory '%s'.", dotsshdir);
1045 printf("Created directory '%s'.\n", dotsshdir);
1047 /* If the file already exists, ask the user to confirm. */
1048 if (stat(identity_file, &st) >= 0) {
1050 printf("%s already exists.\n", identity_file);
1051 printf("Overwrite (y/n)? ");
1053 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1055 if (yesno[0] != 'y' && yesno[0] != 'Y')
1058 /* Ask for a passphrase (twice). */
1059 if (identity_passphrase)
1060 passphrase1 = xstrdup(identity_passphrase);
1061 else if (identity_new_passphrase)
1062 passphrase1 = xstrdup(identity_new_passphrase);
1066 read_passphrase("Enter passphrase (empty for no "
1067 "passphrase): ", RP_ALLOW_STDIN);
1068 passphrase2 = read_passphrase("Enter same passphrase again: ",
1070 if (strcmp(passphrase1, passphrase2) != 0) {
1072 * The passphrases do not match. Clear them and
1075 memset(passphrase1, 0, strlen(passphrase1));
1076 memset(passphrase2, 0, strlen(passphrase2));
1079 printf("Passphrases do not match. Try again.\n");
1080 goto passphrase_again;
1082 /* Clear the other copy of the passphrase. */
1083 memset(passphrase2, 0, strlen(passphrase2));
1087 if (identity_comment) {
1088 strlcpy(comment, identity_comment, sizeof(comment));
1090 /* Create default commend field for the passphrase. */
1091 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1094 /* Save the key with the given passphrase and comment. */
1095 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1096 printf("Saving the key failed: %s.\n", identity_file);
1097 memset(passphrase1, 0, strlen(passphrase1));
1101 /* Clear the passphrase. */
1102 memset(passphrase1, 0, strlen(passphrase1));
1105 /* Clear the private key and the random number generator. */
1110 printf("Your identification has been saved in %s.\n", identity_file);
1112 strlcat(identity_file, ".pub", sizeof(identity_file));
1113 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1115 printf("Could not save your public key in %s\n", identity_file);
1118 f = fdopen(fd, "w");
1120 printf("fdopen %s failed", identity_file);
1123 if (!key_write(public, f))
1124 fprintf(stderr, "write key failed");
1125 fprintf(f, " %s\n", comment);
1129 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1130 printf("Your public key has been saved in %s.\n",
1132 printf("The key fingerprint is:\n");
1133 printf("%s %s\n", fp, comment);