2 * cmdgen.c - command-line form of PuTTYgen
5 #define PUTTY_DO_GLOBALS
21 * This section overrides some definitions below for test purposes.
22 * When compiled with -DTEST_CMDGEN (as cgtest.c will do):
24 * - Calls to get_random_data() are replaced with the diagnostic
25 * function below (I #define the name so that I can still link
26 * with the original set of modules without symbol clash), in
27 * order to avoid depleting the test system's /dev/random
30 * - Calls to console_get_userpass_input() are replaced with the
31 * diagnostic function below, so that I can run tests in an
32 * automated manner and provide their interactive passphrase
35 * - main() is renamed to cmdgen_main(); at the bottom of the file
36 * I define another main() which calls the former repeatedly to
39 #define get_random_data get_random_data_diagnostic
40 char *get_random_data(int len, const char *device)
42 char *buf = snewn(len, char);
43 memset(buf, 'x', len);
46 #define console_get_userpass_input console_get_userpass_input_diagnostic
47 int nprompts, promptsgot;
48 const char *prompts[3];
49 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
53 for (i = 0; i < p->n_prompts; i++) {
54 if (promptsgot < nprompts) {
55 p->prompts[i]->result = dupstr(prompts[promptsgot++]);
57 promptsgot++; /* track number of requests anyway */
63 #define main cmdgen_main
70 static void progress_update(void *param, int action, int phase, int iprogress)
72 struct progress *p = (struct progress *)param;
73 if (action != PROGFN_PROGRESS)
75 if (phase > p->phase) {
80 p->current = iprogress - 1;
82 p->current = iprogress;
84 while (p->current < iprogress) {
91 static void no_progress(void *param, int action, int phase, int iprogress)
95 void modalfatalbox(const char *p, ...)
98 fprintf(stderr, "FATAL ERROR: ");
100 vfprintf(stderr, p, ap);
106 void nonfatal(const char *p, ...)
109 fprintf(stderr, "ERROR: ");
111 vfprintf(stderr, p, ap);
117 * Stubs to let everything else link sensibly.
119 void log_eventlog(void *handle, const char *event)
122 char *x_get_default(const char *key)
126 void sk_cleanup(void)
130 void showversion(void)
132 printf("puttygen: %s\n", ver);
135 void usage(int standalone)
138 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
139 " [ -C comment ] [ -P ] [ -q ]\n"
140 " [ -o output-keyfile ] [ -O type | -l | -L"
144 "Use \"puttygen --help\" for more detail.\n");
150 * Help message is an extended version of the usage message. So
151 * start with that, plus a version heading.
156 " -t specify key type when generating (ed25519, ecdsa, rsa, "
158 " -b specify number of bits when generating key\n"
159 " -C change or specify key comment\n"
160 " -P change key passphrase\n"
161 " -q quiet: do not display progress bar\n"
162 " -O specify output type:\n"
163 " private output PuTTY private key format\n"
164 " private-openssh export OpenSSH private key\n"
165 " private-openssh-new export OpenSSH private key "
166 "(force new file format)\n"
167 " private-sshcom export ssh.com private key\n"
168 " public RFC 4716 / ssh.com public key\n"
169 " public-openssh OpenSSH public key\n"
170 " fingerprint output the key fingerprint\n"
171 " -o specify output file\n"
172 " -l equivalent to `-O fingerprint'\n"
173 " -L equivalent to `-O public-openssh'\n"
174 " -p equivalent to `-O public'\n"
175 " --old-passphrase file\n"
176 " specify file containing old key passphrase\n"
177 " --new-passphrase file\n"
178 " specify file containing new key passphrase\n"
179 " --random-device device\n"
180 " specify device to read entropy from (e.g. /dev/urandom)\n"
184 static int move(char *from, char *to)
188 ret = rename(from, to);
191 * This OS may require us to remove the original file first.
194 ret = rename(from, to);
197 perror("puttygen: cannot move new file on to old one");
203 static char *readpassphrase(const char *filename)
208 fp = fopen(filename, "r");
210 fprintf(stderr, "puttygen: cannot open %s: %s\n",
211 filename, strerror(errno));
216 line[strcspn(line, "\r\n")] = '\0';
218 fprintf(stderr, "puttygen: error reading from %s: %s\n",
219 filename, strerror(errno));
220 else /* empty file */
226 #define DEFAULT_RSADSA_BITS 2048
228 int main(int argc, char **argv)
231 Filename *infilename = NULL, *outfilename = NULL;
232 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
233 char *outfile = NULL, *outfiletmp = NULL;
234 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
235 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
237 char *comment = NULL, *origcomment = NULL;
238 int change_passphrase = FALSE;
239 int errs = FALSE, nogo = FALSE;
240 int intype = SSH_KEYTYPE_UNOPENABLE;
242 struct ssh2_userkey *ssh2key = NULL;
243 struct RSAKey *ssh1key = NULL;
244 unsigned char *ssh2blob = NULL;
245 char *ssh2alg = NULL;
246 const struct ssh_signkey *ssh2algf = NULL;
248 char *old_passphrase = NULL, *new_passphrase = NULL;
250 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
251 const char *random_device = NULL;
253 /* ------------------------------------------------------------------
254 * Parse the command line to figure out what we've been asked to do.
258 * If run with no arguments at all, print the usage message and
267 * Parse command line arguments.
284 opt = p++; /* opt will have _one_ leading - */
285 while (*p && *p != '=')
286 p++; /* find end of option */
293 if (!strcmp(opt, "-help")) {
296 fprintf(stderr, "puttygen: option `-%s'"
297 " expects no argument\n", opt);
302 } else if (!strcmp(opt, "-version")) {
305 fprintf(stderr, "puttygen: option `-%s'"
306 " expects no argument\n", opt);
311 } else if (!strcmp(opt, "-pgpfp")) {
314 fprintf(stderr, "puttygen: option `-%s'"
315 " expects no argument\n", opt);
317 /* support --pgpfp for consistency */
321 } else if (!strcmp(opt, "-old-passphrase")) {
322 if (!val && argc > 1)
323 --argc, val = *++argv;
326 fprintf(stderr, "puttygen: option `-%s'"
327 " expects an argument\n", opt);
329 old_passphrase = readpassphrase(val);
333 } else if (!strcmp(opt, "-new-passphrase")) {
334 if (!val && argc > 1)
335 --argc, val = *++argv;
338 fprintf(stderr, "puttygen: option `-%s'"
339 " expects an argument\n", opt);
341 new_passphrase = readpassphrase(val);
345 } else if (!strcmp(opt, "-random-device")) {
346 if (!val && argc > 1)
347 --argc, val = *++argv;
350 fprintf(stderr, "puttygen: option `-%s'"
351 " expects an argument\n", opt);
358 "puttygen: no such option `-%s'\n", opt);
371 * Option requiring no parameter.
383 change_passphrase = TRUE;
395 progressfn = no_progress;
405 * Option requiring parameter.
411 fprintf(stderr, "puttygen: option `-%c' expects a"
416 * Now c is the option and p is the parameter.
420 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
421 keytype = RSA2, sshver = 2;
422 else if (!strcmp(p, "rsa1"))
423 keytype = RSA1, sshver = 1;
424 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
425 keytype = DSA, sshver = 2;
426 else if (!strcmp(p, "ecdsa"))
427 keytype = ECDSA, sshver = 2;
428 else if (!strcmp(p, "ed25519"))
429 keytype = ED25519, sshver = 2;
432 "puttygen: unknown key type `%s'\n", p);
443 if (!strcmp(p, "public"))
445 else if (!strcmp(p, "public-openssh"))
447 else if (!strcmp(p, "private"))
449 else if (!strcmp(p, "fingerprint"))
451 else if (!strcmp(p, "private-openssh"))
452 outtype = OPENSSH_AUTO, sshver = 2;
453 else if (!strcmp(p, "private-openssh-new"))
454 outtype = OPENSSH_NEW, sshver = 2;
455 else if (!strcmp(p, "private-sshcom"))
456 outtype = SSHCOM, sshver = 2;
459 "puttygen: unknown output type `%s'\n", p);
467 p = NULL; /* prevent continued processing */
471 * Unrecognised option.
474 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
480 * A non-option argument.
486 fprintf(stderr, "puttygen: cannot handle more than one"
494 * No explicit key size was specified. Default varies
495 * depending on key type.
505 bits = DEFAULT_RSADSA_BITS;
510 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
511 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
515 if (keytype == ED25519 && (bits != 256)) {
516 fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
520 if (keytype == RSA2 || keytype == RSA1 || keytype == DSA) {
522 fprintf(stderr, "puttygen: cannot generate %s keys shorter than"
523 " 256 bits\n", (keytype == DSA ? "DSA" : "RSA"));
525 } else if (bits < DEFAULT_RSADSA_BITS) {
526 fprintf(stderr, "puttygen: warning: %s keys shorter than"
527 " %d bits are probably not secure\n",
528 (keytype == DSA ? "DSA" : "RSA"), DEFAULT_RSADSA_BITS);
529 /* but this is just a warning, so proceed anyway */
540 * If run with at least one argument _but_ not the required
541 * ones, print the usage message and return failure.
543 if (!infile && keytype == NOKEYGEN) {
548 /* ------------------------------------------------------------------
549 * Figure out further details of exactly what we're going to do.
553 * Bomb out if we've been asked to both load and generate a
556 if (keytype != NOKEYGEN && infile) {
557 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
562 * We must save the private part when generating a new key.
564 if (keytype != NOKEYGEN &&
565 (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
566 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
567 fprintf(stderr, "puttygen: this would generate a new key but "
568 "discard the private part\n");
573 * Analyse the type of the input file, in case this affects our
577 infilename = filename_from_str(infile);
579 intype = key_type(infilename);
582 case SSH_KEYTYPE_UNOPENABLE:
583 case SSH_KEYTYPE_UNKNOWN:
584 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
585 infile, key_type_to_str(intype));
588 case SSH_KEYTYPE_SSH1:
589 case SSH_KEYTYPE_SSH1_PUBLIC:
591 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
598 case SSH_KEYTYPE_SSH2:
599 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
600 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
601 case SSH_KEYTYPE_OPENSSH_PEM:
602 case SSH_KEYTYPE_OPENSSH_NEW:
603 case SSH_KEYTYPE_SSHCOM:
605 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
612 case SSH_KEYTYPE_OPENSSH_AUTO:
614 assert(0 && "Should never see these types on an input file");
619 * Determine the default output file, if none is provided.
621 * This will usually be equal to stdout, except that if the
622 * input and output file formats are the same then the default
623 * output is to overwrite the input.
625 * Also in this code, we bomb out if the input and output file
626 * formats are the same and no other action is performed.
628 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
629 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
630 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
631 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
632 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
635 outfiletmp = dupcat(outfile, ".tmp", NULL);
638 if (!change_passphrase && !comment) {
639 fprintf(stderr, "puttygen: this command would perform no useful"
646 * Bomb out rather than automatically choosing to write
647 * a private key file to stdout.
649 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
650 outtype == OPENSSH_NEW || outtype == SSHCOM) {
651 fprintf(stderr, "puttygen: need to specify an output file\n");
658 * Figure out whether we need to load the encrypted part of the
659 * key. This will be the case if either (a) we need to write
660 * out a private key format, or (b) the entire input key file
663 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
664 outtype == OPENSSH_NEW || outtype == SSHCOM ||
665 intype == SSH_KEYTYPE_OPENSSH_PEM ||
666 intype == SSH_KEYTYPE_OPENSSH_NEW ||
667 intype == SSH_KEYTYPE_SSHCOM)
668 load_encrypted = TRUE;
670 load_encrypted = FALSE;
672 if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
673 intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
674 intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
675 fprintf(stderr, "puttygen: cannot perform this action on a "
676 "public-key-only input file\n");
680 /* ------------------------------------------------------------------
681 * Now we're ready to actually do some stuff.
685 * Either load or generate a key.
687 if (keytype != NOKEYGEN) {
689 char default_comment[80];
691 struct progress prog;
698 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
699 else if (keytype == ECDSA)
700 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
701 else if (keytype == ED25519)
702 strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
704 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
707 entropy = get_random_data(bits / 8, random_device);
709 fprintf(stderr, "puttygen: failed to collect entropy, "
710 "could not generate key\n");
713 random_add_heavynoise(entropy, bits / 8);
714 smemclr(entropy, bits/8);
717 if (keytype == DSA) {
718 struct dss_key *dsskey = snew(struct dss_key);
719 dsa_generate(dsskey, bits, progressfn, &prog);
720 ssh2key = snew(struct ssh2_userkey);
721 ssh2key->data = dsskey;
722 ssh2key->alg = &ssh_dss;
724 } else if (keytype == ECDSA) {
725 struct ec_key *ec = snew(struct ec_key);
726 ec_generate(ec, bits, progressfn, &prog);
727 ssh2key = snew(struct ssh2_userkey);
729 ssh2key->alg = ec->signalg;
731 } else if (keytype == ED25519) {
732 struct ec_key *ec = snew(struct ec_key);
733 ec_edgenerate(ec, bits, progressfn, &prog);
734 ssh2key = snew(struct ssh2_userkey);
736 ssh2key->alg = &ssh_ecdsa_ed25519;
739 struct RSAKey *rsakey = snew(struct RSAKey);
740 rsa_generate(rsakey, bits, progressfn, &prog);
741 rsakey->comment = NULL;
742 if (keytype == RSA1) {
745 ssh2key = snew(struct ssh2_userkey);
746 ssh2key->data = rsakey;
747 ssh2key->alg = &ssh_rsa;
750 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
753 ssh2key->comment = dupstr(default_comment);
755 ssh1key->comment = dupstr(default_comment);
758 const char *error = NULL;
761 assert(infile != NULL);
764 * Find out whether the input key is encrypted.
766 if (intype == SSH_KEYTYPE_SSH1)
767 encrypted = rsakey_encrypted(infilename, &origcomment);
768 else if (intype == SSH_KEYTYPE_SSH2)
769 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
771 encrypted = import_encrypted(infilename, intype, &origcomment);
774 * If so, ask for a passphrase.
776 if (encrypted && load_encrypted) {
777 if (!old_passphrase) {
778 prompts_t *p = new_prompts(NULL);
780 p->to_server = FALSE;
781 p->name = dupstr("SSH key passphrase");
782 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
783 ret = console_get_userpass_input(p, NULL, 0);
787 perror("puttygen: unable to read passphrase");
790 old_passphrase = dupstr(p->prompts[0]->result);
795 old_passphrase = NULL;
801 case SSH_KEYTYPE_SSH1:
802 case SSH_KEYTYPE_SSH1_PUBLIC:
803 ssh1key = snew(struct RSAKey);
804 if (!load_encrypted) {
809 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
810 &origcomment, &error);
811 blob = (unsigned char *)vblob;
813 n = 4; /* skip modulus bits */
815 l = ssh1_read_bignum(blob + n, bloblen - n,
818 error = "SSH-1 public key blob was too short";
821 l = ssh1_read_bignum(blob + n, bloblen - n,
824 error = "SSH-1 public key blob was too short";
828 ssh1key->comment = dupstr(origcomment);
829 ssh1key->private_exponent = NULL;
832 ssh1key->iqmp = NULL;
834 ret = loadrsakey(infilename, ssh1key, old_passphrase, &error);
839 error = "unknown error";
842 case SSH_KEYTYPE_SSH2:
843 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
844 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
845 if (!load_encrypted) {
846 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
847 &ssh2bloblen, &origcomment,
850 ssh2algf = find_pubkey_alg(ssh2alg);
852 bits = ssh2algf->pubkey_bits(ssh2algf,
853 ssh2blob, ssh2bloblen);
859 ssh2key = ssh2_load_userkey(infilename, old_passphrase,
862 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
865 if (ssh2key == SSH2_WRONG_PASSPHRASE)
866 error = "wrong passphrase";
868 error = "unknown error";
872 case SSH_KEYTYPE_OPENSSH_PEM:
873 case SSH_KEYTYPE_OPENSSH_NEW:
874 case SSH_KEYTYPE_SSHCOM:
875 ssh2key = import_ssh2(infilename, intype, old_passphrase, &error);
877 if (ssh2key != SSH2_WRONG_PASSPHRASE)
880 error = "wrong passphrase";
882 error = "unknown error";
890 fprintf(stderr, "puttygen: error loading `%s': %s\n",
897 * Change the comment if asked to.
902 sfree(ssh1key->comment);
903 ssh1key->comment = dupstr(comment);
906 sfree(ssh2key->comment);
907 ssh2key->comment = dupstr(comment);
912 * Unless we're changing the passphrase, the old one (if any) is a
913 * reasonable default.
915 if (!change_passphrase && old_passphrase && !new_passphrase)
916 new_passphrase = dupstr(old_passphrase);
919 * Prompt for a new passphrase if we have been asked to, or if
920 * we have just generated a key.
922 if (!new_passphrase && (change_passphrase || keytype != NOKEYGEN)) {
923 prompts_t *p = new_prompts(NULL);
926 p->to_server = FALSE;
927 p->name = dupstr("New SSH key passphrase");
928 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
929 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
930 ret = console_get_userpass_input(p, NULL, 0);
934 perror("puttygen: unable to read new passphrase");
937 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
939 fprintf(stderr, "puttygen: passphrases do not match\n");
942 new_passphrase = dupstr(p->prompts[0]->result);
946 if (new_passphrase && !*new_passphrase) {
947 sfree(new_passphrase);
948 new_passphrase = NULL;
954 * (In the case where outfile and outfiletmp are both NULL,
955 * there is no semantic reason to initialise outfilename at
956 * all; but we have to write _something_ to it or some compiler
957 * will probably complain that it might be used uninitialised.)
960 outfilename = filename_from_str(outfiletmp);
962 outfilename = filename_from_str(outfile ? outfile : "");
965 int ret, real_outtype;
970 ret = saversakey(outfilename, ssh1key, new_passphrase);
972 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
977 ret = ssh2_save_userkey(outfilename, ssh2key, new_passphrase);
979 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
984 if (!move(outfiletmp, outfile))
985 return 1; /* rename failed */
995 fp = f_open(outfilename, "w", FALSE);
1000 ssh1_write_pubkey(fp, ssh1key);
1004 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
1008 ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
1009 ssh2blob, ssh2bloblen,
1010 (outtype == PUBLIC ?
1011 SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
1012 SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
1027 fingerprint = snewn(128, char);
1028 rsa_fingerprint(fingerprint, 128, ssh1key);
1031 fingerprint = ssh2_fingerprint(ssh2key->alg,
1035 fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen);
1040 fp = f_open(outfilename, "w", FALSE);
1043 fprintf(fp, "%s\n", fingerprint);
1054 assert(sshver == 2);
1056 random_ref(); /* both foreign key types require randomness,
1057 * for IV or padding */
1060 real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
1063 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1066 real_outtype = SSH_KEYTYPE_SSHCOM;
1069 assert(0 && "control flow goof");
1071 ret = export_ssh2(outfilename, real_outtype, ssh2key, new_passphrase);
1073 fprintf(stderr, "puttygen: unable to export key\n");
1077 if (!move(outfiletmp, outfile))
1078 return 1; /* rename failed */
1083 if (old_passphrase) {
1084 smemclr(old_passphrase, strlen(old_passphrase));
1085 sfree(old_passphrase);
1087 if (new_passphrase) {
1088 smemclr(new_passphrase, strlen(new_passphrase));
1089 sfree(new_passphrase);
1093 freersakey(ssh1key);
1095 ssh2key->alg->freekey(ssh2key->data);
1110 void setup_passphrases(char *first, ...)
1117 prompts[nprompts++] = first;
1118 va_start(ap, first);
1119 while ((next = va_arg(ap, char *)) != NULL) {
1120 assert(nprompts < lenof(prompts));
1121 prompts[nprompts++] = next;
1127 void test(int retval, ...)
1134 va_start(ap, retval);
1135 while (va_arg(ap, char *) != NULL)
1139 argv = snewn(argc+1, char *);
1140 va_start(ap, retval);
1141 for (i = 0; i <= argc; i++)
1142 argv[i] = va_arg(ap, char *);
1146 ret = cmdgen_main(argc, argv);
1148 if (ret != retval) {
1149 printf("FAILED retval (exp %d got %d):", retval, ret);
1150 for (i = 0; i < argc; i++)
1151 printf(" %s", argv[i]);
1154 } else if (promptsgot != nprompts) {
1155 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1156 for (i = 0; i < argc; i++)
1157 printf(" %s", argv[i]);
1165 void filecmp(char *file1, char *file2, char *fmt, ...)
1168 * Ideally I should do file comparison myself, to maximise the
1169 * portability of this test suite once this application begins
1170 * running on non-Unix platforms. For the moment, though,
1171 * calling Unix diff is perfectly adequate.
1176 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1183 printf("FAILED diff (ret=%d): ", ret);
1196 char *cleanup_fp(char *s)
1200 if (!strncmp(s, "ssh-", 4)) {
1201 s += strcspn(s, " \n\t");
1202 s += strspn(s, " \n\t");
1206 s += strcspn(s, " \n\t");
1207 s += strspn(s, " \n\t");
1208 s += strcspn(s, " \n\t");
1210 return dupprintf("%.*s", (int)(s - p), p);
1213 char *get_fp(char *filename)
1216 char buf[256], *ret;
1218 fp = fopen(filename, "r");
1221 ret = fgets(buf, sizeof(buf), fp);
1225 return cleanup_fp(buf);
1228 void check_fp(char *filename, char *fp, char *fmt, ...)
1235 newfp = get_fp(filename);
1237 if (!strcmp(fp, newfp)) {
1242 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1256 int main(int argc, char **argv)
1259 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1262 * Even when this thing is compiled for automatic test mode,
1263 * it's helpful to be able to invoke it with command-line
1264 * options for _manual_ tests.
1267 return cmdgen_main(argc, argv);
1271 for (i = 0; i < lenof(keytypes); i++) {
1272 char filename[128], osfilename[128], scfilename[128];
1273 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1276 sprintf(filename, "test-%s.ppk", keytypes[i]);
1277 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1278 sprintf(osfilename, "test-%s.os", keytypes[i]);
1279 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1280 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1281 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1284 * Create an encrypted key.
1286 setup_passphrases("sponge", "sponge", NULL);
1287 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1290 * List the public key in OpenSSH format.
1292 setup_passphrases(NULL);
1293 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1297 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1298 pubfilename, tmpfilename1);
1299 if (system(cmdbuf) ||
1300 (fp = get_fp(tmpfilename1)) == NULL) {
1301 printf("UNABLE to test fingerprint matching against OpenSSH");
1306 * List the public key in IETF/ssh.com format.
1308 setup_passphrases(NULL);
1309 test(0, "puttygen", "-p", filename, NULL);
1312 * List the fingerprint of the key.
1314 setup_passphrases(NULL);
1315 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1318 * If we can't test fingerprints against OpenSSH, we
1319 * can at the very least test equality of all the
1320 * fingerprints we generate of this key throughout
1323 fp = get_fp(tmpfilename1);
1325 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1329 * Change the comment of the key; this _does_ require a
1330 * passphrase owing to the tamperproofing.
1332 * NOTE: In SSH-1, this only requires a passphrase because
1333 * of inadequacies of the loading and saving mechanisms. In
1334 * _principle_, it should be perfectly possible to modify
1335 * the comment on an SSH-1 key without requiring a
1336 * passphrase; the only reason I can't do it is because my
1337 * loading and saving mechanisms don't include a method of
1338 * loading all the key data without also trying to decrypt
1339 * the private section.
1341 * I don't consider this to be a problem worth solving,
1342 * because (a) to fix it would probably end up bloating
1343 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1344 * it shouldn't be highly significant. If it seriously
1345 * bothers anyone then perhaps I _might_ be persuadable.
1347 setup_passphrases("sponge", NULL);
1348 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1351 * Change the passphrase to nothing.
1353 setup_passphrases("sponge", "", "", NULL);
1354 test(0, "puttygen", "-P", filename, NULL);
1357 * Change the comment of the key again; this time we expect no
1358 * passphrase to be required.
1360 setup_passphrases(NULL);
1361 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1364 * Export the private key into OpenSSH format; no passphrase
1365 * should be required since the key is currently unencrypted.
1366 * For RSA1 keys, this should give an error.
1368 setup_passphrases(NULL);
1369 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1374 * List the fingerprint of the OpenSSH-formatted key.
1376 setup_passphrases(NULL);
1377 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1378 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1381 * List the public half of the OpenSSH-formatted key in
1384 setup_passphrases(NULL);
1385 test(0, "puttygen", "-L", osfilename, NULL);
1388 * List the public half of the OpenSSH-formatted key in
1389 * IETF/ssh.com format.
1391 setup_passphrases(NULL);
1392 test(0, "puttygen", "-p", osfilename, NULL);
1396 * Export the private key into ssh.com format; no passphrase
1397 * should be required since the key is currently unencrypted.
1398 * For RSA1 keys, this should give an error.
1400 setup_passphrases(NULL);
1401 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1406 * List the fingerprint of the ssh.com-formatted key.
1408 setup_passphrases(NULL);
1409 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1410 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1413 * List the public half of the ssh.com-formatted key in
1416 setup_passphrases(NULL);
1417 test(0, "puttygen", "-L", scfilename, NULL);
1420 * List the public half of the ssh.com-formatted key in
1421 * IETF/ssh.com format.
1423 setup_passphrases(NULL);
1424 test(0, "puttygen", "-p", scfilename, NULL);
1429 * Convert from OpenSSH into ssh.com.
1431 setup_passphrases(NULL);
1432 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1433 "-O", "private-sshcom", NULL);
1436 * Convert from ssh.com back into a PuTTY key,
1437 * supplying the same comment as we had before we
1438 * started to ensure the comparison works.
1440 setup_passphrases(NULL);
1441 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1442 "-o", tmpfilename2, NULL);
1445 * See if the PuTTY key thus generated is the same as
1448 filecmp(filename, tmpfilename2,
1449 "p->o->s->p clear %s", keytypes[i]);
1452 * Convert from ssh.com to OpenSSH.
1454 setup_passphrases(NULL);
1455 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1456 "-O", "private-openssh", NULL);
1459 * Convert from OpenSSH back into a PuTTY key,
1460 * supplying the same comment as we had before we
1461 * started to ensure the comparison works.
1463 setup_passphrases(NULL);
1464 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1465 "-o", tmpfilename2, NULL);
1468 * See if the PuTTY key thus generated is the same as
1471 filecmp(filename, tmpfilename2,
1472 "p->s->o->p clear %s", keytypes[i]);
1475 * Finally, do a round-trip conversion between PuTTY
1476 * and ssh.com without involving OpenSSH, to test that
1477 * the key comment is preserved in that case.
1479 setup_passphrases(NULL);
1480 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1482 setup_passphrases(NULL);
1483 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1484 filecmp(filename, tmpfilename2,
1485 "p->s->p clear %s", keytypes[i]);
1489 * Check that mismatched passphrases cause an error.
1491 setup_passphrases("sponge2", "sponge3", NULL);
1492 test(1, "puttygen", "-P", filename, NULL);
1495 * Put a passphrase back on.
1497 setup_passphrases("sponge2", "sponge2", NULL);
1498 test(0, "puttygen", "-P", filename, NULL);
1501 * Export the private key into OpenSSH format, this time
1502 * while encrypted. For RSA1 keys, this should give an
1506 setup_passphrases(NULL); /* error, hence no passphrase read */
1508 setup_passphrases("sponge2", NULL);
1509 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1514 * List the fingerprint of the OpenSSH-formatted key.
1516 setup_passphrases("sponge2", NULL);
1517 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1518 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1521 * List the public half of the OpenSSH-formatted key in
1524 setup_passphrases("sponge2", NULL);
1525 test(0, "puttygen", "-L", osfilename, NULL);
1528 * List the public half of the OpenSSH-formatted key in
1529 * IETF/ssh.com format.
1531 setup_passphrases("sponge2", NULL);
1532 test(0, "puttygen", "-p", osfilename, NULL);
1536 * Export the private key into ssh.com format, this time
1537 * while encrypted. For RSA1 keys, this should give an
1541 setup_passphrases(NULL); /* error, hence no passphrase read */
1543 setup_passphrases("sponge2", NULL);
1544 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1549 * List the fingerprint of the ssh.com-formatted key.
1551 setup_passphrases("sponge2", NULL);
1552 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1553 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1556 * List the public half of the ssh.com-formatted key in
1559 setup_passphrases("sponge2", NULL);
1560 test(0, "puttygen", "-L", scfilename, NULL);
1563 * List the public half of the ssh.com-formatted key in
1564 * IETF/ssh.com format.
1566 setup_passphrases("sponge2", NULL);
1567 test(0, "puttygen", "-p", scfilename, NULL);
1572 * Convert from OpenSSH into ssh.com.
1574 setup_passphrases("sponge2", NULL);
1575 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1576 "-O", "private-sshcom", NULL);
1579 * Convert from ssh.com back into a PuTTY key,
1580 * supplying the same comment as we had before we
1581 * started to ensure the comparison works.
1583 setup_passphrases("sponge2", NULL);
1584 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1585 "-o", tmpfilename2, NULL);
1588 * See if the PuTTY key thus generated is the same as
1591 filecmp(filename, tmpfilename2,
1592 "p->o->s->p encrypted %s", keytypes[i]);
1595 * Convert from ssh.com to OpenSSH.
1597 setup_passphrases("sponge2", NULL);
1598 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1599 "-O", "private-openssh", NULL);
1602 * Convert from OpenSSH back into a PuTTY key,
1603 * supplying the same comment as we had before we
1604 * started to ensure the comparison works.
1606 setup_passphrases("sponge2", NULL);
1607 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1608 "-o", tmpfilename2, NULL);
1611 * See if the PuTTY key thus generated is the same as
1614 filecmp(filename, tmpfilename2,
1615 "p->s->o->p encrypted %s", keytypes[i]);
1618 * Finally, do a round-trip conversion between PuTTY
1619 * and ssh.com without involving OpenSSH, to test that
1620 * the key comment is preserved in that case.
1622 setup_passphrases("sponge2", NULL);
1623 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1625 setup_passphrases("sponge2", NULL);
1626 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1627 filecmp(filename, tmpfilename2,
1628 "p->s->p encrypted %s", keytypes[i]);
1632 * Load with the wrong passphrase.
1634 setup_passphrases("sponge8", NULL);
1635 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1638 * Load a totally bogus file.
1640 setup_passphrases(NULL);
1641 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1643 printf("%d passes, %d fails\n", passes, fails);