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.83 2001/10/25 21:14:32 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 /* default to RSA for SSH-1 */
77 char *key_type_name = "rsa1";
80 #ifdef HAVE___PROGNAME
81 extern char *__progname;
86 char hostname[MAXHOSTNAMELEN];
89 ask_filename(struct passwd *pw, const char *prompt)
94 switch (key_type_from_name(key_type_name)) {
96 name = _PATH_SSH_CLIENT_IDENTITY;
99 name = _PATH_SSH_CLIENT_ID_DSA;
102 name = _PATH_SSH_CLIENT_ID_RSA;
105 fprintf(stderr, "bad key type");
109 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
110 fprintf(stderr, "%s (%s): ", prompt, identity_file);
112 if (fgets(buf, sizeof(buf), stdin) == NULL)
114 if (strchr(buf, '\n'))
115 *strchr(buf, '\n') = 0;
116 if (strcmp(buf, "") != 0)
117 strlcpy(identity_file, buf, sizeof(identity_file));
122 load_identity(char *filename)
127 prv = key_load_private(filename, "", NULL);
129 if (identity_passphrase)
130 pass = xstrdup(identity_passphrase);
132 pass = read_passphrase("Enter passphrase: ",
134 prv = key_load_private(filename, pass, NULL);
135 memset(pass, 0, strlen(pass));
141 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
142 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
143 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
144 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
147 do_convert_to_ssh2(struct passwd *pw)
155 ask_filename(pw, "Enter file in which the key is");
156 if (stat(identity_file, &st) < 0) {
157 perror(identity_file);
160 if ((k = key_load_public(identity_file, NULL)) == NULL) {
161 if ((k = load_identity(identity_file)) == NULL) {
162 fprintf(stderr, "load failed\n");
166 if (key_to_blob(k, &blob, &len) <= 0) {
167 fprintf(stderr, "key_to_blob failed\n");
170 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
172 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
173 key_size(k), key_type(k),
174 pw->pw_name, hostname);
175 dump_base64(stdout, blob, len);
176 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
183 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
185 int bits = buffer_get_int(b);
186 int bytes = (bits + 7) / 8;
188 if (buffer_len(b) < bytes)
189 fatal("buffer_get_bignum_bits: input buffer too small: "
190 "need %d have %d", bytes, buffer_len(b));
191 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
192 buffer_consume(b, bytes);
196 do_convert_private_ssh2_from_blob(u_char *blob, int blen)
201 u_char *sig, data[] = "abcde12345";
202 int magic, rlen, ktype, i1, i2, i3, i4;
207 buffer_append(&b, blob, blen);
209 magic = buffer_get_int(&b);
210 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
211 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
215 i1 = buffer_get_int(&b);
216 type = buffer_get_string(&b, NULL);
217 cipher = buffer_get_string(&b, NULL);
218 i2 = buffer_get_int(&b);
219 i3 = buffer_get_int(&b);
220 i4 = buffer_get_int(&b);
221 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
222 if (strcmp(cipher, "none") != 0) {
223 error("unsupported cipher %s", cipher);
231 if (strstr(type, "dsa")) {
233 } else if (strstr(type, "rsa")) {
239 key = key_new_private(ktype);
244 buffer_get_bignum_bits(&b, key->dsa->p);
245 buffer_get_bignum_bits(&b, key->dsa->g);
246 buffer_get_bignum_bits(&b, key->dsa->q);
247 buffer_get_bignum_bits(&b, key->dsa->pub_key);
248 buffer_get_bignum_bits(&b, key->dsa->priv_key);
251 e = buffer_get_char(&b);
255 e += buffer_get_char(&b);
258 e += buffer_get_char(&b);
261 if (!BN_set_word(key->rsa->e, e)) {
266 buffer_get_bignum_bits(&b, key->rsa->d);
267 buffer_get_bignum_bits(&b, key->rsa->n);
268 buffer_get_bignum_bits(&b, key->rsa->iqmp);
269 buffer_get_bignum_bits(&b, key->rsa->q);
270 buffer_get_bignum_bits(&b, key->rsa->p);
271 rsa_generate_additional_parameters(key->rsa);
274 rlen = buffer_len(&b);
276 error("do_convert_private_ssh2_from_blob: "
277 "remaining bytes in key blob %d", rlen);
281 key_sign(key, &sig, &slen, data, sizeof(data));
282 key_verify(key, sig, slen, data, sizeof(data));
288 do_convert_from_ssh2(struct passwd *pw)
296 int escaped = 0, private = 0, ok;
300 ask_filename(pw, "Enter file in which the key is");
301 if (stat(identity_file, &st) < 0) {
302 perror(identity_file);
305 fp = fopen(identity_file, "r");
307 perror(identity_file);
311 while (fgets(line, sizeof(line), fp)) {
312 if (!(p = strchr(line, '\n'))) {
313 fprintf(stderr, "input line too long.\n");
316 if (p > line && p[-1] == '\\')
318 if (strncmp(line, "----", 4) == 0 ||
319 strstr(line, ": ") != NULL) {
320 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
322 if (strstr(line, " END ") != NULL) {
325 /* fprintf(stderr, "ignore: %s", line); */
330 /* fprintf(stderr, "escaped: %s", line); */
334 strlcat(encoded, line, sizeof(encoded));
336 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
338 fprintf(stderr, "uudecode failed.\n");
342 do_convert_private_ssh2_from_blob(blob, blen) :
343 key_from_blob(blob, blen);
345 fprintf(stderr, "decode blob failed.\n");
349 (k->type == KEY_DSA ?
350 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
351 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
352 key_write(k, stdout);
354 fprintf(stderr, "key write failed");
358 fprintf(stdout, "\n");
364 do_print_public(struct passwd *pw)
370 ask_filename(pw, "Enter file in which the key is");
371 if (stat(identity_file, &st) < 0) {
372 perror(identity_file);
375 prv = load_identity(identity_file);
377 fprintf(stderr, "load failed\n");
380 if (!key_write(prv, stdout))
381 fprintf(stderr, "key_write failed");
383 fprintf(stdout, "\n");
388 #define NUM_RSA_KEY_ELEMENTS 5+1
389 #define COPY_RSA_KEY(x, i) \
391 len = BN_num_bytes(prv->rsa->x); \
392 elements[i] = xmalloc(len); \
393 debug("#bytes %d", len); \
394 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
401 EVP_MD *evp_md = EVP_sha1();
405 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
408 EVP_DigestInit(&md, evp_md);
409 EVP_DigestUpdate(&md, pass, strlen(pass));
410 EVP_DigestFinal(&md, aut0, NULL);
411 memset(pass, 0, strlen(pass));
417 do_upload(struct passwd *pw, const char *sc_reader_id)
421 u_char *elements[NUM_RSA_KEY_ELEMENTS];
423 u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
424 u_char AUT0[EVP_MAX_MD_SIZE];
425 int len, status = 1, i, fd = -1, ret;
426 int sw = 0, cla = 0x00;
428 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
431 ask_filename(pw, "Enter file in which the key is");
432 if (stat(identity_file, &st) < 0) {
433 perror(identity_file);
436 prv = load_identity(identity_file);
438 error("load failed");
443 COPY_RSA_KEY(iqmp, 2);
444 COPY_RSA_KEY(dmq1, 3);
445 COPY_RSA_KEY(dmp1, 4);
447 len = BN_num_bytes(prv->rsa->n);
448 fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
450 error("sectok_open failed: %s", sectok_get_sw(sw));
453 if (! sectok_cardpresent(fd)) {
454 error("smartcard in reader %s not present",
458 ret = sectok_reset(fd, 0, NULL, &sw);
460 error("sectok_reset failed: %s", sectok_get_sw(sw));
463 if ((cla = cyberflex_inq_class(fd)) < 0) {
464 error("cyberflex_inq_class failed");
467 memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
468 if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
469 if (get_AUT0(AUT0) < 0 ||
470 cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
471 error("cyberflex_verify_AUT0 failed");
477 if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
479 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
482 if (!sectok_swOK(sw))
484 log("cyberflex_load_rsa_priv done");
487 if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
489 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
492 if (!sectok_swOK(sw))
494 log("cyberflex_load_rsa_pub done");
496 log("loading key done");
499 memset(elements[0], '\0', BN_num_bytes(prv->rsa->q));
500 memset(elements[1], '\0', BN_num_bytes(prv->rsa->p));
501 memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp));
502 memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1));
503 memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1));
504 memset(elements[5], '\0', BN_num_bytes(prv->rsa->n));
508 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
517 do_download(struct passwd *pw, const char *sc_reader_id)
521 pub = sc_get_key(sc_reader_id);
523 fatal("cannot read public key from smartcard");
524 key_write(pub, stdout);
526 fprintf(stdout, "\n");
529 #endif /* SMARTCARD */
532 do_fingerprint(struct passwd *pw)
536 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
537 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
540 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
541 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
544 ask_filename(pw, "Enter file in which the key is");
545 if (stat(identity_file, &st) < 0) {
546 perror(identity_file);
549 public = key_load_public(identity_file, &comment);
550 if (public != NULL) {
551 fp = key_fingerprint(public, fptype, rep);
552 printf("%d %s %s\n", key_size(public), fp, comment);
561 f = fopen(identity_file, "r");
563 while (fgets(line, sizeof(line), f)) {
564 i = strlen(line) - 1;
565 if (line[i] != '\n') {
566 error("line %d too long: %.40s...", num, line);
577 /* Skip leading whitespace, empty and comment lines. */
578 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
580 if (!*cp || *cp == '\n' || *cp == '#')
582 i = strtol(cp, &ep, 10);
583 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
586 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
587 if (*cp == '\\' && cp[1] == '"')
588 cp++; /* Skip both */
597 public = key_new(KEY_RSA1);
598 if (key_read(public, &cp) != 1) {
601 public = key_new(KEY_UNSPEC);
602 if (key_read(public, &cp) != 1) {
607 comment = *cp ? cp : comment;
608 fp = key_fingerprint(public, fptype, rep);
609 printf("%d %s %s\n", key_size(public), fp,
610 comment ? comment : "no comment");
618 printf("%s is not a public key file.\n", identity_file);
625 * Perform changing a passphrase. The argument is the passwd structure
626 * for the current user.
629 do_change_passphrase(struct passwd *pw)
632 char *old_passphrase, *passphrase1, *passphrase2;
637 ask_filename(pw, "Enter file in which the key is");
638 if (stat(identity_file, &st) < 0) {
639 perror(identity_file);
642 /* Try to load the file with empty passphrase. */
643 private = key_load_private(identity_file, "", &comment);
644 if (private == NULL) {
645 if (identity_passphrase)
646 old_passphrase = xstrdup(identity_passphrase);
649 read_passphrase("Enter old passphrase: ",
651 private = key_load_private(identity_file, old_passphrase,
653 memset(old_passphrase, 0, strlen(old_passphrase));
654 xfree(old_passphrase);
655 if (private == NULL) {
656 printf("Bad passphrase.\n");
660 printf("Key has comment '%s'\n", comment);
662 /* Ask the new passphrase (twice). */
663 if (identity_new_passphrase) {
664 passphrase1 = xstrdup(identity_new_passphrase);
668 read_passphrase("Enter new passphrase (empty for no "
669 "passphrase): ", RP_ALLOW_STDIN);
670 passphrase2 = read_passphrase("Enter same passphrase again: ",
673 /* Verify that they are the same. */
674 if (strcmp(passphrase1, passphrase2) != 0) {
675 memset(passphrase1, 0, strlen(passphrase1));
676 memset(passphrase2, 0, strlen(passphrase2));
679 printf("Pass phrases do not match. Try again.\n");
682 /* Destroy the other copy. */
683 memset(passphrase2, 0, strlen(passphrase2));
687 /* Save the file using the new passphrase. */
688 if (!key_save_private(private, identity_file, passphrase1, comment)) {
689 printf("Saving the key failed: %s.\n", identity_file);
690 memset(passphrase1, 0, strlen(passphrase1));
696 /* Destroy the passphrase and the copy of the key in memory. */
697 memset(passphrase1, 0, strlen(passphrase1));
699 key_free(private); /* Destroys contents */
702 printf("Your identification has been saved with the new passphrase.\n");
707 * Change the comment of a private key file.
710 do_change_comment(struct passwd *pw)
712 char new_comment[1024], *comment, *passphrase;
720 ask_filename(pw, "Enter file in which the key is");
721 if (stat(identity_file, &st) < 0) {
722 perror(identity_file);
725 private = key_load_private(identity_file, "", &comment);
726 if (private == NULL) {
727 if (identity_passphrase)
728 passphrase = xstrdup(identity_passphrase);
729 else if (identity_new_passphrase)
730 passphrase = xstrdup(identity_new_passphrase);
732 passphrase = read_passphrase("Enter passphrase: ",
734 /* Try to load using the passphrase. */
735 private = key_load_private(identity_file, passphrase, &comment);
736 if (private == NULL) {
737 memset(passphrase, 0, strlen(passphrase));
739 printf("Bad passphrase.\n");
743 passphrase = xstrdup("");
745 if (private->type != KEY_RSA1) {
746 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
750 printf("Key now has comment '%s'\n", comment);
752 if (identity_comment) {
753 strlcpy(new_comment, identity_comment, sizeof(new_comment));
755 printf("Enter new comment: ");
757 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
758 memset(passphrase, 0, strlen(passphrase));
762 if (strchr(new_comment, '\n'))
763 *strchr(new_comment, '\n') = 0;
766 /* Save the file using the new passphrase. */
767 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
768 printf("Saving the key failed: %s.\n", identity_file);
769 memset(passphrase, 0, strlen(passphrase));
775 memset(passphrase, 0, strlen(passphrase));
777 public = key_from_private(private);
780 strlcat(identity_file, ".pub", sizeof(identity_file));
781 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
783 printf("Could not save your public key in %s\n", identity_file);
788 printf("fdopen %s failed", identity_file);
791 if (!key_write(public, f))
792 fprintf(stderr, "write key failed");
794 fprintf(f, " %s\n", new_comment);
799 printf("The comment in your key file has been changed.\n");
806 fprintf(stderr, "Usage: %s [options]\n", __progname);
807 fprintf(stderr, "Options:\n");
808 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
809 fprintf(stderr, " -c Change comment in private and public key files.\n");
810 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
811 fprintf(stderr, " -f filename Filename of the key file.\n");
812 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
813 fprintf(stderr, " -l Show fingerprint of key file.\n");
814 fprintf(stderr, " -p Change passphrase of private key file.\n");
815 fprintf(stderr, " -q Quiet.\n");
816 fprintf(stderr, " -y Read private key file and print public key.\n");
817 fprintf(stderr, " -t type Specify type of key to create.\n");
818 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
819 fprintf(stderr, " -C comment Provide new comment.\n");
820 fprintf(stderr, " -N phrase Provide new passphrase.\n");
821 fprintf(stderr, " -P phrase Provide old passphrase.\n");
823 fprintf(stderr, " -D reader Download public key from smartcard.\n");
824 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
825 #endif /* SMARTCARD */
831 * Main program for key management.
834 main(int ac, char **av)
836 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
837 char *reader_id = NULL;
838 Key *private, *public;
841 int opt, type, fd, download = 0;
847 __progname = get_progname(av[0]);
851 SSLeay_add_all_algorithms();
853 /* we need this for the home * directory. */
854 pw = getpwuid(getuid());
856 printf("You don't exist, go away!\n");
859 if (gethostname(hostname, sizeof(hostname)) < 0) {
860 perror("gethostname");
864 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
868 if (bits < 512 || bits > 32768) {
869 printf("Bits has bad value.\n");
874 print_fingerprint = 1;
877 print_bubblebabble = 1;
880 change_passphrase = 1;
886 strlcpy(identity_file, optarg, sizeof(identity_file));
890 identity_passphrase = optarg;
893 identity_new_passphrase = optarg;
896 identity_comment = optarg;
913 convert_from_ssh2 = 1;
919 key_type_name = "dsa";
922 key_type_name = optarg;
935 printf("Too many arguments.\n");
938 if (change_passphrase && change_comment) {
939 printf("Can only have one of -p and -c.\n");
942 if (print_fingerprint || print_bubblebabble)
944 if (change_passphrase)
945 do_change_passphrase(pw);
947 do_change_comment(pw);
949 do_convert_to_ssh2(pw);
950 if (convert_from_ssh2)
951 do_convert_from_ssh2(pw);
954 if (reader_id != NULL) {
957 do_download(pw, reader_id);
959 do_upload(pw, reader_id);
960 #else /* SMARTCARD */
961 fatal("no support for smartcards.");
962 #endif /* SMARTCARD */
967 type = key_type_from_name(key_type_name);
968 if (type == KEY_UNSPEC) {
969 fprintf(stderr, "unknown key type %s\n", key_type_name);
973 printf("Generating public/private %s key pair.\n", key_type_name);
974 private = key_generate(type, bits);
975 if (private == NULL) {
976 fprintf(stderr, "key_generate failed");
979 public = key_from_private(private);
982 ask_filename(pw, "Enter file in which to save the key");
984 /* Create ~/.ssh directory if it doesn\'t already exist. */
985 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
986 if (strstr(identity_file, dotsshdir) != NULL &&
987 stat(dotsshdir, &st) < 0) {
988 if (mkdir(dotsshdir, 0700) < 0)
989 error("Could not create directory '%s'.", dotsshdir);
991 printf("Created directory '%s'.\n", dotsshdir);
993 /* If the file already exists, ask the user to confirm. */
994 if (stat(identity_file, &st) >= 0) {
996 printf("%s already exists.\n", identity_file);
997 printf("Overwrite (y/n)? ");
999 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1001 if (yesno[0] != 'y' && yesno[0] != 'Y')
1004 /* Ask for a passphrase (twice). */
1005 if (identity_passphrase)
1006 passphrase1 = xstrdup(identity_passphrase);
1007 else if (identity_new_passphrase)
1008 passphrase1 = xstrdup(identity_new_passphrase);
1012 read_passphrase("Enter passphrase (empty for no "
1013 "passphrase): ", RP_ALLOW_STDIN);
1014 passphrase2 = read_passphrase("Enter same passphrase again: ",
1016 if (strcmp(passphrase1, passphrase2) != 0) {
1018 * The passphrases do not match. Clear them and
1021 memset(passphrase1, 0, strlen(passphrase1));
1022 memset(passphrase2, 0, strlen(passphrase2));
1025 printf("Passphrases do not match. Try again.\n");
1026 goto passphrase_again;
1028 /* Clear the other copy of the passphrase. */
1029 memset(passphrase2, 0, strlen(passphrase2));
1033 if (identity_comment) {
1034 strlcpy(comment, identity_comment, sizeof(comment));
1036 /* Create default commend field for the passphrase. */
1037 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1040 /* Save the key with the given passphrase and comment. */
1041 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1042 printf("Saving the key failed: %s.\n", identity_file);
1043 memset(passphrase1, 0, strlen(passphrase1));
1047 /* Clear the passphrase. */
1048 memset(passphrase1, 0, strlen(passphrase1));
1051 /* Clear the private key and the random number generator. */
1056 printf("Your identification has been saved in %s.\n", identity_file);
1058 strlcat(identity_file, ".pub", sizeof(identity_file));
1059 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1061 printf("Could not save your public key in %s\n", identity_file);
1064 f = fdopen(fd, "w");
1066 printf("fdopen %s failed", identity_file);
1069 if (!key_write(public, f))
1070 fprintf(stderr, "write key failed");
1071 fprintf(f, " %s\n", comment);
1075 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1076 printf("Your public key has been saved in %s.\n",
1078 printf("The key fingerprint is:\n");
1079 printf("%s %s\n", fp, comment);