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 char *buildinfo_text = buildinfo("\n");
133 printf("puttygen: %s\n%s\n", ver, buildinfo_text);
134 sfree(buildinfo_text);
137 void usage(int standalone)
139 fprintf(standalone ? stderr : stdout,
140 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
141 " [ -C comment ] [ -P ] [ -q ]\n"
142 " [ -o output-keyfile ] [ -O type | -l | -L"
146 "Use \"puttygen --help\" for more detail.\n");
152 * Help message is an extended version of the usage message. So
153 * start with that, plus a version heading.
155 printf("PuTTYgen: key generator and converter for the PuTTY tools\n"
158 printf(" -t specify key type when generating (ed25519, ecdsa, rsa, "
160 " -b specify number of bits when generating key\n"
161 " -C change or specify key comment\n"
162 " -P change key passphrase\n"
163 " -q quiet: do not display progress bar\n"
164 " -O specify output type:\n"
165 " private output PuTTY private key format\n"
166 " private-openssh export OpenSSH private key\n"
167 " private-openssh-new export OpenSSH private key "
168 "(force new format)\n"
169 " private-sshcom export ssh.com private key\n"
170 " public RFC 4716 / ssh.com public key\n"
171 " public-openssh OpenSSH public key\n"
172 " fingerprint output the key fingerprint\n"
173 " -o specify output file\n"
174 " -l equivalent to `-O fingerprint'\n"
175 " -L equivalent to `-O public-openssh'\n"
176 " -p equivalent to `-O public'\n"
177 " --old-passphrase file\n"
178 " specify file containing old key passphrase\n"
179 " --new-passphrase file\n"
180 " specify file containing new key passphrase\n"
181 " --random-device device\n"
182 " specify device to read entropy from (e.g. /dev/urandom)\n"
186 static int move(char *from, char *to)
190 ret = rename(from, to);
193 * This OS may require us to remove the original file first.
196 ret = rename(from, to);
199 perror("puttygen: cannot move new file on to old one");
205 static char *readpassphrase(const char *filename)
210 fp = fopen(filename, "r");
212 fprintf(stderr, "puttygen: cannot open %s: %s\n",
213 filename, strerror(errno));
218 line[strcspn(line, "\r\n")] = '\0';
220 fprintf(stderr, "puttygen: error reading from %s: %s\n",
221 filename, strerror(errno));
222 else /* empty file */
228 #define DEFAULT_RSADSA_BITS 2048
230 /* For Unix in particular, but harmless if this main() is reused elsewhere */
231 const int buildinfo_gtk_relevant = FALSE;
233 int main(int argc, char **argv)
236 Filename *infilename = NULL, *outfilename = NULL;
237 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
238 char *outfile = NULL, *outfiletmp = NULL;
239 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
240 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
242 char *comment = NULL, *origcomment = NULL;
243 int change_passphrase = FALSE;
244 int errs = FALSE, nogo = FALSE;
245 int intype = SSH_KEYTYPE_UNOPENABLE;
247 struct ssh2_userkey *ssh2key = NULL;
248 struct RSAKey *ssh1key = NULL;
249 unsigned char *ssh2blob = NULL;
250 char *ssh2alg = NULL;
251 const struct ssh_signkey *ssh2algf = NULL;
253 char *old_passphrase = NULL, *new_passphrase = NULL;
255 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
256 const char *random_device = NULL;
258 /* ------------------------------------------------------------------
259 * Parse the command line to figure out what we've been asked to do.
263 * If run with no arguments at all, print the usage message and
272 * Parse command line arguments.
289 opt = p++; /* opt will have _one_ leading - */
290 while (*p && *p != '=')
291 p++; /* find end of option */
298 if (!strcmp(opt, "-help")) {
301 fprintf(stderr, "puttygen: option `-%s'"
302 " expects no argument\n", opt);
307 } else if (!strcmp(opt, "-version")) {
310 fprintf(stderr, "puttygen: option `-%s'"
311 " expects no argument\n", opt);
316 } else if (!strcmp(opt, "-pgpfp")) {
319 fprintf(stderr, "puttygen: option `-%s'"
320 " expects no argument\n", opt);
322 /* support --pgpfp for consistency */
326 } else if (!strcmp(opt, "-old-passphrase")) {
327 if (!val && argc > 1)
328 --argc, val = *++argv;
331 fprintf(stderr, "puttygen: option `-%s'"
332 " expects an argument\n", opt);
334 old_passphrase = readpassphrase(val);
338 } else if (!strcmp(opt, "-new-passphrase")) {
339 if (!val && argc > 1)
340 --argc, val = *++argv;
343 fprintf(stderr, "puttygen: option `-%s'"
344 " expects an argument\n", opt);
346 new_passphrase = readpassphrase(val);
350 } else if (!strcmp(opt, "-random-device")) {
351 if (!val && argc > 1)
352 --argc, val = *++argv;
355 fprintf(stderr, "puttygen: option `-%s'"
356 " expects an argument\n", opt);
363 "puttygen: no such option `-%s'\n", opt);
376 * Option requiring no parameter.
388 change_passphrase = TRUE;
400 progressfn = no_progress;
410 * Option requiring parameter.
416 fprintf(stderr, "puttygen: option `-%c' expects a"
421 * Now c is the option and p is the parameter.
425 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
426 keytype = RSA2, sshver = 2;
427 else if (!strcmp(p, "rsa1"))
428 keytype = RSA1, sshver = 1;
429 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
430 keytype = DSA, sshver = 2;
431 else if (!strcmp(p, "ecdsa"))
432 keytype = ECDSA, sshver = 2;
433 else if (!strcmp(p, "ed25519"))
434 keytype = ED25519, sshver = 2;
437 "puttygen: unknown key type `%s'\n", p);
448 if (!strcmp(p, "public"))
450 else if (!strcmp(p, "public-openssh"))
452 else if (!strcmp(p, "private"))
454 else if (!strcmp(p, "fingerprint"))
456 else if (!strcmp(p, "private-openssh"))
457 outtype = OPENSSH_AUTO, sshver = 2;
458 else if (!strcmp(p, "private-openssh-new"))
459 outtype = OPENSSH_NEW, sshver = 2;
460 else if (!strcmp(p, "private-sshcom"))
461 outtype = SSHCOM, sshver = 2;
464 "puttygen: unknown output type `%s'\n", p);
472 p = NULL; /* prevent continued processing */
476 * Unrecognised option.
479 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
485 * A non-option argument.
491 fprintf(stderr, "puttygen: cannot handle more than one"
499 * No explicit key size was specified. Default varies
500 * depending on key type.
510 bits = DEFAULT_RSADSA_BITS;
515 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
516 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
520 if (keytype == ED25519 && (bits != 256)) {
521 fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
525 if (keytype == RSA2 || keytype == RSA1 || keytype == DSA) {
527 fprintf(stderr, "puttygen: cannot generate %s keys shorter than"
528 " 256 bits\n", (keytype == DSA ? "DSA" : "RSA"));
530 } else if (bits < DEFAULT_RSADSA_BITS) {
531 fprintf(stderr, "puttygen: warning: %s keys shorter than"
532 " %d bits are probably not secure\n",
533 (keytype == DSA ? "DSA" : "RSA"), DEFAULT_RSADSA_BITS);
534 /* but this is just a warning, so proceed anyway */
545 * If run with at least one argument _but_ not the required
546 * ones, print the usage message and return failure.
548 if (!infile && keytype == NOKEYGEN) {
553 /* ------------------------------------------------------------------
554 * Figure out further details of exactly what we're going to do.
558 * Bomb out if we've been asked to both load and generate a
561 if (keytype != NOKEYGEN && infile) {
562 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
567 * We must save the private part when generating a new key.
569 if (keytype != NOKEYGEN &&
570 (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
571 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
572 fprintf(stderr, "puttygen: this would generate a new key but "
573 "discard the private part\n");
578 * Analyse the type of the input file, in case this affects our
582 infilename = filename_from_str(infile);
584 intype = key_type(infilename);
587 case SSH_KEYTYPE_UNOPENABLE:
588 case SSH_KEYTYPE_UNKNOWN:
589 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
590 infile, key_type_to_str(intype));
593 case SSH_KEYTYPE_SSH1:
594 case SSH_KEYTYPE_SSH1_PUBLIC:
596 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
603 case SSH_KEYTYPE_SSH2:
604 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
605 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
606 case SSH_KEYTYPE_OPENSSH_PEM:
607 case SSH_KEYTYPE_OPENSSH_NEW:
608 case SSH_KEYTYPE_SSHCOM:
610 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
617 case SSH_KEYTYPE_OPENSSH_AUTO:
619 assert(0 && "Should never see these types on an input file");
624 * Determine the default output file, if none is provided.
626 * This will usually be equal to stdout, except that if the
627 * input and output file formats are the same then the default
628 * output is to overwrite the input.
630 * Also in this code, we bomb out if the input and output file
631 * formats are the same and no other action is performed.
633 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
634 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
635 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
636 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
637 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
640 outfiletmp = dupcat(outfile, ".tmp", NULL);
643 if (!change_passphrase && !comment) {
644 fprintf(stderr, "puttygen: this command would perform no useful"
651 * Bomb out rather than automatically choosing to write
652 * a private key file to stdout.
654 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
655 outtype == OPENSSH_NEW || outtype == SSHCOM) {
656 fprintf(stderr, "puttygen: need to specify an output file\n");
663 * Figure out whether we need to load the encrypted part of the
664 * key. This will be the case if either (a) we need to write
665 * out a private key format, or (b) the entire input key file
668 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
669 outtype == OPENSSH_NEW || outtype == SSHCOM ||
670 intype == SSH_KEYTYPE_OPENSSH_PEM ||
671 intype == SSH_KEYTYPE_OPENSSH_NEW ||
672 intype == SSH_KEYTYPE_SSHCOM)
673 load_encrypted = TRUE;
675 load_encrypted = FALSE;
677 if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
678 intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
679 intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
680 fprintf(stderr, "puttygen: cannot perform this action on a "
681 "public-key-only input file\n");
685 /* ------------------------------------------------------------------
686 * Now we're ready to actually do some stuff.
690 * Either load or generate a key.
692 if (keytype != NOKEYGEN) {
694 char default_comment[80];
696 struct progress prog;
703 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
704 else if (keytype == ECDSA)
705 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
706 else if (keytype == ED25519)
707 strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
709 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
712 entropy = get_random_data(bits / 8, random_device);
714 fprintf(stderr, "puttygen: failed to collect entropy, "
715 "could not generate key\n");
718 random_add_heavynoise(entropy, bits / 8);
719 smemclr(entropy, bits/8);
722 if (keytype == DSA) {
723 struct dss_key *dsskey = snew(struct dss_key);
724 dsa_generate(dsskey, bits, progressfn, &prog);
725 ssh2key = snew(struct ssh2_userkey);
726 ssh2key->data = dsskey;
727 ssh2key->alg = &ssh_dss;
729 } else if (keytype == ECDSA) {
730 struct ec_key *ec = snew(struct ec_key);
731 ec_generate(ec, bits, progressfn, &prog);
732 ssh2key = snew(struct ssh2_userkey);
734 ssh2key->alg = ec->signalg;
736 } else if (keytype == ED25519) {
737 struct ec_key *ec = snew(struct ec_key);
738 ec_edgenerate(ec, bits, progressfn, &prog);
739 ssh2key = snew(struct ssh2_userkey);
741 ssh2key->alg = &ssh_ecdsa_ed25519;
744 struct RSAKey *rsakey = snew(struct RSAKey);
745 rsa_generate(rsakey, bits, progressfn, &prog);
746 rsakey->comment = NULL;
747 if (keytype == RSA1) {
750 ssh2key = snew(struct ssh2_userkey);
751 ssh2key->data = rsakey;
752 ssh2key->alg = &ssh_rsa;
755 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
758 ssh2key->comment = dupstr(default_comment);
760 ssh1key->comment = dupstr(default_comment);
763 const char *error = NULL;
766 assert(infile != NULL);
769 * Find out whether the input key is encrypted.
771 if (intype == SSH_KEYTYPE_SSH1)
772 encrypted = rsakey_encrypted(infilename, &origcomment);
773 else if (intype == SSH_KEYTYPE_SSH2)
774 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
776 encrypted = import_encrypted(infilename, intype, &origcomment);
779 * If so, ask for a passphrase.
781 if (encrypted && load_encrypted) {
782 if (!old_passphrase) {
783 prompts_t *p = new_prompts(NULL);
785 p->to_server = FALSE;
786 p->name = dupstr("SSH key passphrase");
787 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
788 ret = console_get_userpass_input(p, NULL, 0);
792 perror("puttygen: unable to read passphrase");
795 old_passphrase = dupstr(p->prompts[0]->result);
800 old_passphrase = NULL;
806 case SSH_KEYTYPE_SSH1:
807 case SSH_KEYTYPE_SSH1_PUBLIC:
808 ssh1key = snew(struct RSAKey);
809 if (!load_encrypted) {
814 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
815 &origcomment, &error);
816 blob = (unsigned char *)vblob;
818 n = 4; /* skip modulus bits */
820 l = ssh1_read_bignum(blob + n, bloblen - n,
823 error = "SSH-1 public key blob was too short";
826 l = ssh1_read_bignum(blob + n, bloblen - n,
829 error = "SSH-1 public key blob was too short";
833 ssh1key->comment = dupstr(origcomment);
834 ssh1key->private_exponent = NULL;
837 ssh1key->iqmp = NULL;
839 ret = loadrsakey(infilename, ssh1key, old_passphrase, &error);
844 error = "unknown error";
847 case SSH_KEYTYPE_SSH2:
848 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
849 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
850 if (!load_encrypted) {
851 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
852 &ssh2bloblen, &origcomment,
855 ssh2algf = find_pubkey_alg(ssh2alg);
857 bits = ssh2algf->pubkey_bits(ssh2algf,
858 ssh2blob, ssh2bloblen);
864 ssh2key = ssh2_load_userkey(infilename, old_passphrase,
867 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
870 if (ssh2key == SSH2_WRONG_PASSPHRASE)
871 error = "wrong passphrase";
873 error = "unknown error";
877 case SSH_KEYTYPE_OPENSSH_PEM:
878 case SSH_KEYTYPE_OPENSSH_NEW:
879 case SSH_KEYTYPE_SSHCOM:
880 ssh2key = import_ssh2(infilename, intype, old_passphrase, &error);
882 if (ssh2key != SSH2_WRONG_PASSPHRASE)
885 error = "wrong passphrase";
887 error = "unknown error";
895 fprintf(stderr, "puttygen: error loading `%s': %s\n",
902 * Change the comment if asked to.
907 sfree(ssh1key->comment);
908 ssh1key->comment = dupstr(comment);
911 sfree(ssh2key->comment);
912 ssh2key->comment = dupstr(comment);
917 * Unless we're changing the passphrase, the old one (if any) is a
918 * reasonable default.
920 if (!change_passphrase && old_passphrase && !new_passphrase)
921 new_passphrase = dupstr(old_passphrase);
924 * Prompt for a new passphrase if we have been asked to, or if
925 * we have just generated a key.
927 if (!new_passphrase && (change_passphrase || keytype != NOKEYGEN)) {
928 prompts_t *p = new_prompts(NULL);
931 p->to_server = FALSE;
932 p->name = dupstr("New SSH key passphrase");
933 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
934 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
935 ret = console_get_userpass_input(p, NULL, 0);
939 perror("puttygen: unable to read new passphrase");
942 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
944 fprintf(stderr, "puttygen: passphrases do not match\n");
947 new_passphrase = dupstr(p->prompts[0]->result);
951 if (new_passphrase && !*new_passphrase) {
952 sfree(new_passphrase);
953 new_passphrase = NULL;
959 * (In the case where outfile and outfiletmp are both NULL,
960 * there is no semantic reason to initialise outfilename at
961 * all; but we have to write _something_ to it or some compiler
962 * will probably complain that it might be used uninitialised.)
965 outfilename = filename_from_str(outfiletmp);
967 outfilename = filename_from_str(outfile ? outfile : "");
970 int ret, real_outtype;
975 ret = saversakey(outfilename, ssh1key, new_passphrase);
977 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
982 ret = ssh2_save_userkey(outfilename, ssh2key, new_passphrase);
984 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
989 if (!move(outfiletmp, outfile))
990 return 1; /* rename failed */
1000 fp = f_open(outfilename, "w", FALSE);
1005 ssh1_write_pubkey(fp, ssh1key);
1009 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
1013 ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
1014 ssh2blob, ssh2bloblen,
1015 (outtype == PUBLIC ?
1016 SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
1017 SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
1032 fingerprint = snewn(128, char);
1033 rsa_fingerprint(fingerprint, 128, ssh1key);
1036 fingerprint = ssh2_fingerprint(ssh2key->alg,
1040 fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen);
1045 fp = f_open(outfilename, "w", FALSE);
1048 fprintf(fp, "%s\n", fingerprint);
1059 assert(sshver == 2);
1061 random_ref(); /* both foreign key types require randomness,
1062 * for IV or padding */
1065 real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
1068 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1071 real_outtype = SSH_KEYTYPE_SSHCOM;
1074 assert(0 && "control flow goof");
1076 ret = export_ssh2(outfilename, real_outtype, ssh2key, new_passphrase);
1078 fprintf(stderr, "puttygen: unable to export key\n");
1082 if (!move(outfiletmp, outfile))
1083 return 1; /* rename failed */
1088 if (old_passphrase) {
1089 smemclr(old_passphrase, strlen(old_passphrase));
1090 sfree(old_passphrase);
1092 if (new_passphrase) {
1093 smemclr(new_passphrase, strlen(new_passphrase));
1094 sfree(new_passphrase);
1098 freersakey(ssh1key);
1100 ssh2key->alg->freekey(ssh2key->data);
1115 void setup_passphrases(char *first, ...)
1122 prompts[nprompts++] = first;
1123 va_start(ap, first);
1124 while ((next = va_arg(ap, char *)) != NULL) {
1125 assert(nprompts < lenof(prompts));
1126 prompts[nprompts++] = next;
1132 void test(int retval, ...)
1139 va_start(ap, retval);
1140 while (va_arg(ap, char *) != NULL)
1144 argv = snewn(argc+1, char *);
1145 va_start(ap, retval);
1146 for (i = 0; i <= argc; i++)
1147 argv[i] = va_arg(ap, char *);
1151 ret = cmdgen_main(argc, argv);
1153 if (ret != retval) {
1154 printf("FAILED retval (exp %d got %d):", retval, ret);
1155 for (i = 0; i < argc; i++)
1156 printf(" %s", argv[i]);
1159 } else if (promptsgot != nprompts) {
1160 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1161 for (i = 0; i < argc; i++)
1162 printf(" %s", argv[i]);
1172 void filecmp(char *file1, char *file2, char *fmt, ...)
1175 * Ideally I should do file comparison myself, to maximise the
1176 * portability of this test suite once this application begins
1177 * running on non-Unix platforms. For the moment, though,
1178 * calling Unix diff is perfectly adequate.
1183 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1190 printf("FAILED diff (ret=%d): ", ret);
1203 char *cleanup_fp(char *s)
1207 if (!strncmp(s, "ssh-", 4)) {
1208 s += strcspn(s, " \n\t");
1209 s += strspn(s, " \n\t");
1213 s += strcspn(s, " \n\t");
1214 s += strspn(s, " \n\t");
1215 s += strcspn(s, " \n\t");
1217 return dupprintf("%.*s", (int)(s - p), p);
1220 char *get_fp(char *filename)
1223 char buf[256], *ret;
1225 fp = fopen(filename, "r");
1228 ret = fgets(buf, sizeof(buf), fp);
1232 return cleanup_fp(buf);
1235 void check_fp(char *filename, char *fp, char *fmt, ...)
1242 newfp = get_fp(filename);
1244 if (!strcmp(fp, newfp)) {
1249 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1263 int main(int argc, char **argv)
1266 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1269 * Even when this thing is compiled for automatic test mode,
1270 * it's helpful to be able to invoke it with command-line
1271 * options for _manual_ tests.
1274 return cmdgen_main(argc, argv);
1278 for (i = 0; i < lenof(keytypes); i++) {
1279 char filename[128], osfilename[128], scfilename[128];
1280 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1283 sprintf(filename, "test-%s.ppk", keytypes[i]);
1284 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1285 sprintf(osfilename, "test-%s.os", keytypes[i]);
1286 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1287 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1288 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1291 * Create an encrypted key.
1293 setup_passphrases("sponge", "sponge", NULL);
1294 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1297 * List the public key in OpenSSH format.
1299 setup_passphrases(NULL);
1300 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1304 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1305 pubfilename, tmpfilename1);
1306 if (system(cmdbuf) ||
1307 (fp = get_fp(tmpfilename1)) == NULL) {
1308 printf("UNABLE to test fingerprint matching against OpenSSH");
1313 * List the public key in IETF/ssh.com format.
1315 setup_passphrases(NULL);
1316 test(0, "puttygen", "-p", filename, NULL);
1319 * List the fingerprint of the key.
1321 setup_passphrases(NULL);
1322 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1325 * If we can't test fingerprints against OpenSSH, we
1326 * can at the very least test equality of all the
1327 * fingerprints we generate of this key throughout
1330 fp = get_fp(tmpfilename1);
1332 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1336 * Change the comment of the key; this _does_ require a
1337 * passphrase owing to the tamperproofing.
1339 * NOTE: In SSH-1, this only requires a passphrase because
1340 * of inadequacies of the loading and saving mechanisms. In
1341 * _principle_, it should be perfectly possible to modify
1342 * the comment on an SSH-1 key without requiring a
1343 * passphrase; the only reason I can't do it is because my
1344 * loading and saving mechanisms don't include a method of
1345 * loading all the key data without also trying to decrypt
1346 * the private section.
1348 * I don't consider this to be a problem worth solving,
1349 * because (a) to fix it would probably end up bloating
1350 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1351 * it shouldn't be highly significant. If it seriously
1352 * bothers anyone then perhaps I _might_ be persuadable.
1354 setup_passphrases("sponge", NULL);
1355 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1358 * Change the passphrase to nothing.
1360 setup_passphrases("sponge", "", "", NULL);
1361 test(0, "puttygen", "-P", filename, NULL);
1364 * Change the comment of the key again; this time we expect no
1365 * passphrase to be required.
1367 setup_passphrases(NULL);
1368 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1371 * Export the private key into OpenSSH format; no passphrase
1372 * should be required since the key is currently unencrypted.
1373 * For RSA1 keys, this should give an error.
1375 setup_passphrases(NULL);
1376 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1381 * List the fingerprint of the OpenSSH-formatted key.
1383 setup_passphrases(NULL);
1384 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1385 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1388 * List the public half of the OpenSSH-formatted key in
1391 setup_passphrases(NULL);
1392 test(0, "puttygen", "-L", osfilename, NULL);
1395 * List the public half of the OpenSSH-formatted key in
1396 * IETF/ssh.com format.
1398 setup_passphrases(NULL);
1399 test(0, "puttygen", "-p", osfilename, NULL);
1403 * Export the private key into ssh.com format; no passphrase
1404 * should be required since the key is currently unencrypted.
1405 * For RSA1 keys, this should give an error.
1407 setup_passphrases(NULL);
1408 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1413 * List the fingerprint of the ssh.com-formatted key.
1415 setup_passphrases(NULL);
1416 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1417 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1420 * List the public half of the ssh.com-formatted key in
1423 setup_passphrases(NULL);
1424 test(0, "puttygen", "-L", scfilename, NULL);
1427 * List the public half of the ssh.com-formatted key in
1428 * IETF/ssh.com format.
1430 setup_passphrases(NULL);
1431 test(0, "puttygen", "-p", scfilename, NULL);
1436 * Convert from OpenSSH into ssh.com.
1438 setup_passphrases(NULL);
1439 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1440 "-O", "private-sshcom", NULL);
1443 * Convert from ssh.com back into a PuTTY key,
1444 * supplying the same comment as we had before we
1445 * started to ensure the comparison works.
1447 setup_passphrases(NULL);
1448 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1449 "-o", tmpfilename2, NULL);
1452 * See if the PuTTY key thus generated is the same as
1455 filecmp(filename, tmpfilename2,
1456 "p->o->s->p clear %s", keytypes[i]);
1459 * Convert from ssh.com to OpenSSH.
1461 setup_passphrases(NULL);
1462 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1463 "-O", "private-openssh", NULL);
1466 * Convert from OpenSSH back into a PuTTY key,
1467 * supplying the same comment as we had before we
1468 * started to ensure the comparison works.
1470 setup_passphrases(NULL);
1471 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1472 "-o", tmpfilename2, NULL);
1475 * See if the PuTTY key thus generated is the same as
1478 filecmp(filename, tmpfilename2,
1479 "p->s->o->p clear %s", keytypes[i]);
1482 * Finally, do a round-trip conversion between PuTTY
1483 * and ssh.com without involving OpenSSH, to test that
1484 * the key comment is preserved in that case.
1486 setup_passphrases(NULL);
1487 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1489 setup_passphrases(NULL);
1490 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1491 filecmp(filename, tmpfilename2,
1492 "p->s->p clear %s", keytypes[i]);
1496 * Check that mismatched passphrases cause an error.
1498 setup_passphrases("sponge2", "sponge3", NULL);
1499 test(1, "puttygen", "-P", filename, NULL);
1502 * Put a passphrase back on.
1504 setup_passphrases("sponge2", "sponge2", NULL);
1505 test(0, "puttygen", "-P", filename, NULL);
1508 * Export the private key into OpenSSH format, this time
1509 * while encrypted. For RSA1 keys, this should give an
1513 setup_passphrases(NULL); /* error, hence no passphrase read */
1515 setup_passphrases("sponge2", NULL);
1516 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1521 * List the fingerprint of the OpenSSH-formatted key.
1523 setup_passphrases("sponge2", NULL);
1524 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1525 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1528 * List the public half of the OpenSSH-formatted key in
1531 setup_passphrases("sponge2", NULL);
1532 test(0, "puttygen", "-L", osfilename, NULL);
1535 * List the public half of the OpenSSH-formatted key in
1536 * IETF/ssh.com format.
1538 setup_passphrases("sponge2", NULL);
1539 test(0, "puttygen", "-p", osfilename, NULL);
1543 * Export the private key into ssh.com format, this time
1544 * while encrypted. For RSA1 keys, this should give an
1548 setup_passphrases(NULL); /* error, hence no passphrase read */
1550 setup_passphrases("sponge2", NULL);
1551 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1556 * List the fingerprint of the ssh.com-formatted key.
1558 setup_passphrases("sponge2", NULL);
1559 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1560 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1563 * List the public half of the ssh.com-formatted key in
1566 setup_passphrases("sponge2", NULL);
1567 test(0, "puttygen", "-L", scfilename, NULL);
1570 * List the public half of the ssh.com-formatted key in
1571 * IETF/ssh.com format.
1573 setup_passphrases("sponge2", NULL);
1574 test(0, "puttygen", "-p", scfilename, NULL);
1579 * Convert from OpenSSH into ssh.com.
1581 setup_passphrases("sponge2", NULL);
1582 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1583 "-O", "private-sshcom", NULL);
1586 * Convert from ssh.com back into a PuTTY key,
1587 * supplying the same comment as we had before we
1588 * started to ensure the comparison works.
1590 setup_passphrases("sponge2", NULL);
1591 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1592 "-o", tmpfilename2, NULL);
1595 * See if the PuTTY key thus generated is the same as
1598 filecmp(filename, tmpfilename2,
1599 "p->o->s->p encrypted %s", keytypes[i]);
1602 * Convert from ssh.com to OpenSSH.
1604 setup_passphrases("sponge2", NULL);
1605 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1606 "-O", "private-openssh", NULL);
1609 * Convert from OpenSSH back into a PuTTY key,
1610 * supplying the same comment as we had before we
1611 * started to ensure the comparison works.
1613 setup_passphrases("sponge2", NULL);
1614 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1615 "-o", tmpfilename2, NULL);
1618 * See if the PuTTY key thus generated is the same as
1621 filecmp(filename, tmpfilename2,
1622 "p->s->o->p encrypted %s", keytypes[i]);
1625 * Finally, do a round-trip conversion between PuTTY
1626 * and ssh.com without involving OpenSSH, to test that
1627 * the key comment is preserved in that case.
1629 setup_passphrases("sponge2", NULL);
1630 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1632 setup_passphrases("sponge2", NULL);
1633 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1634 filecmp(filename, tmpfilename2,
1635 "p->s->p encrypted %s", keytypes[i]);
1639 * Load with the wrong passphrase.
1641 setup_passphrases("sponge8", NULL);
1642 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1645 * Load a totally bogus file.
1647 setup_passphrases(NULL);
1648 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1650 printf("%d passes, %d fails\n", passes, fails);