2 * cmdgen.c - command-line form of PuTTYgen
5 #define PUTTY_DO_GLOBALS
19 * This section overrides some definitions below for test purposes.
20 * When compiled with -DTEST_CMDGEN:
22 * - Calls to get_random_data() are replaced with the diagnostic
23 * function below (I #define the name so that I can still link
24 * with the original set of modules without symbol clash), in
25 * order to avoid depleting the test system's /dev/random
28 * - Calls to console_get_userpass_input() are replaced with the
29 * diagnostic function below, so that I can run tests in an
30 * automated manner and provide their interactive passphrase
33 * - main() is renamed to cmdgen_main(); at the bottom of the file
34 * I define another main() which calls the former repeatedly to
37 #define get_random_data get_random_data_diagnostic
38 char *get_random_data(int len)
40 char *buf = snewn(len, char);
41 memset(buf, 'x', len);
44 #define console_get_userpass_input console_get_userpass_input_diagnostic
45 int nprompts, promptsgot;
46 const char *prompts[3];
47 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
51 for (i = 0; i < p->n_prompts; i++) {
52 if (promptsgot < nprompts) {
53 assert(strlen(prompts[promptsgot]) < p->prompts[i]->result_len);
54 strcpy(p->prompts[i]->result, prompts[promptsgot++]);
56 promptsgot++; /* track number of requests anyway */
62 #define main cmdgen_main
69 static void progress_update(void *param, int action, int phase, int iprogress)
71 struct progress *p = (struct progress *)param;
72 if (action != PROGFN_PROGRESS)
74 if (phase > p->phase) {
79 p->current = iprogress - 1;
81 p->current = iprogress;
83 while (p->current < iprogress) {
90 static void no_progress(void *param, int action, int phase, int iprogress)
94 void modalfatalbox(char *p, ...)
97 fprintf(stderr, "FATAL ERROR: ");
99 vfprintf(stderr, p, ap);
105 void nonfatal(char *p, ...)
108 fprintf(stderr, "ERROR: ");
110 vfprintf(stderr, p, ap);
116 * Stubs to let everything else link sensibly.
118 void log_eventlog(void *handle, const char *event)
121 char *x_get_default(const char *key)
125 void sk_cleanup(void)
129 void showversion(void)
131 printf("puttygen: %s\n", ver);
134 void usage(int standalone)
137 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
138 " [ -C comment ] [ -P ] [ -q ]\n"
139 " [ -o output-keyfile ] [ -O type | -l | -L"
143 "Use \"puttygen --help\" for more detail.\n");
149 * Help message is an extended version of the usage message. So
150 * start with that, plus a version heading.
155 " -t specify key type when generating (rsa, dsa, rsa1)\n"
156 " -b specify number of bits when generating key\n"
157 " -C change or specify key comment\n"
158 " -P change key passphrase\n"
159 " -q quiet: do not display progress bar\n"
160 " -O specify output type:\n"
161 " private output PuTTY private key format\n"
162 " private-openssh export OpenSSH private key\n"
163 " private-openssh-new export OpenSSH private key "
164 "(force new file format)\n"
165 " private-sshcom export ssh.com private key\n"
166 " public RFC 4716 / ssh.com public key\n"
167 " public-openssh OpenSSH public key\n"
168 " fingerprint output the key fingerprint\n"
169 " -o specify output file\n"
170 " -l equivalent to `-O fingerprint'\n"
171 " -L equivalent to `-O public-openssh'\n"
172 " -p equivalent to `-O public'\n"
176 static int move(char *from, char *to)
180 ret = rename(from, to);
183 * This OS may require us to remove the original file first.
186 ret = rename(from, to);
189 perror("puttygen: cannot move new file on to old one");
195 static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
198 unsigned char digest[16];
199 struct MD5Context md5c;
203 MD5Update(&md5c, blob, bloblen);
204 MD5Final(digest, &md5c);
206 sprintf(buffer, "%s ", alg);
208 sprintf(buffer + strlen(buffer), "%d ", bits);
209 for (i = 0; i < 16; i++)
210 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
213 return dupstr(buffer);
216 int main(int argc, char **argv)
219 Filename *infilename = NULL, *outfilename = NULL;
220 enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA, ED25519 } keytype = NOKEYGEN;
221 char *outfile = NULL, *outfiletmp = NULL;
222 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_AUTO,
223 OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
225 char *comment = NULL, *origcomment = NULL;
226 int change_passphrase = FALSE;
227 int errs = FALSE, nogo = FALSE;
228 int intype = SSH_KEYTYPE_UNOPENABLE;
230 struct ssh2_userkey *ssh2key = NULL;
231 struct RSAKey *ssh1key = NULL;
232 unsigned char *ssh2blob = NULL;
233 char *ssh2alg = NULL;
234 const struct ssh_signkey *ssh2algf = NULL;
236 char *passphrase = NULL;
238 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
240 /* ------------------------------------------------------------------
241 * Parse the command line to figure out what we've been asked to do.
245 * If run with no arguments at all, print the usage message and
254 * Parse command line arguments.
271 opt = p++; /* opt will have _one_ leading - */
272 while (*p && *p != '=')
273 p++; /* find end of option */
280 if (!strcmp(opt, "-help")) {
283 fprintf(stderr, "puttygen: option `-%s'"
284 " expects no argument\n", opt);
289 } else if (!strcmp(opt, "-version")) {
292 fprintf(stderr, "puttygen: option `-%s'"
293 " expects no argument\n", opt);
298 } else if (!strcmp(opt, "-pgpfp")) {
301 fprintf(stderr, "puttygen: option `-%s'"
302 " expects no argument\n", opt);
304 /* support --pgpfp for consistency */
310 * For long options requiring an argument, add
311 * code along the lines of
313 * else if (!strcmp(opt, "-output")) {
316 * fprintf(stderr, "puttygen: option `-%s'"
317 * " expects an argument\n", opt);
325 "puttygen: no such option `-%s'\n", opt);
338 * Option requiring no parameter.
350 change_passphrase = TRUE;
362 progressfn = no_progress;
372 * Option requiring parameter.
378 fprintf(stderr, "puttygen: option `-%c' expects a"
383 * Now c is the option and p is the parameter.
387 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
388 keytype = RSA2, sshver = 2;
389 else if (!strcmp(p, "rsa1"))
390 keytype = RSA1, sshver = 1;
391 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
392 keytype = DSA, sshver = 2;
393 else if (!strcmp(p, "ecdsa"))
394 keytype = ECDSA, sshver = 2;
395 else if (!strcmp(p, "ed25519"))
396 keytype = ED25519, sshver = 2;
399 "puttygen: unknown key type `%s'\n", p);
410 if (!strcmp(p, "public"))
412 else if (!strcmp(p, "public-openssh"))
414 else if (!strcmp(p, "private"))
416 else if (!strcmp(p, "fingerprint"))
418 else if (!strcmp(p, "private-openssh"))
419 outtype = OPENSSH_AUTO, sshver = 2;
420 else if (!strcmp(p, "private-openssh-new"))
421 outtype = OPENSSH_NEW, sshver = 2;
422 else if (!strcmp(p, "private-sshcom"))
423 outtype = SSHCOM, sshver = 2;
426 "puttygen: unknown output type `%s'\n", p);
434 p = NULL; /* prevent continued processing */
438 * Unrecognised option.
441 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
447 * A non-option argument.
453 fprintf(stderr, "puttygen: cannot handle more than one"
461 * No explicit key size was specified. Default varies
462 * depending on key type.
477 if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
478 fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
482 if (keytype == ED25519 && (bits != 256)) {
483 fprintf(stderr, "puttygen: invalid bits for ED25519, choose 256\n");
494 * If run with at least one argument _but_ not the required
495 * ones, print the usage message and return failure.
497 if (!infile && keytype == NOKEYGEN) {
502 /* ------------------------------------------------------------------
503 * Figure out further details of exactly what we're going to do.
507 * Bomb out if we've been asked to both load and generate a
510 if (keytype != NOKEYGEN && infile) {
511 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
516 * We must save the private part when generating a new key.
518 if (keytype != NOKEYGEN &&
519 (outtype != PRIVATE && outtype != OPENSSH_AUTO &&
520 outtype != OPENSSH_NEW && outtype != SSHCOM)) {
521 fprintf(stderr, "puttygen: this would generate a new key but "
522 "discard the private part\n");
527 * Analyse the type of the input file, in case this affects our
531 infilename = filename_from_str(infile);
533 intype = key_type(infilename);
536 case SSH_KEYTYPE_UNOPENABLE:
537 case SSH_KEYTYPE_UNKNOWN:
538 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
539 infile, key_type_to_str(intype));
542 case SSH_KEYTYPE_SSH1:
543 case SSH_KEYTYPE_SSH1_PUBLIC:
545 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
552 case SSH_KEYTYPE_SSH2:
553 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
554 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
555 case SSH_KEYTYPE_OPENSSH_PEM:
556 case SSH_KEYTYPE_OPENSSH_NEW:
557 case SSH_KEYTYPE_SSHCOM:
559 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
566 case SSH_KEYTYPE_OPENSSH_AUTO:
568 assert(0 && "Should never see these types on an input file");
573 * Determine the default output file, if none is provided.
575 * This will usually be equal to stdout, except that if the
576 * input and output file formats are the same then the default
577 * output is to overwrite the input.
579 * Also in this code, we bomb out if the input and output file
580 * formats are the same and no other action is performed.
582 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
583 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
584 (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_AUTO) ||
585 (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
586 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
589 outfiletmp = dupcat(outfile, ".tmp", NULL);
592 if (!change_passphrase && !comment) {
593 fprintf(stderr, "puttygen: this command would perform no useful"
600 * Bomb out rather than automatically choosing to write
601 * a private key file to stdout.
603 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
604 outtype == OPENSSH_NEW || outtype == SSHCOM) {
605 fprintf(stderr, "puttygen: need to specify an output file\n");
612 * Figure out whether we need to load the encrypted part of the
613 * key. This will be the case if either (a) we need to write
614 * out a private key format, or (b) the entire input key file
617 if (outtype == PRIVATE || outtype == OPENSSH_AUTO ||
618 outtype == OPENSSH_NEW || outtype == SSHCOM ||
619 intype == SSH_KEYTYPE_OPENSSH_PEM ||
620 intype == SSH_KEYTYPE_OPENSSH_NEW ||
621 intype == SSH_KEYTYPE_SSHCOM)
622 load_encrypted = TRUE;
624 load_encrypted = FALSE;
626 if (load_encrypted && (intype == SSH_KEYTYPE_SSH1_PUBLIC ||
627 intype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
628 intype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH)) {
629 fprintf(stderr, "puttygen: cannot perform this action on a "
630 "public-key-only input file\n");
634 /* ------------------------------------------------------------------
635 * Now we're ready to actually do some stuff.
639 * Either load or generate a key.
641 if (keytype != NOKEYGEN) {
643 char default_comment[80];
645 struct progress prog;
652 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
653 else if (keytype == ECDSA)
654 strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
655 else if (keytype == ED25519)
656 strftime(default_comment, 30, "ed25519-key-%Y%m%d", &tm);
658 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
661 entropy = get_random_data(bits / 8);
663 fprintf(stderr, "puttygen: failed to collect entropy, "
664 "could not generate key\n");
667 random_add_heavynoise(entropy, bits / 8);
668 smemclr(entropy, bits/8);
671 if (keytype == DSA) {
672 struct dss_key *dsskey = snew(struct dss_key);
673 dsa_generate(dsskey, bits, progressfn, &prog);
674 ssh2key = snew(struct ssh2_userkey);
675 ssh2key->data = dsskey;
676 ssh2key->alg = &ssh_dss;
678 } else if (keytype == ECDSA) {
679 struct ec_key *ec = snew(struct ec_key);
680 ec_generate(ec, bits, progressfn, &prog);
681 ssh2key = snew(struct ssh2_userkey);
684 ssh2key->alg = &ssh_ecdsa_nistp256;
685 } else if (bits == 384) {
686 ssh2key->alg = &ssh_ecdsa_nistp384;
688 ssh2key->alg = &ssh_ecdsa_nistp521;
691 } else if (keytype == ED25519) {
692 struct ec_key *ec = snew(struct ec_key);
693 ec_edgenerate(ec, bits, progressfn, &prog);
694 ssh2key = snew(struct ssh2_userkey);
696 ssh2key->alg = &ssh_ecdsa_ed25519;
699 struct RSAKey *rsakey = snew(struct RSAKey);
700 rsa_generate(rsakey, bits, progressfn, &prog);
701 rsakey->comment = NULL;
702 if (keytype == RSA1) {
705 ssh2key = snew(struct ssh2_userkey);
706 ssh2key->data = rsakey;
707 ssh2key->alg = &ssh_rsa;
710 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
713 ssh2key->comment = dupstr(default_comment);
715 ssh1key->comment = dupstr(default_comment);
718 const char *error = NULL;
721 assert(infile != NULL);
724 * Find out whether the input key is encrypted.
726 if (intype == SSH_KEYTYPE_SSH1)
727 encrypted = rsakey_encrypted(infilename, &origcomment);
728 else if (intype == SSH_KEYTYPE_SSH2)
729 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
731 encrypted = import_encrypted(infilename, intype, &origcomment);
734 * If so, ask for a passphrase.
736 if (encrypted && load_encrypted) {
737 prompts_t *p = new_prompts(NULL);
739 p->to_server = FALSE;
740 p->name = dupstr("SSH key passphrase");
741 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
742 ret = console_get_userpass_input(p, NULL, 0);
746 perror("puttygen: unable to read passphrase");
749 passphrase = dupstr(p->prompts[0]->result);
759 case SSH_KEYTYPE_SSH1:
760 case SSH_KEYTYPE_SSH1_PUBLIC:
761 ssh1key = snew(struct RSAKey);
762 if (!load_encrypted) {
767 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
768 &origcomment, &error);
769 blob = (unsigned char *)vblob;
771 n = 4; /* skip modulus bits */
773 l = ssh1_read_bignum(blob + n, bloblen - n,
776 error = "SSH-1 public key blob was too short";
779 l = ssh1_read_bignum(blob + n, bloblen - n,
782 error = "SSH-1 public key blob was too short";
786 ssh1key->comment = dupstr(origcomment);
787 ssh1key->private_exponent = NULL;
790 ssh1key->iqmp = NULL;
792 ret = loadrsakey(infilename, ssh1key, passphrase, &error);
797 error = "unknown error";
800 case SSH_KEYTYPE_SSH2:
801 case SSH_KEYTYPE_SSH2_PUBLIC_RFC4716:
802 case SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH:
803 if (!load_encrypted) {
804 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
805 &ssh2bloblen, &origcomment,
808 ssh2algf = find_pubkey_alg(ssh2alg);
810 bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
815 ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
817 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
820 if (ssh2key == SSH2_WRONG_PASSPHRASE)
821 error = "wrong passphrase";
823 error = "unknown error";
827 case SSH_KEYTYPE_OPENSSH_PEM:
828 case SSH_KEYTYPE_OPENSSH_NEW:
829 case SSH_KEYTYPE_SSHCOM:
830 ssh2key = import_ssh2(infilename, intype, passphrase, &error);
832 if (ssh2key != SSH2_WRONG_PASSPHRASE)
835 error = "wrong passphrase";
837 error = "unknown error";
845 fprintf(stderr, "puttygen: error loading `%s': %s\n",
852 * Change the comment if asked to.
857 sfree(ssh1key->comment);
858 ssh1key->comment = dupstr(comment);
861 sfree(ssh2key->comment);
862 ssh2key->comment = dupstr(comment);
867 * Prompt for a new passphrase if we have been asked to, or if
868 * we have just generated a key.
870 if (change_passphrase || keytype != NOKEYGEN) {
871 prompts_t *p = new_prompts(NULL);
874 p->to_server = FALSE;
875 p->name = dupstr("New SSH key passphrase");
876 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
877 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
878 ret = console_get_userpass_input(p, NULL, 0);
882 perror("puttygen: unable to read new passphrase");
885 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
887 fprintf(stderr, "puttygen: passphrases do not match\n");
891 smemclr(passphrase, strlen(passphrase));
894 passphrase = dupstr(p->prompts[0]->result);
906 * (In the case where outfile and outfiletmp are both NULL,
907 * there is no semantic reason to initialise outfilename at
908 * all; but we have to write _something_ to it or some compiler
909 * will probably complain that it might be used uninitialised.)
912 outfilename = filename_from_str(outfiletmp);
914 outfilename = filename_from_str(outfile ? outfile : "");
917 int ret, real_outtype;
922 ret = saversakey(outfilename, ssh1key, passphrase);
924 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
929 ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
931 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
936 if (!move(outfiletmp, outfile))
937 return 1; /* rename failed */
947 fp = f_open(outfilename, "w", FALSE);
952 ssh1_write_pubkey(fp, ssh1key);
956 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
960 ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
961 ssh2blob, ssh2bloblen,
963 SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
964 SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
979 fingerprint = snewn(128, char);
980 rsa_fingerprint(fingerprint, 128, ssh1key);
983 fingerprint = ssh2key->alg->fingerprint(ssh2key->data);
986 fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);
991 fp = f_open(outfilename, "w", FALSE);
994 fprintf(fp, "%s\n", fingerprint);
1005 assert(sshver == 2);
1007 random_ref(); /* both foreign key types require randomness,
1008 * for IV or padding */
1011 real_outtype = SSH_KEYTYPE_OPENSSH_AUTO;
1014 real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
1017 real_outtype = SSH_KEYTYPE_SSHCOM;
1020 assert(0 && "control flow goof");
1022 ret = export_ssh2(outfilename, real_outtype, ssh2key, passphrase);
1024 fprintf(stderr, "puttygen: unable to export key\n");
1028 if (!move(outfiletmp, outfile))
1029 return 1; /* rename failed */
1035 smemclr(passphrase, strlen(passphrase));
1040 freersakey(ssh1key);
1042 ssh2key->alg->freekey(ssh2key->data);
1057 void setup_passphrases(char *first, ...)
1064 prompts[nprompts++] = first;
1065 va_start(ap, first);
1066 while ((next = va_arg(ap, char *)) != NULL) {
1067 assert(nprompts < lenof(prompts));
1068 prompts[nprompts++] = next;
1074 void test(int retval, ...)
1081 va_start(ap, retval);
1082 while (va_arg(ap, char *) != NULL)
1086 argv = snewn(argc+1, char *);
1087 va_start(ap, retval);
1088 for (i = 0; i <= argc; i++)
1089 argv[i] = va_arg(ap, char *);
1093 ret = cmdgen_main(argc, argv);
1095 if (ret != retval) {
1096 printf("FAILED retval (exp %d got %d):", retval, ret);
1097 for (i = 0; i < argc; i++)
1098 printf(" %s", argv[i]);
1101 } else if (promptsgot != nprompts) {
1102 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1103 for (i = 0; i < argc; i++)
1104 printf(" %s", argv[i]);
1112 void filecmp(char *file1, char *file2, char *fmt, ...)
1115 * Ideally I should do file comparison myself, to maximise the
1116 * portability of this test suite once this application begins
1117 * running on non-Unix platforms. For the moment, though,
1118 * calling Unix diff is perfectly adequate.
1123 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1130 printf("FAILED diff (ret=%d): ", ret);
1143 char *cleanup_fp(char *s)
1147 if (!strncmp(s, "ssh-", 4)) {
1148 s += strcspn(s, " \n\t");
1149 s += strspn(s, " \n\t");
1153 s += strcspn(s, " \n\t");
1154 s += strspn(s, " \n\t");
1155 s += strcspn(s, " \n\t");
1157 return dupprintf("%.*s", s - p, p);
1160 char *get_fp(char *filename)
1163 char buf[256], *ret;
1165 fp = fopen(filename, "r");
1168 ret = fgets(buf, sizeof(buf), fp);
1172 return cleanup_fp(buf);
1175 void check_fp(char *filename, char *fp, char *fmt, ...)
1182 newfp = get_fp(filename);
1184 if (!strcmp(fp, newfp)) {
1189 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1203 int main(int argc, char **argv)
1206 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1209 * Even when this thing is compiled for automatic test mode,
1210 * it's helpful to be able to invoke it with command-line
1211 * options for _manual_ tests.
1214 return cmdgen_main(argc, argv);
1218 for (i = 0; i < lenof(keytypes); i++) {
1219 char filename[128], osfilename[128], scfilename[128];
1220 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1223 sprintf(filename, "test-%s.ppk", keytypes[i]);
1224 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1225 sprintf(osfilename, "test-%s.os", keytypes[i]);
1226 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1227 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1228 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1231 * Create an encrypted key.
1233 setup_passphrases("sponge", "sponge", NULL);
1234 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1237 * List the public key in OpenSSH format.
1239 setup_passphrases(NULL);
1240 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1244 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1245 pubfilename, tmpfilename1);
1246 if (system(cmdbuf) ||
1247 (fp = get_fp(tmpfilename1)) == NULL) {
1248 printf("UNABLE to test fingerprint matching against OpenSSH");
1253 * List the public key in IETF/ssh.com format.
1255 setup_passphrases(NULL);
1256 test(0, "puttygen", "-p", filename, NULL);
1259 * List the fingerprint of the key.
1261 setup_passphrases(NULL);
1262 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1265 * If we can't test fingerprints against OpenSSH, we
1266 * can at the very least test equality of all the
1267 * fingerprints we generate of this key throughout
1270 fp = get_fp(tmpfilename1);
1272 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1276 * Change the comment of the key; this _does_ require a
1277 * passphrase owing to the tamperproofing.
1279 * NOTE: In SSH-1, this only requires a passphrase because
1280 * of inadequacies of the loading and saving mechanisms. In
1281 * _principle_, it should be perfectly possible to modify
1282 * the comment on an SSH-1 key without requiring a
1283 * passphrase; the only reason I can't do it is because my
1284 * loading and saving mechanisms don't include a method of
1285 * loading all the key data without also trying to decrypt
1286 * the private section.
1288 * I don't consider this to be a problem worth solving,
1289 * because (a) to fix it would probably end up bloating
1290 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1291 * it shouldn't be highly significant. If it seriously
1292 * bothers anyone then perhaps I _might_ be persuadable.
1294 setup_passphrases("sponge", NULL);
1295 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1298 * Change the passphrase to nothing.
1300 setup_passphrases("sponge", "", "", NULL);
1301 test(0, "puttygen", "-P", filename, NULL);
1304 * Change the comment of the key again; this time we expect no
1305 * passphrase to be required.
1307 setup_passphrases(NULL);
1308 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1311 * Export the private key into OpenSSH format; no passphrase
1312 * should be required since the key is currently unencrypted.
1313 * For RSA1 keys, this should give an error.
1315 setup_passphrases(NULL);
1316 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1321 * List the fingerprint of the OpenSSH-formatted key.
1323 setup_passphrases(NULL);
1324 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1325 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1328 * List the public half of the OpenSSH-formatted key in
1331 setup_passphrases(NULL);
1332 test(0, "puttygen", "-L", osfilename, NULL);
1335 * List the public half of the OpenSSH-formatted key in
1336 * IETF/ssh.com format.
1338 setup_passphrases(NULL);
1339 test(0, "puttygen", "-p", osfilename, NULL);
1343 * Export the private key into ssh.com format; no passphrase
1344 * should be required since the key is currently unencrypted.
1345 * For RSA1 keys, this should give an error.
1347 setup_passphrases(NULL);
1348 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1353 * List the fingerprint of the ssh.com-formatted key.
1355 setup_passphrases(NULL);
1356 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1357 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1360 * List the public half of the ssh.com-formatted key in
1363 setup_passphrases(NULL);
1364 test(0, "puttygen", "-L", scfilename, NULL);
1367 * List the public half of the ssh.com-formatted key in
1368 * IETF/ssh.com format.
1370 setup_passphrases(NULL);
1371 test(0, "puttygen", "-p", scfilename, NULL);
1376 * Convert from OpenSSH into ssh.com.
1378 setup_passphrases(NULL);
1379 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1380 "-O", "private-sshcom", NULL);
1383 * Convert from ssh.com back into a PuTTY key,
1384 * supplying the same comment as we had before we
1385 * started to ensure the comparison works.
1387 setup_passphrases(NULL);
1388 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1389 "-o", tmpfilename2, NULL);
1392 * See if the PuTTY key thus generated is the same as
1395 filecmp(filename, tmpfilename2,
1396 "p->o->s->p clear %s", keytypes[i]);
1399 * Convert from ssh.com to OpenSSH.
1401 setup_passphrases(NULL);
1402 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1403 "-O", "private-openssh", NULL);
1406 * Convert from OpenSSH back into a PuTTY key,
1407 * supplying the same comment as we had before we
1408 * started to ensure the comparison works.
1410 setup_passphrases(NULL);
1411 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1412 "-o", tmpfilename2, NULL);
1415 * See if the PuTTY key thus generated is the same as
1418 filecmp(filename, tmpfilename2,
1419 "p->s->o->p clear %s", keytypes[i]);
1422 * Finally, do a round-trip conversion between PuTTY
1423 * and ssh.com without involving OpenSSH, to test that
1424 * the key comment is preserved in that case.
1426 setup_passphrases(NULL);
1427 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1429 setup_passphrases(NULL);
1430 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1431 filecmp(filename, tmpfilename2,
1432 "p->s->p clear %s", keytypes[i]);
1436 * Check that mismatched passphrases cause an error.
1438 setup_passphrases("sponge2", "sponge3", NULL);
1439 test(1, "puttygen", "-P", filename, NULL);
1442 * Put a passphrase back on.
1444 setup_passphrases("sponge2", "sponge2", NULL);
1445 test(0, "puttygen", "-P", filename, NULL);
1448 * Export the private key into OpenSSH format, this time
1449 * while encrypted. For RSA1 keys, this should give an
1453 setup_passphrases(NULL); /* error, hence no passphrase read */
1455 setup_passphrases("sponge2", NULL);
1456 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1461 * List the fingerprint of the OpenSSH-formatted key.
1463 setup_passphrases("sponge2", NULL);
1464 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1465 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1468 * List the public half of the OpenSSH-formatted key in
1471 setup_passphrases("sponge2", NULL);
1472 test(0, "puttygen", "-L", osfilename, NULL);
1475 * List the public half of the OpenSSH-formatted key in
1476 * IETF/ssh.com format.
1478 setup_passphrases("sponge2", NULL);
1479 test(0, "puttygen", "-p", osfilename, NULL);
1483 * Export the private key into ssh.com format, this time
1484 * while encrypted. For RSA1 keys, this should give an
1488 setup_passphrases(NULL); /* error, hence no passphrase read */
1490 setup_passphrases("sponge2", NULL);
1491 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1496 * List the fingerprint of the ssh.com-formatted key.
1498 setup_passphrases("sponge2", NULL);
1499 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1500 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1503 * List the public half of the ssh.com-formatted key in
1506 setup_passphrases("sponge2", NULL);
1507 test(0, "puttygen", "-L", scfilename, NULL);
1510 * List the public half of the ssh.com-formatted key in
1511 * IETF/ssh.com format.
1513 setup_passphrases("sponge2", NULL);
1514 test(0, "puttygen", "-p", scfilename, NULL);
1519 * Convert from OpenSSH into ssh.com.
1521 setup_passphrases("sponge2", NULL);
1522 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1523 "-O", "private-sshcom", NULL);
1526 * Convert from ssh.com back into a PuTTY key,
1527 * supplying the same comment as we had before we
1528 * started to ensure the comparison works.
1530 setup_passphrases("sponge2", NULL);
1531 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1532 "-o", tmpfilename2, NULL);
1535 * See if the PuTTY key thus generated is the same as
1538 filecmp(filename, tmpfilename2,
1539 "p->o->s->p encrypted %s", keytypes[i]);
1542 * Convert from ssh.com to OpenSSH.
1544 setup_passphrases("sponge2", NULL);
1545 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1546 "-O", "private-openssh", NULL);
1549 * Convert from OpenSSH back into a PuTTY key,
1550 * supplying the same comment as we had before we
1551 * started to ensure the comparison works.
1553 setup_passphrases("sponge2", NULL);
1554 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1555 "-o", tmpfilename2, NULL);
1558 * See if the PuTTY key thus generated is the same as
1561 filecmp(filename, tmpfilename2,
1562 "p->s->o->p encrypted %s", keytypes[i]);
1565 * Finally, do a round-trip conversion between PuTTY
1566 * and ssh.com without involving OpenSSH, to test that
1567 * the key comment is preserved in that case.
1569 setup_passphrases("sponge2", NULL);
1570 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1572 setup_passphrases("sponge2", NULL);
1573 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1574 filecmp(filename, tmpfilename2,
1575 "p->s->p encrypted %s", keytypes[i]);
1579 * Load with the wrong passphrase.
1581 setup_passphrases("sponge8", NULL);
1582 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1585 * Load a totally bogus file.
1587 setup_passphrases(NULL);
1588 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1590 printf("%d passes, %d fails\n", passes, fails);