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.115 2004/05/09 00:06:47 djm 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 #ifdef HAVE___PROGNAME
80 extern char *__progname;
85 char hostname[MAXHOSTNAMELEN];
88 int gen_candidates(FILE *, int, int, BIGNUM *);
89 int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
92 ask_filename(struct passwd *pw, const char *prompt)
97 if (key_type_name == NULL)
98 name = _PATH_SSH_CLIENT_ID_RSA;
100 switch (key_type_from_name(key_type_name)) {
102 name = _PATH_SSH_CLIENT_IDENTITY;
105 name = _PATH_SSH_CLIENT_ID_DSA;
108 name = _PATH_SSH_CLIENT_ID_RSA;
111 fprintf(stderr, "bad key type");
116 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
117 fprintf(stderr, "%s (%s): ", prompt, identity_file);
118 if (fgets(buf, sizeof(buf), stdin) == NULL)
120 if (strchr(buf, '\n'))
121 *strchr(buf, '\n') = 0;
122 if (strcmp(buf, "") != 0)
123 strlcpy(identity_file, buf, sizeof(identity_file));
128 load_identity(char *filename)
133 prv = key_load_private(filename, "", NULL);
135 if (identity_passphrase)
136 pass = xstrdup(identity_passphrase);
138 pass = read_passphrase("Enter passphrase: ",
140 prv = key_load_private(filename, pass, NULL);
141 memset(pass, 0, strlen(pass));
147 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
148 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
149 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
150 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
153 do_convert_to_ssh2(struct passwd *pw)
161 ask_filename(pw, "Enter file in which the key is");
162 if (stat(identity_file, &st) < 0) {
163 perror(identity_file);
166 if ((k = key_load_public(identity_file, NULL)) == NULL) {
167 if ((k = load_identity(identity_file)) == NULL) {
168 fprintf(stderr, "load failed\n");
172 if (k->type == KEY_RSA1) {
173 fprintf(stderr, "version 1 keys are not supported\n");
176 if (key_to_blob(k, &blob, &len) <= 0) {
177 fprintf(stderr, "key_to_blob failed\n");
180 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
182 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
183 key_size(k), key_type(k),
184 pw->pw_name, hostname);
185 dump_base64(stdout, blob, len);
186 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
193 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
195 u_int bits = buffer_get_int(b);
196 u_int bytes = (bits + 7) / 8;
198 if (buffer_len(b) < bytes)
199 fatal("buffer_get_bignum_bits: input buffer too small: "
200 "need %d have %d", bytes, buffer_len(b));
201 BN_bin2bn(buffer_ptr(b), bytes, value);
202 buffer_consume(b, bytes);
206 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
211 u_char *sig, data[] = "abcde12345";
212 int magic, rlen, ktype, i1, i2, i3, i4;
217 buffer_append(&b, blob, blen);
219 magic = buffer_get_int(&b);
220 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
221 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
225 i1 = buffer_get_int(&b);
226 type = buffer_get_string(&b, NULL);
227 cipher = buffer_get_string(&b, NULL);
228 i2 = buffer_get_int(&b);
229 i3 = buffer_get_int(&b);
230 i4 = buffer_get_int(&b);
231 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
232 if (strcmp(cipher, "none") != 0) {
233 error("unsupported cipher %s", cipher);
241 if (strstr(type, "dsa")) {
243 } else if (strstr(type, "rsa")) {
249 key = key_new_private(ktype);
254 buffer_get_bignum_bits(&b, key->dsa->p);
255 buffer_get_bignum_bits(&b, key->dsa->g);
256 buffer_get_bignum_bits(&b, key->dsa->q);
257 buffer_get_bignum_bits(&b, key->dsa->pub_key);
258 buffer_get_bignum_bits(&b, key->dsa->priv_key);
261 e = buffer_get_char(&b);
265 e += buffer_get_char(&b);
268 e += buffer_get_char(&b);
271 if (!BN_set_word(key->rsa->e, e)) {
276 buffer_get_bignum_bits(&b, key->rsa->d);
277 buffer_get_bignum_bits(&b, key->rsa->n);
278 buffer_get_bignum_bits(&b, key->rsa->iqmp);
279 buffer_get_bignum_bits(&b, key->rsa->q);
280 buffer_get_bignum_bits(&b, key->rsa->p);
281 rsa_generate_additional_parameters(key->rsa);
284 rlen = buffer_len(&b);
286 error("do_convert_private_ssh2_from_blob: "
287 "remaining bytes in key blob %d", rlen);
291 key_sign(key, &sig, &slen, data, sizeof(data));
292 key_verify(key, sig, slen, data, sizeof(data));
298 do_convert_from_ssh2(struct passwd *pw)
307 int escaped = 0, private = 0, ok;
311 ask_filename(pw, "Enter file in which the key is");
312 if (stat(identity_file, &st) < 0) {
313 perror(identity_file);
316 fp = fopen(identity_file, "r");
318 perror(identity_file);
322 while (fgets(line, sizeof(line), fp)) {
323 if (!(p = strchr(line, '\n'))) {
324 fprintf(stderr, "input line too long.\n");
327 if (p > line && p[-1] == '\\')
329 if (strncmp(line, "----", 4) == 0 ||
330 strstr(line, ": ") != NULL) {
331 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
333 if (strstr(line, " END ") != NULL) {
336 /* fprintf(stderr, "ignore: %s", line); */
341 /* fprintf(stderr, "escaped: %s", line); */
345 strlcat(encoded, line, sizeof(encoded));
347 len = strlen(encoded);
348 if (((len % 4) == 3) &&
349 (encoded[len-1] == '=') &&
350 (encoded[len-2] == '=') &&
351 (encoded[len-3] == '='))
352 encoded[len-3] = '\0';
353 blen = uudecode(encoded, blob, sizeof(blob));
355 fprintf(stderr, "uudecode failed.\n");
359 do_convert_private_ssh2_from_blob(blob, blen) :
360 key_from_blob(blob, blen);
362 fprintf(stderr, "decode blob failed.\n");
366 (k->type == KEY_DSA ?
367 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
368 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
369 key_write(k, stdout);
371 fprintf(stderr, "key write failed");
376 fprintf(stdout, "\n");
382 do_print_public(struct passwd *pw)
388 ask_filename(pw, "Enter file in which the key is");
389 if (stat(identity_file, &st) < 0) {
390 perror(identity_file);
393 prv = load_identity(identity_file);
395 fprintf(stderr, "load failed\n");
398 if (!key_write(prv, stdout))
399 fprintf(stderr, "key_write failed");
401 fprintf(stdout, "\n");
407 do_upload(struct passwd *pw, const char *sc_reader_id)
414 ask_filename(pw, "Enter file in which the key is");
415 if (stat(identity_file, &st) < 0) {
416 perror(identity_file);
419 prv = load_identity(identity_file);
421 error("load failed");
424 ret = sc_put_key(prv, sc_reader_id);
428 logit("loading key done");
433 do_download(struct passwd *pw, const char *sc_reader_id)
438 keys = sc_get_keys(sc_reader_id, NULL);
440 fatal("cannot read public key from smartcard");
441 for (i = 0; keys[i]; i++) {
442 key_write(keys[i], stdout);
444 fprintf(stdout, "\n");
449 #endif /* SMARTCARD */
452 do_fingerprint(struct passwd *pw)
456 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
457 int i, skip = 0, num = 1, invalid = 1;
462 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
463 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
466 ask_filename(pw, "Enter file in which the key is");
467 if (stat(identity_file, &st) < 0) {
468 perror(identity_file);
471 public = key_load_public(identity_file, &comment);
472 if (public != NULL) {
473 fp = key_fingerprint(public, fptype, rep);
474 printf("%u %s %s\n", key_size(public), fp, comment);
483 f = fopen(identity_file, "r");
485 while (fgets(line, sizeof(line), f)) {
486 i = strlen(line) - 1;
487 if (line[i] != '\n') {
488 error("line %d too long: %.40s...", num, line);
499 /* Skip leading whitespace, empty and comment lines. */
500 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
502 if (!*cp || *cp == '\n' || *cp == '#')
504 i = strtol(cp, &ep, 10);
505 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
508 for (; *cp && (quoted || (*cp != ' ' &&
509 *cp != '\t')); cp++) {
510 if (*cp == '\\' && cp[1] == '"')
511 cp++; /* Skip both */
520 public = key_new(KEY_RSA1);
521 if (key_read(public, &cp) != 1) {
524 public = key_new(KEY_UNSPEC);
525 if (key_read(public, &cp) != 1) {
530 comment = *cp ? cp : comment;
531 fp = key_fingerprint(public, fptype, rep);
532 printf("%u %s %s\n", key_size(public), fp,
533 comment ? comment : "no comment");
541 printf("%s is not a public key file.\n", identity_file);
548 * Perform changing a passphrase. The argument is the passwd structure
549 * for the current user.
552 do_change_passphrase(struct passwd *pw)
555 char *old_passphrase, *passphrase1, *passphrase2;
560 ask_filename(pw, "Enter file in which the key is");
561 if (stat(identity_file, &st) < 0) {
562 perror(identity_file);
565 /* Try to load the file with empty passphrase. */
566 private = key_load_private(identity_file, "", &comment);
567 if (private == NULL) {
568 if (identity_passphrase)
569 old_passphrase = xstrdup(identity_passphrase);
572 read_passphrase("Enter old passphrase: ",
574 private = key_load_private(identity_file, old_passphrase,
576 memset(old_passphrase, 0, strlen(old_passphrase));
577 xfree(old_passphrase);
578 if (private == NULL) {
579 printf("Bad passphrase.\n");
583 printf("Key has comment '%s'\n", comment);
585 /* Ask the new passphrase (twice). */
586 if (identity_new_passphrase) {
587 passphrase1 = xstrdup(identity_new_passphrase);
591 read_passphrase("Enter new passphrase (empty for no "
592 "passphrase): ", RP_ALLOW_STDIN);
593 passphrase2 = read_passphrase("Enter same passphrase again: ",
596 /* Verify that they are the same. */
597 if (strcmp(passphrase1, passphrase2) != 0) {
598 memset(passphrase1, 0, strlen(passphrase1));
599 memset(passphrase2, 0, strlen(passphrase2));
602 printf("Pass phrases do not match. Try again.\n");
605 /* Destroy the other copy. */
606 memset(passphrase2, 0, strlen(passphrase2));
610 /* Save the file using the new passphrase. */
611 if (!key_save_private(private, identity_file, passphrase1, comment)) {
612 printf("Saving the key failed: %s.\n", identity_file);
613 memset(passphrase1, 0, strlen(passphrase1));
619 /* Destroy the passphrase and the copy of the key in memory. */
620 memset(passphrase1, 0, strlen(passphrase1));
622 key_free(private); /* Destroys contents */
625 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);
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");
776 fprintf(stderr, " -r hostname Print DNS resource record.\n");
778 fprintf(stderr, " -D reader Download public key from smartcard.\n");
779 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
780 #endif /* SMARTCARD */
782 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli\n");
783 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli\n");
789 * Main program for key management.
792 main(int ac, char **av)
794 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
795 char out_file[MAXPATHLEN], *reader_id = NULL;
796 char *resource_record_hostname = NULL;
797 Key *private, *public;
800 int opt, type, fd, download = 0, memory = 0;
801 int generator_wanted = 0, trials = 100;
802 int do_gen_candidates = 0, do_screen_candidates = 0;
803 int log_level = SYSLOG_LEVEL_INFO;
804 BIGNUM *start = NULL;
810 __progname = ssh_get_progname(av[0]);
812 SSLeay_add_all_algorithms();
813 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
818 /* we need this for the home * directory. */
819 pw = getpwuid(getuid());
821 printf("You don't exist, go away!\n");
824 if (gethostname(hostname, sizeof(hostname)) < 0) {
825 perror("gethostname");
829 while ((opt = getopt(ac, av,
830 "degiqpclBRvxXyb:f:t:U:D:P:N:C:r:g:T:G:M:S:a:W:")) != -1) {
834 if (bits < 512 || bits > 32768) {
835 printf("Bits has bad value.\n");
840 print_fingerprint = 1;
843 print_bubblebabble = 1;
846 change_passphrase = 1;
852 strlcpy(identity_file, optarg, sizeof(identity_file));
859 identity_passphrase = optarg;
862 identity_new_passphrase = optarg;
865 identity_comment = optarg;
882 convert_from_ssh2 = 1;
888 key_type_name = "dsa";
891 key_type_name = optarg;
899 if (log_level == SYSLOG_LEVEL_INFO)
900 log_level = SYSLOG_LEVEL_DEBUG1;
902 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
903 log_level < SYSLOG_LEVEL_DEBUG3)
908 resource_record_hostname = optarg;
911 generator_wanted = atoi(optarg);
912 if (generator_wanted < 1)
913 fatal("Desired generator has bad value.");
916 trials = atoi(optarg);
919 memory = atoi(optarg);
922 do_gen_candidates = 1;
923 strlcpy(out_file, optarg, sizeof(out_file));
926 do_screen_candidates = 1;
927 strlcpy(out_file, optarg, sizeof(out_file));
930 /* XXX - also compare length against bits */
931 if (BN_hex2bn(&start, optarg) == 0)
932 fatal("Invalid start point.");
941 log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
944 printf("Too many arguments.\n");
947 if (change_passphrase && change_comment) {
948 printf("Can only have one of -p and -c.\n");
951 if (print_fingerprint || print_bubblebabble)
953 if (change_passphrase)
954 do_change_passphrase(pw);
956 do_change_comment(pw);
958 do_convert_to_ssh2(pw);
959 if (convert_from_ssh2)
960 do_convert_from_ssh2(pw);
963 if (resource_record_hostname != NULL) {
964 do_print_resource_record(pw, resource_record_hostname);
966 if (reader_id != NULL) {
969 do_download(pw, reader_id);
971 do_upload(pw, reader_id);
972 #else /* SMARTCARD */
973 fatal("no support for smartcards.");
974 #endif /* SMARTCARD */
977 if (do_gen_candidates) {
978 FILE *out = fopen(out_file, "w");
981 error("Couldn't open modulus candidate file \"%s\": %s",
982 out_file, strerror(errno));
985 if (gen_candidates(out, memory, bits, start) != 0)
986 fatal("modulus candidate generation failed\n");
991 if (do_screen_candidates) {
993 FILE *out = fopen(out_file, "w");
995 if (have_identity && strcmp(identity_file, "-") != 0) {
996 if ((in = fopen(identity_file, "r")) == NULL) {
997 fatal("Couldn't open modulus candidate "
998 "file \"%s\": %s", identity_file,
1005 fatal("Couldn't open moduli file \"%s\": %s",
1006 out_file, strerror(errno));
1008 if (prime_test(in, out, trials, generator_wanted) != 0)
1009 fatal("modulus screening failed\n");
1015 if (key_type_name == NULL) {
1016 printf("You must specify a key type (-t).\n");
1019 type = key_type_from_name(key_type_name);
1020 if (type == KEY_UNSPEC) {
1021 fprintf(stderr, "unknown key type %s\n", key_type_name);
1025 printf("Generating public/private %s key pair.\n", key_type_name);
1026 private = key_generate(type, bits);
1027 if (private == NULL) {
1028 fprintf(stderr, "key_generate failed");
1031 public = key_from_private(private);
1034 ask_filename(pw, "Enter file in which to save the key");
1036 /* Create ~/.ssh directory if it doesn\'t already exist. */
1037 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
1038 if (strstr(identity_file, dotsshdir) != NULL &&
1039 stat(dotsshdir, &st) < 0) {
1040 if (mkdir(dotsshdir, 0700) < 0)
1041 error("Could not create directory '%s'.", dotsshdir);
1043 printf("Created directory '%s'.\n", dotsshdir);
1045 /* If the file already exists, ask the user to confirm. */
1046 if (stat(identity_file, &st) >= 0) {
1048 printf("%s already exists.\n", identity_file);
1049 printf("Overwrite (y/n)? ");
1051 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1053 if (yesno[0] != 'y' && yesno[0] != 'Y')
1056 /* Ask for a passphrase (twice). */
1057 if (identity_passphrase)
1058 passphrase1 = xstrdup(identity_passphrase);
1059 else if (identity_new_passphrase)
1060 passphrase1 = xstrdup(identity_new_passphrase);
1064 read_passphrase("Enter passphrase (empty for no "
1065 "passphrase): ", RP_ALLOW_STDIN);
1066 passphrase2 = read_passphrase("Enter same passphrase again: ",
1068 if (strcmp(passphrase1, passphrase2) != 0) {
1070 * The passphrases do not match. Clear them and
1073 memset(passphrase1, 0, strlen(passphrase1));
1074 memset(passphrase2, 0, strlen(passphrase2));
1077 printf("Passphrases do not match. Try again.\n");
1078 goto passphrase_again;
1080 /* Clear the other copy of the passphrase. */
1081 memset(passphrase2, 0, strlen(passphrase2));
1085 if (identity_comment) {
1086 strlcpy(comment, identity_comment, sizeof(comment));
1088 /* Create default commend field for the passphrase. */
1089 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1092 /* Save the key with the given passphrase and comment. */
1093 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1094 printf("Saving the key failed: %s.\n", identity_file);
1095 memset(passphrase1, 0, strlen(passphrase1));
1099 /* Clear the passphrase. */
1100 memset(passphrase1, 0, strlen(passphrase1));
1103 /* Clear the private key and the random number generator. */
1108 printf("Your identification has been saved in %s.\n", identity_file);
1110 strlcat(identity_file, ".pub", sizeof(identity_file));
1111 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1113 printf("Could not save your public key in %s\n", identity_file);
1116 f = fdopen(fd, "w");
1118 printf("fdopen %s failed", identity_file);
1121 if (!key_write(public, f))
1122 fprintf(stderr, "write key failed");
1123 fprintf(f, " %s\n", comment);
1127 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1128 printf("Your public key has been saved in %s.\n",
1130 printf("The key fingerprint is:\n");
1131 printf("%s %s\n", fp, comment);