1 /* $OpenBSD: ssh-keygen.c,v 1.148 2006/07/11 20:07:25 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>
38 #include "pathnames.h"
49 /* Number of bits in the RSA/DSA key. This value can be set on the command line. */
50 #define DEFAULT_BITS 2048
51 #define DEFAULT_BITS_DSA 1024
55 * Flag indicating that we just want to change the passphrase. This can be
56 * set on the command line.
58 int change_passphrase = 0;
61 * Flag indicating that we just want to change the comment. This can be set
62 * on the command line.
64 int change_comment = 0;
68 /* Flag indicating that we want to hash a known_hosts file */
70 /* Flag indicating that we want lookup a host in known_hosts file */
72 /* Flag indicating that we want to delete a host from a known_hosts file */
75 /* Flag indicating that we just want to see the key fingerprint */
76 int print_fingerprint = 0;
77 int print_bubblebabble = 0;
79 /* The identity file name, given on the command line or entered by the user. */
80 char identity_file[1024];
81 int have_identity = 0;
83 /* This is set to the passphrase if given on the command line. */
84 char *identity_passphrase = NULL;
86 /* This is set to the new passphrase if given on the command line. */
87 char *identity_new_passphrase = NULL;
89 /* This is set to the new comment if given on the command line. */
90 char *identity_comment = NULL;
92 /* Dump public key file in format used by real and the original SSH 2 */
93 int convert_to_ssh2 = 0;
94 int convert_from_ssh2 = 0;
96 int print_generic = 0;
98 char *key_type_name = NULL;
101 extern char *__progname;
103 char hostname[MAXHOSTNAMELEN];
106 int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
107 int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
110 ask_filename(struct passwd *pw, const char *prompt)
115 if (key_type_name == NULL)
116 name = _PATH_SSH_CLIENT_ID_RSA;
118 switch (key_type_from_name(key_type_name)) {
120 name = _PATH_SSH_CLIENT_IDENTITY;
123 name = _PATH_SSH_CLIENT_ID_DSA;
126 name = _PATH_SSH_CLIENT_ID_RSA;
129 fprintf(stderr, "bad key type");
134 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
135 fprintf(stderr, "%s (%s): ", prompt, identity_file);
136 if (fgets(buf, sizeof(buf), stdin) == NULL)
138 if (strchr(buf, '\n'))
139 *strchr(buf, '\n') = 0;
140 if (strcmp(buf, "") != 0)
141 strlcpy(identity_file, buf, sizeof(identity_file));
146 load_identity(char *filename)
151 prv = key_load_private(filename, "", NULL);
153 if (identity_passphrase)
154 pass = xstrdup(identity_passphrase);
156 pass = read_passphrase("Enter passphrase: ",
158 prv = key_load_private(filename, pass, NULL);
159 memset(pass, 0, strlen(pass));
165 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
166 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
167 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
168 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
171 do_convert_to_ssh2(struct passwd *pw)
179 ask_filename(pw, "Enter file in which the key is");
180 if (stat(identity_file, &st) < 0) {
181 perror(identity_file);
184 if ((k = key_load_public(identity_file, NULL)) == NULL) {
185 if ((k = load_identity(identity_file)) == NULL) {
186 fprintf(stderr, "load failed\n");
190 if (k->type == KEY_RSA1) {
191 fprintf(stderr, "version 1 keys are not supported\n");
194 if (key_to_blob(k, &blob, &len) <= 0) {
195 fprintf(stderr, "key_to_blob failed\n");
198 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
200 "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
201 key_size(k), key_type(k),
202 pw->pw_name, hostname);
203 dump_base64(stdout, blob, len);
204 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
211 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
213 u_int bignum_bits = buffer_get_int(b);
214 u_int bytes = (bignum_bits + 7) / 8;
216 if (buffer_len(b) < bytes)
217 fatal("buffer_get_bignum_bits: input buffer too small: "
218 "need %d have %d", bytes, buffer_len(b));
219 BN_bin2bn(buffer_ptr(b), bytes, value);
220 buffer_consume(b, bytes);
224 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
229 u_char *sig, data[] = "abcde12345";
230 int magic, rlen, ktype, i1, i2, i3, i4;
235 buffer_append(&b, blob, blen);
237 magic = buffer_get_int(&b);
238 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
239 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
243 i1 = buffer_get_int(&b);
244 type = buffer_get_string(&b, NULL);
245 cipher = buffer_get_string(&b, NULL);
246 i2 = buffer_get_int(&b);
247 i3 = buffer_get_int(&b);
248 i4 = buffer_get_int(&b);
249 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
250 if (strcmp(cipher, "none") != 0) {
251 error("unsupported cipher %s", cipher);
259 if (strstr(type, "dsa")) {
261 } else if (strstr(type, "rsa")) {
268 key = key_new_private(ktype);
273 buffer_get_bignum_bits(&b, key->dsa->p);
274 buffer_get_bignum_bits(&b, key->dsa->g);
275 buffer_get_bignum_bits(&b, key->dsa->q);
276 buffer_get_bignum_bits(&b, key->dsa->pub_key);
277 buffer_get_bignum_bits(&b, key->dsa->priv_key);
280 e = buffer_get_char(&b);
284 e += buffer_get_char(&b);
287 e += buffer_get_char(&b);
290 if (!BN_set_word(key->rsa->e, e)) {
295 buffer_get_bignum_bits(&b, key->rsa->d);
296 buffer_get_bignum_bits(&b, key->rsa->n);
297 buffer_get_bignum_bits(&b, key->rsa->iqmp);
298 buffer_get_bignum_bits(&b, key->rsa->q);
299 buffer_get_bignum_bits(&b, key->rsa->p);
300 rsa_generate_additional_parameters(key->rsa);
303 rlen = buffer_len(&b);
305 error("do_convert_private_ssh2_from_blob: "
306 "remaining bytes in key blob %d", rlen);
310 key_sign(key, &sig, &slen, data, sizeof(data));
311 key_verify(key, sig, slen, data, sizeof(data));
317 get_line(FILE *fp, char *line, size_t len)
323 while ((c = fgetc(fp)) != EOF) {
324 if (pos >= len - 1) {
325 fprintf(stderr, "input line too long.\n");
331 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
332 fprintf(stderr, "unget: %s\n", strerror(errno));
348 do_convert_from_ssh2(struct passwd *pw)
357 int escaped = 0, private = 0, ok;
361 ask_filename(pw, "Enter file in which the key is");
362 if (stat(identity_file, &st) < 0) {
363 perror(identity_file);
366 fp = fopen(identity_file, "r");
368 perror(identity_file);
372 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
373 if (line[blen - 1] == '\\')
375 if (strncmp(line, "----", 4) == 0 ||
376 strstr(line, ": ") != NULL) {
377 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
379 if (strstr(line, " END ") != NULL) {
382 /* fprintf(stderr, "ignore: %s", line); */
387 /* fprintf(stderr, "escaped: %s", line); */
390 strlcat(encoded, line, sizeof(encoded));
392 len = strlen(encoded);
393 if (((len % 4) == 3) &&
394 (encoded[len-1] == '=') &&
395 (encoded[len-2] == '=') &&
396 (encoded[len-3] == '='))
397 encoded[len-3] = '\0';
398 blen = uudecode(encoded, blob, sizeof(blob));
400 fprintf(stderr, "uudecode failed.\n");
404 do_convert_private_ssh2_from_blob(blob, blen) :
405 key_from_blob(blob, blen);
407 fprintf(stderr, "decode blob failed.\n");
411 (k->type == KEY_DSA ?
412 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
413 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
414 key_write(k, stdout);
416 fprintf(stderr, "key write failed");
421 fprintf(stdout, "\n");
427 do_print_public(struct passwd *pw)
433 ask_filename(pw, "Enter file in which the key is");
434 if (stat(identity_file, &st) < 0) {
435 perror(identity_file);
438 prv = load_identity(identity_file);
440 fprintf(stderr, "load failed\n");
443 if (!key_write(prv, stdout))
444 fprintf(stderr, "key_write failed");
446 fprintf(stdout, "\n");
452 do_upload(struct passwd *pw, const char *sc_reader_id)
459 ask_filename(pw, "Enter file in which the key is");
460 if (stat(identity_file, &st) < 0) {
461 perror(identity_file);
464 prv = load_identity(identity_file);
466 error("load failed");
469 ret = sc_put_key(prv, sc_reader_id);
473 logit("loading key done");
478 do_download(struct passwd *pw, const char *sc_reader_id)
483 keys = sc_get_keys(sc_reader_id, NULL);
485 fatal("cannot read public key from smartcard");
486 for (i = 0; keys[i]; i++) {
487 key_write(keys[i], stdout);
489 fprintf(stdout, "\n");
494 #endif /* SMARTCARD */
497 do_fingerprint(struct passwd *pw)
501 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
502 int i, skip = 0, num = 1, invalid = 1;
507 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
508 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
511 ask_filename(pw, "Enter file in which the key is");
512 if (stat(identity_file, &st) < 0) {
513 perror(identity_file);
516 public = key_load_public(identity_file, &comment);
517 if (public != NULL) {
518 fp = key_fingerprint(public, fptype, rep);
519 printf("%u %s %s\n", key_size(public), fp, comment);
530 f = fopen(identity_file, "r");
532 while (fgets(line, sizeof(line), f)) {
533 i = strlen(line) - 1;
534 if (line[i] != '\n') {
535 error("line %d too long: %.40s...", num, line);
546 /* Skip leading whitespace, empty and comment lines. */
547 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
549 if (!*cp || *cp == '\n' || *cp == '#')
551 i = strtol(cp, &ep, 10);
552 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
555 for (; *cp && (quoted || (*cp != ' ' &&
556 *cp != '\t')); cp++) {
557 if (*cp == '\\' && cp[1] == '"')
558 cp++; /* Skip both */
567 public = key_new(KEY_RSA1);
568 if (key_read(public, &cp) != 1) {
571 public = key_new(KEY_UNSPEC);
572 if (key_read(public, &cp) != 1) {
577 comment = *cp ? cp : comment;
578 fp = key_fingerprint(public, fptype, rep);
579 printf("%u %s %s\n", key_size(public), fp,
580 comment ? comment : "no comment");
588 printf("%s is not a public key file.\n", identity_file);
595 print_host(FILE *f, char *name, Key *public, int hash)
597 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
598 fatal("hash_host failed");
599 fprintf(f, "%s ", name);
600 if (!key_write(public, f))
601 fatal("key_write failed");
606 do_known_hosts(struct passwd *pw, const char *name)
608 FILE *in, *out = stdout;
610 char *cp, *cp2, *kp, *kp2;
611 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
612 int c, i, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
614 if (!have_identity) {
615 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
616 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
617 sizeof(identity_file))
618 fatal("Specified known hosts path too long");
622 if ((in = fopen(identity_file, "r")) == NULL)
623 fatal("fopen: %s", strerror(errno));
626 * Find hosts goes to stdout, hash and deletions happen in-place
627 * A corner case is ssh-keygen -HF foo, which should go to stdout
629 if (!find_host && (hash_hosts || delete_host)) {
630 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
631 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
632 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
633 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
634 fatal("known_hosts path too long");
636 if ((c = mkstemp(tmp)) == -1)
637 fatal("mkstemp: %s", strerror(errno));
638 if ((out = fdopen(c, "w")) == NULL) {
641 fatal("fdopen: %s", strerror(c));
646 while (fgets(line, sizeof(line), in)) {
648 i = strlen(line) - 1;
649 if (line[i] != '\n') {
650 error("line %d too long: %.40s...", num, line);
661 /* Skip leading whitespace, empty and comment lines. */
662 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
664 if (!*cp || *cp == '\n' || *cp == '#') {
666 fprintf(out, "%s\n", cp);
669 /* Find the end of the host name portion. */
670 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
672 if (*kp == '\0' || *(kp + 1) == '\0') {
673 error("line %d missing key: %.40s...",
681 public = key_new(KEY_RSA1);
682 if (key_read(public, &kp) != 1) {
685 public = key_new(KEY_UNSPEC);
686 if (key_read(public, &kp) != 1) {
687 error("line %d invalid key: %.40s...",
695 if (*cp == HASH_DELIM) {
696 if (find_host || delete_host) {
697 cp2 = host_hash(name, cp, strlen(cp));
699 error("line %d: invalid hashed "
700 "name: %.64s...", num, line);
704 c = (strcmp(cp2, cp) == 0);
705 if (find_host && c) {
706 printf("# Host %s found: "
707 "line %d type %s\n", name,
708 num, key_type(public));
709 print_host(out, cp, public, 0);
711 if (delete_host && !c)
712 print_host(out, cp, public, 0);
713 } else if (hash_hosts)
714 print_host(out, cp, public, 0);
716 if (find_host || delete_host) {
717 c = (match_hostname(name, cp,
719 if (find_host && c) {
720 printf("# Host %s found: "
721 "line %d type %s\n", name,
722 num, key_type(public));
723 print_host(out, cp, public, hash_hosts);
725 if (delete_host && !c)
726 print_host(out, cp, public, 0);
727 } else if (hash_hosts) {
728 for (cp2 = strsep(&cp, ",");
729 cp2 != NULL && *cp2 != '\0';
730 cp2 = strsep(&cp, ",")) {
731 if (strcspn(cp2, "*?!") != strlen(cp2))
732 fprintf(stderr, "Warning: "
733 "ignoring host name with "
734 "metacharacters: %.64s\n",
737 print_host(out, cp2, public, 1);
747 fprintf(stderr, "%s is not a valid known_host file.\n",
750 fprintf(stderr, "Not replacing existing known_hosts "
751 "file because of errors\n");
761 /* Backup existing file */
762 if (unlink(old) == -1 && errno != ENOENT)
763 fatal("unlink %.100s: %s", old, strerror(errno));
764 if (link(identity_file, old) == -1)
765 fatal("link %.100s to %.100s: %s", identity_file, old,
767 /* Move new one into place */
768 if (rename(tmp, identity_file) == -1) {
769 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
776 fprintf(stderr, "%s updated.\n", identity_file);
777 fprintf(stderr, "Original contents retained as %s\n", old);
779 fprintf(stderr, "WARNING: %s contains unhashed "
781 fprintf(stderr, "Delete this file to ensure privacy "
790 * Perform changing a passphrase. The argument is the passwd structure
791 * for the current user.
794 do_change_passphrase(struct passwd *pw)
797 char *old_passphrase, *passphrase1, *passphrase2;
802 ask_filename(pw, "Enter file in which the key is");
803 if (stat(identity_file, &st) < 0) {
804 perror(identity_file);
807 /* Try to load the file with empty passphrase. */
808 private = key_load_private(identity_file, "", &comment);
809 if (private == NULL) {
810 if (identity_passphrase)
811 old_passphrase = xstrdup(identity_passphrase);
814 read_passphrase("Enter old passphrase: ",
816 private = key_load_private(identity_file, old_passphrase,
818 memset(old_passphrase, 0, strlen(old_passphrase));
819 xfree(old_passphrase);
820 if (private == NULL) {
821 printf("Bad passphrase.\n");
825 printf("Key has comment '%s'\n", comment);
827 /* Ask the new passphrase (twice). */
828 if (identity_new_passphrase) {
829 passphrase1 = xstrdup(identity_new_passphrase);
833 read_passphrase("Enter new passphrase (empty for no "
834 "passphrase): ", RP_ALLOW_STDIN);
835 passphrase2 = read_passphrase("Enter same passphrase again: ",
838 /* Verify that they are the same. */
839 if (strcmp(passphrase1, passphrase2) != 0) {
840 memset(passphrase1, 0, strlen(passphrase1));
841 memset(passphrase2, 0, strlen(passphrase2));
844 printf("Pass phrases do not match. Try again.\n");
847 /* Destroy the other copy. */
848 memset(passphrase2, 0, strlen(passphrase2));
852 /* Save the file using the new passphrase. */
853 if (!key_save_private(private, identity_file, passphrase1, comment)) {
854 printf("Saving the key failed: %s.\n", identity_file);
855 memset(passphrase1, 0, strlen(passphrase1));
861 /* Destroy the passphrase and the copy of the key in memory. */
862 memset(passphrase1, 0, strlen(passphrase1));
864 key_free(private); /* Destroys contents */
867 printf("Your identification has been saved with the new passphrase.\n");
872 * Print the SSHFP RR.
875 do_print_resource_record(struct passwd *pw, char *fname, char *hname)
878 char *comment = NULL;
882 ask_filename(pw, "Enter file in which the key is");
883 if (stat(fname, &st) < 0) {
889 public = key_load_public(fname, &comment);
890 if (public != NULL) {
891 export_dns_rr(hname, public, stdout, print_generic);
899 printf("failed to read v2 public key from %s.\n", fname);
904 * Change the comment of a private key file.
907 do_change_comment(struct passwd *pw)
909 char new_comment[1024], *comment, *passphrase;
917 ask_filename(pw, "Enter file in which the key is");
918 if (stat(identity_file, &st) < 0) {
919 perror(identity_file);
922 private = key_load_private(identity_file, "", &comment);
923 if (private == NULL) {
924 if (identity_passphrase)
925 passphrase = xstrdup(identity_passphrase);
926 else if (identity_new_passphrase)
927 passphrase = xstrdup(identity_new_passphrase);
929 passphrase = read_passphrase("Enter passphrase: ",
931 /* Try to load using the passphrase. */
932 private = key_load_private(identity_file, passphrase, &comment);
933 if (private == NULL) {
934 memset(passphrase, 0, strlen(passphrase));
936 printf("Bad passphrase.\n");
940 passphrase = xstrdup("");
942 if (private->type != KEY_RSA1) {
943 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
947 printf("Key now has comment '%s'\n", comment);
949 if (identity_comment) {
950 strlcpy(new_comment, identity_comment, sizeof(new_comment));
952 printf("Enter new comment: ");
954 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
955 memset(passphrase, 0, strlen(passphrase));
959 if (strchr(new_comment, '\n'))
960 *strchr(new_comment, '\n') = 0;
963 /* Save the file using the new passphrase. */
964 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
965 printf("Saving the key failed: %s.\n", identity_file);
966 memset(passphrase, 0, strlen(passphrase));
972 memset(passphrase, 0, strlen(passphrase));
974 public = key_from_private(private);
977 strlcat(identity_file, ".pub", sizeof(identity_file));
978 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
980 printf("Could not save your public key in %s\n", identity_file);
985 printf("fdopen %s failed", identity_file);
988 if (!key_write(public, f))
989 fprintf(stderr, "write key failed");
991 fprintf(f, " %s\n", new_comment);
996 printf("The comment in your key file has been changed.\n");
1003 fprintf(stderr, "Usage: %s [options]\n", __progname);
1004 fprintf(stderr, "Options:\n");
1005 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
1006 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
1007 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
1008 fprintf(stderr, " -C comment Provide new comment.\n");
1009 fprintf(stderr, " -c Change comment in private and public key files.\n");
1011 fprintf(stderr, " -D reader Download public key from smartcard.\n");
1012 #endif /* SMARTCARD */
1013 fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n");
1014 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1015 fprintf(stderr, " -f filename Filename of the key file.\n");
1016 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1017 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1018 fprintf(stderr, " -H Hash names in known_hosts file.\n");
1019 fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n");
1020 fprintf(stderr, " -l Show fingerprint of key file.\n");
1021 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
1022 fprintf(stderr, " -N phrase Provide new passphrase.\n");
1023 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1024 fprintf(stderr, " -p Change passphrase of private key file.\n");
1025 fprintf(stderr, " -q Quiet.\n");
1026 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1027 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1028 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1029 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1030 fprintf(stderr, " -t type Specify type of key to create.\n");
1032 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
1033 #endif /* SMARTCARD */
1034 fprintf(stderr, " -v Verbose.\n");
1035 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1036 fprintf(stderr, " -y Read private key file and print public key.\n");
1042 * Main program for key management.
1045 main(int ac, char **av)
1047 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
1048 char out_file[MAXPATHLEN], *reader_id = NULL;
1049 char *rr_hostname = NULL;
1050 Key *private, *public;
1053 int opt, type, fd, download = 0;
1054 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
1055 int do_gen_candidates = 0, do_screen_candidates = 0;
1056 int log_level = SYSLOG_LEVEL_INFO;
1057 BIGNUM *start = NULL;
1062 extern char *optarg;
1064 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1067 __progname = ssh_get_progname(av[0]);
1069 SSLeay_add_all_algorithms();
1070 log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
1075 /* we need this for the home * directory. */
1076 pw = getpwuid(getuid());
1078 printf("You don't exist, go away!\n");
1081 if (gethostname(hostname, sizeof(hostname)) < 0) {
1082 perror("gethostname");
1086 while ((opt = getopt(ac, av,
1087 "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {
1090 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
1092 fatal("Bits has bad value %s (%s)",
1097 rr_hostname = optarg;
1104 rr_hostname = optarg;
1107 print_fingerprint = 1;
1110 print_bubblebabble = 1;
1113 change_passphrase = 1;
1119 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1120 sizeof(identity_file))
1121 fatal("Identity filename too long");
1128 identity_passphrase = optarg;
1131 identity_new_passphrase = optarg;
1134 identity_comment = optarg;
1142 convert_to_ssh2 = 1;
1147 convert_from_ssh2 = 1;
1153 key_type_name = "dsa";
1156 key_type_name = optarg;
1165 if (log_level == SYSLOG_LEVEL_INFO)
1166 log_level = SYSLOG_LEVEL_DEBUG1;
1168 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
1169 log_level < SYSLOG_LEVEL_DEBUG3)
1174 rr_hostname = optarg;
1177 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1180 fatal("Desired generator has bad value: %s (%s)",
1184 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
1186 fatal("Invalid number of trials: %s (%s)",
1190 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
1192 fatal("Memory limit is %s: %s", errstr, optarg);
1196 do_gen_candidates = 1;
1197 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1199 fatal("Output filename too long");
1202 do_screen_candidates = 1;
1203 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1205 fatal("Output filename too long");
1208 /* XXX - also compare length against bits */
1209 if (BN_hex2bn(&start, optarg) == 0)
1210 fatal("Invalid start point.");
1219 log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
1222 printf("Too many arguments.\n");
1225 if (change_passphrase && change_comment) {
1226 printf("Can only have one of -p and -c.\n");
1229 if (delete_host || hash_hosts || find_host)
1230 do_known_hosts(pw, rr_hostname);
1231 if (print_fingerprint || print_bubblebabble)
1233 if (change_passphrase)
1234 do_change_passphrase(pw);
1236 do_change_comment(pw);
1237 if (convert_to_ssh2)
1238 do_convert_to_ssh2(pw);
1239 if (convert_from_ssh2)
1240 do_convert_from_ssh2(pw);
1242 do_print_public(pw);
1243 if (rr_hostname != NULL) {
1246 if (have_identity) {
1247 n = do_print_resource_record(pw,
1248 identity_file, rr_hostname);
1250 perror(identity_file);
1256 n += do_print_resource_record(pw,
1257 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1258 n += do_print_resource_record(pw,
1259 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1262 fatal("no keys found.");
1266 if (reader_id != NULL) {
1269 do_download(pw, reader_id);
1271 do_upload(pw, reader_id);
1272 #else /* SMARTCARD */
1273 fatal("no support for smartcards.");
1274 #endif /* SMARTCARD */
1277 if (do_gen_candidates) {
1278 FILE *out = fopen(out_file, "w");
1281 error("Couldn't open modulus candidate file \"%s\": %s",
1282 out_file, strerror(errno));
1286 bits = DEFAULT_BITS;
1287 if (gen_candidates(out, memory, bits, start) != 0)
1288 fatal("modulus candidate generation failed");
1293 if (do_screen_candidates) {
1295 FILE *out = fopen(out_file, "w");
1297 if (have_identity && strcmp(identity_file, "-") != 0) {
1298 if ((in = fopen(identity_file, "r")) == NULL) {
1299 fatal("Couldn't open modulus candidate "
1300 "file \"%s\": %s", identity_file,
1307 fatal("Couldn't open moduli file \"%s\": %s",
1308 out_file, strerror(errno));
1310 if (prime_test(in, out, trials, generator_wanted) != 0)
1311 fatal("modulus screening failed");
1317 if (key_type_name == NULL)
1318 key_type_name = "rsa";
1320 type = key_type_from_name(key_type_name);
1321 if (type == KEY_UNSPEC) {
1322 fprintf(stderr, "unknown key type %s\n", key_type_name);
1326 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
1327 if (type == KEY_DSA && bits != 1024)
1328 fatal("DSA keys must be 1024 bits");
1330 printf("Generating public/private %s key pair.\n", key_type_name);
1331 private = key_generate(type, bits);
1332 if (private == NULL) {
1333 fprintf(stderr, "key_generate failed");
1336 public = key_from_private(private);
1339 ask_filename(pw, "Enter file in which to save the key");
1341 /* Create ~/.ssh directory if it doesn't already exist. */
1342 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
1343 if (strstr(identity_file, dotsshdir) != NULL &&
1344 stat(dotsshdir, &st) < 0) {
1345 if (mkdir(dotsshdir, 0700) < 0)
1346 error("Could not create directory '%s'.", dotsshdir);
1348 printf("Created directory '%s'.\n", dotsshdir);
1350 /* If the file already exists, ask the user to confirm. */
1351 if (stat(identity_file, &st) >= 0) {
1353 printf("%s already exists.\n", identity_file);
1354 printf("Overwrite (y/n)? ");
1356 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1358 if (yesno[0] != 'y' && yesno[0] != 'Y')
1361 /* Ask for a passphrase (twice). */
1362 if (identity_passphrase)
1363 passphrase1 = xstrdup(identity_passphrase);
1364 else if (identity_new_passphrase)
1365 passphrase1 = xstrdup(identity_new_passphrase);
1369 read_passphrase("Enter passphrase (empty for no "
1370 "passphrase): ", RP_ALLOW_STDIN);
1371 passphrase2 = read_passphrase("Enter same passphrase again: ",
1373 if (strcmp(passphrase1, passphrase2) != 0) {
1375 * The passphrases do not match. Clear them and
1378 memset(passphrase1, 0, strlen(passphrase1));
1379 memset(passphrase2, 0, strlen(passphrase2));
1382 printf("Passphrases do not match. Try again.\n");
1383 goto passphrase_again;
1385 /* Clear the other copy of the passphrase. */
1386 memset(passphrase2, 0, strlen(passphrase2));
1390 if (identity_comment) {
1391 strlcpy(comment, identity_comment, sizeof(comment));
1393 /* Create default commend field for the passphrase. */
1394 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1397 /* Save the key with the given passphrase and comment. */
1398 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1399 printf("Saving the key failed: %s.\n", identity_file);
1400 memset(passphrase1, 0, strlen(passphrase1));
1404 /* Clear the passphrase. */
1405 memset(passphrase1, 0, strlen(passphrase1));
1408 /* Clear the private key and the random number generator. */
1413 printf("Your identification has been saved in %s.\n", identity_file);
1415 strlcat(identity_file, ".pub", sizeof(identity_file));
1416 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1418 printf("Could not save your public key in %s\n", identity_file);
1421 f = fdopen(fd, "w");
1423 printf("fdopen %s failed", identity_file);
1426 if (!key_write(public, f))
1427 fprintf(stderr, "write key failed");
1428 fprintf(f, " %s\n", comment);
1432 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1433 printf("Your public key has been saved in %s.\n",
1435 printf("The key fingerprint is:\n");
1436 printf("%s %s\n", fp, comment);