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.93 2002/02/24 19:14:59 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 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);
115 if (fgets(buf, sizeof(buf), stdin) == NULL)
117 if (strchr(buf, '\n'))
118 *strchr(buf, '\n') = 0;
119 if (strcmp(buf, "") != 0)
120 strlcpy(identity_file, buf, sizeof(identity_file));
125 load_identity(char *filename)
130 prv = key_load_private(filename, "", NULL);
132 if (identity_passphrase)
133 pass = xstrdup(identity_passphrase);
135 pass = read_passphrase("Enter passphrase: ",
137 prv = key_load_private(filename, pass, NULL);
138 memset(pass, 0, strlen(pass));
144 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
145 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
146 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
147 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
150 do_convert_to_ssh2(struct passwd *pw)
158 ask_filename(pw, "Enter file in which the key is");
159 if (stat(identity_file, &st) < 0) {
160 perror(identity_file);
163 if ((k = key_load_public(identity_file, NULL)) == NULL) {
164 if ((k = load_identity(identity_file)) == NULL) {
165 fprintf(stderr, "load failed\n");
169 if (key_to_blob(k, &blob, &len) <= 0) {
170 fprintf(stderr, "key_to_blob failed\n");
173 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
175 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
176 key_size(k), key_type(k),
177 pw->pw_name, hostname);
178 dump_base64(stdout, blob, len);
179 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
186 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
188 int bits = buffer_get_int(b);
189 int bytes = (bits + 7) / 8;
191 if (buffer_len(b) < bytes)
192 fatal("buffer_get_bignum_bits: input buffer too small: "
193 "need %d have %d", bytes, buffer_len(b));
194 BN_bin2bn(buffer_ptr(b), bytes, value);
195 buffer_consume(b, bytes);
199 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
204 u_char *sig, data[] = "abcde12345";
205 int magic, rlen, ktype, i1, i2, i3, i4;
210 buffer_append(&b, blob, blen);
212 magic = buffer_get_int(&b);
213 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
214 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
218 i1 = buffer_get_int(&b);
219 type = buffer_get_string(&b, NULL);
220 cipher = buffer_get_string(&b, NULL);
221 i2 = buffer_get_int(&b);
222 i3 = buffer_get_int(&b);
223 i4 = buffer_get_int(&b);
224 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
225 if (strcmp(cipher, "none") != 0) {
226 error("unsupported cipher %s", cipher);
234 if (strstr(type, "dsa")) {
236 } else if (strstr(type, "rsa")) {
242 key = key_new_private(ktype);
247 buffer_get_bignum_bits(&b, key->dsa->p);
248 buffer_get_bignum_bits(&b, key->dsa->g);
249 buffer_get_bignum_bits(&b, key->dsa->q);
250 buffer_get_bignum_bits(&b, key->dsa->pub_key);
251 buffer_get_bignum_bits(&b, key->dsa->priv_key);
254 e = buffer_get_char(&b);
258 e += buffer_get_char(&b);
261 e += buffer_get_char(&b);
264 if (!BN_set_word(key->rsa->e, e)) {
269 buffer_get_bignum_bits(&b, key->rsa->d);
270 buffer_get_bignum_bits(&b, key->rsa->n);
271 buffer_get_bignum_bits(&b, key->rsa->iqmp);
272 buffer_get_bignum_bits(&b, key->rsa->q);
273 buffer_get_bignum_bits(&b, key->rsa->p);
274 rsa_generate_additional_parameters(key->rsa);
277 rlen = buffer_len(&b);
279 error("do_convert_private_ssh2_from_blob: "
280 "remaining bytes in key blob %d", rlen);
284 key_sign(key, &sig, &slen, data, sizeof(data));
285 key_verify(key, sig, slen, data, sizeof(data));
291 do_convert_from_ssh2(struct passwd *pw)
299 int escaped = 0, private = 0, ok;
303 ask_filename(pw, "Enter file in which the key is");
304 if (stat(identity_file, &st) < 0) {
305 perror(identity_file);
308 fp = fopen(identity_file, "r");
310 perror(identity_file);
314 while (fgets(line, sizeof(line), fp)) {
315 if (!(p = strchr(line, '\n'))) {
316 fprintf(stderr, "input line too long.\n");
319 if (p > line && p[-1] == '\\')
321 if (strncmp(line, "----", 4) == 0 ||
322 strstr(line, ": ") != NULL) {
323 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
325 if (strstr(line, " END ") != NULL) {
328 /* fprintf(stderr, "ignore: %s", line); */
333 /* fprintf(stderr, "escaped: %s", line); */
337 strlcat(encoded, line, sizeof(encoded));
339 blen = uudecode(encoded, blob, sizeof(blob));
341 fprintf(stderr, "uudecode failed.\n");
345 do_convert_private_ssh2_from_blob(blob, blen) :
346 key_from_blob(blob, blen);
348 fprintf(stderr, "decode blob failed.\n");
352 (k->type == KEY_DSA ?
353 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
354 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
355 key_write(k, stdout);
357 fprintf(stderr, "key write failed");
362 fprintf(stdout, "\n");
368 do_print_public(struct passwd *pw)
374 ask_filename(pw, "Enter file in which the key is");
375 if (stat(identity_file, &st) < 0) {
376 perror(identity_file);
379 prv = load_identity(identity_file);
381 fprintf(stderr, "load failed\n");
384 if (!key_write(prv, stdout))
385 fprintf(stderr, "key_write failed");
387 fprintf(stdout, "\n");
392 #define NUM_RSA_KEY_ELEMENTS 5+1
393 #define COPY_RSA_KEY(x, i) \
395 len = BN_num_bytes(prv->rsa->x); \
396 elements[i] = xmalloc(len); \
397 debug("#bytes %d", len); \
398 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
405 EVP_MD *evp_md = EVP_sha1();
409 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
412 EVP_DigestInit(&md, evp_md);
413 EVP_DigestUpdate(&md, pass, strlen(pass));
414 EVP_DigestFinal(&md, aut0, NULL);
415 memset(pass, 0, strlen(pass));
421 do_upload(struct passwd *pw, const char *sc_reader_id)
425 u_char *elements[NUM_RSA_KEY_ELEMENTS];
427 u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
428 u_char AUT0[EVP_MAX_MD_SIZE];
429 int len, status = 1, i, fd = -1, ret;
430 int sw = 0, cla = 0x00;
432 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
435 ask_filename(pw, "Enter file in which the key is");
436 if (stat(identity_file, &st) < 0) {
437 perror(identity_file);
440 prv = load_identity(identity_file);
442 error("load failed");
447 COPY_RSA_KEY(iqmp, 2);
448 COPY_RSA_KEY(dmq1, 3);
449 COPY_RSA_KEY(dmp1, 4);
451 len = BN_num_bytes(prv->rsa->n);
452 fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
454 error("sectok_open failed: %s", sectok_get_sw(sw));
457 if (! sectok_cardpresent(fd)) {
458 error("smartcard in reader %s not present",
462 ret = sectok_reset(fd, 0, NULL, &sw);
464 error("sectok_reset failed: %s", sectok_get_sw(sw));
467 if ((cla = cyberflex_inq_class(fd)) < 0) {
468 error("cyberflex_inq_class failed");
471 memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
472 if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
473 if (get_AUT0(AUT0) < 0 ||
474 cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
475 error("cyberflex_verify_AUT0 failed");
481 if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
483 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
486 if (!sectok_swOK(sw))
488 log("cyberflex_load_rsa_priv done");
491 if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
493 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
496 if (!sectok_swOK(sw))
498 log("cyberflex_load_rsa_pub done");
500 log("loading key done");
503 memset(elements[0], '\0', BN_num_bytes(prv->rsa->q));
504 memset(elements[1], '\0', BN_num_bytes(prv->rsa->p));
505 memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp));
506 memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1));
507 memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1));
508 memset(elements[5], '\0', BN_num_bytes(prv->rsa->n));
512 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
521 do_download(struct passwd *pw, const char *sc_reader_id)
525 pub = sc_get_key(sc_reader_id);
527 fatal("cannot read public key from smartcard");
528 key_write(pub, stdout);
530 fprintf(stdout, "\n");
533 #endif /* SMARTCARD */
536 do_fingerprint(struct passwd *pw)
540 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
541 int i, skip = 0, num = 1, invalid = 1;
546 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
547 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
550 ask_filename(pw, "Enter file in which the key is");
551 if (stat(identity_file, &st) < 0) {
552 perror(identity_file);
555 public = key_load_public(identity_file, &comment);
556 if (public != NULL) {
557 fp = key_fingerprint(public, fptype, rep);
558 printf("%d %s %s\n", key_size(public), fp, comment);
567 f = fopen(identity_file, "r");
569 while (fgets(line, sizeof(line), f)) {
570 i = strlen(line) - 1;
571 if (line[i] != '\n') {
572 error("line %d too long: %.40s...", num, line);
583 /* Skip leading whitespace, empty and comment lines. */
584 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
586 if (!*cp || *cp == '\n' || *cp == '#')
588 i = strtol(cp, &ep, 10);
589 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
592 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
593 if (*cp == '\\' && cp[1] == '"')
594 cp++; /* Skip both */
603 public = key_new(KEY_RSA1);
604 if (key_read(public, &cp) != 1) {
607 public = key_new(KEY_UNSPEC);
608 if (key_read(public, &cp) != 1) {
613 comment = *cp ? cp : comment;
614 fp = key_fingerprint(public, fptype, rep);
615 printf("%d %s %s\n", key_size(public), fp,
616 comment ? comment : "no comment");
624 printf("%s is not a public key file.\n", identity_file);
631 * Perform changing a passphrase. The argument is the passwd structure
632 * for the current user.
635 do_change_passphrase(struct passwd *pw)
638 char *old_passphrase, *passphrase1, *passphrase2;
643 ask_filename(pw, "Enter file in which the key is");
644 if (stat(identity_file, &st) < 0) {
645 perror(identity_file);
648 /* Try to load the file with empty passphrase. */
649 private = key_load_private(identity_file, "", &comment);
650 if (private == NULL) {
651 if (identity_passphrase)
652 old_passphrase = xstrdup(identity_passphrase);
655 read_passphrase("Enter old passphrase: ",
657 private = key_load_private(identity_file, old_passphrase,
659 memset(old_passphrase, 0, strlen(old_passphrase));
660 xfree(old_passphrase);
661 if (private == NULL) {
662 printf("Bad passphrase.\n");
666 printf("Key has comment '%s'\n", comment);
668 /* Ask the new passphrase (twice). */
669 if (identity_new_passphrase) {
670 passphrase1 = xstrdup(identity_new_passphrase);
674 read_passphrase("Enter new passphrase (empty for no "
675 "passphrase): ", RP_ALLOW_STDIN);
676 passphrase2 = read_passphrase("Enter same passphrase again: ",
679 /* Verify that they are the same. */
680 if (strcmp(passphrase1, passphrase2) != 0) {
681 memset(passphrase1, 0, strlen(passphrase1));
682 memset(passphrase2, 0, strlen(passphrase2));
685 printf("Pass phrases do not match. Try again.\n");
688 /* Destroy the other copy. */
689 memset(passphrase2, 0, strlen(passphrase2));
693 /* Save the file using the new passphrase. */
694 if (!key_save_private(private, identity_file, passphrase1, comment)) {
695 printf("Saving the key failed: %s.\n", identity_file);
696 memset(passphrase1, 0, strlen(passphrase1));
702 /* Destroy the passphrase and the copy of the key in memory. */
703 memset(passphrase1, 0, strlen(passphrase1));
705 key_free(private); /* Destroys contents */
708 printf("Your identification has been saved with the new passphrase.\n");
713 * Change the comment of a private key file.
716 do_change_comment(struct passwd *pw)
718 char new_comment[1024], *comment, *passphrase;
726 ask_filename(pw, "Enter file in which the key is");
727 if (stat(identity_file, &st) < 0) {
728 perror(identity_file);
731 private = key_load_private(identity_file, "", &comment);
732 if (private == NULL) {
733 if (identity_passphrase)
734 passphrase = xstrdup(identity_passphrase);
735 else if (identity_new_passphrase)
736 passphrase = xstrdup(identity_new_passphrase);
738 passphrase = read_passphrase("Enter passphrase: ",
740 /* Try to load using the passphrase. */
741 private = key_load_private(identity_file, passphrase, &comment);
742 if (private == NULL) {
743 memset(passphrase, 0, strlen(passphrase));
745 printf("Bad passphrase.\n");
749 passphrase = xstrdup("");
751 if (private->type != KEY_RSA1) {
752 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
756 printf("Key now has comment '%s'\n", comment);
758 if (identity_comment) {
759 strlcpy(new_comment, identity_comment, sizeof(new_comment));
761 printf("Enter new comment: ");
763 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
764 memset(passphrase, 0, strlen(passphrase));
768 if (strchr(new_comment, '\n'))
769 *strchr(new_comment, '\n') = 0;
772 /* Save the file using the new passphrase. */
773 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
774 printf("Saving the key failed: %s.\n", identity_file);
775 memset(passphrase, 0, strlen(passphrase));
781 memset(passphrase, 0, strlen(passphrase));
783 public = key_from_private(private);
786 strlcat(identity_file, ".pub", sizeof(identity_file));
787 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
789 printf("Could not save your public key in %s\n", identity_file);
794 printf("fdopen %s failed", identity_file);
797 if (!key_write(public, f))
798 fprintf(stderr, "write key failed");
800 fprintf(f, " %s\n", new_comment);
805 printf("The comment in your key file has been changed.\n");
812 fprintf(stderr, "Usage: %s [options]\n", __progname);
813 fprintf(stderr, "Options:\n");
814 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
815 fprintf(stderr, " -c Change comment in private and public key files.\n");
816 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
817 fprintf(stderr, " -f filename Filename of the key file.\n");
818 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
819 fprintf(stderr, " -l Show fingerprint of key file.\n");
820 fprintf(stderr, " -p Change passphrase of private key file.\n");
821 fprintf(stderr, " -q Quiet.\n");
822 fprintf(stderr, " -y Read private key file and print public key.\n");
823 fprintf(stderr, " -t type Specify type of key to create.\n");
824 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
825 fprintf(stderr, " -C comment Provide new comment.\n");
826 fprintf(stderr, " -N phrase Provide new passphrase.\n");
827 fprintf(stderr, " -P phrase Provide old passphrase.\n");
829 fprintf(stderr, " -D reader Download public key from smartcard.\n");
830 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
831 #endif /* SMARTCARD */
837 * Main program for key management.
840 main(int ac, char **av)
842 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
843 char *reader_id = NULL;
844 Key *private, *public;
847 int opt, type, fd, download = 0;
853 __progname = get_progname(av[0]);
855 SSLeay_add_all_algorithms();
857 /* we need this for the home * directory. */
858 pw = getpwuid(getuid());
860 printf("You don't exist, go away!\n");
863 if (gethostname(hostname, sizeof(hostname)) < 0) {
864 perror("gethostname");
868 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
872 if (bits < 512 || bits > 32768) {
873 printf("Bits has bad value.\n");
878 print_fingerprint = 1;
881 print_bubblebabble = 1;
884 change_passphrase = 1;
890 strlcpy(identity_file, optarg, sizeof(identity_file));
894 identity_passphrase = optarg;
897 identity_new_passphrase = optarg;
900 identity_comment = optarg;
917 convert_from_ssh2 = 1;
923 key_type_name = "dsa";
926 key_type_name = optarg;
939 printf("Too many arguments.\n");
942 if (change_passphrase && change_comment) {
943 printf("Can only have one of -p and -c.\n");
946 if (print_fingerprint || print_bubblebabble)
948 if (change_passphrase)
949 do_change_passphrase(pw);
951 do_change_comment(pw);
953 do_convert_to_ssh2(pw);
954 if (convert_from_ssh2)
955 do_convert_from_ssh2(pw);
958 if (reader_id != NULL) {
961 do_download(pw, reader_id);
963 do_upload(pw, reader_id);
964 #else /* SMARTCARD */
965 fatal("no support for smartcards.");
966 #endif /* SMARTCARD */
973 if (key_type_name == NULL) {
974 printf("You must specify a key type (-t).\n");
977 type = key_type_from_name(key_type_name);
978 if (type == KEY_UNSPEC) {
979 fprintf(stderr, "unknown key type %s\n", key_type_name);
983 printf("Generating public/private %s key pair.\n", key_type_name);
984 private = key_generate(type, bits);
985 if (private == NULL) {
986 fprintf(stderr, "key_generate failed");
989 public = key_from_private(private);
992 ask_filename(pw, "Enter file in which to save the key");
994 /* Create ~/.ssh directory if it doesn\'t already exist. */
995 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
996 if (strstr(identity_file, dotsshdir) != NULL &&
997 stat(dotsshdir, &st) < 0) {
998 if (mkdir(dotsshdir, 0700) < 0)
999 error("Could not create directory '%s'.", dotsshdir);
1001 printf("Created directory '%s'.\n", dotsshdir);
1003 /* If the file already exists, ask the user to confirm. */
1004 if (stat(identity_file, &st) >= 0) {
1006 printf("%s already exists.\n", identity_file);
1007 printf("Overwrite (y/n)? ");
1009 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1011 if (yesno[0] != 'y' && yesno[0] != 'Y')
1014 /* Ask for a passphrase (twice). */
1015 if (identity_passphrase)
1016 passphrase1 = xstrdup(identity_passphrase);
1017 else if (identity_new_passphrase)
1018 passphrase1 = xstrdup(identity_new_passphrase);
1022 read_passphrase("Enter passphrase (empty for no "
1023 "passphrase): ", RP_ALLOW_STDIN);
1024 passphrase2 = read_passphrase("Enter same passphrase again: ",
1026 if (strcmp(passphrase1, passphrase2) != 0) {
1028 * The passphrases do not match. Clear them and
1031 memset(passphrase1, 0, strlen(passphrase1));
1032 memset(passphrase2, 0, strlen(passphrase2));
1035 printf("Passphrases do not match. Try again.\n");
1036 goto passphrase_again;
1038 /* Clear the other copy of the passphrase. */
1039 memset(passphrase2, 0, strlen(passphrase2));
1043 if (identity_comment) {
1044 strlcpy(comment, identity_comment, sizeof(comment));
1046 /* Create default commend field for the passphrase. */
1047 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1050 /* Save the key with the given passphrase and comment. */
1051 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1052 printf("Saving the key failed: %s.\n", identity_file);
1053 memset(passphrase1, 0, strlen(passphrase1));
1057 /* Clear the passphrase. */
1058 memset(passphrase1, 0, strlen(passphrase1));
1061 /* Clear the private key and the random number generator. */
1066 printf("Your identification has been saved in %s.\n", identity_file);
1068 strlcat(identity_file, ".pub", sizeof(identity_file));
1069 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1071 printf("Could not save your public key in %s\n", identity_file);
1074 f = fdopen(fd, "w");
1076 printf("fdopen %s failed", identity_file);
1079 if (!key_write(public, f))
1080 fprintf(stderr, "write key failed");
1081 fprintf(f, " %s\n", comment);
1085 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1086 printf("Your public key has been saved in %s.\n",
1088 printf("The key fingerprint is:\n");
1089 printf("%s %s\n", fp, comment);