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.32 2000/10/09 21:30:44 markus Exp $");
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
19 #include <openssl/rsa.h>
20 #include <openssl/dsa.h>
33 /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
37 * Flag indicating that we just want to change the passphrase. This can be
38 * set on the command line.
40 int change_passphrase = 0;
43 * Flag indicating that we just want to change the comment. This can be set
44 * on the command line.
46 int change_comment = 0;
50 /* Flag indicating that we just want to see the key fingerprint */
51 int print_fingerprint = 0;
53 /* The identity file name, given on the command line or entered by the user. */
54 char identity_file[1024];
55 int have_identity = 0;
57 /* This is set to the passphrase if given on the command line. */
58 char *identity_passphrase = NULL;
60 /* This is set to the new passphrase if given on the command line. */
61 char *identity_new_passphrase = NULL;
63 /* This is set to the new comment if given on the command line. */
64 char *identity_comment = NULL;
66 /* Dump public key file in format used by real and the original SSH 2 */
67 int convert_to_ssh2 = 0;
68 int convert_from_ssh2 = 0;
73 extern char *__progname;
75 char hostname[MAXHOSTNAMELEN];
78 ask_filename(struct passwd *pw, const char *prompt)
81 snprintf(identity_file, sizeof(identity_file), "%s/%s",
83 dsa_mode ? SSH_CLIENT_ID_DSA: SSH_CLIENT_IDENTITY);
84 printf("%s (%s): ", prompt, identity_file);
86 if (fgets(buf, sizeof(buf), stdin) == NULL)
88 if (strchr(buf, '\n'))
89 *strchr(buf, '\n') = 0;
90 if (strcmp(buf, "") != 0)
91 strlcpy(identity_file, buf, sizeof(identity_file));
96 try_load_key(char *filename, Key *k)
99 if (!load_private_key(filename, "", k, NULL)) {
100 char *pass = read_passphrase("Enter passphrase: ", 1);
101 if (!load_private_key(filename, pass, k, NULL)) {
104 memset(pass, 0, strlen(pass));
110 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
111 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
112 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
113 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
116 do_convert_to_ssh2(struct passwd *pw)
124 ask_filename(pw, "Enter file in which the key is");
125 if (stat(identity_file, &st) < 0) {
126 perror(identity_file);
129 k = key_new(KEY_DSA);
130 if (!try_load_key(identity_file, k)) {
131 fprintf(stderr, "load failed\n");
134 dsa_make_key_blob(k, &blob, &len);
135 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
137 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
138 key_size(k), key_type(k),
139 pw->pw_name, hostname);
140 dump_base64(stdout, blob, len);
141 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
148 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
150 int bits = buffer_get_int(b);
151 int bytes = (bits + 7) / 8;
152 if (buffer_len(b) < bytes)
153 fatal("buffer_get_bignum_bits: input buffer too small");
154 BN_bin2bn((unsigned char *)buffer_ptr(b), bytes, value);
155 buffer_consume(b, bytes);
159 do_convert_private_ssh2_from_blob(char *blob, int blen)
164 int ignore, magic, rlen;
168 buffer_append(&b, blob, blen);
170 magic = buffer_get_int(&b);
171 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
172 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
176 ignore = buffer_get_int(&b);
177 type = buffer_get_string(&b, NULL);
178 cipher = buffer_get_string(&b, NULL);
179 ignore = buffer_get_int(&b);
180 ignore = buffer_get_int(&b);
181 ignore = buffer_get_int(&b);
184 if (strcmp(cipher, "none") != 0) {
185 error("unsupported cipher %s", cipher);
192 key = key_new(KEY_DSA);
194 dsa->priv_key = BN_new();
195 if (dsa->priv_key == NULL) {
196 error("alloc priv_key failed");
200 buffer_get_bignum_bits(&b, dsa->p);
201 buffer_get_bignum_bits(&b, dsa->g);
202 buffer_get_bignum_bits(&b, dsa->q);
203 buffer_get_bignum_bits(&b, dsa->pub_key);
204 buffer_get_bignum_bits(&b, dsa->priv_key);
205 rlen = buffer_len(&b);
207 error("do_convert_private_ssh2_from_blob: remaining bytes in key blob %d", rlen);
213 do_convert_from_ssh2(struct passwd *pw)
221 int escaped = 0, private = 0, ok;
225 ask_filename(pw, "Enter file in which the key is");
226 if (stat(identity_file, &st) < 0) {
227 perror(identity_file);
230 fp = fopen(identity_file, "r");
232 perror(identity_file);
236 while (fgets(line, sizeof(line), fp)) {
237 if (!(p = strchr(line, '\n'))) {
238 fprintf(stderr, "input line too long.\n");
241 if (p > line && p[-1] == '\\')
243 if (strncmp(line, "----", 4) == 0 ||
244 strstr(line, ": ") != NULL) {
245 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
247 fprintf(stderr, "ignore: %s", line);
252 fprintf(stderr, "escaped: %s", line);
256 strlcat(encoded, line, sizeof(encoded));
258 blen = uudecode(encoded, (unsigned char *)blob, sizeof(blob));
260 fprintf(stderr, "uudecode failed.\n");
264 do_convert_private_ssh2_from_blob(blob, blen) :
265 dsa_key_from_blob(blob, blen);
267 fprintf(stderr, "decode blob failed.\n");
271 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
272 key_write(k, stdout);
274 fprintf(stderr, "key write failed");
278 fprintf(stdout, "\n");
284 do_print_public(struct passwd *pw)
292 ask_filename(pw, "Enter file in which the key is");
293 if (stat(identity_file, &st) < 0) {
294 perror(identity_file);
297 k = key_new(KEY_DSA);
298 if (!try_load_key(identity_file, k)) {
299 fprintf(stderr, "load failed\n");
302 dsa_make_key_blob(k, &blob, &len);
303 if (!key_write(k, stdout))
304 fprintf(stderr, "key_write failed");
307 fprintf(stdout, "\n");
312 do_fingerprint(struct passwd *pw)
318 char *comment = NULL, *cp, *ep, line[16*1024];
319 int i, skip = 0, num = 1, invalid = 1;
324 ask_filename(pw, "Enter file in which the key is");
325 if (stat(identity_file, &st) < 0) {
326 perror(identity_file);
329 public = key_new(KEY_RSA);
330 if (load_public_key(identity_file, public, &comment)) {
331 printf("%d %s %s\n", BN_num_bits(public->rsa->n),
332 key_fingerprint(public), comment);
337 f = fopen(identity_file, "r");
339 while (fgets(line, sizeof(line), f)) {
340 i = strlen(line) - 1;
341 if (line[i] != '\n') {
342 error("line %d too long: %.40s...", num, line);
353 /* Skip leading whitespace, empty and comment lines. */
354 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
356 if (!*cp || *cp == '\n' || *cp == '#')
358 i = strtol(cp, &ep, 10);
359 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
362 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
363 if (*cp == '\\' && cp[1] == '"')
364 cp++; /* Skip both */
373 if (auth_rsa_read_key(&cp, &ignore, public->rsa->e, public->rsa->n)) {
375 comment = *cp ? cp : comment;
376 printf("%d %s %s\n", key_size(public),
377 key_fingerprint(public),
378 comment ? comment : "no comment");
385 printf("%s is not a valid key file.\n", identity_file);
392 * Perform changing a passphrase. The argument is the passwd structure
393 * for the current user.
396 do_change_passphrase(struct passwd *pw)
399 char *old_passphrase, *passphrase1, *passphrase2;
403 int type = dsa_mode ? KEY_DSA : KEY_RSA;
406 ask_filename(pw, "Enter file in which the key is");
407 if (stat(identity_file, &st) < 0) {
408 perror(identity_file);
412 if (type == KEY_RSA) {
413 /* XXX this works currently only for RSA */
414 public = key_new(type);
415 if (!load_public_key(identity_file, public, NULL)) {
416 printf("%s is not a valid key file.\n", identity_file);
419 /* Clear the public key since we are just about to load the whole file. */
423 /* Try to load the file with empty passphrase. */
424 private = key_new(type);
425 if (!load_private_key(identity_file, "", private, &comment)) {
426 if (identity_passphrase)
427 old_passphrase = xstrdup(identity_passphrase);
429 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
430 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
431 memset(old_passphrase, 0, strlen(old_passphrase));
432 xfree(old_passphrase);
433 printf("Bad passphrase.\n");
436 memset(old_passphrase, 0, strlen(old_passphrase));
437 xfree(old_passphrase);
439 printf("Key has comment '%s'\n", comment);
441 /* Ask the new passphrase (twice). */
442 if (identity_new_passphrase) {
443 passphrase1 = xstrdup(identity_new_passphrase);
447 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
448 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
450 /* Verify that they are the same. */
451 if (strcmp(passphrase1, passphrase2) != 0) {
452 memset(passphrase1, 0, strlen(passphrase1));
453 memset(passphrase2, 0, strlen(passphrase2));
456 printf("Pass phrases do not match. Try again.\n");
459 /* Destroy the other copy. */
460 memset(passphrase2, 0, strlen(passphrase2));
464 /* Save the file using the new passphrase. */
465 if (!save_private_key(identity_file, passphrase1, private, comment)) {
466 printf("Saving the key failed: %s: %s.\n",
467 identity_file, strerror(errno));
468 memset(passphrase1, 0, strlen(passphrase1));
474 /* Destroy the passphrase and the copy of the key in memory. */
475 memset(passphrase1, 0, strlen(passphrase1));
477 key_free(private); /* Destroys contents */
480 printf("Your identification has been saved with the new passphrase.\n");
485 * Change the comment of a private key file.
488 do_change_comment(struct passwd *pw)
490 char new_comment[1024], *comment;
498 ask_filename(pw, "Enter file in which the key is");
499 if (stat(identity_file, &st) < 0) {
500 perror(identity_file);
504 * Try to load the public key from the file the verify that it is
505 * readable and of the proper format.
507 public = key_new(KEY_RSA);
508 if (!load_public_key(identity_file, public, NULL)) {
509 printf("%s is not a valid key file.\n", identity_file);
513 private = key_new(KEY_RSA);
514 if (load_private_key(identity_file, "", private, &comment))
515 passphrase = xstrdup("");
517 if (identity_passphrase)
518 passphrase = xstrdup(identity_passphrase);
519 else if (identity_new_passphrase)
520 passphrase = xstrdup(identity_new_passphrase);
522 passphrase = read_passphrase("Enter passphrase: ", 1);
523 /* Try to load using the passphrase. */
524 if (!load_private_key(identity_file, passphrase, private, &comment)) {
525 memset(passphrase, 0, strlen(passphrase));
527 printf("Bad passphrase.\n");
531 printf("Key now has comment '%s'\n", comment);
533 if (identity_comment) {
534 strlcpy(new_comment, identity_comment, sizeof(new_comment));
536 printf("Enter new comment: ");
538 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
539 memset(passphrase, 0, strlen(passphrase));
543 if (strchr(new_comment, '\n'))
544 *strchr(new_comment, '\n') = 0;
547 /* Save the file using the new passphrase. */
548 if (!save_private_key(identity_file, passphrase, private, new_comment)) {
549 printf("Saving the key failed: %s: %s.\n",
550 identity_file, strerror(errno));
551 memset(passphrase, 0, strlen(passphrase));
557 memset(passphrase, 0, strlen(passphrase));
561 strlcat(identity_file, ".pub", sizeof(identity_file));
562 f = fopen(identity_file, "w");
564 printf("Could not save your public key in %s\n", identity_file);
567 if (!key_write(public, f))
568 fprintf(stderr, "write key failed");
570 fprintf(f, " %s\n", new_comment);
575 printf("The comment in your key file has been changed.\n");
582 printf("Usage: %s [-lpqxXydc] [-b bits] [-f file] [-C comment] [-N new-pass] [-P pass]\n", __progname);
587 * Main program for key management.
590 main(int ac, char **av)
592 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
602 SSLeay_add_all_algorithms();
604 /* we need this for the home * directory. */
605 pw = getpwuid(getuid());
607 printf("You don't exist, go away!\n");
610 if (gethostname(hostname, sizeof(hostname)) < 0) {
611 perror("gethostname");
615 while ((opt = getopt(ac, av, "dqpclRxXyb:f:P:N:C:")) != EOF) {
619 if (bits < 512 || bits > 32768) {
620 printf("Bits has bad value.\n");
626 print_fingerprint = 1;
630 change_passphrase = 1;
638 strlcpy(identity_file, optarg, sizeof(identity_file));
643 identity_passphrase = optarg;
647 identity_new_passphrase = optarg;
651 identity_comment = optarg;
659 if (rsa_alive() == 0)
670 convert_from_ssh2 = 1;
687 printf("Too many arguments.\n");
690 if (change_passphrase && change_comment) {
691 printf("Can only have one of -p and -c.\n");
694 /* check if RSA support is needed and exists */
695 if (dsa_mode == 0 && rsa_alive() == 0) {
697 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
701 if (print_fingerprint)
703 if (change_passphrase)
704 do_change_passphrase(pw);
706 do_change_comment(pw);
708 do_convert_to_ssh2(pw);
709 if (convert_from_ssh2)
710 do_convert_from_ssh2(pw);
718 printf("Generating DSA parameter and key.\n");
719 public = private = dsa_generate_key(bits);
720 if (private == NULL) {
721 fprintf(stderr, "dsa_generate_keys failed");
727 /* Generate the rsa key pair. */
728 public = key_new(KEY_RSA);
729 private = key_new(KEY_RSA);
730 rsa_generate_key(private->rsa, public->rsa, bits);
734 ask_filename(pw, "Enter file in which to save the key");
736 /* Create ~/.ssh directory if it doesn\'t already exist. */
737 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR);
738 if (strstr(identity_file, dotsshdir) != NULL &&
739 stat(dotsshdir, &st) < 0) {
740 if (mkdir(dotsshdir, 0700) < 0)
741 error("Could not create directory '%s'.", dotsshdir);
743 printf("Created directory '%s'.\n", dotsshdir);
745 /* If the file already exists, ask the user to confirm. */
746 if (stat(identity_file, &st) >= 0) {
748 printf("%s already exists.\n", identity_file);
749 printf("Overwrite (y/n)? ");
751 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
753 if (yesno[0] != 'y' && yesno[0] != 'Y')
756 /* Ask for a passphrase (twice). */
757 if (identity_passphrase)
758 passphrase1 = xstrdup(identity_passphrase);
759 else if (identity_new_passphrase)
760 passphrase1 = xstrdup(identity_new_passphrase);
764 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
765 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
766 if (strcmp(passphrase1, passphrase2) != 0) {
767 /* The passphrases do not match. Clear them and retry. */
768 memset(passphrase1, 0, strlen(passphrase1));
769 memset(passphrase2, 0, strlen(passphrase2));
772 printf("Passphrases do not match. Try again.\n");
773 goto passphrase_again;
775 /* Clear the other copy of the passphrase. */
776 memset(passphrase2, 0, strlen(passphrase2));
780 if (identity_comment) {
781 strlcpy(comment, identity_comment, sizeof(comment));
783 /* Create default commend field for the passphrase. */
784 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
787 /* Save the key with the given passphrase and comment. */
788 if (!save_private_key(identity_file, passphrase1, private, comment)) {
789 printf("Saving the key failed: %s: %s.\n",
790 identity_file, strerror(errno));
791 memset(passphrase1, 0, strlen(passphrase1));
795 /* Clear the passphrase. */
796 memset(passphrase1, 0, strlen(passphrase1));
799 /* Clear the private key and the random number generator. */
800 if (private != public) {
806 printf("Your identification has been saved in %s.\n", identity_file);
808 strlcat(identity_file, ".pub", sizeof(identity_file));
809 f = fopen(identity_file, "w");
811 printf("Could not save your public key in %s\n", identity_file);
814 if (!key_write(public, f))
815 fprintf(stderr, "write key failed");
816 fprintf(f, " %s\n", comment);
820 printf("Your public key has been saved in %s.\n",
822 printf("The key fingerprint is:\n");
823 printf("%s %s\n", key_fingerprint(public), comment);