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 | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 10 | RCSID("$Id: ssh-keygen.c,v 1.11 2000/03/09 10:27:51 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 11 | |
| 12 | #include "rsa.h" |
| 13 | #include "ssh.h" |
| 14 | #include "xmalloc.h" |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 15 | #include "fingerprint.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
| 17 | /* Generated private key. */ |
| 18 | RSA *private_key; |
| 19 | |
| 20 | /* Generated public key. */ |
| 21 | RSA *public_key; |
| 22 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 23 | /* Number of bits in the RSA key. This value can be changed on the command line. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 24 | int bits = 1024; |
| 25 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 26 | /* |
| 27 | * Flag indicating that we just want to change the passphrase. This can be |
| 28 | * set on the command line. |
| 29 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 30 | int change_passphrase = 0; |
| 31 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 32 | /* |
| 33 | * Flag indicating that we just want to change the comment. This can be set |
| 34 | * on the command line. |
| 35 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | int change_comment = 0; |
| 37 | |
| 38 | int quiet = 0; |
| 39 | |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 40 | /* Flag indicating that we just want to see the key fingerprint */ |
| 41 | int print_fingerprint = 0; |
| 42 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 43 | /* The identity file name, given on the command line or entered by the user. */ |
| 44 | char identity_file[1024]; |
| 45 | int have_identity = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 46 | |
| 47 | /* This is set to the passphrase if given on the command line. */ |
| 48 | char *identity_passphrase = NULL; |
| 49 | |
| 50 | /* This is set to the new passphrase if given on the command line. */ |
| 51 | char *identity_new_passphrase = NULL; |
| 52 | |
| 53 | /* This is set to the new comment if given on the command line. */ |
| 54 | char *identity_comment = NULL; |
| 55 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 56 | /* argv0 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 57 | #ifdef HAVE___PROGNAME |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 58 | extern char *__progname; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 59 | #else /* HAVE___PROGNAME */ |
| 60 | const char *__progname = "ssh-keygen"; |
| 61 | #endif /* HAVE___PROGNAME */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 63 | void |
| 64 | ask_filename(struct passwd *pw, const char *prompt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 65 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 66 | char buf[1024]; |
| 67 | snprintf(identity_file, sizeof(identity_file), "%s/%s", |
| 68 | pw->pw_dir, SSH_CLIENT_IDENTITY); |
| 69 | printf("%s (%s): ", prompt, identity_file); |
| 70 | fflush(stdout); |
| 71 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 72 | exit(1); |
| 73 | if (strchr(buf, '\n')) |
| 74 | *strchr(buf, '\n') = 0; |
| 75 | if (strcmp(buf, "") != 0) |
| 76 | strlcpy(identity_file, buf, sizeof(identity_file)); |
| 77 | have_identity = 1; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 78 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 80 | void |
| 81 | do_fingerprint(struct passwd *pw) |
| 82 | { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 83 | FILE *f; |
| 84 | BIGNUM *e, *n; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 85 | RSA *public_key; |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 86 | char *comment = NULL, *cp, *ep, line[16*1024]; |
| 87 | int i, skip = 0, num = 1, invalid = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 88 | struct stat st; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 89 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 90 | if (!have_identity) |
| 91 | ask_filename(pw, "Enter file in which the key is"); |
| 92 | if (stat(identity_file, &st) < 0) { |
| 93 | perror(identity_file); |
| 94 | exit(1); |
| 95 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 96 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 97 | public_key = RSA_new(); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 98 | if (load_public_key(identity_file, public_key, &comment)) { |
| 99 | printf("%d %s %s\n", BN_num_bits(public_key->n), |
| 100 | fingerprint(public_key->e, public_key->n), |
| 101 | comment); |
| 102 | RSA_free(public_key); |
| 103 | exit(0); |
| 104 | } |
| 105 | RSA_free(public_key); |
| 106 | |
| 107 | f = fopen(identity_file, "r"); |
| 108 | if (f != NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 109 | n = BN_new(); |
| 110 | e = BN_new(); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 111 | while (fgets(line, sizeof(line), f)) { |
| 112 | i = strlen(line) - 1; |
| 113 | if (line[i] != '\n') { |
| 114 | error("line %d too long: %.40s...", num, line); |
| 115 | skip = 1; |
| 116 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 118 | num++; |
| 119 | if (skip) { |
| 120 | skip = 0; |
| 121 | continue; |
| 122 | } |
| 123 | line[i] = '\0'; |
| 124 | |
| 125 | /* Skip leading whitespace, empty and comment lines. */ |
| 126 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 127 | ; |
| 128 | if (!*cp || *cp == '\n' || *cp == '#') |
| 129 | continue ; |
| 130 | i = strtol(cp, &ep, 10); |
| 131 | if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) { |
| 132 | int quoted = 0; |
| 133 | comment = cp; |
| 134 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 135 | if (*cp == '\\' && cp[1] == '"') |
| 136 | cp++; /* Skip both */ |
| 137 | else if (*cp == '"') |
| 138 | quoted = !quoted; |
| 139 | } |
| 140 | if (!*cp) |
| 141 | continue; |
| 142 | *cp++ = '\0'; |
| 143 | } |
| 144 | ep = cp; |
| 145 | if (auth_rsa_read_key(&cp, &i, e, n)) { |
| 146 | invalid = 0; |
| 147 | comment = *cp ? cp : comment; |
| 148 | printf("%d %s %s\n", BN_num_bits(n), |
| 149 | fingerprint(e, n), |
| 150 | comment ? comment : "no comment"); |
| 151 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 153 | BN_free(e); |
| 154 | BN_free(n); |
| 155 | fclose(f); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 156 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 157 | if (invalid) { |
| 158 | printf("%s is not a valid key file.\n", identity_file); |
| 159 | exit(1); |
| 160 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | exit(0); |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 162 | } |
| 163 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 164 | /* |
| 165 | * Perform changing a passphrase. The argument is the passwd structure |
| 166 | * for the current user. |
| 167 | */ |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 168 | void |
| 169 | do_change_passphrase(struct passwd *pw) |
| 170 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 171 | char *comment; |
| 172 | char *old_passphrase, *passphrase1, *passphrase2; |
| 173 | struct stat st; |
| 174 | RSA *private_key; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 175 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 176 | if (!have_identity) |
| 177 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 178 | if (stat(identity_file, &st) < 0) { |
| 179 | perror(identity_file); |
| 180 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 181 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | public_key = RSA_new(); |
| 183 | if (!load_public_key(identity_file, public_key, NULL)) { |
| 184 | printf("%s is not a valid key file.\n", identity_file); |
| 185 | exit(1); |
| 186 | } |
| 187 | /* Clear the public key since we are just about to load the whole file. */ |
| 188 | RSA_free(public_key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 189 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 190 | /* Try to load the file with empty passphrase. */ |
| 191 | private_key = RSA_new(); |
| 192 | if (!load_private_key(identity_file, "", private_key, &comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 193 | if (identity_passphrase) |
| 194 | old_passphrase = xstrdup(identity_passphrase); |
| 195 | else |
| 196 | old_passphrase = read_passphrase("Enter old passphrase: ", 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 197 | if (!load_private_key(identity_file, old_passphrase, private_key, &comment)) { |
| 198 | memset(old_passphrase, 0, strlen(old_passphrase)); |
| 199 | xfree(old_passphrase); |
| 200 | printf("Bad passphrase.\n"); |
| 201 | exit(1); |
| 202 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 203 | memset(old_passphrase, 0, strlen(old_passphrase)); |
| 204 | xfree(old_passphrase); |
| 205 | } |
| 206 | printf("Key has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 207 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 208 | /* Ask the new passphrase (twice). */ |
| 209 | if (identity_new_passphrase) { |
| 210 | passphrase1 = xstrdup(identity_new_passphrase); |
| 211 | passphrase2 = NULL; |
| 212 | } else { |
| 213 | passphrase1 = |
| 214 | read_passphrase("Enter new passphrase (empty for no passphrase): ", 1); |
| 215 | passphrase2 = read_passphrase("Enter same passphrase again: ", 1); |
| 216 | |
| 217 | /* Verify that they are the same. */ |
| 218 | if (strcmp(passphrase1, passphrase2) != 0) { |
| 219 | memset(passphrase1, 0, strlen(passphrase1)); |
| 220 | memset(passphrase2, 0, strlen(passphrase2)); |
| 221 | xfree(passphrase1); |
| 222 | xfree(passphrase2); |
| 223 | printf("Pass phrases do not match. Try again.\n"); |
| 224 | exit(1); |
| 225 | } |
| 226 | /* Destroy the other copy. */ |
| 227 | memset(passphrase2, 0, strlen(passphrase2)); |
| 228 | xfree(passphrase2); |
| 229 | } |
| 230 | |
| 231 | /* Save the file using the new passphrase. */ |
| 232 | if (!save_private_key(identity_file, passphrase1, private_key, comment)) { |
| 233 | printf("Saving the key failed: %s: %s.\n", |
| 234 | identity_file, strerror(errno)); |
| 235 | memset(passphrase1, 0, strlen(passphrase1)); |
| 236 | xfree(passphrase1); |
| 237 | RSA_free(private_key); |
| 238 | xfree(comment); |
| 239 | exit(1); |
| 240 | } |
| 241 | /* Destroy the passphrase and the copy of the key in memory. */ |
| 242 | memset(passphrase1, 0, strlen(passphrase1)); |
| 243 | xfree(passphrase1); |
| 244 | RSA_free(private_key); /* Destroys contents */ |
| 245 | xfree(comment); |
| 246 | |
| 247 | printf("Your identification has been saved with the new passphrase.\n"); |
| 248 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 249 | } |
| 250 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 251 | /* |
| 252 | * Change the comment of a private key file. |
| 253 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 254 | void |
| 255 | do_change_comment(struct passwd *pw) |
| 256 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 257 | char new_comment[1024], *comment; |
| 258 | RSA *private_key; |
| 259 | char *passphrase; |
| 260 | struct stat st; |
| 261 | FILE *f; |
| 262 | char *tmpbuf; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 263 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 264 | if (!have_identity) |
| 265 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 266 | if (stat(identity_file, &st) < 0) { |
| 267 | perror(identity_file); |
| 268 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 269 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 270 | /* |
| 271 | * Try to load the public key from the file the verify that it is |
| 272 | * readable and of the proper format. |
| 273 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 274 | public_key = RSA_new(); |
| 275 | if (!load_public_key(identity_file, public_key, NULL)) { |
| 276 | printf("%s is not a valid key file.\n", identity_file); |
| 277 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 278 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 279 | private_key = RSA_new(); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 280 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 281 | if (load_private_key(identity_file, "", private_key, &comment)) |
| 282 | passphrase = xstrdup(""); |
| 283 | else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 284 | if (identity_passphrase) |
| 285 | passphrase = xstrdup(identity_passphrase); |
| 286 | else if (identity_new_passphrase) |
| 287 | passphrase = xstrdup(identity_new_passphrase); |
| 288 | else |
| 289 | passphrase = read_passphrase("Enter passphrase: ", 1); |
| 290 | /* Try to load using the passphrase. */ |
| 291 | if (!load_private_key(identity_file, passphrase, private_key, &comment)) { |
| 292 | memset(passphrase, 0, strlen(passphrase)); |
| 293 | xfree(passphrase); |
| 294 | printf("Bad passphrase.\n"); |
| 295 | exit(1); |
| 296 | } |
| 297 | } |
| 298 | printf("Key now has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 299 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 300 | if (identity_comment) { |
| 301 | strlcpy(new_comment, identity_comment, sizeof(new_comment)); |
| 302 | } else { |
| 303 | printf("Enter new comment: "); |
| 304 | fflush(stdout); |
| 305 | if (!fgets(new_comment, sizeof(new_comment), stdin)) { |
| 306 | memset(passphrase, 0, strlen(passphrase)); |
| 307 | RSA_free(private_key); |
| 308 | exit(1); |
| 309 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 310 | if (strchr(new_comment, '\n')) |
| 311 | *strchr(new_comment, '\n') = 0; |
| 312 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 313 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 314 | /* Save the file using the new passphrase. */ |
| 315 | if (!save_private_key(identity_file, passphrase, private_key, new_comment)) { |
| 316 | printf("Saving the key failed: %s: %s.\n", |
| 317 | identity_file, strerror(errno)); |
| 318 | memset(passphrase, 0, strlen(passphrase)); |
| 319 | xfree(passphrase); |
| 320 | RSA_free(private_key); |
| 321 | xfree(comment); |
| 322 | exit(1); |
| 323 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 324 | memset(passphrase, 0, strlen(passphrase)); |
| 325 | xfree(passphrase); |
| 326 | RSA_free(private_key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 327 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 328 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
| 329 | f = fopen(identity_file, "w"); |
| 330 | if (!f) { |
| 331 | printf("Could not save your public key in %s\n", identity_file); |
| 332 | exit(1); |
| 333 | } |
| 334 | fprintf(f, "%d ", BN_num_bits(public_key->n)); |
| 335 | tmpbuf = BN_bn2dec(public_key->e); |
| 336 | fprintf(f, "%s ", tmpbuf); |
| 337 | free(tmpbuf); |
| 338 | tmpbuf = BN_bn2dec(public_key->n); |
| 339 | fprintf(f, "%s %s\n", tmpbuf, new_comment); |
| 340 | free(tmpbuf); |
| 341 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 342 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 343 | xfree(comment); |
| 344 | |
| 345 | printf("The comment in your key file has been changed.\n"); |
| 346 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 347 | } |
| 348 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 349 | void |
| 350 | usage(void) |
| 351 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 352 | printf("ssh-keygen version %s\n", SSH_VERSION); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 353 | printf("Usage: %s [-b bits] [-p] [-c] [-l] [-f file] [-P pass] [-N new-pass] [-C comment]\n", __progname); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 354 | exit(1); |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 355 | } |
| 356 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 357 | /* |
| 358 | * Main program for key management. |
| 359 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 360 | int |
| 361 | main(int ac, char **av) |
| 362 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 363 | char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2; |
| 364 | struct passwd *pw; |
| 365 | char *tmpbuf; |
| 366 | int opt; |
| 367 | struct stat st; |
| 368 | FILE *f; |
| 369 | char hostname[MAXHOSTNAMELEN]; |
| 370 | extern int optind; |
| 371 | extern char *optarg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 372 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 373 | /* check if RSA support exists */ |
| 374 | if (rsa_alive() == 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 375 | fprintf(stderr, |
| 376 | "%s: no RSA support in libssl and libcrypto. See ssl(8).\n", |
| 377 | __progname); |
| 378 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 379 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 380 | /* we need this for the home * directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 381 | pw = getpwuid(getuid()); |
| 382 | if (!pw) { |
| 383 | printf("You don't exist, go away!\n"); |
| 384 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 385 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 386 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 387 | while ((opt = getopt(ac, av, "qpclb:f:P:N:C:")) != EOF) { |
| 388 | switch (opt) { |
| 389 | case 'b': |
| 390 | bits = atoi(optarg); |
| 391 | if (bits < 512 || bits > 32768) { |
| 392 | printf("Bits has bad value.\n"); |
| 393 | exit(1); |
| 394 | } |
| 395 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 396 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 397 | case 'l': |
| 398 | print_fingerprint = 1; |
| 399 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 400 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 401 | case 'p': |
| 402 | change_passphrase = 1; |
| 403 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 404 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 405 | case 'c': |
| 406 | change_comment = 1; |
| 407 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 408 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 409 | case 'f': |
| 410 | strlcpy(identity_file, optarg, sizeof(identity_file)); |
| 411 | have_identity = 1; |
| 412 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 413 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 414 | case 'P': |
| 415 | identity_passphrase = optarg; |
| 416 | break; |
| 417 | |
| 418 | case 'N': |
| 419 | identity_new_passphrase = optarg; |
| 420 | break; |
| 421 | |
| 422 | case 'C': |
| 423 | identity_comment = optarg; |
| 424 | break; |
| 425 | |
| 426 | case 'q': |
| 427 | quiet = 1; |
| 428 | break; |
| 429 | |
| 430 | case '?': |
| 431 | default: |
| 432 | usage(); |
| 433 | } |
| 434 | } |
| 435 | if (optind < ac) { |
| 436 | printf("Too many arguments.\n"); |
| 437 | usage(); |
| 438 | } |
| 439 | if (change_passphrase && change_comment) { |
| 440 | printf("Can only have one of -p and -c.\n"); |
| 441 | usage(); |
| 442 | } |
| 443 | if (print_fingerprint) |
| 444 | do_fingerprint(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 445 | if (change_passphrase) |
| 446 | do_change_passphrase(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 447 | if (change_comment) |
| 448 | do_change_comment(pw); |
| 449 | |
| 450 | arc4random_stir(); |
| 451 | |
| 452 | if (quiet) |
| 453 | rsa_set_verbose(0); |
| 454 | |
| 455 | /* Generate the rsa key pair. */ |
| 456 | private_key = RSA_new(); |
| 457 | public_key = RSA_new(); |
| 458 | rsa_generate_key(private_key, public_key, bits); |
| 459 | |
| 460 | if (!have_identity) |
| 461 | ask_filename(pw, "Enter file in which to save the key"); |
| 462 | |
| 463 | /* Create ~/.ssh directory if it doesn\'t already exist. */ |
| 464 | snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR); |
| 465 | if (strstr(identity_file, dotsshdir) != NULL && |
| 466 | stat(dotsshdir, &st) < 0) { |
| 467 | if (mkdir(dotsshdir, 0755) < 0) |
| 468 | error("Could not create directory '%s'.", dotsshdir); |
| 469 | else if (!quiet) |
| 470 | printf("Created directory '%s'.\n", dotsshdir); |
| 471 | } |
| 472 | /* If the file already exists, ask the user to confirm. */ |
| 473 | if (stat(identity_file, &st) >= 0) { |
| 474 | char yesno[3]; |
| 475 | printf("%s already exists.\n", identity_file); |
| 476 | printf("Overwrite (y/n)? "); |
| 477 | fflush(stdout); |
| 478 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) |
| 479 | exit(1); |
| 480 | if (yesno[0] != 'y' && yesno[0] != 'Y') |
| 481 | exit(1); |
| 482 | } |
| 483 | /* Ask for a passphrase (twice). */ |
| 484 | if (identity_passphrase) |
| 485 | passphrase1 = xstrdup(identity_passphrase); |
| 486 | else if (identity_new_passphrase) |
| 487 | passphrase1 = xstrdup(identity_new_passphrase); |
| 488 | else { |
| 489 | passphrase_again: |
| 490 | passphrase1 = |
| 491 | read_passphrase("Enter passphrase (empty for no passphrase): ", 1); |
| 492 | passphrase2 = read_passphrase("Enter same passphrase again: ", 1); |
| 493 | if (strcmp(passphrase1, passphrase2) != 0) { |
| 494 | /* The passphrases do not match. Clear them and retry. */ |
| 495 | memset(passphrase1, 0, strlen(passphrase1)); |
| 496 | memset(passphrase2, 0, strlen(passphrase2)); |
| 497 | xfree(passphrase1); |
| 498 | xfree(passphrase2); |
| 499 | printf("Passphrases do not match. Try again.\n"); |
| 500 | goto passphrase_again; |
| 501 | } |
| 502 | /* Clear the other copy of the passphrase. */ |
| 503 | memset(passphrase2, 0, strlen(passphrase2)); |
| 504 | xfree(passphrase2); |
| 505 | } |
| 506 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 507 | if (identity_comment) { |
| 508 | strlcpy(comment, identity_comment, sizeof(comment)); |
| 509 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 510 | /* Create default commend field for the passphrase. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 511 | if (gethostname(hostname, sizeof(hostname)) < 0) { |
| 512 | perror("gethostname"); |
| 513 | exit(1); |
| 514 | } |
| 515 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); |
| 516 | } |
| 517 | |
| 518 | /* Save the key with the given passphrase and comment. */ |
| 519 | if (!save_private_key(identity_file, passphrase1, private_key, comment)) { |
| 520 | printf("Saving the key failed: %s: %s.\n", |
| 521 | identity_file, strerror(errno)); |
| 522 | memset(passphrase1, 0, strlen(passphrase1)); |
| 523 | xfree(passphrase1); |
| 524 | exit(1); |
| 525 | } |
| 526 | /* Clear the passphrase. */ |
| 527 | memset(passphrase1, 0, strlen(passphrase1)); |
| 528 | xfree(passphrase1); |
| 529 | |
| 530 | /* Clear the private key and the random number generator. */ |
| 531 | RSA_free(private_key); |
| 532 | arc4random_stir(); |
| 533 | |
| 534 | if (!quiet) |
| 535 | printf("Your identification has been saved in %s.\n", identity_file); |
| 536 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 537 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
| 538 | f = fopen(identity_file, "w"); |
| 539 | if (!f) { |
| 540 | printf("Could not save your public key in %s\n", identity_file); |
| 541 | exit(1); |
| 542 | } |
| 543 | fprintf(f, "%d ", BN_num_bits(public_key->n)); |
| 544 | tmpbuf = BN_bn2dec(public_key->e); |
| 545 | fprintf(f, "%s ", tmpbuf); |
| 546 | free(tmpbuf); |
| 547 | tmpbuf = BN_bn2dec(public_key->n); |
| 548 | fprintf(f, "%s %s\n", tmpbuf, comment); |
| 549 | free(tmpbuf); |
| 550 | fclose(f); |
| 551 | |
| 552 | if (!quiet) { |
| 553 | printf("Your public key has been saved in %s.\n", identity_file); |
| 554 | printf("The key fingerprint is:\n"); |
| 555 | printf("%d %s %s\n", BN_num_bits(public_key->n), |
| 556 | fingerprint(public_key->e, public_key->n), |
| 557 | comment); |
| 558 | } |
| 559 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 560 | } |