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.65 2001/06/24 05:35:33 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: ",
128 prv = key_load_private(filename, pass, NULL);
129 memset(pass, 0, strlen(pass));
135 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
136 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
137 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
138 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
141 do_convert_to_ssh2(struct passwd *pw)
149 ask_filename(pw, "Enter file in which the key is");
150 if (stat(identity_file, &st) < 0) {
151 perror(identity_file);
154 if ((k = key_load_public(identity_file, NULL)) == NULL) {
155 if ((k = load_identity(identity_file)) == NULL) {
156 fprintf(stderr, "load failed\n");
160 key_to_blob(k, &blob, &len);
161 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
163 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
164 key_size(k), key_type(k),
165 pw->pw_name, hostname);
166 dump_base64(stdout, blob, len);
167 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
174 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
176 int bits = buffer_get_int(b);
177 int bytes = (bits + 7) / 8;
179 if (buffer_len(b) < bytes)
180 fatal("buffer_get_bignum_bits: input buffer too small: "
181 "need %d have %d", bytes, buffer_len(b));
182 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
183 buffer_consume(b, bytes);
187 do_convert_private_ssh2_from_blob(char *blob, int blen)
192 u_char *sig, data[10] = "abcde12345";
193 int magic, rlen, ktype, i1, i2, i3, i4;
198 buffer_append(&b, blob, blen);
200 magic = buffer_get_int(&b);
201 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
202 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
206 i1 = buffer_get_int(&b);
207 type = buffer_get_string(&b, NULL);
208 cipher = buffer_get_string(&b, NULL);
209 i2 = buffer_get_int(&b);
210 i3 = buffer_get_int(&b);
211 i4 = buffer_get_int(&b);
212 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
213 if (strcmp(cipher, "none") != 0) {
214 error("unsupported cipher %s", cipher);
222 if (strstr(type, "dsa")) {
224 } else if (strstr(type, "rsa")) {
230 key = key_new_private(ktype);
235 buffer_get_bignum_bits(&b, key->dsa->p);
236 buffer_get_bignum_bits(&b, key->dsa->g);
237 buffer_get_bignum_bits(&b, key->dsa->q);
238 buffer_get_bignum_bits(&b, key->dsa->pub_key);
239 buffer_get_bignum_bits(&b, key->dsa->priv_key);
242 e = buffer_get_char(&b);
246 e += buffer_get_char(&b);
249 e += buffer_get_char(&b);
252 if (!BN_set_word(key->rsa->e, e)) {
257 buffer_get_bignum_bits(&b, key->rsa->d);
258 buffer_get_bignum_bits(&b, key->rsa->n);
259 buffer_get_bignum_bits(&b, key->rsa->iqmp);
260 buffer_get_bignum_bits(&b, key->rsa->q);
261 buffer_get_bignum_bits(&b, key->rsa->p);
262 generate_additional_parameters(key->rsa);
265 rlen = buffer_len(&b);
267 error("do_convert_private_ssh2_from_blob: "
268 "remaining bytes in key blob %d", rlen);
272 key_sign(key, &sig, &slen, data, sizeof(data));
273 key_verify(key, sig, slen, data, sizeof(data));
279 do_convert_from_ssh2(struct passwd *pw)
287 int escaped = 0, private = 0, ok;
291 ask_filename(pw, "Enter file in which the key is");
292 if (stat(identity_file, &st) < 0) {
293 perror(identity_file);
296 fp = fopen(identity_file, "r");
298 perror(identity_file);
302 while (fgets(line, sizeof(line), fp)) {
303 if (!(p = strchr(line, '\n'))) {
304 fprintf(stderr, "input line too long.\n");
307 if (p > line && p[-1] == '\\')
309 if (strncmp(line, "----", 4) == 0 ||
310 strstr(line, ": ") != NULL) {
311 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
313 if (strstr(line, " END ") != NULL) {
316 /* fprintf(stderr, "ignore: %s", line); */
321 /* fprintf(stderr, "escaped: %s", line); */
325 strlcat(encoded, line, sizeof(encoded));
327 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
329 fprintf(stderr, "uudecode failed.\n");
333 do_convert_private_ssh2_from_blob(blob, blen) :
334 key_from_blob(blob, blen);
336 fprintf(stderr, "decode blob failed.\n");
340 (k->type == KEY_DSA ?
341 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
342 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
343 key_write(k, stdout);
345 fprintf(stderr, "key write failed");
349 fprintf(stdout, "\n");
355 do_print_public(struct passwd *pw)
361 ask_filename(pw, "Enter file in which the key is");
362 if (stat(identity_file, &st) < 0) {
363 perror(identity_file);
366 prv = load_identity(identity_file);
368 fprintf(stderr, "load failed\n");
371 if (!key_write(prv, stdout))
372 fprintf(stderr, "key_write failed");
374 fprintf(stdout, "\n");
379 do_fingerprint(struct passwd *pw)
383 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
384 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
387 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
388 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
391 ask_filename(pw, "Enter file in which the key is");
392 if (stat(identity_file, &st) < 0) {
393 perror(identity_file);
396 public = key_load_public(identity_file, &comment);
397 if (public != NULL) {
398 fp = key_fingerprint(public, fptype, rep);
399 printf("%d %s %s\n", key_size(public), fp, comment);
408 f = fopen(identity_file, "r");
410 while (fgets(line, sizeof(line), f)) {
411 i = strlen(line) - 1;
412 if (line[i] != '\n') {
413 error("line %d too long: %.40s...", num, line);
424 /* Skip leading whitespace, empty and comment lines. */
425 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
427 if (!*cp || *cp == '\n' || *cp == '#')
429 i = strtol(cp, &ep, 10);
430 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
433 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
434 if (*cp == '\\' && cp[1] == '"')
435 cp++; /* Skip both */
444 public = key_new(KEY_RSA1);
445 if (key_read(public, &cp) != 1) {
448 public = key_new(KEY_UNSPEC);
449 if (key_read(public, &cp) != 1) {
454 comment = *cp ? cp : comment;
455 fp = key_fingerprint(public, fptype, rep);
456 printf("%d %s %s\n", key_size(public), fp,
457 comment ? comment : "no comment");
465 printf("%s is not a valid key file.\n", identity_file);
472 * Perform changing a passphrase. The argument is the passwd structure
473 * for the current user.
476 do_change_passphrase(struct passwd *pw)
479 char *old_passphrase, *passphrase1, *passphrase2;
484 ask_filename(pw, "Enter file in which the key is");
485 if (stat(identity_file, &st) < 0) {
486 perror(identity_file);
489 /* Try to load the file with empty passphrase. */
490 private = key_load_private(identity_file, "", &comment);
491 if (private == NULL) {
492 if (identity_passphrase)
493 old_passphrase = xstrdup(identity_passphrase);
496 read_passphrase("Enter old passphrase: ",
498 private = key_load_private(identity_file, old_passphrase,
500 memset(old_passphrase, 0, strlen(old_passphrase));
501 xfree(old_passphrase);
502 if (private == NULL) {
503 printf("Bad passphrase.\n");
507 printf("Key has comment '%s'\n", comment);
509 /* Ask the new passphrase (twice). */
510 if (identity_new_passphrase) {
511 passphrase1 = xstrdup(identity_new_passphrase);
515 read_passphrase("Enter new passphrase (empty for no "
516 "passphrase): ", RP_ALLOW_STDIN);
517 passphrase2 = read_passphrase("Enter same passphrase again: ",
520 /* Verify that they are the same. */
521 if (strcmp(passphrase1, passphrase2) != 0) {
522 memset(passphrase1, 0, strlen(passphrase1));
523 memset(passphrase2, 0, strlen(passphrase2));
526 printf("Pass phrases do not match. Try again.\n");
529 /* Destroy the other copy. */
530 memset(passphrase2, 0, strlen(passphrase2));
534 /* Save the file using the new passphrase. */
535 if (!key_save_private(private, identity_file, passphrase1, comment)) {
536 printf("Saving the key failed: %s.\n", identity_file);
537 memset(passphrase1, 0, strlen(passphrase1));
543 /* Destroy the passphrase and the copy of the key in memory. */
544 memset(passphrase1, 0, strlen(passphrase1));
546 key_free(private); /* Destroys contents */
549 printf("Your identification has been saved with the new passphrase.\n");
554 * Change the comment of a private key file.
557 do_change_comment(struct passwd *pw)
559 char new_comment[1024], *comment, *passphrase;
567 ask_filename(pw, "Enter file in which the key is");
568 if (stat(identity_file, &st) < 0) {
569 perror(identity_file);
572 private = key_load_private(identity_file, "", &comment);
573 if (private == NULL) {
574 if (identity_passphrase)
575 passphrase = xstrdup(identity_passphrase);
576 else if (identity_new_passphrase)
577 passphrase = xstrdup(identity_new_passphrase);
579 passphrase = read_passphrase("Enter passphrase: ",
581 /* Try to load using the passphrase. */
582 private = key_load_private(identity_file, passphrase, &comment);
583 if (private == NULL) {
584 memset(passphrase, 0, strlen(passphrase));
586 printf("Bad passphrase.\n");
590 passphrase = xstrdup("");
592 if (private->type != KEY_RSA1) {
593 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
597 printf("Key now has comment '%s'\n", comment);
599 if (identity_comment) {
600 strlcpy(new_comment, identity_comment, sizeof(new_comment));
602 printf("Enter new comment: ");
604 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
605 memset(passphrase, 0, strlen(passphrase));
609 if (strchr(new_comment, '\n'))
610 *strchr(new_comment, '\n') = 0;
613 /* Save the file using the new passphrase. */
614 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
615 printf("Saving the key failed: %s.\n", identity_file);
616 memset(passphrase, 0, strlen(passphrase));
622 memset(passphrase, 0, strlen(passphrase));
624 public = key_from_private(private);
627 strlcat(identity_file, ".pub", sizeof(identity_file));
628 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
630 printf("Could not save your public key in %s\n", identity_file);
635 printf("fdopen %s failed", identity_file);
638 if (!key_write(public, f))
639 fprintf(stderr, "write key failed");
641 fprintf(f, " %s\n", new_comment);
646 printf("The comment in your key file has been changed.\n");
653 printf("Usage: %s [-ceilpqyB] [-t type] [-b bits] [-f file] [-C comment] "
654 "[-N new-pass] [-P pass]\n", __progname);
659 * Main program for key management.
662 main(int ac, char **av)
664 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
665 Key *private, *public;
674 __progname = get_progname(av[0]);
678 SSLeay_add_all_algorithms();
680 /* we need this for the home * directory. */
681 pw = getpwuid(getuid());
683 printf("You don't exist, go away!\n");
686 if (gethostname(hostname, sizeof(hostname)) < 0) {
687 perror("gethostname");
691 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:P:N:C:")) != -1) {
695 if (bits < 512 || bits > 32768) {
696 printf("Bits has bad value.\n");
702 print_fingerprint = 1;
706 print_bubblebabble = 1;
710 change_passphrase = 1;
718 strlcpy(identity_file, optarg, sizeof(identity_file));
723 identity_passphrase = optarg;
727 identity_new_passphrase = optarg;
731 identity_comment = optarg;
752 convert_from_ssh2 = 1;
760 key_type_name = "dsa";
764 key_type_name = optarg;
773 printf("Too many arguments.\n");
776 if (change_passphrase && change_comment) {
777 printf("Can only have one of -p and -c.\n");
780 if (print_fingerprint || print_bubblebabble)
782 if (change_passphrase)
783 do_change_passphrase(pw);
785 do_change_comment(pw);
787 do_convert_to_ssh2(pw);
788 if (convert_from_ssh2)
789 do_convert_from_ssh2(pw);
795 type = key_type_from_name(key_type_name);
796 if (type == KEY_UNSPEC) {
797 fprintf(stderr, "unknown key type %s\n", key_type_name);
801 printf("Generating public/private %s key pair.\n", key_type_name);
802 private = key_generate(type, bits);
803 if (private == NULL) {
804 fprintf(stderr, "key_generate failed");
807 public = key_from_private(private);
810 ask_filename(pw, "Enter file in which to save the key");
812 /* Create ~/.ssh directory if it doesn\'t already exist. */
813 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
814 if (strstr(identity_file, dotsshdir) != NULL &&
815 stat(dotsshdir, &st) < 0) {
816 if (mkdir(dotsshdir, 0700) < 0)
817 error("Could not create directory '%s'.", dotsshdir);
819 printf("Created directory '%s'.\n", dotsshdir);
821 /* If the file already exists, ask the user to confirm. */
822 if (stat(identity_file, &st) >= 0) {
824 printf("%s already exists.\n", identity_file);
825 printf("Overwrite (y/n)? ");
827 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
829 if (yesno[0] != 'y' && yesno[0] != 'Y')
832 /* Ask for a passphrase (twice). */
833 if (identity_passphrase)
834 passphrase1 = xstrdup(identity_passphrase);
835 else if (identity_new_passphrase)
836 passphrase1 = xstrdup(identity_new_passphrase);
840 read_passphrase("Enter passphrase (empty for no "
841 "passphrase): ", RP_ALLOW_STDIN);
842 passphrase2 = read_passphrase("Enter same passphrase again: ",
844 if (strcmp(passphrase1, passphrase2) != 0) {
846 * The passphrases do not match. Clear them and
849 memset(passphrase1, 0, strlen(passphrase1));
850 memset(passphrase2, 0, strlen(passphrase2));
853 printf("Passphrases do not match. Try again.\n");
854 goto passphrase_again;
856 /* Clear the other copy of the passphrase. */
857 memset(passphrase2, 0, strlen(passphrase2));
861 if (identity_comment) {
862 strlcpy(comment, identity_comment, sizeof(comment));
864 /* Create default commend field for the passphrase. */
865 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
868 /* Save the key with the given passphrase and comment. */
869 if (!key_save_private(private, identity_file, passphrase1, comment)) {
870 printf("Saving the key failed: %s.\n", identity_file);
871 memset(passphrase1, 0, strlen(passphrase1));
875 /* Clear the passphrase. */
876 memset(passphrase1, 0, strlen(passphrase1));
879 /* Clear the private key and the random number generator. */
884 printf("Your identification has been saved in %s.\n", identity_file);
886 strlcat(identity_file, ".pub", sizeof(identity_file));
887 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
889 printf("Could not save your public key in %s\n", identity_file);
894 printf("fdopen %s failed", identity_file);
897 if (!key_write(public, f))
898 fprintf(stderr, "write key failed");
899 fprintf(f, " %s\n", comment);
903 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
904 printf("Your public key has been saved in %s.\n",
906 printf("The key fingerprint is:\n");
907 printf("%s %s\n", fp, comment);