2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Created: Mon Mar 27 02:26:40 1995 ylo
6 * Identity and host key generation and maintenance.
15 #include "fingerprint.h"
17 /* Generated private key. */
20 /* Generated public key. */
23 /* Number of bits in the RSA key. This value can be changed on the command line. */
27 * Flag indicating that we just want to change the passphrase. This can be
28 * set on the command line.
30 int change_passphrase = 0;
33 * Flag indicating that we just want to change the comment. This can be set
34 * on the command line.
36 int change_comment = 0;
40 /* Flag indicating that we just want to see the key fingerprint */
41 int print_fingerprint = 0;
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;
47 /* This is set to the passphrase if given on the command line. */
48 char *identity_passphrase = NULL;
50 /* This is set to the new passphrase if given on the command line. */
51 char *identity_new_passphrase = NULL;
53 /* This is set to the new comment if given on the command line. */
54 char *identity_comment = NULL;
57 #ifdef HAVE___PROGNAME
58 extern char *__progname;
59 #else /* HAVE___PROGNAME */
60 const char *__progname = "ssh-keygen";
61 #endif /* HAVE___PROGNAME */
64 ask_filename(struct passwd *pw, const char *prompt)
67 snprintf(identity_file, sizeof(identity_file), "%s/%s",
68 pw->pw_dir, SSH_CLIENT_IDENTITY);
69 printf("%s (%s): ", prompt, identity_file);
71 if (fgets(buf, sizeof(buf), stdin) == NULL)
73 if (strchr(buf, '\n'))
74 *strchr(buf, '\n') = 0;
75 if (strcmp(buf, "") != 0)
76 strlcpy(identity_file, buf, sizeof(identity_file));
81 do_fingerprint(struct passwd *pw)
86 char *comment = NULL, *cp, *ep, line[16*1024];
87 int i, skip = 0, num = 1, invalid = 1;
91 ask_filename(pw, "Enter file in which the key is");
92 if (stat(identity_file, &st) < 0) {
93 perror(identity_file);
97 public_key = RSA_new();
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),
102 RSA_free(public_key);
105 RSA_free(public_key);
107 f = fopen(identity_file, "r");
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);
125 /* Skip leading whitespace, empty and comment lines. */
126 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
128 if (!*cp || *cp == '\n' || *cp == '#')
130 i = strtol(cp, &ep, 10);
131 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
134 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
135 if (*cp == '\\' && cp[1] == '"')
136 cp++; /* Skip both */
145 if (auth_rsa_read_key(&cp, &i, e, n)) {
147 comment = *cp ? cp : comment;
148 printf("%d %s %s\n", BN_num_bits(n),
150 comment ? comment : "no comment");
158 printf("%s is not a valid key file.\n", identity_file);
165 * Perform changing a passphrase. The argument is the passwd structure
166 * for the current user.
169 do_change_passphrase(struct passwd *pw)
172 char *old_passphrase, *passphrase1, *passphrase2;
177 ask_filename(pw, "Enter file in which the key is");
178 if (stat(identity_file, &st) < 0) {
179 perror(identity_file);
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);
187 /* Clear the public key since we are just about to load the whole file. */
188 RSA_free(public_key);
190 /* Try to load the file with empty passphrase. */
191 private_key = RSA_new();
192 if (!load_private_key(identity_file, "", private_key, &comment)) {
193 if (identity_passphrase)
194 old_passphrase = xstrdup(identity_passphrase);
196 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
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");
203 memset(old_passphrase, 0, strlen(old_passphrase));
204 xfree(old_passphrase);
206 printf("Key has comment '%s'\n", comment);
208 /* Ask the new passphrase (twice). */
209 if (identity_new_passphrase) {
210 passphrase1 = xstrdup(identity_new_passphrase);
214 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
215 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
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));
223 printf("Pass phrases do not match. Try again.\n");
226 /* Destroy the other copy. */
227 memset(passphrase2, 0, strlen(passphrase2));
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));
237 RSA_free(private_key);
241 /* Destroy the passphrase and the copy of the key in memory. */
242 memset(passphrase1, 0, strlen(passphrase1));
244 RSA_free(private_key); /* Destroys contents */
247 printf("Your identification has been saved with the new passphrase.\n");
252 * Change the comment of a private key file.
255 do_change_comment(struct passwd *pw)
257 char new_comment[1024], *comment;
265 ask_filename(pw, "Enter file in which the key is");
266 if (stat(identity_file, &st) < 0) {
267 perror(identity_file);
271 * Try to load the public key from the file the verify that it is
272 * readable and of the proper format.
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);
279 private_key = RSA_new();
281 if (load_private_key(identity_file, "", private_key, &comment))
282 passphrase = xstrdup("");
284 if (identity_passphrase)
285 passphrase = xstrdup(identity_passphrase);
286 else if (identity_new_passphrase)
287 passphrase = xstrdup(identity_new_passphrase);
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));
294 printf("Bad passphrase.\n");
298 printf("Key now has comment '%s'\n", comment);
300 if (identity_comment) {
301 strlcpy(new_comment, identity_comment, sizeof(new_comment));
303 printf("Enter new comment: ");
305 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
306 memset(passphrase, 0, strlen(passphrase));
307 RSA_free(private_key);
310 if (strchr(new_comment, '\n'))
311 *strchr(new_comment, '\n') = 0;
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));
320 RSA_free(private_key);
324 memset(passphrase, 0, strlen(passphrase));
326 RSA_free(private_key);
328 strlcat(identity_file, ".pub", sizeof(identity_file));
329 f = fopen(identity_file, "w");
331 printf("Could not save your public key in %s\n", identity_file);
334 fprintf(f, "%d ", BN_num_bits(public_key->n));
335 tmpbuf = BN_bn2dec(public_key->e);
336 fprintf(f, "%s ", tmpbuf);
338 tmpbuf = BN_bn2dec(public_key->n);
339 fprintf(f, "%s %s\n", tmpbuf, new_comment);
345 printf("The comment in your key file has been changed.\n");
352 printf("ssh-keygen version %s\n", SSH_VERSION);
353 printf("Usage: %s [-b bits] [-p] [-c] [-l] [-f file] [-P pass] [-N new-pass] [-C comment]\n", __progname);
358 * Main program for key management.
361 main(int ac, char **av)
363 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
369 char hostname[MAXHOSTNAMELEN];
373 /* check if RSA support exists */
374 if (rsa_alive() == 0) {
376 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
380 /* we need this for the home * directory. */
381 pw = getpwuid(getuid());
383 printf("You don't exist, go away!\n");
387 while ((opt = getopt(ac, av, "qpclb:f:P:N:C:")) != EOF) {
391 if (bits < 512 || bits > 32768) {
392 printf("Bits has bad value.\n");
398 print_fingerprint = 1;
402 change_passphrase = 1;
410 strlcpy(identity_file, optarg, sizeof(identity_file));
415 identity_passphrase = optarg;
419 identity_new_passphrase = optarg;
423 identity_comment = optarg;
436 printf("Too many arguments.\n");
439 if (change_passphrase && change_comment) {
440 printf("Can only have one of -p and -c.\n");
443 if (print_fingerprint)
445 if (change_passphrase)
446 do_change_passphrase(pw);
448 do_change_comment(pw);
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);
461 ask_filename(pw, "Enter file in which to save the key");
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);
470 printf("Created directory '%s'.\n", dotsshdir);
472 /* If the file already exists, ask the user to confirm. */
473 if (stat(identity_file, &st) >= 0) {
475 printf("%s already exists.\n", identity_file);
476 printf("Overwrite (y/n)? ");
478 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
480 if (yesno[0] != 'y' && yesno[0] != 'Y')
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);
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));
499 printf("Passphrases do not match. Try again.\n");
500 goto passphrase_again;
502 /* Clear the other copy of the passphrase. */
503 memset(passphrase2, 0, strlen(passphrase2));
507 if (identity_comment) {
508 strlcpy(comment, identity_comment, sizeof(comment));
510 /* Create default commend field for the passphrase. */
511 if (gethostname(hostname, sizeof(hostname)) < 0) {
512 perror("gethostname");
515 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
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));
526 /* Clear the passphrase. */
527 memset(passphrase1, 0, strlen(passphrase1));
530 /* Clear the private key and the random number generator. */
531 RSA_free(private_key);
535 printf("Your identification has been saved in %s.\n", identity_file);
537 strlcat(identity_file, ".pub", sizeof(identity_file));
538 f = fopen(identity_file, "w");
540 printf("Could not save your public key in %s\n", identity_file);
543 fprintf(f, "%d ", BN_num_bits(public_key->n));
544 tmpbuf = BN_bn2dec(public_key->e);
545 fprintf(f, "%s ", tmpbuf);
547 tmpbuf = BN_bn2dec(public_key->n);
548 fprintf(f, "%s %s\n", tmpbuf, comment);
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),