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 |
| 5 | * Created: Mon Mar 27 02:26:40 1995 ylo |
| 6 | * Identity and host key generation and maintenance. |
| 7 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 8 | |
| 9 | #include "includes.h" |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 10 | RCSID("$Id: ssh-keygen.c,v 1.17 2000/05/07 02:03:19 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 11 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 12 | #include <openssl/evp.h> |
| 13 | #include <openssl/pem.h> |
| 14 | #include <openssl/rsa.h> |
| 15 | #include <openssl/dsa.h> |
| 16 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 17 | #include "ssh.h" |
| 18 | #include "xmalloc.h" |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 19 | #include "fingerprint.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 20 | #include "key.h" |
| 21 | #include "rsa.h" |
| 22 | #include "dsa.h" |
| 23 | #include "authfile.h" |
| 24 | #include "uuencode.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 25 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 26 | /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | int bits = 1024; |
| 28 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 29 | /* |
| 30 | * Flag indicating that we just want to change the passphrase. This can be |
| 31 | * set on the command line. |
| 32 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 33 | int change_passphrase = 0; |
| 34 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 35 | /* |
| 36 | * Flag indicating that we just want to change the comment. This can be set |
| 37 | * on the command line. |
| 38 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | int change_comment = 0; |
| 40 | |
| 41 | int quiet = 0; |
| 42 | |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 43 | /* Flag indicating that we just want to see the key fingerprint */ |
| 44 | int print_fingerprint = 0; |
| 45 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 46 | /* The identity file name, given on the command line or entered by the user. */ |
| 47 | char identity_file[1024]; |
| 48 | int have_identity = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 49 | |
| 50 | /* This is set to the passphrase if given on the command line. */ |
| 51 | char *identity_passphrase = NULL; |
| 52 | |
| 53 | /* This is set to the new passphrase if given on the command line. */ |
| 54 | char *identity_new_passphrase = NULL; |
| 55 | |
| 56 | /* This is set to the new comment if given on the command line. */ |
| 57 | char *identity_comment = NULL; |
| 58 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 59 | /* Dump public key file in format used by real and the original SSH 2 */ |
| 60 | int convert_to_ssh2 = 0; |
| 61 | int convert_from_ssh2 = 0; |
| 62 | int print_public = 0; |
| 63 | int dsa_mode = 0; |
| 64 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 65 | /* argv0 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 66 | #ifdef HAVE___PROGNAME |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 67 | extern char *__progname; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 68 | #else /* HAVE___PROGNAME */ |
Damien Miller | 70fb671 | 2000-05-01 20:59:50 +1000 | [diff] [blame] | 69 | static const char *__progname = "ssh-keygen"; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | #endif /* HAVE___PROGNAME */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 72 | char hostname[MAXHOSTNAMELEN]; |
| 73 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 74 | void |
| 75 | ask_filename(struct passwd *pw, const char *prompt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 77 | char buf[1024]; |
| 78 | snprintf(identity_file, sizeof(identity_file), "%s/%s", |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 79 | pw->pw_dir, |
| 80 | dsa_mode ? SSH_CLIENT_ID_DSA: SSH_CLIENT_IDENTITY); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 81 | printf("%s (%s): ", prompt, identity_file); |
| 82 | fflush(stdout); |
| 83 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 84 | exit(1); |
| 85 | if (strchr(buf, '\n')) |
| 86 | *strchr(buf, '\n') = 0; |
| 87 | if (strcmp(buf, "") != 0) |
| 88 | strlcpy(identity_file, buf, sizeof(identity_file)); |
| 89 | have_identity = 1; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 90 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 91 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 92 | int |
| 93 | try_load_key(char *filename, Key *k) |
| 94 | { |
| 95 | int success = 1; |
| 96 | if (!load_private_key(filename, "", k, NULL)) { |
| 97 | char *pass = read_passphrase("Enter passphrase: ", 1); |
| 98 | if (!load_private_key(filename, pass, k, NULL)) { |
| 99 | success = 0; |
| 100 | } |
| 101 | memset(pass, 0, strlen(pass)); |
| 102 | xfree(pass); |
| 103 | } |
| 104 | return success; |
| 105 | } |
| 106 | |
| 107 | #define SSH_COM_MAGIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----" |
| 108 | #define SSH_COM_MAGIC_END "---- END SSH2 PUBLIC KEY ----" |
| 109 | |
| 110 | void |
| 111 | do_convert_to_ssh2(struct passwd *pw) |
| 112 | { |
| 113 | Key *k; |
| 114 | int len; |
| 115 | unsigned char *blob; |
| 116 | struct stat st; |
| 117 | |
| 118 | if (!have_identity) |
| 119 | ask_filename(pw, "Enter file in which the key is"); |
| 120 | if (stat(identity_file, &st) < 0) { |
| 121 | perror(identity_file); |
| 122 | exit(1); |
| 123 | } |
| 124 | k = key_new(KEY_DSA); |
| 125 | if (!try_load_key(identity_file, k)) { |
| 126 | fprintf(stderr, "load failed\n"); |
| 127 | exit(1); |
| 128 | } |
| 129 | dsa_make_key_blob(k, &blob, &len); |
| 130 | fprintf(stdout, SSH_COM_MAGIC_BEGIN "\n"); |
| 131 | fprintf(stdout, |
| 132 | "Comment: \"%d-bit DSA, converted from openssh by %s@%s\"\n", |
| 133 | BN_num_bits(k->dsa->p), |
| 134 | pw->pw_name, hostname); |
| 135 | dump_base64(stdout, blob, len); |
| 136 | fprintf(stdout, SSH_COM_MAGIC_END "\n"); |
| 137 | key_free(k); |
| 138 | xfree(blob); |
| 139 | exit(0); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | do_convert_from_ssh2(struct passwd *pw) |
| 144 | { |
| 145 | Key *k; |
| 146 | int blen; |
| 147 | char line[1024], *p; |
| 148 | char blob[8096]; |
| 149 | char encoded[8096]; |
| 150 | struct stat st; |
| 151 | FILE *fp; |
| 152 | |
| 153 | if (!have_identity) |
| 154 | ask_filename(pw, "Enter file in which the key is"); |
| 155 | if (stat(identity_file, &st) < 0) { |
| 156 | perror(identity_file); |
| 157 | exit(1); |
| 158 | } |
| 159 | fp = fopen(identity_file, "r"); |
| 160 | if (fp == NULL) { |
| 161 | perror(identity_file); |
| 162 | exit(1); |
| 163 | } |
| 164 | encoded[0] = '\0'; |
| 165 | while (fgets(line, sizeof(line), fp)) { |
| 166 | if (strncmp(line, "----", 4) == 0 || |
| 167 | strstr(line, ": ") != NULL) { |
| 168 | fprintf(stderr, "ignore: %s", line); |
| 169 | continue; |
| 170 | } |
| 171 | if (!(p = strchr(line, '\n'))) { |
| 172 | fprintf(stderr, "input line too long.\n"); |
| 173 | exit(1); |
| 174 | } |
| 175 | *p = '\0'; |
| 176 | strlcat(encoded, line, sizeof(encoded)); |
| 177 | } |
| 178 | blen = uudecode(encoded, (unsigned char *)blob, sizeof(blob)); |
| 179 | if (blen < 0) { |
| 180 | fprintf(stderr, "uudecode failed.\n"); |
| 181 | exit(1); |
| 182 | } |
| 183 | k = dsa_key_from_blob(blob, blen); |
| 184 | if (!key_write(k, stdout)) |
| 185 | fprintf(stderr, "key_write failed"); |
| 186 | key_free(k); |
| 187 | fprintf(stdout, "\n"); |
| 188 | fclose(fp); |
| 189 | exit(0); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | do_print_public(struct passwd *pw) |
| 194 | { |
| 195 | Key *k; |
| 196 | int len; |
| 197 | unsigned char *blob; |
| 198 | struct stat st; |
| 199 | |
| 200 | if (!have_identity) |
| 201 | ask_filename(pw, "Enter file in which the key is"); |
| 202 | if (stat(identity_file, &st) < 0) { |
| 203 | perror(identity_file); |
| 204 | exit(1); |
| 205 | } |
| 206 | k = key_new(KEY_DSA); |
| 207 | if (!try_load_key(identity_file, k)) { |
| 208 | fprintf(stderr, "load failed\n"); |
| 209 | exit(1); |
| 210 | } |
| 211 | dsa_make_key_blob(k, &blob, &len); |
| 212 | if (!key_write(k, stdout)) |
| 213 | fprintf(stderr, "key_write failed"); |
| 214 | key_free(k); |
| 215 | xfree(blob); |
| 216 | fprintf(stdout, "\n"); |
| 217 | exit(0); |
| 218 | } |
| 219 | |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 220 | void |
| 221 | do_fingerprint(struct passwd *pw) |
| 222 | { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 223 | FILE *f; |
| 224 | BIGNUM *e, *n; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 225 | Key *public; |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 226 | char *comment = NULL, *cp, *ep, line[16*1024]; |
| 227 | int i, skip = 0, num = 1, invalid = 1; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 228 | unsigned int ignore; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 229 | struct stat st; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 230 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | if (!have_identity) |
| 232 | ask_filename(pw, "Enter file in which the key is"); |
| 233 | if (stat(identity_file, &st) < 0) { |
| 234 | perror(identity_file); |
| 235 | exit(1); |
| 236 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 237 | public = key_new(KEY_RSA); |
| 238 | if (load_public_key(identity_file, public, &comment)) { |
| 239 | printf("%d %s %s\n", BN_num_bits(public->rsa->n), |
| 240 | key_fingerprint(public), comment); |
| 241 | key_free(public); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 242 | exit(0); |
| 243 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 244 | key_free(public); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 245 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 246 | /* XXX */ |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 247 | f = fopen(identity_file, "r"); |
| 248 | if (f != NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 249 | n = BN_new(); |
| 250 | e = BN_new(); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 251 | while (fgets(line, sizeof(line), f)) { |
| 252 | i = strlen(line) - 1; |
| 253 | if (line[i] != '\n') { |
| 254 | error("line %d too long: %.40s...", num, line); |
| 255 | skip = 1; |
| 256 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 257 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 258 | num++; |
| 259 | if (skip) { |
| 260 | skip = 0; |
| 261 | continue; |
| 262 | } |
| 263 | line[i] = '\0'; |
| 264 | |
| 265 | /* Skip leading whitespace, empty and comment lines. */ |
| 266 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 267 | ; |
| 268 | if (!*cp || *cp == '\n' || *cp == '#') |
| 269 | continue ; |
| 270 | i = strtol(cp, &ep, 10); |
| 271 | if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) { |
| 272 | int quoted = 0; |
| 273 | comment = cp; |
| 274 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 275 | if (*cp == '\\' && cp[1] == '"') |
| 276 | cp++; /* Skip both */ |
| 277 | else if (*cp == '"') |
| 278 | quoted = !quoted; |
| 279 | } |
| 280 | if (!*cp) |
| 281 | continue; |
| 282 | *cp++ = '\0'; |
| 283 | } |
| 284 | ep = cp; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 285 | if (auth_rsa_read_key(&cp, &ignore, e, n)) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 286 | invalid = 0; |
| 287 | comment = *cp ? cp : comment; |
| 288 | printf("%d %s %s\n", BN_num_bits(n), |
| 289 | fingerprint(e, n), |
| 290 | comment ? comment : "no comment"); |
| 291 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 292 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 293 | BN_free(e); |
| 294 | BN_free(n); |
| 295 | fclose(f); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 296 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 297 | if (invalid) { |
| 298 | printf("%s is not a valid key file.\n", identity_file); |
| 299 | exit(1); |
| 300 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 301 | exit(0); |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 302 | } |
| 303 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 304 | /* |
| 305 | * Perform changing a passphrase. The argument is the passwd structure |
| 306 | * for the current user. |
| 307 | */ |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 308 | void |
| 309 | do_change_passphrase(struct passwd *pw) |
| 310 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 311 | char *comment; |
| 312 | char *old_passphrase, *passphrase1, *passphrase2; |
| 313 | struct stat st; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 314 | Key *private; |
| 315 | Key *public; |
| 316 | int type = dsa_mode ? KEY_DSA : KEY_RSA; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 317 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 318 | if (!have_identity) |
| 319 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 320 | if (stat(identity_file, &st) < 0) { |
| 321 | perror(identity_file); |
| 322 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 323 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 324 | |
| 325 | if (type == KEY_RSA) { |
| 326 | /* XXX this works currently only for RSA */ |
| 327 | public = key_new(type); |
| 328 | if (!load_public_key(identity_file, public, NULL)) { |
| 329 | printf("%s is not a valid key file.\n", identity_file); |
| 330 | exit(1); |
| 331 | } |
| 332 | /* Clear the public key since we are just about to load the whole file. */ |
| 333 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 334 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 335 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 336 | /* Try to load the file with empty passphrase. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 337 | private = key_new(type); |
| 338 | if (!load_private_key(identity_file, "", private, &comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 339 | if (identity_passphrase) |
| 340 | old_passphrase = xstrdup(identity_passphrase); |
| 341 | else |
| 342 | old_passphrase = read_passphrase("Enter old passphrase: ", 1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 343 | if (!load_private_key(identity_file, old_passphrase, private, &comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 344 | memset(old_passphrase, 0, strlen(old_passphrase)); |
| 345 | xfree(old_passphrase); |
| 346 | printf("Bad passphrase.\n"); |
| 347 | exit(1); |
| 348 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 349 | memset(old_passphrase, 0, strlen(old_passphrase)); |
| 350 | xfree(old_passphrase); |
| 351 | } |
| 352 | printf("Key has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 353 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 354 | /* Ask the new passphrase (twice). */ |
| 355 | if (identity_new_passphrase) { |
| 356 | passphrase1 = xstrdup(identity_new_passphrase); |
| 357 | passphrase2 = NULL; |
| 358 | } else { |
| 359 | passphrase1 = |
| 360 | read_passphrase("Enter new passphrase (empty for no passphrase): ", 1); |
| 361 | passphrase2 = read_passphrase("Enter same passphrase again: ", 1); |
| 362 | |
| 363 | /* Verify that they are the same. */ |
| 364 | if (strcmp(passphrase1, passphrase2) != 0) { |
| 365 | memset(passphrase1, 0, strlen(passphrase1)); |
| 366 | memset(passphrase2, 0, strlen(passphrase2)); |
| 367 | xfree(passphrase1); |
| 368 | xfree(passphrase2); |
| 369 | printf("Pass phrases do not match. Try again.\n"); |
| 370 | exit(1); |
| 371 | } |
| 372 | /* Destroy the other copy. */ |
| 373 | memset(passphrase2, 0, strlen(passphrase2)); |
| 374 | xfree(passphrase2); |
| 375 | } |
| 376 | |
| 377 | /* Save the file using the new passphrase. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 378 | if (!save_private_key(identity_file, passphrase1, private, comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 379 | printf("Saving the key failed: %s: %s.\n", |
| 380 | identity_file, strerror(errno)); |
| 381 | memset(passphrase1, 0, strlen(passphrase1)); |
| 382 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 383 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 384 | xfree(comment); |
| 385 | exit(1); |
| 386 | } |
| 387 | /* Destroy the passphrase and the copy of the key in memory. */ |
| 388 | memset(passphrase1, 0, strlen(passphrase1)); |
| 389 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 390 | key_free(private); /* Destroys contents */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 391 | xfree(comment); |
| 392 | |
| 393 | printf("Your identification has been saved with the new passphrase.\n"); |
| 394 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 395 | } |
| 396 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 397 | /* |
| 398 | * Change the comment of a private key file. |
| 399 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 400 | void |
| 401 | do_change_comment(struct passwd *pw) |
| 402 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 403 | char new_comment[1024], *comment; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 404 | Key *private; |
| 405 | Key *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 406 | char *passphrase; |
| 407 | struct stat st; |
| 408 | FILE *f; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 409 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 410 | if (!have_identity) |
| 411 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 412 | if (stat(identity_file, &st) < 0) { |
| 413 | perror(identity_file); |
| 414 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 415 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 416 | /* |
| 417 | * Try to load the public key from the file the verify that it is |
| 418 | * readable and of the proper format. |
| 419 | */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 420 | public = key_new(KEY_RSA); |
| 421 | if (!load_public_key(identity_file, public, NULL)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 422 | printf("%s is not a valid key file.\n", identity_file); |
| 423 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 424 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 425 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 426 | private = key_new(KEY_RSA); |
| 427 | if (load_private_key(identity_file, "", private, &comment)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 428 | passphrase = xstrdup(""); |
| 429 | else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 430 | if (identity_passphrase) |
| 431 | passphrase = xstrdup(identity_passphrase); |
| 432 | else if (identity_new_passphrase) |
| 433 | passphrase = xstrdup(identity_new_passphrase); |
| 434 | else |
| 435 | passphrase = read_passphrase("Enter passphrase: ", 1); |
| 436 | /* Try to load using the passphrase. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 437 | if (!load_private_key(identity_file, passphrase, private, &comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 438 | memset(passphrase, 0, strlen(passphrase)); |
| 439 | xfree(passphrase); |
| 440 | printf("Bad passphrase.\n"); |
| 441 | exit(1); |
| 442 | } |
| 443 | } |
| 444 | printf("Key now has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 445 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 446 | if (identity_comment) { |
| 447 | strlcpy(new_comment, identity_comment, sizeof(new_comment)); |
| 448 | } else { |
| 449 | printf("Enter new comment: "); |
| 450 | fflush(stdout); |
| 451 | if (!fgets(new_comment, sizeof(new_comment), stdin)) { |
| 452 | memset(passphrase, 0, strlen(passphrase)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 453 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 454 | exit(1); |
| 455 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 456 | if (strchr(new_comment, '\n')) |
| 457 | *strchr(new_comment, '\n') = 0; |
| 458 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 459 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 460 | /* Save the file using the new passphrase. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 461 | if (!save_private_key(identity_file, passphrase, private, new_comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 462 | printf("Saving the key failed: %s: %s.\n", |
| 463 | identity_file, strerror(errno)); |
| 464 | memset(passphrase, 0, strlen(passphrase)); |
| 465 | xfree(passphrase); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 466 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 467 | xfree(comment); |
| 468 | exit(1); |
| 469 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 470 | memset(passphrase, 0, strlen(passphrase)); |
| 471 | xfree(passphrase); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 472 | key_free(private); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 473 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 474 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
| 475 | f = fopen(identity_file, "w"); |
| 476 | if (!f) { |
| 477 | printf("Could not save your public key in %s\n", identity_file); |
| 478 | exit(1); |
| 479 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 480 | if (!key_write(public, f)) |
| 481 | fprintf(stderr, "write key failed"); |
| 482 | key_free(public); |
| 483 | fprintf(f, " %s\n", new_comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 484 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 485 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 486 | xfree(comment); |
| 487 | |
| 488 | printf("The comment in your key file has been changed.\n"); |
| 489 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 490 | } |
| 491 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 492 | void |
| 493 | usage(void) |
| 494 | { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 495 | printf("Usage: %s [-lpqxXydc] [-b bits] [-f file] [-C comment] [-N new-pass] [-P pass]\n", __progname); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 496 | exit(1); |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 497 | } |
| 498 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 499 | /* |
| 500 | * Main program for key management. |
| 501 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 502 | int |
| 503 | main(int ac, char **av) |
| 504 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 505 | char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2; |
| 506 | struct passwd *pw; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 507 | int opt; |
| 508 | struct stat st; |
| 509 | FILE *f; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 510 | Key *private; |
| 511 | Key *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 512 | extern int optind; |
| 513 | extern char *optarg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 514 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 515 | OpenSSL_add_all_algorithms(); |
| 516 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 517 | /* we need this for the home * directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 518 | pw = getpwuid(getuid()); |
| 519 | if (!pw) { |
| 520 | printf("You don't exist, go away!\n"); |
| 521 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 522 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 523 | if (gethostname(hostname, sizeof(hostname)) < 0) { |
| 524 | perror("gethostname"); |
| 525 | exit(1); |
| 526 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 527 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 528 | while ((opt = getopt(ac, av, "dqpclRxXyb:f:P:N:C:")) != EOF) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 529 | switch (opt) { |
| 530 | case 'b': |
| 531 | bits = atoi(optarg); |
| 532 | if (bits < 512 || bits > 32768) { |
| 533 | printf("Bits has bad value.\n"); |
| 534 | exit(1); |
| 535 | } |
| 536 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 537 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 538 | case 'l': |
| 539 | print_fingerprint = 1; |
| 540 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 541 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 542 | case 'p': |
| 543 | change_passphrase = 1; |
| 544 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 545 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 546 | case 'c': |
| 547 | change_comment = 1; |
| 548 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 549 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 550 | case 'f': |
| 551 | strlcpy(identity_file, optarg, sizeof(identity_file)); |
| 552 | have_identity = 1; |
| 553 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 554 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 555 | case 'P': |
| 556 | identity_passphrase = optarg; |
| 557 | break; |
| 558 | |
| 559 | case 'N': |
| 560 | identity_new_passphrase = optarg; |
| 561 | break; |
| 562 | |
| 563 | case 'C': |
| 564 | identity_comment = optarg; |
| 565 | break; |
| 566 | |
| 567 | case 'q': |
| 568 | quiet = 1; |
| 569 | break; |
| 570 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 571 | case 'R': |
| 572 | if (rsa_alive() == 0) |
| 573 | exit(1); |
| 574 | else |
| 575 | exit(0); |
| 576 | break; |
| 577 | |
| 578 | case 'x': |
| 579 | convert_to_ssh2 = 1; |
| 580 | break; |
| 581 | |
| 582 | case 'X': |
| 583 | convert_from_ssh2 = 1; |
| 584 | break; |
| 585 | |
| 586 | case 'y': |
| 587 | print_public = 1; |
| 588 | break; |
| 589 | |
| 590 | case 'd': |
| 591 | dsa_mode = 1; |
| 592 | break; |
| 593 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 594 | case '?': |
| 595 | default: |
| 596 | usage(); |
| 597 | } |
| 598 | } |
| 599 | if (optind < ac) { |
| 600 | printf("Too many arguments.\n"); |
| 601 | usage(); |
| 602 | } |
| 603 | if (change_passphrase && change_comment) { |
| 604 | printf("Can only have one of -p and -c.\n"); |
| 605 | usage(); |
| 606 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 607 | /* check if RSA support is needed and exists */ |
| 608 | if (dsa_mode == 0 && rsa_alive() == 0) { |
| 609 | fprintf(stderr, |
| 610 | "%s: no RSA support in libssl and libcrypto. See ssl(8).\n", |
| 611 | __progname); |
| 612 | exit(1); |
| 613 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 614 | if (print_fingerprint) |
| 615 | do_fingerprint(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 616 | if (change_passphrase) |
| 617 | do_change_passphrase(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 618 | if (change_comment) |
| 619 | do_change_comment(pw); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 620 | if (convert_to_ssh2) |
| 621 | do_convert_to_ssh2(pw); |
| 622 | if (convert_from_ssh2) |
| 623 | do_convert_from_ssh2(pw); |
| 624 | if (print_public) |
| 625 | do_print_public(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 626 | |
| 627 | arc4random_stir(); |
| 628 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 629 | if (dsa_mode != 0) { |
| 630 | if (!quiet) |
| 631 | printf("Generating DSA parameter and key.\n"); |
| 632 | public = private = dsa_generate_key(bits); |
| 633 | if (private == NULL) { |
| 634 | fprintf(stderr, "dsa_generate_keys failed"); |
| 635 | exit(1); |
| 636 | } |
| 637 | } else { |
| 638 | if (quiet) |
| 639 | rsa_set_verbose(0); |
| 640 | /* Generate the rsa key pair. */ |
| 641 | public = key_new(KEY_RSA); |
| 642 | private = key_new(KEY_RSA); |
| 643 | rsa_generate_key(private->rsa, public->rsa, bits); |
| 644 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 645 | |
| 646 | if (!have_identity) |
| 647 | ask_filename(pw, "Enter file in which to save the key"); |
| 648 | |
| 649 | /* Create ~/.ssh directory if it doesn\'t already exist. */ |
| 650 | snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR); |
| 651 | if (strstr(identity_file, dotsshdir) != NULL && |
| 652 | stat(dotsshdir, &st) < 0) { |
| 653 | if (mkdir(dotsshdir, 0755) < 0) |
| 654 | error("Could not create directory '%s'.", dotsshdir); |
| 655 | else if (!quiet) |
| 656 | printf("Created directory '%s'.\n", dotsshdir); |
| 657 | } |
| 658 | /* If the file already exists, ask the user to confirm. */ |
| 659 | if (stat(identity_file, &st) >= 0) { |
| 660 | char yesno[3]; |
| 661 | printf("%s already exists.\n", identity_file); |
| 662 | printf("Overwrite (y/n)? "); |
| 663 | fflush(stdout); |
| 664 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) |
| 665 | exit(1); |
| 666 | if (yesno[0] != 'y' && yesno[0] != 'Y') |
| 667 | exit(1); |
| 668 | } |
| 669 | /* Ask for a passphrase (twice). */ |
| 670 | if (identity_passphrase) |
| 671 | passphrase1 = xstrdup(identity_passphrase); |
| 672 | else if (identity_new_passphrase) |
| 673 | passphrase1 = xstrdup(identity_new_passphrase); |
| 674 | else { |
| 675 | passphrase_again: |
| 676 | passphrase1 = |
| 677 | read_passphrase("Enter passphrase (empty for no passphrase): ", 1); |
| 678 | passphrase2 = read_passphrase("Enter same passphrase again: ", 1); |
| 679 | if (strcmp(passphrase1, passphrase2) != 0) { |
| 680 | /* The passphrases do not match. Clear them and retry. */ |
| 681 | memset(passphrase1, 0, strlen(passphrase1)); |
| 682 | memset(passphrase2, 0, strlen(passphrase2)); |
| 683 | xfree(passphrase1); |
| 684 | xfree(passphrase2); |
| 685 | printf("Passphrases do not match. Try again.\n"); |
| 686 | goto passphrase_again; |
| 687 | } |
| 688 | /* Clear the other copy of the passphrase. */ |
| 689 | memset(passphrase2, 0, strlen(passphrase2)); |
| 690 | xfree(passphrase2); |
| 691 | } |
| 692 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 693 | if (identity_comment) { |
| 694 | strlcpy(comment, identity_comment, sizeof(comment)); |
| 695 | } else { |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 696 | /* Create default commend field for the passphrase. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 697 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); |
| 698 | } |
| 699 | |
| 700 | /* Save the key with the given passphrase and comment. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 701 | if (!save_private_key(identity_file, passphrase1, private, comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 702 | printf("Saving the key failed: %s: %s.\n", |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 703 | identity_file, strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 704 | memset(passphrase1, 0, strlen(passphrase1)); |
| 705 | xfree(passphrase1); |
| 706 | exit(1); |
| 707 | } |
| 708 | /* Clear the passphrase. */ |
| 709 | memset(passphrase1, 0, strlen(passphrase1)); |
| 710 | xfree(passphrase1); |
| 711 | |
| 712 | /* Clear the private key and the random number generator. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 713 | if (private != public) { |
| 714 | key_free(private); |
| 715 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 716 | arc4random_stir(); |
| 717 | |
| 718 | if (!quiet) |
| 719 | printf("Your identification has been saved in %s.\n", identity_file); |
| 720 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 721 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
| 722 | f = fopen(identity_file, "w"); |
| 723 | if (!f) { |
| 724 | printf("Could not save your public key in %s\n", identity_file); |
| 725 | exit(1); |
| 726 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 727 | if (!key_write(public, f)) |
| 728 | fprintf(stderr, "write key failed"); |
| 729 | fprintf(f, " %s\n", comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 730 | fclose(f); |
| 731 | |
| 732 | if (!quiet) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 733 | printf("Your public key has been saved in %s.\n", |
| 734 | identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 735 | printf("The key fingerprint is:\n"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 736 | printf("%s %s\n", key_fingerprint(public), comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 737 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 738 | |
| 739 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 740 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 741 | } |