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 /* Generated private key. */
26 /* Generated public key. */
29 /* Number of bits in the RSA key. This value can be changed on the command
33 /* Flag indicating that we just want to change the passphrase. This can be
34 set on the command line. */
35 int change_passphrase = 0;
37 /* Flag indicating that we just want to change the comment. This can be set
38 on the command line. */
39 int change_comment = 0;
43 /* This is set to the identity file name if given on the command line. */
44 char *identity_file = NULL;
46 /* This is set to the passphrase if given on the command line. */
47 char *identity_passphrase = NULL;
49 /* This is set to the new passphrase if given on the command line. */
50 char *identity_new_passphrase = NULL;
52 /* This is set to the new comment if given on the command line. */
53 char *identity_comment = NULL;
55 /* Perform changing a passphrase. The argument is the passwd structure
56 for the current user. */
59 do_change_passphrase(struct passwd *pw)
61 char buf[1024], *comment;
62 char *old_passphrase, *passphrase1, *passphrase2;
66 /* Read key file name. */
67 if (identity_file != NULL) {
68 strncpy(buf, identity_file, sizeof(buf));
69 buf[sizeof(buf) - 1] = '\0';
71 printf("Enter file in which the key is ($HOME/%s): ", SSH_CLIENT_IDENTITY);
73 if (fgets(buf, sizeof(buf), stdin) == NULL)
75 if (strchr(buf, '\n'))
76 *strchr(buf, '\n') = 0;
77 if (strcmp(buf, "") == 0)
78 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
81 /* Check if the file exists. */
82 if (stat(buf, &st) < 0)
88 /* Try to load the public key from the file the verify that it is
89 readable and of the proper format. */
90 public_key = RSA_new();
91 if (!load_public_key(buf, public_key, NULL))
93 printf("%s is not a valid key file.\n", buf);
96 /* Clear the public key since we are just about to load the whole file. */
99 /* Try to load the file with empty passphrase. */
100 private_key = RSA_new();
101 if (!load_private_key(buf, "", private_key, &comment)) {
102 /* Read passphrase from the user. */
103 if (identity_passphrase)
104 old_passphrase = xstrdup(identity_passphrase);
106 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
107 /* Try to load using the passphrase. */
108 if (!load_private_key(buf, old_passphrase, private_key, &comment))
110 memset(old_passphrase, 0, strlen(old_passphrase));
111 xfree(old_passphrase);
112 printf("Bad passphrase.\n");
115 /* Destroy the passphrase. */
116 memset(old_passphrase, 0, strlen(old_passphrase));
117 xfree(old_passphrase);
119 printf("Key has comment '%s'\n", comment);
121 /* Ask the new passphrase (twice). */
122 if (identity_new_passphrase)
124 passphrase1 = xstrdup(identity_new_passphrase);
130 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
131 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
133 /* Verify that they are the same. */
134 if (strcmp(passphrase1, passphrase2) != 0)
136 memset(passphrase1, 0, strlen(passphrase1));
137 memset(passphrase2, 0, strlen(passphrase2));
140 printf("Pass phrases do not match. Try again.\n");
143 /* Destroy the other copy. */
144 memset(passphrase2, 0, strlen(passphrase2));
148 /* Save the file using the new passphrase. */
149 if (!save_private_key(buf, passphrase1, private_key, comment))
151 printf("Saving the key failed: %s: %s.\n",
152 buf, strerror(errno));
153 memset(passphrase1, 0, strlen(passphrase1));
155 RSA_free(private_key);
159 /* Destroy the passphrase and the copy of the key in memory. */
160 memset(passphrase1, 0, strlen(passphrase1));
162 RSA_free(private_key); /* Destroys contents */
165 printf("Your identification has been saved with the new passphrase.\n");
169 /* Change the comment of a private key file. */
172 do_change_comment(struct passwd *pw)
174 char buf[1024], new_comment[1024], *comment;
181 /* Read key file name. */
184 strncpy(buf, identity_file, sizeof(buf));
185 buf[sizeof(buf) - 1] = '\0';
189 printf("Enter file in which the key is ($HOME/%s): ",
190 SSH_CLIENT_IDENTITY);
192 if (fgets(buf, sizeof(buf), stdin) == NULL)
194 if (strchr(buf, '\n'))
195 *strchr(buf, '\n') = 0;
196 if (strcmp(buf, "") == 0)
197 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
200 /* Check if the file exists. */
201 if (stat(buf, &st) < 0)
207 /* Try to load the public key from the file the verify that it is
208 readable and of the proper format. */
209 public_key = RSA_new();
210 if (!load_public_key(buf, public_key, NULL))
212 printf("%s is not a valid key file.\n", buf);
216 private_key = RSA_new();
217 /* Try to load the file with empty passphrase. */
218 if (load_private_key(buf, "", private_key, &comment))
219 passphrase = xstrdup("");
222 /* Read passphrase from the user. */
223 if (identity_passphrase)
224 passphrase = xstrdup(identity_passphrase);
226 if (identity_new_passphrase)
227 passphrase = xstrdup(identity_new_passphrase);
229 passphrase = read_passphrase("Enter passphrase: ", 1);
230 /* Try to load using the passphrase. */
231 if (!load_private_key(buf, passphrase, private_key, &comment))
233 memset(passphrase, 0, strlen(passphrase));
235 printf("Bad passphrase.\n");
239 printf("Key now has comment '%s'\n", comment);
241 if (identity_comment)
243 strncpy(new_comment, identity_comment, sizeof(new_comment));
244 new_comment[sizeof(new_comment) - 1] = '\0';
248 printf("Enter new comment: ");
250 if (!fgets(new_comment, sizeof(new_comment), stdin))
252 memset(passphrase, 0, strlen(passphrase));
253 RSA_free(private_key);
257 /* Remove terminating newline from comment. */
258 if (strchr(new_comment, '\n'))
259 *strchr(new_comment, '\n') = 0;
262 /* Save the file using the new passphrase. */
263 if (!save_private_key(buf, passphrase, private_key, new_comment))
265 printf("Saving the key failed: %s: %s.\n",
266 buf, strerror(errno));
267 memset(passphrase, 0, strlen(passphrase));
269 RSA_free(private_key);
274 /* Destroy the passphrase and the private key in memory. */
275 memset(passphrase, 0, strlen(passphrase));
277 RSA_free(private_key);
279 /* Save the public key in text format in a file with the same name but
285 printf("Could not save your public key in %s\n", buf);
288 fprintf(f, "%d ", BN_num_bits(public_key->n));
289 tmpbuf = BN_bn2dec(public_key->e);
290 fprintf(f, "%s ", tmpbuf);
292 tmpbuf = BN_bn2dec(public_key->n);
293 fprintf(f, "%s %s\n", tmpbuf, new_comment);
299 printf("The comment in your key file has been changed.\n");
303 /* Main program for key management. */
306 main(int ac, char **av)
308 char buf[16384], buf2[1024], *passphrase1, *passphrase2;
314 char hostname[MAXHOSTNAMELEN];
318 /* check if RSA support exists */
319 if (rsa_alive() == 0) {
320 extern char *__progname;
323 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
328 /* Get user\'s passwd structure. We need this for the home directory. */
329 pw = getpwuid(getuid());
332 printf("You don't exist, go away!\n");
336 /* Create ~/.ssh directory if it doesn\'t already exist. */
337 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_USER_DIR);
338 if (stat(buf, &st) < 0)
339 if (mkdir(buf, 0755) < 0)
340 error("Could not create directory '%s'.", buf);
342 /* Parse command line arguments. */
343 while ((opt = getopt(ac, av, "qpcb:f:P:N:C:")) != EOF)
349 if (bits < 512 || bits > 32768)
351 printf("Bits has bad value.\n");
357 change_passphrase = 1;
365 identity_file = optarg;
369 identity_passphrase = optarg;
373 identity_new_passphrase = optarg;
377 identity_comment = optarg;
386 printf("ssh-keygen version %s\n", SSH_VERSION);
387 printf("Usage: %s [-b bits] [-p] [-c] [-f file] [-P pass] [-N new-pass] [-C comment]\n", av[0]);
393 printf("Too many arguments.\n");
396 if (change_passphrase && change_comment)
398 printf("Can only have one of -p and -c.\n");
402 /* If the user requested to change the passphrase, do it now. This
403 function never returns. */
404 if (change_passphrase)
405 do_change_passphrase(pw);
407 /* If the user requested to change the comment, do it now. This function
410 do_change_comment(pw);
417 /* Generate the rsa key pair. */
418 private_key = RSA_new();
419 public_key = RSA_new();
420 rsa_generate_key(private_key, public_key, bits);
424 /* Ask for a file to save the key in. */
427 strncpy(buf, identity_file, sizeof(buf));
428 buf[sizeof(buf) - 1] = '\0';
432 printf("Enter file in which to save the key ($HOME/%s): ",
433 SSH_CLIENT_IDENTITY);
435 if (fgets(buf, sizeof(buf), stdin) == NULL)
437 if (strchr(buf, '\n'))
438 *strchr(buf, '\n') = 0;
439 if (strcmp(buf, "") == 0)
440 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
443 /* If the file aready exists, ask the user to confirm. */
444 if (stat(buf, &st) >= 0)
446 printf("%s already exists.\n", buf);
447 printf("Overwrite (y/n)? ");
449 if (fgets(buf2, sizeof(buf2), stdin) == NULL)
451 if (buf2[0] != 'y' && buf2[0] != 'Y')
455 /* Ask for a passphrase (twice). */
456 if (identity_passphrase)
457 passphrase1 = xstrdup(identity_passphrase);
459 if (identity_new_passphrase)
460 passphrase1 = xstrdup(identity_new_passphrase);
465 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
466 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
467 if (strcmp(passphrase1, passphrase2) != 0)
469 /* The passphrases do not match. Clear them and retry. */
470 memset(passphrase1, 0, strlen(passphrase1));
471 memset(passphrase2, 0, strlen(passphrase2));
474 printf("Passphrases do not match. Try again.\n");
475 goto passphrase_again;
477 /* Clear the other copy of the passphrase. */
478 memset(passphrase2, 0, strlen(passphrase2));
482 /* Create default commend field for the passphrase. The user can later
484 if (identity_comment)
486 strlcpy(buf2, identity_comment, sizeof(buf2));
490 if (gethostname(hostname, sizeof(hostname)) < 0)
492 perror("gethostname");
495 snprintf(buf2, sizeof buf2, "%s@%s", pw->pw_name, hostname);
498 /* Save the key with the given passphrase and comment. */
499 if (!save_private_key(buf, passphrase1, private_key, buf2))
501 printf("Saving the key failed: %s: %s.\n",
502 buf, strerror(errno));
503 memset(passphrase1, 0, strlen(passphrase1));
507 /* Clear the passphrase. */
508 memset(passphrase1, 0, strlen(passphrase1));
511 /* Clear the private key and the random number generator. */
512 RSA_free(private_key);
516 printf("Your identification has been saved in %s.\n", buf);
518 /* Display the public key on the screen. */
520 printf("Your public key is:\n");
521 printf("%d ", BN_num_bits(public_key->n));
522 tmpbuf = BN_bn2dec(public_key->e);
523 printf("%s ", tmpbuf);
525 tmpbuf = BN_bn2dec(public_key->n);
526 printf("%s %s\n", tmpbuf, buf2);
530 /* Save the public key in text format in a file with the same name but
536 printf("Could not save your public key in %s\n", buf);
539 fprintf(f, "%d ", BN_num_bits(public_key->n));
540 tmpbuf = BN_bn2dec(public_key->e);
541 fprintf(f, "%s ", tmpbuf);
543 tmpbuf = BN_bn2dec(public_key->n);
544 fprintf(f, "%s %s\n", tmpbuf, buf2);
549 printf("Your public key has been saved in %s\n", buf);