2 * cmdgen.c - command-line form of PuTTYgen
5 #define PUTTY_DO_GLOBALS
21 * This section overrides some definitions below for test purposes.
22 * When compiled with -DTEST_CMDGEN (as cgtest.c will do):
24 * - Calls to get_random_data() are replaced with the diagnostic
25 * function below (I #define the name so that I can still link
26 * with the original set of modules without symbol clash), in
27 * order to avoid depleting the test system's /dev/random
30 * - Calls to console_get_userpass_input() are replaced with the
31 * diagnostic function below, so that I can run tests in an
32 * automated manner and provide their interactive passphrase
35 * - main() is renamed to cmdgen_main(); at the bottom of the file
36 * I define another main() which calls the former repeatedly to
39 #define get_random_data get_random_data_diagnostic
40 char *get_random_data(int len, const char *device)
42 char *buf = snewn(len, char);
43 memset(buf, 'x', len);
46 #define console_get_userpass_input console_get_userpass_input_diagnostic
47 int nprompts, promptsgot;
48 const char *prompts[3];
49 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
53 for (i = 0; i < p->n_prompts; i++) {
54 if (promptsgot < nprompts) {
55 p->prompts[i]->result = dupstr(prompts[promptsgot++]);
57 promptsgot++; /* track number of requests anyway */
63 #define main cmdgen_main
70 static void progress_update(void *param, int action, int phase, int iprogress)
72 struct progress *p = (struct progress *)param;
73 if (action != PROGFN_PROGRESS)
75 if (phase > p->phase) {
80 p->current = iprogress - 1;
82 p->current = iprogress;
84 while (p->current < iprogress) {
91 static void no_progress(void *param, int action, int phase, int iprogress)
95 void modalfatalbox(const char *p, ...)
98 fprintf(stderr, "FATAL ERROR: ");
100 vfprintf(stderr, p, ap);
106 void nonfatal(const char *p, ...)
109 fprintf(stderr, "ERROR: ");
111 vfprintf(stderr, p, ap);
117 * Stubs to let everything else link sensibly.
119 void log_eventlog(void *handle, const char *event)
122 char *x_get_default(const char *key)
126 void sk_cleanup(void)
130 void showversion(void)
132 printf("puttygen: %s\n", ver);
135 void usage(int standalone)
138 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
139 " [ -C comment ] [ -P ] [ -q ]\n"
140 " [ -o output-keyfile ] [ -O type | -l | -L"
144 "Use \"puttygen --help\" for more detail.\n");
150 * Help message is an extended version of the usage message. So
151 * start with that, plus a version heading.
156 " -t specify key type when generating (ed25519, ecdsa, rsa, "
158 " -b specify number of bits when generating key\n"
159 " -C change or specify key comment\n"
160 " -P change key passphrase\n"
161 " -q quiet: do not display progress bar\n"
162 " -O specify output type:\n"
163 " private output PuTTY private key format\n"
164 " private-openssh export OpenSSH private key\n"
165 " private-openssh-new export OpenSSH private key "
166 "(force new file format)\n"
167 " private-sshcom export ssh.com private key\n"
168 " public RFC 4716 / ssh.com public key\n"
169 " public-openssh OpenSSH public key\n"
170 " fingerprint output the key fingerprint\n"
171 " -o specify output file\n"
172 " -l equivalent to `-O fingerprint'\n"
173 " -L equivalent to `-O public-openssh'\n"
174 " -p equivalent to `-O public'\n"
175 " --old-passphrase file\n"
176 " specify file containing old key passphrase\n"
177 " --new-passphrase file\n"
178 " specify file containing new key passphrase\n"
179 " --random-device device\n"
180 " specify device to read entropy from (e.g. /dev/urandom)\n"
184 static int move(char *from, char *to)
188 ret = rename(from, to);
191 * This OS may require us to remove the original file first.
194 ret = rename(from, to);
197 perror("puttygen: cannot move new file on to old one");
203 static char *readpassphrase(const char *filename)
208 fp = fopen(filename, "r");
210 fprintf(stderr, "puttygen: cannot open %s: %s\n",
211 filename, strerror(errno));
216 line[strcspn(line, "\r\n")] = '\0';
218 fprintf(stderr, "puttygen: error reading from %s: %s\n",
219 filename, strerror(errno));
220 else /* empty file */
226 int main(int argc, char **argv)
229 Filename *infilename = NULL, *outfilename = NULL;
230 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
231 char *outfile = NULL, *outfiletmp = NULL;
232 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
233 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
235 char *comment = NULL, *origcomment = NULL;
236 int change_passphrase = FALSE;
237 int errs = FALSE, nogo = FALSE;
238 int intype = SSH_KEYTYPE_UNOPENABLE;
240 struct ssh2_userkey *ssh2key = NULL;
241 struct RSAKey *ssh1key = NULL;
242 unsigned char *ssh2blob = NULL;
243 char *ssh2alg = NULL;
244 const struct ssh_signkey *ssh2algf = NULL;
246 char *old_passphrase = NULL, *new_passphrase = NULL;
248 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
249 const char *random_device = NULL;
251 /* ------------------------------------------------------------------
252 * Parse the command line to figure out what we've been asked to do.
256 * If run with no arguments at all, print the usage message and
265 * Parse command line arguments.
282 opt = p++; /* opt will have _one_ leading - */
283 while (*p && *p != '=')
284 p++; /* find end of option */
291 if (!strcmp(opt, "-help")) {
294 fprintf(stderr, "puttygen: option `-%s'"
295 " expects no argument\n", opt);
300 } else if (!strcmp(opt, "-version")) {
303 fprintf(stderr, "puttygen: option `-%s'"
304 " expects no argument\n", opt);
309 } else if (!strcmp(opt, "-pgpfp")) {
312 fprintf(stderr, "puttygen: option `-%s'"
313 " expects no argument\n", opt);
315 /* support --pgpfp for consistency */
319 } else if (!strcmp(opt, "-old-passphrase")) {
320 if (!val && argc > 1)
321 --argc, val = *++argv;
324 fprintf(stderr, "puttygen: option `-%s'"
325 " expects an argument\n", opt);
327 old_passphrase = readpassphrase(val);
331 } else if (!strcmp(opt, "-new-passphrase")) {
332 if (!val && argc > 1)
333 --argc, val = *++argv;
336 fprintf(stderr, "puttygen: option `-%s'"
337 " expects an argument\n", opt);
339 new_passphrase = readpassphrase(val);
343 } else if (!strcmp(opt, "-random-device")) {
344 if (!val && argc > 1)
345 --argc, val = *++argv;
348 fprintf(stderr, "puttygen: option `-%s'"
349 " expects an argument\n", opt);
356 "puttygen: no such option `-%s'\n", opt);
369 * Option requiring no parameter.
381 change_passphrase = TRUE;
393 progressfn = no_progress;
403 * Option requiring parameter.
409 fprintf(stderr, "puttygen: option `-%c' expects a"
414 * Now c is the option and p is the parameter.
418 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
419 keytype = RSA2, sshver = 2;
420 else if (!strcmp(p, "rsa1"))
421 keytype = RSA1, sshver = 1;
422 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
423 keytype = DSA, sshver = 2;
424 else if (!strcmp(p, "ecdsa"))
425 keytype = ECDSA, sshver = 2;
426 else if (!strcmp(p, "ed25519"))
427 keytype = ED25519, sshver = 2;
430 "puttygen: unknown key type `%s'\n", p);
441 if (!strcmp(p, "public"))
443 else if (!strcmp(p, "public-openssh"))
445 else if (!strcmp(p, "private"))
447 else if (!strcmp(p, "fingerprint"))
449 else if (!strcmp(p, "private-openssh"))
450 outtype = OPENSSH_AUTO, sshver = 2;
451 else if (!strcmp(p, "private-openssh-new"))
452 outtype = OPENSSH_NEW, sshver = 2;
453 else if (!strcmp(p, "private-sshcom"))
454 outtype = SSHCOM, sshver = 2;
457 "puttygen: unknown output type `%s'\n", p);
465 p = NULL; /* prevent continued processing */
469 * Unrecognised option.
472 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
478 * A non-option argument.
484 fprintf(stderr, "puttygen: cannot handle more than one"
492 * No explicit key size was specified. Default varies
493 * depending on key type.
508 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
509 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
513 if (keytype == ED25519 && (bits != 256)) {
514 fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
518 if (keytype == RSA2 || keytype == RSA1 || keytype == DSA) {
520 fprintf(stderr, "puttygen: cannot generate %s keys shorter than"
521 " 256 bits\n", (keytype == DSA ? "DSA" : "RSA"));
533 * If run with at least one argument _but_ not the required
534 * ones, print the usage message and return failure.
536 if (!infile && keytype == NOKEYGEN) {
541 /* ------------------------------------------------------------------
542 * Figure out further details of exactly what we're going to do.
546 * Bomb out if we've been asked to both load and generate a
549 if (keytype != NOKEYGEN && infile) {
550 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
555 * We must save the private part when generating a new key.
557 if (keytype != NOKEYGEN &&
558 (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
559 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
560 fprintf(stderr, "puttygen: this would generate a new key but "
561 "discard the private part\n");
566 * Analyse the type of the input file, in case this affects our
570 infilename = filename_from_str(infile);
572 intype = key_type(infilename);
575 case SSH_KEYTYPE_UNOPENABLE:
576 case SSH_KEYTYPE_UNKNOWN:
577 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
578 infile, key_type_to_str(intype));
581 case SSH_KEYTYPE_SSH1:
582 case SSH_KEYTYPE_SSH1_PUBLIC:
584 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
591 case SSH_KEYTYPE_SSH2:
592 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
593 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
594 case SSH_KEYTYPE_OPENSSH_PEM:
595 case SSH_KEYTYPE_OPENSSH_NEW:
596 case SSH_KEYTYPE_SSHCOM:
598 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
605 case SSH_KEYTYPE_OPENSSH_AUTO:
607 assert(0 && "Should never see these types on an input file");
612 * Determine the default output file, if none is provided.
614 * This will usually be equal to stdout, except that if the
615 * input and output file formats are the same then the default
616 * output is to overwrite the input.
618 * Also in this code, we bomb out if the input and output file
619 * formats are the same and no other action is performed.
621 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
622 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
623 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
624 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
625 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
628 outfiletmp = dupcat(outfile, ".tmp", NULL);
631 if (!change_passphrase && !comment) {
632 fprintf(stderr, "puttygen: this command would perform no useful"
639 * Bomb out rather than automatically choosing to write
640 * a private key file to stdout.
642 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
643 outtype == OPENSSH_NEW || outtype == SSHCOM) {
644 fprintf(stderr, "puttygen: need to specify an output file\n");
651 * Figure out whether we need to load the encrypted part of the
652 * key. This will be the case if either (a) we need to write
653 * out a private key format, or (b) the entire input key file
656 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
657 outtype == OPENSSH_NEW || outtype == SSHCOM ||
658 intype == SSH_KEYTYPE_OPENSSH_PEM ||
659 intype == SSH_KEYTYPE_OPENSSH_NEW ||
660 intype == SSH_KEYTYPE_SSHCOM)
661 load_encrypted = TRUE;
663 load_encrypted = FALSE;
665 if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
666 intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
667 intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
668 fprintf(stderr, "puttygen: cannot perform this action on a "
669 "public-key-only input file\n");
673 /* ------------------------------------------------------------------
674 * Now we're ready to actually do some stuff.
678 * Either load or generate a key.
680 if (keytype != NOKEYGEN) {
682 char default_comment[80];
684 struct progress prog;
691 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
692 else if (keytype == ECDSA)
693 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
694 else if (keytype == ED25519)
695 strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
697 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
700 entropy = get_random_data(bits / 8, random_device);
702 fprintf(stderr, "puttygen: failed to collect entropy, "
703 "could not generate key\n");
706 random_add_heavynoise(entropy, bits / 8);
707 smemclr(entropy, bits/8);
710 if (keytype == DSA) {
711 struct dss_key *dsskey = snew(struct dss_key);
712 dsa_generate(dsskey, bits, progressfn, &prog);
713 ssh2key = snew(struct ssh2_userkey);
714 ssh2key->data = dsskey;
715 ssh2key->alg = &ssh_dss;
717 } else if (keytype == ECDSA) {
718 struct ec_key *ec = snew(struct ec_key);
719 ec_generate(ec, bits, progressfn, &prog);
720 ssh2key = snew(struct ssh2_userkey);
722 ssh2key->alg = ec->signalg;
724 } else if (keytype == ED25519) {
725 struct ec_key *ec = snew(struct ec_key);
726 ec_edgenerate(ec, bits, progressfn, &prog);
727 ssh2key = snew(struct ssh2_userkey);
729 ssh2key->alg = &ssh_ecdsa_ed25519;
732 struct RSAKey *rsakey = snew(struct RSAKey);
733 rsa_generate(rsakey, bits, progressfn, &prog);
734 rsakey->comment = NULL;
735 if (keytype == RSA1) {
738 ssh2key = snew(struct ssh2_userkey);
739 ssh2key->data = rsakey;
740 ssh2key->alg = &ssh_rsa;
743 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
746 ssh2key->comment = dupstr(default_comment);
748 ssh1key->comment = dupstr(default_comment);
751 const char *error = NULL;
754 assert(infile != NULL);
757 * Find out whether the input key is encrypted.
759 if (intype == SSH_KEYTYPE_SSH1)
760 encrypted = rsakey_encrypted(infilename, &origcomment);
761 else if (intype == SSH_KEYTYPE_SSH2)
762 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
764 encrypted = import_encrypted(infilename, intype, &origcomment);
767 * If so, ask for a passphrase.
769 if (encrypted && load_encrypted) {
770 if (!old_passphrase) {
771 prompts_t *p = new_prompts(NULL);
773 p->to_server = FALSE;
774 p->name = dupstr("SSH key passphrase");
775 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
776 ret = console_get_userpass_input(p, NULL, 0);
780 perror("puttygen: unable to read passphrase");
783 old_passphrase = dupstr(p->prompts[0]->result);
788 old_passphrase = NULL;
794 case SSH_KEYTYPE_SSH1:
795 case SSH_KEYTYPE_SSH1_PUBLIC:
796 ssh1key = snew(struct RSAKey);
797 if (!load_encrypted) {
802 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
803 &origcomment, &error);
804 blob = (unsigned char *)vblob;
806 n = 4; /* skip modulus bits */
808 l = ssh1_read_bignum(blob + n, bloblen - n,
811 error = "SSH-1 public key blob was too short";
814 l = ssh1_read_bignum(blob + n, bloblen - n,
817 error = "SSH-1 public key blob was too short";
821 ssh1key->comment = dupstr(origcomment);
822 ssh1key->private_exponent = NULL;
825 ssh1key->iqmp = NULL;
827 ret = loadrsakey(infilename, ssh1key, old_passphrase, &error);
832 error = "unknown error";
835 case SSH_KEYTYPE_SSH2:
836 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
837 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
838 if (!load_encrypted) {
839 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
840 &ssh2bloblen, &origcomment,
843 ssh2algf = find_pubkey_alg(ssh2alg);
845 bits = ssh2algf->pubkey_bits(ssh2algf,
846 ssh2blob, ssh2bloblen);
852 ssh2key = ssh2_load_userkey(infilename, old_passphrase,
855 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
858 if (ssh2key == SSH2_WRONG_PASSPHRASE)
859 error = "wrong passphrase";
861 error = "unknown error";
865 case SSH_KEYTYPE_OPENSSH_PEM:
866 case SSH_KEYTYPE_OPENSSH_NEW:
867 case SSH_KEYTYPE_SSHCOM:
868 ssh2key = import_ssh2(infilename, intype, old_passphrase, &error);
870 if (ssh2key != SSH2_WRONG_PASSPHRASE)
873 error = "wrong passphrase";
875 error = "unknown error";
883 fprintf(stderr, "puttygen: error loading `%s': %s\n",
890 * Change the comment if asked to.
895 sfree(ssh1key->comment);
896 ssh1key->comment = dupstr(comment);
899 sfree(ssh2key->comment);
900 ssh2key->comment = dupstr(comment);
905 * Unless we're changing the passphrase, the old one (if any) is a
906 * reasonable default.
908 if (!change_passphrase && old_passphrase && !new_passphrase)
909 new_passphrase = dupstr(old_passphrase);
912 * Prompt for a new passphrase if we have been asked to, or if
913 * we have just generated a key.
915 if (!new_passphrase && (change_passphrase || keytype != NOKEYGEN)) {
916 prompts_t *p = new_prompts(NULL);
919 p->to_server = FALSE;
920 p->name = dupstr("New SSH key passphrase");
921 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
922 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
923 ret = console_get_userpass_input(p, NULL, 0);
927 perror("puttygen: unable to read new passphrase");
930 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
932 fprintf(stderr, "puttygen: passphrases do not match\n");
935 new_passphrase = dupstr(p->prompts[0]->result);
939 if (new_passphrase && !*new_passphrase) {
940 sfree(new_passphrase);
941 new_passphrase = NULL;
947 * (In the case where outfile and outfiletmp are both NULL,
948 * there is no semantic reason to initialise outfilename at
949 * all; but we have to write _something_ to it or some compiler
950 * will probably complain that it might be used uninitialised.)
953 outfilename = filename_from_str(outfiletmp);
955 outfilename = filename_from_str(outfile ? outfile : "");
958 int ret, real_outtype;
963 ret = saversakey(outfilename, ssh1key, new_passphrase);
965 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
970 ret = ssh2_save_userkey(outfilename, ssh2key, new_passphrase);
972 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
977 if (!move(outfiletmp, outfile))
978 return 1; /* rename failed */
988 fp = f_open(outfilename, "w", FALSE);
993 ssh1_write_pubkey(fp, ssh1key);
997 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
1001 ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
1002 ssh2blob, ssh2bloblen,
1003 (outtype == PUBLIC ?
1004 SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
1005 SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
1020 fingerprint = snewn(128, char);
1021 rsa_fingerprint(fingerprint, 128, ssh1key);
1024 fingerprint = ssh2_fingerprint(ssh2key->alg,
1028 fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen);
1033 fp = f_open(outfilename, "w", FALSE);
1036 fprintf(fp, "%s\n", fingerprint);
1047 assert(sshver == 2);
1049 random_ref(); /* both foreign key types require randomness,
1050 * for IV or padding */
1053 real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
1056 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1059 real_outtype = SSH_KEYTYPE_SSHCOM;
1062 assert(0 && "control flow goof");
1064 ret = export_ssh2(outfilename, real_outtype, ssh2key, new_passphrase);
1066 fprintf(stderr, "puttygen: unable to export key\n");
1070 if (!move(outfiletmp, outfile))
1071 return 1; /* rename failed */
1076 if (old_passphrase) {
1077 smemclr(old_passphrase, strlen(old_passphrase));
1078 sfree(old_passphrase);
1080 if (new_passphrase) {
1081 smemclr(new_passphrase, strlen(new_passphrase));
1082 sfree(new_passphrase);
1086 freersakey(ssh1key);
1088 ssh2key->alg->freekey(ssh2key->data);
1103 void setup_passphrases(char *first, ...)
1110 prompts[nprompts++] = first;
1111 va_start(ap, first);
1112 while ((next = va_arg(ap, char *)) != NULL) {
1113 assert(nprompts < lenof(prompts));
1114 prompts[nprompts++] = next;
1120 void test(int retval, ...)
1127 va_start(ap, retval);
1128 while (va_arg(ap, char *) != NULL)
1132 argv = snewn(argc+1, char *);
1133 va_start(ap, retval);
1134 for (i = 0; i <= argc; i++)
1135 argv[i] = va_arg(ap, char *);
1139 ret = cmdgen_main(argc, argv);
1141 if (ret != retval) {
1142 printf("FAILED retval (exp %d got %d):", retval, ret);
1143 for (i = 0; i < argc; i++)
1144 printf(" %s", argv[i]);
1147 } else if (promptsgot != nprompts) {
1148 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1149 for (i = 0; i < argc; i++)
1150 printf(" %s", argv[i]);
1158 void filecmp(char *file1, char *file2, char *fmt, ...)
1161 * Ideally I should do file comparison myself, to maximise the
1162 * portability of this test suite once this application begins
1163 * running on non-Unix platforms. For the moment, though,
1164 * calling Unix diff is perfectly adequate.
1169 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1176 printf("FAILED diff (ret=%d): ", ret);
1189 char *cleanup_fp(char *s)
1193 if (!strncmp(s, "ssh-", 4)) {
1194 s += strcspn(s, " \n\t");
1195 s += strspn(s, " \n\t");
1199 s += strcspn(s, " \n\t");
1200 s += strspn(s, " \n\t");
1201 s += strcspn(s, " \n\t");
1203 return dupprintf("%.*s", (int)(s - p), p);
1206 char *get_fp(char *filename)
1209 char buf[256], *ret;
1211 fp = fopen(filename, "r");
1214 ret = fgets(buf, sizeof(buf), fp);
1218 return cleanup_fp(buf);
1221 void check_fp(char *filename, char *fp, char *fmt, ...)
1228 newfp = get_fp(filename);
1230 if (!strcmp(fp, newfp)) {
1235 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1249 int main(int argc, char **argv)
1252 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1255 * Even when this thing is compiled for automatic test mode,
1256 * it's helpful to be able to invoke it with command-line
1257 * options for _manual_ tests.
1260 return cmdgen_main(argc, argv);
1264 for (i = 0; i < lenof(keytypes); i++) {
1265 char filename[128], osfilename[128], scfilename[128];
1266 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1269 sprintf(filename, "test-%s.ppk", keytypes[i]);
1270 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1271 sprintf(osfilename, "test-%s.os", keytypes[i]);
1272 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1273 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1274 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1277 * Create an encrypted key.
1279 setup_passphrases("sponge", "sponge", NULL);
1280 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1283 * List the public key in OpenSSH format.
1285 setup_passphrases(NULL);
1286 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1290 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1291 pubfilename, tmpfilename1);
1292 if (system(cmdbuf) ||
1293 (fp = get_fp(tmpfilename1)) == NULL) {
1294 printf("UNABLE to test fingerprint matching against OpenSSH");
1299 * List the public key in IETF/ssh.com format.
1301 setup_passphrases(NULL);
1302 test(0, "puttygen", "-p", filename, NULL);
1305 * List the fingerprint of the key.
1307 setup_passphrases(NULL);
1308 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1311 * If we can't test fingerprints against OpenSSH, we
1312 * can at the very least test equality of all the
1313 * fingerprints we generate of this key throughout
1316 fp = get_fp(tmpfilename1);
1318 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1322 * Change the comment of the key; this _does_ require a
1323 * passphrase owing to the tamperproofing.
1325 * NOTE: In SSH-1, this only requires a passphrase because
1326 * of inadequacies of the loading and saving mechanisms. In
1327 * _principle_, it should be perfectly possible to modify
1328 * the comment on an SSH-1 key without requiring a
1329 * passphrase; the only reason I can't do it is because my
1330 * loading and saving mechanisms don't include a method of
1331 * loading all the key data without also trying to decrypt
1332 * the private section.
1334 * I don't consider this to be a problem worth solving,
1335 * because (a) to fix it would probably end up bloating
1336 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1337 * it shouldn't be highly significant. If it seriously
1338 * bothers anyone then perhaps I _might_ be persuadable.
1340 setup_passphrases("sponge", NULL);
1341 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1344 * Change the passphrase to nothing.
1346 setup_passphrases("sponge", "", "", NULL);
1347 test(0, "puttygen", "-P", filename, NULL);
1350 * Change the comment of the key again; this time we expect no
1351 * passphrase to be required.
1353 setup_passphrases(NULL);
1354 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1357 * Export the private key into OpenSSH format; no passphrase
1358 * should be required since the key is currently unencrypted.
1359 * For RSA1 keys, this should give an error.
1361 setup_passphrases(NULL);
1362 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1367 * List the fingerprint of the OpenSSH-formatted key.
1369 setup_passphrases(NULL);
1370 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1371 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1374 * List the public half of the OpenSSH-formatted key in
1377 setup_passphrases(NULL);
1378 test(0, "puttygen", "-L", osfilename, NULL);
1381 * List the public half of the OpenSSH-formatted key in
1382 * IETF/ssh.com format.
1384 setup_passphrases(NULL);
1385 test(0, "puttygen", "-p", osfilename, NULL);
1389 * Export the private key into ssh.com format; no passphrase
1390 * should be required since the key is currently unencrypted.
1391 * For RSA1 keys, this should give an error.
1393 setup_passphrases(NULL);
1394 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1399 * List the fingerprint of the ssh.com-formatted key.
1401 setup_passphrases(NULL);
1402 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1403 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1406 * List the public half of the ssh.com-formatted key in
1409 setup_passphrases(NULL);
1410 test(0, "puttygen", "-L", scfilename, NULL);
1413 * List the public half of the ssh.com-formatted key in
1414 * IETF/ssh.com format.
1416 setup_passphrases(NULL);
1417 test(0, "puttygen", "-p", scfilename, NULL);
1422 * Convert from OpenSSH into ssh.com.
1424 setup_passphrases(NULL);
1425 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1426 "-O", "private-sshcom", NULL);
1429 * Convert from ssh.com back into a PuTTY key,
1430 * supplying the same comment as we had before we
1431 * started to ensure the comparison works.
1433 setup_passphrases(NULL);
1434 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1435 "-o", tmpfilename2, NULL);
1438 * See if the PuTTY key thus generated is the same as
1441 filecmp(filename, tmpfilename2,
1442 "p->o->s->p clear %s", keytypes[i]);
1445 * Convert from ssh.com to OpenSSH.
1447 setup_passphrases(NULL);
1448 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1449 "-O", "private-openssh", NULL);
1452 * Convert from OpenSSH back into a PuTTY key,
1453 * supplying the same comment as we had before we
1454 * started to ensure the comparison works.
1456 setup_passphrases(NULL);
1457 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1458 "-o", tmpfilename2, NULL);
1461 * See if the PuTTY key thus generated is the same as
1464 filecmp(filename, tmpfilename2,
1465 "p->s->o->p clear %s", keytypes[i]);
1468 * Finally, do a round-trip conversion between PuTTY
1469 * and ssh.com without involving OpenSSH, to test that
1470 * the key comment is preserved in that case.
1472 setup_passphrases(NULL);
1473 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1475 setup_passphrases(NULL);
1476 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1477 filecmp(filename, tmpfilename2,
1478 "p->s->p clear %s", keytypes[i]);
1482 * Check that mismatched passphrases cause an error.
1484 setup_passphrases("sponge2", "sponge3", NULL);
1485 test(1, "puttygen", "-P", filename, NULL);
1488 * Put a passphrase back on.
1490 setup_passphrases("sponge2", "sponge2", NULL);
1491 test(0, "puttygen", "-P", filename, NULL);
1494 * Export the private key into OpenSSH format, this time
1495 * while encrypted. For RSA1 keys, this should give an
1499 setup_passphrases(NULL); /* error, hence no passphrase read */
1501 setup_passphrases("sponge2", NULL);
1502 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1507 * List the fingerprint of the OpenSSH-formatted key.
1509 setup_passphrases("sponge2", NULL);
1510 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1511 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1514 * List the public half of the OpenSSH-formatted key in
1517 setup_passphrases("sponge2", NULL);
1518 test(0, "puttygen", "-L", osfilename, NULL);
1521 * List the public half of the OpenSSH-formatted key in
1522 * IETF/ssh.com format.
1524 setup_passphrases("sponge2", NULL);
1525 test(0, "puttygen", "-p", osfilename, NULL);
1529 * Export the private key into ssh.com format, this time
1530 * while encrypted. For RSA1 keys, this should give an
1534 setup_passphrases(NULL); /* error, hence no passphrase read */
1536 setup_passphrases("sponge2", NULL);
1537 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1542 * List the fingerprint of the ssh.com-formatted key.
1544 setup_passphrases("sponge2", NULL);
1545 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1546 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1549 * List the public half of the ssh.com-formatted key in
1552 setup_passphrases("sponge2", NULL);
1553 test(0, "puttygen", "-L", scfilename, NULL);
1556 * List the public half of the ssh.com-formatted key in
1557 * IETF/ssh.com format.
1559 setup_passphrases("sponge2", NULL);
1560 test(0, "puttygen", "-p", scfilename, NULL);
1565 * Convert from OpenSSH into ssh.com.
1567 setup_passphrases("sponge2", NULL);
1568 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1569 "-O", "private-sshcom", NULL);
1572 * Convert from ssh.com back into a PuTTY key,
1573 * supplying the same comment as we had before we
1574 * started to ensure the comparison works.
1576 setup_passphrases("sponge2", NULL);
1577 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1578 "-o", tmpfilename2, NULL);
1581 * See if the PuTTY key thus generated is the same as
1584 filecmp(filename, tmpfilename2,
1585 "p->o->s->p encrypted %s", keytypes[i]);
1588 * Convert from ssh.com to OpenSSH.
1590 setup_passphrases("sponge2", NULL);
1591 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1592 "-O", "private-openssh", NULL);
1595 * Convert from OpenSSH back into a PuTTY key,
1596 * supplying the same comment as we had before we
1597 * started to ensure the comparison works.
1599 setup_passphrases("sponge2", NULL);
1600 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1601 "-o", tmpfilename2, NULL);
1604 * See if the PuTTY key thus generated is the same as
1607 filecmp(filename, tmpfilename2,
1608 "p->s->o->p encrypted %s", keytypes[i]);
1611 * Finally, do a round-trip conversion between PuTTY
1612 * and ssh.com without involving OpenSSH, to test that
1613 * the key comment is preserved in that case.
1615 setup_passphrases("sponge2", NULL);
1616 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1618 setup_passphrases("sponge2", NULL);
1619 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1620 filecmp(filename, tmpfilename2,
1621 "p->s->p encrypted %s", keytypes[i]);
1625 * Load with the wrong passphrase.
1627 setup_passphrases("sponge8", NULL);
1628 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1631 * Load a totally bogus file.
1633 setup_passphrases(NULL);
1634 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1636 printf("%d passes, %d fails\n", passes, fails);