1 /* $OpenBSD: ssh-keygen.c,v 1.176 2010/01/11 10:51:07 djm 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>
20 #include <sys/param.h>
22 #include <openssl/evp.h>
23 #include <openssl/pem.h>
24 #include "openbsd-compat/openssl-compat.h"
45 #include "pathnames.h"
56 /* Number of bits in the RSA/DSA key. This value can be set on the command line. */
57 #define DEFAULT_BITS 2048
58 #define DEFAULT_BITS_DSA 1024
62 * Flag indicating that we just want to change the passphrase. This can be
63 * set on the command line.
65 int change_passphrase = 0;
68 * Flag indicating that we just want to change the comment. This can be set
69 * on the command line.
71 int change_comment = 0;
75 int log_level = SYSLOG_LEVEL_INFO;
77 /* Flag indicating that we want to hash a known_hosts file */
79 /* Flag indicating that we want lookup a host in known_hosts file */
81 /* Flag indicating that we want to delete a host from a known_hosts file */
84 /* Flag indicating that we just want to see the key fingerprint */
85 int print_fingerprint = 0;
86 int print_bubblebabble = 0;
88 /* The identity file name, given on the command line or entered by the user. */
89 char identity_file[1024];
90 int have_identity = 0;
92 /* This is set to the passphrase if given on the command line. */
93 char *identity_passphrase = NULL;
95 /* This is set to the new passphrase if given on the command line. */
96 char *identity_new_passphrase = NULL;
98 /* This is set to the new comment if given on the command line. */
99 char *identity_comment = NULL;
101 /* Dump public key file in format used by real and the original SSH 2 */
102 int convert_to_ssh2 = 0;
103 int convert_from_ssh2 = 0;
104 int print_public = 0;
105 int print_generic = 0;
107 char *key_type_name = NULL;
110 extern char *__progname;
112 char hostname[MAXHOSTNAMELEN];
115 int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
116 int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
119 ask_filename(struct passwd *pw, const char *prompt)
124 if (key_type_name == NULL)
125 name = _PATH_SSH_CLIENT_ID_RSA;
127 switch (key_type_from_name(key_type_name)) {
129 name = _PATH_SSH_CLIENT_IDENTITY;
132 name = _PATH_SSH_CLIENT_ID_DSA;
135 name = _PATH_SSH_CLIENT_ID_RSA;
138 fprintf(stderr, "bad key type\n");
143 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
144 fprintf(stderr, "%s (%s): ", prompt, identity_file);
145 if (fgets(buf, sizeof(buf), stdin) == NULL)
147 buf[strcspn(buf, "\n")] = '\0';
148 if (strcmp(buf, "") != 0)
149 strlcpy(identity_file, buf, sizeof(identity_file));
154 load_identity(char *filename)
159 prv = key_load_private(filename, "", NULL);
161 if (identity_passphrase)
162 pass = xstrdup(identity_passphrase);
164 pass = read_passphrase("Enter passphrase: ",
166 prv = key_load_private(filename, pass, NULL);
167 memset(pass, 0, strlen(pass));
173 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
174 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
175 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
176 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
179 do_convert_to_ssh2(struct passwd *pw)
188 ask_filename(pw, "Enter file in which the key is");
189 if (stat(identity_file, &st) < 0) {
190 perror(identity_file);
193 if ((k = key_load_public(identity_file, NULL)) == NULL) {
194 if ((k = load_identity(identity_file)) == NULL) {
195 fprintf(stderr, "load failed\n");
199 if (k->type == KEY_RSA1) {
200 fprintf(stderr, "version 1 keys are not supported\n");
203 if (key_to_blob(k, &blob, &len) <= 0) {
204 fprintf(stderr, "key_to_blob failed\n");
207 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
208 snprintf(comment, sizeof(comment),
209 "%u-bit %s, converted by %s@%s from OpenSSH",
210 key_size(k), key_type(k),
211 pw->pw_name, hostname);
213 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
214 fprintf(stdout, "Comment: \"%s\"\n", comment);
215 dump_base64(stdout, blob, len);
216 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
223 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
225 u_int bignum_bits = buffer_get_int(b);
226 u_int bytes = (bignum_bits + 7) / 8;
228 if (buffer_len(b) < bytes)
229 fatal("buffer_get_bignum_bits: input buffer too small: "
230 "need %d have %d", bytes, buffer_len(b));
231 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
232 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
233 buffer_consume(b, bytes);
237 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
242 u_char *sig, data[] = "abcde12345";
243 int magic, rlen, ktype, i1, i2, i3, i4;
248 buffer_append(&b, blob, blen);
250 magic = buffer_get_int(&b);
251 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
252 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
256 i1 = buffer_get_int(&b);
257 type = buffer_get_string(&b, NULL);
258 cipher = buffer_get_string(&b, NULL);
259 i2 = buffer_get_int(&b);
260 i3 = buffer_get_int(&b);
261 i4 = buffer_get_int(&b);
262 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
263 if (strcmp(cipher, "none") != 0) {
264 error("unsupported cipher %s", cipher);
272 if (strstr(type, "dsa")) {
274 } else if (strstr(type, "rsa")) {
281 key = key_new_private(ktype);
286 buffer_get_bignum_bits(&b, key->dsa->p);
287 buffer_get_bignum_bits(&b, key->dsa->g);
288 buffer_get_bignum_bits(&b, key->dsa->q);
289 buffer_get_bignum_bits(&b, key->dsa->pub_key);
290 buffer_get_bignum_bits(&b, key->dsa->priv_key);
293 e = buffer_get_char(&b);
297 e += buffer_get_char(&b);
300 e += buffer_get_char(&b);
303 if (!BN_set_word(key->rsa->e, e)) {
308 buffer_get_bignum_bits(&b, key->rsa->d);
309 buffer_get_bignum_bits(&b, key->rsa->n);
310 buffer_get_bignum_bits(&b, key->rsa->iqmp);
311 buffer_get_bignum_bits(&b, key->rsa->q);
312 buffer_get_bignum_bits(&b, key->rsa->p);
313 rsa_generate_additional_parameters(key->rsa);
316 rlen = buffer_len(&b);
318 error("do_convert_private_ssh2_from_blob: "
319 "remaining bytes in key blob %d", rlen);
323 key_sign(key, &sig, &slen, data, sizeof(data));
324 key_verify(key, sig, slen, data, sizeof(data));
330 get_line(FILE *fp, char *line, size_t len)
336 while ((c = fgetc(fp)) != EOF) {
337 if (pos >= len - 1) {
338 fprintf(stderr, "input line too long.\n");
344 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
345 fprintf(stderr, "unget: %s\n", strerror(errno));
360 do_convert_from_ssh2(struct passwd *pw)
369 int escaped = 0, private = 0, ok;
373 ask_filename(pw, "Enter file in which the key is");
374 if (stat(identity_file, &st) < 0) {
375 perror(identity_file);
378 fp = fopen(identity_file, "r");
380 perror(identity_file);
384 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
385 if (line[blen - 1] == '\\')
387 if (strncmp(line, "----", 4) == 0 ||
388 strstr(line, ": ") != NULL) {
389 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
391 if (strstr(line, " END ") != NULL) {
394 /* fprintf(stderr, "ignore: %s", line); */
399 /* fprintf(stderr, "escaped: %s", line); */
402 strlcat(encoded, line, sizeof(encoded));
404 len = strlen(encoded);
405 if (((len % 4) == 3) &&
406 (encoded[len-1] == '=') &&
407 (encoded[len-2] == '=') &&
408 (encoded[len-3] == '='))
409 encoded[len-3] = '\0';
410 blen = uudecode(encoded, blob, sizeof(blob));
412 fprintf(stderr, "uudecode failed.\n");
416 do_convert_private_ssh2_from_blob(blob, blen) :
417 key_from_blob(blob, blen);
419 fprintf(stderr, "decode blob failed.\n");
423 (k->type == KEY_DSA ?
424 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
425 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
426 key_write(k, stdout);
428 fprintf(stderr, "key write failed\n");
433 fprintf(stdout, "\n");
439 do_print_public(struct passwd *pw)
445 ask_filename(pw, "Enter file in which the key is");
446 if (stat(identity_file, &st) < 0) {
447 perror(identity_file);
450 prv = load_identity(identity_file);
452 fprintf(stderr, "load failed\n");
455 if (!key_write(prv, stdout))
456 fprintf(stderr, "key_write failed");
458 fprintf(stdout, "\n");
464 do_upload(struct passwd *pw, const char *sc_reader_id)
471 ask_filename(pw, "Enter file in which the key is");
472 if (stat(identity_file, &st) < 0) {
473 perror(identity_file);
476 prv = load_identity(identity_file);
478 error("load failed");
481 ret = sc_put_key(prv, sc_reader_id);
485 logit("loading key done");
490 do_download(struct passwd *pw, const char *sc_reader_id)
495 keys = sc_get_keys(sc_reader_id, NULL);
497 fatal("cannot read public key from smartcard");
498 for (i = 0; keys[i]; i++) {
499 key_write(keys[i], stdout);
501 fprintf(stdout, "\n");
506 #endif /* SMARTCARD */
509 do_fingerprint(struct passwd *pw)
513 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
514 int i, skip = 0, num = 0, invalid = 1;
519 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
520 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
523 ask_filename(pw, "Enter file in which the key is");
524 if (stat(identity_file, &st) < 0) {
525 perror(identity_file);
528 public = key_load_public(identity_file, &comment);
529 if (public != NULL) {
530 fp = key_fingerprint(public, fptype, rep);
531 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
532 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
534 if (log_level >= SYSLOG_LEVEL_VERBOSE)
547 f = fopen(identity_file, "r");
549 while (fgets(line, sizeof(line), f)) {
550 if ((cp = strchr(line, '\n')) == NULL) {
551 error("line %d too long: %.40s...",
563 /* Skip leading whitespace, empty and comment lines. */
564 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
566 if (!*cp || *cp == '\n' || *cp == '#')
568 i = strtol(cp, &ep, 10);
569 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
572 for (; *cp && (quoted || (*cp != ' ' &&
573 *cp != '\t')); cp++) {
574 if (*cp == '\\' && cp[1] == '"')
575 cp++; /* Skip both */
584 public = key_new(KEY_RSA1);
585 if (key_read(public, &cp) != 1) {
588 public = key_new(KEY_UNSPEC);
589 if (key_read(public, &cp) != 1) {
594 comment = *cp ? cp : comment;
595 fp = key_fingerprint(public, fptype, rep);
596 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
597 printf("%u %s %s (%s)\n", key_size(public), fp,
598 comment ? comment : "no comment", key_type(public));
599 if (log_level >= SYSLOG_LEVEL_VERBOSE)
609 printf("%s is not a public key file.\n", identity_file);
616 print_host(FILE *f, const char *name, Key *public, int hash)
618 if (print_fingerprint) {
623 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
624 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
625 fp = key_fingerprint(public, fptype, rep);
626 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
627 printf("%u %s %s (%s)\n", key_size(public), fp, name,
629 if (log_level >= SYSLOG_LEVEL_VERBOSE)
634 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
635 fatal("hash_host failed");
636 fprintf(f, "%s ", name);
637 if (!key_write(public, f))
638 fatal("key_write failed");
644 do_known_hosts(struct passwd *pw, const char *name)
646 FILE *in, *out = stdout;
648 char *cp, *cp2, *kp, *kp2;
649 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
650 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
652 if (!have_identity) {
653 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
654 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
655 sizeof(identity_file))
656 fatal("Specified known hosts path too long");
660 if ((in = fopen(identity_file, "r")) == NULL)
661 fatal("fopen: %s", strerror(errno));
664 * Find hosts goes to stdout, hash and deletions happen in-place
665 * A corner case is ssh-keygen -HF foo, which should go to stdout
667 if (!find_host && (hash_hosts || delete_host)) {
668 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
669 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
670 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
671 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
672 fatal("known_hosts path too long");
674 if ((c = mkstemp(tmp)) == -1)
675 fatal("mkstemp: %s", strerror(errno));
676 if ((out = fdopen(c, "w")) == NULL) {
679 fatal("fdopen: %s", strerror(c));
684 while (fgets(line, sizeof(line), in)) {
685 if ((cp = strchr(line, '\n')) == NULL) {
686 error("line %d too long: %.40s...", num + 1, line);
698 /* Skip leading whitespace, empty and comment lines. */
699 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
701 if (!*cp || *cp == '\n' || *cp == '#') {
703 fprintf(out, "%s\n", cp);
706 /* Find the end of the host name portion. */
707 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
709 if (*kp == '\0' || *(kp + 1) == '\0') {
710 error("line %d missing key: %.40s...",
718 public = key_new(KEY_RSA1);
719 if (key_read(public, &kp) != 1) {
722 public = key_new(KEY_UNSPEC);
723 if (key_read(public, &kp) != 1) {
724 error("line %d invalid key: %.40s...",
732 if (*cp == HASH_DELIM) {
733 if (find_host || delete_host) {
734 cp2 = host_hash(name, cp, strlen(cp));
736 error("line %d: invalid hashed "
737 "name: %.64s...", num, line);
741 c = (strcmp(cp2, cp) == 0);
742 if (find_host && c) {
743 printf("# Host %s found: "
744 "line %d type %s\n", name,
745 num, key_type(public));
746 print_host(out, cp, public, 0);
748 if (delete_host && !c)
749 print_host(out, cp, public, 0);
750 } else if (hash_hosts)
751 print_host(out, cp, public, 0);
753 if (find_host || delete_host) {
754 c = (match_hostname(name, cp,
756 if (find_host && c) {
757 printf("# Host %s found: "
758 "line %d type %s\n", name,
759 num, key_type(public));
760 print_host(out, name, public,
763 if (delete_host && !c)
764 print_host(out, cp, public, 0);
765 } else if (hash_hosts) {
766 for (cp2 = strsep(&cp, ",");
767 cp2 != NULL && *cp2 != '\0';
768 cp2 = strsep(&cp, ",")) {
769 if (strcspn(cp2, "*?!") != strlen(cp2))
770 fprintf(stderr, "Warning: "
771 "ignoring host name with "
772 "metacharacters: %.64s\n",
775 print_host(out, cp2, public, 1);
785 fprintf(stderr, "%s is not a valid known_hosts file.\n",
788 fprintf(stderr, "Not replacing existing known_hosts "
789 "file because of errors\n");
799 /* Backup existing file */
800 if (unlink(old) == -1 && errno != ENOENT)
801 fatal("unlink %.100s: %s", old, strerror(errno));
802 if (link(identity_file, old) == -1)
803 fatal("link %.100s to %.100s: %s", identity_file, old,
805 /* Move new one into place */
806 if (rename(tmp, identity_file) == -1) {
807 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
814 fprintf(stderr, "%s updated.\n", identity_file);
815 fprintf(stderr, "Original contents retained as %s\n", old);
817 fprintf(stderr, "WARNING: %s contains unhashed "
819 fprintf(stderr, "Delete this file to ensure privacy "
828 * Perform changing a passphrase. The argument is the passwd structure
829 * for the current user.
832 do_change_passphrase(struct passwd *pw)
835 char *old_passphrase, *passphrase1, *passphrase2;
840 ask_filename(pw, "Enter file in which the key is");
841 if (stat(identity_file, &st) < 0) {
842 perror(identity_file);
845 /* Try to load the file with empty passphrase. */
846 private = key_load_private(identity_file, "", &comment);
847 if (private == NULL) {
848 if (identity_passphrase)
849 old_passphrase = xstrdup(identity_passphrase);
852 read_passphrase("Enter old passphrase: ",
854 private = key_load_private(identity_file, old_passphrase,
856 memset(old_passphrase, 0, strlen(old_passphrase));
857 xfree(old_passphrase);
858 if (private == NULL) {
859 printf("Bad passphrase.\n");
863 printf("Key has comment '%s'\n", comment);
865 /* Ask the new passphrase (twice). */
866 if (identity_new_passphrase) {
867 passphrase1 = xstrdup(identity_new_passphrase);
871 read_passphrase("Enter new passphrase (empty for no "
872 "passphrase): ", RP_ALLOW_STDIN);
873 passphrase2 = read_passphrase("Enter same passphrase again: ",
876 /* Verify that they are the same. */
877 if (strcmp(passphrase1, passphrase2) != 0) {
878 memset(passphrase1, 0, strlen(passphrase1));
879 memset(passphrase2, 0, strlen(passphrase2));
882 printf("Pass phrases do not match. Try again.\n");
885 /* Destroy the other copy. */
886 memset(passphrase2, 0, strlen(passphrase2));
890 /* Save the file using the new passphrase. */
891 if (!key_save_private(private, identity_file, passphrase1, comment)) {
892 printf("Saving the key failed: %s.\n", identity_file);
893 memset(passphrase1, 0, strlen(passphrase1));
899 /* Destroy the passphrase and the copy of the key in memory. */
900 memset(passphrase1, 0, strlen(passphrase1));
902 key_free(private); /* Destroys contents */
905 printf("Your identification has been saved with the new passphrase.\n");
910 * Print the SSHFP RR.
913 do_print_resource_record(struct passwd *pw, char *fname, char *hname)
916 char *comment = NULL;
920 ask_filename(pw, "Enter file in which the key is");
921 if (stat(fname, &st) < 0) {
927 public = key_load_public(fname, &comment);
928 if (public != NULL) {
929 export_dns_rr(hname, public, stdout, print_generic);
937 printf("failed to read v2 public key from %s.\n", fname);
942 * Change the comment of a private key file.
945 do_change_comment(struct passwd *pw)
947 char new_comment[1024], *comment, *passphrase;
955 ask_filename(pw, "Enter file in which the key is");
956 if (stat(identity_file, &st) < 0) {
957 perror(identity_file);
960 private = key_load_private(identity_file, "", &comment);
961 if (private == NULL) {
962 if (identity_passphrase)
963 passphrase = xstrdup(identity_passphrase);
964 else if (identity_new_passphrase)
965 passphrase = xstrdup(identity_new_passphrase);
967 passphrase = read_passphrase("Enter passphrase: ",
969 /* Try to load using the passphrase. */
970 private = key_load_private(identity_file, passphrase, &comment);
971 if (private == NULL) {
972 memset(passphrase, 0, strlen(passphrase));
974 printf("Bad passphrase.\n");
978 passphrase = xstrdup("");
980 if (private->type != KEY_RSA1) {
981 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
985 printf("Key now has comment '%s'\n", comment);
987 if (identity_comment) {
988 strlcpy(new_comment, identity_comment, sizeof(new_comment));
990 printf("Enter new comment: ");
992 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
993 memset(passphrase, 0, strlen(passphrase));
997 new_comment[strcspn(new_comment, "\n")] = '\0';
1000 /* Save the file using the new passphrase. */
1001 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
1002 printf("Saving the key failed: %s.\n", identity_file);
1003 memset(passphrase, 0, strlen(passphrase));
1009 memset(passphrase, 0, strlen(passphrase));
1011 public = key_from_private(private);
1014 strlcat(identity_file, ".pub", sizeof(identity_file));
1015 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1017 printf("Could not save your public key in %s\n", identity_file);
1020 f = fdopen(fd, "w");
1022 printf("fdopen %s failed\n", identity_file);
1025 if (!key_write(public, f))
1026 fprintf(stderr, "write key failed\n");
1028 fprintf(f, " %s\n", new_comment);
1033 printf("The comment in your key file has been changed.\n");
1040 fprintf(stderr, "usage: %s [options]\n", __progname);
1041 fprintf(stderr, "Options:\n");
1042 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
1043 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
1044 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
1045 fprintf(stderr, " -C comment Provide new comment.\n");
1046 fprintf(stderr, " -c Change comment in private and public key files.\n");
1048 fprintf(stderr, " -D reader Download public key from smartcard.\n");
1049 #endif /* SMARTCARD */
1050 fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n");
1051 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1052 fprintf(stderr, " -f filename Filename of the key file.\n");
1053 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1054 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1055 fprintf(stderr, " -H Hash names in known_hosts file.\n");
1056 fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n");
1057 fprintf(stderr, " -l Show fingerprint of key file.\n");
1058 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
1059 fprintf(stderr, " -N phrase Provide new passphrase.\n");
1060 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1061 fprintf(stderr, " -p Change passphrase of private key file.\n");
1062 fprintf(stderr, " -q Quiet.\n");
1063 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1064 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1065 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1066 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1067 fprintf(stderr, " -t type Specify type of key to create.\n");
1069 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
1070 #endif /* SMARTCARD */
1071 fprintf(stderr, " -v Verbose.\n");
1072 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1073 fprintf(stderr, " -y Read private key file and print public key.\n");
1079 * Main program for key management.
1082 main(int argc, char **argv)
1084 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
1085 char out_file[MAXPATHLEN], *reader_id = NULL;
1086 char *rr_hostname = NULL;
1087 Key *private, *public;
1090 int opt, type, fd, download = 0;
1091 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
1092 int do_gen_candidates = 0, do_screen_candidates = 0;
1093 BIGNUM *start = NULL;
1098 extern char *optarg;
1100 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1103 __progname = ssh_get_progname(argv[0]);
1105 SSLeay_add_all_algorithms();
1106 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
1111 /* we need this for the home * directory. */
1112 pw = getpwuid(getuid());
1114 printf("You don't exist, go away!\n");
1117 if (gethostname(hostname, sizeof(hostname)) < 0) {
1118 perror("gethostname");
1122 while ((opt = getopt(argc, argv,
1123 "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {
1126 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
1128 fatal("Bits has bad value %s (%s)",
1133 rr_hostname = optarg;
1140 rr_hostname = optarg;
1143 print_fingerprint = 1;
1146 print_bubblebabble = 1;
1149 change_passphrase = 1;
1155 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1156 sizeof(identity_file))
1157 fatal("Identity filename too long");
1164 identity_passphrase = optarg;
1167 identity_new_passphrase = optarg;
1170 identity_comment = optarg;
1178 convert_to_ssh2 = 1;
1183 convert_from_ssh2 = 1;
1189 key_type_name = "dsa";
1192 key_type_name = optarg;
1201 if (log_level == SYSLOG_LEVEL_INFO)
1202 log_level = SYSLOG_LEVEL_DEBUG1;
1204 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
1205 log_level < SYSLOG_LEVEL_DEBUG3)
1210 rr_hostname = optarg;
1213 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1216 fatal("Desired generator has bad value: %s (%s)",
1220 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
1222 fatal("Invalid number of trials: %s (%s)",
1226 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
1228 fatal("Memory limit is %s: %s", errstr, optarg);
1232 do_gen_candidates = 1;
1233 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1235 fatal("Output filename too long");
1238 do_screen_candidates = 1;
1239 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1241 fatal("Output filename too long");
1244 /* XXX - also compare length against bits */
1245 if (BN_hex2bn(&start, optarg) == 0)
1246 fatal("Invalid start point.");
1255 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
1257 if (optind < argc) {
1258 printf("Too many arguments.\n");
1261 if (change_passphrase && change_comment) {
1262 printf("Can only have one of -p and -c.\n");
1265 if (print_fingerprint && (delete_host || hash_hosts)) {
1266 printf("Cannot use -l with -D or -R.\n");
1269 if (delete_host || hash_hosts || find_host)
1270 do_known_hosts(pw, rr_hostname);
1271 if (print_fingerprint || print_bubblebabble)
1273 if (change_passphrase)
1274 do_change_passphrase(pw);
1276 do_change_comment(pw);
1277 if (convert_to_ssh2)
1278 do_convert_to_ssh2(pw);
1279 if (convert_from_ssh2)
1280 do_convert_from_ssh2(pw);
1282 do_print_public(pw);
1283 if (rr_hostname != NULL) {
1286 if (have_identity) {
1287 n = do_print_resource_record(pw,
1288 identity_file, rr_hostname);
1290 perror(identity_file);
1296 n += do_print_resource_record(pw,
1297 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1298 n += do_print_resource_record(pw,
1299 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1302 fatal("no keys found.");
1306 if (reader_id != NULL) {
1309 do_download(pw, reader_id);
1311 do_upload(pw, reader_id);
1312 #else /* SMARTCARD */
1313 fatal("no support for smartcards.");
1314 #endif /* SMARTCARD */
1317 if (do_gen_candidates) {
1318 FILE *out = fopen(out_file, "w");
1321 error("Couldn't open modulus candidate file \"%s\": %s",
1322 out_file, strerror(errno));
1326 bits = DEFAULT_BITS;
1327 if (gen_candidates(out, memory, bits, start) != 0)
1328 fatal("modulus candidate generation failed");
1333 if (do_screen_candidates) {
1335 FILE *out = fopen(out_file, "w");
1337 if (have_identity && strcmp(identity_file, "-") != 0) {
1338 if ((in = fopen(identity_file, "r")) == NULL) {
1339 fatal("Couldn't open modulus candidate "
1340 "file \"%s\": %s", identity_file,
1347 fatal("Couldn't open moduli file \"%s\": %s",
1348 out_file, strerror(errno));
1350 if (prime_test(in, out, trials, generator_wanted) != 0)
1351 fatal("modulus screening failed");
1357 if (key_type_name == NULL)
1358 key_type_name = "rsa";
1360 type = key_type_from_name(key_type_name);
1361 if (type == KEY_UNSPEC) {
1362 fprintf(stderr, "unknown key type %s\n", key_type_name);
1366 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
1367 if (type == KEY_DSA && bits != 1024)
1368 fatal("DSA keys must be 1024 bits");
1370 printf("Generating public/private %s key pair.\n", key_type_name);
1371 private = key_generate(type, bits);
1372 if (private == NULL) {
1373 fprintf(stderr, "key_generate failed\n");
1376 public = key_from_private(private);
1379 ask_filename(pw, "Enter file in which to save the key");
1381 /* Create ~/.ssh directory if it doesn't already exist. */
1382 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
1383 if (strstr(identity_file, dotsshdir) != NULL &&
1384 stat(dotsshdir, &st) < 0) {
1385 if (mkdir(dotsshdir, 0700) < 0)
1386 error("Could not create directory '%s'.", dotsshdir);
1388 printf("Created directory '%s'.\n", dotsshdir);
1390 /* If the file already exists, ask the user to confirm. */
1391 if (stat(identity_file, &st) >= 0) {
1393 printf("%s already exists.\n", identity_file);
1394 printf("Overwrite (y/n)? ");
1396 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1398 if (yesno[0] != 'y' && yesno[0] != 'Y')
1401 /* Ask for a passphrase (twice). */
1402 if (identity_passphrase)
1403 passphrase1 = xstrdup(identity_passphrase);
1404 else if (identity_new_passphrase)
1405 passphrase1 = xstrdup(identity_new_passphrase);
1409 read_passphrase("Enter passphrase (empty for no "
1410 "passphrase): ", RP_ALLOW_STDIN);
1411 passphrase2 = read_passphrase("Enter same passphrase again: ",
1413 if (strcmp(passphrase1, passphrase2) != 0) {
1415 * The passphrases do not match. Clear them and
1418 memset(passphrase1, 0, strlen(passphrase1));
1419 memset(passphrase2, 0, strlen(passphrase2));
1422 printf("Passphrases do not match. Try again.\n");
1423 goto passphrase_again;
1425 /* Clear the other copy of the passphrase. */
1426 memset(passphrase2, 0, strlen(passphrase2));
1430 if (identity_comment) {
1431 strlcpy(comment, identity_comment, sizeof(comment));
1433 /* Create default comment field for the passphrase. */
1434 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1437 /* Save the key with the given passphrase and comment. */
1438 if (!key_save_private(private, identity_file, passphrase1, comment)) {
1439 printf("Saving the key failed: %s.\n", identity_file);
1440 memset(passphrase1, 0, strlen(passphrase1));
1444 /* Clear the passphrase. */
1445 memset(passphrase1, 0, strlen(passphrase1));
1448 /* Clear the private key and the random number generator. */
1453 printf("Your identification has been saved in %s.\n", identity_file);
1455 strlcat(identity_file, ".pub", sizeof(identity_file));
1456 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1458 printf("Could not save your public key in %s\n", identity_file);
1461 f = fdopen(fd, "w");
1463 printf("fdopen %s failed\n", identity_file);
1466 if (!key_write(public, f))
1467 fprintf(stderr, "write key failed\n");
1468 fprintf(f, " %s\n", comment);
1472 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1473 char *ra = key_fingerprint(public, SSH_FP_MD5,
1475 printf("Your public key has been saved in %s.\n",
1477 printf("The key fingerprint is:\n");
1478 printf("%s %s\n", fp, comment);
1479 printf("The key's randomart image is:\n");