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-sshcom export ssh.com private key\n"
164 " public standard / ssh.com public key\n"
165 " public-openssh OpenSSH public key\n"
166 " fingerprint output the key fingerprint\n"
167 " -o specify output file\n"
168 " -l equivalent to `-O fingerprint'\n"
169 " -L equivalent to `-O public-openssh'\n"
170 " -p equivalent to `-O public'\n"
174 static int save_ssh2_pubkey(char *filename, char *comment,
175 void *v_pub_blob, int pub_len)
177 unsigned char *pub_blob = (unsigned char *)v_pub_blob;
183 fp = fopen(filename, "wb");
189 fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
192 fprintf(fp, "Comment: \"");
193 for (p = comment; *p; p++) {
194 if (*p == '\\' || *p == '\"')
203 while (i < pub_len) {
205 int n = (pub_len - i < 3 ? pub_len - i : 3);
206 base64_encode_atom(pub_blob + i, n, buf);
210 if (++column >= 16) {
218 fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
224 static int move(char *from, char *to)
228 ret = rename(from, to);
231 * This OS may require us to remove the original file first.
234 ret = rename(from, to);
237 perror("puttygen: cannot move new file on to old one");
243 static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
246 unsigned char digest[16];
247 struct MD5Context md5c;
251 MD5Update(&md5c, blob, bloblen);
252 MD5Final(digest, &md5c);
254 sprintf(buffer, "%s ", alg);
256 sprintf(buffer + strlen(buffer), "%d ", bits);
257 for (i = 0; i < 16; i++)
258 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
261 return dupstr(buffer);
264 int main(int argc, char **argv)
267 Filename *infilename = NULL, *outfilename = NULL;
268 enum { NOKEYGEN, RSA1, RSA2, DSA } keytype = NOKEYGEN;
269 char *outfile = NULL, *outfiletmp = NULL;
270 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH, SSHCOM } outtype = PRIVATE;
272 char *comment = NULL, *origcomment = NULL;
273 int change_passphrase = FALSE;
274 int errs = FALSE, nogo = FALSE;
275 int intype = SSH_KEYTYPE_UNOPENABLE;
277 struct ssh2_userkey *ssh2key = NULL;
278 struct RSAKey *ssh1key = NULL;
279 unsigned char *ssh2blob = NULL;
280 char *ssh2alg = NULL;
281 const struct ssh_signkey *ssh2algf = NULL;
283 char *passphrase = NULL;
285 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
287 /* ------------------------------------------------------------------
288 * Parse the command line to figure out what we've been asked to do.
292 * If run with no arguments at all, print the usage message and
301 * Parse command line arguments.
318 opt = p++; /* opt will have _one_ leading - */
319 while (*p && *p != '=')
320 p++; /* find end of option */
327 if (!strcmp(opt, "-help")) {
330 fprintf(stderr, "puttygen: option `-%s'"
331 " expects no argument\n", opt);
336 } else if (!strcmp(opt, "-version")) {
339 fprintf(stderr, "puttygen: option `-%s'"
340 " expects no argument\n", opt);
345 } else if (!strcmp(opt, "-pgpfp")) {
348 fprintf(stderr, "puttygen: option `-%s'"
349 " expects no argument\n", opt);
351 /* support --pgpfp for consistency */
357 * For long options requiring an argument, add
358 * code along the lines of
360 * else if (!strcmp(opt, "-output")) {
363 * fprintf(stderr, "puttygen: option `-%s'"
364 * " expects an argument\n", opt);
372 "puttygen: no such option `-%s'\n", opt);
385 * Option requiring no parameter.
397 change_passphrase = TRUE;
409 progressfn = no_progress;
419 * Option requiring parameter.
425 fprintf(stderr, "puttygen: option `-%c' expects a"
430 * Now c is the option and p is the parameter.
434 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
435 keytype = RSA2, sshver = 2;
436 else if (!strcmp(p, "rsa1"))
437 keytype = RSA1, sshver = 1;
438 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
439 keytype = DSA, sshver = 2;
442 "puttygen: unknown key type `%s'\n", p);
453 if (!strcmp(p, "public"))
455 else if (!strcmp(p, "public-openssh"))
457 else if (!strcmp(p, "private"))
459 else if (!strcmp(p, "fingerprint"))
461 else if (!strcmp(p, "private-openssh"))
462 outtype = OPENSSH, sshver = 2;
463 else if (!strcmp(p, "private-sshcom"))
464 outtype = SSHCOM, sshver = 2;
467 "puttygen: unknown output type `%s'\n", p);
475 p = NULL; /* prevent continued processing */
479 * Unrecognised option.
482 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
488 * A non-option argument.
494 fprintf(stderr, "puttygen: cannot handle more than one"
507 * If run with at least one argument _but_ not the required
508 * ones, print the usage message and return failure.
510 if (!infile && keytype == NOKEYGEN) {
515 /* ------------------------------------------------------------------
516 * Figure out further details of exactly what we're going to do.
520 * Bomb out if we've been asked to both load and generate a
523 if (keytype != NOKEYGEN && infile) {
524 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
529 * We must save the private part when generating a new key.
531 if (keytype != NOKEYGEN &&
532 (outtype != PRIVATE && outtype != OPENSSH && outtype != SSHCOM)) {
533 fprintf(stderr, "puttygen: this would generate a new key but "
534 "discard the private part\n");
539 * Analyse the type of the input file, in case this affects our
543 infilename = filename_from_str(infile);
545 intype = key_type(infilename);
549 * It would be nice here to be able to load _public_
550 * key files, in any of a number of forms, and (a)
551 * convert them to other public key types, (b) print
552 * out their fingerprints. Or, I suppose, for real
553 * orthogonality, (c) change their comment!
555 * In fact this opens some interesting possibilities.
556 * Suppose ssh2_userkey_loadpub() were able to load
557 * public key files as well as extracting the public
558 * key from private ones. And suppose I did the thing
559 * I've been wanting to do, where specifying a
560 * particular private key file for authentication
561 * causes any _other_ key in the agent to be discarded.
562 * Then, if you had an agent forwarded to the machine
563 * you were running Unix PuTTY or Plink on, and you
564 * needed to specify which of the keys in the agent it
565 * should use, you could do that by supplying a
566 * _public_ key file, thus not needing to trust even
567 * your encrypted private key file to the network. Ooh!
570 case SSH_KEYTYPE_UNOPENABLE:
571 case SSH_KEYTYPE_UNKNOWN:
572 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
573 infile, key_type_to_str(intype));
576 case SSH_KEYTYPE_SSH1:
578 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
585 case SSH_KEYTYPE_SSH2:
586 case SSH_KEYTYPE_OPENSSH:
587 case SSH_KEYTYPE_SSHCOM:
589 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
599 * Determine the default output file, if none is provided.
601 * This will usually be equal to stdout, except that if the
602 * input and output file formats are the same then the default
603 * output is to overwrite the input.
605 * Also in this code, we bomb out if the input and output file
606 * formats are the same and no other action is performed.
608 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
609 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
610 (intype == SSH_KEYTYPE_OPENSSH && outtype == OPENSSH) ||
611 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
614 outfiletmp = dupcat(outfile, ".tmp", NULL);
617 if (!change_passphrase && !comment) {
618 fprintf(stderr, "puttygen: this command would perform no useful"
625 * Bomb out rather than automatically choosing to write
626 * a private key file to stdout.
628 if (outtype==PRIVATE || outtype==OPENSSH || outtype==SSHCOM) {
629 fprintf(stderr, "puttygen: need to specify an output file\n");
636 * Figure out whether we need to load the encrypted part of the
637 * key. This will be the case if either (a) we need to write
638 * out a private key format, or (b) the entire input key file
641 if (outtype == PRIVATE || outtype == OPENSSH || outtype == SSHCOM ||
642 intype == SSH_KEYTYPE_OPENSSH || intype == SSH_KEYTYPE_SSHCOM)
643 load_encrypted = TRUE;
645 load_encrypted = FALSE;
647 /* ------------------------------------------------------------------
648 * Now we're ready to actually do some stuff.
652 * Either load or generate a key.
654 if (keytype != NOKEYGEN) {
656 char default_comment[80];
658 struct progress prog;
665 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
667 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
670 entropy = get_random_data(bits / 8);
672 fprintf(stderr, "puttygen: failed to collect entropy, "
673 "could not generate key\n");
676 random_add_heavynoise(entropy, bits / 8);
677 smemclr(entropy, bits/8);
680 if (keytype == DSA) {
681 struct dss_key *dsskey = snew(struct dss_key);
682 dsa_generate(dsskey, bits, progressfn, &prog);
683 ssh2key = snew(struct ssh2_userkey);
684 ssh2key->data = dsskey;
685 ssh2key->alg = &ssh_dss;
688 struct RSAKey *rsakey = snew(struct RSAKey);
689 rsa_generate(rsakey, bits, progressfn, &prog);
690 rsakey->comment = NULL;
691 if (keytype == RSA1) {
694 ssh2key = snew(struct ssh2_userkey);
695 ssh2key->data = rsakey;
696 ssh2key->alg = &ssh_rsa;
699 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
702 ssh2key->comment = dupstr(default_comment);
704 ssh1key->comment = dupstr(default_comment);
707 const char *error = NULL;
710 assert(infile != NULL);
713 * Find out whether the input key is encrypted.
715 if (intype == SSH_KEYTYPE_SSH1)
716 encrypted = rsakey_encrypted(infilename, &origcomment);
717 else if (intype == SSH_KEYTYPE_SSH2)
718 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
720 encrypted = import_encrypted(infilename, intype, &origcomment);
723 * If so, ask for a passphrase.
725 if (encrypted && load_encrypted) {
726 prompts_t *p = new_prompts(NULL);
728 p->to_server = FALSE;
729 p->name = dupstr("SSH key passphrase");
730 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
731 ret = console_get_userpass_input(p, NULL, 0);
735 perror("puttygen: unable to read passphrase");
738 passphrase = dupstr(p->prompts[0]->result);
748 case SSH_KEYTYPE_SSH1:
749 ssh1key = snew(struct RSAKey);
750 if (!load_encrypted) {
755 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
756 &origcomment, &error);
757 blob = (unsigned char *)vblob;
759 n = 4; /* skip modulus bits */
761 l = ssh1_read_bignum(blob + n, bloblen - n,
764 error = "SSH-1 public key blob was too short";
767 l = ssh1_read_bignum(blob + n, bloblen - n,
770 error = "SSH-1 public key blob was too short";
774 ssh1key->comment = dupstr(origcomment);
775 ssh1key->private_exponent = NULL;
778 ssh1key->iqmp = NULL;
780 ret = loadrsakey(infilename, ssh1key, passphrase, &error);
785 error = "unknown error";
788 case SSH_KEYTYPE_SSH2:
789 if (!load_encrypted) {
790 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
791 &ssh2bloblen, NULL, &error);
793 ssh2algf = find_pubkey_alg(ssh2alg);
795 bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
800 ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
802 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
805 if (ssh2key == SSH2_WRONG_PASSPHRASE)
806 error = "wrong passphrase";
808 error = "unknown error";
812 case SSH_KEYTYPE_OPENSSH:
813 case SSH_KEYTYPE_SSHCOM:
814 ssh2key = import_ssh2(infilename, intype, passphrase, &error);
816 if (ssh2key != SSH2_WRONG_PASSPHRASE)
819 error = "wrong passphrase";
821 error = "unknown error";
829 fprintf(stderr, "puttygen: error loading `%s': %s\n",
836 * Change the comment if asked to.
841 sfree(ssh1key->comment);
842 ssh1key->comment = dupstr(comment);
845 sfree(ssh2key->comment);
846 ssh2key->comment = dupstr(comment);
851 * Prompt for a new passphrase if we have been asked to, or if
852 * we have just generated a key.
854 if (change_passphrase || keytype != NOKEYGEN) {
855 prompts_t *p = new_prompts(NULL);
858 p->to_server = FALSE;
859 p->name = dupstr("New SSH key passphrase");
860 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
861 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
862 ret = console_get_userpass_input(p, NULL, 0);
866 perror("puttygen: unable to read new passphrase");
869 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
871 fprintf(stderr, "puttygen: passphrases do not match\n");
875 smemclr(passphrase, strlen(passphrase));
878 passphrase = dupstr(p->prompts[0]->result);
890 * (In the case where outfile and outfiletmp are both NULL,
891 * there is no semantic reason to initialise outfilename at
892 * all; but we have to write _something_ to it or some compiler
893 * will probably complain that it might be used uninitialised.)
896 outfilename = filename_from_str(outfiletmp);
898 outfilename = filename_from_str(outfile ? outfile : "");
906 ret = saversakey(outfilename, ssh1key, passphrase);
908 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
913 ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
915 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
920 if (!move(outfiletmp, outfile))
921 return 1; /* rename failed */
934 fp = f_open(outfilename, "w", FALSE);
937 dec1 = bignum_decimal(ssh1key->exponent);
938 dec2 = bignum_decimal(ssh1key->modulus);
939 fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus),
940 dec1, dec2, ssh1key->comment);
945 } else if (outtype == PUBLIC) {
948 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
951 save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,
952 ssh2blob, ssh2bloblen);
953 } else if (outtype == PUBLICO) {
960 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
965 ssh2alg = ssh2key->alg->name;
968 comment = ssh2key->comment;
970 comment = origcomment;
972 buffer = snewn(strlen(ssh2alg) +
973 4 * ((ssh2bloblen+2) / 3) +
974 strlen(comment) + 3, char);
975 strcpy(buffer, ssh2alg);
976 p = buffer + strlen(buffer);
979 while (i < ssh2bloblen) {
980 int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);
981 base64_encode_atom(ssh2blob + i, n, p);
992 fp = f_open(outfilename, "w", FALSE);
995 fprintf(fp, "%s\n", buffer);
1010 fingerprint = snewn(128, char);
1011 rsa_fingerprint(fingerprint, 128, ssh1key);
1014 fingerprint = ssh2key->alg->fingerprint(ssh2key->data);
1017 fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);
1022 fp = f_open(outfilename, "w", FALSE);
1025 fprintf(fp, "%s\n", fingerprint);
1035 assert(sshver == 2);
1037 random_ref(); /* both foreign key types require randomness,
1038 * for IV or padding */
1039 ret = export_ssh2(outfilename, outtype, ssh2key, passphrase);
1041 fprintf(stderr, "puttygen: unable to export key\n");
1045 if (!move(outfiletmp, outfile))
1046 return 1; /* rename failed */
1052 smemclr(passphrase, strlen(passphrase));
1057 freersakey(ssh1key);
1059 ssh2key->alg->freekey(ssh2key->data);
1074 void setup_passphrases(char *first, ...)
1081 prompts[nprompts++] = first;
1082 va_start(ap, first);
1083 while ((next = va_arg(ap, char *)) != NULL) {
1084 assert(nprompts < lenof(prompts));
1085 prompts[nprompts++] = next;
1091 void test(int retval, ...)
1098 va_start(ap, retval);
1099 while (va_arg(ap, char *) != NULL)
1103 argv = snewn(argc+1, char *);
1104 va_start(ap, retval);
1105 for (i = 0; i <= argc; i++)
1106 argv[i] = va_arg(ap, char *);
1110 ret = cmdgen_main(argc, argv);
1112 if (ret != retval) {
1113 printf("FAILED retval (exp %d got %d):", retval, ret);
1114 for (i = 0; i < argc; i++)
1115 printf(" %s", argv[i]);
1118 } else if (promptsgot != nprompts) {
1119 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1120 for (i = 0; i < argc; i++)
1121 printf(" %s", argv[i]);
1129 void filecmp(char *file1, char *file2, char *fmt, ...)
1132 * Ideally I should do file comparison myself, to maximise the
1133 * portability of this test suite once this application begins
1134 * running on non-Unix platforms. For the moment, though,
1135 * calling Unix diff is perfectly adequate.
1140 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1147 printf("FAILED diff (ret=%d): ", ret);
1160 char *cleanup_fp(char *s)
1164 if (!strncmp(s, "ssh-", 4)) {
1165 s += strcspn(s, " \n\t");
1166 s += strspn(s, " \n\t");
1170 s += strcspn(s, " \n\t");
1171 s += strspn(s, " \n\t");
1172 s += strcspn(s, " \n\t");
1174 return dupprintf("%.*s", s - p, p);
1177 char *get_fp(char *filename)
1180 char buf[256], *ret;
1182 fp = fopen(filename, "r");
1185 ret = fgets(buf, sizeof(buf), fp);
1189 return cleanup_fp(buf);
1192 void check_fp(char *filename, char *fp, char *fmt, ...)
1199 newfp = get_fp(filename);
1201 if (!strcmp(fp, newfp)) {
1206 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1220 int main(int argc, char **argv)
1223 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1226 * Even when this thing is compiled for automatic test mode,
1227 * it's helpful to be able to invoke it with command-line
1228 * options for _manual_ tests.
1231 return cmdgen_main(argc, argv);
1235 for (i = 0; i < lenof(keytypes); i++) {
1236 char filename[128], osfilename[128], scfilename[128];
1237 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1240 sprintf(filename, "test-%s.ppk", keytypes[i]);
1241 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1242 sprintf(osfilename, "test-%s.os", keytypes[i]);
1243 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1244 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1245 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1248 * Create an encrypted key.
1250 setup_passphrases("sponge", "sponge", NULL);
1251 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1254 * List the public key in OpenSSH format.
1256 setup_passphrases(NULL);
1257 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1261 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1262 pubfilename, tmpfilename1);
1263 if (system(cmdbuf) ||
1264 (fp = get_fp(tmpfilename1)) == NULL) {
1265 printf("UNABLE to test fingerprint matching against OpenSSH");
1270 * List the public key in IETF/ssh.com format.
1272 setup_passphrases(NULL);
1273 test(0, "puttygen", "-p", filename, NULL);
1276 * List the fingerprint of the key.
1278 setup_passphrases(NULL);
1279 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1282 * If we can't test fingerprints against OpenSSH, we
1283 * can at the very least test equality of all the
1284 * fingerprints we generate of this key throughout
1287 fp = get_fp(tmpfilename1);
1289 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1293 * Change the comment of the key; this _does_ require a
1294 * passphrase owing to the tamperproofing.
1296 * NOTE: In SSH-1, this only requires a passphrase because
1297 * of inadequacies of the loading and saving mechanisms. In
1298 * _principle_, it should be perfectly possible to modify
1299 * the comment on an SSH-1 key without requiring a
1300 * passphrase; the only reason I can't do it is because my
1301 * loading and saving mechanisms don't include a method of
1302 * loading all the key data without also trying to decrypt
1303 * the private section.
1305 * I don't consider this to be a problem worth solving,
1306 * because (a) to fix it would probably end up bloating
1307 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1308 * it shouldn't be highly significant. If it seriously
1309 * bothers anyone then perhaps I _might_ be persuadable.
1311 setup_passphrases("sponge", NULL);
1312 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1315 * Change the passphrase to nothing.
1317 setup_passphrases("sponge", "", "", NULL);
1318 test(0, "puttygen", "-P", filename, NULL);
1321 * Change the comment of the key again; this time we expect no
1322 * passphrase to be required.
1324 setup_passphrases(NULL);
1325 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1328 * Export the private key into OpenSSH format; no passphrase
1329 * should be required since the key is currently unencrypted.
1330 * For RSA1 keys, this should give an error.
1332 setup_passphrases(NULL);
1333 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1338 * List the fingerprint of the OpenSSH-formatted key.
1340 setup_passphrases(NULL);
1341 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1342 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1345 * List the public half of the OpenSSH-formatted key in
1348 setup_passphrases(NULL);
1349 test(0, "puttygen", "-L", osfilename, NULL);
1352 * List the public half of the OpenSSH-formatted key in
1353 * IETF/ssh.com format.
1355 setup_passphrases(NULL);
1356 test(0, "puttygen", "-p", osfilename, NULL);
1360 * Export the private key into ssh.com format; no passphrase
1361 * should be required since the key is currently unencrypted.
1362 * For RSA1 keys, this should give an error.
1364 setup_passphrases(NULL);
1365 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1370 * List the fingerprint of the ssh.com-formatted key.
1372 setup_passphrases(NULL);
1373 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1374 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1377 * List the public half of the ssh.com-formatted key in
1380 setup_passphrases(NULL);
1381 test(0, "puttygen", "-L", scfilename, NULL);
1384 * List the public half of the ssh.com-formatted key in
1385 * IETF/ssh.com format.
1387 setup_passphrases(NULL);
1388 test(0, "puttygen", "-p", scfilename, NULL);
1393 * Convert from OpenSSH into ssh.com.
1395 setup_passphrases(NULL);
1396 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1397 "-O", "private-sshcom", NULL);
1400 * Convert from ssh.com back into a PuTTY key,
1401 * supplying the same comment as we had before we
1402 * started to ensure the comparison works.
1404 setup_passphrases(NULL);
1405 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1406 "-o", tmpfilename2, NULL);
1409 * See if the PuTTY key thus generated is the same as
1412 filecmp(filename, tmpfilename2,
1413 "p->o->s->p clear %s", keytypes[i]);
1416 * Convert from ssh.com to OpenSSH.
1418 setup_passphrases(NULL);
1419 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1420 "-O", "private-openssh", NULL);
1423 * Convert from OpenSSH back into a PuTTY key,
1424 * supplying the same comment as we had before we
1425 * started to ensure the comparison works.
1427 setup_passphrases(NULL);
1428 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1429 "-o", tmpfilename2, NULL);
1432 * See if the PuTTY key thus generated is the same as
1435 filecmp(filename, tmpfilename2,
1436 "p->s->o->p clear %s", keytypes[i]);
1439 * Finally, do a round-trip conversion between PuTTY
1440 * and ssh.com without involving OpenSSH, to test that
1441 * the key comment is preserved in that case.
1443 setup_passphrases(NULL);
1444 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1446 setup_passphrases(NULL);
1447 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1448 filecmp(filename, tmpfilename2,
1449 "p->s->p clear %s", keytypes[i]);
1453 * Check that mismatched passphrases cause an error.
1455 setup_passphrases("sponge2", "sponge3", NULL);
1456 test(1, "puttygen", "-P", filename, NULL);
1459 * Put a passphrase back on.
1461 setup_passphrases("sponge2", "sponge2", NULL);
1462 test(0, "puttygen", "-P", filename, NULL);
1465 * Export the private key into OpenSSH format, this time
1466 * while encrypted. For RSA1 keys, this should give an
1470 setup_passphrases(NULL); /* error, hence no passphrase read */
1472 setup_passphrases("sponge2", NULL);
1473 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1478 * List the fingerprint of the OpenSSH-formatted key.
1480 setup_passphrases("sponge2", NULL);
1481 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1482 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1485 * List the public half of the OpenSSH-formatted key in
1488 setup_passphrases("sponge2", NULL);
1489 test(0, "puttygen", "-L", osfilename, NULL);
1492 * List the public half of the OpenSSH-formatted key in
1493 * IETF/ssh.com format.
1495 setup_passphrases("sponge2", NULL);
1496 test(0, "puttygen", "-p", osfilename, NULL);
1500 * Export the private key into ssh.com format, this time
1501 * while encrypted. For RSA1 keys, this should give an
1505 setup_passphrases(NULL); /* error, hence no passphrase read */
1507 setup_passphrases("sponge2", NULL);
1508 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1513 * List the fingerprint of the ssh.com-formatted key.
1515 setup_passphrases("sponge2", NULL);
1516 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1517 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1520 * List the public half of the ssh.com-formatted key in
1523 setup_passphrases("sponge2", NULL);
1524 test(0, "puttygen", "-L", scfilename, NULL);
1527 * List the public half of the ssh.com-formatted key in
1528 * IETF/ssh.com format.
1530 setup_passphrases("sponge2", NULL);
1531 test(0, "puttygen", "-p", scfilename, NULL);
1536 * Convert from OpenSSH into ssh.com.
1538 setup_passphrases("sponge2", NULL);
1539 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1540 "-O", "private-sshcom", NULL);
1543 * Convert from ssh.com back into a PuTTY key,
1544 * supplying the same comment as we had before we
1545 * started to ensure the comparison works.
1547 setup_passphrases("sponge2", NULL);
1548 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1549 "-o", tmpfilename2, NULL);
1552 * See if the PuTTY key thus generated is the same as
1555 filecmp(filename, tmpfilename2,
1556 "p->o->s->p encrypted %s", keytypes[i]);
1559 * Convert from ssh.com to OpenSSH.
1561 setup_passphrases("sponge2", NULL);
1562 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1563 "-O", "private-openssh", NULL);
1566 * Convert from OpenSSH back into a PuTTY key,
1567 * supplying the same comment as we had before we
1568 * started to ensure the comparison works.
1570 setup_passphrases("sponge2", NULL);
1571 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1572 "-o", tmpfilename2, NULL);
1575 * See if the PuTTY key thus generated is the same as
1578 filecmp(filename, tmpfilename2,
1579 "p->s->o->p encrypted %s", keytypes[i]);
1582 * Finally, do a round-trip conversion between PuTTY
1583 * and ssh.com without involving OpenSSH, to test that
1584 * the key comment is preserved in that case.
1586 setup_passphrases("sponge2", NULL);
1587 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1589 setup_passphrases("sponge2", NULL);
1590 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1591 filecmp(filename, tmpfilename2,
1592 "p->s->p encrypted %s", keytypes[i]);
1596 * Load with the wrong passphrase.
1598 setup_passphrases("sponge8", NULL);
1599 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1602 * Load a totally bogus file.
1604 setup_passphrases(NULL);
1605 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1607 printf("%d passes, %d fails\n", passes, fails);