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.52 2001/03/26 08:07:09 markus Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
26 #include "pathnames.h"
30 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
34 * Flag indicating that we just want to change the passphrase. This can be
35 * set on the command line.
37 int change_passphrase = 0;
40 * Flag indicating that we just want to change the comment. This can be set
41 * on the command line.
43 int change_comment = 0;
47 /* Flag indicating that we just want to see the key fingerprint */
48 int print_fingerprint = 0;
49 int print_bubblebabble = 0;
51 /* The identity file name, given on the command line or entered by the user. */
52 char identity_file[1024];
53 int have_identity = 0;
55 /* This is set to the passphrase if given on the command line. */
56 char *identity_passphrase = NULL;
58 /* This is set to the new passphrase if given on the command line. */
59 char *identity_new_passphrase = NULL;
61 /* This is set to the new comment if given on the command line. */
62 char *identity_comment = NULL;
64 /* Dump public key file in format used by real and the original SSH 2 */
65 int convert_to_ssh2 = 0;
66 int convert_from_ssh2 = 0;
69 /* default to RSA for SSH-1 */
70 char *key_type_name = "rsa1";
73 #ifdef HAVE___PROGNAME
74 extern char *__progname;
79 char hostname[MAXHOSTNAMELEN];
82 ask_filename(struct passwd *pw, const char *prompt)
87 switch (key_type_from_name(key_type_name)) {
89 name = _PATH_SSH_CLIENT_IDENTITY;
92 name = _PATH_SSH_CLIENT_ID_DSA;
95 name = _PATH_SSH_CLIENT_ID_RSA;
98 fprintf(stderr, "bad key type");
102 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
103 fprintf(stderr, "%s (%s): ", prompt, identity_file);
105 if (fgets(buf, sizeof(buf), stdin) == NULL)
107 if (strchr(buf, '\n'))
108 *strchr(buf, '\n') = 0;
109 if (strcmp(buf, "") != 0)
110 strlcpy(identity_file, buf, sizeof(identity_file));
115 try_load_pem_key(char *filename)
120 prv = key_load_private(filename, "", NULL);
122 pass = read_passphrase("Enter passphrase: ", 1);
123 prv = key_load_private(filename, pass, 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 prv = try_load_pem_key(identity_file);
151 fprintf(stderr, "load failed\n");
154 key_to_blob(prv, &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(prv), key_type(prv),
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 prv = try_load_pem_key(identity_file);
317 fprintf(stderr, "load failed\n");
320 if (!key_write(prv, stdout))
321 fprintf(stderr, "key_write failed");
323 fprintf(stdout, "\n");
328 do_fingerprint(struct passwd *pw)
332 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
333 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
336 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
337 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
340 ask_filename(pw, "Enter file in which the key is");
341 if (stat(identity_file, &st) < 0) {
342 perror(identity_file);
345 public = key_load_public(identity_file, &comment);
346 if (public != NULL) {
347 fp = key_fingerprint(public, fptype, rep);
348 printf("%d %s %s\n", key_size(public), fp, comment);
357 f = fopen(identity_file, "r");
359 while (fgets(line, sizeof(line), f)) {
360 i = strlen(line) - 1;
361 if (line[i] != '\n') {
362 error("line %d too long: %.40s...", num, line);
373 /* Skip leading whitespace, empty and comment lines. */
374 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
376 if (!*cp || *cp == '\n' || *cp == '#')
378 i = strtol(cp, &ep, 10);
379 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
382 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
383 if (*cp == '\\' && cp[1] == '"')
384 cp++; /* Skip both */
393 public = key_new(KEY_RSA1);
394 if (key_read(public, &cp) != 1) {
397 public = key_new(KEY_UNSPEC);
398 if (key_read(public, &cp) != 1) {
403 comment = *cp ? cp : comment;
404 fp = key_fingerprint(public, fptype, rep);
405 printf("%d %s %s\n", key_size(public), fp,
406 comment ? comment : "no comment");
414 printf("%s is not a valid key file.\n", identity_file);
421 * Perform changing a passphrase. The argument is the passwd structure
422 * for the current user.
425 do_change_passphrase(struct passwd *pw)
428 char *old_passphrase, *passphrase1, *passphrase2;
433 ask_filename(pw, "Enter file in which the key is");
434 if (stat(identity_file, &st) < 0) {
435 perror(identity_file);
438 /* Try to load the file with empty passphrase. */
439 private = key_load_private(identity_file, "", &comment);
440 if (private == NULL) {
441 if (identity_passphrase)
442 old_passphrase = xstrdup(identity_passphrase);
444 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
445 private = key_load_private(identity_file, old_passphrase , &comment);
446 memset(old_passphrase, 0, strlen(old_passphrase));
447 xfree(old_passphrase);
448 if (private == NULL) {
449 printf("Bad passphrase.\n");
453 printf("Key has comment '%s'\n", comment);
455 /* Ask the new passphrase (twice). */
456 if (identity_new_passphrase) {
457 passphrase1 = xstrdup(identity_new_passphrase);
461 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
462 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
464 /* Verify that they are the same. */
465 if (strcmp(passphrase1, passphrase2) != 0) {
466 memset(passphrase1, 0, strlen(passphrase1));
467 memset(passphrase2, 0, strlen(passphrase2));
470 printf("Pass phrases do not match. Try again.\n");
473 /* Destroy the other copy. */
474 memset(passphrase2, 0, strlen(passphrase2));
478 /* Save the file using the new passphrase. */
479 if (!key_save_private(private, identity_file, passphrase1, comment)) {
480 printf("Saving the key failed: %s: %s.\n",
481 identity_file, strerror(errno));
482 memset(passphrase1, 0, strlen(passphrase1));
488 /* Destroy the passphrase and the copy of the key in memory. */
489 memset(passphrase1, 0, strlen(passphrase1));
491 key_free(private); /* Destroys contents */
494 printf("Your identification has been saved with the new passphrase.\n");
499 * Change the comment of a private key file.
502 do_change_comment(struct passwd *pw)
504 char new_comment[1024], *comment, *passphrase;
512 ask_filename(pw, "Enter file in which the key is");
513 if (stat(identity_file, &st) < 0) {
514 perror(identity_file);
517 private = key_load_private(identity_file, "", &comment);
518 if (private == NULL) {
519 if (identity_passphrase)
520 passphrase = xstrdup(identity_passphrase);
521 else if (identity_new_passphrase)
522 passphrase = xstrdup(identity_new_passphrase);
524 passphrase = read_passphrase("Enter passphrase: ", 1);
525 /* Try to load using the passphrase. */
526 private = key_load_private(identity_file, passphrase, &comment);
527 if (private == NULL) {
528 memset(passphrase, 0, strlen(passphrase));
530 printf("Bad passphrase.\n");
534 passphrase = xstrdup("");
536 if (private->type != KEY_RSA1) {
537 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
541 printf("Key now has comment '%s'\n", comment);
543 if (identity_comment) {
544 strlcpy(new_comment, identity_comment, sizeof(new_comment));
546 printf("Enter new comment: ");
548 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
549 memset(passphrase, 0, strlen(passphrase));
553 if (strchr(new_comment, '\n'))
554 *strchr(new_comment, '\n') = 0;
557 /* Save the file using the new passphrase. */
558 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
559 printf("Saving the key failed: %s: %s.\n",
560 identity_file, strerror(errno));
561 memset(passphrase, 0, strlen(passphrase));
567 memset(passphrase, 0, strlen(passphrase));
569 public = key_from_private(private);
572 strlcat(identity_file, ".pub", sizeof(identity_file));
573 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
575 printf("Could not save your public key in %s\n", identity_file);
580 printf("fdopen %s failed", identity_file);
583 if (!key_write(public, f))
584 fprintf(stderr, "write key failed");
586 fprintf(f, " %s\n", new_comment);
591 printf("The comment in your key file has been changed.\n");
598 printf("Usage: %s [-lBpqxXyc] [-t type] [-b bits] [-f file] [-C comment] "
599 "[-N new-pass] [-P pass]\n", __progname);
604 * Main program for key management.
607 main(int ac, char **av)
609 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
610 Key *private, *public;
619 __progname = get_progname(av[0]);
623 SSLeay_add_all_algorithms();
625 /* we need this for the home * directory. */
626 pw = getpwuid(getuid());
628 printf("You don't exist, go away!\n");
631 if (gethostname(hostname, sizeof(hostname)) < 0) {
632 perror("gethostname");
636 while ((opt = getopt(ac, av, "dqpclBRxXyb:f:t:P:N:C:")) != -1) {
640 if (bits < 512 || bits > 32768) {
641 printf("Bits has bad value.\n");
647 print_fingerprint = 1;
651 print_bubblebabble = 1;
655 change_passphrase = 1;
663 strlcpy(identity_file, optarg, sizeof(identity_file));
668 identity_passphrase = optarg;
672 identity_new_passphrase = optarg;
676 identity_comment = optarg;
693 convert_from_ssh2 = 1;
701 key_type_name = "dsa";
705 key_type_name = optarg;
714 printf("Too many arguments.\n");
717 if (change_passphrase && change_comment) {
718 printf("Can only have one of -p and -c.\n");
721 if (print_fingerprint || print_bubblebabble)
723 if (change_passphrase)
724 do_change_passphrase(pw);
726 do_change_comment(pw);
728 do_convert_to_ssh2(pw);
729 if (convert_from_ssh2)
730 do_convert_from_ssh2(pw);
736 type = key_type_from_name(key_type_name);
737 if (type == KEY_UNSPEC) {
738 fprintf(stderr, "unknown key type %s\n", key_type_name);
742 printf("Generating public/private %s key pair.\n", key_type_name);
743 private = key_generate(type, bits);
744 if (private == NULL) {
745 fprintf(stderr, "key_generate failed");
748 public = key_from_private(private);
751 ask_filename(pw, "Enter file in which to save the key");
753 /* Create ~/.ssh directory if it doesn\'t already exist. */
754 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
755 if (strstr(identity_file, dotsshdir) != NULL &&
756 stat(dotsshdir, &st) < 0) {
757 if (mkdir(dotsshdir, 0700) < 0)
758 error("Could not create directory '%s'.", dotsshdir);
760 printf("Created directory '%s'.\n", dotsshdir);
762 /* If the file already exists, ask the user to confirm. */
763 if (stat(identity_file, &st) >= 0) {
765 printf("%s already exists.\n", identity_file);
766 printf("Overwrite (y/n)? ");
768 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
770 if (yesno[0] != 'y' && yesno[0] != 'Y')
773 /* Ask for a passphrase (twice). */
774 if (identity_passphrase)
775 passphrase1 = xstrdup(identity_passphrase);
776 else if (identity_new_passphrase)
777 passphrase1 = xstrdup(identity_new_passphrase);
781 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
782 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
783 if (strcmp(passphrase1, passphrase2) != 0) {
784 /* The passphrases do not match. Clear them and retry. */
785 memset(passphrase1, 0, strlen(passphrase1));
786 memset(passphrase2, 0, strlen(passphrase2));
789 printf("Passphrases do not match. Try again.\n");
790 goto passphrase_again;
792 /* Clear the other copy of the passphrase. */
793 memset(passphrase2, 0, strlen(passphrase2));
797 if (identity_comment) {
798 strlcpy(comment, identity_comment, sizeof(comment));
800 /* Create default commend field for the passphrase. */
801 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
804 /* Save the key with the given passphrase and comment. */
805 if (!key_save_private(private, identity_file, passphrase1, comment)) {
806 printf("Saving the key failed: %s: %s.\n",
807 identity_file, strerror(errno));
808 memset(passphrase1, 0, strlen(passphrase1));
812 /* Clear the passphrase. */
813 memset(passphrase1, 0, strlen(passphrase1));
816 /* Clear the private key and the random number generator. */
821 printf("Your identification has been saved in %s.\n", identity_file);
823 strlcat(identity_file, ".pub", sizeof(identity_file));
824 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
826 printf("Could not save your public key in %s\n", identity_file);
831 printf("fdopen %s failed", identity_file);
834 if (!key_write(public, f))
835 fprintf(stderr, "write key failed");
836 fprintf(f, " %s\n", comment);
840 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
841 printf("Your public key has been saved in %s.\n",
843 printf("The key fingerprint is:\n");
844 printf("%s %s\n", fp, comment);