2 * cmdgen.c - command-line form of PuTTYgen
5 #define PUTTY_DO_GLOBALS
19 * This section overrides some definitions below for test purposes.
20 * When compiled with -DTEST_CMDGEN:
22 * - Calls to get_random_data() are replaced with the diagnostic
23 * function below (I #define the name so that I can still link
24 * with the original set of modules without symbol clash), in
25 * order to avoid depleting the test system's /dev/random
28 * - Calls to console_get_userpass_input() are replaced with the
29 * diagnostic function below, so that I can run tests in an
30 * automated manner and provide their interactive passphrase
33 * - main() is renamed to cmdgen_main(); at the bottom of the file
34 * I define another main() which calls the former repeatedly to
37 #define get_random_data get_random_data_diagnostic
38 char *get_random_data(int len)
40 char *buf = snewn(len, char);
41 memset(buf, 'x', len);
44 #define console_get_userpass_input console_get_userpass_input_diagnostic
45 int nprompts, promptsgot;
46 const char *prompts[3];
47 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
51 for (i = 0; i < p->n_prompts; i++) {
52 if (promptsgot < nprompts) {
53 assert(strlen(prompts[promptsgot]) < p->prompts[i]->result_len);
54 strcpy(p->prompts[i]->result, prompts[promptsgot++]);
56 promptsgot++; /* track number of requests anyway */
62 #define main cmdgen_main
69 static void progress_update(void *param, int action, int phase, int iprogress)
71 struct progress *p = (struct progress *)param;
72 if (action != PROGFN_PROGRESS)
74 if (phase > p->phase) {
79 p->current = iprogress - 1;
81 p->current = iprogress;
83 while (p->current < iprogress) {
90 static void no_progress(void *param, int action, int phase, int iprogress)
94 void modalfatalbox(char *p, ...)
97 fprintf(stderr, "FATAL ERROR: ");
99 vfprintf(stderr, p, ap);
105 void nonfatal(char *p, ...)
108 fprintf(stderr, "ERROR: ");
110 vfprintf(stderr, p, ap);
116 * Stubs to let everything else link sensibly.
118 void log_eventlog(void *handle, const char *event)
121 char *x_get_default(const char *key)
125 void sk_cleanup(void)
129 void showversion(void)
131 printf("puttygen: %s\n", ver);
134 void usage(int standalone)
137 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
138 " [ -C comment ] [ -P ] [ -q ]\n"
139 " [ -o output-keyfile ] [ -O type | -l | -L"
143 "Use \"puttygen --help\" for more detail.\n");
149 * Help message is an extended version of the usage message. So
150 * start with that, plus a version heading.
155 " -t specify key type when generating (rsa, dsa, rsa1)\n"
156 " -b specify number of bits when generating key\n"
157 " -C change or specify key comment\n"
158 " -P change key passphrase\n"
159 " -q quiet: do not display progress bar\n"
160 " -O specify output type:\n"
161 " private output PuTTY private key format\n"
162 " private-openssh export OpenSSH private key\n"
163 " private-openssh-new export OpenSSH private key "
164 "(force new file format)\n"
165 " private-sshcom export ssh.com private key\n"
166 " public RFC 4716 / ssh.com public key\n"
167 " public-openssh OpenSSH public key\n"
168 " fingerprint output the key fingerprint\n"
169 " -o specify output file\n"
170 " -l equivalent to `-O fingerprint'\n"
171 " -L equivalent to `-O public-openssh'\n"
172 " -p equivalent to `-O public'\n"
176 static int move(char *from, char *to)
180 ret = rename(from, to);
183 * This OS may require us to remove the original file first.
186 ret = rename(from, to);
189 perror("puttygen: cannot move new file on to old one");
195 int main(int argc, char **argv)
198 Filename *infilename = NULL, *outfilename = NULL;
199 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
200 char *outfile = NULL, *outfiletmp = NULL;
201 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
202 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
204 char *comment = NULL, *origcomment = NULL;
205 int change_passphrase = FALSE;
206 int errs = FALSE, nogo = FALSE;
207 int intype = SSH_KEYTYPE_UNOPENABLE;
209 struct ssh2_userkey *ssh2key = NULL;
210 struct RSAKey *ssh1key = NULL;
211 unsigned char *ssh2blob = NULL;
212 char *ssh2alg = NULL;
213 const struct ssh_signkey *ssh2algf = NULL;
215 char *passphrase = NULL;
217 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
219 /* ------------------------------------------------------------------
220 * Parse the command line to figure out what we've been asked to do.
224 * If run with no arguments at all, print the usage message and
233 * Parse command line arguments.
250 opt = p++; /* opt will have _one_ leading - */
251 while (*p && *p != '=')
252 p++; /* find end of option */
259 if (!strcmp(opt, "-help")) {
262 fprintf(stderr, "puttygen: option `-%s'"
263 " expects no argument\n", opt);
268 } else if (!strcmp(opt, "-version")) {
271 fprintf(stderr, "puttygen: option `-%s'"
272 " expects no argument\n", opt);
277 } else if (!strcmp(opt, "-pgpfp")) {
280 fprintf(stderr, "puttygen: option `-%s'"
281 " expects no argument\n", opt);
283 /* support --pgpfp for consistency */
289 * For long options requiring an argument, add
290 * code along the lines of
292 * else if (!strcmp(opt, "-output")) {
295 * fprintf(stderr, "puttygen: option `-%s'"
296 * " expects an argument\n", opt);
304 "puttygen: no such option `-%s'\n", opt);
317 * Option requiring no parameter.
329 change_passphrase = TRUE;
341 progressfn = no_progress;
351 * Option requiring parameter.
357 fprintf(stderr, "puttygen: option `-%c' expects a"
362 * Now c is the option and p is the parameter.
366 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
367 keytype = RSA2, sshver = 2;
368 else if (!strcmp(p, "rsa1"))
369 keytype = RSA1, sshver = 1;
370 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
371 keytype = DSA, sshver = 2;
372 else if (!strcmp(p, "ecdsa"))
373 keytype = ECDSA, sshver = 2;
374 else if (!strcmp(p, "ed25519"))
375 keytype = ED25519, sshver = 2;
378 "puttygen: unknown key type `%s'\n", p);
389 if (!strcmp(p, "public"))
391 else if (!strcmp(p, "public-openssh"))
393 else if (!strcmp(p, "private"))
395 else if (!strcmp(p, "fingerprint"))
397 else if (!strcmp(p, "private-openssh"))
398 outtype = OPENSSH_AUTO, sshver = 2;
399 else if (!strcmp(p, "private-openssh-new"))
400 outtype = OPENSSH_NEW, sshver = 2;
401 else if (!strcmp(p, "private-sshcom"))
402 outtype = SSHCOM, sshver = 2;
405 "puttygen: unknown output type `%s'\n", p);
413 p = NULL; /* prevent continued processing */
417 * Unrecognised option.
420 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
426 * A non-option argument.
432 fprintf(stderr, "puttygen: cannot handle more than one"
440 * No explicit key size was specified. Default varies
441 * depending on key type.
456 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
457 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
461 if (keytype == ED25519 && (bits != 256)) {
462 fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
473 * If run with at least one argument _but_ not the required
474 * ones, print the usage message and return failure.
476 if (!infile && keytype == NOKEYGEN) {
481 /* ------------------------------------------------------------------
482 * Figure out further details of exactly what we're going to do.
486 * Bomb out if we've been asked to both load and generate a
489 if (keytype != NOKEYGEN && infile) {
490 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
495 * We must save the private part when generating a new key.
497 if (keytype != NOKEYGEN &&
498 (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
499 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
500 fprintf(stderr, "puttygen: this would generate a new key but "
501 "discard the private part\n");
506 * Analyse the type of the input file, in case this affects our
510 infilename = filename_from_str(infile);
512 intype = key_type(infilename);
515 case SSH_KEYTYPE_UNOPENABLE:
516 case SSH_KEYTYPE_UNKNOWN:
517 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
518 infile, key_type_to_str(intype));
521 case SSH_KEYTYPE_SSH1:
522 case SSH_KEYTYPE_SSH1_PUBLIC:
524 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
531 case SSH_KEYTYPE_SSH2:
532 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
533 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
534 case SSH_KEYTYPE_OPENSSH_PEM:
535 case SSH_KEYTYPE_OPENSSH_NEW:
536 case SSH_KEYTYPE_SSHCOM:
538 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
545 case SSH_KEYTYPE_OPENSSH_AUTO:
547 assert(0 && "Should never see these types on an input file");
552 * Determine the default output file, if none is provided.
554 * This will usually be equal to stdout, except that if the
555 * input and output file formats are the same then the default
556 * output is to overwrite the input.
558 * Also in this code, we bomb out if the input and output file
559 * formats are the same and no other action is performed.
561 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
562 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
563 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
564 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
565 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
568 outfiletmp = dupcat(outfile, ".tmp", NULL);
571 if (!change_passphrase && !comment) {
572 fprintf(stderr, "puttygen: this command would perform no useful"
579 * Bomb out rather than automatically choosing to write
580 * a private key file to stdout.
582 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
583 outtype == OPENSSH_NEW || outtype == SSHCOM) {
584 fprintf(stderr, "puttygen: need to specify an output file\n");
591 * Figure out whether we need to load the encrypted part of the
592 * key. This will be the case if either (a) we need to write
593 * out a private key format, or (b) the entire input key file
596 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
597 outtype == OPENSSH_NEW || outtype == SSHCOM ||
598 intype == SSH_KEYTYPE_OPENSSH_PEM ||
599 intype == SSH_KEYTYPE_OPENSSH_NEW ||
600 intype == SSH_KEYTYPE_SSHCOM)
601 load_encrypted = TRUE;
603 load_encrypted = FALSE;
605 if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
606 intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
607 intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
608 fprintf(stderr, "puttygen: cannot perform this action on a "
609 "public-key-only input file\n");
613 /* ------------------------------------------------------------------
614 * Now we're ready to actually do some stuff.
618 * Either load or generate a key.
620 if (keytype != NOKEYGEN) {
622 char default_comment[80];
624 struct progress prog;
631 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
632 else if (keytype == ECDSA)
633 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
634 else if (keytype == ED25519)
635 strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
637 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
640 entropy = get_random_data(bits / 8);
642 fprintf(stderr, "puttygen: failed to collect entropy, "
643 "could not generate key\n");
646 random_add_heavynoise(entropy, bits / 8);
647 smemclr(entropy, bits/8);
650 if (keytype == DSA) {
651 struct dss_key *dsskey = snew(struct dss_key);
652 dsa_generate(dsskey, bits, progressfn, &prog);
653 ssh2key = snew(struct ssh2_userkey);
654 ssh2key->data = dsskey;
655 ssh2key->alg = &ssh_dss;
657 } else if (keytype == ECDSA) {
658 struct ec_key *ec = snew(struct ec_key);
659 ec_generate(ec, bits, progressfn, &prog);
660 ssh2key = snew(struct ssh2_userkey);
663 ssh2key->alg = &ssh_ecdsa_nistp256;
664 } else if (bits == 384) {
665 ssh2key->alg = &ssh_ecdsa_nistp384;
667 ssh2key->alg = &ssh_ecdsa_nistp521;
670 } else if (keytype == ED25519) {
671 struct ec_key *ec = snew(struct ec_key);
672 ec_edgenerate(ec, bits, progressfn, &prog);
673 ssh2key = snew(struct ssh2_userkey);
675 ssh2key->alg = &ssh_ecdsa_ed25519;
678 struct RSAKey *rsakey = snew(struct RSAKey);
679 rsa_generate(rsakey, bits, progressfn, &prog);
680 rsakey->comment = NULL;
681 if (keytype == RSA1) {
684 ssh2key = snew(struct ssh2_userkey);
685 ssh2key->data = rsakey;
686 ssh2key->alg = &ssh_rsa;
689 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
692 ssh2key->comment = dupstr(default_comment);
694 ssh1key->comment = dupstr(default_comment);
697 const char *error = NULL;
700 assert(infile != NULL);
703 * Find out whether the input key is encrypted.
705 if (intype == SSH_KEYTYPE_SSH1)
706 encrypted = rsakey_encrypted(infilename, &origcomment);
707 else if (intype == SSH_KEYTYPE_SSH2)
708 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
710 encrypted = import_encrypted(infilename, intype, &origcomment);
713 * If so, ask for a passphrase.
715 if (encrypted && load_encrypted) {
716 prompts_t *p = new_prompts(NULL);
718 p->to_server = FALSE;
719 p->name = dupstr("SSH key passphrase");
720 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
721 ret = console_get_userpass_input(p, NULL, 0);
725 perror("puttygen: unable to read passphrase");
728 passphrase = dupstr(p->prompts[0]->result);
738 case SSH_KEYTYPE_SSH1:
739 case SSH_KEYTYPE_SSH1_PUBLIC:
740 ssh1key = snew(struct RSAKey);
741 if (!load_encrypted) {
746 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
747 &origcomment, &error);
748 blob = (unsigned char *)vblob;
750 n = 4; /* skip modulus bits */
752 l = ssh1_read_bignum(blob + n, bloblen - n,
755 error = "SSH-1 public key blob was too short";
758 l = ssh1_read_bignum(blob + n, bloblen - n,
761 error = "SSH-1 public key blob was too short";
765 ssh1key->comment = dupstr(origcomment);
766 ssh1key->private_exponent = NULL;
769 ssh1key->iqmp = NULL;
771 ret = loadrsakey(infilename, ssh1key, passphrase, &error);
776 error = "unknown error";
779 case SSH_KEYTYPE_SSH2:
780 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
781 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
782 if (!load_encrypted) {
783 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
784 &ssh2bloblen, &origcomment,
787 ssh2algf = find_pubkey_alg(ssh2alg);
789 bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
795 ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
797 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
800 if (ssh2key == SSH2_WRONG_PASSPHRASE)
801 error = "wrong passphrase";
803 error = "unknown error";
807 case SSH_KEYTYPE_OPENSSH_PEM:
808 case SSH_KEYTYPE_OPENSSH_NEW:
809 case SSH_KEYTYPE_SSHCOM:
810 ssh2key = import_ssh2(infilename, intype, passphrase, &error);
812 if (ssh2key != SSH2_WRONG_PASSPHRASE)
815 error = "wrong passphrase";
817 error = "unknown error";
825 fprintf(stderr, "puttygen: error loading `%s': %s\n",
832 * Change the comment if asked to.
837 sfree(ssh1key->comment);
838 ssh1key->comment = dupstr(comment);
841 sfree(ssh2key->comment);
842 ssh2key->comment = dupstr(comment);
847 * Prompt for a new passphrase if we have been asked to, or if
848 * we have just generated a key.
850 if (change_passphrase || keytype != NOKEYGEN) {
851 prompts_t *p = new_prompts(NULL);
854 p->to_server = FALSE;
855 p->name = dupstr("New SSH key passphrase");
856 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
857 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
858 ret = console_get_userpass_input(p, NULL, 0);
862 perror("puttygen: unable to read new passphrase");
865 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
867 fprintf(stderr, "puttygen: passphrases do not match\n");
871 smemclr(passphrase, strlen(passphrase));
874 passphrase = dupstr(p->prompts[0]->result);
886 * (In the case where outfile and outfiletmp are both NULL,
887 * there is no semantic reason to initialise outfilename at
888 * all; but we have to write _something_ to it or some compiler
889 * will probably complain that it might be used uninitialised.)
892 outfilename = filename_from_str(outfiletmp);
894 outfilename = filename_from_str(outfile ? outfile : "");
897 int ret, real_outtype;
902 ret = saversakey(outfilename, ssh1key, passphrase);
904 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
909 ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
911 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
916 if (!move(outfiletmp, outfile))
917 return 1; /* rename failed */
927 fp = f_open(outfilename, "w", FALSE);
932 ssh1_write_pubkey(fp, ssh1key);
936 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
940 ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
941 ssh2blob, ssh2bloblen,
943 SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
944 SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
959 fingerprint = snewn(128, char);
960 rsa_fingerprint(fingerprint, 128, ssh1key);
963 fingerprint = ssh2_fingerprint(ssh2key->alg,
967 fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen);
972 fp = f_open(outfilename, "w", FALSE);
975 fprintf(fp, "%s\n", fingerprint);
988 random_ref(); /* both foreign key types require randomness,
989 * for IV or padding */
992 real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
995 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
998 real_outtype = SSH_KEYTYPE_SSHCOM;
1001 assert(0 && "control flow goof");
1003 ret = export_ssh2(outfilename, real_outtype, ssh2key, passphrase);
1005 fprintf(stderr, "puttygen: unable to export key\n");
1009 if (!move(outfiletmp, outfile))
1010 return 1; /* rename failed */
1016 smemclr(passphrase, strlen(passphrase));
1021 freersakey(ssh1key);
1023 ssh2key->alg->freekey(ssh2key->data);
1038 void setup_passphrases(char *first, ...)
1045 prompts[nprompts++] = first;
1046 va_start(ap, first);
1047 while ((next = va_arg(ap, char *)) != NULL) {
1048 assert(nprompts < lenof(prompts));
1049 prompts[nprompts++] = next;
1055 void test(int retval, ...)
1062 va_start(ap, retval);
1063 while (va_arg(ap, char *) != NULL)
1067 argv = snewn(argc+1, char *);
1068 va_start(ap, retval);
1069 for (i = 0; i <= argc; i++)
1070 argv[i] = va_arg(ap, char *);
1074 ret = cmdgen_main(argc, argv);
1076 if (ret != retval) {
1077 printf("FAILED retval (exp %d got %d):", retval, ret);
1078 for (i = 0; i < argc; i++)
1079 printf(" %s", argv[i]);
1082 } else if (promptsgot != nprompts) {
1083 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1084 for (i = 0; i < argc; i++)
1085 printf(" %s", argv[i]);
1093 void filecmp(char *file1, char *file2, char *fmt, ...)
1096 * Ideally I should do file comparison myself, to maximise the
1097 * portability of this test suite once this application begins
1098 * running on non-Unix platforms. For the moment, though,
1099 * calling Unix diff is perfectly adequate.
1104 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1111 printf("FAILED diff (ret=%d): ", ret);
1124 char *cleanup_fp(char *s)
1128 if (!strncmp(s, "ssh-", 4)) {
1129 s += strcspn(s, " \n\t");
1130 s += strspn(s, " \n\t");
1134 s += strcspn(s, " \n\t");
1135 s += strspn(s, " \n\t");
1136 s += strcspn(s, " \n\t");
1138 return dupprintf("%.*s", s - p, p);
1141 char *get_fp(char *filename)
1144 char buf[256], *ret;
1146 fp = fopen(filename, "r");
1149 ret = fgets(buf, sizeof(buf), fp);
1153 return cleanup_fp(buf);
1156 void check_fp(char *filename, char *fp, char *fmt, ...)
1163 newfp = get_fp(filename);
1165 if (!strcmp(fp, newfp)) {
1170 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1184 int main(int argc, char **argv)
1187 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1190 * Even when this thing is compiled for automatic test mode,
1191 * it's helpful to be able to invoke it with command-line
1192 * options for _manual_ tests.
1195 return cmdgen_main(argc, argv);
1199 for (i = 0; i < lenof(keytypes); i++) {
1200 char filename[128], osfilename[128], scfilename[128];
1201 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1204 sprintf(filename, "test-%s.ppk", keytypes[i]);
1205 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1206 sprintf(osfilename, "test-%s.os", keytypes[i]);
1207 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1208 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1209 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1212 * Create an encrypted key.
1214 setup_passphrases("sponge", "sponge", NULL);
1215 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1218 * List the public key in OpenSSH format.
1220 setup_passphrases(NULL);
1221 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1225 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1226 pubfilename, tmpfilename1);
1227 if (system(cmdbuf) ||
1228 (fp = get_fp(tmpfilename1)) == NULL) {
1229 printf("UNABLE to test fingerprint matching against OpenSSH");
1234 * List the public key in IETF/ssh.com format.
1236 setup_passphrases(NULL);
1237 test(0, "puttygen", "-p", filename, NULL);
1240 * List the fingerprint of the key.
1242 setup_passphrases(NULL);
1243 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1246 * If we can't test fingerprints against OpenSSH, we
1247 * can at the very least test equality of all the
1248 * fingerprints we generate of this key throughout
1251 fp = get_fp(tmpfilename1);
1253 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1257 * Change the comment of the key; this _does_ require a
1258 * passphrase owing to the tamperproofing.
1260 * NOTE: In SSH-1, this only requires a passphrase because
1261 * of inadequacies of the loading and saving mechanisms. In
1262 * _principle_, it should be perfectly possible to modify
1263 * the comment on an SSH-1 key without requiring a
1264 * passphrase; the only reason I can't do it is because my
1265 * loading and saving mechanisms don't include a method of
1266 * loading all the key data without also trying to decrypt
1267 * the private section.
1269 * I don't consider this to be a problem worth solving,
1270 * because (a) to fix it would probably end up bloating
1271 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1272 * it shouldn't be highly significant. If it seriously
1273 * bothers anyone then perhaps I _might_ be persuadable.
1275 setup_passphrases("sponge", NULL);
1276 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1279 * Change the passphrase to nothing.
1281 setup_passphrases("sponge", "", "", NULL);
1282 test(0, "puttygen", "-P", filename, NULL);
1285 * Change the comment of the key again; this time we expect no
1286 * passphrase to be required.
1288 setup_passphrases(NULL);
1289 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1292 * Export the private key into OpenSSH format; no passphrase
1293 * should be required since the key is currently unencrypted.
1294 * For RSA1 keys, this should give an error.
1296 setup_passphrases(NULL);
1297 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1302 * List the fingerprint of the OpenSSH-formatted key.
1304 setup_passphrases(NULL);
1305 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1306 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1309 * List the public half of the OpenSSH-formatted key in
1312 setup_passphrases(NULL);
1313 test(0, "puttygen", "-L", osfilename, NULL);
1316 * List the public half of the OpenSSH-formatted key in
1317 * IETF/ssh.com format.
1319 setup_passphrases(NULL);
1320 test(0, "puttygen", "-p", osfilename, NULL);
1324 * Export the private key into ssh.com format; no passphrase
1325 * should be required since the key is currently unencrypted.
1326 * For RSA1 keys, this should give an error.
1328 setup_passphrases(NULL);
1329 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1334 * List the fingerprint of the ssh.com-formatted key.
1336 setup_passphrases(NULL);
1337 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1338 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1341 * List the public half of the ssh.com-formatted key in
1344 setup_passphrases(NULL);
1345 test(0, "puttygen", "-L", scfilename, NULL);
1348 * List the public half of the ssh.com-formatted key in
1349 * IETF/ssh.com format.
1351 setup_passphrases(NULL);
1352 test(0, "puttygen", "-p", scfilename, NULL);
1357 * Convert from OpenSSH into ssh.com.
1359 setup_passphrases(NULL);
1360 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1361 "-O", "private-sshcom", NULL);
1364 * Convert from ssh.com back into a PuTTY key,
1365 * supplying the same comment as we had before we
1366 * started to ensure the comparison works.
1368 setup_passphrases(NULL);
1369 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1370 "-o", tmpfilename2, NULL);
1373 * See if the PuTTY key thus generated is the same as
1376 filecmp(filename, tmpfilename2,
1377 "p->o->s->p clear %s", keytypes[i]);
1380 * Convert from ssh.com to OpenSSH.
1382 setup_passphrases(NULL);
1383 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1384 "-O", "private-openssh", NULL);
1387 * Convert from OpenSSH back into a PuTTY key,
1388 * supplying the same comment as we had before we
1389 * started to ensure the comparison works.
1391 setup_passphrases(NULL);
1392 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1393 "-o", tmpfilename2, NULL);
1396 * See if the PuTTY key thus generated is the same as
1399 filecmp(filename, tmpfilename2,
1400 "p->s->o->p clear %s", keytypes[i]);
1403 * Finally, do a round-trip conversion between PuTTY
1404 * and ssh.com without involving OpenSSH, to test that
1405 * the key comment is preserved in that case.
1407 setup_passphrases(NULL);
1408 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1410 setup_passphrases(NULL);
1411 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1412 filecmp(filename, tmpfilename2,
1413 "p->s->p clear %s", keytypes[i]);
1417 * Check that mismatched passphrases cause an error.
1419 setup_passphrases("sponge2", "sponge3", NULL);
1420 test(1, "puttygen", "-P", filename, NULL);
1423 * Put a passphrase back on.
1425 setup_passphrases("sponge2", "sponge2", NULL);
1426 test(0, "puttygen", "-P", filename, NULL);
1429 * Export the private key into OpenSSH format, this time
1430 * while encrypted. For RSA1 keys, this should give an
1434 setup_passphrases(NULL); /* error, hence no passphrase read */
1436 setup_passphrases("sponge2", NULL);
1437 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1442 * List the fingerprint of the OpenSSH-formatted key.
1444 setup_passphrases("sponge2", NULL);
1445 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1446 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1449 * List the public half of the OpenSSH-formatted key in
1452 setup_passphrases("sponge2", NULL);
1453 test(0, "puttygen", "-L", osfilename, NULL);
1456 * List the public half of the OpenSSH-formatted key in
1457 * IETF/ssh.com format.
1459 setup_passphrases("sponge2", NULL);
1460 test(0, "puttygen", "-p", osfilename, NULL);
1464 * Export the private key into ssh.com format, this time
1465 * while encrypted. For RSA1 keys, this should give an
1469 setup_passphrases(NULL); /* error, hence no passphrase read */
1471 setup_passphrases("sponge2", NULL);
1472 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1477 * List the fingerprint of the ssh.com-formatted key.
1479 setup_passphrases("sponge2", NULL);
1480 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1481 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1484 * List the public half of the ssh.com-formatted key in
1487 setup_passphrases("sponge2", NULL);
1488 test(0, "puttygen", "-L", scfilename, NULL);
1491 * List the public half of the ssh.com-formatted key in
1492 * IETF/ssh.com format.
1494 setup_passphrases("sponge2", NULL);
1495 test(0, "puttygen", "-p", scfilename, NULL);
1500 * Convert from OpenSSH into ssh.com.
1502 setup_passphrases("sponge2", NULL);
1503 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1504 "-O", "private-sshcom", NULL);
1507 * Convert from ssh.com back into a PuTTY key,
1508 * supplying the same comment as we had before we
1509 * started to ensure the comparison works.
1511 setup_passphrases("sponge2", NULL);
1512 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1513 "-o", tmpfilename2, NULL);
1516 * See if the PuTTY key thus generated is the same as
1519 filecmp(filename, tmpfilename2,
1520 "p->o->s->p encrypted %s", keytypes[i]);
1523 * Convert from ssh.com to OpenSSH.
1525 setup_passphrases("sponge2", NULL);
1526 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1527 "-O", "private-openssh", NULL);
1530 * Convert from OpenSSH back into a PuTTY key,
1531 * supplying the same comment as we had before we
1532 * started to ensure the comparison works.
1534 setup_passphrases("sponge2", NULL);
1535 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1536 "-o", tmpfilename2, NULL);
1539 * See if the PuTTY key thus generated is the same as
1542 filecmp(filename, tmpfilename2,
1543 "p->s->o->p encrypted %s", keytypes[i]);
1546 * Finally, do a round-trip conversion between PuTTY
1547 * and ssh.com without involving OpenSSH, to test that
1548 * the key comment is preserved in that case.
1550 setup_passphrases("sponge2", NULL);
1551 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1553 setup_passphrases("sponge2", NULL);
1554 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1555 filecmp(filename, tmpfilename2,
1556 "p->s->p encrypted %s", keytypes[i]);
1560 * Load with the wrong passphrase.
1562 setup_passphrases("sponge8", NULL);
1563 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1566 * Load a totally bogus file.
1568 setup_passphrases(NULL);
1569 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1571 printf("%d passes, %d fails\n", passes, fails);