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.88 2001/12/27 18:10:29 markus Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
27 #include "pathnames.h"
33 #include <openssl/engine.h>
37 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
41 * Flag indicating that we just want to change the passphrase. This can be
42 * set on the command line.
44 int change_passphrase = 0;
47 * Flag indicating that we just want to change the comment. This can be set
48 * on the command line.
50 int change_comment = 0;
54 /* Flag indicating that we just want to see the key fingerprint */
55 int print_fingerprint = 0;
56 int print_bubblebabble = 0;
58 /* The identity file name, given on the command line or entered by the user. */
59 char identity_file[1024];
60 int have_identity = 0;
62 /* This is set to the passphrase if given on the command line. */
63 char *identity_passphrase = NULL;
65 /* This is set to the new passphrase if given on the command line. */
66 char *identity_new_passphrase = NULL;
68 /* This is set to the new comment if given on the command line. */
69 char *identity_comment = NULL;
71 /* Dump public key file in format used by real and the original SSH 2 */
72 int convert_to_ssh2 = 0;
73 int convert_from_ssh2 = 0;
76 char *key_type_name = NULL;
79 #ifdef HAVE___PROGNAME
80 extern char *__progname;
85 char hostname[MAXHOSTNAMELEN];
88 ask_filename(struct passwd *pw, const char *prompt)
93 switch (key_type_from_name(key_type_name)) {
95 name = _PATH_SSH_CLIENT_IDENTITY;
98 name = _PATH_SSH_CLIENT_ID_DSA;
101 name = _PATH_SSH_CLIENT_ID_RSA;
104 fprintf(stderr, "bad key type");
108 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
109 fprintf(stderr, "%s (%s): ", prompt, identity_file);
111 if (fgets(buf, sizeof(buf), stdin) == NULL)
113 if (strchr(buf, '\n'))
114 *strchr(buf, '\n') = 0;
115 if (strcmp(buf, "") != 0)
116 strlcpy(identity_file, buf, sizeof(identity_file));
121 load_identity(char *filename)
126 prv = key_load_private(filename, "", NULL);
128 if (identity_passphrase)
129 pass = xstrdup(identity_passphrase);
131 pass = read_passphrase("Enter passphrase: ",
133 prv = key_load_private(filename, pass, NULL);
134 memset(pass, 0, strlen(pass));
140 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
141 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
142 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
143 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
146 do_convert_to_ssh2(struct passwd *pw)
154 ask_filename(pw, "Enter file in which the key is");
155 if (stat(identity_file, &st) < 0) {
156 perror(identity_file);
159 if ((k = key_load_public(identity_file, NULL)) == NULL) {
160 if ((k = load_identity(identity_file)) == NULL) {
161 fprintf(stderr, "load failed\n");
165 if (key_to_blob(k, &blob, &len) <= 0) {
166 fprintf(stderr, "key_to_blob failed\n");
169 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
171 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
172 key_size(k), key_type(k),
173 pw->pw_name, hostname);
174 dump_base64(stdout, blob, len);
175 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
182 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
184 int bits = buffer_get_int(b);
185 int bytes = (bits + 7) / 8;
187 if (buffer_len(b) < bytes)
188 fatal("buffer_get_bignum_bits: input buffer too small: "
189 "need %d have %d", bytes, buffer_len(b));
190 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
191 buffer_consume(b, bytes);
195 do_convert_private_ssh2_from_blob(u_char *blob, int blen)
200 u_char *sig, data[] = "abcde12345";
201 int magic, rlen, ktype, i1, i2, i3, i4;
206 buffer_append(&b, blob, blen);
208 magic = buffer_get_int(&b);
209 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
210 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
214 i1 = buffer_get_int(&b);
215 type = buffer_get_string(&b, NULL);
216 cipher = buffer_get_string(&b, NULL);
217 i2 = buffer_get_int(&b);
218 i3 = buffer_get_int(&b);
219 i4 = buffer_get_int(&b);
220 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
221 if (strcmp(cipher, "none") != 0) {
222 error("unsupported cipher %s", cipher);
230 if (strstr(type, "dsa")) {
232 } else if (strstr(type, "rsa")) {
238 key = key_new_private(ktype);
243 buffer_get_bignum_bits(&b, key->dsa->p);
244 buffer_get_bignum_bits(&b, key->dsa->g);
245 buffer_get_bignum_bits(&b, key->dsa->q);
246 buffer_get_bignum_bits(&b, key->dsa->pub_key);
247 buffer_get_bignum_bits(&b, key->dsa->priv_key);
250 e = buffer_get_char(&b);
254 e += buffer_get_char(&b);
257 e += buffer_get_char(&b);
260 if (!BN_set_word(key->rsa->e, e)) {
265 buffer_get_bignum_bits(&b, key->rsa->d);
266 buffer_get_bignum_bits(&b, key->rsa->n);
267 buffer_get_bignum_bits(&b, key->rsa->iqmp);
268 buffer_get_bignum_bits(&b, key->rsa->q);
269 buffer_get_bignum_bits(&b, key->rsa->p);
270 rsa_generate_additional_parameters(key->rsa);
273 rlen = buffer_len(&b);
275 error("do_convert_private_ssh2_from_blob: "
276 "remaining bytes in key blob %d", rlen);
280 key_sign(key, &sig, &slen, data, sizeof(data));
281 key_verify(key, sig, slen, data, sizeof(data));
287 do_convert_from_ssh2(struct passwd *pw)
295 int escaped = 0, private = 0, ok;
299 ask_filename(pw, "Enter file in which the key is");
300 if (stat(identity_file, &st) < 0) {
301 perror(identity_file);
304 fp = fopen(identity_file, "r");
306 perror(identity_file);
310 while (fgets(line, sizeof(line), fp)) {
311 if (!(p = strchr(line, '\n'))) {
312 fprintf(stderr, "input line too long.\n");
315 if (p > line && p[-1] == '\\')
317 if (strncmp(line, "----", 4) == 0 ||
318 strstr(line, ": ") != NULL) {
319 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
321 if (strstr(line, " END ") != NULL) {
324 /* fprintf(stderr, "ignore: %s", line); */
329 /* fprintf(stderr, "escaped: %s", line); */
333 strlcat(encoded, line, sizeof(encoded));
335 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
337 fprintf(stderr, "uudecode failed.\n");
341 do_convert_private_ssh2_from_blob(blob, blen) :
342 key_from_blob(blob, blen);
344 fprintf(stderr, "decode blob failed.\n");
348 (k->type == KEY_DSA ?
349 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
350 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
351 key_write(k, stdout);
353 fprintf(stderr, "key write failed");
357 fprintf(stdout, "\n");
363 do_print_public(struct passwd *pw)
369 ask_filename(pw, "Enter file in which the key is");
370 if (stat(identity_file, &st) < 0) {
371 perror(identity_file);
374 prv = load_identity(identity_file);
376 fprintf(stderr, "load failed\n");
379 if (!key_write(prv, stdout))
380 fprintf(stderr, "key_write failed");
382 fprintf(stdout, "\n");
387 #define NUM_RSA_KEY_ELEMENTS 5+1
388 #define COPY_RSA_KEY(x, i) \
390 len = BN_num_bytes(prv->rsa->x); \
391 elements[i] = xmalloc(len); \
392 debug("#bytes %d", len); \
393 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
400 EVP_MD *evp_md = EVP_sha1();
404 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
407 EVP_DigestInit(&md, evp_md);
408 EVP_DigestUpdate(&md, pass, strlen(pass));
409 EVP_DigestFinal(&md, aut0, NULL);
410 memset(pass, 0, strlen(pass));
416 do_upload(struct passwd *pw, const char *sc_reader_id)
420 u_char *elements[NUM_RSA_KEY_ELEMENTS];
422 u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
423 u_char AUT0[EVP_MAX_MD_SIZE];
424 int len, status = 1, i, fd = -1, ret;
425 int sw = 0, cla = 0x00;
427 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
430 ask_filename(pw, "Enter file in which the key is");
431 if (stat(identity_file, &st) < 0) {
432 perror(identity_file);
435 prv = load_identity(identity_file);
437 error("load failed");
442 COPY_RSA_KEY(iqmp, 2);
443 COPY_RSA_KEY(dmq1, 3);
444 COPY_RSA_KEY(dmp1, 4);
446 len = BN_num_bytes(prv->rsa->n);
447 fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
449 error("sectok_open failed: %s", sectok_get_sw(sw));
452 if (! sectok_cardpresent(fd)) {
453 error("smartcard in reader %s not present",
457 ret = sectok_reset(fd, 0, NULL, &sw);
459 error("sectok_reset failed: %s", sectok_get_sw(sw));
462 if ((cla = cyberflex_inq_class(fd)) < 0) {
463 error("cyberflex_inq_class failed");
466 memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
467 if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
468 if (get_AUT0(AUT0) < 0 ||
469 cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
470 error("cyberflex_verify_AUT0 failed");
476 if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
478 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
481 if (!sectok_swOK(sw))
483 log("cyberflex_load_rsa_priv done");
486 if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
488 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
491 if (!sectok_swOK(sw))
493 log("cyberflex_load_rsa_pub done");
495 log("loading key done");
498 memset(elements[0], '\0', BN_num_bytes(prv->rsa->q));
499 memset(elements[1], '\0', BN_num_bytes(prv->rsa->p));
500 memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp));
501 memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1));
502 memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1));
503 memset(elements[5], '\0', BN_num_bytes(prv->rsa->n));
507 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
516 do_download(struct passwd *pw, const char *sc_reader_id)
520 pub = sc_get_key(sc_reader_id);
522 fatal("cannot read public key from smartcard");
523 key_write(pub, stdout);
525 fprintf(stdout, "\n");
528 #endif /* SMARTCARD */
531 do_fingerprint(struct passwd *pw)
535 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
536 int i, skip = 0, num = 1, invalid = 1;
541 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
542 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
545 ask_filename(pw, "Enter file in which the key is");
546 if (stat(identity_file, &st) < 0) {
547 perror(identity_file);
550 public = key_load_public(identity_file, &comment);
551 if (public != NULL) {
552 fp = key_fingerprint(public, fptype, rep);
553 printf("%d %s %s\n", key_size(public), fp, comment);
562 f = fopen(identity_file, "r");
564 while (fgets(line, sizeof(line), f)) {
565 i = strlen(line) - 1;
566 if (line[i] != '\n') {
567 error("line %d too long: %.40s...", num, line);
578 /* Skip leading whitespace, empty and comment lines. */
579 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
581 if (!*cp || *cp == '\n' || *cp == '#')
583 i = strtol(cp, &ep, 10);
584 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
587 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
588 if (*cp == '\\' && cp[1] == '"')
589 cp++; /* Skip both */
598 public = key_new(KEY_RSA1);
599 if (key_read(public, &cp) != 1) {
602 public = key_new(KEY_UNSPEC);
603 if (key_read(public, &cp) != 1) {
608 comment = *cp ? cp : comment;
609 fp = key_fingerprint(public, fptype, rep);
610 printf("%d %s %s\n", key_size(public), fp,
611 comment ? comment : "no comment");
619 printf("%s is not a public key file.\n", identity_file);
626 * Perform changing a passphrase. The argument is the passwd structure
627 * for the current user.
630 do_change_passphrase(struct passwd *pw)
633 char *old_passphrase, *passphrase1, *passphrase2;
638 ask_filename(pw, "Enter file in which the key is");
639 if (stat(identity_file, &st) < 0) {
640 perror(identity_file);
643 /* Try to load the file with empty passphrase. */
644 private = key_load_private(identity_file, "", &comment);
645 if (private == NULL) {
646 if (identity_passphrase)
647 old_passphrase = xstrdup(identity_passphrase);
650 read_passphrase("Enter old passphrase: ",
652 private = key_load_private(identity_file, old_passphrase,
654 memset(old_passphrase, 0, strlen(old_passphrase));
655 xfree(old_passphrase);
656 if (private == NULL) {
657 printf("Bad passphrase.\n");
661 printf("Key has comment '%s'\n", comment);
663 /* Ask the new passphrase (twice). */
664 if (identity_new_passphrase) {
665 passphrase1 = xstrdup(identity_new_passphrase);
669 read_passphrase("Enter new passphrase (empty for no "
670 "passphrase): ", RP_ALLOW_STDIN);
671 passphrase2 = read_passphrase("Enter same passphrase again: ",
674 /* Verify that they are the same. */
675 if (strcmp(passphrase1, passphrase2) != 0) {
676 memset(passphrase1, 0, strlen(passphrase1));
677 memset(passphrase2, 0, strlen(passphrase2));
680 printf("Pass phrases do not match. Try again.\n");
683 /* Destroy the other copy. */
684 memset(passphrase2, 0, strlen(passphrase2));
688 /* Save the file using the new passphrase. */
689 if (!key_save_private(private, identity_file, passphrase1, comment)) {
690 printf("Saving the key failed: %s.\n", identity_file);
691 memset(passphrase1, 0, strlen(passphrase1));
697 /* Destroy the passphrase and the copy of the key in memory. */
698 memset(passphrase1, 0, strlen(passphrase1));
700 key_free(private); /* Destroys contents */
703 printf("Your identification has been saved with the new passphrase.\n");
708 * Change the comment of a private key file.
711 do_change_comment(struct passwd *pw)
713 char new_comment[1024], *comment, *passphrase;
721 ask_filename(pw, "Enter file in which the key is");
722 if (stat(identity_file, &st) < 0) {
723 perror(identity_file);
726 private = key_load_private(identity_file, "", &comment);
727 if (private == NULL) {
728 if (identity_passphrase)
729 passphrase = xstrdup(identity_passphrase);
730 else if (identity_new_passphrase)
731 passphrase = xstrdup(identity_new_passphrase);
733 passphrase = read_passphrase("Enter passphrase: ",
735 /* Try to load using the passphrase. */
736 private = key_load_private(identity_file, passphrase, &comment);
737 if (private == NULL) {
738 memset(passphrase, 0, strlen(passphrase));
740 printf("Bad passphrase.\n");
744 passphrase = xstrdup("");
746 if (private->type != KEY_RSA1) {
747 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
751 printf("Key now has comment '%s'\n", comment);
753 if (identity_comment) {
754 strlcpy(new_comment, identity_comment, sizeof(new_comment));
756 printf("Enter new comment: ");
758 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
759 memset(passphrase, 0, strlen(passphrase));
763 if (strchr(new_comment, '\n'))
764 *strchr(new_comment, '\n') = 0;
767 /* Save the file using the new passphrase. */
768 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
769 printf("Saving the key failed: %s.\n", identity_file);
770 memset(passphrase, 0, strlen(passphrase));
776 memset(passphrase, 0, strlen(passphrase));
778 public = key_from_private(private);
781 strlcat(identity_file, ".pub", sizeof(identity_file));
782 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
784 printf("Could not save your public key in %s\n", identity_file);
789 printf("fdopen %s failed", identity_file);
792 if (!key_write(public, f))
793 fprintf(stderr, "write key failed");
795 fprintf(f, " %s\n", new_comment);
800 printf("The comment in your key file has been changed.\n");
807 fprintf(stderr, "Usage: %s [options]\n", __progname);
808 fprintf(stderr, "Options:\n");
809 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
810 fprintf(stderr, " -c Change comment in private and public key files.\n");
811 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
812 fprintf(stderr, " -f filename Filename of the key file.\n");
813 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
814 fprintf(stderr, " -l Show fingerprint of key file.\n");
815 fprintf(stderr, " -p Change passphrase of private key file.\n");
816 fprintf(stderr, " -q Quiet.\n");
817 fprintf(stderr, " -y Read private key file and print public key.\n");
818 fprintf(stderr, " -t type Specify type of key to create.\n");
819 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
820 fprintf(stderr, " -C comment Provide new comment.\n");
821 fprintf(stderr, " -N phrase Provide new passphrase.\n");
822 fprintf(stderr, " -P phrase Provide old passphrase.\n");
824 fprintf(stderr, " -D reader Download public key from smartcard.\n");
825 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
826 #endif /* SMARTCARD */
832 * Main program for key management.
835 main(int ac, char **av)
837 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
838 char *reader_id = NULL;
839 Key *private, *public;
842 int opt, type, fd, download = 0;
848 __progname = get_progname(av[0]);
852 SSLeay_add_all_algorithms();
854 /* we need this for the home * directory. */
855 pw = getpwuid(getuid());
857 printf("You don't exist, go away!\n");
860 if (gethostname(hostname, sizeof(hostname)) < 0) {
861 perror("gethostname");
865 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
869 if (bits < 512 || bits > 32768) {
870 printf("Bits has bad value.\n");
875 print_fingerprint = 1;
878 print_bubblebabble = 1;
881 change_passphrase = 1;
887 strlcpy(identity_file, optarg, sizeof(identity_file));
891 identity_passphrase = optarg;
894 identity_new_passphrase = optarg;
897 identity_comment = optarg;
914 convert_from_ssh2 = 1;
920 key_type_name = "dsa";
923 key_type_name = optarg;
936 printf("Too many arguments.\n");
939 if (change_passphrase && change_comment) {
940 printf("Can only have one of -p and -c.\n");
943 if (print_fingerprint || print_bubblebabble)
945 if (change_passphrase)
946 do_change_passphrase(pw);
948 do_change_comment(pw);
950 do_convert_to_ssh2(pw);
951 if (convert_from_ssh2)
952 do_convert_from_ssh2(pw);
955 if (reader_id != NULL) {
958 do_download(pw, reader_id);
960 do_upload(pw, reader_id);
961 #else /* SMARTCARD */
962 fatal("no support for smartcards.");
963 #endif /* SMARTCARD */
968 if (key_type_name == NULL) {
969 printf("You must specify a key type (-t).\n");
972 type = key_type_from_name(key_type_name);
973 if (type == KEY_UNSPEC) {
974 fprintf(stderr, "unknown key type %s\n", key_type_name);
978 printf("Generating public/private %s key pair.\n", key_type_name);
979 private = key_generate(type, bits);
980 if (private == NULL) {
981 fprintf(stderr, "key_generate failed");
984 public = key_from_private(private);
987 ask_filename(pw, "Enter file in which to save the key");
989 /* Create ~/.ssh directory if it doesn\'t already exist. */
990 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
991 if (strstr(identity_file, dotsshdir) != NULL &&
992 stat(dotsshdir, &st) < 0) {
993 if (mkdir(dotsshdir, 0700) < 0)
994 error("Could not create directory '%s'.", dotsshdir);
996 printf("Created directory '%s'.\n", dotsshdir);
998 /* If the file already exists, ask the user to confirm. */
999 if (stat(identity_file, &st) >= 0) {
1001 printf("%s already exists.\n", identity_file);
1002 printf("Overwrite (y/n)? ");
1004 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1006 if (yesno[0] != 'y' && yesno[0] != 'Y')
1009 /* Ask for a passphrase (twice). */
1010 if (identity_passphrase)
1011 passphrase1 = xstrdup(identity_passphrase);
1012 else if (identity_new_passphrase)
1013 passphrase1 = xstrdup(identity_new_passphrase);
1017 read_passphrase("Enter passphrase (empty for no "
1018 "passphrase): ", RP_ALLOW_STDIN);
1019 passphrase2 = read_passphrase("Enter same passphrase again: ",
1021 if (strcmp(passphrase1, passphrase2) != 0) {
1023 * The passphrases do not match. Clear them and
1026 memset(passphrase1, 0, strlen(passphrase1));
1027 memset(passphrase2, 0, strlen(passphrase2));
1030 printf("Passphrases do not match. Try again.\n");
1031 goto passphrase_again;
1033 /* Clear the other copy of the passphrase. */
1034 memset(passphrase2, 0, strlen(passphrase2));
1038 if (identity_comment) {
1039 strlcpy(comment, identity_comment, sizeof(comment));
1041 /* Create default commend field for the passphrase. */
1042 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1045 /* Save the key with the given passphrase and comment. */
1046 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1047 printf("Saving the key failed: %s.\n", identity_file);
1048 memset(passphrase1, 0, strlen(passphrase1));
1052 /* Clear the passphrase. */
1053 memset(passphrase1, 0, strlen(passphrase1));
1056 /* Clear the private key and the random number generator. */
1061 printf("Your identification has been saved in %s.\n", identity_file);
1063 strlcat(identity_file, ".pub", sizeof(identity_file));
1064 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1066 printf("Could not save your public key in %s\n", identity_file);
1069 f = fdopen(fd, "w");
1071 printf("fdopen %s failed", identity_file);
1074 if (!key_write(public, f))
1075 fprintf(stderr, "write key failed");
1076 fprintf(f, " %s\n", comment);
1080 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1081 printf("Your public key has been saved in %s.\n",
1083 printf("The key fingerprint is:\n");
1084 printf("%s %s\n", fp, comment);