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 /* This is set to the identity file name if given on the command line. */
54 char *identity_file = NULL;
56 /* This is set to the passphrase if given on the command line. */
57 char *identity_passphrase = NULL;
59 /* This is set to the new passphrase if given on the command line. */
60 char *identity_new_passphrase = NULL;
62 /* This is set to the new comment if given on the command line. */
63 char *identity_comment = NULL;
65 /* Perform changing a passphrase. The argument is the passwd structure
66 for the current user. */
69 get_filename(struct passwd *pw, const char *prompt)
71 char buf[1024], default_file[1024];
73 /* Read key file name. */
74 if (identity_file != NULL) {
75 return xstrdup(identity_file);
77 snprintf(default_file, sizeof default_file, "%s/%s",
78 pw->pw_dir, SSH_CLIENT_IDENTITY);
79 printf("%s (%s): ", prompt, default_file);
81 if (fgets(buf, sizeof(buf), stdin) == NULL)
83 if (strchr(buf, '\n'))
84 *strchr(buf, '\n') = 0;
85 if (strcmp(buf, "") == 0)
86 return xstrdup(default_file);
92 do_fingerprint(struct passwd *pw)
98 file = get_filename(pw, "Enter file in which the key is");
99 if (stat(file, &st) < 0)
104 public_key = RSA_new();
105 if (!load_public_key(file, public_key, &comment)) {
106 char *cp, line[1024];
108 int dummy, invalid = 0;
109 FILE *f = fopen(file, "r");
112 if (f && fgets(line, sizeof(line), f)) {
114 line[strlen(line)-1] = '\0';
115 if (auth_rsa_read_key(&cp, &dummy, e, n)) {
118 comment = xstrdup(cp ? cp : "no comment");
126 printf("%s is not a valid key file.\n", file);
133 printf("%d %s %s\n", BN_num_bits(public_key->n),
134 fingerprint(public_key->e, public_key->n),
136 RSA_free(public_key);
142 do_change_passphrase(struct passwd *pw)
144 char *file, *comment;
145 char *old_passphrase, *passphrase1, *passphrase2;
149 file = get_filename(pw, "Enter file in which the key is");
150 /* Check if the file exists. */
151 if (stat(file, &st) < 0)
157 /* Try to load the public key from the file the verify that it is
158 readable and of the proper format. */
159 public_key = RSA_new();
160 if (!load_public_key(file, public_key, NULL))
162 printf("%s is not a valid key file.\n", file);
165 /* Clear the public key since we are just about to load the whole file. */
166 RSA_free(public_key);
168 /* Try to load the file with empty passphrase. */
169 private_key = RSA_new();
170 if (!load_private_key(file, "", private_key, &comment)) {
171 /* Read passphrase from the user. */
172 if (identity_passphrase)
173 old_passphrase = xstrdup(identity_passphrase);
175 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
176 /* Try to load using the passphrase. */
177 if (!load_private_key(file, old_passphrase, private_key, &comment))
179 memset(old_passphrase, 0, strlen(old_passphrase));
180 xfree(old_passphrase);
181 printf("Bad passphrase.\n");
184 /* Destroy the passphrase. */
185 memset(old_passphrase, 0, strlen(old_passphrase));
186 xfree(old_passphrase);
188 printf("Key has comment '%s'\n", comment);
190 /* Ask the new passphrase (twice). */
191 if (identity_new_passphrase)
193 passphrase1 = xstrdup(identity_new_passphrase);
199 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
200 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
202 /* Verify that they are the same. */
203 if (strcmp(passphrase1, passphrase2) != 0)
205 memset(passphrase1, 0, strlen(passphrase1));
206 memset(passphrase2, 0, strlen(passphrase2));
209 printf("Pass phrases do not match. Try again.\n");
212 /* Destroy the other copy. */
213 memset(passphrase2, 0, strlen(passphrase2));
217 /* Save the file using the new passphrase. */
218 if (!save_private_key(file, passphrase1, private_key, comment))
220 printf("Saving the key failed: %s: %s.\n",
221 file, strerror(errno));
222 memset(passphrase1, 0, strlen(passphrase1));
224 RSA_free(private_key);
228 /* Destroy the passphrase and the copy of the key in memory. */
229 memset(passphrase1, 0, strlen(passphrase1));
231 RSA_free(private_key); /* Destroys contents */
234 printf("Your identification has been saved with the new passphrase.\n");
238 /* Change the comment of a private key file. */
241 do_change_comment(struct passwd *pw)
243 char new_comment[1024], *file, *comment;
250 file = get_filename(pw, "Enter file in which the key is");
251 /* Check if the file exists. */
252 if (stat(file, &st) < 0)
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(file, public_key, NULL))
263 printf("%s is not a valid key file.\n", file);
267 private_key = RSA_new();
268 /* Try to load the file with empty passphrase. */
269 if (load_private_key(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(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(file, passphrase, private_key, new_comment))
315 printf("Saving the key failed: %s: %s.\n",
316 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 strcat(file, ".pub");
332 f = fopen(file, "w");
335 printf("Could not save your public key in %s\n", 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");
353 /* Main program for key management. */
356 main(int ac, char **av)
358 char buf[16384], buf2[1024], *passphrase1, *passphrase2;
364 char hostname[MAXHOSTNAMELEN];
368 /* check if RSA support exists */
369 if (rsa_alive() == 0) {
370 extern char *__progname;
373 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
378 /* Get user\'s passwd structure. We need this for the home directory. */
379 pw = getpwuid(getuid());
382 printf("You don't exist, go away!\n");
386 /* Create ~/.ssh directory if it doesn\'t already exist. */
387 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_USER_DIR);
388 if (stat(buf, &st) < 0)
389 if (mkdir(buf, 0755) < 0)
390 error("Could not create directory '%s'.", buf);
392 /* Parse command line arguments. */
393 while ((opt = getopt(ac, av, "qpclb:f:P:N:C:")) != EOF)
399 if (bits < 512 || bits > 32768)
401 printf("Bits has bad value.\n");
407 print_fingerprint = 1;
411 change_passphrase = 1;
419 identity_file = optarg;
423 identity_passphrase = optarg;
427 identity_new_passphrase = optarg;
431 identity_comment = optarg;
440 printf("ssh-keygen version %s\n", SSH_VERSION);
441 printf("Usage: %s [-b bits] [-p] [-c] [-f file] [-P pass] [-N new-pass] [-C comment]\n", av[0]);
447 printf("Too many arguments.\n");
450 if (change_passphrase && change_comment)
452 printf("Can only have one of -p and -c.\n");
456 if (print_fingerprint)
459 /* If the user requested to change the passphrase, do it now. This
460 function never returns. */
461 if (change_passphrase)
462 do_change_passphrase(pw);
464 /* If the user requested to change the comment, do it now. This function
467 do_change_comment(pw);
474 /* Generate the rsa key pair. */
475 private_key = RSA_new();
476 public_key = RSA_new();
477 rsa_generate_key(private_key, public_key, bits);
481 file = get_filename(pw, "Enter file in which to save the key");
483 /* If the file aready exists, ask the user to confirm. */
484 if (stat(file, &st) >= 0)
486 printf("%s already exists.\n", file);
487 printf("Overwrite (y/n)? ");
489 if (fgets(buf2, sizeof(buf2), stdin) == NULL)
491 if (buf2[0] != 'y' && buf2[0] != 'Y')
495 /* Ask for a passphrase (twice). */
496 if (identity_passphrase)
497 passphrase1 = xstrdup(identity_passphrase);
499 if (identity_new_passphrase)
500 passphrase1 = xstrdup(identity_new_passphrase);
505 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
506 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
507 if (strcmp(passphrase1, passphrase2) != 0)
509 /* The passphrases do not match. Clear them and retry. */
510 memset(passphrase1, 0, strlen(passphrase1));
511 memset(passphrase2, 0, strlen(passphrase2));
514 printf("Passphrases do not match. Try again.\n");
515 goto passphrase_again;
517 /* Clear the other copy of the passphrase. */
518 memset(passphrase2, 0, strlen(passphrase2));
522 /* Create default commend field for the passphrase. The user can later
524 if (identity_comment)
526 strlcpy(buf2, identity_comment, sizeof(buf2));
530 if (gethostname(hostname, sizeof(hostname)) < 0)
532 perror("gethostname");
535 snprintf(buf2, sizeof buf2, "%s@%s", pw->pw_name, hostname);
538 /* Save the key with the given passphrase and comment. */
539 if (!save_private_key(file, passphrase1, private_key, buf2))
541 printf("Saving the key failed: %s: %s.\n",
542 file, strerror(errno));
543 memset(passphrase1, 0, strlen(passphrase1));
548 /* Clear the passphrase. */
549 memset(passphrase1, 0, strlen(passphrase1));
552 /* Clear the private key and the random number generator. */
553 RSA_free(private_key);
557 printf("Your identification has been saved in %s.\n", file);
559 /* Display the public key on the screen. */
561 printf("Your public key is:\n");
562 printf("%d ", BN_num_bits(public_key->n));
563 tmpbuf = BN_bn2dec(public_key->e);
564 printf("%s ", tmpbuf);
566 tmpbuf = BN_bn2dec(public_key->n);
567 printf("%s %s\n", tmpbuf, buf2);
571 /* Save the public key in text format in a file with the same name but
573 file = xrealloc(file, strlen(file) + 5);
574 strcat(file, ".pub");
575 f = fopen(file, "w");
578 printf("Could not save your public key in %s\n", file);
581 fprintf(f, "%d ", BN_num_bits(public_key->n));
582 tmpbuf = BN_bn2dec(public_key->e);
583 fprintf(f, "%s ", tmpbuf);
585 tmpbuf = BN_bn2dec(public_key->n);
586 fprintf(f, "%s %s\n", tmpbuf, buf2);
591 printf("Your public key has been saved in %s\n", file);