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.74 2001/08/01 23:33:09 markus Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
31 #include "pathnames.h"
36 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
40 * Flag indicating that we just want to change the passphrase. This can be
41 * set on the command line.
43 int change_passphrase = 0;
46 * Flag indicating that we just want to change the comment. This can be set
47 * on the command line.
49 int change_comment = 0;
53 /* Flag indicating that we just want to see the key fingerprint */
54 int print_fingerprint = 0;
55 int print_bubblebabble = 0;
57 /* The identity file name, given on the command line or entered by the user. */
58 char identity_file[1024];
59 int have_identity = 0;
61 /* This is set to the passphrase if given on the command line. */
62 char *identity_passphrase = NULL;
64 /* This is set to the new passphrase if given on the command line. */
65 char *identity_new_passphrase = NULL;
67 /* This is set to the new comment if given on the command line. */
68 char *identity_comment = NULL;
70 /* Dump public key file in format used by real and the original SSH 2 */
71 int convert_to_ssh2 = 0;
72 int convert_from_ssh2 = 0;
75 /* default to RSA for SSH-1 */
76 char *key_type_name = "rsa1";
79 #ifdef HAVE___PROGNAME
80 extern char *__progname;
85 char hostname[MAXHOSTNAMELEN];
88 ask_filename(struct passwd *pw, const char *prompt)
93 switch (key_type_from_name(key_type_name)) {
95 name = _PATH_SSH_CLIENT_IDENTITY;
98 name = _PATH_SSH_CLIENT_ID_DSA;
101 name = _PATH_SSH_CLIENT_ID_RSA;
104 fprintf(stderr, "bad key type");
108 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
109 fprintf(stderr, "%s (%s): ", prompt, identity_file);
111 if (fgets(buf, sizeof(buf), stdin) == NULL)
113 if (strchr(buf, '\n'))
114 *strchr(buf, '\n') = 0;
115 if (strcmp(buf, "") != 0)
116 strlcpy(identity_file, buf, sizeof(identity_file));
121 load_identity(char *filename)
126 prv = key_load_private(filename, "", NULL);
128 if (identity_passphrase)
129 pass = xstrdup(identity_passphrase);
131 pass = read_passphrase("Enter passphrase: ",
133 prv = key_load_private(filename, pass, NULL);
134 memset(pass, 0, strlen(pass));
140 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
141 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
142 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
143 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
146 do_convert_to_ssh2(struct passwd *pw)
154 ask_filename(pw, "Enter file in which the key is");
155 if (stat(identity_file, &st) < 0) {
156 perror(identity_file);
159 if ((k = key_load_public(identity_file, NULL)) == NULL) {
160 if ((k = load_identity(identity_file)) == NULL) {
161 fprintf(stderr, "load failed\n");
165 key_to_blob(k, &blob, &len);
166 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
168 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
169 key_size(k), key_type(k),
170 pw->pw_name, hostname);
171 dump_base64(stdout, blob, len);
172 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
179 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
181 int bits = buffer_get_int(b);
182 int bytes = (bits + 7) / 8;
184 if (buffer_len(b) < bytes)
185 fatal("buffer_get_bignum_bits: input buffer too small: "
186 "need %d have %d", bytes, buffer_len(b));
187 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
188 buffer_consume(b, bytes);
192 do_convert_private_ssh2_from_blob(char *blob, int blen)
197 u_char *sig, data[] = "abcde12345";
198 int magic, rlen, ktype, i1, i2, i3, i4;
203 buffer_append(&b, blob, blen);
205 magic = buffer_get_int(&b);
206 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
207 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
211 i1 = buffer_get_int(&b);
212 type = buffer_get_string(&b, NULL);
213 cipher = buffer_get_string(&b, NULL);
214 i2 = buffer_get_int(&b);
215 i3 = buffer_get_int(&b);
216 i4 = buffer_get_int(&b);
217 debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
218 if (strcmp(cipher, "none") != 0) {
219 error("unsupported cipher %s", cipher);
227 if (strstr(type, "dsa")) {
229 } else if (strstr(type, "rsa")) {
235 key = key_new_private(ktype);
240 buffer_get_bignum_bits(&b, key->dsa->p);
241 buffer_get_bignum_bits(&b, key->dsa->g);
242 buffer_get_bignum_bits(&b, key->dsa->q);
243 buffer_get_bignum_bits(&b, key->dsa->pub_key);
244 buffer_get_bignum_bits(&b, key->dsa->priv_key);
247 e = buffer_get_char(&b);
251 e += buffer_get_char(&b);
254 e += buffer_get_char(&b);
257 if (!BN_set_word(key->rsa->e, e)) {
262 buffer_get_bignum_bits(&b, key->rsa->d);
263 buffer_get_bignum_bits(&b, key->rsa->n);
264 buffer_get_bignum_bits(&b, key->rsa->iqmp);
265 buffer_get_bignum_bits(&b, key->rsa->q);
266 buffer_get_bignum_bits(&b, key->rsa->p);
267 rsa_generate_additional_parameters(key->rsa);
270 rlen = buffer_len(&b);
272 error("do_convert_private_ssh2_from_blob: "
273 "remaining bytes in key blob %d", rlen);
277 key_sign(key, &sig, &slen, data, sizeof(data));
278 key_verify(key, sig, slen, data, sizeof(data));
284 do_convert_from_ssh2(struct passwd *pw)
292 int escaped = 0, private = 0, ok;
296 ask_filename(pw, "Enter file in which the key is");
297 if (stat(identity_file, &st) < 0) {
298 perror(identity_file);
301 fp = fopen(identity_file, "r");
303 perror(identity_file);
307 while (fgets(line, sizeof(line), fp)) {
308 if (!(p = strchr(line, '\n'))) {
309 fprintf(stderr, "input line too long.\n");
312 if (p > line && p[-1] == '\\')
314 if (strncmp(line, "----", 4) == 0 ||
315 strstr(line, ": ") != NULL) {
316 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
318 if (strstr(line, " END ") != NULL) {
321 /* fprintf(stderr, "ignore: %s", line); */
326 /* fprintf(stderr, "escaped: %s", line); */
330 strlcat(encoded, line, sizeof(encoded));
332 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
334 fprintf(stderr, "uudecode failed.\n");
338 do_convert_private_ssh2_from_blob(blob, blen) :
339 key_from_blob(blob, blen);
341 fprintf(stderr, "decode blob failed.\n");
345 (k->type == KEY_DSA ?
346 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
347 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
348 key_write(k, stdout);
350 fprintf(stderr, "key write failed");
354 fprintf(stdout, "\n");
360 do_print_public(struct passwd *pw)
366 ask_filename(pw, "Enter file in which the key is");
367 if (stat(identity_file, &st) < 0) {
368 perror(identity_file);
371 prv = load_identity(identity_file);
373 fprintf(stderr, "load failed\n");
376 if (!key_write(prv, stdout))
377 fprintf(stderr, "key_write failed");
379 fprintf(stdout, "\n");
384 #define NUM_RSA_KEY_ELEMENTS 5+1
385 #define COPY_RSA_KEY(x, i) \
387 len = BN_num_bytes(prv->rsa->x); \
388 elements[i] = xmalloc(len); \
389 debug("#bytes %d", len); \
390 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
397 EVP_MD *evp_md = EVP_sha1();
401 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
404 EVP_DigestInit(&md, evp_md);
405 EVP_DigestUpdate(&md, pass, strlen(pass));
406 EVP_DigestFinal(&md, aut0, NULL);
407 memset(pass, 0, strlen(pass));
413 do_upload(struct passwd *pw, int reader)
417 u_char *elements[NUM_RSA_KEY_ELEMENTS];
419 u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
420 u_char AUT0[EVP_MAX_MD_SIZE];
421 int len, status = 1, i, fd = -1, ret;
422 int sw = 0, cla = 0x00;
424 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
427 ask_filename(pw, "Enter file in which the key is");
428 if (stat(identity_file, &st) < 0) {
429 perror(identity_file);
432 prv = load_identity(identity_file);
434 error("load failed");
439 COPY_RSA_KEY(iqmp, 2);
440 COPY_RSA_KEY(dmq1, 3);
441 COPY_RSA_KEY(dmp1, 4);
443 len = BN_num_bytes(prv->rsa->n);
444 fd = sectok_open(reader, STONOWAIT, &sw);
446 error("sectok_open failed: %s", sectok_get_sw(sw));
449 ret = sectok_reset(fd, 0, NULL, &sw);
451 error("sectok_reset failed: %s", sectok_get_sw(sw));
454 if ((cla = cyberflex_inq_class(fd)) < 0) {
455 error("cyberflex_inq_class failed");
458 memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
459 if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
460 if (get_AUT0(AUT0) < 0 ||
461 cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
462 error("cyberflex_verify_AUT0 failed");
468 if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
470 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
473 if (!sectok_swOK(sw))
475 log("cyberflex_load_rsa_priv done");
478 if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
480 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
483 if (!sectok_swOK(sw))
485 log("cyberflex_load_rsa_pub done");
487 log("loading key done");
491 for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
501 do_fingerprint(struct passwd *pw)
505 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
506 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
509 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
510 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
513 ask_filename(pw, "Enter file in which the key is");
514 if (stat(identity_file, &st) < 0) {
515 perror(identity_file);
518 public = key_load_public(identity_file, &comment);
519 if (public != NULL) {
520 fp = key_fingerprint(public, fptype, rep);
521 printf("%d %s %s\n", key_size(public), fp, comment);
530 f = fopen(identity_file, "r");
532 while (fgets(line, sizeof(line), f)) {
533 i = strlen(line) - 1;
534 if (line[i] != '\n') {
535 error("line %d too long: %.40s...", num, line);
546 /* Skip leading whitespace, empty and comment lines. */
547 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
549 if (!*cp || *cp == '\n' || *cp == '#')
551 i = strtol(cp, &ep, 10);
552 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
555 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
556 if (*cp == '\\' && cp[1] == '"')
557 cp++; /* Skip both */
566 public = key_new(KEY_RSA1);
567 if (key_read(public, &cp) != 1) {
570 public = key_new(KEY_UNSPEC);
571 if (key_read(public, &cp) != 1) {
576 comment = *cp ? cp : comment;
577 fp = key_fingerprint(public, fptype, rep);
578 printf("%d %s %s\n", key_size(public), fp,
579 comment ? comment : "no comment");
587 printf("%s is not a valid key file.\n", identity_file);
594 * Perform changing a passphrase. The argument is the passwd structure
595 * for the current user.
598 do_change_passphrase(struct passwd *pw)
601 char *old_passphrase, *passphrase1, *passphrase2;
606 ask_filename(pw, "Enter file in which the key is");
607 if (stat(identity_file, &st) < 0) {
608 perror(identity_file);
611 /* Try to load the file with empty passphrase. */
612 private = key_load_private(identity_file, "", &comment);
613 if (private == NULL) {
614 if (identity_passphrase)
615 old_passphrase = xstrdup(identity_passphrase);
618 read_passphrase("Enter old passphrase: ",
620 private = key_load_private(identity_file, old_passphrase,
622 memset(old_passphrase, 0, strlen(old_passphrase));
623 xfree(old_passphrase);
624 if (private == NULL) {
625 printf("Bad passphrase.\n");
629 printf("Key has comment '%s'\n", comment);
631 /* Ask the new passphrase (twice). */
632 if (identity_new_passphrase) {
633 passphrase1 = xstrdup(identity_new_passphrase);
637 read_passphrase("Enter new passphrase (empty for no "
638 "passphrase): ", RP_ALLOW_STDIN);
639 passphrase2 = read_passphrase("Enter same passphrase again: ",
642 /* Verify that they are the same. */
643 if (strcmp(passphrase1, passphrase2) != 0) {
644 memset(passphrase1, 0, strlen(passphrase1));
645 memset(passphrase2, 0, strlen(passphrase2));
648 printf("Pass phrases do not match. Try again.\n");
651 /* Destroy the other copy. */
652 memset(passphrase2, 0, strlen(passphrase2));
656 /* Save the file using the new passphrase. */
657 if (!key_save_private(private, identity_file, passphrase1, comment)) {
658 printf("Saving the key failed: %s.\n", identity_file);
659 memset(passphrase1, 0, strlen(passphrase1));
665 /* Destroy the passphrase and the copy of the key in memory. */
666 memset(passphrase1, 0, strlen(passphrase1));
668 key_free(private); /* Destroys contents */
671 printf("Your identification has been saved with the new passphrase.\n");
676 * Change the comment of a private key file.
679 do_change_comment(struct passwd *pw)
681 char new_comment[1024], *comment, *passphrase;
689 ask_filename(pw, "Enter file in which the key is");
690 if (stat(identity_file, &st) < 0) {
691 perror(identity_file);
694 private = key_load_private(identity_file, "", &comment);
695 if (private == NULL) {
696 if (identity_passphrase)
697 passphrase = xstrdup(identity_passphrase);
698 else if (identity_new_passphrase)
699 passphrase = xstrdup(identity_new_passphrase);
701 passphrase = read_passphrase("Enter passphrase: ",
703 /* Try to load using the passphrase. */
704 private = key_load_private(identity_file, passphrase, &comment);
705 if (private == NULL) {
706 memset(passphrase, 0, strlen(passphrase));
708 printf("Bad passphrase.\n");
712 passphrase = xstrdup("");
714 if (private->type != KEY_RSA1) {
715 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
719 printf("Key now has comment '%s'\n", comment);
721 if (identity_comment) {
722 strlcpy(new_comment, identity_comment, sizeof(new_comment));
724 printf("Enter new comment: ");
726 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
727 memset(passphrase, 0, strlen(passphrase));
731 if (strchr(new_comment, '\n'))
732 *strchr(new_comment, '\n') = 0;
735 /* Save the file using the new passphrase. */
736 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
737 printf("Saving the key failed: %s.\n", identity_file);
738 memset(passphrase, 0, strlen(passphrase));
744 memset(passphrase, 0, strlen(passphrase));
746 public = key_from_private(private);
749 strlcat(identity_file, ".pub", sizeof(identity_file));
750 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
752 printf("Could not save your public key in %s\n", identity_file);
757 printf("fdopen %s failed", identity_file);
760 if (!key_write(public, f))
761 fprintf(stderr, "write key failed");
763 fprintf(f, " %s\n", new_comment);
768 printf("The comment in your key file has been changed.\n");
775 printf("Usage: %s [-ceilpqyB] [-t type] [-b bits] [-f file] [-C comment] "
776 "[-N new-pass] [-P pass]\n", __progname);
781 * Main program for key management.
784 main(int ac, char **av)
786 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
787 Key *private, *public;
789 int opt, type, fd, reader = -1;
796 __progname = get_progname(av[0]);
800 SSLeay_add_all_algorithms();
802 /* we need this for the home * directory. */
803 pw = getpwuid(getuid());
805 printf("You don't exist, go away!\n");
808 if (gethostname(hostname, sizeof(hostname)) < 0) {
809 perror("gethostname");
813 while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:u:P:N:C:")) != -1) {
817 if (bits < 512 || bits > 32768) {
818 printf("Bits has bad value.\n");
823 print_fingerprint = 1;
826 print_bubblebabble = 1;
829 change_passphrase = 1;
835 strlcpy(identity_file, optarg, sizeof(identity_file));
839 identity_passphrase = optarg;
842 identity_new_passphrase = optarg;
845 identity_comment = optarg;
862 convert_from_ssh2 = 1;
868 key_type_name = "dsa";
871 key_type_name = optarg;
874 reader = atoi(optarg); /*XXX*/
882 printf("Too many arguments.\n");
885 if (change_passphrase && change_comment) {
886 printf("Can only have one of -p and -c.\n");
889 if (print_fingerprint || print_bubblebabble)
891 if (change_passphrase)
892 do_change_passphrase(pw);
894 do_change_comment(pw);
896 do_convert_to_ssh2(pw);
897 if (convert_from_ssh2)
898 do_convert_from_ssh2(pw);
903 do_upload(pw, reader);
905 fatal("no support for smartcards.");
910 type = key_type_from_name(key_type_name);
911 if (type == KEY_UNSPEC) {
912 fprintf(stderr, "unknown key type %s\n", key_type_name);
916 printf("Generating public/private %s key pair.\n", key_type_name);
917 private = key_generate(type, bits);
918 if (private == NULL) {
919 fprintf(stderr, "key_generate failed");
922 public = key_from_private(private);
925 ask_filename(pw, "Enter file in which to save the key");
927 /* Create ~/.ssh directory if it doesn\'t already exist. */
928 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
929 if (strstr(identity_file, dotsshdir) != NULL &&
930 stat(dotsshdir, &st) < 0) {
931 if (mkdir(dotsshdir, 0700) < 0)
932 error("Could not create directory '%s'.", dotsshdir);
934 printf("Created directory '%s'.\n", dotsshdir);
936 /* If the file already exists, ask the user to confirm. */
937 if (stat(identity_file, &st) >= 0) {
939 printf("%s already exists.\n", identity_file);
940 printf("Overwrite (y/n)? ");
942 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
944 if (yesno[0] != 'y' && yesno[0] != 'Y')
947 /* Ask for a passphrase (twice). */
948 if (identity_passphrase)
949 passphrase1 = xstrdup(identity_passphrase);
950 else if (identity_new_passphrase)
951 passphrase1 = xstrdup(identity_new_passphrase);
955 read_passphrase("Enter passphrase (empty for no "
956 "passphrase): ", RP_ALLOW_STDIN);
957 passphrase2 = read_passphrase("Enter same passphrase again: ",
959 if (strcmp(passphrase1, passphrase2) != 0) {
961 * The passphrases do not match. Clear them and
964 memset(passphrase1, 0, strlen(passphrase1));
965 memset(passphrase2, 0, strlen(passphrase2));
968 printf("Passphrases do not match. Try again.\n");
969 goto passphrase_again;
971 /* Clear the other copy of the passphrase. */
972 memset(passphrase2, 0, strlen(passphrase2));
976 if (identity_comment) {
977 strlcpy(comment, identity_comment, sizeof(comment));
979 /* Create default commend field for the passphrase. */
980 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
983 /* Save the key with the given passphrase and comment. */
984 if (!key_save_private(private, identity_file, passphrase1, comment)) {
985 printf("Saving the key failed: %s.\n", identity_file);
986 memset(passphrase1, 0, strlen(passphrase1));
990 /* Clear the passphrase. */
991 memset(passphrase1, 0, strlen(passphrase1));
994 /* Clear the private key and the random number generator. */
999 printf("Your identification has been saved in %s.\n", identity_file);
1001 strlcat(identity_file, ".pub", sizeof(identity_file));
1002 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1004 printf("Could not save your public key in %s\n", identity_file);
1007 f = fdopen(fd, "w");
1009 printf("fdopen %s failed", identity_file);
1012 if (!key_write(public, f))
1013 fprintf(stderr, "write key failed");
1014 fprintf(f, " %s\n", comment);
1018 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1019 printf("Your public key has been saved in %s.\n",
1021 printf("The key fingerprint is:\n");
1022 printf("%s %s\n", fp, comment);