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.43 2001/02/12 16:16:23 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;
50 /* The identity file name, given on the command line or entered by the user. */
51 char identity_file[1024];
52 int have_identity = 0;
54 /* This is set to the passphrase if given on the command line. */
55 char *identity_passphrase = NULL;
57 /* This is set to the new passphrase if given on the command line. */
58 char *identity_new_passphrase = NULL;
60 /* This is set to the new comment if given on the command line. */
61 char *identity_comment = NULL;
63 /* Dump public key file in format used by real and the original SSH 2 */
64 int convert_to_ssh2 = 0;
65 int convert_from_ssh2 = 0;
68 /* default to RSA for SSH-1 */
69 char *key_type_name = "rsa1";
72 #ifdef HAVE___PROGNAME
73 extern char *__progname;
78 char hostname[MAXHOSTNAMELEN];
81 ask_filename(struct passwd *pw, const char *prompt)
86 switch (key_type_from_name(key_type_name)) {
88 name = _PATH_SSH_CLIENT_IDENTITY;
91 name = _PATH_SSH_CLIENT_ID_DSA;
94 name = _PATH_SSH_CLIENT_ID_RSA;
97 fprintf(stderr, "bad key type");
101 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
102 fprintf(stderr, "%s (%s): ", prompt, identity_file);
104 if (fgets(buf, sizeof(buf), stdin) == NULL)
106 if (strchr(buf, '\n'))
107 *strchr(buf, '\n') = 0;
108 if (strcmp(buf, "") != 0)
109 strlcpy(identity_file, buf, sizeof(identity_file));
114 try_load_key(char *filename, Key *k)
117 if (!load_private_key(filename, "", k, NULL)) {
118 char *pass = read_passphrase("Enter passphrase: ", 1);
119 if (!load_private_key(filename, pass, k, NULL)) {
122 memset(pass, 0, strlen(pass));
128 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
129 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
130 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
131 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
134 do_convert_to_ssh2(struct passwd *pw)
142 ask_filename(pw, "Enter file in which the key is");
143 if (stat(identity_file, &st) < 0) {
144 perror(identity_file);
147 k = key_new(KEY_UNSPEC);
148 if (!try_load_key(identity_file, k)) {
149 fprintf(stderr, "load failed\n");
152 key_to_blob(k, &blob, &len);
153 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
155 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
156 key_size(k), key_type(k),
157 pw->pw_name, hostname);
158 dump_base64(stdout, blob, len);
159 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
166 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
168 int bits = buffer_get_int(b);
169 int bytes = (bits + 7) / 8;
170 if (buffer_len(b) < bytes)
171 fatal("buffer_get_bignum_bits: input buffer too small");
172 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
173 buffer_consume(b, bytes);
177 do_convert_private_ssh2_from_blob(char *blob, int blen)
182 int ignore, magic, rlen;
186 buffer_append(&b, blob, blen);
188 magic = buffer_get_int(&b);
189 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
190 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
194 ignore = buffer_get_int(&b);
195 type = buffer_get_string(&b, NULL);
196 cipher = buffer_get_string(&b, NULL);
197 ignore = buffer_get_int(&b);
198 ignore = buffer_get_int(&b);
199 ignore = buffer_get_int(&b);
202 if (strcmp(cipher, "none") != 0) {
203 error("unsupported cipher %s", cipher);
210 key = key_new(KEY_DSA);
212 dsa->priv_key = BN_new();
213 if (dsa->priv_key == NULL) {
214 error("alloc priv_key failed");
218 buffer_get_bignum_bits(&b, dsa->p);
219 buffer_get_bignum_bits(&b, dsa->g);
220 buffer_get_bignum_bits(&b, dsa->q);
221 buffer_get_bignum_bits(&b, dsa->pub_key);
222 buffer_get_bignum_bits(&b, dsa->priv_key);
223 rlen = buffer_len(&b);
225 error("do_convert_private_ssh2_from_blob: remaining bytes in key blob %d", rlen);
231 do_convert_from_ssh2(struct passwd *pw)
239 int escaped = 0, private = 0, ok;
243 ask_filename(pw, "Enter file in which the key is");
244 if (stat(identity_file, &st) < 0) {
245 perror(identity_file);
248 fp = fopen(identity_file, "r");
250 perror(identity_file);
254 while (fgets(line, sizeof(line), fp)) {
255 if (!(p = strchr(line, '\n'))) {
256 fprintf(stderr, "input line too long.\n");
259 if (p > line && p[-1] == '\\')
261 if (strncmp(line, "----", 4) == 0 ||
262 strstr(line, ": ") != NULL) {
263 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
265 fprintf(stderr, "ignore: %s", line);
270 fprintf(stderr, "escaped: %s", line);
274 strlcat(encoded, line, sizeof(encoded));
276 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
278 fprintf(stderr, "uudecode failed.\n");
282 do_convert_private_ssh2_from_blob(blob, blen) :
283 key_from_blob(blob, blen);
285 fprintf(stderr, "decode blob failed.\n");
289 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
290 key_write(k, stdout);
292 fprintf(stderr, "key write failed");
296 fprintf(stdout, "\n");
302 do_print_public(struct passwd *pw)
308 ask_filename(pw, "Enter file in which the key is");
309 if (stat(identity_file, &st) < 0) {
310 perror(identity_file);
313 k = key_new(KEY_UNSPEC);
314 if (!try_load_key(identity_file, k)) {
315 fprintf(stderr, "load failed\n");
318 if (!key_write(k, stdout))
319 fprintf(stderr, "key_write failed");
321 fprintf(stdout, "\n");
326 do_fingerprint(struct passwd *pw)
331 char *comment = NULL, *cp, *ep, line[16*1024];
332 int i, skip = 0, num = 1, invalid = 1, success = 0;
336 ask_filename(pw, "Enter file in which the key is");
337 if (stat(identity_file, &st) < 0) {
338 perror(identity_file);
341 public = key_new(KEY_RSA1);
342 if (load_public_key(identity_file, public, &comment)) {
346 public = key_new(KEY_UNSPEC);
347 if (try_load_public_key(identity_file, public, &comment))
350 debug("try_load_public_key KEY_UNSPEC failed");
353 printf("%d %s %s\n", key_size(public), key_fingerprint(public), comment);
359 f = fopen(identity_file, "r");
361 while (fgets(line, sizeof(line), f)) {
362 i = strlen(line) - 1;
363 if (line[i] != '\n') {
364 error("line %d too long: %.40s...", num, line);
375 /* Skip leading whitespace, empty and comment lines. */
376 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
378 if (!*cp || *cp == '\n' || *cp == '#')
380 i = strtol(cp, &ep, 10);
381 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
384 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
385 if (*cp == '\\' && cp[1] == '"')
386 cp++; /* Skip both */
395 public = key_new(KEY_RSA1);
396 if (key_read(public, &cp) != 1) {
399 public = key_new(KEY_UNSPEC);
400 if (key_read(public, &cp) != 1) {
405 comment = *cp ? cp : comment;
406 printf("%d %s %s\n", key_size(public),
407 key_fingerprint(public),
408 comment ? comment : "no comment");
415 printf("%s is not a valid key file.\n", identity_file);
422 * Perform changing a passphrase. The argument is the passwd structure
423 * for the current user.
426 do_change_passphrase(struct passwd *pw)
429 char *old_passphrase, *passphrase1, *passphrase2;
436 ask_filename(pw, "Enter file in which the key is");
437 if (stat(identity_file, &st) < 0) {
438 perror(identity_file);
441 public = key_new(type);
442 if (!load_public_key(identity_file, public, NULL)) {
445 /* Clear the public key since we are just about to load the whole file. */
448 /* Try to load the file with empty passphrase. */
449 private = key_new(type);
450 if (!load_private_key(identity_file, "", private, &comment)) {
451 if (identity_passphrase)
452 old_passphrase = xstrdup(identity_passphrase);
454 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
455 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
456 memset(old_passphrase, 0, strlen(old_passphrase));
457 xfree(old_passphrase);
458 printf("Bad passphrase.\n");
461 memset(old_passphrase, 0, strlen(old_passphrase));
462 xfree(old_passphrase);
464 printf("Key has comment '%s'\n", comment);
466 /* Ask the new passphrase (twice). */
467 if (identity_new_passphrase) {
468 passphrase1 = xstrdup(identity_new_passphrase);
472 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
473 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
475 /* Verify that they are the same. */
476 if (strcmp(passphrase1, passphrase2) != 0) {
477 memset(passphrase1, 0, strlen(passphrase1));
478 memset(passphrase2, 0, strlen(passphrase2));
481 printf("Pass phrases do not match. Try again.\n");
484 /* Destroy the other copy. */
485 memset(passphrase2, 0, strlen(passphrase2));
489 /* Save the file using the new passphrase. */
490 if (!save_private_key(identity_file, passphrase1, private, comment)) {
491 printf("Saving the key failed: %s: %s.\n",
492 identity_file, strerror(errno));
493 memset(passphrase1, 0, strlen(passphrase1));
499 /* Destroy the passphrase and the copy of the key in memory. */
500 memset(passphrase1, 0, strlen(passphrase1));
502 key_free(private); /* Destroys contents */
505 printf("Your identification has been saved with the new passphrase.\n");
510 * Change the comment of a private key file.
513 do_change_comment(struct passwd *pw)
515 char new_comment[1024], *comment;
523 ask_filename(pw, "Enter file in which the key is");
524 if (stat(identity_file, &st) < 0) {
525 perror(identity_file);
529 * Try to load the public key from the file the verify that it is
530 * readable and of the proper format.
532 public = key_new(KEY_RSA1);
533 if (!load_public_key(identity_file, public, NULL)) {
534 printf("%s is not a valid key file.\n", identity_file);
535 printf("Comments are only supported in RSA1 keys\n");
539 private = key_new(KEY_RSA1);
540 if (load_private_key(identity_file, "", private, &comment))
541 passphrase = xstrdup("");
543 if (identity_passphrase)
544 passphrase = xstrdup(identity_passphrase);
545 else if (identity_new_passphrase)
546 passphrase = xstrdup(identity_new_passphrase);
548 passphrase = read_passphrase("Enter passphrase: ", 1);
549 /* Try to load using the passphrase. */
550 if (!load_private_key(identity_file, passphrase, private, &comment)) {
551 memset(passphrase, 0, strlen(passphrase));
553 printf("Bad passphrase.\n");
557 printf("Key now has comment '%s'\n", comment);
559 if (identity_comment) {
560 strlcpy(new_comment, identity_comment, sizeof(new_comment));
562 printf("Enter new comment: ");
564 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
565 memset(passphrase, 0, strlen(passphrase));
569 if (strchr(new_comment, '\n'))
570 *strchr(new_comment, '\n') = 0;
573 /* Save the file using the new passphrase. */
574 if (!save_private_key(identity_file, passphrase, private, new_comment)) {
575 printf("Saving the key failed: %s: %s.\n",
576 identity_file, strerror(errno));
577 memset(passphrase, 0, strlen(passphrase));
583 memset(passphrase, 0, strlen(passphrase));
587 strlcat(identity_file, ".pub", sizeof(identity_file));
588 f = fopen(identity_file, "w");
590 printf("Could not save your public key in %s\n", identity_file);
593 if (!key_write(public, f))
594 fprintf(stderr, "write key failed");
596 fprintf(f, " %s\n", new_comment);
601 printf("The comment in your key file has been changed.\n");
608 printf("Usage: %s [-lpqxXyc] [-t type] [-b bits] [-f file] [-C comment] [-N new-pass] [-P pass]\n", __progname);
613 * Main program for key management.
616 main(int ac, char **av)
618 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
629 __progname = get_progname(av[0]);
632 SSLeay_add_all_algorithms();
634 /* we need this for the home * directory. */
635 pw = getpwuid(getuid());
637 printf("You don't exist, go away!\n");
640 if (gethostname(hostname, sizeof(hostname)) < 0) {
641 perror("gethostname");
645 while ((opt = getopt(ac, av, "dqpclRxXyb:f:t:P:N:C:")) != -1) {
649 if (bits < 512 || bits > 32768) {
650 printf("Bits has bad value.\n");
656 print_fingerprint = 1;
660 change_passphrase = 1;
668 strlcpy(identity_file, optarg, sizeof(identity_file));
673 identity_passphrase = optarg;
677 identity_new_passphrase = optarg;
681 identity_comment = optarg;
698 convert_from_ssh2 = 1;
706 key_type_name = "dsa";
710 key_type_name = optarg;
719 printf("Too many arguments.\n");
722 if (change_passphrase && change_comment) {
723 printf("Can only have one of -p and -c.\n");
726 if (print_fingerprint)
728 if (change_passphrase)
729 do_change_passphrase(pw);
731 do_change_comment(pw);
733 do_convert_to_ssh2(pw);
734 if (convert_from_ssh2)
735 do_convert_from_ssh2(pw);
741 type = key_type_from_name(key_type_name);
742 if (type == KEY_UNSPEC) {
743 fprintf(stderr, "unknown key type %s\n", key_type_name);
747 printf("Generating public/private %s key pair.\n", key_type_name);
748 private = key_generate(type, bits);
749 if (private == NULL) {
750 fprintf(stderr, "key_generate failed");
753 public = key_from_private(private);
756 ask_filename(pw, "Enter file in which to save the key");
758 /* Create ~/.ssh directory if it doesn\'t already exist. */
759 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
760 if (strstr(identity_file, dotsshdir) != NULL &&
761 stat(dotsshdir, &st) < 0) {
762 if (mkdir(dotsshdir, 0700) < 0)
763 error("Could not create directory '%s'.", dotsshdir);
765 printf("Created directory '%s'.\n", dotsshdir);
767 /* If the file already exists, ask the user to confirm. */
768 if (stat(identity_file, &st) >= 0) {
770 printf("%s already exists.\n", identity_file);
771 printf("Overwrite (y/n)? ");
773 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
775 if (yesno[0] != 'y' && yesno[0] != 'Y')
778 /* Ask for a passphrase (twice). */
779 if (identity_passphrase)
780 passphrase1 = xstrdup(identity_passphrase);
781 else if (identity_new_passphrase)
782 passphrase1 = xstrdup(identity_new_passphrase);
786 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
787 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
788 if (strcmp(passphrase1, passphrase2) != 0) {
789 /* The passphrases do not match. Clear them and retry. */
790 memset(passphrase1, 0, strlen(passphrase1));
791 memset(passphrase2, 0, strlen(passphrase2));
794 printf("Passphrases do not match. Try again.\n");
795 goto passphrase_again;
797 /* Clear the other copy of the passphrase. */
798 memset(passphrase2, 0, strlen(passphrase2));
802 if (identity_comment) {
803 strlcpy(comment, identity_comment, sizeof(comment));
805 /* Create default commend field for the passphrase. */
806 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
809 /* Save the key with the given passphrase and comment. */
810 if (!save_private_key(identity_file, passphrase1, private, comment)) {
811 printf("Saving the key failed: %s: %s.\n",
812 identity_file, strerror(errno));
813 memset(passphrase1, 0, strlen(passphrase1));
817 /* Clear the passphrase. */
818 memset(passphrase1, 0, strlen(passphrase1));
821 /* Clear the private key and the random number generator. */
826 printf("Your identification has been saved in %s.\n", identity_file);
828 strlcat(identity_file, ".pub", sizeof(identity_file));
829 f = fopen(identity_file, "w");
831 printf("Could not save your public key in %s\n", identity_file);
834 if (!key_write(public, f))
835 fprintf(stderr, "write key failed");
836 fprintf(f, " %s\n", comment);
840 printf("Your public key has been saved in %s.\n",
842 printf("The key fingerprint is:\n");
843 printf("%s %s\n", key_fingerprint(public), comment);