1 /* $OpenBSD: ssh-keygen.c,v 1.147 2006/07/09 15:15:11 stevesk Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Identity and host key generation and maintenance.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
17 #include <sys/types.h>
18 #include <sys/socket.h>
21 #include <openssl/evp.h>
22 #include <openssl/pem.h>
37 #include "pathnames.h"
48 /* Number of bits in the RSA/DSA key. This value can be set on the command line. */
49 #define DEFAULT_BITS 2048
50 #define DEFAULT_BITS_DSA 1024
54 * Flag indicating that we just want to change the passphrase. This can be
55 * set on the command line.
57 int change_passphrase = 0;
60 * Flag indicating that we just want to change the comment. This can be set
61 * on the command line.
63 int change_comment = 0;
67 /* Flag indicating that we want to hash a known_hosts file */
69 /* Flag indicating that we want lookup a host in known_hosts file */
71 /* Flag indicating that we want to delete a host from a known_hosts file */
74 /* Flag indicating that we just want to see the key fingerprint */
75 int print_fingerprint = 0;
76 int print_bubblebabble = 0;
78 /* The identity file name, given on the command line or entered by the user. */
79 char identity_file[1024];
80 int have_identity = 0;
82 /* This is set to the passphrase if given on the command line. */
83 char *identity_passphrase = NULL;
85 /* This is set to the new passphrase if given on the command line. */
86 char *identity_new_passphrase = NULL;
88 /* This is set to the new comment if given on the command line. */
89 char *identity_comment = NULL;
91 /* Dump public key file in format used by real and the original SSH 2 */
92 int convert_to_ssh2 = 0;
93 int convert_from_ssh2 = 0;
95 int print_generic = 0;
97 char *key_type_name = NULL;
100 extern char *__progname;
102 char hostname[MAXHOSTNAMELEN];
105 int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
106 int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
109 ask_filename(struct passwd *pw, const char *prompt)
114 if (key_type_name == NULL)
115 name = _PATH_SSH_CLIENT_ID_RSA;
117 switch (key_type_from_name(key_type_name)) {
119 name = _PATH_SSH_CLIENT_IDENTITY;
122 name = _PATH_SSH_CLIENT_ID_DSA;
125 name = _PATH_SSH_CLIENT_ID_RSA;
128 fprintf(stderr, "bad key type");
133 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
134 fprintf(stderr, "%s (%s): ", prompt, identity_file);
135 if (fgets(buf, sizeof(buf), stdin) == NULL)
137 if (strchr(buf, '\n'))
138 *strchr(buf, '\n') = 0;
139 if (strcmp(buf, "") != 0)
140 strlcpy(identity_file, buf, sizeof(identity_file));
145 load_identity(char *filename)
150 prv = key_load_private(filename, "", NULL);
152 if (identity_passphrase)
153 pass = xstrdup(identity_passphrase);
155 pass = read_passphrase("Enter passphrase: ",
157 prv = key_load_private(filename, pass, NULL);
158 memset(pass, 0, strlen(pass));
164 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
165 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
166 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
167 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
170 do_convert_to_ssh2(struct passwd *pw)
178 ask_filename(pw, "Enter file in which the key is");
179 if (stat(identity_file, &st) < 0) {
180 perror(identity_file);
183 if ((k = key_load_public(identity_file, NULL)) == NULL) {
184 if ((k = load_identity(identity_file)) == NULL) {
185 fprintf(stderr, "load failed\n");
189 if (k->type == KEY_RSA1) {
190 fprintf(stderr, "version 1 keys are not supported\n");
193 if (key_to_blob(k, &blob, &len) <= 0) {
194 fprintf(stderr, "key_to_blob failed\n");
197 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
199 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
200 key_size(k), key_type(k),
201 pw->pw_name, hostname);
202 dump_base64(stdout, blob, len);
203 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
210 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
212 u_int bignum_bits = buffer_get_int(b);
213 u_int bytes = (bignum_bits + 7) / 8;
215 if (buffer_len(b) < bytes)
216 fatal("buffer_get_bignum_bits: input buffer too small: "
217 "need %d have %d", bytes, buffer_len(b));
218 BN_bin2bn(buffer_ptr(b), bytes, value);
219 buffer_consume(b, bytes);
223 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
228 u_char *sig, data[] = "abcde12345";
229 int magic, rlen, ktype, i1, i2, i3, i4;
234 buffer_append(&b, blob, blen);
236 magic = buffer_get_int(&b);
237 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
238 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
242 i1 = buffer_get_int(&b);
243 type = buffer_get_string(&b, NULL);
244 cipher = buffer_get_string(&b, NULL);
245 i2 = buffer_get_int(&b);
246 i3 = buffer_get_int(&b);
247 i4 = buffer_get_int(&b);
248 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
249 if (strcmp(cipher, "none") != 0) {
250 error("unsupported cipher %s", cipher);
258 if (strstr(type, "dsa")) {
260 } else if (strstr(type, "rsa")) {
267 key = key_new_private(ktype);
272 buffer_get_bignum_bits(&b, key->dsa->p);
273 buffer_get_bignum_bits(&b, key->dsa->g);
274 buffer_get_bignum_bits(&b, key->dsa->q);
275 buffer_get_bignum_bits(&b, key->dsa->pub_key);
276 buffer_get_bignum_bits(&b, key->dsa->priv_key);
279 e = buffer_get_char(&b);
283 e += buffer_get_char(&b);
286 e += buffer_get_char(&b);
289 if (!BN_set_word(key->rsa->e, e)) {
294 buffer_get_bignum_bits(&b, key->rsa->d);
295 buffer_get_bignum_bits(&b, key->rsa->n);
296 buffer_get_bignum_bits(&b, key->rsa->iqmp);
297 buffer_get_bignum_bits(&b, key->rsa->q);
298 buffer_get_bignum_bits(&b, key->rsa->p);
299 rsa_generate_additional_parameters(key->rsa);
302 rlen = buffer_len(&b);
304 error("do_convert_private_ssh2_from_blob: "
305 "remaining bytes in key blob %d", rlen);
309 key_sign(key, &sig, &slen, data, sizeof(data));
310 key_verify(key, sig, slen, data, sizeof(data));
316 get_line(FILE *fp, char *line, size_t len)
322 while ((c = fgetc(fp)) != EOF) {
323 if (pos >= len - 1) {
324 fprintf(stderr, "input line too long.\n");
330 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
331 fprintf(stderr, "unget: %s\n", strerror(errno));
347 do_convert_from_ssh2(struct passwd *pw)
356 int escaped = 0, private = 0, ok;
360 ask_filename(pw, "Enter file in which the key is");
361 if (stat(identity_file, &st) < 0) {
362 perror(identity_file);
365 fp = fopen(identity_file, "r");
367 perror(identity_file);
371 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
372 if (line[blen - 1] == '\\')
374 if (strncmp(line, "----", 4) == 0 ||
375 strstr(line, ": ") != NULL) {
376 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
378 if (strstr(line, " END ") != NULL) {
381 /* fprintf(stderr, "ignore: %s", line); */
386 /* fprintf(stderr, "escaped: %s", line); */
389 strlcat(encoded, line, sizeof(encoded));
391 len = strlen(encoded);
392 if (((len % 4) == 3) &&
393 (encoded[len-1] == '=') &&
394 (encoded[len-2] == '=') &&
395 (encoded[len-3] == '='))
396 encoded[len-3] = '\0';
397 blen = uudecode(encoded, blob, sizeof(blob));
399 fprintf(stderr, "uudecode failed.\n");
403 do_convert_private_ssh2_from_blob(blob, blen) :
404 key_from_blob(blob, blen);
406 fprintf(stderr, "decode blob failed.\n");
410 (k->type == KEY_DSA ?
411 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
412 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
413 key_write(k, stdout);
415 fprintf(stderr, "key write failed");
420 fprintf(stdout, "\n");
426 do_print_public(struct passwd *pw)
432 ask_filename(pw, "Enter file in which the key is");
433 if (stat(identity_file, &st) < 0) {
434 perror(identity_file);
437 prv = load_identity(identity_file);
439 fprintf(stderr, "load failed\n");
442 if (!key_write(prv, stdout))
443 fprintf(stderr, "key_write failed");
445 fprintf(stdout, "\n");
451 do_upload(struct passwd *pw, const char *sc_reader_id)
458 ask_filename(pw, "Enter file in which the key is");
459 if (stat(identity_file, &st) < 0) {
460 perror(identity_file);
463 prv = load_identity(identity_file);
465 error("load failed");
468 ret = sc_put_key(prv, sc_reader_id);
472 logit("loading key done");
477 do_download(struct passwd *pw, const char *sc_reader_id)
482 keys = sc_get_keys(sc_reader_id, NULL);
484 fatal("cannot read public key from smartcard");
485 for (i = 0; keys[i]; i++) {
486 key_write(keys[i], stdout);
488 fprintf(stdout, "\n");
493 #endif /* SMARTCARD */
496 do_fingerprint(struct passwd *pw)
500 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
501 int i, skip = 0, num = 1, invalid = 1;
506 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
507 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
510 ask_filename(pw, "Enter file in which the key is");
511 if (stat(identity_file, &st) < 0) {
512 perror(identity_file);
515 public = key_load_public(identity_file, &comment);
516 if (public != NULL) {
517 fp = key_fingerprint(public, fptype, rep);
518 printf("%u %s %s\n", key_size(public), fp, comment);
529 f = fopen(identity_file, "r");
531 while (fgets(line, sizeof(line), f)) {
532 i = strlen(line) - 1;
533 if (line[i] != '\n') {
534 error("line %d too long: %.40s...", num, line);
545 /* Skip leading whitespace, empty and comment lines. */
546 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
548 if (!*cp || *cp == '\n' || *cp == '#')
550 i = strtol(cp, &ep, 10);
551 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
554 for (; *cp && (quoted || (*cp != ' ' &&
555 *cp != '\t')); cp++) {
556 if (*cp == '\\' && cp[1] == '"')
557 cp++; /* Skip both */
566 public = key_new(KEY_RSA1);
567 if (key_read(public, &cp) != 1) {
570 public = key_new(KEY_UNSPEC);
571 if (key_read(public, &cp) != 1) {
576 comment = *cp ? cp : comment;
577 fp = key_fingerprint(public, fptype, rep);
578 printf("%u %s %s\n", key_size(public), fp,
579 comment ? comment : "no comment");
587 printf("%s is not a public key file.\n", identity_file);
594 print_host(FILE *f, char *name, Key *public, int hash)
596 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
597 fatal("hash_host failed");
598 fprintf(f, "%s ", name);
599 if (!key_write(public, f))
600 fatal("key_write failed");
605 do_known_hosts(struct passwd *pw, const char *name)
607 FILE *in, *out = stdout;
609 char *cp, *cp2, *kp, *kp2;
610 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
611 int c, i, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
613 if (!have_identity) {
614 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
615 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
616 sizeof(identity_file))
617 fatal("Specified known hosts path too long");
621 if ((in = fopen(identity_file, "r")) == NULL)
622 fatal("fopen: %s", strerror(errno));
625 * Find hosts goes to stdout, hash and deletions happen in-place
626 * A corner case is ssh-keygen -HF foo, which should go to stdout
628 if (!find_host && (hash_hosts || delete_host)) {
629 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
630 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
631 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
632 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
633 fatal("known_hosts path too long");
635 if ((c = mkstemp(tmp)) == -1)
636 fatal("mkstemp: %s", strerror(errno));
637 if ((out = fdopen(c, "w")) == NULL) {
640 fatal("fdopen: %s", strerror(c));
645 while (fgets(line, sizeof(line), in)) {
647 i = strlen(line) - 1;
648 if (line[i] != '\n') {
649 error("line %d too long: %.40s...", num, line);
660 /* Skip leading whitespace, empty and comment lines. */
661 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
663 if (!*cp || *cp == '\n' || *cp == '#') {
665 fprintf(out, "%s\n", cp);
668 /* Find the end of the host name portion. */
669 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
671 if (*kp == '\0' || *(kp + 1) == '\0') {
672 error("line %d missing key: %.40s...",
680 public = key_new(KEY_RSA1);
681 if (key_read(public, &kp) != 1) {
684 public = key_new(KEY_UNSPEC);
685 if (key_read(public, &kp) != 1) {
686 error("line %d invalid key: %.40s...",
694 if (*cp == HASH_DELIM) {
695 if (find_host || delete_host) {
696 cp2 = host_hash(name, cp, strlen(cp));
698 error("line %d: invalid hashed "
699 "name: %.64s...", num, line);
703 c = (strcmp(cp2, cp) == 0);
704 if (find_host && c) {
705 printf("# Host %s found: "
706 "line %d type %s\n", name,
707 num, key_type(public));
708 print_host(out, cp, public, 0);
710 if (delete_host && !c)
711 print_host(out, cp, public, 0);
712 } else if (hash_hosts)
713 print_host(out, cp, public, 0);
715 if (find_host || delete_host) {
716 c = (match_hostname(name, cp,
718 if (find_host && c) {
719 printf("# Host %s found: "
720 "line %d type %s\n", name,
721 num, key_type(public));
722 print_host(out, cp, public, hash_hosts);
724 if (delete_host && !c)
725 print_host(out, cp, public, 0);
726 } else if (hash_hosts) {
727 for (cp2 = strsep(&cp, ",");
728 cp2 != NULL && *cp2 != '\0';
729 cp2 = strsep(&cp, ",")) {
730 if (strcspn(cp2, "*?!") != strlen(cp2))
731 fprintf(stderr, "Warning: "
732 "ignoring host name with "
733 "metacharacters: %.64s\n",
736 print_host(out, cp2, public, 1);
746 fprintf(stderr, "%s is not a valid known_host file.\n",
749 fprintf(stderr, "Not replacing existing known_hosts "
750 "file because of errors\n");
760 /* Backup existing file */
761 if (unlink(old) == -1 && errno != ENOENT)
762 fatal("unlink %.100s: %s", old, strerror(errno));
763 if (link(identity_file, old) == -1)
764 fatal("link %.100s to %.100s: %s", identity_file, old,
766 /* Move new one into place */
767 if (rename(tmp, identity_file) == -1) {
768 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
775 fprintf(stderr, "%s updated.\n", identity_file);
776 fprintf(stderr, "Original contents retained as %s\n", old);
778 fprintf(stderr, "WARNING: %s contains unhashed "
780 fprintf(stderr, "Delete this file to ensure privacy "
789 * Perform changing a passphrase. The argument is the passwd structure
790 * for the current user.
793 do_change_passphrase(struct passwd *pw)
796 char *old_passphrase, *passphrase1, *passphrase2;
801 ask_filename(pw, "Enter file in which the key is");
802 if (stat(identity_file, &st) < 0) {
803 perror(identity_file);
806 /* Try to load the file with empty passphrase. */
807 private = key_load_private(identity_file, "", &comment);
808 if (private == NULL) {
809 if (identity_passphrase)
810 old_passphrase = xstrdup(identity_passphrase);
813 read_passphrase("Enter old passphrase: ",
815 private = key_load_private(identity_file, old_passphrase,
817 memset(old_passphrase, 0, strlen(old_passphrase));
818 xfree(old_passphrase);
819 if (private == NULL) {
820 printf("Bad passphrase.\n");
824 printf("Key has comment '%s'\n", comment);
826 /* Ask the new passphrase (twice). */
827 if (identity_new_passphrase) {
828 passphrase1 = xstrdup(identity_new_passphrase);
832 read_passphrase("Enter new passphrase (empty for no "
833 "passphrase): ", RP_ALLOW_STDIN);
834 passphrase2 = read_passphrase("Enter same passphrase again: ",
837 /* Verify that they are the same. */
838 if (strcmp(passphrase1, passphrase2) != 0) {
839 memset(passphrase1, 0, strlen(passphrase1));
840 memset(passphrase2, 0, strlen(passphrase2));
843 printf("Pass phrases do not match. Try again.\n");
846 /* Destroy the other copy. */
847 memset(passphrase2, 0, strlen(passphrase2));
851 /* Save the file using the new passphrase. */
852 if (!key_save_private(private, identity_file, passphrase1, comment)) {
853 printf("Saving the key failed: %s.\n", identity_file);
854 memset(passphrase1, 0, strlen(passphrase1));
860 /* Destroy the passphrase and the copy of the key in memory. */
861 memset(passphrase1, 0, strlen(passphrase1));
863 key_free(private); /* Destroys contents */
866 printf("Your identification has been saved with the new passphrase.\n");
871 * Print the SSHFP RR.
874 do_print_resource_record(struct passwd *pw, char *fname, char *hname)
877 char *comment = NULL;
881 ask_filename(pw, "Enter file in which the key is");
882 if (stat(fname, &st) < 0) {
888 public = key_load_public(fname, &comment);
889 if (public != NULL) {
890 export_dns_rr(hname, public, stdout, print_generic);
898 printf("failed to read v2 public key from %s.\n", fname);
903 * Change the comment of a private key file.
906 do_change_comment(struct passwd *pw)
908 char new_comment[1024], *comment, *passphrase;
916 ask_filename(pw, "Enter file in which the key is");
917 if (stat(identity_file, &st) < 0) {
918 perror(identity_file);
921 private = key_load_private(identity_file, "", &comment);
922 if (private == NULL) {
923 if (identity_passphrase)
924 passphrase = xstrdup(identity_passphrase);
925 else if (identity_new_passphrase)
926 passphrase = xstrdup(identity_new_passphrase);
928 passphrase = read_passphrase("Enter passphrase: ",
930 /* Try to load using the passphrase. */
931 private = key_load_private(identity_file, passphrase, &comment);
932 if (private == NULL) {
933 memset(passphrase, 0, strlen(passphrase));
935 printf("Bad passphrase.\n");
939 passphrase = xstrdup("");
941 if (private->type != KEY_RSA1) {
942 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
946 printf("Key now has comment '%s'\n", comment);
948 if (identity_comment) {
949 strlcpy(new_comment, identity_comment, sizeof(new_comment));
951 printf("Enter new comment: ");
953 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
954 memset(passphrase, 0, strlen(passphrase));
958 if (strchr(new_comment, '\n'))
959 *strchr(new_comment, '\n') = 0;
962 /* Save the file using the new passphrase. */
963 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
964 printf("Saving the key failed: %s.\n", identity_file);
965 memset(passphrase, 0, strlen(passphrase));
971 memset(passphrase, 0, strlen(passphrase));
973 public = key_from_private(private);
976 strlcat(identity_file, ".pub", sizeof(identity_file));
977 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
979 printf("Could not save your public key in %s\n", identity_file);
984 printf("fdopen %s failed", identity_file);
987 if (!key_write(public, f))
988 fprintf(stderr, "write key failed");
990 fprintf(f, " %s\n", new_comment);
995 printf("The comment in your key file has been changed.\n");
1002 fprintf(stderr, "Usage: %s [options]\n", __progname);
1003 fprintf(stderr, "Options:\n");
1004 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
1005 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
1006 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
1007 fprintf(stderr, " -C comment Provide new comment.\n");
1008 fprintf(stderr, " -c Change comment in private and public key files.\n");
1010 fprintf(stderr, " -D reader Download public key from smartcard.\n");
1011 #endif /* SMARTCARD */
1012 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
1013 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1014 fprintf(stderr, " -f filename Filename of the key file.\n");
1015 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1016 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1017 fprintf(stderr, " -H Hash names in known_hosts file.\n");
1018 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
1019 fprintf(stderr, " -l Show fingerprint of key file.\n");
1020 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
1021 fprintf(stderr, " -N phrase Provide new passphrase.\n");
1022 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1023 fprintf(stderr, " -p Change passphrase of private key file.\n");
1024 fprintf(stderr, " -q Quiet.\n");
1025 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1026 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1027 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1028 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1029 fprintf(stderr, " -t type Specify type of key to create.\n");
1031 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
1032 #endif /* SMARTCARD */
1033 fprintf(stderr, " -v Verbose.\n");
1034 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1035 fprintf(stderr, " -y Read private key file and print public key.\n");
1041 * Main program for key management.
1044 main(int ac, char **av)
1046 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
1047 char out_file[MAXPATHLEN], *reader_id = NULL;
1048 char *rr_hostname = NULL;
1049 Key *private, *public;
1052 int opt, type, fd, download = 0;
1053 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
1054 int do_gen_candidates = 0, do_screen_candidates = 0;
1055 int log_level = SYSLOG_LEVEL_INFO;
1056 BIGNUM *start = NULL;
1061 extern char *optarg;
1063 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1066 __progname = ssh_get_progname(av[0]);
1068 SSLeay_add_all_algorithms();
1069 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
1074 /* we need this for the home * directory. */
1075 pw = getpwuid(getuid());
1077 printf("You don't exist, go away!\n");
1080 if (gethostname(hostname, sizeof(hostname)) < 0) {
1081 perror("gethostname");
1085 while ((opt = getopt(ac, av,
1086 "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {
1089 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
1091 fatal("Bits has bad value %s (%s)",
1096 rr_hostname = optarg;
1103 rr_hostname = optarg;
1106 print_fingerprint = 1;
1109 print_bubblebabble = 1;
1112 change_passphrase = 1;
1118 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1119 sizeof(identity_file))
1120 fatal("Identity filename too long");
1127 identity_passphrase = optarg;
1130 identity_new_passphrase = optarg;
1133 identity_comment = optarg;
1141 convert_to_ssh2 = 1;
1146 convert_from_ssh2 = 1;
1152 key_type_name = "dsa";
1155 key_type_name = optarg;
1164 if (log_level == SYSLOG_LEVEL_INFO)
1165 log_level = SYSLOG_LEVEL_DEBUG1;
1167 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
1168 log_level < SYSLOG_LEVEL_DEBUG3)
1173 rr_hostname = optarg;
1176 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1179 fatal("Desired generator has bad value: %s (%s)",
1183 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
1185 fatal("Invalid number of trials: %s (%s)",
1189 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
1191 fatal("Memory limit is %s: %s", errstr, optarg);
1195 do_gen_candidates = 1;
1196 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1198 fatal("Output filename too long");
1201 do_screen_candidates = 1;
1202 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1204 fatal("Output filename too long");
1207 /* XXX - also compare length against bits */
1208 if (BN_hex2bn(&start, optarg) == 0)
1209 fatal("Invalid start point.");
1218 log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
1221 printf("Too many arguments.\n");
1224 if (change_passphrase && change_comment) {
1225 printf("Can only have one of -p and -c.\n");
1228 if (delete_host || hash_hosts || find_host)
1229 do_known_hosts(pw, rr_hostname);
1230 if (print_fingerprint || print_bubblebabble)
1232 if (change_passphrase)
1233 do_change_passphrase(pw);
1235 do_change_comment(pw);
1236 if (convert_to_ssh2)
1237 do_convert_to_ssh2(pw);
1238 if (convert_from_ssh2)
1239 do_convert_from_ssh2(pw);
1241 do_print_public(pw);
1242 if (rr_hostname != NULL) {
1245 if (have_identity) {
1246 n = do_print_resource_record(pw,
1247 identity_file, rr_hostname);
1249 perror(identity_file);
1255 n += do_print_resource_record(pw,
1256 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1257 n += do_print_resource_record(pw,
1258 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1261 fatal("no keys found.");
1265 if (reader_id != NULL) {
1268 do_download(pw, reader_id);
1270 do_upload(pw, reader_id);
1271 #else /* SMARTCARD */
1272 fatal("no support for smartcards.");
1273 #endif /* SMARTCARD */
1276 if (do_gen_candidates) {
1277 FILE *out = fopen(out_file, "w");
1280 error("Couldn't open modulus candidate file \"%s\": %s",
1281 out_file, strerror(errno));
1285 bits = DEFAULT_BITS;
1286 if (gen_candidates(out, memory, bits, start) != 0)
1287 fatal("modulus candidate generation failed");
1292 if (do_screen_candidates) {
1294 FILE *out = fopen(out_file, "w");
1296 if (have_identity && strcmp(identity_file, "-") != 0) {
1297 if ((in = fopen(identity_file, "r")) == NULL) {
1298 fatal("Couldn't open modulus candidate "
1299 "file \"%s\": %s", identity_file,
1306 fatal("Couldn't open moduli file \"%s\": %s",
1307 out_file, strerror(errno));
1309 if (prime_test(in, out, trials, generator_wanted) != 0)
1310 fatal("modulus screening failed");
1316 if (key_type_name == NULL)
1317 key_type_name = "rsa";
1319 type = key_type_from_name(key_type_name);
1320 if (type == KEY_UNSPEC) {
1321 fprintf(stderr, "unknown key type %s\n", key_type_name);
1325 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
1326 if (type == KEY_DSA && bits != 1024)
1327 fatal("DSA keys must be 1024 bits");
1329 printf("Generating public/private %s key pair.\n", key_type_name);
1330 private = key_generate(type, bits);
1331 if (private == NULL) {
1332 fprintf(stderr, "key_generate failed");
1335 public = key_from_private(private);
1338 ask_filename(pw, "Enter file in which to save the key");
1340 /* Create ~/.ssh directory if it doesn't already exist. */
1341 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
1342 if (strstr(identity_file, dotsshdir) != NULL &&
1343 stat(dotsshdir, &st) < 0) {
1344 if (mkdir(dotsshdir, 0700) < 0)
1345 error("Could not create directory '%s'.", dotsshdir);
1347 printf("Created directory '%s'.\n", dotsshdir);
1349 /* If the file already exists, ask the user to confirm. */
1350 if (stat(identity_file, &st) >= 0) {
1352 printf("%s already exists.\n", identity_file);
1353 printf("Overwrite (y/n)? ");
1355 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1357 if (yesno[0] != 'y' && yesno[0] != 'Y')
1360 /* Ask for a passphrase (twice). */
1361 if (identity_passphrase)
1362 passphrase1 = xstrdup(identity_passphrase);
1363 else if (identity_new_passphrase)
1364 passphrase1 = xstrdup(identity_new_passphrase);
1368 read_passphrase("Enter passphrase (empty for no "
1369 "passphrase): ", RP_ALLOW_STDIN);
1370 passphrase2 = read_passphrase("Enter same passphrase again: ",
1372 if (strcmp(passphrase1, passphrase2) != 0) {
1374 * The passphrases do not match. Clear them and
1377 memset(passphrase1, 0, strlen(passphrase1));
1378 memset(passphrase2, 0, strlen(passphrase2));
1381 printf("Passphrases do not match. Try again.\n");
1382 goto passphrase_again;
1384 /* Clear the other copy of the passphrase. */
1385 memset(passphrase2, 0, strlen(passphrase2));
1389 if (identity_comment) {
1390 strlcpy(comment, identity_comment, sizeof(comment));
1392 /* Create default commend field for the passphrase. */
1393 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1396 /* Save the key with the given passphrase and comment. */
1397 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1398 printf("Saving the key failed: %s.\n", identity_file);
1399 memset(passphrase1, 0, strlen(passphrase1));
1403 /* Clear the passphrase. */
1404 memset(passphrase1, 0, strlen(passphrase1));
1407 /* Clear the private key and the random number generator. */
1412 printf("Your identification has been saved in %s.\n", identity_file);
1414 strlcat(identity_file, ".pub", sizeof(identity_file));
1415 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1417 printf("Could not save your public key in %s\n", identity_file);
1420 f = fdopen(fd, "w");
1422 printf("fdopen %s failed", identity_file);
1425 if (!key_write(public, f))
1426 fprintf(stderr, "write key failed");
1427 fprintf(f, " %s\n", comment);
1431 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1432 printf("Your public key has been saved in %s.\n",
1434 printf("The key fingerprint is:\n");
1435 printf("%s %s\n", fp, comment);