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 ret = export_ssh2(outfilename, outtype, ssh2key, passphrase);
1039 fprintf(stderr, "puttygen: unable to export key\n");
1043 if (!move(outfiletmp, outfile))
1044 return 1; /* rename failed */
1050 smemclr(passphrase, strlen(passphrase));
1055 freersakey(ssh1key);
1057 ssh2key->alg->freekey(ssh2key->data);
1072 void setup_passphrases(char *first, ...)
1079 prompts[nprompts++] = first;
1080 va_start(ap, first);
1081 while ((next = va_arg(ap, char *)) != NULL) {
1082 assert(nprompts < lenof(prompts));
1083 prompts[nprompts++] = next;
1089 void test(int retval, ...)
1096 va_start(ap, retval);
1097 while (va_arg(ap, char *) != NULL)
1101 argv = snewn(argc+1, char *);
1102 va_start(ap, retval);
1103 for (i = 0; i <= argc; i++)
1104 argv[i] = va_arg(ap, char *);
1108 ret = cmdgen_main(argc, argv);
1110 if (ret != retval) {
1111 printf("FAILED retval (exp %d got %d):", retval, ret);
1112 for (i = 0; i < argc; i++)
1113 printf(" %s", argv[i]);
1116 } else if (promptsgot != nprompts) {
1117 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1118 for (i = 0; i < argc; i++)
1119 printf(" %s", argv[i]);
1127 void filecmp(char *file1, char *file2, char *fmt, ...)
1130 * Ideally I should do file comparison myself, to maximise the
1131 * portability of this test suite once this application begins
1132 * running on non-Unix platforms. For the moment, though,
1133 * calling Unix diff is perfectly adequate.
1138 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1145 printf("FAILED diff (ret=%d): ", ret);
1158 char *cleanup_fp(char *s)
1162 if (!strncmp(s, "ssh-", 4)) {
1163 s += strcspn(s, " \n\t");
1164 s += strspn(s, " \n\t");
1168 s += strcspn(s, " \n\t");
1169 s += strspn(s, " \n\t");
1170 s += strcspn(s, " \n\t");
1172 return dupprintf("%.*s", s - p, p);
1175 char *get_fp(char *filename)
1178 char buf[256], *ret;
1180 fp = fopen(filename, "r");
1183 ret = fgets(buf, sizeof(buf), fp);
1187 return cleanup_fp(buf);
1190 void check_fp(char *filename, char *fp, char *fmt, ...)
1197 newfp = get_fp(filename);
1199 if (!strcmp(fp, newfp)) {
1204 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1218 int main(int argc, char **argv)
1221 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1224 * Even when this thing is compiled for automatic test mode,
1225 * it's helpful to be able to invoke it with command-line
1226 * options for _manual_ tests.
1229 return cmdgen_main(argc, argv);
1233 for (i = 0; i < lenof(keytypes); i++) {
1234 char filename[128], osfilename[128], scfilename[128];
1235 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1238 sprintf(filename, "test-%s.ppk", keytypes[i]);
1239 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1240 sprintf(osfilename, "test-%s.os", keytypes[i]);
1241 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1242 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1243 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1246 * Create an encrypted key.
1248 setup_passphrases("sponge", "sponge", NULL);
1249 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1252 * List the public key in OpenSSH format.
1254 setup_passphrases(NULL);
1255 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1259 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1260 pubfilename, tmpfilename1);
1261 if (system(cmdbuf) ||
1262 (fp = get_fp(tmpfilename1)) == NULL) {
1263 printf("UNABLE to test fingerprint matching against OpenSSH");
1268 * List the public key in IETF/ssh.com format.
1270 setup_passphrases(NULL);
1271 test(0, "puttygen", "-p", filename, NULL);
1274 * List the fingerprint of the key.
1276 setup_passphrases(NULL);
1277 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1280 * If we can't test fingerprints against OpenSSH, we
1281 * can at the very least test equality of all the
1282 * fingerprints we generate of this key throughout
1285 fp = get_fp(tmpfilename1);
1287 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1291 * Change the comment of the key; this _does_ require a
1292 * passphrase owing to the tamperproofing.
1294 * NOTE: In SSH-1, this only requires a passphrase because
1295 * of inadequacies of the loading and saving mechanisms. In
1296 * _principle_, it should be perfectly possible to modify
1297 * the comment on an SSH-1 key without requiring a
1298 * passphrase; the only reason I can't do it is because my
1299 * loading and saving mechanisms don't include a method of
1300 * loading all the key data without also trying to decrypt
1301 * the private section.
1303 * I don't consider this to be a problem worth solving,
1304 * because (a) to fix it would probably end up bloating
1305 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1306 * it shouldn't be highly significant. If it seriously
1307 * bothers anyone then perhaps I _might_ be persuadable.
1309 setup_passphrases("sponge", NULL);
1310 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1313 * Change the passphrase to nothing.
1315 setup_passphrases("sponge", "", "", NULL);
1316 test(0, "puttygen", "-P", filename, NULL);
1319 * Change the comment of the key again; this time we expect no
1320 * passphrase to be required.
1322 setup_passphrases(NULL);
1323 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1326 * Export the private key into OpenSSH format; no passphrase
1327 * should be required since the key is currently unencrypted.
1328 * For RSA1 keys, this should give an error.
1330 setup_passphrases(NULL);
1331 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1336 * List the fingerprint of the OpenSSH-formatted key.
1338 setup_passphrases(NULL);
1339 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1340 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1343 * List the public half of the OpenSSH-formatted key in
1346 setup_passphrases(NULL);
1347 test(0, "puttygen", "-L", osfilename, NULL);
1350 * List the public half of the OpenSSH-formatted key in
1351 * IETF/ssh.com format.
1353 setup_passphrases(NULL);
1354 test(0, "puttygen", "-p", osfilename, NULL);
1358 * Export the private key into ssh.com format; no passphrase
1359 * should be required since the key is currently unencrypted.
1360 * For RSA1 keys, this should give an error.
1362 setup_passphrases(NULL);
1363 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1368 * List the fingerprint of the ssh.com-formatted key.
1370 setup_passphrases(NULL);
1371 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1372 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1375 * List the public half of the ssh.com-formatted key in
1378 setup_passphrases(NULL);
1379 test(0, "puttygen", "-L", scfilename, NULL);
1382 * List the public half of the ssh.com-formatted key in
1383 * IETF/ssh.com format.
1385 setup_passphrases(NULL);
1386 test(0, "puttygen", "-p", scfilename, NULL);
1391 * Convert from OpenSSH into ssh.com.
1393 setup_passphrases(NULL);
1394 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1395 "-O", "private-sshcom", NULL);
1398 * Convert from ssh.com back into a PuTTY key,
1399 * supplying the same comment as we had before we
1400 * started to ensure the comparison works.
1402 setup_passphrases(NULL);
1403 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1404 "-o", tmpfilename2, NULL);
1407 * See if the PuTTY key thus generated is the same as
1410 filecmp(filename, tmpfilename2,
1411 "p->o->s->p clear %s", keytypes[i]);
1414 * Convert from ssh.com to OpenSSH.
1416 setup_passphrases(NULL);
1417 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1418 "-O", "private-openssh", NULL);
1421 * Convert from OpenSSH back into a PuTTY key,
1422 * supplying the same comment as we had before we
1423 * started to ensure the comparison works.
1425 setup_passphrases(NULL);
1426 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1427 "-o", tmpfilename2, NULL);
1430 * See if the PuTTY key thus generated is the same as
1433 filecmp(filename, tmpfilename2,
1434 "p->s->o->p clear %s", keytypes[i]);
1437 * Finally, do a round-trip conversion between PuTTY
1438 * and ssh.com without involving OpenSSH, to test that
1439 * the key comment is preserved in that case.
1441 setup_passphrases(NULL);
1442 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1444 setup_passphrases(NULL);
1445 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1446 filecmp(filename, tmpfilename2,
1447 "p->s->p clear %s", keytypes[i]);
1451 * Check that mismatched passphrases cause an error.
1453 setup_passphrases("sponge2", "sponge3", NULL);
1454 test(1, "puttygen", "-P", filename, NULL);
1457 * Put a passphrase back on.
1459 setup_passphrases("sponge2", "sponge2", NULL);
1460 test(0, "puttygen", "-P", filename, NULL);
1463 * Export the private key into OpenSSH format, this time
1464 * while encrypted. For RSA1 keys, this should give an
1468 setup_passphrases(NULL); /* error, hence no passphrase read */
1470 setup_passphrases("sponge2", NULL);
1471 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1476 * List the fingerprint of the OpenSSH-formatted key.
1478 setup_passphrases("sponge2", NULL);
1479 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1480 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1483 * List the public half of the OpenSSH-formatted key in
1486 setup_passphrases("sponge2", NULL);
1487 test(0, "puttygen", "-L", osfilename, NULL);
1490 * List the public half of the OpenSSH-formatted key in
1491 * IETF/ssh.com format.
1493 setup_passphrases("sponge2", NULL);
1494 test(0, "puttygen", "-p", osfilename, NULL);
1498 * Export the private key into ssh.com format, this time
1499 * while encrypted. For RSA1 keys, this should give an
1503 setup_passphrases(NULL); /* error, hence no passphrase read */
1505 setup_passphrases("sponge2", NULL);
1506 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1511 * List the fingerprint of the ssh.com-formatted key.
1513 setup_passphrases("sponge2", NULL);
1514 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1515 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1518 * List the public half of the ssh.com-formatted key in
1521 setup_passphrases("sponge2", NULL);
1522 test(0, "puttygen", "-L", scfilename, NULL);
1525 * List the public half of the ssh.com-formatted key in
1526 * IETF/ssh.com format.
1528 setup_passphrases("sponge2", NULL);
1529 test(0, "puttygen", "-p", scfilename, NULL);
1534 * Convert from OpenSSH into ssh.com.
1536 setup_passphrases("sponge2", NULL);
1537 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1538 "-O", "private-sshcom", NULL);
1541 * Convert from ssh.com back into a PuTTY key,
1542 * supplying the same comment as we had before we
1543 * started to ensure the comparison works.
1545 setup_passphrases("sponge2", NULL);
1546 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1547 "-o", tmpfilename2, NULL);
1550 * See if the PuTTY key thus generated is the same as
1553 filecmp(filename, tmpfilename2,
1554 "p->o->s->p encrypted %s", keytypes[i]);
1557 * Convert from ssh.com to OpenSSH.
1559 setup_passphrases("sponge2", NULL);
1560 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1561 "-O", "private-openssh", NULL);
1564 * Convert from OpenSSH back into a PuTTY key,
1565 * supplying the same comment as we had before we
1566 * started to ensure the comparison works.
1568 setup_passphrases("sponge2", NULL);
1569 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1570 "-o", tmpfilename2, NULL);
1573 * See if the PuTTY key thus generated is the same as
1576 filecmp(filename, tmpfilename2,
1577 "p->s->o->p encrypted %s", keytypes[i]);
1580 * Finally, do a round-trip conversion between PuTTY
1581 * and ssh.com without involving OpenSSH, to test that
1582 * the key comment is preserved in that case.
1584 setup_passphrases("sponge2", NULL);
1585 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1587 setup_passphrases("sponge2", NULL);
1588 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1589 filecmp(filename, tmpfilename2,
1590 "p->s->p encrypted %s", keytypes[i]);
1594 * Load with the wrong passphrase.
1596 setup_passphrases("sponge8", NULL);
1597 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1600 * Load a totally bogus file.
1602 setup_passphrases(NULL);
1603 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1605 printf("%d passes, %d fails\n", passes, fails);