]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 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 | */ | |
8efc0c15 | 8 | |
9 | #include "includes.h" | |
10 | RCSID("$Id$"); | |
11 | ||
12 | #include "rsa.h" | |
13 | #include "ssh.h" | |
14 | #include "xmalloc.h" | |
f095fcc7 | 15 | #include "fingerprint.h" |
8efc0c15 | 16 | |
17 | /* Generated private key. */ | |
18 | RSA *private_key; | |
19 | ||
20 | /* Generated public key. */ | |
21 | RSA *public_key; | |
22 | ||
aa3378df | 23 | /* Number of bits in the RSA key. This value can be changed on the command line. */ |
8efc0c15 | 24 | int bits = 1024; |
25 | ||
aa3378df | 26 | /* |
27 | * Flag indicating that we just want to change the passphrase. This can be | |
28 | * set on the command line. | |
29 | */ | |
8efc0c15 | 30 | int change_passphrase = 0; |
31 | ||
aa3378df | 32 | /* |
33 | * Flag indicating that we just want to change the comment. This can be set | |
34 | * on the command line. | |
35 | */ | |
8efc0c15 | 36 | int change_comment = 0; |
37 | ||
38 | int quiet = 0; | |
39 | ||
f095fcc7 | 40 | /* Flag indicating that we just want to see the key fingerprint */ |
41 | int print_fingerprint = 0; | |
42 | ||
5ad13cd7 | 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; | |
8efc0c15 | 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 | ||
5ad13cd7 | 56 | /* argv0 */ |
5260325f | 57 | #ifdef HAVE___PROGNAME |
5ad13cd7 | 58 | extern char *__progname; |
5260325f | 59 | #else /* HAVE___PROGNAME */ |
60 | const char *__progname = "ssh-keygen"; | |
61 | #endif /* HAVE___PROGNAME */ | |
8efc0c15 | 62 | |
5ad13cd7 | 63 | void |
64 | ask_filename(struct passwd *pw, const char *prompt) | |
8efc0c15 | 65 | { |
5260325f | 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; | |
f095fcc7 | 78 | } |
8efc0c15 | 79 | |
f095fcc7 | 80 | void |
81 | do_fingerprint(struct passwd *pw) | |
82 | { | |
c8d54615 | 83 | FILE *f; |
84 | BIGNUM *e, *n; | |
5260325f | 85 | RSA *public_key; |
c8d54615 | 86 | char *comment = NULL, *cp, *ep, line[16*1024]; |
87 | int i, skip = 0, num = 1, invalid = 1; | |
5260325f | 88 | struct stat st; |
89 | ||
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 | } | |
c8d54615 | 96 | |
5260325f | 97 | public_key = RSA_new(); |
c8d54615 | 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) { | |
5260325f | 109 | n = BN_new(); |
110 | e = BN_new(); | |
c8d54615 | 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; | |
117 | } | |
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"); | |
5260325f | 151 | } |
5260325f | 152 | } |
c8d54615 | 153 | BN_free(e); |
154 | BN_free(n); | |
155 | fclose(f); | |
156 | } | |
157 | if (invalid) { | |
158 | printf("%s is not a valid key file.\n", identity_file); | |
159 | exit(1); | |
5260325f | 160 | } |
5260325f | 161 | exit(0); |
f095fcc7 | 162 | } |
163 | ||
5260325f | 164 | /* |
165 | * Perform changing a passphrase. The argument is the passwd structure | |
166 | * for the current user. | |
167 | */ | |
f095fcc7 | 168 | void |
169 | do_change_passphrase(struct passwd *pw) | |
170 | { | |
5260325f | 171 | char *comment; |
172 | char *old_passphrase, *passphrase1, *passphrase2; | |
173 | struct stat st; | |
174 | RSA *private_key; | |
175 | ||
176 | if (!have_identity) | |
177 | ask_filename(pw, "Enter file in which the key is"); | |
5260325f | 178 | if (stat(identity_file, &st) < 0) { |
179 | perror(identity_file); | |
180 | exit(1); | |
181 | } | |
5260325f | 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); | |
189 | ||
190 | /* Try to load the file with empty passphrase. */ | |
191 | private_key = RSA_new(); | |
192 | if (!load_private_key(identity_file, "", private_key, &comment)) { | |
5260325f | 193 | if (identity_passphrase) |
194 | old_passphrase = xstrdup(identity_passphrase); | |
195 | else | |
196 | old_passphrase = read_passphrase("Enter old passphrase: ", 1); | |
5260325f | 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 | } | |
5260325f | 203 | memset(old_passphrase, 0, strlen(old_passphrase)); |
204 | xfree(old_passphrase); | |
205 | } | |
206 | printf("Key has comment '%s'\n", comment); | |
207 | ||
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); | |
8efc0c15 | 229 | } |
8efc0c15 | 230 | |
5260325f | 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); | |
249 | } | |
8efc0c15 | 250 | |
5260325f | 251 | /* |
252 | * Change the comment of a private key file. | |
253 | */ | |
8efc0c15 | 254 | void |
255 | do_change_comment(struct passwd *pw) | |
256 | { | |
5260325f | 257 | char new_comment[1024], *comment; |
258 | RSA *private_key; | |
259 | char *passphrase; | |
260 | struct stat st; | |
261 | FILE *f; | |
262 | char *tmpbuf; | |
263 | ||
264 | if (!have_identity) | |
265 | ask_filename(pw, "Enter file in which the key is"); | |
5260325f | 266 | if (stat(identity_file, &st) < 0) { |
267 | perror(identity_file); | |
268 | exit(1); | |
269 | } | |
aa3378df | 270 | /* |
271 | * Try to load the public key from the file the verify that it is | |
272 | * readable and of the proper format. | |
273 | */ | |
5260325f | 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); | |
8efc0c15 | 278 | } |
5260325f | 279 | private_key = RSA_new(); |
aa3378df | 280 | |
5260325f | 281 | if (load_private_key(identity_file, "", private_key, &comment)) |
282 | passphrase = xstrdup(""); | |
283 | else { | |
5260325f | 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); | |
299 | ||
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 | } | |
5260325f | 310 | if (strchr(new_comment, '\n')) |
311 | *strchr(new_comment, '\n') = 0; | |
312 | } | |
313 | ||
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); | |
8efc0c15 | 323 | } |
5260325f | 324 | memset(passphrase, 0, strlen(passphrase)); |
325 | xfree(passphrase); | |
326 | RSA_free(private_key); | |
327 | ||
5260325f | 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); | |
342 | ||
343 | xfree(comment); | |
344 | ||
345 | printf("The comment in your key file has been changed.\n"); | |
346 | exit(0); | |
8efc0c15 | 347 | } |
348 | ||
5ad13cd7 | 349 | void |
350 | usage(void) | |
351 | { | |
5260325f | 352 | printf("ssh-keygen version %s\n", SSH_VERSION); |
c8d54615 | 353 | printf("Usage: %s [-b bits] [-p] [-c] [-l] [-f file] [-P pass] [-N new-pass] [-C comment]\n", __progname); |
5260325f | 354 | exit(1); |
5ad13cd7 | 355 | } |
356 | ||
5260325f | 357 | /* |
358 | * Main program for key management. | |
359 | */ | |
8efc0c15 | 360 | int |
361 | main(int ac, char **av) | |
362 | { | |
5260325f | 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; | |
372 | ||
373 | /* check if RSA support exists */ | |
374 | if (rsa_alive() == 0) { | |
5260325f | 375 | fprintf(stderr, |
376 | "%s: no RSA support in libssl and libcrypto. See ssl(8).\n", | |
377 | __progname); | |
378 | exit(1); | |
8efc0c15 | 379 | } |
aa3378df | 380 | /* we need this for the home * directory. */ |
5260325f | 381 | pw = getpwuid(getuid()); |
382 | if (!pw) { | |
383 | printf("You don't exist, go away!\n"); | |
384 | exit(1); | |
385 | } | |
aa3378df | 386 | |
5260325f | 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; | |
396 | ||
397 | case 'l': | |
398 | print_fingerprint = 1; | |
399 | break; | |
400 | ||
401 | case 'p': | |
402 | change_passphrase = 1; | |
403 | break; | |
404 | ||
405 | case 'c': | |
406 | change_comment = 1; | |
407 | break; | |
408 | ||
409 | case 'f': | |
410 | strlcpy(identity_file, optarg, sizeof(identity_file)); | |
411 | have_identity = 1; | |
412 | break; | |
413 | ||
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); | |
5260325f | 445 | if (change_passphrase) |
446 | do_change_passphrase(pw); | |
5260325f | 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 | ||
5260325f | 507 | if (identity_comment) { |
508 | strlcpy(comment, identity_comment, sizeof(comment)); | |
509 | } else { | |
aa3378df | 510 | /* Create default commend field for the passphrase. */ |
5260325f | 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 | ||
5260325f | 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); | |
8efc0c15 | 558 | } |
5260325f | 559 | exit(0); |
8efc0c15 | 560 | } |