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.61 2001/05/25 14:37:32 markus Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
27 #include "pathnames.h"
31 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
35 * Flag indicating that we just want to change the passphrase. This can be
36 * set on the command line.
38 int change_passphrase = 0;
41 * Flag indicating that we just want to change the comment. This can be set
42 * on the command line.
44 int change_comment = 0;
48 /* Flag indicating that we just want to see the key fingerprint */
49 int print_fingerprint = 0;
50 int print_bubblebabble = 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 = _PATH_SSH_CLIENT_IDENTITY;
93 name = _PATH_SSH_CLIENT_ID_DSA;
96 name = _PATH_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 load_identity(char *filename)
121 prv = key_load_private(filename, "", NULL);
123 if (identity_passphrase)
124 pass = xstrdup(identity_passphrase);
126 pass = read_passphrase("Enter passphrase: ", 1);
127 prv = key_load_private(filename, pass, NULL);
128 memset(pass, 0, strlen(pass));
134 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
135 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
136 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
137 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
140 do_convert_to_ssh2(struct passwd *pw)
148 ask_filename(pw, "Enter file in which the key is");
149 if (stat(identity_file, &st) < 0) {
150 perror(identity_file);
153 if ((k = key_load_public(identity_file, NULL)) == NULL) {
154 if ((k = load_identity(identity_file)) == NULL) {
155 fprintf(stderr, "load failed\n");
159 key_to_blob(k, &blob, &len);
160 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
162 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
163 key_size(k), key_type(k),
164 pw->pw_name, hostname);
165 dump_base64(stdout, blob, len);
166 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
173 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
175 int bits = buffer_get_int(b);
176 int bytes = (bits + 7) / 8;
178 if (buffer_len(b) < bytes)
179 fatal("buffer_get_bignum_bits: input buffer too small: "
180 "need %d have %d", bytes, buffer_len(b));
181 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
182 buffer_consume(b, bytes);
186 do_convert_private_ssh2_from_blob(char *blob, int blen)
190 int ignore, magic, rlen, ktype;
194 buffer_append(&b, blob, blen);
196 magic = buffer_get_int(&b);
197 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
198 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
202 ignore = buffer_get_int(&b);
203 type = buffer_get_string(&b, NULL);
204 cipher = buffer_get_string(&b, NULL);
205 ignore = buffer_get_int(&b);
206 ignore = buffer_get_int(&b);
207 ignore = buffer_get_int(&b);
209 if (strcmp(cipher, "none") != 0) {
210 error("unsupported cipher %s", cipher);
218 if (strstr(type, "dsa")) {
220 } else if (strstr(type, "rsa")) {
226 key = key_new_private(ktype);
231 buffer_get_bignum_bits(&b, key->dsa->p);
232 buffer_get_bignum_bits(&b, key->dsa->g);
233 buffer_get_bignum_bits(&b, key->dsa->q);
234 buffer_get_bignum_bits(&b, key->dsa->pub_key);
235 buffer_get_bignum_bits(&b, key->dsa->priv_key);
238 if (!BN_set_word(key->rsa->e, (u_long) buffer_get_char(&b))) {
243 buffer_get_bignum_bits(&b, key->rsa->d);
244 buffer_get_bignum_bits(&b, key->rsa->n);
245 buffer_get_bignum_bits(&b, key->rsa->iqmp);
246 buffer_get_bignum_bits(&b, key->rsa->q);
247 buffer_get_bignum_bits(&b, key->rsa->p);
248 generate_additional_parameters(key->rsa);
251 rlen = buffer_len(&b);
253 error("do_convert_private_ssh2_from_blob: "
254 "remaining bytes in key blob %d", rlen);
259 u_char *sig, data[10] = "abcde12345";
261 key_sign(key, &sig, &slen, data, sizeof data);
262 key_verify(key, sig, slen, data, sizeof data);
270 do_convert_from_ssh2(struct passwd *pw)
278 int escaped = 0, private = 0, ok;
282 ask_filename(pw, "Enter file in which the key is");
283 if (stat(identity_file, &st) < 0) {
284 perror(identity_file);
287 fp = fopen(identity_file, "r");
289 perror(identity_file);
293 while (fgets(line, sizeof(line), fp)) {
294 if (!(p = strchr(line, '\n'))) {
295 fprintf(stderr, "input line too long.\n");
298 if (p > line && p[-1] == '\\')
300 if (strncmp(line, "----", 4) == 0 ||
301 strstr(line, ": ") != NULL) {
302 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
304 /* fprintf(stderr, "ignore: %s", line); */
309 /* fprintf(stderr, "escaped: %s", line); */
313 strlcat(encoded, line, sizeof(encoded));
315 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
317 fprintf(stderr, "uudecode failed.\n");
321 do_convert_private_ssh2_from_blob(blob, blen) :
322 key_from_blob(blob, blen);
324 fprintf(stderr, "decode blob failed.\n");
328 (k->type == KEY_DSA ?
329 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
330 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
331 key_write(k, stdout);
333 fprintf(stderr, "key write failed");
337 fprintf(stdout, "\n");
343 do_print_public(struct passwd *pw)
349 ask_filename(pw, "Enter file in which the key is");
350 if (stat(identity_file, &st) < 0) {
351 perror(identity_file);
354 prv = load_identity(identity_file);
356 fprintf(stderr, "load failed\n");
359 if (!key_write(prv, stdout))
360 fprintf(stderr, "key_write failed");
362 fprintf(stdout, "\n");
367 do_fingerprint(struct passwd *pw)
371 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
372 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
375 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
376 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
379 ask_filename(pw, "Enter file in which the key is");
380 if (stat(identity_file, &st) < 0) {
381 perror(identity_file);
384 public = key_load_public(identity_file, &comment);
385 if (public != NULL) {
386 fp = key_fingerprint(public, fptype, rep);
387 printf("%d %s %s\n", key_size(public), fp, comment);
396 f = fopen(identity_file, "r");
398 while (fgets(line, sizeof(line), f)) {
399 i = strlen(line) - 1;
400 if (line[i] != '\n') {
401 error("line %d too long: %.40s...", num, line);
412 /* Skip leading whitespace, empty and comment lines. */
413 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
415 if (!*cp || *cp == '\n' || *cp == '#')
417 i = strtol(cp, &ep, 10);
418 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
421 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
422 if (*cp == '\\' && cp[1] == '"')
423 cp++; /* Skip both */
432 public = key_new(KEY_RSA1);
433 if (key_read(public, &cp) != 1) {
436 public = key_new(KEY_UNSPEC);
437 if (key_read(public, &cp) != 1) {
442 comment = *cp ? cp : comment;
443 fp = key_fingerprint(public, fptype, rep);
444 printf("%d %s %s\n", key_size(public), fp,
445 comment ? comment : "no comment");
453 printf("%s is not a valid key file.\n", identity_file);
460 * Perform changing a passphrase. The argument is the passwd structure
461 * for the current user.
464 do_change_passphrase(struct passwd *pw)
467 char *old_passphrase, *passphrase1, *passphrase2;
472 ask_filename(pw, "Enter file in which the key is");
473 if (stat(identity_file, &st) < 0) {
474 perror(identity_file);
477 /* Try to load the file with empty passphrase. */
478 private = key_load_private(identity_file, "", &comment);
479 if (private == NULL) {
480 if (identity_passphrase)
481 old_passphrase = xstrdup(identity_passphrase);
483 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
484 private = key_load_private(identity_file, old_passphrase , &comment);
485 memset(old_passphrase, 0, strlen(old_passphrase));
486 xfree(old_passphrase);
487 if (private == NULL) {
488 printf("Bad passphrase.\n");
492 printf("Key has comment '%s'\n", comment);
494 /* Ask the new passphrase (twice). */
495 if (identity_new_passphrase) {
496 passphrase1 = xstrdup(identity_new_passphrase);
500 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
501 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
503 /* Verify that they are the same. */
504 if (strcmp(passphrase1, passphrase2) != 0) {
505 memset(passphrase1, 0, strlen(passphrase1));
506 memset(passphrase2, 0, strlen(passphrase2));
509 printf("Pass phrases do not match. Try again.\n");
512 /* Destroy the other copy. */
513 memset(passphrase2, 0, strlen(passphrase2));
517 /* Save the file using the new passphrase. */
518 if (!key_save_private(private, identity_file, passphrase1, comment)) {
519 printf("Saving the key failed: %s.\n", identity_file);
520 memset(passphrase1, 0, strlen(passphrase1));
526 /* Destroy the passphrase and the copy of the key in memory. */
527 memset(passphrase1, 0, strlen(passphrase1));
529 key_free(private); /* Destroys contents */
532 printf("Your identification has been saved with the new passphrase.\n");
537 * Change the comment of a private key file.
540 do_change_comment(struct passwd *pw)
542 char new_comment[1024], *comment, *passphrase;
550 ask_filename(pw, "Enter file in which the key is");
551 if (stat(identity_file, &st) < 0) {
552 perror(identity_file);
555 private = key_load_private(identity_file, "", &comment);
556 if (private == NULL) {
557 if (identity_passphrase)
558 passphrase = xstrdup(identity_passphrase);
559 else if (identity_new_passphrase)
560 passphrase = xstrdup(identity_new_passphrase);
562 passphrase = read_passphrase("Enter passphrase: ", 1);
563 /* Try to load using the passphrase. */
564 private = key_load_private(identity_file, passphrase, &comment);
565 if (private == NULL) {
566 memset(passphrase, 0, strlen(passphrase));
568 printf("Bad passphrase.\n");
572 passphrase = xstrdup("");
574 if (private->type != KEY_RSA1) {
575 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
579 printf("Key now has comment '%s'\n", comment);
581 if (identity_comment) {
582 strlcpy(new_comment, identity_comment, sizeof(new_comment));
584 printf("Enter new comment: ");
586 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
587 memset(passphrase, 0, strlen(passphrase));
591 if (strchr(new_comment, '\n'))
592 *strchr(new_comment, '\n') = 0;
595 /* Save the file using the new passphrase. */
596 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
597 printf("Saving the key failed: %s.\n", identity_file);
598 memset(passphrase, 0, strlen(passphrase));
604 memset(passphrase, 0, strlen(passphrase));
606 public = key_from_private(private);
609 strlcat(identity_file, ".pub", sizeof(identity_file));
610 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
612 printf("Could not save your public key in %s\n", identity_file);
617 printf("fdopen %s failed", identity_file);
620 if (!key_write(public, f))
621 fprintf(stderr, "write key failed");
623 fprintf(f, " %s\n", new_comment);
628 printf("The comment in your key file has been changed.\n");
635 printf("Usage: %s [-ceilpqyB] [-t type] [-b bits] [-f file] [-C comment] "
636 "[-N new-pass] [-P pass]\n", __progname);
641 * Main program for key management.
644 main(int ac, char **av)
646 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
647 Key *private, *public;
656 __progname = get_progname(av[0]);
660 SSLeay_add_all_algorithms();
662 /* we need this for the home * directory. */
663 pw = getpwuid(getuid());
665 printf("You don't exist, go away!\n");
668 if (gethostname(hostname, sizeof(hostname)) < 0) {
669 perror("gethostname");
673 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:P:N:C:")) != -1) {
677 if (bits < 512 || bits > 32768) {
678 printf("Bits has bad value.\n");
684 print_fingerprint = 1;
688 print_bubblebabble = 1;
692 change_passphrase = 1;
700 strlcpy(identity_file, optarg, sizeof(identity_file));
705 identity_passphrase = optarg;
709 identity_new_passphrase = optarg;
713 identity_comment = optarg;
734 convert_from_ssh2 = 1;
742 key_type_name = "dsa";
746 key_type_name = optarg;
755 printf("Too many arguments.\n");
758 if (change_passphrase && change_comment) {
759 printf("Can only have one of -p and -c.\n");
762 if (print_fingerprint || print_bubblebabble)
764 if (change_passphrase)
765 do_change_passphrase(pw);
767 do_change_comment(pw);
769 do_convert_to_ssh2(pw);
770 if (convert_from_ssh2)
771 do_convert_from_ssh2(pw);
777 type = key_type_from_name(key_type_name);
778 if (type == KEY_UNSPEC) {
779 fprintf(stderr, "unknown key type %s\n", key_type_name);
783 printf("Generating public/private %s key pair.\n", key_type_name);
784 private = key_generate(type, bits);
785 if (private == NULL) {
786 fprintf(stderr, "key_generate failed");
789 public = key_from_private(private);
792 ask_filename(pw, "Enter file in which to save the key");
794 /* Create ~/.ssh directory if it doesn\'t already exist. */
795 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
796 if (strstr(identity_file, dotsshdir) != NULL &&
797 stat(dotsshdir, &st) < 0) {
798 if (mkdir(dotsshdir, 0700) < 0)
799 error("Could not create directory '%s'.", dotsshdir);
801 printf("Created directory '%s'.\n", dotsshdir);
803 /* If the file already exists, ask the user to confirm. */
804 if (stat(identity_file, &st) >= 0) {
806 printf("%s already exists.\n", identity_file);
807 printf("Overwrite (y/n)? ");
809 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
811 if (yesno[0] != 'y' && yesno[0] != 'Y')
814 /* Ask for a passphrase (twice). */
815 if (identity_passphrase)
816 passphrase1 = xstrdup(identity_passphrase);
817 else if (identity_new_passphrase)
818 passphrase1 = xstrdup(identity_new_passphrase);
822 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
823 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
824 if (strcmp(passphrase1, passphrase2) != 0) {
825 /* The passphrases do not match. Clear them and retry. */
826 memset(passphrase1, 0, strlen(passphrase1));
827 memset(passphrase2, 0, strlen(passphrase2));
830 printf("Passphrases do not match. Try again.\n");
831 goto passphrase_again;
833 /* Clear the other copy of the passphrase. */
834 memset(passphrase2, 0, strlen(passphrase2));
838 if (identity_comment) {
839 strlcpy(comment, identity_comment, sizeof(comment));
841 /* Create default commend field for the passphrase. */
842 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
845 /* Save the key with the given passphrase and comment. */
846 if (!key_save_private(private, identity_file, passphrase1, comment)) {
847 printf("Saving the key failed: %s.\n", identity_file);
848 memset(passphrase1, 0, strlen(passphrase1));
852 /* Clear the passphrase. */
853 memset(passphrase1, 0, strlen(passphrase1));
856 /* Clear the private key and the random number generator. */
861 printf("Your identification has been saved in %s.\n", identity_file);
863 strlcat(identity_file, ".pub", sizeof(identity_file));
864 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
866 printf("Could not save your public key in %s\n", identity_file);
871 printf("fdopen %s failed", identity_file);
874 if (!key_write(public, f))
875 fprintf(stderr, "write key failed");
876 fprintf(f, " %s\n", comment);
880 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
881 printf("Your public key has been saved in %s.\n",
883 printf("The key fingerprint is:\n");
884 printf("%s %s\n", fp, comment);