2 * cmdgen.c - command-line form of PuTTYgen
5 #define PUTTY_DO_GLOBALS
21 * This section overrides some definitions below for test purposes.
22 * When compiled with -DTEST_CMDGEN (as cgtest.c will do):
24 * - Calls to get_random_data() are replaced with the diagnostic
25 * function below (I #define the name so that I can still link
26 * with the original set of modules without symbol clash), in
27 * order to avoid depleting the test system's /dev/random
30 * - Calls to console_get_userpass_input() are replaced with the
31 * diagnostic function below, so that I can run tests in an
32 * automated manner and provide their interactive passphrase
35 * - main() is renamed to cmdgen_main(); at the bottom of the file
36 * I define another main() which calls the former repeatedly to
39 #define get_random_data get_random_data_diagnostic
40 char *get_random_data(int len, const char *device)
42 char *buf = snewn(len, char);
43 memset(buf, 'x', len);
46 #define console_get_userpass_input console_get_userpass_input_diagnostic
47 int nprompts, promptsgot;
48 const char *prompts[3];
49 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
53 for (i = 0; i < p->n_prompts; i++) {
54 if (promptsgot < nprompts) {
55 p->prompts[i]->result = dupstr(prompts[promptsgot++]);
57 promptsgot++; /* track number of requests anyway */
63 #define main cmdgen_main
70 static void progress_update(void *param, int action, int phase, int iprogress)
72 struct progress *p = (struct progress *)param;
73 if (action != PROGFN_PROGRESS)
75 if (phase > p->phase) {
80 p->current = iprogress - 1;
82 p->current = iprogress;
84 while (p->current < iprogress) {
91 static void no_progress(void *param, int action, int phase, int iprogress)
95 void modalfatalbox(const char *p, ...)
98 fprintf(stderr, "FATAL ERROR: ");
100 vfprintf(stderr, p, ap);
106 void nonfatal(const char *p, ...)
109 fprintf(stderr, "ERROR: ");
111 vfprintf(stderr, p, ap);
117 * Stubs to let everything else link sensibly.
119 void log_eventlog(void *handle, const char *event)
122 char *x_get_default(const char *key)
126 void sk_cleanup(void)
130 void showversion(void)
132 printf("puttygen: %s\n", ver);
135 void usage(int standalone)
138 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
139 " [ -C comment ] [ -P ] [ -q ]\n"
140 " [ -o output-keyfile ] [ -O type | -l | -L"
144 "Use \"puttygen --help\" for more detail.\n");
150 * Help message is an extended version of the usage message. So
151 * start with that, plus a version heading.
156 " -t specify key type when generating (ed25519, ecdsa, rsa, "
158 " -b specify number of bits when generating key\n"
159 " -C change or specify key comment\n"
160 " -P change key passphrase\n"
161 " -q quiet: do not display progress bar\n"
162 " -O specify output type:\n"
163 " private output PuTTY private key format\n"
164 " private-openssh export OpenSSH private key\n"
165 " private-openssh-new export OpenSSH private key "
166 "(force new file format)\n"
167 " private-sshcom export ssh.com private key\n"
168 " public RFC 4716 / ssh.com public key\n"
169 " public-openssh OpenSSH public key\n"
170 " fingerprint output the key fingerprint\n"
171 " -o specify output file\n"
172 " -l equivalent to `-O fingerprint'\n"
173 " -L equivalent to `-O public-openssh'\n"
174 " -p equivalent to `-O public'\n"
175 " --old-passphrase file\n"
176 " specify file containing old key passphrase\n"
177 " --new-passphrase file\n"
178 " specify file containing new key passphrase\n"
179 " --random-device device\n"
180 " specify device to read entropy from (e.g. /dev/urandom)\n"
184 static int move(char *from, char *to)
188 ret = rename(from, to);
191 * This OS may require us to remove the original file first.
194 ret = rename(from, to);
197 perror("puttygen: cannot move new file on to old one");
203 static char *readpassphrase(const char *filename)
208 fp = fopen(filename, "r");
210 fprintf(stderr, "puttygen: cannot open %s: %s\n",
211 filename, strerror(errno));
216 line[strcspn(line, "\r\n")] = '\0';
218 fprintf(stderr, "puttygen: error reading from %s: %s\n",
219 filename, strerror(errno));
220 else /* empty file */
226 int main(int argc, char **argv)
229 Filename *infilename = NULL, *outfilename = NULL;
230 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
231 char *outfile = NULL, *outfiletmp = NULL;
232 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
233 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
235 char *comment = NULL, *origcomment = NULL;
236 int change_passphrase = FALSE;
237 int errs = FALSE, nogo = FALSE;
238 int intype = SSH_KEYTYPE_UNOPENABLE;
240 struct ssh2_userkey *ssh2key = NULL;
241 struct RSAKey *ssh1key = NULL;
242 unsigned char *ssh2blob = NULL;
243 char *ssh2alg = NULL;
244 const struct ssh_signkey *ssh2algf = NULL;
246 char *old_passphrase = NULL, *new_passphrase = NULL;
248 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
249 const char *random_device = NULL;
251 /* ------------------------------------------------------------------
252 * Parse the command line to figure out what we've been asked to do.
256 * If run with no arguments at all, print the usage message and
265 * Parse command line arguments.
282 opt = p++; /* opt will have _one_ leading - */
283 while (*p && *p != '=')
284 p++; /* find end of option */
291 if (!strcmp(opt, "-help")) {
294 fprintf(stderr, "puttygen: option `-%s'"
295 " expects no argument\n", opt);
300 } else if (!strcmp(opt, "-version")) {
303 fprintf(stderr, "puttygen: option `-%s'"
304 " expects no argument\n", opt);
309 } else if (!strcmp(opt, "-pgpfp")) {
312 fprintf(stderr, "puttygen: option `-%s'"
313 " expects no argument\n", opt);
315 /* support --pgpfp for consistency */
319 } else if (!strcmp(opt, "-old-passphrase")) {
320 if (!val && argc > 1)
321 --argc, val = *++argv;
324 fprintf(stderr, "puttygen: option `-%s'"
325 " expects an argument\n", opt);
327 old_passphrase = readpassphrase(val);
331 } else if (!strcmp(opt, "-new-passphrase")) {
332 if (!val && argc > 1)
333 --argc, val = *++argv;
336 fprintf(stderr, "puttygen: option `-%s'"
337 " expects an argument\n", opt);
339 new_passphrase = readpassphrase(val);
343 } else if (!strcmp(opt, "-random-device")) {
344 if (!val && argc > 1)
345 --argc, val = *++argv;
348 fprintf(stderr, "puttygen: option `-%s'"
349 " expects an argument\n", opt);
356 "puttygen: no such option `-%s'\n", opt);
369 * Option requiring no parameter.
381 change_passphrase = TRUE;
393 progressfn = no_progress;
403 * Option requiring parameter.
409 fprintf(stderr, "puttygen: option `-%c' expects a"
414 * Now c is the option and p is the parameter.
418 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
419 keytype = RSA2, sshver = 2;
420 else if (!strcmp(p, "rsa1"))
421 keytype = RSA1, sshver = 1;
422 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
423 keytype = DSA, sshver = 2;
424 else if (!strcmp(p, "ecdsa"))
425 keytype = ECDSA, sshver = 2;
426 else if (!strcmp(p, "ed25519"))
427 keytype = ED25519, sshver = 2;
430 "puttygen: unknown key type `%s'\n", p);
441 if (!strcmp(p, "public"))
443 else if (!strcmp(p, "public-openssh"))
445 else if (!strcmp(p, "private"))
447 else if (!strcmp(p, "fingerprint"))
449 else if (!strcmp(p, "private-openssh"))
450 outtype = OPENSSH_AUTO, sshver = 2;
451 else if (!strcmp(p, "private-openssh-new"))
452 outtype = OPENSSH_NEW, sshver = 2;
453 else if (!strcmp(p, "private-sshcom"))
454 outtype = SSHCOM, sshver = 2;
457 "puttygen: unknown output type `%s'\n", p);
465 p = NULL; /* prevent continued processing */
469 * Unrecognised option.
472 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
478 * A non-option argument.
484 fprintf(stderr, "puttygen: cannot handle more than one"
492 * No explicit key size was specified. Default varies
493 * depending on key type.
508 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
509 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
513 if (keytype == ED25519 && (bits != 256)) {
514 fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
525 * If run with at least one argument _but_ not the required
526 * ones, print the usage message and return failure.
528 if (!infile && keytype == NOKEYGEN) {
533 /* ------------------------------------------------------------------
534 * Figure out further details of exactly what we're going to do.
538 * Bomb out if we've been asked to both load and generate a
541 if (keytype != NOKEYGEN && infile) {
542 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
547 * We must save the private part when generating a new key.
549 if (keytype != NOKEYGEN &&
550 (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
551 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
552 fprintf(stderr, "puttygen: this would generate a new key but "
553 "discard the private part\n");
558 * Analyse the type of the input file, in case this affects our
562 infilename = filename_from_str(infile);
564 intype = key_type(infilename);
567 case SSH_KEYTYPE_UNOPENABLE:
568 case SSH_KEYTYPE_UNKNOWN:
569 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
570 infile, key_type_to_str(intype));
573 case SSH_KEYTYPE_SSH1:
574 case SSH_KEYTYPE_SSH1_PUBLIC:
576 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
583 case SSH_KEYTYPE_SSH2:
584 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
585 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
586 case SSH_KEYTYPE_OPENSSH_PEM:
587 case SSH_KEYTYPE_OPENSSH_NEW:
588 case SSH_KEYTYPE_SSHCOM:
590 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
597 case SSH_KEYTYPE_OPENSSH_AUTO:
599 assert(0 && "Should never see these types on an input file");
604 * Determine the default output file, if none is provided.
606 * This will usually be equal to stdout, except that if the
607 * input and output file formats are the same then the default
608 * output is to overwrite the input.
610 * Also in this code, we bomb out if the input and output file
611 * formats are the same and no other action is performed.
613 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
614 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
615 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
616 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
617 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
620 outfiletmp = dupcat(outfile, ".tmp", NULL);
623 if (!change_passphrase && !comment) {
624 fprintf(stderr, "puttygen: this command would perform no useful"
631 * Bomb out rather than automatically choosing to write
632 * a private key file to stdout.
634 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
635 outtype == OPENSSH_NEW || outtype == SSHCOM) {
636 fprintf(stderr, "puttygen: need to specify an output file\n");
643 * Figure out whether we need to load the encrypted part of the
644 * key. This will be the case if either (a) we need to write
645 * out a private key format, or (b) the entire input key file
648 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
649 outtype == OPENSSH_NEW || outtype == SSHCOM ||
650 intype == SSH_KEYTYPE_OPENSSH_PEM ||
651 intype == SSH_KEYTYPE_OPENSSH_NEW ||
652 intype == SSH_KEYTYPE_SSHCOM)
653 load_encrypted = TRUE;
655 load_encrypted = FALSE;
657 if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
658 intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
659 intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
660 fprintf(stderr, "puttygen: cannot perform this action on a "
661 "public-key-only input file\n");
665 /* ------------------------------------------------------------------
666 * Now we're ready to actually do some stuff.
670 * Either load or generate a key.
672 if (keytype != NOKEYGEN) {
674 char default_comment[80];
676 struct progress prog;
683 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
684 else if (keytype == ECDSA)
685 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
686 else if (keytype == ED25519)
687 strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
689 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
692 entropy = get_random_data(bits / 8, random_device);
694 fprintf(stderr, "puttygen: failed to collect entropy, "
695 "could not generate key\n");
698 random_add_heavynoise(entropy, bits / 8);
699 smemclr(entropy, bits/8);
702 if (keytype == DSA) {
703 struct dss_key *dsskey = snew(struct dss_key);
704 dsa_generate(dsskey, bits, progressfn, &prog);
705 ssh2key = snew(struct ssh2_userkey);
706 ssh2key->data = dsskey;
707 ssh2key->alg = &ssh_dss;
709 } else if (keytype == ECDSA) {
710 struct ec_key *ec = snew(struct ec_key);
711 ec_generate(ec, bits, progressfn, &prog);
712 ssh2key = snew(struct ssh2_userkey);
714 ssh2key->alg = ec->signalg;
716 } else if (keytype == ED25519) {
717 struct ec_key *ec = snew(struct ec_key);
718 ec_edgenerate(ec, bits, progressfn, &prog);
719 ssh2key = snew(struct ssh2_userkey);
721 ssh2key->alg = &ssh_ecdsa_ed25519;
724 struct RSAKey *rsakey = snew(struct RSAKey);
725 rsa_generate(rsakey, bits, progressfn, &prog);
726 rsakey->comment = NULL;
727 if (keytype == RSA1) {
730 ssh2key = snew(struct ssh2_userkey);
731 ssh2key->data = rsakey;
732 ssh2key->alg = &ssh_rsa;
735 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
738 ssh2key->comment = dupstr(default_comment);
740 ssh1key->comment = dupstr(default_comment);
743 const char *error = NULL;
746 assert(infile != NULL);
749 * Find out whether the input key is encrypted.
751 if (intype == SSH_KEYTYPE_SSH1)
752 encrypted = rsakey_encrypted(infilename, &origcomment);
753 else if (intype == SSH_KEYTYPE_SSH2)
754 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
756 encrypted = import_encrypted(infilename, intype, &origcomment);
759 * If so, ask for a passphrase.
761 if (encrypted && load_encrypted) {
762 if (!old_passphrase) {
763 prompts_t *p = new_prompts(NULL);
765 p->to_server = FALSE;
766 p->name = dupstr("SSH key passphrase");
767 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
768 ret = console_get_userpass_input(p, NULL, 0);
772 perror("puttygen: unable to read passphrase");
775 old_passphrase = dupstr(p->prompts[0]->result);
780 old_passphrase = NULL;
786 case SSH_KEYTYPE_SSH1:
787 case SSH_KEYTYPE_SSH1_PUBLIC:
788 ssh1key = snew(struct RSAKey);
789 if (!load_encrypted) {
794 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
795 &origcomment, &error);
796 blob = (unsigned char *)vblob;
798 n = 4; /* skip modulus bits */
800 l = ssh1_read_bignum(blob + n, bloblen - n,
803 error = "SSH-1 public key blob was too short";
806 l = ssh1_read_bignum(blob + n, bloblen - n,
809 error = "SSH-1 public key blob was too short";
813 ssh1key->comment = dupstr(origcomment);
814 ssh1key->private_exponent = NULL;
817 ssh1key->iqmp = NULL;
819 ret = loadrsakey(infilename, ssh1key, old_passphrase, &error);
824 error = "unknown error";
827 case SSH_KEYTYPE_SSH2:
828 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
829 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
830 if (!load_encrypted) {
831 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
832 &ssh2bloblen, &origcomment,
835 ssh2algf = find_pubkey_alg(ssh2alg);
837 bits = ssh2algf->pubkey_bits(ssh2algf,
838 ssh2blob, ssh2bloblen);
844 ssh2key = ssh2_load_userkey(infilename, old_passphrase,
847 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
850 if (ssh2key == SSH2_WRONG_PASSPHRASE)
851 error = "wrong passphrase";
853 error = "unknown error";
857 case SSH_KEYTYPE_OPENSSH_PEM:
858 case SSH_KEYTYPE_OPENSSH_NEW:
859 case SSH_KEYTYPE_SSHCOM:
860 ssh2key = import_ssh2(infilename, intype, old_passphrase, &error);
862 if (ssh2key != SSH2_WRONG_PASSPHRASE)
865 error = "wrong passphrase";
867 error = "unknown error";
875 fprintf(stderr, "puttygen: error loading `%s': %s\n",
882 * Change the comment if asked to.
887 sfree(ssh1key->comment);
888 ssh1key->comment = dupstr(comment);
891 sfree(ssh2key->comment);
892 ssh2key->comment = dupstr(comment);
897 * Unless we're changing the passphrase, the old one (if any) is a
898 * reasonable default.
900 if (!change_passphrase && old_passphrase && !new_passphrase)
901 new_passphrase = dupstr(old_passphrase);
904 * Prompt for a new passphrase if we have been asked to, or if
905 * we have just generated a key.
907 if (!new_passphrase && (change_passphrase || keytype != NOKEYGEN)) {
908 prompts_t *p = new_prompts(NULL);
911 p->to_server = FALSE;
912 p->name = dupstr("New SSH key passphrase");
913 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
914 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
915 ret = console_get_userpass_input(p, NULL, 0);
919 perror("puttygen: unable to read new passphrase");
922 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
924 fprintf(stderr, "puttygen: passphrases do not match\n");
927 new_passphrase = dupstr(p->prompts[0]->result);
931 if (new_passphrase && !*new_passphrase) {
932 sfree(new_passphrase);
933 new_passphrase = NULL;
939 * (In the case where outfile and outfiletmp are both NULL,
940 * there is no semantic reason to initialise outfilename at
941 * all; but we have to write _something_ to it or some compiler
942 * will probably complain that it might be used uninitialised.)
945 outfilename = filename_from_str(outfiletmp);
947 outfilename = filename_from_str(outfile ? outfile : "");
950 int ret, real_outtype;
955 ret = saversakey(outfilename, ssh1key, new_passphrase);
957 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
962 ret = ssh2_save_userkey(outfilename, ssh2key, new_passphrase);
964 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
969 if (!move(outfiletmp, outfile))
970 return 1; /* rename failed */
980 fp = f_open(outfilename, "w", FALSE);
985 ssh1_write_pubkey(fp, ssh1key);
989 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
993 ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
994 ssh2blob, ssh2bloblen,
996 SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
997 SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
1012 fingerprint = snewn(128, char);
1013 rsa_fingerprint(fingerprint, 128, ssh1key);
1016 fingerprint = ssh2_fingerprint(ssh2key->alg,
1020 fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen);
1025 fp = f_open(outfilename, "w", FALSE);
1028 fprintf(fp, "%s\n", fingerprint);
1039 assert(sshver == 2);
1041 random_ref(); /* both foreign key types require randomness,
1042 * for IV or padding */
1045 real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
1048 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1051 real_outtype = SSH_KEYTYPE_SSHCOM;
1054 assert(0 && "control flow goof");
1056 ret = export_ssh2(outfilename, real_outtype, ssh2key, new_passphrase);
1058 fprintf(stderr, "puttygen: unable to export key\n");
1062 if (!move(outfiletmp, outfile))
1063 return 1; /* rename failed */
1068 if (old_passphrase) {
1069 smemclr(old_passphrase, strlen(old_passphrase));
1070 sfree(old_passphrase);
1072 if (new_passphrase) {
1073 smemclr(new_passphrase, strlen(new_passphrase));
1074 sfree(new_passphrase);
1078 freersakey(ssh1key);
1080 ssh2key->alg->freekey(ssh2key->data);
1095 void setup_passphrases(char *first, ...)
1102 prompts[nprompts++] = first;
1103 va_start(ap, first);
1104 while ((next = va_arg(ap, char *)) != NULL) {
1105 assert(nprompts < lenof(prompts));
1106 prompts[nprompts++] = next;
1112 void test(int retval, ...)
1119 va_start(ap, retval);
1120 while (va_arg(ap, char *) != NULL)
1124 argv = snewn(argc+1, char *);
1125 va_start(ap, retval);
1126 for (i = 0; i <= argc; i++)
1127 argv[i] = va_arg(ap, char *);
1131 ret = cmdgen_main(argc, argv);
1133 if (ret != retval) {
1134 printf("FAILED retval (exp %d got %d):", retval, ret);
1135 for (i = 0; i < argc; i++)
1136 printf(" %s", argv[i]);
1139 } else if (promptsgot != nprompts) {
1140 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1141 for (i = 0; i < argc; i++)
1142 printf(" %s", argv[i]);
1150 void filecmp(char *file1, char *file2, char *fmt, ...)
1153 * Ideally I should do file comparison myself, to maximise the
1154 * portability of this test suite once this application begins
1155 * running on non-Unix platforms. For the moment, though,
1156 * calling Unix diff is perfectly adequate.
1161 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1168 printf("FAILED diff (ret=%d): ", ret);
1181 char *cleanup_fp(char *s)
1185 if (!strncmp(s, "ssh-", 4)) {
1186 s += strcspn(s, " \n\t");
1187 s += strspn(s, " \n\t");
1191 s += strcspn(s, " \n\t");
1192 s += strspn(s, " \n\t");
1193 s += strcspn(s, " \n\t");
1195 return dupprintf("%.*s", (int)(s - p), p);
1198 char *get_fp(char *filename)
1201 char buf[256], *ret;
1203 fp = fopen(filename, "r");
1206 ret = fgets(buf, sizeof(buf), fp);
1210 return cleanup_fp(buf);
1213 void check_fp(char *filename, char *fp, char *fmt, ...)
1220 newfp = get_fp(filename);
1222 if (!strcmp(fp, newfp)) {
1227 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1241 int main(int argc, char **argv)
1244 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1247 * Even when this thing is compiled for automatic test mode,
1248 * it's helpful to be able to invoke it with command-line
1249 * options for _manual_ tests.
1252 return cmdgen_main(argc, argv);
1256 for (i = 0; i < lenof(keytypes); i++) {
1257 char filename[128], osfilename[128], scfilename[128];
1258 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1261 sprintf(filename, "test-%s.ppk", keytypes[i]);
1262 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1263 sprintf(osfilename, "test-%s.os", keytypes[i]);
1264 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1265 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1266 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1269 * Create an encrypted key.
1271 setup_passphrases("sponge", "sponge", NULL);
1272 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1275 * List the public key in OpenSSH format.
1277 setup_passphrases(NULL);
1278 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1282 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1283 pubfilename, tmpfilename1);
1284 if (system(cmdbuf) ||
1285 (fp = get_fp(tmpfilename1)) == NULL) {
1286 printf("UNABLE to test fingerprint matching against OpenSSH");
1291 * List the public key in IETF/ssh.com format.
1293 setup_passphrases(NULL);
1294 test(0, "puttygen", "-p", filename, NULL);
1297 * List the fingerprint of the key.
1299 setup_passphrases(NULL);
1300 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1303 * If we can't test fingerprints against OpenSSH, we
1304 * can at the very least test equality of all the
1305 * fingerprints we generate of this key throughout
1308 fp = get_fp(tmpfilename1);
1310 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1314 * Change the comment of the key; this _does_ require a
1315 * passphrase owing to the tamperproofing.
1317 * NOTE: In SSH-1, this only requires a passphrase because
1318 * of inadequacies of the loading and saving mechanisms. In
1319 * _principle_, it should be perfectly possible to modify
1320 * the comment on an SSH-1 key without requiring a
1321 * passphrase; the only reason I can't do it is because my
1322 * loading and saving mechanisms don't include a method of
1323 * loading all the key data without also trying to decrypt
1324 * the private section.
1326 * I don't consider this to be a problem worth solving,
1327 * because (a) to fix it would probably end up bloating
1328 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1329 * it shouldn't be highly significant. If it seriously
1330 * bothers anyone then perhaps I _might_ be persuadable.
1332 setup_passphrases("sponge", NULL);
1333 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1336 * Change the passphrase to nothing.
1338 setup_passphrases("sponge", "", "", NULL);
1339 test(0, "puttygen", "-P", filename, NULL);
1342 * Change the comment of the key again; this time we expect no
1343 * passphrase to be required.
1345 setup_passphrases(NULL);
1346 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1349 * Export the private key into OpenSSH format; no passphrase
1350 * should be required since the key is currently unencrypted.
1351 * For RSA1 keys, this should give an error.
1353 setup_passphrases(NULL);
1354 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1359 * List the fingerprint of the OpenSSH-formatted key.
1361 setup_passphrases(NULL);
1362 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1363 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1366 * List the public half of the OpenSSH-formatted key in
1369 setup_passphrases(NULL);
1370 test(0, "puttygen", "-L", osfilename, NULL);
1373 * List the public half of the OpenSSH-formatted key in
1374 * IETF/ssh.com format.
1376 setup_passphrases(NULL);
1377 test(0, "puttygen", "-p", osfilename, NULL);
1381 * Export the private key into ssh.com format; no passphrase
1382 * should be required since the key is currently unencrypted.
1383 * For RSA1 keys, this should give an error.
1385 setup_passphrases(NULL);
1386 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1391 * List the fingerprint of the ssh.com-formatted key.
1393 setup_passphrases(NULL);
1394 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1395 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1398 * List the public half of the ssh.com-formatted key in
1401 setup_passphrases(NULL);
1402 test(0, "puttygen", "-L", scfilename, NULL);
1405 * List the public half of the ssh.com-formatted key in
1406 * IETF/ssh.com format.
1408 setup_passphrases(NULL);
1409 test(0, "puttygen", "-p", scfilename, NULL);
1414 * Convert from OpenSSH into ssh.com.
1416 setup_passphrases(NULL);
1417 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1418 "-O", "private-sshcom", NULL);
1421 * Convert from ssh.com back into a PuTTY key,
1422 * supplying the same comment as we had before we
1423 * started to ensure the comparison works.
1425 setup_passphrases(NULL);
1426 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1427 "-o", tmpfilename2, NULL);
1430 * See if the PuTTY key thus generated is the same as
1433 filecmp(filename, tmpfilename2,
1434 "p->o->s->p clear %s", keytypes[i]);
1437 * Convert from ssh.com to OpenSSH.
1439 setup_passphrases(NULL);
1440 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1441 "-O", "private-openssh", NULL);
1444 * Convert from OpenSSH back into a PuTTY key,
1445 * supplying the same comment as we had before we
1446 * started to ensure the comparison works.
1448 setup_passphrases(NULL);
1449 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1450 "-o", tmpfilename2, NULL);
1453 * See if the PuTTY key thus generated is the same as
1456 filecmp(filename, tmpfilename2,
1457 "p->s->o->p clear %s", keytypes[i]);
1460 * Finally, do a round-trip conversion between PuTTY
1461 * and ssh.com without involving OpenSSH, to test that
1462 * the key comment is preserved in that case.
1464 setup_passphrases(NULL);
1465 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1467 setup_passphrases(NULL);
1468 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1469 filecmp(filename, tmpfilename2,
1470 "p->s->p clear %s", keytypes[i]);
1474 * Check that mismatched passphrases cause an error.
1476 setup_passphrases("sponge2", "sponge3", NULL);
1477 test(1, "puttygen", "-P", filename, NULL);
1480 * Put a passphrase back on.
1482 setup_passphrases("sponge2", "sponge2", NULL);
1483 test(0, "puttygen", "-P", filename, NULL);
1486 * Export the private key into OpenSSH format, this time
1487 * while encrypted. For RSA1 keys, this should give an
1491 setup_passphrases(NULL); /* error, hence no passphrase read */
1493 setup_passphrases("sponge2", NULL);
1494 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1499 * List the fingerprint of the OpenSSH-formatted key.
1501 setup_passphrases("sponge2", NULL);
1502 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1503 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1506 * List the public half of the OpenSSH-formatted key in
1509 setup_passphrases("sponge2", NULL);
1510 test(0, "puttygen", "-L", osfilename, NULL);
1513 * List the public half of the OpenSSH-formatted key in
1514 * IETF/ssh.com format.
1516 setup_passphrases("sponge2", NULL);
1517 test(0, "puttygen", "-p", osfilename, NULL);
1521 * Export the private key into ssh.com format, this time
1522 * while encrypted. For RSA1 keys, this should give an
1526 setup_passphrases(NULL); /* error, hence no passphrase read */
1528 setup_passphrases("sponge2", NULL);
1529 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1534 * List the fingerprint of the ssh.com-formatted key.
1536 setup_passphrases("sponge2", NULL);
1537 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1538 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1541 * List the public half of the ssh.com-formatted key in
1544 setup_passphrases("sponge2", NULL);
1545 test(0, "puttygen", "-L", scfilename, NULL);
1548 * List the public half of the ssh.com-formatted key in
1549 * IETF/ssh.com format.
1551 setup_passphrases("sponge2", NULL);
1552 test(0, "puttygen", "-p", scfilename, NULL);
1557 * Convert from OpenSSH into ssh.com.
1559 setup_passphrases("sponge2", NULL);
1560 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1561 "-O", "private-sshcom", NULL);
1564 * Convert from ssh.com back into a PuTTY key,
1565 * supplying the same comment as we had before we
1566 * started to ensure the comparison works.
1568 setup_passphrases("sponge2", NULL);
1569 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1570 "-o", tmpfilename2, NULL);
1573 * See if the PuTTY key thus generated is the same as
1576 filecmp(filename, tmpfilename2,
1577 "p->o->s->p encrypted %s", keytypes[i]);
1580 * Convert from ssh.com to OpenSSH.
1582 setup_passphrases("sponge2", NULL);
1583 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1584 "-O", "private-openssh", NULL);
1587 * Convert from OpenSSH back into a PuTTY key,
1588 * supplying the same comment as we had before we
1589 * started to ensure the comparison works.
1591 setup_passphrases("sponge2", NULL);
1592 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1593 "-o", tmpfilename2, NULL);
1596 * See if the PuTTY key thus generated is the same as
1599 filecmp(filename, tmpfilename2,
1600 "p->s->o->p encrypted %s", keytypes[i]);
1603 * Finally, do a round-trip conversion between PuTTY
1604 * and ssh.com without involving OpenSSH, to test that
1605 * the key comment is preserved in that case.
1607 setup_passphrases("sponge2", NULL);
1608 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1610 setup_passphrases("sponge2", NULL);
1611 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1612 filecmp(filename, tmpfilename2,
1613 "p->s->p encrypted %s", keytypes[i]);
1617 * Load with the wrong passphrase.
1619 setup_passphrases("sponge8", NULL);
1620 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1623 * Load a totally bogus file.
1625 setup_passphrases(NULL);
1626 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1628 printf("%d passes, %d fails\n", passes, fails);