Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Identity and host key generation and maintenance. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 6 | * |
| 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". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 15 | |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 18 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 19 | #include <openssl/evp.h> |
| 20 | #include <openssl/pem.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 21 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 22 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 23 | #include "key.h" |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 24 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 25 | #include "authfile.h" |
| 26 | #include "uuencode.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 27 | #include "buffer.h" |
| 28 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 29 | #include "pathnames.h" |
| 30 | #include "log.h" |
Darren Tucker | e608ca2 | 2004-05-13 16:15:47 +1000 | [diff] [blame] | 31 | #include "misc.h" |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 32 | #include "match.h" |
| 33 | #include "hostfile.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 34 | |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 35 | #ifdef SMARTCARD |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 36 | #include "scard.h" |
Ben Lindstrom | bcc1808 | 2001-08-06 21:59:25 +0000 | [diff] [blame] | 37 | #endif |
Damien Miller | ed12a26 | 2003-05-15 13:37:43 +1000 | [diff] [blame] | 38 | #include "dns.h" |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 39 | |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 40 | /* Number of bits in the RSA/DSA key. This value can be set on the command line. */ |
| 41 | #define DEFAULT_BITS 2048 |
| 42 | #define DEFAULT_BITS_DSA 1024 |
| 43 | u_int32_t bits = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 45 | /* |
| 46 | * Flag indicating that we just want to change the passphrase. This can be |
| 47 | * set on the command line. |
| 48 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 49 | int change_passphrase = 0; |
| 50 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 51 | /* |
| 52 | * Flag indicating that we just want to change the comment. This can be set |
| 53 | * on the command line. |
| 54 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 55 | int change_comment = 0; |
| 56 | |
| 57 | int quiet = 0; |
| 58 | |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 59 | /* Flag indicating that we want to hash a known_hosts file */ |
| 60 | int hash_hosts = 0; |
| 61 | /* Flag indicating that we want lookup a host in known_hosts file */ |
| 62 | int find_host = 0; |
| 63 | /* Flag indicating that we want to delete a host from a known_hosts file */ |
| 64 | int delete_host = 0; |
| 65 | |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 66 | /* Flag indicating that we just want to see the key fingerprint */ |
| 67 | int print_fingerprint = 0; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 68 | int print_bubblebabble = 0; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 69 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 70 | /* The identity file name, given on the command line or entered by the user. */ |
| 71 | char identity_file[1024]; |
| 72 | int have_identity = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 73 | |
| 74 | /* This is set to the passphrase if given on the command line. */ |
| 75 | char *identity_passphrase = NULL; |
| 76 | |
| 77 | /* This is set to the new passphrase if given on the command line. */ |
| 78 | char *identity_new_passphrase = NULL; |
| 79 | |
| 80 | /* This is set to the new comment if given on the command line. */ |
| 81 | char *identity_comment = NULL; |
| 82 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 83 | /* Dump public key file in format used by real and the original SSH 2 */ |
| 84 | int convert_to_ssh2 = 0; |
| 85 | int convert_from_ssh2 = 0; |
| 86 | int print_public = 0; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 87 | int print_generic = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 88 | |
Damien Miller | a41c8b1 | 2002-01-22 23:05:08 +1100 | [diff] [blame] | 89 | char *key_type_name = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 90 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 91 | /* argv0 */ |
| 92 | extern char *__progname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 93 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 94 | char hostname[MAXHOSTNAMELEN]; |
| 95 | |
Darren Tucker | 770fc01 | 2004-05-13 16:24:32 +1000 | [diff] [blame] | 96 | /* moduli.c */ |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 97 | int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *); |
Darren Tucker | 770fc01 | 2004-05-13 16:24:32 +1000 | [diff] [blame] | 98 | int prime_test(FILE *, FILE *, u_int32_t, u_int32_t); |
| 99 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 100 | static void |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 101 | ask_filename(struct passwd *pw, const char *prompt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 102 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 103 | char buf[1024]; |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 104 | char *name = NULL; |
| 105 | |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 106 | if (key_type_name == NULL) |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 107 | name = _PATH_SSH_CLIENT_ID_RSA; |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 108 | else |
| 109 | switch (key_type_from_name(key_type_name)) { |
| 110 | case KEY_RSA1: |
| 111 | name = _PATH_SSH_CLIENT_IDENTITY; |
| 112 | break; |
| 113 | case KEY_DSA: |
| 114 | name = _PATH_SSH_CLIENT_ID_DSA; |
| 115 | break; |
| 116 | case KEY_RSA: |
| 117 | name = _PATH_SSH_CLIENT_ID_RSA; |
| 118 | break; |
| 119 | default: |
| 120 | fprintf(stderr, "bad key type"); |
| 121 | exit(1); |
| 122 | break; |
| 123 | } |
| 124 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 125 | snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name); |
Ben Lindstrom | 3deda8b | 2000-12-22 20:27:43 +0000 | [diff] [blame] | 126 | fprintf(stderr, "%s (%s): ", prompt, identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 127 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 128 | exit(1); |
| 129 | if (strchr(buf, '\n')) |
| 130 | *strchr(buf, '\n') = 0; |
| 131 | if (strcmp(buf, "") != 0) |
| 132 | strlcpy(identity_file, buf, sizeof(identity_file)); |
| 133 | have_identity = 1; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 134 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 135 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 136 | static Key * |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 137 | load_identity(char *filename) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 138 | { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 139 | char *pass; |
| 140 | Key *prv; |
| 141 | |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 142 | prv = key_load_private(filename, "", NULL); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 143 | if (prv == NULL) { |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 144 | if (identity_passphrase) |
| 145 | pass = xstrdup(identity_passphrase); |
| 146 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 147 | pass = read_passphrase("Enter passphrase: ", |
| 148 | RP_ALLOW_STDIN); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 149 | prv = key_load_private(filename, pass, NULL); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 150 | memset(pass, 0, strlen(pass)); |
| 151 | xfree(pass); |
| 152 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 153 | return prv; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 154 | } |
| 155 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 156 | #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----" |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 157 | #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 158 | #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----" |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 159 | #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 160 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 161 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 162 | do_convert_to_ssh2(struct passwd *pw) |
| 163 | { |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 164 | Key *k; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 165 | u_int len; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 166 | u_char *blob; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 167 | struct stat st; |
| 168 | |
| 169 | if (!have_identity) |
| 170 | ask_filename(pw, "Enter file in which the key is"); |
| 171 | if (stat(identity_file, &st) < 0) { |
| 172 | perror(identity_file); |
| 173 | exit(1); |
| 174 | } |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 175 | if ((k = key_load_public(identity_file, NULL)) == NULL) { |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 176 | if ((k = load_identity(identity_file)) == NULL) { |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 177 | fprintf(stderr, "load failed\n"); |
| 178 | exit(1); |
| 179 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 180 | } |
Damien Miller | db27472 | 2003-05-14 13:45:22 +1000 | [diff] [blame] | 181 | if (k->type == KEY_RSA1) { |
| 182 | fprintf(stderr, "version 1 keys are not supported\n"); |
| 183 | exit(1); |
| 184 | } |
Ben Lindstrom | 99a30f1 | 2001-09-18 05:49:14 +0000 | [diff] [blame] | 185 | if (key_to_blob(k, &blob, &len) <= 0) { |
| 186 | fprintf(stderr, "key_to_blob failed\n"); |
| 187 | exit(1); |
| 188 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 189 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 190 | fprintf(stdout, |
Ben Lindstrom | 58d3b72 | 2002-06-23 21:28:13 +0000 | [diff] [blame] | 191 | "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n", |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 192 | key_size(k), key_type(k), |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 193 | pw->pw_name, hostname); |
| 194 | dump_base64(stdout, blob, len); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 195 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END); |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 196 | key_free(k); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 197 | xfree(blob); |
| 198 | exit(0); |
| 199 | } |
| 200 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 201 | static void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 202 | buffer_get_bignum_bits(Buffer *b, BIGNUM *value) |
| 203 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 204 | u_int bignum_bits = buffer_get_int(b); |
| 205 | u_int bytes = (bignum_bits + 7) / 8; |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 206 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 207 | if (buffer_len(b) < bytes) |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 208 | fatal("buffer_get_bignum_bits: input buffer too small: " |
| 209 | "need %d have %d", bytes, buffer_len(b)); |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 210 | BN_bin2bn(buffer_ptr(b), bytes, value); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 211 | buffer_consume(b, bytes); |
| 212 | } |
| 213 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 214 | static Key * |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 215 | do_convert_private_ssh2_from_blob(u_char *blob, u_int blen) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 216 | { |
| 217 | Buffer b; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 218 | Key *key = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 219 | char *type, *cipher; |
Ben Lindstrom | 511d69e | 2001-07-04 05:05:27 +0000 | [diff] [blame] | 220 | u_char *sig, data[] = "abcde12345"; |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 221 | int magic, rlen, ktype, i1, i2, i3, i4; |
| 222 | u_int slen; |
| 223 | u_long e; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 224 | |
| 225 | buffer_init(&b); |
| 226 | buffer_append(&b, blob, blen); |
| 227 | |
| 228 | magic = buffer_get_int(&b); |
| 229 | if (magic != SSH_COM_PRIVATE_KEY_MAGIC) { |
| 230 | error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC); |
| 231 | buffer_free(&b); |
| 232 | return NULL; |
| 233 | } |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 234 | i1 = buffer_get_int(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 235 | type = buffer_get_string(&b, NULL); |
| 236 | cipher = buffer_get_string(&b, NULL); |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 237 | i2 = buffer_get_int(&b); |
| 238 | i3 = buffer_get_int(&b); |
| 239 | i4 = buffer_get_int(&b); |
| 240 | debug("ignore (%d %d %d %d)", i1,i2,i3,i4); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 241 | if (strcmp(cipher, "none") != 0) { |
| 242 | error("unsupported cipher %s", cipher); |
| 243 | xfree(cipher); |
| 244 | buffer_free(&b); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 245 | xfree(type); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 246 | return NULL; |
| 247 | } |
| 248 | xfree(cipher); |
| 249 | |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 250 | if (strstr(type, "dsa")) { |
| 251 | ktype = KEY_DSA; |
| 252 | } else if (strstr(type, "rsa")) { |
| 253 | ktype = KEY_RSA; |
| 254 | } else { |
Darren Tucker | 7cfeecf | 2005-01-20 10:56:31 +1100 | [diff] [blame] | 255 | buffer_free(&b); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 256 | xfree(type); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 257 | return NULL; |
| 258 | } |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 259 | key = key_new_private(ktype); |
| 260 | xfree(type); |
| 261 | |
| 262 | switch (key->type) { |
| 263 | case KEY_DSA: |
| 264 | buffer_get_bignum_bits(&b, key->dsa->p); |
| 265 | buffer_get_bignum_bits(&b, key->dsa->g); |
| 266 | buffer_get_bignum_bits(&b, key->dsa->q); |
| 267 | buffer_get_bignum_bits(&b, key->dsa->pub_key); |
| 268 | buffer_get_bignum_bits(&b, key->dsa->priv_key); |
| 269 | break; |
| 270 | case KEY_RSA: |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 271 | e = buffer_get_char(&b); |
| 272 | debug("e %lx", e); |
| 273 | if (e < 30) { |
| 274 | e <<= 8; |
| 275 | e += buffer_get_char(&b); |
| 276 | debug("e %lx", e); |
| 277 | e <<= 8; |
| 278 | e += buffer_get_char(&b); |
| 279 | debug("e %lx", e); |
| 280 | } |
| 281 | if (!BN_set_word(key->rsa->e, e)) { |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 282 | buffer_free(&b); |
| 283 | key_free(key); |
| 284 | return NULL; |
| 285 | } |
| 286 | buffer_get_bignum_bits(&b, key->rsa->d); |
| 287 | buffer_get_bignum_bits(&b, key->rsa->n); |
| 288 | buffer_get_bignum_bits(&b, key->rsa->iqmp); |
| 289 | buffer_get_bignum_bits(&b, key->rsa->q); |
| 290 | buffer_get_bignum_bits(&b, key->rsa->p); |
Ben Lindstrom | f7297dd | 2001-07-04 05:02:23 +0000 | [diff] [blame] | 291 | rsa_generate_additional_parameters(key->rsa); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 292 | break; |
| 293 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 294 | rlen = buffer_len(&b); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 295 | if (rlen != 0) |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 296 | error("do_convert_private_ssh2_from_blob: " |
| 297 | "remaining bytes in key blob %d", rlen); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 298 | buffer_free(&b); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 299 | |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 300 | /* try the key */ |
| 301 | key_sign(key, &sig, &slen, data, sizeof(data)); |
| 302 | key_verify(key, sig, slen, data, sizeof(data)); |
| 303 | xfree(sig); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 304 | return key; |
| 305 | } |
| 306 | |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 307 | static int |
| 308 | get_line(FILE *fp, char *line, size_t len) |
| 309 | { |
| 310 | int c; |
| 311 | size_t pos = 0; |
| 312 | |
| 313 | line[0] = '\0'; |
| 314 | while ((c = fgetc(fp)) != EOF) { |
| 315 | if (pos >= len - 1) { |
| 316 | fprintf(stderr, "input line too long.\n"); |
| 317 | exit(1); |
| 318 | } |
| 319 | switch(c) { |
| 320 | case '\r': |
| 321 | c = fgetc(fp); |
| 322 | if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) { |
| 323 | fprintf(stderr, "unget: %s\n", strerror(errno)); |
| 324 | exit(1); |
| 325 | } |
| 326 | return pos; |
| 327 | case '\n': |
| 328 | return pos; |
| 329 | } |
| 330 | line[pos++] = c; |
| 331 | line[pos] = '\0'; |
| 332 | } |
| 333 | return pos; |
| 334 | } |
| 335 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 336 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 337 | do_convert_from_ssh2(struct passwd *pw) |
| 338 | { |
| 339 | Key *k; |
| 340 | int blen; |
Ben Lindstrom | 155b981 | 2002-04-02 20:26:26 +0000 | [diff] [blame] | 341 | u_int len; |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 342 | char line[1024]; |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 343 | u_char blob[8096]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 344 | char encoded[8096]; |
| 345 | struct stat st; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 346 | int escaped = 0, private = 0, ok; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 347 | FILE *fp; |
| 348 | |
| 349 | if (!have_identity) |
| 350 | ask_filename(pw, "Enter file in which the key is"); |
| 351 | if (stat(identity_file, &st) < 0) { |
| 352 | perror(identity_file); |
| 353 | exit(1); |
| 354 | } |
| 355 | fp = fopen(identity_file, "r"); |
| 356 | if (fp == NULL) { |
| 357 | perror(identity_file); |
| 358 | exit(1); |
| 359 | } |
| 360 | encoded[0] = '\0'; |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 361 | while ((blen = get_line(fp, line, sizeof(line))) != -1) { |
| 362 | if (line[blen - 1] == '\\') |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 363 | escaped++; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 364 | if (strncmp(line, "----", 4) == 0 || |
| 365 | strstr(line, ": ") != NULL) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 366 | if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL) |
| 367 | private = 1; |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 368 | if (strstr(line, " END ") != NULL) { |
| 369 | break; |
| 370 | } |
Ben Lindstrom | 3035860 | 2001-04-24 16:59:28 +0000 | [diff] [blame] | 371 | /* fprintf(stderr, "ignore: %s", line); */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 372 | continue; |
| 373 | } |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 374 | if (escaped) { |
| 375 | escaped--; |
Ben Lindstrom | 3035860 | 2001-04-24 16:59:28 +0000 | [diff] [blame] | 376 | /* fprintf(stderr, "escaped: %s", line); */ |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 377 | continue; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 378 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 379 | strlcat(encoded, line, sizeof(encoded)); |
| 380 | } |
Ben Lindstrom | 155b981 | 2002-04-02 20:26:26 +0000 | [diff] [blame] | 381 | len = strlen(encoded); |
| 382 | if (((len % 4) == 3) && |
| 383 | (encoded[len-1] == '=') && |
| 384 | (encoded[len-2] == '=') && |
| 385 | (encoded[len-3] == '=')) |
| 386 | encoded[len-3] = '\0'; |
Damien Miller | 4a8ed54 | 2002-01-22 23:33:31 +1100 | [diff] [blame] | 387 | blen = uudecode(encoded, blob, sizeof(blob)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 388 | if (blen < 0) { |
| 389 | fprintf(stderr, "uudecode failed.\n"); |
| 390 | exit(1); |
| 391 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 392 | k = private ? |
| 393 | do_convert_private_ssh2_from_blob(blob, blen) : |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 394 | key_from_blob(blob, blen); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 395 | if (k == NULL) { |
| 396 | fprintf(stderr, "decode blob failed.\n"); |
| 397 | exit(1); |
| 398 | } |
| 399 | ok = private ? |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 400 | (k->type == KEY_DSA ? |
| 401 | PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) : |
| 402 | PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) : |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 403 | key_write(k, stdout); |
| 404 | if (!ok) { |
| 405 | fprintf(stderr, "key write failed"); |
| 406 | exit(1); |
| 407 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 408 | key_free(k); |
Damien Miller | a1db12b | 2002-01-22 23:20:15 +1100 | [diff] [blame] | 409 | if (!private) |
| 410 | fprintf(stdout, "\n"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 411 | fclose(fp); |
| 412 | exit(0); |
| 413 | } |
| 414 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 415 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 416 | do_print_public(struct passwd *pw) |
| 417 | { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 418 | Key *prv; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 419 | struct stat st; |
| 420 | |
| 421 | if (!have_identity) |
| 422 | ask_filename(pw, "Enter file in which the key is"); |
| 423 | if (stat(identity_file, &st) < 0) { |
| 424 | perror(identity_file); |
| 425 | exit(1); |
| 426 | } |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 427 | prv = load_identity(identity_file); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 428 | if (prv == NULL) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 429 | fprintf(stderr, "load failed\n"); |
| 430 | exit(1); |
| 431 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 432 | if (!key_write(prv, stdout)) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 433 | fprintf(stderr, "key_write failed"); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 434 | key_free(prv); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 435 | fprintf(stdout, "\n"); |
| 436 | exit(0); |
| 437 | } |
| 438 | |
Ben Lindstrom | 6818bfb | 2001-08-06 21:40:04 +0000 | [diff] [blame] | 439 | #ifdef SMARTCARD |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 440 | static void |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 441 | do_upload(struct passwd *pw, const char *sc_reader_id) |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 442 | { |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 443 | Key *prv = NULL; |
| 444 | struct stat st; |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 445 | int ret; |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 446 | |
| 447 | if (!have_identity) |
| 448 | ask_filename(pw, "Enter file in which the key is"); |
| 449 | if (stat(identity_file, &st) < 0) { |
| 450 | perror(identity_file); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 451 | exit(1); |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 452 | } |
| 453 | prv = load_identity(identity_file); |
| 454 | if (prv == NULL) { |
| 455 | error("load failed"); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 456 | exit(1); |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 457 | } |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 458 | ret = sc_put_key(prv, sc_reader_id); |
| 459 | key_free(prv); |
| 460 | if (ret < 0) |
| 461 | exit(1); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 462 | logit("loading key done"); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 463 | exit(0); |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 464 | } |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 465 | |
| 466 | static void |
| 467 | do_download(struct passwd *pw, const char *sc_reader_id) |
| 468 | { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 469 | Key **keys = NULL; |
| 470 | int i; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 471 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 472 | keys = sc_get_keys(sc_reader_id, NULL); |
| 473 | if (keys == NULL) |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 474 | fatal("cannot read public key from smartcard"); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 475 | for (i = 0; keys[i]; i++) { |
| 476 | key_write(keys[i], stdout); |
| 477 | key_free(keys[i]); |
| 478 | fprintf(stdout, "\n"); |
| 479 | } |
| 480 | xfree(keys); |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 481 | exit(0); |
| 482 | } |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 483 | #endif /* SMARTCARD */ |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 484 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 485 | static void |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 486 | do_fingerprint(struct passwd *pw) |
| 487 | { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 488 | FILE *f; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 489 | Key *public; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 490 | char *comment = NULL, *cp, *ep, line[16*1024], *fp; |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 491 | int i, skip = 0, num = 1, invalid = 1; |
| 492 | enum fp_rep rep; |
| 493 | enum fp_type fptype; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 494 | struct stat st; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 495 | |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 496 | fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5; |
| 497 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 498 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 499 | if (!have_identity) |
| 500 | ask_filename(pw, "Enter file in which the key is"); |
| 501 | if (stat(identity_file, &st) < 0) { |
| 502 | perror(identity_file); |
| 503 | exit(1); |
| 504 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 505 | public = key_load_public(identity_file, &comment); |
| 506 | if (public != NULL) { |
| 507 | fp = key_fingerprint(public, fptype, rep); |
Ben Lindstrom | 58d3b72 | 2002-06-23 21:28:13 +0000 | [diff] [blame] | 508 | printf("%u %s %s\n", key_size(public), fp, comment); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 509 | key_free(public); |
| 510 | xfree(comment); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 511 | xfree(fp); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 512 | exit(0); |
| 513 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 514 | if (comment) |
| 515 | xfree(comment); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 516 | |
| 517 | f = fopen(identity_file, "r"); |
| 518 | if (f != NULL) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 519 | while (fgets(line, sizeof(line), f)) { |
| 520 | i = strlen(line) - 1; |
| 521 | if (line[i] != '\n') { |
| 522 | error("line %d too long: %.40s...", num, line); |
| 523 | skip = 1; |
| 524 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 525 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 526 | num++; |
| 527 | if (skip) { |
| 528 | skip = 0; |
| 529 | continue; |
| 530 | } |
| 531 | line[i] = '\0'; |
| 532 | |
| 533 | /* Skip leading whitespace, empty and comment lines. */ |
| 534 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 535 | ; |
| 536 | if (!*cp || *cp == '\n' || *cp == '#') |
| 537 | continue ; |
| 538 | i = strtol(cp, &ep, 10); |
| 539 | if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) { |
| 540 | int quoted = 0; |
| 541 | comment = cp; |
Ben Lindstrom | 58d3b72 | 2002-06-23 21:28:13 +0000 | [diff] [blame] | 542 | for (; *cp && (quoted || (*cp != ' ' && |
| 543 | *cp != '\t')); cp++) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 544 | if (*cp == '\\' && cp[1] == '"') |
| 545 | cp++; /* Skip both */ |
| 546 | else if (*cp == '"') |
| 547 | quoted = !quoted; |
| 548 | } |
| 549 | if (!*cp) |
| 550 | continue; |
| 551 | *cp++ = '\0'; |
| 552 | } |
| 553 | ep = cp; |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 554 | public = key_new(KEY_RSA1); |
| 555 | if (key_read(public, &cp) != 1) { |
| 556 | cp = ep; |
| 557 | key_free(public); |
| 558 | public = key_new(KEY_UNSPEC); |
| 559 | if (key_read(public, &cp) != 1) { |
| 560 | key_free(public); |
| 561 | continue; |
| 562 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 563 | } |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 564 | comment = *cp ? cp : comment; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 565 | fp = key_fingerprint(public, fptype, rep); |
Ben Lindstrom | 58d3b72 | 2002-06-23 21:28:13 +0000 | [diff] [blame] | 566 | printf("%u %s %s\n", key_size(public), fp, |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 567 | comment ? comment : "no comment"); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 568 | xfree(fp); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 569 | key_free(public); |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 570 | invalid = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 571 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 572 | fclose(f); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 573 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 574 | if (invalid) { |
Damien Miller | eb5fec6 | 2001-11-12 10:52:44 +1100 | [diff] [blame] | 575 | printf("%s is not a public key file.\n", identity_file); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 576 | exit(1); |
| 577 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 578 | exit(0); |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 579 | } |
| 580 | |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 581 | static void |
| 582 | print_host(FILE *f, char *name, Key *public, int hash) |
| 583 | { |
| 584 | if (hash && (name = host_hash(name, NULL, 0)) == NULL) |
| 585 | fatal("hash_host failed"); |
| 586 | fprintf(f, "%s ", name); |
| 587 | if (!key_write(public, f)) |
| 588 | fatal("key_write failed"); |
| 589 | fprintf(f, "\n"); |
| 590 | } |
| 591 | |
| 592 | static void |
| 593 | do_known_hosts(struct passwd *pw, const char *name) |
| 594 | { |
| 595 | FILE *in, *out = stdout; |
| 596 | Key *public; |
| 597 | char *cp, *cp2, *kp, *kp2; |
| 598 | char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN]; |
| 599 | int c, i, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0; |
| 600 | |
| 601 | if (!have_identity) { |
| 602 | cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid); |
| 603 | if (strlcpy(identity_file, cp, sizeof(identity_file)) >= |
| 604 | sizeof(identity_file)) |
| 605 | fatal("Specified known hosts path too long"); |
| 606 | xfree(cp); |
| 607 | have_identity = 1; |
| 608 | } |
| 609 | if ((in = fopen(identity_file, "r")) == NULL) |
| 610 | fatal("fopen: %s", strerror(errno)); |
| 611 | |
| 612 | /* |
| 613 | * Find hosts goes to stdout, hash and deletions happen in-place |
| 614 | * A corner case is ssh-keygen -HF foo, which should go to stdout |
| 615 | */ |
| 616 | if (!find_host && (hash_hosts || delete_host)) { |
| 617 | if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) || |
| 618 | strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) || |
| 619 | strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) || |
| 620 | strlcat(old, ".old", sizeof(old)) >= sizeof(old)) |
| 621 | fatal("known_hosts path too long"); |
| 622 | umask(077); |
| 623 | if ((c = mkstemp(tmp)) == -1) |
| 624 | fatal("mkstemp: %s", strerror(errno)); |
| 625 | if ((out = fdopen(c, "w")) == NULL) { |
| 626 | c = errno; |
| 627 | unlink(tmp); |
| 628 | fatal("fdopen: %s", strerror(c)); |
| 629 | } |
| 630 | inplace = 1; |
| 631 | } |
| 632 | |
| 633 | while (fgets(line, sizeof(line), in)) { |
| 634 | num++; |
| 635 | i = strlen(line) - 1; |
| 636 | if (line[i] != '\n') { |
| 637 | error("line %d too long: %.40s...", num, line); |
| 638 | skip = 1; |
| 639 | invalid = 1; |
| 640 | continue; |
| 641 | } |
| 642 | if (skip) { |
| 643 | skip = 0; |
| 644 | continue; |
| 645 | } |
| 646 | line[i] = '\0'; |
| 647 | |
| 648 | /* Skip leading whitespace, empty and comment lines. */ |
| 649 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 650 | ; |
| 651 | if (!*cp || *cp == '\n' || *cp == '#') { |
| 652 | if (inplace) |
| 653 | fprintf(out, "%s\n", cp); |
| 654 | continue; |
| 655 | } |
| 656 | /* Find the end of the host name portion. */ |
| 657 | for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++) |
| 658 | ; |
| 659 | if (*kp == '\0' || *(kp + 1) == '\0') { |
| 660 | error("line %d missing key: %.40s...", |
| 661 | num, line); |
| 662 | invalid = 1; |
| 663 | continue; |
| 664 | } |
| 665 | *kp++ = '\0'; |
| 666 | kp2 = kp; |
| 667 | |
| 668 | public = key_new(KEY_RSA1); |
| 669 | if (key_read(public, &kp) != 1) { |
| 670 | kp = kp2; |
| 671 | key_free(public); |
| 672 | public = key_new(KEY_UNSPEC); |
| 673 | if (key_read(public, &kp) != 1) { |
| 674 | error("line %d invalid key: %.40s...", |
| 675 | num, line); |
| 676 | key_free(public); |
| 677 | invalid = 1; |
| 678 | continue; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | if (*cp == HASH_DELIM) { |
| 683 | if (find_host || delete_host) { |
| 684 | cp2 = host_hash(name, cp, strlen(cp)); |
| 685 | if (cp2 == NULL) { |
| 686 | error("line %d: invalid hashed " |
| 687 | "name: %.64s...", num, line); |
| 688 | invalid = 1; |
| 689 | continue; |
| 690 | } |
| 691 | c = (strcmp(cp2, cp) == 0); |
| 692 | if (find_host && c) { |
| 693 | printf("# Host %s found: " |
| 694 | "line %d type %s\n", name, |
| 695 | num, key_type(public)); |
| 696 | print_host(out, cp, public, 0); |
| 697 | } |
| 698 | if (delete_host && !c) |
| 699 | print_host(out, cp, public, 0); |
| 700 | } else if (hash_hosts) |
| 701 | print_host(out, cp, public, 0); |
| 702 | } else { |
| 703 | if (find_host || delete_host) { |
| 704 | c = (match_hostname(name, cp, |
| 705 | strlen(cp)) == 1); |
| 706 | if (find_host && c) { |
| 707 | printf("# Host %s found: " |
| 708 | "line %d type %s\n", name, |
| 709 | num, key_type(public)); |
| 710 | print_host(out, cp, public, hash_hosts); |
| 711 | } |
| 712 | if (delete_host && !c) |
| 713 | print_host(out, cp, public, 0); |
| 714 | } else if (hash_hosts) { |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 715 | for (cp2 = strsep(&cp, ","); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 716 | cp2 != NULL && *cp2 != '\0'; |
Damien Miller | 89eac80 | 2005-03-02 12:33:04 +1100 | [diff] [blame] | 717 | cp2 = strsep(&cp, ",")) { |
| 718 | if (strcspn(cp2, "*?!") != strlen(cp2)) |
| 719 | fprintf(stderr, "Warning: " |
| 720 | "ignoring host name with " |
| 721 | "metacharacters: %.64s\n", |
| 722 | cp2); |
| 723 | else |
| 724 | print_host(out, cp2, public, 1); |
| 725 | } |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 726 | has_unhashed = 1; |
| 727 | } |
| 728 | } |
| 729 | key_free(public); |
| 730 | } |
| 731 | fclose(in); |
| 732 | |
| 733 | if (invalid) { |
| 734 | fprintf(stderr, "%s is not a valid known_host file.\n", |
| 735 | identity_file); |
| 736 | if (inplace) { |
| 737 | fprintf(stderr, "Not replacing existing known_hosts " |
Darren Tucker | 9f438a9 | 2005-03-14 23:09:18 +1100 | [diff] [blame] | 738 | "file because of errors\n"); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 739 | fclose(out); |
| 740 | unlink(tmp); |
| 741 | } |
| 742 | exit(1); |
| 743 | } |
| 744 | |
| 745 | if (inplace) { |
| 746 | fclose(out); |
| 747 | |
| 748 | /* Backup existing file */ |
| 749 | if (unlink(old) == -1 && errno != ENOENT) |
| 750 | fatal("unlink %.100s: %s", old, strerror(errno)); |
| 751 | if (link(identity_file, old) == -1) |
| 752 | fatal("link %.100s to %.100s: %s", identity_file, old, |
| 753 | strerror(errno)); |
| 754 | /* Move new one into place */ |
| 755 | if (rename(tmp, identity_file) == -1) { |
| 756 | error("rename\"%s\" to \"%s\": %s", tmp, identity_file, |
| 757 | strerror(errno)); |
| 758 | unlink(tmp); |
| 759 | unlink(old); |
| 760 | exit(1); |
| 761 | } |
| 762 | |
| 763 | fprintf(stderr, "%s updated.\n", identity_file); |
| 764 | fprintf(stderr, "Original contents retained as %s\n", old); |
| 765 | if (has_unhashed) { |
| 766 | fprintf(stderr, "WARNING: %s contains unhashed " |
| 767 | "entries\n", old); |
| 768 | fprintf(stderr, "Delete this file to ensure privacy " |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 769 | "of hostnames\n"); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | exit(0); |
| 774 | } |
| 775 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 776 | /* |
| 777 | * Perform changing a passphrase. The argument is the passwd structure |
| 778 | * for the current user. |
| 779 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 780 | static void |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 781 | do_change_passphrase(struct passwd *pw) |
| 782 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 783 | char *comment; |
| 784 | char *old_passphrase, *passphrase1, *passphrase2; |
| 785 | struct stat st; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 786 | Key *private; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 787 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 788 | if (!have_identity) |
| 789 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 790 | if (stat(identity_file, &st) < 0) { |
| 791 | perror(identity_file); |
| 792 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 793 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 794 | /* Try to load the file with empty passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 795 | private = key_load_private(identity_file, "", &comment); |
| 796 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 797 | if (identity_passphrase) |
| 798 | old_passphrase = xstrdup(identity_passphrase); |
| 799 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 800 | old_passphrase = |
| 801 | read_passphrase("Enter old passphrase: ", |
| 802 | RP_ALLOW_STDIN); |
| 803 | private = key_load_private(identity_file, old_passphrase, |
| 804 | &comment); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 805 | memset(old_passphrase, 0, strlen(old_passphrase)); |
| 806 | xfree(old_passphrase); |
| 807 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 808 | printf("Bad passphrase.\n"); |
| 809 | exit(1); |
| 810 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 811 | } |
| 812 | printf("Key has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 813 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 814 | /* Ask the new passphrase (twice). */ |
| 815 | if (identity_new_passphrase) { |
| 816 | passphrase1 = xstrdup(identity_new_passphrase); |
| 817 | passphrase2 = NULL; |
| 818 | } else { |
| 819 | passphrase1 = |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 820 | read_passphrase("Enter new passphrase (empty for no " |
| 821 | "passphrase): ", RP_ALLOW_STDIN); |
| 822 | passphrase2 = read_passphrase("Enter same passphrase again: ", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 823 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 824 | |
| 825 | /* Verify that they are the same. */ |
| 826 | if (strcmp(passphrase1, passphrase2) != 0) { |
| 827 | memset(passphrase1, 0, strlen(passphrase1)); |
| 828 | memset(passphrase2, 0, strlen(passphrase2)); |
| 829 | xfree(passphrase1); |
| 830 | xfree(passphrase2); |
| 831 | printf("Pass phrases do not match. Try again.\n"); |
| 832 | exit(1); |
| 833 | } |
| 834 | /* Destroy the other copy. */ |
| 835 | memset(passphrase2, 0, strlen(passphrase2)); |
| 836 | xfree(passphrase2); |
| 837 | } |
| 838 | |
| 839 | /* Save the file using the new passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 840 | if (!key_save_private(private, identity_file, passphrase1, comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 841 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 842 | memset(passphrase1, 0, strlen(passphrase1)); |
| 843 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 844 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 845 | xfree(comment); |
| 846 | exit(1); |
| 847 | } |
| 848 | /* Destroy the passphrase and the copy of the key in memory. */ |
| 849 | memset(passphrase1, 0, strlen(passphrase1)); |
| 850 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 851 | key_free(private); /* Destroys contents */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 852 | xfree(comment); |
| 853 | |
| 854 | printf("Your identification has been saved with the new passphrase.\n"); |
| 855 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 856 | } |
| 857 | |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 858 | /* |
| 859 | * Print the SSHFP RR. |
| 860 | */ |
| 861 | static void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 862 | do_print_resource_record(struct passwd *pw, char *hname) |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 863 | { |
| 864 | Key *public; |
| 865 | char *comment = NULL; |
| 866 | struct stat st; |
| 867 | |
| 868 | if (!have_identity) |
| 869 | ask_filename(pw, "Enter file in which the key is"); |
| 870 | if (stat(identity_file, &st) < 0) { |
| 871 | perror(identity_file); |
| 872 | exit(1); |
| 873 | } |
| 874 | public = key_load_public(identity_file, &comment); |
| 875 | if (public != NULL) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 876 | export_dns_rr(hname, public, stdout, print_generic); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 877 | key_free(public); |
| 878 | xfree(comment); |
| 879 | exit(0); |
| 880 | } |
| 881 | if (comment) |
| 882 | xfree(comment); |
| 883 | |
| 884 | printf("failed to read v2 public key from %s.\n", identity_file); |
| 885 | exit(1); |
| 886 | } |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 887 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 888 | /* |
| 889 | * Change the comment of a private key file. |
| 890 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 891 | static void |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 892 | do_change_comment(struct passwd *pw) |
| 893 | { |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 894 | char new_comment[1024], *comment, *passphrase; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 895 | Key *private; |
| 896 | Key *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 897 | struct stat st; |
| 898 | FILE *f; |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 899 | int fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 900 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 901 | if (!have_identity) |
| 902 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 903 | if (stat(identity_file, &st) < 0) { |
| 904 | perror(identity_file); |
| 905 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 906 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 907 | private = key_load_private(identity_file, "", &comment); |
| 908 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 909 | if (identity_passphrase) |
| 910 | passphrase = xstrdup(identity_passphrase); |
| 911 | else if (identity_new_passphrase) |
| 912 | passphrase = xstrdup(identity_new_passphrase); |
| 913 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 914 | passphrase = read_passphrase("Enter passphrase: ", |
| 915 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 916 | /* Try to load using the passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 917 | private = key_load_private(identity_file, passphrase, &comment); |
| 918 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 919 | memset(passphrase, 0, strlen(passphrase)); |
| 920 | xfree(passphrase); |
| 921 | printf("Bad passphrase.\n"); |
| 922 | exit(1); |
| 923 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 924 | } else { |
| 925 | passphrase = xstrdup(""); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 926 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 927 | if (private->type != KEY_RSA1) { |
| 928 | fprintf(stderr, "Comments are only supported for RSA1 keys.\n"); |
| 929 | key_free(private); |
| 930 | exit(1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 931 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 932 | printf("Key now has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 933 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 934 | if (identity_comment) { |
| 935 | strlcpy(new_comment, identity_comment, sizeof(new_comment)); |
| 936 | } else { |
| 937 | printf("Enter new comment: "); |
| 938 | fflush(stdout); |
| 939 | if (!fgets(new_comment, sizeof(new_comment), stdin)) { |
| 940 | memset(passphrase, 0, strlen(passphrase)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 941 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 942 | exit(1); |
| 943 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 944 | if (strchr(new_comment, '\n')) |
| 945 | *strchr(new_comment, '\n') = 0; |
| 946 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 947 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 948 | /* Save the file using the new passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 949 | if (!key_save_private(private, identity_file, passphrase, new_comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 950 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 951 | memset(passphrase, 0, strlen(passphrase)); |
| 952 | xfree(passphrase); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 953 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 954 | xfree(comment); |
| 955 | exit(1); |
| 956 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 957 | memset(passphrase, 0, strlen(passphrase)); |
| 958 | xfree(passphrase); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 959 | public = key_from_private(private); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 960 | key_free(private); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 961 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 962 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 963 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 964 | if (fd == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 965 | printf("Could not save your public key in %s\n", identity_file); |
| 966 | exit(1); |
| 967 | } |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 968 | f = fdopen(fd, "w"); |
| 969 | if (f == NULL) { |
| 970 | printf("fdopen %s failed", identity_file); |
| 971 | exit(1); |
| 972 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 973 | if (!key_write(public, f)) |
| 974 | fprintf(stderr, "write key failed"); |
| 975 | key_free(public); |
| 976 | fprintf(f, " %s\n", new_comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 977 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 978 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 979 | xfree(comment); |
| 980 | |
| 981 | printf("The comment in your key file has been changed.\n"); |
| 982 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 983 | } |
| 984 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 985 | static void |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 986 | usage(void) |
| 987 | { |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 988 | fprintf(stderr, "Usage: %s [options]\n", __progname); |
| 989 | fprintf(stderr, "Options:\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 990 | fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n"); |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 991 | fprintf(stderr, " -B Show bubblebabble digest of key file.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 992 | fprintf(stderr, " -b bits Number of bits in the key to create.\n"); |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 993 | fprintf(stderr, " -C comment Provide new comment.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 994 | fprintf(stderr, " -c Change comment in private and public key files.\n"); |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 995 | #ifdef SMARTCARD |
| 996 | fprintf(stderr, " -D reader Download public key from smartcard.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 997 | #endif /* SMARTCARD */ |
| 998 | fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n"); |
| 999 | fprintf(stderr, " -F hostname Find hostname in known hosts file.\n"); |
| 1000 | fprintf(stderr, " -f filename Filename of the key file.\n"); |
| 1001 | fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n"); |
| 1002 | fprintf(stderr, " -g Use generic DNS resource record format.\n"); |
| 1003 | fprintf(stderr, " -H Hash names in known_hosts file.\n"); |
| 1004 | fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n"); |
| 1005 | fprintf(stderr, " -l Show fingerprint of key file.\n"); |
| 1006 | fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n"); |
| 1007 | fprintf(stderr, " -N phrase Provide new passphrase.\n"); |
| 1008 | fprintf(stderr, " -P phrase Provide old passphrase.\n"); |
| 1009 | fprintf(stderr, " -p Change passphrase of private key file.\n"); |
| 1010 | fprintf(stderr, " -q Quiet.\n"); |
| 1011 | fprintf(stderr, " -R hostname Remove host from known_hosts file.\n"); |
| 1012 | fprintf(stderr, " -r hostname Print DNS resource record.\n"); |
| 1013 | fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n"); |
| 1014 | fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n"); |
| 1015 | fprintf(stderr, " -t type Specify type of key to create.\n"); |
| 1016 | #ifdef SMARTCARD |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 1017 | fprintf(stderr, " -U reader Upload private key to smartcard.\n"); |
| 1018 | #endif /* SMARTCARD */ |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1019 | fprintf(stderr, " -v Verbose.\n"); |
| 1020 | fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n"); |
| 1021 | fprintf(stderr, " -y Read private key file and print public key.\n"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1022 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1023 | exit(1); |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 1024 | } |
| 1025 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1026 | /* |
| 1027 | * Main program for key management. |
| 1028 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1029 | int |
| 1030 | main(int ac, char **av) |
| 1031 | { |
Damien Miller | a41c8b1 | 2002-01-22 23:05:08 +1100 | [diff] [blame] | 1032 | char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2; |
Tim Rice | 2e0e38e | 2003-09-08 16:11:33 -0700 | [diff] [blame] | 1033 | char out_file[MAXPATHLEN], *reader_id = NULL; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1034 | char *rr_hostname = NULL; |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1035 | Key *private, *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1036 | struct passwd *pw; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1037 | struct stat st; |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1038 | int opt, type, fd, download = 0; |
Darren Tucker | 2db8ae6 | 2005-06-01 23:02:25 +1000 | [diff] [blame] | 1039 | u_int32_t memory = 0, generator_wanted = 0, trials = 100; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1040 | int do_gen_candidates = 0, do_screen_candidates = 0; |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1041 | int log_level = SYSLOG_LEVEL_INFO; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1042 | BIGNUM *start = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1043 | FILE *f; |
Damien Miller | 02e754f | 2005-05-26 12:19:39 +1000 | [diff] [blame] | 1044 | const char *errstr; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1045 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1046 | extern int optind; |
| 1047 | extern char *optarg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1048 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1049 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 1050 | sanitise_stdfd(); |
| 1051 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 1052 | __progname = ssh_get_progname(av[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 1053 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 1054 | SSLeay_add_all_algorithms(); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1055 | log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1); |
| 1056 | |
Kevin Steves | 3a88191 | 2002-07-20 19:05:40 +0000 | [diff] [blame] | 1057 | init_rng(); |
| 1058 | seed_rng(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1059 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1060 | /* we need this for the home * directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1061 | pw = getpwuid(getuid()); |
| 1062 | if (!pw) { |
| 1063 | printf("You don't exist, go away!\n"); |
| 1064 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1065 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1066 | if (gethostname(hostname, sizeof(hostname)) < 0) { |
| 1067 | perror("gethostname"); |
| 1068 | exit(1); |
| 1069 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1070 | |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1071 | while ((opt = getopt(ac, av, |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1072 | "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1073 | switch (opt) { |
| 1074 | case 'b': |
Darren Tucker | 9f64733 | 2005-11-28 16:41:46 +1100 | [diff] [blame] | 1075 | bits = strtonum(optarg, 768, 32768, &errstr); |
Damien Miller | 02e754f | 2005-05-26 12:19:39 +1000 | [diff] [blame] | 1076 | if (errstr) |
| 1077 | fatal("Bits has bad value %s (%s)", |
| 1078 | optarg, errstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1079 | break; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1080 | case 'F': |
| 1081 | find_host = 1; |
| 1082 | rr_hostname = optarg; |
| 1083 | break; |
| 1084 | case 'H': |
| 1085 | hash_hosts = 1; |
| 1086 | break; |
| 1087 | case 'R': |
| 1088 | delete_host = 1; |
| 1089 | rr_hostname = optarg; |
| 1090 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1091 | case 'l': |
| 1092 | print_fingerprint = 1; |
| 1093 | break; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 1094 | case 'B': |
| 1095 | print_bubblebabble = 1; |
| 1096 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1097 | case 'p': |
| 1098 | change_passphrase = 1; |
| 1099 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1100 | case 'c': |
| 1101 | change_comment = 1; |
| 1102 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1103 | case 'f': |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1104 | if (strlcpy(identity_file, optarg, sizeof(identity_file)) >= |
| 1105 | sizeof(identity_file)) |
| 1106 | fatal("Identity filename too long"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1107 | have_identity = 1; |
| 1108 | break; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1109 | case 'g': |
| 1110 | print_generic = 1; |
| 1111 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1112 | case 'P': |
| 1113 | identity_passphrase = optarg; |
| 1114 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1115 | case 'N': |
| 1116 | identity_new_passphrase = optarg; |
| 1117 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1118 | case 'C': |
| 1119 | identity_comment = optarg; |
| 1120 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1121 | case 'q': |
| 1122 | quiet = 1; |
| 1123 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1124 | case 'e': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1125 | case 'x': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1126 | /* export key */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1127 | convert_to_ssh2 = 1; |
| 1128 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1129 | case 'i': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1130 | case 'X': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1131 | /* import key */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1132 | convert_from_ssh2 = 1; |
| 1133 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1134 | case 'y': |
| 1135 | print_public = 1; |
| 1136 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1137 | case 'd': |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1138 | key_type_name = "dsa"; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1139 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1140 | case 't': |
| 1141 | key_type_name = optarg; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1142 | break; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 1143 | case 'D': |
| 1144 | download = 1; |
Ben Lindstrom | f19578c | 2001-08-06 21:46:54 +0000 | [diff] [blame] | 1145 | case 'U': |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 1146 | reader_id = optarg; |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 1147 | break; |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1148 | case 'v': |
| 1149 | if (log_level == SYSLOG_LEVEL_INFO) |
| 1150 | log_level = SYSLOG_LEVEL_DEBUG1; |
| 1151 | else { |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1152 | if (log_level >= SYSLOG_LEVEL_DEBUG1 && |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1153 | log_level < SYSLOG_LEVEL_DEBUG3) |
| 1154 | log_level++; |
| 1155 | } |
| 1156 | break; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1157 | case 'r': |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1158 | rr_hostname = optarg; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1159 | break; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1160 | case 'W': |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1161 | generator_wanted = strtonum(optarg, 1, UINT_MAX, &errstr); |
| 1162 | if (errstr) |
| 1163 | fatal("Desired generator has bad value: %s (%s)", |
| 1164 | optarg, errstr); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1165 | break; |
| 1166 | case 'a': |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1167 | trials = strtonum(optarg, 1, UINT_MAX, &errstr); |
| 1168 | if (errstr) |
| 1169 | fatal("Invalid number of trials: %s (%s)", |
| 1170 | optarg, errstr); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1171 | break; |
| 1172 | case 'M': |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1173 | memory = strtonum(optarg, 1, UINT_MAX, &errstr); |
| 1174 | if (errstr) { |
| 1175 | fatal("Memory limit is %s: %s", errstr, optarg); |
| 1176 | } |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1177 | break; |
| 1178 | case 'G': |
| 1179 | do_gen_candidates = 1; |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1180 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= |
| 1181 | sizeof(out_file)) |
| 1182 | fatal("Output filename too long"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1183 | break; |
| 1184 | case 'T': |
| 1185 | do_screen_candidates = 1; |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1186 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= |
| 1187 | sizeof(out_file)) |
| 1188 | fatal("Output filename too long"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1189 | break; |
| 1190 | case 'S': |
| 1191 | /* XXX - also compare length against bits */ |
| 1192 | if (BN_hex2bn(&start, optarg) == 0) |
| 1193 | fatal("Invalid start point."); |
| 1194 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1195 | case '?': |
| 1196 | default: |
| 1197 | usage(); |
| 1198 | } |
| 1199 | } |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1200 | |
| 1201 | /* reinit */ |
| 1202 | log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1); |
| 1203 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1204 | if (optind < ac) { |
| 1205 | printf("Too many arguments.\n"); |
| 1206 | usage(); |
| 1207 | } |
| 1208 | if (change_passphrase && change_comment) { |
| 1209 | printf("Can only have one of -p and -c.\n"); |
| 1210 | usage(); |
| 1211 | } |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1212 | if (delete_host || hash_hosts || find_host) |
| 1213 | do_known_hosts(pw, rr_hostname); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 1214 | if (print_fingerprint || print_bubblebabble) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1215 | do_fingerprint(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1216 | if (change_passphrase) |
| 1217 | do_change_passphrase(pw); |
Damien Miller | 4a10d2e | 2002-03-11 22:53:29 +1100 | [diff] [blame] | 1218 | if (change_comment) |
| 1219 | do_change_comment(pw); |
Kevin Steves | 3a88191 | 2002-07-20 19:05:40 +0000 | [diff] [blame] | 1220 | if (convert_to_ssh2) |
| 1221 | do_convert_to_ssh2(pw); |
| 1222 | if (convert_from_ssh2) |
| 1223 | do_convert_from_ssh2(pw); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1224 | if (print_public) |
| 1225 | do_print_public(pw); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1226 | if (rr_hostname != NULL) { |
| 1227 | do_print_resource_record(pw, rr_hostname); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1228 | } |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 1229 | if (reader_id != NULL) { |
Ben Lindstrom | 6818bfb | 2001-08-06 21:40:04 +0000 | [diff] [blame] | 1230 | #ifdef SMARTCARD |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 1231 | if (download) |
| 1232 | do_download(pw, reader_id); |
| 1233 | else |
| 1234 | do_upload(pw, reader_id); |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 1235 | #else /* SMARTCARD */ |
Ben Lindstrom | 6818bfb | 2001-08-06 21:40:04 +0000 | [diff] [blame] | 1236 | fatal("no support for smartcards."); |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 1237 | #endif /* SMARTCARD */ |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 1238 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1239 | |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1240 | if (do_gen_candidates) { |
| 1241 | FILE *out = fopen(out_file, "w"); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1242 | |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1243 | if (out == NULL) { |
| 1244 | error("Couldn't open modulus candidate file \"%s\": %s", |
| 1245 | out_file, strerror(errno)); |
| 1246 | return (1); |
| 1247 | } |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 1248 | if (bits == 0) |
| 1249 | bits = DEFAULT_BITS; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1250 | if (gen_candidates(out, memory, bits, start) != 0) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 1251 | fatal("modulus candidate generation failed"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1252 | |
| 1253 | return (0); |
| 1254 | } |
| 1255 | |
| 1256 | if (do_screen_candidates) { |
| 1257 | FILE *in; |
| 1258 | FILE *out = fopen(out_file, "w"); |
| 1259 | |
| 1260 | if (have_identity && strcmp(identity_file, "-") != 0) { |
| 1261 | if ((in = fopen(identity_file, "r")) == NULL) { |
| 1262 | fatal("Couldn't open modulus candidate " |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1263 | "file \"%s\": %s", identity_file, |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1264 | strerror(errno)); |
| 1265 | } |
| 1266 | } else |
| 1267 | in = stdin; |
| 1268 | |
| 1269 | if (out == NULL) { |
| 1270 | fatal("Couldn't open moduli file \"%s\": %s", |
| 1271 | out_file, strerror(errno)); |
| 1272 | } |
| 1273 | if (prime_test(in, out, trials, generator_wanted) != 0) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 1274 | fatal("modulus screening failed"); |
Darren Tucker | f4220e6 | 2003-08-21 16:44:07 +1000 | [diff] [blame] | 1275 | return (0); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1276 | } |
| 1277 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1278 | arc4random_stir(); |
| 1279 | |
Damien Miller | f14be5c | 2005-11-05 15:15:49 +1100 | [diff] [blame] | 1280 | if (key_type_name == NULL) |
| 1281 | key_type_name = "rsa"; |
| 1282 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 1283 | type = key_type_from_name(key_type_name); |
| 1284 | if (type == KEY_UNSPEC) { |
| 1285 | fprintf(stderr, "unknown key type %s\n", key_type_name); |
| 1286 | exit(1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1287 | } |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 1288 | if (bits == 0) |
| 1289 | bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS; |
Tim Rice | 660c340 | 2005-11-28 17:45:32 -0800 | [diff] [blame] | 1290 | if (type == KEY_DSA && bits != 1024) |
| 1291 | fatal("DSA keys must be 1024 bits"); |
Darren Tucker | 3af2ac5 | 2005-11-29 13:10:24 +1100 | [diff] [blame] | 1292 | if (!quiet) |
| 1293 | printf("Generating public/private %s key pair.\n", key_type_name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1294 | private = key_generate(type, bits); |
| 1295 | if (private == NULL) { |
| 1296 | fprintf(stderr, "key_generate failed"); |
| 1297 | exit(1); |
| 1298 | } |
| 1299 | public = key_from_private(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1300 | |
| 1301 | if (!have_identity) |
| 1302 | ask_filename(pw, "Enter file in which to save the key"); |
| 1303 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 1304 | /* Create ~/.ssh directory if it doesn't already exist. */ |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1305 | snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1306 | if (strstr(identity_file, dotsshdir) != NULL && |
| 1307 | stat(dotsshdir, &st) < 0) { |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1308 | if (mkdir(dotsshdir, 0700) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1309 | error("Could not create directory '%s'.", dotsshdir); |
| 1310 | else if (!quiet) |
| 1311 | printf("Created directory '%s'.\n", dotsshdir); |
| 1312 | } |
| 1313 | /* If the file already exists, ask the user to confirm. */ |
| 1314 | if (stat(identity_file, &st) >= 0) { |
| 1315 | char yesno[3]; |
| 1316 | printf("%s already exists.\n", identity_file); |
| 1317 | printf("Overwrite (y/n)? "); |
| 1318 | fflush(stdout); |
| 1319 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) |
| 1320 | exit(1); |
| 1321 | if (yesno[0] != 'y' && yesno[0] != 'Y') |
| 1322 | exit(1); |
| 1323 | } |
| 1324 | /* Ask for a passphrase (twice). */ |
| 1325 | if (identity_passphrase) |
| 1326 | passphrase1 = xstrdup(identity_passphrase); |
| 1327 | else if (identity_new_passphrase) |
| 1328 | passphrase1 = xstrdup(identity_new_passphrase); |
| 1329 | else { |
| 1330 | passphrase_again: |
| 1331 | passphrase1 = |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1332 | read_passphrase("Enter passphrase (empty for no " |
| 1333 | "passphrase): ", RP_ALLOW_STDIN); |
| 1334 | passphrase2 = read_passphrase("Enter same passphrase again: ", |
| 1335 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1336 | if (strcmp(passphrase1, passphrase2) != 0) { |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1337 | /* |
| 1338 | * The passphrases do not match. Clear them and |
| 1339 | * retry. |
| 1340 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1341 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1342 | memset(passphrase2, 0, strlen(passphrase2)); |
| 1343 | xfree(passphrase1); |
| 1344 | xfree(passphrase2); |
| 1345 | printf("Passphrases do not match. Try again.\n"); |
| 1346 | goto passphrase_again; |
| 1347 | } |
| 1348 | /* Clear the other copy of the passphrase. */ |
| 1349 | memset(passphrase2, 0, strlen(passphrase2)); |
| 1350 | xfree(passphrase2); |
| 1351 | } |
| 1352 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1353 | if (identity_comment) { |
| 1354 | strlcpy(comment, identity_comment, sizeof(comment)); |
| 1355 | } else { |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1356 | /* Create default commend field for the passphrase. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1357 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); |
| 1358 | } |
| 1359 | |
| 1360 | /* Save the key with the given passphrase and comment. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1361 | if (!key_save_private(private, identity_file, passphrase1, comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 1362 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1363 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1364 | xfree(passphrase1); |
| 1365 | exit(1); |
| 1366 | } |
| 1367 | /* Clear the passphrase. */ |
| 1368 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1369 | xfree(passphrase1); |
| 1370 | |
| 1371 | /* Clear the private key and the random number generator. */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1372 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1373 | arc4random_stir(); |
| 1374 | |
| 1375 | if (!quiet) |
| 1376 | printf("Your identification has been saved in %s.\n", identity_file); |
| 1377 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1378 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1379 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1380 | if (fd == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1381 | printf("Could not save your public key in %s\n", identity_file); |
| 1382 | exit(1); |
| 1383 | } |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1384 | f = fdopen(fd, "w"); |
| 1385 | if (f == NULL) { |
| 1386 | printf("fdopen %s failed", identity_file); |
| 1387 | exit(1); |
| 1388 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1389 | if (!key_write(public, f)) |
| 1390 | fprintf(stderr, "write key failed"); |
| 1391 | fprintf(f, " %s\n", comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1392 | fclose(f); |
| 1393 | |
| 1394 | if (!quiet) { |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1395 | char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1396 | printf("Your public key has been saved in %s.\n", |
| 1397 | identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1398 | printf("The key fingerprint is:\n"); |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1399 | printf("%s %s\n", fp, comment); |
| 1400 | xfree(fp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1401 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1402 | |
| 1403 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1404 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1405 | } |