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);
106 * Stubs to let everything else link sensibly.
108 void log_eventlog(void *handle, const char *event)
111 char *x_get_default(const char *key)
115 void sk_cleanup(void)
119 void showversion(void)
121 printf("puttygen: %s\n", ver);
124 void usage(int standalone)
127 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
128 " [ -C comment ] [ -P ] [ -q ]\n"
129 " [ -o output-keyfile ] [ -O type | -l | -L"
133 "Use \"puttygen --help\" for more detail.\n");
139 * Help message is an extended version of the usage message. So
140 * start with that, plus a version heading.
145 " -t specify key type when generating (rsa, dsa, rsa1)\n"
146 " -b specify number of bits when generating key\n"
147 " -C change or specify key comment\n"
148 " -P change key passphrase\n"
149 " -q quiet: do not display progress bar\n"
150 " -O specify output type:\n"
151 " private output PuTTY private key format\n"
152 " private-openssh export OpenSSH private key\n"
153 " private-sshcom export ssh.com private key\n"
154 " public standard / ssh.com public key\n"
155 " public-openssh OpenSSH public key\n"
156 " fingerprint output the key fingerprint\n"
157 " -o specify output file\n"
158 " -l equivalent to `-O fingerprint'\n"
159 " -L equivalent to `-O public-openssh'\n"
160 " -p equivalent to `-O public'\n"
164 static int save_ssh2_pubkey(char *filename, char *comment,
165 void *v_pub_blob, int pub_len)
167 unsigned char *pub_blob = (unsigned char *)v_pub_blob;
173 fp = fopen(filename, "wb");
179 fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
182 fprintf(fp, "Comment: \"");
183 for (p = comment; *p; p++) {
184 if (*p == '\\' || *p == '\"')
193 while (i < pub_len) {
195 int n = (pub_len - i < 3 ? pub_len - i : 3);
196 base64_encode_atom(pub_blob + i, n, buf);
200 if (++column >= 16) {
208 fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
214 static int move(char *from, char *to)
218 ret = rename(from, to);
221 * This OS may require us to remove the original file first.
224 ret = rename(from, to);
227 perror("puttygen: cannot move new file on to old one");
233 static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
236 unsigned char digest[16];
237 struct MD5Context md5c;
241 MD5Update(&md5c, blob, bloblen);
242 MD5Final(digest, &md5c);
244 sprintf(buffer, "%s ", alg);
246 sprintf(buffer + strlen(buffer), "%d ", bits);
247 for (i = 0; i < 16; i++)
248 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
251 return dupstr(buffer);
254 int main(int argc, char **argv)
257 Filename *infilename = NULL, *outfilename = NULL;
258 enum { NOKEYGEN, RSA1, RSA2, DSA } keytype = NOKEYGEN;
259 char *outfile = NULL, *outfiletmp = NULL;
260 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH, SSHCOM } outtype = PRIVATE;
262 char *comment = NULL, *origcomment = NULL;
263 int change_passphrase = FALSE;
264 int errs = FALSE, nogo = FALSE;
265 int intype = SSH_KEYTYPE_UNOPENABLE;
267 struct ssh2_userkey *ssh2key = NULL;
268 struct RSAKey *ssh1key = NULL;
269 unsigned char *ssh2blob = NULL;
270 char *ssh2alg = NULL;
271 const struct ssh_signkey *ssh2algf = NULL;
273 char *passphrase = NULL;
275 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
277 /* ------------------------------------------------------------------
278 * Parse the command line to figure out what we've been asked to do.
282 * If run with no arguments at all, print the usage message and
291 * Parse command line arguments.
308 opt = p++; /* opt will have _one_ leading - */
309 while (*p && *p != '=')
310 p++; /* find end of option */
317 if (!strcmp(opt, "-help")) {
320 fprintf(stderr, "puttygen: option `-%s'"
321 " expects no argument\n", opt);
326 } else if (!strcmp(opt, "-version")) {
329 fprintf(stderr, "puttygen: option `-%s'"
330 " expects no argument\n", opt);
335 } else if (!strcmp(opt, "-pgpfp")) {
338 fprintf(stderr, "puttygen: option `-%s'"
339 " expects no argument\n", opt);
341 /* support --pgpfp for consistency */
347 * For long options requiring an argument, add
348 * code along the lines of
350 * else if (!strcmp(opt, "-output")) {
353 * fprintf(stderr, "puttygen: option `-%s'"
354 * " expects an argument\n", opt);
362 "puttygen: no such option `-%s'\n", opt);
375 * Option requiring no parameter.
387 change_passphrase = TRUE;
399 progressfn = no_progress;
409 * Option requiring parameter.
415 fprintf(stderr, "puttygen: option `-%c' expects a"
420 * Now c is the option and p is the parameter.
424 if (!strcmp(p, "rsa") || !strcmp(p, "rsa2"))
425 keytype = RSA2, sshver = 2;
426 else if (!strcmp(p, "rsa1"))
427 keytype = RSA1, sshver = 1;
428 else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
429 keytype = DSA, sshver = 2;
432 "puttygen: unknown key type `%s'\n", p);
443 if (!strcmp(p, "public"))
445 else if (!strcmp(p, "public-openssh"))
447 else if (!strcmp(p, "private"))
449 else if (!strcmp(p, "fingerprint"))
451 else if (!strcmp(p, "private-openssh"))
452 outtype = OPENSSH, 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"
497 * If run with at least one argument _but_ not the required
498 * ones, print the usage message and return failure.
500 if (!infile && keytype == NOKEYGEN) {
505 /* ------------------------------------------------------------------
506 * Figure out further details of exactly what we're going to do.
510 * Bomb out if we've been asked to both load and generate a
513 if (keytype != NOKEYGEN && infile) {
514 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
519 * We must save the private part when generating a new key.
521 if (keytype != NOKEYGEN &&
522 (outtype != PRIVATE && outtype != OPENSSH && outtype != SSHCOM)) {
523 fprintf(stderr, "puttygen: this would generate a new key but "
524 "discard the private part\n");
529 * Analyse the type of the input file, in case this affects our
533 infilename = filename_from_str(infile);
535 intype = key_type(infilename);
539 * It would be nice here to be able to load _public_
540 * key files, in any of a number of forms, and (a)
541 * convert them to other public key types, (b) print
542 * out their fingerprints. Or, I suppose, for real
543 * orthogonality, (c) change their comment!
545 * In fact this opens some interesting possibilities.
546 * Suppose ssh2_userkey_loadpub() were able to load
547 * public key files as well as extracting the public
548 * key from private ones. And suppose I did the thing
549 * I've been wanting to do, where specifying a
550 * particular private key file for authentication
551 * causes any _other_ key in the agent to be discarded.
552 * Then, if you had an agent forwarded to the machine
553 * you were running Unix PuTTY or Plink on, and you
554 * needed to specify which of the keys in the agent it
555 * should use, you could do that by supplying a
556 * _public_ key file, thus not needing to trust even
557 * your encrypted private key file to the network. Ooh!
560 case SSH_KEYTYPE_UNOPENABLE:
561 case SSH_KEYTYPE_UNKNOWN:
562 fprintf(stderr, "puttygen: unable to load file `%s': %s\n",
563 infile, key_type_to_str(intype));
566 case SSH_KEYTYPE_SSH1:
568 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
575 case SSH_KEYTYPE_SSH2:
576 case SSH_KEYTYPE_OPENSSH:
577 case SSH_KEYTYPE_SSHCOM:
579 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
589 * Determine the default output file, if none is provided.
591 * This will usually be equal to stdout, except that if the
592 * input and output file formats are the same then the default
593 * output is to overwrite the input.
595 * Also in this code, we bomb out if the input and output file
596 * formats are the same and no other action is performed.
598 if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
599 (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
600 (intype == SSH_KEYTYPE_OPENSSH && outtype == OPENSSH) ||
601 (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
604 outfiletmp = dupcat(outfile, ".tmp", NULL);
607 if (!change_passphrase && !comment) {
608 fprintf(stderr, "puttygen: this command would perform no useful"
615 * Bomb out rather than automatically choosing to write
616 * a private key file to stdout.
618 if (outtype==PRIVATE || outtype==OPENSSH || outtype==SSHCOM) {
619 fprintf(stderr, "puttygen: need to specify an output file\n");
626 * Figure out whether we need to load the encrypted part of the
627 * key. This will be the case if either (a) we need to write
628 * out a private key format, or (b) the entire input key file
631 if (outtype == PRIVATE || outtype == OPENSSH || outtype == SSHCOM ||
632 intype == SSH_KEYTYPE_OPENSSH || intype == SSH_KEYTYPE_SSHCOM)
633 load_encrypted = TRUE;
635 load_encrypted = FALSE;
637 /* ------------------------------------------------------------------
638 * Now we're ready to actually do some stuff.
642 * Either load or generate a key.
644 if (keytype != NOKEYGEN) {
646 char default_comment[80];
648 struct progress prog;
655 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
657 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
660 entropy = get_random_data(bits / 8);
662 fprintf(stderr, "puttygen: failed to collect entropy, "
663 "could not generate key\n");
666 random_add_heavynoise(entropy, bits / 8);
667 smemclr(entropy, bits/8);
670 if (keytype == DSA) {
671 struct dss_key *dsskey = snew(struct dss_key);
672 dsa_generate(dsskey, bits, progressfn, &prog);
673 ssh2key = snew(struct ssh2_userkey);
674 ssh2key->data = dsskey;
675 ssh2key->alg = &ssh_dss;
678 struct RSAKey *rsakey = snew(struct RSAKey);
679 rsa_generate(rsakey, bits, progressfn, &prog);
680 rsakey->comment = NULL;
681 if (keytype == RSA1) {
684 ssh2key = snew(struct ssh2_userkey);
685 ssh2key->data = rsakey;
686 ssh2key->alg = &ssh_rsa;
689 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
692 ssh2key->comment = dupstr(default_comment);
694 ssh1key->comment = dupstr(default_comment);
697 const char *error = NULL;
700 assert(infile != NULL);
703 * Find out whether the input key is encrypted.
705 if (intype == SSH_KEYTYPE_SSH1)
706 encrypted = rsakey_encrypted(infilename, &origcomment);
707 else if (intype == SSH_KEYTYPE_SSH2)
708 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
710 encrypted = import_encrypted(infilename, intype, &origcomment);
713 * If so, ask for a passphrase.
715 if (encrypted && load_encrypted) {
716 prompts_t *p = new_prompts(NULL);
718 p->to_server = FALSE;
719 p->name = dupstr("SSH key passphrase");
720 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
721 ret = console_get_userpass_input(p, NULL, 0);
725 perror("puttygen: unable to read passphrase");
728 passphrase = dupstr(p->prompts[0]->result);
738 case SSH_KEYTYPE_SSH1:
739 ssh1key = snew(struct RSAKey);
740 if (!load_encrypted) {
745 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
746 &origcomment, &error);
747 blob = (unsigned char *)vblob;
749 n = 4; /* skip modulus bits */
751 l = ssh1_read_bignum(blob + n, bloblen - n,
754 error = "SSH-1 public key blob was too short";
757 l = ssh1_read_bignum(blob + n, bloblen - n,
760 error = "SSH-1 public key blob was too short";
764 ssh1key->comment = dupstr(origcomment);
765 ssh1key->private_exponent = NULL;
768 ssh1key->iqmp = NULL;
770 ret = loadrsakey(infilename, ssh1key, passphrase, &error);
775 error = "unknown error";
778 case SSH_KEYTYPE_SSH2:
779 if (!load_encrypted) {
780 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
781 &ssh2bloblen, NULL, &error);
783 ssh2algf = find_pubkey_alg(ssh2alg);
785 bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
790 ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
792 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
795 if (ssh2key == SSH2_WRONG_PASSPHRASE)
796 error = "wrong passphrase";
798 error = "unknown error";
802 case SSH_KEYTYPE_OPENSSH:
803 case SSH_KEYTYPE_SSHCOM:
804 ssh2key = import_ssh2(infilename, intype, passphrase, &error);
806 if (ssh2key != SSH2_WRONG_PASSPHRASE)
809 error = "wrong passphrase";
811 error = "unknown error";
819 fprintf(stderr, "puttygen: error loading `%s': %s\n",
826 * Change the comment if asked to.
831 sfree(ssh1key->comment);
832 ssh1key->comment = dupstr(comment);
835 sfree(ssh2key->comment);
836 ssh2key->comment = dupstr(comment);
841 * Prompt for a new passphrase if we have been asked to, or if
842 * we have just generated a key.
844 if (change_passphrase || keytype != NOKEYGEN) {
845 prompts_t *p = new_prompts(NULL);
848 p->to_server = FALSE;
849 p->name = dupstr("New SSH key passphrase");
850 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
851 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
852 ret = console_get_userpass_input(p, NULL, 0);
856 perror("puttygen: unable to read new passphrase");
859 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
861 fprintf(stderr, "puttygen: passphrases do not match\n");
865 smemclr(passphrase, strlen(passphrase));
868 passphrase = dupstr(p->prompts[0]->result);
880 * (In the case where outfile and outfiletmp are both NULL,
881 * there is no semantic reason to initialise outfilename at
882 * all; but we have to write _something_ to it or some compiler
883 * will probably complain that it might be used uninitialised.)
886 outfilename = filename_from_str(outfiletmp);
888 outfilename = filename_from_str(outfile ? outfile : "");
896 ret = saversakey(outfilename, ssh1key, passphrase);
898 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
903 ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
905 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
910 if (!move(outfiletmp, outfile))
911 return 1; /* rename failed */
924 fp = f_open(outfilename, "w", FALSE);
927 dec1 = bignum_decimal(ssh1key->exponent);
928 dec2 = bignum_decimal(ssh1key->modulus);
929 fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus),
930 dec1, dec2, ssh1key->comment);
935 } else if (outtype == PUBLIC) {
938 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
941 save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,
942 ssh2blob, ssh2bloblen);
943 } else if (outtype == PUBLICO) {
950 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
955 ssh2alg = ssh2key->alg->name;
958 comment = ssh2key->comment;
960 comment = origcomment;
962 buffer = snewn(strlen(ssh2alg) +
963 4 * ((ssh2bloblen+2) / 3) +
964 strlen(comment) + 3, char);
965 strcpy(buffer, ssh2alg);
966 p = buffer + strlen(buffer);
969 while (i < ssh2bloblen) {
970 int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);
971 base64_encode_atom(ssh2blob + i, n, p);
982 fp = f_open(outfilename, "w", FALSE);
985 fprintf(fp, "%s\n", buffer);
1000 fingerprint = snewn(128, char);
1001 rsa_fingerprint(fingerprint, 128, ssh1key);
1004 fingerprint = ssh2key->alg->fingerprint(ssh2key->data);
1007 fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);
1012 fp = f_open(outfilename, "w", FALSE);
1015 fprintf(fp, "%s\n", fingerprint);
1025 assert(sshver == 2);
1027 ret = export_ssh2(outfilename, outtype, ssh2key, passphrase);
1029 fprintf(stderr, "puttygen: unable to export key\n");
1033 if (!move(outfiletmp, outfile))
1034 return 1; /* rename failed */
1040 smemclr(passphrase, strlen(passphrase));
1045 freersakey(ssh1key);
1047 ssh2key->alg->freekey(ssh2key->data);
1062 void setup_passphrases(char *first, ...)
1069 prompts[nprompts++] = first;
1070 va_start(ap, first);
1071 while ((next = va_arg(ap, char *)) != NULL) {
1072 assert(nprompts < lenof(prompts));
1073 prompts[nprompts++] = next;
1079 void test(int retval, ...)
1086 va_start(ap, retval);
1087 while (va_arg(ap, char *) != NULL)
1091 argv = snewn(argc+1, char *);
1092 va_start(ap, retval);
1093 for (i = 0; i <= argc; i++)
1094 argv[i] = va_arg(ap, char *);
1098 ret = cmdgen_main(argc, argv);
1100 if (ret != retval) {
1101 printf("FAILED retval (exp %d got %d):", retval, ret);
1102 for (i = 0; i < argc; i++)
1103 printf(" %s", argv[i]);
1106 } else if (promptsgot != nprompts) {
1107 printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot);
1108 for (i = 0; i < argc; i++)
1109 printf(" %s", argv[i]);
1117 void filecmp(char *file1, char *file2, char *fmt, ...)
1120 * Ideally I should do file comparison myself, to maximise the
1121 * portability of this test suite once this application begins
1122 * running on non-Unix platforms. For the moment, though,
1123 * calling Unix diff is perfectly adequate.
1128 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1135 printf("FAILED diff (ret=%d): ", ret);
1148 char *cleanup_fp(char *s)
1152 if (!strncmp(s, "ssh-", 4)) {
1153 s += strcspn(s, " \n\t");
1154 s += strspn(s, " \n\t");
1158 s += strcspn(s, " \n\t");
1159 s += strspn(s, " \n\t");
1160 s += strcspn(s, " \n\t");
1162 return dupprintf("%.*s", s - p, p);
1165 char *get_fp(char *filename)
1168 char buf[256], *ret;
1170 fp = fopen(filename, "r");
1173 ret = fgets(buf, sizeof(buf), fp);
1177 return cleanup_fp(buf);
1180 void check_fp(char *filename, char *fp, char *fmt, ...)
1187 newfp = get_fp(filename);
1189 if (!strcmp(fp, newfp)) {
1194 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1208 int main(int argc, char **argv)
1211 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1214 * Even when this thing is compiled for automatic test mode,
1215 * it's helpful to be able to invoke it with command-line
1216 * options for _manual_ tests.
1219 return cmdgen_main(argc, argv);
1223 for (i = 0; i < lenof(keytypes); i++) {
1224 char filename[128], osfilename[128], scfilename[128];
1225 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1228 sprintf(filename, "test-%s.ppk", keytypes[i]);
1229 sprintf(pubfilename, "test-%s.pub", keytypes[i]);
1230 sprintf(osfilename, "test-%s.os", keytypes[i]);
1231 sprintf(scfilename, "test-%s.sc", keytypes[i]);
1232 sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]);
1233 sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]);
1236 * Create an encrypted key.
1238 setup_passphrases("sponge", "sponge", NULL);
1239 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1242 * List the public key in OpenSSH format.
1244 setup_passphrases(NULL);
1245 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1249 sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'",
1250 pubfilename, tmpfilename1);
1251 if (system(cmdbuf) ||
1252 (fp = get_fp(tmpfilename1)) == NULL) {
1253 printf("UNABLE to test fingerprint matching against OpenSSH");
1258 * List the public key in IETF/ssh.com format.
1260 setup_passphrases(NULL);
1261 test(0, "puttygen", "-p", filename, NULL);
1264 * List the fingerprint of the key.
1266 setup_passphrases(NULL);
1267 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1270 * If we can't test fingerprints against OpenSSH, we
1271 * can at the very least test equality of all the
1272 * fingerprints we generate of this key throughout
1275 fp = get_fp(tmpfilename1);
1277 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1281 * Change the comment of the key; this _does_ require a
1282 * passphrase owing to the tamperproofing.
1284 * NOTE: In SSH-1, this only requires a passphrase because
1285 * of inadequacies of the loading and saving mechanisms. In
1286 * _principle_, it should be perfectly possible to modify
1287 * the comment on an SSH-1 key without requiring a
1288 * passphrase; the only reason I can't do it is because my
1289 * loading and saving mechanisms don't include a method of
1290 * loading all the key data without also trying to decrypt
1291 * the private section.
1293 * I don't consider this to be a problem worth solving,
1294 * because (a) to fix it would probably end up bloating
1295 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
1296 * it shouldn't be highly significant. If it seriously
1297 * bothers anyone then perhaps I _might_ be persuadable.
1299 setup_passphrases("sponge", NULL);
1300 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1303 * Change the passphrase to nothing.
1305 setup_passphrases("sponge", "", "", NULL);
1306 test(0, "puttygen", "-P", filename, NULL);
1309 * Change the comment of the key again; this time we expect no
1310 * passphrase to be required.
1312 setup_passphrases(NULL);
1313 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1316 * Export the private key into OpenSSH format; no passphrase
1317 * should be required since the key is currently unencrypted.
1318 * For RSA1 keys, this should give an error.
1320 setup_passphrases(NULL);
1321 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1326 * List the fingerprint of the OpenSSH-formatted key.
1328 setup_passphrases(NULL);
1329 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1330 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1333 * List the public half of the OpenSSH-formatted key in
1336 setup_passphrases(NULL);
1337 test(0, "puttygen", "-L", osfilename, NULL);
1340 * List the public half of the OpenSSH-formatted key in
1341 * IETF/ssh.com format.
1343 setup_passphrases(NULL);
1344 test(0, "puttygen", "-p", osfilename, NULL);
1348 * Export the private key into ssh.com format; no passphrase
1349 * should be required since the key is currently unencrypted.
1350 * For RSA1 keys, this should give an error.
1352 setup_passphrases(NULL);
1353 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1358 * List the fingerprint of the ssh.com-formatted key.
1360 setup_passphrases(NULL);
1361 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1362 check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]);
1365 * List the public half of the ssh.com-formatted key in
1368 setup_passphrases(NULL);
1369 test(0, "puttygen", "-L", scfilename, NULL);
1372 * List the public half of the ssh.com-formatted key in
1373 * IETF/ssh.com format.
1375 setup_passphrases(NULL);
1376 test(0, "puttygen", "-p", scfilename, NULL);
1381 * Convert from OpenSSH into ssh.com.
1383 setup_passphrases(NULL);
1384 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1385 "-O", "private-sshcom", NULL);
1388 * Convert from ssh.com back into a PuTTY key,
1389 * supplying the same comment as we had before we
1390 * started to ensure the comparison works.
1392 setup_passphrases(NULL);
1393 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1394 "-o", tmpfilename2, NULL);
1397 * See if the PuTTY key thus generated is the same as
1400 filecmp(filename, tmpfilename2,
1401 "p->o->s->p clear %s", keytypes[i]);
1404 * Convert from ssh.com to OpenSSH.
1406 setup_passphrases(NULL);
1407 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1408 "-O", "private-openssh", NULL);
1411 * Convert from OpenSSH back into a PuTTY key,
1412 * supplying the same comment as we had before we
1413 * started to ensure the comparison works.
1415 setup_passphrases(NULL);
1416 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1417 "-o", tmpfilename2, NULL);
1420 * See if the PuTTY key thus generated is the same as
1423 filecmp(filename, tmpfilename2,
1424 "p->s->o->p clear %s", keytypes[i]);
1427 * Finally, do a round-trip conversion between PuTTY
1428 * and ssh.com without involving OpenSSH, to test that
1429 * the key comment is preserved in that case.
1431 setup_passphrases(NULL);
1432 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1434 setup_passphrases(NULL);
1435 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1436 filecmp(filename, tmpfilename2,
1437 "p->s->p clear %s", keytypes[i]);
1441 * Check that mismatched passphrases cause an error.
1443 setup_passphrases("sponge2", "sponge3", NULL);
1444 test(1, "puttygen", "-P", filename, NULL);
1447 * Put a passphrase back on.
1449 setup_passphrases("sponge2", "sponge2", NULL);
1450 test(0, "puttygen", "-P", filename, NULL);
1453 * Export the private key into OpenSSH format, this time
1454 * while encrypted. For RSA1 keys, this should give an
1458 setup_passphrases(NULL); /* error, hence no passphrase read */
1460 setup_passphrases("sponge2", NULL);
1461 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1466 * List the fingerprint of the OpenSSH-formatted key.
1468 setup_passphrases("sponge2", NULL);
1469 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1470 check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]);
1473 * List the public half of the OpenSSH-formatted key in
1476 setup_passphrases("sponge2", NULL);
1477 test(0, "puttygen", "-L", osfilename, NULL);
1480 * List the public half of the OpenSSH-formatted key in
1481 * IETF/ssh.com format.
1483 setup_passphrases("sponge2", NULL);
1484 test(0, "puttygen", "-p", osfilename, NULL);
1488 * Export the private key into ssh.com format, this time
1489 * while encrypted. For RSA1 keys, this should give an
1493 setup_passphrases(NULL); /* error, hence no passphrase read */
1495 setup_passphrases("sponge2", NULL);
1496 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1501 * List the fingerprint of the ssh.com-formatted key.
1503 setup_passphrases("sponge2", NULL);
1504 test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL);
1505 check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]);
1508 * List the public half of the ssh.com-formatted key in
1511 setup_passphrases("sponge2", NULL);
1512 test(0, "puttygen", "-L", scfilename, NULL);
1515 * List the public half of the ssh.com-formatted key in
1516 * IETF/ssh.com format.
1518 setup_passphrases("sponge2", NULL);
1519 test(0, "puttygen", "-p", scfilename, NULL);
1524 * Convert from OpenSSH into ssh.com.
1526 setup_passphrases("sponge2", NULL);
1527 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1528 "-O", "private-sshcom", NULL);
1531 * Convert from ssh.com back into a PuTTY key,
1532 * supplying the same comment as we had before we
1533 * started to ensure the comparison works.
1535 setup_passphrases("sponge2", NULL);
1536 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1537 "-o", tmpfilename2, NULL);
1540 * See if the PuTTY key thus generated is the same as
1543 filecmp(filename, tmpfilename2,
1544 "p->o->s->p encrypted %s", keytypes[i]);
1547 * Convert from ssh.com to OpenSSH.
1549 setup_passphrases("sponge2", NULL);
1550 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1551 "-O", "private-openssh", NULL);
1554 * Convert from OpenSSH back into a PuTTY key,
1555 * supplying the same comment as we had before we
1556 * started to ensure the comparison works.
1558 setup_passphrases("sponge2", NULL);
1559 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1560 "-o", tmpfilename2, NULL);
1563 * See if the PuTTY key thus generated is the same as
1566 filecmp(filename, tmpfilename2,
1567 "p->s->o->p encrypted %s", keytypes[i]);
1570 * Finally, do a round-trip conversion between PuTTY
1571 * and ssh.com without involving OpenSSH, to test that
1572 * the key comment is preserved in that case.
1574 setup_passphrases("sponge2", NULL);
1575 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1577 setup_passphrases("sponge2", NULL);
1578 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1579 filecmp(filename, tmpfilename2,
1580 "p->s->p encrypted %s", keytypes[i]);
1584 * Load with the wrong passphrase.
1586 setup_passphrases("sponge8", NULL);
1587 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1590 * Load a totally bogus file.
1592 setup_passphrases(NULL);
1593 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1595 printf("%d passes, %d fails\n", passes, fails);