5 Author: Tatu Ylonen <ylo@cs.hut.fi>
7 Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 Created: Mon Mar 27 02:26:40 1995 ylo
12 Identity and host key generation and maintenance.
23 #ifdef HAVE___PROGNAME
24 extern char *__progname;
25 #else /* HAVE___PROGNAME */
26 const char *__progname = "ssh-keygen";
27 #endif /* HAVE___PROGNAME */
29 /* Generated private key. */
32 /* Generated public key. */
35 /* Number of bits in the RSA key. This value can be changed on the command
39 /* Flag indicating that we just want to change the passphrase. This can be
40 set on the command line. */
41 int change_passphrase = 0;
43 /* Flag indicating that we just want to change the comment. This can be set
44 on the command line. */
45 int change_comment = 0;
49 /* This is set to the identity file name if given on the command line. */
50 char *identity_file = NULL;
52 /* This is set to the passphrase if given on the command line. */
53 char *identity_passphrase = NULL;
55 /* This is set to the new passphrase if given on the command line. */
56 char *identity_new_passphrase = NULL;
58 /* This is set to the new comment if given on the command line. */
59 char *identity_comment = NULL;
61 /* Perform changing a passphrase. The argument is the passwd structure
62 for the current user. */
65 do_change_passphrase(struct passwd *pw)
67 char buf[1024], *comment;
68 char *old_passphrase, *passphrase1, *passphrase2;
72 /* Read key file name. */
73 if (identity_file != NULL) {
74 strncpy(buf, identity_file, sizeof(buf));
75 buf[sizeof(buf) - 1] = '\0';
77 printf("Enter file in which the key is ($HOME/%s): ", SSH_CLIENT_IDENTITY);
79 if (fgets(buf, sizeof(buf), stdin) == NULL)
81 if (strchr(buf, '\n'))
82 *strchr(buf, '\n') = 0;
83 if (strcmp(buf, "") == 0)
84 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
87 /* Check if the file exists. */
88 if (stat(buf, &st) < 0)
94 /* Try to load the public key from the file the verify that it is
95 readable and of the proper format. */
96 public_key = RSA_new();
97 if (!load_public_key(buf, public_key, NULL))
99 printf("%s is not a valid key file.\n", buf);
102 /* Clear the public key since we are just about to load the whole file. */
103 RSA_free(public_key);
105 /* Try to load the file with empty passphrase. */
106 private_key = RSA_new();
107 if (!load_private_key(buf, "", private_key, &comment)) {
108 /* Read passphrase from the user. */
109 if (identity_passphrase)
110 old_passphrase = xstrdup(identity_passphrase);
112 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
113 /* Try to load using the passphrase. */
114 if (!load_private_key(buf, old_passphrase, private_key, &comment))
116 memset(old_passphrase, 0, strlen(old_passphrase));
117 xfree(old_passphrase);
118 printf("Bad passphrase.\n");
121 /* Destroy the passphrase. */
122 memset(old_passphrase, 0, strlen(old_passphrase));
123 xfree(old_passphrase);
125 printf("Key has comment '%s'\n", comment);
127 /* Ask the new passphrase (twice). */
128 if (identity_new_passphrase)
130 passphrase1 = xstrdup(identity_new_passphrase);
136 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
137 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
139 /* Verify that they are the same. */
140 if (strcmp(passphrase1, passphrase2) != 0)
142 memset(passphrase1, 0, strlen(passphrase1));
143 memset(passphrase2, 0, strlen(passphrase2));
146 printf("Pass phrases do not match. Try again.\n");
149 /* Destroy the other copy. */
150 memset(passphrase2, 0, strlen(passphrase2));
154 /* Save the file using the new passphrase. */
155 if (!save_private_key(buf, passphrase1, private_key, comment))
157 printf("Saving the key failed: %s: %s.\n",
158 buf, strerror(errno));
159 memset(passphrase1, 0, strlen(passphrase1));
161 RSA_free(private_key);
165 /* Destroy the passphrase and the copy of the key in memory. */
166 memset(passphrase1, 0, strlen(passphrase1));
168 RSA_free(private_key); /* Destroys contents */
171 printf("Your identification has been saved with the new passphrase.\n");
175 /* Change the comment of a private key file. */
178 do_change_comment(struct passwd *pw)
180 char buf[1024], new_comment[1024], *comment;
187 /* Read key file name. */
190 strncpy(buf, identity_file, sizeof(buf));
191 buf[sizeof(buf) - 1] = '\0';
195 printf("Enter file in which the key is ($HOME/%s): ",
196 SSH_CLIENT_IDENTITY);
198 if (fgets(buf, sizeof(buf), stdin) == NULL)
200 if (strchr(buf, '\n'))
201 *strchr(buf, '\n') = 0;
202 if (strcmp(buf, "") == 0)
203 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
206 /* Check if the file exists. */
207 if (stat(buf, &st) < 0)
213 /* Try to load the public key from the file the verify that it is
214 readable and of the proper format. */
215 public_key = RSA_new();
216 if (!load_public_key(buf, public_key, NULL))
218 printf("%s is not a valid key file.\n", buf);
222 private_key = RSA_new();
223 /* Try to load the file with empty passphrase. */
224 if (load_private_key(buf, "", private_key, &comment))
225 passphrase = xstrdup("");
228 /* Read passphrase from the user. */
229 if (identity_passphrase)
230 passphrase = xstrdup(identity_passphrase);
232 if (identity_new_passphrase)
233 passphrase = xstrdup(identity_new_passphrase);
235 passphrase = read_passphrase("Enter passphrase: ", 1);
236 /* Try to load using the passphrase. */
237 if (!load_private_key(buf, passphrase, private_key, &comment))
239 memset(passphrase, 0, strlen(passphrase));
241 printf("Bad passphrase.\n");
245 printf("Key now has comment '%s'\n", comment);
247 if (identity_comment)
249 strncpy(new_comment, identity_comment, sizeof(new_comment));
250 new_comment[sizeof(new_comment) - 1] = '\0';
254 printf("Enter new comment: ");
256 if (!fgets(new_comment, sizeof(new_comment), stdin))
258 memset(passphrase, 0, strlen(passphrase));
259 RSA_free(private_key);
263 /* Remove terminating newline from comment. */
264 if (strchr(new_comment, '\n'))
265 *strchr(new_comment, '\n') = 0;
268 /* Save the file using the new passphrase. */
269 if (!save_private_key(buf, passphrase, private_key, new_comment))
271 printf("Saving the key failed: %s: %s.\n",
272 buf, strerror(errno));
273 memset(passphrase, 0, strlen(passphrase));
275 RSA_free(private_key);
280 /* Destroy the passphrase and the private key in memory. */
281 memset(passphrase, 0, strlen(passphrase));
283 RSA_free(private_key);
285 /* Save the public key in text format in a file with the same name but
291 printf("Could not save your public key in %s\n", buf);
294 fprintf(f, "%d ", BN_num_bits(public_key->n));
295 tmpbuf = BN_bn2dec(public_key->e);
296 fprintf(f, "%s ", tmpbuf);
298 tmpbuf = BN_bn2dec(public_key->n);
299 fprintf(f, "%s %s\n", tmpbuf, new_comment);
305 printf("The comment in your key file has been changed.\n");
309 /* Main program for key management. */
312 main(int ac, char **av)
314 char buf[16384], buf2[1024], *passphrase1, *passphrase2;
320 char hostname[MAXHOSTNAMELEN];
324 /* check if RSA support exists */
325 if (rsa_alive() == 0) {
328 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
333 /* Get user\'s passwd structure. We need this for the home directory. */
334 pw = getpwuid(getuid());
337 printf("You don't exist, go away!\n");
341 /* Create ~/.ssh directory if it doesn\'t already exist. */
342 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_USER_DIR);
343 if (stat(buf, &st) < 0)
344 if (mkdir(buf, 0755) < 0)
345 error("Could not create directory '%s'.", buf);
347 /* Parse command line arguments. */
348 while ((opt = getopt(ac, av, "qpcb:f:P:N:C:")) != EOF)
354 if (bits < 512 || bits > 32768)
356 printf("Bits has bad value.\n");
362 change_passphrase = 1;
370 identity_file = optarg;
374 identity_passphrase = optarg;
378 identity_new_passphrase = optarg;
382 identity_comment = optarg;
391 printf("ssh-keygen version %s\n", SSH_VERSION);
392 printf("Usage: %s [-b bits] [-p] [-c] [-f file] [-P pass] [-N new-pass] [-C comment]\n", av[0]);
398 printf("Too many arguments.\n");
401 if (change_passphrase && change_comment)
403 printf("Can only have one of -p and -c.\n");
407 /* If the user requested to change the passphrase, do it now. This
408 function never returns. */
409 if (change_passphrase)
410 do_change_passphrase(pw);
412 /* If the user requested to change the comment, do it now. This function
415 do_change_comment(pw);
422 /* Generate the rsa key pair. */
423 private_key = RSA_new();
424 public_key = RSA_new();
425 rsa_generate_key(private_key, public_key, bits);
429 /* Ask for a file to save the key in. */
432 strncpy(buf, identity_file, sizeof(buf));
433 buf[sizeof(buf) - 1] = '\0';
437 printf("Enter file in which to save the key ($HOME/%s): ",
438 SSH_CLIENT_IDENTITY);
440 if (fgets(buf, sizeof(buf), stdin) == NULL)
442 if (strchr(buf, '\n'))
443 *strchr(buf, '\n') = 0;
444 if (strcmp(buf, "") == 0)
445 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
448 /* If the file aready exists, ask the user to confirm. */
449 if (stat(buf, &st) >= 0)
451 printf("%s already exists.\n", buf);
452 printf("Overwrite (y/n)? ");
454 if (fgets(buf2, sizeof(buf2), stdin) == NULL)
456 if (buf2[0] != 'y' && buf2[0] != 'Y')
460 /* Ask for a passphrase (twice). */
461 if (identity_passphrase)
462 passphrase1 = xstrdup(identity_passphrase);
464 if (identity_new_passphrase)
465 passphrase1 = xstrdup(identity_new_passphrase);
470 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
471 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
472 if (strcmp(passphrase1, passphrase2) != 0)
474 /* The passphrases do not match. Clear them and retry. */
475 memset(passphrase1, 0, strlen(passphrase1));
476 memset(passphrase2, 0, strlen(passphrase2));
479 printf("Passphrases do not match. Try again.\n");
480 goto passphrase_again;
482 /* Clear the other copy of the passphrase. */
483 memset(passphrase2, 0, strlen(passphrase2));
487 /* Create default commend field for the passphrase. The user can later
489 if (identity_comment)
491 strlcpy(buf2, identity_comment, sizeof(buf2));
495 if (gethostname(hostname, sizeof(hostname)) < 0)
497 perror("gethostname");
500 snprintf(buf2, sizeof buf2, "%s@%s", pw->pw_name, hostname);
503 /* Save the key with the given passphrase and comment. */
504 if (!save_private_key(buf, passphrase1, private_key, buf2))
506 printf("Saving the key failed: %s: %s.\n",
507 buf, strerror(errno));
508 memset(passphrase1, 0, strlen(passphrase1));
512 /* Clear the passphrase. */
513 memset(passphrase1, 0, strlen(passphrase1));
516 /* Clear the private key and the random number generator. */
517 RSA_free(private_key);
521 printf("Your identification has been saved in %s.\n", buf);
523 /* Display the public key on the screen. */
525 printf("Your public key is:\n");
526 printf("%d ", BN_num_bits(public_key->n));
527 tmpbuf = BN_bn2dec(public_key->e);
528 printf("%s ", tmpbuf);
530 tmpbuf = BN_bn2dec(public_key->n);
531 printf("%s %s\n", tmpbuf, buf2);
535 /* Save the public key in text format in a file with the same name but
541 printf("Could not save your public key in %s\n", buf);
544 fprintf(f, "%d ", BN_num_bits(public_key->n));
545 tmpbuf = BN_bn2dec(public_key->e);
546 fprintf(f, "%s ", tmpbuf);
548 tmpbuf = BN_bn2dec(public_key->n);
549 fprintf(f, "%s %s\n", tmpbuf, buf2);
554 printf("Your public key has been saved in %s\n", buf);