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 int main(int argc, char **argv)
233 Filename *infilename = NULL, *outfilename = NULL;
234 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
235 char *outfile = NULL, *outfiletmp = NULL;
236 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
237 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
239 char *comment = NULL, *origcomment = NULL;
240 int change_passphrase = FALSE;
241 int errs = FALSE, nogo = FALSE;
242 int intype = SSH_KEYTYPE_UNOPENABLE;
244 struct ssh2_userkey *ssh2key = NULL;
245 struct RSAKey *ssh1key = NULL;
246 unsigned char *ssh2blob = NULL;
247 char *ssh2alg = NULL;
248 const struct ssh_signkey *ssh2algf = NULL;
250 char *old_passphrase = NULL, *new_passphrase = NULL;
252 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
253 const char *random_device = NULL;
255 /* ------------------------------------------------------------------
256 * Parse the command line to figure out what we've been asked to do.
260 * If run with no arguments at all, print the usage message and
269 * Parse command line arguments.
286 opt = p++; /* opt will have _one_ leading - */
287 while (*p && *p != '=')
288 p++; /* find end of option */
295 if (!strcmp(opt, "-help")) {
298 fprintf(stderr, "puttygen: option `-%s'"
299 " expects no argument\n", opt);
304 } else if (!strcmp(opt, "-version")) {
307 fprintf(stderr, "puttygen: option `-%s'"
308 " expects no argument\n", opt);
313 } else if (!strcmp(opt, "-pgpfp")) {
316 fprintf(stderr, "puttygen: option `-%s'"
317 " expects no argument\n", opt);
319 /* support --pgpfp for consistency */
323 } else if (!strcmp(opt, "-old-passphrase")) {
324 if (!val && argc > 1)
325 --argc, val = *++argv;
328 fprintf(stderr, "puttygen: option `-%s'"
329 " expects an argument\n", opt);
331 old_passphrase = readpassphrase(val);
335 } else if (!strcmp(opt, "-new-passphrase")) {
336 if (!val && argc > 1)
337 --argc, val = *++argv;
340 fprintf(stderr, "puttygen: option `-%s'"
341 " expects an argument\n", opt);
343 new_passphrase = readpassphrase(val);
347 } else if (!strcmp(opt, "-random-device")) {
348 if (!val && argc > 1)
349 --argc, val = *++argv;
352 fprintf(stderr, "puttygen: option `-%s'"
353 " expects an argument\n", opt);
360 "puttygen: no such option `-%s'\n", opt);
373 * Option requiring no parameter.
385 change_passphrase = TRUE;
397 progressfn = no_progress;
407 * Option requiring parameter.
413 fprintf(stderr, "puttygen: option `-%c' expects a"
418 * Now c is the option and p is the parameter.
422 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
423 keytype = RSA2, sshver = 2;
424 else if (!strcmp(p, "rsa1"))
425 keytype = RSA1, sshver = 1;
426 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
427 keytype = DSA, sshver = 2;
428 else if (!strcmp(p, "ecdsa"))
429 keytype = ECDSA, sshver = 2;
430 else if (!strcmp(p, "ed25519"))
431 keytype = ED25519, sshver = 2;
434 "puttygen: unknown key type `%s'\n", p);
445 if (!strcmp(p, "public"))
447 else if (!strcmp(p, "public-openssh"))
449 else if (!strcmp(p, "private"))
451 else if (!strcmp(p, "fingerprint"))
453 else if (!strcmp(p, "private-openssh"))
454 outtype = OPENSSH_AUTO, sshver = 2;
455 else if (!strcmp(p, "private-openssh-new"))
456 outtype = OPENSSH_NEW, sshver = 2;
457 else if (!strcmp(p, "private-sshcom"))
458 outtype = SSHCOM, sshver = 2;
461 "puttygen: unknown output type `%s'\n", p);
469 p = NULL; /* prevent continued processing */
473 * Unrecognised option.
476 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
482 * A non-option argument.
488 fprintf(stderr, "puttygen: cannot handle more than one"
496 * No explicit key size was specified. Default varies
497 * depending on key type.
507 bits = DEFAULT_RSADSA_BITS;
512 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
513 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
517 if (keytype == ED25519 && (bits != 256)) {
518 fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
522 if (keytype == RSA2 || keytype == RSA1 || keytype == DSA) {
524 fprintf(stderr, "puttygen: cannot generate %s keys shorter than"
525 " 256 bits\n", (keytype == DSA ? "DSA" : "RSA"));
527 } else if (bits < DEFAULT_RSADSA_BITS) {
528 fprintf(stderr, "puttygen: warning: %s keys shorter than"
529 " %d bits are probably not secure\n",
530 (keytype == DSA ? "DSA" : "RSA"), DEFAULT_RSADSA_BITS);
531 /* but this is just a warning, so proceed anyway */
542 * If run with at least one argument _but_ not the required
543 * ones, print the usage message and return failure.
545 if (!infile && keytype == NOKEYGEN) {
550 /* ------------------------------------------------------------------
551 * Figure out further details of exactly what we're going to do.
555 * Bomb out if we've been asked to both load and generate a
558 if (keytype != NOKEYGEN && infile) {
559 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
564 * We must save the private part when generating a new key.
566 if (keytype != NOKEYGEN &&
567 (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
568 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
569 fprintf(stderr, "puttygen: this would generate a new key but "
570 "discard the private part\n");
575 * Analyse the type of the input file, in case this affects our
579 infilename = filename_from_str(infile);
581 intype = key_type(infilename);
584 case SSH_KEYTYPE_UNOPENABLE:
585 case SSH_KEYTYPE_UNKNOWN:
586 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
587 infile, key_type_to_str(intype));
590 case SSH_KEYTYPE_SSH1:
591 case SSH_KEYTYPE_SSH1_PUBLIC:
593 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
600 case SSH_KEYTYPE_SSH2:
601 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
602 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
603 case SSH_KEYTYPE_OPENSSH_PEM:
604 case SSH_KEYTYPE_OPENSSH_NEW:
605 case SSH_KEYTYPE_SSHCOM:
607 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
614 case SSH_KEYTYPE_OPENSSH_AUTO:
616 assert(0 && "Should never see these types on an input file");
621 * Determine the default output file, if none is provided.
623 * This will usually be equal to stdout, except that if the
624 * input and output file formats are the same then the default
625 * output is to overwrite the input.
627 * Also in this code, we bomb out if the input and output file
628 * formats are the same and no other action is performed.
630 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
631 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
632 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
633 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
634 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
637 outfiletmp = dupcat(outfile, ".tmp", NULL);
640 if (!change_passphrase && !comment) {
641 fprintf(stderr, "puttygen: this command would perform no useful"
648 * Bomb out rather than automatically choosing to write
649 * a private key file to stdout.
651 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
652 outtype == OPENSSH_NEW || outtype == SSHCOM) {
653 fprintf(stderr, "puttygen: need to specify an output file\n");
660 * Figure out whether we need to load the encrypted part of the
661 * key. This will be the case if either (a) we need to write
662 * out a private key format, or (b) the entire input key file
665 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
666 outtype == OPENSSH_NEW || outtype == SSHCOM ||
667 intype == SSH_KEYTYPE_OPENSSH_PEM ||
668 intype == SSH_KEYTYPE_OPENSSH_NEW ||
669 intype == SSH_KEYTYPE_SSHCOM)
670 load_encrypted = TRUE;
672 load_encrypted = FALSE;
674 if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
675 intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
676 intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
677 fprintf(stderr, "puttygen: cannot perform this action on a "
678 "public-key-only input file\n");
682 /* ------------------------------------------------------------------
683 * Now we're ready to actually do some stuff.
687 * Either load or generate a key.
689 if (keytype != NOKEYGEN) {
691 char default_comment[80];
693 struct progress prog;
700 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
701 else if (keytype == ECDSA)
702 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
703 else if (keytype == ED25519)
704 strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
706 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
709 entropy = get_random_data(bits / 8, random_device);
711 fprintf(stderr, "puttygen: failed to collect entropy, "
712 "could not generate key\n");
715 random_add_heavynoise(entropy, bits / 8);
716 smemclr(entropy, bits/8);
719 if (keytype == DSA) {
720 struct dss_key *dsskey = snew(struct dss_key);
721 dsa_generate(dsskey, bits, progressfn, &prog);
722 ssh2key = snew(struct ssh2_userkey);
723 ssh2key->data = dsskey;
724 ssh2key->alg = &ssh_dss;
726 } else if (keytype == ECDSA) {
727 struct ec_key *ec = snew(struct ec_key);
728 ec_generate(ec, bits, progressfn, &prog);
729 ssh2key = snew(struct ssh2_userkey);
731 ssh2key->alg = ec->signalg;
733 } else if (keytype == ED25519) {
734 struct ec_key *ec = snew(struct ec_key);
735 ec_edgenerate(ec, bits, progressfn, &prog);
736 ssh2key = snew(struct ssh2_userkey);
738 ssh2key->alg = &ssh_ecdsa_ed25519;
741 struct RSAKey *rsakey = snew(struct RSAKey);
742 rsa_generate(rsakey, bits, progressfn, &prog);
743 rsakey->comment = NULL;
744 if (keytype == RSA1) {
747 ssh2key = snew(struct ssh2_userkey);
748 ssh2key->data = rsakey;
749 ssh2key->alg = &ssh_rsa;
752 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
755 ssh2key->comment = dupstr(default_comment);
757 ssh1key->comment = dupstr(default_comment);
760 const char *error = NULL;
763 assert(infile != NULL);
766 * Find out whether the input key is encrypted.
768 if (intype == SSH_KEYTYPE_SSH1)
769 encrypted = rsakey_encrypted(infilename, &origcomment);
770 else if (intype == SSH_KEYTYPE_SSH2)
771 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
773 encrypted = import_encrypted(infilename, intype, &origcomment);
776 * If so, ask for a passphrase.
778 if (encrypted && load_encrypted) {
779 if (!old_passphrase) {
780 prompts_t *p = new_prompts(NULL);
782 p->to_server = FALSE;
783 p->name = dupstr("SSH key passphrase");
784 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
785 ret = console_get_userpass_input(p, NULL, 0);
789 perror("puttygen: unable to read passphrase");
792 old_passphrase = dupstr(p->prompts[0]->result);
797 old_passphrase = NULL;
803 case SSH_KEYTYPE_SSH1:
804 case SSH_KEYTYPE_SSH1_PUBLIC:
805 ssh1key = snew(struct RSAKey);
806 if (!load_encrypted) {
811 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
812 &origcomment, &error);
813 blob = (unsigned char *)vblob;
815 n = 4; /* skip modulus bits */
817 l = ssh1_read_bignum(blob + n, bloblen - n,
820 error = "SSH-1 public key blob was too short";
823 l = ssh1_read_bignum(blob + n, bloblen - n,
826 error = "SSH-1 public key blob was too short";
830 ssh1key->comment = dupstr(origcomment);
831 ssh1key->private_exponent = NULL;
834 ssh1key->iqmp = NULL;
836 ret = loadrsakey(infilename, ssh1key, old_passphrase, &error);
841 error = "unknown error";
844 case SSH_KEYTYPE_SSH2:
845 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
846 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
847 if (!load_encrypted) {
848 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
849 &ssh2bloblen, &origcomment,
852 ssh2algf = find_pubkey_alg(ssh2alg);
854 bits = ssh2algf->pubkey_bits(ssh2algf,
855 ssh2blob, ssh2bloblen);
861 ssh2key = ssh2_load_userkey(infilename, old_passphrase,
864 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
867 if (ssh2key == SSH2_WRONG_PASSPHRASE)
868 error = "wrong passphrase";
870 error = "unknown error";
874 case SSH_KEYTYPE_OPENSSH_PEM:
875 case SSH_KEYTYPE_OPENSSH_NEW:
876 case SSH_KEYTYPE_SSHCOM:
877 ssh2key = import_ssh2(infilename, intype, old_passphrase, &error);
879 if (ssh2key != SSH2_WRONG_PASSPHRASE)
882 error = "wrong passphrase";
884 error = "unknown error";
892 fprintf(stderr, "puttygen: error loading `%s': %s\n",
899 * Change the comment if asked to.
904 sfree(ssh1key->comment);
905 ssh1key->comment = dupstr(comment);
908 sfree(ssh2key->comment);
909 ssh2key->comment = dupstr(comment);
914 * Unless we're changing the passphrase, the old one (if any) is a
915 * reasonable default.
917 if (!change_passphrase && old_passphrase && !new_passphrase)
918 new_passphrase = dupstr(old_passphrase);
921 * Prompt for a new passphrase if we have been asked to, or if
922 * we have just generated a key.
924 if (!new_passphrase && (change_passphrase || keytype != NOKEYGEN)) {
925 prompts_t *p = new_prompts(NULL);
928 p->to_server = FALSE;
929 p->name = dupstr("New SSH key passphrase");
930 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
931 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
932 ret = console_get_userpass_input(p, NULL, 0);
936 perror("puttygen: unable to read new passphrase");
939 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
941 fprintf(stderr, "puttygen: passphrases do not match\n");
944 new_passphrase = dupstr(p->prompts[0]->result);
948 if (new_passphrase && !*new_passphrase) {
949 sfree(new_passphrase);
950 new_passphrase = NULL;
956 * (In the case where outfile and outfiletmp are both NULL,
957 * there is no semantic reason to initialise outfilename at
958 * all; but we have to write _something_ to it or some compiler
959 * will probably complain that it might be used uninitialised.)
962 outfilename = filename_from_str(outfiletmp);
964 outfilename = filename_from_str(outfile ? outfile : "");
967 int ret, real_outtype;
972 ret = saversakey(outfilename, ssh1key, new_passphrase);
974 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
979 ret = ssh2_save_userkey(outfilename, ssh2key, new_passphrase);
981 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
986 if (!move(outfiletmp, outfile))
987 return 1; /* rename failed */
997 fp = f_open(outfilename, "w", FALSE);
1002 ssh1_write_pubkey(fp, ssh1key);
1006 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
1010 ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
1011 ssh2blob, ssh2bloblen,
1012 (outtype == PUBLIC ?
1013 SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
1014 SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
1029 fingerprint = snewn(128, char);
1030 rsa_fingerprint(fingerprint, 128, ssh1key);
1033 fingerprint = ssh2_fingerprint(ssh2key->alg,
1037 fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen);
1042 fp = f_open(outfilename, "w", FALSE);
1045 fprintf(fp, "%s\n", fingerprint);
1056 assert(sshver == 2);
1058 random_ref(); /* both foreign key types require randomness,
1059 * for IV or padding */
1062 real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
1065 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1068 real_outtype = SSH_KEYTYPE_SSHCOM;
1071 assert(0 && "control flow goof");
1073 ret = export_ssh2(outfilename, real_outtype, ssh2key, new_passphrase);
1075 fprintf(stderr, "puttygen: unable to export key\n");
1079 if (!move(outfiletmp, outfile))
1080 return 1; /* rename failed */
1085 if (old_passphrase) {
1086 smemclr(old_passphrase, strlen(old_passphrase));
1087 sfree(old_passphrase);
1089 if (new_passphrase) {
1090 smemclr(new_passphrase, strlen(new_passphrase));
1091 sfree(new_passphrase);
1095 freersakey(ssh1key);
1097 ssh2key->alg->freekey(ssh2key->data);
1112 void setup_passphrases(char *first, ...)
1119 prompts[nprompts++] = first;
1120 va_start(ap, first);
1121 while ((next = va_arg(ap, char *)) != NULL) {
1122 assert(nprompts < lenof(prompts));
1123 prompts[nprompts++] = next;
1129 void test(int retval, ...)
1136 va_start(ap, retval);
1137 while (va_arg(ap, char *) != NULL)
1141 argv = snewn(argc+1, char *);
1142 va_start(ap, retval);
1143 for (i = 0; i <= argc; i++)
1144 argv[i] = va_arg(ap, char *);
1148 ret = cmdgen_main(argc, argv);
1150 if (ret != retval) {
1151 printf("FAILED retval (exp %d got %d):", retval, ret);
1152 for (i = 0; i < argc; i++)
1153 printf(" %s", argv[i]);
1156 } else if (promptsgot != nprompts) {
1157 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1158 for (i = 0; i < argc; i++)
1159 printf(" %s", argv[i]);
1169 void filecmp(char *file1, char *file2, char *fmt, ...)
1172 * Ideally I should do file comparison myself, to maximise the
1173 * portability of this test suite once this application begins
1174 * running on non-Unix platforms. For the moment, though,
1175 * calling Unix diff is perfectly adequate.
1180 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1187 printf("FAILED diff (ret=%d): ", ret);
1200 char *cleanup_fp(char *s)
1204 if (!strncmp(s, "ssh-", 4)) {
1205 s += strcspn(s, " \n\t");
1206 s += strspn(s, " \n\t");
1210 s += strcspn(s, " \n\t");
1211 s += strspn(s, " \n\t");
1212 s += strcspn(s, " \n\t");
1214 return dupprintf("%.*s", (int)(s - p), p);
1217 char *get_fp(char *filename)
1220 char buf[256], *ret;
1222 fp = fopen(filename, "r");
1225 ret = fgets(buf, sizeof(buf), fp);
1229 return cleanup_fp(buf);
1232 void check_fp(char *filename, char *fp, char *fmt, ...)
1239 newfp = get_fp(filename);
1241 if (!strcmp(fp, newfp)) {
1246 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1260 int main(int argc, char **argv)
1263 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1266 * Even when this thing is compiled for automatic test mode,
1267 * it's helpful to be able to invoke it with command-line
1268 * options for _manual_ tests.
1271 return cmdgen_main(argc, argv);
1275 for (i = 0; i < lenof(keytypes); i++) {
1276 char filename[128], osfilename[128], scfilename[128];
1277 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1280 sprintf(filename, "test-%s.ppk", keytypes[i]);
1281 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1282 sprintf(osfilename, "test-%s.os", keytypes[i]);
1283 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1284 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1285 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1288 * Create an encrypted key.
1290 setup_passphrases("sponge", "sponge", NULL);
1291 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1294 * List the public key in OpenSSH format.
1296 setup_passphrases(NULL);
1297 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1301 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1302 pubfilename, tmpfilename1);
1303 if (system(cmdbuf) ||
1304 (fp = get_fp(tmpfilename1)) == NULL) {
1305 printf("UNABLE to test fingerprint matching against OpenSSH");
1310 * List the public key in IETF/ssh.com format.
1312 setup_passphrases(NULL);
1313 test(0, "puttygen", "-p", filename, NULL);
1316 * List the fingerprint of the key.
1318 setup_passphrases(NULL);
1319 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1322 * If we can't test fingerprints against OpenSSH, we
1323 * can at the very least test equality of all the
1324 * fingerprints we generate of this key throughout
1327 fp = get_fp(tmpfilename1);
1329 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1333 * Change the comment of the key; this _does_ require a
1334 * passphrase owing to the tamperproofing.
1336 * NOTE: In SSH-1, this only requires a passphrase because
1337 * of inadequacies of the loading and saving mechanisms. In
1338 * _principle_, it should be perfectly possible to modify
1339 * the comment on an SSH-1 key without requiring a
1340 * passphrase; the only reason I can't do it is because my
1341 * loading and saving mechanisms don't include a method of
1342 * loading all the key data without also trying to decrypt
1343 * the private section.
1345 * I don't consider this to be a problem worth solving,
1346 * because (a) to fix it would probably end up bloating
1347 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1348 * it shouldn't be highly significant. If it seriously
1349 * bothers anyone then perhaps I _might_ be persuadable.
1351 setup_passphrases("sponge", NULL);
1352 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1355 * Change the passphrase to nothing.
1357 setup_passphrases("sponge", "", "", NULL);
1358 test(0, "puttygen", "-P", filename, NULL);
1361 * Change the comment of the key again; this time we expect no
1362 * passphrase to be required.
1364 setup_passphrases(NULL);
1365 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1368 * Export the private key into OpenSSH format; no passphrase
1369 * should be required since the key is currently unencrypted.
1370 * For RSA1 keys, this should give an error.
1372 setup_passphrases(NULL);
1373 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1378 * List the fingerprint of the OpenSSH-formatted key.
1380 setup_passphrases(NULL);
1381 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1382 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1385 * List the public half of the OpenSSH-formatted key in
1388 setup_passphrases(NULL);
1389 test(0, "puttygen", "-L", osfilename, NULL);
1392 * List the public half of the OpenSSH-formatted key in
1393 * IETF/ssh.com format.
1395 setup_passphrases(NULL);
1396 test(0, "puttygen", "-p", osfilename, NULL);
1400 * Export the private key into ssh.com format; no passphrase
1401 * should be required since the key is currently unencrypted.
1402 * For RSA1 keys, this should give an error.
1404 setup_passphrases(NULL);
1405 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1410 * List the fingerprint of the ssh.com-formatted key.
1412 setup_passphrases(NULL);
1413 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1414 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1417 * List the public half of the ssh.com-formatted key in
1420 setup_passphrases(NULL);
1421 test(0, "puttygen", "-L", scfilename, NULL);
1424 * List the public half of the ssh.com-formatted key in
1425 * IETF/ssh.com format.
1427 setup_passphrases(NULL);
1428 test(0, "puttygen", "-p", scfilename, NULL);
1433 * Convert from OpenSSH into ssh.com.
1435 setup_passphrases(NULL);
1436 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1437 "-O", "private-sshcom", NULL);
1440 * Convert from ssh.com back into a PuTTY key,
1441 * supplying the same comment as we had before we
1442 * started to ensure the comparison works.
1444 setup_passphrases(NULL);
1445 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1446 "-o", tmpfilename2, NULL);
1449 * See if the PuTTY key thus generated is the same as
1452 filecmp(filename, tmpfilename2,
1453 "p->o->s->p clear %s", keytypes[i]);
1456 * Convert from ssh.com to OpenSSH.
1458 setup_passphrases(NULL);
1459 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1460 "-O", "private-openssh", NULL);
1463 * Convert from OpenSSH back into a PuTTY key,
1464 * supplying the same comment as we had before we
1465 * started to ensure the comparison works.
1467 setup_passphrases(NULL);
1468 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1469 "-o", tmpfilename2, NULL);
1472 * See if the PuTTY key thus generated is the same as
1475 filecmp(filename, tmpfilename2,
1476 "p->s->o->p clear %s", keytypes[i]);
1479 * Finally, do a round-trip conversion between PuTTY
1480 * and ssh.com without involving OpenSSH, to test that
1481 * the key comment is preserved in that case.
1483 setup_passphrases(NULL);
1484 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1486 setup_passphrases(NULL);
1487 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1488 filecmp(filename, tmpfilename2,
1489 "p->s->p clear %s", keytypes[i]);
1493 * Check that mismatched passphrases cause an error.
1495 setup_passphrases("sponge2", "sponge3", NULL);
1496 test(1, "puttygen", "-P", filename, NULL);
1499 * Put a passphrase back on.
1501 setup_passphrases("sponge2", "sponge2", NULL);
1502 test(0, "puttygen", "-P", filename, NULL);
1505 * Export the private key into OpenSSH format, this time
1506 * while encrypted. For RSA1 keys, this should give an
1510 setup_passphrases(NULL); /* error, hence no passphrase read */
1512 setup_passphrases("sponge2", NULL);
1513 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1518 * List the fingerprint of the OpenSSH-formatted key.
1520 setup_passphrases("sponge2", NULL);
1521 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1522 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1525 * List the public half of the OpenSSH-formatted key in
1528 setup_passphrases("sponge2", NULL);
1529 test(0, "puttygen", "-L", osfilename, NULL);
1532 * List the public half of the OpenSSH-formatted key in
1533 * IETF/ssh.com format.
1535 setup_passphrases("sponge2", NULL);
1536 test(0, "puttygen", "-p", osfilename, NULL);
1540 * Export the private key into ssh.com format, this time
1541 * while encrypted. For RSA1 keys, this should give an
1545 setup_passphrases(NULL); /* error, hence no passphrase read */
1547 setup_passphrases("sponge2", NULL);
1548 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1553 * List the fingerprint of the ssh.com-formatted key.
1555 setup_passphrases("sponge2", NULL);
1556 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1557 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1560 * List the public half of the ssh.com-formatted key in
1563 setup_passphrases("sponge2", NULL);
1564 test(0, "puttygen", "-L", scfilename, NULL);
1567 * List the public half of the ssh.com-formatted key in
1568 * IETF/ssh.com format.
1570 setup_passphrases("sponge2", NULL);
1571 test(0, "puttygen", "-p", scfilename, NULL);
1576 * Convert from OpenSSH into ssh.com.
1578 setup_passphrases("sponge2", NULL);
1579 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1580 "-O", "private-sshcom", NULL);
1583 * Convert from ssh.com back into a PuTTY key,
1584 * supplying the same comment as we had before we
1585 * started to ensure the comparison works.
1587 setup_passphrases("sponge2", NULL);
1588 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1589 "-o", tmpfilename2, NULL);
1592 * See if the PuTTY key thus generated is the same as
1595 filecmp(filename, tmpfilename2,
1596 "p->o->s->p encrypted %s", keytypes[i]);
1599 * Convert from ssh.com to OpenSSH.
1601 setup_passphrases("sponge2", NULL);
1602 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1603 "-O", "private-openssh", NULL);
1606 * Convert from OpenSSH back into a PuTTY key,
1607 * supplying the same comment as we had before we
1608 * started to ensure the comparison works.
1610 setup_passphrases("sponge2", NULL);
1611 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1612 "-o", tmpfilename2, NULL);
1615 * See if the PuTTY key thus generated is the same as
1618 filecmp(filename, tmpfilename2,
1619 "p->s->o->p encrypted %s", keytypes[i]);
1622 * Finally, do a round-trip conversion between PuTTY
1623 * and ssh.com without involving OpenSSH, to test that
1624 * the key comment is preserved in that case.
1626 setup_passphrases("sponge2", NULL);
1627 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1629 setup_passphrases("sponge2", NULL);
1630 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1631 filecmp(filename, tmpfilename2,
1632 "p->s->p encrypted %s", keytypes[i]);
1636 * Load with the wrong passphrase.
1638 setup_passphrases("sponge8", NULL);
1639 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1642 * Load a totally bogus file.
1644 setup_passphrases(NULL);
1645 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1647 printf("%d passes, %d fails\n", passes, fails);