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.
22 #include "fingerprint.h"
24 #ifdef HAVE___PROGNAME
25 extern char *__progname;
26 #else /* HAVE___PROGNAME */
27 const char *__progname = "ssh-keygen";
28 #endif /* HAVE___PROGNAME */
30 /* Generated private key. */
33 /* Generated public key. */
36 /* Number of bits in the RSA key. This value can be changed on the command
40 /* Flag indicating that we just want to change the passphrase. This can be
41 set on the command line. */
42 int change_passphrase = 0;
44 /* Flag indicating that we just want to change the comment. This can be set
45 on the command line. */
46 int change_comment = 0;
50 /* Flag indicating that we just want to see the key fingerprint */
51 int print_fingerprint = 0;
53 /* The identity file name, given on the command line or entered by the user. */
54 char identity_file[1024];
55 int have_identity = 0;
57 /* This is set to the passphrase if given on the command line. */
58 char *identity_passphrase = NULL;
60 /* This is set to the new passphrase if given on the command line. */
61 char *identity_new_passphrase = NULL;
63 /* This is set to the new comment if given on the command line. */
64 char *identity_comment = NULL;
67 extern char *__progname;
70 ask_filename(struct passwd *pw, const char *prompt)
73 snprintf(identity_file, sizeof(identity_file), "%s/%s",
74 pw->pw_dir, SSH_CLIENT_IDENTITY);
75 printf("%s (%s): ", prompt, identity_file);
77 if (fgets(buf, sizeof(buf), stdin) == NULL)
79 if (strchr(buf, '\n'))
80 *strchr(buf, '\n') = 0;
81 if (strcmp(buf, "") != 0)
82 strlcpy(identity_file, buf, sizeof(identity_file));
87 do_fingerprint(struct passwd *pw)
94 ask_filename(pw, "Enter file in which the key is");
95 if (stat(identity_file, &st) < 0)
97 perror(identity_file);
100 public_key = RSA_new();
101 if (!load_public_key(identity_file, public_key, &comment)) {
102 char *cp, line[1024];
104 int dummy, invalid = 0;
105 FILE *f = fopen(identity_file, "r");
108 if (f && fgets(line, sizeof(line), f)) {
110 line[strlen(line)-1] = '\0';
111 if (auth_rsa_read_key(&cp, &dummy, e, n)) {
114 comment = xstrdup(cp ? cp : "no comment");
122 printf("%s is not a valid key file.\n", identity_file);
129 printf("%d %s %s\n", BN_num_bits(public_key->n),
130 fingerprint(public_key->e, public_key->n),
132 RSA_free(public_key);
136 /* Perform changing a passphrase. The argument is the passwd structure
137 for the current user. */
140 do_change_passphrase(struct passwd *pw)
143 char *old_passphrase, *passphrase1, *passphrase2;
148 ask_filename(pw, "Enter file in which the key is");
149 /* Check if the file exists. */
150 if (stat(identity_file, &st) < 0)
152 perror(identity_file);
156 /* Try to load the public key from the file the verify that it is
157 readable and of the proper format. */
158 public_key = RSA_new();
159 if (!load_public_key(identity_file, public_key, NULL))
161 printf("%s is not a valid key file.\n", identity_file);
164 /* Clear the public key since we are just about to load the whole file. */
165 RSA_free(public_key);
167 /* Try to load the file with empty passphrase. */
168 private_key = RSA_new();
169 if (!load_private_key(identity_file, "", private_key, &comment)) {
170 /* Read passphrase from the user. */
171 if (identity_passphrase)
172 old_passphrase = xstrdup(identity_passphrase);
174 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
175 /* Try to load using the passphrase. */
176 if (!load_private_key(identity_file, old_passphrase, private_key, &comment))
178 memset(old_passphrase, 0, strlen(old_passphrase));
179 xfree(old_passphrase);
180 printf("Bad passphrase.\n");
183 /* Destroy the passphrase. */
184 memset(old_passphrase, 0, strlen(old_passphrase));
185 xfree(old_passphrase);
187 printf("Key has comment '%s'\n", comment);
189 /* Ask the new passphrase (twice). */
190 if (identity_new_passphrase)
192 passphrase1 = xstrdup(identity_new_passphrase);
198 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
199 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
201 /* Verify that they are the same. */
202 if (strcmp(passphrase1, passphrase2) != 0)
204 memset(passphrase1, 0, strlen(passphrase1));
205 memset(passphrase2, 0, strlen(passphrase2));
208 printf("Pass phrases do not match. Try again.\n");
211 /* Destroy the other copy. */
212 memset(passphrase2, 0, strlen(passphrase2));
216 /* Save the file using the new passphrase. */
217 if (!save_private_key(identity_file, passphrase1, private_key, comment))
219 printf("Saving the key failed: %s: %s.\n",
220 identity_file, strerror(errno));
221 memset(passphrase1, 0, strlen(passphrase1));
223 RSA_free(private_key);
227 /* Destroy the passphrase and the copy of the key in memory. */
228 memset(passphrase1, 0, strlen(passphrase1));
230 RSA_free(private_key); /* Destroys contents */
233 printf("Your identification has been saved with the new passphrase.\n");
237 /* Change the comment of a private key file. */
240 do_change_comment(struct passwd *pw)
242 char new_comment[1024], *comment;
250 ask_filename(pw, "Enter file in which the key is");
251 /* Check if the file exists. */
252 if (stat(identity_file, &st) < 0)
254 perror(identity_file);
258 /* Try to load the public key from the file the verify that it is
259 readable and of the proper format. */
260 public_key = RSA_new();
261 if (!load_public_key(identity_file, public_key, NULL))
263 printf("%s is not a valid key file.\n", identity_file);
267 private_key = RSA_new();
268 /* Try to load the file with empty passphrase. */
269 if (load_private_key(identity_file, "", private_key, &comment))
270 passphrase = xstrdup("");
273 /* Read passphrase from the user. */
274 if (identity_passphrase)
275 passphrase = xstrdup(identity_passphrase);
277 if (identity_new_passphrase)
278 passphrase = xstrdup(identity_new_passphrase);
280 passphrase = read_passphrase("Enter passphrase: ", 1);
281 /* Try to load using the passphrase. */
282 if (!load_private_key(identity_file, passphrase, private_key, &comment))
284 memset(passphrase, 0, strlen(passphrase));
286 printf("Bad passphrase.\n");
290 printf("Key now has comment '%s'\n", comment);
292 if (identity_comment)
294 strlcpy(new_comment, identity_comment, sizeof(new_comment));
298 printf("Enter new comment: ");
300 if (!fgets(new_comment, sizeof(new_comment), stdin))
302 memset(passphrase, 0, strlen(passphrase));
303 RSA_free(private_key);
307 /* Remove terminating newline from comment. */
308 if (strchr(new_comment, '\n'))
309 *strchr(new_comment, '\n') = 0;
312 /* Save the file using the new passphrase. */
313 if (!save_private_key(identity_file, passphrase, private_key, new_comment))
315 printf("Saving the key failed: %s: %s.\n",
316 identity_file, strerror(errno));
317 memset(passphrase, 0, strlen(passphrase));
319 RSA_free(private_key);
324 /* Destroy the passphrase and the private key in memory. */
325 memset(passphrase, 0, strlen(passphrase));
327 RSA_free(private_key);
329 /* Save the public key in text format in a file with the same name but
331 strlcat(identity_file, ".pub", sizeof(identity_file));
332 f = fopen(identity_file, "w");
335 printf("Could not save your public key in %s\n", identity_file);
338 fprintf(f, "%d ", BN_num_bits(public_key->n));
339 tmpbuf = BN_bn2dec(public_key->e);
340 fprintf(f, "%s ", tmpbuf);
342 tmpbuf = BN_bn2dec(public_key->n);
343 fprintf(f, "%s %s\n", tmpbuf, new_comment);
349 printf("The comment in your key file has been changed.\n");
356 printf("ssh-keygen version %s\n", SSH_VERSION);
357 printf("Usage: %s [-b bits] [-p] [-c] [-f file] [-P pass] [-N new-pass] [-C comment]\n", __progname);
361 /* Main program for key management. */
364 main(int ac, char **av)
366 char buf[16384], buf2[1024], *passphrase1, *passphrase2;
372 char hostname[MAXHOSTNAMELEN];
376 /* check if RSA support exists */
377 if (rsa_alive() == 0) {
378 extern char *__progname;
381 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
386 /* Get user\'s passwd structure. We need this for the home directory. */
387 pw = getpwuid(getuid());
390 printf("You don't exist, go away!\n");
394 /* Create ~/.ssh directory if it doesn\'t already exist. */
395 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_USER_DIR);
396 if (stat(buf, &st) < 0)
397 if (mkdir(buf, 0755) < 0)
398 error("Could not create directory '%s'.", buf);
400 /* Parse command line arguments. */
401 while ((opt = getopt(ac, av, "qpclb:f:P:N:C:")) != EOF)
407 if (bits < 512 || bits > 32768)
409 printf("Bits has bad value.\n");
415 print_fingerprint = 1;
419 change_passphrase = 1;
427 strlcpy(identity_file, optarg, sizeof(identity_file));
432 identity_passphrase = optarg;
436 identity_new_passphrase = optarg;
440 identity_comment = optarg;
454 printf("Too many arguments.\n");
457 if (change_passphrase && change_comment)
459 printf("Can only have one of -p and -c.\n");
463 if (print_fingerprint)
466 /* If the user requested to change the passphrase, do it now. This
467 function never returns. */
468 if (change_passphrase)
469 do_change_passphrase(pw);
471 /* If the user requested to change the comment, do it now. This function
474 do_change_comment(pw);
481 /* Generate the rsa key pair. */
482 private_key = RSA_new();
483 public_key = RSA_new();
484 rsa_generate_key(private_key, public_key, bits);
487 ask_filename(pw, "Enter file in which to save the key");
489 /* If the file aready exists, ask the user to confirm. */
490 if (stat(identity_file, &st) >= 0)
492 printf("%s already exists.\n", identity_file);
493 printf("Overwrite (y/n)? ");
495 if (fgets(buf2, sizeof(buf2), stdin) == NULL)
497 if (buf2[0] != 'y' && buf2[0] != 'Y')
501 /* Ask for a passphrase (twice). */
502 if (identity_passphrase)
503 passphrase1 = xstrdup(identity_passphrase);
505 if (identity_new_passphrase)
506 passphrase1 = xstrdup(identity_new_passphrase);
511 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
512 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
513 if (strcmp(passphrase1, passphrase2) != 0)
515 /* The passphrases do not match. Clear them and retry. */
516 memset(passphrase1, 0, strlen(passphrase1));
517 memset(passphrase2, 0, strlen(passphrase2));
520 printf("Passphrases do not match. Try again.\n");
521 goto passphrase_again;
523 /* Clear the other copy of the passphrase. */
524 memset(passphrase2, 0, strlen(passphrase2));
528 /* Create default commend field for the passphrase. The user can later
530 if (identity_comment)
532 strlcpy(buf2, identity_comment, sizeof(buf2));
536 if (gethostname(hostname, sizeof(hostname)) < 0)
538 perror("gethostname");
541 snprintf(buf2, sizeof buf2, "%s@%s", pw->pw_name, hostname);
544 /* Save the key with the given passphrase and comment. */
545 if (!save_private_key(identity_file, passphrase1, private_key, buf2))
547 printf("Saving the key failed: %s: %s.\n",
548 identity_file, strerror(errno));
549 memset(passphrase1, 0, strlen(passphrase1));
553 /* Clear the passphrase. */
554 memset(passphrase1, 0, strlen(passphrase1));
557 /* Clear the private key and the random number generator. */
558 RSA_free(private_key);
562 printf("Your identification has been saved in %s.\n", identity_file);
564 /* Display the public key on the screen. */
566 printf("Your public key is:\n");
567 printf("%d ", BN_num_bits(public_key->n));
568 tmpbuf = BN_bn2dec(public_key->e);
569 printf("%s ", tmpbuf);
571 tmpbuf = BN_bn2dec(public_key->n);
572 printf("%s %s\n", tmpbuf, buf2);
576 /* Save the public key in text format in a file with the same name but
578 strlcat(identity_file, ".pub", sizeof(identity_file));
579 f = fopen(identity_file, "w");
582 printf("Could not save your public key in %s\n", identity_file);
585 fprintf(f, "%d ", BN_num_bits(public_key->n));
586 tmpbuf = BN_bn2dec(public_key->e);
587 fprintf(f, "%s ", tmpbuf);
589 tmpbuf = BN_bn2dec(public_key->n);
590 fprintf(f, "%s %s\n", tmpbuf, buf2);
595 printf("Your public key has been saved in %s\n", identity_file);