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.59 2001/04/23 21:57:07 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 try_load_pem_key(char *filename)
121 prv = key_load_private(filename, "", NULL);
123 pass = read_passphrase("Enter passphrase: ", 1);
124 prv = key_load_private(filename, pass, NULL);
125 memset(pass, 0, strlen(pass));
131 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
132 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
133 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
134 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
137 do_convert_to_ssh2(struct passwd *pw)
145 ask_filename(pw, "Enter file in which the key is");
146 if (stat(identity_file, &st) < 0) {
147 perror(identity_file);
150 if ((k = key_load_public(identity_file, NULL)) == NULL) {
151 if ((k = try_load_pem_key(identity_file)) == NULL) {
152 fprintf(stderr, "load failed\n");
156 key_to_blob(k, &blob, &len);
157 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
159 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
160 key_size(k), key_type(k),
161 pw->pw_name, hostname);
162 dump_base64(stdout, blob, len);
163 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
170 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
172 int bits = buffer_get_int(b);
173 int bytes = (bits + 7) / 8;
175 if (buffer_len(b) < bytes)
176 fatal("buffer_get_bignum_bits: input buffer too small: "
177 "need %d have %d", bytes, buffer_len(b));
178 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
179 buffer_consume(b, bytes);
183 do_convert_private_ssh2_from_blob(char *blob, int blen)
187 int ignore, magic, rlen, ktype;
191 buffer_append(&b, blob, blen);
193 magic = buffer_get_int(&b);
194 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
195 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
199 ignore = buffer_get_int(&b);
200 type = buffer_get_string(&b, NULL);
201 cipher = buffer_get_string(&b, NULL);
202 ignore = buffer_get_int(&b);
203 ignore = buffer_get_int(&b);
204 ignore = buffer_get_int(&b);
206 if (strcmp(cipher, "none") != 0) {
207 error("unsupported cipher %s", cipher);
215 if (strstr(type, "dsa")) {
217 } else if (strstr(type, "rsa")) {
223 key = key_new_private(ktype);
228 buffer_get_bignum_bits(&b, key->dsa->p);
229 buffer_get_bignum_bits(&b, key->dsa->g);
230 buffer_get_bignum_bits(&b, key->dsa->q);
231 buffer_get_bignum_bits(&b, key->dsa->pub_key);
232 buffer_get_bignum_bits(&b, key->dsa->priv_key);
235 if (!BN_set_word(key->rsa->e, (u_long) buffer_get_char(&b))) {
240 buffer_get_bignum_bits(&b, key->rsa->d);
241 buffer_get_bignum_bits(&b, key->rsa->n);
242 buffer_get_bignum_bits(&b, key->rsa->iqmp);
243 buffer_get_bignum_bits(&b, key->rsa->q);
244 buffer_get_bignum_bits(&b, key->rsa->p);
245 generate_additional_parameters(key->rsa);
248 rlen = buffer_len(&b);
250 error("do_convert_private_ssh2_from_blob: "
251 "remaining bytes in key blob %d", rlen);
256 u_char *sig, data[10] = "abcde12345";
258 key_sign(key, &sig, &slen, data, sizeof data);
259 key_verify(key, sig, slen, data, sizeof data);
267 do_convert_from_ssh2(struct passwd *pw)
275 int escaped = 0, private = 0, ok;
279 ask_filename(pw, "Enter file in which the key is");
280 if (stat(identity_file, &st) < 0) {
281 perror(identity_file);
284 fp = fopen(identity_file, "r");
286 perror(identity_file);
290 while (fgets(line, sizeof(line), fp)) {
291 if (!(p = strchr(line, '\n'))) {
292 fprintf(stderr, "input line too long.\n");
295 if (p > line && p[-1] == '\\')
297 if (strncmp(line, "----", 4) == 0 ||
298 strstr(line, ": ") != NULL) {
299 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
301 fprintf(stderr, "ignore: %s", line);
306 fprintf(stderr, "escaped: %s", line);
310 strlcat(encoded, line, sizeof(encoded));
312 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
314 fprintf(stderr, "uudecode failed.\n");
318 do_convert_private_ssh2_from_blob(blob, blen) :
319 key_from_blob(blob, blen);
321 fprintf(stderr, "decode blob failed.\n");
325 (k->type == KEY_DSA ?
326 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
327 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
328 key_write(k, stdout);
330 fprintf(stderr, "key write failed");
334 fprintf(stdout, "\n");
340 do_print_public(struct passwd *pw)
346 ask_filename(pw, "Enter file in which the key is");
347 if (stat(identity_file, &st) < 0) {
348 perror(identity_file);
351 prv = try_load_pem_key(identity_file);
353 fprintf(stderr, "load failed\n");
356 if (!key_write(prv, stdout))
357 fprintf(stderr, "key_write failed");
359 fprintf(stdout, "\n");
364 do_fingerprint(struct passwd *pw)
368 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
369 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
372 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
373 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
376 ask_filename(pw, "Enter file in which the key is");
377 if (stat(identity_file, &st) < 0) {
378 perror(identity_file);
381 public = key_load_public(identity_file, &comment);
382 if (public != NULL) {
383 fp = key_fingerprint(public, fptype, rep);
384 printf("%d %s %s\n", key_size(public), fp, comment);
393 f = fopen(identity_file, "r");
395 while (fgets(line, sizeof(line), f)) {
396 i = strlen(line) - 1;
397 if (line[i] != '\n') {
398 error("line %d too long: %.40s...", num, line);
409 /* Skip leading whitespace, empty and comment lines. */
410 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
412 if (!*cp || *cp == '\n' || *cp == '#')
414 i = strtol(cp, &ep, 10);
415 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
418 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
419 if (*cp == '\\' && cp[1] == '"')
420 cp++; /* Skip both */
429 public = key_new(KEY_RSA1);
430 if (key_read(public, &cp) != 1) {
433 public = key_new(KEY_UNSPEC);
434 if (key_read(public, &cp) != 1) {
439 comment = *cp ? cp : comment;
440 fp = key_fingerprint(public, fptype, rep);
441 printf("%d %s %s\n", key_size(public), fp,
442 comment ? comment : "no comment");
450 printf("%s is not a valid key file.\n", identity_file);
457 * Perform changing a passphrase. The argument is the passwd structure
458 * for the current user.
461 do_change_passphrase(struct passwd *pw)
464 char *old_passphrase, *passphrase1, *passphrase2;
469 ask_filename(pw, "Enter file in which the key is");
470 if (stat(identity_file, &st) < 0) {
471 perror(identity_file);
474 /* Try to load the file with empty passphrase. */
475 private = key_load_private(identity_file, "", &comment);
476 if (private == NULL) {
477 if (identity_passphrase)
478 old_passphrase = xstrdup(identity_passphrase);
480 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
481 private = key_load_private(identity_file, old_passphrase , &comment);
482 memset(old_passphrase, 0, strlen(old_passphrase));
483 xfree(old_passphrase);
484 if (private == NULL) {
485 printf("Bad passphrase.\n");
489 printf("Key has comment '%s'\n", comment);
491 /* Ask the new passphrase (twice). */
492 if (identity_new_passphrase) {
493 passphrase1 = xstrdup(identity_new_passphrase);
497 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
498 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
500 /* Verify that they are the same. */
501 if (strcmp(passphrase1, passphrase2) != 0) {
502 memset(passphrase1, 0, strlen(passphrase1));
503 memset(passphrase2, 0, strlen(passphrase2));
506 printf("Pass phrases do not match. Try again.\n");
509 /* Destroy the other copy. */
510 memset(passphrase2, 0, strlen(passphrase2));
514 /* Save the file using the new passphrase. */
515 if (!key_save_private(private, identity_file, passphrase1, comment)) {
516 printf("Saving the key failed: %s.\n", identity_file);
517 memset(passphrase1, 0, strlen(passphrase1));
523 /* Destroy the passphrase and the copy of the key in memory. */
524 memset(passphrase1, 0, strlen(passphrase1));
526 key_free(private); /* Destroys contents */
529 printf("Your identification has been saved with the new passphrase.\n");
534 * Change the comment of a private key file.
537 do_change_comment(struct passwd *pw)
539 char new_comment[1024], *comment, *passphrase;
547 ask_filename(pw, "Enter file in which the key is");
548 if (stat(identity_file, &st) < 0) {
549 perror(identity_file);
552 private = key_load_private(identity_file, "", &comment);
553 if (private == NULL) {
554 if (identity_passphrase)
555 passphrase = xstrdup(identity_passphrase);
556 else if (identity_new_passphrase)
557 passphrase = xstrdup(identity_new_passphrase);
559 passphrase = read_passphrase("Enter passphrase: ", 1);
560 /* Try to load using the passphrase. */
561 private = key_load_private(identity_file, passphrase, &comment);
562 if (private == NULL) {
563 memset(passphrase, 0, strlen(passphrase));
565 printf("Bad passphrase.\n");
569 passphrase = xstrdup("");
571 if (private->type != KEY_RSA1) {
572 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
576 printf("Key now has comment '%s'\n", comment);
578 if (identity_comment) {
579 strlcpy(new_comment, identity_comment, sizeof(new_comment));
581 printf("Enter new comment: ");
583 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
584 memset(passphrase, 0, strlen(passphrase));
588 if (strchr(new_comment, '\n'))
589 *strchr(new_comment, '\n') = 0;
592 /* Save the file using the new passphrase. */
593 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
594 printf("Saving the key failed: %s.\n", identity_file);
595 memset(passphrase, 0, strlen(passphrase));
601 memset(passphrase, 0, strlen(passphrase));
603 public = key_from_private(private);
606 strlcat(identity_file, ".pub", sizeof(identity_file));
607 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
609 printf("Could not save your public key in %s\n", identity_file);
614 printf("fdopen %s failed", identity_file);
617 if (!key_write(public, f))
618 fprintf(stderr, "write key failed");
620 fprintf(f, " %s\n", new_comment);
625 printf("The comment in your key file has been changed.\n");
632 printf("Usage: %s [-ceilpqyB] [-t type] [-b bits] [-f file] [-C comment] "
633 "[-N new-pass] [-P pass]\n", __progname);
638 * Main program for key management.
641 main(int ac, char **av)
643 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
644 Key *private, *public;
653 __progname = get_progname(av[0]);
657 SSLeay_add_all_algorithms();
659 /* we need this for the home * directory. */
660 pw = getpwuid(getuid());
662 printf("You don't exist, go away!\n");
665 if (gethostname(hostname, sizeof(hostname)) < 0) {
666 perror("gethostname");
670 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:P:N:C:")) != -1) {
674 if (bits < 512 || bits > 32768) {
675 printf("Bits has bad value.\n");
681 print_fingerprint = 1;
685 print_bubblebabble = 1;
689 change_passphrase = 1;
697 strlcpy(identity_file, optarg, sizeof(identity_file));
702 identity_passphrase = optarg;
706 identity_new_passphrase = optarg;
710 identity_comment = optarg;
731 convert_from_ssh2 = 1;
739 key_type_name = "dsa";
743 key_type_name = optarg;
752 printf("Too many arguments.\n");
755 if (change_passphrase && change_comment) {
756 printf("Can only have one of -p and -c.\n");
759 if (print_fingerprint || print_bubblebabble)
761 if (change_passphrase)
762 do_change_passphrase(pw);
764 do_change_comment(pw);
766 do_convert_to_ssh2(pw);
767 if (convert_from_ssh2)
768 do_convert_from_ssh2(pw);
774 type = key_type_from_name(key_type_name);
775 if (type == KEY_UNSPEC) {
776 fprintf(stderr, "unknown key type %s\n", key_type_name);
780 printf("Generating public/private %s key pair.\n", key_type_name);
781 private = key_generate(type, bits);
782 if (private == NULL) {
783 fprintf(stderr, "key_generate failed");
786 public = key_from_private(private);
789 ask_filename(pw, "Enter file in which to save the key");
791 /* Create ~/.ssh directory if it doesn\'t already exist. */
792 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
793 if (strstr(identity_file, dotsshdir) != NULL &&
794 stat(dotsshdir, &st) < 0) {
795 if (mkdir(dotsshdir, 0700) < 0)
796 error("Could not create directory '%s'.", dotsshdir);
798 printf("Created directory '%s'.\n", dotsshdir);
800 /* If the file already exists, ask the user to confirm. */
801 if (stat(identity_file, &st) >= 0) {
803 printf("%s already exists.\n", identity_file);
804 printf("Overwrite (y/n)? ");
806 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
808 if (yesno[0] != 'y' && yesno[0] != 'Y')
811 /* Ask for a passphrase (twice). */
812 if (identity_passphrase)
813 passphrase1 = xstrdup(identity_passphrase);
814 else if (identity_new_passphrase)
815 passphrase1 = xstrdup(identity_new_passphrase);
819 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
820 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
821 if (strcmp(passphrase1, passphrase2) != 0) {
822 /* The passphrases do not match. Clear them and retry. */
823 memset(passphrase1, 0, strlen(passphrase1));
824 memset(passphrase2, 0, strlen(passphrase2));
827 printf("Passphrases do not match. Try again.\n");
828 goto passphrase_again;
830 /* Clear the other copy of the passphrase. */
831 memset(passphrase2, 0, strlen(passphrase2));
835 if (identity_comment) {
836 strlcpy(comment, identity_comment, sizeof(comment));
838 /* Create default commend field for the passphrase. */
839 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
842 /* Save the key with the given passphrase and comment. */
843 if (!key_save_private(private, identity_file, passphrase1, comment)) {
844 printf("Saving the key failed: %s.\n", identity_file);
845 memset(passphrase1, 0, strlen(passphrase1));
849 /* Clear the passphrase. */
850 memset(passphrase1, 0, strlen(passphrase1));
853 /* Clear the private key and the random number generator. */
858 printf("Your identification has been saved in %s.\n", identity_file);
860 strlcat(identity_file, ".pub", sizeof(identity_file));
861 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
863 printf("Could not save your public key in %s\n", identity_file);
868 printf("fdopen %s failed", identity_file);
871 if (!key_write(public, f))
872 fprintf(stderr, "write key failed");
873 fprintf(f, " %s\n", comment);
877 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
878 printf("Your public key has been saved in %s.\n",
880 printf("The key fingerprint is:\n");
881 printf("%s %s\n", fp, comment);