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.53 2001/03/26 23:23:24 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 prv = try_load_pem_key(identity_file);
152 fprintf(stderr, "load failed\n");
155 key_to_blob(prv, &blob, &len);
156 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
158 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
159 key_size(prv), key_type(prv),
160 pw->pw_name, hostname);
161 dump_base64(stdout, blob, len);
162 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
169 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
171 int bits = buffer_get_int(b);
172 int bytes = (bits + 7) / 8;
174 if (buffer_len(b) < bytes)
175 fatal("buffer_get_bignum_bits: input buffer too small: "
176 "need %d have %d", bytes, buffer_len(b));
177 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
178 buffer_consume(b, bytes);
182 do_convert_private_ssh2_from_blob(char *blob, int blen)
186 int ignore, magic, rlen, ktype;
190 buffer_append(&b, blob, blen);
192 magic = buffer_get_int(&b);
193 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
194 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
198 ignore = buffer_get_int(&b);
199 type = buffer_get_string(&b, NULL);
200 cipher = buffer_get_string(&b, NULL);
201 ignore = buffer_get_int(&b);
202 ignore = buffer_get_int(&b);
203 ignore = buffer_get_int(&b);
205 if (strcmp(cipher, "none") != 0) {
206 error("unsupported cipher %s", cipher);
214 if (strstr(type, "dsa")) {
216 } else if (strstr(type, "rsa")) {
222 key = key_new_private(ktype);
227 buffer_get_bignum_bits(&b, key->dsa->p);
228 buffer_get_bignum_bits(&b, key->dsa->g);
229 buffer_get_bignum_bits(&b, key->dsa->q);
230 buffer_get_bignum_bits(&b, key->dsa->pub_key);
231 buffer_get_bignum_bits(&b, key->dsa->priv_key);
234 if (!BN_set_word(key->rsa->e, (u_long) buffer_get_char(&b))) {
239 buffer_get_bignum_bits(&b, key->rsa->d);
240 buffer_get_bignum_bits(&b, key->rsa->n);
241 buffer_get_bignum_bits(&b, key->rsa->iqmp);
242 buffer_get_bignum_bits(&b, key->rsa->q);
243 buffer_get_bignum_bits(&b, key->rsa->p);
244 generate_additional_parameters(key->rsa);
247 rlen = buffer_len(&b);
249 error("do_convert_private_ssh2_from_blob: "
250 "remaining bytes in key blob %d", rlen);
255 u_char *sig, data[10] = "abcde12345";
257 key_sign(key, &sig, &slen, data, sizeof data);
258 key_verify(key, sig, slen, data, sizeof data);
266 do_convert_from_ssh2(struct passwd *pw)
274 int escaped = 0, private = 0, ok;
278 ask_filename(pw, "Enter file in which the key is");
279 if (stat(identity_file, &st) < 0) {
280 perror(identity_file);
283 fp = fopen(identity_file, "r");
285 perror(identity_file);
289 while (fgets(line, sizeof(line), fp)) {
290 if (!(p = strchr(line, '\n'))) {
291 fprintf(stderr, "input line too long.\n");
294 if (p > line && p[-1] == '\\')
296 if (strncmp(line, "----", 4) == 0 ||
297 strstr(line, ": ") != NULL) {
298 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
300 fprintf(stderr, "ignore: %s", line);
305 fprintf(stderr, "escaped: %s", line);
309 strlcat(encoded, line, sizeof(encoded));
311 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
313 fprintf(stderr, "uudecode failed.\n");
317 do_convert_private_ssh2_from_blob(blob, blen) :
318 key_from_blob(blob, blen);
320 fprintf(stderr, "decode blob failed.\n");
324 (k->type == KEY_DSA ?
325 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
326 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
327 key_write(k, stdout);
329 fprintf(stderr, "key write failed");
333 fprintf(stdout, "\n");
339 do_print_public(struct passwd *pw)
345 ask_filename(pw, "Enter file in which the key is");
346 if (stat(identity_file, &st) < 0) {
347 perror(identity_file);
350 prv = try_load_pem_key(identity_file);
352 fprintf(stderr, "load failed\n");
355 if (!key_write(prv, stdout))
356 fprintf(stderr, "key_write failed");
358 fprintf(stdout, "\n");
363 do_fingerprint(struct passwd *pw)
367 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
368 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
371 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
372 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
375 ask_filename(pw, "Enter file in which the key is");
376 if (stat(identity_file, &st) < 0) {
377 perror(identity_file);
380 public = key_load_public(identity_file, &comment);
381 if (public != NULL) {
382 fp = key_fingerprint(public, fptype, rep);
383 printf("%d %s %s\n", key_size(public), fp, comment);
392 f = fopen(identity_file, "r");
394 while (fgets(line, sizeof(line), f)) {
395 i = strlen(line) - 1;
396 if (line[i] != '\n') {
397 error("line %d too long: %.40s...", num, line);
408 /* Skip leading whitespace, empty and comment lines. */
409 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
411 if (!*cp || *cp == '\n' || *cp == '#')
413 i = strtol(cp, &ep, 10);
414 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
417 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
418 if (*cp == '\\' && cp[1] == '"')
419 cp++; /* Skip both */
428 public = key_new(KEY_RSA1);
429 if (key_read(public, &cp) != 1) {
432 public = key_new(KEY_UNSPEC);
433 if (key_read(public, &cp) != 1) {
438 comment = *cp ? cp : comment;
439 fp = key_fingerprint(public, fptype, rep);
440 printf("%d %s %s\n", key_size(public), fp,
441 comment ? comment : "no comment");
449 printf("%s is not a valid key file.\n", identity_file);
456 * Perform changing a passphrase. The argument is the passwd structure
457 * for the current user.
460 do_change_passphrase(struct passwd *pw)
463 char *old_passphrase, *passphrase1, *passphrase2;
468 ask_filename(pw, "Enter file in which the key is");
469 if (stat(identity_file, &st) < 0) {
470 perror(identity_file);
473 /* Try to load the file with empty passphrase. */
474 private = key_load_private(identity_file, "", &comment);
475 if (private == NULL) {
476 if (identity_passphrase)
477 old_passphrase = xstrdup(identity_passphrase);
479 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
480 private = key_load_private(identity_file, old_passphrase , &comment);
481 memset(old_passphrase, 0, strlen(old_passphrase));
482 xfree(old_passphrase);
483 if (private == NULL) {
484 printf("Bad passphrase.\n");
488 printf("Key has comment '%s'\n", comment);
490 /* Ask the new passphrase (twice). */
491 if (identity_new_passphrase) {
492 passphrase1 = xstrdup(identity_new_passphrase);
496 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
497 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
499 /* Verify that they are the same. */
500 if (strcmp(passphrase1, passphrase2) != 0) {
501 memset(passphrase1, 0, strlen(passphrase1));
502 memset(passphrase2, 0, strlen(passphrase2));
505 printf("Pass phrases do not match. Try again.\n");
508 /* Destroy the other copy. */
509 memset(passphrase2, 0, strlen(passphrase2));
513 /* Save the file using the new passphrase. */
514 if (!key_save_private(private, identity_file, passphrase1, comment)) {
515 printf("Saving the key failed: %s: %s.\n",
516 identity_file, strerror(errno));
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: %s.\n",
595 identity_file, strerror(errno));
596 memset(passphrase, 0, strlen(passphrase));
602 memset(passphrase, 0, strlen(passphrase));
604 public = key_from_private(private);
607 strlcat(identity_file, ".pub", sizeof(identity_file));
608 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
610 printf("Could not save your public key in %s\n", identity_file);
615 printf("fdopen %s failed", identity_file);
618 if (!key_write(public, f))
619 fprintf(stderr, "write key failed");
621 fprintf(f, " %s\n", new_comment);
626 printf("The comment in your key file has been changed.\n");
633 printf("Usage: %s [-lBpqxXyc] [-t type] [-b bits] [-f file] [-C comment] "
634 "[-N new-pass] [-P pass]\n", __progname);
639 * Main program for key management.
642 main(int ac, char **av)
644 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
645 Key *private, *public;
654 __progname = get_progname(av[0]);
658 SSLeay_add_all_algorithms();
660 /* we need this for the home * directory. */
661 pw = getpwuid(getuid());
663 printf("You don't exist, go away!\n");
666 if (gethostname(hostname, sizeof(hostname)) < 0) {
667 perror("gethostname");
671 while ((opt = getopt(ac, av, "dqpclBRxXyb:f:t:P:N:C:")) != -1) {
675 if (bits < 512 || bits > 32768) {
676 printf("Bits has bad value.\n");
682 print_fingerprint = 1;
686 print_bubblebabble = 1;
690 change_passphrase = 1;
698 strlcpy(identity_file, optarg, sizeof(identity_file));
703 identity_passphrase = optarg;
707 identity_new_passphrase = optarg;
711 identity_comment = optarg;
728 convert_from_ssh2 = 1;
736 key_type_name = "dsa";
740 key_type_name = optarg;
749 printf("Too many arguments.\n");
752 if (change_passphrase && change_comment) {
753 printf("Can only have one of -p and -c.\n");
756 if (print_fingerprint || print_bubblebabble)
758 if (change_passphrase)
759 do_change_passphrase(pw);
761 do_change_comment(pw);
763 do_convert_to_ssh2(pw);
764 if (convert_from_ssh2)
765 do_convert_from_ssh2(pw);
771 type = key_type_from_name(key_type_name);
772 if (type == KEY_UNSPEC) {
773 fprintf(stderr, "unknown key type %s\n", key_type_name);
777 printf("Generating public/private %s key pair.\n", key_type_name);
778 private = key_generate(type, bits);
779 if (private == NULL) {
780 fprintf(stderr, "key_generate failed");
783 public = key_from_private(private);
786 ask_filename(pw, "Enter file in which to save the key");
788 /* Create ~/.ssh directory if it doesn\'t already exist. */
789 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
790 if (strstr(identity_file, dotsshdir) != NULL &&
791 stat(dotsshdir, &st) < 0) {
792 if (mkdir(dotsshdir, 0700) < 0)
793 error("Could not create directory '%s'.", dotsshdir);
795 printf("Created directory '%s'.\n", dotsshdir);
797 /* If the file already exists, ask the user to confirm. */
798 if (stat(identity_file, &st) >= 0) {
800 printf("%s already exists.\n", identity_file);
801 printf("Overwrite (y/n)? ");
803 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
805 if (yesno[0] != 'y' && yesno[0] != 'Y')
808 /* Ask for a passphrase (twice). */
809 if (identity_passphrase)
810 passphrase1 = xstrdup(identity_passphrase);
811 else if (identity_new_passphrase)
812 passphrase1 = xstrdup(identity_new_passphrase);
816 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
817 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
818 if (strcmp(passphrase1, passphrase2) != 0) {
819 /* The passphrases do not match. Clear them and retry. */
820 memset(passphrase1, 0, strlen(passphrase1));
821 memset(passphrase2, 0, strlen(passphrase2));
824 printf("Passphrases do not match. Try again.\n");
825 goto passphrase_again;
827 /* Clear the other copy of the passphrase. */
828 memset(passphrase2, 0, strlen(passphrase2));
832 if (identity_comment) {
833 strlcpy(comment, identity_comment, sizeof(comment));
835 /* Create default commend field for the passphrase. */
836 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
839 /* Save the key with the given passphrase and comment. */
840 if (!key_save_private(private, identity_file, passphrase1, comment)) {
841 printf("Saving the key failed: %s: %s.\n",
842 identity_file, strerror(errno));
843 memset(passphrase1, 0, strlen(passphrase1));
847 /* Clear the passphrase. */
848 memset(passphrase1, 0, strlen(passphrase1));
851 /* Clear the private key and the random number generator. */
856 printf("Your identification has been saved in %s.\n", identity_file);
858 strlcat(identity_file, ".pub", sizeof(identity_file));
859 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
861 printf("Could not save your public key in %s\n", identity_file);
866 printf("fdopen %s failed", identity_file);
869 if (!key_write(public, f))
870 fprintf(stderr, "write key failed");
871 fprintf(f, " %s\n", comment);
875 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
876 printf("Your public key has been saved in %s.\n",
878 printf("The key fingerprint is:\n");
879 printf("%s %s\n", fp, comment);