2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Identity and host key generation and maintenance.
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
15 RCSID("$OpenBSD: ssh-keygen.c,v 1.38 2000/12/28 18:58:39 markus Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
19 #include <openssl/rsa.h>
20 #include <openssl/dsa.h>
32 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
36 * Flag indicating that we just want to change the passphrase. This can be
37 * set on the command line.
39 int change_passphrase = 0;
42 * Flag indicating that we just want to change the comment. This can be set
43 * on the command line.
45 int change_comment = 0;
49 /* Flag indicating that we just want to see the key fingerprint */
50 int print_fingerprint = 0;
52 /* The identity file name, given on the command line or entered by the user. */
53 char identity_file[1024];
54 int have_identity = 0;
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 /* Dump public key file in format used by real and the original SSH 2 */
66 int convert_to_ssh2 = 0;
67 int convert_from_ssh2 = 0;
70 /* default to RSA for SSH-1 */
71 char *key_type_name = "rsa1";
74 #ifdef HAVE___PROGNAME
75 extern char *__progname;
80 char hostname[MAXHOSTNAMELEN];
83 ask_filename(struct passwd *pw, const char *prompt)
88 switch (key_type_from_name(key_type_name)) {
90 name = SSH_CLIENT_IDENTITY;
93 name = SSH_CLIENT_ID_DSA;
96 name = SSH_CLIENT_ID_RSA;
99 fprintf(stderr, "bad key type");
103 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
104 fprintf(stderr, "%s (%s): ", prompt, identity_file);
106 if (fgets(buf, sizeof(buf), stdin) == NULL)
108 if (strchr(buf, '\n'))
109 *strchr(buf, '\n') = 0;
110 if (strcmp(buf, "") != 0)
111 strlcpy(identity_file, buf, sizeof(identity_file));
116 try_load_key(char *filename, Key *k)
119 if (!load_private_key(filename, "", k, NULL)) {
120 char *pass = read_passphrase("Enter passphrase: ", 1);
121 if (!load_private_key(filename, pass, k, NULL)) {
124 memset(pass, 0, strlen(pass));
130 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
131 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
132 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
133 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
136 do_convert_to_ssh2(struct passwd *pw)
144 ask_filename(pw, "Enter file in which the key is");
145 if (stat(identity_file, &st) < 0) {
146 perror(identity_file);
149 k = key_new(KEY_UNSPEC);
150 if (!try_load_key(identity_file, k)) {
151 fprintf(stderr, "load failed\n");
154 key_to_blob(k, &blob, &len);
155 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
157 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
158 key_size(k), key_type(k),
159 pw->pw_name, hostname);
160 dump_base64(stdout, blob, len);
161 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
168 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
170 int bits = buffer_get_int(b);
171 int bytes = (bits + 7) / 8;
172 if (buffer_len(b) < bytes)
173 fatal("buffer_get_bignum_bits: input buffer too small");
174 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
175 buffer_consume(b, bytes);
179 do_convert_private_ssh2_from_blob(char *blob, int blen)
184 int ignore, magic, rlen;
188 buffer_append(&b, blob, blen);
190 magic = buffer_get_int(&b);
191 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
192 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
196 ignore = buffer_get_int(&b);
197 type = buffer_get_string(&b, NULL);
198 cipher = buffer_get_string(&b, NULL);
199 ignore = buffer_get_int(&b);
200 ignore = buffer_get_int(&b);
201 ignore = buffer_get_int(&b);
204 if (strcmp(cipher, "none") != 0) {
205 error("unsupported cipher %s", cipher);
212 key = key_new(KEY_DSA);
214 dsa->priv_key = BN_new();
215 if (dsa->priv_key == NULL) {
216 error("alloc priv_key failed");
220 buffer_get_bignum_bits(&b, dsa->p);
221 buffer_get_bignum_bits(&b, dsa->g);
222 buffer_get_bignum_bits(&b, dsa->q);
223 buffer_get_bignum_bits(&b, dsa->pub_key);
224 buffer_get_bignum_bits(&b, dsa->priv_key);
225 rlen = buffer_len(&b);
227 error("do_convert_private_ssh2_from_blob: remaining bytes in key blob %d", rlen);
233 do_convert_from_ssh2(struct passwd *pw)
241 int escaped = 0, private = 0, ok;
245 ask_filename(pw, "Enter file in which the key is");
246 if (stat(identity_file, &st) < 0) {
247 perror(identity_file);
250 fp = fopen(identity_file, "r");
252 perror(identity_file);
256 while (fgets(line, sizeof(line), fp)) {
257 if (!(p = strchr(line, '\n'))) {
258 fprintf(stderr, "input line too long.\n");
261 if (p > line && p[-1] == '\\')
263 if (strncmp(line, "----", 4) == 0 ||
264 strstr(line, ": ") != NULL) {
265 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
267 fprintf(stderr, "ignore: %s", line);
272 fprintf(stderr, "escaped: %s", line);
276 strlcat(encoded, line, sizeof(encoded));
278 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
280 fprintf(stderr, "uudecode failed.\n");
284 do_convert_private_ssh2_from_blob(blob, blen) :
285 key_from_blob(blob, blen);
287 fprintf(stderr, "decode blob failed.\n");
291 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
292 key_write(k, stdout);
294 fprintf(stderr, "key write failed");
298 fprintf(stdout, "\n");
304 do_print_public(struct passwd *pw)
310 ask_filename(pw, "Enter file in which the key is");
311 if (stat(identity_file, &st) < 0) {
312 perror(identity_file);
315 k = key_new(KEY_UNSPEC);
316 if (!try_load_key(identity_file, k)) {
317 fprintf(stderr, "load failed\n");
320 if (!key_write(k, stdout))
321 fprintf(stderr, "key_write failed");
323 fprintf(stdout, "\n");
328 do_fingerprint(struct passwd *pw)
333 char *comment = NULL, *cp, *ep, line[16*1024];
334 int i, skip = 0, num = 1, invalid = 1, success = 0;
338 ask_filename(pw, "Enter file in which the key is");
339 if (stat(identity_file, &st) < 0) {
340 perror(identity_file);
343 public = key_new(KEY_RSA1);
344 if (load_public_key(identity_file, public, &comment)) {
348 public = key_new(KEY_UNSPEC);
349 if (try_load_public_key(identity_file, public, &comment))
352 debug("try_load_public_key KEY_UNSPEC failed");
355 printf("%d %s %s\n", key_size(public), key_fingerprint(public), comment);
361 f = fopen(identity_file, "r");
363 while (fgets(line, sizeof(line), f)) {
364 i = strlen(line) - 1;
365 if (line[i] != '\n') {
366 error("line %d too long: %.40s...", num, line);
377 /* Skip leading whitespace, empty and comment lines. */
378 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
380 if (!*cp || *cp == '\n' || *cp == '#')
382 i = strtol(cp, &ep, 10);
383 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
386 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
387 if (*cp == '\\' && cp[1] == '"')
388 cp++; /* Skip both */
397 public = key_new(KEY_RSA1);
398 if (key_read(public, &cp) != 1) {
401 public = key_new(KEY_UNSPEC);
402 if (key_read(public, &cp) != 1) {
407 comment = *cp ? cp : comment;
408 printf("%d %s %s\n", key_size(public),
409 key_fingerprint(public),
410 comment ? comment : "no comment");
417 printf("%s is not a valid key file.\n", identity_file);
424 * Perform changing a passphrase. The argument is the passwd structure
425 * for the current user.
428 do_change_passphrase(struct passwd *pw)
431 char *old_passphrase, *passphrase1, *passphrase2;
438 ask_filename(pw, "Enter file in which the key is");
439 if (stat(identity_file, &st) < 0) {
440 perror(identity_file);
443 public = key_new(type);
444 if (!load_public_key(identity_file, public, NULL)) {
447 /* Clear the public key since we are just about to load the whole file. */
450 /* Try to load the file with empty passphrase. */
451 private = key_new(type);
452 if (!load_private_key(identity_file, "", private, &comment)) {
453 if (identity_passphrase)
454 old_passphrase = xstrdup(identity_passphrase);
456 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
457 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
458 memset(old_passphrase, 0, strlen(old_passphrase));
459 xfree(old_passphrase);
460 printf("Bad passphrase.\n");
463 memset(old_passphrase, 0, strlen(old_passphrase));
464 xfree(old_passphrase);
466 printf("Key has comment '%s'\n", comment);
468 /* Ask the new passphrase (twice). */
469 if (identity_new_passphrase) {
470 passphrase1 = xstrdup(identity_new_passphrase);
474 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
475 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
477 /* Verify that they are the same. */
478 if (strcmp(passphrase1, passphrase2) != 0) {
479 memset(passphrase1, 0, strlen(passphrase1));
480 memset(passphrase2, 0, strlen(passphrase2));
483 printf("Pass phrases do not match. Try again.\n");
486 /* Destroy the other copy. */
487 memset(passphrase2, 0, strlen(passphrase2));
491 /* Save the file using the new passphrase. */
492 if (!save_private_key(identity_file, passphrase1, private, comment)) {
493 printf("Saving the key failed: %s: %s.\n",
494 identity_file, strerror(errno));
495 memset(passphrase1, 0, strlen(passphrase1));
501 /* Destroy the passphrase and the copy of the key in memory. */
502 memset(passphrase1, 0, strlen(passphrase1));
504 key_free(private); /* Destroys contents */
507 printf("Your identification has been saved with the new passphrase.\n");
512 * Change the comment of a private key file.
515 do_change_comment(struct passwd *pw)
517 char new_comment[1024], *comment;
525 ask_filename(pw, "Enter file in which the key is");
526 if (stat(identity_file, &st) < 0) {
527 perror(identity_file);
531 * Try to load the public key from the file the verify that it is
532 * readable and of the proper format.
534 public = key_new(KEY_RSA1);
535 if (!load_public_key(identity_file, public, NULL)) {
536 printf("%s is not a valid key file.\n", identity_file);
540 private = key_new(KEY_RSA1);
541 if (load_private_key(identity_file, "", private, &comment))
542 passphrase = xstrdup("");
544 if (identity_passphrase)
545 passphrase = xstrdup(identity_passphrase);
546 else if (identity_new_passphrase)
547 passphrase = xstrdup(identity_new_passphrase);
549 passphrase = read_passphrase("Enter passphrase: ", 1);
550 /* Try to load using the passphrase. */
551 if (!load_private_key(identity_file, passphrase, private, &comment)) {
552 memset(passphrase, 0, strlen(passphrase));
554 printf("Bad passphrase.\n");
558 printf("Key now has comment '%s'\n", comment);
560 if (identity_comment) {
561 strlcpy(new_comment, identity_comment, sizeof(new_comment));
563 printf("Enter new comment: ");
565 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
566 memset(passphrase, 0, strlen(passphrase));
570 if (strchr(new_comment, '\n'))
571 *strchr(new_comment, '\n') = 0;
574 /* Save the file using the new passphrase. */
575 if (!save_private_key(identity_file, passphrase, private, new_comment)) {
576 printf("Saving the key failed: %s: %s.\n",
577 identity_file, strerror(errno));
578 memset(passphrase, 0, strlen(passphrase));
584 memset(passphrase, 0, strlen(passphrase));
588 strlcat(identity_file, ".pub", sizeof(identity_file));
589 f = fopen(identity_file, "w");
591 printf("Could not save your public key in %s\n", identity_file);
594 if (!key_write(public, f))
595 fprintf(stderr, "write key failed");
597 fprintf(f, " %s\n", new_comment);
602 printf("The comment in your key file has been changed.\n");
609 printf("Usage: %s [-lpqxXyc] [-t type] [-b bits] [-f file] [-C comment] [-N new-pass] [-P pass]\n", __progname);
614 * Main program for key management.
617 main(int ac, char **av)
619 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
630 __progname = get_progname(av[0]);
633 SSLeay_add_all_algorithms();
635 /* we need this for the home * directory. */
636 pw = getpwuid(getuid());
638 printf("You don't exist, go away!\n");
641 if (gethostname(hostname, sizeof(hostname)) < 0) {
642 perror("gethostname");
646 while ((opt = getopt(ac, av, "dqpclRxXyb:f:t:P:N:C:")) != EOF) {
650 if (bits < 512 || bits > 32768) {
651 printf("Bits has bad value.\n");
657 print_fingerprint = 1;
661 change_passphrase = 1;
669 strlcpy(identity_file, optarg, sizeof(identity_file));
674 identity_passphrase = optarg;
678 identity_new_passphrase = optarg;
682 identity_comment = optarg;
699 convert_from_ssh2 = 1;
707 key_type_name = "dsa";
711 key_type_name = optarg;
720 printf("Too many arguments.\n");
723 if (change_passphrase && change_comment) {
724 printf("Can only have one of -p and -c.\n");
727 if (print_fingerprint)
729 if (change_passphrase)
730 do_change_passphrase(pw);
732 do_change_comment(pw);
734 do_convert_to_ssh2(pw);
735 if (convert_from_ssh2)
736 do_convert_from_ssh2(pw);
742 type = key_type_from_name(key_type_name);
743 if (type == KEY_UNSPEC) {
744 fprintf(stderr, "unknown key type %s\n", key_type_name);
748 printf("Generating public/private %s key pair.\n", key_type_name);
749 private = key_generate(type, bits);
750 if (private == NULL) {
751 fprintf(stderr, "key_generate failed");
754 public = key_from_private(private);
757 ask_filename(pw, "Enter file in which to save the key");
759 /* Create ~/.ssh directory if it doesn\'t already exist. */
760 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR);
761 if (strstr(identity_file, dotsshdir) != NULL &&
762 stat(dotsshdir, &st) < 0) {
763 if (mkdir(dotsshdir, 0700) < 0)
764 error("Could not create directory '%s'.", dotsshdir);
766 printf("Created directory '%s'.\n", dotsshdir);
768 /* If the file already exists, ask the user to confirm. */
769 if (stat(identity_file, &st) >= 0) {
771 printf("%s already exists.\n", identity_file);
772 printf("Overwrite (y/n)? ");
774 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
776 if (yesno[0] != 'y' && yesno[0] != 'Y')
779 /* Ask for a passphrase (twice). */
780 if (identity_passphrase)
781 passphrase1 = xstrdup(identity_passphrase);
782 else if (identity_new_passphrase)
783 passphrase1 = xstrdup(identity_new_passphrase);
787 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
788 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
789 if (strcmp(passphrase1, passphrase2) != 0) {
790 /* The passphrases do not match. Clear them and retry. */
791 memset(passphrase1, 0, strlen(passphrase1));
792 memset(passphrase2, 0, strlen(passphrase2));
795 printf("Passphrases do not match. Try again.\n");
796 goto passphrase_again;
798 /* Clear the other copy of the passphrase. */
799 memset(passphrase2, 0, strlen(passphrase2));
803 if (identity_comment) {
804 strlcpy(comment, identity_comment, sizeof(comment));
806 /* Create default commend field for the passphrase. */
807 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
810 /* Save the key with the given passphrase and comment. */
811 if (!save_private_key(identity_file, passphrase1, private, comment)) {
812 printf("Saving the key failed: %s: %s.\n",
813 identity_file, strerror(errno));
814 memset(passphrase1, 0, strlen(passphrase1));
818 /* Clear the passphrase. */
819 memset(passphrase1, 0, strlen(passphrase1));
822 /* Clear the private key and the random number generator. */
827 printf("Your identification has been saved in %s.\n", identity_file);
829 strlcat(identity_file, ".pub", sizeof(identity_file));
830 f = fopen(identity_file, "w");
832 printf("Could not save your public key in %s\n", identity_file);
835 if (!key_write(public, f))
836 fprintf(stderr, "write key failed");
837 fprintf(f, " %s\n", comment);
841 printf("Your public key has been saved in %s.\n",
843 printf("The key fingerprint is:\n");
844 printf("%s %s\n", key_fingerprint(public), comment);